1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987-2020 Free Software Foundation, Inc.
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.
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
12 (at your option) any later version.
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
17 GNU General Public License for more details.
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/>.
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
30 #include "posixstat.h"
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
58 /* System-specific feature definitions and include files. */
63 # define INCL_DOSPROCESS
67 /* Some standard library routines. */
71 #include "rlprivate.h"
75 #ifndef RL_LIBRARY_VERSION
76 # define RL_LIBRARY_VERSION "8.0"
79 #ifndef RL_READLINE_VERSION
80 # define RL_READLINE_VERSION 0x0800
83 extern void _rl_free_history_entry
PARAMS((HIST_ENTRY
*));
85 #if defined (COLOR_SUPPORT)
86 extern void _rl_parse_colors
PARAMS((void)); /* XXX */
90 /* Forward declarations used in this file. */
91 static char *readline_internal
PARAMS((void));
92 static void readline_initialize_everything
PARAMS((void));
94 static void bind_arrow_keys_internal
PARAMS((Keymap
));
95 static void bind_arrow_keys
PARAMS((void));
97 static void bind_bracketed_paste_prefix
PARAMS((void));
99 static void readline_default_bindings
PARAMS((void));
100 static void reset_default_bindings
PARAMS((void));
102 static int _rl_subseq_result
PARAMS((int, Keymap
, int, int));
103 static int _rl_subseq_getchar
PARAMS((int));
105 /* **************************************************************** */
107 /* Line editing input utility */
109 /* **************************************************************** */
111 const char *rl_library_version
= RL_LIBRARY_VERSION
;
113 int rl_readline_version
= RL_READLINE_VERSION
;
115 /* True if this is `real' readline as opposed to some stub substitute. */
116 int rl_gnu_readline_p
= 1;
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
;
122 /* The current style of editing. */
123 int rl_editing_mode
= emacs_mode
;
125 /* The current insert mode: input (the default) or overwrite */
126 int rl_insert_mode
= RL_IM_DEFAULT
;
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. */
133 /* Non-zero if the previous command was a kill command. */
134 int _rl_last_command_was_kill
= 0;
136 /* The current value of the numeric argument specified by the user. */
137 int rl_numeric_arg
= 1;
139 /* Non-zero if an argument was typed. */
140 int rl_explicit_arg
= 0;
142 /* Temporary value used while generating the argument. */
145 /* Non-zero means we have been called at least once before. */
146 static int rl_initialized
;
149 /* If non-zero, this program is running in an EMACS buffer. */
150 static int running_in_emacs
;
153 /* Flags word encapsulating the current readline state. */
154 unsigned long rl_readline_state
= RL_STATE_NONE
;
156 /* The current offset in the current input line. */
159 /* Mark in the current input line. */
162 /* Length of the current input line. */
165 /* Make this non-zero to return the current input_line. */
168 /* The last function executed by readline. */
169 rl_command_func_t
*rl_last_func
= (rl_command_func_t
*)NULL
;
171 /* Top level environment for readline_internal (). */
172 procenv_t _rl_top_level
;
174 /* The streams we interact with. */
175 FILE *_rl_in_stream
, *_rl_out_stream
;
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
;
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. */
185 int _rl_echoing_p
= 0;
187 /* Current prompt. */
188 char *rl_prompt
= (char *)NULL
;
189 int rl_visible_prompt_length
= 0;
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;
195 /* The number of characters read in order to type this complete command. */
196 int rl_key_sequence_length
= 0;
198 /* If non-zero, then this is the address of a function to call just
199 before readline_internal_setup () prints the first prompt. */
200 rl_hook_func_t
*rl_startup_hook
= (rl_hook_func_t
*)NULL
;
202 /* Any readline function can set this and have it run just before the user's
204 rl_hook_func_t
*_rl_internal_startup_hook
= (rl_hook_func_t
*)NULL
;
206 /* If non-zero, this is the address of a function to call just before
207 readline_internal_setup () returns and readline_internal starts
208 reading input characters. */
209 rl_hook_func_t
*rl_pre_input_hook
= (rl_hook_func_t
*)NULL
;
211 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
212 static char *the_line
;
214 /* The character that can generate an EOF. Really read from
215 the terminal driver... just defaulted here. */
216 int _rl_eof_char
= CTRL ('D');
218 /* Non-zero makes this the next keystroke to read. */
219 int rl_pending_input
= 0;
221 /* If non-zero when readline_internal returns, it means we found EOF */
222 int _rl_eof_found
= 0;
224 /* Pointer to a useful terminal name. */
225 const char *rl_terminal_name
= (const char *)NULL
;
227 /* Non-zero means to always use horizontal scrolling in line display. */
228 int _rl_horizontal_scroll_mode
= 0;
230 /* Non-zero means to display an asterisk at the starts of history lines
231 which have been modified. */
232 int _rl_mark_modified_lines
= 0;
234 /* The style of `bell' notification preferred. This can be set to NO_BELL,
235 AUDIBLE_BELL, or VISIBLE_BELL. */
236 int _rl_bell_preference
= AUDIBLE_BELL
;
238 /* String inserted into the line by rl_insert_comment (). */
239 char *_rl_comment_begin
;
241 /* Keymap holding the function currently being executed. */
242 Keymap rl_executing_keymap
;
244 /* Keymap we're currently using to dispatch. */
245 Keymap _rl_dispatching_keymap
;
247 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
248 int rl_erase_empty_line
= 0;
250 /* Non-zero means to read only this many characters rather than up to a
251 character bound to accept-line. */
252 int rl_num_chars_to_read
= 0;
254 /* Line buffer and maintenance. */
255 char *rl_line_buffer
= (char *)NULL
;
256 int rl_line_buffer_len
= 0;
258 /* Key sequence `contexts' */
259 _rl_keyseq_cxt
*_rl_kscxt
= 0;
261 int rl_executing_key
;
262 char *rl_executing_keyseq
= 0;
263 int _rl_executing_keyseq_size
= 0;
265 struct _rl_cmd _rl_pending_command
;
266 struct _rl_cmd
*_rl_command_to_execute
= (struct _rl_cmd
*)NULL
;
268 /* Timeout (specified in milliseconds) when reading characters making up an
269 ambiguous multiple-key sequence */
270 int _rl_keyseq_timeout
= 500;
272 #define RESIZE_KEYSEQ_BUFFER() \
275 if (rl_key_sequence_length + 2 >= _rl_executing_keyseq_size) \
277 _rl_executing_keyseq_size += 16; \
278 rl_executing_keyseq = xrealloc (rl_executing_keyseq, _rl_executing_keyseq_size); \
283 /* Forward declarations used by the display, termcap, and history code. */
285 /* **************************************************************** */
287 /* `Forward' declarations */
289 /* **************************************************************** */
291 /* Non-zero means do not parse any lines other than comments and
292 parser directives. */
293 unsigned char _rl_parsing_conditionalized_out
= 0;
295 /* Non-zero means to convert characters with the meta bit set to
296 escape-prefixed characters so we can indirect through
297 emacs_meta_keymap or vi_escape_keymap. */
298 int _rl_convert_meta_chars_to_ascii
= 1;
300 /* Non-zero means to output characters with the meta bit set directly
301 rather than as a meta-prefixed escape sequence. */
302 int _rl_output_meta_chars
= 0;
304 /* Non-zero means to look at the termios special characters and bind
305 them to equivalent readline functions at startup. */
306 int _rl_bind_stty_chars
= 1;
308 /* Non-zero means to go through the history list at every newline (or
309 whenever rl_done is set and readline returns) and revert each line to
310 its initial state. */
311 int _rl_revert_all_at_newline
= 0;
313 /* Non-zero means to honor the termios ECHOCTL bit and echo control
314 characters corresponding to keyboard-generated signals. */
315 int _rl_echo_control_chars
= 1;
317 /* Non-zero means to prefix the displayed prompt with a character indicating
318 the editing mode: @ for emacs, : for vi-command, + for vi-insert. */
319 int _rl_show_mode_in_prompt
= 0;
321 /* Non-zero means to attempt to put the terminal in `bracketed paste mode',
322 where it will prefix pasted text with an escape sequence and send
323 another to mark the end of the paste. */
324 int _rl_enable_bracketed_paste
= BRACKETED_PASTE_DEFAULT
;
325 int _rl_enable_active_region
= BRACKETED_PASTE_DEFAULT
;
327 /* **************************************************************** */
329 /* Top Level Functions */
331 /* **************************************************************** */
333 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
334 int _rl_meta_flag
= 0; /* Forward declaration */
336 /* Set up the prompt and expand it. Called from readline() and
337 rl_callback_handler_install (). */
339 rl_set_prompt (const char *prompt
)
342 rl_prompt
= prompt
? savestring (prompt
) : (char *)NULL
;
343 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
345 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
349 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
350 none. A return value of NULL means that EOF was encountered. */
352 readline (const char *prompt
)
359 /* If we are at EOF return a NULL string. */
360 if (rl_pending_input
== EOF
)
362 rl_clear_pending_input ();
363 return ((char *)NULL
);
367 /* If readline() is called after installing a callback handler, temporarily
368 turn off the callback state to avoid ensuing messiness. Patch supplied
369 by the gdb folks. XXX -- disabled. This can be fooled and readline
370 left in a strange state by a poorly-timed longjmp. */
371 if (in_callback
= RL_ISSTATE (RL_STATE_CALLBACK
))
372 RL_UNSETSTATE (RL_STATE_CALLBACK
);
375 rl_set_prompt (prompt
);
378 if (rl_prep_term_function
)
379 (*rl_prep_term_function
) (_rl_meta_flag
);
381 #if defined (HANDLE_SIGNALS)
385 value
= readline_internal ();
386 if (rl_deprep_term_function
)
387 (*rl_deprep_term_function
) ();
389 #if defined (HANDLE_SIGNALS)
395 RL_SETSTATE (RL_STATE_CALLBACK
);
398 #if HAVE_DECL_AUDIT_USER_TTY && defined (HAVE_LIBAUDIT_H) && defined (ENABLE_TTY_AUDIT_SUPPORT)
400 _rl_audit_tty (value
);
406 #if defined (READLINE_CALLBACKS)
407 # define STATIC_CALLBACK
409 # define STATIC_CALLBACK static
413 readline_internal_setup (void)
417 _rl_in_stream
= rl_instream
;
418 _rl_out_stream
= rl_outstream
;
420 /* Enable the meta key only for the duration of readline(), if this
421 terminal has one and the terminal has been initialized */
422 if (_rl_enable_meta
& RL_ISSTATE (RL_STATE_TERMPREPPED
))
423 _rl_enable_meta_key ();
426 (*rl_startup_hook
) ();
428 if (_rl_internal_startup_hook
)
429 (*_rl_internal_startup_hook
) ();
431 rl_deactivate_mark ();
433 #if defined (VI_MODE)
434 if (rl_editing_mode
== vi_mode
)
435 rl_vi_insertion_mode (1, 'i'); /* don't want to reset last */
438 if (_rl_show_mode_in_prompt
)
441 /* If we're not echoing, we still want to at least print a prompt, because
442 rl_redisplay will not do it for us. If the calling application has a
443 custom redisplay function, though, let that function handle it. */
444 if (_rl_echoing_p
== 0 && rl_redisplay_function
== rl_redisplay
)
446 if (rl_prompt
&& rl_already_prompted
== 0)
448 nprompt
= _rl_strip_prompt (rl_prompt
);
449 fprintf (_rl_out_stream
, "%s", nprompt
);
450 fflush (_rl_out_stream
);
456 if (rl_prompt
&& rl_already_prompted
)
457 rl_on_new_line_with_prompt ();
460 (*rl_redisplay_function
) ();
463 if (rl_pre_input_hook
)
464 (*rl_pre_input_hook
) ();
469 STATIC_CALLBACK
char *
470 readline_internal_teardown (int eof
)
477 /* Restore the original of this history line, iff the line that we
478 are editing was originally in the history, AND the line has changed. */
479 entry
= current_history ();
481 if (entry
&& rl_undo_list
)
483 temp
= savestring (the_line
);
484 rl_revert_line (1, 0);
485 entry
= replace_history_entry (where_history (), the_line
, (histdata_t
)NULL
);
486 _rl_free_history_entry (entry
);
488 strcpy (the_line
, temp
);
492 if (_rl_revert_all_at_newline
)
493 _rl_revert_all_lines ();
495 /* At any rate, it is highly likely that this line has an undo list. Get
498 rl_free_undo_list ();
500 /* Disable the meta key, if this terminal has one and we were told to use it.
501 The check whether or not we sent the enable string is in
502 _rl_disable_meta_key(); the flag is set in _rl_enable_meta_key */
503 _rl_disable_meta_key ();
505 /* Restore normal cursor, if available. */
506 _rl_set_insert_mode (RL_IM_INSERT
, 0);
508 return (eof
? (char *)NULL
: savestring (the_line
));
512 _rl_internal_char_cleanup (void)
514 #if defined (VI_MODE)
515 /* In vi mode, when you exit insert mode, the cursor moves back
516 over the previous character. We explicitly check for that here. */
517 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
)
521 if (rl_num_chars_to_read
&& rl_end
>= rl_num_chars_to_read
)
523 (*rl_redisplay_function
) ();
524 _rl_want_redisplay
= 0;
525 rl_newline (1, '\n');
530 (*rl_redisplay_function
) ();
531 _rl_want_redisplay
= 0;
534 /* If the application writer has told us to erase the entire line if
535 the only character typed was something bound to rl_newline, do so. */
536 if (rl_erase_empty_line
&& rl_done
&& rl_last_func
== rl_newline
&&
537 rl_point
== 0 && rl_end
== 0)
538 _rl_erase_entire_line ();
542 #if defined (READLINE_CALLBACKS)
543 readline_internal_char (void)
545 readline_internal_charloop (void)
548 static int lastc
, eof_found
;
553 #if !defined (READLINE_CALLBACKS)
558 lk
= _rl_last_command_was_kill
;
560 #if defined (HAVE_POSIX_SIGSETJMP)
561 code
= sigsetjmp (_rl_top_level
, 0);
563 code
= setjmp (_rl_top_level
);
568 (*rl_redisplay_function
) ();
569 _rl_want_redisplay
= 0;
570 /* If we get here, we're not being called from something dispatched
571 from _rl_callback_read_char(), which sets up its own value of
572 _rl_top_level (saving and restoring the old, of course), so
573 we can just return here. */
574 if (RL_ISSTATE (RL_STATE_CALLBACK
))
578 if (rl_pending_input
== 0)
580 /* Then initialize the argument and number of keys read. */
581 _rl_reset_argument ();
582 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
585 RL_SETSTATE(RL_STATE_READCMD
);
587 RL_UNSETSTATE(RL_STATE_READCMD
);
589 /* look at input.c:rl_getc() for the circumstances under which this will
590 be returned; punt immediately on read error without converting it to
591 a newline; assume that rl_read_key has already called the signal
595 #if defined (READLINE_CALLBACKS)
596 RL_SETSTATE(RL_STATE_DONE
);
597 return (rl_done
= 1);
604 /* EOF typed to a non-blank line is ^D the first time, EOF the second
605 time in a row. This won't return any partial line read from the tty.
606 If we want to change this, to force any existing line to be returned
607 when read(2) reads EOF, for example, this is the place to change. */
608 if (c
== EOF
&& rl_end
)
610 if (RL_SIG_RECEIVED ())
613 if (rl_signal_event_hook
)
614 (*rl_signal_event_hook
) (); /* XXX */
617 /* XXX - reading two consecutive EOFs returns EOF */
618 if (RL_ISSTATE (RL_STATE_TERMPREPPED
))
620 if (lastc
== _rl_eof_char
|| lastc
== EOF
)
629 /* The character _rl_eof_char typed to blank line, and not as the
630 previous character is interpreted as EOF. This doesn't work when
631 READLINE_CALLBACKS is defined, so hitting a series of ^Ds will
632 erase all the chars on the line and then return EOF. */
633 if (((c
== _rl_eof_char
&& lastc
!= c
) || c
== EOF
) && rl_end
== 0)
635 #if defined (READLINE_CALLBACKS)
636 RL_SETSTATE(RL_STATE_DONE
);
637 return (rl_done
= 1);
645 r
= _rl_dispatch ((unsigned char)c
, _rl_keymap
);
648 if (_rl_command_to_execute
)
650 (*rl_redisplay_function
) ();
652 rl_executing_keymap
= _rl_command_to_execute
->map
;
653 rl_executing_key
= _rl_command_to_execute
->key
;
656 RL_SETSTATE(RL_STATE_DISPATCHING
);
657 r
= (*(_rl_command_to_execute
->func
)) (_rl_command_to_execute
->count
, _rl_command_to_execute
->key
);
658 _rl_command_to_execute
= 0;
659 RL_UNSETSTATE(RL_STATE_DISPATCHING
);
665 /* If there was no change in _rl_last_command_was_kill, then no kill
666 has taken place. Note that if input is pending we are reading
667 a prefix command, so nothing has changed yet. */
668 if (rl_pending_input
== 0 && lk
== _rl_last_command_was_kill
)
669 _rl_last_command_was_kill
= 0;
671 if (_rl_keep_mark_active
)
672 _rl_keep_mark_active
= 0;
673 else if (rl_mark_active_p ())
674 rl_deactivate_mark ();
676 _rl_internal_char_cleanup ();
678 #if defined (READLINE_CALLBACKS)
687 #if defined (READLINE_CALLBACKS)
689 readline_internal_charloop (void)
694 eof
= readline_internal_char ();
697 #endif /* READLINE_CALLBACKS */
699 /* Read a line of input from the global rl_instream, doing output on
700 the global rl_outstream.
701 If rl_prompt is non-null, then that is our prompt. */
703 readline_internal (void)
705 readline_internal_setup ();
706 _rl_eof_found
= readline_internal_charloop ();
707 return (readline_internal_teardown (_rl_eof_found
));
711 _rl_init_line_state (void)
713 rl_point
= rl_end
= rl_mark
= 0;
714 the_line
= rl_line_buffer
;
719 _rl_set_the_line (void)
721 the_line
= rl_line_buffer
;
724 #if defined (READLINE_CALLBACKS)
726 _rl_keyseq_cxt_alloc (void)
730 cxt
= (_rl_keyseq_cxt
*)xmalloc (sizeof (_rl_keyseq_cxt
));
732 cxt
->flags
= cxt
->subseq_arg
= cxt
->subseq_retval
= 0;
735 cxt
->ocxt
= _rl_kscxt
;
736 cxt
->childval
= 42; /* sentinel value */
742 _rl_keyseq_cxt_dispose (_rl_keyseq_cxt
*cxt
)
748 _rl_keyseq_chain_dispose (void)
755 _rl_kscxt
= _rl_kscxt
->ocxt
;
756 _rl_keyseq_cxt_dispose (cxt
);
762 _rl_subseq_getchar (int key
)
767 RL_SETSTATE(RL_STATE_METANEXT
);
768 RL_SETSTATE(RL_STATE_MOREINPUT
);
770 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
772 RL_UNSETSTATE(RL_STATE_METANEXT
);
777 #if defined (READLINE_CALLBACKS)
779 _rl_dispatch_callback (_rl_keyseq_cxt
*cxt
)
784 /* The first time this context is used, we want to read input and dispatch
785 on it. When traversing the chain of contexts back `up', we want to use
786 the value from the next context down. We're simulating recursion using
787 a chain of contexts. */
788 if ((cxt
->flags
& KSEQ_DISPATCHED
) == 0)
790 nkey
= _rl_subseq_getchar (cxt
->okey
);
793 _rl_abort_internal ();
796 r
= _rl_dispatch_subseq (nkey
, cxt
->dmap
, cxt
->subseq_arg
);
797 cxt
->flags
|= KSEQ_DISPATCHED
;
803 if (r
!= -3) /* don't do this if we indicate there will be other matches */
804 r
= _rl_subseq_result (r
, cxt
->oldmap
, cxt
->okey
, (cxt
->flags
& KSEQ_SUBSEQ
));
807 /* We only treat values < 0 specially to simulate recursion. */
808 if (r
>= 0 || (r
== -1 && (cxt
->flags
& KSEQ_SUBSEQ
) == 0)) /* success! or failure! */
810 _rl_keyseq_chain_dispose ();
811 RL_UNSETSTATE (RL_STATE_MULTIKEY
);
815 if (r
!= -3) /* magic value that says we added to the chain */
816 _rl_kscxt
= cxt
->ocxt
;
818 _rl_kscxt
->childval
= r
;
820 _rl_keyseq_cxt_dispose (cxt
);
824 #endif /* READLINE_CALLBACKS */
826 /* Do the command associated with KEY in MAP.
827 If the associated command is really a keymap, then read
828 another key, and dispatch into that map. */
830 _rl_dispatch (register int key
, Keymap map
)
832 _rl_dispatching_keymap
= map
;
833 return _rl_dispatch_subseq (key
, map
, 0);
837 _rl_dispatch_subseq (register int key
, Keymap map
, int got_subseq
)
841 rl_command_func_t
*func
;
842 #if defined (READLINE_CALLBACKS)
846 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
848 if (map
[ESC
].type
== ISKMAP
)
850 if (RL_ISSTATE (RL_STATE_MACRODEF
))
851 _rl_add_macro_char (ESC
);
852 RESIZE_KEYSEQ_BUFFER ();
853 rl_executing_keyseq
[rl_key_sequence_length
++] = ESC
;
854 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
856 return (_rl_dispatch (key
, map
));
863 if (RL_ISSTATE (RL_STATE_MACRODEF
))
864 _rl_add_macro_char (key
);
867 switch (map
[key
].type
)
870 func
= map
[key
].function
;
873 /* Special case rl_do_lowercase_version (). */
874 if (func
== rl_do_lowercase_version
)
875 /* Should we do anything special if key == ANYOTHERKEY? */
876 return (_rl_dispatch (_rl_to_lower ((unsigned char)key
), map
));
878 rl_executing_keymap
= map
;
879 rl_executing_key
= key
;
881 RESIZE_KEYSEQ_BUFFER();
882 rl_executing_keyseq
[rl_key_sequence_length
++] = key
;
883 rl_executing_keyseq
[rl_key_sequence_length
] = '\0';
886 RL_SETSTATE(RL_STATE_DISPATCHING
);
887 r
= (*func
) (rl_numeric_arg
* rl_arg_sign
, key
);
888 RL_UNSETSTATE(RL_STATE_DISPATCHING
);
891 /* If we have input pending, then the last command was a prefix
892 command. Don't change the state of rl_last_func. Otherwise,
893 remember the last command executed in this variable. */
894 #if defined (VI_MODE)
895 if (rl_pending_input
== 0 && map
[key
].function
!= rl_digit_argument
&& map
[key
].function
!= rl_vi_arg_digit
)
897 if (rl_pending_input
== 0 && map
[key
].function
!= rl_digit_argument
)
899 rl_last_func
= map
[key
].function
;
903 else if (map
[ANYOTHERKEY
].function
)
905 /* OK, there's no function bound in this map, but there is a
906 shadow function that was overridden when the current keymap
907 was created. Return -2 to note that. */
908 if (RL_ISSTATE (RL_STATE_MACROINPUT
))
909 _rl_prev_macro_key ();
911 _rl_unget_char (key
);
912 if (rl_key_sequence_length
> 0)
913 rl_executing_keyseq
[--rl_key_sequence_length
] = '\0';
918 /* Return -1 to note that we're in a subsequence, but we don't
919 have a matching key, nor was one overridden. This means
920 we need to back up the recursion chain and find the last
921 subsequence that is bound to a function. */
922 if (RL_ISSTATE (RL_STATE_MACROINPUT
))
923 _rl_prev_macro_key ();
925 _rl_unget_char (key
);
926 if (rl_key_sequence_length
> 0)
927 rl_executing_keyseq
[--rl_key_sequence_length
] = '\0';
932 #if defined (READLINE_CALLBACKS)
933 RL_UNSETSTATE (RL_STATE_MULTIKEY
);
934 _rl_keyseq_chain_dispose ();
936 _rl_abort_internal ();
942 if (map
[key
].function
!= 0)
944 #if defined (VI_MODE)
945 /* The only way this test will be true is if a subsequence has been
946 bound starting with ESC, generally the arrow keys. What we do is
947 check whether there's input in the queue, which there generally
948 will be if an arrow key has been pressed, and, if there's not,
949 just dispatch to (what we assume is) rl_vi_movement_mode right
950 away. This is essentially an input test with a zero timeout (by
951 default) or a timeout determined by the value of `keyseq-timeout' */
952 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
953 takes microseconds, so multiply by 1000 */
954 if (rl_editing_mode
== vi_mode
&& key
== ESC
&& map
== vi_insertion_keymap
&&
955 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
956 _rl_pushed_input_available () == 0 &&
957 _rl_input_queued ((_rl_keyseq_timeout
> 0) ? _rl_keyseq_timeout
*1000 : 0) == 0)
958 return (_rl_dispatch (ANYOTHERKEY
, FUNCTION_TO_KEYMAP (map
, key
)));
959 /* This is a very specific test. It can possibly be generalized in
960 the future, but for now it handles a specific case of ESC being
961 the last character in a keyboard macro. */
962 if (rl_editing_mode
== vi_mode
&& key
== ESC
&& map
== vi_insertion_keymap
&&
963 (RL_ISSTATE (RL_STATE_INPUTPENDING
) == 0) &&
964 (RL_ISSTATE (RL_STATE_MACROINPUT
) && _rl_peek_macro_key () == 0) &&
965 _rl_pushed_input_available () == 0 &&
966 _rl_input_queued ((_rl_keyseq_timeout
> 0) ? _rl_keyseq_timeout
*1000 : 0) == 0)
967 return (_rl_dispatch (ANYOTHERKEY
, FUNCTION_TO_KEYMAP (map
, key
)));
970 RESIZE_KEYSEQ_BUFFER ();
971 rl_executing_keyseq
[rl_key_sequence_length
++] = key
;
972 _rl_dispatching_keymap
= FUNCTION_TO_KEYMAP (map
, key
);
974 /* Allocate new context here. Use linked contexts (linked through
975 cxt->ocxt) to simulate recursion */
976 #if defined (READLINE_CALLBACKS)
977 # if defined (VI_MODE)
978 /* If we're redoing a vi mode command and we know there is a shadowed
979 function corresponding to this key, just call it -- all the redoable
980 vi mode commands already have all the input they need, and rl_vi_redo
981 assumes that one call to rl_dispatch is sufficient to complete the
983 if (_rl_vi_redoing
&& RL_ISSTATE (RL_STATE_CALLBACK
) &&
984 map
[ANYOTHERKEY
].function
!= 0)
985 return (_rl_subseq_result (-2, map
, key
, got_subseq
));
987 if (RL_ISSTATE (RL_STATE_CALLBACK
))
989 /* Return 0 only the first time, to indicate success to
990 _rl_callback_read_char. The rest of the time, we're called
991 from _rl_dispatch_callback, so we return -3 to indicate
992 special handling is necessary. */
993 r
= RL_ISSTATE (RL_STATE_MULTIKEY
) ? -3 : 0;
994 cxt
= _rl_keyseq_cxt_alloc ();
997 cxt
->flags
|= KSEQ_SUBSEQ
;
1000 cxt
->dmap
= _rl_dispatching_keymap
;
1001 cxt
->subseq_arg
= got_subseq
|| cxt
->dmap
[ANYOTHERKEY
].function
;
1003 RL_SETSTATE (RL_STATE_MULTIKEY
);
1006 return r
; /* don't indicate immediate success */
1010 /* Tentative inter-character timeout for potential multi-key
1011 sequences? If no input within timeout, abort sequence and
1012 act as if we got non-matching input. */
1013 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
1014 takes microseconds, so multiply by 1000 */
1015 if (_rl_keyseq_timeout
> 0 &&
1016 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
1017 _rl_pushed_input_available () == 0 &&
1018 _rl_dispatching_keymap
[ANYOTHERKEY
].function
&&
1019 _rl_input_queued (_rl_keyseq_timeout
*1000) == 0)
1021 if (rl_key_sequence_length
> 0)
1022 rl_executing_keyseq
[--rl_key_sequence_length
] = '\0';
1023 return (_rl_subseq_result (-2, map
, key
, got_subseq
));
1026 newkey
= _rl_subseq_getchar (key
);
1029 _rl_abort_internal ();
1033 r
= _rl_dispatch_subseq (newkey
, _rl_dispatching_keymap
, got_subseq
|| map
[ANYOTHERKEY
].function
);
1034 return _rl_subseq_result (r
, map
, key
, got_subseq
);
1038 _rl_abort_internal (); /* XXX */
1044 if (map
[key
].function
!= 0)
1046 rl_executing_keyseq
[rl_key_sequence_length
] = '\0';
1047 macro
= savestring ((char *)map
[key
].function
);
1048 _rl_with_macro_input (macro
);
1054 #if defined (VI_MODE)
1055 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
&&
1056 key
!= ANYOTHERKEY
&&
1057 _rl_dispatching_keymap
== vi_movement_keymap
&&
1058 _rl_vi_textmod_command (key
))
1059 _rl_vi_set_last (key
, rl_numeric_arg
, rl_arg_sign
);
1066 _rl_subseq_result (int r
, Keymap map
, int key
, int got_subseq
)
1070 rl_command_func_t
*func
, *nf
;
1073 /* We didn't match anything, and the keymap we're indexed into
1074 shadowed a function previously bound to that prefix. Call
1075 the function. The recursive call to _rl_dispatch_subseq has
1076 already taken care of pushing any necessary input back onto
1077 the input queue with _rl_unget_char. */
1079 m
= _rl_dispatching_keymap
;
1080 type
= m
[ANYOTHERKEY
].type
;
1081 func
= m
[ANYOTHERKEY
].function
;
1082 if (type
== ISFUNC
&& func
== rl_do_lowercase_version
)
1083 r
= _rl_dispatch (_rl_to_lower ((unsigned char)key
), map
);
1084 else if (type
== ISFUNC
)
1086 /* If we shadowed a function, whatever it is, we somehow need a
1087 keymap with map[key].func == shadowed-function.
1088 Let's use this one. Then we can dispatch using the original
1089 key, since there are commands (e.g., in vi mode) for which it
1092 nf
= m
[key
].function
;
1095 m
[key
].function
= func
;
1096 /* Don't change _rl_dispatching_keymap, set it here */
1097 _rl_dispatching_keymap
= map
; /* previous map */
1098 r
= _rl_dispatch_subseq (key
, m
, 0);
1100 m
[key
].function
= nf
;
1103 /* We probably shadowed a keymap, so keep going. */
1104 r
= _rl_dispatch (ANYOTHERKEY
, m
);
1106 else if (r
< 0 && map
[ANYOTHERKEY
].function
)
1108 /* We didn't match (r is probably -1), so return something to
1109 tell the caller that it should try ANYOTHERKEY for an
1110 overridden function. */
1111 if (RL_ISSTATE (RL_STATE_MACROINPUT
))
1112 _rl_prev_macro_key ();
1114 _rl_unget_char (key
);
1115 if (rl_key_sequence_length
> 0)
1116 rl_executing_keyseq
[--rl_key_sequence_length
] = '\0';
1117 _rl_dispatching_keymap
= map
;
1120 else if (r
< 0 && got_subseq
) /* XXX */
1122 /* OK, back up the chain. */
1123 if (RL_ISSTATE (RL_STATE_MACROINPUT
))
1124 _rl_prev_macro_key ();
1126 _rl_unget_char (key
);
1127 if (rl_key_sequence_length
> 0)
1128 rl_executing_keyseq
[--rl_key_sequence_length
] = '\0';
1129 _rl_dispatching_keymap
= map
;
1136 /* **************************************************************** */
1138 /* Initializations */
1140 /* **************************************************************** */
1142 /* Initialize readline (and terminal if not already). */
1144 rl_initialize (void)
1146 /* If we have never been called before, initialize the
1147 terminal and data structures. */
1148 if (rl_initialized
== 0)
1150 RL_SETSTATE(RL_STATE_INITIALIZING
);
1151 readline_initialize_everything ();
1152 RL_UNSETSTATE(RL_STATE_INITIALIZING
);
1154 RL_SETSTATE(RL_STATE_INITIALIZED
);
1157 (void)_rl_init_locale (); /* check current locale */
1159 /* Initialize the current line information. */
1160 _rl_init_line_state ();
1162 /* We aren't done yet. We haven't even gotten started yet! */
1164 RL_UNSETSTATE(RL_STATE_DONE
);
1166 /* Tell the history routines what is going on. */
1167 _rl_start_using_history ();
1169 /* Make the display buffer match the state of the line. */
1170 rl_reset_line_state ();
1172 /* No such function typed yet. */
1173 rl_last_func
= (rl_command_func_t
*)NULL
;
1175 /* Parsing of key-bindings begins in an enabled state. */
1176 _rl_parsing_conditionalized_out
= 0;
1178 #if defined (VI_MODE)
1179 if (rl_editing_mode
== vi_mode
)
1180 _rl_vi_initialize_line ();
1183 /* Each line starts in insert mode (the default). */
1184 _rl_set_insert_mode (RL_IM_DEFAULT
, 1);
1190 #if defined (__EMX__)
1192 _emx_build_environ (void)
1199 DosGetInfoBlocks (&tibp
, &pibp
);
1200 t
= pibp
->pib_pchenv
;
1201 for (c
= 1; *t
; c
++)
1202 t
+= strlen (t
) + 1;
1203 tp
= environ
= (char **)xmalloc ((c
+ 1) * sizeof (char *));
1204 t
= pibp
->pib_pchenv
;
1208 t
+= strlen (t
) + 1;
1212 #endif /* __EMX__ */
1215 /* Initialize the entire state of the world. */
1217 readline_initialize_everything (void)
1220 #if defined (__EMX__)
1222 _emx_build_environ ();
1227 /* Find out if we are running in Emacs -- UNUSED. */
1228 running_in_emacs
= sh_get_env_value ("EMACS") != (char *)0;
1231 /* Set up input and output if they are not already set up. */
1233 rl_instream
= stdin
;
1236 rl_outstream
= stdout
;
1238 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
1239 may change, but they may also be used before readline_internal ()
1241 _rl_in_stream
= rl_instream
;
1242 _rl_out_stream
= rl_outstream
;
1244 /* Allocate data structures. */
1245 if (rl_line_buffer
== 0)
1246 rl_line_buffer
= (char *)xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
1248 /* Initialize the terminal interface. */
1249 if (rl_terminal_name
== 0)
1250 rl_terminal_name
= sh_get_env_value ("TERM");
1251 _rl_init_terminal_io (rl_terminal_name
);
1253 /* Bind tty characters to readline functions. */
1254 readline_default_bindings ();
1256 /* Initialize the function names. */
1257 rl_initialize_funmap ();
1259 /* Decide whether we should automatically go into eight-bit mode. */
1260 _rl_init_eightbit ();
1262 /* Read in the init file. */
1263 rl_read_init_file ((char *)NULL
);
1266 if (_rl_horizontal_scroll_mode
&& _rl_term_autowrap
)
1269 _rl_screenchars
-= _rl_screenheight
;
1272 /* Override the effect of any `set keymap' assignments in the
1274 rl_set_keymap_from_edit_mode ();
1276 /* Try to bind a common arrow key prefix, if not already bound. */
1279 /* Bind the bracketed paste prefix assuming that the user will enable
1280 it on terminals that support it. */
1281 bind_bracketed_paste_prefix ();
1283 /* If the completion parser's default word break characters haven't
1284 been set yet, then do so now. */
1285 if (rl_completer_word_break_characters
== (char *)NULL
)
1286 rl_completer_word_break_characters
= (char *)rl_basic_word_break_characters
;
1288 #if defined (COLOR_SUPPORT)
1289 if (_rl_colored_stats
|| _rl_colored_completion_prefix
)
1290 _rl_parse_colors ();
1293 rl_executing_keyseq
= malloc (_rl_executing_keyseq_size
= 16);
1294 if (rl_executing_keyseq
)
1295 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
1298 /* If this system allows us to look at the values of the regular
1299 input editing characters, then bind them to their readline
1300 equivalents, iff the characters are not bound to keymaps. */
1302 readline_default_bindings (void)
1304 if (_rl_bind_stty_chars
)
1305 rl_tty_set_default_bindings (_rl_keymap
);
1308 /* Reset the default bindings for the terminal special characters we're
1309 interested in back to rl_insert and read the new ones. */
1311 reset_default_bindings (void)
1313 if (_rl_bind_stty_chars
)
1315 rl_tty_unset_default_bindings (_rl_keymap
);
1316 rl_tty_set_default_bindings (_rl_keymap
);
1320 /* Bind some common arrow key sequences in MAP. */
1322 bind_arrow_keys_internal (Keymap map
)
1326 xkeymap
= _rl_keymap
;
1329 #if defined (__MSDOS__)
1330 rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history
);
1331 rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char
);
1332 rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char
);
1333 rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history
);
1336 rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history
);
1337 rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history
);
1338 rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char
);
1339 rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char
);
1340 rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line
);
1341 rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line
);
1343 rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history
);
1344 rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history
);
1345 rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char
);
1346 rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char
);
1347 rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line
);
1348 rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line
);
1350 /* Key bindings for control-arrow keys */
1351 rl_bind_keyseq_if_unbound ("\033[1;5C", rl_forward_word
);
1352 rl_bind_keyseq_if_unbound ("\033[1;5D", rl_backward_word
);
1353 rl_bind_keyseq_if_unbound ("\033[3;5~", rl_kill_word
);
1355 /* Key bindings for alt-arrow keys */
1356 rl_bind_keyseq_if_unbound ("\033[1;3C", rl_forward_word
);
1357 rl_bind_keyseq_if_unbound ("\033[1;3D", rl_backward_word
);
1359 #if defined (__MINGW32__)
1360 rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history
);
1361 rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history
);
1362 rl_bind_keyseq_if_unbound ("\340M", rl_forward_char
);
1363 rl_bind_keyseq_if_unbound ("\340K", rl_backward_char
);
1364 rl_bind_keyseq_if_unbound ("\340G", rl_beg_of_line
);
1365 rl_bind_keyseq_if_unbound ("\340O", rl_end_of_line
);
1366 rl_bind_keyseq_if_unbound ("\340S", rl_delete
);
1367 rl_bind_keyseq_if_unbound ("\340R", rl_overwrite_mode
);
1369 /* These may or may not work because of the embedded NUL. */
1370 rl_bind_keyseq_if_unbound ("\\000H", rl_get_previous_history
);
1371 rl_bind_keyseq_if_unbound ("\\000P", rl_get_next_history
);
1372 rl_bind_keyseq_if_unbound ("\\000M", rl_forward_char
);
1373 rl_bind_keyseq_if_unbound ("\\000K", rl_backward_char
);
1374 rl_bind_keyseq_if_unbound ("\\000G", rl_beg_of_line
);
1375 rl_bind_keyseq_if_unbound ("\\000O", rl_end_of_line
);
1376 rl_bind_keyseq_if_unbound ("\\000S", rl_delete
);
1377 rl_bind_keyseq_if_unbound ("\\000R", rl_overwrite_mode
);
1380 _rl_keymap
= xkeymap
;
1383 /* Try and bind the common arrow key prefixes after giving termcap and
1384 the inputrc file a chance to bind them and create `real' keymaps
1385 for the arrow key prefix. */
1387 bind_arrow_keys (void)
1389 bind_arrow_keys_internal (emacs_standard_keymap
);
1391 #if defined (VI_MODE)
1392 bind_arrow_keys_internal (vi_movement_keymap
);
1393 /* Unbind vi_movement_keymap[ESC] to allow users to repeatedly hit ESC
1394 in vi command mode while still allowing the arrow keys to work. */
1395 if (vi_movement_keymap
[ESC
].type
== ISKMAP
)
1396 rl_bind_keyseq_in_map ("\033", (rl_command_func_t
*)NULL
, vi_movement_keymap
);
1397 bind_arrow_keys_internal (vi_insertion_keymap
);
1402 bind_bracketed_paste_prefix (void)
1406 xkeymap
= _rl_keymap
;
1408 _rl_keymap
= emacs_standard_keymap
;
1409 rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF
, rl_bracketed_paste_begin
);
1411 #if defined (VI_MODE)
1412 _rl_keymap
= vi_insertion_keymap
;
1413 rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF
, rl_bracketed_paste_begin
);
1414 /* XXX - is there a reason to do this in the vi command keymap? */
1417 _rl_keymap
= xkeymap
;
1420 /* **************************************************************** */
1422 /* Saving and Restoring Readline's state */
1424 /* **************************************************************** */
1427 rl_save_state (struct readline_state
*sp
)
1432 sp
->point
= rl_point
;
1435 sp
->buffer
= rl_line_buffer
;
1436 sp
->buflen
= rl_line_buffer_len
;
1437 sp
->ul
= rl_undo_list
;
1438 sp
->prompt
= rl_prompt
;
1440 sp
->rlstate
= rl_readline_state
;
1442 sp
->kmap
= _rl_keymap
;
1444 sp
->lastfunc
= rl_last_func
;
1445 sp
->insmode
= rl_insert_mode
;
1446 sp
->edmode
= rl_editing_mode
;
1447 sp
->kseq
= rl_executing_keyseq
;
1448 sp
->kseqlen
= rl_key_sequence_length
;
1449 sp
->inf
= rl_instream
;
1450 sp
->outf
= rl_outstream
;
1451 sp
->pendingin
= rl_pending_input
;
1452 sp
->macro
= rl_executing_macro
;
1454 sp
->catchsigs
= rl_catch_signals
;
1455 sp
->catchsigwinch
= rl_catch_sigwinch
;
1457 sp
->entryfunc
= rl_completion_entry_function
;
1458 sp
->menuentryfunc
= rl_menu_completion_entry_function
;
1459 sp
->ignorefunc
= rl_ignore_some_completions_function
;
1460 sp
->attemptfunc
= rl_attempted_completion_function
;
1461 sp
->wordbreakchars
= rl_completer_word_break_characters
;
1467 rl_restore_state (struct readline_state
*sp
)
1472 rl_point
= sp
->point
;
1475 the_line
= rl_line_buffer
= sp
->buffer
;
1476 rl_line_buffer_len
= sp
->buflen
;
1477 rl_undo_list
= sp
->ul
;
1478 rl_prompt
= sp
->prompt
;
1480 rl_readline_state
= sp
->rlstate
;
1482 _rl_keymap
= sp
->kmap
;
1484 rl_last_func
= sp
->lastfunc
;
1485 rl_insert_mode
= sp
->insmode
;
1486 rl_editing_mode
= sp
->edmode
;
1487 rl_executing_keyseq
= sp
->kseq
;
1488 rl_key_sequence_length
= sp
->kseqlen
;
1489 rl_instream
= sp
->inf
;
1490 rl_outstream
= sp
->outf
;
1491 rl_pending_input
= sp
->pendingin
;
1492 rl_executing_macro
= sp
->macro
;
1494 rl_catch_signals
= sp
->catchsigs
;
1495 rl_catch_sigwinch
= sp
->catchsigwinch
;
1497 rl_completion_entry_function
= sp
->entryfunc
;
1498 rl_menu_completion_entry_function
= sp
->menuentryfunc
;
1499 rl_ignore_some_completions_function
= sp
->ignorefunc
;
1500 rl_attempted_completion_function
= sp
->attemptfunc
;
1501 rl_completer_word_break_characters
= sp
->wordbreakchars
;
1503 rl_deactivate_mark ();
1508 /* Functions to manage the string that is the current key sequence. */
1511 _rl_init_executing_keyseq (void)
1513 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
1517 _rl_term_executing_keyseq (void)
1519 rl_executing_keyseq
[rl_key_sequence_length
] = '\0';
1523 _rl_end_executing_keyseq (void)
1525 if (rl_key_sequence_length
> 0)
1526 rl_executing_keyseq
[--rl_key_sequence_length
] = '\0';
1530 _rl_add_executing_keyseq (int key
)
1532 RESIZE_KEYSEQ_BUFFER ();
1533 rl_executing_keyseq
[rl_key_sequence_length
++] = key
;