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

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