Sync readline/ to version 7.0 alpha
[external/binutils.git] / readline / bind.c
1 /* bind.c -- key binding and startup file support for the readline library. */
2
3 /* Copyright (C) 1987-2012 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library (Readline), a library
6    for reading lines of text with interactive input and history editing.
7
8    Readline is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12
13    Readline is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (__TANDEM)
25 #  include <floss.h>
26 #endif
27
28 #if defined (HAVE_CONFIG_H)
29 #  include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #if defined (HAVE_SYS_FILE_H)
36 #  include <sys/file.h>
37 #endif /* HAVE_SYS_FILE_H */
38
39 #if defined (HAVE_UNISTD_H)
40 #  include <unistd.h>
41 #endif /* HAVE_UNISTD_H */
42
43 #if defined (HAVE_STDLIB_H)
44 #  include <stdlib.h>
45 #else
46 #  include "ansi_stdlib.h"
47 #endif /* HAVE_STDLIB_H */
48
49 #include <errno.h>
50
51 #if !defined (errno)
52 extern int errno;
53 #endif /* !errno */
54
55 #include "posixstat.h"
56
57 /* System-specific feature definitions and include files. */
58 #include "rldefs.h"
59
60 /* Some standard library routines. */
61 #include "readline.h"
62 #include "history.h"
63
64 #include "rlprivate.h"
65 #include "rlshell.h"
66 #include "xmalloc.h"
67
68 #if !defined (strchr) && !defined (__STDC__)
69 extern char *strchr (), *strrchr ();
70 #endif /* !strchr && !__STDC__ */
71
72 /* Variables exported by this file. */
73 Keymap rl_binding_keymap;
74
75 static int _rl_skip_to_delim PARAMS((char *, int, int));
76
77 static char *_rl_read_file PARAMS((char *, size_t *));
78 static void _rl_init_file_error PARAMS((const char *));
79 static int _rl_read_init_file PARAMS((const char *, int));
80 static int glean_key_from_name PARAMS((char *));
81
82 static int find_boolean_var PARAMS((const char *));
83 static int find_string_var PARAMS((const char *));
84
85 static char *_rl_get_string_variable_value PARAMS((const char *));
86 static int substring_member_of_array PARAMS((const char *, const char * const *));
87
88 static int currently_reading_init_file;
89
90 /* used only in this file */
91 static int _rl_prefer_visible_bell = 1;
92
93 /* **************************************************************** */
94 /*                                                                  */
95 /*                      Binding keys                                */
96 /*                                                                  */
97 /* **************************************************************** */
98
99 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
100    Add NAME to the list of named functions.  Make FUNCTION be the function
101    that gets called.  If KEY is not -1, then bind it. */
102 int
103 rl_add_defun (name, function, key)
104      const char *name;
105      rl_command_func_t *function;
106      int key;
107 {
108   if (key != -1)
109     rl_bind_key (key, function);
110   rl_add_funmap_entry (name, function);
111   return 0;
112 }
113
114 /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
115 int
116 rl_bind_key (key, function)
117      int key;
118      rl_command_func_t *function;
119 {
120   char keyseq[3];
121   int l;
122
123   if (key < 0)
124     return (key);
125
126   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
127     {
128       if (_rl_keymap[ESC].type == ISKMAP)
129         {
130           Keymap escmap;
131
132           escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
133           key = UNMETA (key);
134           escmap[key].type = ISFUNC;
135           escmap[key].function = function;
136           return (0);
137         }
138       return (key);
139     }
140
141   /* If it's bound to a function or macro, just overwrite.  Otherwise we have
142      to treat it as a key sequence so rl_generic_bind handles shadow keymaps
143      for us.  If we are binding '\' make sure to escape it so it makes it
144      through the call to rl_translate_keyseq. */
145   if (_rl_keymap[key].type != ISKMAP)
146     {
147       _rl_keymap[key].type = ISFUNC;
148       _rl_keymap[key].function = function;
149     }
150   else
151     {
152       l = 0;
153       if (key == '\\')
154         keyseq[l++] = '\\';
155       keyseq[l++] = key;
156       keyseq[l] = '\0';
157       rl_bind_keyseq (keyseq, function);
158     }
159   rl_binding_keymap = _rl_keymap;
160   return (0);
161 }
162
163 /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
164    KEY. */
165 int
166 rl_bind_key_in_map (key, function, map)
167      int key;
168      rl_command_func_t *function;
169      Keymap map;
170 {
171   int result;
172   Keymap oldmap;
173
174   oldmap = _rl_keymap;
175   _rl_keymap = map;
176   result = rl_bind_key (key, function);
177   _rl_keymap = oldmap;
178   return (result);
179 }
180
181 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound.  Right
182    now, this is always used to attempt to bind the arrow keys, hence the
183    check for rl_vi_movement_mode. */
184 int
185 rl_bind_key_if_unbound_in_map (key, default_func, kmap)
186      int key;
187      rl_command_func_t *default_func;
188      Keymap kmap;
189 {
190   char keyseq[2];
191
192   keyseq[0] = (unsigned char)key;
193   keyseq[1] = '\0';
194   return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap));
195 }
196
197 int
198 rl_bind_key_if_unbound (key, default_func)
199      int key;
200      rl_command_func_t *default_func;
201 {
202   char keyseq[2];
203
204   keyseq[0] = (unsigned char)key;
205   keyseq[1] = '\0';
206   return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap));
207 }
208
209 /* Make KEY do nothing in the currently selected keymap.
210    Returns non-zero in case of error. */
211 int
212 rl_unbind_key (key)
213      int key;
214 {
215   return (rl_bind_key (key, (rl_command_func_t *)NULL));
216 }
217
218 /* Make KEY do nothing in MAP.
219    Returns non-zero in case of error. */
220 int
221 rl_unbind_key_in_map (key, map)
222      int key;
223      Keymap map;
224 {
225   return (rl_bind_key_in_map (key, (rl_command_func_t *)NULL, map));
226 }
227
228 /* Unbind all keys bound to FUNCTION in MAP. */
229 int
230 rl_unbind_function_in_map (func, map)
231      rl_command_func_t *func;
232      Keymap map;
233 {
234   register int i, rval;
235
236   for (i = rval = 0; i < KEYMAP_SIZE; i++)
237     {
238       if (map[i].type == ISFUNC && map[i].function == func)
239         {
240           map[i].function = (rl_command_func_t *)NULL;
241           rval = 1;
242         }
243     }
244   return rval;
245 }
246
247 int
248 rl_unbind_command_in_map (command, map)
249      const char *command;
250      Keymap map;
251 {
252   rl_command_func_t *func;
253
254   func = rl_named_function (command);
255   if (func == 0)
256     return 0;
257   return (rl_unbind_function_in_map (func, map));
258 }
259
260 /* Bind the key sequence represented by the string KEYSEQ to
261    FUNCTION, starting in the current keymap.  This makes new
262    keymaps as necessary. */
263 int
264 rl_bind_keyseq (keyseq, function)
265      const char *keyseq;
266      rl_command_func_t *function;
267 {
268   return (rl_generic_bind (ISFUNC, keyseq, (char *)function, _rl_keymap));
269 }
270
271 /* Bind the key sequence represented by the string KEYSEQ to
272    FUNCTION.  This makes new keymaps as necessary.  The initial
273    place to do bindings is in MAP. */
274 int
275 rl_bind_keyseq_in_map (keyseq, function, map)
276      const char *keyseq;
277      rl_command_func_t *function;
278      Keymap map;
279 {
280   return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
281 }
282
283 /* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */
284 int
285 rl_set_key (keyseq, function, map)
286      const char *keyseq;
287      rl_command_func_t *function;
288      Keymap map;
289 {
290   return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
291 }
292
293 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound.  Right
294    now, this is always used to attempt to bind the arrow keys, hence the
295    check for rl_vi_movement_mode. */
296 int
297 rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap)
298      const char *keyseq;
299      rl_command_func_t *default_func;
300      Keymap kmap;
301 {
302   rl_command_func_t *func;
303
304   if (keyseq)
305     {
306       func = rl_function_of_keyseq (keyseq, kmap, (int *)NULL);
307 #if defined (VI_MODE)
308       if (!func || func == rl_do_lowercase_version || func == rl_vi_movement_mode)
309 #else
310       if (!func || func == rl_do_lowercase_version)
311 #endif
312         return (rl_bind_keyseq_in_map (keyseq, default_func, kmap));
313       else
314         return 1;
315     }
316   return 0;
317 }
318
319 int
320 rl_bind_keyseq_if_unbound (keyseq, default_func)
321      const char *keyseq;
322      rl_command_func_t *default_func;
323 {
324   return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap));
325 }
326
327 /* Bind the key sequence represented by the string KEYSEQ to
328    the string of characters MACRO.  This makes new keymaps as
329    necessary.  The initial place to do bindings is in MAP. */
330 int
331 rl_macro_bind (keyseq, macro, map)
332      const char *keyseq, *macro;
333      Keymap map;
334 {
335   char *macro_keys;
336   int macro_keys_len;
337
338   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
339
340   if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
341     {
342       xfree (macro_keys);
343       return -1;
344     }
345   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
346   return 0;
347 }
348
349 /* Bind the key sequence represented by the string KEYSEQ to
350    the arbitrary pointer DATA.  TYPE says what kind of data is
351    pointed to by DATA, right now this can be a function (ISFUNC),
352    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
353    as necessary.  The initial place to do bindings is in MAP. */
354 int
355 rl_generic_bind (type, keyseq, data, map)
356      int type;
357      const char *keyseq;
358      char *data;
359      Keymap map;
360 {
361   char *keys;
362   int keys_len;
363   register int i;
364   KEYMAP_ENTRY k;
365
366   k.function = 0;
367
368   /* If no keys to bind to, exit right away. */
369   if (keyseq == 0 || *keyseq == 0)
370     {
371       if (type == ISMACR)
372         xfree (data);
373       return -1;
374     }
375
376   keys = (char *)xmalloc (1 + (2 * strlen (keyseq)));
377
378   /* Translate the ASCII representation of KEYSEQ into an array of
379      characters.  Stuff the characters into KEYS, and the length of
380      KEYS into KEYS_LEN. */
381   if (rl_translate_keyseq (keyseq, keys, &keys_len))
382     {
383       xfree (keys);
384       return -1;
385     }
386
387   /* Bind keys, making new keymaps as necessary. */
388   for (i = 0; i < keys_len; i++)
389     {
390       unsigned char uc = keys[i];
391       int ic;
392
393       ic = uc;
394       if (ic < 0 || ic >= KEYMAP_SIZE)
395         {
396           xfree (keys);
397           return -1;
398         }
399
400       if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
401         {
402           ic = UNMETA (ic);
403           if (map[ESC].type == ISKMAP)
404             map = FUNCTION_TO_KEYMAP (map, ESC);
405         }
406
407       if ((i + 1) < keys_len)
408         {
409           if (map[ic].type != ISKMAP)
410             {
411               /* We allow subsequences of keys.  If a keymap is being
412                  created that will `shadow' an existing function or macro
413                  key binding, we save that keybinding into the ANYOTHERKEY
414                  index in the new map.  The dispatch code will look there
415                  to find the function to execute if the subsequence is not
416                  matched.  ANYOTHERKEY was chosen to be greater than
417                  UCHAR_MAX. */
418               k = map[ic];
419
420               map[ic].type = ISKMAP;
421               map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
422             }
423           map = FUNCTION_TO_KEYMAP (map, ic);
424           /* The dispatch code will return this function if no matching
425              key sequence is found in the keymap.  This (with a little
426              help from the dispatch code in readline.c) allows `a' to be
427              mapped to something, `abc' to be mapped to something else,
428              and the function bound  to `a' to be executed when the user
429              types `abx', leaving `bx' in the input queue. */
430           if (k.function && ((k.type == ISFUNC && k.function != rl_do_lowercase_version) || k.type == ISMACR))
431             {
432               map[ANYOTHERKEY] = k;
433               k.function = 0;
434             }
435         }
436       else
437         {
438           if (map[ic].type == ISMACR)
439             xfree ((char *)map[ic].function);
440           else if (map[ic].type == ISKMAP)
441             {
442               map = FUNCTION_TO_KEYMAP (map, ic);
443               ic = ANYOTHERKEY;
444               /* If we're trying to override a keymap with a null function
445                  (e.g., trying to unbind it), we can't use a null pointer
446                  here because that's indistinguishable from having not been
447                  overridden.  We use a special bindable function that does
448                  nothing. */
449               if (type == ISFUNC && data == 0)
450                 data = (char *)_rl_null_function;
451             }
452
453           map[ic].function = KEYMAP_TO_FUNCTION (data);
454           map[ic].type = type;
455         }
456
457       rl_binding_keymap = map;
458     }
459   xfree (keys);
460   return 0;
461 }
462
463 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
464    an array of characters.  LEN gets the final length of ARRAY.  Return
465    non-zero if there was an error parsing SEQ. */
466 int
467 rl_translate_keyseq (seq, array, len)
468      const char *seq;
469      char *array;
470      int *len;
471 {
472   register int i, c, l, temp;
473
474   for (i = l = 0; c = seq[i]; i++)
475     {
476       if (c == '\\')
477         {
478           c = seq[++i];
479
480           if (c == 0)
481             break;
482
483           /* Handle \C- and \M- prefixes. */
484           if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
485             {
486               /* Handle special case of backwards define. */
487               if (strncmp (&seq[i], "C-\\M-", 5) == 0)
488                 {
489                   array[l++] = ESC;     /* ESC is meta-prefix */
490                   i += 5;
491                   array[l++] = CTRL (_rl_to_upper (seq[i]));
492                   if (seq[i] == '\0')
493                     i--;
494                 }
495               else if (c == 'M')
496                 {
497                   i++;          /* seq[i] == '-' */
498                   /* XXX - obey convert-meta setting */
499                   if (_rl_convert_meta_chars_to_ascii && _rl_keymap[ESC].type == ISKMAP)
500                     array[l++] = ESC;   /* ESC is meta-prefix */
501                   else if (seq[i+1] == '\\' && seq[i+2] == 'C' && seq[i+3] == '-')
502                     {
503                       i += 4;
504                       temp = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
505                       array[l++] = META (temp);
506                     }
507                   else
508                     {
509                       /* This doesn't yet handle things like \M-\a, which may
510                          or may not have any reasonable meaning.  You're
511                          probably better off using straight octal or hex. */
512                       i++;
513                       array[l++] = META (seq[i]);
514                     }
515                 }
516               else if (c == 'C')
517                 {
518                   i += 2;
519                   /* Special hack for C-?... */
520                   array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
521                 }
522               continue;
523             }         
524
525           /* Translate other backslash-escaped characters.  These are the
526              same escape sequences that bash's `echo' and `printf' builtins
527              handle, with the addition of \d -> RUBOUT.  A backslash
528              preceding a character that is not special is stripped. */
529           switch (c)
530             {
531             case 'a':
532               array[l++] = '\007';
533               break;
534             case 'b':
535               array[l++] = '\b';
536               break;
537             case 'd':
538               array[l++] = RUBOUT;      /* readline-specific */
539               break;
540             case 'e':
541               array[l++] = ESC;
542               break;
543             case 'f':
544               array[l++] = '\f';
545               break;
546             case 'n':
547               array[l++] = NEWLINE;
548               break;
549             case 'r':
550               array[l++] = RETURN;
551               break;
552             case 't':
553               array[l++] = TAB;
554               break;
555             case 'v':
556               array[l++] = 0x0B;
557               break;
558             case '\\':
559               array[l++] = '\\';
560               break;
561             case '0': case '1': case '2': case '3':
562             case '4': case '5': case '6': case '7':
563               i++;
564               for (temp = 2, c -= '0'; ISOCTAL ((unsigned char)seq[i]) && temp--; i++)
565                 c = (c * 8) + OCTVALUE (seq[i]);
566               i--;      /* auto-increment in for loop */
567               array[l++] = c & largest_char;
568               break;
569             case 'x':
570               i++;
571               for (temp = 2, c = 0; ISXDIGIT ((unsigned char)seq[i]) && temp--; i++)
572                 c = (c * 16) + HEXVALUE (seq[i]);
573               if (temp == 2)
574                 c = 'x';
575               i--;      /* auto-increment in for loop */
576               array[l++] = c & largest_char;
577               break;
578             default:    /* backslashes before non-special chars just add the char */
579               array[l++] = c;
580               break;    /* the backslash is stripped */
581             }
582           continue;
583         }
584
585       array[l++] = c;
586     }
587
588   *len = l;
589   array[l] = '\0';
590   return (0);
591 }
592
593 static int
594 _rl_isescape (c)
595      int c;
596 {
597   switch (c)
598     {
599     case '\007':
600     case '\b':
601     case '\f':
602     case '\n':
603     case '\r':
604     case TAB:
605     case 0x0b:  return (1);
606     default: return (0);
607     }
608 }
609
610 static int
611 _rl_escchar (c)
612      int c;
613 {
614   switch (c)
615     {
616     case '\007':  return ('a');
617     case '\b':  return ('b');
618     case '\f':  return ('f');
619     case '\n':  return ('n');
620     case '\r':  return ('r');
621     case TAB:  return ('t');
622     case 0x0b:  return ('v');
623     default: return (c);
624     }
625 }
626
627 char *
628 rl_untranslate_keyseq (seq)
629      int seq;
630 {
631   static char kseq[16];
632   int i, c;
633
634   i = 0;
635   c = seq;
636   if (META_CHAR (c))
637     {
638       kseq[i++] = '\\';
639       kseq[i++] = 'M';
640       kseq[i++] = '-';
641       c = UNMETA (c);
642     }
643   else if (c == ESC)
644     {
645       kseq[i++] = '\\';
646       c = 'e';
647     }
648   else if (CTRL_CHAR (c))
649     {
650       kseq[i++] = '\\';
651       kseq[i++] = 'C';
652       kseq[i++] = '-';
653       c = _rl_to_lower (UNCTRL (c));
654     }
655   else if (c == RUBOUT)
656     {
657       kseq[i++] = '\\';
658       kseq[i++] = 'C';
659       kseq[i++] = '-';
660       c = '?';
661     }
662
663   if (c == ESC)
664     {
665       kseq[i++] = '\\';
666       c = 'e';
667     }
668   else if (c == '\\' || c == '"')
669     {
670       kseq[i++] = '\\';
671     }
672
673   kseq[i++] = (unsigned char) c;
674   kseq[i] = '\0';
675   return kseq;
676 }
677
678 char *
679 _rl_untranslate_macro_value (seq, use_escapes)
680      char *seq;
681      int use_escapes;
682 {
683   char *ret, *r, *s;
684   int c;
685
686   r = ret = (char *)xmalloc (7 * strlen (seq) + 1);
687   for (s = seq; *s; s++)
688     {
689       c = *s;
690       if (META_CHAR (c))
691         {
692           *r++ = '\\';
693           *r++ = 'M';
694           *r++ = '-';
695           c = UNMETA (c);
696         }
697       else if (c == ESC)
698         {
699           *r++ = '\\';
700           c = 'e';
701         }
702       else if (CTRL_CHAR (c))
703         {
704           *r++ = '\\';
705           if (use_escapes && _rl_isescape (c))
706             c = _rl_escchar (c);
707           else
708             {
709               *r++ = 'C';
710               *r++ = '-';
711               c = _rl_to_lower (UNCTRL (c));
712             }
713         }
714       else if (c == RUBOUT)
715         {
716           *r++ = '\\';
717           *r++ = 'C';
718           *r++ = '-';
719           c = '?';
720         }
721
722       if (c == ESC)
723         {
724           *r++ = '\\';
725           c = 'e';
726         }
727       else if (c == '\\' || c == '"')
728         *r++ = '\\';
729
730       *r++ = (unsigned char)c;
731     }
732   *r = '\0';
733   return ret;
734 }
735
736 /* Return a pointer to the function that STRING represents.
737    If STRING doesn't have a matching function, then a NULL pointer
738    is returned. */
739 rl_command_func_t *
740 rl_named_function (string)
741      const char *string;
742 {
743   register int i;
744
745   rl_initialize_funmap ();
746
747   for (i = 0; funmap[i]; i++)
748     if (_rl_stricmp (funmap[i]->name, string) == 0)
749       return (funmap[i]->function);
750   return ((rl_command_func_t *)NULL);
751 }
752
753 /* Return the function (or macro) definition which would be invoked via
754    KEYSEQ if executed in MAP.  If MAP is NULL, then the current keymap is
755    used.  TYPE, if non-NULL, is a pointer to an int which will receive the
756    type of the object pointed to.  One of ISFUNC (function), ISKMAP (keymap),
757    or ISMACR (macro). */
758 rl_command_func_t *
759 rl_function_of_keyseq (keyseq, map, type)
760      const char *keyseq;
761      Keymap map;
762      int *type;
763 {
764   register int i;
765
766   if (map == 0)
767     map = _rl_keymap;
768
769   for (i = 0; keyseq && keyseq[i]; i++)
770     {
771       unsigned char ic = keyseq[i];
772
773       if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
774         {
775           if (map[ESC].type == ISKMAP)
776             {
777               map = FUNCTION_TO_KEYMAP (map, ESC);
778               ic = UNMETA (ic);
779             }
780           /* XXX - should we just return NULL here, since this obviously
781              doesn't match? */
782           else
783             {
784               if (type)
785                 *type = map[ESC].type;
786
787               return (map[ESC].function);
788             }
789         }
790
791       if (map[ic].type == ISKMAP)
792         {
793           /* If this is the last key in the key sequence, return the
794              map. */
795           if (keyseq[i + 1] == '\0')
796             {
797               if (type)
798                 *type = ISKMAP;
799
800               return (map[ic].function);
801             }
802           else
803             map = FUNCTION_TO_KEYMAP (map, ic);
804         }
805       /* If we're not at the end of the key sequence, and the current key
806          is bound to something other than a keymap, then the entire key
807          sequence is not bound. */
808       else if (map[ic].type != ISKMAP && keyseq[i+1])
809         return ((rl_command_func_t *)NULL);
810       else      /* map[ic].type != ISKMAP && keyseq[i+1] == 0 */
811         {
812           if (type)
813             *type = map[ic].type;
814
815           return (map[ic].function);
816         }
817     }
818   return ((rl_command_func_t *) NULL);
819 }
820
821 /* The last key bindings file read. */
822 static char *last_readline_init_file = (char *)NULL;
823
824 /* The file we're currently reading key bindings from. */
825 static const char *current_readline_init_file;
826 static int current_readline_init_include_level;
827 static int current_readline_init_lineno;
828
829 /* Read FILENAME into a locally-allocated buffer and return the buffer.
830    The size of the buffer is returned in *SIZEP.  Returns NULL if any
831    errors were encountered. */
832 static char *
833 _rl_read_file (filename, sizep)
834      char *filename;
835      size_t *sizep;
836 {
837   struct stat finfo;
838   size_t file_size;
839   char *buffer;
840   int i, file;
841
842   if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
843     return ((char *)NULL);
844
845   file_size = (size_t)finfo.st_size;
846
847   /* check for overflow on very large files */
848   if (file_size != finfo.st_size || file_size + 1 < file_size)
849     {
850       if (file >= 0)
851         close (file);
852 #if defined (EFBIG)
853       errno = EFBIG;
854 #endif
855       return ((char *)NULL);
856     }
857
858   /* Read the file into BUFFER. */
859   buffer = (char *)xmalloc (file_size + 1);
860   i = read (file, buffer, file_size);
861   close (file);
862
863   if (i < 0)
864     {
865       xfree (buffer);
866       return ((char *)NULL);
867     }
868
869   RL_CHECK_SIGNALS ();
870
871   buffer[i] = '\0';
872   if (sizep)
873     *sizep = i;
874
875   return (buffer);
876 }
877
878 /* Re-read the current keybindings file. */
879 int
880 rl_re_read_init_file (count, ignore)
881      int count, ignore;
882 {
883   int r;
884   r = rl_read_init_file ((const char *)NULL);
885   rl_set_keymap_from_edit_mode ();
886   return r;
887 }
888
889 /* Do key bindings from a file.  If FILENAME is NULL it defaults
890    to the first non-null filename from this list:
891      1. the filename used for the previous call
892      2. the value of the shell variable `INPUTRC'
893      3. ~/.inputrc
894      4. /etc/inputrc
895    If the file existed and could be opened and read, 0 is returned,
896    otherwise errno is returned. */
897 int
898 rl_read_init_file (filename)
899      const char *filename;
900 {
901   /* Default the filename. */
902   if (filename == 0)
903     filename = last_readline_init_file;
904   if (filename == 0)
905     filename = sh_get_env_value ("INPUTRC");
906   if (filename == 0 || *filename == 0)
907     {
908       filename = DEFAULT_INPUTRC;
909       /* Try to read DEFAULT_INPUTRC; fall back to SYS_INPUTRC on failure */
910       if (_rl_read_init_file (filename, 0) == 0)
911         return 0;
912       filename = SYS_INPUTRC;
913     }
914
915 #if defined (__MSDOS__)
916   if (_rl_read_init_file (filename, 0) == 0)
917     return 0;
918   filename = "~/_inputrc";
919 #endif
920   return (_rl_read_init_file (filename, 0));
921 }
922
923 static int
924 _rl_read_init_file (filename, include_level)
925      const char *filename;
926      int include_level;
927 {
928   register int i;
929   char *buffer, *openname, *line, *end;
930   size_t file_size;
931
932   current_readline_init_file = filename;
933   current_readline_init_include_level = include_level;
934
935   openname = tilde_expand (filename);
936   buffer = _rl_read_file (openname, &file_size);
937   xfree (openname);
938
939   RL_CHECK_SIGNALS ();
940   if (buffer == 0)
941     return (errno);
942   
943   if (include_level == 0 && filename != last_readline_init_file)
944     {
945       FREE (last_readline_init_file);
946       last_readline_init_file = savestring (filename);
947     }
948
949   currently_reading_init_file = 1;
950
951   /* Loop over the lines in the file.  Lines that start with `#' are
952      comments; all other lines are commands for readline initialization. */
953   current_readline_init_lineno = 1;
954   line = buffer;
955   end = buffer + file_size;
956   while (line < end)
957     {
958       /* Find the end of this line. */
959       for (i = 0; line + i != end && line[i] != '\n'; i++);
960
961 #if defined (__CYGWIN__)
962       /* ``Be liberal in what you accept.'' */
963       if (line[i] == '\n' && line[i-1] == '\r')
964         line[i - 1] = '\0';
965 #endif
966
967       /* Mark end of line. */
968       line[i] = '\0';
969
970       /* Skip leading whitespace. */
971       while (*line && whitespace (*line))
972         {
973           line++;
974           i--;
975         }
976
977       /* If the line is not a comment, then parse it. */
978       if (*line && *line != '#')
979         rl_parse_and_bind (line);
980
981       /* Move to the next line. */
982       line += i + 1;
983       current_readline_init_lineno++;
984     }
985
986   xfree (buffer);
987   currently_reading_init_file = 0;
988   return (0);
989 }
990
991 static void
992 _rl_init_file_error (msg)
993      const char *msg;
994 {
995   if (currently_reading_init_file)
996     _rl_errmsg ("%s: line %d: %s\n", current_readline_init_file,
997                      current_readline_init_lineno, msg);
998   else
999     _rl_errmsg ("%s", msg);
1000 }
1001
1002 /* **************************************************************** */
1003 /*                                                                  */
1004 /*                      Parser Directives                           */
1005 /*                                                                  */
1006 /* **************************************************************** */
1007
1008 typedef int _rl_parser_func_t PARAMS((char *));
1009
1010 /* Things that mean `Control'. */
1011 const char * const _rl_possible_control_prefixes[] = {
1012   "Control-", "C-", "CTRL-", (const char *)NULL
1013 };
1014
1015 const char * const _rl_possible_meta_prefixes[] = {
1016   "Meta", "M-", (const char *)NULL
1017 };
1018
1019 /* Conditionals. */
1020
1021 /* Calling programs set this to have their argv[0]. */
1022 const char *rl_readline_name = "other";
1023
1024 /* Stack of previous values of parsing_conditionalized_out. */
1025 static unsigned char *if_stack = (unsigned char *)NULL;
1026 static int if_stack_depth;
1027 static int if_stack_size;
1028
1029 /* Push _rl_parsing_conditionalized_out, and set parser state based
1030    on ARGS. */
1031 static int
1032 parser_if (args)
1033      char *args;
1034 {
1035   register int i;
1036
1037   /* Push parser state. */
1038   if (if_stack_depth + 1 >= if_stack_size)
1039     {
1040       if (!if_stack)
1041         if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
1042       else
1043         if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
1044     }
1045   if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
1046
1047   /* If parsing is turned off, then nothing can turn it back on except
1048      for finding the matching endif.  In that case, return right now. */
1049   if (_rl_parsing_conditionalized_out)
1050     return 0;
1051
1052   /* Isolate first argument. */
1053   for (i = 0; args[i] && !whitespace (args[i]); i++);
1054
1055   if (args[i])
1056     args[i++] = '\0';
1057
1058   /* Handle "$if term=foo" and "$if mode=emacs" constructs.  If this
1059      isn't term=foo, or mode=emacs, then check to see if the first
1060      word in ARGS is the same as the value stored in rl_readline_name. */
1061   if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
1062     {
1063       char *tem, *tname;
1064
1065       /* Terminals like "aaa-60" are equivalent to "aaa". */
1066       tname = savestring (rl_terminal_name);
1067       tem = strchr (tname, '-');
1068       if (tem)
1069         *tem = '\0';
1070
1071       /* Test the `long' and `short' forms of the terminal name so that
1072          if someone has a `sun-cmd' and does not want to have bindings
1073          that will be executed if the terminal is a `sun', they can put
1074          `$if term=sun-cmd' into their .inputrc. */
1075       _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
1076                                         _rl_stricmp (args + 5, rl_terminal_name);
1077       xfree (tname);
1078     }
1079 #if defined (VI_MODE)
1080   else if (_rl_strnicmp (args, "mode=", 5) == 0)
1081     {
1082       int mode;
1083
1084       if (_rl_stricmp (args + 5, "emacs") == 0)
1085         mode = emacs_mode;
1086       else if (_rl_stricmp (args + 5, "vi") == 0)
1087         mode = vi_mode;
1088       else
1089         mode = no_mode;
1090
1091       _rl_parsing_conditionalized_out = mode != rl_editing_mode;
1092     }
1093 #endif /* VI_MODE */
1094   /* Check to see if the first word in ARGS is the same as the
1095      value stored in rl_readline_name. */
1096   else if (_rl_stricmp (args, rl_readline_name) == 0)
1097     _rl_parsing_conditionalized_out = 0;
1098   else
1099     _rl_parsing_conditionalized_out = 1;
1100   return 0;
1101 }
1102
1103 /* Invert the current parser state if there is anything on the stack. */
1104 static int
1105 parser_else (args)
1106      char *args;
1107 {
1108   register int i;
1109
1110   if (if_stack_depth == 0)
1111     {
1112       _rl_init_file_error ("$else found without matching $if");
1113       return 0;
1114     }
1115
1116 #if 0
1117   /* Check the previous (n - 1) levels of the stack to make sure that
1118      we haven't previously turned off parsing. */
1119   for (i = 0; i < if_stack_depth - 1; i++)
1120 #else
1121   /* Check the previous (n) levels of the stack to make sure that
1122      we haven't previously turned off parsing. */
1123   for (i = 0; i < if_stack_depth; i++)
1124 #endif
1125     if (if_stack[i] == 1)
1126       return 0;
1127
1128   /* Invert the state of parsing if at top level. */
1129   _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
1130   return 0;
1131 }
1132
1133 /* Terminate a conditional, popping the value of
1134    _rl_parsing_conditionalized_out from the stack. */
1135 static int
1136 parser_endif (args)
1137      char *args;
1138 {
1139   if (if_stack_depth)
1140     _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
1141   else
1142     _rl_init_file_error ("$endif without matching $if");
1143   return 0;
1144 }
1145
1146 static int
1147 parser_include (args)
1148      char *args;
1149 {
1150   const char *old_init_file;
1151   char *e;
1152   int old_line_number, old_include_level, r;
1153
1154   if (_rl_parsing_conditionalized_out)
1155     return (0);
1156
1157   old_init_file = current_readline_init_file;
1158   old_line_number = current_readline_init_lineno;
1159   old_include_level = current_readline_init_include_level;
1160
1161   e = strchr (args, '\n');
1162   if (e)
1163     *e = '\0';
1164   r = _rl_read_init_file ((const char *)args, old_include_level + 1);
1165
1166   current_readline_init_file = old_init_file;
1167   current_readline_init_lineno = old_line_number;
1168   current_readline_init_include_level = old_include_level;
1169
1170   return r;
1171 }
1172   
1173 /* Associate textual names with actual functions. */
1174 static const struct {
1175   const char * const name;
1176   _rl_parser_func_t *function;
1177 } parser_directives [] = {
1178   { "if", parser_if },
1179   { "endif", parser_endif },
1180   { "else", parser_else },
1181   { "include", parser_include },
1182   { (char *)0x0, (_rl_parser_func_t *)0x0 }
1183 };
1184
1185 /* Handle a parser directive.  STATEMENT is the line of the directive
1186    without any leading `$'. */
1187 static int
1188 handle_parser_directive (statement)
1189      char *statement;
1190 {
1191   register int i;
1192   char *directive, *args;
1193
1194   /* Isolate the actual directive. */
1195
1196   /* Skip whitespace. */
1197   for (i = 0; whitespace (statement[i]); i++);
1198
1199   directive = &statement[i];
1200
1201   for (; statement[i] && !whitespace (statement[i]); i++);
1202
1203   if (statement[i])
1204     statement[i++] = '\0';
1205
1206   for (; statement[i] && whitespace (statement[i]); i++);
1207
1208   args = &statement[i];
1209
1210   /* Lookup the command, and act on it. */
1211   for (i = 0; parser_directives[i].name; i++)
1212     if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1213       {
1214         (*parser_directives[i].function) (args);
1215         return (0);
1216       }
1217
1218   /* display an error message about the unknown parser directive */
1219   _rl_init_file_error ("unknown parser directive");
1220   return (1);
1221 }
1222
1223 /* Start at STRING[START] and look for DELIM.  Return I where STRING[I] ==
1224    DELIM or STRING[I] == 0.  DELIM is usually a double quote. */
1225 static int
1226 _rl_skip_to_delim (string, start, delim)
1227      char *string;
1228      int start, delim;
1229 {
1230   int i, c, passc;
1231
1232   for (i = start,passc = 0; c = string[i]; i++)
1233     {
1234       if (passc)
1235         {
1236           passc = 0;
1237           if (c == 0)
1238             break;
1239           continue;
1240         }
1241
1242       if (c == '\\')
1243         {
1244           passc = 1;
1245           continue;
1246         }
1247
1248       if (c == delim)
1249         break;
1250     }
1251
1252   return i;
1253 }
1254
1255 /* Read the binding command from STRING and perform it.
1256    A key binding command looks like: Keyname: function-name\0,
1257    a variable binding command looks like: set variable value.
1258    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1259 int
1260 rl_parse_and_bind (string)
1261      char *string;
1262 {
1263   char *funname, *kname;
1264   register int c, i;
1265   int key, equivalency;
1266
1267   while (string && whitespace (*string))
1268     string++;
1269
1270   if (string == 0 || *string == 0 || *string == '#')
1271     return 0;
1272
1273   /* If this is a parser directive, act on it. */
1274   if (*string == '$')
1275     {
1276       handle_parser_directive (&string[1]);
1277       return 0;
1278     }
1279
1280   /* If we aren't supposed to be parsing right now, then we're done. */
1281   if (_rl_parsing_conditionalized_out)
1282     return 0;
1283
1284   i = 0;
1285   /* If this keyname is a complex key expression surrounded by quotes,
1286      advance to after the matching close quote.  This code allows the
1287      backslash to quote characters in the key expression. */
1288   if (*string == '"')
1289     {
1290       i = _rl_skip_to_delim (string, 1, '"');
1291
1292       /* If we didn't find a closing quote, abort the line. */
1293       if (string[i] == '\0')
1294         {
1295           _rl_init_file_error ("no closing `\"' in key binding");
1296           return 1;
1297         }
1298       else
1299         i++;    /* skip past closing double quote */
1300     }
1301
1302   /* Advance to the colon (:) or whitespace which separates the two objects. */
1303   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1304
1305   equivalency = (c == ':' && string[i + 1] == '=');
1306
1307   /* Mark the end of the command (or keyname). */
1308   if (string[i])
1309     string[i++] = '\0';
1310
1311   /* If doing assignment, skip the '=' sign as well. */
1312   if (equivalency)
1313     string[i++] = '\0';
1314
1315   /* If this is a command to set a variable, then do that. */
1316   if (_rl_stricmp (string, "set") == 0)
1317     {
1318       char *var, *value, *e;
1319       int s;
1320
1321       var = string + i;
1322       /* Make VAR point to start of variable name. */
1323       while (*var && whitespace (*var)) var++;
1324
1325       /* Make VALUE point to start of value string. */
1326       value = var;
1327       while (*value && whitespace (*value) == 0) value++;
1328       if (*value)
1329         *value++ = '\0';
1330       while (*value && whitespace (*value)) value++;
1331
1332       /* Strip trailing whitespace from values of boolean variables. */
1333       if (find_boolean_var (var) >= 0)
1334         {
1335           /* remove trailing whitespace */
1336 remove_trailing:
1337           e = value + strlen (value) - 1;
1338           while (e >= value && whitespace (*e))
1339             e--;
1340           e++;          /* skip back to whitespace or EOS */
1341           
1342           if (*e && e >= value)
1343             *e = '\0';
1344         }
1345       else if ((i = find_string_var (var)) >= 0)
1346         {
1347           /* Allow quoted strings in variable values */
1348           if (*value == '"')
1349             {
1350               i = _rl_skip_to_delim (value, 1, *value);
1351               value[i] = '\0';
1352               value++;  /* skip past the quote */
1353             }
1354           else
1355             goto remove_trailing;
1356         }
1357         
1358       rl_variable_bind (var, value);
1359       return 0;
1360     }
1361
1362   /* Skip any whitespace between keyname and funname. */
1363   for (; string[i] && whitespace (string[i]); i++);
1364   funname = &string[i];
1365
1366   /* Now isolate funname.
1367      For straight function names just look for whitespace, since
1368      that will signify the end of the string.  But this could be a
1369      macro definition.  In that case, the string is quoted, so skip
1370      to the matching delimiter.  We allow the backslash to quote the
1371      delimiter characters in the macro body. */
1372   /* This code exists to allow whitespace in macro expansions, which
1373      would otherwise be gobbled up by the next `for' loop.*/
1374   /* XXX - it may be desirable to allow backslash quoting only if " is
1375      the quoted string delimiter, like the shell. */
1376   if (*funname == '\'' || *funname == '"')
1377     {
1378       i = _rl_skip_to_delim (string, i+1, *funname);
1379       if (string[i])
1380         i++;
1381     }
1382
1383   /* Advance to the end of the string.  */
1384   for (; string[i] && whitespace (string[i]) == 0; i++);
1385
1386   /* No extra whitespace at the end of the string. */
1387   string[i] = '\0';
1388
1389   /* Handle equivalency bindings here.  Make the left-hand side be exactly
1390      whatever the right-hand evaluates to, including keymaps. */
1391   if (equivalency)
1392     {
1393       return 0;
1394     }
1395
1396   /* If this is a new-style key-binding, then do the binding with
1397      rl_bind_keyseq ().  Otherwise, let the older code deal with it. */
1398   if (*string == '"')
1399     {
1400       char *seq;
1401       register int j, k, passc;
1402
1403       seq = (char *)xmalloc (1 + strlen (string));
1404       for (j = 1, k = passc = 0; string[j]; j++)
1405         {
1406           /* Allow backslash to quote characters, but leave them in place.
1407              This allows a string to end with a backslash quoting another
1408              backslash, or with a backslash quoting a double quote.  The
1409              backslashes are left in place for rl_translate_keyseq (). */
1410           if (passc || (string[j] == '\\'))
1411             {
1412               seq[k++] = string[j];
1413               passc = !passc;
1414               continue;
1415             }
1416
1417           if (string[j] == '"')
1418             break;
1419
1420           seq[k++] = string[j];
1421         }
1422       seq[k] = '\0';
1423
1424       /* Binding macro? */
1425       if (*funname == '\'' || *funname == '"')
1426         {
1427           j = strlen (funname);
1428
1429           /* Remove the delimiting quotes from each end of FUNNAME. */
1430           if (j && funname[j - 1] == *funname)
1431             funname[j - 1] = '\0';
1432
1433           rl_macro_bind (seq, &funname[1], _rl_keymap);
1434         }
1435       else
1436         rl_bind_keyseq (seq, rl_named_function (funname));
1437
1438       xfree (seq);
1439       return 0;
1440     }
1441
1442   /* Get the actual character we want to deal with. */
1443   kname = strrchr (string, '-');
1444   if (kname == 0)
1445     kname = string;
1446   else
1447     kname++;
1448
1449   key = glean_key_from_name (kname);
1450
1451   /* Add in control and meta bits. */
1452   if (substring_member_of_array (string, _rl_possible_control_prefixes))
1453     key = CTRL (_rl_to_upper (key));
1454
1455   if (substring_member_of_array (string, _rl_possible_meta_prefixes))
1456     key = META (key);
1457
1458   /* Temporary.  Handle old-style keyname with macro-binding. */
1459   if (*funname == '\'' || *funname == '"')
1460     {
1461       char useq[2];
1462       int fl = strlen (funname);
1463
1464       useq[0] = key; useq[1] = '\0';
1465       if (fl && funname[fl - 1] == *funname)
1466         funname[fl - 1] = '\0';
1467
1468       rl_macro_bind (useq, &funname[1], _rl_keymap);
1469     }
1470 #if defined (PREFIX_META_HACK)
1471   /* Ugly, but working hack to keep prefix-meta around. */
1472   else if (_rl_stricmp (funname, "prefix-meta") == 0)
1473     {
1474       char seq[2];
1475
1476       seq[0] = key;
1477       seq[1] = '\0';
1478       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1479     }
1480 #endif /* PREFIX_META_HACK */
1481   else
1482     rl_bind_key (key, rl_named_function (funname));
1483   return 0;
1484 }
1485
1486 /* Simple structure for boolean readline variables (i.e., those that can
1487    have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1488    false. */
1489
1490 #define V_SPECIAL       0x1
1491
1492 static const struct {
1493   const char * const name;
1494   int *value;
1495   int flags;
1496 } boolean_varlist [] = {
1497   { "bind-tty-special-chars",   &_rl_bind_stty_chars,           0 },
1498   { "blink-matching-paren",     &rl_blink_matching_paren,       V_SPECIAL },
1499   { "byte-oriented",            &rl_byte_oriented,              0 },
1500 #if defined (COLOR_SUPPORT)
1501   { "colored-completion-prefix",&_rl_colored_completion_prefix, 0 },
1502   { "colored-stats",            &_rl_colored_stats,             0 },
1503 #endif
1504   { "completion-ignore-case",   &_rl_completion_case_fold,      0 },
1505   { "completion-map-case",      &_rl_completion_case_map,       0 },
1506   { "convert-meta",             &_rl_convert_meta_chars_to_ascii, 0 },
1507   { "disable-completion",       &rl_inhibit_completion,         0 },
1508   { "echo-control-characters",  &_rl_echo_control_chars,        0 },
1509   { "enable-bracketed-paste",   &_rl_enable_bracketed_paste,    0 },
1510   { "enable-keypad",            &_rl_enable_keypad,             0 },
1511   { "enable-meta-key",          &_rl_enable_meta,               0 },
1512   { "expand-tilde",             &rl_complete_with_tilde_expansion, 0 },
1513   { "history-preserve-point",   &_rl_history_preserve_point,    0 },
1514   { "horizontal-scroll-mode",   &_rl_horizontal_scroll_mode,    0 },
1515   { "input-meta",               &_rl_meta_flag,                 0 },
1516   { "mark-directories",         &_rl_complete_mark_directories, 0 },
1517   { "mark-modified-lines",      &_rl_mark_modified_lines,       0 },
1518   { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 },
1519   { "match-hidden-files",       &_rl_match_hidden_files,        0 },
1520   { "menu-complete-display-prefix", &_rl_menu_complete_prefix_first, 0 },
1521   { "meta-flag",                &_rl_meta_flag,                 0 },
1522   { "output-meta",              &_rl_output_meta_chars,         0 },
1523   { "page-completions",         &_rl_page_completions,          0 },
1524   { "prefer-visible-bell",      &_rl_prefer_visible_bell,       V_SPECIAL },
1525   { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1526   { "revert-all-at-newline",    &_rl_revert_all_at_newline,     0 },
1527   { "show-all-if-ambiguous",    &_rl_complete_show_all,         0 },
1528   { "show-all-if-unmodified",   &_rl_complete_show_unmodified,  0 },
1529   { "show-mode-in-prompt",      &_rl_show_mode_in_prompt,       0 },
1530   { "skip-completed-text",      &_rl_skip_completed_text,       0 },
1531 #if defined (VISIBLE_STATS)
1532   { "visible-stats",            &rl_visible_stats,              0 },
1533 #endif /* VISIBLE_STATS */
1534   { (char *)NULL, (int *)NULL, 0 }
1535 };
1536
1537 static int
1538 find_boolean_var (name)
1539      const char *name;
1540 {
1541   register int i;
1542
1543   for (i = 0; boolean_varlist[i].name; i++)
1544     if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1545       return i;
1546   return -1;
1547 }
1548
1549 /* Hooks for handling special boolean variables, where a
1550    function needs to be called or another variable needs
1551    to be changed when they're changed. */
1552 static void
1553 hack_special_boolean_var (i)
1554      int i;
1555 {
1556   const char *name;
1557
1558   name = boolean_varlist[i].name;
1559
1560   if (_rl_stricmp (name, "blink-matching-paren") == 0)
1561     _rl_enable_paren_matching (rl_blink_matching_paren);
1562   else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1563     {
1564       if (_rl_prefer_visible_bell)
1565         _rl_bell_preference = VISIBLE_BELL;
1566       else
1567         _rl_bell_preference = AUDIBLE_BELL;
1568     }
1569   else if (_rl_stricmp (name, "show-mode-in-prompt") == 0)
1570     _rl_reset_prompt ();
1571 }
1572
1573 typedef int _rl_sv_func_t PARAMS((const char *));
1574
1575 /* These *must* correspond to the array indices for the appropriate
1576    string variable.  (Though they're not used right now.) */
1577 #define V_BELLSTYLE     0
1578 #define V_COMBEGIN      1
1579 #define V_EDITMODE      2
1580 #define V_ISRCHTERM     3
1581 #define V_KEYMAP        4
1582
1583 #define V_STRING        1
1584 #define V_INT           2
1585
1586 /* Forward declarations */
1587 static int sv_bell_style PARAMS((const char *));
1588 static int sv_combegin PARAMS((const char *));
1589 static int sv_dispprefix PARAMS((const char *));
1590 static int sv_compquery PARAMS((const char *));
1591 static int sv_compwidth PARAMS((const char *));
1592 static int sv_editmode PARAMS((const char *));
1593 static int sv_emacs_modestr PARAMS((const char *));
1594 static int sv_histsize PARAMS((const char *));
1595 static int sv_isrchterm PARAMS((const char *));
1596 static int sv_keymap PARAMS((const char *));
1597 static int sv_seqtimeout PARAMS((const char *));
1598 static int sv_viins_modestr PARAMS((const char *));
1599 static int sv_vicmd_modestr PARAMS((const char *));
1600
1601 static const struct {
1602   const char * const name;
1603   int flags;
1604   _rl_sv_func_t *set_func;
1605 } string_varlist[] = {
1606   { "bell-style",       V_STRING,       sv_bell_style },
1607   { "comment-begin",    V_STRING,       sv_combegin },
1608   { "completion-display-width", V_INT,  sv_compwidth },
1609   { "completion-prefix-display-length", V_INT,  sv_dispprefix },
1610   { "completion-query-items", V_INT,    sv_compquery },
1611   { "editing-mode",     V_STRING,       sv_editmode },
1612   { "emacs-mode-string", V_STRING,      sv_emacs_modestr },  
1613   { "history-size",     V_INT,          sv_histsize },
1614   { "isearch-terminators", V_STRING,    sv_isrchterm },
1615   { "keymap",           V_STRING,       sv_keymap },
1616   { "keyseq-timeout",   V_INT,          sv_seqtimeout },
1617   { "vi-cmd-mode-string", V_STRING,     sv_vicmd_modestr }, 
1618   { "vi-ins-mode-string", V_STRING,     sv_viins_modestr }, 
1619   { (char *)NULL,       0, (_rl_sv_func_t *)0 }
1620 };
1621
1622 static int
1623 find_string_var (name)
1624      const char *name;
1625 {
1626   register int i;
1627
1628   for (i = 0; string_varlist[i].name; i++)
1629     if (_rl_stricmp (name, string_varlist[i].name) == 0)
1630       return i;
1631   return -1;
1632 }
1633
1634 /* A boolean value that can appear in a `set variable' command is true if
1635    the value is null or empty, `on' (case-insensitive), or "1".  Any other
1636    values result in 0 (false). */
1637 static int
1638 bool_to_int (value)
1639      const char *value;
1640 {
1641   return (value == 0 || *value == '\0' ||
1642                 (_rl_stricmp (value, "on") == 0) ||
1643                 (value[0] == '1' && value[1] == '\0'));
1644 }
1645
1646 char *
1647 rl_variable_value (name)
1648      const char *name;
1649 {
1650   register int i;
1651
1652   /* Check for simple variables first. */
1653   i = find_boolean_var (name);
1654   if (i >= 0)
1655     return (*boolean_varlist[i].value ? "on" : "off");
1656
1657   i = find_string_var (name);
1658   if (i >= 0)
1659     return (_rl_get_string_variable_value (string_varlist[i].name));
1660
1661   /* Unknown variable names return NULL. */
1662   return 0;
1663 }
1664
1665 int
1666 rl_variable_bind (name, value)
1667      const char *name, *value;
1668 {
1669   register int i;
1670   int   v;
1671
1672   /* Check for simple variables first. */
1673   i = find_boolean_var (name);
1674   if (i >= 0)
1675     {
1676       *boolean_varlist[i].value = bool_to_int (value);
1677       if (boolean_varlist[i].flags & V_SPECIAL)
1678         hack_special_boolean_var (i);
1679       return 0;
1680     }
1681
1682   i = find_string_var (name);
1683
1684   /* For the time being, unknown variable names or string names without a
1685      handler function are simply ignored. */
1686   if (i < 0 || string_varlist[i].set_func == 0)
1687     return 0;
1688
1689   v = (*string_varlist[i].set_func) (value);
1690   return v;
1691 }
1692
1693 static int
1694 sv_editmode (value)
1695      const char *value;
1696 {
1697   if (_rl_strnicmp (value, "vi", 2) == 0)
1698     {
1699 #if defined (VI_MODE)
1700       _rl_keymap = vi_insertion_keymap;
1701       rl_editing_mode = vi_mode;
1702 #endif /* VI_MODE */
1703       return 0;
1704     }
1705   else if (_rl_strnicmp (value, "emacs", 5) == 0)
1706     {
1707       _rl_keymap = emacs_standard_keymap;
1708       rl_editing_mode = emacs_mode;
1709       return 0;
1710     }
1711   return 1;
1712 }
1713
1714 static int
1715 sv_combegin (value)
1716      const char *value;
1717 {
1718   if (value && *value)
1719     {
1720       FREE (_rl_comment_begin);
1721       _rl_comment_begin = savestring (value);
1722       return 0;
1723     }
1724   return 1;
1725 }
1726
1727 static int
1728 sv_dispprefix (value)
1729      const char *value;
1730 {
1731   int nval = 0;
1732
1733   if (value && *value)
1734     {
1735       nval = atoi (value);
1736       if (nval < 0)
1737         nval = 0;
1738     }
1739   _rl_completion_prefix_display_length = nval;
1740   return 0;
1741 }
1742
1743 static int
1744 sv_compquery (value)
1745      const char *value;
1746 {
1747   int nval = 100;
1748
1749   if (value && *value)
1750     {
1751       nval = atoi (value);
1752       if (nval < 0)
1753         nval = 0;
1754     }
1755   rl_completion_query_items = nval;
1756   return 0;
1757 }
1758
1759 static int
1760 sv_compwidth (value)
1761      const char *value;
1762 {
1763   int nval = -1;
1764
1765   if (value && *value)
1766     nval = atoi (value);
1767
1768   _rl_completion_columns = nval;
1769   return 0;
1770 }
1771
1772 static int
1773 sv_histsize (value)
1774      const char *value;
1775 {
1776   int nval;
1777
1778   nval = 500;
1779   if (value && *value)
1780     {
1781       nval = atoi (value);
1782       if (nval < 0)
1783         {
1784           unstifle_history ();
1785           return 0;
1786         }
1787     }
1788   stifle_history (nval);
1789   return 0;
1790 }
1791
1792 static int
1793 sv_keymap (value)
1794      const char *value;
1795 {
1796   Keymap kmap;
1797
1798   kmap = rl_get_keymap_by_name (value);
1799   if (kmap)
1800     {
1801       rl_set_keymap (kmap);
1802       return 0;
1803     }
1804   return 1;
1805 }
1806
1807 static int
1808 sv_seqtimeout (value)
1809      const char *value;
1810 {
1811   int nval;
1812
1813   nval = 0;
1814   if (value && *value)
1815     {
1816       nval = atoi (value);
1817       if (nval < 0)
1818         nval = 0;
1819     }
1820   _rl_keyseq_timeout = nval;
1821   return 0;
1822 }
1823
1824 static int
1825 sv_bell_style (value)
1826      const char *value;
1827 {
1828   if (value == 0 || *value == '\0')
1829     _rl_bell_preference = AUDIBLE_BELL;
1830   else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1831     _rl_bell_preference = NO_BELL;
1832   else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1833     _rl_bell_preference = AUDIBLE_BELL;
1834   else if (_rl_stricmp (value, "visible") == 0)
1835     _rl_bell_preference = VISIBLE_BELL;
1836   else
1837     return 1;
1838   return 0;
1839 }
1840
1841 static int
1842 sv_isrchterm (value)
1843      const char *value;
1844 {
1845   int beg, end, delim;
1846   char *v;
1847
1848   if (value == 0)
1849     return 1;
1850
1851   /* Isolate the value and translate it into a character string. */
1852   v = savestring (value);
1853   FREE (_rl_isearch_terminators);
1854   if (v[0] == '"' || v[0] == '\'')
1855     {
1856       delim = v[0];
1857       for (beg = end = 1; v[end] && v[end] != delim; end++)
1858         ;
1859     }
1860   else
1861     {
1862       for (beg = end = 0; v[end] && whitespace (v[end]) == 0; end++)
1863         ;
1864     }
1865
1866   v[end] = '\0';
1867
1868   /* The value starts at v + beg.  Translate it into a character string. */
1869   _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1);
1870   rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1871   _rl_isearch_terminators[end] = '\0';
1872
1873   xfree (v);
1874   return 0;
1875 }
1876
1877 extern char *_rl_emacs_mode_str;
1878
1879 static int
1880 sv_emacs_modestr (value)
1881      const char *value;
1882 {
1883   if (value && *value)
1884     {
1885       FREE (_rl_emacs_mode_str);
1886       _rl_emacs_mode_str = (char *)xmalloc (2 * strlen (value) + 1);
1887       rl_translate_keyseq (value, _rl_emacs_mode_str, &_rl_emacs_modestr_len);
1888       _rl_emacs_mode_str[_rl_emacs_modestr_len] = '\0';
1889       return 0;
1890     }
1891   else if (value)
1892     {
1893       FREE (_rl_emacs_mode_str);
1894       _rl_emacs_mode_str = (char *)xmalloc (1);
1895       _rl_emacs_mode_str[_rl_emacs_modestr_len = 0] = '\0';
1896       return 0;
1897     }
1898   else if (value == 0)
1899     {
1900       FREE (_rl_emacs_mode_str);
1901       _rl_emacs_mode_str = 0;   /* prompt_modestr does the right thing */
1902       _rl_emacs_modestr_len = 0;
1903       return 0;
1904     }
1905   return 1;
1906 }
1907
1908 static int
1909 sv_viins_modestr (value)
1910      const char *value;
1911 {
1912   if (value && *value)
1913     {
1914       FREE (_rl_vi_ins_mode_str);
1915       _rl_vi_ins_mode_str = (char *)xmalloc (2 * strlen (value) + 1);
1916       rl_translate_keyseq (value, _rl_vi_ins_mode_str, &_rl_vi_ins_modestr_len);
1917       _rl_vi_ins_mode_str[_rl_vi_ins_modestr_len] = '\0';
1918       return 0;
1919     }
1920   else if (value)
1921     {
1922       FREE (_rl_vi_ins_mode_str);
1923       _rl_vi_ins_mode_str = (char *)xmalloc (1);
1924       _rl_vi_ins_mode_str[_rl_vi_ins_modestr_len = 0] = '\0';
1925       return 0;
1926     }
1927   else if (value == 0)
1928     {
1929       FREE (_rl_vi_ins_mode_str);
1930       _rl_vi_ins_mode_str = 0;  /* prompt_modestr does the right thing */
1931       _rl_vi_ins_modestr_len = 0;
1932       return 0;
1933     }
1934   return 1;
1935 }
1936
1937 static int
1938 sv_vicmd_modestr (value)
1939      const char *value;
1940 {
1941   if (value && *value)
1942     {
1943       FREE (_rl_vi_cmd_mode_str);
1944       _rl_vi_cmd_mode_str = (char *)xmalloc (2 * strlen (value) + 1);
1945       rl_translate_keyseq (value, _rl_vi_cmd_mode_str, &_rl_vi_cmd_modestr_len);
1946       _rl_vi_cmd_mode_str[_rl_vi_cmd_modestr_len] = '\0';
1947       return 0;
1948     }
1949   else if (value)
1950     {
1951       FREE (_rl_vi_cmd_mode_str);
1952       _rl_vi_cmd_mode_str = (char *)xmalloc (1);
1953       _rl_vi_cmd_mode_str[_rl_vi_cmd_modestr_len = 0] = '\0';
1954       return 0;
1955     }
1956   else if (value == 0)
1957     {
1958       FREE (_rl_vi_cmd_mode_str);
1959       _rl_vi_cmd_mode_str = 0;  /* prompt_modestr does the right thing */
1960       _rl_vi_cmd_modestr_len = 0;
1961       return 0;
1962     }
1963   return 1;
1964 }
1965
1966 /* Return the character which matches NAME.
1967    For example, `Space' returns ' '. */
1968
1969 typedef struct {
1970   const char * const name;
1971   int value;
1972 } assoc_list;
1973
1974 static const assoc_list name_key_alist[] = {
1975   { "DEL", 0x7f },
1976   { "ESC", '\033' },
1977   { "Escape", '\033' },
1978   { "LFD", '\n' },
1979   { "Newline", '\n' },
1980   { "RET", '\r' },
1981   { "Return", '\r' },
1982   { "Rubout", 0x7f },
1983   { "SPC", ' ' },
1984   { "Space", ' ' },
1985   { "Tab", 0x09 },
1986   { (char *)0x0, 0 }
1987 };
1988
1989 static int
1990 glean_key_from_name (name)
1991      char *name;
1992 {
1993   register int i;
1994
1995   for (i = 0; name_key_alist[i].name; i++)
1996     if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1997       return (name_key_alist[i].value);
1998
1999   return (*(unsigned char *)name);      /* XXX was return (*name) */
2000 }
2001
2002 /* Auxiliary functions to manage keymaps. */
2003 static const struct {
2004   const char * const name;
2005   Keymap map;
2006 } keymap_names[] = {
2007   { "emacs", emacs_standard_keymap },
2008   { "emacs-standard", emacs_standard_keymap },
2009   { "emacs-meta", emacs_meta_keymap },
2010   { "emacs-ctlx", emacs_ctlx_keymap },
2011 #if defined (VI_MODE)
2012   { "vi", vi_movement_keymap },
2013   { "vi-move", vi_movement_keymap },
2014   { "vi-command", vi_movement_keymap },
2015   { "vi-insert", vi_insertion_keymap },
2016 #endif /* VI_MODE */
2017   { (char *)0x0, (Keymap)0x0 }
2018 };
2019
2020 Keymap
2021 rl_get_keymap_by_name (name)
2022      const char *name;
2023 {
2024   register int i;
2025
2026   for (i = 0; keymap_names[i].name; i++)
2027     if (_rl_stricmp (name, keymap_names[i].name) == 0)
2028       return (keymap_names[i].map);
2029   return ((Keymap) NULL);
2030 }
2031
2032 char *
2033 rl_get_keymap_name (map)
2034      Keymap map;
2035 {
2036   register int i;
2037   for (i = 0; keymap_names[i].name; i++)
2038     if (map == keymap_names[i].map)
2039       return ((char *)keymap_names[i].name);
2040   return ((char *)NULL);
2041 }
2042   
2043 void
2044 rl_set_keymap (map)
2045      Keymap map;
2046 {
2047   if (map)
2048     _rl_keymap = map;
2049 }
2050
2051 Keymap
2052 rl_get_keymap ()
2053 {
2054   return (_rl_keymap);
2055 }
2056
2057 void
2058 rl_set_keymap_from_edit_mode ()
2059 {
2060   if (rl_editing_mode == emacs_mode)
2061     _rl_keymap = emacs_standard_keymap;
2062 #if defined (VI_MODE)
2063   else if (rl_editing_mode == vi_mode)
2064     _rl_keymap = vi_insertion_keymap;
2065 #endif /* VI_MODE */
2066 }
2067
2068 char *
2069 rl_get_keymap_name_from_edit_mode ()
2070 {
2071   if (rl_editing_mode == emacs_mode)
2072     return "emacs";
2073 #if defined (VI_MODE)
2074   else if (rl_editing_mode == vi_mode)
2075     return "vi";
2076 #endif /* VI_MODE */
2077   else
2078     return "none";
2079 }
2080
2081 /* **************************************************************** */
2082 /*                                                                  */
2083 /*                Key Binding and Function Information              */
2084 /*                                                                  */
2085 /* **************************************************************** */
2086
2087 /* Each of the following functions produces information about the
2088    state of keybindings and functions known to Readline.  The info
2089    is always printed to rl_outstream, and in such a way that it can
2090    be read back in (i.e., passed to rl_parse_and_bind ()). */
2091
2092 /* Print the names of functions known to Readline. */
2093 void
2094 rl_list_funmap_names ()
2095 {
2096   register int i;
2097   const char **funmap_names;
2098
2099   funmap_names = rl_funmap_names ();
2100
2101   if (!funmap_names)
2102     return;
2103
2104   for (i = 0; funmap_names[i]; i++)
2105     fprintf (rl_outstream, "%s\n", funmap_names[i]);
2106
2107   xfree (funmap_names);
2108 }
2109
2110 static char *
2111 _rl_get_keyname (key)
2112      int key;
2113 {
2114   char *keyname;
2115   int i, c;
2116
2117   keyname = (char *)xmalloc (8);
2118
2119   c = key;
2120   /* Since this is going to be used to write out keysequence-function
2121      pairs for possible inclusion in an inputrc file, we don't want to
2122      do any special meta processing on KEY. */
2123
2124 #if 1
2125   /* XXX - Experimental */
2126   /* We might want to do this, but the old version of the code did not. */
2127
2128   /* If this is an escape character, we don't want to do any more processing.
2129      Just add the special ESC key sequence and return. */
2130   if (c == ESC)
2131     {
2132       keyname[0] = '\\';
2133       keyname[1] = 'e';
2134       keyname[2] = '\0';
2135       return keyname;
2136     }
2137 #endif
2138
2139   /* RUBOUT is translated directly into \C-? */
2140   if (key == RUBOUT)
2141     {
2142       keyname[0] = '\\';
2143       keyname[1] = 'C';
2144       keyname[2] = '-';
2145       keyname[3] = '?';
2146       keyname[4] = '\0';
2147       return keyname;
2148     }
2149
2150   i = 0;
2151   /* Now add special prefixes needed for control characters.  This can
2152      potentially change C. */
2153   if (CTRL_CHAR (c))
2154     {
2155       keyname[i++] = '\\';
2156       keyname[i++] = 'C';
2157       keyname[i++] = '-';
2158       c = _rl_to_lower (UNCTRL (c));
2159     }
2160
2161   /* XXX experimental code.  Turn the characters that are not ASCII or
2162      ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
2163      This changes C. */
2164   if (c >= 128 && c <= 159)
2165     {
2166       keyname[i++] = '\\';
2167       keyname[i++] = '2';
2168       c -= 128;
2169       keyname[i++] = (c / 8) + '0';
2170       c = (c % 8) + '0';
2171     }
2172
2173   /* Now, if the character needs to be quoted with a backslash, do that. */
2174   if (c == '\\' || c == '"')
2175     keyname[i++] = '\\';
2176
2177   /* Now add the key, terminate the string, and return it. */
2178   keyname[i++] = (char) c;
2179   keyname[i] = '\0';
2180
2181   return keyname;
2182 }
2183
2184 /* Return a NULL terminated array of strings which represent the key
2185    sequences that are used to invoke FUNCTION in MAP. */
2186 char **
2187 rl_invoking_keyseqs_in_map (function, map)
2188      rl_command_func_t *function;
2189      Keymap map;
2190 {
2191   register int key;
2192   char **result;
2193   int result_index, result_size;
2194
2195   result = (char **)NULL;
2196   result_index = result_size = 0;
2197
2198   for (key = 0; key < KEYMAP_SIZE; key++)
2199     {
2200       switch (map[key].type)
2201         {
2202         case ISMACR:
2203           /* Macros match, if, and only if, the pointers are identical.
2204              Thus, they are treated exactly like functions in here. */
2205         case ISFUNC:
2206           /* If the function in the keymap is the one we are looking for,
2207              then add the current KEY to the list of invoking keys. */
2208           if (map[key].function == function)
2209             {
2210               char *keyname;
2211
2212               keyname = _rl_get_keyname (key);
2213
2214               if (result_index + 2 > result_size)
2215                 {
2216                   result_size += 10;
2217                   result = (char **)xrealloc (result, result_size * sizeof (char *));
2218                 }
2219
2220               result[result_index++] = keyname;
2221               result[result_index] = (char *)NULL;
2222             }
2223           break;
2224
2225         case ISKMAP:
2226           {
2227             char **seqs;
2228             register int i;
2229
2230             /* Find the list of keyseqs in this map which have FUNCTION as
2231                their target.  Add the key sequences found to RESULT. */
2232             if (map[key].function)
2233               seqs =
2234                 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
2235             else
2236               break;
2237
2238             if (seqs == 0)
2239               break;
2240
2241             for (i = 0; seqs[i]; i++)
2242               {
2243                 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
2244
2245                 if (key == ESC)
2246                   {
2247                     /* If ESC is the meta prefix and we're converting chars
2248                        with the eighth bit set to ESC-prefixed sequences, then
2249                        we can use \M-.  Otherwise we need to use the sequence
2250                        for ESC. */
2251                     if (_rl_convert_meta_chars_to_ascii && map[ESC].type == ISKMAP)
2252                       sprintf (keyname, "\\M-");
2253                     else
2254                       sprintf (keyname, "\\e");
2255                   }
2256                 else if (CTRL_CHAR (key))
2257                   sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
2258                 else if (key == RUBOUT)
2259                   sprintf (keyname, "\\C-?");
2260                 else if (key == '\\' || key == '"')
2261                   {
2262                     keyname[0] = '\\';
2263                     keyname[1] = (char) key;
2264                     keyname[2] = '\0';
2265                   }
2266                 else
2267                   {
2268                     keyname[0] = (char) key;
2269                     keyname[1] = '\0';
2270                   }
2271                 
2272                 strcat (keyname, seqs[i]);
2273                 xfree (seqs[i]);
2274
2275                 if (result_index + 2 > result_size)
2276                   {
2277                     result_size += 10;
2278                     result = (char **)xrealloc (result, result_size * sizeof (char *));
2279                   }
2280
2281                 result[result_index++] = keyname;
2282                 result[result_index] = (char *)NULL;
2283               }
2284
2285             xfree (seqs);
2286           }
2287           break;
2288         }
2289     }
2290   return (result);
2291 }
2292
2293 /* Return a NULL terminated array of strings which represent the key
2294    sequences that can be used to invoke FUNCTION using the current keymap. */
2295 char **
2296 rl_invoking_keyseqs (function)
2297      rl_command_func_t *function;
2298 {
2299   return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
2300 }
2301
2302 /* Print all of the functions and their bindings to rl_outstream.  If
2303    PRINT_READABLY is non-zero, then print the output in such a way
2304    that it can be read back in. */
2305 void
2306 rl_function_dumper (print_readably)
2307      int print_readably;
2308 {
2309   register int i;
2310   const char **names;
2311   const char *name;
2312
2313   names = rl_funmap_names ();
2314
2315   fprintf (rl_outstream, "\n");
2316
2317   for (i = 0; name = names[i]; i++)
2318     {
2319       rl_command_func_t *function;
2320       char **invokers;
2321
2322       function = rl_named_function (name);
2323       invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
2324
2325       if (print_readably)
2326         {
2327           if (!invokers)
2328             fprintf (rl_outstream, "# %s (not bound)\n", name);
2329           else
2330             {
2331               register int j;
2332
2333               for (j = 0; invokers[j]; j++)
2334                 {
2335                   fprintf (rl_outstream, "\"%s\": %s\n",
2336                            invokers[j], name);
2337                   xfree (invokers[j]);
2338                 }
2339
2340               xfree (invokers);
2341             }
2342         }
2343       else
2344         {
2345           if (!invokers)
2346             fprintf (rl_outstream, "%s is not bound to any keys\n",
2347                      name);
2348           else
2349             {
2350               register int j;
2351
2352               fprintf (rl_outstream, "%s can be found on ", name);
2353
2354               for (j = 0; invokers[j] && j < 5; j++)
2355                 {
2356                   fprintf (rl_outstream, "\"%s\"%s", invokers[j],
2357                            invokers[j + 1] ? ", " : ".\n");
2358                 }
2359
2360               if (j == 5 && invokers[j])
2361                 fprintf (rl_outstream, "...\n");
2362
2363               for (j = 0; invokers[j]; j++)
2364                 xfree (invokers[j]);
2365
2366               xfree (invokers);
2367             }
2368         }
2369     }
2370
2371   xfree (names);
2372 }
2373
2374 /* Print all of the current functions and their bindings to
2375    rl_outstream.  If an explicit argument is given, then print
2376    the output in such a way that it can be read back in. */
2377 int
2378 rl_dump_functions (count, key)
2379      int count, key;
2380 {
2381   if (rl_dispatching)
2382     fprintf (rl_outstream, "\r\n");
2383   rl_function_dumper (rl_explicit_arg);
2384   rl_on_new_line ();
2385   return (0);
2386 }
2387
2388 static void
2389 _rl_macro_dumper_internal (print_readably, map, prefix)
2390      int print_readably;
2391      Keymap map;
2392      char *prefix;
2393 {
2394   register int key;
2395   char *keyname, *out;
2396   int prefix_len;
2397
2398   for (key = 0; key < KEYMAP_SIZE; key++)
2399     {
2400       switch (map[key].type)
2401         {
2402         case ISMACR:
2403           keyname = _rl_get_keyname (key);
2404           out = _rl_untranslate_macro_value ((char *)map[key].function, 0);
2405
2406           if (print_readably)
2407             fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
2408                                                          keyname,
2409                                                          out ? out : "");
2410           else
2411             fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
2412                                                         keyname,
2413                                                         out ? out : "");
2414           xfree (keyname);
2415           xfree (out);
2416           break;
2417         case ISFUNC:
2418           break;
2419         case ISKMAP:
2420           prefix_len = prefix ? strlen (prefix) : 0;
2421           if (key == ESC)
2422             {
2423               keyname = (char *)xmalloc (3 + prefix_len);
2424               if (prefix)
2425                 strcpy (keyname, prefix);
2426               keyname[prefix_len] = '\\';
2427               keyname[prefix_len + 1] = 'e';
2428               keyname[prefix_len + 2] = '\0';
2429             }
2430           else
2431             {
2432               keyname = _rl_get_keyname (key);
2433               if (prefix)
2434                 {
2435                   out = (char *)xmalloc (strlen (keyname) + prefix_len + 1);
2436                   strcpy (out, prefix);
2437                   strcpy (out + prefix_len, keyname);
2438                   xfree (keyname);
2439                   keyname = out;
2440                 }
2441             }
2442
2443           _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
2444           xfree (keyname);
2445           break;
2446         }
2447     }
2448 }
2449
2450 void
2451 rl_macro_dumper (print_readably)
2452      int print_readably;
2453 {
2454   _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
2455 }
2456
2457 int
2458 rl_dump_macros (count, key)
2459      int count, key;
2460 {
2461   if (rl_dispatching)
2462     fprintf (rl_outstream, "\r\n");
2463   rl_macro_dumper (rl_explicit_arg);
2464   rl_on_new_line ();
2465   return (0);
2466 }
2467
2468 static char *
2469 _rl_get_string_variable_value (name)
2470      const char *name;
2471 {
2472   static char numbuf[32];
2473   char *ret;
2474
2475   if (_rl_stricmp (name, "bell-style") == 0)
2476     {
2477       switch (_rl_bell_preference)
2478         {
2479           case NO_BELL:
2480             return "none";
2481           case VISIBLE_BELL:
2482             return "visible";
2483           case AUDIBLE_BELL:
2484           default:
2485             return "audible";
2486         }
2487     }
2488   else if (_rl_stricmp (name, "comment-begin") == 0)
2489     return (_rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2490   else if (_rl_stricmp (name, "completion-display-width") == 0)
2491     {
2492       sprintf (numbuf, "%d", _rl_completion_columns);
2493       return (numbuf);
2494     }
2495   else if (_rl_stricmp (name, "completion-prefix-display-length") == 0)
2496     {
2497       sprintf (numbuf, "%d", _rl_completion_prefix_display_length);
2498       return (numbuf);
2499     }
2500   else if (_rl_stricmp (name, "completion-query-items") == 0)
2501     {
2502       sprintf (numbuf, "%d", rl_completion_query_items);
2503       return (numbuf);
2504     }
2505   else if (_rl_stricmp (name, "editing-mode") == 0)
2506     return (rl_get_keymap_name_from_edit_mode ());
2507   else if (_rl_stricmp (name, "history-size") == 0)
2508     {
2509       sprintf (numbuf, "%d", history_is_stifled() ? history_max_entries : 0);
2510       return (numbuf);
2511     }
2512   else if (_rl_stricmp (name, "isearch-terminators") == 0)
2513     {
2514       if (_rl_isearch_terminators == 0)
2515         return 0;
2516       ret = _rl_untranslate_macro_value (_rl_isearch_terminators, 0);
2517       if (ret)
2518         {
2519           strncpy (numbuf, ret, sizeof (numbuf) - 1);
2520           xfree (ret);
2521           numbuf[sizeof(numbuf) - 1] = '\0';
2522         }
2523       else
2524         numbuf[0] = '\0';
2525       return numbuf;
2526     }
2527   else if (_rl_stricmp (name, "keymap") == 0)
2528     {
2529       ret = rl_get_keymap_name (_rl_keymap);
2530       if (ret == 0)
2531         ret = rl_get_keymap_name_from_edit_mode ();
2532       return (ret ? ret : "none");
2533     }
2534   else if (_rl_stricmp (name, "keyseq-timeout") == 0)
2535     {
2536       sprintf (numbuf, "%d", _rl_keyseq_timeout);    
2537       return (numbuf);
2538     }
2539   else if (_rl_stricmp (name, "emacs-mode-string") == 0)
2540     return (_rl_emacs_mode_str ? _rl_emacs_mode_str : RL_EMACS_MODESTR_DEFAULT);
2541   else if (_rl_stricmp (name, "vi-cmd-mode-string") == 0)
2542     return (_rl_emacs_mode_str ? _rl_emacs_mode_str : RL_VI_CMD_MODESTR_DEFAULT);
2543   else if (_rl_stricmp (name, "vi-ins-mode-string") == 0)
2544     return (_rl_emacs_mode_str ? _rl_emacs_mode_str : RL_VI_INS_MODESTR_DEFAULT);
2545   else
2546     return (0);
2547 }
2548
2549 void
2550 rl_variable_dumper (print_readably)
2551      int print_readably;
2552 {
2553   int i;
2554   char *v;
2555
2556   for (i = 0; boolean_varlist[i].name; i++)
2557     {
2558       if (print_readably)
2559         fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
2560                                *boolean_varlist[i].value ? "on" : "off");
2561       else
2562         fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
2563                                *boolean_varlist[i].value ? "on" : "off");
2564     }
2565
2566   for (i = 0; string_varlist[i].name; i++)
2567     {
2568       v = _rl_get_string_variable_value (string_varlist[i].name);
2569       if (v == 0)       /* _rl_isearch_terminators can be NULL */
2570         continue;
2571       if (print_readably)
2572         fprintf (rl_outstream, "set %s %s\n", string_varlist[i].name, v);
2573       else
2574         fprintf (rl_outstream, "%s is set to `%s'\n", string_varlist[i].name, v);
2575     }
2576 }
2577
2578 /* Print all of the current variables and their values to
2579    rl_outstream.  If an explicit argument is given, then print
2580    the output in such a way that it can be read back in. */
2581 int
2582 rl_dump_variables (count, key)
2583      int count, key;
2584 {
2585   if (rl_dispatching)
2586     fprintf (rl_outstream, "\r\n");
2587   rl_variable_dumper (rl_explicit_arg);
2588   rl_on_new_line ();
2589   return (0);
2590 }
2591
2592 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2593 static int
2594 substring_member_of_array (string, array)
2595      const char *string;
2596      const char * const *array;
2597 {
2598   while (*array)
2599     {
2600       if (_rl_strindex (string, *array))
2601         return (1);
2602       array++;
2603     }
2604   return (0);
2605 }