Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* readline.c -- a general facility for reading lines of input |
2 | with emacs style editing and completion. */ | |
3 | ||
cb41b9e7 | 4 | /* Copyright (C) 1987-2017 Free Software Foundation, Inc. |
d60d9f65 | 5 | |
cc88a640 JK |
6 | This file is part of the GNU Readline Library (Readline), a library |
7 | for reading lines of text with interactive input and history editing. | |
d60d9f65 | 8 | |
cc88a640 JK |
9 | Readline is free software: you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation, either version 3 of the License, or | |
d60d9f65 SS |
12 | (at your option) any later version. |
13 | ||
cc88a640 JK |
14 | Readline is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
d60d9f65 SS |
17 | GNU General Public License for more details. |
18 | ||
cc88a640 JK |
19 | You should have received a copy of the GNU General Public License |
20 | along with Readline. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
d60d9f65 SS |
23 | #define READLINE_LIBRARY |
24 | ||
25 | #if defined (HAVE_CONFIG_H) | |
26 | # include <config.h> | |
27 | #endif | |
28 | ||
29 | #include <sys/types.h> | |
30 | #include "posixstat.h" | |
31 | #include <fcntl.h> | |
32 | #if defined (HAVE_SYS_FILE_H) | |
33 | # include <sys/file.h> | |
34 | #endif /* HAVE_SYS_FILE_H */ | |
35 | ||
36 | #if defined (HAVE_UNISTD_H) | |
37 | # include <unistd.h> | |
38 | #endif /* HAVE_UNISTD_H */ | |
39 | ||
40 | #if defined (HAVE_STDLIB_H) | |
41 | # include <stdlib.h> | |
42 | #else | |
43 | # include "ansi_stdlib.h" | |
44 | #endif /* HAVE_STDLIB_H */ | |
45 | ||
46 | #if defined (HAVE_LOCALE_H) | |
47 | # include <locale.h> | |
48 | #endif | |
49 | ||
d60d9f65 SS |
50 | #include <stdio.h> |
51 | #include "posixjmp.h" | |
cc88a640 JK |
52 | #include <errno.h> |
53 | ||
54 | #if !defined (errno) | |
55 | extern int errno; | |
56 | #endif /* !errno */ | |
d60d9f65 SS |
57 | |
58 | /* System-specific feature definitions and include files. */ | |
59 | #include "rldefs.h" | |
9255ee31 | 60 | #include "rlmbutil.h" |
d60d9f65 SS |
61 | |
62 | #if defined (__EMX__) | |
63 | # define INCL_DOSPROCESS | |
64 | # include <os2.h> | |
65 | #endif /* __EMX__ */ | |
66 | ||
67 | /* Some standard library routines. */ | |
68 | #include "readline.h" | |
69 | #include "history.h" | |
70 | ||
1b17e766 EZ |
71 | #include "rlprivate.h" |
72 | #include "rlshell.h" | |
73 | #include "xmalloc.h" | |
74 | ||
d60d9f65 | 75 | #ifndef RL_LIBRARY_VERSION |
5bdf8622 | 76 | # define RL_LIBRARY_VERSION "5.1" |
d60d9f65 SS |
77 | #endif |
78 | ||
9255ee31 | 79 | #ifndef RL_READLINE_VERSION |
5bdf8622 | 80 | # define RL_READLINE_VERSION 0x0501 |
9255ee31 EZ |
81 | #endif |
82 | ||
83 | extern void _rl_free_history_entry PARAMS((HIST_ENTRY *)); | |
d60d9f65 | 84 | |
775e241e TT |
85 | #if defined (COLOR_SUPPORT) |
86 | extern void _rl_parse_colors PARAMS((void)); /* XXX */ | |
87 | #endif | |
88 | ||
89 | ||
d60d9f65 | 90 | /* Forward declarations used in this file. */ |
9255ee31 EZ |
91 | static char *readline_internal PARAMS((void)); |
92 | static void readline_initialize_everything PARAMS((void)); | |
d60d9f65 | 93 | |
9255ee31 EZ |
94 | static void bind_arrow_keys_internal PARAMS((Keymap)); |
95 | static void bind_arrow_keys PARAMS((void)); | |
d60d9f65 | 96 | |
775e241e TT |
97 | static void bind_bracketed_paste_prefix PARAMS((void)); |
98 | ||
9255ee31 | 99 | static void readline_default_bindings PARAMS((void)); |
5bdf8622 DJ |
100 | static void reset_default_bindings PARAMS((void)); |
101 | ||
102 | static int _rl_subseq_result PARAMS((int, Keymap, int, int)); | |
103 | static int _rl_subseq_getchar PARAMS((int)); | |
d60d9f65 SS |
104 | |
105 | /* **************************************************************** */ | |
106 | /* */ | |
107 | /* Line editing input utility */ | |
108 | /* */ | |
109 | /* **************************************************************** */ | |
110 | ||
9255ee31 | 111 | const char *rl_library_version = RL_LIBRARY_VERSION; |
d60d9f65 | 112 | |
9255ee31 EZ |
113 | int rl_readline_version = RL_READLINE_VERSION; |
114 | ||
115 | /* True if this is `real' readline as opposed to some stub substitute. */ | |
1b17e766 EZ |
116 | int rl_gnu_readline_p = 1; |
117 | ||
d60d9f65 SS |
118 | /* A pointer to the keymap that is currently in use. |
119 | By default, it is the standard emacs keymap. */ | |
120 | Keymap _rl_keymap = emacs_standard_keymap; | |
121 | ||
122 | /* The current style of editing. */ | |
123 | int rl_editing_mode = emacs_mode; | |
124 | ||
9255ee31 EZ |
125 | /* The current insert mode: input (the default) or overwrite */ |
126 | int rl_insert_mode = RL_IM_DEFAULT; | |
127 | ||
d60d9f65 SS |
128 | /* Non-zero if we called this function from _rl_dispatch(). It's present |
129 | so functions can find out whether they were called from a key binding | |
130 | or directly from an application. */ | |
131 | int rl_dispatching; | |
132 | ||
133 | /* Non-zero if the previous command was a kill command. */ | |
134 | int _rl_last_command_was_kill = 0; | |
135 | ||
136 | /* The current value of the numeric argument specified by the user. */ | |
137 | int rl_numeric_arg = 1; | |
138 | ||
139 | /* Non-zero if an argument was typed. */ | |
140 | int rl_explicit_arg = 0; | |
141 | ||
142 | /* Temporary value used while generating the argument. */ | |
143 | int rl_arg_sign = 1; | |
144 | ||
145 | /* Non-zero means we have been called at least once before. */ | |
146 | static int rl_initialized; | |
147 | ||
9255ee31 | 148 | #if 0 |
d60d9f65 SS |
149 | /* If non-zero, this program is running in an EMACS buffer. */ |
150 | static int running_in_emacs; | |
9255ee31 EZ |
151 | #endif |
152 | ||
153 | /* Flags word encapsulating the current readline state. */ | |
775e241e | 154 | unsigned long rl_readline_state = RL_STATE_NONE; |
d60d9f65 SS |
155 | |
156 | /* The current offset in the current input line. */ | |
157 | int rl_point; | |
158 | ||
159 | /* Mark in the current input line. */ | |
160 | int rl_mark; | |
161 | ||
162 | /* Length of the current input line. */ | |
163 | int rl_end; | |
164 | ||
165 | /* Make this non-zero to return the current input_line. */ | |
166 | int rl_done; | |
167 | ||
168 | /* The last function executed by readline. */ | |
9255ee31 | 169 | rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL; |
d60d9f65 SS |
170 | |
171 | /* Top level environment for readline_internal (). */ | |
cc88a640 | 172 | procenv_t _rl_top_level; |
d60d9f65 SS |
173 | |
174 | /* The streams we interact with. */ | |
175 | FILE *_rl_in_stream, *_rl_out_stream; | |
176 | ||
177 | /* The names of the streams that we do input and output to. */ | |
178 | FILE *rl_instream = (FILE *)NULL; | |
179 | FILE *rl_outstream = (FILE *)NULL; | |
180 | ||
9255ee31 EZ |
181 | /* Non-zero means echo characters as they are read. Defaults to no echo; |
182 | set to 1 if there is a controlling terminal, we can get its attributes, | |
183 | and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings | |
184 | for the code that sets it. */ | |
cc88a640 | 185 | int _rl_echoing_p = 0; |
d60d9f65 SS |
186 | |
187 | /* Current prompt. */ | |
9255ee31 | 188 | char *rl_prompt = (char *)NULL; |
d60d9f65 SS |
189 | int rl_visible_prompt_length = 0; |
190 | ||
1b17e766 EZ |
191 | /* Set to non-zero by calling application if it has already printed rl_prompt |
192 | and does not want readline to do it the first time. */ | |
193 | int rl_already_prompted = 0; | |
194 | ||
d60d9f65 SS |
195 | /* The number of characters read in order to type this complete command. */ |
196 | int rl_key_sequence_length = 0; | |
197 | ||
198 | /* If non-zero, then this is the address of a function to call just | |
c862e87b | 199 | before readline_internal_setup () prints the first prompt. */ |
9255ee31 | 200 | rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL; |
d60d9f65 | 201 | |
c862e87b JM |
202 | /* If non-zero, this is the address of a function to call just before |
203 | readline_internal_setup () returns and readline_internal starts | |
204 | reading input characters. */ | |
9255ee31 | 205 | rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL; |
c862e87b | 206 | |
d60d9f65 SS |
207 | /* What we use internally. You should always refer to RL_LINE_BUFFER. */ |
208 | static char *the_line; | |
209 | ||
210 | /* The character that can generate an EOF. Really read from | |
211 | the terminal driver... just defaulted here. */ | |
212 | int _rl_eof_char = CTRL ('D'); | |
213 | ||
214 | /* Non-zero makes this the next keystroke to read. */ | |
215 | int rl_pending_input = 0; | |
216 | ||
cb41b9e7 TT |
217 | /* If non-zero when readline_internal returns, it means we found EOF */ |
218 | int _rl_eof_found = 0; | |
219 | ||
d60d9f65 | 220 | /* Pointer to a useful terminal name. */ |
9255ee31 | 221 | const char *rl_terminal_name = (const char *)NULL; |
d60d9f65 SS |
222 | |
223 | /* Non-zero means to always use horizontal scrolling in line display. */ | |
224 | int _rl_horizontal_scroll_mode = 0; | |
225 | ||
226 | /* Non-zero means to display an asterisk at the starts of history lines | |
227 | which have been modified. */ | |
cb41b9e7 | 228 | int _rl_mark_modified_lines = 0; |
d60d9f65 SS |
229 | |
230 | /* The style of `bell' notification preferred. This can be set to NO_BELL, | |
231 | AUDIBLE_BELL, or VISIBLE_BELL. */ | |
232 | int _rl_bell_preference = AUDIBLE_BELL; | |
233 | ||
234 | /* String inserted into the line by rl_insert_comment (). */ | |
235 | char *_rl_comment_begin; | |
236 | ||
237 | /* Keymap holding the function currently being executed. */ | |
238 | Keymap rl_executing_keymap; | |
239 | ||
5bdf8622 DJ |
240 | /* Keymap we're currently using to dispatch. */ |
241 | Keymap _rl_dispatching_keymap; | |
242 | ||
c862e87b JM |
243 | /* Non-zero means to erase entire line, including prompt, on empty input lines. */ |
244 | int rl_erase_empty_line = 0; | |
245 | ||
1b17e766 EZ |
246 | /* Non-zero means to read only this many characters rather than up to a |
247 | character bound to accept-line. */ | |
cb41b9e7 | 248 | int rl_num_chars_to_read = 0; |
1b17e766 | 249 | |
775e241e | 250 | /* Line buffer and maintenance. */ |
d60d9f65 SS |
251 | char *rl_line_buffer = (char *)NULL; |
252 | int rl_line_buffer_len = 0; | |
253 | ||
5bdf8622 DJ |
254 | /* Key sequence `contexts' */ |
255 | _rl_keyseq_cxt *_rl_kscxt = 0; | |
256 | ||
775e241e TT |
257 | int rl_executing_key; |
258 | char *rl_executing_keyseq = 0; | |
259 | int _rl_executing_keyseq_size = 0; | |
260 | ||
261 | /* Timeout (specified in milliseconds) when reading characters making up an | |
262 | ambiguous multiple-key sequence */ | |
263 | int _rl_keyseq_timeout = 500; | |
264 | ||
265 | #define RESIZE_KEYSEQ_BUFFER() \ | |
266 | do \ | |
267 | { \ | |
268 | if (rl_key_sequence_length + 2 >= _rl_executing_keyseq_size) \ | |
269 | { \ | |
270 | _rl_executing_keyseq_size += 16; \ | |
271 | rl_executing_keyseq = xrealloc (rl_executing_keyseq, _rl_executing_keyseq_size); \ | |
272 | } \ | |
273 | } \ | |
274 | while (0); | |
275 | ||
9255ee31 | 276 | /* Forward declarations used by the display, termcap, and history code. */ |
d60d9f65 SS |
277 | |
278 | /* **************************************************************** */ | |
279 | /* */ | |
280 | /* `Forward' declarations */ | |
281 | /* */ | |
282 | /* **************************************************************** */ | |
283 | ||
284 | /* Non-zero means do not parse any lines other than comments and | |
285 | parser directives. */ | |
286 | unsigned char _rl_parsing_conditionalized_out = 0; | |
287 | ||
288 | /* Non-zero means to convert characters with the meta bit set to | |
289 | escape-prefixed characters so we can indirect through | |
290 | emacs_meta_keymap or vi_escape_keymap. */ | |
291 | int _rl_convert_meta_chars_to_ascii = 1; | |
292 | ||
293 | /* Non-zero means to output characters with the meta bit set directly | |
294 | rather than as a meta-prefixed escape sequence. */ | |
295 | int _rl_output_meta_chars = 0; | |
296 | ||
5bdf8622 DJ |
297 | /* Non-zero means to look at the termios special characters and bind |
298 | them to equivalent readline functions at startup. */ | |
299 | int _rl_bind_stty_chars = 1; | |
300 | ||
cc88a640 JK |
301 | /* Non-zero means to go through the history list at every newline (or |
302 | whenever rl_done is set and readline returns) and revert each line to | |
303 | its initial state. */ | |
304 | int _rl_revert_all_at_newline = 0; | |
305 | ||
306 | /* Non-zero means to honor the termios ECHOCTL bit and echo control | |
307 | characters corresponding to keyboard-generated signals. */ | |
308 | int _rl_echo_control_chars = 1; | |
309 | ||
775e241e TT |
310 | /* Non-zero means to prefix the displayed prompt with a character indicating |
311 | the editing mode: @ for emacs, : for vi-command, + for vi-insert. */ | |
312 | int _rl_show_mode_in_prompt = 0; | |
313 | ||
314 | /* Non-zero means to attempt to put the terminal in `bracketed paste mode', | |
315 | where it will prefix pasted text with an escape sequence and send | |
316 | another to mark the end of the paste. */ | |
317 | int _rl_enable_bracketed_paste = 0; | |
318 | ||
d60d9f65 SS |
319 | /* **************************************************************** */ |
320 | /* */ | |
321 | /* Top Level Functions */ | |
322 | /* */ | |
323 | /* **************************************************************** */ | |
324 | ||
325 | /* Non-zero means treat 0200 bit in terminal input as Meta bit. */ | |
326 | int _rl_meta_flag = 0; /* Forward declaration */ | |
327 | ||
9255ee31 EZ |
328 | /* Set up the prompt and expand it. Called from readline() and |
329 | rl_callback_handler_install (). */ | |
330 | int | |
cb41b9e7 | 331 | rl_set_prompt (const char *prompt) |
9255ee31 EZ |
332 | { |
333 | FREE (rl_prompt); | |
334 | rl_prompt = prompt ? savestring (prompt) : (char *)NULL; | |
5bdf8622 | 335 | rl_display_prompt = rl_prompt ? rl_prompt : ""; |
9255ee31 EZ |
336 | |
337 | rl_visible_prompt_length = rl_expand_prompt (rl_prompt); | |
338 | return 0; | |
339 | } | |
340 | ||
d60d9f65 SS |
341 | /* Read a line of input. Prompt with PROMPT. An empty PROMPT means |
342 | none. A return value of NULL means that EOF was encountered. */ | |
343 | char * | |
cb41b9e7 | 344 | readline (const char *prompt) |
d60d9f65 SS |
345 | { |
346 | char *value; | |
cc88a640 JK |
347 | #if 0 |
348 | int in_callback; | |
349 | #endif | |
d60d9f65 | 350 | |
d60d9f65 SS |
351 | /* If we are at EOF return a NULL string. */ |
352 | if (rl_pending_input == EOF) | |
353 | { | |
9255ee31 | 354 | rl_clear_pending_input (); |
d60d9f65 SS |
355 | return ((char *)NULL); |
356 | } | |
357 | ||
cc88a640 JK |
358 | #if 0 |
359 | /* If readline() is called after installing a callback handler, temporarily | |
360 | turn off the callback state to avoid ensuing messiness. Patch supplied | |
361 | by the gdb folks. XXX -- disabled. This can be fooled and readline | |
362 | left in a strange state by a poorly-timed longjmp. */ | |
363 | if (in_callback = RL_ISSTATE (RL_STATE_CALLBACK)) | |
364 | RL_UNSETSTATE (RL_STATE_CALLBACK); | |
365 | #endif | |
366 | ||
9255ee31 | 367 | rl_set_prompt (prompt); |
d60d9f65 SS |
368 | |
369 | rl_initialize (); | |
5bdf8622 DJ |
370 | if (rl_prep_term_function) |
371 | (*rl_prep_term_function) (_rl_meta_flag); | |
d60d9f65 SS |
372 | |
373 | #if defined (HANDLE_SIGNALS) | |
374 | rl_set_signals (); | |
375 | #endif | |
376 | ||
377 | value = readline_internal (); | |
5bdf8622 DJ |
378 | if (rl_deprep_term_function) |
379 | (*rl_deprep_term_function) (); | |
d60d9f65 SS |
380 | |
381 | #if defined (HANDLE_SIGNALS) | |
382 | rl_clear_signals (); | |
383 | #endif | |
384 | ||
cc88a640 JK |
385 | #if 0 |
386 | if (in_callback) | |
387 | RL_SETSTATE (RL_STATE_CALLBACK); | |
388 | #endif | |
389 | ||
775e241e TT |
390 | #if HAVE_DECL_AUDIT_USER_TTY && defined (HAVE_LIBAUDIT_H) && defined (ENABLE_TTY_AUDIT_SUPPORT) |
391 | if (value) | |
392 | _rl_audit_tty (value); | |
393 | #endif | |
394 | ||
d60d9f65 SS |
395 | return (value); |
396 | } | |
397 | ||
398 | #if defined (READLINE_CALLBACKS) | |
399 | # define STATIC_CALLBACK | |
400 | #else | |
401 | # define STATIC_CALLBACK static | |
402 | #endif | |
403 | ||
404 | STATIC_CALLBACK void | |
cb41b9e7 | 405 | readline_internal_setup (void) |
d60d9f65 | 406 | { |
1b17e766 EZ |
407 | char *nprompt; |
408 | ||
d60d9f65 SS |
409 | _rl_in_stream = rl_instream; |
410 | _rl_out_stream = rl_outstream; | |
411 | ||
775e241e TT |
412 | /* Enable the meta key only for the duration of readline(), if this |
413 | terminal has one and the terminal has been initialized */ | |
414 | if (_rl_enable_meta & RL_ISSTATE (RL_STATE_TERMPREPPED)) | |
415 | _rl_enable_meta_key (); | |
416 | ||
d60d9f65 SS |
417 | if (rl_startup_hook) |
418 | (*rl_startup_hook) (); | |
419 | ||
775e241e TT |
420 | #if defined (VI_MODE) |
421 | if (rl_editing_mode == vi_mode) | |
422 | rl_vi_insertion_mode (1, 'i'); /* don't want to reset last */ | |
cb41b9e7 | 423 | else |
775e241e | 424 | #endif /* VI_MODE */ |
cb41b9e7 TT |
425 | if (_rl_show_mode_in_prompt) |
426 | _rl_reset_prompt (); | |
775e241e | 427 | |
9255ee31 EZ |
428 | /* If we're not echoing, we still want to at least print a prompt, because |
429 | rl_redisplay will not do it for us. If the calling application has a | |
430 | custom redisplay function, though, let that function handle it. */ | |
cc88a640 | 431 | if (_rl_echoing_p == 0 && rl_redisplay_function == rl_redisplay) |
d60d9f65 | 432 | { |
1b17e766 | 433 | if (rl_prompt && rl_already_prompted == 0) |
d60d9f65 | 434 | { |
1b17e766 EZ |
435 | nprompt = _rl_strip_prompt (rl_prompt); |
436 | fprintf (_rl_out_stream, "%s", nprompt); | |
d60d9f65 | 437 | fflush (_rl_out_stream); |
cc88a640 | 438 | xfree (nprompt); |
d60d9f65 SS |
439 | } |
440 | } | |
441 | else | |
442 | { | |
1b17e766 EZ |
443 | if (rl_prompt && rl_already_prompted) |
444 | rl_on_new_line_with_prompt (); | |
445 | else | |
446 | rl_on_new_line (); | |
d60d9f65 | 447 | (*rl_redisplay_function) (); |
9255ee31 EZ |
448 | } |
449 | ||
c862e87b JM |
450 | if (rl_pre_input_hook) |
451 | (*rl_pre_input_hook) (); | |
cc88a640 JK |
452 | |
453 | RL_CHECK_SIGNALS (); | |
d60d9f65 SS |
454 | } |
455 | ||
456 | STATIC_CALLBACK char * | |
cb41b9e7 | 457 | readline_internal_teardown (int eof) |
d60d9f65 SS |
458 | { |
459 | char *temp; | |
460 | HIST_ENTRY *entry; | |
461 | ||
cc88a640 JK |
462 | RL_CHECK_SIGNALS (); |
463 | ||
d60d9f65 SS |
464 | /* Restore the original of this history line, iff the line that we |
465 | are editing was originally in the history, AND the line has changed. */ | |
466 | entry = current_history (); | |
467 | ||
468 | if (entry && rl_undo_list) | |
469 | { | |
470 | temp = savestring (the_line); | |
471 | rl_revert_line (1, 0); | |
c862e87b | 472 | entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL); |
d60d9f65 SS |
473 | _rl_free_history_entry (entry); |
474 | ||
475 | strcpy (the_line, temp); | |
cc88a640 | 476 | xfree (temp); |
d60d9f65 SS |
477 | } |
478 | ||
cc88a640 JK |
479 | if (_rl_revert_all_at_newline) |
480 | _rl_revert_all_lines (); | |
481 | ||
d60d9f65 SS |
482 | /* At any rate, it is highly likely that this line has an undo list. Get |
483 | rid of it now. */ | |
484 | if (rl_undo_list) | |
9255ee31 EZ |
485 | rl_free_undo_list (); |
486 | ||
775e241e TT |
487 | /* Disable the meta key, if this terminal has one and we were told to use it. |
488 | The check whether or not we sent the enable string is in | |
489 | _rl_disable_meta_key(); the flag is set in _rl_enable_meta_key */ | |
490 | _rl_disable_meta_key (); | |
491 | ||
9255ee31 EZ |
492 | /* Restore normal cursor, if available. */ |
493 | _rl_set_insert_mode (RL_IM_INSERT, 0); | |
d60d9f65 SS |
494 | |
495 | return (eof ? (char *)NULL : savestring (the_line)); | |
496 | } | |
497 | ||
5bdf8622 | 498 | void |
cb41b9e7 | 499 | _rl_internal_char_cleanup (void) |
5bdf8622 DJ |
500 | { |
501 | #if defined (VI_MODE) | |
502 | /* In vi mode, when you exit insert mode, the cursor moves back | |
503 | over the previous character. We explicitly check for that here. */ | |
504 | if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap) | |
505 | rl_vi_check (); | |
506 | #endif /* VI_MODE */ | |
507 | ||
508 | if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read) | |
509 | { | |
510 | (*rl_redisplay_function) (); | |
511 | _rl_want_redisplay = 0; | |
512 | rl_newline (1, '\n'); | |
513 | } | |
514 | ||
515 | if (rl_done == 0) | |
516 | { | |
517 | (*rl_redisplay_function) (); | |
518 | _rl_want_redisplay = 0; | |
519 | } | |
520 | ||
521 | /* If the application writer has told us to erase the entire line if | |
522 | the only character typed was something bound to rl_newline, do so. */ | |
523 | if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline && | |
524 | rl_point == 0 && rl_end == 0) | |
525 | _rl_erase_entire_line (); | |
526 | } | |
527 | ||
d60d9f65 SS |
528 | STATIC_CALLBACK int |
529 | #if defined (READLINE_CALLBACKS) | |
cb41b9e7 | 530 | readline_internal_char (void) |
d60d9f65 | 531 | #else |
cb41b9e7 | 532 | readline_internal_charloop (void) |
d60d9f65 SS |
533 | #endif |
534 | { | |
535 | static int lastc, eof_found; | |
cb41b9e7 | 536 | int c, code, lk, r; |
d60d9f65 | 537 | |
775e241e | 538 | lastc = EOF; |
d60d9f65 SS |
539 | |
540 | #if !defined (READLINE_CALLBACKS) | |
775e241e | 541 | eof_found = 0; |
d60d9f65 SS |
542 | while (rl_done == 0) |
543 | { | |
544 | #endif | |
545 | lk = _rl_last_command_was_kill; | |
546 | ||
775e241e TT |
547 | #if defined (HAVE_POSIX_SIGSETJMP) |
548 | code = sigsetjmp (_rl_top_level, 0); | |
549 | #else | |
cc88a640 | 550 | code = setjmp (_rl_top_level); |
775e241e | 551 | #endif |
d60d9f65 SS |
552 | |
553 | if (code) | |
5bdf8622 DJ |
554 | { |
555 | (*rl_redisplay_function) (); | |
556 | _rl_want_redisplay = 0; | |
557 | /* If we get here, we're not being called from something dispatched | |
558 | from _rl_callback_read_char(), which sets up its own value of | |
cc88a640 | 559 | _rl_top_level (saving and restoring the old, of course), so |
5bdf8622 DJ |
560 | we can just return here. */ |
561 | if (RL_ISSTATE (RL_STATE_CALLBACK)) | |
562 | return (0); | |
563 | } | |
d60d9f65 SS |
564 | |
565 | if (rl_pending_input == 0) | |
566 | { | |
567 | /* Then initialize the argument and number of keys read. */ | |
5bdf8622 | 568 | _rl_reset_argument (); |
775e241e | 569 | rl_executing_keyseq[rl_key_sequence_length = 0] = '\0'; |
d60d9f65 SS |
570 | } |
571 | ||
9255ee31 | 572 | RL_SETSTATE(RL_STATE_READCMD); |
d60d9f65 | 573 | c = rl_read_key (); |
9255ee31 | 574 | RL_UNSETSTATE(RL_STATE_READCMD); |
d60d9f65 | 575 | |
cc88a640 JK |
576 | /* look at input.c:rl_getc() for the circumstances under which this will |
577 | be returned; punt immediately on read error without converting it to | |
775e241e TT |
578 | a newline; assume that rl_read_key has already called the signal |
579 | handler. */ | |
cc88a640 JK |
580 | if (c == READERR) |
581 | { | |
582 | #if defined (READLINE_CALLBACKS) | |
583 | RL_SETSTATE(RL_STATE_DONE); | |
584 | return (rl_done = 1); | |
585 | #else | |
586 | eof_found = 1; | |
587 | break; | |
588 | #endif | |
589 | } | |
590 | ||
775e241e TT |
591 | /* EOF typed to a non-blank line is ^D the first time, EOF the second |
592 | time in a row. This won't return any partial line read from the tty. | |
593 | If we want to change this, to force any existing line to be returned | |
594 | when read(2) reads EOF, for example, this is the place to change. */ | |
d60d9f65 | 595 | if (c == EOF && rl_end) |
775e241e TT |
596 | { |
597 | if (RL_SIG_RECEIVED ()) | |
598 | { | |
599 | RL_CHECK_SIGNALS (); | |
600 | if (rl_signal_event_hook) | |
601 | (*rl_signal_event_hook) (); /* XXX */ | |
602 | } | |
603 | ||
604 | /* XXX - reading two consecutive EOFs returns EOF */ | |
605 | if (RL_ISSTATE (RL_STATE_TERMPREPPED)) | |
606 | { | |
607 | if (lastc == _rl_eof_char || lastc == EOF) | |
608 | rl_end = 0; | |
609 | else | |
610 | c = _rl_eof_char; | |
611 | } | |
612 | else | |
613 | c = NEWLINE; | |
614 | } | |
d60d9f65 SS |
615 | |
616 | /* The character _rl_eof_char typed to blank line, and not as the | |
775e241e TT |
617 | previous character is interpreted as EOF. This doesn't work when |
618 | READLINE_CALLBACKS is defined, so hitting a series of ^Ds will | |
619 | erase all the chars on the line and then return EOF. */ | |
620 | if (((c == _rl_eof_char && lastc != c) || c == EOF) && rl_end == 0) | |
d60d9f65 SS |
621 | { |
622 | #if defined (READLINE_CALLBACKS) | |
9255ee31 | 623 | RL_SETSTATE(RL_STATE_DONE); |
d60d9f65 SS |
624 | return (rl_done = 1); |
625 | #else | |
626 | eof_found = 1; | |
627 | break; | |
628 | #endif | |
629 | } | |
630 | ||
631 | lastc = c; | |
cb41b9e7 | 632 | r = _rl_dispatch ((unsigned char)c, _rl_keymap); |
cc88a640 | 633 | RL_CHECK_SIGNALS (); |
d60d9f65 SS |
634 | |
635 | /* If there was no change in _rl_last_command_was_kill, then no kill | |
636 | has taken place. Note that if input is pending we are reading | |
637 | a prefix command, so nothing has changed yet. */ | |
638 | if (rl_pending_input == 0 && lk == _rl_last_command_was_kill) | |
639 | _rl_last_command_was_kill = 0; | |
640 | ||
5bdf8622 | 641 | _rl_internal_char_cleanup (); |
c862e87b | 642 | |
d60d9f65 SS |
643 | #if defined (READLINE_CALLBACKS) |
644 | return 0; | |
645 | #else | |
646 | } | |
647 | ||
648 | return (eof_found); | |
649 | #endif | |
650 | } | |
651 | ||
652 | #if defined (READLINE_CALLBACKS) | |
653 | static int | |
cb41b9e7 | 654 | readline_internal_charloop (void) |
d60d9f65 | 655 | { |
c862e87b | 656 | int eof = 1; |
d60d9f65 SS |
657 | |
658 | while (rl_done == 0) | |
659 | eof = readline_internal_char (); | |
660 | return (eof); | |
661 | } | |
662 | #endif /* READLINE_CALLBACKS */ | |
663 | ||
664 | /* Read a line of input from the global rl_instream, doing output on | |
665 | the global rl_outstream. | |
666 | If rl_prompt is non-null, then that is our prompt. */ | |
667 | static char * | |
cb41b9e7 | 668 | readline_internal (void) |
d60d9f65 | 669 | { |
d60d9f65 | 670 | readline_internal_setup (); |
cb41b9e7 TT |
671 | _rl_eof_found = readline_internal_charloop (); |
672 | return (readline_internal_teardown (_rl_eof_found)); | |
d60d9f65 SS |
673 | } |
674 | ||
675 | void | |
cb41b9e7 | 676 | _rl_init_line_state (void) |
d60d9f65 | 677 | { |
9255ee31 | 678 | rl_point = rl_end = rl_mark = 0; |
d60d9f65 SS |
679 | the_line = rl_line_buffer; |
680 | the_line[0] = 0; | |
681 | } | |
682 | ||
683 | void | |
cb41b9e7 | 684 | _rl_set_the_line (void) |
d60d9f65 SS |
685 | { |
686 | the_line = rl_line_buffer; | |
687 | } | |
688 | ||
5bdf8622 DJ |
689 | #if defined (READLINE_CALLBACKS) |
690 | _rl_keyseq_cxt * | |
cb41b9e7 | 691 | _rl_keyseq_cxt_alloc (void) |
5bdf8622 DJ |
692 | { |
693 | _rl_keyseq_cxt *cxt; | |
694 | ||
695 | cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt)); | |
696 | ||
697 | cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0; | |
698 | ||
699 | cxt->okey = 0; | |
700 | cxt->ocxt = _rl_kscxt; | |
701 | cxt->childval = 42; /* sentinel value */ | |
702 | ||
703 | return cxt; | |
704 | } | |
705 | ||
706 | void | |
cb41b9e7 | 707 | _rl_keyseq_cxt_dispose (_rl_keyseq_cxt *cxt) |
5bdf8622 | 708 | { |
cc88a640 | 709 | xfree (cxt); |
5bdf8622 DJ |
710 | } |
711 | ||
712 | void | |
cb41b9e7 | 713 | _rl_keyseq_chain_dispose (void) |
5bdf8622 DJ |
714 | { |
715 | _rl_keyseq_cxt *cxt; | |
716 | ||
717 | while (_rl_kscxt) | |
718 | { | |
719 | cxt = _rl_kscxt; | |
720 | _rl_kscxt = _rl_kscxt->ocxt; | |
721 | _rl_keyseq_cxt_dispose (cxt); | |
722 | } | |
723 | } | |
724 | #endif | |
725 | ||
726 | static int | |
cb41b9e7 | 727 | _rl_subseq_getchar (int key) |
5bdf8622 DJ |
728 | { |
729 | int k; | |
730 | ||
731 | if (key == ESC) | |
732 | RL_SETSTATE(RL_STATE_METANEXT); | |
733 | RL_SETSTATE(RL_STATE_MOREINPUT); | |
734 | k = rl_read_key (); | |
735 | RL_UNSETSTATE(RL_STATE_MOREINPUT); | |
736 | if (key == ESC) | |
737 | RL_UNSETSTATE(RL_STATE_METANEXT); | |
738 | ||
739 | return k; | |
740 | } | |
741 | ||
742 | #if defined (READLINE_CALLBACKS) | |
743 | int | |
cb41b9e7 | 744 | _rl_dispatch_callback (_rl_keyseq_cxt *cxt) |
5bdf8622 DJ |
745 | { |
746 | int nkey, r; | |
747 | ||
748 | /* For now */ | |
5bdf8622 DJ |
749 | /* The first time this context is used, we want to read input and dispatch |
750 | on it. When traversing the chain of contexts back `up', we want to use | |
751 | the value from the next context down. We're simulating recursion using | |
752 | a chain of contexts. */ | |
753 | if ((cxt->flags & KSEQ_DISPATCHED) == 0) | |
754 | { | |
755 | nkey = _rl_subseq_getchar (cxt->okey); | |
cc88a640 JK |
756 | if (nkey < 0) |
757 | { | |
758 | _rl_abort_internal (); | |
759 | return -1; | |
760 | } | |
5bdf8622 DJ |
761 | r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg); |
762 | cxt->flags |= KSEQ_DISPATCHED; | |
763 | } | |
764 | else | |
765 | r = cxt->childval; | |
5bdf8622 DJ |
766 | |
767 | /* For now */ | |
cc88a640 JK |
768 | if (r != -3) /* don't do this if we indicate there will be other matches */ |
769 | r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ)); | |
5bdf8622 | 770 | |
cc88a640 | 771 | RL_CHECK_SIGNALS (); |
775e241e TT |
772 | /* We only treat values < 0 specially to simulate recursion. */ |
773 | if (r >= 0 || (r == -1 && (cxt->flags & KSEQ_SUBSEQ) == 0)) /* success! or failure! */ | |
5bdf8622 DJ |
774 | { |
775 | _rl_keyseq_chain_dispose (); | |
776 | RL_UNSETSTATE (RL_STATE_MULTIKEY); | |
777 | return r; | |
778 | } | |
779 | ||
780 | if (r != -3) /* magic value that says we added to the chain */ | |
781 | _rl_kscxt = cxt->ocxt; | |
782 | if (_rl_kscxt) | |
783 | _rl_kscxt->childval = r; | |
784 | if (r != -3) | |
785 | _rl_keyseq_cxt_dispose (cxt); | |
786 | ||
787 | return r; | |
788 | } | |
789 | #endif /* READLINE_CALLBACKS */ | |
790 | ||
d60d9f65 SS |
791 | /* Do the command associated with KEY in MAP. |
792 | If the associated command is really a keymap, then read | |
793 | another key, and dispatch into that map. */ | |
794 | int | |
cb41b9e7 | 795 | _rl_dispatch (register int key, Keymap map) |
9255ee31 | 796 | { |
5bdf8622 | 797 | _rl_dispatching_keymap = map; |
9255ee31 EZ |
798 | return _rl_dispatch_subseq (key, map, 0); |
799 | } | |
800 | ||
801 | int | |
cb41b9e7 | 802 | _rl_dispatch_subseq (register int key, Keymap map, int got_subseq) |
d60d9f65 SS |
803 | { |
804 | int r, newkey; | |
805 | char *macro; | |
9255ee31 | 806 | rl_command_func_t *func; |
5bdf8622 DJ |
807 | #if defined (READLINE_CALLBACKS) |
808 | _rl_keyseq_cxt *cxt; | |
809 | #endif | |
d60d9f65 SS |
810 | |
811 | if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii) | |
812 | { | |
813 | if (map[ESC].type == ISKMAP) | |
814 | { | |
9255ee31 | 815 | if (RL_ISSTATE (RL_STATE_MACRODEF)) |
d60d9f65 | 816 | _rl_add_macro_char (ESC); |
775e241e TT |
817 | RESIZE_KEYSEQ_BUFFER (); |
818 | rl_executing_keyseq[rl_key_sequence_length++] = ESC; | |
d60d9f65 SS |
819 | map = FUNCTION_TO_KEYMAP (map, ESC); |
820 | key = UNMETA (key); | |
d60d9f65 SS |
821 | return (_rl_dispatch (key, map)); |
822 | } | |
823 | else | |
9255ee31 | 824 | rl_ding (); |
d60d9f65 SS |
825 | return 0; |
826 | } | |
827 | ||
9255ee31 | 828 | if (RL_ISSTATE (RL_STATE_MACRODEF)) |
d60d9f65 SS |
829 | _rl_add_macro_char (key); |
830 | ||
831 | r = 0; | |
832 | switch (map[key].type) | |
833 | { | |
834 | case ISFUNC: | |
835 | func = map[key].function; | |
9255ee31 | 836 | if (func) |
d60d9f65 SS |
837 | { |
838 | /* Special case rl_do_lowercase_version (). */ | |
839 | if (func == rl_do_lowercase_version) | |
775e241e TT |
840 | /* Should we do anything special if key == ANYOTHERKEY? */ |
841 | return (_rl_dispatch (_rl_to_lower ((unsigned char)key), map)); | |
d60d9f65 SS |
842 | |
843 | rl_executing_keymap = map; | |
775e241e TT |
844 | rl_executing_key = key; |
845 | ||
846 | RESIZE_KEYSEQ_BUFFER(); | |
847 | rl_executing_keyseq[rl_key_sequence_length++] = key; | |
848 | rl_executing_keyseq[rl_key_sequence_length] = '\0'; | |
d60d9f65 | 849 | |
d60d9f65 | 850 | rl_dispatching = 1; |
9255ee31 | 851 | RL_SETSTATE(RL_STATE_DISPATCHING); |
775e241e | 852 | r = (*func) (rl_numeric_arg * rl_arg_sign, key); |
9255ee31 | 853 | RL_UNSETSTATE(RL_STATE_DISPATCHING); |
d60d9f65 SS |
854 | rl_dispatching = 0; |
855 | ||
856 | /* If we have input pending, then the last command was a prefix | |
857 | command. Don't change the state of rl_last_func. Otherwise, | |
858 | remember the last command executed in this variable. */ | |
9255ee31 | 859 | if (rl_pending_input == 0 && map[key].function != rl_digit_argument) |
d60d9f65 | 860 | rl_last_func = map[key].function; |
cc88a640 JK |
861 | |
862 | RL_CHECK_SIGNALS (); | |
d60d9f65 | 863 | } |
9255ee31 EZ |
864 | else if (map[ANYOTHERKEY].function) |
865 | { | |
866 | /* OK, there's no function bound in this map, but there is a | |
867 | shadow function that was overridden when the current keymap | |
868 | was created. Return -2 to note that. */ | |
775e241e TT |
869 | if (RL_ISSTATE (RL_STATE_MACROINPUT)) |
870 | _rl_prev_macro_key (); | |
871 | else | |
872 | _rl_unget_char (key); | |
9255ee31 EZ |
873 | return -2; |
874 | } | |
875 | else if (got_subseq) | |
876 | { | |
877 | /* Return -1 to note that we're in a subsequence, but we don't | |
878 | have a matching key, nor was one overridden. This means | |
879 | we need to back up the recursion chain and find the last | |
880 | subsequence that is bound to a function. */ | |
775e241e TT |
881 | if (RL_ISSTATE (RL_STATE_MACROINPUT)) |
882 | _rl_prev_macro_key (); | |
883 | else | |
884 | _rl_unget_char (key); | |
9255ee31 EZ |
885 | return -1; |
886 | } | |
d60d9f65 SS |
887 | else |
888 | { | |
5bdf8622 DJ |
889 | #if defined (READLINE_CALLBACKS) |
890 | RL_UNSETSTATE (RL_STATE_MULTIKEY); | |
891 | _rl_keyseq_chain_dispose (); | |
892 | #endif | |
d60d9f65 SS |
893 | _rl_abort_internal (); |
894 | return -1; | |
895 | } | |
896 | break; | |
897 | ||
898 | case ISKMAP: | |
9255ee31 | 899 | if (map[key].function != 0) |
d60d9f65 | 900 | { |
9255ee31 EZ |
901 | #if defined (VI_MODE) |
902 | /* The only way this test will be true is if a subsequence has been | |
903 | bound starting with ESC, generally the arrow keys. What we do is | |
904 | check whether there's input in the queue, which there generally | |
905 | will be if an arrow key has been pressed, and, if there's not, | |
906 | just dispatch to (what we assume is) rl_vi_movement_mode right | |
775e241e TT |
907 | away. This is essentially an input test with a zero timeout (by |
908 | default) or a timeout determined by the value of `keyseq-timeout' */ | |
909 | /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued | |
910 | takes microseconds, so multiply by 1000 */ | |
911 | if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap && | |
912 | (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) && | |
913 | _rl_pushed_input_available () == 0 && | |
914 | _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0) | |
9255ee31 | 915 | return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key))); |
cb41b9e7 TT |
916 | /* This is a very specific test. It can possibly be generalized in |
917 | the future, but for now it handles a specific case of ESC being | |
918 | the last character in a keyboard macro. */ | |
919 | if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap && | |
920 | (RL_ISSTATE (RL_STATE_INPUTPENDING) == 0) && | |
921 | (RL_ISSTATE (RL_STATE_MACROINPUT) && _rl_peek_macro_key () == 0) && | |
922 | _rl_pushed_input_available () == 0 && | |
923 | _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0) | |
924 | return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key))); | |
9255ee31 EZ |
925 | #endif |
926 | ||
775e241e TT |
927 | RESIZE_KEYSEQ_BUFFER (); |
928 | rl_executing_keyseq[rl_key_sequence_length++] = key; | |
5bdf8622 | 929 | _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key); |
9255ee31 | 930 | |
5bdf8622 DJ |
931 | /* Allocate new context here. Use linked contexts (linked through |
932 | cxt->ocxt) to simulate recursion */ | |
933 | #if defined (READLINE_CALLBACKS) | |
775e241e TT |
934 | # if defined (VI_MODE) |
935 | /* If we're redoing a vi mode command and we know there is a shadowed | |
936 | function corresponding to this key, just call it -- all the redoable | |
937 | vi mode commands already have all the input they need, and rl_vi_redo | |
938 | assumes that one call to rl_dispatch is sufficient to complete the | |
939 | command. */ | |
940 | if (_rl_vi_redoing && RL_ISSTATE (RL_STATE_CALLBACK) && | |
941 | map[ANYOTHERKEY].function != 0) | |
942 | return (_rl_subseq_result (-2, map, key, got_subseq)); | |
943 | # endif | |
5bdf8622 DJ |
944 | if (RL_ISSTATE (RL_STATE_CALLBACK)) |
945 | { | |
946 | /* Return 0 only the first time, to indicate success to | |
947 | _rl_callback_read_char. The rest of the time, we're called | |
cc88a640 | 948 | from _rl_dispatch_callback, so we return -3 to indicate |
5bdf8622 DJ |
949 | special handling is necessary. */ |
950 | r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0; | |
951 | cxt = _rl_keyseq_cxt_alloc (); | |
952 | ||
953 | if (got_subseq) | |
954 | cxt->flags |= KSEQ_SUBSEQ; | |
955 | cxt->okey = key; | |
956 | cxt->oldmap = map; | |
957 | cxt->dmap = _rl_dispatching_keymap; | |
958 | cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function; | |
959 | ||
960 | RL_SETSTATE (RL_STATE_MULTIKEY); | |
961 | _rl_kscxt = cxt; | |
962 | ||
963 | return r; /* don't indicate immediate success */ | |
964 | } | |
965 | #endif | |
9255ee31 | 966 | |
775e241e TT |
967 | /* Tentative inter-character timeout for potential multi-key |
968 | sequences? If no input within timeout, abort sequence and | |
969 | act as if we got non-matching input. */ | |
970 | /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued | |
971 | takes microseconds, so multiply by 1000 */ | |
972 | if (_rl_keyseq_timeout > 0 && | |
973 | (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) && | |
974 | _rl_pushed_input_available () == 0 && | |
975 | _rl_dispatching_keymap[ANYOTHERKEY].function && | |
976 | _rl_input_queued (_rl_keyseq_timeout*1000) == 0) | |
977 | return (_rl_subseq_result (-2, map, key, got_subseq)); | |
978 | ||
5bdf8622 | 979 | newkey = _rl_subseq_getchar (key); |
9255ee31 EZ |
980 | if (newkey < 0) |
981 | { | |
982 | _rl_abort_internal (); | |
983 | return -1; | |
984 | } | |
985 | ||
5bdf8622 DJ |
986 | r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function); |
987 | return _rl_subseq_result (r, map, key, got_subseq); | |
d60d9f65 SS |
988 | } |
989 | else | |
990 | { | |
775e241e | 991 | _rl_abort_internal (); /* XXX */ |
d60d9f65 SS |
992 | return -1; |
993 | } | |
994 | break; | |
995 | ||
996 | case ISMACR: | |
9255ee31 | 997 | if (map[key].function != 0) |
d60d9f65 | 998 | { |
775e241e | 999 | rl_executing_keyseq[rl_key_sequence_length] = '\0'; |
d60d9f65 SS |
1000 | macro = savestring ((char *)map[key].function); |
1001 | _rl_with_macro_input (macro); | |
1002 | return 0; | |
1003 | } | |
1004 | break; | |
1005 | } | |
775e241e | 1006 | |
d60d9f65 SS |
1007 | #if defined (VI_MODE) |
1008 | if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap && | |
5af408ce | 1009 | key != ANYOTHERKEY && |
775e241e | 1010 | _rl_dispatching_keymap == vi_movement_keymap && |
d60d9f65 SS |
1011 | _rl_vi_textmod_command (key)) |
1012 | _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign); | |
1013 | #endif | |
5bdf8622 | 1014 | |
d60d9f65 SS |
1015 | return (r); |
1016 | } | |
1017 | ||
5bdf8622 | 1018 | static int |
cb41b9e7 | 1019 | _rl_subseq_result (int r, Keymap map, int key, int got_subseq) |
5bdf8622 DJ |
1020 | { |
1021 | Keymap m; | |
1022 | int type, nt; | |
1023 | rl_command_func_t *func, *nf; | |
cc88a640 | 1024 | |
5bdf8622 DJ |
1025 | if (r == -2) |
1026 | /* We didn't match anything, and the keymap we're indexed into | |
1027 | shadowed a function previously bound to that prefix. Call | |
1028 | the function. The recursive call to _rl_dispatch_subseq has | |
1029 | already taken care of pushing any necessary input back onto | |
1030 | the input queue with _rl_unget_char. */ | |
1031 | { | |
1032 | m = _rl_dispatching_keymap; | |
1033 | type = m[ANYOTHERKEY].type; | |
1034 | func = m[ANYOTHERKEY].function; | |
1035 | if (type == ISFUNC && func == rl_do_lowercase_version) | |
775e241e TT |
1036 | r = _rl_dispatch (_rl_to_lower ((unsigned char)key), map); |
1037 | else if (type == ISFUNC) | |
5bdf8622 | 1038 | { |
775e241e TT |
1039 | /* If we shadowed a function, whatever it is, we somehow need a |
1040 | keymap with map[key].func == shadowed-function. | |
1041 | Let's use this one. Then we can dispatch using the original | |
1042 | key, since there are commands (e.g., in vi mode) for which it | |
1043 | matters. */ | |
5bdf8622 DJ |
1044 | nt = m[key].type; |
1045 | nf = m[key].function; | |
1046 | ||
1047 | m[key].type = type; | |
1048 | m[key].function = func; | |
775e241e TT |
1049 | /* Don't change _rl_dispatching_keymap, set it here */ |
1050 | _rl_dispatching_keymap = map; /* previous map */ | |
1051 | r = _rl_dispatch_subseq (key, m, 0); | |
5bdf8622 DJ |
1052 | m[key].type = nt; |
1053 | m[key].function = nf; | |
1054 | } | |
1055 | else | |
775e241e | 1056 | /* We probably shadowed a keymap, so keep going. */ |
5bdf8622 DJ |
1057 | r = _rl_dispatch (ANYOTHERKEY, m); |
1058 | } | |
775e241e | 1059 | else if (r < 0 && map[ANYOTHERKEY].function) |
5bdf8622 DJ |
1060 | { |
1061 | /* We didn't match (r is probably -1), so return something to | |
1062 | tell the caller that it should try ANYOTHERKEY for an | |
1063 | overridden function. */ | |
775e241e TT |
1064 | if (RL_ISSTATE (RL_STATE_MACROINPUT)) |
1065 | _rl_prev_macro_key (); | |
1066 | else | |
1067 | _rl_unget_char (key); | |
5bdf8622 DJ |
1068 | _rl_dispatching_keymap = map; |
1069 | return -2; | |
1070 | } | |
775e241e | 1071 | else if (r < 0 && got_subseq) /* XXX */ |
5bdf8622 DJ |
1072 | { |
1073 | /* OK, back up the chain. */ | |
775e241e TT |
1074 | if (RL_ISSTATE (RL_STATE_MACROINPUT)) |
1075 | _rl_prev_macro_key (); | |
1076 | else | |
1077 | _rl_unget_char (key); | |
5bdf8622 DJ |
1078 | _rl_dispatching_keymap = map; |
1079 | return -1; | |
1080 | } | |
1081 | ||
1082 | return r; | |
1083 | } | |
1084 | ||
d60d9f65 SS |
1085 | /* **************************************************************** */ |
1086 | /* */ | |
1087 | /* Initializations */ | |
1088 | /* */ | |
1089 | /* **************************************************************** */ | |
1090 | ||
1091 | /* Initialize readline (and terminal if not already). */ | |
1092 | int | |
cb41b9e7 | 1093 | rl_initialize (void) |
d60d9f65 SS |
1094 | { |
1095 | /* If we have never been called before, initialize the | |
1096 | terminal and data structures. */ | |
cb41b9e7 | 1097 | if (rl_initialized == 0) |
d60d9f65 | 1098 | { |
9255ee31 | 1099 | RL_SETSTATE(RL_STATE_INITIALIZING); |
d60d9f65 | 1100 | readline_initialize_everything (); |
9255ee31 | 1101 | RL_UNSETSTATE(RL_STATE_INITIALIZING); |
d60d9f65 | 1102 | rl_initialized++; |
9255ee31 | 1103 | RL_SETSTATE(RL_STATE_INITIALIZED); |
d60d9f65 | 1104 | } |
cb41b9e7 TT |
1105 | else |
1106 | (void)_rl_init_locale (); /* check current locale */ | |
d60d9f65 | 1107 | |
775e241e | 1108 | /* Initialize the current line information. */ |
d60d9f65 SS |
1109 | _rl_init_line_state (); |
1110 | ||
1111 | /* We aren't done yet. We haven't even gotten started yet! */ | |
1112 | rl_done = 0; | |
9255ee31 | 1113 | RL_UNSETSTATE(RL_STATE_DONE); |
d60d9f65 SS |
1114 | |
1115 | /* Tell the history routines what is going on. */ | |
9255ee31 | 1116 | _rl_start_using_history (); |
d60d9f65 SS |
1117 | |
1118 | /* Make the display buffer match the state of the line. */ | |
1119 | rl_reset_line_state (); | |
1120 | ||
1121 | /* No such function typed yet. */ | |
9255ee31 | 1122 | rl_last_func = (rl_command_func_t *)NULL; |
d60d9f65 SS |
1123 | |
1124 | /* Parsing of key-bindings begins in an enabled state. */ | |
1125 | _rl_parsing_conditionalized_out = 0; | |
1126 | ||
1127 | #if defined (VI_MODE) | |
1128 | if (rl_editing_mode == vi_mode) | |
1129 | _rl_vi_initialize_line (); | |
1130 | #endif | |
1131 | ||
9255ee31 EZ |
1132 | /* Each line starts in insert mode (the default). */ |
1133 | _rl_set_insert_mode (RL_IM_DEFAULT, 1); | |
1134 | ||
d60d9f65 SS |
1135 | return 0; |
1136 | } | |
1137 | ||
1b17e766 | 1138 | #if 0 |
d60d9f65 SS |
1139 | #if defined (__EMX__) |
1140 | static void | |
cb41b9e7 | 1141 | _emx_build_environ (void) |
d60d9f65 SS |
1142 | { |
1143 | TIB *tibp; | |
1144 | PIB *pibp; | |
1145 | char *t, **tp; | |
1146 | int c; | |
1147 | ||
1148 | DosGetInfoBlocks (&tibp, &pibp); | |
1149 | t = pibp->pib_pchenv; | |
1150 | for (c = 1; *t; c++) | |
1151 | t += strlen (t) + 1; | |
1152 | tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *)); | |
1153 | t = pibp->pib_pchenv; | |
1154 | while (*t) | |
1155 | { | |
1156 | *tp++ = t; | |
1157 | t += strlen (t) + 1; | |
1158 | } | |
1159 | *tp = 0; | |
1160 | } | |
1161 | #endif /* __EMX__ */ | |
1b17e766 | 1162 | #endif |
d60d9f65 SS |
1163 | |
1164 | /* Initialize the entire state of the world. */ | |
1165 | static void | |
cb41b9e7 | 1166 | readline_initialize_everything (void) |
d60d9f65 | 1167 | { |
1b17e766 | 1168 | #if 0 |
d60d9f65 SS |
1169 | #if defined (__EMX__) |
1170 | if (environ == 0) | |
1171 | _emx_build_environ (); | |
1b17e766 | 1172 | #endif |
d60d9f65 SS |
1173 | #endif |
1174 | ||
9255ee31 EZ |
1175 | #if 0 |
1176 | /* Find out if we are running in Emacs -- UNUSED. */ | |
1177 | running_in_emacs = sh_get_env_value ("EMACS") != (char *)0; | |
1178 | #endif | |
d60d9f65 SS |
1179 | |
1180 | /* Set up input and output if they are not already set up. */ | |
1181 | if (!rl_instream) | |
1182 | rl_instream = stdin; | |
1183 | ||
1184 | if (!rl_outstream) | |
1185 | rl_outstream = stdout; | |
1186 | ||
1187 | /* Bind _rl_in_stream and _rl_out_stream immediately. These values | |
1188 | may change, but they may also be used before readline_internal () | |
1189 | is called. */ | |
1190 | _rl_in_stream = rl_instream; | |
1191 | _rl_out_stream = rl_outstream; | |
1192 | ||
1193 | /* Allocate data structures. */ | |
1194 | if (rl_line_buffer == 0) | |
9255ee31 | 1195 | rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE); |
d60d9f65 SS |
1196 | |
1197 | /* Initialize the terminal interface. */ | |
9255ee31 EZ |
1198 | if (rl_terminal_name == 0) |
1199 | rl_terminal_name = sh_get_env_value ("TERM"); | |
1200 | _rl_init_terminal_io (rl_terminal_name); | |
d60d9f65 | 1201 | |
d60d9f65 SS |
1202 | /* Bind tty characters to readline functions. */ |
1203 | readline_default_bindings (); | |
d60d9f65 SS |
1204 | |
1205 | /* Initialize the function names. */ | |
1206 | rl_initialize_funmap (); | |
1207 | ||
1208 | /* Decide whether we should automatically go into eight-bit mode. */ | |
1209 | _rl_init_eightbit (); | |
1210 | ||
1211 | /* Read in the init file. */ | |
1212 | rl_read_init_file ((char *)NULL); | |
1213 | ||
1214 | /* XXX */ | |
1215 | if (_rl_horizontal_scroll_mode && _rl_term_autowrap) | |
1216 | { | |
9255ee31 EZ |
1217 | _rl_screenwidth--; |
1218 | _rl_screenchars -= _rl_screenheight; | |
d60d9f65 SS |
1219 | } |
1220 | ||
1221 | /* Override the effect of any `set keymap' assignments in the | |
1222 | inputrc file. */ | |
1223 | rl_set_keymap_from_edit_mode (); | |
1224 | ||
1225 | /* Try to bind a common arrow key prefix, if not already bound. */ | |
1226 | bind_arrow_keys (); | |
1227 | ||
775e241e TT |
1228 | /* Bind the bracketed paste prefix assuming that the user will enable |
1229 | it on terminals that support it. */ | |
1230 | bind_bracketed_paste_prefix (); | |
d60d9f65 SS |
1231 | |
1232 | /* If the completion parser's default word break characters haven't | |
1233 | been set yet, then do so now. */ | |
1234 | if (rl_completer_word_break_characters == (char *)NULL) | |
5bdf8622 | 1235 | rl_completer_word_break_characters = (char *)rl_basic_word_break_characters; |
775e241e TT |
1236 | |
1237 | #if defined (COLOR_SUPPORT) | |
1238 | if (_rl_colored_stats || _rl_colored_completion_prefix) | |
1239 | _rl_parse_colors (); | |
1240 | #endif | |
1241 | ||
1242 | rl_executing_keyseq = malloc (_rl_executing_keyseq_size = 16); | |
1243 | if (rl_executing_keyseq) | |
1244 | rl_executing_keyseq[0] = '\0'; | |
d60d9f65 SS |
1245 | } |
1246 | ||
1247 | /* If this system allows us to look at the values of the regular | |
1248 | input editing characters, then bind them to their readline | |
1249 | equivalents, iff the characters are not bound to keymaps. */ | |
1250 | static void | |
cb41b9e7 | 1251 | readline_default_bindings (void) |
d60d9f65 | 1252 | { |
5bdf8622 DJ |
1253 | if (_rl_bind_stty_chars) |
1254 | rl_tty_set_default_bindings (_rl_keymap); | |
1255 | } | |
1256 | ||
1257 | /* Reset the default bindings for the terminal special characters we're | |
1258 | interested in back to rl_insert and read the new ones. */ | |
1259 | static void | |
cb41b9e7 | 1260 | reset_default_bindings (void) |
5bdf8622 DJ |
1261 | { |
1262 | if (_rl_bind_stty_chars) | |
1263 | { | |
1264 | rl_tty_unset_default_bindings (_rl_keymap); | |
1265 | rl_tty_set_default_bindings (_rl_keymap); | |
1266 | } | |
d60d9f65 SS |
1267 | } |
1268 | ||
9255ee31 | 1269 | /* Bind some common arrow key sequences in MAP. */ |
d60d9f65 | 1270 | static void |
cb41b9e7 | 1271 | bind_arrow_keys_internal (Keymap map) |
d60d9f65 | 1272 | { |
9255ee31 EZ |
1273 | Keymap xkeymap; |
1274 | ||
1275 | xkeymap = _rl_keymap; | |
1276 | _rl_keymap = map; | |
d60d9f65 | 1277 | |
1b17e766 | 1278 | #if defined (__MSDOS__) |
5bdf8622 DJ |
1279 | rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history); |
1280 | rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char); | |
1281 | rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char); | |
1282 | rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history); | |
1b17e766 | 1283 | #endif |
d60d9f65 | 1284 | |
5bdf8622 DJ |
1285 | rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history); |
1286 | rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history); | |
1287 | rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char); | |
1288 | rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char); | |
1289 | rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line); | |
1290 | rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line); | |
1291 | ||
1292 | rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history); | |
1293 | rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history); | |
1294 | rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char); | |
1295 | rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char); | |
1296 | rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line); | |
1297 | rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line); | |
1298 | ||
cb41b9e7 TT |
1299 | /* Key bindings for control-arrow keys */ |
1300 | rl_bind_keyseq_if_unbound ("\033[1;5C", rl_forward_word); | |
1301 | rl_bind_keyseq_if_unbound ("\033[1;5D", rl_backward_word); | |
1302 | rl_bind_keyseq_if_unbound ("\033[3;5~", rl_kill_word); | |
1303 | ||
1304 | /* Key bindings for alt-arrow keys */ | |
1305 | rl_bind_keyseq_if_unbound ("\033[1;3C", rl_forward_word); | |
1306 | rl_bind_keyseq_if_unbound ("\033[1;3D", rl_backward_word); | |
1307 | ||
5bdf8622 DJ |
1308 | #if defined (__MINGW32__) |
1309 | rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history); | |
1310 | rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history); | |
1311 | rl_bind_keyseq_if_unbound ("\340M", rl_forward_char); | |
1312 | rl_bind_keyseq_if_unbound ("\340K", rl_backward_char); | |
cd5f70ff PM |
1313 | rl_bind_keyseq_if_unbound ("\340G", rl_beg_of_line); |
1314 | rl_bind_keyseq_if_unbound ("\340O", rl_end_of_line); | |
1315 | rl_bind_keyseq_if_unbound ("\340S", rl_delete); | |
1316 | rl_bind_keyseq_if_unbound ("\340R", rl_overwrite_mode); | |
775e241e TT |
1317 | |
1318 | /* These may or may not work because of the embedded NUL. */ | |
1319 | rl_bind_keyseq_if_unbound ("\\000H", rl_get_previous_history); | |
1320 | rl_bind_keyseq_if_unbound ("\\000P", rl_get_next_history); | |
1321 | rl_bind_keyseq_if_unbound ("\\000M", rl_forward_char); | |
1322 | rl_bind_keyseq_if_unbound ("\\000K", rl_backward_char); | |
1323 | rl_bind_keyseq_if_unbound ("\\000G", rl_beg_of_line); | |
1324 | rl_bind_keyseq_if_unbound ("\\000O", rl_end_of_line); | |
1325 | rl_bind_keyseq_if_unbound ("\\000S", rl_delete); | |
1326 | rl_bind_keyseq_if_unbound ("\\000R", rl_overwrite_mode); | |
fd8be987 MM |
1327 | #endif |
1328 | ||
9255ee31 | 1329 | _rl_keymap = xkeymap; |
d60d9f65 SS |
1330 | } |
1331 | ||
9255ee31 | 1332 | /* Try and bind the common arrow key prefixes after giving termcap and |
d60d9f65 SS |
1333 | the inputrc file a chance to bind them and create `real' keymaps |
1334 | for the arrow key prefix. */ | |
1335 | static void | |
cb41b9e7 | 1336 | bind_arrow_keys (void) |
d60d9f65 | 1337 | { |
9255ee31 | 1338 | bind_arrow_keys_internal (emacs_standard_keymap); |
d60d9f65 SS |
1339 | |
1340 | #if defined (VI_MODE) | |
9255ee31 | 1341 | bind_arrow_keys_internal (vi_movement_keymap); |
cc88a640 JK |
1342 | /* Unbind vi_movement_keymap[ESC] to allow users to repeatedly hit ESC |
1343 | in vi command mode while still allowing the arrow keys to work. */ | |
1344 | if (vi_movement_keymap[ESC].type == ISKMAP) | |
1345 | rl_bind_keyseq_in_map ("\033", (rl_command_func_t *)NULL, vi_movement_keymap); | |
9255ee31 | 1346 | bind_arrow_keys_internal (vi_insertion_keymap); |
d60d9f65 | 1347 | #endif |
d60d9f65 SS |
1348 | } |
1349 | ||
775e241e | 1350 | static void |
cb41b9e7 | 1351 | bind_bracketed_paste_prefix (void) |
775e241e TT |
1352 | { |
1353 | Keymap xkeymap; | |
1354 | ||
1355 | xkeymap = _rl_keymap; | |
1356 | ||
1357 | _rl_keymap = emacs_standard_keymap; | |
1358 | rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin); | |
1359 | ||
1360 | _rl_keymap = vi_insertion_keymap; | |
1361 | rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin); | |
1362 | ||
1363 | _rl_keymap = xkeymap; | |
1364 | } | |
1365 | ||
d60d9f65 SS |
1366 | /* **************************************************************** */ |
1367 | /* */ | |
9255ee31 | 1368 | /* Saving and Restoring Readline's state */ |
d60d9f65 SS |
1369 | /* */ |
1370 | /* **************************************************************** */ | |
1371 | ||
d60d9f65 | 1372 | int |
cb41b9e7 | 1373 | rl_save_state (struct readline_state *sp) |
d60d9f65 | 1374 | { |
9255ee31 EZ |
1375 | if (sp == 0) |
1376 | return -1; | |
d60d9f65 | 1377 | |
9255ee31 EZ |
1378 | sp->point = rl_point; |
1379 | sp->end = rl_end; | |
1380 | sp->mark = rl_mark; | |
1381 | sp->buffer = rl_line_buffer; | |
1382 | sp->buflen = rl_line_buffer_len; | |
1383 | sp->ul = rl_undo_list; | |
1384 | sp->prompt = rl_prompt; | |
1385 | ||
1386 | sp->rlstate = rl_readline_state; | |
1387 | sp->done = rl_done; | |
1388 | sp->kmap = _rl_keymap; | |
1389 | ||
1390 | sp->lastfunc = rl_last_func; | |
1391 | sp->insmode = rl_insert_mode; | |
1392 | sp->edmode = rl_editing_mode; | |
775e241e | 1393 | sp->kseq = rl_executing_keyseq; |
9255ee31 EZ |
1394 | sp->kseqlen = rl_key_sequence_length; |
1395 | sp->inf = rl_instream; | |
1396 | sp->outf = rl_outstream; | |
1397 | sp->pendingin = rl_pending_input; | |
1398 | sp->macro = rl_executing_macro; | |
1399 | ||
1400 | sp->catchsigs = rl_catch_signals; | |
1401 | sp->catchsigwinch = rl_catch_sigwinch; | |
d60d9f65 | 1402 | |
775e241e TT |
1403 | sp->entryfunc = rl_completion_entry_function; |
1404 | sp->menuentryfunc = rl_menu_completion_entry_function; | |
1405 | sp->ignorefunc = rl_ignore_some_completions_function; | |
1406 | sp->attemptfunc = rl_attempted_completion_function; | |
1407 | sp->wordbreakchars = rl_completer_word_break_characters; | |
1408 | ||
9255ee31 | 1409 | return (0); |
d60d9f65 SS |
1410 | } |
1411 | ||
d60d9f65 | 1412 | int |
cb41b9e7 | 1413 | rl_restore_state (struct readline_state *sp) |
d60d9f65 | 1414 | { |
9255ee31 EZ |
1415 | if (sp == 0) |
1416 | return -1; | |
d60d9f65 | 1417 | |
9255ee31 EZ |
1418 | rl_point = sp->point; |
1419 | rl_end = sp->end; | |
1420 | rl_mark = sp->mark; | |
1421 | the_line = rl_line_buffer = sp->buffer; | |
1422 | rl_line_buffer_len = sp->buflen; | |
1423 | rl_undo_list = sp->ul; | |
1424 | rl_prompt = sp->prompt; | |
1425 | ||
1426 | rl_readline_state = sp->rlstate; | |
1427 | rl_done = sp->done; | |
1428 | _rl_keymap = sp->kmap; | |
1429 | ||
1430 | rl_last_func = sp->lastfunc; | |
1431 | rl_insert_mode = sp->insmode; | |
1432 | rl_editing_mode = sp->edmode; | |
775e241e | 1433 | rl_executing_keyseq = sp->kseq; |
9255ee31 EZ |
1434 | rl_key_sequence_length = sp->kseqlen; |
1435 | rl_instream = sp->inf; | |
1436 | rl_outstream = sp->outf; | |
1437 | rl_pending_input = sp->pendingin; | |
1438 | rl_executing_macro = sp->macro; | |
1439 | ||
1440 | rl_catch_signals = sp->catchsigs; | |
1441 | rl_catch_sigwinch = sp->catchsigwinch; | |
d60d9f65 | 1442 | |
775e241e TT |
1443 | rl_completion_entry_function = sp->entryfunc; |
1444 | rl_menu_completion_entry_function = sp->menuentryfunc; | |
1445 | rl_ignore_some_completions_function = sp->ignorefunc; | |
1446 | rl_attempted_completion_function = sp->attemptfunc; | |
1447 | rl_completer_word_break_characters = sp->wordbreakchars; | |
1448 | ||
d60d9f65 SS |
1449 | return (0); |
1450 | } |