callback.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* callback.c -- functions to use readline as an X `callback' mechanism. */
  2. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include "rlconf.h"
  21. #if defined (READLINE_CALLBACKS)
  22. #include <sys/types.h>
  23. #ifdef HAVE_STDLIB_H
  24. # include <stdlib.h>
  25. #else
  26. # include "ansi_stdlib.h"
  27. #endif
  28. #include <stdio.h>
  29. /* System-specific feature definitions and include files. */
  30. #include "rldefs.h"
  31. #include "readline.h"
  32. #include "rlprivate.h"
  33. #include "xmalloc.h"
  34. /* Private data for callback registration functions. See comments in
  35. rl_callback_read_char for more details. */
  36. _rl_callback_func_t *_rl_callback_func = 0;
  37. _rl_callback_generic_arg *_rl_callback_data = 0;
  38. /* **************************************************************** */
  39. /* */
  40. /* Callback Readline Functions */
  41. /* */
  42. /* **************************************************************** */
  43. /* Allow using readline in situations where a program may have multiple
  44. things to handle at once, and dispatches them via select(). Call
  45. rl_callback_handler_install() with the prompt and a function to call
  46. whenever a complete line of input is ready. The user must then
  47. call rl_callback_read_char() every time some input is available, and
  48. rl_callback_read_char() will call the user's function with the complete
  49. text read in at each end of line. The terminal is kept prepped and
  50. signals handled all the time, except during calls to the user's function. */
  51. rl_vcpfunc_t *rl_linefunc; /* user callback function */
  52. static int in_handler; /* terminal_prepped and signals set? */
  53. /* Make sure the terminal is set up, initialize readline, and prompt. */
  54. static void
  55. _rl_callback_newline ()
  56. {
  57. rl_initialize ();
  58. if (in_handler == 0)
  59. {
  60. in_handler = 1;
  61. if (rl_prep_term_function)
  62. (*rl_prep_term_function) (_rl_meta_flag);
  63. #if defined (HANDLE_SIGNALS)
  64. rl_set_signals ();
  65. #endif
  66. }
  67. readline_internal_setup ();
  68. RL_CHECK_SIGNALS ();
  69. }
  70. /* Install a readline handler, set up the terminal, and issue the prompt. */
  71. void
  72. rl_callback_handler_install (prompt, linefunc)
  73. const char *prompt;
  74. rl_vcpfunc_t *linefunc;
  75. {
  76. rl_set_prompt (prompt);
  77. RL_SETSTATE (RL_STATE_CALLBACK);
  78. rl_linefunc = linefunc;
  79. _rl_callback_newline ();
  80. }
  81. /* Read one character, and dispatch to the handler if it ends the line. */
  82. void
  83. rl_callback_read_char ()
  84. {
  85. char *line;
  86. int eof, jcode;
  87. static procenv_t olevel;
  88. if (rl_linefunc == NULL)
  89. {
  90. _rl_errmsg ("readline_callback_read_char() called with no handler!");
  91. abort ();
  92. }
  93. memcpy ((void *)olevel, (void *)_rl_top_level, sizeof (procenv_t));
  94. jcode = setjmp (_rl_top_level);
  95. if (jcode)
  96. {
  97. (*rl_redisplay_function) ();
  98. _rl_want_redisplay = 0;
  99. memcpy ((void *)_rl_top_level, (void *)olevel, sizeof (procenv_t));
  100. return;
  101. }
  102. do
  103. {
  104. RL_CHECK_SIGNALS ();
  105. if (RL_ISSTATE (RL_STATE_ISEARCH))
  106. {
  107. eof = _rl_isearch_callback (_rl_iscxt);
  108. if (eof == 0 && (RL_ISSTATE (RL_STATE_ISEARCH) == 0) && RL_ISSTATE (RL_STATE_INPUTPENDING))
  109. rl_callback_read_char ();
  110. return;
  111. }
  112. else if (RL_ISSTATE (RL_STATE_NSEARCH))
  113. {
  114. eof = _rl_nsearch_callback (_rl_nscxt);
  115. return;
  116. }
  117. #if defined (VI_MODE)
  118. else if (RL_ISSTATE (RL_STATE_VIMOTION))
  119. {
  120. eof = _rl_vi_domove_callback (_rl_vimvcxt);
  121. /* Should handle everything, including cleanup, numeric arguments,
  122. and turning off RL_STATE_VIMOTION */
  123. if (RL_ISSTATE (RL_STATE_NUMERICARG) == 0)
  124. _rl_internal_char_cleanup ();
  125. return;
  126. }
  127. #endif
  128. else if (RL_ISSTATE (RL_STATE_NUMERICARG))
  129. {
  130. eof = _rl_arg_callback (_rl_argcxt);
  131. if (eof == 0 && (RL_ISSTATE (RL_STATE_NUMERICARG) == 0) && RL_ISSTATE (RL_STATE_INPUTPENDING))
  132. rl_callback_read_char ();
  133. /* XXX - this should handle _rl_last_command_was_kill better */
  134. else if (RL_ISSTATE (RL_STATE_NUMERICARG) == 0)
  135. _rl_internal_char_cleanup ();
  136. return;
  137. }
  138. else if (RL_ISSTATE (RL_STATE_MULTIKEY))
  139. {
  140. eof = _rl_dispatch_callback (_rl_kscxt); /* For now */
  141. while ((eof == -1 || eof == -2) && RL_ISSTATE (RL_STATE_MULTIKEY) && _rl_kscxt && (_rl_kscxt->flags & KSEQ_DISPATCHED))
  142. eof = _rl_dispatch_callback (_rl_kscxt);
  143. if (RL_ISSTATE (RL_STATE_MULTIKEY) == 0)
  144. {
  145. _rl_internal_char_cleanup ();
  146. _rl_want_redisplay = 1;
  147. }
  148. }
  149. else if (_rl_callback_func)
  150. {
  151. /* This allows functions that simply need to read an additional
  152. character (like quoted-insert) to register a function to be
  153. called when input is available. _rl_callback_data is simply a
  154. pointer to a struct that has the argument count originally
  155. passed to the registering function and space for any additional
  156. parameters. */
  157. eof = (*_rl_callback_func) (_rl_callback_data);
  158. /* If the function `deregisters' itself, make sure the data is
  159. cleaned up. */
  160. if (_rl_callback_func == 0)
  161. {
  162. if (_rl_callback_data)
  163. {
  164. _rl_callback_data_dispose (_rl_callback_data);
  165. _rl_callback_data = 0;
  166. }
  167. _rl_internal_char_cleanup ();
  168. }
  169. }
  170. else
  171. eof = readline_internal_char ();
  172. RL_CHECK_SIGNALS ();
  173. if (rl_done == 0 && _rl_want_redisplay)
  174. {
  175. (*rl_redisplay_function) ();
  176. _rl_want_redisplay = 0;
  177. }
  178. if (rl_done)
  179. {
  180. line = readline_internal_teardown (eof);
  181. if (rl_deprep_term_function)
  182. (*rl_deprep_term_function) ();
  183. #if defined (HANDLE_SIGNALS)
  184. rl_clear_signals ();
  185. #endif
  186. in_handler = 0;
  187. (*rl_linefunc) (line);
  188. /* If the user did not clear out the line, do it for him. */
  189. if (rl_line_buffer[0])
  190. _rl_init_line_state ();
  191. /* Redisplay the prompt if readline_handler_{install,remove}
  192. not called. */
  193. if (in_handler == 0 && rl_linefunc)
  194. _rl_callback_newline ();
  195. }
  196. }
  197. while (rl_pending_input || _rl_pushed_input_available () || RL_ISSTATE (RL_STATE_MACROINPUT));
  198. }
  199. /* Remove the handler, and make sure the terminal is in its normal state. */
  200. void
  201. rl_callback_handler_remove ()
  202. {
  203. rl_linefunc = NULL;
  204. RL_UNSETSTATE (RL_STATE_CALLBACK);
  205. RL_CHECK_SIGNALS ();
  206. if (in_handler)
  207. {
  208. in_handler = 0;
  209. if (rl_deprep_term_function)
  210. (*rl_deprep_term_function) ();
  211. #if defined (HANDLE_SIGNALS)
  212. rl_clear_signals ();
  213. #endif
  214. }
  215. }
  216. _rl_callback_generic_arg *
  217. _rl_callback_data_alloc (count)
  218. int count;
  219. {
  220. _rl_callback_generic_arg *arg;
  221. arg = (_rl_callback_generic_arg *)xmalloc (sizeof (_rl_callback_generic_arg));
  222. arg->count = count;
  223. arg->i1 = arg->i2 = 0;
  224. return arg;
  225. }
  226. void _rl_callback_data_dispose (arg)
  227. _rl_callback_generic_arg *arg;
  228. {
  229. xfree (arg);
  230. }
  231. #endif