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