source: trunk/third/readline/bind.c @ 12992

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