00001 /* bind.c -- key binding and startup file support for the readline library. */ 00002 00003 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc. 00004 00005 This file is part of the GNU Readline Library, a library for 00006 reading lines of text with interactive input and history editing. 00007 00008 The GNU Readline Library is free software; you can redistribute it 00009 and/or modify it under the terms of the GNU General Public License 00010 as published by the Free Software Foundation; either version 2, or 00011 (at your option) any later version. 00012 00013 The GNU Readline Library is distributed in the hope that it will be 00014 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00015 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 The GNU General Public License is often shipped with GNU software, and 00019 is generally kept in a file called COPYING or LICENSE. If you do not 00020 have a copy of the license, write to the Free Software Foundation, 00021 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 00022 00023 #define READLINE_LIBRARY 00024 00025 #if defined (__TANDEM) 00026 # include <floss.h> 00027 #endif 00028 00029 #include "config_readline.h" 00030 00031 #include <stdio.h> 00032 #include <sys/types.h> 00033 #include <fcntl.h> 00034 #if defined (HAVE_SYS_FILE_H) 00035 # include <sys/file.h> 00036 #endif /* HAVE_SYS_FILE_H */ 00037 00038 #if defined (HAVE_UNISTD_H) 00039 # include <unistd.h> 00040 #endif /* HAVE_UNISTD_H */ 00041 00042 #if defined (HAVE_STDLIB_H) 00043 # include <stdlib.h> 00044 #else 00045 # include "ansi_stdlib.h" 00046 #endif /* HAVE_STDLIB_H */ 00047 00048 #include <errno.h> 00049 00050 #if !defined (errno) 00051 extern int errno; 00052 #endif /* !errno */ 00053 00054 #include "posixstat.h" 00055 00056 /* System-specific feature definitions and include files. */ 00057 #include "rldefs.h" 00058 00059 /* Some standard library routines. */ 00060 #include "readline.h" 00061 #include "history.h" 00062 00063 #include "rlprivate.h" 00064 #include "rlshell.h" 00065 #include "xmalloc.h" 00066 00067 #if !defined (strchr) && !defined (__STDC__) 00068 extern char *strchr (), *strrchr (); 00069 #endif /* !strchr && !__STDC__ */ 00070 00071 /* Variables exported by this file. */ 00072 Keymap rl_binding_keymap; 00073 00074 static char *_rl_read_file PARAMS((char *, size_t *)); 00075 static void _rl_init_file_error PARAMS((const char *)); 00076 static int _rl_read_init_file PARAMS((const char *, int)); 00077 static int glean_key_from_name PARAMS((char *)); 00078 static int substring_member_of_array PARAMS((char *, const char **)); 00079 00080 static int currently_reading_init_file; 00081 00082 /* used only in this file */ 00083 static int _rl_prefer_visible_bell = 1; 00084 00085 /* **************************************************************** */ 00086 /* */ 00087 /* Binding keys */ 00088 /* */ 00089 /* **************************************************************** */ 00090 00091 /* rl_add_defun (char *name, rl_command_func_t *function, int key) 00092 Add NAME to the list of named functions. Make FUNCTION be the function 00093 that gets called. If KEY is not -1, then bind it. */ 00094 int 00095 rl_add_defun (name, function, key) 00096 const char *name; 00097 rl_command_func_t *function; 00098 int key; 00099 { 00100 if (key != -1) 00101 rl_bind_key (key, function); 00102 rl_add_funmap_entry (name, function); 00103 return 0; 00104 } 00105 00106 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */ 00107 int 00108 rl_bind_key (key, function) 00109 int key; 00110 rl_command_func_t *function; 00111 { 00112 if (key < 0) 00113 return (key); 00114 00115 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii) 00116 { 00117 if (_rl_keymap[ESC].type == ISKMAP) 00118 { 00119 Keymap escmap; 00120 00121 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC); 00122 key = UNMETA (key); 00123 escmap[key].type = ISFUNC; 00124 escmap[key].function = function; 00125 return (0); 00126 } 00127 return (key); 00128 } 00129 00130 _rl_keymap[key].type = ISFUNC; 00131 _rl_keymap[key].function = function; 00132 rl_binding_keymap = _rl_keymap; 00133 return (0); 00134 } 00135 00136 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid 00137 KEY. */ 00138 int 00139 rl_bind_key_in_map (key, function, map) 00140 int key; 00141 rl_command_func_t *function; 00142 Keymap map; 00143 { 00144 int result; 00145 Keymap oldmap; 00146 00147 oldmap = _rl_keymap; 00148 _rl_keymap = map; 00149 result = rl_bind_key (key, function); 00150 _rl_keymap = oldmap; 00151 return (result); 00152 } 00153 00154 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right 00155 now, this is always used to attempt to bind the arrow keys, hence the 00156 check for rl_vi_movement_mode. */ 00157 int 00158 rl_bind_key_if_unbound_in_map (key, default_func, kmap) 00159 int key; 00160 rl_command_func_t *default_func; 00161 Keymap kmap; 00162 { 00163 char keyseq[2]; 00164 00165 keyseq[0] = (unsigned char)key; 00166 keyseq[1] = '\0'; 00167 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap)); 00168 } 00169 00170 int 00171 rl_bind_key_if_unbound (key, default_func) 00172 int key; 00173 rl_command_func_t *default_func; 00174 { 00175 char keyseq[2]; 00176 00177 keyseq[0] = (unsigned char)key; 00178 keyseq[1] = '\0'; 00179 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap)); 00180 } 00181 00182 /* Make KEY do nothing in the currently selected keymap. 00183 Returns non-zero in case of error. */ 00184 int 00185 rl_unbind_key (key) 00186 int key; 00187 { 00188 return (rl_bind_key (key, (rl_command_func_t *)NULL)); 00189 } 00190 00191 /* Make KEY do nothing in MAP. 00192 Returns non-zero in case of error. */ 00193 int 00194 rl_unbind_key_in_map (key, map) 00195 int key; 00196 Keymap map; 00197 { 00198 return (rl_bind_key_in_map (key, (rl_command_func_t *)NULL, map)); 00199 } 00200 00201 /* Unbind all keys bound to FUNCTION in MAP. */ 00202 int 00203 rl_unbind_function_in_map (func, map) 00204 rl_command_func_t *func; 00205 Keymap map; 00206 { 00207 register int i, rval; 00208 00209 for (i = rval = 0; i < KEYMAP_SIZE; i++) 00210 { 00211 if (map[i].type == ISFUNC && map[i].function == func) 00212 { 00213 map[i].function = (rl_command_func_t *)NULL; 00214 rval = 1; 00215 } 00216 } 00217 return rval; 00218 } 00219 00220 int 00221 rl_unbind_command_in_map (command, map) 00222 const char *command; 00223 Keymap map; 00224 { 00225 rl_command_func_t *func; 00226 00227 func = rl_named_function (command); 00228 if (func == 0) 00229 return 0; 00230 return (rl_unbind_function_in_map (func, map)); 00231 } 00232 00233 /* Bind the key sequence represented by the string KEYSEQ to 00234 FUNCTION, starting in the current keymap. This makes new 00235 keymaps as necessary. */ 00236 int 00237 rl_bind_keyseq (keyseq, function) 00238 const char *keyseq; 00239 rl_command_func_t *function; 00240 { 00241 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, _rl_keymap)); 00242 } 00243 00244 /* Bind the key sequence represented by the string KEYSEQ to 00245 FUNCTION. This makes new keymaps as necessary. The initial 00246 place to do bindings is in MAP. */ 00247 int 00248 rl_bind_keyseq_in_map (keyseq, function, map) 00249 const char *keyseq; 00250 rl_command_func_t *function; 00251 Keymap map; 00252 { 00253 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map)); 00254 } 00255 00256 /* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */ 00257 int 00258 rl_set_key (keyseq, function, map) 00259 const char *keyseq; 00260 rl_command_func_t *function; 00261 Keymap map; 00262 { 00263 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map)); 00264 } 00265 00266 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right 00267 now, this is always used to attempt to bind the arrow keys, hence the 00268 check for rl_vi_movement_mode. */ 00269 int 00270 rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, kmap) 00271 const char *keyseq; 00272 rl_command_func_t *default_func; 00273 Keymap kmap; 00274 { 00275 rl_command_func_t *func; 00276 00277 if (keyseq) 00278 { 00279 func = rl_function_of_keyseq (keyseq, kmap, (int *)NULL); 00280 #if defined (VI_MODE) 00281 if (!func || func == rl_do_lowercase_version || func == rl_vi_movement_mode) 00282 #else 00283 if (!func || func == rl_do_lowercase_version) 00284 #endif 00285 return (rl_bind_keyseq_in_map (keyseq, default_func, kmap)); 00286 else 00287 return 1; 00288 } 00289 return 0; 00290 } 00291 00292 int 00293 rl_bind_keyseq_if_unbound (keyseq, default_func) 00294 const char *keyseq; 00295 rl_command_func_t *default_func; 00296 { 00297 return (rl_bind_keyseq_if_unbound_in_map (keyseq, default_func, _rl_keymap)); 00298 } 00299 00300 /* Bind the key sequence represented by the string KEYSEQ to 00301 the string of characters MACRO. This makes new keymaps as 00302 necessary. The initial place to do bindings is in MAP. */ 00303 int 00304 rl_macro_bind (keyseq, macro, map) 00305 const char *keyseq, *macro; 00306 Keymap map; 00307 { 00308 char *macro_keys; 00309 int macro_keys_len; 00310 00311 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1); 00312 00313 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len)) 00314 { 00315 free (macro_keys); 00316 return -1; 00317 } 00318 rl_generic_bind (ISMACR, keyseq, macro_keys, map); 00319 return 0; 00320 } 00321 00322 /* Bind the key sequence represented by the string KEYSEQ to 00323 the arbitrary pointer DATA. TYPE says what kind of data is 00324 pointed to by DATA, right now this can be a function (ISFUNC), 00325 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps 00326 as necessary. The initial place to do bindings is in MAP. */ 00327 int 00328 rl_generic_bind (type, keyseq, data, map) 00329 int type; 00330 const char *keyseq; 00331 char *data; 00332 Keymap map; 00333 { 00334 char *keys; 00335 int keys_len; 00336 register int i; 00337 KEYMAP_ENTRY k; 00338 00339 k.function = 0; 00340 00341 /* If no keys to bind to, exit right away. */ 00342 if (!keyseq || !*keyseq) 00343 { 00344 if (type == ISMACR) 00345 free (data); 00346 return -1; 00347 } 00348 00349 keys = (char *)xmalloc (1 + (2 * strlen (keyseq))); 00350 00351 /* Translate the ASCII representation of KEYSEQ into an array of 00352 characters. Stuff the characters into KEYS, and the length of 00353 KEYS into KEYS_LEN. */ 00354 if (rl_translate_keyseq (keyseq, keys, &keys_len)) 00355 { 00356 free (keys); 00357 return -1; 00358 } 00359 00360 /* Bind keys, making new keymaps as necessary. */ 00361 for (i = 0; i < keys_len; i++) 00362 { 00363 unsigned char uc = keys[i]; 00364 int ic; 00365 00366 ic = uc; 00367 if (ic < 0 || ic >= KEYMAP_SIZE) 00368 return -1; 00369 00370 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic)) 00371 { 00372 ic = UNMETA (ic); 00373 if (map[ESC].type == ISKMAP) 00374 map = FUNCTION_TO_KEYMAP (map, ESC); 00375 } 00376 00377 if ((i + 1) < keys_len) 00378 { 00379 if (map[ic].type != ISKMAP) 00380 { 00381 /* We allow subsequences of keys. If a keymap is being 00382 created that will `shadow' an existing function or macro 00383 key binding, we save that keybinding into the ANYOTHERKEY 00384 index in the new map. The dispatch code will look there 00385 to find the function to execute if the subsequence is not 00386 matched. ANYOTHERKEY was chosen to be greater than 00387 UCHAR_MAX. */ 00388 k = map[ic]; 00389 00390 map[ic].type = ISKMAP; 00391 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap()); 00392 } 00393 map = FUNCTION_TO_KEYMAP (map, ic); 00394 /* The dispatch code will return this function if no matching 00395 key sequence is found in the keymap. This (with a little 00396 help from the dispatch code in readline.c) allows `a' to be 00397 mapped to something, `abc' to be mapped to something else, 00398 and the function bound to `a' to be executed when the user 00399 types `abx', leaving `bx' in the input queue. */ 00400 if (k.function && ((k.type == ISFUNC && k.function != rl_do_lowercase_version) || k.type == ISMACR)) 00401 { 00402 map[ANYOTHERKEY] = k; 00403 k.function = 0; 00404 } 00405 } 00406 else 00407 { 00408 if (map[ic].type == ISMACR) 00409 free ((char *)map[ic].function); 00410 else if (map[ic].type == ISKMAP) 00411 { 00412 map = FUNCTION_TO_KEYMAP (map, ic); 00413 ic = ANYOTHERKEY; 00414 } 00415 00416 map[ic].function = KEYMAP_TO_FUNCTION (data); 00417 map[ic].type = type; 00418 } 00419 00420 rl_binding_keymap = map; 00421 } 00422 free (keys); 00423 return 0; 00424 } 00425 00426 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY, 00427 an array of characters. LEN gets the final length of ARRAY. Return 00428 non-zero if there was an error parsing SEQ. */ 00429 int 00430 rl_translate_keyseq (seq, array, len) 00431 const char *seq; 00432 char *array; 00433 int *len; 00434 { 00435 register int i, c, l, temp; 00436 00437 for (i = l = 0; c = seq[i]; i++) 00438 { 00439 if (c == '\\') 00440 { 00441 c = seq[++i]; 00442 00443 if (c == 0) 00444 break; 00445 00446 /* Handle \C- and \M- prefixes. */ 00447 if ((c == 'C' || c == 'M') && seq[i + 1] == '-') 00448 { 00449 /* Handle special case of backwards define. */ 00450 if (strncmp (&seq[i], "C-\\M-", 5) == 0) 00451 { 00452 array[l++] = ESC; /* ESC is meta-prefix */ 00453 i += 5; 00454 array[l++] = CTRL (_rl_to_upper (seq[i])); 00455 if (seq[i] == '\0') 00456 i--; 00457 } 00458 else if (c == 'M') 00459 { 00460 i++; 00461 array[l++] = ESC; /* ESC is meta-prefix */ 00462 } 00463 else if (c == 'C') 00464 { 00465 i += 2; 00466 /* Special hack for C-?... */ 00467 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i])); 00468 } 00469 continue; 00470 } 00471 00472 /* Translate other backslash-escaped characters. These are the 00473 same escape sequences that bash's `echo' and `printf' builtins 00474 handle, with the addition of \d -> RUBOUT. A backslash 00475 preceding a character that is not special is stripped. */ 00476 switch (c) 00477 { 00478 case 'a': 00479 array[l++] = '\007'; 00480 break; 00481 case 'b': 00482 array[l++] = '\b'; 00483 break; 00484 case 'd': 00485 array[l++] = RUBOUT; /* readline-specific */ 00486 break; 00487 case 'e': 00488 array[l++] = ESC; 00489 break; 00490 case 'f': 00491 array[l++] = '\f'; 00492 break; 00493 case 'n': 00494 array[l++] = NEWLINE; 00495 break; 00496 case 'r': 00497 array[l++] = RETURN; 00498 break; 00499 case 't': 00500 array[l++] = TAB; 00501 break; 00502 case 'v': 00503 array[l++] = 0x0B; 00504 break; 00505 case '\\': 00506 array[l++] = '\\'; 00507 break; 00508 case '0': case '1': case '2': case '3': 00509 case '4': case '5': case '6': case '7': 00510 i++; 00511 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++) 00512 c = (c * 8) + OCTVALUE (seq[i]); 00513 i--; /* auto-increment in for loop */ 00514 array[l++] = c & largest_char; 00515 break; 00516 case 'x': 00517 i++; 00518 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)seq[i]) && temp--; i++) 00519 c = (c * 16) + HEXVALUE (seq[i]); 00520 if (temp == 2) 00521 c = 'x'; 00522 i--; /* auto-increment in for loop */ 00523 array[l++] = c & largest_char; 00524 break; 00525 default: /* backslashes before non-special chars just add the char */ 00526 array[l++] = c; 00527 break; /* the backslash is stripped */ 00528 } 00529 continue; 00530 } 00531 00532 array[l++] = c; 00533 } 00534 00535 *len = l; 00536 array[l] = '\0'; 00537 return (0); 00538 } 00539 00540 char * 00541 rl_untranslate_keyseq (seq) 00542 int seq; 00543 { 00544 static char kseq[16]; 00545 int i, c; 00546 00547 i = 0; 00548 c = seq; 00549 if (META_CHAR (c)) 00550 { 00551 kseq[i++] = '\\'; 00552 kseq[i++] = 'M'; 00553 kseq[i++] = '-'; 00554 c = UNMETA (c); 00555 } 00556 else if (CTRL_CHAR (c)) 00557 { 00558 kseq[i++] = '\\'; 00559 kseq[i++] = 'C'; 00560 kseq[i++] = '-'; 00561 c = _rl_to_lower (UNCTRL (c)); 00562 } 00563 else if (c == RUBOUT) 00564 { 00565 kseq[i++] = '\\'; 00566 kseq[i++] = 'C'; 00567 kseq[i++] = '-'; 00568 c = '?'; 00569 } 00570 00571 if (c == ESC) 00572 { 00573 kseq[i++] = '\\'; 00574 c = 'e'; 00575 } 00576 else if (c == '\\' || c == '"') 00577 { 00578 kseq[i++] = '\\'; 00579 } 00580 00581 kseq[i++] = (unsigned char) c; 00582 kseq[i] = '\0'; 00583 return kseq; 00584 } 00585 00586 static char * 00587 _rl_untranslate_macro_value (seq) 00588 char *seq; 00589 { 00590 char *ret, *r, *s; 00591 int c; 00592 00593 r = ret = (char *)xmalloc (7 * strlen (seq) + 1); 00594 for (s = seq; *s; s++) 00595 { 00596 c = *s; 00597 if (META_CHAR (c)) 00598 { 00599 *r++ = '\\'; 00600 *r++ = 'M'; 00601 *r++ = '-'; 00602 c = UNMETA (c); 00603 } 00604 else if (CTRL_CHAR (c) && c != ESC) 00605 { 00606 *r++ = '\\'; 00607 *r++ = 'C'; 00608 *r++ = '-'; 00609 c = _rl_to_lower (UNCTRL (c)); 00610 } 00611 else if (c == RUBOUT) 00612 { 00613 *r++ = '\\'; 00614 *r++ = 'C'; 00615 *r++ = '-'; 00616 c = '?'; 00617 } 00618 00619 if (c == ESC) 00620 { 00621 *r++ = '\\'; 00622 c = 'e'; 00623 } 00624 else if (c == '\\' || c == '"') 00625 *r++ = '\\'; 00626 00627 *r++ = (unsigned char)c; 00628 } 00629 *r = '\0'; 00630 return ret; 00631 } 00632 00633 /* Return a pointer to the function that STRING represents. 00634 If STRING doesn't have a matching function, then a NULL pointer 00635 is returned. */ 00636 rl_command_func_t * 00637 rl_named_function (string) 00638 const char *string; 00639 { 00640 register int i; 00641 00642 rl_initialize_funmap (); 00643 00644 for (i = 0; funmap[i]; i++) 00645 if (_rl_stricmp (funmap[i]->name, string) == 0) 00646 return (funmap[i]->function); 00647 return ((rl_command_func_t *)NULL); 00648 } 00649 00650 /* Return the function (or macro) definition which would be invoked via 00651 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is 00652 used. TYPE, if non-NULL, is a pointer to an int which will receive the 00653 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap), 00654 or ISMACR (macro). */ 00655 rl_command_func_t * 00656 rl_function_of_keyseq (keyseq, map, type) 00657 const char *keyseq; 00658 Keymap map; 00659 int *type; 00660 { 00661 register int i; 00662 00663 if (!map) 00664 map = _rl_keymap; 00665 00666 for (i = 0; keyseq && keyseq[i]; i++) 00667 { 00668 unsigned char ic = keyseq[i]; 00669 00670 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii) 00671 { 00672 if (map[ESC].type != ISKMAP) 00673 { 00674 if (type) 00675 *type = map[ESC].type; 00676 00677 return (map[ESC].function); 00678 } 00679 else 00680 { 00681 map = FUNCTION_TO_KEYMAP (map, ESC); 00682 ic = UNMETA (ic); 00683 } 00684 } 00685 00686 if (map[ic].type == ISKMAP) 00687 { 00688 /* If this is the last key in the key sequence, return the 00689 map. */ 00690 if (!keyseq[i + 1]) 00691 { 00692 if (type) 00693 *type = ISKMAP; 00694 00695 return (map[ic].function); 00696 } 00697 else 00698 map = FUNCTION_TO_KEYMAP (map, ic); 00699 } 00700 else 00701 { 00702 if (type) 00703 *type = map[ic].type; 00704 00705 return (map[ic].function); 00706 } 00707 } 00708 return ((rl_command_func_t *) NULL); 00709 } 00710 00711 /* The last key bindings file read. */ 00712 static char *last_readline_init_file = (char *)NULL; 00713 00714 /* The file we're currently reading key bindings from. */ 00715 static const char *current_readline_init_file; 00716 static int current_readline_init_include_level; 00717 static int current_readline_init_lineno; 00718 00719 /* Read FILENAME into a locally-allocated buffer and return the buffer. 00720 The size of the buffer is returned in *SIZEP. Returns NULL if any 00721 errors were encountered. */ 00722 static char * 00723 _rl_read_file (filename, sizep) 00724 char *filename; 00725 size_t *sizep; 00726 { 00727 struct stat finfo; 00728 size_t file_size; 00729 char *buffer; 00730 int i, file; 00731 00732 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0) 00733 return ((char *)NULL); 00734 00735 file_size = (size_t)finfo.st_size; 00736 00737 /* check for overflow on very large files */ 00738 if (file_size != finfo.st_size || file_size + 1 < file_size) 00739 { 00740 if (file >= 0) 00741 close (file); 00742 #if defined (EFBIG) 00743 errno = EFBIG; 00744 #endif 00745 return ((char *)NULL); 00746 } 00747 00748 /* Read the file into BUFFER. */ 00749 buffer = (char *)xmalloc (file_size + 1); 00750 i = read (file, buffer, file_size); 00751 close (file); 00752 00753 if (i < 0) 00754 { 00755 free (buffer); 00756 return ((char *)NULL); 00757 } 00758 00759 buffer[i] = '\0'; 00760 if (sizep) 00761 *sizep = i; 00762 00763 return (buffer); 00764 } 00765 00766 /* Re-read the current keybindings file. */ 00767 int 00768 rl_re_read_init_file (count, ignore) 00769 int count, ignore; 00770 { 00771 int r; 00772 r = rl_read_init_file ((const char *)NULL); 00773 rl_set_keymap_from_edit_mode (); 00774 return r; 00775 } 00776 00777 /* Do key bindings from a file. If FILENAME is NULL it defaults 00778 to the first non-null filename from this list: 00779 1. the filename used for the previous call 00780 2. the value of the shell variable `INPUTRC' 00781 3. ~/.inputrc 00782 If the file existed and could be opened and read, 0 is returned, 00783 otherwise errno is returned. */ 00784 int 00785 rl_read_init_file (filename) 00786 const char *filename; 00787 { 00788 /* Default the filename. */ 00789 if (filename == 0) 00790 { 00791 filename = last_readline_init_file; 00792 if (filename == 0) 00793 filename = sh_get_env_value ("INPUTRC"); 00794 if (filename == 0) 00795 filename = DEFAULT_INPUTRC; 00796 } 00797 00798 if (*filename == 0) 00799 filename = DEFAULT_INPUTRC; 00800 00801 #if defined (__MSDOS__) 00802 if (_rl_read_init_file (filename, 0) == 0) 00803 return 0; 00804 filename = "~/_inputrc"; 00805 #endif 00806 return (_rl_read_init_file (filename, 0)); 00807 } 00808 00809 static int 00810 _rl_read_init_file (filename, include_level) 00811 const char *filename; 00812 int include_level; 00813 { 00814 register int i; 00815 char *buffer, *openname, *line, *end; 00816 size_t file_size; 00817 00818 current_readline_init_file = filename; 00819 current_readline_init_include_level = include_level; 00820 00821 openname = tilde_expand (filename); 00822 buffer = _rl_read_file (openname, &file_size); 00823 free (openname); 00824 00825 if (buffer == 0) 00826 return (errno); 00827 00828 if (include_level == 0 && filename != last_readline_init_file) 00829 { 00830 FREE (last_readline_init_file); 00831 last_readline_init_file = savestring (filename); 00832 } 00833 00834 currently_reading_init_file = 1; 00835 00836 /* Loop over the lines in the file. Lines that start with `#' are 00837 comments; all other lines are commands for readline initialization. */ 00838 current_readline_init_lineno = 1; 00839 line = buffer; 00840 end = buffer + file_size; 00841 while (line < end) 00842 { 00843 /* Find the end of this line. */ 00844 for (i = 0; line + i != end && line[i] != '\n'; i++); 00845 00846 #if defined (__CYGWIN__) 00847 /* ``Be liberal in what you accept.'' */ 00848 if (line[i] == '\n' && line[i-1] == '\r') 00849 line[i - 1] = '\0'; 00850 #endif 00851 00852 /* Mark end of line. */ 00853 line[i] = '\0'; 00854 00855 /* Skip leading whitespace. */ 00856 while (*line && whitespace (*line)) 00857 { 00858 line++; 00859 i--; 00860 } 00861 00862 /* If the line is not a comment, then parse it. */ 00863 if (*line && *line != '#') 00864 rl_parse_and_bind (line); 00865 00866 /* Move to the next line. */ 00867 line += i + 1; 00868 current_readline_init_lineno++; 00869 } 00870 00871 free (buffer); 00872 currently_reading_init_file = 0; 00873 return (0); 00874 } 00875 00876 static void 00877 _rl_init_file_error (msg) 00878 const char *msg; 00879 { 00880 if (currently_reading_init_file) 00881 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file, 00882 current_readline_init_lineno, msg); 00883 else 00884 fprintf (stderr, "readline: %s\n", msg); 00885 } 00886 00887 /* **************************************************************** */ 00888 /* */ 00889 /* Parser Directives */ 00890 /* */ 00891 /* **************************************************************** */ 00892 00893 typedef int _rl_parser_func_t PARAMS((char *)); 00894 00895 /* Things that mean `Control'. */ 00896 const char *_rl_possible_control_prefixes[] = { 00897 "Control-", "C-", "CTRL-", (const char *)NULL 00898 }; 00899 00900 const char *_rl_possible_meta_prefixes[] = { 00901 "Meta", "M-", (const char *)NULL 00902 }; 00903 00904 /* Conditionals. */ 00905 00906 /* Calling programs set this to have their argv[0]. */ 00907 const char *rl_readline_name = "other"; 00908 00909 /* Stack of previous values of parsing_conditionalized_out. */ 00910 static unsigned char *if_stack = (unsigned char *)NULL; 00911 static int if_stack_depth; 00912 static int if_stack_size; 00913 00914 /* Push _rl_parsing_conditionalized_out, and set parser state based 00915 on ARGS. */ 00916 static int 00917 parser_if (args) 00918 char *args; 00919 { 00920 register int i; 00921 00922 /* Push parser state. */ 00923 if (if_stack_depth + 1 >= if_stack_size) 00924 { 00925 if (!if_stack) 00926 if_stack = (unsigned char *)xmalloc (if_stack_size = 20); 00927 else 00928 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20); 00929 } 00930 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out; 00931 00932 /* If parsing is turned off, then nothing can turn it back on except 00933 for finding the matching endif. In that case, return right now. */ 00934 if (_rl_parsing_conditionalized_out) 00935 return 0; 00936 00937 /* Isolate first argument. */ 00938 for (i = 0; args[i] && !whitespace (args[i]); i++); 00939 00940 if (args[i]) 00941 args[i++] = '\0'; 00942 00943 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this 00944 isn't term=foo, or mode=emacs, then check to see if the first 00945 word in ARGS is the same as the value stored in rl_readline_name. */ 00946 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0) 00947 { 00948 char *tem, *tname; 00949 00950 /* Terminals like "aaa-60" are equivalent to "aaa". */ 00951 tname = savestring (rl_terminal_name); 00952 tem = strchr (tname, '-'); 00953 if (tem) 00954 *tem = '\0'; 00955 00956 /* Test the `long' and `short' forms of the terminal name so that 00957 if someone has a `sun-cmd' and does not want to have bindings 00958 that will be executed if the terminal is a `sun', they can put 00959 `$if term=sun-cmd' into their .inputrc. */ 00960 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) && 00961 _rl_stricmp (args + 5, rl_terminal_name); 00962 free (tname); 00963 } 00964 #if defined (VI_MODE) 00965 else if (_rl_strnicmp (args, "mode=", 5) == 0) 00966 { 00967 int mode; 00968 00969 if (_rl_stricmp (args + 5, "emacs") == 0) 00970 mode = emacs_mode; 00971 else if (_rl_stricmp (args + 5, "vi") == 0) 00972 mode = vi_mode; 00973 else 00974 mode = no_mode; 00975 00976 _rl_parsing_conditionalized_out = mode != rl_editing_mode; 00977 } 00978 #endif /* VI_MODE */ 00979 /* Check to see if the first word in ARGS is the same as the 00980 value stored in rl_readline_name. */ 00981 else if (_rl_stricmp (args, rl_readline_name) == 0) 00982 _rl_parsing_conditionalized_out = 0; 00983 else 00984 _rl_parsing_conditionalized_out = 1; 00985 return 0; 00986 } 00987 00988 /* Invert the current parser state if there is anything on the stack. */ 00989 static int 00990 parser_else (args) 00991 char *args; 00992 { 00993 register int i; 00994 00995 if (if_stack_depth == 0) 00996 { 00997 _rl_init_file_error ("$else found without matching $if"); 00998 return 0; 00999 } 01000 01001 #if 0 01002 /* Check the previous (n - 1) levels of the stack to make sure that 01003 we haven't previously turned off parsing. */ 01004 for (i = 0; i < if_stack_depth - 1; i++) 01005 #else 01006 /* Check the previous (n) levels of the stack to make sure that 01007 we haven't previously turned off parsing. */ 01008 for (i = 0; i < if_stack_depth; i++) 01009 #endif 01010 if (if_stack[i] == 1) 01011 return 0; 01012 01013 /* Invert the state of parsing if at top level. */ 01014 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out; 01015 return 0; 01016 } 01017 01018 /* Terminate a conditional, popping the value of 01019 _rl_parsing_conditionalized_out from the stack. */ 01020 static int 01021 parser_endif (args) 01022 char *args; 01023 { 01024 if (if_stack_depth) 01025 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth]; 01026 else 01027 _rl_init_file_error ("$endif without matching $if"); 01028 return 0; 01029 } 01030 01031 static int 01032 parser_include (args) 01033 char *args; 01034 { 01035 const char *old_init_file; 01036 char *e; 01037 int old_line_number, old_include_level, r; 01038 01039 if (_rl_parsing_conditionalized_out) 01040 return (0); 01041 01042 old_init_file = current_readline_init_file; 01043 old_line_number = current_readline_init_lineno; 01044 old_include_level = current_readline_init_include_level; 01045 01046 e = strchr (args, '\n'); 01047 if (e) 01048 *e = '\0'; 01049 r = _rl_read_init_file ((const char *)args, old_include_level + 1); 01050 01051 current_readline_init_file = old_init_file; 01052 current_readline_init_lineno = old_line_number; 01053 current_readline_init_include_level = old_include_level; 01054 01055 return r; 01056 } 01057 01058 /* Associate textual names with actual functions. */ 01059 static struct { 01060 const char *name; 01061 _rl_parser_func_t *function; 01062 } parser_directives [] = { 01063 { "if", parser_if }, 01064 { "endif", parser_endif }, 01065 { "else", parser_else }, 01066 { "include", parser_include }, 01067 { (char *)0x0, (_rl_parser_func_t *)0x0 } 01068 }; 01069 01070 /* Handle a parser directive. STATEMENT is the line of the directive 01071 without any leading `$'. */ 01072 static int 01073 handle_parser_directive (statement) 01074 char *statement; 01075 { 01076 register int i; 01077 char *directive, *args; 01078 01079 /* Isolate the actual directive. */ 01080 01081 /* Skip whitespace. */ 01082 for (i = 0; whitespace (statement[i]); i++); 01083 01084 directive = &statement[i]; 01085 01086 for (; statement[i] && !whitespace (statement[i]); i++); 01087 01088 if (statement[i]) 01089 statement[i++] = '\0'; 01090 01091 for (; statement[i] && whitespace (statement[i]); i++); 01092 01093 args = &statement[i]; 01094 01095 /* Lookup the command, and act on it. */ 01096 for (i = 0; parser_directives[i].name; i++) 01097 if (_rl_stricmp (directive, parser_directives[i].name) == 0) 01098 { 01099 (*parser_directives[i].function) (args); 01100 return (0); 01101 } 01102 01103 /* display an error message about the unknown parser directive */ 01104 _rl_init_file_error ("unknown parser directive"); 01105 return (1); 01106 } 01107 01108 /* Read the binding command from STRING and perform it. 01109 A key binding command looks like: Keyname: function-name\0, 01110 a variable binding command looks like: set variable value. 01111 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */ 01112 int 01113 rl_parse_and_bind (string) 01114 char *string; 01115 { 01116 char *funname, *kname; 01117 register int c, i; 01118 int key, equivalency; 01119 01120 while (string && whitespace (*string)) 01121 string++; 01122 01123 if (!string || !*string || *string == '#') 01124 return 0; 01125 01126 /* If this is a parser directive, act on it. */ 01127 if (*string == '$') 01128 { 01129 handle_parser_directive (&string[1]); 01130 return 0; 01131 } 01132 01133 /* If we aren't supposed to be parsing right now, then we're done. */ 01134 if (_rl_parsing_conditionalized_out) 01135 return 0; 01136 01137 i = 0; 01138 /* If this keyname is a complex key expression surrounded by quotes, 01139 advance to after the matching close quote. This code allows the 01140 backslash to quote characters in the key expression. */ 01141 if (*string == '"') 01142 { 01143 int passc = 0; 01144 01145 for (i = 1; c = string[i]; i++) 01146 { 01147 if (passc) 01148 { 01149 passc = 0; 01150 continue; 01151 } 01152 01153 if (c == '\\') 01154 { 01155 passc++; 01156 continue; 01157 } 01158 01159 if (c == '"') 01160 break; 01161 } 01162 /* If we didn't find a closing quote, abort the line. */ 01163 if (string[i] == '\0') 01164 { 01165 _rl_init_file_error ("no closing `\"' in key binding"); 01166 return 1; 01167 } 01168 } 01169 01170 /* Advance to the colon (:) or whitespace which separates the two objects. */ 01171 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ ); 01172 01173 equivalency = (c == ':' && string[i + 1] == '='); 01174 01175 /* Mark the end of the command (or keyname). */ 01176 if (string[i]) 01177 string[i++] = '\0'; 01178 01179 /* If doing assignment, skip the '=' sign as well. */ 01180 if (equivalency) 01181 string[i++] = '\0'; 01182 01183 /* If this is a command to set a variable, then do that. */ 01184 if (_rl_stricmp (string, "set") == 0) 01185 { 01186 char *var = string + i; 01187 char *value; 01188 01189 /* Make VAR point to start of variable name. */ 01190 while (*var && whitespace (*var)) var++; 01191 01192 /* Make VALUE point to start of value string. */ 01193 value = var; 01194 while (*value && !whitespace (*value)) value++; 01195 if (*value) 01196 *value++ = '\0'; 01197 while (*value && whitespace (*value)) value++; 01198 01199 rl_variable_bind (var, value); 01200 return 0; 01201 } 01202 01203 /* Skip any whitespace between keyname and funname. */ 01204 for (; string[i] && whitespace (string[i]); i++); 01205 funname = &string[i]; 01206 01207 /* Now isolate funname. 01208 For straight function names just look for whitespace, since 01209 that will signify the end of the string. But this could be a 01210 macro definition. In that case, the string is quoted, so skip 01211 to the matching delimiter. We allow the backslash to quote the 01212 delimiter characters in the macro body. */ 01213 /* This code exists to allow whitespace in macro expansions, which 01214 would otherwise be gobbled up by the next `for' loop.*/ 01215 /* XXX - it may be desirable to allow backslash quoting only if " is 01216 the quoted string delimiter, like the shell. */ 01217 if (*funname == '\'' || *funname == '"') 01218 { 01219 int delimiter = string[i++], passc; 01220 01221 for (passc = 0; c = string[i]; i++) 01222 { 01223 if (passc) 01224 { 01225 passc = 0; 01226 continue; 01227 } 01228 01229 if (c == '\\') 01230 { 01231 passc = 1; 01232 continue; 01233 } 01234 01235 if (c == delimiter) 01236 break; 01237 } 01238 if (c) 01239 i++; 01240 } 01241 01242 /* Advance to the end of the string. */ 01243 for (; string[i] && !whitespace (string[i]); i++); 01244 01245 /* No extra whitespace at the end of the string. */ 01246 string[i] = '\0'; 01247 01248 /* Handle equivalency bindings here. Make the left-hand side be exactly 01249 whatever the right-hand evaluates to, including keymaps. */ 01250 if (equivalency) 01251 { 01252 return 0; 01253 } 01254 01255 /* If this is a new-style key-binding, then do the binding with 01256 rl_bind_keyseq (). Otherwise, let the older code deal with it. */ 01257 if (*string == '"') 01258 { 01259 char *seq; 01260 register int j, k, passc; 01261 01262 seq = (char *)xmalloc (1 + strlen (string)); 01263 for (j = 1, k = passc = 0; string[j]; j++) 01264 { 01265 /* Allow backslash to quote characters, but leave them in place. 01266 This allows a string to end with a backslash quoting another 01267 backslash, or with a backslash quoting a double quote. The 01268 backslashes are left in place for rl_translate_keyseq (). */ 01269 if (passc || (string[j] == '\\')) 01270 { 01271 seq[k++] = string[j]; 01272 passc = !passc; 01273 continue; 01274 } 01275 01276 if (string[j] == '"') 01277 break; 01278 01279 seq[k++] = string[j]; 01280 } 01281 seq[k] = '\0'; 01282 01283 /* Binding macro? */ 01284 if (*funname == '\'' || *funname == '"') 01285 { 01286 j = strlen (funname); 01287 01288 /* Remove the delimiting quotes from each end of FUNNAME. */ 01289 if (j && funname[j - 1] == *funname) 01290 funname[j - 1] = '\0'; 01291 01292 rl_macro_bind (seq, &funname[1], _rl_keymap); 01293 } 01294 else 01295 rl_bind_keyseq (seq, rl_named_function (funname)); 01296 01297 free (seq); 01298 return 0; 01299 } 01300 01301 /* Get the actual character we want to deal with. */ 01302 kname = strrchr (string, '-'); 01303 if (!kname) 01304 kname = string; 01305 else 01306 kname++; 01307 01308 key = glean_key_from_name (kname); 01309 01310 /* Add in control and meta bits. */ 01311 if (substring_member_of_array (string, _rl_possible_control_prefixes)) 01312 key = CTRL (_rl_to_upper (key)); 01313 01314 if (substring_member_of_array (string, _rl_possible_meta_prefixes)) 01315 key = META (key); 01316 01317 /* Temporary. Handle old-style keyname with macro-binding. */ 01318 if (*funname == '\'' || *funname == '"') 01319 { 01320 char useq[2]; 01321 int fl = strlen (funname); 01322 01323 useq[0] = key; useq[1] = '\0'; 01324 if (fl && funname[fl - 1] == *funname) 01325 funname[fl - 1] = '\0'; 01326 01327 rl_macro_bind (useq, &funname[1], _rl_keymap); 01328 } 01329 #if defined (PREFIX_META_HACK) 01330 /* Ugly, but working hack to keep prefix-meta around. */ 01331 else if (_rl_stricmp (funname, "prefix-meta") == 0) 01332 { 01333 char seq[2]; 01334 01335 seq[0] = key; 01336 seq[1] = '\0'; 01337 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap); 01338 } 01339 #endif /* PREFIX_META_HACK */ 01340 else 01341 rl_bind_key (key, rl_named_function (funname)); 01342 return 0; 01343 } 01344 01345 /* Simple structure for boolean readline variables (i.e., those that can 01346 have one of two values; either "On" or 1 for truth, or "Off" or 0 for 01347 false. */ 01348 01349 #define V_SPECIAL 0x1 01350 01351 static struct { 01352 const char *name; 01353 int *value; 01354 int flags; 01355 } boolean_varlist [] = { 01356 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL }, 01357 { "byte-oriented", &rl_byte_oriented, 0 }, 01358 { "completion-ignore-case", &_rl_completion_case_fold, 0 }, 01359 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 }, 01360 { "disable-completion", &rl_inhibit_completion, 0 }, 01361 { "enable-keypad", &_rl_enable_keypad, 0 }, 01362 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 }, 01363 { "history-preserve-point", &_rl_history_preserve_point, 0 }, 01364 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 }, 01365 { "input-meta", &_rl_meta_flag, 0 }, 01366 { "mark-directories", &_rl_complete_mark_directories, 0 }, 01367 { "mark-modified-lines", &_rl_mark_modified_lines, 0 }, 01368 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 }, 01369 { "match-hidden-files", &_rl_match_hidden_files, 0 }, 01370 { "meta-flag", &_rl_meta_flag, 0 }, 01371 { "output-meta", &_rl_output_meta_chars, 0 }, 01372 { "page-completions", &_rl_page_completions, 0 }, 01373 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL }, 01374 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 }, 01375 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 }, 01376 { "show-all-if-unmodified", &_rl_complete_show_unmodified, 0 }, 01377 #if defined (VISIBLE_STATS) 01378 { "visible-stats", &rl_visible_stats, 0 }, 01379 #endif /* VISIBLE_STATS */ 01380 { (char *)NULL, (int *)NULL } 01381 }; 01382 01383 static int 01384 find_boolean_var (name) 01385 const char *name; 01386 { 01387 register int i; 01388 01389 for (i = 0; boolean_varlist[i].name; i++) 01390 if (_rl_stricmp (name, boolean_varlist[i].name) == 0) 01391 return i; 01392 return -1; 01393 } 01394 01395 /* Hooks for handling special boolean variables, where a 01396 function needs to be called or another variable needs 01397 to be changed when they're changed. */ 01398 static void 01399 hack_special_boolean_var (i) 01400 int i; 01401 { 01402 const char *name; 01403 01404 name = boolean_varlist[i].name; 01405 01406 if (_rl_stricmp (name, "blink-matching-paren") == 0) 01407 _rl_enable_paren_matching (rl_blink_matching_paren); 01408 else if (_rl_stricmp (name, "prefer-visible-bell") == 0) 01409 { 01410 if (_rl_prefer_visible_bell) 01411 _rl_bell_preference = VISIBLE_BELL; 01412 else 01413 _rl_bell_preference = AUDIBLE_BELL; 01414 } 01415 } 01416 01417 typedef int _rl_sv_func_t PARAMS((const char *)); 01418 01419 /* These *must* correspond to the array indices for the appropriate 01420 string variable. (Though they're not used right now.) */ 01421 #define V_BELLSTYLE 0 01422 #define V_COMBEGIN 1 01423 #define V_EDITMODE 2 01424 #define V_ISRCHTERM 3 01425 #define V_KEYMAP 4 01426 01427 #define V_STRING 1 01428 #define V_INT 2 01429 01430 /* Forward declarations */ 01431 static int sv_bell_style PARAMS((const char *)); 01432 static int sv_combegin PARAMS((const char *)); 01433 static int sv_compquery PARAMS((const char *)); 01434 static int sv_editmode PARAMS((const char *)); 01435 static int sv_isrchterm PARAMS((const char *)); 01436 static int sv_keymap PARAMS((const char *)); 01437 01438 static struct { 01439 const char *name; 01440 int flags; 01441 _rl_sv_func_t *set_func; 01442 } string_varlist[] = { 01443 { "bell-style", V_STRING, sv_bell_style }, 01444 { "comment-begin", V_STRING, sv_combegin }, 01445 { "completion-query-items", V_INT, sv_compquery }, 01446 { "editing-mode", V_STRING, sv_editmode }, 01447 { "isearch-terminators", V_STRING, sv_isrchterm }, 01448 { "keymap", V_STRING, sv_keymap }, 01449 { (char *)NULL, 0 } 01450 }; 01451 01452 static int 01453 find_string_var (name) 01454 const char *name; 01455 { 01456 register int i; 01457 01458 for (i = 0; string_varlist[i].name; i++) 01459 if (_rl_stricmp (name, string_varlist[i].name) == 0) 01460 return i; 01461 return -1; 01462 } 01463 01464 /* A boolean value that can appear in a `set variable' command is true if 01465 the value is null or empty, `on' (case-insenstive), or "1". Any other 01466 values result in 0 (false). */ 01467 static int 01468 bool_to_int (value) 01469 char *value; 01470 { 01471 return (value == 0 || *value == '\0' || 01472 (_rl_stricmp (value, "on") == 0) || 01473 (value[0] == '1' && value[1] == '\0')); 01474 } 01475 01476 int 01477 rl_variable_bind (name, value) 01478 const char *name, *value; 01479 { 01480 register int i; 01481 int v; 01482 01483 /* Check for simple variables first. */ 01484 i = find_boolean_var (name); 01485 if (i >= 0) 01486 { 01487 *boolean_varlist[i].value = bool_to_int (value); 01488 if (boolean_varlist[i].flags & V_SPECIAL) 01489 hack_special_boolean_var (i); 01490 return 0; 01491 } 01492 01493 i = find_string_var (name); 01494 01495 /* For the time being, unknown variable names or string names without a 01496 handler function are simply ignored. */ 01497 if (i < 0 || string_varlist[i].set_func == 0) 01498 return 0; 01499 01500 v = (*string_varlist[i].set_func) (value); 01501 return v; 01502 } 01503 01504 static int 01505 sv_editmode (value) 01506 const char *value; 01507 { 01508 if (_rl_strnicmp (value, "vi", 2) == 0) 01509 { 01510 #if defined (VI_MODE) 01511 _rl_keymap = vi_insertion_keymap; 01512 rl_editing_mode = vi_mode; 01513 #endif /* VI_MODE */ 01514 return 0; 01515 } 01516 else if (_rl_strnicmp (value, "emacs", 5) == 0) 01517 { 01518 _rl_keymap = emacs_standard_keymap; 01519 rl_editing_mode = emacs_mode; 01520 return 0; 01521 } 01522 return 1; 01523 } 01524 01525 static int 01526 sv_combegin (value) 01527 const char *value; 01528 { 01529 if (value && *value) 01530 { 01531 FREE (_rl_comment_begin); 01532 _rl_comment_begin = savestring (value); 01533 return 0; 01534 } 01535 return 1; 01536 } 01537 01538 static int 01539 sv_compquery (value) 01540 const char *value; 01541 { 01542 int nval = 100; 01543 01544 if (value && *value) 01545 { 01546 nval = atoi (value); 01547 if (nval < 0) 01548 nval = 0; 01549 } 01550 rl_completion_query_items = nval; 01551 return 0; 01552 } 01553 01554 static int 01555 sv_keymap (value) 01556 const char *value; 01557 { 01558 Keymap kmap; 01559 01560 kmap = rl_get_keymap_by_name (value); 01561 if (kmap) 01562 { 01563 rl_set_keymap (kmap); 01564 return 0; 01565 } 01566 return 1; 01567 } 01568 01569 static int 01570 sv_bell_style (value) 01571 const char *value; 01572 { 01573 if (value == 0 || *value == '\0') 01574 _rl_bell_preference = AUDIBLE_BELL; 01575 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0) 01576 _rl_bell_preference = NO_BELL; 01577 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0) 01578 _rl_bell_preference = AUDIBLE_BELL; 01579 else if (_rl_stricmp (value, "visible") == 0) 01580 _rl_bell_preference = VISIBLE_BELL; 01581 else 01582 return 1; 01583 return 0; 01584 } 01585 01586 static int 01587 sv_isrchterm (value) 01588 const char *value; 01589 { 01590 int beg, end, delim; 01591 char *v; 01592 01593 if (value == 0) 01594 return 1; 01595 01596 /* Isolate the value and translate it into a character string. */ 01597 v = savestring (value); 01598 FREE (_rl_isearch_terminators); 01599 if (v[0] == '"' || v[0] == '\'') 01600 { 01601 delim = v[0]; 01602 for (beg = end = 1; v[end] && v[end] != delim; end++) 01603 ; 01604 } 01605 else 01606 { 01607 for (beg = end = 0; whitespace (v[end]) == 0; end++) 01608 ; 01609 } 01610 01611 v[end] = '\0'; 01612 01613 /* The value starts at v + beg. Translate it into a character string. */ 01614 _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1); 01615 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end); 01616 _rl_isearch_terminators[end] = '\0'; 01617 01618 free (v); 01619 return 0; 01620 } 01621 01622 /* Return the character which matches NAME. 01623 For example, `Space' returns ' '. */ 01624 01625 typedef struct { 01626 const char *name; 01627 int value; 01628 } assoc_list; 01629 01630 static assoc_list name_key_alist[] = { 01631 { "DEL", 0x7f }, 01632 { "ESC", '\033' }, 01633 { "Escape", '\033' }, 01634 { "LFD", '\n' }, 01635 { "Newline", '\n' }, 01636 { "RET", '\r' }, 01637 { "Return", '\r' }, 01638 { "Rubout", 0x7f }, 01639 { "SPC", ' ' }, 01640 { "Space", ' ' }, 01641 { "Tab", 0x09 }, 01642 { (char *)0x0, 0 } 01643 }; 01644 01645 static int 01646 glean_key_from_name (name) 01647 char *name; 01648 { 01649 register int i; 01650 01651 for (i = 0; name_key_alist[i].name; i++) 01652 if (_rl_stricmp (name, name_key_alist[i].name) == 0) 01653 return (name_key_alist[i].value); 01654 01655 return (*(unsigned char *)name); /* XXX was return (*name) */ 01656 } 01657 01658 /* Auxiliary functions to manage keymaps. */ 01659 static struct { 01660 const char *name; 01661 Keymap map; 01662 } keymap_names[] = { 01663 { "emacs", emacs_standard_keymap }, 01664 { "emacs-standard", emacs_standard_keymap }, 01665 { "emacs-meta", emacs_meta_keymap }, 01666 { "emacs-ctlx", emacs_ctlx_keymap }, 01667 #if defined (VI_MODE) 01668 { "vi", vi_movement_keymap }, 01669 { "vi-move", vi_movement_keymap }, 01670 { "vi-command", vi_movement_keymap }, 01671 { "vi-insert", vi_insertion_keymap }, 01672 #endif /* VI_MODE */ 01673 { (char *)0x0, (Keymap)0x0 } 01674 }; 01675 01676 Keymap 01677 rl_get_keymap_by_name (name) 01678 const char *name; 01679 { 01680 register int i; 01681 01682 for (i = 0; keymap_names[i].name; i++) 01683 if (_rl_stricmp (name, keymap_names[i].name) == 0) 01684 return (keymap_names[i].map); 01685 return ((Keymap) NULL); 01686 } 01687 01688 char * 01689 rl_get_keymap_name (map) 01690 Keymap map; 01691 { 01692 register int i; 01693 for (i = 0; keymap_names[i].name; i++) 01694 if (map == keymap_names[i].map) 01695 return ((char *)keymap_names[i].name); 01696 return ((char *)NULL); 01697 } 01698 01699 void 01700 rl_set_keymap (map) 01701 Keymap map; 01702 { 01703 if (map) 01704 _rl_keymap = map; 01705 } 01706 01707 Keymap 01708 rl_get_keymap () 01709 { 01710 return (_rl_keymap); 01711 } 01712 01713 void 01714 rl_set_keymap_from_edit_mode () 01715 { 01716 if (rl_editing_mode == emacs_mode) 01717 _rl_keymap = emacs_standard_keymap; 01718 #if defined (VI_MODE) 01719 else if (rl_editing_mode == vi_mode) 01720 _rl_keymap = vi_insertion_keymap; 01721 #endif /* VI_MODE */ 01722 } 01723 01724 char * 01725 rl_get_keymap_name_from_edit_mode () 01726 { 01727 if (rl_editing_mode == emacs_mode) 01728 return "emacs"; 01729 #if defined (VI_MODE) 01730 else if (rl_editing_mode == vi_mode) 01731 return "vi"; 01732 #endif /* VI_MODE */ 01733 else 01734 return "none"; 01735 } 01736 01737 /* **************************************************************** */ 01738 /* */ 01739 /* Key Binding and Function Information */ 01740 /* */ 01741 /* **************************************************************** */ 01742 01743 /* Each of the following functions produces information about the 01744 state of keybindings and functions known to Readline. The info 01745 is always printed to rl_outstream, and in such a way that it can 01746 be read back in (i.e., passed to rl_parse_and_bind ()). */ 01747 01748 /* Print the names of functions known to Readline. */ 01749 void 01750 rl_list_funmap_names () 01751 { 01752 register int i; 01753 const char **funmap_names; 01754 01755 funmap_names = rl_funmap_names (); 01756 01757 if (!funmap_names) 01758 return; 01759 01760 for (i = 0; funmap_names[i]; i++) 01761 fprintf (rl_outstream, "%s\n", funmap_names[i]); 01762 01763 free (funmap_names); 01764 } 01765 01766 static char * 01767 _rl_get_keyname (key) 01768 int key; 01769 { 01770 char *keyname; 01771 int i, c; 01772 01773 keyname = (char *)xmalloc (8); 01774 01775 c = key; 01776 /* Since this is going to be used to write out keysequence-function 01777 pairs for possible inclusion in an inputrc file, we don't want to 01778 do any special meta processing on KEY. */ 01779 01780 #if 1 01781 /* XXX - Experimental */ 01782 /* We might want to do this, but the old version of the code did not. */ 01783 01784 /* If this is an escape character, we don't want to do any more processing. 01785 Just add the special ESC key sequence and return. */ 01786 if (c == ESC) 01787 { 01788 keyname[0] = '\\'; 01789 keyname[1] = 'e'; 01790 keyname[2] = '\0'; 01791 return keyname; 01792 } 01793 #endif 01794 01795 /* RUBOUT is translated directly into \C-? */ 01796 if (key == RUBOUT) 01797 { 01798 keyname[0] = '\\'; 01799 keyname[1] = 'C'; 01800 keyname[2] = '-'; 01801 keyname[3] = '?'; 01802 keyname[4] = '\0'; 01803 return keyname; 01804 } 01805 01806 i = 0; 01807 /* Now add special prefixes needed for control characters. This can 01808 potentially change C. */ 01809 if (CTRL_CHAR (c)) 01810 { 01811 keyname[i++] = '\\'; 01812 keyname[i++] = 'C'; 01813 keyname[i++] = '-'; 01814 c = _rl_to_lower (UNCTRL (c)); 01815 } 01816 01817 /* XXX experimental code. Turn the characters that are not ASCII or 01818 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237). 01819 This changes C. */ 01820 if (c >= 128 && c <= 159) 01821 { 01822 keyname[i++] = '\\'; 01823 keyname[i++] = '2'; 01824 c -= 128; 01825 keyname[i++] = (c / 8) + '0'; 01826 c = (c % 8) + '0'; 01827 } 01828 01829 /* Now, if the character needs to be quoted with a backslash, do that. */ 01830 if (c == '\\' || c == '"') 01831 keyname[i++] = '\\'; 01832 01833 /* Now add the key, terminate the string, and return it. */ 01834 keyname[i++] = (char) c; 01835 keyname[i] = '\0'; 01836 01837 return keyname; 01838 } 01839 01840 /* Return a NULL terminated array of strings which represent the key 01841 sequences that are used to invoke FUNCTION in MAP. */ 01842 char ** 01843 rl_invoking_keyseqs_in_map (function, map) 01844 rl_command_func_t *function; 01845 Keymap map; 01846 { 01847 register int key; 01848 char **result; 01849 int result_index, result_size; 01850 01851 result = (char **)NULL; 01852 result_index = result_size = 0; 01853 01854 for (key = 0; key < KEYMAP_SIZE; key++) 01855 { 01856 switch (map[key].type) 01857 { 01858 case ISMACR: 01859 /* Macros match, if, and only if, the pointers are identical. 01860 Thus, they are treated exactly like functions in here. */ 01861 case ISFUNC: 01862 /* If the function in the keymap is the one we are looking for, 01863 then add the current KEY to the list of invoking keys. */ 01864 if (map[key].function == function) 01865 { 01866 char *keyname; 01867 01868 keyname = _rl_get_keyname (key); 01869 01870 if (result_index + 2 > result_size) 01871 { 01872 result_size += 10; 01873 result = (char **)xrealloc (result, result_size * sizeof (char *)); 01874 } 01875 01876 result[result_index++] = keyname; 01877 result[result_index] = (char *)NULL; 01878 } 01879 break; 01880 01881 case ISKMAP: 01882 { 01883 char **seqs; 01884 register int i; 01885 01886 /* Find the list of keyseqs in this map which have FUNCTION as 01887 their target. Add the key sequences found to RESULT. */ 01888 if (map[key].function) 01889 seqs = 01890 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key)); 01891 else 01892 break; 01893 01894 if (seqs == 0) 01895 break; 01896 01897 for (i = 0; seqs[i]; i++) 01898 { 01899 char *keyname = (char *)xmalloc (6 + strlen (seqs[i])); 01900 01901 if (key == ESC) 01902 #if 0 01903 sprintf (keyname, "\\e"); 01904 #else 01905 /* XXX - experimental */ 01906 sprintf (keyname, "\\M-"); 01907 #endif 01908 else if (CTRL_CHAR (key)) 01909 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key))); 01910 else if (key == RUBOUT) 01911 sprintf (keyname, "\\C-?"); 01912 else if (key == '\\' || key == '"') 01913 { 01914 keyname[0] = '\\'; 01915 keyname[1] = (char) key; 01916 keyname[2] = '\0'; 01917 } 01918 else 01919 { 01920 keyname[0] = (char) key; 01921 keyname[1] = '\0'; 01922 } 01923 01924 strcat (keyname, seqs[i]); 01925 free (seqs[i]); 01926 01927 if (result_index + 2 > result_size) 01928 { 01929 result_size += 10; 01930 result = (char **)xrealloc (result, result_size * sizeof (char *)); 01931 } 01932 01933 result[result_index++] = keyname; 01934 result[result_index] = (char *)NULL; 01935 } 01936 01937 free (seqs); 01938 } 01939 break; 01940 } 01941 } 01942 return (result); 01943 } 01944 01945 /* Return a NULL terminated array of strings which represent the key 01946 sequences that can be used to invoke FUNCTION using the current keymap. */ 01947 char ** 01948 rl_invoking_keyseqs (function) 01949 rl_command_func_t *function; 01950 { 01951 return (rl_invoking_keyseqs_in_map (function, _rl_keymap)); 01952 } 01953 01954 /* Print all of the functions and their bindings to rl_outstream. If 01955 PRINT_READABLY is non-zero, then print the output in such a way 01956 that it can be read back in. */ 01957 void 01958 rl_function_dumper (print_readably) 01959 int print_readably; 01960 { 01961 register int i; 01962 const char **names; 01963 const char *name; 01964 01965 names = rl_funmap_names (); 01966 01967 fprintf (rl_outstream, "\n"); 01968 01969 for (i = 0; name = names[i]; i++) 01970 { 01971 rl_command_func_t *function; 01972 char **invokers; 01973 01974 function = rl_named_function (name); 01975 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap); 01976 01977 if (print_readably) 01978 { 01979 if (!invokers) 01980 fprintf (rl_outstream, "# %s (not bound)\n", name); 01981 else 01982 { 01983 register int j; 01984 01985 for (j = 0; invokers[j]; j++) 01986 { 01987 fprintf (rl_outstream, "\"%s\": %s\n", 01988 invokers[j], name); 01989 free (invokers[j]); 01990 } 01991 01992 free (invokers); 01993 } 01994 } 01995 else 01996 { 01997 if (!invokers) 01998 fprintf (rl_outstream, "%s is not bound to any keys\n", 01999 name); 02000 else 02001 { 02002 register int j; 02003 02004 fprintf (rl_outstream, "%s can be found on ", name); 02005 02006 for (j = 0; invokers[j] && j < 5; j++) 02007 { 02008 fprintf (rl_outstream, "\"%s\"%s", invokers[j], 02009 invokers[j + 1] ? ", " : ".\n"); 02010 } 02011 02012 if (j == 5 && invokers[j]) 02013 fprintf (rl_outstream, "...\n"); 02014 02015 for (j = 0; invokers[j]; j++) 02016 free (invokers[j]); 02017 02018 free (invokers); 02019 } 02020 } 02021 } 02022 } 02023 02024 /* Print all of the current functions and their bindings to 02025 rl_outstream. If an explicit argument is given, then print 02026 the output in such a way that it can be read back in. */ 02027 int 02028 rl_dump_functions (count, key) 02029 int count, key; 02030 { 02031 if (rl_dispatching) 02032 fprintf (rl_outstream, "\r\n"); 02033 rl_function_dumper (rl_explicit_arg); 02034 rl_on_new_line (); 02035 return (0); 02036 } 02037 02038 static void 02039 _rl_macro_dumper_internal (print_readably, map, prefix) 02040 int print_readably; 02041 Keymap map; 02042 char *prefix; 02043 { 02044 register int key; 02045 char *keyname, *out; 02046 int prefix_len; 02047 02048 for (key = 0; key < KEYMAP_SIZE; key++) 02049 { 02050 switch (map[key].type) 02051 { 02052 case ISMACR: 02053 keyname = _rl_get_keyname (key); 02054 out = _rl_untranslate_macro_value ((char *)map[key].function); 02055 02056 if (print_readably) 02057 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "", 02058 keyname, 02059 out ? out : ""); 02060 else 02061 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "", 02062 keyname, 02063 out ? out : ""); 02064 free (keyname); 02065 free (out); 02066 break; 02067 case ISFUNC: 02068 break; 02069 case ISKMAP: 02070 prefix_len = prefix ? strlen (prefix) : 0; 02071 if (key == ESC) 02072 { 02073 keyname = (char *)xmalloc (3 + prefix_len); 02074 if (prefix) 02075 strcpy (keyname, prefix); 02076 keyname[prefix_len] = '\\'; 02077 keyname[prefix_len + 1] = 'e'; 02078 keyname[prefix_len + 2] = '\0'; 02079 } 02080 else 02081 { 02082 keyname = _rl_get_keyname (key); 02083 if (prefix) 02084 { 02085 out = (char *)xmalloc (strlen (keyname) + prefix_len + 1); 02086 strcpy (out, prefix); 02087 strcpy (out + prefix_len, keyname); 02088 free (keyname); 02089 keyname = out; 02090 } 02091 } 02092 02093 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname); 02094 free (keyname); 02095 break; 02096 } 02097 } 02098 } 02099 02100 void 02101 rl_macro_dumper (print_readably) 02102 int print_readably; 02103 { 02104 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL); 02105 } 02106 02107 int 02108 rl_dump_macros (count, key) 02109 int count, key; 02110 { 02111 if (rl_dispatching) 02112 fprintf (rl_outstream, "\r\n"); 02113 rl_macro_dumper (rl_explicit_arg); 02114 rl_on_new_line (); 02115 return (0); 02116 } 02117 02118 void 02119 rl_variable_dumper (print_readably) 02120 int print_readably; 02121 { 02122 int i; 02123 const char *kname; 02124 02125 for (i = 0; boolean_varlist[i].name; i++) 02126 { 02127 if (print_readably) 02128 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name, 02129 *boolean_varlist[i].value ? "on" : "off"); 02130 else 02131 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name, 02132 *boolean_varlist[i].value ? "on" : "off"); 02133 } 02134 02135 /* bell-style */ 02136 switch (_rl_bell_preference) 02137 { 02138 case NO_BELL: 02139 kname = "none"; break; 02140 case VISIBLE_BELL: 02141 kname = "visible"; break; 02142 case AUDIBLE_BELL: 02143 default: 02144 kname = "audible"; break; 02145 } 02146 if (print_readably) 02147 fprintf (rl_outstream, "set bell-style %s\n", kname); 02148 else 02149 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname); 02150 02151 /* comment-begin */ 02152 if (print_readably) 02153 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT); 02154 else 02155 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT); 02156 02157 /* completion-query-items */ 02158 if (print_readably) 02159 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items); 02160 else 02161 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items); 02162 02163 /* editing-mode */ 02164 if (print_readably) 02165 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi"); 02166 else 02167 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi"); 02168 02169 /* isearch-terminators */ 02170 if (_rl_isearch_terminators) 02171 { 02172 char *disp; 02173 02174 disp = _rl_untranslate_macro_value (_rl_isearch_terminators); 02175 02176 if (print_readably) 02177 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp); 02178 else 02179 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp); 02180 02181 free (disp); 02182 } 02183 02184 /* keymap */ 02185 kname = rl_get_keymap_name (_rl_keymap); 02186 if (kname == 0) 02187 kname = rl_get_keymap_name_from_edit_mode (); 02188 if (print_readably) 02189 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none"); 02190 else 02191 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none"); 02192 } 02193 02194 /* Print all of the current variables and their values to 02195 rl_outstream. If an explicit argument is given, then print 02196 the output in such a way that it can be read back in. */ 02197 int 02198 rl_dump_variables (count, key) 02199 int count, key; 02200 { 02201 if (rl_dispatching) 02202 fprintf (rl_outstream, "\r\n"); 02203 rl_variable_dumper (rl_explicit_arg); 02204 rl_on_new_line (); 02205 return (0); 02206 } 02207 02208 /* Return non-zero if any members of ARRAY are a substring in STRING. */ 02209 static int 02210 substring_member_of_array (string, array) 02211 char *string; 02212 const char **array; 02213 { 02214 while (*array) 02215 { 02216 if (_rl_strindex (string, *array)) 02217 return (1); 02218 array++; 02219 } 02220 return (0); 02221 }
1.4.7

