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