Import readline 7.0 (patch 5)
[deliverable/binutils-gdb.git] / readline / bind.c
1 /* bind.c -- key binding and startup file support for the readline library. */
2
3 /* Copyright (C) 1987-2016 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
7
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (__TANDEM)
25 # include <floss.h>
26 #endif
27
28 #if defined (HAVE_CONFIG_H)
29 # include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #if defined (HAVE_SYS_FILE_H)
36 # include <sys/file.h>
37 #endif /* HAVE_SYS_FILE_H */
38
39 #if defined (HAVE_UNISTD_H)
40 # include <unistd.h>
41 #endif /* HAVE_UNISTD_H */
42
43 #if defined (HAVE_STDLIB_H)
44 # include <stdlib.h>
45 #else
46 # include "ansi_stdlib.h"
47 #endif /* HAVE_STDLIB_H */
48
49 #include <errno.h>
50
51 #if !defined (errno)
52 extern int errno;
53 #endif /* !errno */
54
55 #include "posixstat.h"
56
57 /* System-specific feature definitions and include files. */
58 #include "rldefs.h"
59
60 /* Some standard library routines. */
61 #include "readline.h"
62 #include "history.h"
63
64 #include "rlprivate.h"
65 #include "rlshell.h"
66 #include "xmalloc.h"
67
68 #if !defined (strchr) && !defined (__STDC__)
69 extern char *strchr (), *strrchr ();
70 #endif /* !strchr && !__STDC__ */
71
72 /* Variables exported by this file. */
73 Keymap rl_binding_keymap;
74
75 static int _rl_skip_to_delim PARAMS((char *, int, int));
76
77 #if defined (USE_VARARGS) && defined (PREFER_STDARG)
78 static void _rl_init_file_error (const char *, ...) __attribute__((__format__ (printf, 1, 2)));
79 #else
80 static void _rl_init_file_error ();
81 #endif
82
83 static char *_rl_read_file PARAMS((char *, size_t *));
84 static int _rl_read_init_file PARAMS((const char *, int));
85 static int glean_key_from_name PARAMS((char *));
86
87 static int find_boolean_var PARAMS((const char *));
88 static int find_string_var PARAMS((const char *));
89
90 static char *_rl_get_string_variable_value PARAMS((const char *));
91 static int substring_member_of_array PARAMS((const char *, const char * const *));
92
93 static int currently_reading_init_file;
94
95 /* used only in this file */
96 static int _rl_prefer_visible_bell = 1;
97
98 /* **************************************************************** */
99 /* */
100 /* Binding keys */
101 /* */
102 /* **************************************************************** */
103
104 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
105 Add NAME to the list of named functions. Make FUNCTION be the function
106 that gets called. If KEY is not -1, then bind it. */
107 int
108 rl_add_defun (name, function, key)
109 const char *name;
110 rl_command_func_t *function;
111 int key;
112 {
113 if (key != -1)
114 rl_bind_key (key, function);
115 rl_add_funmap_entry (name, function);
116 return 0;
117 }
118
119 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
120 int
121 rl_bind_key (key, function)
122 int key;
123 rl_command_func_t *function;
124 {
125 char keyseq[3];
126 int l;
127
128 if (key < 0)
129 return (key);
130
131 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
132 {
133 if (_rl_keymap[ESC].type == ISKMAP)
134 {
135 Keymap escmap;
136
137 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
138 key = UNMETA (key);
139 escmap[key].type = ISFUNC;
140 escmap[key].function = function;
141 return (0);
142 }
143 return (key);
144 }
145
146 /* If it's bound to a function or macro, just overwrite. Otherwise we have
147 to treat it as a key sequence so rl_generic_bind handles shadow keymaps
148 for us. If we are binding '\' make sure to escape it so it makes it
149 through the call to rl_translate_keyseq. */
150 if (_rl_keymap[key].type != ISKMAP)
151 {
152 _rl_keymap[key].type = ISFUNC;
153 _rl_keymap[key].function = function;
154 }
155 else
156 {
157 l = 0;
158 if (key == '\\')
159 keyseq[l++] = '\\';
160 keyseq[l++] = key;
161 keyseq[l] = '\0';
162 rl_bind_keyseq (keyseq, function);
163 }
164 rl_binding_keymap = _rl_keymap;
165 return (0);
166 }
167
168 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
169 KEY. */
170 int
171 rl_bind_key_in_map (key, function, map)
172 int key;
173 rl_command_func_t *function;
174 Keymap map;
175 {
176 int result;
177 Keymap oldmap;
178
179 oldmap = _rl_keymap;
180 _rl_keymap = map;
181 result = rl_bind_key (key, function);
182 _rl_keymap = oldmap;
183 return (result);
184 }
185
186 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
187 now, this is always used to attempt to bind the arrow keys, hence the
188 check for rl_vi_movement_mode. */
189 int
190 rl_bind_key_if_unbound_in_map (key, default_func, kmap)
191 int key;
192 rl_command_func_t *default_func;
193 Keymap kmap;
194 {
195 char keyseq[2];
196
197 keyseq[0] = (unsigned char)key;
198 keyseq[1] = '\0';
199 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap));
200 }
201
202 int
203 rl_bind_key_if_unbound (key, default_func)
204 int key;
205 rl_command_func_t *default_func;
206 {
207 char keyseq[2];
208
209 keyseq[0] = (unsigned char)key;
210 keyseq[1] = '\0';
211 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap));
212 }
213
214 /* Make KEY do nothing in the currently selected keymap.
215 Returns non-zero in case of error. */
216 int
217 rl_unbind_key (key)
218 int key;
219 {
220 return (rl_bind_key (key, (rl_command_func_t *)NULL));
221 }
222
223 /* Make KEY do nothing in MAP.
224 Returns non-zero in case of error. */
225 int
226 rl_unbind_key_in_map (key, map)
227 int key;
228 Keymap map;
229 {
230 return (rl_bind_key_in_map (key, (rl_command_func_t *)NULL, map));
231 }
232
233 /* Unbind all keys bound to FUNCTION in MAP. */
234 int
235 rl_unbind_function_in_map (func, map)
236 rl_command_func_t *func;
237 Keymap map;
238 {
239 register int i, rval;
240
241 for (i = rval = 0; i < KEYMAP_SIZE; i++)
242 {
243 if (map[i].type == ISFUNC && map[i].function == func)
244 {
245 map[i].function = (rl_command_func_t *)NULL;
246 rval = 1;
247 }
248 }
249 return rval;
250 }
251
252 int
253 rl_unbind_command_in_map (command, map)
254 const char *command;
255 Keymap map;
256 {
257 rl_command_func_t *func;
258
259 func = rl_named_function (command);
260 if (func == 0)
261 return 0;
262 return (rl_unbind_function_in_map (func, map));
263 }
264
265 /* Bind the key sequence represented by the string KEYSEQ to
266 FUNCTION, starting in the current keymap. This makes new
267 keymaps as necessary. */
268 int
269 rl_bind_keyseq (keyseq, function)
270 const char *keyseq;
271 rl_command_func_t *function;
272 {
273 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, _rl_keymap));
274 }
275
276 /* Bind the key sequence represented by the string KEYSEQ to
277 FUNCTION. This makes new keymaps as necessary. The initial
278 place to do bindings is in MAP. */
279 int
280 rl_bind_keyseq_in_map (keyseq, function, map)
281 const char *keyseq;
282 rl_command_func_t *function;
283 Keymap map;
284 {
285 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
286 }
287
288 /* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */
289 int
290 rl_set_key (keyseq, function, map)
291 const char *keyseq;
292 rl_command_func_t *function;
293 Keymap map;
294 {
295 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
296 }
297
298 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
299 now, this is always used to attempt to bind the arrow keys, hence the
300 check for rl_vi_movement_mode. */
301 int
302 rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap)
303 const char *keyseq;
304 rl_command_func_t *default_func;
305 Keymap kmap;
306 {
307 rl_command_func_t *func;
308
309 if (keyseq)
310 {
311 func = rl_function_of_keyseq (keyseq, kmap, (int *)NULL);
312 #if defined (VI_MODE)
313 if (!func || func == rl_do_lowercase_version || func == rl_vi_movement_mode)
314 #else
315 if (!func || func == rl_do_lowercase_version)
316 #endif
317 return (rl_bind_keyseq_in_map (keyseq, default_func, kmap));
318 else
319 return 1;
320 }
321 return 0;
322 }
323
324 int
325 rl_bind_keyseq_if_unbound (keyseq, default_func)
326 const char *keyseq;
327 rl_command_func_t *default_func;
328 {
329 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap));
330 }
331
332 /* Bind the key sequence represented by the string KEYSEQ to
333 the string of characters MACRO. This makes new keymaps as
334 necessary. The initial place to do bindings is in MAP. */
335 int
336 rl_macro_bind (keyseq, macro, map)
337 const char *keyseq, *macro;
338 Keymap map;
339 {
340 char *macro_keys;
341 int macro_keys_len;
342
343 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
344
345 if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
346 {
347 xfree (macro_keys);
348 return -1;
349 }
350 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
351 return 0;
352 }
353
354 /* Bind the key sequence represented by the string KEYSEQ to
355 the arbitrary pointer DATA. TYPE says what kind of data is
356 pointed to by DATA, right now this can be a function (ISFUNC),
357 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
358 as necessary. The initial place to do bindings is in MAP. */
359 int
360 rl_generic_bind (type, keyseq, data, map)
361 int type;
362 const char *keyseq;
363 char *data;
364 Keymap map;
365 {
366 char *keys;
367 int keys_len;
368 register int i;
369 KEYMAP_ENTRY k;
370
371 k.function = 0;
372
373 /* If no keys to bind to, exit right away. */
374 if (keyseq == 0 || *keyseq == 0)
375 {
376 if (type == ISMACR)
377 xfree (data);
378 return -1;
379 }
380
381 keys = (char *)xmalloc (1 + (2 * strlen (keyseq)));
382
383 /* Translate the ASCII representation of KEYSEQ into an array of
384 characters. Stuff the characters into KEYS, and the length of
385 KEYS into KEYS_LEN. */
386 if (rl_translate_keyseq (keyseq, keys, &keys_len))
387 {
388 xfree (keys);
389 return -1;
390 }
391
392 /* Bind keys, making new keymaps as necessary. */
393 for (i = 0; i < keys_len; i++)
394 {
395 unsigned char uc = keys[i];
396 int ic;
397
398 ic = uc;
399 if (ic < 0 || ic >= KEYMAP_SIZE)
400 {
401 xfree (keys);
402 return -1;
403 }
404
405 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
406 {
407 ic = UNMETA (ic);
408 if (map[ESC].type == ISKMAP)
409 map = FUNCTION_TO_KEYMAP (map, ESC);
410 }
411
412 if ((i + 1) < keys_len)
413 {
414 if (map[ic].type != ISKMAP)
415 {
416 /* We allow subsequences of keys. If a keymap is being
417 created that will `shadow' an existing function or macro
418 key binding, we save that keybinding into the ANYOTHERKEY
419 index in the new map. The dispatch code will look there
420 to find the function to execute if the subsequence is not
421 matched. ANYOTHERKEY was chosen to be greater than
422 UCHAR_MAX. */
423 k = map[ic];
424
425 map[ic].type = ISKMAP;
426 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
427 }
428 map = FUNCTION_TO_KEYMAP (map, ic);
429 /* The dispatch code will return this function if no matching
430 key sequence is found in the keymap. This (with a little
431 help from the dispatch code in readline.c) allows `a' to be
432 mapped to something, `abc' to be mapped to something else,
433 and the function bound to `a' to be executed when the user
434 types `abx', leaving `bx' in the input queue. */
435 if (k.function && ((k.type == ISFUNC && k.function != rl_do_lowercase_version) || k.type == ISMACR))
436 {
437 map[ANYOTHERKEY] = k;
438 k.function = 0;
439 }
440 }
441 else
442 {
443 if (map[ic].type == ISMACR)
444 xfree ((char *)map[ic].function);
445 else if (map[ic].type == ISKMAP)
446 {
447 map = FUNCTION_TO_KEYMAP (map, ic);
448 ic = ANYOTHERKEY;
449 /* If we're trying to override a keymap with a null function
450 (e.g., trying to unbind it), we can't use a null pointer
451 here because that's indistinguishable from having not been
452 overridden. We use a special bindable function that does
453 nothing. */
454 if (type == ISFUNC && data == 0)
455 data = (char *)_rl_null_function;
456 }
457
458 map[ic].function = KEYMAP_TO_FUNCTION (data);
459 map[ic].type = type;
460 }
461
462 rl_binding_keymap = map;
463 }
464 xfree (keys);
465 return 0;
466 }
467
468 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
469 an array of characters. LEN gets the final length of ARRAY. Return
470 non-zero if there was an error parsing SEQ. */
471 int
472 rl_translate_keyseq (seq, array, len)
473 const char *seq;
474 char *array;
475 int *len;
476 {
477 register int i, c, l, temp;
478
479 for (i = l = 0; c = seq[i]; i++)
480 {
481 if (c == '\\')
482 {
483 c = seq[++i];
484
485 if (c == 0)
486 break;
487
488 /* Handle \C- and \M- prefixes. */
489 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
490 {
491 /* Handle special case of backwards define. */
492 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
493 {
494 array[l++] = ESC; /* ESC is meta-prefix */
495 i += 5;
496 array[l++] = CTRL (_rl_to_upper (seq[i]));
497 if (seq[i] == '\0')
498 i--;
499 }
500 else if (c == 'M')
501 {
502 i++; /* seq[i] == '-' */
503 /* XXX - obey convert-meta setting */
504 if (_rl_convert_meta_chars_to_ascii && _rl_keymap[ESC].type == ISKMAP)
505 array[l++] = ESC; /* ESC is meta-prefix */
506 else if (seq[i+1] == '\\' && seq[i+2] == 'C' && seq[i+3] == '-')
507 {
508 i += 4;
509 temp = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
510 array[l++] = META (temp);
511 }
512 else
513 {
514 /* This doesn't yet handle things like \M-\a, which may
515 or may not have any reasonable meaning. You're
516 probably better off using straight octal or hex. */
517 i++;
518 array[l++] = META (seq[i]);
519 }
520 }
521 else if (c == 'C')
522 {
523 i += 2;
524 /* Special hack for C-?... */
525 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
526 }
527 continue;
528 }
529
530 /* Translate other backslash-escaped characters. These are the
531 same escape sequences that bash's `echo' and `printf' builtins
532 handle, with the addition of \d -> RUBOUT. A backslash
533 preceding a character that is not special is stripped. */
534 switch (c)
535 {
536 case 'a':
537 array[l++] = '\007';
538 break;
539 case 'b':
540 array[l++] = '\b';
541 break;
542 case 'd':
543 array[l++] = RUBOUT; /* readline-specific */
544 break;
545 case 'e':
546 array[l++] = ESC;
547 break;
548 case 'f':
549 array[l++] = '\f';
550 break;
551 case 'n':
552 array[l++] = NEWLINE;
553 break;
554 case 'r':
555 array[l++] = RETURN;
556 break;
557 case 't':
558 array[l++] = TAB;
559 break;
560 case 'v':
561 array[l++] = 0x0B;
562 break;
563 case '\\':
564 array[l++] = '\\';
565 break;
566 case '0': case '1': case '2': case '3':
567 case '4': case '5': case '6': case '7':
568 i++;
569 for (temp = 2, c -= '0'; ISOCTAL ((unsigned char)seq[i]) && temp--; i++)
570 c = (c * 8) + OCTVALUE (seq[i]);
571 i--; /* auto-increment in for loop */
572 array[l++] = c & largest_char;
573 break;
574 case 'x':
575 i++;
576 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)seq[i]) && temp--; i++)
577 c = (c * 16) + HEXVALUE (seq[i]);
578 if (temp == 2)
579 c = 'x';
580 i--; /* auto-increment in for loop */
581 array[l++] = c & largest_char;
582 break;
583 default: /* backslashes before non-special chars just add the char */
584 array[l++] = c;
585 break; /* the backslash is stripped */
586 }
587 continue;
588 }
589
590 array[l++] = c;
591 }
592
593 *len = l;
594 array[l] = '\0';
595 return (0);
596 }
597
598 static int
599 _rl_isescape (c)
600 int c;
601 {
602 switch (c)
603 {
604 case '\007':
605 case '\b':
606 case '\f':
607 case '\n':
608 case '\r':
609 case TAB:
610 case 0x0b: return (1);
611 default: return (0);
612 }
613 }
614
615 static int
616 _rl_escchar (c)
617 int c;
618 {
619 switch (c)
620 {
621 case '\007': return ('a');
622 case '\b': return ('b');
623 case '\f': return ('f');
624 case '\n': return ('n');
625 case '\r': return ('r');
626 case TAB: return ('t');
627 case 0x0b: return ('v');
628 default: return (c);
629 }
630 }
631
632 char *
633 rl_untranslate_keyseq (seq)
634 int seq;
635 {
636 static char kseq[16];
637 int i, c;
638
639 i = 0;
640 c = seq;
641 if (META_CHAR (c))
642 {
643 kseq[i++] = '\\';
644 kseq[i++] = 'M';
645 kseq[i++] = '-';
646 c = UNMETA (c);
647 }
648 else if (c == ESC)
649 {
650 kseq[i++] = '\\';
651 c = 'e';
652 }
653 else if (CTRL_CHAR (c))
654 {
655 kseq[i++] = '\\';
656 kseq[i++] = 'C';
657 kseq[i++] = '-';
658 c = _rl_to_lower (UNCTRL (c));
659 }
660 else if (c == RUBOUT)
661 {
662 kseq[i++] = '\\';
663 kseq[i++] = 'C';
664 kseq[i++] = '-';
665 c = '?';
666 }
667
668 if (c == ESC)
669 {
670 kseq[i++] = '\\';
671 c = 'e';
672 }
673 else if (c == '\\' || c == '"')
674 {
675 kseq[i++] = '\\';
676 }
677
678 kseq[i++] = (unsigned char) c;
679 kseq[i] = '\0';
680 return kseq;
681 }
682
683 char *
684 _rl_untranslate_macro_value (seq, use_escapes)
685 char *seq;
686 int use_escapes;
687 {
688 char *ret, *r, *s;
689 int c;
690
691 r = ret = (char *)xmalloc (7 * strlen (seq) + 1);
692 for (s = seq; *s; s++)
693 {
694 c = *s;
695 if (META_CHAR (c))
696 {
697 *r++ = '\\';
698 *r++ = 'M';
699 *r++ = '-';
700 c = UNMETA (c);
701 }
702 else if (c == ESC)
703 {
704 *r++ = '\\';
705 c = 'e';
706 }
707 else if (CTRL_CHAR (c))
708 {
709 *r++ = '\\';
710 if (use_escapes && _rl_isescape (c))
711 c = _rl_escchar (c);
712 else
713 {
714 *r++ = 'C';
715 *r++ = '-';
716 c = _rl_to_lower (UNCTRL (c));
717 }
718 }
719 else if (c == RUBOUT)
720 {
721 *r++ = '\\';
722 *r++ = 'C';
723 *r++ = '-';
724 c = '?';
725 }
726
727 if (c == ESC)
728 {
729 *r++ = '\\';
730 c = 'e';
731 }
732 else if (c == '\\' || c == '"')
733 *r++ = '\\';
734
735 *r++ = (unsigned char)c;
736 }
737 *r = '\0';
738 return ret;
739 }
740
741 /* Return a pointer to the function that STRING represents.
742 If STRING doesn't have a matching function, then a NULL pointer
743 is returned. */
744 rl_command_func_t *
745 rl_named_function (string)
746 const char *string;
747 {
748 register int i;
749
750 rl_initialize_funmap ();
751
752 for (i = 0; funmap[i]; i++)
753 if (_rl_stricmp (funmap[i]->name, string) == 0)
754 return (funmap[i]->function);
755 return ((rl_command_func_t *)NULL);
756 }
757
758 /* Return the function (or macro) definition which would be invoked via
759 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
760 used. TYPE, if non-NULL, is a pointer to an int which will receive the
761 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
762 or ISMACR (macro). */
763 rl_command_func_t *
764 rl_function_of_keyseq (keyseq, map, type)
765 const char *keyseq;
766 Keymap map;
767 int *type;
768 {
769 register int i;
770
771 if (map == 0)
772 map = _rl_keymap;
773
774 for (i = 0; keyseq && keyseq[i]; i++)
775 {
776 unsigned char ic = keyseq[i];
777
778 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
779 {
780 if (map[ESC].type == ISKMAP)
781 {
782 map = FUNCTION_TO_KEYMAP (map, ESC);
783 ic = UNMETA (ic);
784 }
785 /* XXX - should we just return NULL here, since this obviously
786 doesn't match? */
787 else
788 {
789 if (type)
790 *type = map[ESC].type;
791
792 return (map[ESC].function);
793 }
794 }
795
796 if (map[ic].type == ISKMAP)
797 {
798 /* If this is the last key in the key sequence, return the
799 map. */
800 if (keyseq[i + 1] == '\0')
801 {
802 if (type)
803 *type = ISKMAP;
804
805 return (map[ic].function);
806 }
807 else
808 map = FUNCTION_TO_KEYMAP (map, ic);
809 }
810 /* If we're not at the end of the key sequence, and the current key
811 is bound to something other than a keymap, then the entire key
812 sequence is not bound. */
813 else if (map[ic].type != ISKMAP && keyseq[i+1])
814 return ((rl_command_func_t *)NULL);
815 else /* map[ic].type != ISKMAP && keyseq[i+1] == 0 */
816 {
817 if (type)
818 *type = map[ic].type;
819
820 return (map[ic].function);
821 }
822 }
823 return ((rl_command_func_t *) NULL);
824 }
825
826 /* The last key bindings file read. */
827 static char *last_readline_init_file = (char *)NULL;
828
829 /* The file we're currently reading key bindings from. */
830 static const char *current_readline_init_file;
831 static int current_readline_init_include_level;
832 static int current_readline_init_lineno;
833
834 /* Read FILENAME into a locally-allocated buffer and return the buffer.
835 The size of the buffer is returned in *SIZEP. Returns NULL if any
836 errors were encountered. */
837 static char *
838 _rl_read_file (filename, sizep)
839 char *filename;
840 size_t *sizep;
841 {
842 struct stat finfo;
843 size_t file_size;
844 char *buffer;
845 int i, file;
846
847 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
848 return ((char *)NULL);
849
850 file_size = (size_t)finfo.st_size;
851
852 /* check for overflow on very large files */
853 if (file_size != finfo.st_size || file_size + 1 < file_size)
854 {
855 if (file >= 0)
856 close (file);
857 #if defined (EFBIG)
858 errno = EFBIG;
859 #endif
860 return ((char *)NULL);
861 }
862
863 /* Read the file into BUFFER. */
864 buffer = (char *)xmalloc (file_size + 1);
865 i = read (file, buffer, file_size);
866 close (file);
867
868 if (i < 0)
869 {
870 xfree (buffer);
871 return ((char *)NULL);
872 }
873
874 RL_CHECK_SIGNALS ();
875
876 buffer[i] = '\0';
877 if (sizep)
878 *sizep = i;
879
880 return (buffer);
881 }
882
883 /* Re-read the current keybindings file. */
884 int
885 rl_re_read_init_file (count, ignore)
886 int count, ignore;
887 {
888 int r;
889 r = rl_read_init_file ((const char *)NULL);
890 rl_set_keymap_from_edit_mode ();
891 return r;
892 }
893
894 /* Do key bindings from a file. If FILENAME is NULL it defaults
895 to the first non-null filename from this list:
896 1. the filename used for the previous call
897 2. the value of the shell variable `INPUTRC'
898 3. ~/.inputrc
899 4. /etc/inputrc
900 If the file existed and could be opened and read, 0 is returned,
901 otherwise errno is returned. */
902 int
903 rl_read_init_file (filename)
904 const char *filename;
905 {
906 /* Default the filename. */
907 if (filename == 0)
908 filename = last_readline_init_file;
909 if (filename == 0)
910 filename = sh_get_env_value ("INPUTRC");
911 if (filename == 0 || *filename == 0)
912 {
913 filename = DEFAULT_INPUTRC;
914 /* Try to read DEFAULT_INPUTRC; fall back to SYS_INPUTRC on failure */
915 if (_rl_read_init_file (filename, 0) == 0)
916 return 0;
917 filename = SYS_INPUTRC;
918 }
919
920 #if defined (__MSDOS__)
921 if (_rl_read_init_file (filename, 0) == 0)
922 return 0;
923 filename = "~/_inputrc";
924 #endif
925 return (_rl_read_init_file (filename, 0));
926 }
927
928 static int
929 _rl_read_init_file (filename, include_level)
930 const char *filename;
931 int include_level;
932 {
933 register int i;
934 char *buffer, *openname, *line, *end;
935 size_t file_size;
936
937 current_readline_init_file = filename;
938 current_readline_init_include_level = include_level;
939
940 openname = tilde_expand (filename);
941 buffer = _rl_read_file (openname, &file_size);
942 xfree (openname);
943
944 RL_CHECK_SIGNALS ();
945 if (buffer == 0)
946 return (errno);
947
948 if (include_level == 0 && filename != last_readline_init_file)
949 {
950 FREE (last_readline_init_file);
951 last_readline_init_file = savestring (filename);
952 }
953
954 currently_reading_init_file = 1;
955
956 /* Loop over the lines in the file. Lines that start with `#' are
957 comments; all other lines are commands for readline initialization. */
958 current_readline_init_lineno = 1;
959 line = buffer;
960 end = buffer + file_size;
961 while (line < end)
962 {
963 /* Find the end of this line. */
964 for (i = 0; line + i != end && line[i] != '\n'; i++);
965
966 #if defined (__CYGWIN__)
967 /* ``Be liberal in what you accept.'' */
968 if (line[i] == '\n' && line[i-1] == '\r')
969 line[i - 1] = '\0';
970 #endif
971
972 /* Mark end of line. */
973 line[i] = '\0';
974
975 /* Skip leading whitespace. */
976 while (*line && whitespace (*line))
977 {
978 line++;
979 i--;
980 }
981
982 /* If the line is not a comment, then parse it. */
983 if (*line && *line != '#')
984 rl_parse_and_bind (line);
985
986 /* Move to the next line. */
987 line += i + 1;
988 current_readline_init_lineno++;
989 }
990
991 xfree (buffer);
992 currently_reading_init_file = 0;
993 return (0);
994 }
995
996 static void
997 #if defined (PREFER_STDARG)
998 _rl_init_file_error (const char *format, ...)
999 #else
1000 _rl_init_file_error (va_alist)
1001 va_dcl
1002 #endif
1003 {
1004 va_list args;
1005 #if defined (PREFER_VARARGS)
1006 char *format;
1007 #endif
1008
1009 #if defined (PREFER_STDARG)
1010 va_start (args, format);
1011 #else
1012 va_start (args);
1013 format = va_arg (args, char *);
1014 #endif
1015
1016 fprintf (stderr, "readline: ");
1017 if (currently_reading_init_file)
1018 fprintf (stderr, "%s: line %d: ", current_readline_init_file,
1019 current_readline_init_lineno);
1020
1021 vfprintf (stderr, format, args);
1022 fprintf (stderr, "\n");
1023 fflush (stderr);
1024
1025 va_end (args);
1026 }
1027
1028 /* **************************************************************** */
1029 /* */
1030 /* Parser Directives */
1031 /* */
1032 /* **************************************************************** */
1033
1034 typedef int _rl_parser_func_t PARAMS((char *));
1035
1036 /* Things that mean `Control'. */
1037 const char * const _rl_possible_control_prefixes[] = {
1038 "Control-", "C-", "CTRL-", (const char *)NULL
1039 };
1040
1041 const char * const _rl_possible_meta_prefixes[] = {
1042 "Meta", "M-", (const char *)NULL
1043 };
1044
1045 /* Conditionals. */
1046
1047 /* Calling programs set this to have their argv[0]. */
1048 const char *rl_readline_name = "other";
1049
1050 /* Stack of previous values of parsing_conditionalized_out. */
1051 static unsigned char *if_stack = (unsigned char *)NULL;
1052 static int if_stack_depth;
1053 static int if_stack_size;
1054
1055 /* Push _rl_parsing_conditionalized_out, and set parser state based
1056 on ARGS. */
1057 static int
1058 parser_if (args)
1059 char *args;
1060 {
1061 register int i;
1062
1063 /* Push parser state. */
1064 if (if_stack_depth + 1 >= if_stack_size)
1065 {
1066 if (!if_stack)
1067 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
1068 else
1069 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
1070 }
1071 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
1072
1073 /* If parsing is turned off, then nothing can turn it back on except
1074 for finding the matching endif. In that case, return right now. */
1075 if (_rl_parsing_conditionalized_out)
1076 return 0;
1077
1078 /* Isolate first argument. */
1079 for (i = 0; args[i] && !whitespace (args[i]); i++);
1080
1081 if (args[i])
1082 args[i++] = '\0';
1083
1084 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
1085 isn't term=foo, or mode=emacs, then check to see if the first
1086 word in ARGS is the same as the value stored in rl_readline_name. */
1087 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
1088 {
1089 char *tem, *tname;
1090
1091 /* Terminals like "aaa-60" are equivalent to "aaa". */
1092 tname = savestring (rl_terminal_name);
1093 tem = strchr (tname, '-');
1094 if (tem)
1095 *tem = '\0';
1096
1097 /* Test the `long' and `short' forms of the terminal name so that
1098 if someone has a `sun-cmd' and does not want to have bindings
1099 that will be executed if the terminal is a `sun', they can put
1100 `$if term=sun-cmd' into their .inputrc. */
1101 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
1102 _rl_stricmp (args + 5, rl_terminal_name);
1103 xfree (tname);
1104 }
1105 #if defined (VI_MODE)
1106 else if (_rl_strnicmp (args, "mode=", 5) == 0)
1107 {
1108 int mode;
1109
1110 if (_rl_stricmp (args + 5, "emacs") == 0)
1111 mode = emacs_mode;
1112 else if (_rl_stricmp (args + 5, "vi") == 0)
1113 mode = vi_mode;
1114 else
1115 mode = no_mode;
1116
1117 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
1118 }
1119 #endif /* VI_MODE */
1120 /* Check to see if the first word in ARGS is the same as the
1121 value stored in rl_readline_name. */
1122 else if (_rl_stricmp (args, rl_readline_name) == 0)
1123 _rl_parsing_conditionalized_out = 0;
1124 else
1125 _rl_parsing_conditionalized_out = 1;
1126 return 0;
1127 }
1128
1129 /* Invert the current parser state if there is anything on the stack. */
1130 static int
1131 parser_else (args)
1132 char *args;
1133 {
1134 register int i;
1135
1136 if (if_stack_depth == 0)
1137 {
1138 _rl_init_file_error ("$else found without matching $if");
1139 return 0;
1140 }
1141
1142 #if 0
1143 /* Check the previous (n - 1) levels of the stack to make sure that
1144 we haven't previously turned off parsing. */
1145 for (i = 0; i < if_stack_depth - 1; i++)
1146 #else
1147 /* Check the previous (n) levels of the stack to make sure that
1148 we haven't previously turned off parsing. */
1149 for (i = 0; i < if_stack_depth; i++)
1150 #endif
1151 if (if_stack[i] == 1)
1152 return 0;
1153
1154 /* Invert the state of parsing if at top level. */
1155 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
1156 return 0;
1157 }
1158
1159 /* Terminate a conditional, popping the value of
1160 _rl_parsing_conditionalized_out from the stack. */
1161 static int
1162 parser_endif (args)
1163 char *args;
1164 {
1165 if (if_stack_depth)
1166 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
1167 else
1168 _rl_init_file_error ("$endif without matching $if");
1169 return 0;
1170 }
1171
1172 static int
1173 parser_include (args)
1174 char *args;
1175 {
1176 const char *old_init_file;
1177 char *e;
1178 int old_line_number, old_include_level, r;
1179
1180 if (_rl_parsing_conditionalized_out)
1181 return (0);
1182
1183 old_init_file = current_readline_init_file;
1184 old_line_number = current_readline_init_lineno;
1185 old_include_level = current_readline_init_include_level;
1186
1187 e = strchr (args, '\n');
1188 if (e)
1189 *e = '\0';
1190 r = _rl_read_init_file ((const char *)args, old_include_level + 1);
1191
1192 current_readline_init_file = old_init_file;
1193 current_readline_init_lineno = old_line_number;
1194 current_readline_init_include_level = old_include_level;
1195
1196 return r;
1197 }
1198
1199 /* Associate textual names with actual functions. */
1200 static const struct {
1201 const char * const name;
1202 _rl_parser_func_t *function;
1203 } parser_directives [] = {
1204 { "if", parser_if },
1205 { "endif", parser_endif },
1206 { "else", parser_else },
1207 { "include", parser_include },
1208 { (char *)0x0, (_rl_parser_func_t *)0x0 }
1209 };
1210
1211 /* Handle a parser directive. STATEMENT is the line of the directive
1212 without any leading `$'. */
1213 static int
1214 handle_parser_directive (statement)
1215 char *statement;
1216 {
1217 register int i;
1218 char *directive, *args;
1219
1220 /* Isolate the actual directive. */
1221
1222 /* Skip whitespace. */
1223 for (i = 0; whitespace (statement[i]); i++);
1224
1225 directive = &statement[i];
1226
1227 for (; statement[i] && !whitespace (statement[i]); i++);
1228
1229 if (statement[i])
1230 statement[i++] = '\0';
1231
1232 for (; statement[i] && whitespace (statement[i]); i++);
1233
1234 args = &statement[i];
1235
1236 /* Lookup the command, and act on it. */
1237 for (i = 0; parser_directives[i].name; i++)
1238 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1239 {
1240 (*parser_directives[i].function) (args);
1241 return (0);
1242 }
1243
1244 /* display an error message about the unknown parser directive */
1245 _rl_init_file_error ("%s: unknown parser directive", directive);
1246 return (1);
1247 }
1248
1249 /* Start at STRING[START] and look for DELIM. Return I where STRING[I] ==
1250 DELIM or STRING[I] == 0. DELIM is usually a double quote. */
1251 static int
1252 _rl_skip_to_delim (string, start, delim)
1253 char *string;
1254 int start, delim;
1255 {
1256 int i, c, passc;
1257
1258 for (i = start,passc = 0; c = string[i]; i++)
1259 {
1260 if (passc)
1261 {
1262 passc = 0;
1263 if (c == 0)
1264 break;
1265 continue;
1266 }
1267
1268 if (c == '\\')
1269 {
1270 passc = 1;
1271 continue;
1272 }
1273
1274 if (c == delim)
1275 break;
1276 }
1277
1278 return i;
1279 }
1280
1281 /* Read the binding command from STRING and perform it.
1282 A key binding command looks like: Keyname: function-name\0,
1283 a variable binding command looks like: set variable value.
1284 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1285 int
1286 rl_parse_and_bind (string)
1287 char *string;
1288 {
1289 char *funname, *kname;
1290 register int c, i;
1291 int key, equivalency, foundmod, foundsep;
1292
1293 while (string && whitespace (*string))
1294 string++;
1295
1296 if (string == 0 || *string == 0 || *string == '#')
1297 return 0;
1298
1299 /* If this is a parser directive, act on it. */
1300 if (*string == '$')
1301 {
1302 handle_parser_directive (&string[1]);
1303 return 0;
1304 }
1305
1306 /* If we aren't supposed to be parsing right now, then we're done. */
1307 if (_rl_parsing_conditionalized_out)
1308 return 0;
1309
1310 i = 0;
1311 /* If this keyname is a complex key expression surrounded by quotes,
1312 advance to after the matching close quote. This code allows the
1313 backslash to quote characters in the key expression. */
1314 if (*string == '"')
1315 {
1316 i = _rl_skip_to_delim (string, 1, '"');
1317
1318 /* If we didn't find a closing quote, abort the line. */
1319 if (string[i] == '\0')
1320 {
1321 _rl_init_file_error ("%s: no closing `\"' in key binding", string);
1322 return 1;
1323 }
1324 else
1325 i++; /* skip past closing double quote */
1326 }
1327
1328 /* Advance to the colon (:) or whitespace which separates the two objects. */
1329 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1330
1331 equivalency = (c == ':' && string[i + 1] == '=');
1332
1333 foundsep = c != 0;
1334
1335 /* Mark the end of the command (or keyname). */
1336 if (string[i])
1337 string[i++] = '\0';
1338
1339 /* If doing assignment, skip the '=' sign as well. */
1340 if (equivalency)
1341 string[i++] = '\0';
1342
1343 /* If this is a command to set a variable, then do that. */
1344 if (_rl_stricmp (string, "set") == 0)
1345 {
1346 char *var, *value, *e;
1347 int s;
1348
1349 var = string + i;
1350 /* Make VAR point to start of variable name. */
1351 while (*var && whitespace (*var)) var++;
1352
1353 /* Make VALUE point to start of value string. */
1354 value = var;
1355 while (*value && whitespace (*value) == 0) value++;
1356 if (*value)
1357 *value++ = '\0';
1358 while (*value && whitespace (*value)) value++;
1359
1360 /* Strip trailing whitespace from values of boolean variables. */
1361 if (find_boolean_var (var) >= 0)
1362 {
1363 /* remove trailing whitespace */
1364 remove_trailing:
1365 e = value + strlen (value) - 1;
1366 while (e >= value && whitespace (*e))
1367 e--;
1368 e++; /* skip back to whitespace or EOS */
1369
1370 if (*e && e >= value)
1371 *e = '\0';
1372 }
1373 else if ((i = find_string_var (var)) >= 0)
1374 {
1375 /* Allow quoted strings in variable values */
1376 if (*value == '"')
1377 {
1378 i = _rl_skip_to_delim (value, 1, *value);
1379 value[i] = '\0';
1380 value++; /* skip past the quote */
1381 }
1382 else
1383 goto remove_trailing;
1384 }
1385
1386 rl_variable_bind (var, value);
1387 return 0;
1388 }
1389
1390 /* Skip any whitespace between keyname and funname. */
1391 for (; string[i] && whitespace (string[i]); i++);
1392 funname = &string[i];
1393
1394 /* Now isolate funname.
1395 For straight function names just look for whitespace, since
1396 that will signify the end of the string. But this could be a
1397 macro definition. In that case, the string is quoted, so skip
1398 to the matching delimiter. We allow the backslash to quote the
1399 delimiter characters in the macro body. */
1400 /* This code exists to allow whitespace in macro expansions, which
1401 would otherwise be gobbled up by the next `for' loop.*/
1402 /* XXX - it may be desirable to allow backslash quoting only if " is
1403 the quoted string delimiter, like the shell. */
1404 if (*funname == '\'' || *funname == '"')
1405 {
1406 i = _rl_skip_to_delim (string, i+1, *funname);
1407 if (string[i])
1408 i++;
1409 }
1410
1411 /* Advance to the end of the string. */
1412 for (; string[i] && whitespace (string[i]) == 0; i++);
1413
1414 /* No extra whitespace at the end of the string. */
1415 string[i] = '\0';
1416
1417 /* Handle equivalency bindings here. Make the left-hand side be exactly
1418 whatever the right-hand evaluates to, including keymaps. */
1419 if (equivalency)
1420 {
1421 return 0;
1422 }
1423
1424 if (foundsep == 0)
1425 {
1426 _rl_init_file_error ("%s: no key sequence terminator", string);
1427 return 1;
1428 }
1429
1430 /* If this is a new-style key-binding, then do the binding with
1431 rl_bind_keyseq (). Otherwise, let the older code deal with it. */
1432 if (*string == '"')
1433 {
1434 char *seq;
1435 register int j, k, passc;
1436
1437 seq = (char *)xmalloc (1 + strlen (string));
1438 for (j = 1, k = passc = 0; string[j]; j++)
1439 {
1440 /* Allow backslash to quote characters, but leave them in place.
1441 This allows a string to end with a backslash quoting another
1442 backslash, or with a backslash quoting a double quote. The
1443 backslashes are left in place for rl_translate_keyseq (). */
1444 if (passc || (string[j] == '\\'))
1445 {
1446 seq[k++] = string[j];
1447 passc = !passc;
1448 continue;
1449 }
1450
1451 if (string[j] == '"')
1452 break;
1453
1454 seq[k++] = string[j];
1455 }
1456 seq[k] = '\0';
1457
1458 /* Binding macro? */
1459 if (*funname == '\'' || *funname == '"')
1460 {
1461 j = strlen (funname);
1462
1463 /* Remove the delimiting quotes from each end of FUNNAME. */
1464 if (j && funname[j - 1] == *funname)
1465 funname[j - 1] = '\0';
1466
1467 rl_macro_bind (seq, &funname[1], _rl_keymap);
1468 }
1469 else
1470 rl_bind_keyseq (seq, rl_named_function (funname));
1471
1472 xfree (seq);
1473 return 0;
1474 }
1475
1476 /* Get the actual character we want to deal with. */
1477 kname = strrchr (string, '-');
1478 if (kname == 0)
1479 kname = string;
1480 else
1481 kname++;
1482
1483 key = glean_key_from_name (kname);
1484
1485 /* Add in control and meta bits. */
1486 foundmod = 0;
1487 if (substring_member_of_array (string, _rl_possible_control_prefixes))
1488 {
1489 key = CTRL (_rl_to_upper (key));
1490 foundmod = 1;
1491 }
1492
1493 if (substring_member_of_array (string, _rl_possible_meta_prefixes))
1494 {
1495 key = META (key);
1496 foundmod = 1;
1497 }
1498
1499 if (foundmod == 0 && kname != string)
1500 {
1501 _rl_init_file_error ("%s: unknown key modifier", string);
1502 return 1;
1503 }
1504
1505 /* Temporary. Handle old-style keyname with macro-binding. */
1506 if (*funname == '\'' || *funname == '"')
1507 {
1508 char useq[2];
1509 int fl = strlen (funname);
1510
1511 useq[0] = key; useq[1] = '\0';
1512 if (fl && funname[fl - 1] == *funname)
1513 funname[fl - 1] = '\0';
1514
1515 rl_macro_bind (useq, &funname[1], _rl_keymap);
1516 }
1517 #if defined (PREFIX_META_HACK)
1518 /* Ugly, but working hack to keep prefix-meta around. */
1519 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1520 {
1521 char seq[2];
1522
1523 seq[0] = key;
1524 seq[1] = '\0';
1525 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1526 }
1527 #endif /* PREFIX_META_HACK */
1528 else
1529 rl_bind_key (key, rl_named_function (funname));
1530
1531 return 0;
1532 }
1533
1534 /* Simple structure for boolean readline variables (i.e., those that can
1535 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1536 false. */
1537
1538 #define V_SPECIAL 0x1
1539
1540 static const struct {
1541 const char * const name;
1542 int *value;
1543 int flags;
1544 } boolean_varlist [] = {
1545 { "bind-tty-special-chars", &_rl_bind_stty_chars, 0 },
1546 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL },
1547 { "byte-oriented", &rl_byte_oriented, 0 },
1548 #if defined (COLOR_SUPPORT)
1549 { "colored-completion-prefix",&_rl_colored_completion_prefix, 0 },
1550 { "colored-stats", &_rl_colored_stats, 0 },
1551 #endif
1552 { "completion-ignore-case", &_rl_completion_case_fold, 0 },
1553 { "completion-map-case", &_rl_completion_case_map, 0 },
1554 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 },
1555 { "disable-completion", &rl_inhibit_completion, 0 },
1556 { "echo-control-characters", &_rl_echo_control_chars, 0 },
1557 { "enable-bracketed-paste", &_rl_enable_bracketed_paste, 0 },
1558 { "enable-keypad", &_rl_enable_keypad, 0 },
1559 { "enable-meta-key", &_rl_enable_meta, 0 },
1560 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 },
1561 { "history-preserve-point", &_rl_history_preserve_point, 0 },
1562 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 },
1563 { "input-meta", &_rl_meta_flag, 0 },
1564 { "mark-directories", &_rl_complete_mark_directories, 0 },
1565 { "mark-modified-lines", &_rl_mark_modified_lines, 0 },
1566 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 },
1567 { "match-hidden-files", &_rl_match_hidden_files, 0 },
1568 { "menu-complete-display-prefix", &_rl_menu_complete_prefix_first, 0 },
1569 { "meta-flag", &_rl_meta_flag, 0 },
1570 { "output-meta", &_rl_output_meta_chars, 0 },
1571 { "page-completions", &_rl_page_completions, 0 },
1572 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL },
1573 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1574 { "revert-all-at-newline", &_rl_revert_all_at_newline, 0 },
1575 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 },
1576 { "show-all-if-unmodified", &_rl_complete_show_unmodified, 0 },
1577 { "show-mode-in-prompt", &_rl_show_mode_in_prompt, 0 },
1578 { "skip-completed-text", &_rl_skip_completed_text, 0 },
1579 #if defined (VISIBLE_STATS)
1580 { "visible-stats", &rl_visible_stats, 0 },
1581 #endif /* VISIBLE_STATS */
1582 { (char *)NULL, (int *)NULL, 0 }
1583 };
1584
1585 static int
1586 find_boolean_var (name)
1587 const char *name;
1588 {
1589 register int i;
1590
1591 for (i = 0; boolean_varlist[i].name; i++)
1592 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1593 return i;
1594 return -1;
1595 }
1596
1597 /* Hooks for handling special boolean variables, where a
1598 function needs to be called or another variable needs
1599 to be changed when they're changed. */
1600 static void
1601 hack_special_boolean_var (i)
1602 int i;
1603 {
1604 const char *name;
1605
1606 name = boolean_varlist[i].name;
1607
1608 if (_rl_stricmp (name, "blink-matching-paren") == 0)
1609 _rl_enable_paren_matching (rl_blink_matching_paren);
1610 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1611 {
1612 if (_rl_prefer_visible_bell)
1613 _rl_bell_preference = VISIBLE_BELL;
1614 else
1615 _rl_bell_preference = AUDIBLE_BELL;
1616 }
1617 else if (_rl_stricmp (name, "show-mode-in-prompt") == 0)
1618 _rl_reset_prompt ();
1619 }
1620
1621 typedef int _rl_sv_func_t PARAMS((const char *));
1622
1623 /* These *must* correspond to the array indices for the appropriate
1624 string variable. (Though they're not used right now.) */
1625 #define V_BELLSTYLE 0
1626 #define V_COMBEGIN 1
1627 #define V_EDITMODE 2
1628 #define V_ISRCHTERM 3
1629 #define V_KEYMAP 4
1630
1631 #define V_STRING 1
1632 #define V_INT 2
1633
1634 /* Forward declarations */
1635 static int sv_bell_style PARAMS((const char *));
1636 static int sv_combegin PARAMS((const char *));
1637 static int sv_dispprefix PARAMS((const char *));
1638 static int sv_compquery PARAMS((const char *));
1639 static int sv_compwidth PARAMS((const char *));
1640 static int sv_editmode PARAMS((const char *));
1641 static int sv_emacs_modestr PARAMS((const char *));
1642 static int sv_histsize PARAMS((const char *));
1643 static int sv_isrchterm PARAMS((const char *));
1644 static int sv_keymap PARAMS((const char *));
1645 static int sv_seqtimeout PARAMS((const char *));
1646 static int sv_viins_modestr PARAMS((const char *));
1647 static int sv_vicmd_modestr PARAMS((const char *));
1648
1649 static const struct {
1650 const char * const name;
1651 int flags;
1652 _rl_sv_func_t *set_func;
1653 } string_varlist[] = {
1654 { "bell-style", V_STRING, sv_bell_style },
1655 { "comment-begin", V_STRING, sv_combegin },
1656 { "completion-display-width", V_INT, sv_compwidth },
1657 { "completion-prefix-display-length", V_INT, sv_dispprefix },
1658 { "completion-query-items", V_INT, sv_compquery },
1659 { "editing-mode", V_STRING, sv_editmode },
1660 { "emacs-mode-string", V_STRING, sv_emacs_modestr },
1661 { "history-size", V_INT, sv_histsize },
1662 { "isearch-terminators", V_STRING, sv_isrchterm },
1663 { "keymap", V_STRING, sv_keymap },
1664 { "keyseq-timeout", V_INT, sv_seqtimeout },
1665 { "vi-cmd-mode-string", V_STRING, sv_vicmd_modestr },
1666 { "vi-ins-mode-string", V_STRING, sv_viins_modestr },
1667 { (char *)NULL, 0, (_rl_sv_func_t *)0 }
1668 };
1669
1670 static int
1671 find_string_var (name)
1672 const char *name;
1673 {
1674 register int i;
1675
1676 for (i = 0; string_varlist[i].name; i++)
1677 if (_rl_stricmp (name, string_varlist[i].name) == 0)
1678 return i;
1679 return -1;
1680 }
1681
1682 /* A boolean value that can appear in a `set variable' command is true if
1683 the value is null or empty, `on' (case-insensitive), or "1". Any other
1684 values result in 0 (false). */
1685 static int
1686 bool_to_int (value)
1687 const char *value;
1688 {
1689 return (value == 0 || *value == '\0' ||
1690 (_rl_stricmp (value, "on") == 0) ||
1691 (value[0] == '1' && value[1] == '\0'));
1692 }
1693
1694 char *
1695 rl_variable_value (name)
1696 const char *name;
1697 {
1698 register int i;
1699
1700 /* Check for simple variables first. */
1701 i = find_boolean_var (name);
1702 if (i >= 0)
1703 return (*boolean_varlist[i].value ? "on" : "off");
1704
1705 i = find_string_var (name);
1706 if (i >= 0)
1707 return (_rl_get_string_variable_value (string_varlist[i].name));
1708
1709 /* Unknown variable names return NULL. */
1710 return 0;
1711 }
1712
1713 int
1714 rl_variable_bind (name, value)
1715 const char *name, *value;
1716 {
1717 register int i;
1718 int v;
1719
1720 /* Check for simple variables first. */
1721 i = find_boolean_var (name);
1722 if (i >= 0)
1723 {
1724 *boolean_varlist[i].value = bool_to_int (value);
1725 if (boolean_varlist[i].flags & V_SPECIAL)
1726 hack_special_boolean_var (i);
1727 return 0;
1728 }
1729
1730 i = find_string_var (name);
1731
1732 /* For the time being, string names without a handler function are simply
1733 ignored. */
1734 if (i < 0 || string_varlist[i].set_func == 0)
1735 {
1736 if (i < 0)
1737 _rl_init_file_error ("%s: unknown variable name", name);
1738 return 0;
1739 }
1740
1741 v = (*string_varlist[i].set_func) (value);
1742 return v;
1743 }
1744
1745 static int
1746 sv_editmode (value)
1747 const char *value;
1748 {
1749 if (_rl_strnicmp (value, "vi", 2) == 0)
1750 {
1751 #if defined (VI_MODE)
1752 _rl_keymap = vi_insertion_keymap;
1753 rl_editing_mode = vi_mode;
1754 #endif /* VI_MODE */
1755 return 0;
1756 }
1757 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1758 {
1759 _rl_keymap = emacs_standard_keymap;
1760 rl_editing_mode = emacs_mode;
1761 return 0;
1762 }
1763 return 1;
1764 }
1765
1766 static int
1767 sv_combegin (value)
1768 const char *value;
1769 {
1770 if (value && *value)
1771 {
1772 FREE (_rl_comment_begin);
1773 _rl_comment_begin = savestring (value);
1774 return 0;
1775 }
1776 return 1;
1777 }
1778
1779 static int
1780 sv_dispprefix (value)
1781 const char *value;
1782 {
1783 int nval = 0;
1784
1785 if (value && *value)
1786 {
1787 nval = atoi (value);
1788 if (nval < 0)
1789 nval = 0;
1790 }
1791 _rl_completion_prefix_display_length = nval;
1792 return 0;
1793 }
1794
1795 static int
1796 sv_compquery (value)
1797 const char *value;
1798 {
1799 int nval = 100;
1800
1801 if (value && *value)
1802 {
1803 nval = atoi (value);
1804 if (nval < 0)
1805 nval = 0;
1806 }
1807 rl_completion_query_items = nval;
1808 return 0;
1809 }
1810
1811 static int
1812 sv_compwidth (value)
1813 const char *value;
1814 {
1815 int nval = -1;
1816
1817 if (value && *value)
1818 nval = atoi (value);
1819
1820 _rl_completion_columns = nval;
1821 return 0;
1822 }
1823
1824 static int
1825 sv_histsize (value)
1826 const char *value;
1827 {
1828 int nval;
1829
1830 nval = 500;
1831 if (value && *value)
1832 {
1833 nval = atoi (value);
1834 if (nval < 0)
1835 {
1836 unstifle_history ();
1837 return 0;
1838 }
1839 }
1840 stifle_history (nval);
1841 return 0;
1842 }
1843
1844 static int
1845 sv_keymap (value)
1846 const char *value;
1847 {
1848 Keymap kmap;
1849
1850 kmap = rl_get_keymap_by_name (value);
1851 if (kmap)
1852 {
1853 rl_set_keymap (kmap);
1854 return 0;
1855 }
1856 return 1;
1857 }
1858
1859 static int
1860 sv_seqtimeout (value)
1861 const char *value;
1862 {
1863 int nval;
1864
1865 nval = 0;
1866 if (value && *value)
1867 {
1868 nval = atoi (value);
1869 if (nval < 0)
1870 nval = 0;
1871 }
1872 _rl_keyseq_timeout = nval;
1873 return 0;
1874 }
1875
1876 static int
1877 sv_bell_style (value)
1878 const char *value;
1879 {
1880 if (value == 0 || *value == '\0')
1881 _rl_bell_preference = AUDIBLE_BELL;
1882 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1883 _rl_bell_preference = NO_BELL;
1884 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1885 _rl_bell_preference = AUDIBLE_BELL;
1886 else if (_rl_stricmp (value, "visible") == 0)
1887 _rl_bell_preference = VISIBLE_BELL;
1888 else
1889 return 1;
1890 return 0;
1891 }
1892
1893 static int
1894 sv_isrchterm (value)
1895 const char *value;
1896 {
1897 int beg, end, delim;
1898 char *v;
1899
1900 if (value == 0)
1901 return 1;
1902
1903 /* Isolate the value and translate it into a character string. */
1904 v = savestring (value);
1905 FREE (_rl_isearch_terminators);
1906 if (v[0] == '"' || v[0] == '\'')
1907 {
1908 delim = v[0];
1909 for (beg = end = 1; v[end] && v[end] != delim; end++)
1910 ;
1911 }
1912 else
1913 {
1914 for (beg = end = 0; v[end] && whitespace (v[end]) == 0; end++)
1915 ;
1916 }
1917
1918 v[end] = '\0';
1919
1920 /* The value starts at v + beg. Translate it into a character string. */
1921 _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1);
1922 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1923 _rl_isearch_terminators[end] = '\0';
1924
1925 xfree (v);
1926 return 0;
1927 }
1928
1929 extern char *_rl_emacs_mode_str;
1930
1931 static int
1932 sv_emacs_modestr (value)
1933 const char *value;
1934 {
1935 if (value && *value)
1936 {
1937 FREE (_rl_emacs_mode_str);
1938 _rl_emacs_mode_str = (char *)xmalloc (2 * strlen (value) + 1);
1939 rl_translate_keyseq (value, _rl_emacs_mode_str, &_rl_emacs_modestr_len);
1940 _rl_emacs_mode_str[_rl_emacs_modestr_len] = '\0';
1941 return 0;
1942 }
1943 else if (value)
1944 {
1945 FREE (_rl_emacs_mode_str);
1946 _rl_emacs_mode_str = (char *)xmalloc (1);
1947 _rl_emacs_mode_str[_rl_emacs_modestr_len = 0] = '\0';
1948 return 0;
1949 }
1950 else if (value == 0)
1951 {
1952 FREE (_rl_emacs_mode_str);
1953 _rl_emacs_mode_str = 0; /* prompt_modestr does the right thing */
1954 _rl_emacs_modestr_len = 0;
1955 return 0;
1956 }
1957 return 1;
1958 }
1959
1960 static int
1961 sv_viins_modestr (value)
1962 const char *value;
1963 {
1964 if (value && *value)
1965 {
1966 FREE (_rl_vi_ins_mode_str);
1967 _rl_vi_ins_mode_str = (char *)xmalloc (2 * strlen (value) + 1);
1968 rl_translate_keyseq (value, _rl_vi_ins_mode_str, &_rl_vi_ins_modestr_len);
1969 _rl_vi_ins_mode_str[_rl_vi_ins_modestr_len] = '\0';
1970 return 0;
1971 }
1972 else if (value)
1973 {
1974 FREE (_rl_vi_ins_mode_str);
1975 _rl_vi_ins_mode_str = (char *)xmalloc (1);
1976 _rl_vi_ins_mode_str[_rl_vi_ins_modestr_len = 0] = '\0';
1977 return 0;
1978 }
1979 else if (value == 0)
1980 {
1981 FREE (_rl_vi_ins_mode_str);
1982 _rl_vi_ins_mode_str = 0; /* prompt_modestr does the right thing */
1983 _rl_vi_ins_modestr_len = 0;
1984 return 0;
1985 }
1986 return 1;
1987 }
1988
1989 static int
1990 sv_vicmd_modestr (value)
1991 const char *value;
1992 {
1993 if (value && *value)
1994 {
1995 FREE (_rl_vi_cmd_mode_str);
1996 _rl_vi_cmd_mode_str = (char *)xmalloc (2 * strlen (value) + 1);
1997 rl_translate_keyseq (value, _rl_vi_cmd_mode_str, &_rl_vi_cmd_modestr_len);
1998 _rl_vi_cmd_mode_str[_rl_vi_cmd_modestr_len] = '\0';
1999 return 0;
2000 }
2001 else if (value)
2002 {
2003 FREE (_rl_vi_cmd_mode_str);
2004 _rl_vi_cmd_mode_str = (char *)xmalloc (1);
2005 _rl_vi_cmd_mode_str[_rl_vi_cmd_modestr_len = 0] = '\0';
2006 return 0;
2007 }
2008 else if (value == 0)
2009 {
2010 FREE (_rl_vi_cmd_mode_str);
2011 _rl_vi_cmd_mode_str = 0; /* prompt_modestr does the right thing */
2012 _rl_vi_cmd_modestr_len = 0;
2013 return 0;
2014 }
2015 return 1;
2016 }
2017
2018 /* Return the character which matches NAME.
2019 For example, `Space' returns ' '. */
2020
2021 typedef struct {
2022 const char * const name;
2023 int value;
2024 } assoc_list;
2025
2026 static const assoc_list name_key_alist[] = {
2027 { "DEL", 0x7f },
2028 { "ESC", '\033' },
2029 { "Escape", '\033' },
2030 { "LFD", '\n' },
2031 { "Newline", '\n' },
2032 { "RET", '\r' },
2033 { "Return", '\r' },
2034 { "Rubout", 0x7f },
2035 { "SPC", ' ' },
2036 { "Space", ' ' },
2037 { "Tab", 0x09 },
2038 { (char *)0x0, 0 }
2039 };
2040
2041 static int
2042 glean_key_from_name (name)
2043 char *name;
2044 {
2045 register int i;
2046
2047 for (i = 0; name_key_alist[i].name; i++)
2048 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
2049 return (name_key_alist[i].value);
2050
2051 return (*(unsigned char *)name); /* XXX was return (*name) */
2052 }
2053
2054 /* Auxiliary functions to manage keymaps. */
2055 static const struct {
2056 const char * const name;
2057 Keymap map;
2058 } keymap_names[] = {
2059 { "emacs", emacs_standard_keymap },
2060 { "emacs-standard", emacs_standard_keymap },
2061 { "emacs-meta", emacs_meta_keymap },
2062 { "emacs-ctlx", emacs_ctlx_keymap },
2063 #if defined (VI_MODE)
2064 { "vi", vi_movement_keymap },
2065 { "vi-move", vi_movement_keymap },
2066 { "vi-command", vi_movement_keymap },
2067 { "vi-insert", vi_insertion_keymap },
2068 #endif /* VI_MODE */
2069 { (char *)0x0, (Keymap)0x0 }
2070 };
2071
2072 Keymap
2073 rl_get_keymap_by_name (name)
2074 const char *name;
2075 {
2076 register int i;
2077
2078 for (i = 0; keymap_names[i].name; i++)
2079 if (_rl_stricmp (name, keymap_names[i].name) == 0)
2080 return (keymap_names[i].map);
2081 return ((Keymap) NULL);
2082 }
2083
2084 char *
2085 rl_get_keymap_name (map)
2086 Keymap map;
2087 {
2088 register int i;
2089 for (i = 0; keymap_names[i].name; i++)
2090 if (map == keymap_names[i].map)
2091 return ((char *)keymap_names[i].name);
2092 return ((char *)NULL);
2093 }
2094
2095 void
2096 rl_set_keymap (map)
2097 Keymap map;
2098 {
2099 if (map)
2100 _rl_keymap = map;
2101 }
2102
2103 Keymap
2104 rl_get_keymap ()
2105 {
2106 return (_rl_keymap);
2107 }
2108
2109 void
2110 rl_set_keymap_from_edit_mode ()
2111 {
2112 if (rl_editing_mode == emacs_mode)
2113 _rl_keymap = emacs_standard_keymap;
2114 #if defined (VI_MODE)
2115 else if (rl_editing_mode == vi_mode)
2116 _rl_keymap = vi_insertion_keymap;
2117 #endif /* VI_MODE */
2118 }
2119
2120 char *
2121 rl_get_keymap_name_from_edit_mode ()
2122 {
2123 if (rl_editing_mode == emacs_mode)
2124 return "emacs";
2125 #if defined (VI_MODE)
2126 else if (rl_editing_mode == vi_mode)
2127 return "vi";
2128 #endif /* VI_MODE */
2129 else
2130 return "none";
2131 }
2132
2133 /* **************************************************************** */
2134 /* */
2135 /* Key Binding and Function Information */
2136 /* */
2137 /* **************************************************************** */
2138
2139 /* Each of the following functions produces information about the
2140 state of keybindings and functions known to Readline. The info
2141 is always printed to rl_outstream, and in such a way that it can
2142 be read back in (i.e., passed to rl_parse_and_bind ()). */
2143
2144 /* Print the names of functions known to Readline. */
2145 void
2146 rl_list_funmap_names ()
2147 {
2148 register int i;
2149 const char **funmap_names;
2150
2151 funmap_names = rl_funmap_names ();
2152
2153 if (!funmap_names)
2154 return;
2155
2156 for (i = 0; funmap_names[i]; i++)
2157 fprintf (rl_outstream, "%s\n", funmap_names[i]);
2158
2159 xfree (funmap_names);
2160 }
2161
2162 static char *
2163 _rl_get_keyname (key)
2164 int key;
2165 {
2166 char *keyname;
2167 int i, c;
2168
2169 keyname = (char *)xmalloc (8);
2170
2171 c = key;
2172 /* Since this is going to be used to write out keysequence-function
2173 pairs for possible inclusion in an inputrc file, we don't want to
2174 do any special meta processing on KEY. */
2175
2176 #if 1
2177 /* XXX - Experimental */
2178 /* We might want to do this, but the old version of the code did not. */
2179
2180 /* If this is an escape character, we don't want to do any more processing.
2181 Just add the special ESC key sequence and return. */
2182 if (c == ESC)
2183 {
2184 keyname[0] = '\\';
2185 keyname[1] = 'e';
2186 keyname[2] = '\0';
2187 return keyname;
2188 }
2189 #endif
2190
2191 /* RUBOUT is translated directly into \C-? */
2192 if (key == RUBOUT)
2193 {
2194 keyname[0] = '\\';
2195 keyname[1] = 'C';
2196 keyname[2] = '-';
2197 keyname[3] = '?';
2198 keyname[4] = '\0';
2199 return keyname;
2200 }
2201
2202 i = 0;
2203 /* Now add special prefixes needed for control characters. This can
2204 potentially change C. */
2205 if (CTRL_CHAR (c))
2206 {
2207 keyname[i++] = '\\';
2208 keyname[i++] = 'C';
2209 keyname[i++] = '-';
2210 c = _rl_to_lower (UNCTRL (c));
2211 }
2212
2213 /* XXX experimental code. Turn the characters that are not ASCII or
2214 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
2215 This changes C. */
2216 if (c >= 128 && c <= 159)
2217 {
2218 keyname[i++] = '\\';
2219 keyname[i++] = '2';
2220 c -= 128;
2221 keyname[i++] = (c / 8) + '0';
2222 c = (c % 8) + '0';
2223 }
2224
2225 /* Now, if the character needs to be quoted with a backslash, do that. */
2226 if (c == '\\' || c == '"')
2227 keyname[i++] = '\\';
2228
2229 /* Now add the key, terminate the string, and return it. */
2230 keyname[i++] = (char) c;
2231 keyname[i] = '\0';
2232
2233 return keyname;
2234 }
2235
2236 /* Return a NULL terminated array of strings which represent the key
2237 sequences that are used to invoke FUNCTION in MAP. */
2238 char **
2239 rl_invoking_keyseqs_in_map (function, map)
2240 rl_command_func_t *function;
2241 Keymap map;
2242 {
2243 register int key;
2244 char **result;
2245 int result_index, result_size;
2246
2247 result = (char **)NULL;
2248 result_index = result_size = 0;
2249
2250 for (key = 0; key < KEYMAP_SIZE; key++)
2251 {
2252 switch (map[key].type)
2253 {
2254 case ISMACR:
2255 /* Macros match, if, and only if, the pointers are identical.
2256 Thus, they are treated exactly like functions in here. */
2257 case ISFUNC:
2258 /* If the function in the keymap is the one we are looking for,
2259 then add the current KEY to the list of invoking keys. */
2260 if (map[key].function == function)
2261 {
2262 char *keyname;
2263
2264 keyname = _rl_get_keyname (key);
2265
2266 if (result_index + 2 > result_size)
2267 {
2268 result_size += 10;
2269 result = (char **)xrealloc (result, result_size * sizeof (char *));
2270 }
2271
2272 result[result_index++] = keyname;
2273 result[result_index] = (char *)NULL;
2274 }
2275 break;
2276
2277 case ISKMAP:
2278 {
2279 char **seqs;
2280 register int i;
2281
2282 /* Find the list of keyseqs in this map which have FUNCTION as
2283 their target. Add the key sequences found to RESULT. */
2284 if (map[key].function)
2285 seqs =
2286 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
2287 else
2288 break;
2289
2290 if (seqs == 0)
2291 break;
2292
2293 for (i = 0; seqs[i]; i++)
2294 {
2295 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
2296
2297 if (key == ESC)
2298 {
2299 /* If ESC is the meta prefix and we're converting chars
2300 with the eighth bit set to ESC-prefixed sequences, then
2301 we can use \M-. Otherwise we need to use the sequence
2302 for ESC. */
2303 if (_rl_convert_meta_chars_to_ascii && map[ESC].type == ISKMAP)
2304 sprintf (keyname, "\\M-");
2305 else
2306 sprintf (keyname, "\\e");
2307 }
2308 else if (CTRL_CHAR (key))
2309 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
2310 else if (key == RUBOUT)
2311 sprintf (keyname, "\\C-?");
2312 else if (key == '\\' || key == '"')
2313 {
2314 keyname[0] = '\\';
2315 keyname[1] = (char) key;
2316 keyname[2] = '\0';
2317 }
2318 else
2319 {
2320 keyname[0] = (char) key;
2321 keyname[1] = '\0';
2322 }
2323
2324 strcat (keyname, seqs[i]);
2325 xfree (seqs[i]);
2326
2327 if (result_index + 2 > result_size)
2328 {
2329 result_size += 10;
2330 result = (char **)xrealloc (result, result_size * sizeof (char *));
2331 }
2332
2333 result[result_index++] = keyname;
2334 result[result_index] = (char *)NULL;
2335 }
2336
2337 xfree (seqs);
2338 }
2339 break;
2340 }
2341 }
2342 return (result);
2343 }
2344
2345 /* Return a NULL terminated array of strings which represent the key
2346 sequences that can be used to invoke FUNCTION using the current keymap. */
2347 char **
2348 rl_invoking_keyseqs (function)
2349 rl_command_func_t *function;
2350 {
2351 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
2352 }
2353
2354 /* Print all of the functions and their bindings to rl_outstream. If
2355 PRINT_READABLY is non-zero, then print the output in such a way
2356 that it can be read back in. */
2357 void
2358 rl_function_dumper (print_readably)
2359 int print_readably;
2360 {
2361 register int i;
2362 const char **names;
2363 const char *name;
2364
2365 names = rl_funmap_names ();
2366
2367 fprintf (rl_outstream, "\n");
2368
2369 for (i = 0; name = names[i]; i++)
2370 {
2371 rl_command_func_t *function;
2372 char **invokers;
2373
2374 function = rl_named_function (name);
2375 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
2376
2377 if (print_readably)
2378 {
2379 if (!invokers)
2380 fprintf (rl_outstream, "# %s (not bound)\n", name);
2381 else
2382 {
2383 register int j;
2384
2385 for (j = 0; invokers[j]; j++)
2386 {
2387 fprintf (rl_outstream, "\"%s\": %s\n",
2388 invokers[j], name);
2389 xfree (invokers[j]);
2390 }
2391
2392 xfree (invokers);
2393 }
2394 }
2395 else
2396 {
2397 if (!invokers)
2398 fprintf (rl_outstream, "%s is not bound to any keys\n",
2399 name);
2400 else
2401 {
2402 register int j;
2403
2404 fprintf (rl_outstream, "%s can be found on ", name);
2405
2406 for (j = 0; invokers[j] && j < 5; j++)
2407 {
2408 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
2409 invokers[j + 1] ? ", " : ".\n");
2410 }
2411
2412 if (j == 5 && invokers[j])
2413 fprintf (rl_outstream, "...\n");
2414
2415 for (j = 0; invokers[j]; j++)
2416 xfree (invokers[j]);
2417
2418 xfree (invokers);
2419 }
2420 }
2421 }
2422
2423 xfree (names);
2424 }
2425
2426 /* Print all of the current functions and their bindings to
2427 rl_outstream. If an explicit argument is given, then print
2428 the output in such a way that it can be read back in. */
2429 int
2430 rl_dump_functions (count, key)
2431 int count, key;
2432 {
2433 if (rl_dispatching)
2434 fprintf (rl_outstream, "\r\n");
2435 rl_function_dumper (rl_explicit_arg);
2436 rl_on_new_line ();
2437 return (0);
2438 }
2439
2440 static void
2441 _rl_macro_dumper_internal (print_readably, map, prefix)
2442 int print_readably;
2443 Keymap map;
2444 char *prefix;
2445 {
2446 register int key;
2447 char *keyname, *out;
2448 int prefix_len;
2449
2450 for (key = 0; key < KEYMAP_SIZE; key++)
2451 {
2452 switch (map[key].type)
2453 {
2454 case ISMACR:
2455 keyname = _rl_get_keyname (key);
2456 out = _rl_untranslate_macro_value ((char *)map[key].function, 0);
2457
2458 if (print_readably)
2459 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
2460 keyname,
2461 out ? out : "");
2462 else
2463 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
2464 keyname,
2465 out ? out : "");
2466 xfree (keyname);
2467 xfree (out);
2468 break;
2469 case ISFUNC:
2470 break;
2471 case ISKMAP:
2472 prefix_len = prefix ? strlen (prefix) : 0;
2473 if (key == ESC)
2474 {
2475 keyname = (char *)xmalloc (3 + prefix_len);
2476 if (prefix)
2477 strcpy (keyname, prefix);
2478 keyname[prefix_len] = '\\';
2479 keyname[prefix_len + 1] = 'e';
2480 keyname[prefix_len + 2] = '\0';
2481 }
2482 else
2483 {
2484 keyname = _rl_get_keyname (key);
2485 if (prefix)
2486 {
2487 out = (char *)xmalloc (strlen (keyname) + prefix_len + 1);
2488 strcpy (out, prefix);
2489 strcpy (out + prefix_len, keyname);
2490 xfree (keyname);
2491 keyname = out;
2492 }
2493 }
2494
2495 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
2496 xfree (keyname);
2497 break;
2498 }
2499 }
2500 }
2501
2502 void
2503 rl_macro_dumper (print_readably)
2504 int print_readably;
2505 {
2506 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
2507 }
2508
2509 int
2510 rl_dump_macros (count, key)
2511 int count, key;
2512 {
2513 if (rl_dispatching)
2514 fprintf (rl_outstream, "\r\n");
2515 rl_macro_dumper (rl_explicit_arg);
2516 rl_on_new_line ();
2517 return (0);
2518 }
2519
2520 static char *
2521 _rl_get_string_variable_value (name)
2522 const char *name;
2523 {
2524 static char numbuf[32];
2525 char *ret;
2526
2527 if (_rl_stricmp (name, "bell-style") == 0)
2528 {
2529 switch (_rl_bell_preference)
2530 {
2531 case NO_BELL:
2532 return "none";
2533 case VISIBLE_BELL:
2534 return "visible";
2535 case AUDIBLE_BELL:
2536 default:
2537 return "audible";
2538 }
2539 }
2540 else if (_rl_stricmp (name, "comment-begin") == 0)
2541 return (_rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2542 else if (_rl_stricmp (name, "completion-display-width") == 0)
2543 {
2544 sprintf (numbuf, "%d", _rl_completion_columns);
2545 return (numbuf);
2546 }
2547 else if (_rl_stricmp (name, "completion-prefix-display-length") == 0)
2548 {
2549 sprintf (numbuf, "%d", _rl_completion_prefix_display_length);
2550 return (numbuf);
2551 }
2552 else if (_rl_stricmp (name, "completion-query-items") == 0)
2553 {
2554 sprintf (numbuf, "%d", rl_completion_query_items);
2555 return (numbuf);
2556 }
2557 else if (_rl_stricmp (name, "editing-mode") == 0)
2558 return (rl_get_keymap_name_from_edit_mode ());
2559 else if (_rl_stricmp (name, "history-size") == 0)
2560 {
2561 sprintf (numbuf, "%d", history_is_stifled() ? history_max_entries : 0);
2562 return (numbuf);
2563 }
2564 else if (_rl_stricmp (name, "isearch-terminators") == 0)
2565 {
2566 if (_rl_isearch_terminators == 0)
2567 return 0;
2568 ret = _rl_untranslate_macro_value (_rl_isearch_terminators, 0);
2569 if (ret)
2570 {
2571 strncpy (numbuf, ret, sizeof (numbuf) - 1);
2572 xfree (ret);
2573 numbuf[sizeof(numbuf) - 1] = '\0';
2574 }
2575 else
2576 numbuf[0] = '\0';
2577 return numbuf;
2578 }
2579 else if (_rl_stricmp (name, "keymap") == 0)
2580 {
2581 ret = rl_get_keymap_name (_rl_keymap);
2582 if (ret == 0)
2583 ret = rl_get_keymap_name_from_edit_mode ();
2584 return (ret ? ret : "none");
2585 }
2586 else if (_rl_stricmp (name, "keyseq-timeout") == 0)
2587 {
2588 sprintf (numbuf, "%d", _rl_keyseq_timeout);
2589 return (numbuf);
2590 }
2591 else if (_rl_stricmp (name, "emacs-mode-string") == 0)
2592 return (_rl_emacs_mode_str ? _rl_emacs_mode_str : RL_EMACS_MODESTR_DEFAULT);
2593 else if (_rl_stricmp (name, "vi-cmd-mode-string") == 0)
2594 return (_rl_vi_cmd_mode_str ? _rl_vi_cmd_mode_str : RL_VI_CMD_MODESTR_DEFAULT);
2595 else if (_rl_stricmp (name, "vi-ins-mode-string") == 0)
2596 return (_rl_vi_ins_mode_str ? _rl_vi_ins_mode_str : RL_VI_INS_MODESTR_DEFAULT);
2597 else
2598 return (0);
2599 }
2600
2601 void
2602 rl_variable_dumper (print_readably)
2603 int print_readably;
2604 {
2605 int i;
2606 char *v;
2607
2608 for (i = 0; boolean_varlist[i].name; i++)
2609 {
2610 if (print_readably)
2611 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
2612 *boolean_varlist[i].value ? "on" : "off");
2613 else
2614 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
2615 *boolean_varlist[i].value ? "on" : "off");
2616 }
2617
2618 for (i = 0; string_varlist[i].name; i++)
2619 {
2620 v = _rl_get_string_variable_value (string_varlist[i].name);
2621 if (v == 0) /* _rl_isearch_terminators can be NULL */
2622 continue;
2623 if (print_readably)
2624 fprintf (rl_outstream, "set %s %s\n", string_varlist[i].name, v);
2625 else
2626 fprintf (rl_outstream, "%s is set to `%s'\n", string_varlist[i].name, v);
2627 }
2628 }
2629
2630 /* Print all of the current variables and their values to
2631 rl_outstream. If an explicit argument is given, then print
2632 the output in such a way that it can be read back in. */
2633 int
2634 rl_dump_variables (count, key)
2635 int count, key;
2636 {
2637 if (rl_dispatching)
2638 fprintf (rl_outstream, "\r\n");
2639 rl_variable_dumper (rl_explicit_arg);
2640 rl_on_new_line ();
2641 return (0);
2642 }
2643
2644 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2645 static int
2646 substring_member_of_array (string, array)
2647 const char *string;
2648 const char * const *array;
2649 {
2650 while (*array)
2651 {
2652 if (_rl_strindex (string, *array))
2653 return (1);
2654 array++;
2655 }
2656 return (0);
2657 }
This page took 0.086143 seconds and 5 git commands to generate.