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