Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* callback.c -- functions to use readline as an X `callback' mechanism. */ |
| 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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> |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 33 | |
| 34 | #ifdef HAVE_STDLIB_H |
| 35 | # include <stdlib.h> |
| 36 | #else |
| 37 | # include "ansi_stdlib.h" |
| 38 | #endif |
| 39 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | |
| 42 | /* System-specific feature definitions and include files. */ |
| 43 | #include "rldefs.h" |
| 44 | #include "readline.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 45 | #include "rlprivate.h" |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 46 | #include "xmalloc.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 47 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 53 | /* **************************************************************** */ |
| 54 | /* */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 55 | /* Callback Readline Functions */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 69 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 70 | rl_vcpfunc_t *rl_linefunc; /* user callback function */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 71 | static int in_handler; /* terminal_prepped and signals set? */ |
| 72 | |
| 73 | /* Make sure the terminal is set up, initialize readline, and prompt. */ |
| 74 | static void |
| 75 | _rl_callback_newline () |
| 76 | { |
| 77 | rl_initialize (); |
| 78 | |
| 79 | if (in_handler == 0) |
| 80 | { |
| 81 | in_handler = 1; |
| 82 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 83 | if (rl_prep_term_function) |
| 84 | (*rl_prep_term_function) (_rl_meta_flag); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | readline_internal_setup (); |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 88 | RL_CHECK_SIGNALS (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Install a readline handler, set up the terminal, and issue the prompt. */ |
| 92 | void |
| 93 | rl_callback_handler_install (prompt, linefunc) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 94 | const char *prompt; |
| 95 | rl_vcpfunc_t *linefunc; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 96 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 97 | rl_set_prompt (prompt); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 98 | RL_SETSTATE (RL_STATE_CALLBACK); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 99 | rl_linefunc = linefunc; |
| 100 | _rl_callback_newline (); |
| 101 | } |
| 102 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 103 | #if defined (HANDLE_SIGNALS) |
| 104 | #define CALLBACK_READ_RETURN() \ |
| 105 | do { \ |
| 106 | rl_clear_signals (); \ |
| 107 | return; \ |
| 108 | } while (0) |
| 109 | #else |
| 110 | #define CALLBACK_READ_RETURN() return |
| 111 | #endif |
| 112 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 113 | /* Read one character, and dispatch to the handler if it ends the line. */ |
| 114 | void |
| 115 | rl_callback_read_char () |
| 116 | { |
| 117 | char *line; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 118 | int eof, jcode; |
| 119 | static procenv_t olevel; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | |
| 121 | if (rl_linefunc == NULL) |
| 122 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 123 | _rl_errmsg ("readline_callback_read_char() called with no handler!"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 124 | abort (); |
| 125 | } |
| 126 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 127 | memcpy ((void *)olevel, (void *)_rl_top_level, sizeof (procenv_t)); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 128 | #if defined (HAVE_POSIX_SIGSETJMP) |
| 129 | jcode = sigsetjmp (_rl_top_level, 0); |
| 130 | #else |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 131 | jcode = setjmp (_rl_top_level); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 132 | #endif |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 133 | if (jcode) |
| 134 | { |
| 135 | (*rl_redisplay_function) (); |
| 136 | _rl_want_redisplay = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 137 | memcpy ((void *)_rl_top_level, (void *)olevel, sizeof (procenv_t)); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 138 | CALLBACK_READ_RETURN (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 141 | #if defined (HANDLE_SIGNALS) |
| 142 | /* Install signal handlers only when readline has control. */ |
| 143 | rl_set_signals (); |
| 144 | #endif |
| 145 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 146 | do |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 147 | { |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 148 | RL_CHECK_SIGNALS (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 149 | if (RL_ISSTATE (RL_STATE_ISEARCH)) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 150 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 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 (); |
| 154 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 155 | CALLBACK_READ_RETURN (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 156 | } |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 157 | else if (RL_ISSTATE (RL_STATE_NSEARCH)) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 158 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 159 | eof = _rl_nsearch_callback (_rl_nscxt); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 160 | |
| 161 | CALLBACK_READ_RETURN (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 162 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 163 | #if defined (VI_MODE) |
| 164 | else if (RL_ISSTATE (RL_STATE_VIMOTION)) |
| 165 | { |
| 166 | eof = _rl_vi_domove_callback (_rl_vimvcxt); |
| 167 | /* Should handle everything, including cleanup, numeric arguments, |
| 168 | and turning off RL_STATE_VIMOTION */ |
Chet Ramey | 23c597e | 2011-11-22 19:59:52 -0500 | [diff] [blame] | 169 | if (RL_ISSTATE (RL_STATE_NUMERICARG) == 0) |
| 170 | _rl_internal_char_cleanup (); |
| 171 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 172 | CALLBACK_READ_RETURN (); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 173 | } |
| 174 | #endif |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 175 | else if (RL_ISSTATE (RL_STATE_NUMERICARG)) |
| 176 | { |
| 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 (); |
| 183 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 184 | CALLBACK_READ_RETURN (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 185 | } |
| 186 | else if (RL_ISSTATE (RL_STATE_MULTIKEY)) |
| 187 | { |
| 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) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 192 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 193 | _rl_internal_char_cleanup (); |
| 194 | _rl_want_redisplay = 1; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 195 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 196 | } |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 197 | else if (_rl_callback_func) |
| 198 | { |
| 199 | /* This allows functions that simply need to read an additional |
| 200 | character (like quoted-insert) to register a function to be |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 201 | called when input is available. _rl_callback_data is a |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 202 | pointer to a struct that has the argument count originally |
| 203 | passed to the registering function and space for any additional |
| 204 | parameters. */ |
| 205 | eof = (*_rl_callback_func) (_rl_callback_data); |
| 206 | /* If the function `deregisters' itself, make sure the data is |
| 207 | cleaned up. */ |
| 208 | if (_rl_callback_func == 0) |
| 209 | { |
| 210 | if (_rl_callback_data) |
| 211 | { |
| 212 | _rl_callback_data_dispose (_rl_callback_data); |
| 213 | _rl_callback_data = 0; |
| 214 | } |
| 215 | _rl_internal_char_cleanup (); |
| 216 | } |
| 217 | } |
| 218 | else |
| 219 | eof = readline_internal_char (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 220 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 221 | RL_CHECK_SIGNALS (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 222 | if (rl_done == 0 && _rl_want_redisplay) |
| 223 | { |
| 224 | (*rl_redisplay_function) (); |
| 225 | _rl_want_redisplay = 0; |
| 226 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 227 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 228 | if (rl_done) |
| 229 | { |
| 230 | line = readline_internal_teardown (eof); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 231 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 232 | if (rl_deprep_term_function) |
| 233 | (*rl_deprep_term_function) (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 234 | #if defined (HANDLE_SIGNALS) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 235 | rl_clear_signals (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 236 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 237 | in_handler = 0; |
| 238 | (*rl_linefunc) (line); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 239 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 (); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 243 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 244 | /* Redisplay the prompt if readline_handler_{install,remove} |
| 245 | not called. */ |
| 246 | if (in_handler == 0 && rl_linefunc) |
| 247 | _rl_callback_newline (); |
| 248 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 249 | } |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 250 | while (rl_pending_input || _rl_pushed_input_available () || RL_ISSTATE (RL_STATE_MACROINPUT)); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 251 | |
| 252 | CALLBACK_READ_RETURN (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Remove the handler, and make sure the terminal is in its normal state. */ |
| 256 | void |
| 257 | rl_callback_handler_remove () |
| 258 | { |
| 259 | rl_linefunc = NULL; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 260 | RL_UNSETSTATE (RL_STATE_CALLBACK); |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 261 | RL_CHECK_SIGNALS (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 262 | if (in_handler) |
| 263 | { |
| 264 | in_handler = 0; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 265 | if (rl_deprep_term_function) |
| 266 | (*rl_deprep_term_function) (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 267 | #if defined (HANDLE_SIGNALS) |
| 268 | rl_clear_signals (); |
| 269 | #endif |
| 270 | } |
| 271 | } |
| 272 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 273 | _rl_callback_generic_arg * |
| 274 | _rl_callback_data_alloc (count) |
| 275 | int count; |
| 276 | { |
| 277 | _rl_callback_generic_arg *arg; |
| 278 | |
| 279 | arg = (_rl_callback_generic_arg *)xmalloc (sizeof (_rl_callback_generic_arg)); |
| 280 | arg->count = count; |
| 281 | |
| 282 | arg->i1 = arg->i2 = 0; |
| 283 | |
| 284 | return arg; |
| 285 | } |
| 286 | |
| 287 | void _rl_callback_data_dispose (arg) |
| 288 | _rl_callback_generic_arg *arg; |
| 289 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 290 | xfree (arg); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 293 | #endif |