1 /* callback.c -- functions to use readline as an X `callback' mechanism. */
3 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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 and
66 signals handled all the time, except during calls to the user's function. */
68 rl_vcpfunc_t
*rl_linefunc
; /* user callback function */
69 static int in_handler
; /* terminal_prepped and signals set? */
71 /* Make sure the terminal is set up, initialize readline, and prompt. */
73 _rl_callback_newline ()
81 if (rl_prep_term_function
)
82 (*rl_prep_term_function
) (_rl_meta_flag
);
84 #if defined (HANDLE_SIGNALS)
89 readline_internal_setup ();
92 /* Install a readline handler, set up the terminal, and issue the prompt. */
94 rl_callback_handler_install (prompt
, linefunc
)
96 rl_vcpfunc_t
*linefunc
;
98 rl_set_prompt (prompt
);
99 RL_SETSTATE (RL_STATE_CALLBACK
);
100 rl_linefunc
= linefunc
;
101 _rl_callback_newline ();
104 /* Read one character, and dispatch to the handler if it ends the line. */
106 rl_callback_read_char ()
110 static procenv_t olevel
;
112 if (rl_linefunc
== NULL
)
114 fprintf (stderr
, "readline: readline_callback_read_char() called with no handler!\r\n");
118 memcpy ((void *)olevel
, (void *)readline_top_level
, sizeof (procenv_t
));
119 jcode
= setjmp (readline_top_level
);
122 (*rl_redisplay_function
) ();
123 _rl_want_redisplay
= 0;
124 memcpy ((void *)readline_top_level
, (void *)olevel
, sizeof (procenv_t
));
128 if (RL_ISSTATE (RL_STATE_ISEARCH
))
130 eof
= _rl_isearch_callback (_rl_iscxt
);
131 if (eof
== 0 && (RL_ISSTATE (RL_STATE_ISEARCH
) == 0) && RL_ISSTATE (RL_STATE_INPUTPENDING
))
132 rl_callback_read_char ();
136 else if (RL_ISSTATE (RL_STATE_NSEARCH
))
138 eof
= _rl_nsearch_callback (_rl_nscxt
);
141 else if (RL_ISSTATE (RL_STATE_NUMERICARG
))
143 eof
= _rl_arg_callback (_rl_argcxt
);
144 if (eof
== 0 && (RL_ISSTATE (RL_STATE_NUMERICARG
) == 0) && RL_ISSTATE (RL_STATE_INPUTPENDING
))
145 rl_callback_read_char ();
146 /* XXX - this should handle _rl_last_command_was_kill better */
147 else if (RL_ISSTATE (RL_STATE_NUMERICARG
) == 0)
148 _rl_internal_char_cleanup ();
152 else if (RL_ISSTATE (RL_STATE_MULTIKEY
))
154 eof
= _rl_dispatch_callback (_rl_kscxt
); /* For now */
155 while ((eof
== -1 || eof
== -2) && RL_ISSTATE (RL_STATE_MULTIKEY
) && _rl_kscxt
&& (_rl_kscxt
->flags
& KSEQ_DISPATCHED
))
156 eof
= _rl_dispatch_callback (_rl_kscxt
);
157 if (RL_ISSTATE (RL_STATE_MULTIKEY
) == 0)
159 _rl_internal_char_cleanup ();
160 _rl_want_redisplay
= 1;
163 else if (_rl_callback_func
)
165 /* This allows functions that simply need to read an additional character
166 (like quoted-insert) to register a function to be called when input is
167 available. _rl_callback_data is simply a pointer to a struct that has
168 the argument count originally passed to the registering function and
169 space for any additional parameters. */
170 eof
= (*_rl_callback_func
) (_rl_callback_data
);
171 /* If the function `deregisters' itself, make sure the data is cleaned
173 if (_rl_callback_func
== 0)
175 if (_rl_callback_data
)
177 _rl_callback_data_dispose (_rl_callback_data
);
178 _rl_callback_data
= 0;
180 _rl_internal_char_cleanup ();
184 eof
= readline_internal_char ();
186 if (rl_done
== 0 && _rl_want_redisplay
)
188 (*rl_redisplay_function
) ();
189 _rl_want_redisplay
= 0;
192 /* We loop in case some function has pushed input back with rl_execute_next. */
197 line
= readline_internal_teardown (eof
);
199 if (rl_deprep_term_function
)
200 (*rl_deprep_term_function
) ();
201 #if defined (HANDLE_SIGNALS)
205 (*rl_linefunc
) (line
);
207 /* If the user did not clear out the line, do it for him. */
208 if (rl_line_buffer
[0])
209 _rl_init_line_state ();
211 /* Redisplay the prompt if readline_handler_{install,remove}
213 if (in_handler
== 0 && rl_linefunc
)
214 _rl_callback_newline ();
216 if (rl_pending_input
|| _rl_pushed_input_available () || RL_ISSTATE (RL_STATE_MACROINPUT
))
217 eof
= readline_internal_char ();
223 /* Remove the handler, and make sure the terminal is in its normal state. */
225 rl_callback_handler_remove ()
228 RL_UNSETSTATE (RL_STATE_CALLBACK
);
232 if (rl_deprep_term_function
)
233 (*rl_deprep_term_function
) ();
234 #if defined (HANDLE_SIGNALS)
240 _rl_callback_generic_arg
*
241 _rl_callback_data_alloc (count
)
244 _rl_callback_generic_arg
*arg
;
246 arg
= (_rl_callback_generic_arg
*)xmalloc (sizeof (_rl_callback_generic_arg
));
249 arg
->i1
= arg
->i2
= 0;
254 void _rl_callback_data_dispose (arg
)
255 _rl_callback_generic_arg
*arg
;
This page took 0.04353 seconds and 4 git commands to generate.