1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4 /* Copyright (C) 1987-2022 Free Software Foundation, Inc.
5
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.
8
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
12 (at your option) any later version.
13
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
17 GNU General Public License for more details.
18
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
23 #define READLINE_LIBRARY
24
25 #if defined (HAVE_CONFIG_H)
26 # include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #include "posixstat.h"
31 #include <fcntl.h>
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
35
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39
40 #if defined (HAVE_STDLIB_H)
41 # include <stdlib.h>
42 #else
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
45
46 #if defined (HAVE_LOCALE_H)
47 # include <locale.h>
48 #endif
49
50 #include <stdio.h>
51 #include "posixjmp.h"
52 #include <errno.h>
53
54 #if !defined (errno)
55 extern int errno;
56 #endif /* !errno */
57
58 /* System-specific feature definitions and include files. */
59 #include "rldefs.h"
60 #include "rlmbutil.h"
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
71 #include "rlprivate.h"
72 #include "rlshell.h"
73 #include "xmalloc.h"
74
75 #ifndef RL_LIBRARY_VERSION
76 # define RL_LIBRARY_VERSION "8.0"
77 #endif
78
79 #ifndef RL_READLINE_VERSION
80 # define RL_READLINE_VERSION 0x0800
81 #endif
82
83 extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
84
85 #if defined (COLOR_SUPPORT)
86 extern void _rl_parse_colors PARAMS((void)); /* XXX */
87 #endif
88
89
90 /* Forward declarations used in this file. */
91 static char *readline_internal PARAMS((void));
92 static void readline_initialize_everything PARAMS((void));
93
94 static void bind_arrow_keys_internal PARAMS((Keymap));
95 static void bind_arrow_keys PARAMS((void));
96
97 static void bind_bracketed_paste_prefix PARAMS((void));
98
99 static void readline_default_bindings PARAMS((void));
100 static void reset_default_bindings PARAMS((void));
101
102 static int _rl_subseq_result PARAMS((int, Keymap, int, int));
103 static int _rl_subseq_getchar PARAMS((int));
104
105 /* **************************************************************** */
106 /* */
107 /* Line editing input utility */
108 /* */
109 /* **************************************************************** */
110
111 const char *rl_library_version = RL_LIBRARY_VERSION;
112
113 int rl_readline_version = RL_READLINE_VERSION;
114
115 /* True if this is `real' readline as opposed to some stub substitute. */
116 int rl_gnu_readline_p = 1;
117
118 /* A pointer to the keymap that is currently in use.
119 By default, it is the standard emacs keymap. */
120 Keymap _rl_keymap = emacs_standard_keymap;
121
122 /* The current style of editing. */
123 int rl_editing_mode = emacs_mode;
124
125 /* The current insert mode: input (the default) or overwrite */
126 int rl_insert_mode = RL_IM_DEFAULT;
127
128 /* Non-zero if we called this function from _rl_dispatch(). It's present
129 so functions can find out whether they were called from a key binding
130 or directly from an application. */
131 int rl_dispatching;
132
133 /* Non-zero if the previous command was a kill command. */
134 int _rl_last_command_was_kill = 0;
135
136 /* The current value of the numeric argument specified by the user. */
137 int rl_numeric_arg = 1;
138
139 /* Non-zero if an argument was typed. */
140 int rl_explicit_arg = 0;
141
142 /* Temporary value used while generating the argument. */
143 int rl_arg_sign = 1;
144
145 /* Non-zero means we have been called at least once before. */
146 static int rl_initialized;
147
148 #if 0
149 /* If non-zero, this program is running in an EMACS buffer. */
150 static int running_in_emacs;
151 #endif
152
153 /* Flags word encapsulating the current readline state. */
154 unsigned long rl_readline_state = RL_STATE_NONE;
155
156 /* The current offset in the current input line. */
157 int rl_point;
158
159 /* Mark in the current input line. */
160 int rl_mark;
161
162 /* Length of the current input line. */
163 int rl_end;
164
165 /* Make this non-zero to return the current input_line. */
166 int rl_done;
167
168 /* If non-zero when readline_internal returns, it means we found EOF */
169 int rl_eof_found = 0;
170
171 /* The last function executed by readline. */
172 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
173
174 /* Top level environment for readline_internal (). */
175 procenv_t _rl_top_level;
176
177 /* The streams we interact with. */
178 FILE *_rl_in_stream, *_rl_out_stream;
179
180 /* The names of the streams that we do input and output to. */
181 FILE *rl_instream = (FILE *)NULL;
182 FILE *rl_outstream = (FILE *)NULL;
183
184 /* Non-zero means echo characters as they are read. Defaults to no echo;
185 set to 1 if there is a controlling terminal, we can get its attributes,
186 and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
187 for the code that sets it. */
188 int _rl_echoing_p = 0;
189
190 /* Current prompt. */
191 char *rl_prompt = (char *)NULL;
192 int rl_visible_prompt_length = 0;
193
194 /* Set to non-zero by calling application if it has already printed rl_prompt
195 and does not want readline to do it the first time. */
196 int rl_already_prompted = 0;
197
198 /* The number of characters read in order to type this complete command. */
199 int rl_key_sequence_length = 0;
200
201 /* If non-zero, then this is the address of a function to call just
202 before readline_internal_setup () prints the first prompt. */
203 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
204
205 /* Any readline function can set this and have it run just before the user's
206 rl_startup_hook. */
207 rl_hook_func_t *_rl_internal_startup_hook = (rl_hook_func_t *)NULL;
208
209 /* If non-zero, this is the address of a function to call just before
210 readline_internal_setup () returns and readline_internal starts
211 reading input characters. */
212 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
213
214 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
215 static char *the_line;
216
217 /* The character that can generate an EOF. Really read from
218 the terminal driver... just defaulted here. */
219 int _rl_eof_char = CTRL ('D');
220
221 /* Non-zero makes this the next keystroke to read. */
222 int rl_pending_input = 0;
223
224 /* Pointer to a useful terminal name. */
225 const char *rl_terminal_name = (const char *)NULL;
226
227 /* Non-zero means to always use horizontal scrolling in line display. */
228 int _rl_horizontal_scroll_mode = 0;
229
230 /* Non-zero means to display an asterisk at the starts of history lines
231 which have been modified. */
232 int _rl_mark_modified_lines = 0;
233
234 /* The style of `bell' notification preferred. This can be set to NO_BELL,
235 AUDIBLE_BELL, or VISIBLE_BELL. */
236 int _rl_bell_preference = AUDIBLE_BELL;
237
238 /* String inserted into the line by rl_insert_comment (). */
239 char *_rl_comment_begin;
240
241 /* Keymap holding the function currently being executed. */
242 Keymap rl_executing_keymap;
243
244 /* Keymap we're currently using to dispatch. */
245 Keymap _rl_dispatching_keymap;
246
247 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
248 int rl_erase_empty_line = 0;
249
250 /* Non-zero means to read only this many characters rather than up to a
251 character bound to accept-line. */
252 int rl_num_chars_to_read = 0;
253
254 /* Line buffer and maintenance. */
255 char *rl_line_buffer = (char *)NULL;
256 int rl_line_buffer_len = 0;
257
258 /* Key sequence `contexts' */
259 _rl_keyseq_cxt *_rl_kscxt = 0;
260
261 int rl_executing_key;
262 char *rl_executing_keyseq = 0;
263 int _rl_executing_keyseq_size = 0;
264
265 struct _rl_cmd _rl_pending_command;
266 struct _rl_cmd *_rl_command_to_execute = (struct _rl_cmd *)NULL;
267
268 /* Timeout (specified in milliseconds) when reading characters making up an
269 ambiguous multiple-key sequence */
270 int _rl_keyseq_timeout = 500;
271
272 #define RESIZE_KEYSEQ_BUFFER() \
273 do \
274 { \
275 if (rl_key_sequence_length + 2 >= _rl_executing_keyseq_size) \
276 { \
277 _rl_executing_keyseq_size += 16; \
278 rl_executing_keyseq = xrealloc (rl_executing_keyseq, _rl_executing_keyseq_size); \
279 } \
280 } \
281 while (0);
282
283 /* Forward declarations used by the display, termcap, and history code. */
284
285 /* **************************************************************** */
286 /* */
287 /* `Forward' declarations */
288 /* */
289 /* **************************************************************** */
290
291 /* Non-zero means do not parse any lines other than comments and
292 parser directives. */
293 unsigned char _rl_parsing_conditionalized_out = 0;
294
295 /* Non-zero means to convert characters with the meta bit set to
296 escape-prefixed characters so we can indirect through
297 emacs_meta_keymap or vi_escape_keymap. */
298 int _rl_convert_meta_chars_to_ascii = 1;
299
300 /* Non-zero means to output characters with the meta bit set directly
301 rather than as a meta-prefixed escape sequence. */
302 int _rl_output_meta_chars = 0;
303
304 /* Non-zero means to look at the termios special characters and bind
305 them to equivalent readline functions at startup. */
306 int _rl_bind_stty_chars = 1;
307
308 /* Non-zero means to go through the history list at every newline (or
309 whenever rl_done is set and readline returns) and revert each line to
310 its initial state. */
311 int _rl_revert_all_at_newline = 0;
312
313 /* Non-zero means to honor the termios ECHOCTL bit and echo control
314 characters corresponding to keyboard-generated signals. */
315 int _rl_echo_control_chars = 1;
316
317 /* Non-zero means to prefix the displayed prompt with a character indicating
318 the editing mode: @ for emacs, : for vi-command, + for vi-insert. */
319 int _rl_show_mode_in_prompt = 0;
320
321 /* Non-zero means to attempt to put the terminal in `bracketed paste mode',
322 where it will prefix pasted text with an escape sequence and send
323 another to mark the end of the paste. */
324 int _rl_enable_bracketed_paste = BRACKETED_PASTE_DEFAULT;
325 int _rl_enable_active_region = BRACKETED_PASTE_DEFAULT;
326
327 /* **************************************************************** */
328 /* */
329 /* Top Level Functions */
330 /* */
331 /* **************************************************************** */
332
333 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
334 int _rl_meta_flag = 0; /* Forward declaration */
335
336 /* Set up the prompt and expand it. Called from readline() and
337 rl_callback_handler_install (). */
338 int
rl_set_prompt(const char * prompt)339 rl_set_prompt (const char *prompt)
340 {
341 FREE (rl_prompt);
342 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
343 rl_display_prompt = rl_prompt ? rl_prompt : "";
344
345 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
346 return 0;
347 }
348
349 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
350 none. A return value of NULL means that EOF was encountered. */
351 char *
readline(const char * prompt)352 readline (const char *prompt)
353 {
354 char *value;
355 #if 0
356 int in_callback;
357 #endif
358
359 /* If we are at EOF return a NULL string. */
360 if (rl_pending_input == EOF)
361 {
362 rl_clear_pending_input ();
363 return ((char *)NULL);
364 }
365
366 #if 0
367 /* If readline() is called after installing a callback handler, temporarily
368 turn off the callback state to avoid ensuing messiness. Patch supplied
369 by the gdb folks. XXX -- disabled. This can be fooled and readline
370 left in a strange state by a poorly-timed longjmp. */
371 if (in_callback = RL_ISSTATE (RL_STATE_CALLBACK))
372 RL_UNSETSTATE (RL_STATE_CALLBACK);
373 #endif
374
375 rl_set_prompt (prompt);
376
377 rl_initialize ();
378 if (rl_prep_term_function)
379 (*rl_prep_term_function) (_rl_meta_flag);
380
381 #if defined (HANDLE_SIGNALS)
382 rl_set_signals ();
383 #endif
384
385 value = readline_internal ();
386 if (rl_deprep_term_function)
387 (*rl_deprep_term_function) ();
388
389 #if defined (HANDLE_SIGNALS)
390 rl_clear_signals ();
391 #endif
392
393 #if 0
394 if (in_callback)
395 RL_SETSTATE (RL_STATE_CALLBACK);
396 #endif
397
398 #if HAVE_DECL_AUDIT_USER_TTY && defined (HAVE_LIBAUDIT_H) && defined (ENABLE_TTY_AUDIT_SUPPORT)
399 if (value)
400 _rl_audit_tty (value);
401 #endif
402
403 return (value);
404 }
405
406 #if defined (READLINE_CALLBACKS)
407 # define STATIC_CALLBACK
408 #else
409 # define STATIC_CALLBACK static
410 #endif
411
412 STATIC_CALLBACK void
readline_internal_setup(void)413 readline_internal_setup (void)
414 {
415 char *nprompt;
416
417 _rl_in_stream = rl_instream;
418 _rl_out_stream = rl_outstream;
419
420 /* Enable the meta key only for the duration of readline(), if this
421 terminal has one and the terminal has been initialized */
422 if (_rl_enable_meta & RL_ISSTATE (RL_STATE_TERMPREPPED))
423 _rl_enable_meta_key ();
424
425 if (rl_startup_hook)
426 (*rl_startup_hook) ();
427
428 if (_rl_internal_startup_hook)
429 (*_rl_internal_startup_hook) ();
430
431 rl_deactivate_mark ();
432
433 #if defined (VI_MODE)
434 if (rl_editing_mode == vi_mode)
435 rl_vi_insertion_mode (1, 'i'); /* don't want to reset last */
436 else
437 #endif /* VI_MODE */
438 if (_rl_show_mode_in_prompt)
439 _rl_reset_prompt ();
440
441 /* If we're not echoing, we still want to at least print a prompt, because
442 rl_redisplay will not do it for us. If the calling application has a
443 custom redisplay function, though, let that function handle it. */
444 if (_rl_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
445 {
446 if (rl_prompt && rl_already_prompted == 0)
447 {
448 nprompt = _rl_strip_prompt (rl_prompt);
449 fprintf (_rl_out_stream, "%s", nprompt);
450 fflush (_rl_out_stream);
451 xfree (nprompt);
452 }
453 }
454 else
455 {
456 if (rl_prompt && rl_already_prompted)
457 rl_on_new_line_with_prompt ();
458 else
459 rl_on_new_line ();
460 (*rl_redisplay_function) ();
461 }
462
463 if (rl_pre_input_hook)
464 (*rl_pre_input_hook) ();
465
466 RL_CHECK_SIGNALS ();
467 }
468
469 STATIC_CALLBACK char *
readline_internal_teardown(int eof)470 readline_internal_teardown (int eof)
471 {
472 char *temp;
473 HIST_ENTRY *entry;
474
475 RL_CHECK_SIGNALS ();
476
477 if (eof)
478 RL_SETSTATE (RL_STATE_EOF); /* XXX */
479
480 /* Restore the original of this history line, iff the line that we
481 are editing was originally in the history, AND the line has changed. */
482 entry = current_history ();
483
484 if (entry && rl_undo_list)
485 {
486 temp = savestring (the_line);
487 rl_revert_line (1, 0);
488 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
489 _rl_free_history_entry (entry);
490
491 strcpy (the_line, temp);
492 xfree (temp);
493 }
494
495 if (_rl_revert_all_at_newline)
496 _rl_revert_all_lines ();
497
498 /* At any rate, it is highly likely that this line has an undo list. Get
499 rid of it now. */
500 if (rl_undo_list)
501 rl_free_undo_list ();
502
503 /* Disable the meta key, if this terminal has one and we were told to use it.
504 The check whether or not we sent the enable string is in
505 _rl_disable_meta_key(); the flag is set in _rl_enable_meta_key */
506 _rl_disable_meta_key ();
507
508 /* Restore normal cursor, if available. */
509 _rl_set_insert_mode (RL_IM_INSERT, 0);
510
511 return (eof ? (char *)NULL : savestring (the_line));
512 }
513
514 void
_rl_internal_char_cleanup(void)515 _rl_internal_char_cleanup (void)
516 {
517 #if defined (VI_MODE)
518 /* In vi mode, when you exit insert mode, the cursor moves back
519 over the previous character. We explicitly check for that here. */
520 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
521 rl_vi_check ();
522 #endif /* VI_MODE */
523
524 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
525 {
526 (*rl_redisplay_function) ();
527 _rl_want_redisplay = 0;
528 rl_newline (1, '\n');
529 }
530
531 if (rl_done == 0)
532 {
533 (*rl_redisplay_function) ();
534 _rl_want_redisplay = 0;
535 }
536
537 /* If the application writer has told us to erase the entire line if
538 the only character typed was something bound to rl_newline, do so. */
539 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
540 rl_point == 0 && rl_end == 0)
541 _rl_erase_entire_line ();
542 }
543
544 STATIC_CALLBACK int
545 #if defined (READLINE_CALLBACKS)
readline_internal_char(void)546 readline_internal_char (void)
547 #else
548 readline_internal_charloop (void)
549 #endif
550 {
551 static int lastc, eof_found;
552 int c, code, lk, r;
553
554 lastc = EOF;
555
556 #if !defined (READLINE_CALLBACKS)
557 eof_found = 0;
558 while (rl_done == 0)
559 {
560 #endif
561 lk = _rl_last_command_was_kill;
562
563 #if defined (HAVE_POSIX_SIGSETJMP)
564 code = sigsetjmp (_rl_top_level, 0);
565 #else
566 code = setjmp (_rl_top_level);
567 #endif
568
569 if (code)
570 {
571 (*rl_redisplay_function) ();
572 _rl_want_redisplay = 0;
573 /* If we get here, we're not being called from something dispatched
574 from _rl_callback_read_char(), which sets up its own value of
575 _rl_top_level (saving and restoring the old, of course), so
576 we can just return here. */
577 if (RL_ISSTATE (RL_STATE_CALLBACK))
578 return (0);
579 }
580
581 if (rl_pending_input == 0)
582 {
583 /* Then initialize the argument and number of keys read. */
584 _rl_reset_argument ();
585 rl_executing_keyseq[rl_key_sequence_length = 0] = '\0';
586 }
587
588 RL_SETSTATE(RL_STATE_READCMD);
589 c = rl_read_key ();
590 RL_UNSETSTATE(RL_STATE_READCMD);
591
592 /* look at input.c:rl_getc() for the circumstances under which this will
593 be returned; punt immediately on read error without converting it to
594 a newline; assume that rl_read_key has already called the signal
595 handler. */
596 if (c == READERR)
597 {
598 #if defined (READLINE_CALLBACKS)
599 RL_SETSTATE(RL_STATE_DONE);
600 return (rl_done = 1);
601 #else
602 RL_SETSTATE(RL_STATE_EOF);
603 eof_found = 1;
604 break;
605 #endif
606 }
607
608 /* EOF typed to a non-blank line is ^D the first time, EOF the second
609 time in a row. This won't return any partial line read from the tty.
610 If we want to change this, to force any existing line to be returned
611 when read(2) reads EOF, for example, this is the place to change. */
612 if (c == EOF && rl_end)
613 {
614 if (RL_SIG_RECEIVED ())
615 {
616 RL_CHECK_SIGNALS ();
617 if (rl_signal_event_hook)
618 (*rl_signal_event_hook) (); /* XXX */
619 }
620
621 /* XXX - reading two consecutive EOFs returns EOF */
622 if (RL_ISSTATE (RL_STATE_TERMPREPPED))
623 {
624 if (lastc == _rl_eof_char || lastc == EOF)
625 rl_end = 0;
626 else
627 c = _rl_eof_char;
628 }
629 else
630 c = NEWLINE;
631 }
632
633 /* The character _rl_eof_char typed to blank line, and not as the
634 previous character is interpreted as EOF. This doesn't work when
635 READLINE_CALLBACKS is defined, so hitting a series of ^Ds will
636 erase all the chars on the line and then return EOF. */
637 if (((c == _rl_eof_char && lastc != c) || c == EOF) && rl_end == 0)
638 {
639 #if defined (READLINE_CALLBACKS)
640 RL_SETSTATE(RL_STATE_DONE);
641 return (rl_done = 1);
642 #else
643 RL_SETSTATE(RL_STATE_EOF);
644 eof_found = 1;
645 break;
646 #endif
647 }
648
649 lastc = c;
650 r = _rl_dispatch ((unsigned char)c, _rl_keymap);
651 RL_CHECK_SIGNALS ();
652
653 if (_rl_command_to_execute)
654 {
655 (*rl_redisplay_function) ();
656
657 rl_executing_keymap = _rl_command_to_execute->map;
658 rl_executing_key = _rl_command_to_execute->key;
659
660 rl_dispatching = 1;
661 RL_SETSTATE(RL_STATE_DISPATCHING);
662 r = (*(_rl_command_to_execute->func)) (_rl_command_to_execute->count, _rl_command_to_execute->key);
663 _rl_command_to_execute = 0;
664 RL_UNSETSTATE(RL_STATE_DISPATCHING);
665 rl_dispatching = 0;
666
667 RL_CHECK_SIGNALS ();
668 }
669
670 /* If there was no change in _rl_last_command_was_kill, then no kill
671 has taken place. Note that if input is pending we are reading
672 a prefix command, so nothing has changed yet. */
673 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
674 _rl_last_command_was_kill = 0;
675
676 if (_rl_keep_mark_active)
677 _rl_keep_mark_active = 0;
678 else if (rl_mark_active_p ())
679 rl_deactivate_mark ();
680
681 _rl_internal_char_cleanup ();
682
683 #if defined (READLINE_CALLBACKS)
684 return 0;
685 #else
686 }
687
688 return (eof_found);
689 #endif
690 }
691
692 #if defined (READLINE_CALLBACKS)
693 static int
readline_internal_charloop(void)694 readline_internal_charloop (void)
695 {
696 int eof = 1;
697
698 while (rl_done == 0)
699 eof = readline_internal_char ();
700 return (eof);
701 }
702 #endif /* READLINE_CALLBACKS */
703
704 /* Read a line of input from the global rl_instream, doing output on
705 the global rl_outstream.
706 If rl_prompt is non-null, then that is our prompt. */
707 static char *
readline_internal(void)708 readline_internal (void)
709 {
710 readline_internal_setup ();
711 rl_eof_found = readline_internal_charloop ();
712 return (readline_internal_teardown (rl_eof_found));
713 }
714
715 void
_rl_init_line_state(void)716 _rl_init_line_state (void)
717 {
718 rl_point = rl_end = rl_mark = 0;
719 the_line = rl_line_buffer;
720 the_line[0] = 0;
721 }
722
723 void
_rl_set_the_line(void)724 _rl_set_the_line (void)
725 {
726 the_line = rl_line_buffer;
727 }
728
729 #if defined (READLINE_CALLBACKS)
730 _rl_keyseq_cxt *
_rl_keyseq_cxt_alloc(void)731 _rl_keyseq_cxt_alloc (void)
732 {
733 _rl_keyseq_cxt *cxt;
734
735 cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
736
737 cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
738
739 cxt->okey = 0;
740 cxt->ocxt = _rl_kscxt;
741 cxt->childval = 42; /* sentinel value */
742
743 return cxt;
744 }
745
746 void
_rl_keyseq_cxt_dispose(_rl_keyseq_cxt * cxt)747 _rl_keyseq_cxt_dispose (_rl_keyseq_cxt *cxt)
748 {
749 xfree (cxt);
750 }
751
752 void
_rl_keyseq_chain_dispose(void)753 _rl_keyseq_chain_dispose (void)
754 {
755 _rl_keyseq_cxt *cxt;
756
757 while (_rl_kscxt)
758 {
759 cxt = _rl_kscxt;
760 _rl_kscxt = _rl_kscxt->ocxt;
761 _rl_keyseq_cxt_dispose (cxt);
762 }
763 }
764 #endif
765
766 static int
_rl_subseq_getchar(int key)767 _rl_subseq_getchar (int key)
768 {
769 int k;
770
771 if (key == ESC)
772 RL_SETSTATE(RL_STATE_METANEXT);
773 RL_SETSTATE(RL_STATE_MOREINPUT);
774 k = rl_read_key ();
775 RL_UNSETSTATE(RL_STATE_MOREINPUT);
776 if (key == ESC)
777 RL_UNSETSTATE(RL_STATE_METANEXT);
778
779 return k;
780 }
781
782 #if defined (READLINE_CALLBACKS)
783 int
_rl_dispatch_callback(_rl_keyseq_cxt * cxt)784 _rl_dispatch_callback (_rl_keyseq_cxt *cxt)
785 {
786 int nkey, r;
787
788 /* For now */
789 /* The first time this context is used, we want to read input and dispatch
790 on it. When traversing the chain of contexts back `up', we want to use
791 the value from the next context down. We're simulating recursion using
792 a chain of contexts. */
793 if ((cxt->flags & KSEQ_DISPATCHED) == 0)
794 {
795 nkey = _rl_subseq_getchar (cxt->okey);
796 if (nkey < 0)
797 {
798 _rl_abort_internal ();
799 return -1;
800 }
801 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
802 cxt->flags |= KSEQ_DISPATCHED;
803 }
804 else
805 r = cxt->childval;
806
807 /* For now */
808 if (r != -3) /* don't do this if we indicate there will be other matches */
809 r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
810
811 RL_CHECK_SIGNALS ();
812 /* We only treat values < 0 specially to simulate recursion. */
813 if (r >= 0 || (r == -1 && (cxt->flags & KSEQ_SUBSEQ) == 0)) /* success! or failure! */
814 {
815 _rl_keyseq_chain_dispose ();
816 RL_UNSETSTATE (RL_STATE_MULTIKEY);
817 return r;
818 }
819
820 if (r != -3) /* magic value that says we added to the chain */
821 _rl_kscxt = cxt->ocxt;
822 if (_rl_kscxt)
823 _rl_kscxt->childval = r;
824 if (r != -3)
825 _rl_keyseq_cxt_dispose (cxt);
826
827 return r;
828 }
829 #endif /* READLINE_CALLBACKS */
830
831 /* Do the command associated with KEY in MAP.
832 If the associated command is really a keymap, then read
833 another key, and dispatch into that map. */
834 int
_rl_dispatch(register int key,Keymap map)835 _rl_dispatch (register int key, Keymap map)
836 {
837 _rl_dispatching_keymap = map;
838 return _rl_dispatch_subseq (key, map, 0);
839 }
840
841 int
_rl_dispatch_subseq(register int key,Keymap map,int got_subseq)842 _rl_dispatch_subseq (register int key, Keymap map, int got_subseq)
843 {
844 int r, newkey;
845 char *macro;
846 rl_command_func_t *func;
847 #if defined (READLINE_CALLBACKS)
848 _rl_keyseq_cxt *cxt;
849 #endif
850
851 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
852 {
853 if (map[ESC].type == ISKMAP)
854 {
855 if (RL_ISSTATE (RL_STATE_MACRODEF))
856 _rl_add_macro_char (ESC);
857 RESIZE_KEYSEQ_BUFFER ();
858 rl_executing_keyseq[rl_key_sequence_length++] = ESC;
859 map = FUNCTION_TO_KEYMAP (map, ESC);
860 key = UNMETA (key);
861 return (_rl_dispatch (key, map));
862 }
863 else
864 rl_ding ();
865 return 0;
866 }
867
868 if (RL_ISSTATE (RL_STATE_MACRODEF))
869 _rl_add_macro_char (key);
870
871 r = 0;
872 switch (map[key].type)
873 {
874 case ISFUNC:
875 func = map[key].function;
876 if (func)
877 {
878 /* Special case rl_do_lowercase_version (). */
879 if (func == rl_do_lowercase_version)
880 /* Should we do anything special if key == ANYOTHERKEY? */
881 return (_rl_dispatch (_rl_to_lower ((unsigned char)key), map));
882
883 rl_executing_keymap = map;
884 rl_executing_key = key;
885
886 RESIZE_KEYSEQ_BUFFER();
887 rl_executing_keyseq[rl_key_sequence_length++] = key;
888 rl_executing_keyseq[rl_key_sequence_length] = '\0';
889
890 rl_dispatching = 1;
891 RL_SETSTATE(RL_STATE_DISPATCHING);
892 r = (*func) (rl_numeric_arg * rl_arg_sign, key);
893 RL_UNSETSTATE(RL_STATE_DISPATCHING);
894 rl_dispatching = 0;
895
896 /* If we have input pending, then the last command was a prefix
897 command. Don't change the state of rl_last_func. Otherwise,
898 remember the last command executed in this variable. */
899 #if defined (VI_MODE)
900 if (rl_pending_input == 0 && map[key].function != rl_digit_argument && map[key].function != rl_vi_arg_digit)
901 #else
902 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
903 #endif
904 rl_last_func = map[key].function;
905
906 RL_CHECK_SIGNALS ();
907 }
908 else if (map[ANYOTHERKEY].function)
909 {
910 /* OK, there's no function bound in this map, but there is a
911 shadow function that was overridden when the current keymap
912 was created. Return -2 to note that. */
913 if (RL_ISSTATE (RL_STATE_MACROINPUT))
914 _rl_prev_macro_key ();
915 else
916 _rl_unget_char (key);
917 if (rl_key_sequence_length > 0)
918 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
919 return -2;
920 }
921 else if (got_subseq)
922 {
923 /* Return -1 to note that we're in a subsequence, but we don't
924 have a matching key, nor was one overridden. This means
925 we need to back up the recursion chain and find the last
926 subsequence that is bound to a function. */
927 if (RL_ISSTATE (RL_STATE_MACROINPUT))
928 _rl_prev_macro_key ();
929 else
930 _rl_unget_char (key);
931 if (rl_key_sequence_length > 0)
932 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
933 return -1;
934 }
935 else
936 {
937 #if defined (READLINE_CALLBACKS)
938 RL_UNSETSTATE (RL_STATE_MULTIKEY);
939 _rl_keyseq_chain_dispose ();
940 #endif
941 _rl_abort_internal ();
942 return -1;
943 }
944 break;
945
946 case ISKMAP:
947 if (map[key].function != 0)
948 {
949 #if defined (VI_MODE)
950 /* The only way this test will be true is if a subsequence has been
951 bound starting with ESC, generally the arrow keys. What we do is
952 check whether there's input in the queue, which there generally
953 will be if an arrow key has been pressed, and, if there's not,
954 just dispatch to (what we assume is) rl_vi_movement_mode right
955 away. This is essentially an input test with a zero timeout (by
956 default) or a timeout determined by the value of `keyseq-timeout' */
957 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
958 takes microseconds, so multiply by 1000 */
959 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap &&
960 (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) &&
961 _rl_pushed_input_available () == 0 &&
962 _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0)
963 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
964 /* This is a very specific test. It can possibly be generalized in
965 the future, but for now it handles a specific case of ESC being
966 the last character in a keyboard macro. */
967 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap &&
968 (RL_ISSTATE (RL_STATE_INPUTPENDING) == 0) &&
969 (RL_ISSTATE (RL_STATE_MACROINPUT) && _rl_peek_macro_key () == 0) &&
970 _rl_pushed_input_available () == 0 &&
971 _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0)
972 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
973 #endif
974
975 RESIZE_KEYSEQ_BUFFER ();
976 rl_executing_keyseq[rl_key_sequence_length++] = key;
977 _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
978
979 /* Allocate new context here. Use linked contexts (linked through
980 cxt->ocxt) to simulate recursion */
981 #if defined (READLINE_CALLBACKS)
982 # if defined (VI_MODE)
983 /* If we're redoing a vi mode command and we know there is a shadowed
984 function corresponding to this key, just call it -- all the redoable
985 vi mode commands already have all the input they need, and rl_vi_redo
986 assumes that one call to rl_dispatch is sufficient to complete the
987 command. */
988 if (_rl_vi_redoing && RL_ISSTATE (RL_STATE_CALLBACK) &&
989 map[ANYOTHERKEY].function != 0)
990 return (_rl_subseq_result (-2, map, key, got_subseq));
991 # endif
992 if (RL_ISSTATE (RL_STATE_CALLBACK))
993 {
994 /* Return 0 only the first time, to indicate success to
995 _rl_callback_read_char. The rest of the time, we're called
996 from _rl_dispatch_callback, so we return -3 to indicate
997 special handling is necessary. */
998 r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
999 cxt = _rl_keyseq_cxt_alloc ();
1000
1001 if (got_subseq)
1002 cxt->flags |= KSEQ_SUBSEQ;
1003 cxt->okey = key;
1004 cxt->oldmap = map;
1005 cxt->dmap = _rl_dispatching_keymap;
1006 cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
1007
1008 RL_SETSTATE (RL_STATE_MULTIKEY);
1009 _rl_kscxt = cxt;
1010
1011 return r; /* don't indicate immediate success */
1012 }
1013 #endif
1014
1015 /* Tentative inter-character timeout for potential multi-key
1016 sequences? If no input within timeout, abort sequence and
1017 act as if we got non-matching input. */
1018 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
1019 takes microseconds, so multiply by 1000 */
1020 if (_rl_keyseq_timeout > 0 &&
1021 (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) &&
1022 _rl_pushed_input_available () == 0 &&
1023 _rl_dispatching_keymap[ANYOTHERKEY].function &&
1024 _rl_input_queued (_rl_keyseq_timeout*1000) == 0)
1025 {
1026 if (rl_key_sequence_length > 0)
1027 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1028 return (_rl_subseq_result (-2, map, key, got_subseq));
1029 }
1030
1031 newkey = _rl_subseq_getchar (key);
1032 if (newkey < 0)
1033 {
1034 _rl_abort_internal ();
1035 return -1;
1036 }
1037
1038 r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
1039 return _rl_subseq_result (r, map, key, got_subseq);
1040 }
1041 else
1042 {
1043 _rl_abort_internal (); /* XXX */
1044 return -1;
1045 }
1046 break;
1047
1048 case ISMACR:
1049 if (map[key].function != 0)
1050 {
1051 rl_executing_keyseq[rl_key_sequence_length] = '\0';
1052 macro = savestring ((char *)map[key].function);
1053 _rl_with_macro_input (macro);
1054 return 0;
1055 }
1056 break;
1057 }
1058
1059 #if defined (VI_MODE)
1060 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
1061 key != ANYOTHERKEY &&
1062 _rl_dispatching_keymap == vi_movement_keymap &&
1063 _rl_vi_textmod_command (key))
1064 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
1065 #endif
1066
1067 return (r);
1068 }
1069
1070 static int
_rl_subseq_result(int r,Keymap map,int key,int got_subseq)1071 _rl_subseq_result (int r, Keymap map, int key, int got_subseq)
1072 {
1073 Keymap m;
1074 int type, nt;
1075 rl_command_func_t *func, *nf;
1076
1077 if (r == -2)
1078 /* We didn't match anything, and the keymap we're indexed into
1079 shadowed a function previously bound to that prefix. Call
1080 the function. The recursive call to _rl_dispatch_subseq has
1081 already taken care of pushing any necessary input back onto
1082 the input queue with _rl_unget_char. */
1083 {
1084 m = _rl_dispatching_keymap;
1085 type = m[ANYOTHERKEY].type;
1086 func = m[ANYOTHERKEY].function;
1087 if (type == ISFUNC && func == rl_do_lowercase_version)
1088 r = _rl_dispatch (_rl_to_lower ((unsigned char)key), map);
1089 else if (type == ISFUNC)
1090 {
1091 /* If we shadowed a function, whatever it is, we somehow need a
1092 keymap with map[key].func == shadowed-function.
1093 Let's use this one. Then we can dispatch using the original
1094 key, since there are commands (e.g., in vi mode) for which it
1095 matters. */
1096 nt = m[key].type;
1097 nf = m[key].function;
1098
1099 m[key].type = type;
1100 m[key].function = func;
1101 /* Don't change _rl_dispatching_keymap, set it here */
1102 _rl_dispatching_keymap = map; /* previous map */
1103 r = _rl_dispatch_subseq (key, m, 0);
1104 m[key].type = nt;
1105 m[key].function = nf;
1106 }
1107 else
1108 /* We probably shadowed a keymap, so keep going. */
1109 r = _rl_dispatch (ANYOTHERKEY, m);
1110 }
1111 else if (r < 0 && map[ANYOTHERKEY].function)
1112 {
1113 /* We didn't match (r is probably -1), so return something to
1114 tell the caller that it should try ANYOTHERKEY for an
1115 overridden function. */
1116 if (RL_ISSTATE (RL_STATE_MACROINPUT))
1117 _rl_prev_macro_key ();
1118 else
1119 _rl_unget_char (key);
1120 if (rl_key_sequence_length > 0)
1121 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1122 _rl_dispatching_keymap = map;
1123 return -2;
1124 }
1125 else if (r < 0 && got_subseq) /* XXX */
1126 {
1127 /* OK, back up the chain. */
1128 if (RL_ISSTATE (RL_STATE_MACROINPUT))
1129 _rl_prev_macro_key ();
1130 else
1131 _rl_unget_char (key);
1132 if (rl_key_sequence_length > 0)
1133 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1134 _rl_dispatching_keymap = map;
1135 return -1;
1136 }
1137
1138 return r;
1139 }
1140
1141 /* **************************************************************** */
1142 /* */
1143 /* Initializations */
1144 /* */
1145 /* **************************************************************** */
1146
1147 /* Initialize readline (and terminal if not already). */
1148 int
rl_initialize(void)1149 rl_initialize (void)
1150 {
1151 /* If we have never been called before, initialize the
1152 terminal and data structures. */
1153 if (rl_initialized == 0)
1154 {
1155 RL_SETSTATE(RL_STATE_INITIALIZING);
1156 readline_initialize_everything ();
1157 RL_UNSETSTATE(RL_STATE_INITIALIZING);
1158 rl_initialized++;
1159 RL_SETSTATE(RL_STATE_INITIALIZED);
1160 }
1161 else
1162 (void)_rl_init_locale (); /* check current locale */
1163
1164 /* Initialize the current line information. */
1165 _rl_init_line_state ();
1166
1167 /* We aren't done yet. We haven't even gotten started yet! */
1168 rl_done = 0;
1169 RL_UNSETSTATE(RL_STATE_DONE|RL_STATE_EOF);
1170
1171 /* Tell the history routines what is going on. */
1172 _rl_start_using_history ();
1173
1174 /* Make the display buffer match the state of the line. */
1175 rl_reset_line_state ();
1176
1177 /* No such function typed yet. */
1178 rl_last_func = (rl_command_func_t *)NULL;
1179
1180 /* Parsing of key-bindings begins in an enabled state. */
1181 _rl_parsing_conditionalized_out = 0;
1182
1183 #if defined (VI_MODE)
1184 if (rl_editing_mode == vi_mode)
1185 _rl_vi_initialize_line ();
1186 #endif
1187
1188 /* Each line starts in insert mode (the default). */
1189 _rl_set_insert_mode (RL_IM_DEFAULT, 1);
1190
1191 return 0;
1192 }
1193
1194 #if 0
1195 #if defined (__EMX__)
1196 static void
1197 _emx_build_environ (void)
1198 {
1199 TIB *tibp;
1200 PIB *pibp;
1201 char *t, **tp;
1202 int c;
1203
1204 DosGetInfoBlocks (&tibp, &pibp);
1205 t = pibp->pib_pchenv;
1206 for (c = 1; *t; c++)
1207 t += strlen (t) + 1;
1208 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
1209 t = pibp->pib_pchenv;
1210 while (*t)
1211 {
1212 *tp++ = t;
1213 t += strlen (t) + 1;
1214 }
1215 *tp = 0;
1216 }
1217 #endif /* __EMX__ */
1218 #endif
1219
1220 /* Initialize the entire state of the world. */
1221 static void
readline_initialize_everything(void)1222 readline_initialize_everything (void)
1223 {
1224 #if 0
1225 #if defined (__EMX__)
1226 if (environ == 0)
1227 _emx_build_environ ();
1228 #endif
1229 #endif
1230
1231 #if 0
1232 /* Find out if we are running in Emacs -- UNUSED. */
1233 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
1234 #endif
1235
1236 /* Set up input and output if they are not already set up. */
1237 if (!rl_instream)
1238 rl_instream = stdin;
1239
1240 if (!rl_outstream)
1241 rl_outstream = stdout;
1242
1243 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
1244 may change, but they may also be used before readline_internal ()
1245 is called. */
1246 _rl_in_stream = rl_instream;
1247 _rl_out_stream = rl_outstream;
1248
1249 /* Allocate data structures. */
1250 if (rl_line_buffer == 0)
1251 rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1252
1253 /* Initialize the terminal interface. */
1254 if (rl_terminal_name == 0)
1255 rl_terminal_name = sh_get_env_value ("TERM");
1256 _rl_init_terminal_io (rl_terminal_name);
1257
1258 /* Bind tty characters to readline functions. */
1259 readline_default_bindings ();
1260
1261 /* Initialize the function names. */
1262 rl_initialize_funmap ();
1263
1264 /* Decide whether we should automatically go into eight-bit mode. */
1265 _rl_init_eightbit ();
1266
1267 /* Read in the init file. */
1268 rl_read_init_file ((char *)NULL);
1269
1270 /* XXX */
1271 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
1272 {
1273 _rl_screenwidth--;
1274 _rl_screenchars -= _rl_screenheight;
1275 }
1276
1277 /* Override the effect of any `set keymap' assignments in the
1278 inputrc file. */
1279 rl_set_keymap_from_edit_mode ();
1280
1281 /* Try to bind a common arrow key prefix, if not already bound. */
1282 bind_arrow_keys ();
1283
1284 /* Bind the bracketed paste prefix assuming that the user will enable
1285 it on terminals that support it. */
1286 bind_bracketed_paste_prefix ();
1287
1288 /* If the completion parser's default word break characters haven't
1289 been set yet, then do so now. */
1290 if (rl_completer_word_break_characters == (char *)NULL)
1291 rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
1292
1293 #if defined (COLOR_SUPPORT)
1294 if (_rl_colored_stats || _rl_colored_completion_prefix)
1295 _rl_parse_colors ();
1296 #endif
1297
1298 rl_executing_keyseq = malloc (_rl_executing_keyseq_size = 16);
1299 if (rl_executing_keyseq)
1300 rl_executing_keyseq[rl_key_sequence_length = 0] = '\0';
1301 }
1302
1303 /* If this system allows us to look at the values of the regular
1304 input editing characters, then bind them to their readline
1305 equivalents, iff the characters are not bound to keymaps. */
1306 static void
readline_default_bindings(void)1307 readline_default_bindings (void)
1308 {
1309 if (_rl_bind_stty_chars)
1310 rl_tty_set_default_bindings (_rl_keymap);
1311 }
1312
1313 /* Reset the default bindings for the terminal special characters we're
1314 interested in back to rl_insert and read the new ones. */
1315 static void
reset_default_bindings(void)1316 reset_default_bindings (void)
1317 {
1318 if (_rl_bind_stty_chars)
1319 {
1320 rl_tty_unset_default_bindings (_rl_keymap);
1321 rl_tty_set_default_bindings (_rl_keymap);
1322 }
1323 }
1324
1325 /* Bind some common arrow key sequences in MAP. */
1326 static void
bind_arrow_keys_internal(Keymap map)1327 bind_arrow_keys_internal (Keymap map)
1328 {
1329 Keymap xkeymap;
1330
1331 xkeymap = _rl_keymap;
1332 _rl_keymap = map;
1333
1334 #if defined (__MSDOS__)
1335 rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
1336 rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
1337 rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
1338 rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
1339 #endif
1340
1341 rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
1342 rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
1343 rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
1344 rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
1345 rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
1346 rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
1347
1348 rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
1349 rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
1350 rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
1351 rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
1352 rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
1353 rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
1354
1355 /* Key bindings for control-arrow keys */
1356 rl_bind_keyseq_if_unbound ("\033[1;5C", rl_forward_word);
1357 rl_bind_keyseq_if_unbound ("\033[1;5D", rl_backward_word);
1358 rl_bind_keyseq_if_unbound ("\033[3;5~", rl_kill_word);
1359
1360 /* Key bindings for alt-arrow keys */
1361 rl_bind_keyseq_if_unbound ("\033[1;3C", rl_forward_word);
1362 rl_bind_keyseq_if_unbound ("\033[1;3D", rl_backward_word);
1363
1364 #if defined (__MINGW32__)
1365 rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
1366 rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
1367 rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
1368 rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
1369 rl_bind_keyseq_if_unbound ("\340G", rl_beg_of_line);
1370 rl_bind_keyseq_if_unbound ("\340O", rl_end_of_line);
1371 rl_bind_keyseq_if_unbound ("\340S", rl_delete);
1372 rl_bind_keyseq_if_unbound ("\340R", rl_overwrite_mode);
1373
1374 /* These may or may not work because of the embedded NUL. */
1375 rl_bind_keyseq_if_unbound ("\\000H", rl_get_previous_history);
1376 rl_bind_keyseq_if_unbound ("\\000P", rl_get_next_history);
1377 rl_bind_keyseq_if_unbound ("\\000M", rl_forward_char);
1378 rl_bind_keyseq_if_unbound ("\\000K", rl_backward_char);
1379 rl_bind_keyseq_if_unbound ("\\000G", rl_beg_of_line);
1380 rl_bind_keyseq_if_unbound ("\\000O", rl_end_of_line);
1381 rl_bind_keyseq_if_unbound ("\\000S", rl_delete);
1382 rl_bind_keyseq_if_unbound ("\\000R", rl_overwrite_mode);
1383 #endif
1384
1385 _rl_keymap = xkeymap;
1386 }
1387
1388 /* Try and bind the common arrow key prefixes after giving termcap and
1389 the inputrc file a chance to bind them and create `real' keymaps
1390 for the arrow key prefix. */
1391 static void
bind_arrow_keys(void)1392 bind_arrow_keys (void)
1393 {
1394 bind_arrow_keys_internal (emacs_standard_keymap);
1395
1396 #if defined (VI_MODE)
1397 bind_arrow_keys_internal (vi_movement_keymap);
1398 /* Unbind vi_movement_keymap[ESC] to allow users to repeatedly hit ESC
1399 in vi command mode while still allowing the arrow keys to work. */
1400 if (vi_movement_keymap[ESC].type == ISKMAP)
1401 rl_bind_keyseq_in_map ("\033", (rl_command_func_t *)NULL, vi_movement_keymap);
1402 bind_arrow_keys_internal (vi_insertion_keymap);
1403 #endif
1404 }
1405
1406 static void
bind_bracketed_paste_prefix(void)1407 bind_bracketed_paste_prefix (void)
1408 {
1409 Keymap xkeymap;
1410
1411 xkeymap = _rl_keymap;
1412
1413 _rl_keymap = emacs_standard_keymap;
1414 rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin);
1415
1416 #if defined (VI_MODE)
1417 _rl_keymap = vi_insertion_keymap;
1418 rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin);
1419 /* XXX - is there a reason to do this in the vi command keymap? */
1420 #endif
1421
1422 _rl_keymap = xkeymap;
1423 }
1424
1425 /* **************************************************************** */
1426 /* */
1427 /* Saving and Restoring Readline's state */
1428 /* */
1429 /* **************************************************************** */
1430
1431 int
rl_save_state(struct readline_state * sp)1432 rl_save_state (struct readline_state *sp)
1433 {
1434 if (sp == 0)
1435 return -1;
1436
1437 sp->point = rl_point;
1438 sp->end = rl_end;
1439 sp->mark = rl_mark;
1440 sp->buffer = rl_line_buffer;
1441 sp->buflen = rl_line_buffer_len;
1442 sp->ul = rl_undo_list;
1443 sp->prompt = rl_prompt;
1444
1445 sp->rlstate = rl_readline_state;
1446 sp->done = rl_done;
1447 sp->kmap = _rl_keymap;
1448
1449 sp->lastfunc = rl_last_func;
1450 sp->insmode = rl_insert_mode;
1451 sp->edmode = rl_editing_mode;
1452 sp->kseq = rl_executing_keyseq;
1453 sp->kseqlen = rl_key_sequence_length;
1454 sp->inf = rl_instream;
1455 sp->outf = rl_outstream;
1456 sp->pendingin = rl_pending_input;
1457 sp->macro = rl_executing_macro;
1458
1459 sp->catchsigs = rl_catch_signals;
1460 sp->catchsigwinch = rl_catch_sigwinch;
1461
1462 sp->entryfunc = rl_completion_entry_function;
1463 sp->menuentryfunc = rl_menu_completion_entry_function;
1464 sp->ignorefunc = rl_ignore_some_completions_function;
1465 sp->attemptfunc = rl_attempted_completion_function;
1466 sp->wordbreakchars = rl_completer_word_break_characters;
1467
1468 return (0);
1469 }
1470
1471 int
rl_restore_state(struct readline_state * sp)1472 rl_restore_state (struct readline_state *sp)
1473 {
1474 if (sp == 0)
1475 return -1;
1476
1477 rl_point = sp->point;
1478 rl_end = sp->end;
1479 rl_mark = sp->mark;
1480 the_line = rl_line_buffer = sp->buffer;
1481 rl_line_buffer_len = sp->buflen;
1482 rl_undo_list = sp->ul;
1483 rl_prompt = sp->prompt;
1484
1485 rl_readline_state = sp->rlstate;
1486 rl_done = sp->done;
1487 _rl_keymap = sp->kmap;
1488
1489 rl_last_func = sp->lastfunc;
1490 rl_insert_mode = sp->insmode;
1491 rl_editing_mode = sp->edmode;
1492 rl_executing_keyseq = sp->kseq;
1493 rl_key_sequence_length = sp->kseqlen;
1494 rl_instream = sp->inf;
1495 rl_outstream = sp->outf;
1496 rl_pending_input = sp->pendingin;
1497 rl_executing_macro = sp->macro;
1498
1499 rl_catch_signals = sp->catchsigs;
1500 rl_catch_sigwinch = sp->catchsigwinch;
1501
1502 rl_completion_entry_function = sp->entryfunc;
1503 rl_menu_completion_entry_function = sp->menuentryfunc;
1504 rl_ignore_some_completions_function = sp->ignorefunc;
1505 rl_attempted_completion_function = sp->attemptfunc;
1506 rl_completer_word_break_characters = sp->wordbreakchars;
1507
1508 rl_deactivate_mark ();
1509
1510 return (0);
1511 }
1512
1513 /* Functions to manage the string that is the current key sequence. */
1514
1515 void
_rl_init_executing_keyseq(void)1516 _rl_init_executing_keyseq (void)
1517 {
1518 rl_executing_keyseq[rl_key_sequence_length = 0] = '\0';
1519 }
1520
1521 void
_rl_term_executing_keyseq(void)1522 _rl_term_executing_keyseq (void)
1523 {
1524 rl_executing_keyseq[rl_key_sequence_length] = '\0';
1525 }
1526
1527 void
_rl_end_executing_keyseq(void)1528 _rl_end_executing_keyseq (void)
1529 {
1530 if (rl_key_sequence_length > 0)
1531 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1532 }
1533
1534 void
_rl_add_executing_keyseq(int key)1535 _rl_add_executing_keyseq (int key)
1536 {
1537 RESIZE_KEYSEQ_BUFFER ();
1538 rl_executing_keyseq[rl_key_sequence_length++] = key;
1539 }
1540