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