rl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * rl - command-line interface to read a line from the standard input
  3. * (or another fd) using readline.
  4. *
  5. * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
  6. */
  7. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  8. This file is part of the GNU Readline Library (Readline), a library for
  9. reading lines of text with interactive input and history editing.
  10. Readline is free software: you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 3 of the License, or
  13. (at your option) any later version.
  14. Readline is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #if defined (HAVE_CONFIG_H)
  22. # include <config.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #ifdef HAVE_STDLIB_H
  27. # include <stdlib.h>
  28. #else
  29. extern void exit();
  30. #endif
  31. #if defined (READLINE_LIBRARY)
  32. # include "posixstat.h"
  33. # include "readline.h"
  34. # include "history.h"
  35. #else
  36. # include <sys/stat.h>
  37. # include <readline/readline.h>
  38. # include <readline/history.h>
  39. #endif
  40. extern int optind;
  41. extern char *optarg;
  42. #if !defined (strchr) && !defined (__STDC__)
  43. extern char *strrchr();
  44. #endif
  45. static char *progname;
  46. static char *deftext;
  47. static int
  48. set_deftext ()
  49. {
  50. if (deftext)
  51. {
  52. rl_insert_text (deftext);
  53. deftext = (char *)NULL;
  54. rl_startup_hook = (rl_hook_func_t *)NULL;
  55. }
  56. return 0;
  57. }
  58. static void
  59. usage()
  60. {
  61. fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
  62. progname, progname);
  63. }
  64. int
  65. main (argc, argv)
  66. int argc;
  67. char **argv;
  68. {
  69. char *temp, *prompt;
  70. struct stat sb;
  71. int opt, fd, nch;
  72. FILE *ifp;
  73. progname = strrchr(argv[0], '/');
  74. if (progname == 0)
  75. progname = argv[0];
  76. else
  77. progname++;
  78. /* defaults */
  79. prompt = "readline$ ";
  80. fd = nch = 0;
  81. deftext = (char *)0;
  82. while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
  83. {
  84. switch (opt)
  85. {
  86. case 'p':
  87. prompt = optarg;
  88. break;
  89. case 'u':
  90. fd = atoi(optarg);
  91. if (fd < 0)
  92. {
  93. fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
  94. exit (2);
  95. }
  96. break;
  97. case 'd':
  98. deftext = optarg;
  99. break;
  100. case 'n':
  101. nch = atoi(optarg);
  102. if (nch < 0)
  103. {
  104. fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
  105. exit (2);
  106. }
  107. break;
  108. default:
  109. usage ();
  110. exit (2);
  111. }
  112. }
  113. if (fd != 0)
  114. {
  115. if (fstat (fd, &sb) < 0)
  116. {
  117. fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
  118. exit (1);
  119. }
  120. ifp = fdopen (fd, "r");
  121. rl_instream = ifp;
  122. }
  123. if (deftext && *deftext)
  124. rl_startup_hook = set_deftext;
  125. if (nch > 0)
  126. rl_num_chars_to_read = nch;
  127. temp = readline (prompt);
  128. /* Test for EOF. */
  129. if (temp == 0)
  130. exit (1);
  131. printf ("%s\n", temp);
  132. exit (0);
  133. }