source: trunk/essentials/app-shells/bash/lib/readline/readline.c

Last change on this file was 3253, checked in by bird, 18 years ago

Applied readline-51-001

  • Property svn:eol-style set to native
File size: 31.2 KB
Line 
1/* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4/* Copyright (C) 1987-2005 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
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.
13
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.
18
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 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23#define READLINE_LIBRARY
24
25#if defined (HAVE_CONFIG_H)
26# include <config.h>
27#endif
28
29#include <sys/types.h>
30#include "posixstat.h"
31#include <fcntl.h>
32#if defined (HAVE_SYS_FILE_H)
33# include <sys/file.h>
34#endif /* HAVE_SYS_FILE_H */
35
36#if defined (HAVE_UNISTD_H)
37# include <unistd.h>
38#endif /* HAVE_UNISTD_H */
39
40#if defined (HAVE_STDLIB_H)
41# include <stdlib.h>
42#else
43# include "ansi_stdlib.h"
44#endif /* HAVE_STDLIB_H */
45
46#if defined (HAVE_LOCALE_H)
47# include <locale.h>
48#endif
49
50#include <stdio.h>
51#include "posixjmp.h"
52
53/* System-specific feature definitions and include files. */
54#include "rldefs.h"
55#include "rlmbutil.h"
56
57#if defined (__EMX__)
58# define INCL_DOSPROCESS
59# include <os2.h>
60#endif /* __EMX__ */
61
62/* Some standard library routines. */
63#include "readline.h"
64#include "history.h"
65
66#include "rlprivate.h"
67#include "rlshell.h"
68#include "xmalloc.h"
69
70#ifndef RL_LIBRARY_VERSION
71# define RL_LIBRARY_VERSION "5.1"
72#endif
73
74#ifndef RL_READLINE_VERSION
75# define RL_READLINE_VERSION 0x0501
76#endif
77
78extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
79
80/* Forward declarations used in this file. */
81static char *readline_internal PARAMS((void));
82static void readline_initialize_everything PARAMS((void));
83
84static void bind_arrow_keys_internal PARAMS((Keymap));
85static void bind_arrow_keys PARAMS((void));
86
87static void readline_default_bindings PARAMS((void));
88static void reset_default_bindings PARAMS((void));
89
90static int _rl_subseq_result PARAMS((int, Keymap, int, int));
91static int _rl_subseq_getchar PARAMS((int));
92
93/* **************************************************************** */
94/* */
95/* Line editing input utility */
96/* */
97/* **************************************************************** */
98
99const char *rl_library_version = RL_LIBRARY_VERSION;
100
101int rl_readline_version = RL_READLINE_VERSION;
102
103/* True if this is `real' readline as opposed to some stub substitute. */
104int rl_gnu_readline_p = 1;
105
106/* A pointer to the keymap that is currently in use.
107 By default, it is the standard emacs keymap. */
108Keymap _rl_keymap = emacs_standard_keymap;
109
110
111/* The current style of editing. */
112int rl_editing_mode = emacs_mode;
113
114/* The current insert mode: input (the default) or overwrite */
115int rl_insert_mode = RL_IM_DEFAULT;
116
117/* Non-zero if we called this function from _rl_dispatch(). It's present
118 so functions can find out whether they were called from a key binding
119 or directly from an application. */
120int rl_dispatching;
121
122/* Non-zero if the previous command was a kill command. */
123int _rl_last_command_was_kill = 0;
124
125/* The current value of the numeric argument specified by the user. */
126int rl_numeric_arg = 1;
127
128/* Non-zero if an argument was typed. */
129int rl_explicit_arg = 0;
130
131/* Temporary value used while generating the argument. */
132int rl_arg_sign = 1;
133
134/* Non-zero means we have been called at least once before. */
135static int rl_initialized;
136
137#if 0
138/* If non-zero, this program is running in an EMACS buffer. */
139static int running_in_emacs;
140#endif
141
142/* Flags word encapsulating the current readline state. */
143int rl_readline_state = RL_STATE_NONE;
144
145/* The current offset in the current input line. */
146int rl_point;
147
148/* Mark in the current input line. */
149int rl_mark;
150
151/* Length of the current input line. */
152int rl_end;
153
154/* Make this non-zero to return the current input_line. */
155int rl_done;
156
157/* The last function executed by readline. */
158rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
159
160/* Top level environment for readline_internal (). */
161procenv_t readline_top_level;
162
163/* The streams we interact with. */
164FILE *_rl_in_stream, *_rl_out_stream;
165
166/* The names of the streams that we do input and output to. */
167FILE *rl_instream = (FILE *)NULL;
168FILE *rl_outstream = (FILE *)NULL;
169
170/* Non-zero means echo characters as they are read. Defaults to no echo;
171 set to 1 if there is a controlling terminal, we can get its attributes,
172 and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
173 for the code that sets it. */
174#if defined (__OS2__)
175int readline_echoing_p = 1;
176#else
177int readline_echoing_p = 0;
178#endif
179
180/* Current prompt. */
181char *rl_prompt = (char *)NULL;
182int rl_visible_prompt_length = 0;
183
184/* Set to non-zero by calling application if it has already printed rl_prompt
185 and does not want readline to do it the first time. */
186int rl_already_prompted = 0;
187
188/* The number of characters read in order to type this complete command. */
189int rl_key_sequence_length = 0;
190
191/* If non-zero, then this is the address of a function to call just
192 before readline_internal_setup () prints the first prompt. */
193rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
194
195/* If non-zero, this is the address of a function to call just before
196 readline_internal_setup () returns and readline_internal starts
197 reading input characters. */
198rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
199
200/* What we use internally. You should always refer to RL_LINE_BUFFER. */
201static char *the_line;
202
203/* The character that can generate an EOF. Really read from
204 the terminal driver... just defaulted here. */
205int _rl_eof_char = CTRL ('D');
206
207/* Non-zero makes this the next keystroke to read. */
208int rl_pending_input = 0;
209
210/* Pointer to a useful terminal name. */
211const char *rl_terminal_name = (const char *)NULL;
212
213/* Non-zero means to always use horizontal scrolling in line display. */
214int _rl_horizontal_scroll_mode = 0;
215
216/* Non-zero means to display an asterisk at the starts of history lines
217 which have been modified. */
218int _rl_mark_modified_lines = 0;
219
220/* The style of `bell' notification preferred. This can be set to NO_BELL,
221 AUDIBLE_BELL, or VISIBLE_BELL. */
222int _rl_bell_preference = AUDIBLE_BELL;
223
224/* String inserted into the line by rl_insert_comment (). */
225char *_rl_comment_begin;
226
227/* Keymap holding the function currently being executed. */
228Keymap rl_executing_keymap;
229
230/* Keymap we're currently using to dispatch. */
231Keymap _rl_dispatching_keymap;
232
233/* Non-zero means to erase entire line, including prompt, on empty input lines. */
234int rl_erase_empty_line = 0;
235
236/* Non-zero means to read only this many characters rather than up to a
237 character bound to accept-line. */
238int rl_num_chars_to_read;
239
240/* Line buffer and maintenence. */
241char *rl_line_buffer = (char *)NULL;
242int rl_line_buffer_len = 0;
243
244/* Key sequence `contexts' */
245_rl_keyseq_cxt *_rl_kscxt = 0;
246
247/* Forward declarations used by the display, termcap, and history code. */
248
249/* **************************************************************** */
250/* */
251/* `Forward' declarations */
252/* */
253/* **************************************************************** */
254
255/* Non-zero means do not parse any lines other than comments and
256 parser directives. */
257unsigned char _rl_parsing_conditionalized_out = 0;
258
259/* Non-zero means to convert characters with the meta bit set to
260 escape-prefixed characters so we can indirect through
261 emacs_meta_keymap or vi_escape_keymap. */
262int _rl_convert_meta_chars_to_ascii = 1;
263
264/* Non-zero means to output characters with the meta bit set directly
265 rather than as a meta-prefixed escape sequence. */
266int _rl_output_meta_chars = 0;
267
268/* Non-zero means to look at the termios special characters and bind
269 them to equivalent readline functions at startup. */
270int _rl_bind_stty_chars = 1;
271
272/* **************************************************************** */
273/* */
274/* Top Level Functions */
275/* */
276/* **************************************************************** */
277
278/* Non-zero means treat 0200 bit in terminal input as Meta bit. */
279int _rl_meta_flag = 0; /* Forward declaration */
280
281/* Set up the prompt and expand it. Called from readline() and
282 rl_callback_handler_install (). */
283int
284rl_set_prompt (prompt)
285 const char *prompt;
286{
287 FREE (rl_prompt);
288 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
289 rl_display_prompt = rl_prompt ? rl_prompt : "";
290
291 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
292 return 0;
293}
294
295/* Read a line of input. Prompt with PROMPT. An empty PROMPT means
296 none. A return value of NULL means that EOF was encountered. */
297char *
298readline (prompt)
299 const char *prompt;
300{
301 char *value;
302
303 /* If we are at EOF return a NULL string. */
304 if (rl_pending_input == EOF)
305 {
306 rl_clear_pending_input ();
307 return ((char *)NULL);
308 }
309
310 rl_set_prompt (prompt);
311
312 rl_initialize ();
313 if (rl_prep_term_function)
314 (*rl_prep_term_function) (_rl_meta_flag);
315
316#if defined (HANDLE_SIGNALS)
317 rl_set_signals ();
318#endif
319
320 value = readline_internal ();
321 if (rl_deprep_term_function)
322 (*rl_deprep_term_function) ();
323
324#if defined (HANDLE_SIGNALS)
325 rl_clear_signals ();
326#endif
327
328 return (value);
329}
330
331#if defined (READLINE_CALLBACKS)
332# define STATIC_CALLBACK
333#else
334# define STATIC_CALLBACK static
335#endif
336
337STATIC_CALLBACK void
338readline_internal_setup ()
339{
340 char *nprompt;
341
342 _rl_in_stream = rl_instream;
343 _rl_out_stream = rl_outstream;
344
345 if (rl_startup_hook)
346 (*rl_startup_hook) ();
347
348 /* If we're not echoing, we still want to at least print a prompt, because
349 rl_redisplay will not do it for us. If the calling application has a
350 custom redisplay function, though, let that function handle it. */
351 if (readline_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
352 {
353 if (rl_prompt && rl_already_prompted == 0)
354 {
355 nprompt = _rl_strip_prompt (rl_prompt);
356 fprintf (_rl_out_stream, "%s", nprompt);
357 fflush (_rl_out_stream);
358 free (nprompt);
359 }
360 }
361 else
362 {
363 if (rl_prompt && rl_already_prompted)
364 rl_on_new_line_with_prompt ();
365 else
366 rl_on_new_line ();
367 (*rl_redisplay_function) ();
368 }
369
370#if defined (VI_MODE)
371 if (rl_editing_mode == vi_mode)
372 rl_vi_insertion_mode (1, 'i');
373#endif /* VI_MODE */
374
375 if (rl_pre_input_hook)
376 (*rl_pre_input_hook) ();
377}
378
379STATIC_CALLBACK char *
380readline_internal_teardown (eof)
381 int eof;
382{
383 char *temp;
384 HIST_ENTRY *entry;
385
386 /* Restore the original of this history line, iff the line that we
387 are editing was originally in the history, AND the line has changed. */
388 entry = current_history ();
389
390 if (entry && rl_undo_list)
391 {
392 temp = savestring (the_line);
393 rl_revert_line (1, 0);
394 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
395 _rl_free_history_entry (entry);
396
397 strcpy (the_line, temp);
398 free (temp);
399 }
400
401 /* At any rate, it is highly likely that this line has an undo list. Get
402 rid of it now. */
403 if (rl_undo_list)
404 rl_free_undo_list ();
405
406 /* Restore normal cursor, if available. */
407 _rl_set_insert_mode (RL_IM_INSERT, 0);
408
409 return (eof ? (char *)NULL : savestring (the_line));
410}
411
412void
413_rl_internal_char_cleanup ()
414{
415#if defined (VI_MODE)
416 /* In vi mode, when you exit insert mode, the cursor moves back
417 over the previous character. We explicitly check for that here. */
418 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
419 rl_vi_check ();
420#endif /* VI_MODE */
421
422 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
423 {
424 (*rl_redisplay_function) ();
425 _rl_want_redisplay = 0;
426 rl_newline (1, '\n');
427 }
428
429 if (rl_done == 0)
430 {
431 (*rl_redisplay_function) ();
432 _rl_want_redisplay = 0;
433 }
434
435 /* If the application writer has told us to erase the entire line if
436 the only character typed was something bound to rl_newline, do so. */
437 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
438 rl_point == 0 && rl_end == 0)
439 _rl_erase_entire_line ();
440}
441
442STATIC_CALLBACK int
443#if defined (READLINE_CALLBACKS)
444readline_internal_char ()
445#else
446readline_internal_charloop ()
447#endif
448{
449 static int lastc, eof_found;
450 int c, code, lk;
451
452 lastc = -1;
453 eof_found = 0;
454
455#if !defined (READLINE_CALLBACKS)
456 while (rl_done == 0)
457 {
458#endif
459 lk = _rl_last_command_was_kill;
460
461 code = setjmp (readline_top_level);
462
463 if (code)
464 {
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);
473 }
474
475 if (rl_pending_input == 0)
476 {
477 /* Then initialize the argument and number of keys read. */
478 _rl_reset_argument ();
479 rl_key_sequence_length = 0;
480 }
481
482 RL_SETSTATE(RL_STATE_READCMD);
483 c = rl_read_key ();
484 RL_UNSETSTATE(RL_STATE_READCMD);
485
486 /* EOF typed to a non-blank line is a <NL>. */
487 if (c == EOF && rl_end)
488 c = NEWLINE;
489
490 /* The character _rl_eof_char typed to blank line, and not as the
491 previous character is interpreted as EOF. */
492 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
493 {
494#if defined (READLINE_CALLBACKS)
495 RL_SETSTATE(RL_STATE_DONE);
496 return (rl_done = 1);
497#else
498 eof_found = 1;
499 break;
500#endif
501 }
502
503 lastc = c;
504 _rl_dispatch ((unsigned char)c, _rl_keymap);
505
506 /* If there was no change in _rl_last_command_was_kill, then no kill
507 has taken place. Note that if input is pending we are reading
508 a prefix command, so nothing has changed yet. */
509 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
510 _rl_last_command_was_kill = 0;
511
512 _rl_internal_char_cleanup ();
513
514#if defined (READLINE_CALLBACKS)
515 return 0;
516#else
517 }
518
519 return (eof_found);
520#endif
521}
522
523#if defined (READLINE_CALLBACKS)
524static int
525readline_internal_charloop ()
526{
527 int eof = 1;
528
529 while (rl_done == 0)
530 eof = readline_internal_char ();
531 return (eof);
532}
533#endif /* READLINE_CALLBACKS */
534
535/* Read a line of input from the global rl_instream, doing output on
536 the global rl_outstream.
537 If rl_prompt is non-null, then that is our prompt. */
538static char *
539readline_internal ()
540{
541 int eof;
542
543 readline_internal_setup ();
544 eof = readline_internal_charloop ();
545 return (readline_internal_teardown (eof));
546}
547
548void
549_rl_init_line_state ()
550{
551 rl_point = rl_end = rl_mark = 0;
552 the_line = rl_line_buffer;
553 the_line[0] = 0;
554}
555
556void
557_rl_set_the_line ()
558{
559 the_line = rl_line_buffer;
560}
561
562#if defined (READLINE_CALLBACKS)
563_rl_keyseq_cxt *
564_rl_keyseq_cxt_alloc ()
565{
566 _rl_keyseq_cxt *cxt;
567
568 cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
569
570 cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
571
572 cxt->okey = 0;
573 cxt->ocxt = _rl_kscxt;
574 cxt->childval = 42; /* sentinel value */
575
576 return cxt;
577}
578
579void
580_rl_keyseq_cxt_dispose (cxt)
581 _rl_keyseq_cxt *cxt;
582{
583 free (cxt);
584}
585
586void
587_rl_keyseq_chain_dispose ()
588{
589 _rl_keyseq_cxt *cxt;
590
591 while (_rl_kscxt)
592 {
593 cxt = _rl_kscxt;
594 _rl_kscxt = _rl_kscxt->ocxt;
595 _rl_keyseq_cxt_dispose (cxt);
596 }
597}
598#endif
599
600static int
601_rl_subseq_getchar (key)
602 int key;
603{
604 int k;
605
606 if (key == ESC)
607 RL_SETSTATE(RL_STATE_METANEXT);
608 RL_SETSTATE(RL_STATE_MOREINPUT);
609 k = rl_read_key ();
610 RL_UNSETSTATE(RL_STATE_MOREINPUT);
611 if (key == ESC)
612 RL_UNSETSTATE(RL_STATE_METANEXT);
613
614 return k;
615}
616
617#if defined (READLINE_CALLBACKS)
618int
619_rl_dispatch_callback (cxt)
620 _rl_keyseq_cxt *cxt;
621{
622 int nkey, r;
623
624 /* For now */
625#if 1
626 /* The first time this context is used, we want to read input and dispatch
627 on it. When traversing the chain of contexts back `up', we want to use
628 the value from the next context down. We're simulating recursion using
629 a chain of contexts. */
630 if ((cxt->flags & KSEQ_DISPATCHED) == 0)
631 {
632 nkey = _rl_subseq_getchar (cxt->okey);
633 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
634 cxt->flags |= KSEQ_DISPATCHED;
635 }
636 else
637 r = cxt->childval;
638#else
639 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
640#endif
641
642 /* For now */
643 r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
644
645 if (r == 0) /* success! */
646 {
647 _rl_keyseq_chain_dispose ();
648 RL_UNSETSTATE (RL_STATE_MULTIKEY);
649 return r;
650 }
651
652 if (r != -3) /* magic value that says we added to the chain */
653 _rl_kscxt = cxt->ocxt;
654 if (_rl_kscxt)
655 _rl_kscxt->childval = r;
656 if (r != -3)
657 _rl_keyseq_cxt_dispose (cxt);
658
659 return r;
660}
661#endif /* READLINE_CALLBACKS */
662
663/* Do the command associated with KEY in MAP.
664 If the associated command is really a keymap, then read
665 another key, and dispatch into that map. */
666int
667_rl_dispatch (key, map)
668 register int key;
669 Keymap map;
670{
671 _rl_dispatching_keymap = map;
672 return _rl_dispatch_subseq (key, map, 0);
673}
674
675int
676_rl_dispatch_subseq (key, map, got_subseq)
677 register int key;
678 Keymap map;
679 int got_subseq;
680{
681 int r, newkey;
682 char *macro;
683 rl_command_func_t *func;
684#if defined (READLINE_CALLBACKS)
685 _rl_keyseq_cxt *cxt;
686#endif
687
688 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
689 {
690 if (map[ESC].type == ISKMAP)
691 {
692 if (RL_ISSTATE (RL_STATE_MACRODEF))
693 _rl_add_macro_char (ESC);
694 map = FUNCTION_TO_KEYMAP (map, ESC);
695 key = UNMETA (key);
696 rl_key_sequence_length += 2;
697 return (_rl_dispatch (key, map));
698 }
699 else
700 rl_ding ();
701 return 0;
702 }
703
704 if (RL_ISSTATE (RL_STATE_MACRODEF))
705 _rl_add_macro_char (key);
706
707 r = 0;
708 switch (map[key].type)
709 {
710 case ISFUNC:
711 func = map[key].function;
712 if (func)
713 {
714 /* Special case rl_do_lowercase_version (). */
715 if (func == rl_do_lowercase_version)
716 return (_rl_dispatch (_rl_to_lower (key), map));
717
718 rl_executing_keymap = map;
719
720 rl_dispatching = 1;
721 RL_SETSTATE(RL_STATE_DISPATCHING);
722 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
723 RL_UNSETSTATE(RL_STATE_DISPATCHING);
724 rl_dispatching = 0;
725
726 /* If we have input pending, then the last command was a prefix
727 command. Don't change the state of rl_last_func. Otherwise,
728 remember the last command executed in this variable. */
729 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
730 rl_last_func = map[key].function;
731 }
732 else if (map[ANYOTHERKEY].function)
733 {
734 /* OK, there's no function bound in this map, but there is a
735 shadow function that was overridden when the current keymap
736 was created. Return -2 to note that. */
737 _rl_unget_char (key);
738 return -2;
739 }
740 else if (got_subseq)
741 {
742 /* Return -1 to note that we're in a subsequence, but we don't
743 have a matching key, nor was one overridden. This means
744 we need to back up the recursion chain and find the last
745 subsequence that is bound to a function. */
746 _rl_unget_char (key);
747 return -1;
748 }
749 else
750 {
751#if defined (READLINE_CALLBACKS)
752 RL_UNSETSTATE (RL_STATE_MULTIKEY);
753 _rl_keyseq_chain_dispose ();
754#endif
755 _rl_abort_internal ();
756 return -1;
757 }
758 break;
759
760 case ISKMAP:
761 if (map[key].function != 0)
762 {
763#if defined (VI_MODE)
764 /* The only way this test will be true is if a subsequence has been
765 bound starting with ESC, generally the arrow keys. What we do is
766 check whether there's input in the queue, which there generally
767 will be if an arrow key has been pressed, and, if there's not,
768 just dispatch to (what we assume is) rl_vi_movement_mode right
769 away. This is essentially an input test with a zero timeout. */
770 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap
771 && _rl_input_queued (0) == 0)
772 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
773#endif
774
775 rl_key_sequence_length++;
776 _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
777
778 /* Allocate new context here. Use linked contexts (linked through
779 cxt->ocxt) to simulate recursion */
780#if defined (READLINE_CALLBACKS)
781 if (RL_ISSTATE (RL_STATE_CALLBACK))
782 {
783 /* Return 0 only the first time, to indicate success to
784 _rl_callback_read_char. The rest of the time, we're called
785 from _rl_dispatch_callback, so we return 3 to indicate
786 special handling is necessary. */
787 r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
788 cxt = _rl_keyseq_cxt_alloc ();
789
790 if (got_subseq)
791 cxt->flags |= KSEQ_SUBSEQ;
792 cxt->okey = key;
793 cxt->oldmap = map;
794 cxt->dmap = _rl_dispatching_keymap;
795 cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
796
797 RL_SETSTATE (RL_STATE_MULTIKEY);
798 _rl_kscxt = cxt;
799
800 return r; /* don't indicate immediate success */
801 }
802#endif
803
804 newkey = _rl_subseq_getchar (key);
805 if (newkey < 0)
806 {
807 _rl_abort_internal ();
808 return -1;
809 }
810
811 r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
812 return _rl_subseq_result (r, map, key, got_subseq);
813 }
814 else
815 {
816 _rl_abort_internal ();
817 return -1;
818 }
819 break;
820
821 case ISMACR:
822 if (map[key].function != 0)
823 {
824 macro = savestring ((char *)map[key].function);
825 _rl_with_macro_input (macro);
826 return 0;
827 }
828 break;
829 }
830#if defined (VI_MODE)
831 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
832 key != ANYOTHERKEY &&
833 _rl_vi_textmod_command (key))
834 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
835#endif
836
837 return (r);
838}
839
840static int
841_rl_subseq_result (r, map, key, got_subseq)
842 int r;
843 Keymap map;
844 int key, got_subseq;
845{
846 Keymap m;
847 int type, nt;
848 rl_command_func_t *func, *nf;
849
850 if (r == -2)
851 /* We didn't match anything, and the keymap we're indexed into
852 shadowed a function previously bound to that prefix. Call
853 the function. The recursive call to _rl_dispatch_subseq has
854 already taken care of pushing any necessary input back onto
855 the input queue with _rl_unget_char. */
856 {
857 m = _rl_dispatching_keymap;
858 type = m[ANYOTHERKEY].type;
859 func = m[ANYOTHERKEY].function;
860 if (type == ISFUNC && func == rl_do_lowercase_version)
861 r = _rl_dispatch (_rl_to_lower (key), map);
862 else if (type == ISFUNC && func == rl_insert)
863 {
864 /* If the function that was shadowed was self-insert, we
865 somehow need a keymap with map[key].func == self-insert.
866 Let's use this one. */
867 nt = m[key].type;
868 nf = m[key].function;
869
870 m[key].type = type;
871 m[key].function = func;
872 r = _rl_dispatch (key, m);
873 m[key].type = nt;
874 m[key].function = nf;
875 }
876 else
877 r = _rl_dispatch (ANYOTHERKEY, m);
878 }
879 else if (r && map[ANYOTHERKEY].function)
880 {
881 /* We didn't match (r is probably -1), so return something to
882 tell the caller that it should try ANYOTHERKEY for an
883 overridden function. */
884 _rl_unget_char (key);
885 _rl_dispatching_keymap = map;
886 return -2;
887 }
888 else if (r && got_subseq)
889 {
890 /* OK, back up the chain. */
891 _rl_unget_char (key);
892 _rl_dispatching_keymap = map;
893 return -1;
894 }
895
896 return r;
897}
898
899/* **************************************************************** */
900/* */
901/* Initializations */
902/* */
903/* **************************************************************** */
904
905/* Initialize readline (and terminal if not already). */
906int
907rl_initialize ()
908{
909 /* If we have never been called before, initialize the
910 terminal and data structures. */
911 if (!rl_initialized)
912 {
913 RL_SETSTATE(RL_STATE_INITIALIZING);
914 readline_initialize_everything ();
915 RL_UNSETSTATE(RL_STATE_INITIALIZING);
916 rl_initialized++;
917 RL_SETSTATE(RL_STATE_INITIALIZED);
918 }
919
920 /* Initalize the current line information. */
921 _rl_init_line_state ();
922
923 /* We aren't done yet. We haven't even gotten started yet! */
924 rl_done = 0;
925 RL_UNSETSTATE(RL_STATE_DONE);
926
927 /* Tell the history routines what is going on. */
928 _rl_start_using_history ();
929
930 /* Make the display buffer match the state of the line. */
931 rl_reset_line_state ();
932
933 /* No such function typed yet. */
934 rl_last_func = (rl_command_func_t *)NULL;
935
936 /* Parsing of key-bindings begins in an enabled state. */
937 _rl_parsing_conditionalized_out = 0;
938
939#if defined (VI_MODE)
940 if (rl_editing_mode == vi_mode)
941 _rl_vi_initialize_line ();
942#endif
943
944 /* Each line starts in insert mode (the default). */
945 _rl_set_insert_mode (RL_IM_DEFAULT, 1);
946
947 return 0;
948}
949
950#if 0
951#if defined (__EMX__)
952static void
953_emx_build_environ ()
954{
955 TIB *tibp;
956 PIB *pibp;
957 char *t, **tp;
958 int c;
959
960 DosGetInfoBlocks (&tibp, &pibp);
961 t = pibp->pib_pchenv;
962 for (c = 1; *t; c++)
963 t += strlen (t) + 1;
964 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
965 t = pibp->pib_pchenv;
966 while (*t)
967 {
968 *tp++ = t;
969 t += strlen (t) + 1;
970 }
971 *tp = 0;
972}
973#endif /* __EMX__ */
974#endif
975
976/* Initialize the entire state of the world. */
977static void
978readline_initialize_everything ()
979{
980#if 0
981#if defined (__EMX__)
982 if (environ == 0)
983 _emx_build_environ ();
984#endif
985#endif
986
987#if 0
988 /* Find out if we are running in Emacs -- UNUSED. */
989 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
990#endif
991
992 /* Set up input and output if they are not already set up. */
993 if (!rl_instream)
994 rl_instream = stdin;
995
996 if (!rl_outstream)
997 rl_outstream = stdout;
998
999 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
1000 may change, but they may also be used before readline_internal ()
1001 is called. */
1002 _rl_in_stream = rl_instream;
1003 _rl_out_stream = rl_outstream;
1004
1005 /* Allocate data structures. */
1006 if (rl_line_buffer == 0)
1007 rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1008
1009 /* Initialize the terminal interface. */
1010 if (rl_terminal_name == 0)
1011 rl_terminal_name = sh_get_env_value ("TERM");
1012 _rl_init_terminal_io (rl_terminal_name);
1013
1014 /* Bind tty characters to readline functions. */
1015 readline_default_bindings ();
1016
1017 /* Initialize the function names. */
1018 rl_initialize_funmap ();
1019
1020 /* Decide whether we should automatically go into eight-bit mode. */
1021 _rl_init_eightbit ();
1022
1023 /* Read in the init file. */
1024 rl_read_init_file ((char *)NULL);
1025
1026 /* XXX */
1027 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
1028 {
1029 _rl_screenwidth--;
1030 _rl_screenchars -= _rl_screenheight;
1031 }
1032
1033 /* Override the effect of any `set keymap' assignments in the
1034 inputrc file. */
1035 rl_set_keymap_from_edit_mode ();
1036
1037 /* Try to bind a common arrow key prefix, if not already bound. */
1038 bind_arrow_keys ();
1039
1040 /* Enable the meta key, if this terminal has one. */
1041 if (_rl_enable_meta)
1042 _rl_enable_meta_key ();
1043
1044 /* If the completion parser's default word break characters haven't
1045 been set yet, then do so now. */
1046 if (rl_completer_word_break_characters == (char *)NULL)
1047 rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
1048}
1049
1050/* If this system allows us to look at the values of the regular
1051 input editing characters, then bind them to their readline
1052 equivalents, iff the characters are not bound to keymaps. */
1053static void
1054readline_default_bindings ()
1055{
1056 if (_rl_bind_stty_chars)
1057 rl_tty_set_default_bindings (_rl_keymap);
1058}
1059
1060/* Reset the default bindings for the terminal special characters we're
1061 interested in back to rl_insert and read the new ones. */
1062static void
1063reset_default_bindings ()
1064{
1065 if (_rl_bind_stty_chars)
1066 {
1067 rl_tty_unset_default_bindings (_rl_keymap);
1068 rl_tty_set_default_bindings (_rl_keymap);
1069 }
1070}
1071
1072/* Bind some common arrow key sequences in MAP. */
1073static void
1074bind_arrow_keys_internal (map)
1075 Keymap map;
1076{
1077 Keymap xkeymap;
1078
1079 xkeymap = _rl_keymap;
1080 _rl_keymap = map;
1081
1082#if defined (__MSDOS__)
1083 rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
1084 rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
1085 rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
1086 rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
1087#endif
1088
1089 rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
1090 rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
1091 rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
1092 rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
1093 rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
1094 rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
1095#if defined (__OS2__)
1096 rl_bind_keyseq_if_unbound ("\033[4h", rl_overwrite_mode);
1097#endif
1098
1099 rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
1100 rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
1101 rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
1102 rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
1103 rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
1104 rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
1105
1106#if defined (__MINGW32__)
1107 rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
1108 rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
1109 rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
1110 rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
1111#endif
1112
1113 _rl_keymap = xkeymap;
1114}
1115
1116/* Try and bind the common arrow key prefixes after giving termcap and
1117 the inputrc file a chance to bind them and create `real' keymaps
1118 for the arrow key prefix. */
1119static void
1120bind_arrow_keys ()
1121{
1122 bind_arrow_keys_internal (emacs_standard_keymap);
1123
1124#if defined (VI_MODE)
1125 bind_arrow_keys_internal (vi_movement_keymap);
1126 bind_arrow_keys_internal (vi_insertion_keymap);
1127#endif
1128}
1129
1130/* **************************************************************** */
1131/* */
1132/* Saving and Restoring Readline's state */
1133/* */
1134/* **************************************************************** */
1135
1136int
1137rl_save_state (sp)
1138 struct readline_state *sp;
1139{
1140 if (sp == 0)
1141 return -1;
1142
1143 sp->point = rl_point;
1144 sp->end = rl_end;
1145 sp->mark = rl_mark;
1146 sp->buffer = rl_line_buffer;
1147 sp->buflen = rl_line_buffer_len;
1148 sp->ul = rl_undo_list;
1149 sp->prompt = rl_prompt;
1150
1151 sp->rlstate = rl_readline_state;
1152 sp->done = rl_done;
1153 sp->kmap = _rl_keymap;
1154
1155 sp->lastfunc = rl_last_func;
1156 sp->insmode = rl_insert_mode;
1157 sp->edmode = rl_editing_mode;
1158 sp->kseqlen = rl_key_sequence_length;
1159 sp->inf = rl_instream;
1160 sp->outf = rl_outstream;
1161 sp->pendingin = rl_pending_input;
1162 sp->macro = rl_executing_macro;
1163
1164 sp->catchsigs = rl_catch_signals;
1165 sp->catchsigwinch = rl_catch_sigwinch;
1166
1167 return (0);
1168}
1169
1170int
1171rl_restore_state (sp)
1172 struct readline_state *sp;
1173{
1174 if (sp == 0)
1175 return -1;
1176
1177 rl_point = sp->point;
1178 rl_end = sp->end;
1179 rl_mark = sp->mark;
1180 the_line = rl_line_buffer = sp->buffer;
1181 rl_line_buffer_len = sp->buflen;
1182 rl_undo_list = sp->ul;
1183 rl_prompt = sp->prompt;
1184
1185 rl_readline_state = sp->rlstate;
1186 rl_done = sp->done;
1187 _rl_keymap = sp->kmap;
1188
1189 rl_last_func = sp->lastfunc;
1190 rl_insert_mode = sp->insmode;
1191 rl_editing_mode = sp->edmode;
1192 rl_key_sequence_length = sp->kseqlen;
1193 rl_instream = sp->inf;
1194 rl_outstream = sp->outf;
1195 rl_pending_input = sp->pendingin;
1196 rl_executing_macro = sp->macro;
1197
1198 rl_catch_signals = sp->catchsigs;
1199 rl_catch_sigwinch = sp->catchsigwinch;
1200
1201 return (0);
1202}
Note: See TracBrowser for help on using the repository browser.