home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / BIND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-20  |  35.1 KB  |  1,488 lines

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