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