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