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