1 /* callback.c -- functions to use readline as an X `callback' mechanism. */
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
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.
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
11 (at your option) any later version.
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
16 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/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
30 #if defined (READLINE_CALLBACKS)
32 #include <sys/types.h>
37 # include "ansi_stdlib.h"
42 /* System-specific feature definitions and include files. */
45 #include "rlprivate.h"
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;
53 /* **************************************************************** */
55 /* Callback Readline Functions */
57 /* **************************************************************** */
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
66 all the time, except during calls to the user's function. Signal
67 handlers are only installed when the application calls back into
68 readline, so readline doesn't `steal' signals from the application. */
70 rl_vcpfunc_t
*rl_linefunc
; /* user callback function */
71 static int in_handler
; /* terminal_prepped and signals set? */
73 /* Make sure the terminal is set up, initialize readline, and prompt. */
75 _rl_callback_newline ()
83 if (rl_prep_term_function
)
84 (*rl_prep_term_function
) (_rl_meta_flag
);
87 readline_internal_setup ();
91 /* Install a readline handler, set up the terminal, and issue the prompt. */
93 rl_callback_handler_install (prompt
, linefunc
)
95 rl_vcpfunc_t
*linefunc
;
97 rl_set_prompt (prompt
);
98 RL_SETSTATE (RL_STATE_CALLBACK
);
99 rl_linefunc
= linefunc
;
100 _rl_callback_newline ();
103 #if defined (HANDLE_SIGNALS)
104 #define CALLBACK_READ_RETURN() \
106 rl_clear_signals (); \
110 #define CALLBACK_READ_RETURN() return
113 /* Read one character, and dispatch to the handler if it ends the line. */
115 rl_callback_read_char ()
119 static procenv_t olevel
;
121 if (rl_linefunc
== NULL
)
123 _rl_errmsg ("readline_callback_read_char() called with no handler!");
127 memcpy ((void *)olevel
, (void *)_rl_top_level
, sizeof (procenv_t
));
128 #if defined (HAVE_POSIX_SIGSETJMP)
129 jcode
= sigsetjmp (_rl_top_level
, 0);
131 jcode
= setjmp (_rl_top_level
);
135 (*rl_redisplay_function
) ();
136 _rl_want_redisplay
= 0;
137 memcpy ((void *)_rl_top_level
, (void *)olevel
, sizeof (procenv_t
));
138 CALLBACK_READ_RETURN ();
141 #if defined (HANDLE_SIGNALS)
142 /* Install signal handlers only when readline has control. */
149 if (RL_ISSTATE (RL_STATE_ISEARCH
))
151 eof
= _rl_isearch_callback (_rl_iscxt
);
152 if (eof
== 0 && (RL_ISSTATE (RL_STATE_ISEARCH
) == 0) && RL_ISSTATE (RL_STATE_INPUTPENDING
))
153 rl_callback_read_char ();
155 CALLBACK_READ_RETURN ();
157 else if (RL_ISSTATE (RL_STATE_NSEARCH
))
159 eof
= _rl_nsearch_callback (_rl_nscxt
);
161 CALLBACK_READ_RETURN ();
163 #if defined (VI_MODE)
164 else if (RL_ISSTATE (RL_STATE_VIMOTION
))
166 eof
= _rl_vi_domove_callback (_rl_vimvcxt
);
167 /* Should handle everything, including cleanup, numeric arguments,
168 and turning off RL_STATE_VIMOTION */
169 if (RL_ISSTATE (RL_STATE_NUMERICARG
) == 0)
170 _rl_internal_char_cleanup ();
172 CALLBACK_READ_RETURN ();
175 else if (RL_ISSTATE (RL_STATE_NUMERICARG
))
177 eof
= _rl_arg_callback (_rl_argcxt
);
178 if (eof
== 0 && (RL_ISSTATE (RL_STATE_NUMERICARG
) == 0) && RL_ISSTATE (RL_STATE_INPUTPENDING
))
179 rl_callback_read_char ();
180 /* XXX - this should handle _rl_last_command_was_kill better */
181 else if (RL_ISSTATE (RL_STATE_NUMERICARG
) == 0)
182 _rl_internal_char_cleanup ();
184 CALLBACK_READ_RETURN ();
186 else if (RL_ISSTATE (RL_STATE_MULTIKEY
))
188 eof
= _rl_dispatch_callback (_rl_kscxt
); /* For now */
189 while ((eof
== -1 || eof
== -2) && RL_ISSTATE (RL_STATE_MULTIKEY
) && _rl_kscxt
&& (_rl_kscxt
->flags
& KSEQ_DISPATCHED
))
190 eof
= _rl_dispatch_callback (_rl_kscxt
);
191 if (RL_ISSTATE (RL_STATE_MULTIKEY
) == 0)
193 _rl_internal_char_cleanup ();
194 _rl_want_redisplay
= 1;
197 else if (_rl_callback_func
)
199 /* This allows functions that simply need to read an additional
200 character (like quoted-insert) to register a function to be
201 called when input is available. _rl_callback_data is a
202 pointer to a struct that has the argument count originally
203 passed to the registering function and space for any additional
205 eof
= (*_rl_callback_func
) (_rl_callback_data
);
206 /* If the function `deregisters' itself, make sure the data is
208 if (_rl_callback_func
== 0)
210 if (_rl_callback_data
)
212 _rl_callback_data_dispose (_rl_callback_data
);
213 _rl_callback_data
= 0;
215 _rl_internal_char_cleanup ();
219 eof
= readline_internal_char ();
222 if (rl_done
== 0 && _rl_want_redisplay
)
224 (*rl_redisplay_function
) ();
225 _rl_want_redisplay
= 0;
230 line
= readline_internal_teardown (eof
);
232 if (rl_deprep_term_function
)
233 (*rl_deprep_term_function
) ();
234 #if defined (HANDLE_SIGNALS)
238 (*rl_linefunc
) (line
);
240 /* If the user did not clear out the line, do it for him. */
241 if (rl_line_buffer
[0])
242 _rl_init_line_state ();
244 /* Redisplay the prompt if readline_handler_{install,remove}
246 if (in_handler
== 0 && rl_linefunc
)
247 _rl_callback_newline ();
250 while (rl_pending_input
|| _rl_pushed_input_available () || RL_ISSTATE (RL_STATE_MACROINPUT
));
252 CALLBACK_READ_RETURN ();
255 /* Remove the handler, and make sure the terminal is in its normal state. */
257 rl_callback_handler_remove ()
260 RL_UNSETSTATE (RL_STATE_CALLBACK
);
265 if (rl_deprep_term_function
)
266 (*rl_deprep_term_function
) ();
267 #if defined (HANDLE_SIGNALS)
273 _rl_callback_generic_arg
*
274 _rl_callback_data_alloc (count
)
277 _rl_callback_generic_arg
*arg
;
279 arg
= (_rl_callback_generic_arg
*)xmalloc (sizeof (_rl_callback_generic_arg
));
282 arg
->i1
= arg
->i2
= 0;
288 _rl_callback_data_dispose (arg
)
289 _rl_callback_generic_arg
*arg
;
294 /* Make sure that this agrees with cases in rl_callback_read_char */
296 rl_callback_sigcleanup ()
298 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
301 if (RL_ISSTATE (RL_STATE_ISEARCH
))
302 _rl_isearch_cleanup (_rl_iscxt
, 0);
303 else if (RL_ISSTATE (RL_STATE_NSEARCH
))
304 _rl_nsearch_cleanup (_rl_nscxt
, 0);
305 else if (RL_ISSTATE (RL_STATE_VIMOTION
))
306 RL_UNSETSTATE (RL_STATE_VIMOTION
);
307 else if (RL_ISSTATE (RL_STATE_NUMERICARG
))
310 RL_UNSETSTATE (RL_STATE_NUMERICARG
);
312 else if (RL_ISSTATE (RL_STATE_MULTIKEY
))
313 RL_UNSETSTATE (RL_STATE_MULTIKEY
);
315 _rl_callback_func
= 0;