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