bind.c 58 KB

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