OSDN Git Service

From Martin Simmons:
[pf3gnuchains/pf3gnuchains4x.git] / readline / readline.c
1 /* readline.c -- a general facility for reading lines of input
2    with emacs style editing and completion. */
3
4 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
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
11    as published by the Free Software Foundation; either version 2, or
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,
22    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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
53 /* System-specific feature definitions and include files. */
54 #include "rldefs.h"
55 #include "rlmbutil.h"
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
66 #include "rlprivate.h"
67 #include "rlshell.h"
68 #include "xmalloc.h"
69
70 #ifndef RL_LIBRARY_VERSION
71 #  define RL_LIBRARY_VERSION "4.3"
72 #endif
73
74 #ifndef RL_READLINE_VERSION
75 #  define RL_READLINE_VERSION   0x0403
76 #endif
77
78 extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
79
80 /* Forward declarations used in this file. */
81 static char *readline_internal PARAMS((void));
82 static void readline_initialize_everything PARAMS((void));
83
84 static void bind_arrow_keys_internal PARAMS((Keymap));
85 static void bind_arrow_keys PARAMS((void));
86
87 static void readline_default_bindings PARAMS((void));
88
89 /* **************************************************************** */
90 /*                                                                  */
91 /*                      Line editing input utility                  */
92 /*                                                                  */
93 /* **************************************************************** */
94
95 const char *rl_library_version = RL_LIBRARY_VERSION;
96
97 int rl_readline_version = RL_READLINE_VERSION;
98
99 /* True if this is `real' readline as opposed to some stub substitute. */
100 int rl_gnu_readline_p = 1;
101
102 /* A pointer to the keymap that is currently in use.
103    By default, it is the standard emacs keymap. */
104 Keymap _rl_keymap = emacs_standard_keymap;
105
106 /* The current style of editing. */
107 int rl_editing_mode = emacs_mode;
108
109 /* The current insert mode:  input (the default) or overwrite */
110 int rl_insert_mode = RL_IM_DEFAULT;
111
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. */
115 int rl_dispatching;
116
117 /* Non-zero if the previous command was a kill command. */
118 int _rl_last_command_was_kill = 0;
119
120 /* The current value of the numeric argument specified by the user. */
121 int rl_numeric_arg = 1;
122
123 /* Non-zero if an argument was typed. */
124 int rl_explicit_arg = 0;
125
126 /* Temporary value used while generating the argument. */
127 int rl_arg_sign = 1;
128
129 /* Non-zero means we have been called at least once before. */
130 static int rl_initialized;
131
132 #if 0
133 /* If non-zero, this program is running in an EMACS buffer. */
134 static int running_in_emacs;
135 #endif
136
137 /* Flags word encapsulating the current readline state. */
138 int rl_readline_state = RL_STATE_NONE;
139
140 /* The current offset in the current input line. */
141 int rl_point;
142
143 /* Mark in the current input line. */
144 int rl_mark;
145
146 /* Length of the current input line. */
147 int rl_end;
148
149 /* Make this non-zero to return the current input_line. */
150 int rl_done;
151
152 /* The last function executed by readline. */
153 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
154
155 /* Top level environment for readline_internal (). */
156 procenv_t readline_top_level;
157
158 /* The streams we interact with. */
159 FILE *_rl_in_stream, *_rl_out_stream;
160
161 /* The names of the streams that we do input and output to. */
162 FILE *rl_instream = (FILE *)NULL;
163 FILE *rl_outstream = (FILE *)NULL;
164
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. */
169 int readline_echoing_p = 0;
170
171 /* Current prompt. */
172 char *rl_prompt = (char *)NULL;
173 int rl_visible_prompt_length = 0;
174
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. */
177 int rl_already_prompted = 0;
178
179 /* The number of characters read in order to type this complete command. */
180 int rl_key_sequence_length = 0;
181
182 /* If non-zero, then this is the address of a function to call just
183    before readline_internal_setup () prints the first prompt. */
184 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
185
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. */
189 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
190
191 /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
192 static char *the_line;
193
194 /* The character that can generate an EOF.  Really read from
195    the terminal driver... just defaulted here. */
196 int _rl_eof_char = CTRL ('D');
197
198 /* Non-zero makes this the next keystroke to read. */
199 int rl_pending_input = 0;
200
201 /* Pointer to a useful terminal name. */
202 const char *rl_terminal_name = (const char *)NULL;
203
204 /* Non-zero means to always use horizontal scrolling in line display. */
205 int _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. */
209 int _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. */
213 int _rl_bell_preference = AUDIBLE_BELL;
214      
215 /* String inserted into the line by rl_insert_comment (). */
216 char *_rl_comment_begin;
217
218 /* Keymap holding the function currently being executed. */
219 Keymap rl_executing_keymap;
220
221 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
222 int rl_erase_empty_line = 0;
223
224 /* Non-zero means to read only this many characters rather than up to a
225    character bound to accept-line. */
226 int rl_num_chars_to_read;
227
228 /* Line buffer and maintenence. */
229 char *rl_line_buffer = (char *)NULL;
230 int rl_line_buffer_len = 0;
231
232 /* Forward declarations used by the display, termcap, and history code. */
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. */
242 unsigned 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. */
247 int _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. */
251 int _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. */
260 int _rl_meta_flag = 0;  /* Forward declaration */
261
262 /* Set up the prompt and expand it.  Called from readline() and
263    rl_callback_handler_install (). */
264 int
265 rl_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   
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. */
277 char *
278 readline (prompt)
279      const char *prompt;
280 {
281   char *value;
282
283   /* If we are at EOF return a NULL string. */
284   if (rl_pending_input == EOF)
285     {
286       rl_clear_pending_input ();
287       return ((char *)NULL);
288     }
289
290   rl_set_prompt (prompt);
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
315 STATIC_CALLBACK void
316 readline_internal_setup ()
317 {
318   char *nprompt;
319
320   _rl_in_stream = rl_instream;
321   _rl_out_stream = rl_outstream;
322
323   if (rl_startup_hook)
324     (*rl_startup_hook) ();
325
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)
330     {
331       if (rl_prompt && rl_already_prompted == 0)
332         {
333           nprompt = _rl_strip_prompt (rl_prompt);
334           fprintf (_rl_out_stream, "%s", nprompt);
335           fflush (_rl_out_stream);
336           free (nprompt);
337         }
338     }
339   else
340     {
341       if (rl_prompt && rl_already_prompted)
342         rl_on_new_line_with_prompt ();
343       else
344         rl_on_new_line ();
345       (*rl_redisplay_function) ();
346     }
347
348 #if defined (VI_MODE)
349   if (rl_editing_mode == vi_mode)
350     rl_vi_insertion_mode (1, 0);
351 #endif /* VI_MODE */
352
353   if (rl_pre_input_hook)
354     (*rl_pre_input_hook) ();
355 }
356
357 STATIC_CALLBACK char *
358 readline_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);
372       entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
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)
382     rl_free_undo_list ();
383
384   /* Restore normal cursor, if available. */
385   _rl_set_insert_mode (RL_IM_INSERT, 0);
386
387   return (eof ? (char *)NULL : savestring (the_line));
388 }
389
390 STATIC_CALLBACK int
391 #if defined (READLINE_CALLBACKS)
392 readline_internal_char ()
393 #else
394 readline_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
421       RL_SETSTATE(RL_STATE_READCMD);
422       c = rl_read_key ();
423       RL_UNSETSTATE(RL_STATE_READCMD);
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)
434           RL_SETSTATE(RL_STATE_DONE);
435           return (rl_done = 1);
436 #else
437           eof_found = 1;
438           break;
439 #endif
440         }
441
442       lastc = c;
443       _rl_dispatch ((unsigned char)c, _rl_keymap);
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
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
464       if (rl_done == 0)
465         (*rl_redisplay_function) ();
466
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
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)
483 static int
484 readline_internal_charloop ()
485 {
486   int eof = 1;
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. */
497 static char *
498 readline_internal ()
499 {
500   int eof;
501
502   readline_internal_setup ();
503   eof = readline_internal_charloop ();
504   return (readline_internal_teardown (eof));
505 }
506
507 void
508 _rl_init_line_state ()
509 {
510   rl_point = rl_end = rl_mark = 0;
511   the_line = rl_line_buffer;
512   the_line[0] = 0;
513 }
514
515 void
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. */
524 int
525 _rl_dispatch (key, map)
526      register int key;
527      Keymap map;
528 {
529   return _rl_dispatch_subseq (key, map, 0);
530 }
531
532 int
533 _rl_dispatch_subseq (key, map, got_subseq)
534      register int key;
535      Keymap map;
536      int got_subseq;
537 {
538   int r, newkey;
539   char *macro;
540   rl_command_func_t *func;
541
542   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
543     {
544       if (map[ESC].type == ISKMAP)
545         {
546           if (RL_ISSTATE (RL_STATE_MACRODEF))
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
554         rl_ding ();
555       return 0;
556     }
557
558   if (RL_ISSTATE (RL_STATE_MACRODEF))
559     _rl_add_macro_char (key);
560
561   r = 0;
562   switch (map[key].type)
563     {
564     case ISFUNC:
565       func = map[key].function;
566       if (func)
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;
579           RL_SETSTATE(RL_STATE_DISPATCHING);
580           r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
581           RL_UNSETSTATE(RL_STATE_DISPATCHING);
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. */
587           if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
588             rl_last_func = map[key].function;
589         }
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         }
607       else
608         {
609           _rl_abort_internal ();
610           return -1;
611         }
612       break;
613
614     case ISKMAP:
615       if (map[key].function != 0)
616         {
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
629           rl_key_sequence_length++;
630
631           if (key == ESC)
632             RL_SETSTATE(RL_STATE_METANEXT);
633           RL_SETSTATE(RL_STATE_MOREINPUT);
634           newkey = rl_read_key ();
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             }
668         }
669       else
670         {
671           _rl_abort_internal ();
672           return -1;
673         }
674       break;
675
676     case ISMACR:
677       if (map[key].function != 0)
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       key != ANYOTHERKEY &&
688       _rl_vi_textmod_command (key))
689     _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
690 #endif
691   return (r);
692 }
693
694 /* **************************************************************** */
695 /*                                                                  */
696 /*                      Initializations                             */
697 /*                                                                  */
698 /* **************************************************************** */
699
700 /* Initialize readline (and terminal if not already). */
701 int
702 rl_initialize ()
703 {
704   /* If we have never been called before, initialize the
705      terminal and data structures. */
706   if (!rl_initialized)
707     {
708       RL_SETSTATE(RL_STATE_INITIALIZING);
709       readline_initialize_everything ();
710       RL_UNSETSTATE(RL_STATE_INITIALIZING);
711       rl_initialized++;
712       RL_SETSTATE(RL_STATE_INITIALIZED);
713     }
714
715   /* Initalize the current line information. */
716   _rl_init_line_state ();
717
718   /* We aren't done yet.  We haven't even gotten started yet! */
719   rl_done = 0;
720   RL_UNSETSTATE(RL_STATE_DONE);
721
722   /* Tell the history routines what is going on. */
723   _rl_start_using_history ();
724
725   /* Make the display buffer match the state of the line. */
726   rl_reset_line_state ();
727
728   /* No such function typed yet. */
729   rl_last_func = (rl_command_func_t *)NULL;
730
731   /* Parsing of key-bindings begins in an enabled state. */
732   _rl_parsing_conditionalized_out = 0;
733
734 #if defined (VI_MODE)
735   if (rl_editing_mode == vi_mode)
736     _rl_vi_initialize_line ();
737 #endif
738
739   /* Each line starts in insert mode (the default). */
740   _rl_set_insert_mode (RL_IM_DEFAULT, 1);
741
742   return 0;
743 }
744
745 #if 0
746 #if defined (__EMX__)
747 static void
748 _emx_build_environ ()
749 {
750   TIB *tibp;
751   PIB *pibp;
752   char *t, **tp;
753   int c;
754
755   DosGetInfoBlocks (&tibp, &pibp);
756   t = pibp->pib_pchenv;
757   for (c = 1; *t; c++)
758     t += strlen (t) + 1;
759   tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
760   t = pibp->pib_pchenv;
761   while (*t)
762     {
763       *tp++ = t;
764       t += strlen (t) + 1;
765     }
766   *tp = 0;
767 }
768 #endif /* __EMX__ */
769 #endif
770
771 /* Initialize the entire state of the world. */
772 static void
773 readline_initialize_everything ()
774 {
775 #if 0
776 #if defined (__EMX__)
777   if (environ == 0)
778     _emx_build_environ ();
779 #endif
780 #endif
781
782 #if 0
783   /* Find out if we are running in Emacs -- UNUSED. */
784   running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
785 #endif
786
787   /* Set up input and output if they are not already set up. */
788   if (!rl_instream)
789     rl_instream = stdin;
790
791   if (!rl_outstream)
792     rl_outstream = stdout;
793
794   /* Bind _rl_in_stream and _rl_out_stream immediately.  These values
795      may change, but they may also be used before readline_internal ()
796      is called. */
797   _rl_in_stream = rl_instream;
798   _rl_out_stream = rl_outstream;
799
800   /* Allocate data structures. */
801   if (rl_line_buffer == 0)
802     rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
803
804   /* Initialize the terminal interface. */
805   if (rl_terminal_name == 0)
806     rl_terminal_name = sh_get_env_value ("TERM");
807   _rl_init_terminal_io (rl_terminal_name);
808
809   /* Bind tty characters to readline functions. */
810   readline_default_bindings ();
811
812   /* Initialize the function names. */
813   rl_initialize_funmap ();
814
815   /* Decide whether we should automatically go into eight-bit mode. */
816   _rl_init_eightbit ();
817       
818   /* Read in the init file. */
819   rl_read_init_file ((char *)NULL);
820
821   /* XXX */
822   if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
823     {
824       _rl_screenwidth--;
825       _rl_screenchars -= _rl_screenheight;
826     }
827
828   /* Override the effect of any `set keymap' assignments in the
829      inputrc file. */
830   rl_set_keymap_from_edit_mode ();
831
832   /* Try to bind a common arrow key prefix, if not already bound. */
833   bind_arrow_keys ();
834
835   /* Enable the meta key, if this terminal has one. */
836   if (_rl_enable_meta)
837     _rl_enable_meta_key ();
838
839   /* If the completion parser's default word break characters haven't
840      been set yet, then do so now. */
841   if (rl_completer_word_break_characters == (char *)NULL)
842     rl_completer_word_break_characters = rl_basic_word_break_characters;
843 }
844
845 /* If this system allows us to look at the values of the regular
846    input editing characters, then bind them to their readline
847    equivalents, iff the characters are not bound to keymaps. */
848 static void
849 readline_default_bindings ()
850 {
851   rl_tty_set_default_bindings (_rl_keymap);
852 }
853
854 /* Bind some common arrow key sequences in MAP. */
855 static void
856 bind_arrow_keys_internal (map)
857      Keymap map;
858 {
859   Keymap xkeymap;
860
861   xkeymap = _rl_keymap;
862   _rl_keymap = map;
863
864 #if defined (__MSDOS__)
865    _rl_bind_if_unbound ("\033[0A", rl_get_previous_history);
866    _rl_bind_if_unbound ("\033[0B", rl_backward_char);
867    _rl_bind_if_unbound ("\033[0C", rl_forward_char);
868    _rl_bind_if_unbound ("\033[0D", rl_get_next_history);
869 #endif
870
871   _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
872   _rl_bind_if_unbound ("\033[B", rl_get_next_history);
873   _rl_bind_if_unbound ("\033[C", rl_forward_char);
874   _rl_bind_if_unbound ("\033[D", rl_backward_char);
875   _rl_bind_if_unbound ("\033[H", rl_beg_of_line);
876   _rl_bind_if_unbound ("\033[F", rl_end_of_line);
877
878   _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
879   _rl_bind_if_unbound ("\033OB", rl_get_next_history);
880   _rl_bind_if_unbound ("\033OC", rl_forward_char);
881   _rl_bind_if_unbound ("\033OD", rl_backward_char);
882   _rl_bind_if_unbound ("\033OH", rl_beg_of_line);
883   _rl_bind_if_unbound ("\033OF", rl_end_of_line);
884
885   _rl_keymap = xkeymap;
886 }
887
888 /* Try and bind the common arrow key prefixes after giving termcap and
889    the inputrc file a chance to bind them and create `real' keymaps
890    for the arrow key prefix. */
891 static void
892 bind_arrow_keys ()
893 {
894   bind_arrow_keys_internal (emacs_standard_keymap);
895
896 #if defined (VI_MODE)
897   bind_arrow_keys_internal (vi_movement_keymap);
898   bind_arrow_keys_internal (vi_insertion_keymap);
899 #endif
900 }
901
902 /* **************************************************************** */
903 /*                                                                  */
904 /*              Saving and Restoring Readline's state               */
905 /*                                                                  */
906 /* **************************************************************** */
907
908 int
909 rl_save_state (sp)
910      struct readline_state *sp;
911 {
912   if (sp == 0)
913     return -1;
914
915   sp->point = rl_point;
916   sp->end = rl_end;
917   sp->mark = rl_mark;
918   sp->buffer = rl_line_buffer;
919   sp->buflen = rl_line_buffer_len;
920   sp->ul = rl_undo_list;
921   sp->prompt = rl_prompt;
922
923   sp->rlstate = rl_readline_state;
924   sp->done = rl_done;
925   sp->kmap = _rl_keymap;
926
927   sp->lastfunc = rl_last_func;
928   sp->insmode = rl_insert_mode;
929   sp->edmode = rl_editing_mode;
930   sp->kseqlen = rl_key_sequence_length;
931   sp->inf = rl_instream;
932   sp->outf = rl_outstream;
933   sp->pendingin = rl_pending_input;
934   sp->macro = rl_executing_macro;
935
936   sp->catchsigs = rl_catch_signals;
937 #if defined (SIGWINCH)
938   sp->catchsigwinch = rl_catch_sigwinch;
939 #endif
940
941   return (0);
942 }
943
944 int
945 rl_restore_state (sp)
946      struct readline_state *sp;
947 {
948   if (sp == 0)
949     return -1;
950
951   rl_point = sp->point;
952   rl_end = sp->end;
953   rl_mark = sp->mark;
954   the_line = rl_line_buffer = sp->buffer;
955   rl_line_buffer_len = sp->buflen;
956   rl_undo_list = sp->ul;
957   rl_prompt = sp->prompt;
958
959   rl_readline_state = sp->rlstate;
960   rl_done = sp->done;
961   _rl_keymap = sp->kmap;
962
963   rl_last_func = sp->lastfunc;
964   rl_insert_mode = sp->insmode;
965   rl_editing_mode = sp->edmode;
966   rl_key_sequence_length = sp->kseqlen;
967   rl_instream = sp->inf;
968   rl_outstream = sp->outf;
969   rl_pending_input = sp->pendingin;
970   rl_executing_macro = sp->macro;
971
972   rl_catch_signals = sp->catchsigs;
973 #if defined (SIGWINCH)
974   rl_catch_sigwinch = sp->catchsigwinch;
975 #endif
976
977   return (0);
978 }