HAMMER 60I/Many: Mirroring
[dragonfly.git] / contrib / readline-5.0 / bind.c
blob15d0e4e3a65a35db31daebf97cdd114e4aa34fa8
1 /* bind.c -- key binding and startup file support for the readline library. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
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 2, or
11 (at your option) any later version.
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.
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 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (__TANDEM)
26 # include <floss.h>
27 #endif
29 #if defined (HAVE_CONFIG_H)
30 # include <config.h>
31 #endif
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #if defined (HAVE_SYS_FILE_H)
37 # include <sys/file.h>
38 #endif /* HAVE_SYS_FILE_H */
40 #if defined (HAVE_UNISTD_H)
41 # include <unistd.h>
42 #endif /* HAVE_UNISTD_H */
44 #if defined (HAVE_STDLIB_H)
45 # include <stdlib.h>
46 #else
47 # include "ansi_stdlib.h"
48 #endif /* HAVE_STDLIB_H */
50 #include <errno.h>
52 #if !defined (errno)
53 extern int errno;
54 #endif /* !errno */
56 #include "posixstat.h"
58 /* System-specific feature definitions and include files. */
59 #include "rldefs.h"
61 /* Some standard library routines. */
62 #include "readline.h"
63 #include "history.h"
65 #include "rlprivate.h"
66 #include "rlshell.h"
67 #include "xmalloc.h"
69 #if !defined (strchr) && !defined (__STDC__)
70 extern char *strchr (), *strrchr ();
71 #endif /* !strchr && !__STDC__ */
73 /* Variables exported by this file. */
74 Keymap rl_binding_keymap;
76 static char *_rl_read_file PARAMS((char *, size_t *));
77 static void _rl_init_file_error PARAMS((const char *));
78 static int _rl_read_init_file PARAMS((const char *, int));
79 static int glean_key_from_name PARAMS((char *));
80 static int substring_member_of_array PARAMS((char *, const char **));
82 static int currently_reading_init_file;
84 /* used only in this file */
85 static int _rl_prefer_visible_bell = 1;
87 /* **************************************************************** */
88 /* */
89 /* Binding keys */
90 /* */
91 /* **************************************************************** */
93 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
94 Add NAME to the list of named functions. Make FUNCTION be the function
95 that gets called. If KEY is not -1, then bind it. */
96 int
97 rl_add_defun (name, function, key)
98 const char *name;
99 rl_command_func_t *function;
100 int key;
102 if (key != -1)
103 rl_bind_key (key, function);
104 rl_add_funmap_entry (name, function);
105 return 0;
108 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
110 rl_bind_key (key, function)
111 int key;
112 rl_command_func_t *function;
114 if (key < 0)
115 return (key);
117 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
119 if (_rl_keymap[ESC].type == ISKMAP)
121 Keymap escmap;
123 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
124 key = UNMETA (key);
125 escmap[key].type = ISFUNC;
126 escmap[key].function = function;
127 return (0);
129 return (key);
132 _rl_keymap[key].type = ISFUNC;
133 _rl_keymap[key].function = function;
134 rl_binding_keymap = _rl_keymap;
135 return (0);
138 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
139 KEY. */
141 rl_bind_key_in_map (key, function, map)
142 int key;
143 rl_command_func_t *function;
144 Keymap map;
146 int result;
147 Keymap oldmap;
149 oldmap = _rl_keymap;
150 _rl_keymap = map;
151 result = rl_bind_key (key, function);
152 _rl_keymap = oldmap;
153 return (result);
156 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
157 now, this is always used to attempt to bind the arrow keys, hence the
158 check for rl_vi_movement_mode. */
160 rl_bind_key_if_unbound_in_map (key, default_func, kmap)
161 int key;
162 rl_command_func_t *default_func;
163 Keymap kmap;
165 char keyseq[2];
167 keyseq[0] = (unsigned char)key;
168 keyseq[1] = '\0';
169 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap));
173 rl_bind_key_if_unbound (key, default_func)
174 int key;
175 rl_command_func_t *default_func;
177 char keyseq[2];
179 keyseq[0] = (unsigned char)key;
180 keyseq[1] = '\0';
181 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap));
184 /* Make KEY do nothing in the currently selected keymap.
185 Returns non-zero in case of error. */
187 rl_unbind_key (key)
188 int key;
190 return (rl_bind_key (key, (rl_command_func_t *)NULL));
193 /* Make KEY do nothing in MAP.
194 Returns non-zero in case of error. */
196 rl_unbind_key_in_map (key, map)
197 int key;
198 Keymap map;
200 return (rl_bind_key_in_map (key, (rl_command_func_t *)NULL, map));
203 /* Unbind all keys bound to FUNCTION in MAP. */
205 rl_unbind_function_in_map (func, map)
206 rl_command_func_t *func;
207 Keymap map;
209 register int i, rval;
211 for (i = rval = 0; i < KEYMAP_SIZE; i++)
213 if (map[i].type == ISFUNC && map[i].function == func)
215 map[i].function = (rl_command_func_t *)NULL;
216 rval = 1;
219 return rval;
223 rl_unbind_command_in_map (command, map)
224 const char *command;
225 Keymap map;
227 rl_command_func_t *func;
229 func = rl_named_function (command);
230 if (func == 0)
231 return 0;
232 return (rl_unbind_function_in_map (func, map));
235 /* Bind the key sequence represented by the string KEYSEQ to
236 FUNCTION, starting in the current keymap. This makes new
237 keymaps as necessary. */
239 rl_bind_keyseq (keyseq, function)
240 const char *keyseq;
241 rl_command_func_t *function;
243 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, _rl_keymap));
246 /* Bind the key sequence represented by the string KEYSEQ to
247 FUNCTION. This makes new keymaps as necessary. The initial
248 place to do bindings is in MAP. */
250 rl_bind_keyseq_in_map (keyseq, function, map)
251 const char *keyseq;
252 rl_command_func_t *function;
253 Keymap map;
255 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
258 /* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */
260 rl_set_key (keyseq, function, map)
261 const char *keyseq;
262 rl_command_func_t *function;
263 Keymap map;
265 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
268 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
269 now, this is always used to attempt to bind the arrow keys, hence the
270 check for rl_vi_movement_mode. */
272 rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap)
273 const char *keyseq;
274 rl_command_func_t *default_func;
275 Keymap kmap;
277 rl_command_func_t *func;
279 if (keyseq)
281 func = rl_function_of_keyseq (keyseq, kmap, (int *)NULL);
282 #if defined (VI_MODE)
283 if (!func || func == rl_do_lowercase_version || func == rl_vi_movement_mode)
284 #else
285 if (!func || func == rl_do_lowercase_version)
286 #endif
287 return (rl_bind_keyseq_in_map (keyseq, default_func, kmap));
288 else
289 return 1;
291 return 0;
295 rl_bind_keyseq_if_unbound (keyseq, default_func)
296 const char *keyseq;
297 rl_command_func_t *default_func;
299 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap));
302 /* Bind the key sequence represented by the string KEYSEQ to
303 the string of characters MACRO. This makes new keymaps as
304 necessary. The initial place to do bindings is in MAP. */
306 rl_macro_bind (keyseq, macro, map)
307 const char *keyseq, *macro;
308 Keymap map;
310 char *macro_keys;
311 int macro_keys_len;
313 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
315 if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
317 free (macro_keys);
318 return -1;
320 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
321 return 0;
324 /* Bind the key sequence represented by the string KEYSEQ to
325 the arbitrary pointer DATA. TYPE says what kind of data is
326 pointed to by DATA, right now this can be a function (ISFUNC),
327 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
328 as necessary. The initial place to do bindings is in MAP. */
330 rl_generic_bind (type, keyseq, data, map)
331 int type;
332 const char *keyseq;
333 char *data;
334 Keymap map;
336 char *keys;
337 int keys_len;
338 register int i;
339 KEYMAP_ENTRY k;
341 k.function = 0;
343 /* If no keys to bind to, exit right away. */
344 if (!keyseq || !*keyseq)
346 if (type == ISMACR)
347 free (data);
348 return -1;
351 keys = (char *)xmalloc (1 + (2 * strlen (keyseq)));
353 /* Translate the ASCII representation of KEYSEQ into an array of
354 characters. Stuff the characters into KEYS, and the length of
355 KEYS into KEYS_LEN. */
356 if (rl_translate_keyseq (keyseq, keys, &keys_len))
358 free (keys);
359 return -1;
362 /* Bind keys, making new keymaps as necessary. */
363 for (i = 0; i < keys_len; i++)
365 unsigned char uc = keys[i];
366 int ic;
368 ic = uc;
369 if (ic < 0 || ic >= KEYMAP_SIZE)
370 return -1;
372 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
374 ic = UNMETA (ic);
375 if (map[ESC].type == ISKMAP)
376 map = FUNCTION_TO_KEYMAP (map, ESC);
379 if ((i + 1) < keys_len)
381 if (map[ic].type != ISKMAP)
383 /* We allow subsequences of keys. If a keymap is being
384 created that will `shadow' an existing function or macro
385 key binding, we save that keybinding into the ANYOTHERKEY
386 index in the new map. The dispatch code will look there
387 to find the function to execute if the subsequence is not
388 matched. ANYOTHERKEY was chosen to be greater than
389 UCHAR_MAX. */
390 k = map[ic];
392 map[ic].type = ISKMAP;
393 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
395 map = FUNCTION_TO_KEYMAP (map, ic);
396 /* The dispatch code will return this function if no matching
397 key sequence is found in the keymap. This (with a little
398 help from the dispatch code in readline.c) allows `a' to be
399 mapped to something, `abc' to be mapped to something else,
400 and the function bound to `a' to be executed when the user
401 types `abx', leaving `bx' in the input queue. */
402 if (k.function && ((k.type == ISFUNC && k.function != rl_do_lowercase_version) || k.type == ISMACR))
404 map[ANYOTHERKEY] = k;
405 k.function = 0;
408 else
410 if (map[ic].type == ISMACR)
411 free ((char *)map[ic].function);
412 else if (map[ic].type == ISKMAP)
414 map = FUNCTION_TO_KEYMAP (map, ic);
415 ic = ANYOTHERKEY;
418 map[ic].function = KEYMAP_TO_FUNCTION (data);
419 map[ic].type = type;
422 rl_binding_keymap = map;
424 free (keys);
425 return 0;
428 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
429 an array of characters. LEN gets the final length of ARRAY. Return
430 non-zero if there was an error parsing SEQ. */
432 rl_translate_keyseq (seq, array, len)
433 const char *seq;
434 char *array;
435 int *len;
437 register int i, c, l, temp;
439 for (i = l = 0; c = seq[i]; i++)
441 if (c == '\\')
443 c = seq[++i];
445 if (c == 0)
446 break;
448 /* Handle \C- and \M- prefixes. */
449 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
451 /* Handle special case of backwards define. */
452 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
454 array[l++] = ESC; /* ESC is meta-prefix */
455 i += 5;
456 array[l++] = CTRL (_rl_to_upper (seq[i]));
457 if (seq[i] == '\0')
458 i--;
460 else if (c == 'M')
462 i++;
463 array[l++] = ESC; /* ESC is meta-prefix */
465 else if (c == 'C')
467 i += 2;
468 /* Special hack for C-?... */
469 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
471 continue;
474 /* Translate other backslash-escaped characters. These are the
475 same escape sequences that bash's `echo' and `printf' builtins
476 handle, with the addition of \d -> RUBOUT. A backslash
477 preceding a character that is not special is stripped. */
478 switch (c)
480 case 'a':
481 array[l++] = '\007';
482 break;
483 case 'b':
484 array[l++] = '\b';
485 break;
486 case 'd':
487 array[l++] = RUBOUT; /* readline-specific */
488 break;
489 case 'e':
490 array[l++] = ESC;
491 break;
492 case 'f':
493 array[l++] = '\f';
494 break;
495 case 'n':
496 array[l++] = NEWLINE;
497 break;
498 case 'r':
499 array[l++] = RETURN;
500 break;
501 case 't':
502 array[l++] = TAB;
503 break;
504 case 'v':
505 array[l++] = 0x0B;
506 break;
507 case '\\':
508 array[l++] = '\\';
509 break;
510 case '0': case '1': case '2': case '3':
511 case '4': case '5': case '6': case '7':
512 i++;
513 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
514 c = (c * 8) + OCTVALUE (seq[i]);
515 i--; /* auto-increment in for loop */
516 array[l++] = c & largest_char;
517 break;
518 case 'x':
519 i++;
520 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)seq[i]) && temp--; i++)
521 c = (c * 16) + HEXVALUE (seq[i]);
522 if (temp == 2)
523 c = 'x';
524 i--; /* auto-increment in for loop */
525 array[l++] = c & largest_char;
526 break;
527 default: /* backslashes before non-special chars just add the char */
528 array[l++] = c;
529 break; /* the backslash is stripped */
531 continue;
534 array[l++] = c;
537 *len = l;
538 array[l] = '\0';
539 return (0);
542 char *
543 rl_untranslate_keyseq (seq)
544 int seq;
546 static char kseq[16];
547 int i, c;
549 i = 0;
550 c = seq;
551 if (META_CHAR (c))
553 kseq[i++] = '\\';
554 kseq[i++] = 'M';
555 kseq[i++] = '-';
556 c = UNMETA (c);
558 else if (CTRL_CHAR (c))
560 kseq[i++] = '\\';
561 kseq[i++] = 'C';
562 kseq[i++] = '-';
563 c = _rl_to_lower (UNCTRL (c));
565 else if (c == RUBOUT)
567 kseq[i++] = '\\';
568 kseq[i++] = 'C';
569 kseq[i++] = '-';
570 c = '?';
573 if (c == ESC)
575 kseq[i++] = '\\';
576 c = 'e';
578 else if (c == '\\' || c == '"')
580 kseq[i++] = '\\';
583 kseq[i++] = (unsigned char) c;
584 kseq[i] = '\0';
585 return kseq;
588 static char *
589 _rl_untranslate_macro_value (seq)
590 char *seq;
592 char *ret, *r, *s;
593 int c;
595 r = ret = (char *)xmalloc (7 * strlen (seq) + 1);
596 for (s = seq; *s; s++)
598 c = *s;
599 if (META_CHAR (c))
601 *r++ = '\\';
602 *r++ = 'M';
603 *r++ = '-';
604 c = UNMETA (c);
606 else if (CTRL_CHAR (c) && c != ESC)
608 *r++ = '\\';
609 *r++ = 'C';
610 *r++ = '-';
611 c = _rl_to_lower (UNCTRL (c));
613 else if (c == RUBOUT)
615 *r++ = '\\';
616 *r++ = 'C';
617 *r++ = '-';
618 c = '?';
621 if (c == ESC)
623 *r++ = '\\';
624 c = 'e';
626 else if (c == '\\' || c == '"')
627 *r++ = '\\';
629 *r++ = (unsigned char)c;
631 *r = '\0';
632 return ret;
635 /* Return a pointer to the function that STRING represents.
636 If STRING doesn't have a matching function, then a NULL pointer
637 is returned. */
638 rl_command_func_t *
639 rl_named_function (string)
640 const char *string;
642 register int i;
644 rl_initialize_funmap ();
646 for (i = 0; funmap[i]; i++)
647 if (_rl_stricmp (funmap[i]->name, string) == 0)
648 return (funmap[i]->function);
649 return ((rl_command_func_t *)NULL);
652 /* Return the function (or macro) definition which would be invoked via
653 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
654 used. TYPE, if non-NULL, is a pointer to an int which will receive the
655 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
656 or ISMACR (macro). */
657 rl_command_func_t *
658 rl_function_of_keyseq (keyseq, map, type)
659 const char *keyseq;
660 Keymap map;
661 int *type;
663 register int i;
665 if (!map)
666 map = _rl_keymap;
668 for (i = 0; keyseq && keyseq[i]; i++)
670 unsigned char ic = keyseq[i];
672 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
674 if (map[ESC].type != ISKMAP)
676 if (type)
677 *type = map[ESC].type;
679 return (map[ESC].function);
681 else
683 map = FUNCTION_TO_KEYMAP (map, ESC);
684 ic = UNMETA (ic);
688 if (map[ic].type == ISKMAP)
690 /* If this is the last key in the key sequence, return the
691 map. */
692 if (!keyseq[i + 1])
694 if (type)
695 *type = ISKMAP;
697 return (map[ic].function);
699 else
700 map = FUNCTION_TO_KEYMAP (map, ic);
702 else
704 if (type)
705 *type = map[ic].type;
707 return (map[ic].function);
710 return ((rl_command_func_t *) NULL);
713 /* The last key bindings file read. */
714 static char *last_readline_init_file = (char *)NULL;
716 /* The file we're currently reading key bindings from. */
717 static const char *current_readline_init_file;
718 static int current_readline_init_include_level;
719 static int current_readline_init_lineno;
721 /* Read FILENAME into a locally-allocated buffer and return the buffer.
722 The size of the buffer is returned in *SIZEP. Returns NULL if any
723 errors were encountered. */
724 static char *
725 _rl_read_file (filename, sizep)
726 char *filename;
727 size_t *sizep;
729 struct stat finfo;
730 size_t file_size;
731 char *buffer;
732 int i, file;
734 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
735 return ((char *)NULL);
737 file_size = (size_t)finfo.st_size;
739 /* check for overflow on very large files */
740 if (file_size != finfo.st_size || file_size + 1 < file_size)
742 if (file >= 0)
743 close (file);
744 #if defined (EFBIG)
745 errno = EFBIG;
746 #endif
747 return ((char *)NULL);
750 /* Read the file into BUFFER. */
751 buffer = (char *)xmalloc (file_size + 1);
752 i = read (file, buffer, file_size);
753 close (file);
755 if (i < 0)
757 free (buffer);
758 return ((char *)NULL);
761 buffer[i] = '\0';
762 if (sizep)
763 *sizep = i;
765 return (buffer);
768 /* Re-read the current keybindings file. */
770 rl_re_read_init_file (count, ignore)
771 int count, ignore;
773 int r;
774 r = rl_read_init_file ((const char *)NULL);
775 rl_set_keymap_from_edit_mode ();
776 return r;
779 /* Do key bindings from a file. If FILENAME is NULL it defaults
780 to the first non-null filename from this list:
781 1. the filename used for the previous call
782 2. the value of the shell variable `INPUTRC'
783 3. ~/.inputrc
784 If the file existed and could be opened and read, 0 is returned,
785 otherwise errno is returned. */
787 rl_read_init_file (filename)
788 const char *filename;
790 /* Default the filename. */
791 if (filename == 0)
793 filename = last_readline_init_file;
794 if (filename == 0)
795 filename = sh_get_env_value ("INPUTRC");
796 if (filename == 0)
797 filename = DEFAULT_INPUTRC;
800 if (*filename == 0)
801 filename = DEFAULT_INPUTRC;
803 #if defined (__MSDOS__)
804 if (_rl_read_init_file (filename, 0) == 0)
805 return 0;
806 filename = "~/_inputrc";
807 #endif
808 return (_rl_read_init_file (filename, 0));
811 static int
812 _rl_read_init_file (filename, include_level)
813 const char *filename;
814 int include_level;
816 register int i;
817 char *buffer, *openname, *line, *end;
818 size_t file_size;
820 current_readline_init_file = filename;
821 current_readline_init_include_level = include_level;
823 openname = tilde_expand (filename);
824 buffer = _rl_read_file (openname, &file_size);
825 free (openname);
827 if (buffer == 0)
828 return (errno);
830 if (include_level == 0 && filename != last_readline_init_file)
832 FREE (last_readline_init_file);
833 last_readline_init_file = savestring (filename);
836 currently_reading_init_file = 1;
838 /* Loop over the lines in the file. Lines that start with `#' are
839 comments; all other lines are commands for readline initialization. */
840 current_readline_init_lineno = 1;
841 line = buffer;
842 end = buffer + file_size;
843 while (line < end)
845 /* Find the end of this line. */
846 for (i = 0; line + i != end && line[i] != '\n'; i++);
848 #if defined (__CYGWIN__)
849 /* ``Be liberal in what you accept.'' */
850 if (line[i] == '\n' && line[i-1] == '\r')
851 line[i - 1] = '\0';
852 #endif
854 /* Mark end of line. */
855 line[i] = '\0';
857 /* Skip leading whitespace. */
858 while (*line && whitespace (*line))
860 line++;
861 i--;
864 /* If the line is not a comment, then parse it. */
865 if (*line && *line != '#')
866 rl_parse_and_bind (line);
868 /* Move to the next line. */
869 line += i + 1;
870 current_readline_init_lineno++;
873 free (buffer);
874 currently_reading_init_file = 0;
875 return (0);
878 static void
879 _rl_init_file_error (msg)
880 const char *msg;
882 if (currently_reading_init_file)
883 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
884 current_readline_init_lineno, msg);
885 else
886 fprintf (stderr, "readline: %s\n", msg);
889 /* **************************************************************** */
890 /* */
891 /* Parser Directives */
892 /* */
893 /* **************************************************************** */
895 typedef int _rl_parser_func_t PARAMS((char *));
897 /* Things that mean `Control'. */
898 const char *_rl_possible_control_prefixes[] = {
899 "Control-", "C-", "CTRL-", (const char *)NULL
902 const char *_rl_possible_meta_prefixes[] = {
903 "Meta", "M-", (const char *)NULL
906 /* Conditionals. */
908 /* Calling programs set this to have their argv[0]. */
909 const char *rl_readline_name = "other";
911 /* Stack of previous values of parsing_conditionalized_out. */
912 static unsigned char *if_stack = (unsigned char *)NULL;
913 static int if_stack_depth;
914 static int if_stack_size;
916 /* Push _rl_parsing_conditionalized_out, and set parser state based
917 on ARGS. */
918 static int
919 parser_if (args)
920 char *args;
922 register int i;
924 /* Push parser state. */
925 if (if_stack_depth + 1 >= if_stack_size)
927 if (!if_stack)
928 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
929 else
930 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
932 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
934 /* If parsing is turned off, then nothing can turn it back on except
935 for finding the matching endif. In that case, return right now. */
936 if (_rl_parsing_conditionalized_out)
937 return 0;
939 /* Isolate first argument. */
940 for (i = 0; args[i] && !whitespace (args[i]); i++);
942 if (args[i])
943 args[i++] = '\0';
945 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
946 isn't term=foo, or mode=emacs, then check to see if the first
947 word in ARGS is the same as the value stored in rl_readline_name. */
948 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
950 char *tem, *tname;
952 /* Terminals like "aaa-60" are equivalent to "aaa". */
953 tname = savestring (rl_terminal_name);
954 tem = strchr (tname, '-');
955 if (tem)
956 *tem = '\0';
958 /* Test the `long' and `short' forms of the terminal name so that
959 if someone has a `sun-cmd' and does not want to have bindings
960 that will be executed if the terminal is a `sun', they can put
961 `$if term=sun-cmd' into their .inputrc. */
962 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
963 _rl_stricmp (args + 5, rl_terminal_name);
964 free (tname);
966 #if defined (VI_MODE)
967 else if (_rl_strnicmp (args, "mode=", 5) == 0)
969 int mode;
971 if (_rl_stricmp (args + 5, "emacs") == 0)
972 mode = emacs_mode;
973 else if (_rl_stricmp (args + 5, "vi") == 0)
974 mode = vi_mode;
975 else
976 mode = no_mode;
978 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
980 #endif /* VI_MODE */
981 /* Check to see if the first word in ARGS is the same as the
982 value stored in rl_readline_name. */
983 else if (_rl_stricmp (args, rl_readline_name) == 0)
984 _rl_parsing_conditionalized_out = 0;
985 else
986 _rl_parsing_conditionalized_out = 1;
987 return 0;
990 /* Invert the current parser state if there is anything on the stack. */
991 static int
992 parser_else (args)
993 char *args;
995 register int i;
997 if (if_stack_depth == 0)
999 _rl_init_file_error ("$else found without matching $if");
1000 return 0;
1003 #if 0
1004 /* Check the previous (n - 1) levels of the stack to make sure that
1005 we haven't previously turned off parsing. */
1006 for (i = 0; i < if_stack_depth - 1; i++)
1007 #else
1008 /* Check the previous (n) levels of the stack to make sure that
1009 we haven't previously turned off parsing. */
1010 for (i = 0; i < if_stack_depth; i++)
1011 #endif
1012 if (if_stack[i] == 1)
1013 return 0;
1015 /* Invert the state of parsing if at top level. */
1016 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
1017 return 0;
1020 /* Terminate a conditional, popping the value of
1021 _rl_parsing_conditionalized_out from the stack. */
1022 static int
1023 parser_endif (args)
1024 char *args;
1026 if (if_stack_depth)
1027 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
1028 else
1029 _rl_init_file_error ("$endif without matching $if");
1030 return 0;
1033 static int
1034 parser_include (args)
1035 char *args;
1037 const char *old_init_file;
1038 char *e;
1039 int old_line_number, old_include_level, r;
1041 if (_rl_parsing_conditionalized_out)
1042 return (0);
1044 old_init_file = current_readline_init_file;
1045 old_line_number = current_readline_init_lineno;
1046 old_include_level = current_readline_init_include_level;
1048 e = strchr (args, '\n');
1049 if (e)
1050 *e = '\0';
1051 r = _rl_read_init_file ((const char *)args, old_include_level + 1);
1053 current_readline_init_file = old_init_file;
1054 current_readline_init_lineno = old_line_number;
1055 current_readline_init_include_level = old_include_level;
1057 return r;
1060 /* Associate textual names with actual functions. */
1061 static struct {
1062 const char *name;
1063 _rl_parser_func_t *function;
1064 } parser_directives [] = {
1065 { "if", parser_if },
1066 { "endif", parser_endif },
1067 { "else", parser_else },
1068 { "include", parser_include },
1069 { (char *)0x0, (_rl_parser_func_t *)0x0 }
1072 /* Handle a parser directive. STATEMENT is the line of the directive
1073 without any leading `$'. */
1074 static int
1075 handle_parser_directive (statement)
1076 char *statement;
1078 register int i;
1079 char *directive, *args;
1081 /* Isolate the actual directive. */
1083 /* Skip whitespace. */
1084 for (i = 0; whitespace (statement[i]); i++);
1086 directive = &statement[i];
1088 for (; statement[i] && !whitespace (statement[i]); i++);
1090 if (statement[i])
1091 statement[i++] = '\0';
1093 for (; statement[i] && whitespace (statement[i]); i++);
1095 args = &statement[i];
1097 /* Lookup the command, and act on it. */
1098 for (i = 0; parser_directives[i].name; i++)
1099 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1101 (*parser_directives[i].function) (args);
1102 return (0);
1105 /* display an error message about the unknown parser directive */
1106 _rl_init_file_error ("unknown parser directive");
1107 return (1);
1110 /* Read the binding command from STRING and perform it.
1111 A key binding command looks like: Keyname: function-name\0,
1112 a variable binding command looks like: set variable value.
1113 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1115 rl_parse_and_bind (string)
1116 char *string;
1118 char *funname, *kname;
1119 register int c, i;
1120 int key, equivalency;
1122 while (string && whitespace (*string))
1123 string++;
1125 if (!string || !*string || *string == '#')
1126 return 0;
1128 /* If this is a parser directive, act on it. */
1129 if (*string == '$')
1131 handle_parser_directive (&string[1]);
1132 return 0;
1135 /* If we aren't supposed to be parsing right now, then we're done. */
1136 if (_rl_parsing_conditionalized_out)
1137 return 0;
1139 i = 0;
1140 /* If this keyname is a complex key expression surrounded by quotes,
1141 advance to after the matching close quote. This code allows the
1142 backslash to quote characters in the key expression. */
1143 if (*string == '"')
1145 int passc = 0;
1147 for (i = 1; c = string[i]; i++)
1149 if (passc)
1151 passc = 0;
1152 continue;
1155 if (c == '\\')
1157 passc++;
1158 continue;
1161 if (c == '"')
1162 break;
1164 /* If we didn't find a closing quote, abort the line. */
1165 if (string[i] == '\0')
1167 _rl_init_file_error ("no closing `\"' in key binding");
1168 return 1;
1172 /* Advance to the colon (:) or whitespace which separates the two objects. */
1173 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1175 equivalency = (c == ':' && string[i + 1] == '=');
1177 /* Mark the end of the command (or keyname). */
1178 if (string[i])
1179 string[i++] = '\0';
1181 /* If doing assignment, skip the '=' sign as well. */
1182 if (equivalency)
1183 string[i++] = '\0';
1185 /* If this is a command to set a variable, then do that. */
1186 if (_rl_stricmp (string, "set") == 0)
1188 char *var = string + i;
1189 char *value;
1191 /* Make VAR point to start of variable name. */
1192 while (*var && whitespace (*var)) var++;
1194 /* Make VALUE point to start of value string. */
1195 value = var;
1196 while (*value && !whitespace (*value)) value++;
1197 if (*value)
1198 *value++ = '\0';
1199 while (*value && whitespace (*value)) value++;
1201 rl_variable_bind (var, value);
1202 return 0;
1205 /* Skip any whitespace between keyname and funname. */
1206 for (; string[i] && whitespace (string[i]); i++);
1207 funname = &string[i];
1209 /* Now isolate funname.
1210 For straight function names just look for whitespace, since
1211 that will signify the end of the string. But this could be a
1212 macro definition. In that case, the string is quoted, so skip
1213 to the matching delimiter. We allow the backslash to quote the
1214 delimiter characters in the macro body. */
1215 /* This code exists to allow whitespace in macro expansions, which
1216 would otherwise be gobbled up by the next `for' loop.*/
1217 /* XXX - it may be desirable to allow backslash quoting only if " is
1218 the quoted string delimiter, like the shell. */
1219 if (*funname == '\'' || *funname == '"')
1221 int delimiter = string[i++], passc;
1223 for (passc = 0; c = string[i]; i++)
1225 if (passc)
1227 passc = 0;
1228 continue;
1231 if (c == '\\')
1233 passc = 1;
1234 continue;
1237 if (c == delimiter)
1238 break;
1240 if (c)
1241 i++;
1244 /* Advance to the end of the string. */
1245 for (; string[i] && !whitespace (string[i]); i++);
1247 /* No extra whitespace at the end of the string. */
1248 string[i] = '\0';
1250 /* Handle equivalency bindings here. Make the left-hand side be exactly
1251 whatever the right-hand evaluates to, including keymaps. */
1252 if (equivalency)
1254 return 0;
1257 /* If this is a new-style key-binding, then do the binding with
1258 rl_bind_keyseq (). Otherwise, let the older code deal with it. */
1259 if (*string == '"')
1261 char *seq;
1262 register int j, k, passc;
1264 seq = (char *)xmalloc (1 + strlen (string));
1265 for (j = 1, k = passc = 0; string[j]; j++)
1267 /* Allow backslash to quote characters, but leave them in place.
1268 This allows a string to end with a backslash quoting another
1269 backslash, or with a backslash quoting a double quote. The
1270 backslashes are left in place for rl_translate_keyseq (). */
1271 if (passc || (string[j] == '\\'))
1273 seq[k++] = string[j];
1274 passc = !passc;
1275 continue;
1278 if (string[j] == '"')
1279 break;
1281 seq[k++] = string[j];
1283 seq[k] = '\0';
1285 /* Binding macro? */
1286 if (*funname == '\'' || *funname == '"')
1288 j = strlen (funname);
1290 /* Remove the delimiting quotes from each end of FUNNAME. */
1291 if (j && funname[j - 1] == *funname)
1292 funname[j - 1] = '\0';
1294 rl_macro_bind (seq, &funname[1], _rl_keymap);
1296 else
1297 rl_bind_keyseq (seq, rl_named_function (funname));
1299 free (seq);
1300 return 0;
1303 /* Get the actual character we want to deal with. */
1304 kname = strrchr (string, '-');
1305 if (!kname)
1306 kname = string;
1307 else
1308 kname++;
1310 key = glean_key_from_name (kname);
1312 /* Add in control and meta bits. */
1313 if (substring_member_of_array (string, _rl_possible_control_prefixes))
1314 key = CTRL (_rl_to_upper (key));
1316 if (substring_member_of_array (string, _rl_possible_meta_prefixes))
1317 key = META (key);
1319 /* Temporary. Handle old-style keyname with macro-binding. */
1320 if (*funname == '\'' || *funname == '"')
1322 char useq[2];
1323 int fl = strlen (funname);
1325 useq[0] = key; useq[1] = '\0';
1326 if (fl && funname[fl - 1] == *funname)
1327 funname[fl - 1] = '\0';
1329 rl_macro_bind (useq, &funname[1], _rl_keymap);
1331 #if defined (PREFIX_META_HACK)
1332 /* Ugly, but working hack to keep prefix-meta around. */
1333 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1335 char seq[2];
1337 seq[0] = key;
1338 seq[1] = '\0';
1339 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1341 #endif /* PREFIX_META_HACK */
1342 else
1343 rl_bind_key (key, rl_named_function (funname));
1344 return 0;
1347 /* Simple structure for boolean readline variables (i.e., those that can
1348 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1349 false. */
1351 #define V_SPECIAL 0x1
1353 static struct {
1354 const char *name;
1355 int *value;
1356 int flags;
1357 } boolean_varlist [] = {
1358 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL },
1359 { "byte-oriented", &rl_byte_oriented, 0 },
1360 { "completion-ignore-case", &_rl_completion_case_fold, 0 },
1361 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 },
1362 { "disable-completion", &rl_inhibit_completion, 0 },
1363 { "enable-keypad", &_rl_enable_keypad, 0 },
1364 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 },
1365 { "history-preserve-point", &_rl_history_preserve_point, 0 },
1366 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 },
1367 { "input-meta", &_rl_meta_flag, 0 },
1368 { "mark-directories", &_rl_complete_mark_directories, 0 },
1369 { "mark-modified-lines", &_rl_mark_modified_lines, 0 },
1370 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 },
1371 { "match-hidden-files", &_rl_match_hidden_files, 0 },
1372 { "meta-flag", &_rl_meta_flag, 0 },
1373 { "output-meta", &_rl_output_meta_chars, 0 },
1374 { "page-completions", &_rl_page_completions, 0 },
1375 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL },
1376 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1377 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 },
1378 { "show-all-if-unmodified", &_rl_complete_show_unmodified, 0 },
1379 #if defined (VISIBLE_STATS)
1380 { "visible-stats", &rl_visible_stats, 0 },
1381 #endif /* VISIBLE_STATS */
1382 { (char *)NULL, (int *)NULL }
1385 static int
1386 find_boolean_var (name)
1387 const char *name;
1389 register int i;
1391 for (i = 0; boolean_varlist[i].name; i++)
1392 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1393 return i;
1394 return -1;
1397 /* Hooks for handling special boolean variables, where a
1398 function needs to be called or another variable needs
1399 to be changed when they're changed. */
1400 static void
1401 hack_special_boolean_var (i)
1402 int i;
1404 const char *name;
1406 name = boolean_varlist[i].name;
1408 if (_rl_stricmp (name, "blink-matching-paren") == 0)
1409 _rl_enable_paren_matching (rl_blink_matching_paren);
1410 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1412 if (_rl_prefer_visible_bell)
1413 _rl_bell_preference = VISIBLE_BELL;
1414 else
1415 _rl_bell_preference = AUDIBLE_BELL;
1419 typedef int _rl_sv_func_t PARAMS((const char *));
1421 /* These *must* correspond to the array indices for the appropriate
1422 string variable. (Though they're not used right now.) */
1423 #define V_BELLSTYLE 0
1424 #define V_COMBEGIN 1
1425 #define V_EDITMODE 2
1426 #define V_ISRCHTERM 3
1427 #define V_KEYMAP 4
1429 #define V_STRING 1
1430 #define V_INT 2
1432 /* Forward declarations */
1433 static int sv_bell_style PARAMS((const char *));
1434 static int sv_combegin PARAMS((const char *));
1435 static int sv_compquery PARAMS((const char *));
1436 static int sv_editmode PARAMS((const char *));
1437 static int sv_isrchterm PARAMS((const char *));
1438 static int sv_keymap PARAMS((const char *));
1440 static struct {
1441 const char *name;
1442 int flags;
1443 _rl_sv_func_t *set_func;
1444 } string_varlist[] = {
1445 { "bell-style", V_STRING, sv_bell_style },
1446 { "comment-begin", V_STRING, sv_combegin },
1447 { "completion-query-items", V_INT, sv_compquery },
1448 { "editing-mode", V_STRING, sv_editmode },
1449 { "isearch-terminators", V_STRING, sv_isrchterm },
1450 { "keymap", V_STRING, sv_keymap },
1451 { (char *)NULL, 0 }
1454 static int
1455 find_string_var (name)
1456 const char *name;
1458 register int i;
1460 for (i = 0; string_varlist[i].name; i++)
1461 if (_rl_stricmp (name, string_varlist[i].name) == 0)
1462 return i;
1463 return -1;
1466 /* A boolean value that can appear in a `set variable' command is true if
1467 the value is null or empty, `on' (case-insenstive), or "1". Any other
1468 values result in 0 (false). */
1469 static int
1470 bool_to_int (value)
1471 char *value;
1473 return (value == 0 || *value == '\0' ||
1474 (_rl_stricmp (value, "on") == 0) ||
1475 (value[0] == '1' && value[1] == '\0'));
1479 rl_variable_bind (name, value)
1480 const char *name, *value;
1482 register int i;
1483 int v;
1485 /* Check for simple variables first. */
1486 i = find_boolean_var (name);
1487 if (i >= 0)
1489 *boolean_varlist[i].value = bool_to_int (value);
1490 if (boolean_varlist[i].flags & V_SPECIAL)
1491 hack_special_boolean_var (i);
1492 return 0;
1495 i = find_string_var (name);
1497 /* For the time being, unknown variable names or string names without a
1498 handler function are simply ignored. */
1499 if (i < 0 || string_varlist[i].set_func == 0)
1500 return 0;
1502 v = (*string_varlist[i].set_func) (value);
1503 return v;
1506 static int
1507 sv_editmode (value)
1508 const char *value;
1510 if (_rl_strnicmp (value, "vi", 2) == 0)
1512 #if defined (VI_MODE)
1513 _rl_keymap = vi_insertion_keymap;
1514 rl_editing_mode = vi_mode;
1515 #endif /* VI_MODE */
1516 return 0;
1518 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1520 _rl_keymap = emacs_standard_keymap;
1521 rl_editing_mode = emacs_mode;
1522 return 0;
1524 return 1;
1527 static int
1528 sv_combegin (value)
1529 const char *value;
1531 if (value && *value)
1533 FREE (_rl_comment_begin);
1534 _rl_comment_begin = savestring (value);
1535 return 0;
1537 return 1;
1540 static int
1541 sv_compquery (value)
1542 const char *value;
1544 int nval = 100;
1546 if (value && *value)
1548 nval = atoi (value);
1549 if (nval < 0)
1550 nval = 0;
1552 rl_completion_query_items = nval;
1553 return 0;
1556 static int
1557 sv_keymap (value)
1558 const char *value;
1560 Keymap kmap;
1562 kmap = rl_get_keymap_by_name (value);
1563 if (kmap)
1565 rl_set_keymap (kmap);
1566 return 0;
1568 return 1;
1571 static int
1572 sv_bell_style (value)
1573 const char *value;
1575 if (value == 0 || *value == '\0')
1576 _rl_bell_preference = AUDIBLE_BELL;
1577 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1578 _rl_bell_preference = NO_BELL;
1579 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1580 _rl_bell_preference = AUDIBLE_BELL;
1581 else if (_rl_stricmp (value, "visible") == 0)
1582 _rl_bell_preference = VISIBLE_BELL;
1583 else
1584 return 1;
1585 return 0;
1588 static int
1589 sv_isrchterm (value)
1590 const char *value;
1592 int beg, end, delim;
1593 char *v;
1595 if (value == 0)
1596 return 1;
1598 /* Isolate the value and translate it into a character string. */
1599 v = savestring (value);
1600 FREE (_rl_isearch_terminators);
1601 if (v[0] == '"' || v[0] == '\'')
1603 delim = v[0];
1604 for (beg = end = 1; v[end] && v[end] != delim; end++)
1607 else
1609 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1613 v[end] = '\0';
1615 /* The value starts at v + beg. Translate it into a character string. */
1616 _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1);
1617 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1618 _rl_isearch_terminators[end] = '\0';
1620 free (v);
1621 return 0;
1624 /* Return the character which matches NAME.
1625 For example, `Space' returns ' '. */
1627 typedef struct {
1628 const char *name;
1629 int value;
1630 } assoc_list;
1632 static assoc_list name_key_alist[] = {
1633 { "DEL", 0x7f },
1634 { "ESC", '\033' },
1635 { "Escape", '\033' },
1636 { "LFD", '\n' },
1637 { "Newline", '\n' },
1638 { "RET", '\r' },
1639 { "Return", '\r' },
1640 { "Rubout", 0x7f },
1641 { "SPC", ' ' },
1642 { "Space", ' ' },
1643 { "Tab", 0x09 },
1644 { (char *)0x0, 0 }
1647 static int
1648 glean_key_from_name (name)
1649 char *name;
1651 register int i;
1653 for (i = 0; name_key_alist[i].name; i++)
1654 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1655 return (name_key_alist[i].value);
1657 return (*(unsigned char *)name); /* XXX was return (*name) */
1660 /* Auxiliary functions to manage keymaps. */
1661 static struct {
1662 const char *name;
1663 Keymap map;
1664 } keymap_names[] = {
1665 { "emacs", emacs_standard_keymap },
1666 { "emacs-standard", emacs_standard_keymap },
1667 { "emacs-meta", emacs_meta_keymap },
1668 { "emacs-ctlx", emacs_ctlx_keymap },
1669 #if defined (VI_MODE)
1670 { "vi", vi_movement_keymap },
1671 { "vi-move", vi_movement_keymap },
1672 { "vi-command", vi_movement_keymap },
1673 { "vi-insert", vi_insertion_keymap },
1674 #endif /* VI_MODE */
1675 { (char *)0x0, (Keymap)0x0 }
1678 Keymap
1679 rl_get_keymap_by_name (name)
1680 const char *name;
1682 register int i;
1684 for (i = 0; keymap_names[i].name; i++)
1685 if (_rl_stricmp (name, keymap_names[i].name) == 0)
1686 return (keymap_names[i].map);
1687 return ((Keymap) NULL);
1690 char *
1691 rl_get_keymap_name (map)
1692 Keymap map;
1694 register int i;
1695 for (i = 0; keymap_names[i].name; i++)
1696 if (map == keymap_names[i].map)
1697 return ((char *)keymap_names[i].name);
1698 return ((char *)NULL);
1701 void
1702 rl_set_keymap (map)
1703 Keymap map;
1705 if (map)
1706 _rl_keymap = map;
1709 Keymap
1710 rl_get_keymap ()
1712 return (_rl_keymap);
1715 void
1716 rl_set_keymap_from_edit_mode ()
1718 if (rl_editing_mode == emacs_mode)
1719 _rl_keymap = emacs_standard_keymap;
1720 #if defined (VI_MODE)
1721 else if (rl_editing_mode == vi_mode)
1722 _rl_keymap = vi_insertion_keymap;
1723 #endif /* VI_MODE */
1726 char *
1727 rl_get_keymap_name_from_edit_mode ()
1729 if (rl_editing_mode == emacs_mode)
1730 return "emacs";
1731 #if defined (VI_MODE)
1732 else if (rl_editing_mode == vi_mode)
1733 return "vi";
1734 #endif /* VI_MODE */
1735 else
1736 return "none";
1739 /* **************************************************************** */
1740 /* */
1741 /* Key Binding and Function Information */
1742 /* */
1743 /* **************************************************************** */
1745 /* Each of the following functions produces information about the
1746 state of keybindings and functions known to Readline. The info
1747 is always printed to rl_outstream, and in such a way that it can
1748 be read back in (i.e., passed to rl_parse_and_bind ()). */
1750 /* Print the names of functions known to Readline. */
1751 void
1752 rl_list_funmap_names ()
1754 register int i;
1755 const char **funmap_names;
1757 funmap_names = rl_funmap_names ();
1759 if (!funmap_names)
1760 return;
1762 for (i = 0; funmap_names[i]; i++)
1763 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1765 free (funmap_names);
1768 static char *
1769 _rl_get_keyname (key)
1770 int key;
1772 char *keyname;
1773 int i, c;
1775 keyname = (char *)xmalloc (8);
1777 c = key;
1778 /* Since this is going to be used to write out keysequence-function
1779 pairs for possible inclusion in an inputrc file, we don't want to
1780 do any special meta processing on KEY. */
1782 #if 1
1783 /* XXX - Experimental */
1784 /* We might want to do this, but the old version of the code did not. */
1786 /* If this is an escape character, we don't want to do any more processing.
1787 Just add the special ESC key sequence and return. */
1788 if (c == ESC)
1790 keyname[0] = '\\';
1791 keyname[1] = 'e';
1792 keyname[2] = '\0';
1793 return keyname;
1795 #endif
1797 /* RUBOUT is translated directly into \C-? */
1798 if (key == RUBOUT)
1800 keyname[0] = '\\';
1801 keyname[1] = 'C';
1802 keyname[2] = '-';
1803 keyname[3] = '?';
1804 keyname[4] = '\0';
1805 return keyname;
1808 i = 0;
1809 /* Now add special prefixes needed for control characters. This can
1810 potentially change C. */
1811 if (CTRL_CHAR (c))
1813 keyname[i++] = '\\';
1814 keyname[i++] = 'C';
1815 keyname[i++] = '-';
1816 c = _rl_to_lower (UNCTRL (c));
1819 /* XXX experimental code. Turn the characters that are not ASCII or
1820 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1821 This changes C. */
1822 if (c >= 128 && c <= 159)
1824 keyname[i++] = '\\';
1825 keyname[i++] = '2';
1826 c -= 128;
1827 keyname[i++] = (c / 8) + '0';
1828 c = (c % 8) + '0';
1831 /* Now, if the character needs to be quoted with a backslash, do that. */
1832 if (c == '\\' || c == '"')
1833 keyname[i++] = '\\';
1835 /* Now add the key, terminate the string, and return it. */
1836 keyname[i++] = (char) c;
1837 keyname[i] = '\0';
1839 return keyname;
1842 /* Return a NULL terminated array of strings which represent the key
1843 sequences that are used to invoke FUNCTION in MAP. */
1844 char **
1845 rl_invoking_keyseqs_in_map (function, map)
1846 rl_command_func_t *function;
1847 Keymap map;
1849 register int key;
1850 char **result;
1851 int result_index, result_size;
1853 result = (char **)NULL;
1854 result_index = result_size = 0;
1856 for (key = 0; key < KEYMAP_SIZE; key++)
1858 switch (map[key].type)
1860 case ISMACR:
1861 /* Macros match, if, and only if, the pointers are identical.
1862 Thus, they are treated exactly like functions in here. */
1863 case ISFUNC:
1864 /* If the function in the keymap is the one we are looking for,
1865 then add the current KEY to the list of invoking keys. */
1866 if (map[key].function == function)
1868 char *keyname;
1870 keyname = _rl_get_keyname (key);
1872 if (result_index + 2 > result_size)
1874 result_size += 10;
1875 result = (char **)xrealloc (result, result_size * sizeof (char *));
1878 result[result_index++] = keyname;
1879 result[result_index] = (char *)NULL;
1881 break;
1883 case ISKMAP:
1885 char **seqs;
1886 register int i;
1888 /* Find the list of keyseqs in this map which have FUNCTION as
1889 their target. Add the key sequences found to RESULT. */
1890 if (map[key].function)
1891 seqs =
1892 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1893 else
1894 break;
1896 if (seqs == 0)
1897 break;
1899 for (i = 0; seqs[i]; i++)
1901 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1903 if (key == ESC)
1904 #if 0
1905 sprintf (keyname, "\\e");
1906 #else
1907 /* XXX - experimental */
1908 sprintf (keyname, "\\M-");
1909 #endif
1910 else if (CTRL_CHAR (key))
1911 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1912 else if (key == RUBOUT)
1913 sprintf (keyname, "\\C-?");
1914 else if (key == '\\' || key == '"')
1916 keyname[0] = '\\';
1917 keyname[1] = (char) key;
1918 keyname[2] = '\0';
1920 else
1922 keyname[0] = (char) key;
1923 keyname[1] = '\0';
1926 strcat (keyname, seqs[i]);
1927 free (seqs[i]);
1929 if (result_index + 2 > result_size)
1931 result_size += 10;
1932 result = (char **)xrealloc (result, result_size * sizeof (char *));
1935 result[result_index++] = keyname;
1936 result[result_index] = (char *)NULL;
1939 free (seqs);
1941 break;
1944 return (result);
1947 /* Return a NULL terminated array of strings which represent the key
1948 sequences that can be used to invoke FUNCTION using the current keymap. */
1949 char **
1950 rl_invoking_keyseqs (function)
1951 rl_command_func_t *function;
1953 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1956 /* Print all of the functions and their bindings to rl_outstream. If
1957 PRINT_READABLY is non-zero, then print the output in such a way
1958 that it can be read back in. */
1959 void
1960 rl_function_dumper (print_readably)
1961 int print_readably;
1963 register int i;
1964 const char **names;
1965 const char *name;
1967 names = rl_funmap_names ();
1969 fprintf (rl_outstream, "\n");
1971 for (i = 0; name = names[i]; i++)
1973 rl_command_func_t *function;
1974 char **invokers;
1976 function = rl_named_function (name);
1977 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1979 if (print_readably)
1981 if (!invokers)
1982 fprintf (rl_outstream, "# %s (not bound)\n", name);
1983 else
1985 register int j;
1987 for (j = 0; invokers[j]; j++)
1989 fprintf (rl_outstream, "\"%s\": %s\n",
1990 invokers[j], name);
1991 free (invokers[j]);
1994 free (invokers);
1997 else
1999 if (!invokers)
2000 fprintf (rl_outstream, "%s is not bound to any keys\n",
2001 name);
2002 else
2004 register int j;
2006 fprintf (rl_outstream, "%s can be found on ", name);
2008 for (j = 0; invokers[j] && j < 5; j++)
2010 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
2011 invokers[j + 1] ? ", " : ".\n");
2014 if (j == 5 && invokers[j])
2015 fprintf (rl_outstream, "...\n");
2017 for (j = 0; invokers[j]; j++)
2018 free (invokers[j]);
2020 free (invokers);
2026 /* Print all of the current functions and their bindings to
2027 rl_outstream. If an explicit argument is given, then print
2028 the output in such a way that it can be read back in. */
2030 rl_dump_functions (count, key)
2031 int count, key;
2033 if (rl_dispatching)
2034 fprintf (rl_outstream, "\r\n");
2035 rl_function_dumper (rl_explicit_arg);
2036 rl_on_new_line ();
2037 return (0);
2040 static void
2041 _rl_macro_dumper_internal (print_readably, map, prefix)
2042 int print_readably;
2043 Keymap map;
2044 char *prefix;
2046 register int key;
2047 char *keyname, *out;
2048 int prefix_len;
2050 for (key = 0; key < KEYMAP_SIZE; key++)
2052 switch (map[key].type)
2054 case ISMACR:
2055 keyname = _rl_get_keyname (key);
2056 out = _rl_untranslate_macro_value ((char *)map[key].function);
2058 if (print_readably)
2059 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
2060 keyname,
2061 out ? out : "");
2062 else
2063 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
2064 keyname,
2065 out ? out : "");
2066 free (keyname);
2067 free (out);
2068 break;
2069 case ISFUNC:
2070 break;
2071 case ISKMAP:
2072 prefix_len = prefix ? strlen (prefix) : 0;
2073 if (key == ESC)
2075 keyname = (char *)xmalloc (3 + prefix_len);
2076 if (prefix)
2077 strcpy (keyname, prefix);
2078 keyname[prefix_len] = '\\';
2079 keyname[prefix_len + 1] = 'e';
2080 keyname[prefix_len + 2] = '\0';
2082 else
2084 keyname = _rl_get_keyname (key);
2085 if (prefix)
2087 out = (char *)xmalloc (strlen (keyname) + prefix_len + 1);
2088 strcpy (out, prefix);
2089 strcpy (out + prefix_len, keyname);
2090 free (keyname);
2091 keyname = out;
2095 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
2096 free (keyname);
2097 break;
2102 void
2103 rl_macro_dumper (print_readably)
2104 int print_readably;
2106 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
2110 rl_dump_macros (count, key)
2111 int count, key;
2113 if (rl_dispatching)
2114 fprintf (rl_outstream, "\r\n");
2115 rl_macro_dumper (rl_explicit_arg);
2116 rl_on_new_line ();
2117 return (0);
2120 void
2121 rl_variable_dumper (print_readably)
2122 int print_readably;
2124 int i;
2125 const char *kname;
2127 for (i = 0; boolean_varlist[i].name; i++)
2129 if (print_readably)
2130 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
2131 *boolean_varlist[i].value ? "on" : "off");
2132 else
2133 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
2134 *boolean_varlist[i].value ? "on" : "off");
2137 /* bell-style */
2138 switch (_rl_bell_preference)
2140 case NO_BELL:
2141 kname = "none"; break;
2142 case VISIBLE_BELL:
2143 kname = "visible"; break;
2144 case AUDIBLE_BELL:
2145 default:
2146 kname = "audible"; break;
2148 if (print_readably)
2149 fprintf (rl_outstream, "set bell-style %s\n", kname);
2150 else
2151 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
2153 /* comment-begin */
2154 if (print_readably)
2155 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2156 else
2157 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2159 /* completion-query-items */
2160 if (print_readably)
2161 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
2162 else
2163 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
2165 /* editing-mode */
2166 if (print_readably)
2167 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2168 else
2169 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2171 /* isearch-terminators */
2172 if (_rl_isearch_terminators)
2174 char *disp;
2176 disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
2178 if (print_readably)
2179 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
2180 else
2181 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
2183 free (disp);
2186 /* keymap */
2187 kname = rl_get_keymap_name (_rl_keymap);
2188 if (kname == 0)
2189 kname = rl_get_keymap_name_from_edit_mode ();
2190 if (print_readably)
2191 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
2192 else
2193 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
2196 /* Print all of the current variables and their values to
2197 rl_outstream. If an explicit argument is given, then print
2198 the output in such a way that it can be read back in. */
2200 rl_dump_variables (count, key)
2201 int count, key;
2203 if (rl_dispatching)
2204 fprintf (rl_outstream, "\r\n");
2205 rl_variable_dumper (rl_explicit_arg);
2206 rl_on_new_line ();
2207 return (0);
2210 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2211 static int
2212 substring_member_of_array (string, array)
2213 char *string;
2214 const char **array;
2216 while (*array)
2218 if (_rl_strindex (string, *array))
2219 return (1);
2220 array++;
2222 return (0);