readline.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* readline.c --- Simple implementation of readline.
  2. Copyright (C) 2005-2007, 2009-2020 Free Software Foundation, Inc.
  3. Written by Simon Josefsson
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* This module is intended to be used when the application only needs
  16. the readline interface. If you need more functions from the
  17. readline library, it is recommended to require the readline library
  18. (or improve this module) rather than #if-protect part of your
  19. application (doing so would add assumptions of this module into
  20. your application). The application should use #include
  21. "readline.h", that header file will include <readline/readline.h>
  22. if the real library is present on the system. */
  23. /* Get specification. */
  24. #include "readline.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. char *
  28. readline (const char *prompt)
  29. {
  30. char *out = NULL;
  31. size_t size = 0;
  32. if (prompt)
  33. {
  34. fputs (prompt, stdout);
  35. fflush (stdout);
  36. }
  37. if (getline (&out, &size, stdin) < 0)
  38. return NULL;
  39. while (*out && (out[strlen (out) - 1] == '\r'
  40. || out[strlen (out) - 1] == '\n'))
  41. out[strlen (out) - 1] = '\0';
  42. return out;
  43. }