mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / cmd-line-utils / readline / readline.c
blobf8b70275e44fce418d65f06a6ab2fe3e4f1c7a67
1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
26 # include "config_readline.h"
27 #endif
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 */
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
41 # include <stdlib.h>
42 #else
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
47 # include <locale.h>
48 #endif
50 #include <stdio.h>
51 #include "posixjmp.h"
52 #include <errno.h>
54 #if !defined (errno)
55 extern int errno;
56 #endif /* !errno */
58 /* System-specific feature definitions and include files. */
59 #include "rldefs.h"
60 #include "rlmbutil.h"
62 #if defined (__EMX__)
63 # define INCL_DOSPROCESS
64 # include <os2.h>
65 #endif /* __EMX__ */
67 /* Some standard library routines. */
68 #include "readline.h"
69 #include "history.h"
71 #include "rlprivate.h"
72 #include "rlshell.h"
73 #include "xmalloc.h"
75 #ifndef RL_LIBRARY_VERSION
76 # define RL_LIBRARY_VERSION "5.1"
77 #endif
79 #ifndef RL_READLINE_VERSION
80 # define RL_READLINE_VERSION 0x0501
81 #endif
83 extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
85 /* Forward declarations used in this file. */
86 static char *readline_internal PARAMS((void));
87 static void readline_initialize_everything PARAMS((void));
89 static void bind_arrow_keys_internal PARAMS((Keymap));
90 static void bind_arrow_keys PARAMS((void));
92 static void readline_default_bindings PARAMS((void));
93 static void reset_default_bindings PARAMS((void)) __attribute__((unused));
95 static int _rl_subseq_result PARAMS((int, Keymap, int, int));
96 static int _rl_subseq_getchar PARAMS((int));
98 /* **************************************************************** */
99 /* */
100 /* Line editing input utility */
101 /* */
102 /* **************************************************************** */
104 const char *rl_library_version = RL_LIBRARY_VERSION;
106 int rl_readline_version = RL_READLINE_VERSION;
108 /* True if this is `real' readline as opposed to some stub substitute. */
109 int rl_gnu_readline_p = 1;
111 /* A pointer to the keymap that is currently in use.
112 By default, it is the standard emacs keymap. */
113 Keymap _rl_keymap = emacs_standard_keymap;
116 /* The current style of editing. */
117 int rl_editing_mode = emacs_mode;
119 /* The current insert mode: input (the default) or overwrite */
120 int rl_insert_mode = RL_IM_DEFAULT;
122 /* Non-zero if we called this function from _rl_dispatch(). It's present
123 so functions can find out whether they were called from a key binding
124 or directly from an application. */
125 int rl_dispatching;
127 /* Non-zero if the previous command was a kill command. */
128 int _rl_last_command_was_kill = 0;
130 /* The current value of the numeric argument specified by the user. */
131 int rl_numeric_arg = 1;
133 /* Non-zero if an argument was typed. */
134 int rl_explicit_arg = 0;
136 /* Temporary value used while generating the argument. */
137 int rl_arg_sign = 1;
139 /* Non-zero means we have been called at least once before. */
140 static int rl_initialized;
142 #if 0
143 /* If non-zero, this program is running in an EMACS buffer. */
144 static int running_in_emacs;
145 #endif
147 /* Flags word encapsulating the current readline state. */
148 int rl_readline_state = RL_STATE_NONE;
150 /* The current offset in the current input line. */
151 int rl_point;
153 /* Mark in the current input line. */
154 int rl_mark;
156 /* Length of the current input line. */
157 int rl_end;
159 /* Make this non-zero to return the current input_line. */
160 int rl_done;
162 /* The last function executed by readline. */
163 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
165 /* Top level environment for readline_internal (). */
166 procenv_t readline_top_level;
168 /* The streams we interact with. */
169 FILE *_rl_in_stream, *_rl_out_stream;
171 /* The names of the streams that we do input and output to. */
172 FILE *rl_instream = (FILE *)NULL;
173 FILE *rl_outstream = (FILE *)NULL;
175 /* Non-zero means echo characters as they are read. Defaults to no echo;
176 set to 1 if there is a controlling terminal, we can get its attributes,
177 and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
178 for the code that sets it. */
179 int readline_echoing_p = 0;
181 /* Current prompt. */
182 char *rl_prompt = (char *)NULL;
183 int rl_visible_prompt_length = 0;
185 /* Set to non-zero by calling application if it has already printed rl_prompt
186 and does not want readline to do it the first time. */
187 int rl_already_prompted = 0;
189 /* The number of characters read in order to type this complete command. */
190 int rl_key_sequence_length = 0;
192 /* If non-zero, then this is the address of a function to call just
193 before readline_internal_setup () prints the first prompt. */
194 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
196 /* If non-zero, this is the address of a function to call just before
197 readline_internal_setup () returns and readline_internal starts
198 reading input characters. */
199 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
201 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
202 static char *the_line;
204 /* The character that can generate an EOF. Really read from
205 the terminal driver... just defaulted here. */
206 int _rl_eof_char = CTRL ('D');
208 /* Non-zero makes this the next keystroke to read. */
209 int rl_pending_input = 0;
211 /* Pointer to a useful terminal name. */
212 const char *rl_terminal_name = (const char *)NULL;
214 /* Non-zero means to always use horizontal scrolling in line display. */
215 int _rl_horizontal_scroll_mode = 0;
217 /* Non-zero means to display an asterisk at the starts of history lines
218 which have been modified. */
219 int _rl_mark_modified_lines = 0;
221 /* The style of `bell' notification preferred. This can be set to NO_BELL,
222 AUDIBLE_BELL, or VISIBLE_BELL. */
223 int _rl_bell_preference = AUDIBLE_BELL;
225 /* String inserted into the line by rl_insert_comment (). */
226 char *_rl_comment_begin;
228 /* Keymap holding the function currently being executed. */
229 Keymap rl_executing_keymap;
231 /* Keymap we're currently using to dispatch. */
232 Keymap _rl_dispatching_keymap;
234 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
235 int rl_erase_empty_line = 0;
237 /* Non-zero means to read only this many characters rather than up to a
238 character bound to accept-line. */
239 int rl_num_chars_to_read;
241 /* Line buffer and maintenence. */
242 char *rl_line_buffer = (char *)NULL;
243 int rl_line_buffer_len = 0;
245 /* Key sequence `contexts' */
246 _rl_keyseq_cxt *_rl_kscxt = 0;
248 /* Forward declarations used by the display, termcap, and history code. */
250 /* **************************************************************** */
251 /* */
252 /* `Forward' declarations */
253 /* */
254 /* **************************************************************** */
256 /* Non-zero means do not parse any lines other than comments and
257 parser directives. */
258 unsigned char _rl_parsing_conditionalized_out = 0;
260 /* Non-zero means to convert characters with the meta bit set to
261 escape-prefixed characters so we can indirect through
262 emacs_meta_keymap or vi_escape_keymap. */
263 int _rl_convert_meta_chars_to_ascii = 1;
265 /* Non-zero means to output characters with the meta bit set directly
266 rather than as a meta-prefixed escape sequence. */
267 int _rl_output_meta_chars = 0;
269 /* Non-zero means to look at the termios special characters and bind
270 them to equivalent readline functions at startup. */
271 int _rl_bind_stty_chars = 1;
273 /* **************************************************************** */
274 /* */
275 /* Top Level Functions */
276 /* */
277 /* **************************************************************** */
279 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
280 int _rl_meta_flag = 0; /* Forward declaration */
282 /* Set up the prompt and expand it. Called from readline() and
283 rl_callback_handler_install (). */
285 rl_set_prompt (prompt)
286 const char *prompt;
288 FREE (rl_prompt);
289 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
290 rl_display_prompt = rl_prompt ? rl_prompt : "";
292 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
293 return 0;
296 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
297 none. A return value of NULL means that EOF was encountered. */
298 char *
299 readline (prompt)
300 const char *prompt;
302 char *value;
304 /* If we are at EOF return a NULL string. */
305 if (rl_pending_input == EOF)
307 rl_clear_pending_input ();
308 return ((char *)NULL);
311 rl_set_prompt (prompt);
313 rl_initialize ();
314 if (rl_prep_term_function)
315 (*rl_prep_term_function) (_rl_meta_flag);
317 #if defined (HANDLE_SIGNALS)
318 rl_set_signals ();
319 #endif
321 value = readline_internal ();
322 if (rl_deprep_term_function)
323 (*rl_deprep_term_function) ();
325 #if defined (HANDLE_SIGNALS)
326 rl_clear_signals ();
327 #endif
329 return (value);
332 #if defined (READLINE_CALLBACKS)
333 # define STATIC_CALLBACK
334 #else
335 # define STATIC_CALLBACK static
336 #endif
338 STATIC_CALLBACK void
339 readline_internal_setup ()
341 char *nprompt;
343 _rl_in_stream = rl_instream;
344 _rl_out_stream = rl_outstream;
346 if (rl_startup_hook)
347 (*rl_startup_hook) ();
349 /* If we're not echoing, we still want to at least print a prompt, because
350 rl_redisplay will not do it for us. If the calling application has a
351 custom redisplay function, though, let that function handle it. */
352 if (readline_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
354 if (rl_prompt && rl_already_prompted == 0)
356 nprompt = _rl_strip_prompt (rl_prompt);
357 fprintf (_rl_out_stream, "%s", nprompt);
358 fflush (_rl_out_stream);
359 free (nprompt);
362 else
364 if (rl_prompt && rl_already_prompted)
365 rl_on_new_line_with_prompt ();
366 else
367 rl_on_new_line ();
368 (*rl_redisplay_function) ();
371 #if defined (VI_MODE)
372 if (rl_editing_mode == vi_mode)
373 rl_vi_insertion_mode (1, 'i');
374 #endif /* VI_MODE */
376 if (rl_pre_input_hook)
377 (*rl_pre_input_hook) ();
380 STATIC_CALLBACK char *
381 readline_internal_teardown (eof)
382 int eof;
384 char *temp;
385 HIST_ENTRY *entry;
387 /* Restore the original of this history line, iff the line that we
388 are editing was originally in the history, AND the line has changed. */
389 entry = current_history ();
391 if (entry && rl_undo_list)
393 temp = savestring (the_line);
394 rl_revert_line (1, 0);
395 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
396 _rl_free_history_entry (entry);
398 strcpy (the_line, temp);
399 free (temp);
402 /* At any rate, it is highly likely that this line has an undo list. Get
403 rid of it now. */
404 if (rl_undo_list)
405 rl_free_undo_list ();
407 /* Restore normal cursor, if available. */
408 _rl_set_insert_mode (RL_IM_INSERT, 0);
410 return (eof ? (char *)NULL : savestring (the_line));
413 void
414 _rl_internal_char_cleanup ()
416 #if defined (VI_MODE)
417 /* In vi mode, when you exit insert mode, the cursor moves back
418 over the previous character. We explicitly check for that here. */
419 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
420 rl_vi_check ();
421 #endif /* VI_MODE */
423 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
425 (*rl_redisplay_function) ();
426 _rl_want_redisplay = 0;
427 rl_newline (1, '\n');
430 if (rl_done == 0)
432 (*rl_redisplay_function) ();
433 _rl_want_redisplay = 0;
436 /* If the application writer has told us to erase the entire line if
437 the only character typed was something bound to rl_newline, do so. */
438 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
439 rl_point == 0 && rl_end == 0)
440 _rl_erase_entire_line ();
443 STATIC_CALLBACK int
444 #if defined (READLINE_CALLBACKS)
445 readline_internal_char ()
446 #else
447 readline_internal_charloop ()
448 #endif
450 static int lastc;
451 int c, code, lk;
453 lastc = -1;
455 #if !defined (READLINE_CALLBACKS)
456 while (rl_done == 0)
458 #endif
459 lk = _rl_last_command_was_kill;
461 code = setjmp (readline_top_level);
463 if (code)
465 (*rl_redisplay_function) ();
466 _rl_want_redisplay = 0;
467 /* If we get here, we're not being called from something dispatched
468 from _rl_callback_read_char(), which sets up its own value of
469 readline_top_level (saving and restoring the old, of course), so
470 we can just return here. */
471 if (RL_ISSTATE (RL_STATE_CALLBACK))
472 return (0);
475 if (rl_pending_input == 0)
477 /* Then initialize the argument and number of keys read. */
478 _rl_reset_argument ();
479 rl_key_sequence_length = 0;
482 RL_SETSTATE(RL_STATE_READCMD);
483 c = rl_read_key ();
484 RL_UNSETSTATE(RL_STATE_READCMD);
486 /* look at input.c:rl_getc() for the circumstances under which this will
487 be returned; punt immediately on read error without converting it to
488 a newline. */
489 if (c == READERR)
491 #if defined (READLINE_CALLBACKS)
492 RL_SETSTATE(RL_STATE_DONE);
493 return (rl_done = 1);
494 #else
495 eof_found = 1;
496 break;
497 #endif
500 /* EOF typed to a non-blank line is a <NL>. */
501 if (c == EOF && rl_end)
502 c = NEWLINE;
504 /* The character _rl_eof_char typed to blank line, and not as the
505 previous character is interpreted as EOF. */
506 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
508 #if defined (READLINE_CALLBACKS)
509 RL_SETSTATE(RL_STATE_DONE);
510 return (rl_done = 1);
511 #else
512 eof_found = 1;
513 break;
514 #endif
517 lastc = c;
518 _rl_dispatch ((unsigned char)c, _rl_keymap);
520 /* If there was no change in _rl_last_command_was_kill, then no kill
521 has taken place. Note that if input is pending we are reading
522 a prefix command, so nothing has changed yet. */
523 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
524 _rl_last_command_was_kill = 0;
526 _rl_internal_char_cleanup ();
528 #if defined (READLINE_CALLBACKS)
529 return 0;
530 #else
533 return (eof_found);
534 #endif
537 #if defined (READLINE_CALLBACKS)
538 static int
539 readline_internal_charloop ()
541 int eof = 1;
543 while (rl_done == 0)
544 eof = readline_internal_char ();
545 return (eof);
547 #endif /* READLINE_CALLBACKS */
549 /* Read a line of input from the global rl_instream, doing output on
550 the global rl_outstream.
551 If rl_prompt is non-null, then that is our prompt. */
552 static char *
553 readline_internal ()
555 int eof;
557 readline_internal_setup ();
558 eof = readline_internal_charloop ();
559 return (readline_internal_teardown (eof));
562 void
563 _rl_init_line_state ()
565 rl_point = rl_end = rl_mark = 0;
566 the_line = rl_line_buffer;
567 the_line[0] = 0;
570 void
571 _rl_set_the_line ()
573 the_line = rl_line_buffer;
576 #if defined (READLINE_CALLBACKS)
577 _rl_keyseq_cxt *
578 _rl_keyseq_cxt_alloc ()
580 _rl_keyseq_cxt *cxt;
582 cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
584 cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
586 cxt->okey = 0;
587 cxt->ocxt = _rl_kscxt;
588 cxt->childval = 42; /* sentinel value */
590 return cxt;
593 void
594 _rl_keyseq_cxt_dispose (cxt)
595 _rl_keyseq_cxt *cxt;
597 free (cxt);
600 void
601 _rl_keyseq_chain_dispose ()
603 _rl_keyseq_cxt *cxt;
605 while (_rl_kscxt)
607 cxt = _rl_kscxt;
608 _rl_kscxt = _rl_kscxt->ocxt;
609 _rl_keyseq_cxt_dispose (cxt);
612 #endif
614 static int
615 _rl_subseq_getchar (key)
616 int key;
618 int k;
620 if (key == ESC)
621 RL_SETSTATE(RL_STATE_METANEXT);
622 RL_SETSTATE(RL_STATE_MOREINPUT);
623 k = rl_read_key ();
624 RL_UNSETSTATE(RL_STATE_MOREINPUT);
625 if (key == ESC)
626 RL_UNSETSTATE(RL_STATE_METANEXT);
628 return k;
631 #if defined (READLINE_CALLBACKS)
633 _rl_dispatch_callback (cxt)
634 _rl_keyseq_cxt *cxt;
636 int nkey, r;
638 /* For now */
639 #if 1
640 /* The first time this context is used, we want to read input and dispatch
641 on it. When traversing the chain of contexts back `up', we want to use
642 the value from the next context down. We're simulating recursion using
643 a chain of contexts. */
644 if ((cxt->flags & KSEQ_DISPATCHED) == 0)
646 nkey = _rl_subseq_getchar (cxt->okey);
647 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
648 cxt->flags |= KSEQ_DISPATCHED;
650 else
651 r = cxt->childval;
652 #else
653 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
654 #endif
656 /* For now */
657 r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
659 if (r == 0) /* success! */
661 _rl_keyseq_chain_dispose ();
662 RL_UNSETSTATE (RL_STATE_MULTIKEY);
663 return r;
666 if (r != -3) /* magic value that says we added to the chain */
667 _rl_kscxt = cxt->ocxt;
668 if (_rl_kscxt)
669 _rl_kscxt->childval = r;
670 if (r != -3)
671 _rl_keyseq_cxt_dispose (cxt);
673 return r;
675 #endif /* READLINE_CALLBACKS */
677 /* Do the command associated with KEY in MAP.
678 If the associated command is really a keymap, then read
679 another key, and dispatch into that map. */
681 _rl_dispatch (key, map)
682 register int key;
683 Keymap map;
685 _rl_dispatching_keymap = map;
686 return _rl_dispatch_subseq (key, map, 0);
690 _rl_dispatch_subseq (key, map, got_subseq)
691 register int key;
692 Keymap map;
693 int got_subseq;
695 int r, newkey;
696 char *macro;
697 rl_command_func_t *func;
698 #if defined (READLINE_CALLBACKS)
699 _rl_keyseq_cxt *cxt;
700 #endif
702 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
704 if (map[ESC].type == ISKMAP)
706 if (RL_ISSTATE (RL_STATE_MACRODEF))
707 _rl_add_macro_char (ESC);
708 map = FUNCTION_TO_KEYMAP (map, ESC);
709 key = UNMETA (key);
710 rl_key_sequence_length += 2;
711 return (_rl_dispatch (key, map));
713 else
714 rl_ding ();
715 return 0;
718 if (RL_ISSTATE (RL_STATE_MACRODEF))
719 _rl_add_macro_char (key);
721 r = 0;
722 switch (map[key].type)
724 case ISFUNC:
725 func = map[key].function;
726 if (func)
728 /* Special case rl_do_lowercase_version (). */
729 if (func == rl_do_lowercase_version)
730 return (_rl_dispatch (_rl_to_lower (key), map));
732 rl_executing_keymap = map;
734 rl_dispatching = 1;
735 RL_SETSTATE(RL_STATE_DISPATCHING);
736 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
737 RL_UNSETSTATE(RL_STATE_DISPATCHING);
738 rl_dispatching = 0;
740 /* If we have input pending, then the last command was a prefix
741 command. Don't change the state of rl_last_func. Otherwise,
742 remember the last command executed in this variable. */
743 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
744 rl_last_func = map[key].function;
746 else if (map[ANYOTHERKEY].function)
748 /* OK, there's no function bound in this map, but there is a
749 shadow function that was overridden when the current keymap
750 was created. Return -2 to note that. */
751 _rl_unget_char (key);
752 return -2;
754 else if (got_subseq)
756 /* Return -1 to note that we're in a subsequence, but we don't
757 have a matching key, nor was one overridden. This means
758 we need to back up the recursion chain and find the last
759 subsequence that is bound to a function. */
760 _rl_unget_char (key);
761 return -1;
763 else
765 #if defined (READLINE_CALLBACKS)
766 RL_UNSETSTATE (RL_STATE_MULTIKEY);
767 _rl_keyseq_chain_dispose ();
768 #endif
769 _rl_abort_internal ();
770 return -1;
772 break;
774 case ISKMAP:
775 if (map[key].function != 0)
777 #if defined (VI_MODE)
778 /* The only way this test will be true is if a subsequence has been
779 bound starting with ESC, generally the arrow keys. What we do is
780 check whether there's input in the queue, which there generally
781 will be if an arrow key has been pressed, and, if there's not,
782 just dispatch to (what we assume is) rl_vi_movement_mode right
783 away. This is essentially an input test with a zero timeout. */
784 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap
785 && _rl_input_queued (0) == 0)
786 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
787 #endif
789 rl_key_sequence_length++;
790 _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
792 /* Allocate new context here. Use linked contexts (linked through
793 cxt->ocxt) to simulate recursion */
794 #if defined (READLINE_CALLBACKS)
795 if (RL_ISSTATE (RL_STATE_CALLBACK))
797 /* Return 0 only the first time, to indicate success to
798 _rl_callback_read_char. The rest of the time, we're called
799 from _rl_dispatch_callback, so we return 3 to indicate
800 special handling is necessary. */
801 r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
802 cxt = _rl_keyseq_cxt_alloc ();
804 if (got_subseq)
805 cxt->flags |= KSEQ_SUBSEQ;
806 cxt->okey = key;
807 cxt->oldmap = map;
808 cxt->dmap = _rl_dispatching_keymap;
809 cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
811 RL_SETSTATE (RL_STATE_MULTIKEY);
812 _rl_kscxt = cxt;
814 return r; /* don't indicate immediate success */
816 #endif
818 newkey = _rl_subseq_getchar (key);
819 if (newkey < 0)
821 _rl_abort_internal ();
822 return -1;
825 r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
826 return _rl_subseq_result (r, map, key, got_subseq);
828 else
830 _rl_abort_internal ();
831 return -1;
833 break;
835 case ISMACR:
836 if (map[key].function != 0)
838 macro = savestring ((char *)map[key].function);
839 _rl_with_macro_input (macro);
840 return 0;
842 break;
844 #if defined (VI_MODE)
845 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
846 key != ANYOTHERKEY &&
847 _rl_vi_textmod_command (key))
848 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
849 #endif
851 return (r);
854 static int
855 _rl_subseq_result (r, map, key, got_subseq)
856 int r;
857 Keymap map;
858 int key, got_subseq;
860 Keymap m;
861 int type, nt;
862 rl_command_func_t *func, *nf;
864 if (r == -2)
865 /* We didn't match anything, and the keymap we're indexed into
866 shadowed a function previously bound to that prefix. Call
867 the function. The recursive call to _rl_dispatch_subseq has
868 already taken care of pushing any necessary input back onto
869 the input queue with _rl_unget_char. */
871 m = _rl_dispatching_keymap;
872 type = m[ANYOTHERKEY].type;
873 func = m[ANYOTHERKEY].function;
874 if (type == ISFUNC && func == rl_do_lowercase_version)
875 r = _rl_dispatch (_rl_to_lower (key), map);
876 else if (type == ISFUNC && func == rl_insert)
878 /* If the function that was shadowed was self-insert, we
879 somehow need a keymap with map[key].func == self-insert.
880 Let's use this one. */
881 nt = m[key].type;
882 nf = m[key].function;
884 m[key].type = type;
885 m[key].function = func;
886 r = _rl_dispatch (key, m);
887 m[key].type = nt;
888 m[key].function = nf;
890 else
891 r = _rl_dispatch (ANYOTHERKEY, m);
893 else if (r && map[ANYOTHERKEY].function)
895 /* We didn't match (r is probably -1), so return something to
896 tell the caller that it should try ANYOTHERKEY for an
897 overridden function. */
898 _rl_unget_char (key);
899 _rl_dispatching_keymap = map;
900 return -2;
902 else if (r && got_subseq)
904 /* OK, back up the chain. */
905 _rl_unget_char (key);
906 _rl_dispatching_keymap = map;
907 return -1;
910 return r;
913 /* **************************************************************** */
914 /* */
915 /* Initializations */
916 /* */
917 /* **************************************************************** */
919 /* Initialize readline (and terminal if not already). */
921 rl_initialize ()
923 /* If we have never been called before, initialize the
924 terminal and data structures. */
925 if (!rl_initialized)
927 RL_SETSTATE(RL_STATE_INITIALIZING);
928 readline_initialize_everything ();
929 RL_UNSETSTATE(RL_STATE_INITIALIZING);
930 rl_initialized++;
931 RL_SETSTATE(RL_STATE_INITIALIZED);
934 /* Initalize the current line information. */
935 _rl_init_line_state ();
937 /* We aren't done yet. We haven't even gotten started yet! */
938 rl_done = 0;
939 RL_UNSETSTATE(RL_STATE_DONE);
941 /* Tell the history routines what is going on. */
942 _rl_start_using_history ();
944 /* Make the display buffer match the state of the line. */
945 rl_reset_line_state ();
947 /* No such function typed yet. */
948 rl_last_func = (rl_command_func_t *)NULL;
950 /* Parsing of key-bindings begins in an enabled state. */
951 _rl_parsing_conditionalized_out = 0;
953 #if defined (VI_MODE)
954 if (rl_editing_mode == vi_mode)
955 _rl_vi_initialize_line ();
956 #endif
958 /* Each line starts in insert mode (the default). */
959 _rl_set_insert_mode (RL_IM_DEFAULT, 1);
961 return 0;
964 #if 0
965 #if defined (__EMX__)
966 static void
967 _emx_build_environ ()
969 TIB *tibp;
970 PIB *pibp;
971 char *t, **tp;
972 int c;
974 DosGetInfoBlocks (&tibp, &pibp);
975 t = pibp->pib_pchenv;
976 for (c = 1; *t; c++)
977 t += strlen (t) + 1;
978 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
979 t = pibp->pib_pchenv;
980 while (*t)
982 *tp++ = t;
983 t += strlen (t) + 1;
985 *tp = 0;
987 #endif /* __EMX__ */
988 #endif
990 /* Initialize the entire state of the world. */
991 static void
992 readline_initialize_everything ()
994 #if 0
995 #if defined (__EMX__)
996 if (environ == 0)
997 _emx_build_environ ();
998 #endif
999 #endif
1001 #if 0
1002 /* Find out if we are running in Emacs -- UNUSED. */
1003 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
1004 #endif
1006 /* Set up input and output if they are not already set up. */
1007 if (!rl_instream)
1008 rl_instream = stdin;
1010 if (!rl_outstream)
1011 rl_outstream = stdout;
1013 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
1014 may change, but they may also be used before readline_internal ()
1015 is called. */
1016 _rl_in_stream = rl_instream;
1017 _rl_out_stream = rl_outstream;
1019 /* Allocate data structures. */
1020 if (rl_line_buffer == 0)
1021 rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1023 /* Initialize the terminal interface. */
1024 if (rl_terminal_name == 0)
1025 rl_terminal_name = sh_get_env_value ("TERM");
1026 _rl_init_terminal_io (rl_terminal_name);
1028 /* Bind tty characters to readline functions. */
1029 readline_default_bindings ();
1031 /* Initialize the function names. */
1032 rl_initialize_funmap ();
1034 /* Decide whether we should automatically go into eight-bit mode. */
1035 _rl_init_eightbit ();
1037 /* Read in the init file. */
1038 rl_read_init_file ((char *)NULL);
1040 /* XXX */
1041 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
1043 _rl_screenwidth--;
1044 _rl_screenchars -= _rl_screenheight;
1047 /* Override the effect of any `set keymap' assignments in the
1048 inputrc file. */
1049 rl_set_keymap_from_edit_mode ();
1051 /* Try to bind a common arrow key prefix, if not already bound. */
1052 bind_arrow_keys ();
1054 /* Enable the meta key, if this terminal has one. */
1055 if (_rl_enable_meta)
1056 _rl_enable_meta_key ();
1058 /* If the completion parser's default word break characters haven't
1059 been set yet, then do so now. */
1060 if (rl_completer_word_break_characters == (char *)NULL)
1061 rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
1064 /* If this system allows us to look at the values of the regular
1065 input editing characters, then bind them to their readline
1066 equivalents, iff the characters are not bound to keymaps. */
1067 static void
1068 readline_default_bindings ()
1070 if (_rl_bind_stty_chars)
1071 rl_tty_set_default_bindings (_rl_keymap);
1074 /* Reset the default bindings for the terminal special characters we're
1075 interested in back to rl_insert and read the new ones. */
1076 static void
1077 reset_default_bindings ()
1079 if (_rl_bind_stty_chars)
1081 rl_tty_unset_default_bindings (_rl_keymap);
1082 rl_tty_set_default_bindings (_rl_keymap);
1086 /* Bind some common arrow key sequences in MAP. */
1087 static void
1088 bind_arrow_keys_internal (map)
1089 Keymap map;
1091 Keymap xkeymap;
1093 xkeymap = _rl_keymap;
1094 _rl_keymap = map;
1096 #if defined (__MSDOS__)
1097 rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
1098 rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
1099 rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
1100 rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
1101 #endif
1103 rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
1104 rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
1105 rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
1106 rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
1107 rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
1108 rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
1110 rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
1111 rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
1112 rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
1113 rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
1114 rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
1115 rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
1117 #if defined (__MINGW32__)
1118 rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
1119 rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
1120 rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
1121 rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
1122 #endif
1124 _rl_keymap = xkeymap;
1127 /* Try and bind the common arrow key prefixes after giving termcap and
1128 the inputrc file a chance to bind them and create `real' keymaps
1129 for the arrow key prefix. */
1130 static void
1131 bind_arrow_keys ()
1133 bind_arrow_keys_internal (emacs_standard_keymap);
1135 #if defined (VI_MODE)
1136 bind_arrow_keys_internal (vi_movement_keymap);
1137 bind_arrow_keys_internal (vi_insertion_keymap);
1138 #endif
1141 /* **************************************************************** */
1142 /* */
1143 /* Saving and Restoring Readline's state */
1144 /* */
1145 /* **************************************************************** */
1148 rl_save_state (sp)
1149 struct readline_state *sp;
1151 if (sp == 0)
1152 return -1;
1154 sp->point = rl_point;
1155 sp->end = rl_end;
1156 sp->mark = rl_mark;
1157 sp->buffer = rl_line_buffer;
1158 sp->buflen = rl_line_buffer_len;
1159 sp->ul = rl_undo_list;
1160 sp->prompt = rl_prompt;
1162 sp->rlstate = rl_readline_state;
1163 sp->done = rl_done;
1164 sp->kmap = _rl_keymap;
1166 sp->lastfunc = rl_last_func;
1167 sp->insmode = rl_insert_mode;
1168 sp->edmode = rl_editing_mode;
1169 sp->kseqlen = rl_key_sequence_length;
1170 sp->inf = rl_instream;
1171 sp->outf = rl_outstream;
1172 sp->pendingin = rl_pending_input;
1173 sp->macro = rl_executing_macro;
1175 sp->catchsigs = rl_catch_signals;
1176 sp->catchsigwinch = rl_catch_sigwinch;
1178 return (0);
1182 rl_restore_state (sp)
1183 struct readline_state *sp;
1185 if (sp == 0)
1186 return -1;
1188 rl_point = sp->point;
1189 rl_end = sp->end;
1190 rl_mark = sp->mark;
1191 the_line = rl_line_buffer = sp->buffer;
1192 rl_line_buffer_len = sp->buflen;
1193 rl_undo_list = sp->ul;
1194 rl_prompt = sp->prompt;
1196 rl_readline_state = sp->rlstate;
1197 rl_done = sp->done;
1198 _rl_keymap = sp->kmap;
1200 rl_last_func = sp->lastfunc;
1201 rl_insert_mode = sp->insmode;
1202 rl_editing_mode = sp->edmode;
1203 rl_key_sequence_length = sp->kseqlen;
1204 rl_instream = sp->inf;
1205 rl_outstream = sp->outf;
1206 rl_pending_input = sp->pendingin;
1207 rl_executing_macro = sp->macro;
1209 rl_catch_signals = sp->catchsigs;
1210 rl_catch_sigwinch = sp->catchsigwinch;
1212 return (0);