1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987-2013 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 "5.1"
79 #ifndef RL_READLINE_VERSION
80 # define RL_READLINE_VERSION 0x0501
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 readline_default_bindings PARAMS((void));
98 static void reset_default_bindings PARAMS((void));
100 static int _rl_subseq_result PARAMS((int, Keymap, int, int));
101 static int _rl_subseq_getchar PARAMS((int));
103 /* **************************************************************** */
105 /* Line editing input utility */
107 /* **************************************************************** */
109 const char *rl_library_version = RL_LIBRARY_VERSION;
111 int rl_readline_version = RL_READLINE_VERSION;
113 /* True if this is `real' readline as opposed to some stub substitute. */
114 int rl_gnu_readline_p = 1;
116 /* A pointer to the keymap that is currently in use.
117 By default, it is the standard emacs keymap. */
118 Keymap _rl_keymap = emacs_standard_keymap;
120 /* The current style of editing. */
121 int rl_editing_mode = emacs_mode;
123 /* The current insert mode: input (the default) or overwrite */
124 int rl_insert_mode = RL_IM_DEFAULT;
126 /* Non-zero if we called this function from _rl_dispatch(). It's present
127 so functions can find out whether they were called from a key binding
128 or directly from an application. */
131 /* Non-zero if the previous command was a kill command. */
132 int _rl_last_command_was_kill = 0;
134 /* The current value of the numeric argument specified by the user. */
135 int rl_numeric_arg = 1;
137 /* Non-zero if an argument was typed. */
138 int rl_explicit_arg = 0;
140 /* Temporary value used while generating the argument. */
143 /* Non-zero means we have been called at least once before. */
144 static int rl_initialized;
147 /* If non-zero, this program is running in an EMACS buffer. */
148 static int running_in_emacs;
151 /* Flags word encapsulating the current readline state. */
152 int rl_readline_state = RL_STATE_NONE;
154 /* The current offset in the current input line. */
157 /* Mark in the current input line. */
160 /* Length of the current input line. */
163 /* Make this non-zero to return the current input_line. */
166 /* The last function executed by readline. */
167 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
169 /* Top level environment for readline_internal (). */
170 procenv_t _rl_top_level;
172 /* The streams we interact with. */
173 FILE *_rl_in_stream, *_rl_out_stream;
175 /* The names of the streams that we do input and output to. */
176 FILE *rl_instream = (FILE *)NULL;
177 FILE *rl_outstream = (FILE *)NULL;
179 /* Non-zero means echo characters as they are read. Defaults to no echo;
180 set to 1 if there is a controlling terminal, we can get its attributes,
181 and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
182 for the code that sets it. */
183 int _rl_echoing_p = 0;
185 /* Current prompt. */
186 char *rl_prompt = (char *)NULL;
187 int rl_visible_prompt_length = 0;
189 /* Set to non-zero by calling application if it has already printed rl_prompt
190 and does not want readline to do it the first time. */
191 int rl_already_prompted = 0;
193 /* The number of characters read in order to type this complete command. */
194 int rl_key_sequence_length = 0;
196 /* If non-zero, then this is the address of a function to call just
197 before readline_internal_setup () prints the first prompt. */
198 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
200 /* If non-zero, this is the address of a function to call just before
201 readline_internal_setup () returns and readline_internal starts
202 reading input characters. */
203 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
205 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
206 static char *the_line;
208 /* The character that can generate an EOF. Really read from
209 the terminal driver... just defaulted here. */
210 int _rl_eof_char = CTRL ('D');
212 /* Non-zero makes this the next keystroke to read. */
213 int rl_pending_input = 0;
215 /* Pointer to a useful terminal name. */
216 const char *rl_terminal_name = (const char *)NULL;
218 /* Non-zero means to always use horizontal scrolling in line display. */
219 int _rl_horizontal_scroll_mode = 0;
221 /* Non-zero means to display an asterisk at the starts of history lines
222 which have been modified. */
223 int _rl_mark_modified_lines = 0;
225 /* The style of `bell' notification preferred. This can be set to NO_BELL,
226 AUDIBLE_BELL, or VISIBLE_BELL. */
227 int _rl_bell_preference = AUDIBLE_BELL;
229 /* String inserted into the line by rl_insert_comment (). */
230 char *_rl_comment_begin;
232 /* Keymap holding the function currently being executed. */
233 Keymap rl_executing_keymap;
235 /* Keymap we're currently using to dispatch. */
236 Keymap _rl_dispatching_keymap;
238 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
239 int rl_erase_empty_line = 0;
241 /* Non-zero means to read only this many characters rather than up to a
242 character bound to accept-line. */
243 int rl_num_chars_to_read;
245 /* Line buffer and maintenance. */
246 char *rl_line_buffer = (char *)NULL;
247 int rl_line_buffer_len = 0;
249 /* Key sequence `contexts' */
250 _rl_keyseq_cxt *_rl_kscxt = 0;
252 int rl_executing_key;
253 char *rl_executing_keyseq = 0;
254 int _rl_executing_keyseq_size = 0;
256 /* Timeout (specified in milliseconds) when reading characters making up an
257 ambiguous multiple-key sequence */
258 int _rl_keyseq_timeout = 500;
260 #define RESIZE_KEYSEQ_BUFFER() \
263 if (rl_key_sequence_length + 2 >= _rl_executing_keyseq_size) \
265 _rl_executing_keyseq_size += 16; \
266 rl_executing_keyseq = xrealloc (rl_executing_keyseq, _rl_executing_keyseq_size); \
271 /* Forward declarations used by the display, termcap, and history code. */
273 /* **************************************************************** */
275 /* `Forward' declarations */
277 /* **************************************************************** */
279 /* Non-zero means do not parse any lines other than comments and
280 parser directives. */
281 unsigned char _rl_parsing_conditionalized_out = 0;
283 /* Non-zero means to convert characters with the meta bit set to
284 escape-prefixed characters so we can indirect through
285 emacs_meta_keymap or vi_escape_keymap. */
286 int _rl_convert_meta_chars_to_ascii = 1;
288 /* Non-zero means to output characters with the meta bit set directly
289 rather than as a meta-prefixed escape sequence. */
290 int _rl_output_meta_chars = 0;
292 /* Non-zero means to look at the termios special characters and bind
293 them to equivalent readline functions at startup. */
294 int _rl_bind_stty_chars = 1;
296 /* Non-zero means to go through the history list at every newline (or
297 whenever rl_done is set and readline returns) and revert each line to
298 its initial state. */
299 int _rl_revert_all_at_newline = 0;
301 /* Non-zero means to honor the termios ECHOCTL bit and echo control
302 characters corresponding to keyboard-generated signals. */
303 int _rl_echo_control_chars = 1;
305 /* Non-zero means to prefix the displayed prompt with a character indicating
306 the editing mode: @ for emacs, : for vi-command, + for vi-insert. */
307 int _rl_show_mode_in_prompt = 0;
309 /* **************************************************************** */
311 /* Top Level Functions */
313 /* **************************************************************** */
315 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
316 int _rl_meta_flag = 0; /* Forward declaration */
318 /* Set up the prompt and expand it. Called from readline() and
319 rl_callback_handler_install (). */
321 rl_set_prompt (prompt)
325 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
326 rl_display_prompt = rl_prompt ? rl_prompt : "";
328 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
332 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
333 none. A return value of NULL means that EOF was encountered. */
343 /* If we are at EOF return a NULL string. */
344 if (rl_pending_input == EOF)
346 rl_clear_pending_input ();
347 return ((char *)NULL);
351 /* If readline() is called after installing a callback handler, temporarily
352 turn off the callback state to avoid ensuing messiness. Patch supplied
353 by the gdb folks. XXX -- disabled. This can be fooled and readline
354 left in a strange state by a poorly-timed longjmp. */
355 if (in_callback = RL_ISSTATE (RL_STATE_CALLBACK))
356 RL_UNSETSTATE (RL_STATE_CALLBACK);
359 rl_set_prompt (prompt);
362 if (rl_prep_term_function)
363 (*rl_prep_term_function) (_rl_meta_flag);
365 #if defined (HANDLE_SIGNALS)
369 value = readline_internal ();
370 if (rl_deprep_term_function)
371 (*rl_deprep_term_function) ();
373 #if defined (HANDLE_SIGNALS)
379 RL_SETSTATE (RL_STATE_CALLBACK);
382 #if HAVE_DECL_AUDIT_TTY && defined (ENABLE_TTY_AUDIT_SUPPORT)
384 _rl_audit_tty (value);
390 #if defined (READLINE_CALLBACKS)
391 # define STATIC_CALLBACK
393 # define STATIC_CALLBACK static
397 readline_internal_setup ()
401 _rl_in_stream = rl_instream;
402 _rl_out_stream = rl_outstream;
404 /* Enable the meta key only for the duration of readline(), if this
405 terminal has one and the terminal has been initialized */
406 if (_rl_enable_meta & RL_ISSTATE (RL_STATE_TERMPREPPED))
407 _rl_enable_meta_key ();
410 (*rl_startup_hook) ();
412 #if defined (VI_MODE)
413 if (rl_editing_mode == vi_mode)
414 rl_vi_insertion_mode (1, 'i'); /* don't want to reset last */
417 /* If we're not echoing, we still want to at least print a prompt, because
418 rl_redisplay will not do it for us. If the calling application has a
419 custom redisplay function, though, let that function handle it. */
420 if (_rl_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
422 if (rl_prompt && rl_already_prompted == 0)
424 nprompt = _rl_strip_prompt (rl_prompt);
425 fprintf (_rl_out_stream, "%s", nprompt);
426 fflush (_rl_out_stream);
432 if (rl_prompt && rl_already_prompted)
433 rl_on_new_line_with_prompt ();
436 (*rl_redisplay_function) ();
439 if (rl_pre_input_hook)
440 (*rl_pre_input_hook) ();
445 STATIC_CALLBACK char *
446 readline_internal_teardown (eof)
454 /* Restore the original of this history line, iff the line that we
455 are editing was originally in the history, AND the line has changed. */
456 entry = current_history ();
458 if (entry && rl_undo_list)
460 temp = savestring (the_line);
461 rl_revert_line (1, 0);
462 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
463 _rl_free_history_entry (entry);
465 strcpy (the_line, temp);
469 if (_rl_revert_all_at_newline)
470 _rl_revert_all_lines ();
472 /* At any rate, it is highly likely that this line has an undo list. Get
475 rl_free_undo_list ();
477 /* Disable the meta key, if this terminal has one and we were told to use it.
478 The check whether or not we sent the enable string is in
479 _rl_disable_meta_key(); the flag is set in _rl_enable_meta_key */
480 _rl_disable_meta_key ();
482 /* Restore normal cursor, if available. */
483 _rl_set_insert_mode (RL_IM_INSERT, 0);
485 return (eof ? (char *)NULL : savestring (the_line));
489 _rl_internal_char_cleanup ()
491 #if defined (VI_MODE)
492 /* In vi mode, when you exit insert mode, the cursor moves back
493 over the previous character. We explicitly check for that here. */
494 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
498 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
500 (*rl_redisplay_function) ();
501 _rl_want_redisplay = 0;
502 rl_newline (1, '\n');
507 (*rl_redisplay_function) ();
508 _rl_want_redisplay = 0;
511 /* If the application writer has told us to erase the entire line if
512 the only character typed was something bound to rl_newline, do so. */
513 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
514 rl_point == 0 && rl_end == 0)
515 _rl_erase_entire_line ();
519 #if defined (READLINE_CALLBACKS)
520 readline_internal_char ()
522 readline_internal_charloop ()
525 static int lastc, eof_found;
531 #if !defined (READLINE_CALLBACKS)
535 lk = _rl_last_command_was_kill;
537 #if defined (HAVE_POSIX_SIGSETJMP)
538 code = sigsetjmp (_rl_top_level, 0);
540 code = setjmp (_rl_top_level);
545 (*rl_redisplay_function) ();
546 _rl_want_redisplay = 0;
547 /* If we get here, we're not being called from something dispatched
548 from _rl_callback_read_char(), which sets up its own value of
549 _rl_top_level (saving and restoring the old, of course), so
550 we can just return here. */
551 if (RL_ISSTATE (RL_STATE_CALLBACK))
555 if (rl_pending_input == 0)
557 /* Then initialize the argument and number of keys read. */
558 _rl_reset_argument ();
559 rl_key_sequence_length = 0;
560 rl_executing_keyseq[0] = 0;
563 RL_SETSTATE(RL_STATE_READCMD);
565 RL_UNSETSTATE(RL_STATE_READCMD);
567 /* look at input.c:rl_getc() for the circumstances under which this will
568 be returned; punt immediately on read error without converting it to
569 a newline; assume that rl_read_key has already called the signal
573 #if defined (READLINE_CALLBACKS)
574 RL_SETSTATE(RL_STATE_DONE);
575 return (rl_done = 1);
582 /* EOF typed to a non-blank line is a <NL>. If we want to change this,
583 to force any existing line to be ignored when read(2) reads EOF,
584 for example, this is the place to change. */
585 if (c == EOF && rl_end)
588 /* The character _rl_eof_char typed to blank line, and not as the
589 previous character is interpreted as EOF. */
590 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
592 #if defined (READLINE_CALLBACKS)
593 RL_SETSTATE(RL_STATE_DONE);
594 return (rl_done = 1);
602 _rl_dispatch ((unsigned char)c, _rl_keymap);
605 /* If there was no change in _rl_last_command_was_kill, then no kill
606 has taken place. Note that if input is pending we are reading
607 a prefix command, so nothing has changed yet. */
608 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
609 _rl_last_command_was_kill = 0;
611 _rl_internal_char_cleanup ();
613 #if defined (READLINE_CALLBACKS)
622 #if defined (READLINE_CALLBACKS)
624 readline_internal_charloop ()
629 eof = readline_internal_char ();
632 #endif /* READLINE_CALLBACKS */
634 /* Read a line of input from the global rl_instream, doing output on
635 the global rl_outstream.
636 If rl_prompt is non-null, then that is our prompt. */
642 readline_internal_setup ();
643 eof = readline_internal_charloop ();
644 return (readline_internal_teardown (eof));
648 _rl_init_line_state ()
650 rl_point = rl_end = rl_mark = 0;
651 the_line = rl_line_buffer;
658 the_line = rl_line_buffer;
661 #if defined (READLINE_CALLBACKS)
663 _rl_keyseq_cxt_alloc ()
667 cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
669 cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
672 cxt->ocxt = _rl_kscxt;
673 cxt->childval = 42; /* sentinel value */
679 _rl_keyseq_cxt_dispose (cxt)
686 _rl_keyseq_chain_dispose ()
693 _rl_kscxt = _rl_kscxt->ocxt;
694 _rl_keyseq_cxt_dispose (cxt);
700 _rl_subseq_getchar (key)
706 RL_SETSTATE(RL_STATE_METANEXT);
707 RL_SETSTATE(RL_STATE_MOREINPUT);
709 RL_UNSETSTATE(RL_STATE_MOREINPUT);
711 RL_UNSETSTATE(RL_STATE_METANEXT);
716 #if defined (READLINE_CALLBACKS)
718 _rl_dispatch_callback (cxt)
724 /* The first time this context is used, we want to read input and dispatch
725 on it. When traversing the chain of contexts back `up', we want to use
726 the value from the next context down. We're simulating recursion using
727 a chain of contexts. */
728 if ((cxt->flags & KSEQ_DISPATCHED) == 0)
730 nkey = _rl_subseq_getchar (cxt->okey);
733 _rl_abort_internal ();
736 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
737 cxt->flags |= KSEQ_DISPATCHED;
743 if (r != -3) /* don't do this if we indicate there will be other matches */
744 r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
747 if (r == 0) /* success! */
749 _rl_keyseq_chain_dispose ();
750 RL_UNSETSTATE (RL_STATE_MULTIKEY);
754 if (r != -3) /* magic value that says we added to the chain */
755 _rl_kscxt = cxt->ocxt;
757 _rl_kscxt->childval = r;
759 _rl_keyseq_cxt_dispose (cxt);
763 #endif /* READLINE_CALLBACKS */
765 /* Do the command associated with KEY in MAP.
766 If the associated command is really a keymap, then read
767 another key, and dispatch into that map. */
769 _rl_dispatch (key, map)
773 _rl_dispatching_keymap = map;
774 return _rl_dispatch_subseq (key, map, 0);
778 _rl_dispatch_subseq (key, map, got_subseq)
785 rl_command_func_t *func;
786 #if defined (READLINE_CALLBACKS)
790 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
792 if (map[ESC].type == ISKMAP)
794 if (RL_ISSTATE (RL_STATE_MACRODEF))
795 _rl_add_macro_char (ESC);
796 RESIZE_KEYSEQ_BUFFER ();
797 rl_executing_keyseq[rl_key_sequence_length++] = ESC;
798 map = FUNCTION_TO_KEYMAP (map, ESC);
800 return (_rl_dispatch (key, map));
807 if (RL_ISSTATE (RL_STATE_MACRODEF))
808 _rl_add_macro_char (key);
811 switch (map[key].type)
814 func = map[key].function;
817 /* Special case rl_do_lowercase_version (). */
818 if (func == rl_do_lowercase_version)
819 /* Should we do anything special if key == ANYOTHERKEY? */
820 return (_rl_dispatch (_rl_to_lower (key), map));
822 rl_executing_keymap = map;
823 rl_executing_key = key;
825 RESIZE_KEYSEQ_BUFFER();
826 rl_executing_keyseq[rl_key_sequence_length++] = key;
827 rl_executing_keyseq[rl_key_sequence_length] = '\0';
830 RL_SETSTATE(RL_STATE_DISPATCHING);
831 r = (*func) (rl_numeric_arg * rl_arg_sign, key);
832 RL_UNSETSTATE(RL_STATE_DISPATCHING);
835 /* If we have input pending, then the last command was a prefix
836 command. Don't change the state of rl_last_func. Otherwise,
837 remember the last command executed in this variable. */
838 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
839 rl_last_func = map[key].function;
843 else if (map[ANYOTHERKEY].function)
845 /* OK, there's no function bound in this map, but there is a
846 shadow function that was overridden when the current keymap
847 was created. Return -2 to note that. */
848 if (RL_ISSTATE (RL_STATE_MACROINPUT))
849 _rl_prev_macro_key ();
851 _rl_unget_char (key);
856 /* Return -1 to note that we're in a subsequence, but we don't
857 have a matching key, nor was one overridden. This means
858 we need to back up the recursion chain and find the last
859 subsequence that is bound to a function. */
860 if (RL_ISSTATE (RL_STATE_MACROINPUT))
861 _rl_prev_macro_key ();
863 _rl_unget_char (key);
868 #if defined (READLINE_CALLBACKS)
869 RL_UNSETSTATE (RL_STATE_MULTIKEY);
870 _rl_keyseq_chain_dispose ();
872 _rl_abort_internal ();
878 if (map[key].function != 0)
880 #if defined (VI_MODE)
881 /* The only way this test will be true is if a subsequence has been
882 bound starting with ESC, generally the arrow keys. What we do is
883 check whether there's input in the queue, which there generally
884 will be if an arrow key has been pressed, and, if there's not,
885 just dispatch to (what we assume is) rl_vi_movement_mode right
886 away. This is essentially an input test with a zero timeout (by
887 default) or a timeout determined by the value of `keyseq-timeout' */
888 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
889 takes microseconds, so multiply by 1000 */
890 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap
891 && _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0)
892 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
895 RESIZE_KEYSEQ_BUFFER ();
896 rl_executing_keyseq[rl_key_sequence_length++] = key;
897 _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
899 /* Allocate new context here. Use linked contexts (linked through
900 cxt->ocxt) to simulate recursion */
901 #if defined (READLINE_CALLBACKS)
902 if (RL_ISSTATE (RL_STATE_CALLBACK))
904 /* Return 0 only the first time, to indicate success to
905 _rl_callback_read_char. The rest of the time, we're called
906 from _rl_dispatch_callback, so we return -3 to indicate
907 special handling is necessary. */
908 r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
909 cxt = _rl_keyseq_cxt_alloc ();
912 cxt->flags |= KSEQ_SUBSEQ;
915 cxt->dmap = _rl_dispatching_keymap;
916 cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
918 RL_SETSTATE (RL_STATE_MULTIKEY);
921 return r; /* don't indicate immediate success */
925 /* Tentative inter-character timeout for potential multi-key
926 sequences? If no input within timeout, abort sequence and
927 act as if we got non-matching input. */
928 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
929 takes microseconds, so multiply by 1000 */
930 if (_rl_keyseq_timeout > 0 &&
931 (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) &&
932 _rl_pushed_input_available () == 0 &&
933 _rl_dispatching_keymap[ANYOTHERKEY].function &&
934 _rl_input_queued (_rl_keyseq_timeout*1000) == 0)
935 return (_rl_subseq_result (-2, map, key, got_subseq));
937 newkey = _rl_subseq_getchar (key);
940 _rl_abort_internal ();
944 r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
945 return _rl_subseq_result (r, map, key, got_subseq);
949 _rl_abort_internal ();
955 if (map[key].function != 0)
957 rl_executing_keyseq[rl_key_sequence_length] = '\0';
958 macro = savestring ((char *)map[key].function);
959 _rl_with_macro_input (macro);
964 #if defined (VI_MODE)
965 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
966 key != ANYOTHERKEY &&
967 rl_key_sequence_length == 1 && /* XXX */
968 _rl_vi_textmod_command (key))
969 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
976 _rl_subseq_result (r, map, key, got_subseq)
983 rl_command_func_t *func, *nf;
986 /* We didn't match anything, and the keymap we're indexed into
987 shadowed a function previously bound to that prefix. Call
988 the function. The recursive call to _rl_dispatch_subseq has
989 already taken care of pushing any necessary input back onto
990 the input queue with _rl_unget_char. */
992 m = _rl_dispatching_keymap;
993 type = m[ANYOTHERKEY].type;
994 func = m[ANYOTHERKEY].function;
995 if (type == ISFUNC && func == rl_do_lowercase_version)
996 r = _rl_dispatch (_rl_to_lower (key), map);
997 else if (type == ISFUNC && func == rl_insert)
999 /* If the function that was shadowed was self-insert, we
1000 somehow need a keymap with map[key].func == self-insert.
1001 Let's use this one. */
1003 nf = m[key].function;
1006 m[key].function = func;
1007 r = _rl_dispatch (key, m);
1009 m[key].function = nf;
1012 r = _rl_dispatch (ANYOTHERKEY, m);
1014 else if (r && map[ANYOTHERKEY].function)
1016 /* We didn't match (r is probably -1), so return something to
1017 tell the caller that it should try ANYOTHERKEY for an
1018 overridden function. */
1019 if (RL_ISSTATE (RL_STATE_MACROINPUT))
1020 _rl_prev_macro_key ();
1022 _rl_unget_char (key);
1023 _rl_dispatching_keymap = map;
1026 else if (r && got_subseq)
1028 /* OK, back up the chain. */
1029 if (RL_ISSTATE (RL_STATE_MACROINPUT))
1030 _rl_prev_macro_key ();
1032 _rl_unget_char (key);
1033 _rl_dispatching_keymap = map;
1040 /* **************************************************************** */
1042 /* Initializations */
1044 /* **************************************************************** */
1046 /* Initialize readline (and terminal if not already). */
1050 /* If we have never been called before, initialize the
1051 terminal and data structures. */
1052 if (!rl_initialized)
1054 RL_SETSTATE(RL_STATE_INITIALIZING);
1055 readline_initialize_everything ();
1056 RL_UNSETSTATE(RL_STATE_INITIALIZING);
1058 RL_SETSTATE(RL_STATE_INITIALIZED);
1061 /* Initialize the current line information. */
1062 _rl_init_line_state ();
1064 /* We aren't done yet. We haven't even gotten started yet! */
1066 RL_UNSETSTATE(RL_STATE_DONE);
1068 /* Tell the history routines what is going on. */
1069 _rl_start_using_history ();
1071 /* Make the display buffer match the state of the line. */
1072 rl_reset_line_state ();
1074 /* No such function typed yet. */
1075 rl_last_func = (rl_command_func_t *)NULL;
1077 /* Parsing of key-bindings begins in an enabled state. */
1078 _rl_parsing_conditionalized_out = 0;
1080 #if defined (VI_MODE)
1081 if (rl_editing_mode == vi_mode)
1082 _rl_vi_initialize_line ();
1085 /* Each line starts in insert mode (the default). */
1086 _rl_set_insert_mode (RL_IM_DEFAULT, 1);
1092 #if defined (__EMX__)
1094 _emx_build_environ ()
1101 DosGetInfoBlocks (&tibp, &pibp);
1102 t = pibp->pib_pchenv;
1103 for (c = 1; *t; c++)
1104 t += strlen (t) + 1;
1105 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
1106 t = pibp->pib_pchenv;
1110 t += strlen (t) + 1;
1114 #endif /* __EMX__ */
1117 /* Initialize the entire state of the world. */
1119 readline_initialize_everything ()
1122 #if defined (__EMX__)
1124 _emx_build_environ ();
1129 /* Find out if we are running in Emacs -- UNUSED. */
1130 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
1133 /* Set up input and output if they are not already set up. */
1135 rl_instream = stdin;
1138 rl_outstream = stdout;
1140 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
1141 may change, but they may also be used before readline_internal ()
1143 _rl_in_stream = rl_instream;
1144 _rl_out_stream = rl_outstream;
1146 /* Allocate data structures. */
1147 if (rl_line_buffer == 0)
1148 rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1150 /* Initialize the terminal interface. */
1151 if (rl_terminal_name == 0)
1152 rl_terminal_name = sh_get_env_value ("TERM");
1153 _rl_init_terminal_io (rl_terminal_name);
1155 /* Bind tty characters to readline functions. */
1156 readline_default_bindings ();
1158 /* Initialize the function names. */
1159 rl_initialize_funmap ();
1161 /* Decide whether we should automatically go into eight-bit mode. */
1162 _rl_init_eightbit ();
1164 /* Read in the init file. */
1165 rl_read_init_file ((char *)NULL);
1168 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
1171 _rl_screenchars -= _rl_screenheight;
1174 /* Override the effect of any `set keymap' assignments in the
1176 rl_set_keymap_from_edit_mode ();
1178 /* Try to bind a common arrow key prefix, if not already bound. */
1181 /* If the completion parser's default word break characters haven't
1182 been set yet, then do so now. */
1183 if (rl_completer_word_break_characters == (char *)NULL)
1184 rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
1186 #if defined (COLOR_SUPPORT)
1187 if (_rl_colored_stats)
1188 _rl_parse_colors ();
1191 rl_executing_keyseq = malloc (_rl_executing_keyseq_size = 16);
1192 if (rl_executing_keyseq)
1193 rl_executing_keyseq[0] = '\0';
1196 /* If this system allows us to look at the values of the regular
1197 input editing characters, then bind them to their readline
1198 equivalents, iff the characters are not bound to keymaps. */
1200 readline_default_bindings ()
1202 if (_rl_bind_stty_chars)
1203 rl_tty_set_default_bindings (_rl_keymap);
1206 /* Reset the default bindings for the terminal special characters we're
1207 interested in back to rl_insert and read the new ones. */
1209 reset_default_bindings ()
1211 if (_rl_bind_stty_chars)
1213 rl_tty_unset_default_bindings (_rl_keymap);
1214 rl_tty_set_default_bindings (_rl_keymap);
1218 /* Bind some common arrow key sequences in MAP. */
1220 bind_arrow_keys_internal (map)
1225 xkeymap = _rl_keymap;
1228 #if defined (__MSDOS__)
1229 rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
1230 rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
1231 rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
1232 rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
1235 rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
1236 rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
1237 rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
1238 rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
1239 rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
1240 rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
1242 rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
1243 rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
1244 rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
1245 rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
1246 rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
1247 rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
1249 #if defined (__MINGW32__)
1250 rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
1251 rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
1252 rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
1253 rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
1254 rl_bind_keyseq_if_unbound ("\340G", rl_beg_of_line);
1255 rl_bind_keyseq_if_unbound ("\340O", rl_end_of_line);
1256 rl_bind_keyseq_if_unbound ("\340S", rl_delete);
1257 rl_bind_keyseq_if_unbound ("\340R", rl_overwrite_mode);
1259 /* These may or may not work because of the embedded NUL. */
1260 rl_bind_keyseq_if_unbound ("\\000H", rl_get_previous_history);
1261 rl_bind_keyseq_if_unbound ("\\000P", rl_get_next_history);
1262 rl_bind_keyseq_if_unbound ("\\000M", rl_forward_char);
1263 rl_bind_keyseq_if_unbound ("\\000K", rl_backward_char);
1264 rl_bind_keyseq_if_unbound ("\\000G", rl_beg_of_line);
1265 rl_bind_keyseq_if_unbound ("\\000O", rl_end_of_line);
1266 rl_bind_keyseq_if_unbound ("\\000S", rl_delete);
1267 rl_bind_keyseq_if_unbound ("\\000R", rl_overwrite_mode);
1270 _rl_keymap = xkeymap;
1273 /* Try and bind the common arrow key prefixes after giving termcap and
1274 the inputrc file a chance to bind them and create `real' keymaps
1275 for the arrow key prefix. */
1279 bind_arrow_keys_internal (emacs_standard_keymap);
1281 #if defined (VI_MODE)
1282 bind_arrow_keys_internal (vi_movement_keymap);
1283 /* Unbind vi_movement_keymap[ESC] to allow users to repeatedly hit ESC
1284 in vi command mode while still allowing the arrow keys to work. */
1285 if (vi_movement_keymap[ESC].type == ISKMAP)
1286 rl_bind_keyseq_in_map ("\033", (rl_command_func_t *)NULL, vi_movement_keymap);
1287 bind_arrow_keys_internal (vi_insertion_keymap);
1291 /* **************************************************************** */
1293 /* Saving and Restoring Readline's state */
1295 /* **************************************************************** */
1299 struct readline_state *sp;
1304 sp->point = rl_point;
1307 sp->buffer = rl_line_buffer;
1308 sp->buflen = rl_line_buffer_len;
1309 sp->ul = rl_undo_list;
1310 sp->prompt = rl_prompt;
1312 sp->rlstate = rl_readline_state;
1314 sp->kmap = _rl_keymap;
1316 sp->lastfunc = rl_last_func;
1317 sp->insmode = rl_insert_mode;
1318 sp->edmode = rl_editing_mode;
1319 sp->kseqlen = rl_key_sequence_length;
1320 sp->inf = rl_instream;
1321 sp->outf = rl_outstream;
1322 sp->pendingin = rl_pending_input;
1323 sp->macro = rl_executing_macro;
1325 sp->catchsigs = rl_catch_signals;
1326 sp->catchsigwinch = rl_catch_sigwinch;
1332 rl_restore_state (sp)
1333 struct readline_state *sp;
1338 rl_point = sp->point;
1341 the_line = rl_line_buffer = sp->buffer;
1342 rl_line_buffer_len = sp->buflen;
1343 rl_undo_list = sp->ul;
1344 rl_prompt = sp->prompt;
1346 rl_readline_state = sp->rlstate;
1348 _rl_keymap = sp->kmap;
1350 rl_last_func = sp->lastfunc;
1351 rl_insert_mode = sp->insmode;
1352 rl_editing_mode = sp->edmode;
1353 rl_key_sequence_length = sp->kseqlen;
1354 rl_instream = sp->inf;
1355 rl_outstream = sp->outf;
1356 rl_pending_input = sp->pendingin;
1357 rl_executing_macro = sp->macro;
1359 rl_catch_signals = sp->catchsigs;
1360 rl_catch_sigwinch = sp->catchsigwinch;