300530a0781fa1a2a5b1ca61b2f8c9b03a839794
[platform/upstream/bash.git] / lib / readline / bind.c
1 /* bind.c -- key binding and startup file support for the readline library. */
2
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
8    The GNU Readline Library is free software; you can redistribute it
9    and/or modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 1, or
11    (at your option) any later version.
12
13    The GNU Readline Library is distributed in the hope that it will be
14    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    The GNU General Public License is often shipped with GNU software, and
19    is generally kept in a file called COPYING or LICENSE.  If you do not
20    have a copy of the license, write to the Free Software Foundation,
21    675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #if defined (HAVE_SYS_FILE_H)
32 #  include <sys/file.h>
33 #endif /* HAVE_SYS_FILE_H */
34
35 #if defined (HAVE_UNISTD_H)
36 #  include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38
39 #if defined (HAVE_STDLIB_H)
40 #  include <stdlib.h>
41 #else
42 #  include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
44
45 #include <signal.h>
46 #include <errno.h>
47
48 #if !defined (errno)
49 extern int errno;
50 #endif /* !errno */
51
52 #include "posixstat.h"
53
54 /* System-specific feature definitions and include files. */
55 #include "rldefs.h"
56
57 /* Some standard library routines. */
58 #include "readline.h"
59 #include "history.h"
60
61 #if !defined (strchr) && !defined (__STDC__)
62 extern char *strchr (), *strrchr ();
63 #endif /* !strchr && !__STDC__ */
64
65 extern int _rl_horizontal_scroll_mode;
66 extern int _rl_mark_modified_lines;
67 extern int _rl_bell_preference;
68 extern int _rl_meta_flag;
69 extern int _rl_convert_meta_chars_to_ascii;
70 extern int _rl_output_meta_chars;
71 extern int _rl_complete_show_all;
72 extern int _rl_complete_mark_directories;
73 extern int _rl_print_completions_horizontally;
74 extern int _rl_completion_case_fold;
75 extern int _rl_enable_keypad;
76 #if defined (PAREN_MATCHING)
77 extern int rl_blink_matching_paren;
78 #endif /* PAREN_MATCHING */
79 #if defined (VISIBLE_STATS)
80 extern int rl_visible_stats;
81 #endif /* VISIBLE_STATS */
82 extern int rl_complete_with_tilde_expansion;
83 extern int rl_completion_query_items;
84 extern int rl_inhibit_completion;
85 extern char *_rl_comment_begin;
86
87 extern int rl_explicit_arg;
88 extern int rl_editing_mode;
89 extern unsigned char _rl_parsing_conditionalized_out;
90 extern Keymap _rl_keymap;
91
92 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
93
94 /* Functions imported from funmap.c */
95 extern char **rl_funmap_names ();
96 extern int rl_add_funmap_entry ();
97
98 /* Functions imported from util.c */
99 extern char *_rl_strindex ();
100
101 /* Functions imported from shell.c */
102 extern char *get_env_value ();
103
104 /* Variables exported by this file. */
105 Keymap rl_binding_keymap;
106
107 /* Forward declarations */
108 void rl_set_keymap_from_edit_mode ();
109
110 static int _rl_read_init_file ();
111 static int glean_key_from_name ();
112 static int substring_member_of_array ();
113
114 extern char *xmalloc (), *xrealloc ();
115
116 /* **************************************************************** */
117 /*                                                                  */
118 /*                      Binding keys                                */
119 /*                                                                  */
120 /* **************************************************************** */
121
122 /* rl_add_defun (char *name, Function *function, int key)
123    Add NAME to the list of named functions.  Make FUNCTION be the function
124    that gets called.  If KEY is not -1, then bind it. */
125 int
126 rl_add_defun (name, function, key)
127      char *name;
128      Function *function;
129      int key;
130 {
131   if (key != -1)
132     rl_bind_key (key, function);
133   rl_add_funmap_entry (name, function);
134   return 0;
135 }
136
137 /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
138 int
139 rl_bind_key (key, function)
140      int key;
141      Function *function;
142 {
143   if (key < 0)
144     return (key);
145
146   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
147     {
148       if (_rl_keymap[ESC].type == ISKMAP)
149         {
150           Keymap escmap;
151
152           escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
153           key = UNMETA (key);
154           escmap[key].type = ISFUNC;
155           escmap[key].function = function;
156           return (0);
157         }
158       return (key);
159     }
160
161   _rl_keymap[key].type = ISFUNC;
162   _rl_keymap[key].function = function;
163   rl_binding_keymap = _rl_keymap;
164   return (0);
165 }
166
167 /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
168    KEY. */
169 int
170 rl_bind_key_in_map (key, function, map)
171      int key;
172      Function *function;
173      Keymap map;
174 {
175   int result;
176   Keymap oldmap;
177
178   oldmap = _rl_keymap;
179   _rl_keymap = map;
180   result = rl_bind_key (key, function);
181   _rl_keymap = oldmap;
182   return (result);
183 }
184
185 /* Make KEY do nothing in the currently selected keymap.
186    Returns non-zero in case of error. */
187 int
188 rl_unbind_key (key)
189      int key;
190 {
191   return (rl_bind_key (key, (Function *)NULL));
192 }
193
194 /* Make KEY do nothing in MAP.
195    Returns non-zero in case of error. */
196 int
197 rl_unbind_key_in_map (key, map)
198      int key;
199      Keymap map;
200 {
201   return (rl_bind_key_in_map (key, (Function *)NULL, map));
202 }
203
204 /* Unbind all keys bound to FUNCTION in MAP. */
205 int
206 rl_unbind_function_in_map (func, map)
207      Function *func;
208      Keymap map;
209 {
210   register int i;
211
212   for (i = 0; i < KEYMAP_SIZE; i++)
213     {
214       if (map[i].type == ISFUNC && map[i].function == func)
215         map[i].function = (Function *)NULL;
216     }
217 }
218
219 int
220 rl_unbind_command_in_map (command, map)
221      char *command;
222      Keymap map;
223 {
224   Function *func;
225   register int i;
226
227   func = rl_named_function (command);
228   if (func == 0)
229     return 0;
230   return (rl_unbind_function_in_map (func, map));
231 }
232
233 /* Bind the key sequence represented by the string KEYSEQ to
234    FUNCTION.  This makes new keymaps as necessary.  The initial
235    place to do bindings is in MAP. */
236 int
237 rl_set_key (keyseq, function, map)
238      char *keyseq;
239      Function *function;
240      Keymap map;
241 {
242   return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
243 }
244
245 /* Bind the key sequence represented by the string KEYSEQ to
246    the string of characters MACRO.  This makes new keymaps as
247    necessary.  The initial place to do bindings is in MAP. */
248 int
249 rl_macro_bind (keyseq, macro, map)
250      char *keyseq, *macro;
251      Keymap map;
252 {
253   char *macro_keys;
254   int macro_keys_len;
255
256   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
257
258   if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
259     {
260       free (macro_keys);
261       return -1;
262     }
263   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
264   return 0;
265 }
266
267 /* Bind the key sequence represented by the string KEYSEQ to
268    the arbitrary pointer DATA.  TYPE says what kind of data is
269    pointed to by DATA, right now this can be a function (ISFUNC),
270    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
271    as necessary.  The initial place to do bindings is in MAP. */
272 int
273 rl_generic_bind (type, keyseq, data, map)
274      int type;
275      char *keyseq, *data;
276      Keymap map;
277 {
278   char *keys;
279   int keys_len;
280   register int i;
281
282   /* If no keys to bind to, exit right away. */
283   if (!keyseq || !*keyseq)
284     {
285       if (type == ISMACR)
286         free (data);
287       return -1;
288     }
289
290   keys = xmalloc (1 + (2 * strlen (keyseq)));
291
292   /* Translate the ASCII representation of KEYSEQ into an array of
293      characters.  Stuff the characters into KEYS, and the length of
294      KEYS into KEYS_LEN. */
295   if (rl_translate_keyseq (keyseq, keys, &keys_len))
296     {
297       free (keys);
298       return -1;
299     }
300
301   /* Bind keys, making new keymaps as necessary. */
302   for (i = 0; i < keys_len; i++)
303     {
304       int ic = (int) ((unsigned char)keys[i]);
305
306       if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
307         {
308           ic = UNMETA (ic);
309           if (map[ESC].type == ISKMAP)
310             map = FUNCTION_TO_KEYMAP (map, ESC);
311         }
312
313       if ((i + 1) < keys_len)
314         {
315           if (map[ic].type != ISKMAP)
316             {
317               if (map[ic].type == ISMACR)
318                 free ((char *)map[ic].function);
319
320               map[ic].type = ISKMAP;
321               map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
322             }
323           map = FUNCTION_TO_KEYMAP (map, ic);
324         }
325       else
326         {
327           if (map[ic].type == ISMACR)
328             free ((char *)map[ic].function);
329
330           map[ic].function = KEYMAP_TO_FUNCTION (data);
331           map[ic].type = type;
332         }
333
334       rl_binding_keymap = map;
335     }
336   free (keys);
337   return 0;
338 }
339
340 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
341    an array of characters.  LEN gets the final length of ARRAY.  Return
342    non-zero if there was an error parsing SEQ. */
343 int
344 rl_translate_keyseq (seq, array, len)
345      char *seq, *array;
346      int *len;
347 {
348   register int i, c, l, temp;
349
350   for (i = l = 0; c = seq[i]; i++)
351     {
352       if (c == '\\')
353         {
354           c = seq[++i];
355
356           if (c == 0)
357             break;
358
359           /* Handle \C- and \M- prefixes. */
360           if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
361             {
362               /* Handle special case of backwards define. */
363               if (strncmp (&seq[i], "C-\\M-", 5) == 0)
364                 {
365                   array[l++] = ESC;
366                   i += 5;
367                   array[l++] = CTRL (_rl_to_upper (seq[i]));
368                   if (seq[i] == '\0')
369                     i--;
370                 }
371               else if (c == 'M')
372                 {
373                   i++;
374                   array[l++] = ESC;     /* XXX */
375                 }
376               else if (c == 'C')
377                 {
378                   i += 2;
379                   /* Special hack for C-?... */
380                   array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
381                 }
382               continue;
383             }         
384
385           /* Translate other backslash-escaped characters.  These are the
386              same escape sequences that bash's `echo' and `printf' builtins
387              handle, with the addition of \d -> RUBOUT.  A backslash
388              preceding a character that is not special is stripped. */
389           switch (c)
390             {
391             case 'a':
392               array[l++] = '\007';
393               break;
394             case 'b':
395               array[l++] = '\b';
396               break;
397             case 'd':
398               array[l++] = RUBOUT;      /* readline-specific */
399               break;
400             case 'e':
401               array[l++] = ESC;
402               break;
403             case 'f':
404               array[l++] = '\f';
405               break;
406             case 'n':
407               array[l++] = NEWLINE;
408               break;
409             case 'r':
410               array[l++] = RETURN;
411               break;
412             case 't':
413               array[l++] = TAB;
414               break;
415             case 'v':
416               array[l++] = 0x0B;
417               break;
418             case '\\':
419               array[l++] = '\\';
420               break;
421             case '0': case '1': case '2': case '3':
422             case '4': case '5': case '6': case '7':
423               i++;
424               for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
425                 c = (c * 8) + OCTVALUE (seq[i]);
426               i--;      /* auto-increment in for loop */
427               array[l++] = c % (largest_char + 1);
428               break;
429             case 'x':
430               i++;
431               for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
432                 c = (c * 16) + HEXVALUE (seq[i]);
433               if (temp == 3)
434                 c = 'x';
435               i--;      /* auto-increment in for loop */
436               array[l++] = c % (largest_char + 1);
437               break;
438             default:    /* backslashes before non-special chars just add the char */
439               array[l++] = c;
440               break;    /* the backslash is stripped */
441             }
442           continue;
443         }
444
445       array[l++] = c;
446     }
447
448   *len = l;
449   array[l] = '\0';
450   return (0);
451 }
452
453 char *
454 rl_untranslate_keyseq (seq)
455      int seq;
456 {
457   static char kseq[16];
458   int i, c;
459
460   i = 0;
461   c = seq;
462   if (META_CHAR (c))
463     {
464       kseq[i++] = '\\';
465       kseq[i++] = 'M';
466       kseq[i++] = '-';
467       c = UNMETA (c);
468     }
469   else if (CTRL_CHAR (c))
470     {
471       kseq[i++] = '\\';
472       kseq[i++] = 'C';
473       kseq[i++] = '-';
474       c = _rl_to_lower (UNCTRL (c));
475     }
476   else if (c == RUBOUT)
477     {
478       kseq[i++] = '\\';
479       kseq[i++] = 'C';
480       kseq[i++] = '-';
481       c = '?';
482     }
483
484   if (c == ESC)
485     {
486       kseq[i++] = '\\';
487       c = 'e';
488     }
489   else if (c == '\\' || c == '"')
490     {
491       kseq[i++] = '\\';
492     }
493
494   kseq[i++] = (unsigned char) c;
495   kseq[i] = '\0';
496   return kseq;
497 }
498
499 static char *
500 _rl_untranslate_macro_value (seq)
501      char *seq;
502 {
503   char *ret, *r, *s;
504   int c;
505
506   r = ret = xmalloc (7 * strlen (seq) + 1);
507   for (s = seq; *s; s++)
508     {
509       c = *s;
510       if (META_CHAR (c))
511         {
512           *r++ = '\\';
513           *r++ = 'M';
514           *r++ = '-';
515           c = UNMETA (c);
516         }
517       else if (CTRL_CHAR (c) && c != ESC)
518         {
519           *r++ = '\\';
520           *r++ = 'C';
521           *r++ = '-';
522           c = _rl_to_lower (UNCTRL (c));
523         }
524       else if (c == RUBOUT)
525         {
526           *r++ = '\\';
527           *r++ = 'C';
528           *r++ = '-';
529           c = '?';
530         }
531
532       if (c == ESC)
533         {
534           *r++ = '\\';
535           c = 'e';
536         }
537       else if (c == '\\' || c == '"')
538         *r++ = '\\';
539
540       *r++ = (unsigned char)c;
541     }
542   *r = '\0';
543   return ret;
544 }
545
546 /* Return a pointer to the function that STRING represents.
547    If STRING doesn't have a matching function, then a NULL pointer
548    is returned. */
549 Function *
550 rl_named_function (string)
551      char *string;
552 {
553   register int i;
554
555   rl_initialize_funmap ();
556
557   for (i = 0; funmap[i]; i++)
558     if (_rl_stricmp (funmap[i]->name, string) == 0)
559       return (funmap[i]->function);
560   return ((Function *)NULL);
561 }
562
563 /* Return the function (or macro) definition which would be invoked via
564    KEYSEQ if executed in MAP.  If MAP is NULL, then the current keymap is
565    used.  TYPE, if non-NULL, is a pointer to an int which will receive the
566    type of the object pointed to.  One of ISFUNC (function), ISKMAP (keymap),
567    or ISMACR (macro). */
568 Function *
569 rl_function_of_keyseq (keyseq, map, type)
570      char *keyseq;
571      Keymap map;
572      int *type;
573 {
574   register int i;
575
576   if (!map)
577     map = _rl_keymap;
578
579   for (i = 0; keyseq && keyseq[i]; i++)
580     {
581       int ic = keyseq[i];
582
583       if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
584         {
585           if (map[ESC].type != ISKMAP)
586             {
587               if (type)
588                 *type = map[ESC].type;
589
590               return (map[ESC].function);
591             }
592           else
593             {
594               map = FUNCTION_TO_KEYMAP (map, ESC);
595               ic = UNMETA (ic);
596             }
597         }
598
599       if (map[ic].type == ISKMAP)
600         {
601           /* If this is the last key in the key sequence, return the
602              map. */
603           if (!keyseq[i + 1])
604             {
605               if (type)
606                 *type = ISKMAP;
607
608               return (map[ic].function);
609             }
610           else
611             map = FUNCTION_TO_KEYMAP (map, ic);
612         }
613       else
614         {
615           if (type)
616             *type = map[ic].type;
617
618           return (map[ic].function);
619         }
620     }
621   return ((Function *) NULL);
622 }
623
624 /* The last key bindings file read. */
625 static char *last_readline_init_file = (char *)NULL;
626
627 /* The file we're currently reading key bindings from. */
628 static char *current_readline_init_file;
629 static int current_readline_init_include_level;
630 static int current_readline_init_lineno;
631
632 /* Read FILENAME into a locally-allocated buffer and return the buffer.
633    The size of the buffer is returned in *SIZEP.  Returns NULL if any
634    errors were encountered. */
635 static char *
636 _rl_read_file (filename, sizep)
637      char *filename;
638      size_t *sizep;
639 {
640   struct stat finfo;
641   size_t file_size;
642   char *buffer;
643   int i, file;
644
645   if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
646     return ((char *)NULL);
647
648   file_size = (size_t)finfo.st_size;
649
650   /* check for overflow on very large files */
651   if (file_size != finfo.st_size || file_size + 1 < file_size)
652     {
653       if (file >= 0)
654         close (file);
655 #if defined (EFBIG)
656       errno = EFBIG;
657 #endif
658       return ((char *)NULL);
659     }
660
661   /* Read the file into BUFFER. */
662   buffer = (char *)xmalloc (file_size + 1);
663   i = read (file, buffer, file_size);
664   close (file);
665
666   if (i < file_size)
667     {
668       free (buffer);
669       return ((char *)NULL);
670     }
671
672   buffer[file_size] = '\0';
673   if (sizep)
674     *sizep = file_size;
675   return (buffer);
676 }
677
678 /* Re-read the current keybindings file. */
679 int
680 rl_re_read_init_file (count, ignore)
681      int count, ignore;
682 {
683   int r;
684   r = rl_read_init_file ((char *)NULL);
685   rl_set_keymap_from_edit_mode ();
686   return r;
687 }
688
689 /* Do key bindings from a file.  If FILENAME is NULL it defaults
690    to the first non-null filename from this list:
691      1. the filename used for the previous call
692      2. the value of the shell variable `INPUTRC'
693      3. ~/.inputrc
694    If the file existed and could be opened and read, 0 is returned,
695    otherwise errno is returned. */
696 int
697 rl_read_init_file (filename)
698      char *filename;
699 {
700   /* Default the filename. */
701   if (filename == 0)
702     {
703       filename = last_readline_init_file;
704       if (filename == 0)
705         filename = get_env_value ("INPUTRC");
706       if (filename == 0)
707         filename = DEFAULT_INPUTRC;
708     }
709
710   if (*filename == 0)
711     filename = DEFAULT_INPUTRC;
712
713   return (_rl_read_init_file (filename, 0));
714 }
715
716 static int
717 _rl_read_init_file (filename, include_level)
718      char *filename;
719      int include_level;
720 {
721   register int i;
722   char *buffer, *openname, *line, *end;
723   size_t file_size;
724
725   current_readline_init_file = filename;
726   current_readline_init_include_level = include_level;
727
728   openname = tilde_expand (filename);
729   buffer = _rl_read_file (openname, &file_size);
730   if (buffer == 0)
731     return (errno);
732   
733   if (include_level == 0 && filename != last_readline_init_file)
734     {
735       FREE (last_readline_init_file);
736       last_readline_init_file = savestring (filename);
737     }
738
739   /* Loop over the lines in the file.  Lines that start with `#' are
740      comments; all other lines are commands for readline initialization. */
741   current_readline_init_lineno = 1;
742   line = buffer;
743   end = buffer + file_size;
744   while (line < end)
745     {
746       /* Find the end of this line. */
747       for (i = 0; line + i != end && line[i] != '\n'; i++);
748
749       /* Mark end of line. */
750       line[i] = '\0';
751
752       /* Skip leading whitespace. */
753       while (*line && whitespace (*line))
754         {
755           line++;
756           i--;
757         }
758
759       /* If the line is not a comment, then parse it. */
760       if (*line && *line != '#')
761         rl_parse_and_bind (line);
762
763       /* Move to the next line. */
764       line += i + 1;
765       current_readline_init_lineno++;
766     }
767
768   free (buffer);
769   return (0);
770 }
771
772 static void
773 _rl_init_file_error (msg)
774      char *msg;
775 {
776   fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
777                    current_readline_init_lineno,
778                    msg);
779 }
780
781 /* **************************************************************** */
782 /*                                                                  */
783 /*                      Parser Directives                           */
784 /*                                                                  */
785 /* **************************************************************** */
786
787 /* Conditionals. */
788
789 /* Calling programs set this to have their argv[0]. */
790 char *rl_readline_name = "other";
791
792 /* Stack of previous values of parsing_conditionalized_out. */
793 static unsigned char *if_stack = (unsigned char *)NULL;
794 static int if_stack_depth;
795 static int if_stack_size;
796
797 /* Push _rl_parsing_conditionalized_out, and set parser state based
798    on ARGS. */
799 static int
800 parser_if (args)
801      char *args;
802 {
803   register int i;
804
805   /* Push parser state. */
806   if (if_stack_depth + 1 >= if_stack_size)
807     {
808       if (!if_stack)
809         if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
810       else
811         if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
812     }
813   if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
814
815   /* If parsing is turned off, then nothing can turn it back on except
816      for finding the matching endif.  In that case, return right now. */
817   if (_rl_parsing_conditionalized_out)
818     return 0;
819
820   /* Isolate first argument. */
821   for (i = 0; args[i] && !whitespace (args[i]); i++);
822
823   if (args[i])
824     args[i++] = '\0';
825
826   /* Handle "$if term=foo" and "$if mode=emacs" constructs.  If this
827      isn't term=foo, or mode=emacs, then check to see if the first
828      word in ARGS is the same as the value stored in rl_readline_name. */
829   if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
830     {
831       char *tem, *tname;
832
833       /* Terminals like "aaa-60" are equivalent to "aaa". */
834       tname = savestring (rl_terminal_name);
835       tem = strchr (tname, '-');
836       if (tem)
837         *tem = '\0';
838
839       /* Test the `long' and `short' forms of the terminal name so that
840          if someone has a `sun-cmd' and does not want to have bindings
841          that will be executed if the terminal is a `sun', they can put
842          `$if term=sun-cmd' into their .inputrc. */
843       _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
844                                         _rl_stricmp (args + 5, rl_terminal_name);
845       free (tname);
846     }
847 #if defined (VI_MODE)
848   else if (_rl_strnicmp (args, "mode=", 5) == 0)
849     {
850       int mode;
851
852       if (_rl_stricmp (args + 5, "emacs") == 0)
853         mode = emacs_mode;
854       else if (_rl_stricmp (args + 5, "vi") == 0)
855         mode = vi_mode;
856       else
857         mode = no_mode;
858
859       _rl_parsing_conditionalized_out = mode != rl_editing_mode;
860     }
861 #endif /* VI_MODE */
862   /* Check to see if the first word in ARGS is the same as the
863      value stored in rl_readline_name. */
864   else if (_rl_stricmp (args, rl_readline_name) == 0)
865     _rl_parsing_conditionalized_out = 0;
866   else
867     _rl_parsing_conditionalized_out = 1;
868   return 0;
869 }
870
871 /* Invert the current parser state if there is anything on the stack. */
872 static int
873 parser_else (args)
874      char *args;
875 {
876   register int i;
877
878   if (if_stack_depth == 0)
879     {
880       _rl_init_file_error ("$else found without matching $if");
881       return 0;
882     }
883
884   /* Check the previous (n - 1) levels of the stack to make sure that
885      we haven't previously turned off parsing. */
886   for (i = 0; i < if_stack_depth - 1; i++)
887     if (if_stack[i] == 1)
888       return 0;
889
890   /* Invert the state of parsing if at top level. */
891   _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
892   return 0;
893 }
894
895 /* Terminate a conditional, popping the value of
896    _rl_parsing_conditionalized_out from the stack. */
897 static int
898 parser_endif (args)
899      char *args;
900 {
901   if (if_stack_depth)
902     _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
903   else
904     _rl_init_file_error ("$endif without matching $if");
905   return 0;
906 }
907
908 static int
909 parser_include (args)
910      char *args;
911 {
912   char *old_init_file, *e;
913   int old_line_number, old_include_level, r;
914
915   if (_rl_parsing_conditionalized_out)
916     return (0);
917
918   old_init_file = current_readline_init_file;
919   old_line_number = current_readline_init_lineno;
920   old_include_level = current_readline_init_include_level;
921
922   e = strchr (args, '\n');
923   if (e)
924     *e = '\0';
925   r = _rl_read_init_file (args, old_include_level + 1);
926
927   current_readline_init_file = old_init_file;
928   current_readline_init_lineno = old_line_number;
929   current_readline_init_include_level = old_include_level;
930
931   return r;
932 }
933   
934 /* Associate textual names with actual functions. */
935 static struct {
936   char *name;
937   Function *function;
938 } parser_directives [] = {
939   { "if", parser_if },
940   { "endif", parser_endif },
941   { "else", parser_else },
942   { "include", parser_include },
943   { (char *)0x0, (Function *)0x0 }
944 };
945
946 /* Handle a parser directive.  STATEMENT is the line of the directive
947    without any leading `$'. */
948 static int
949 handle_parser_directive (statement)
950      char *statement;
951 {
952   register int i;
953   char *directive, *args;
954
955   /* Isolate the actual directive. */
956
957   /* Skip whitespace. */
958   for (i = 0; whitespace (statement[i]); i++);
959
960   directive = &statement[i];
961
962   for (; statement[i] && !whitespace (statement[i]); i++);
963
964   if (statement[i])
965     statement[i++] = '\0';
966
967   for (; statement[i] && whitespace (statement[i]); i++);
968
969   args = &statement[i];
970
971   /* Lookup the command, and act on it. */
972   for (i = 0; parser_directives[i].name; i++)
973     if (_rl_stricmp (directive, parser_directives[i].name) == 0)
974       {
975         (*parser_directives[i].function) (args);
976         return (0);
977       }
978
979   /* display an error message about the unknown parser directive */
980   _rl_init_file_error ("unknown parser directive");
981   return (1);
982 }
983
984 /* Read the binding command from STRING and perform it.
985    A key binding command looks like: Keyname: function-name\0,
986    a variable binding command looks like: set variable value.
987    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
988 int
989 rl_parse_and_bind (string)
990      char *string;
991 {
992   char *funname, *kname;
993   register int c, i;
994   int key, equivalency;
995
996   while (string && whitespace (*string))
997     string++;
998
999   if (!string || !*string || *string == '#')
1000     return 0;
1001
1002   /* If this is a parser directive, act on it. */
1003   if (*string == '$')
1004     {
1005       handle_parser_directive (&string[1]);
1006       return 0;
1007     }
1008
1009   /* If we aren't supposed to be parsing right now, then we're done. */
1010   if (_rl_parsing_conditionalized_out)
1011     return 0;
1012
1013   i = 0;
1014   /* If this keyname is a complex key expression surrounded by quotes,
1015      advance to after the matching close quote.  This code allows the
1016      backslash to quote characters in the key expression. */
1017   if (*string == '"')
1018     {
1019       int passc = 0;
1020
1021       for (i = 1; c = string[i]; i++)
1022         {
1023           if (passc)
1024             {
1025               passc = 0;
1026               continue;
1027             }
1028
1029           if (c == '\\')
1030             {
1031               passc++;
1032               continue;
1033             }
1034
1035           if (c == '"')
1036             break;
1037         }
1038       /* If we didn't find a closing quote, abort the line. */
1039       if (string[i] == '\0')
1040         {
1041           _rl_init_file_error ("no closing `\"' in key binding");
1042           return 1;
1043         }
1044     }
1045
1046   /* Advance to the colon (:) or whitespace which separates the two objects. */
1047   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1048
1049   equivalency = (c == ':' && string[i + 1] == '=');
1050
1051   /* Mark the end of the command (or keyname). */
1052   if (string[i])
1053     string[i++] = '\0';
1054
1055   /* If doing assignment, skip the '=' sign as well. */
1056   if (equivalency)
1057     string[i++] = '\0';
1058
1059   /* If this is a command to set a variable, then do that. */
1060   if (_rl_stricmp (string, "set") == 0)
1061     {
1062       char *var = string + i;
1063       char *value;
1064
1065       /* Make VAR point to start of variable name. */
1066       while (*var && whitespace (*var)) var++;
1067
1068       /* Make value point to start of value string. */
1069       value = var;
1070       while (*value && !whitespace (*value)) value++;
1071       if (*value)
1072         *value++ = '\0';
1073       while (*value && whitespace (*value)) value++;
1074
1075       rl_variable_bind (var, value);
1076       return 0;
1077     }
1078
1079   /* Skip any whitespace between keyname and funname. */
1080   for (; string[i] && whitespace (string[i]); i++);
1081   funname = &string[i];
1082
1083   /* Now isolate funname.
1084      For straight function names just look for whitespace, since
1085      that will signify the end of the string.  But this could be a
1086      macro definition.  In that case, the string is quoted, so skip
1087      to the matching delimiter.  We allow the backslash to quote the
1088      delimiter characters in the macro body. */
1089   /* This code exists to allow whitespace in macro expansions, which
1090      would otherwise be gobbled up by the next `for' loop.*/
1091   /* XXX - it may be desirable to allow backslash quoting only if " is
1092      the quoted string delimiter, like the shell. */
1093   if (*funname == '\'' || *funname == '"')
1094     {
1095       int delimiter = string[i++], passc;
1096
1097       for (passc = 0; c = string[i]; i++)
1098         {
1099           if (passc)
1100             {
1101               passc = 0;
1102               continue;
1103             }
1104
1105           if (c == '\\')
1106             {
1107               passc = 1;
1108               continue;
1109             }
1110
1111           if (c == delimiter)
1112             break;
1113         }
1114       if (c)
1115         i++;
1116     }
1117
1118   /* Advance to the end of the string.  */
1119   for (; string[i] && !whitespace (string[i]); i++);
1120
1121   /* No extra whitespace at the end of the string. */
1122   string[i] = '\0';
1123
1124   /* Handle equivalency bindings here.  Make the left-hand side be exactly
1125      whatever the right-hand evaluates to, including keymaps. */
1126   if (equivalency)
1127     {
1128       return 0;
1129     }
1130
1131   /* If this is a new-style key-binding, then do the binding with
1132      rl_set_key ().  Otherwise, let the older code deal with it. */
1133   if (*string == '"')
1134     {
1135       char *seq;
1136       register int j, k, passc;
1137
1138       seq = xmalloc (1 + strlen (string));
1139       for (j = 1, k = passc = 0; string[j]; j++)
1140         {
1141           /* Allow backslash to quote characters, but leave them in place.
1142              This allows a string to end with a backslash quoting another
1143              backslash, or with a backslash quoting a double quote.  The
1144              backslashes are left in place for rl_translate_keyseq (). */
1145           if (passc || (string[j] == '\\'))
1146             {
1147               seq[k++] = string[j];
1148               passc = !passc;
1149               continue;
1150             }
1151
1152           if (string[j] == '"')
1153             break;
1154
1155           seq[k++] = string[j];
1156         }
1157       seq[k] = '\0';
1158
1159       /* Binding macro? */
1160       if (*funname == '\'' || *funname == '"')
1161         {
1162           j = strlen (funname);
1163
1164           /* Remove the delimiting quotes from each end of FUNNAME. */
1165           if (j && funname[j - 1] == *funname)
1166             funname[j - 1] = '\0';
1167
1168           rl_macro_bind (seq, &funname[1], _rl_keymap);
1169         }
1170       else
1171         rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1172
1173       free (seq);
1174       return 0;
1175     }
1176
1177   /* Get the actual character we want to deal with. */
1178   kname = strrchr (string, '-');
1179   if (!kname)
1180     kname = string;
1181   else
1182     kname++;
1183
1184   key = glean_key_from_name (kname);
1185
1186   /* Add in control and meta bits. */
1187   if (substring_member_of_array (string, possible_control_prefixes))
1188     key = CTRL (_rl_to_upper (key));
1189
1190   if (substring_member_of_array (string, possible_meta_prefixes))
1191     key = META (key);
1192
1193   /* Temporary.  Handle old-style keyname with macro-binding. */
1194   if (*funname == '\'' || *funname == '"')
1195     {
1196       unsigned char useq[2];
1197       int fl = strlen (funname);
1198
1199       useq[0] = key; useq[1] = '\0';
1200       if (fl && funname[fl - 1] == *funname)
1201         funname[fl - 1] = '\0';
1202
1203       rl_macro_bind (useq, &funname[1], _rl_keymap);
1204     }
1205 #if defined (PREFIX_META_HACK)
1206   /* Ugly, but working hack to keep prefix-meta around. */
1207   else if (_rl_stricmp (funname, "prefix-meta") == 0)
1208     {
1209       char seq[2];
1210
1211       seq[0] = key;
1212       seq[1] = '\0';
1213       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1214     }
1215 #endif /* PREFIX_META_HACK */
1216   else
1217     rl_bind_key (key, rl_named_function (funname));
1218   return 0;
1219 }
1220
1221 /* Simple structure for boolean readline variables (i.e., those that can
1222    have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1223    false. */
1224
1225 static struct {
1226   char *name;
1227   int *value;
1228 } boolean_varlist [] = {
1229 #if defined (PAREN_MATCHING)
1230   { "blink-matching-paren",     &rl_blink_matching_paren },
1231 #endif
1232   { "completion-ignore-case",   &_rl_completion_case_fold },
1233   { "convert-meta",             &_rl_convert_meta_chars_to_ascii },
1234   { "disable-completion",       &rl_inhibit_completion },
1235   { "enable-keypad",            &_rl_enable_keypad },
1236   { "expand-tilde",             &rl_complete_with_tilde_expansion },
1237   { "horizontal-scroll-mode",   &_rl_horizontal_scroll_mode },
1238   { "input-meta",               &_rl_meta_flag },
1239   { "mark-directories",         &_rl_complete_mark_directories },
1240   { "mark-modified-lines",      &_rl_mark_modified_lines },
1241   { "meta-flag",                &_rl_meta_flag },
1242   { "output-meta",              &_rl_output_meta_chars },
1243   { "print-completions-horizontally", &_rl_print_completions_horizontally },
1244   { "show-all-if-ambiguous",    &_rl_complete_show_all },
1245 #if defined (VISIBLE_STATS)
1246   { "visible-stats",            &rl_visible_stats },
1247 #endif /* VISIBLE_STATS */
1248   { (char *)NULL, (int *)NULL }
1249 };
1250
1251 int
1252 rl_variable_bind (name, value)
1253      char *name, *value;
1254 {
1255   register int i;
1256
1257   /* Check for simple variables first. */
1258   for (i = 0; boolean_varlist[i].name; i++)
1259     {
1260       if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1261         {
1262           /* A variable is TRUE if the "value" is "on", "1" or "". */
1263           *boolean_varlist[i].value = *value == 0 ||
1264                                       _rl_stricmp (value, "on") == 0 ||
1265                                       (value[0] == '1' && value[1] == '\0');
1266           return 0;
1267         }
1268     }
1269
1270   /* Not a boolean variable, so check for specials. */
1271
1272   /* Editing mode change? */
1273   if (_rl_stricmp (name, "editing-mode") == 0)
1274     {
1275       if (_rl_strnicmp (value, "vi", 2) == 0)
1276         {
1277 #if defined (VI_MODE)
1278           _rl_keymap = vi_insertion_keymap;
1279           rl_editing_mode = vi_mode;
1280 #endif /* VI_MODE */
1281         }
1282       else if (_rl_strnicmp (value, "emacs", 5) == 0)
1283         {
1284           _rl_keymap = emacs_standard_keymap;
1285           rl_editing_mode = emacs_mode;
1286         }
1287     }
1288
1289   /* Comment string change? */
1290   else if (_rl_stricmp (name, "comment-begin") == 0)
1291     {
1292       if (*value)
1293         {
1294           if (_rl_comment_begin)
1295             free (_rl_comment_begin);
1296
1297           _rl_comment_begin = savestring (value);
1298         }
1299     }
1300   else if (_rl_stricmp (name, "completion-query-items") == 0)
1301     {
1302       int nval = 100;
1303       if (*value)
1304         {
1305           nval = atoi (value);
1306           if (nval < 0)
1307             nval = 0;
1308         }
1309       rl_completion_query_items = nval;
1310     }
1311   else if (_rl_stricmp (name, "keymap") == 0)
1312     {
1313       Keymap kmap;
1314       kmap = rl_get_keymap_by_name (value);
1315       if (kmap)
1316         rl_set_keymap (kmap);
1317     }
1318   else if (_rl_stricmp (name, "bell-style") == 0)
1319     {
1320       if (!*value)
1321         _rl_bell_preference = AUDIBLE_BELL;
1322       else
1323         {
1324           if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1325             _rl_bell_preference = NO_BELL;
1326           else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1327             _rl_bell_preference = AUDIBLE_BELL;
1328           else if (_rl_stricmp (value, "visible") == 0)
1329             _rl_bell_preference = VISIBLE_BELL;
1330         }
1331     }
1332   else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1333     {
1334       /* Backwards compatibility. */
1335       if (*value && (_rl_stricmp (value, "on") == 0 ||
1336                      (*value == '1' && !value[1])))
1337         _rl_bell_preference = VISIBLE_BELL;
1338       else
1339         _rl_bell_preference = AUDIBLE_BELL;
1340     }
1341
1342   /* For the time being, unknown variable names are simply ignored. */
1343   return 0;
1344 }
1345
1346 /* Return the character which matches NAME.
1347    For example, `Space' returns ' '. */
1348
1349 typedef struct {
1350   char *name;
1351   int value;
1352 } assoc_list;
1353
1354 static assoc_list name_key_alist[] = {
1355   { "DEL", 0x7f },
1356   { "ESC", '\033' },
1357   { "Escape", '\033' },
1358   { "LFD", '\n' },
1359   { "Newline", '\n' },
1360   { "RET", '\r' },
1361   { "Return", '\r' },
1362   { "Rubout", 0x7f },
1363   { "SPC", ' ' },
1364   { "Space", ' ' },
1365   { "Tab", 0x09 },
1366   { (char *)0x0, 0 }
1367 };
1368
1369 static int
1370 glean_key_from_name (name)
1371      char *name;
1372 {
1373   register int i;
1374
1375   for (i = 0; name_key_alist[i].name; i++)
1376     if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1377       return (name_key_alist[i].value);
1378
1379   return (*(unsigned char *)name);      /* XXX was return (*name) */
1380 }
1381
1382 /* Auxiliary functions to manage keymaps. */
1383 static struct {
1384   char *name;
1385   Keymap map;
1386 } keymap_names[] = {
1387   { "emacs", emacs_standard_keymap },
1388   { "emacs-standard", emacs_standard_keymap },
1389   { "emacs-meta", emacs_meta_keymap },
1390   { "emacs-ctlx", emacs_ctlx_keymap },
1391 #if defined (VI_MODE)
1392   { "vi", vi_movement_keymap },
1393   { "vi-move", vi_movement_keymap },
1394   { "vi-command", vi_movement_keymap },
1395   { "vi-insert", vi_insertion_keymap },
1396 #endif /* VI_MODE */
1397   { (char *)0x0, (Keymap)0x0 }
1398 };
1399
1400 Keymap
1401 rl_get_keymap_by_name (name)
1402      char *name;
1403 {
1404   register int i;
1405
1406   for (i = 0; keymap_names[i].name; i++)
1407     if (strcmp (name, keymap_names[i].name) == 0)
1408       return (keymap_names[i].map);
1409   return ((Keymap) NULL);
1410 }
1411
1412 char *
1413 rl_get_keymap_name (map)
1414      Keymap map;
1415 {
1416   register int i;
1417   for (i = 0; keymap_names[i].name; i++)
1418     if (map == keymap_names[i].map)
1419       return (keymap_names[i].name);
1420   return ((char *)NULL);
1421 }
1422   
1423 void
1424 rl_set_keymap (map)
1425      Keymap map;
1426 {
1427   if (map)
1428     _rl_keymap = map;
1429 }
1430
1431 Keymap
1432 rl_get_keymap ()
1433 {
1434   return (_rl_keymap);
1435 }
1436
1437 void
1438 rl_set_keymap_from_edit_mode ()
1439 {
1440   if (rl_editing_mode == emacs_mode)
1441     _rl_keymap = emacs_standard_keymap;
1442 #if defined (VI_MODE)
1443   else if (rl_editing_mode == vi_mode)
1444     _rl_keymap = vi_insertion_keymap;
1445 #endif /* VI_MODE */
1446 }
1447
1448 char *
1449 rl_get_keymap_name_from_edit_mode ()
1450 {
1451   if (rl_editing_mode == emacs_mode)
1452     return "emacs";
1453 #if defined (VI_MODE)
1454   else if (rl_editing_mode == vi_mode)
1455     return "vi";
1456 #endif /* VI_MODE */
1457   else
1458     return "none";
1459 }
1460
1461 /* **************************************************************** */
1462 /*                                                                  */
1463 /*                Key Binding and Function Information              */
1464 /*                                                                  */
1465 /* **************************************************************** */
1466
1467 /* Each of the following functions produces information about the
1468    state of keybindings and functions known to Readline.  The info
1469    is always printed to rl_outstream, and in such a way that it can
1470    be read back in (i.e., passed to rl_parse_and_bind (). */
1471
1472 /* Print the names of functions known to Readline. */
1473 void
1474 rl_list_funmap_names ()
1475 {
1476   register int i;
1477   char **funmap_names;
1478
1479   funmap_names = rl_funmap_names ();
1480
1481   if (!funmap_names)
1482     return;
1483
1484   for (i = 0; funmap_names[i]; i++)
1485     fprintf (rl_outstream, "%s\n", funmap_names[i]);
1486
1487   free (funmap_names);
1488 }
1489
1490 static char *
1491 _rl_get_keyname (key)
1492      int key;
1493 {
1494   char *keyname;
1495   int i, c, v;
1496
1497   keyname = (char *)xmalloc (8);
1498
1499   c = key;
1500   /* Since this is going to be used to write out keysequence-function
1501      pairs for possible inclusion in an inputrc file, we don't want to
1502      do any special meta processing on KEY. */
1503
1504 #if 0
1505   /* We might want to do this, but the old version of the code did not. */
1506
1507   /* If this is an escape character, we don't want to do any more processing.
1508      Just add the special ESC key sequence and return. */
1509   if (c == ESC)
1510     {
1511       keyseq[0] = '\\';
1512       keyseq[1] = 'e';
1513       keyseq[2] = '\0';
1514       return keyseq;
1515     }
1516 #endif
1517
1518   /* RUBOUT is translated directly into \C-? */
1519   if (key == RUBOUT)
1520     {
1521       keyname[0] = '\\';
1522       keyname[1] = 'C';
1523       keyname[2] = '-';
1524       keyname[3] = '?';
1525       keyname[4] = '\0';
1526       return keyname;
1527     }
1528
1529   i = 0;
1530   /* Now add special prefixes needed for control characters.  This can
1531      potentially change C. */
1532   if (CTRL_CHAR (c))
1533     {
1534       keyname[i++] = '\\';
1535       keyname[i++] = 'C';
1536       keyname[i++] = '-';
1537       c = _rl_to_lower (UNCTRL (c));
1538     }
1539
1540   /* XXX experimental code.  Turn the characters that are not ASCII or
1541      ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1542      This changes C. */
1543   if (c >= 128 && c <= 159)
1544     {
1545       keyname[i++] = '\\';
1546       keyname[i++] = '2';
1547       c -= 128;
1548       keyname[i++] = (c / 8) + '0';
1549       c = (c % 8) + '0';
1550     }
1551
1552   /* Now, if the character needs to be quoted with a backslash, do that. */
1553   if (c == '\\' || c == '"')
1554     keyname[i++] = '\\';
1555
1556   /* Now add the key, terminate the string, and return it. */
1557   keyname[i++] = (char) c;
1558   keyname[i] = '\0';
1559
1560   return keyname;
1561 }
1562
1563 /* Return a NULL terminated array of strings which represent the key
1564    sequences that are used to invoke FUNCTION in MAP. */
1565 char **
1566 rl_invoking_keyseqs_in_map (function, map)
1567      Function *function;
1568      Keymap map;
1569 {
1570   register int key;
1571   char **result;
1572   int result_index, result_size;
1573
1574   result = (char **)NULL;
1575   result_index = result_size = 0;
1576
1577   for (key = 0; key < KEYMAP_SIZE; key++)
1578     {
1579       switch (map[key].type)
1580         {
1581         case ISMACR:
1582           /* Macros match, if, and only if, the pointers are identical.
1583              Thus, they are treated exactly like functions in here. */
1584         case ISFUNC:
1585           /* If the function in the keymap is the one we are looking for,
1586              then add the current KEY to the list of invoking keys. */
1587           if (map[key].function == function)
1588             {
1589               char *keyname;
1590
1591               keyname = _rl_get_keyname (key);
1592
1593               if (result_index + 2 > result_size)
1594                 {
1595                   result_size += 10;
1596                   result = (char **) xrealloc (result, result_size * sizeof (char *));
1597                 }
1598
1599               result[result_index++] = keyname;
1600               result[result_index] = (char *)NULL;
1601             }
1602           break;
1603
1604         case ISKMAP:
1605           {
1606             char **seqs;
1607             register int i;
1608
1609             /* Find the list of keyseqs in this map which have FUNCTION as
1610                their target.  Add the key sequences found to RESULT. */
1611             if (map[key].function)
1612               seqs =
1613                 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1614             else
1615               break;
1616
1617             if (seqs == 0)
1618               break;
1619
1620             for (i = 0; seqs[i]; i++)
1621               {
1622                 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1623
1624                 if (key == ESC)
1625                   sprintf (keyname, "\\e");
1626                 else if (CTRL_CHAR (key))
1627                   sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1628                 else if (key == RUBOUT)
1629                   sprintf (keyname, "\\C-?");
1630                 else if (key == '\\' || key == '"')
1631                   {
1632                     keyname[0] = '\\';
1633                     keyname[1] = (char) key;
1634                     keyname[2] = '\0';
1635                   }
1636                 else
1637                   {
1638                     keyname[0] = (char) key;
1639                     keyname[1] = '\0';
1640                   }
1641                 
1642                 strcat (keyname, seqs[i]);
1643                 free (seqs[i]);
1644
1645                 if (result_index + 2 > result_size)
1646                   {
1647                     result_size += 10;
1648                     result = (char **) xrealloc (result, result_size * sizeof (char *));
1649                   }
1650
1651                 result[result_index++] = keyname;
1652                 result[result_index] = (char *)NULL;
1653               }
1654
1655             free (seqs);
1656           }
1657           break;
1658         }
1659     }
1660   return (result);
1661 }
1662
1663 /* Return a NULL terminated array of strings which represent the key
1664    sequences that can be used to invoke FUNCTION using the current keymap. */
1665 char **
1666 rl_invoking_keyseqs (function)
1667      Function *function;
1668 {
1669   return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1670 }
1671
1672 /* Print all of the functions and their bindings to rl_outstream.  If
1673    PRINT_READABLY is non-zero, then print the output in such a way
1674    that it can be read back in. */
1675 void
1676 rl_function_dumper (print_readably)
1677      int print_readably;
1678 {
1679   register int i;
1680   char **names;
1681   char *name;
1682
1683   names = rl_funmap_names ();
1684
1685   fprintf (rl_outstream, "\n");
1686
1687   for (i = 0; name = names[i]; i++)
1688     {
1689       Function *function;
1690       char **invokers;
1691
1692       function = rl_named_function (name);
1693       invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1694
1695       if (print_readably)
1696         {
1697           if (!invokers)
1698             fprintf (rl_outstream, "# %s (not bound)\n", name);
1699           else
1700             {
1701               register int j;
1702
1703               for (j = 0; invokers[j]; j++)
1704                 {
1705                   fprintf (rl_outstream, "\"%s\": %s\n",
1706                            invokers[j], name);
1707                   free (invokers[j]);
1708                 }
1709
1710               free (invokers);
1711             }
1712         }
1713       else
1714         {
1715           if (!invokers)
1716             fprintf (rl_outstream, "%s is not bound to any keys\n",
1717                      name);
1718           else
1719             {
1720               register int j;
1721
1722               fprintf (rl_outstream, "%s can be found on ", name);
1723
1724               for (j = 0; invokers[j] && j < 5; j++)
1725                 {
1726                   fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1727                            invokers[j + 1] ? ", " : ".\n");
1728                 }
1729
1730               if (j == 5 && invokers[j])
1731                 fprintf (rl_outstream, "...\n");
1732
1733               for (j = 0; invokers[j]; j++)
1734                 free (invokers[j]);
1735
1736               free (invokers);
1737             }
1738         }
1739     }
1740 }
1741
1742 /* Print all of the current functions and their bindings to
1743    rl_outstream.  If an explicit argument is given, then print
1744    the output in such a way that it can be read back in. */
1745 int
1746 rl_dump_functions (count, key)
1747      int count, key;
1748 {
1749   if (rl_dispatching)
1750     fprintf (rl_outstream, "\r\n");
1751   rl_function_dumper (rl_explicit_arg);
1752   rl_on_new_line ();
1753   return (0);
1754 }
1755
1756 static void
1757 _rl_macro_dumper_internal (print_readably, map, prefix)
1758      int print_readably;
1759      Keymap map;
1760      char *prefix;
1761 {
1762   register int key;
1763   char *keyname, *out;
1764   int prefix_len;
1765
1766   for (key = 0; key < KEYMAP_SIZE; key++)
1767     {
1768       switch (map[key].type)
1769         {
1770         case ISMACR:
1771           keyname = _rl_get_keyname (key);
1772 #if 0
1773           out = (char *)map[key].function;
1774 #else
1775           out = _rl_untranslate_macro_value ((char *)map[key].function);
1776 #endif
1777           if (print_readably)
1778             fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1779                                                          keyname,
1780                                                          out ? out : "");
1781           else
1782             fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1783                                                         keyname,
1784                                                         out ? out : "");
1785           free (keyname);
1786 #if 1
1787           free (out);
1788 #endif
1789           break;
1790         case ISFUNC:
1791           break;
1792         case ISKMAP:
1793           prefix_len = prefix ? strlen (prefix) : 0;
1794           if (key == ESC)
1795             {
1796               keyname = xmalloc (3 + prefix_len);
1797               if (prefix)
1798                 strcpy (keyname, prefix);
1799               keyname[prefix_len] = '\\';
1800               keyname[prefix_len + 1] = 'e';
1801               keyname[prefix_len + 2] = '\0';
1802             }
1803           else
1804             {
1805               keyname = _rl_get_keyname (key);
1806               if (prefix)
1807                 {
1808                   out = xmalloc (strlen (keyname) + prefix_len + 1);
1809                   strcpy (out, prefix);
1810                   strcpy (out + prefix_len, keyname);
1811                   free (keyname);
1812                   keyname = out;
1813                 }
1814             }
1815
1816           _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1817           free (keyname);
1818           break;
1819         }
1820     }
1821 }
1822
1823 void
1824 rl_macro_dumper (print_readably)
1825      int print_readably;
1826 {
1827   _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1828 }
1829
1830 int
1831 rl_dump_macros (count, key)
1832      int count, key;
1833 {
1834   if (rl_dispatching)
1835     fprintf (rl_outstream, "\r\n");
1836   rl_macro_dumper (rl_explicit_arg);
1837   rl_on_new_line ();
1838   return (0);
1839 }
1840
1841 void
1842 rl_variable_dumper (print_readably)
1843      int print_readably;
1844 {
1845   int i;
1846   char *kname;
1847
1848   for (i = 0; boolean_varlist[i].name; i++)
1849     {
1850       if (print_readably)
1851         fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1852                                *boolean_varlist[i].value ? "on" : "off");
1853       else
1854         fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1855                                *boolean_varlist[i].value ? "on" : "off");
1856     }
1857
1858   /* bell-style */
1859   switch (_rl_bell_preference)
1860     {
1861     case NO_BELL:
1862       kname = "none"; break;
1863     case VISIBLE_BELL:
1864       kname = "visible"; break;
1865     case AUDIBLE_BELL:
1866     default:
1867       kname = "audible"; break;
1868     }
1869   if (print_readably)
1870     fprintf (rl_outstream, "set bell-style %s\n", kname);
1871   else
1872     fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1873
1874   /* comment-begin */
1875   if (print_readably)
1876     fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1877   else
1878     fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1879
1880   /* completion-query-items */
1881   if (print_readably)
1882     fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1883   else
1884     fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1885
1886   /* editing-mode */
1887   if (print_readably)
1888     fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1889   else
1890     fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1891
1892   /* keymap */
1893   kname = rl_get_keymap_name (_rl_keymap);
1894   if (kname == 0)
1895     kname = rl_get_keymap_name_from_edit_mode ();
1896   if (print_readably)
1897     fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1898   else
1899     fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1900 }
1901
1902 /* Print all of the current variables and their values to
1903    rl_outstream.  If an explicit argument is given, then print
1904    the output in such a way that it can be read back in. */
1905 int
1906 rl_dump_variables (count, key)
1907      int count, key;
1908 {
1909   if (rl_dispatching)
1910     fprintf (rl_outstream, "\r\n");
1911   rl_variable_dumper (rl_explicit_arg);
1912   rl_on_new_line ();
1913   return (0);
1914 }
1915
1916 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1917 void
1918 _rl_bind_if_unbound (keyseq, default_func)
1919      char *keyseq;
1920      Function *default_func;
1921 {
1922   Function *func;
1923
1924   if (keyseq)
1925     {
1926       func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1927       if (!func || func == rl_do_lowercase_version)
1928         rl_set_key (keyseq, default_func, _rl_keymap);
1929     }
1930 }
1931
1932 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1933 static int
1934 substring_member_of_array (string, array)
1935      char *string, **array;
1936 {
1937   while (*array)
1938     {
1939       if (_rl_strindex (string, *array))
1940         return (1);
1941       array++;
1942     }
1943   return (0);
1944 }