Debian Codegraph @

libsrc/readline/bind.c

raw
1 /* bind.c -- key binding and startup file support for the readline library. */
2
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 1, or
11 (at your option) any later version.
12
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #if !defined (NO_SYS_FILE)
28 # include <sys/file.h>
29 #endif /* !NO_SYS_FILE */
30 #include <signal.h>
31
32 #if defined (HAVE_UNISTD_H)
33 # include <unistd.h>
34 #endif /* HAVE_UNISTD_H */
35
36 #if defined (HAVE_STDLIB_H)
37 # include <stdlib.h>
38 #else
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
41
42 #include <errno.h>
43 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
44 #if !defined (errno)
45 extern int errno;
46 #endif /* !errno */
47
48 #include "posixstat.h"
49
50 /* System-specific feature definitions and include files. */
51 #include "rldefs.h"
52
53 /* Some standard library routines. */
54 #include "readline.h"
55 #include "history.h"
56
57 #if !defined (strchr) && !defined (__STDC__)
58 extern char *strchr (), *strrchr ();
59 #endif /* !strchr && !__STDC__ */
60
61 extern int _rl_horizontal_scroll_mode;
62 extern int _rl_mark_modified_lines;
63 extern int _rl_bell_preference;
64 extern int _rl_meta_flag;
65 extern int _rl_convert_meta_chars_to_ascii;
66 extern int _rl_output_meta_chars;
67 extern int _rl_complete_show_all;
68 #if defined (PAREN_MATCHING)
69 extern int rl_blink_matching_paren;
70 #endif /* PAREN_MATCHING */
71 #if defined (VISIBLE_STATS)
72 extern int rl_visible_stats;
73 #endif /* VISIBLE_STATS */
74 extern int rl_complete_with_tilde_expansion;
75 extern int rl_completion_query_items;
76 #if defined (VI_MODE)
77 extern char *rl_vi_comment_begin;
78 #endif
79
80 extern int rl_explicit_arg;
81 extern int rl_editing_mode;
82 extern unsigned short _rl_parsing_conditionalized_out;
83 extern Keymap _rl_keymap;
84
85 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
86
87 extern char **rl_funmap_names ();
88
89 /* Forward declarations */
90 void rl_set_keymap_from_edit_mode ();
91
92 static int glean_key_from_name ();
93
94 #if defined (HAVE_STRCASECMP)
95 #define stricmp strcasecmp
96 #define strnicmp strncasecmp
97 #else
98 static int stricmp (), strnicmp ();
99 #endif
100
101 #if defined (STATIC_MALLOC)
102 static char *xmalloc (), *xrealloc ();
103 #else
104 extern char *xmalloc (), *xrealloc ();
105 #endif /* STATIC_MALLOC */
106
107 /* **************************************************************** */
108 /* */
109 /* Binding keys */
110 /* */
111 /* **************************************************************** */
112
113 /* rl_add_defun (char *name, Function *function, int key)
114 Add NAME to the list of named functions. Make FUNCTION be the function
115 that gets called. If KEY is not -1, then bind it. */
116 rl_add_defun (name, function, key)
117 char *name;
118 Function *function;
119 int key;
120 {
121 if (key != -1)
122 rl_bind_key (key, function);
123 rl_add_funmap_entry (name, function);
124 return 0;
125 }
126
127 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
128 int
129 rl_bind_key (key, function)
130 int key;
131 Function *function;
132 {
133 if (key < 0)
134 return (key);
135
136 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
137 {
138 if (_rl_keymap[ESC].type == ISKMAP)
139 {
140 Keymap escmap;
141
142 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
143 key = UNMETA (key);
144 escmap[key].type = ISFUNC;
145 escmap[key].function = function;
146 return (0);
147 }
148 return (key);
149 }
150
151 _rl_keymap[key].type = ISFUNC;
152 _rl_keymap[key].function = function;
153 return (0);
154 }
155
156 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
157 KEY. */
158 int
159 rl_bind_key_in_map (key, function, map)
160 int key;
161 Function *function;
162 Keymap map;
163 {
164 int result;
165 Keymap oldmap = _rl_keymap;
166
167 _rl_keymap = map;
168 result = rl_bind_key (key, function);
169 _rl_keymap = oldmap;
170 return (result);
171 }
172
173 /* Make KEY do nothing in the currently selected keymap.
174 Returns non-zero in case of error. */
175 int
176 rl_unbind_key (key)
177 int key;
178 {
179 return (rl_bind_key (key, (Function *)NULL));
180 }
181
182 /* Make KEY do nothing in MAP.
183 Returns non-zero in case of error. */
184 int
185 rl_unbind_key_in_map (key, map)
186 int key;
187 Keymap map;
188 {
189 return (rl_bind_key_in_map (key, (Function *)NULL, map));
190 }
191
192 /* Bind the key sequence represented by the string KEYSEQ to
193 FUNCTION. This makes new keymaps as necessary. The initial
194 place to do bindings is in MAP. */
195 rl_set_key (keyseq, function, map)
196 char *keyseq;
197 Function *function;
198 Keymap map;
199 {
200 return (rl_generic_bind (ISFUNC, keyseq, function, map));
201 }
202
203 /* Bind the key sequence represented by the string KEYSEQ to
204 the string of characters MACRO. This makes new keymaps as
205 necessary. The initial place to do bindings is in MAP. */
206 rl_macro_bind (keyseq, macro, map)
207 char *keyseq, *macro;
208 Keymap map;
209 {
210 char *macro_keys;
211 int macro_keys_len;
212
213 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
214
215 if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
216 {
217 free (macro_keys);
218 return -1;
219 }
220 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
221 return 0;
222 }
223
224 /* Bind the key sequence represented by the string KEYSEQ to
225 the arbitrary pointer DATA. TYPE says what kind of data is
226 pointed to by DATA, right now this can be a function (ISFUNC),
227 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
228 as necessary. The initial place to do bindings is in MAP. */
229 rl_generic_bind (type, keyseq, data, map)
230 int type;
231 char *keyseq, *data;
232 Keymap map;
233 {
234 char *keys;
235 int keys_len;
236 register int i;
237
238 /* If no keys to bind to, exit right away. */
239 if (!keyseq || !*keyseq)
240 {
241 if (type == ISMACR)
242 free (data);
243 return -1;
244 }
245
246 keys = xmalloc (1 + (2 * strlen (keyseq)));
247
248 /* Translate the ASCII representation of KEYSEQ into an array of
249 characters. Stuff the characters into KEYS, and the length of
250 KEYS into KEYS_LEN. */
251 if (rl_translate_keyseq (keyseq, keys, &keys_len))
252 {
253 free (keys);
254 return -1;
255 }
256
257 /* Bind keys, making new keymaps as necessary. */
258 for (i = 0; i < keys_len; i++)
259 {
260 int ic = (int) ((unsigned char)keys[i]);
261
262 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
263 {
264 ic = UNMETA (ic);
265 if (map[ESC].type == ISKMAP)
266 map = FUNCTION_TO_KEYMAP (map, ESC);
267 }
268
269 if ((i + 1) < keys_len)
270 {
271 if (map[ic].type != ISKMAP)
272 {
273 if (map[ic].type == ISMACR)
274 free ((char *)map[ic].function);
275
276 map[ic].type = ISKMAP;
277 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
278 }
279 map = FUNCTION_TO_KEYMAP (map, ic);
280 }
281 else
282 {
283 if (map[ic].type == ISMACR)
284 free ((char *)map[ic].function);
285
286 map[ic].function = KEYMAP_TO_FUNCTION (data);
287 map[ic].type = type;
288 }
289 }
290 free (keys);
291 return 0;
292 }
293
294 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
295 an array of characters. LEN gets the final length of ARRAY. Return
296 non-zero if there was an error parsing SEQ. */
297 rl_translate_keyseq (seq, array, len)
298 char *seq, *array;
299 int *len;
300 {
301 register int i, c, l = 0;
302
303 for (i = 0; c = seq[i]; i++)
304 {
305 if (c == '\\')
306 {
307 c = seq[++i];
308
309 if (!c)
310 break;
311
312 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
313 (c == 'e'))
314 {
315 /* Handle special case of backwards define. */
316 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
317 {
318 array[l++] = ESC;
319 i += 5;
320 array[l++] = CTRL (to_upper (seq[i]));
321 if (!seq[i])
322 i--;
323 continue;
324 }
325
326 switch (c)
327 {
328 case 'M':
329 i++;
330 array[l++] = ESC;
331 break;
332
333 case 'C':
334 i += 2;
335 /* Special hack for C-?... */
336 if (seq[i] == '?')
337 array[l++] = RUBOUT;
338 else
339 array[l++] = CTRL (to_upper (seq[i]));
340 break;
341
342 case 'e':
343 array[l++] = ESC;
344 }
345
346 continue;
347 }
348 }
349 array[l++] = c;
350 }
351
352 *len = l;
353 array[l] = '\0';
354 return (0);
355 }
356
357 /* Return a pointer to the function that STRING represents.
358 If STRING doesn't have a matching function, then a NULL pointer
359 is returned. */
360 Function *
361 rl_named_function (string)
362 char *string;
363 {
364 register int i;
365
366 rl_initialize_funmap ();
367
368 for (i = 0; funmap[i]; i++)
369 if (stricmp (funmap[i]->name, string) == 0)
370 return (funmap[i]->function);
371 return ((Function *)NULL);
372 }
373
374 /* Return the function (or macro) definition which would be invoked via
375 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
376 used. TYPE, if non-NULL, is a pointer to an int which will receive the
377 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
378 or ISMACR (macro). */
379 Function *
380 rl_function_of_keyseq (keyseq, map, type)
381 char *keyseq;
382 Keymap map;
383 int *type;
384 {
385 register int i;
386
387 if (!map)
388 map = _rl_keymap;
389
390 for (i = 0; keyseq && keyseq[i]; i++)
391 {
392 int ic = keyseq[i];
393
394 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
395 {
396 if (map[ESC].type != ISKMAP)
397 {
398 if (type)
399 *type = map[ESC].type;
400
401 return (map[ESC].function);
402 }
403 else
404 {
405 map = FUNCTION_TO_KEYMAP (map, ESC);
406 ic = UNMETA (ic);
407 }
408 }
409
410 if (map[ic].type == ISKMAP)
411 {
412 /* If this is the last key in the key sequence, return the
413 map. */
414 if (!keyseq[i + 1])
415 {
416 if (type)
417 *type = ISKMAP;
418
419 return (map[ic].function);
420 }
421 else
422 map = FUNCTION_TO_KEYMAP (map, ic);
423 }
424 else
425 {
426 if (type)
427 *type = map[ic].type;
428
429 return (map[ic].function);
430 }
431 }
432 return ((Function *) NULL);
433 }
434
435 /* The last key bindings file read. */
436 static char *last_readline_init_file = (char *)NULL;
437
438 /* Re-read the current keybindings file. */
439 rl_re_read_init_file (count, ignore)
440 int count, ignore;
441 {
442 int r;
443 r = rl_read_init_file ((char *)NULL);
444 rl_set_keymap_from_edit_mode ();
445 return r;
446 }
447
448 /* Do key bindings from a file. If FILENAME is NULL it defaults
449 to the first non-null filename from this list:
450 1. the filename used for the previous call
451 2. the value of the shell variable `INPUTRC'
452 3. ~/.inputrc
453 If the file existed and could be opened and read, 0 is returned,
454 otherwise errno is returned. */
455 int
456 rl_read_init_file (filename)
457 char *filename;
458 {
459 register int i;
460 char *buffer, *openname, *line, *end;
461 struct stat finfo;
462 int file;
463
464 /* Default the filename. */
465 if (!filename)
466 {
467 filename = last_readline_init_file;
468 if (!filename)
469 filename = getenv ("INPUTRC");
470 if (!filename)
471 filename = DEFAULT_INPUTRC;
472 }
473
474 if (!*filename)
475 filename = DEFAULT_INPUTRC;
476
477 openname = tilde_expand (filename);
478
479 if ((stat (openname, &finfo) < 0) ||
480 (file = open (openname, O_RDONLY, 0666)) < 0)
481 {
482 free (openname);
483 return (errno);
484 }
485 else
486 free (openname);
487
488 if (filename != last_readline_init_file)
489 {
490 if (last_readline_init_file)
491 free (last_readline_init_file);
492
493 last_readline_init_file = savestring (filename);
494 }
495
496 /* Read the file into BUFFER. */
497 buffer = (char *)xmalloc ((int)finfo.st_size + 1);
498 i = read (file, buffer, finfo.st_size);
499 close (file);
500
501 if (i != finfo.st_size)
502 return (errno);
503
504 /* Loop over the lines in the file. Lines that start with `#' are
505 comments; all other lines are commands for readline initialization. */
506 line = buffer;
507 end = buffer + finfo.st_size;
508 while (line < end)
509 {
510 /* Find the end of this line. */
511 for (i = 0; line + i != end && line[i] != '\n'; i++);
512
513 /* Mark end of line. */
514 line[i] = '\0';
515
516 /* Skip leading whitespace. */
517 while (*line && whitespace (*line))
518 line++;
519
520 /* If the line is not a comment, then parse it. */
521 if (*line && *line != '#')
522 rl_parse_and_bind (line);
523
524 /* Move to the next line. */
525 line += i + 1;
526 }
527 free (buffer);
528 return (0);
529 }
530
531 /* **************************************************************** */
532 /* */
533 /* Parser Directives */
534 /* */
535 /* **************************************************************** */
536
537 /* Conditionals. */
538
539 /* Calling programs set this to have their argv[0]. */
540 char *rl_readline_name = "other";
541
542 /* Stack of previous values of parsing_conditionalized_out. */
543 static unsigned char *if_stack = (unsigned char *)NULL;
544 static int if_stack_depth = 0;
545 static int if_stack_size = 0;
546
547 /* Push _rl_parsing_conditionalized_out, and set parser state based
548 on ARGS. */
549 static int
550 parser_if (args)
551 char *args;
552 {
553 register int i;
554
555 /* Push parser state. */
556 if (if_stack_depth + 1 >= if_stack_size)
557 {
558 if (!if_stack)
559 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
560 else
561 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
562 }
563 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
564
565 /* If parsing is turned off, then nothing can turn it back on except
566 for finding the matching endif. In that case, return right now. */
567 if (_rl_parsing_conditionalized_out)
568 return 0;
569
570 /* Isolate first argument. */
571 for (i = 0; args[i] && !whitespace (args[i]); i++);
572
573 if (args[i])
574 args[i++] = '\0';
575
576 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
577 isn't term=foo, or mode=emacs, then check to see if the first
578 word in ARGS is the same as the value stored in rl_readline_name. */
579 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
580 {
581 char *tem, *tname;
582
583 /* Terminals like "aaa-60" are equivalent to "aaa". */
584 tname = savestring (rl_terminal_name);
585 tem = strchr (tname, '-');
586 if (tem)
587 *tem = '\0';
588
589 /* Test the `long' and `short' forms of the terminal name so that
590 if someone has a `sun-cmd' and does not want to have bindings
591 that will be executed if the terminal is a `sun', they can put
592 `$if term=sun-cmd' into their .inputrc. */
593 if ((stricmp (args + 5, tname) == 0) ||
594 (stricmp (args + 5, rl_terminal_name) == 0))
595 _rl_parsing_conditionalized_out = 0;
596 else
597 _rl_parsing_conditionalized_out = 1;
598
599 free (tname);
600 }
601 #if defined (VI_MODE)
602 else if (strnicmp (args, "mode=", 5) == 0)
603 {
604 int mode;
605
606 if (stricmp (args + 5, "emacs") == 0)
607 mode = emacs_mode;
608 else if (stricmp (args + 5, "vi") == 0)
609 mode = vi_mode;
610 else
611 mode = no_mode;
612
613 if (mode == rl_editing_mode)
614 _rl_parsing_conditionalized_out = 0;
615 else
616 _rl_parsing_conditionalized_out = 1;
617 }
618 #endif /* VI_MODE */
619 /* Check to see if the first word in ARGS is the same as the
620 value stored in rl_readline_name. */
621 else if (stricmp (args, rl_readline_name) == 0)
622 _rl_parsing_conditionalized_out = 0;
623 else
624 _rl_parsing_conditionalized_out = 1;
625 return 0;
626 }
627
628 /* Invert the current parser state if there is anything on the stack. */
629 static int
630 parser_else (args)
631 char *args;
632 {
633 register int i;
634
635 if (!if_stack_depth)
636 {
637 /* Error message? */
638 return 0;
639 }
640
641 /* Check the previous (n - 1) levels of the stack to make sure that
642 we haven't previously turned off parsing. */
643 for (i = 0; i < if_stack_depth - 1; i++)
644 if (if_stack[i] == 1)
645 return 0;
646
647 /* Invert the state of parsing if at top level. */
648 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
649 return 0;
650 }
651
652 /* Terminate a conditional, popping the value of
653 _rl_parsing_conditionalized_out from the stack. */
654 static int
655 parser_endif (args)
656 char *args;
657 {
658 if (if_stack_depth)
659 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
660 else
661 {
662 /* *** What, no error message? *** */
663 }
664 return 0;
665 }
666
667 /* Associate textual names with actual functions. */
668 static struct {
669 char *name;
670 Function *function;
671 } parser_directives [] = {
672 { "if", parser_if },
673 { "endif", parser_endif },
674 { "else", parser_else },
675 { (char *)0x0, (Function *)0x0 }
676 };
677
678 /* Handle a parser directive. STATEMENT is the line of the directive
679 without any leading `$'. */
680 static int
681 handle_parser_directive (statement)
682 char *statement;
683 {
684 register int i;
685 char *directive, *args;
686
687 /* Isolate the actual directive. */
688
689 /* Skip whitespace. */
690 for (i = 0; whitespace (statement[i]); i++);
691
692 directive = &statement[i];
693
694 for (; statement[i] && !whitespace (statement[i]); i++);
695
696 if (statement[i])
697 statement[i++] = '\0';
698
699 for (; statement[i] && whitespace (statement[i]); i++);
700
701 args = &statement[i];
702
703 /* Lookup the command, and act on it. */
704 for (i = 0; parser_directives[i].name; i++)
705 if (stricmp (directive, parser_directives[i].name) == 0)
706 {
707 (*parser_directives[i].function) (args);
708 return (0);
709 }
710
711 /* *** Should an error message be output? */
712 return (1);
713 }
714
715 static int substring_member_of_array ();
716
717 /* Read the binding command from STRING and perform it.
718 A key binding command looks like: Keyname: function-name\0,
719 a variable binding command looks like: set variable value.
720 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
721 rl_parse_and_bind (string)
722 char *string;
723 {
724 char *funname, *kname;
725 register int c, i;
726 int key, equivalency;
727
728 while (string && whitespace (*string))
729 string++;
730
731 if (!string || !*string || *string == '#')
732 return 0;
733
734 /* If this is a parser directive, act on it. */
735 if (*string == '$')
736 {
737 handle_parser_directive (&string[1]);
738 return 0;
739 }
740
741 /* If we aren't supposed to be parsing right now, then we're done. */
742 if (_rl_parsing_conditionalized_out)
743 return 0;
744
745 i = 0;
746 /* If this keyname is a complex key expression surrounded by quotes,
747 advance to after the matching close quote. This code allows the
748 backslash to quote characters in the key expression. */
749 if (*string == '"')
750 {
751 int passc = 0;
752
753 for (i = 1; c = string[i]; i++)
754 {
755 if (passc)
756 {
757 passc = 0;
758 continue;
759 }
760
761 if (c == '\\')
762 {
763 passc++;
764 continue;
765 }
766
767 if (c == '"')
768 break;
769 }
770 }
771
772 /* Advance to the colon (:) or whitespace which separates the two objects. */
773 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
774
775 equivalency = (c == ':' && string[i + 1] == '=');
776
777 /* Mark the end of the command (or keyname). */
778 if (string[i])
779 string[i++] = '\0';
780
781 /* If doing assignment, skip the '=' sign as well. */
782 if (equivalency)
783 string[i++] = '\0';
784
785 /* If this is a command to set a variable, then do that. */
786 if (stricmp (string, "set") == 0)
787 {
788 char *var = string + i;
789 char *value;
790
791 /* Make VAR point to start of variable name. */
792 while (*var && whitespace (*var)) var++;
793
794 /* Make value point to start of value string. */
795 value = var;
796 while (*value && !whitespace (*value)) value++;
797 if (*value)
798 *value++ = '\0';
799 while (*value && whitespace (*value)) value++;
800
801 rl_variable_bind (var, value);
802 return 0;
803 }
804
805 /* Skip any whitespace between keyname and funname. */
806 for (; string[i] && whitespace (string[i]); i++);
807 funname = &string[i];
808
809 /* Now isolate funname.
810 For straight function names just look for whitespace, since
811 that will signify the end of the string. But this could be a
812 macro definition. In that case, the string is quoted, so skip
813 to the matching delimiter. We allow the backslash to quote the
814 delimiter characters in the macro body. */
815 /* This code exists to allow whitespace in macro expansions, which
816 would otherwise be gobbled up by the next `for' loop.*/
817 /* XXX - it may be desirable to allow backslash quoting only if " is
818 the quoted string delimiter, like the shell. */
819 if (*funname == '\'' || *funname == '"')
820 {
821 int delimiter = string[i++];
822 int passc = 0;
823
824 for (; c = string[i]; i++)
825 {
826 if (passc)
827 {
828 passc = 0;
829 continue;
830 }
831
832 if (c == '\\')
833 {
834 passc = 1;
835 continue;
836 }
837
838 if (c == delimiter)
839 break;
840 }
841 if (c)
842 i++;
843 }
844
845 /* Advance to the end of the string. */
846 for (; string[i] && !whitespace (string[i]); i++);
847
848 /* No extra whitespace at the end of the string. */
849 string[i] = '\0';
850
851 /* Handle equivalency bindings here. Make the left-hand side be exactly
852 whatever the right-hand evaluates to, including keymaps. */
853 if (equivalency)
854 {
855 return 0;
856 }
857
858 /* If this is a new-style key-binding, then do the binding with
859 rl_set_key (). Otherwise, let the older code deal with it. */
860 if (*string == '"')
861 {
862 char *seq = xmalloc (1 + strlen (string));
863 register int j, k = 0;
864 int passc = 0;
865
866 for (j = 1; string[j]; j++)
867 {
868 /* Allow backslash to quote characters, but leave them in place.
869 This allows a string to end with a backslash quoting another
870 backslash, or with a backslash quoting a double quote. The
871 backslashes are left in place for rl_translate_keyseq (). */
872 if (passc || (string[j] == '\\'))
873 {
874 seq[k++] = string[j];
875 passc = !passc;
876 continue;
877 }
878
879 if (string[j] == '"')
880 break;
881
882 seq[k++] = string[j];
883 }
884 seq[k] = '\0';
885
886 /* Binding macro? */
887 if (*funname == '\'' || *funname == '"')
888 {
889 j = strlen (funname);
890
891 /* Remove the delimiting quotes from each end of FUNNAME. */
892 if (j && funname[j - 1] == *funname)
893 funname[j - 1] = '\0';
894
895 rl_macro_bind (seq, &funname[1], _rl_keymap);
896 }
897 else
898 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
899
900 free (seq);
901 return 0;
902 }
903
904 /* Get the actual character we want to deal with. */
905 kname = strrchr (string, '-');
906 if (!kname)
907 kname = string;
908 else
909 kname++;
910
911 key = glean_key_from_name (kname);
912
913 /* Add in control and meta bits. */
914 if (substring_member_of_array (string, possible_control_prefixes))
915 key = CTRL (to_upper (key));
916
917 if (substring_member_of_array (string, possible_meta_prefixes))
918 key = META (key);
919
920 /* Temporary. Handle old-style keyname with macro-binding. */
921 if (*funname == '\'' || *funname == '"')
922 {
923 char seq[2];
924 int fl = strlen (funname);
925
926 seq[0] = key; seq[1] = '\0';
927 if (fl && funname[fl - 1] == *funname)
928 funname[fl - 1] = '\0';
929
930 rl_macro_bind (seq, &funname[1], _rl_keymap);
931 }
932 #if defined (PREFIX_META_HACK)
933 /* Ugly, but working hack to keep prefix-meta around. */
934 else if (stricmp (funname, "prefix-meta") == 0)
935 {
936 char seq[2];
937
938 seq[0] = key;
939 seq[1] = '\0';
940 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
941 }
942 #endif /* PREFIX_META_HACK */
943 else
944 rl_bind_key (key, rl_named_function (funname));
945 return 0;
946 }
947
948 /* Simple structure for boolean readline variables (i.e., those that can
949 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
950 false. */
951
952 static struct {
953 char *name;
954 int *value;
955 } boolean_varlist [] = {
956 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
957 { "mark-modified-lines", &_rl_mark_modified_lines },
958 { "meta-flag", &_rl_meta_flag },
959 #if defined (PAREN_MATCHING)
960 { "blink-matching-paren", &rl_blink_matching_paren },
961 #endif
962 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
963 { "show-all-if-ambiguous", &_rl_complete_show_all },
964 { "output-meta", &_rl_output_meta_chars },
965 #if defined (VISIBLE_STATS)
966 { "visible-stats", &rl_visible_stats },
967 #endif /* VISIBLE_STATS */
968 { "expand-tilde", &rl_complete_with_tilde_expansion },
969 { (char *)NULL, (int *)NULL }
970 };
971
972 rl_variable_bind (name, value)
973 char *name, *value;
974 {
975 register int i;
976
977 /* Check for simple variables first. */
978 for (i = 0; boolean_varlist[i].name; i++)
979 {
980 if (stricmp (name, boolean_varlist[i].name) == 0)
981 {
982 /* A variable is TRUE if the "value" is "on", "1" or "". */
983 if ((!*value) ||
984 (stricmp (value, "On") == 0) ||
985 (value[0] == '1' && value[1] == '\0'))
986 *boolean_varlist[i].value = 1;
987 else
988 *boolean_varlist[i].value = 0;
989 return 0;
990 }
991 }
992
993 /* Not a boolean variable, so check for specials. */
994
995 /* Editing mode change? */
996 if (stricmp (name, "editing-mode") == 0)
997 {
998 if (strnicmp (value, "vi", 2) == 0)
999 {
1000 #if defined (VI_MODE)
1001 _rl_keymap = vi_insertion_keymap;
1002 rl_editing_mode = vi_mode;
1003 #endif /* VI_MODE */
1004 }
1005 else if (strnicmp (value, "emacs", 5) == 0)
1006 {
1007 _rl_keymap = emacs_standard_keymap;
1008 rl_editing_mode = emacs_mode;
1009 }
1010 }
1011
1012 /* Comment string change? */
1013 else if (stricmp (name, "comment-begin") == 0)
1014 {
1015 #if defined (VI_MODE)
1016 if (*value)
1017 {
1018 if (rl_vi_comment_begin)
1019 free (rl_vi_comment_begin);
1020
1021 rl_vi_comment_begin = savestring (value);
1022 }
1023 #endif /* VI_MODE */
1024 }
1025 else if (stricmp (name, "completion-query-items") == 0)
1026 {
1027 int nval = 100;
1028 if (*value)
1029 {
1030 nval = atoi (value);
1031 if (nval < 0)
1032 nval = 0;
1033 }
1034 rl_completion_query_items = nval;
1035 }
1036 else if (stricmp (name, "keymap") == 0)
1037 {
1038 Keymap kmap;
1039 kmap = rl_get_keymap_by_name (value);
1040 if (kmap)
1041 rl_set_keymap (kmap);
1042 }
1043 else if (stricmp (name, "bell-style") == 0)
1044 {
1045 if (!*value)
1046 _rl_bell_preference = AUDIBLE_BELL;
1047 else
1048 {
1049 if (stricmp (value, "none") == 0 || stricmp (value, "off") == 0)
1050 _rl_bell_preference = NO_BELL;
1051 else if (stricmp (value, "audible") == 0 || stricmp (value, "on") == 0)
1052 _rl_bell_preference = AUDIBLE_BELL;
1053 else if (stricmp (value, "visible") == 0)
1054 _rl_bell_preference = VISIBLE_BELL;
1055 }
1056 }
1057 else if (stricmp (name, "prefer-visible-bell") == 0)
1058 {
1059 /* Backwards compatibility. */
1060 if (*value && (stricmp (value, "on") == 0 ||
1061 (*value == '1' && !value[1])))
1062 _rl_bell_preference = VISIBLE_BELL;
1063 else
1064 _rl_bell_preference = AUDIBLE_BELL;
1065 }
1066
1067 return 0;
1068 }
1069
1070 /* Return the character which matches NAME.
1071 For example, `Space' returns ' '. */
1072
1073 typedef struct {
1074 char *name;
1075 int value;
1076 } assoc_list;
1077
1078 static assoc_list name_key_alist[] = {
1079 { "DEL", 0x7f },
1080 { "ESC", '\033' },
1081 { "Escape", '\033' },
1082 { "LFD", '\n' },
1083 { "Newline", '\n' },
1084 { "RET", '\r' },
1085 { "Return", '\r' },
1086 { "Rubout", 0x7f },
1087 { "SPC", ' ' },
1088 { "Space", ' ' },
1089 { "Tab", 0x09 },
1090 { (char *)0x0, 0 }
1091 };
1092
1093 static int
1094 glean_key_from_name (name)
1095 char *name;
1096 {
1097 register int i;
1098
1099 for (i = 0; name_key_alist[i].name; i++)
1100 if (stricmp (name, name_key_alist[i].name) == 0)
1101 return (name_key_alist[i].value);
1102
1103 return (*(unsigned char *)name); /* XXX was return (*name) */
1104 }
1105
1106 /* Auxiliary functions to manage keymaps. */
1107 static struct {
1108 char *name;
1109 Keymap map;
1110 } keymap_names[] = {
1111 { "emacs", emacs_standard_keymap },
1112 { "emacs-standard", emacs_standard_keymap },
1113 { "emacs-meta", emacs_meta_keymap },
1114 { "emacs-ctlx", emacs_ctlx_keymap },
1115 #if defined (VI_MODE)
1116 { "vi", vi_movement_keymap },
1117 { "vi-move", vi_movement_keymap },
1118 { "vi-command", vi_movement_keymap },
1119 { "vi-insert", vi_insertion_keymap },
1120 #endif /* VI_MODE */
1121 { (char *)0x0, (Keymap)0x0 }
1122 };
1123
1124 Keymap
1125 rl_get_keymap_by_name (name)
1126 char *name;
1127 {
1128 register int i;
1129
1130 for (i = 0; keymap_names[i].name; i++)
1131 if (strcmp (name, keymap_names[i].name) == 0)
1132 return (keymap_names[i].map);
1133 return ((Keymap) NULL);
1134 }
1135
1136 void
1137 rl_set_keymap (map)
1138 Keymap map;
1139 {
1140 if (map)
1141 _rl_keymap = map;
1142 }
1143
1144 Keymap
1145 rl_get_keymap ()
1146 {
1147 return (_rl_keymap);
1148 }
1149
1150 void
1151 rl_set_keymap_from_edit_mode ()
1152 {
1153 if (rl_editing_mode == emacs_mode)
1154 _rl_keymap = emacs_standard_keymap;
1155 #if defined (VI_MODE)
1156 else if (rl_editing_mode == vi_mode)
1157 _rl_keymap = vi_insertion_keymap;
1158 #endif /* VI_MODE */
1159 }
1160
1161 /* **************************************************************** */
1162 /* */
1163 /* Key Binding and Function Information */
1164 /* */
1165 /* **************************************************************** */
1166
1167 /* Each of the following functions produces information about the
1168 state of keybindings and functions known to Readline. The info
1169 is always printed to rl_outstream, and in such a way that it can
1170 be read back in (i.e., passed to rl_parse_and_bind (). */
1171
1172 /* Print the names of functions known to Readline. */
1173 void
1174 rl_list_funmap_names (ignore)
1175 int ignore;
1176 {
1177 register int i;
1178 char **funmap_names;
1179
1180 funmap_names = rl_funmap_names ();
1181
1182 if (!funmap_names)
1183 return;
1184
1185 for (i = 0; funmap_names[i]; i++)
1186 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1187
1188 free (funmap_names);
1189 }
1190
1191 /* Return a NULL terminated array of strings which represent the key
1192 sequences that are used to invoke FUNCTION in MAP. */
1193 char **
1194 rl_invoking_keyseqs_in_map (function, map)
1195 Function *function;
1196 Keymap map;
1197 {
1198 register int key;
1199 char **result;
1200 int result_index, result_size;
1201
1202 result = (char **)NULL;
1203 result_index = result_size = 0;
1204
1205 for (key = 0; key < 128; key++)
1206 {
1207 switch (map[key].type)
1208 {
1209 case ISMACR:
1210 /* Macros match, if, and only if, the pointers are identical.
1211 Thus, they are treated exactly like functions in here. */
1212 case ISFUNC:
1213 /* If the function in the keymap is the one we are looking for,
1214 then add the current KEY to the list of invoking keys. */
1215 if (map[key].function == function)
1216 {
1217 char *keyname = (char *)xmalloc (5);
1218
1219 if (CTRL_P (key))
1220 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
1221 else if (key == RUBOUT)
1222 sprintf (keyname, "\\C-?");
1223 else if (key == '\\' || key == '"')
1224 {
1225 keyname[0] = '\\';
1226 keyname[1] = (char) key;
1227 keyname[2] = '\0';
1228 }
1229 else
1230 {
1231 keyname[0] = (char) key;
1232 keyname[1] = '\0';
1233 }
1234
1235 if (result_index + 2 > result_size)
1236 result = (char **) xrealloc
1237 (result, (result_size += 10) * sizeof (char *));
1238
1239 result[result_index++] = keyname;
1240 result[result_index] = (char *)NULL;
1241 }
1242 break;
1243
1244 case ISKMAP:
1245 {
1246 char **seqs = (char **)NULL;
1247
1248 /* Find the list of keyseqs in this map which have FUNCTION as
1249 their target. Add the key sequences found to RESULT. */
1250 if (map[key].function)
1251 seqs =
1252 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1253
1254 if (seqs)
1255 {
1256 register int i;
1257
1258 for (i = 0; seqs[i]; i++)
1259 {
1260 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1261
1262 if (key == ESC)
1263 sprintf (keyname, "\\e");
1264 else if (CTRL_P (key))
1265 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
1266 else if (key == RUBOUT)
1267 sprintf (keyname, "\\C-?");
1268 else if (key == '\\' || key == '"')
1269 {
1270 keyname[0] = '\\';
1271 keyname[1] = (char) key;
1272 keyname[2] = '\0';
1273 }
1274 else
1275 {
1276 keyname[0] = (char) key;
1277 keyname[1] = '\0';
1278 }
1279
1280 strcat (keyname, seqs[i]);
1281 free (seqs[i]);
1282
1283 if (result_index + 2 > result_size)
1284 result = (char **) xrealloc
1285 (result, (result_size += 10) * sizeof (char *));
1286
1287 result[result_index++] = keyname;
1288 result[result_index] = (char *)NULL;
1289 }
1290
1291 free (seqs);
1292 }
1293 }
1294 break;
1295 }
1296 }
1297 return (result);
1298 }
1299
1300 /* Return a NULL terminated array of strings which represent the key
1301 sequences that can be used to invoke FUNCTION using the current keymap. */
1302 char **
1303 rl_invoking_keyseqs (function)
1304 Function *function;
1305 {
1306 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1307 }
1308
1309 /* Print all of the current functions and their bindings to
1310 rl_outstream. If an explicit argument is given, then print
1311 the output in such a way that it can be read back in. */
1312 int
1313 rl_dump_functions (count, key)
1314 int count, key;
1315 {
1316 rl_function_dumper (rl_explicit_arg);
1317 rl_on_new_line ();
1318 return (0);
1319 }
1320
1321 /* Print all of the functions and their bindings to rl_outstream. If
1322 PRINT_READABLY is non-zero, then print the output in such a way
1323 that it can be read back in. */
1324 void
1325 rl_function_dumper (print_readably)
1326 int print_readably;
1327 {
1328 register int i;
1329 char **names;
1330 char *name;
1331
1332 names = rl_funmap_names ();
1333
1334 fprintf (rl_outstream, "\n");
1335
1336 for (i = 0; name = names[i]; i++)
1337 {
1338 Function *function;
1339 char **invokers;
1340
1341 function = rl_named_function (name);
1342 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1343
1344 if (print_readably)
1345 {
1346 if (!invokers)
1347 fprintf (rl_outstream, "# %s (not bound)\n", name);
1348 else
1349 {
1350 register int j;
1351
1352 for (j = 0; invokers[j]; j++)
1353 {
1354 fprintf (rl_outstream, "\"%s\": %s\n",
1355 invokers[j], name);
1356 free (invokers[j]);
1357 }
1358
1359 free (invokers);
1360 }
1361 }
1362 else
1363 {
1364 if (!invokers)
1365 fprintf (rl_outstream, "%s is not bound to any keys\n",
1366 name);
1367 else
1368 {
1369 register int j;
1370
1371 fprintf (rl_outstream, "%s can be found on ", name);
1372
1373 for (j = 0; invokers[j] && j < 5; j++)
1374 {
1375 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1376 invokers[j + 1] ? ", " : ".\n");
1377 }
1378
1379 if (j == 5 && invokers[j])
1380 fprintf (rl_outstream, "...\n");
1381
1382 for (j = 0; invokers[j]; j++)
1383 free (invokers[j]);
1384
1385 free (invokers);
1386 }
1387 }
1388 }
1389 }
1390
1391 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1392 void
1393 _rl_bind_if_unbound (keyseq, default_func)
1394 char *keyseq;
1395 Function *default_func;
1396 {
1397 Function *func;
1398
1399 if (keyseq)
1400 {
1401 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1402 if (!func || func == rl_do_lowercase_version)
1403 rl_set_key (keyseq, default_func, _rl_keymap);
1404 }
1405 }
1406
1407 /* **************************************************************** */
1408 /* */
1409 /* String Utility Functions */
1410 /* */
1411 /* **************************************************************** */
1412
1413 static char *strindex ();
1414
1415 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1416 static int
1417 substring_member_of_array (string, array)
1418 char *string, **array;
1419 {
1420 while (*array)
1421 {
1422 if (strindex (string, *array))
1423 return (1);
1424 array++;
1425 }
1426 return (0);
1427 }
1428
1429 #if !defined (HAVE_STRCASECMP)
1430 /* Whoops, Unix doesn't have strnicmp. */
1431
1432 /* Compare at most COUNT characters from string1 to string2. Case
1433 doesn't matter. */
1434 static int
1435 strnicmp (string1, string2, count)
1436 char *string1, *string2;
1437 int count;
1438 {
1439 register char ch1, ch2;
1440
1441 while (count)
1442 {
1443 ch1 = *string1++;
1444 ch2 = *string2++;
1445 if (to_upper(ch1) == to_upper(ch2))
1446 count--;
1447 else
1448 break;
1449 }
1450 return (count);
1451 }
1452
1453 /* strcmp (), but caseless. */
1454 static int
1455 stricmp (string1, string2)
1456 char *string1, *string2;
1457 {
1458 register char ch1, ch2;
1459
1460 while (*string1 && *string2)
1461 {
1462 ch1 = *string1++;
1463 ch2 = *string2++;
1464 if (to_upper(ch1) != to_upper(ch2))
1465 return (1);
1466 }
1467 return (*string1 - *string2);
1468 }
1469 #endif /* !HAVE_STRCASECMP */
1470
1471 /* Determine if s2 occurs in s1. If so, return a pointer to the
1472 match in s1. The compare is case insensitive. */
1473 static char *
1474 strindex (s1, s2)
1475 register char *s1, *s2;
1476 {
1477 register int i, l = strlen (s2);
1478 register int len = strlen (s1);
1479
1480 for (i = 0; (len - i) >= l; i++)
1481 if (strnicmp (s1 + i, s2, l) == 0)
1482 return (s1 + i);
1483 return ((char *)NULL);
1484 }