PR gdb/2138
[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-2005 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++;
466 /* XXX - should 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
470 {
471 i++;
472 array[l++] = META (seq[i]);
473 }
474 }
475 else if (c == 'C')
476 {
477 i += 2;
478 /* Special hack for C-?... */
479 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
480 }
481 continue;
482 }
483
484 /* Translate other backslash-escaped characters. These are the
485 same escape sequences that bash's `echo' and `printf' builtins
486 handle, with the addition of \d -> RUBOUT. A backslash
487 preceding a character that is not special is stripped. */
488 switch (c)
489 {
490 case 'a':
491 array[l++] = '\007';
492 break;
493 case 'b':
494 array[l++] = '\b';
495 break;
496 case 'd':
497 array[l++] = RUBOUT; /* readline-specific */
498 break;
499 case 'e':
500 array[l++] = ESC;
501 break;
502 case 'f':
503 array[l++] = '\f';
504 break;
505 case 'n':
506 array[l++] = NEWLINE;
507 break;
508 case 'r':
509 array[l++] = RETURN;
510 break;
511 case 't':
512 array[l++] = TAB;
513 break;
514 case 'v':
515 array[l++] = 0x0B;
516 break;
517 case '\\':
518 array[l++] = '\\';
519 break;
520 case '0': case '1': case '2': case '3':
521 case '4': case '5': case '6': case '7':
522 i++;
523 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
524 c = (c * 8) + OCTVALUE (seq[i]);
525 i--; /* auto-increment in for loop */
526 array[l++] = c & largest_char;
527 break;
528 case 'x':
529 i++;
530 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)seq[i]) && temp--; i++)
531 c = (c * 16) + HEXVALUE (seq[i]);
532 if (temp == 2)
533 c = 'x';
534 i--; /* auto-increment in for loop */
535 array[l++] = c & largest_char;
536 break;
537 default: /* backslashes before non-special chars just add the char */
538 array[l++] = c;
539 break; /* the backslash is stripped */
540 }
541 continue;
542 }
543
544 array[l++] = c;
545 }
546
547 *len = l;
548 array[l] = '\0';
549 return (0);
550 }
551
552 char *
553 rl_untranslate_keyseq (seq)
554 int seq;
555 {
556 static char kseq[16];
557 int i, c;
558
559 i = 0;
560 c = seq;
561 if (META_CHAR (c))
562 {
563 kseq[i++] = '\\';
564 kseq[i++] = 'M';
565 kseq[i++] = '-';
566 c = UNMETA (c);
567 }
568 else if (CTRL_CHAR (c))
569 {
570 kseq[i++] = '\\';
571 kseq[i++] = 'C';
572 kseq[i++] = '-';
573 c = _rl_to_lower (UNCTRL (c));
574 }
575 else if (c == RUBOUT)
576 {
577 kseq[i++] = '\\';
578 kseq[i++] = 'C';
579 kseq[i++] = '-';
580 c = '?';
581 }
582
583 if (c == ESC)
584 {
585 kseq[i++] = '\\';
586 c = 'e';
587 }
588 else if (c == '\\' || c == '"')
589 {
590 kseq[i++] = '\\';
591 }
592
593 kseq[i++] = (unsigned char) c;
594 kseq[i] = '\0';
595 return kseq;
596 }
597
598 static char *
599 _rl_untranslate_macro_value (seq)
600 char *seq;
601 {
602 char *ret, *r, *s;
603 int c;
604
605 r = ret = (char *)xmalloc (7 * strlen (seq) + 1);
606 for (s = seq; *s; s++)
607 {
608 c = *s;
609 if (META_CHAR (c))
610 {
611 *r++ = '\\';
612 *r++ = 'M';
613 *r++ = '-';
614 c = UNMETA (c);
615 }
616 else if (CTRL_CHAR (c) && c != ESC)
617 {
618 *r++ = '\\';
619 *r++ = 'C';
620 *r++ = '-';
621 c = _rl_to_lower (UNCTRL (c));
622 }
623 else if (c == RUBOUT)
624 {
625 *r++ = '\\';
626 *r++ = 'C';
627 *r++ = '-';
628 c = '?';
629 }
630
631 if (c == ESC)
632 {
633 *r++ = '\\';
634 c = 'e';
635 }
636 else if (c == '\\' || c == '"')
637 *r++ = '\\';
638
639 *r++ = (unsigned char)c;
640 }
641 *r = '\0';
642 return ret;
643 }
644
645 /* Return a pointer to the function that STRING represents.
646 If STRING doesn't have a matching function, then a NULL pointer
647 is returned. */
648 rl_command_func_t *
649 rl_named_function (string)
650 const char *string;
651 {
652 register int i;
653
654 rl_initialize_funmap ();
655
656 for (i = 0; funmap[i]; i++)
657 if (_rl_stricmp (funmap[i]->name, string) == 0)
658 return (funmap[i]->function);
659 return ((rl_command_func_t *)NULL);
660 }
661
662 /* Return the function (or macro) definition which would be invoked via
663 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
664 used. TYPE, if non-NULL, is a pointer to an int which will receive the
665 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
666 or ISMACR (macro). */
667 rl_command_func_t *
668 rl_function_of_keyseq (keyseq, map, type)
669 const char *keyseq;
670 Keymap map;
671 int *type;
672 {
673 register int i;
674
675 if (!map)
676 map = _rl_keymap;
677
678 for (i = 0; keyseq && keyseq[i]; i++)
679 {
680 unsigned char ic = keyseq[i];
681
682 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
683 {
684 if (map[ESC].type != ISKMAP)
685 {
686 if (type)
687 *type = map[ESC].type;
688
689 return (map[ESC].function);
690 }
691 else
692 {
693 map = FUNCTION_TO_KEYMAP (map, ESC);
694 ic = UNMETA (ic);
695 }
696 }
697
698 if (map[ic].type == ISKMAP)
699 {
700 /* If this is the last key in the key sequence, return the
701 map. */
702 if (!keyseq[i + 1])
703 {
704 if (type)
705 *type = ISKMAP;
706
707 return (map[ic].function);
708 }
709 else
710 map = FUNCTION_TO_KEYMAP (map, ic);
711 }
712 else
713 {
714 if (type)
715 *type = map[ic].type;
716
717 return (map[ic].function);
718 }
719 }
720 return ((rl_command_func_t *) NULL);
721 }
722
723 /* The last key bindings file read. */
724 static char *last_readline_init_file = (char *)NULL;
725
726 /* The file we're currently reading key bindings from. */
727 static const char *current_readline_init_file;
728 static int current_readline_init_include_level;
729 static int current_readline_init_lineno;
730
731 /* Read FILENAME into a locally-allocated buffer and return the buffer.
732 The size of the buffer is returned in *SIZEP. Returns NULL if any
733 errors were encountered. */
734 static char *
735 _rl_read_file (filename, sizep)
736 char *filename;
737 size_t *sizep;
738 {
739 struct stat finfo;
740 size_t file_size;
741 char *buffer;
742 int i, file;
743
744 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
745 return ((char *)NULL);
746
747 file_size = (size_t)finfo.st_size;
748
749 /* check for overflow on very large files */
750 if (file_size != finfo.st_size || file_size + 1 < file_size)
751 {
752 if (file >= 0)
753 close (file);
754 #if defined (EFBIG)
755 errno = EFBIG;
756 #endif
757 return ((char *)NULL);
758 }
759
760 /* Read the file into BUFFER. */
761 buffer = (char *)xmalloc (file_size + 1);
762 i = read (file, buffer, file_size);
763 close (file);
764
765 if (i < 0)
766 {
767 free (buffer);
768 return ((char *)NULL);
769 }
770
771 buffer[i] = '\0';
772 if (sizep)
773 *sizep = i;
774
775 return (buffer);
776 }
777
778 /* Re-read the current keybindings file. */
779 int
780 rl_re_read_init_file (count, ignore)
781 int count, ignore;
782 {
783 int r;
784 r = rl_read_init_file ((const char *)NULL);
785 rl_set_keymap_from_edit_mode ();
786 return r;
787 }
788
789 /* Do key bindings from a file. If FILENAME is NULL it defaults
790 to the first non-null filename from this list:
791 1. the filename used for the previous call
792 2. the value of the shell variable `INPUTRC'
793 3. ~/.inputrc
794 If the file existed and could be opened and read, 0 is returned,
795 otherwise errno is returned. */
796 int
797 rl_read_init_file (filename)
798 const char *filename;
799 {
800 /* Default the filename. */
801 if (filename == 0)
802 {
803 filename = last_readline_init_file;
804 if (filename == 0)
805 filename = sh_get_env_value ("INPUTRC");
806 if (filename == 0)
807 filename = DEFAULT_INPUTRC;
808 }
809
810 if (*filename == 0)
811 filename = DEFAULT_INPUTRC;
812
813 #if defined (__MSDOS__)
814 if (_rl_read_init_file (filename, 0) == 0)
815 return 0;
816 filename = "~/_inputrc";
817 #endif
818 return (_rl_read_init_file (filename, 0));
819 }
820
821 static int
822 _rl_read_init_file (filename, include_level)
823 const char *filename;
824 int include_level;
825 {
826 register int i;
827 char *buffer, *openname, *line, *end;
828 size_t file_size;
829
830 current_readline_init_file = filename;
831 current_readline_init_include_level = include_level;
832
833 openname = tilde_expand (filename);
834 buffer = _rl_read_file (openname, &file_size);
835 free (openname);
836
837 if (buffer == 0)
838 return (errno);
839
840 if (include_level == 0 && filename != last_readline_init_file)
841 {
842 FREE (last_readline_init_file);
843 last_readline_init_file = savestring (filename);
844 }
845
846 currently_reading_init_file = 1;
847
848 /* Loop over the lines in the file. Lines that start with `#' are
849 comments; all other lines are commands for readline initialization. */
850 current_readline_init_lineno = 1;
851 line = buffer;
852 end = buffer + file_size;
853 while (line < end)
854 {
855 /* Find the end of this line. */
856 for (i = 0; line + i != end && line[i] != '\n'; i++);
857
858 #if defined (__CYGWIN__)
859 /* ``Be liberal in what you accept.'' */
860 if (line[i] == '\n' && line[i-1] == '\r')
861 line[i - 1] = '\0';
862 #endif
863
864 /* Mark end of line. */
865 line[i] = '\0';
866
867 /* Skip leading whitespace. */
868 while (*line && whitespace (*line))
869 {
870 line++;
871 i--;
872 }
873
874 /* If the line is not a comment, then parse it. */
875 if (*line && *line != '#')
876 rl_parse_and_bind (line);
877
878 /* Move to the next line. */
879 line += i + 1;
880 current_readline_init_lineno++;
881 }
882
883 free (buffer);
884 currently_reading_init_file = 0;
885 return (0);
886 }
887
888 static void
889 _rl_init_file_error (msg)
890 const char *msg;
891 {
892 if (currently_reading_init_file)
893 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
894 current_readline_init_lineno, msg);
895 else
896 fprintf (stderr, "readline: %s\n", msg);
897 }
898
899 /* **************************************************************** */
900 /* */
901 /* Parser Directives */
902 /* */
903 /* **************************************************************** */
904
905 typedef int _rl_parser_func_t PARAMS((char *));
906
907 /* Things that mean `Control'. */
908 const char *_rl_possible_control_prefixes[] = {
909 "Control-", "C-", "CTRL-", (const char *)NULL
910 };
911
912 const char *_rl_possible_meta_prefixes[] = {
913 "Meta", "M-", (const char *)NULL
914 };
915
916 /* Conditionals. */
917
918 /* Calling programs set this to have their argv[0]. */
919 const char *rl_readline_name = "other";
920
921 /* Stack of previous values of parsing_conditionalized_out. */
922 static unsigned char *if_stack = (unsigned char *)NULL;
923 static int if_stack_depth;
924 static int if_stack_size;
925
926 /* Push _rl_parsing_conditionalized_out, and set parser state based
927 on ARGS. */
928 static int
929 parser_if (args)
930 char *args;
931 {
932 register int i;
933
934 /* Push parser state. */
935 if (if_stack_depth + 1 >= if_stack_size)
936 {
937 if (!if_stack)
938 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
939 else
940 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
941 }
942 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
943
944 /* If parsing is turned off, then nothing can turn it back on except
945 for finding the matching endif. In that case, return right now. */
946 if (_rl_parsing_conditionalized_out)
947 return 0;
948
949 /* Isolate first argument. */
950 for (i = 0; args[i] && !whitespace (args[i]); i++);
951
952 if (args[i])
953 args[i++] = '\0';
954
955 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
956 isn't term=foo, or mode=emacs, then check to see if the first
957 word in ARGS is the same as the value stored in rl_readline_name. */
958 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
959 {
960 char *tem, *tname;
961
962 /* Terminals like "aaa-60" are equivalent to "aaa". */
963 tname = savestring (rl_terminal_name);
964 tem = strchr (tname, '-');
965 if (tem)
966 *tem = '\0';
967
968 /* Test the `long' and `short' forms of the terminal name so that
969 if someone has a `sun-cmd' and does not want to have bindings
970 that will be executed if the terminal is a `sun', they can put
971 `$if term=sun-cmd' into their .inputrc. */
972 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
973 _rl_stricmp (args + 5, rl_terminal_name);
974 free (tname);
975 }
976 #if defined (VI_MODE)
977 else if (_rl_strnicmp (args, "mode=", 5) == 0)
978 {
979 int mode;
980
981 if (_rl_stricmp (args + 5, "emacs") == 0)
982 mode = emacs_mode;
983 else if (_rl_stricmp (args + 5, "vi") == 0)
984 mode = vi_mode;
985 else
986 mode = no_mode;
987
988 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
989 }
990 #endif /* VI_MODE */
991 /* Check to see if the first word in ARGS is the same as the
992 value stored in rl_readline_name. */
993 else if (_rl_stricmp (args, rl_readline_name) == 0)
994 _rl_parsing_conditionalized_out = 0;
995 else
996 _rl_parsing_conditionalized_out = 1;
997 return 0;
998 }
999
1000 /* Invert the current parser state if there is anything on the stack. */
1001 static int
1002 parser_else (args)
1003 char *args;
1004 {
1005 register int i;
1006
1007 if (if_stack_depth == 0)
1008 {
1009 _rl_init_file_error ("$else found without matching $if");
1010 return 0;
1011 }
1012
1013 #if 0
1014 /* Check the previous (n - 1) levels of the stack to make sure that
1015 we haven't previously turned off parsing. */
1016 for (i = 0; i < if_stack_depth - 1; i++)
1017 #else
1018 /* Check the previous (n) levels of the stack to make sure that
1019 we haven't previously turned off parsing. */
1020 for (i = 0; i < if_stack_depth; i++)
1021 #endif
1022 if (if_stack[i] == 1)
1023 return 0;
1024
1025 /* Invert the state of parsing if at top level. */
1026 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
1027 return 0;
1028 }
1029
1030 /* Terminate a conditional, popping the value of
1031 _rl_parsing_conditionalized_out from the stack. */
1032 static int
1033 parser_endif (args)
1034 char *args;
1035 {
1036 if (if_stack_depth)
1037 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
1038 else
1039 _rl_init_file_error ("$endif without matching $if");
1040 return 0;
1041 }
1042
1043 static int
1044 parser_include (args)
1045 char *args;
1046 {
1047 const char *old_init_file;
1048 char *e;
1049 int old_line_number, old_include_level, r;
1050
1051 if (_rl_parsing_conditionalized_out)
1052 return (0);
1053
1054 old_init_file = current_readline_init_file;
1055 old_line_number = current_readline_init_lineno;
1056 old_include_level = current_readline_init_include_level;
1057
1058 e = strchr (args, '\n');
1059 if (e)
1060 *e = '\0';
1061 r = _rl_read_init_file ((const char *)args, old_include_level + 1);
1062
1063 current_readline_init_file = old_init_file;
1064 current_readline_init_lineno = old_line_number;
1065 current_readline_init_include_level = old_include_level;
1066
1067 return r;
1068 }
1069
1070 /* Associate textual names with actual functions. */
1071 static struct {
1072 const char *name;
1073 _rl_parser_func_t *function;
1074 } parser_directives [] = {
1075 { "if", parser_if },
1076 { "endif", parser_endif },
1077 { "else", parser_else },
1078 { "include", parser_include },
1079 { (char *)0x0, (_rl_parser_func_t *)0x0 }
1080 };
1081
1082 /* Handle a parser directive. STATEMENT is the line of the directive
1083 without any leading `$'. */
1084 static int
1085 handle_parser_directive (statement)
1086 char *statement;
1087 {
1088 register int i;
1089 char *directive, *args;
1090
1091 /* Isolate the actual directive. */
1092
1093 /* Skip whitespace. */
1094 for (i = 0; whitespace (statement[i]); i++);
1095
1096 directive = &statement[i];
1097
1098 for (; statement[i] && !whitespace (statement[i]); i++);
1099
1100 if (statement[i])
1101 statement[i++] = '\0';
1102
1103 for (; statement[i] && whitespace (statement[i]); i++);
1104
1105 args = &statement[i];
1106
1107 /* Lookup the command, and act on it. */
1108 for (i = 0; parser_directives[i].name; i++)
1109 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1110 {
1111 (*parser_directives[i].function) (args);
1112 return (0);
1113 }
1114
1115 /* display an error message about the unknown parser directive */
1116 _rl_init_file_error ("unknown parser directive");
1117 return (1);
1118 }
1119
1120 /* Read the binding command from STRING and perform it.
1121 A key binding command looks like: Keyname: function-name\0,
1122 a variable binding command looks like: set variable value.
1123 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1124 int
1125 rl_parse_and_bind (string)
1126 char *string;
1127 {
1128 char *funname, *kname;
1129 register int c, i;
1130 int key, equivalency;
1131
1132 while (string && whitespace (*string))
1133 string++;
1134
1135 if (!string || !*string || *string == '#')
1136 return 0;
1137
1138 /* If this is a parser directive, act on it. */
1139 if (*string == '$')
1140 {
1141 handle_parser_directive (&string[1]);
1142 return 0;
1143 }
1144
1145 /* If we aren't supposed to be parsing right now, then we're done. */
1146 if (_rl_parsing_conditionalized_out)
1147 return 0;
1148
1149 i = 0;
1150 /* If this keyname is a complex key expression surrounded by quotes,
1151 advance to after the matching close quote. This code allows the
1152 backslash to quote characters in the key expression. */
1153 if (*string == '"')
1154 {
1155 int passc = 0;
1156
1157 for (i = 1; c = string[i]; i++)
1158 {
1159 if (passc)
1160 {
1161 passc = 0;
1162 continue;
1163 }
1164
1165 if (c == '\\')
1166 {
1167 passc++;
1168 continue;
1169 }
1170
1171 if (c == '"')
1172 break;
1173 }
1174 /* If we didn't find a closing quote, abort the line. */
1175 if (string[i] == '\0')
1176 {
1177 _rl_init_file_error ("no closing `\"' in key binding");
1178 return 1;
1179 }
1180 }
1181
1182 /* Advance to the colon (:) or whitespace which separates the two objects. */
1183 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1184
1185 equivalency = (c == ':' && string[i + 1] == '=');
1186
1187 /* Mark the end of the command (or keyname). */
1188 if (string[i])
1189 string[i++] = '\0';
1190
1191 /* If doing assignment, skip the '=' sign as well. */
1192 if (equivalency)
1193 string[i++] = '\0';
1194
1195 /* If this is a command to set a variable, then do that. */
1196 if (_rl_stricmp (string, "set") == 0)
1197 {
1198 char *var, *value, *e;
1199
1200 var = string + i;
1201 /* Make VAR point to start of variable name. */
1202 while (*var && whitespace (*var)) var++;
1203
1204 /* Make VALUE point to start of value string. */
1205 value = var;
1206 while (*value && !whitespace (*value)) value++;
1207 if (*value)
1208 *value++ = '\0';
1209 while (*value && whitespace (*value)) value++;
1210
1211 /* Strip trailing whitespace from values to boolean variables. Temp
1212 fix until I get a real quoted-string parser here. */
1213 i = find_boolean_var (var);
1214 if (i >= 0)
1215 {
1216 /* remove trailing whitespace */
1217 e = value + strlen (value) - 1;
1218 while (e >= value && whitespace (*e))
1219 e--;
1220 e++; /* skip back to whitespace or EOS */
1221 if (*e && e >= value)
1222 *e = '\0';
1223 }
1224
1225 rl_variable_bind (var, value);
1226 return 0;
1227 }
1228
1229 /* Skip any whitespace between keyname and funname. */
1230 for (; string[i] && whitespace (string[i]); i++);
1231 funname = &string[i];
1232
1233 /* Now isolate funname.
1234 For straight function names just look for whitespace, since
1235 that will signify the end of the string. But this could be a
1236 macro definition. In that case, the string is quoted, so skip
1237 to the matching delimiter. We allow the backslash to quote the
1238 delimiter characters in the macro body. */
1239 /* This code exists to allow whitespace in macro expansions, which
1240 would otherwise be gobbled up by the next `for' loop.*/
1241 /* XXX - it may be desirable to allow backslash quoting only if " is
1242 the quoted string delimiter, like the shell. */
1243 if (*funname == '\'' || *funname == '"')
1244 {
1245 int delimiter, passc;
1246
1247 delimiter = string[i++];
1248 for (passc = 0; c = string[i]; i++)
1249 {
1250 if (passc)
1251 {
1252 passc = 0;
1253 continue;
1254 }
1255
1256 if (c == '\\')
1257 {
1258 passc = 1;
1259 continue;
1260 }
1261
1262 if (c == delimiter)
1263 break;
1264 }
1265 if (c)
1266 i++;
1267 }
1268
1269 /* Advance to the end of the string. */
1270 for (; string[i] && !whitespace (string[i]); i++);
1271
1272 /* No extra whitespace at the end of the string. */
1273 string[i] = '\0';
1274
1275 /* Handle equivalency bindings here. Make the left-hand side be exactly
1276 whatever the right-hand evaluates to, including keymaps. */
1277 if (equivalency)
1278 {
1279 return 0;
1280 }
1281
1282 /* If this is a new-style key-binding, then do the binding with
1283 rl_bind_keyseq (). Otherwise, let the older code deal with it. */
1284 if (*string == '"')
1285 {
1286 char *seq;
1287 register int j, k, passc;
1288
1289 seq = (char *)xmalloc (1 + strlen (string));
1290 for (j = 1, k = passc = 0; string[j]; j++)
1291 {
1292 /* Allow backslash to quote characters, but leave them in place.
1293 This allows a string to end with a backslash quoting another
1294 backslash, or with a backslash quoting a double quote. The
1295 backslashes are left in place for rl_translate_keyseq (). */
1296 if (passc || (string[j] == '\\'))
1297 {
1298 seq[k++] = string[j];
1299 passc = !passc;
1300 continue;
1301 }
1302
1303 if (string[j] == '"')
1304 break;
1305
1306 seq[k++] = string[j];
1307 }
1308 seq[k] = '\0';
1309
1310 /* Binding macro? */
1311 if (*funname == '\'' || *funname == '"')
1312 {
1313 j = strlen (funname);
1314
1315 /* Remove the delimiting quotes from each end of FUNNAME. */
1316 if (j && funname[j - 1] == *funname)
1317 funname[j - 1] = '\0';
1318
1319 rl_macro_bind (seq, &funname[1], _rl_keymap);
1320 }
1321 else
1322 rl_bind_keyseq (seq, rl_named_function (funname));
1323
1324 free (seq);
1325 return 0;
1326 }
1327
1328 /* Get the actual character we want to deal with. */
1329 kname = strrchr (string, '-');
1330 if (!kname)
1331 kname = string;
1332 else
1333 kname++;
1334
1335 key = glean_key_from_name (kname);
1336
1337 /* Add in control and meta bits. */
1338 if (substring_member_of_array (string, _rl_possible_control_prefixes))
1339 key = CTRL (_rl_to_upper (key));
1340
1341 if (substring_member_of_array (string, _rl_possible_meta_prefixes))
1342 key = META (key);
1343
1344 /* Temporary. Handle old-style keyname with macro-binding. */
1345 if (*funname == '\'' || *funname == '"')
1346 {
1347 char useq[2];
1348 int fl = strlen (funname);
1349
1350 useq[0] = key; useq[1] = '\0';
1351 if (fl && funname[fl - 1] == *funname)
1352 funname[fl - 1] = '\0';
1353
1354 rl_macro_bind (useq, &funname[1], _rl_keymap);
1355 }
1356 #if defined (PREFIX_META_HACK)
1357 /* Ugly, but working hack to keep prefix-meta around. */
1358 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1359 {
1360 char seq[2];
1361
1362 seq[0] = key;
1363 seq[1] = '\0';
1364 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1365 }
1366 #endif /* PREFIX_META_HACK */
1367 else
1368 rl_bind_key (key, rl_named_function (funname));
1369 return 0;
1370 }
1371
1372 /* Simple structure for boolean readline variables (i.e., those that can
1373 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1374 false. */
1375
1376 #define V_SPECIAL 0x1
1377
1378 static struct {
1379 const char *name;
1380 int *value;
1381 int flags;
1382 } boolean_varlist [] = {
1383 { "bind-tty-special-chars", &_rl_bind_stty_chars, 0 },
1384 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL },
1385 { "byte-oriented", &rl_byte_oriented, 0 },
1386 { "completion-ignore-case", &_rl_completion_case_fold, 0 },
1387 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 },
1388 { "disable-completion", &rl_inhibit_completion, 0 },
1389 { "enable-keypad", &_rl_enable_keypad, 0 },
1390 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 },
1391 { "history-preserve-point", &_rl_history_preserve_point, 0 },
1392 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 },
1393 { "input-meta", &_rl_meta_flag, 0 },
1394 { "mark-directories", &_rl_complete_mark_directories, 0 },
1395 { "mark-modified-lines", &_rl_mark_modified_lines, 0 },
1396 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 },
1397 { "match-hidden-files", &_rl_match_hidden_files, 0 },
1398 { "meta-flag", &_rl_meta_flag, 0 },
1399 { "output-meta", &_rl_output_meta_chars, 0 },
1400 { "page-completions", &_rl_page_completions, 0 },
1401 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL },
1402 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1403 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 },
1404 { "show-all-if-unmodified", &_rl_complete_show_unmodified, 0 },
1405 #if defined (VISIBLE_STATS)
1406 { "visible-stats", &rl_visible_stats, 0 },
1407 #endif /* VISIBLE_STATS */
1408 { (char *)NULL, (int *)NULL }
1409 };
1410
1411 static int
1412 find_boolean_var (name)
1413 const char *name;
1414 {
1415 register int i;
1416
1417 for (i = 0; boolean_varlist[i].name; i++)
1418 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1419 return i;
1420 return -1;
1421 }
1422
1423 /* Hooks for handling special boolean variables, where a
1424 function needs to be called or another variable needs
1425 to be changed when they're changed. */
1426 static void
1427 hack_special_boolean_var (i)
1428 int i;
1429 {
1430 const char *name;
1431
1432 name = boolean_varlist[i].name;
1433
1434 if (_rl_stricmp (name, "blink-matching-paren") == 0)
1435 _rl_enable_paren_matching (rl_blink_matching_paren);
1436 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1437 {
1438 if (_rl_prefer_visible_bell)
1439 _rl_bell_preference = VISIBLE_BELL;
1440 else
1441 _rl_bell_preference = AUDIBLE_BELL;
1442 }
1443 }
1444
1445 typedef int _rl_sv_func_t PARAMS((const char *));
1446
1447 /* These *must* correspond to the array indices for the appropriate
1448 string variable. (Though they're not used right now.) */
1449 #define V_BELLSTYLE 0
1450 #define V_COMBEGIN 1
1451 #define V_EDITMODE 2
1452 #define V_ISRCHTERM 3
1453 #define V_KEYMAP 4
1454
1455 #define V_STRING 1
1456 #define V_INT 2
1457
1458 /* Forward declarations */
1459 static int sv_bell_style PARAMS((const char *));
1460 static int sv_combegin PARAMS((const char *));
1461 static int sv_compquery PARAMS((const char *));
1462 static int sv_editmode PARAMS((const char *));
1463 static int sv_isrchterm PARAMS((const char *));
1464 static int sv_keymap PARAMS((const char *));
1465
1466 static struct {
1467 const char *name;
1468 int flags;
1469 _rl_sv_func_t *set_func;
1470 } string_varlist[] = {
1471 { "bell-style", V_STRING, sv_bell_style },
1472 { "comment-begin", V_STRING, sv_combegin },
1473 { "completion-query-items", V_INT, sv_compquery },
1474 { "editing-mode", V_STRING, sv_editmode },
1475 { "isearch-terminators", V_STRING, sv_isrchterm },
1476 { "keymap", V_STRING, sv_keymap },
1477 { (char *)NULL, 0 }
1478 };
1479
1480 static int
1481 find_string_var (name)
1482 const char *name;
1483 {
1484 register int i;
1485
1486 for (i = 0; string_varlist[i].name; i++)
1487 if (_rl_stricmp (name, string_varlist[i].name) == 0)
1488 return i;
1489 return -1;
1490 }
1491
1492 /* A boolean value that can appear in a `set variable' command is true if
1493 the value is null or empty, `on' (case-insenstive), or "1". Any other
1494 values result in 0 (false). */
1495 static int
1496 bool_to_int (value)
1497 const char *value;
1498 {
1499 return (value == 0 || *value == '\0' ||
1500 (_rl_stricmp (value, "on") == 0) ||
1501 (value[0] == '1' && value[1] == '\0'));
1502 }
1503
1504 char *
1505 rl_variable_value (name)
1506 const char *name;
1507 {
1508 register int i;
1509 int v;
1510 char *ret;
1511
1512 /* Check for simple variables first. */
1513 i = find_boolean_var (name);
1514 if (i >= 0)
1515 return (*boolean_varlist[i].value ? "on" : "off");
1516
1517 i = find_string_var (name);
1518 if (i >= 0)
1519 return (_rl_get_string_variable_value (string_varlist[i].name));
1520
1521 /* Unknown variable names return NULL. */
1522 return 0;
1523 }
1524
1525 int
1526 rl_variable_bind (name, value)
1527 const char *name, *value;
1528 {
1529 register int i;
1530 int v;
1531
1532 /* Check for simple variables first. */
1533 i = find_boolean_var (name);
1534 if (i >= 0)
1535 {
1536 *boolean_varlist[i].value = bool_to_int (value);
1537 if (boolean_varlist[i].flags & V_SPECIAL)
1538 hack_special_boolean_var (i);
1539 return 0;
1540 }
1541
1542 i = find_string_var (name);
1543
1544 /* For the time being, unknown variable names or string names without a
1545 handler function are simply ignored. */
1546 if (i < 0 || string_varlist[i].set_func == 0)
1547 return 0;
1548
1549 v = (*string_varlist[i].set_func) (value);
1550 return v;
1551 }
1552
1553 static int
1554 sv_editmode (value)
1555 const char *value;
1556 {
1557 if (_rl_strnicmp (value, "vi", 2) == 0)
1558 {
1559 #if defined (VI_MODE)
1560 _rl_keymap = vi_insertion_keymap;
1561 rl_editing_mode = vi_mode;
1562 #endif /* VI_MODE */
1563 return 0;
1564 }
1565 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1566 {
1567 _rl_keymap = emacs_standard_keymap;
1568 rl_editing_mode = emacs_mode;
1569 return 0;
1570 }
1571 return 1;
1572 }
1573
1574 static int
1575 sv_combegin (value)
1576 const char *value;
1577 {
1578 if (value && *value)
1579 {
1580 FREE (_rl_comment_begin);
1581 _rl_comment_begin = savestring (value);
1582 return 0;
1583 }
1584 return 1;
1585 }
1586
1587 static int
1588 sv_compquery (value)
1589 const char *value;
1590 {
1591 int nval = 100;
1592
1593 if (value && *value)
1594 {
1595 nval = atoi (value);
1596 if (nval < 0)
1597 nval = 0;
1598 }
1599 rl_completion_query_items = nval;
1600 return 0;
1601 }
1602
1603 static int
1604 sv_keymap (value)
1605 const char *value;
1606 {
1607 Keymap kmap;
1608
1609 kmap = rl_get_keymap_by_name (value);
1610 if (kmap)
1611 {
1612 rl_set_keymap (kmap);
1613 return 0;
1614 }
1615 return 1;
1616 }
1617
1618 static int
1619 sv_bell_style (value)
1620 const char *value;
1621 {
1622 if (value == 0 || *value == '\0')
1623 _rl_bell_preference = AUDIBLE_BELL;
1624 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1625 _rl_bell_preference = NO_BELL;
1626 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1627 _rl_bell_preference = AUDIBLE_BELL;
1628 else if (_rl_stricmp (value, "visible") == 0)
1629 _rl_bell_preference = VISIBLE_BELL;
1630 else
1631 return 1;
1632 return 0;
1633 }
1634
1635 static int
1636 sv_isrchterm (value)
1637 const char *value;
1638 {
1639 int beg, end, delim;
1640 char *v;
1641
1642 if (value == 0)
1643 return 1;
1644
1645 /* Isolate the value and translate it into a character string. */
1646 v = savestring (value);
1647 FREE (_rl_isearch_terminators);
1648 if (v[0] == '"' || v[0] == '\'')
1649 {
1650 delim = v[0];
1651 for (beg = end = 1; v[end] && v[end] != delim; end++)
1652 ;
1653 }
1654 else
1655 {
1656 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1657 ;
1658 }
1659
1660 v[end] = '\0';
1661
1662 /* The value starts at v + beg. Translate it into a character string. */
1663 _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1);
1664 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1665 _rl_isearch_terminators[end] = '\0';
1666
1667 free (v);
1668 return 0;
1669 }
1670
1671 /* Return the character which matches NAME.
1672 For example, `Space' returns ' '. */
1673
1674 typedef struct {
1675 const char *name;
1676 int value;
1677 } assoc_list;
1678
1679 static assoc_list name_key_alist[] = {
1680 { "DEL", 0x7f },
1681 { "ESC", '\033' },
1682 { "Escape", '\033' },
1683 { "LFD", '\n' },
1684 { "Newline", '\n' },
1685 { "RET", '\r' },
1686 { "Return", '\r' },
1687 { "Rubout", 0x7f },
1688 { "SPC", ' ' },
1689 { "Space", ' ' },
1690 { "Tab", 0x09 },
1691 { (char *)0x0, 0 }
1692 };
1693
1694 static int
1695 glean_key_from_name (name)
1696 char *name;
1697 {
1698 register int i;
1699
1700 for (i = 0; name_key_alist[i].name; i++)
1701 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1702 return (name_key_alist[i].value);
1703
1704 return (*(unsigned char *)name); /* XXX was return (*name) */
1705 }
1706
1707 /* Auxiliary functions to manage keymaps. */
1708 static struct {
1709 const char *name;
1710 Keymap map;
1711 } keymap_names[] = {
1712 { "emacs", emacs_standard_keymap },
1713 { "emacs-standard", emacs_standard_keymap },
1714 { "emacs-meta", emacs_meta_keymap },
1715 { "emacs-ctlx", emacs_ctlx_keymap },
1716 #if defined (VI_MODE)
1717 { "vi", vi_movement_keymap },
1718 { "vi-move", vi_movement_keymap },
1719 { "vi-command", vi_movement_keymap },
1720 { "vi-insert", vi_insertion_keymap },
1721 #endif /* VI_MODE */
1722 { (char *)0x0, (Keymap)0x0 }
1723 };
1724
1725 Keymap
1726 rl_get_keymap_by_name (name)
1727 const char *name;
1728 {
1729 register int i;
1730
1731 for (i = 0; keymap_names[i].name; i++)
1732 if (_rl_stricmp (name, keymap_names[i].name) == 0)
1733 return (keymap_names[i].map);
1734 return ((Keymap) NULL);
1735 }
1736
1737 char *
1738 rl_get_keymap_name (map)
1739 Keymap map;
1740 {
1741 register int i;
1742 for (i = 0; keymap_names[i].name; i++)
1743 if (map == keymap_names[i].map)
1744 return ((char *)keymap_names[i].name);
1745 return ((char *)NULL);
1746 }
1747
1748 void
1749 rl_set_keymap (map)
1750 Keymap map;
1751 {
1752 if (map)
1753 _rl_keymap = map;
1754 }
1755
1756 Keymap
1757 rl_get_keymap ()
1758 {
1759 return (_rl_keymap);
1760 }
1761
1762 void
1763 rl_set_keymap_from_edit_mode ()
1764 {
1765 if (rl_editing_mode == emacs_mode)
1766 _rl_keymap = emacs_standard_keymap;
1767 #if defined (VI_MODE)
1768 else if (rl_editing_mode == vi_mode)
1769 _rl_keymap = vi_insertion_keymap;
1770 #endif /* VI_MODE */
1771 }
1772
1773 char *
1774 rl_get_keymap_name_from_edit_mode ()
1775 {
1776 if (rl_editing_mode == emacs_mode)
1777 return "emacs";
1778 #if defined (VI_MODE)
1779 else if (rl_editing_mode == vi_mode)
1780 return "vi";
1781 #endif /* VI_MODE */
1782 else
1783 return "none";
1784 }
1785
1786 /* **************************************************************** */
1787 /* */
1788 /* Key Binding and Function Information */
1789 /* */
1790 /* **************************************************************** */
1791
1792 /* Each of the following functions produces information about the
1793 state of keybindings and functions known to Readline. The info
1794 is always printed to rl_outstream, and in such a way that it can
1795 be read back in (i.e., passed to rl_parse_and_bind ()). */
1796
1797 /* Print the names of functions known to Readline. */
1798 void
1799 rl_list_funmap_names ()
1800 {
1801 register int i;
1802 const char **funmap_names;
1803
1804 funmap_names = rl_funmap_names ();
1805
1806 if (!funmap_names)
1807 return;
1808
1809 for (i = 0; funmap_names[i]; i++)
1810 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1811
1812 free (funmap_names);
1813 }
1814
1815 static char *
1816 _rl_get_keyname (key)
1817 int key;
1818 {
1819 char *keyname;
1820 int i, c;
1821
1822 keyname = (char *)xmalloc (8);
1823
1824 c = key;
1825 /* Since this is going to be used to write out keysequence-function
1826 pairs for possible inclusion in an inputrc file, we don't want to
1827 do any special meta processing on KEY. */
1828
1829 #if 1
1830 /* XXX - Experimental */
1831 /* We might want to do this, but the old version of the code did not. */
1832
1833 /* If this is an escape character, we don't want to do any more processing.
1834 Just add the special ESC key sequence and return. */
1835 if (c == ESC)
1836 {
1837 keyname[0] = '\\';
1838 keyname[1] = 'e';
1839 keyname[2] = '\0';
1840 return keyname;
1841 }
1842 #endif
1843
1844 /* RUBOUT is translated directly into \C-? */
1845 if (key == RUBOUT)
1846 {
1847 keyname[0] = '\\';
1848 keyname[1] = 'C';
1849 keyname[2] = '-';
1850 keyname[3] = '?';
1851 keyname[4] = '\0';
1852 return keyname;
1853 }
1854
1855 i = 0;
1856 /* Now add special prefixes needed for control characters. This can
1857 potentially change C. */
1858 if (CTRL_CHAR (c))
1859 {
1860 keyname[i++] = '\\';
1861 keyname[i++] = 'C';
1862 keyname[i++] = '-';
1863 c = _rl_to_lower (UNCTRL (c));
1864 }
1865
1866 /* XXX experimental code. Turn the characters that are not ASCII or
1867 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1868 This changes C. */
1869 if (c >= 128 && c <= 159)
1870 {
1871 keyname[i++] = '\\';
1872 keyname[i++] = '2';
1873 c -= 128;
1874 keyname[i++] = (c / 8) + '0';
1875 c = (c % 8) + '0';
1876 }
1877
1878 /* Now, if the character needs to be quoted with a backslash, do that. */
1879 if (c == '\\' || c == '"')
1880 keyname[i++] = '\\';
1881
1882 /* Now add the key, terminate the string, and return it. */
1883 keyname[i++] = (char) c;
1884 keyname[i] = '\0';
1885
1886 return keyname;
1887 }
1888
1889 /* Return a NULL terminated array of strings which represent the key
1890 sequences that are used to invoke FUNCTION in MAP. */
1891 char **
1892 rl_invoking_keyseqs_in_map (function, map)
1893 rl_command_func_t *function;
1894 Keymap map;
1895 {
1896 register int key;
1897 char **result;
1898 int result_index, result_size;
1899
1900 result = (char **)NULL;
1901 result_index = result_size = 0;
1902
1903 for (key = 0; key < KEYMAP_SIZE; key++)
1904 {
1905 switch (map[key].type)
1906 {
1907 case ISMACR:
1908 /* Macros match, if, and only if, the pointers are identical.
1909 Thus, they are treated exactly like functions in here. */
1910 case ISFUNC:
1911 /* If the function in the keymap is the one we are looking for,
1912 then add the current KEY to the list of invoking keys. */
1913 if (map[key].function == function)
1914 {
1915 char *keyname;
1916
1917 keyname = _rl_get_keyname (key);
1918
1919 if (result_index + 2 > result_size)
1920 {
1921 result_size += 10;
1922 result = (char **)xrealloc (result, result_size * sizeof (char *));
1923 }
1924
1925 result[result_index++] = keyname;
1926 result[result_index] = (char *)NULL;
1927 }
1928 break;
1929
1930 case ISKMAP:
1931 {
1932 char **seqs;
1933 register int i;
1934
1935 /* Find the list of keyseqs in this map which have FUNCTION as
1936 their target. Add the key sequences found to RESULT. */
1937 if (map[key].function)
1938 seqs =
1939 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1940 else
1941 break;
1942
1943 if (seqs == 0)
1944 break;
1945
1946 for (i = 0; seqs[i]; i++)
1947 {
1948 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1949
1950 if (key == ESC)
1951 #if 0
1952 sprintf (keyname, "\\e");
1953 #else
1954 /* XXX - experimental */
1955 sprintf (keyname, "\\M-");
1956 #endif
1957 else if (CTRL_CHAR (key))
1958 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1959 else if (key == RUBOUT)
1960 sprintf (keyname, "\\C-?");
1961 else if (key == '\\' || key == '"')
1962 {
1963 keyname[0] = '\\';
1964 keyname[1] = (char) key;
1965 keyname[2] = '\0';
1966 }
1967 else
1968 {
1969 keyname[0] = (char) key;
1970 keyname[1] = '\0';
1971 }
1972
1973 strcat (keyname, seqs[i]);
1974 free (seqs[i]);
1975
1976 if (result_index + 2 > result_size)
1977 {
1978 result_size += 10;
1979 result = (char **)xrealloc (result, result_size * sizeof (char *));
1980 }
1981
1982 result[result_index++] = keyname;
1983 result[result_index] = (char *)NULL;
1984 }
1985
1986 free (seqs);
1987 }
1988 break;
1989 }
1990 }
1991 return (result);
1992 }
1993
1994 /* Return a NULL terminated array of strings which represent the key
1995 sequences that can be used to invoke FUNCTION using the current keymap. */
1996 char **
1997 rl_invoking_keyseqs (function)
1998 rl_command_func_t *function;
1999 {
2000 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
2001 }
2002
2003 /* Print all of the functions and their bindings to rl_outstream. If
2004 PRINT_READABLY is non-zero, then print the output in such a way
2005 that it can be read back in. */
2006 void
2007 rl_function_dumper (print_readably)
2008 int print_readably;
2009 {
2010 register int i;
2011 const char **names;
2012 const char *name;
2013
2014 names = rl_funmap_names ();
2015
2016 fprintf (rl_outstream, "\n");
2017
2018 for (i = 0; name = names[i]; i++)
2019 {
2020 rl_command_func_t *function;
2021 char **invokers;
2022
2023 function = rl_named_function (name);
2024 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
2025
2026 if (print_readably)
2027 {
2028 if (!invokers)
2029 fprintf (rl_outstream, "# %s (not bound)\n", name);
2030 else
2031 {
2032 register int j;
2033
2034 for (j = 0; invokers[j]; j++)
2035 {
2036 fprintf (rl_outstream, "\"%s\": %s\n",
2037 invokers[j], name);
2038 free (invokers[j]);
2039 }
2040
2041 free (invokers);
2042 }
2043 }
2044 else
2045 {
2046 if (!invokers)
2047 fprintf (rl_outstream, "%s is not bound to any keys\n",
2048 name);
2049 else
2050 {
2051 register int j;
2052
2053 fprintf (rl_outstream, "%s can be found on ", name);
2054
2055 for (j = 0; invokers[j] && j < 5; j++)
2056 {
2057 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
2058 invokers[j + 1] ? ", " : ".\n");
2059 }
2060
2061 if (j == 5 && invokers[j])
2062 fprintf (rl_outstream, "...\n");
2063
2064 for (j = 0; invokers[j]; j++)
2065 free (invokers[j]);
2066
2067 free (invokers);
2068 }
2069 }
2070 }
2071 }
2072
2073 /* Print all of the current functions and their bindings to
2074 rl_outstream. If an explicit argument is given, then print
2075 the output in such a way that it can be read back in. */
2076 int
2077 rl_dump_functions (count, key)
2078 int count, key;
2079 {
2080 if (rl_dispatching)
2081 fprintf (rl_outstream, "\r\n");
2082 rl_function_dumper (rl_explicit_arg);
2083 rl_on_new_line ();
2084 return (0);
2085 }
2086
2087 static void
2088 _rl_macro_dumper_internal (print_readably, map, prefix)
2089 int print_readably;
2090 Keymap map;
2091 char *prefix;
2092 {
2093 register int key;
2094 char *keyname, *out;
2095 int prefix_len;
2096
2097 for (key = 0; key < KEYMAP_SIZE; key++)
2098 {
2099 switch (map[key].type)
2100 {
2101 case ISMACR:
2102 keyname = _rl_get_keyname (key);
2103 out = _rl_untranslate_macro_value ((char *)map[key].function);
2104
2105 if (print_readably)
2106 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
2107 keyname,
2108 out ? out : "");
2109 else
2110 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
2111 keyname,
2112 out ? out : "");
2113 free (keyname);
2114 free (out);
2115 break;
2116 case ISFUNC:
2117 break;
2118 case ISKMAP:
2119 prefix_len = prefix ? strlen (prefix) : 0;
2120 if (key == ESC)
2121 {
2122 keyname = (char *)xmalloc (3 + prefix_len);
2123 if (prefix)
2124 strcpy (keyname, prefix);
2125 keyname[prefix_len] = '\\';
2126 keyname[prefix_len + 1] = 'e';
2127 keyname[prefix_len + 2] = '\0';
2128 }
2129 else
2130 {
2131 keyname = _rl_get_keyname (key);
2132 if (prefix)
2133 {
2134 out = (char *)xmalloc (strlen (keyname) + prefix_len + 1);
2135 strcpy (out, prefix);
2136 strcpy (out + prefix_len, keyname);
2137 free (keyname);
2138 keyname = out;
2139 }
2140 }
2141
2142 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
2143 free (keyname);
2144 break;
2145 }
2146 }
2147 }
2148
2149 void
2150 rl_macro_dumper (print_readably)
2151 int print_readably;
2152 {
2153 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
2154 }
2155
2156 int
2157 rl_dump_macros (count, key)
2158 int count, key;
2159 {
2160 if (rl_dispatching)
2161 fprintf (rl_outstream, "\r\n");
2162 rl_macro_dumper (rl_explicit_arg);
2163 rl_on_new_line ();
2164 return (0);
2165 }
2166
2167 static char *
2168 _rl_get_string_variable_value (name)
2169 const char *name;
2170 {
2171 static char numbuf[32];
2172 char *ret;
2173 int n;
2174
2175 if (_rl_stricmp (name, "bell-style") == 0)
2176 {
2177 switch (_rl_bell_preference)
2178 {
2179 case NO_BELL:
2180 return "none";
2181 case VISIBLE_BELL:
2182 return "visible";
2183 case AUDIBLE_BELL:
2184 default:
2185 return "audible";
2186 }
2187 }
2188 else if (_rl_stricmp (name, "comment-begin") == 0)
2189 return (_rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2190 else if (_rl_stricmp (name, "completion-query-items") == 0)
2191 {
2192 sprintf (numbuf, "%d", rl_completion_query_items);
2193 return (numbuf);
2194 }
2195 else if (_rl_stricmp (name, "editing-mode") == 0)
2196 return (rl_get_keymap_name_from_edit_mode ());
2197 else if (_rl_stricmp (name, "isearch-terminators") == 0)
2198 {
2199 if (_rl_isearch_terminators == 0)
2200 return 0;
2201 ret = _rl_untranslate_macro_value (_rl_isearch_terminators);
2202 if (ret)
2203 {
2204 strncpy (numbuf, ret, sizeof (numbuf) - 1);
2205 free (ret);
2206 numbuf[sizeof(numbuf) - 1] = '\0';
2207 }
2208 else
2209 numbuf[0] = '\0';
2210 return numbuf;
2211 }
2212 else if (_rl_stricmp (name, "keymap") == 0)
2213 {
2214 ret = rl_get_keymap_name (_rl_keymap);
2215 if (ret == 0)
2216 ret = rl_get_keymap_name_from_edit_mode ();
2217 return (ret ? ret : "none");
2218 }
2219 else
2220 return (0);
2221 }
2222
2223 void
2224 rl_variable_dumper (print_readably)
2225 int print_readably;
2226 {
2227 int i;
2228 char *v;
2229
2230 for (i = 0; boolean_varlist[i].name; i++)
2231 {
2232 if (print_readably)
2233 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
2234 *boolean_varlist[i].value ? "on" : "off");
2235 else
2236 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
2237 *boolean_varlist[i].value ? "on" : "off");
2238 }
2239
2240 for (i = 0; string_varlist[i].name; i++)
2241 {
2242 v = _rl_get_string_variable_value (string_varlist[i].name);
2243 if (v == 0) /* _rl_isearch_terminators can be NULL */
2244 continue;
2245 if (print_readably)
2246 fprintf (rl_outstream, "set %s %s\n", string_varlist[i].name, v);
2247 else
2248 fprintf (rl_outstream, "%s is set to `%s'\n", string_varlist[i].name, v);
2249 }
2250 }
2251
2252 /* Print all of the current variables and their values to
2253 rl_outstream. If an explicit argument is given, then print
2254 the output in such a way that it can be read back in. */
2255 int
2256 rl_dump_variables (count, key)
2257 int count, key;
2258 {
2259 if (rl_dispatching)
2260 fprintf (rl_outstream, "\r\n");
2261 rl_variable_dumper (rl_explicit_arg);
2262 rl_on_new_line ();
2263 return (0);
2264 }
2265
2266 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2267 static int
2268 substring_member_of_array (string, array)
2269 char *string;
2270 const char **array;
2271 {
2272 while (*array)
2273 {
2274 if (_rl_strindex (string, *array))
2275 return (1);
2276 array++;
2277 }
2278 return (0);
2279 }
This page took 0.128014 seconds and 4 git commands to generate.