home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / readline / bind.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  43KB  |  1,777 lines

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