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