The world's most popular open source database
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

