rlevent.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. event_hook ()
  49. {
  50. fprintf (stderr, "ding!\n");
  51. sleep (1);
  52. return 0;
  53. }
  54. static int
  55. set_deftext ()
  56. {
  57. if (deftext)
  58. {
  59. rl_insert_text (deftext);
  60. deftext = (char *)NULL;
  61. rl_startup_hook = (rl_hook_func_t *)NULL;
  62. }
  63. return 0;
  64. }
  65. static void
  66. usage()
  67. {
  68. fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
  69. progname, progname);
  70. }
  71. int
  72. main (argc, argv)
  73. int argc;
  74. char **argv;
  75. {
  76. char *temp, *prompt;
  77. struct stat sb;
  78. int opt, fd, nch;
  79. FILE *ifp;
  80. progname = strrchr(argv[0], '/');
  81. if (progname == 0)
  82. progname = argv[0];
  83. else
  84. progname++;
  85. /* defaults */
  86. prompt = "readline$ ";
  87. fd = nch = 0;
  88. deftext = (char *)0;
  89. while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
  90. {
  91. switch (opt)
  92. {
  93. case 'p':
  94. prompt = optarg;
  95. break;
  96. case 'u':
  97. fd = atoi(optarg);
  98. if (fd < 0)
  99. {
  100. fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
  101. exit (2);
  102. }
  103. break;
  104. case 'd':
  105. deftext = optarg;
  106. break;
  107. case 'n':
  108. nch = atoi(optarg);
  109. if (nch < 0)
  110. {
  111. fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
  112. exit (2);
  113. }
  114. break;
  115. default:
  116. usage ();
  117. exit (2);
  118. }
  119. }
  120. if (fd != 0)
  121. {
  122. if (fstat (fd, &sb) < 0)
  123. {
  124. fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
  125. exit (1);
  126. }
  127. ifp = fdopen (fd, "r");
  128. rl_instream = ifp;
  129. }
  130. if (deftext && *deftext)
  131. rl_startup_hook = set_deftext;
  132. if (nch > 0)
  133. rl_num_chars_to_read = nch;
  134. rl_event_hook = event_hook;
  135. temp = readline (prompt);
  136. /* Test for EOF. */
  137. if (temp == 0)
  138. exit (1);
  139. printf ("%s\n", temp);
  140. exit (0);
  141. }