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