readline.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /* readline.c -- a general facility for reading lines of input
  2. with emacs style editing and completion. */
  3. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  4. This file is part of the GNU Readline Library (Readline), a library
  5. for reading lines of text with interactive input and history editing.
  6. Readline is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Readline is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define READLINE_LIBRARY
  18. #if defined (HAVE_CONFIG_H)
  19. # include <config.h>
  20. #endif
  21. #include <sys/types.h>
  22. #include "posixstat.h"
  23. #include <fcntl.h>
  24. #if defined (HAVE_SYS_FILE_H)
  25. # include <sys/file.h>
  26. #endif /* HAVE_SYS_FILE_H */
  27. #if defined (HAVE_UNISTD_H)
  28. # include <unistd.h>
  29. #endif /* HAVE_UNISTD_H */
  30. #if defined (HAVE_STDLIB_H)
  31. # include <stdlib.h>
  32. #else
  33. # include "ansi_stdlib.h"
  34. #endif /* HAVE_STDLIB_H */
  35. #if defined (HAVE_LOCALE_H)
  36. # include <locale.h>
  37. #endif
  38. #include <stdio.h>
  39. #include "posixjmp.h"
  40. #include <errno.h>
  41. #if !defined (errno)
  42. extern int errno;
  43. #endif /* !errno */
  44. /* System-specific feature definitions and include files. */
  45. #include "rldefs.h"
  46. #include "rlmbutil.h"
  47. #if defined (__EMX__)
  48. # define INCL_DOSPROCESS
  49. # include <os2.h>
  50. #endif /* __EMX__ */
  51. /* Some standard library routines. */
  52. #include "readline.h"
  53. #include "history.h"
  54. #include "rlprivate.h"
  55. #include "rlshell.h"
  56. #include "xmalloc.h"
  57. #ifndef RL_LIBRARY_VERSION
  58. # define RL_LIBRARY_VERSION "5.1"
  59. #endif
  60. #ifndef RL_READLINE_VERSION
  61. # define RL_READLINE_VERSION 0x0501
  62. #endif
  63. extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
  64. /* Forward declarations used in this file. */
  65. static char *readline_internal PARAMS((void));
  66. static void readline_initialize_everything PARAMS((void));
  67. static void bind_arrow_keys_internal PARAMS((Keymap));
  68. static void bind_arrow_keys PARAMS((void));
  69. static void readline_default_bindings PARAMS((void));
  70. static void reset_default_bindings PARAMS((void));
  71. static int _rl_subseq_result PARAMS((int, Keymap, int, int));
  72. static int _rl_subseq_getchar PARAMS((int));
  73. /* **************************************************************** */
  74. /* */
  75. /* Line editing input utility */
  76. /* */
  77. /* **************************************************************** */
  78. const char *rl_library_version = RL_LIBRARY_VERSION;
  79. int rl_readline_version = RL_READLINE_VERSION;
  80. /* True if this is `real' readline as opposed to some stub substitute. */
  81. int rl_gnu_readline_p = 1;
  82. /* A pointer to the keymap that is currently in use.
  83. By default, it is the standard emacs keymap. */
  84. Keymap _rl_keymap = emacs_standard_keymap;
  85. /* The current style of editing. */
  86. int rl_editing_mode = emacs_mode;
  87. /* The current insert mode: input (the default) or overwrite */
  88. int rl_insert_mode = RL_IM_DEFAULT;
  89. /* Non-zero if we called this function from _rl_dispatch(). It's present
  90. so functions can find out whether they were called from a key binding
  91. or directly from an application. */
  92. int rl_dispatching;
  93. /* Non-zero if the previous command was a kill command. */
  94. int _rl_last_command_was_kill = 0;
  95. /* The current value of the numeric argument specified by the user. */
  96. int rl_numeric_arg = 1;
  97. /* Non-zero if an argument was typed. */
  98. int rl_explicit_arg = 0;
  99. /* Temporary value used while generating the argument. */
  100. int rl_arg_sign = 1;
  101. /* Non-zero means we have been called at least once before. */
  102. static int rl_initialized;
  103. #if 0
  104. /* If non-zero, this program is running in an EMACS buffer. */
  105. static int running_in_emacs;
  106. #endif
  107. /* Flags word encapsulating the current readline state. */
  108. int rl_readline_state = RL_STATE_NONE;
  109. /* The current offset in the current input line. */
  110. int rl_point;
  111. /* Mark in the current input line. */
  112. int rl_mark;
  113. /* Length of the current input line. */
  114. int rl_end;
  115. /* Make this non-zero to return the current input_line. */
  116. int rl_done;
  117. /* The last function executed by readline. */
  118. rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
  119. /* Top level environment for readline_internal (). */
  120. procenv_t _rl_top_level;
  121. /* The streams we interact with. */
  122. FILE *_rl_in_stream, *_rl_out_stream;
  123. /* The names of the streams that we do input and output to. */
  124. FILE *rl_instream = (FILE *)NULL;
  125. FILE *rl_outstream = (FILE *)NULL;
  126. /* Non-zero means echo characters as they are read. Defaults to no echo;
  127. set to 1 if there is a controlling terminal, we can get its attributes,
  128. and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
  129. for the code that sets it. */
  130. int _rl_echoing_p = 0;
  131. /* Current prompt. */
  132. char *rl_prompt = (char *)NULL;
  133. int rl_visible_prompt_length = 0;
  134. /* Set to non-zero by calling application if it has already printed rl_prompt
  135. and does not want readline to do it the first time. */
  136. int rl_already_prompted = 0;
  137. /* The number of characters read in order to type this complete command. */
  138. int rl_key_sequence_length = 0;
  139. /* If non-zero, then this is the address of a function to call just
  140. before readline_internal_setup () prints the first prompt. */
  141. rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
  142. /* If non-zero, this is the address of a function to call just before
  143. readline_internal_setup () returns and readline_internal starts
  144. reading input characters. */
  145. rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
  146. /* What we use internally. You should always refer to RL_LINE_BUFFER. */
  147. static char *the_line;
  148. /* The character that can generate an EOF. Really read from
  149. the terminal driver... just defaulted here. */
  150. int _rl_eof_char = CTRL ('D');
  151. /* Non-zero makes this the next keystroke to read. */
  152. int rl_pending_input = 0;
  153. /* Pointer to a useful terminal name. */
  154. const char *rl_terminal_name = (const char *)NULL;
  155. /* Non-zero means to always use horizontal scrolling in line display. */
  156. int _rl_horizontal_scroll_mode = 0;
  157. /* Non-zero means to display an asterisk at the starts of history lines
  158. which have been modified. */
  159. int _rl_mark_modified_lines = 0;
  160. /* The style of `bell' notification preferred. This can be set to NO_BELL,
  161. AUDIBLE_BELL, or VISIBLE_BELL. */
  162. int _rl_bell_preference = AUDIBLE_BELL;
  163. /* String inserted into the line by rl_insert_comment (). */
  164. char *_rl_comment_begin;
  165. /* Keymap holding the function currently being executed. */
  166. Keymap rl_executing_keymap;
  167. /* Keymap we're currently using to dispatch. */
  168. Keymap _rl_dispatching_keymap;
  169. /* Non-zero means to erase entire line, including prompt, on empty input lines. */
  170. int rl_erase_empty_line = 0;
  171. /* Non-zero means to read only this many characters rather than up to a
  172. character bound to accept-line. */
  173. int rl_num_chars_to_read;
  174. /* Line buffer and maintenence. */
  175. char *rl_line_buffer = (char *)NULL;
  176. int rl_line_buffer_len = 0;
  177. /* Key sequence `contexts' */
  178. _rl_keyseq_cxt *_rl_kscxt = 0;
  179. /* Forward declarations used by the display, termcap, and history code. */
  180. /* **************************************************************** */
  181. /* */
  182. /* `Forward' declarations */
  183. /* */
  184. /* **************************************************************** */
  185. /* Non-zero means do not parse any lines other than comments and
  186. parser directives. */
  187. unsigned char _rl_parsing_conditionalized_out = 0;
  188. /* Non-zero means to convert characters with the meta bit set to
  189. escape-prefixed characters so we can indirect through
  190. emacs_meta_keymap or vi_escape_keymap. */
  191. int _rl_convert_meta_chars_to_ascii = 1;
  192. /* Non-zero means to output characters with the meta bit set directly
  193. rather than as a meta-prefixed escape sequence. */
  194. int _rl_output_meta_chars = 0;
  195. /* Non-zero means to look at the termios special characters and bind
  196. them to equivalent readline functions at startup. */
  197. int _rl_bind_stty_chars = 1;
  198. /* Non-zero means to go through the history list at every newline (or
  199. whenever rl_done is set and readline returns) and revert each line to
  200. its initial state. */
  201. int _rl_revert_all_at_newline = 0;
  202. /* Non-zero means to honor the termios ECHOCTL bit and echo control
  203. characters corresponding to keyboard-generated signals. */
  204. int _rl_echo_control_chars = 1;
  205. /* **************************************************************** */
  206. /* */
  207. /* Top Level Functions */
  208. /* */
  209. /* **************************************************************** */
  210. /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
  211. int _rl_meta_flag = 0; /* Forward declaration */
  212. /* Set up the prompt and expand it. Called from readline() and
  213. rl_callback_handler_install (). */
  214. int
  215. rl_set_prompt (prompt)
  216. const char *prompt;
  217. {
  218. FREE (rl_prompt);
  219. rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
  220. rl_display_prompt = rl_prompt ? rl_prompt : "";
  221. rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
  222. return 0;
  223. }
  224. /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
  225. none. A return value of NULL means that EOF was encountered. */
  226. char *
  227. readline (prompt)
  228. const char *prompt;
  229. {
  230. char *value;
  231. #if 0
  232. int in_callback;
  233. #endif
  234. /* If we are at EOF return a NULL string. */
  235. if (rl_pending_input == EOF)
  236. {
  237. rl_clear_pending_input ();
  238. return ((char *)NULL);
  239. }
  240. #if 0
  241. /* If readline() is called after installing a callback handler, temporarily
  242. turn off the callback state to avoid ensuing messiness. Patch supplied
  243. by the gdb folks. XXX -- disabled. This can be fooled and readline
  244. left in a strange state by a poorly-timed longjmp. */
  245. if (in_callback = RL_ISSTATE (RL_STATE_CALLBACK))
  246. RL_UNSETSTATE (RL_STATE_CALLBACK);
  247. #endif
  248. rl_set_prompt (prompt);
  249. rl_initialize ();
  250. if (rl_prep_term_function)
  251. (*rl_prep_term_function) (_rl_meta_flag);
  252. #if defined (HANDLE_SIGNALS)
  253. rl_set_signals ();
  254. #endif
  255. value = readline_internal ();
  256. if (rl_deprep_term_function)
  257. (*rl_deprep_term_function) ();
  258. #if defined (HANDLE_SIGNALS)
  259. rl_clear_signals ();
  260. #endif
  261. #if 0
  262. if (in_callback)
  263. RL_SETSTATE (RL_STATE_CALLBACK);
  264. #endif
  265. return (value);
  266. }
  267. #if defined (READLINE_CALLBACKS)
  268. # define STATIC_CALLBACK
  269. #else
  270. # define STATIC_CALLBACK static
  271. #endif
  272. STATIC_CALLBACK void
  273. readline_internal_setup ()
  274. {
  275. char *nprompt;
  276. _rl_in_stream = rl_instream;
  277. _rl_out_stream = rl_outstream;
  278. if (rl_startup_hook)
  279. (*rl_startup_hook) ();
  280. /* If we're not echoing, we still want to at least print a prompt, because
  281. rl_redisplay will not do it for us. If the calling application has a
  282. custom redisplay function, though, let that function handle it. */
  283. if (_rl_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
  284. {
  285. if (rl_prompt && rl_already_prompted == 0)
  286. {
  287. nprompt = _rl_strip_prompt (rl_prompt);
  288. fprintf (_rl_out_stream, "%s", nprompt);
  289. fflush (_rl_out_stream);
  290. xfree (nprompt);
  291. }
  292. }
  293. else
  294. {
  295. if (rl_prompt && rl_already_prompted)
  296. rl_on_new_line_with_prompt ();
  297. else
  298. rl_on_new_line ();
  299. (*rl_redisplay_function) ();
  300. }
  301. #if defined (VI_MODE)
  302. if (rl_editing_mode == vi_mode)
  303. rl_vi_insert_mode (1, 'i');
  304. #endif /* VI_MODE */
  305. if (rl_pre_input_hook)
  306. (*rl_pre_input_hook) ();
  307. RL_CHECK_SIGNALS ();
  308. }
  309. STATIC_CALLBACK char *
  310. readline_internal_teardown (eof)
  311. int eof;
  312. {
  313. char *temp;
  314. HIST_ENTRY *entry;
  315. RL_CHECK_SIGNALS ();
  316. /* Restore the original of this history line, iff the line that we
  317. are editing was originally in the history, AND the line has changed. */
  318. entry = current_history ();
  319. if (entry && rl_undo_list)
  320. {
  321. temp = savestring (the_line);
  322. rl_revert_line (1, 0);
  323. entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
  324. _rl_free_history_entry (entry);
  325. strcpy (the_line, temp);
  326. xfree (temp);
  327. }
  328. if (_rl_revert_all_at_newline)
  329. _rl_revert_all_lines ();
  330. /* At any rate, it is highly likely that this line has an undo list. Get
  331. rid of it now. */
  332. if (rl_undo_list)
  333. rl_free_undo_list ();
  334. /* Restore normal cursor, if available. */
  335. _rl_set_insert_mode (RL_IM_INSERT, 0);
  336. return (eof ? (char *)NULL : savestring (the_line));
  337. }
  338. void
  339. _rl_internal_char_cleanup ()
  340. {
  341. #if defined (VI_MODE)
  342. /* In vi mode, when you exit insert mode, the cursor moves back
  343. over the previous character. We explicitly check for that here. */
  344. if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
  345. rl_vi_check ();
  346. #endif /* VI_MODE */
  347. if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
  348. {
  349. (*rl_redisplay_function) ();
  350. _rl_want_redisplay = 0;
  351. rl_newline (1, '\n');
  352. }
  353. if (rl_done == 0)
  354. {
  355. (*rl_redisplay_function) ();
  356. _rl_want_redisplay = 0;
  357. }
  358. /* If the application writer has told us to erase the entire line if
  359. the only character typed was something bound to rl_newline, do so. */
  360. if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
  361. rl_point == 0 && rl_end == 0)
  362. _rl_erase_entire_line ();
  363. }
  364. STATIC_CALLBACK int
  365. #if defined (READLINE_CALLBACKS)
  366. readline_internal_char ()
  367. #else
  368. readline_internal_charloop ()
  369. #endif
  370. {
  371. static int lastc, eof_found;
  372. int c, code, lk;
  373. lastc = -1;
  374. eof_found = 0;
  375. #if !defined (READLINE_CALLBACKS)
  376. while (rl_done == 0)
  377. {
  378. #endif
  379. lk = _rl_last_command_was_kill;
  380. code = setjmp (_rl_top_level);
  381. if (code)
  382. {
  383. (*rl_redisplay_function) ();
  384. _rl_want_redisplay = 0;
  385. /* If we get here, we're not being called from something dispatched
  386. from _rl_callback_read_char(), which sets up its own value of
  387. _rl_top_level (saving and restoring the old, of course), so
  388. we can just return here. */
  389. if (RL_ISSTATE (RL_STATE_CALLBACK))
  390. return (0);
  391. }
  392. if (rl_pending_input == 0)
  393. {
  394. /* Then initialize the argument and number of keys read. */
  395. _rl_reset_argument ();
  396. rl_key_sequence_length = 0;
  397. }
  398. RL_SETSTATE(RL_STATE_READCMD);
  399. c = rl_read_key ();
  400. RL_UNSETSTATE(RL_STATE_READCMD);
  401. /* look at input.c:rl_getc() for the circumstances under which this will
  402. be returned; punt immediately on read error without converting it to
  403. a newline. */
  404. if (c == READERR)
  405. {
  406. #if defined (READLINE_CALLBACKS)
  407. RL_SETSTATE(RL_STATE_DONE);
  408. return (rl_done = 1);
  409. #else
  410. eof_found = 1;
  411. break;
  412. #endif
  413. }
  414. /* EOF typed to a non-blank line is a <NL>. */
  415. if (c == EOF && rl_end)
  416. c = NEWLINE;
  417. /* The character _rl_eof_char typed to blank line, and not as the
  418. previous character is interpreted as EOF. */
  419. if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
  420. {
  421. #if defined (READLINE_CALLBACKS)
  422. RL_SETSTATE(RL_STATE_DONE);
  423. return (rl_done = 1);
  424. #else
  425. eof_found = 1;
  426. break;
  427. #endif
  428. }
  429. lastc = c;
  430. _rl_dispatch ((unsigned char)c, _rl_keymap);
  431. RL_CHECK_SIGNALS ();
  432. /* If there was no change in _rl_last_command_was_kill, then no kill
  433. has taken place. Note that if input is pending we are reading
  434. a prefix command, so nothing has changed yet. */
  435. if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
  436. _rl_last_command_was_kill = 0;
  437. _rl_internal_char_cleanup ();
  438. #if defined (READLINE_CALLBACKS)
  439. return 0;
  440. #else
  441. }
  442. return (eof_found);
  443. #endif
  444. }
  445. #if defined (READLINE_CALLBACKS)
  446. static int
  447. readline_internal_charloop ()
  448. {
  449. int eof = 1;
  450. while (rl_done == 0)
  451. eof = readline_internal_char ();
  452. return (eof);
  453. }
  454. #endif /* READLINE_CALLBACKS */
  455. /* Read a line of input from the global rl_instream, doing output on
  456. the global rl_outstream.
  457. If rl_prompt is non-null, then that is our prompt. */
  458. static char *
  459. readline_internal ()
  460. {
  461. int eof;
  462. readline_internal_setup ();
  463. eof = readline_internal_charloop ();
  464. return (readline_internal_teardown (eof));
  465. }
  466. void
  467. _rl_init_line_state ()
  468. {
  469. rl_point = rl_end = rl_mark = 0;
  470. the_line = rl_line_buffer;
  471. the_line[0] = 0;
  472. }
  473. void
  474. _rl_set_the_line ()
  475. {
  476. the_line = rl_line_buffer;
  477. }
  478. #if defined (READLINE_CALLBACKS)
  479. _rl_keyseq_cxt *
  480. _rl_keyseq_cxt_alloc ()
  481. {
  482. _rl_keyseq_cxt *cxt;
  483. cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
  484. cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
  485. cxt->okey = 0;
  486. cxt->ocxt = _rl_kscxt;
  487. cxt->childval = 42; /* sentinel value */
  488. return cxt;
  489. }
  490. void
  491. _rl_keyseq_cxt_dispose (cxt)
  492. _rl_keyseq_cxt *cxt;
  493. {
  494. xfree (cxt);
  495. }
  496. void
  497. _rl_keyseq_chain_dispose ()
  498. {
  499. _rl_keyseq_cxt *cxt;
  500. while (_rl_kscxt)
  501. {
  502. cxt = _rl_kscxt;
  503. _rl_kscxt = _rl_kscxt->ocxt;
  504. _rl_keyseq_cxt_dispose (cxt);
  505. }
  506. }
  507. #endif
  508. static int
  509. _rl_subseq_getchar (key)
  510. int key;
  511. {
  512. int k;
  513. if (key == ESC)
  514. RL_SETSTATE(RL_STATE_METANEXT);
  515. RL_SETSTATE(RL_STATE_MOREINPUT);
  516. k = rl_read_key ();
  517. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  518. if (key == ESC)
  519. RL_UNSETSTATE(RL_STATE_METANEXT);
  520. return k;
  521. }
  522. #if defined (READLINE_CALLBACKS)
  523. int
  524. _rl_dispatch_callback (cxt)
  525. _rl_keyseq_cxt *cxt;
  526. {
  527. int nkey, r;
  528. /* For now */
  529. /* The first time this context is used, we want to read input and dispatch
  530. on it. When traversing the chain of contexts back `up', we want to use
  531. the value from the next context down. We're simulating recursion using
  532. a chain of contexts. */
  533. if ((cxt->flags & KSEQ_DISPATCHED) == 0)
  534. {
  535. nkey = _rl_subseq_getchar (cxt->okey);
  536. if (nkey < 0)
  537. {
  538. _rl_abort_internal ();
  539. return -1;
  540. }
  541. r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
  542. cxt->flags |= KSEQ_DISPATCHED;
  543. }
  544. else
  545. r = cxt->childval;
  546. /* For now */
  547. if (r != -3) /* don't do this if we indicate there will be other matches */
  548. r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
  549. RL_CHECK_SIGNALS ();
  550. if (r == 0) /* success! */
  551. {
  552. _rl_keyseq_chain_dispose ();
  553. RL_UNSETSTATE (RL_STATE_MULTIKEY);
  554. return r;
  555. }
  556. if (r != -3) /* magic value that says we added to the chain */
  557. _rl_kscxt = cxt->ocxt;
  558. if (_rl_kscxt)
  559. _rl_kscxt->childval = r;
  560. if (r != -3)
  561. _rl_keyseq_cxt_dispose (cxt);
  562. return r;
  563. }
  564. #endif /* READLINE_CALLBACKS */
  565. /* Do the command associated with KEY in MAP.
  566. If the associated command is really a keymap, then read
  567. another key, and dispatch into that map. */
  568. int
  569. _rl_dispatch (key, map)
  570. register int key;
  571. Keymap map;
  572. {
  573. _rl_dispatching_keymap = map;
  574. return _rl_dispatch_subseq (key, map, 0);
  575. }
  576. int
  577. _rl_dispatch_subseq (key, map, got_subseq)
  578. register int key;
  579. Keymap map;
  580. int got_subseq;
  581. {
  582. int r, newkey;
  583. char *macro;
  584. rl_command_func_t *func;
  585. #if defined (READLINE_CALLBACKS)
  586. _rl_keyseq_cxt *cxt;
  587. #endif
  588. if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
  589. {
  590. if (map[ESC].type == ISKMAP)
  591. {
  592. if (RL_ISSTATE (RL_STATE_MACRODEF))
  593. _rl_add_macro_char (ESC);
  594. map = FUNCTION_TO_KEYMAP (map, ESC);
  595. key = UNMETA (key);
  596. rl_key_sequence_length += 2;
  597. return (_rl_dispatch (key, map));
  598. }
  599. else
  600. rl_ding ();
  601. return 0;
  602. }
  603. if (RL_ISSTATE (RL_STATE_MACRODEF))
  604. _rl_add_macro_char (key);
  605. r = 0;
  606. switch (map[key].type)
  607. {
  608. case ISFUNC:
  609. func = map[key].function;
  610. if (func)
  611. {
  612. /* Special case rl_do_lowercase_version (). */
  613. if (func == rl_do_lowercase_version)
  614. return (_rl_dispatch (_rl_to_lower (key), map));
  615. rl_executing_keymap = map;
  616. rl_dispatching = 1;
  617. RL_SETSTATE(RL_STATE_DISPATCHING);
  618. (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  619. RL_UNSETSTATE(RL_STATE_DISPATCHING);
  620. rl_dispatching = 0;
  621. /* If we have input pending, then the last command was a prefix
  622. command. Don't change the state of rl_last_func. Otherwise,
  623. remember the last command executed in this variable. */
  624. if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
  625. rl_last_func = map[key].function;
  626. RL_CHECK_SIGNALS ();
  627. }
  628. else if (map[ANYOTHERKEY].function)
  629. {
  630. /* OK, there's no function bound in this map, but there is a
  631. shadow function that was overridden when the current keymap
  632. was created. Return -2 to note that. */
  633. _rl_unget_char (key);
  634. return -2;
  635. }
  636. else if (got_subseq)
  637. {
  638. /* Return -1 to note that we're in a subsequence, but we don't
  639. have a matching key, nor was one overridden. This means
  640. we need to back up the recursion chain and find the last
  641. subsequence that is bound to a function. */
  642. _rl_unget_char (key);
  643. return -1;
  644. }
  645. else
  646. {
  647. #if defined (READLINE_CALLBACKS)
  648. RL_UNSETSTATE (RL_STATE_MULTIKEY);
  649. _rl_keyseq_chain_dispose ();
  650. #endif
  651. _rl_abort_internal ();
  652. return -1;
  653. }
  654. break;
  655. case ISKMAP:
  656. if (map[key].function != 0)
  657. {
  658. #if defined (VI_MODE)
  659. /* The only way this test will be true is if a subsequence has been
  660. bound starting with ESC, generally the arrow keys. What we do is
  661. check whether there's input in the queue, which there generally
  662. will be if an arrow key has been pressed, and, if there's not,
  663. just dispatch to (what we assume is) rl_vi_movement_mode right
  664. away. This is essentially an input test with a zero timeout. */
  665. if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap
  666. && _rl_input_queued (0) == 0)
  667. return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
  668. #endif
  669. rl_key_sequence_length++;
  670. _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
  671. /* Allocate new context here. Use linked contexts (linked through
  672. cxt->ocxt) to simulate recursion */
  673. #if defined (READLINE_CALLBACKS)
  674. if (RL_ISSTATE (RL_STATE_CALLBACK))
  675. {
  676. /* Return 0 only the first time, to indicate success to
  677. _rl_callback_read_char. The rest of the time, we're called
  678. from _rl_dispatch_callback, so we return -3 to indicate
  679. special handling is necessary. */
  680. r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
  681. cxt = _rl_keyseq_cxt_alloc ();
  682. if (got_subseq)
  683. cxt->flags |= KSEQ_SUBSEQ;
  684. cxt->okey = key;
  685. cxt->oldmap = map;
  686. cxt->dmap = _rl_dispatching_keymap;
  687. cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
  688. RL_SETSTATE (RL_STATE_MULTIKEY);
  689. _rl_kscxt = cxt;
  690. return r; /* don't indicate immediate success */
  691. }
  692. #endif
  693. newkey = _rl_subseq_getchar (key);
  694. if (newkey < 0)
  695. {
  696. _rl_abort_internal ();
  697. return -1;
  698. }
  699. r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
  700. return _rl_subseq_result (r, map, key, got_subseq);
  701. }
  702. else
  703. {
  704. _rl_abort_internal ();
  705. return -1;
  706. }
  707. break;
  708. case ISMACR:
  709. if (map[key].function != 0)
  710. {
  711. macro = savestring ((char *)map[key].function);
  712. _rl_with_macro_input (macro);
  713. return 0;
  714. }
  715. break;
  716. }
  717. #if defined (VI_MODE)
  718. if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
  719. key != ANYOTHERKEY &&
  720. _rl_vi_textmod_command (key))
  721. _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
  722. #endif
  723. return (r);
  724. }
  725. static int
  726. _rl_subseq_result (r, map, key, got_subseq)
  727. int r;
  728. Keymap map;
  729. int key, got_subseq;
  730. {
  731. Keymap m;
  732. int type, nt;
  733. rl_command_func_t *func, *nf;
  734. if (r == -2)
  735. /* We didn't match anything, and the keymap we're indexed into
  736. shadowed a function previously bound to that prefix. Call
  737. the function. The recursive call to _rl_dispatch_subseq has
  738. already taken care of pushing any necessary input back onto
  739. the input queue with _rl_unget_char. */
  740. {
  741. m = _rl_dispatching_keymap;
  742. type = m[ANYOTHERKEY].type;
  743. func = m[ANYOTHERKEY].function;
  744. if (type == ISFUNC && func == rl_do_lowercase_version)
  745. r = _rl_dispatch (_rl_to_lower (key), map);
  746. else if (type == ISFUNC && func == rl_insert)
  747. {
  748. /* If the function that was shadowed was self-insert, we
  749. somehow need a keymap with map[key].func == self-insert.
  750. Let's use this one. */
  751. nt = m[key].type;
  752. nf = m[key].function;
  753. m[key].type = type;
  754. m[key].function = func;
  755. r = _rl_dispatch (key, m);
  756. m[key].type = nt;
  757. m[key].function = nf;
  758. }
  759. else
  760. r = _rl_dispatch (ANYOTHERKEY, m);
  761. }
  762. else if (r && map[ANYOTHERKEY].function)
  763. {
  764. /* We didn't match (r is probably -1), so return something to
  765. tell the caller that it should try ANYOTHERKEY for an
  766. overridden function. */
  767. _rl_unget_char (key);
  768. _rl_dispatching_keymap = map;
  769. return -2;
  770. }
  771. else if (r && got_subseq)
  772. {
  773. /* OK, back up the chain. */
  774. _rl_unget_char (key);
  775. _rl_dispatching_keymap = map;
  776. return -1;
  777. }
  778. return r;
  779. }
  780. /* **************************************************************** */
  781. /* */
  782. /* Initializations */
  783. /* */
  784. /* **************************************************************** */
  785. /* Initialize readline (and terminal if not already). */
  786. int
  787. rl_initialize ()
  788. {
  789. /* If we have never been called before, initialize the
  790. terminal and data structures. */
  791. if (!rl_initialized)
  792. {
  793. RL_SETSTATE(RL_STATE_INITIALIZING);
  794. readline_initialize_everything ();
  795. RL_UNSETSTATE(RL_STATE_INITIALIZING);
  796. rl_initialized++;
  797. RL_SETSTATE(RL_STATE_INITIALIZED);
  798. }
  799. /* Initalize the current line information. */
  800. _rl_init_line_state ();
  801. /* We aren't done yet. We haven't even gotten started yet! */
  802. rl_done = 0;
  803. RL_UNSETSTATE(RL_STATE_DONE);
  804. /* Tell the history routines what is going on. */
  805. _rl_start_using_history ();
  806. /* Make the display buffer match the state of the line. */
  807. rl_reset_line_state ();
  808. /* No such function typed yet. */
  809. rl_last_func = (rl_command_func_t *)NULL;
  810. /* Parsing of key-bindings begins in an enabled state. */
  811. _rl_parsing_conditionalized_out = 0;
  812. #if defined (VI_MODE)
  813. if (rl_editing_mode == vi_mode)
  814. _rl_vi_initialize_line ();
  815. #endif
  816. /* Each line starts in insert mode (the default). */
  817. _rl_set_insert_mode (RL_IM_DEFAULT, 1);
  818. return 0;
  819. }
  820. #if 0
  821. #if defined (__EMX__)
  822. static void
  823. _emx_build_environ ()
  824. {
  825. TIB *tibp;
  826. PIB *pibp;
  827. char *t, **tp;
  828. int c;
  829. DosGetInfoBlocks (&tibp, &pibp);
  830. t = pibp->pib_pchenv;
  831. for (c = 1; *t; c++)
  832. t += strlen (t) + 1;
  833. tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
  834. t = pibp->pib_pchenv;
  835. while (*t)
  836. {
  837. *tp++ = t;
  838. t += strlen (t) + 1;
  839. }
  840. *tp = 0;
  841. }
  842. #endif /* __EMX__ */
  843. #endif
  844. /* Initialize the entire state of the world. */
  845. static void
  846. readline_initialize_everything ()
  847. {
  848. #if 0
  849. #if defined (__EMX__)
  850. if (environ == 0)
  851. _emx_build_environ ();
  852. #endif
  853. #endif
  854. #if 0
  855. /* Find out if we are running in Emacs -- UNUSED. */
  856. running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
  857. #endif
  858. /* Set up input and output if they are not already set up. */
  859. if (!rl_instream)
  860. rl_instream = stdin;
  861. if (!rl_outstream)
  862. rl_outstream = stdout;
  863. /* Bind _rl_in_stream and _rl_out_stream immediately. These values
  864. may change, but they may also be used before readline_internal ()
  865. is called. */
  866. _rl_in_stream = rl_instream;
  867. _rl_out_stream = rl_outstream;
  868. /* Allocate data structures. */
  869. if (rl_line_buffer == 0)
  870. rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  871. /* Initialize the terminal interface. */
  872. if (rl_terminal_name == 0)
  873. rl_terminal_name = sh_get_env_value ("TERM");
  874. _rl_init_terminal_io (rl_terminal_name);
  875. /* Bind tty characters to readline functions. */
  876. readline_default_bindings ();
  877. /* Initialize the function names. */
  878. rl_initialize_funmap ();
  879. /* Decide whether we should automatically go into eight-bit mode. */
  880. _rl_init_eightbit ();
  881. /* Read in the init file. */
  882. rl_read_init_file ((char *)NULL);
  883. /* XXX */
  884. if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
  885. {
  886. _rl_screenwidth--;
  887. _rl_screenchars -= _rl_screenheight;
  888. }
  889. /* Override the effect of any `set keymap' assignments in the
  890. inputrc file. */
  891. rl_set_keymap_from_edit_mode ();
  892. /* Try to bind a common arrow key prefix, if not already bound. */
  893. bind_arrow_keys ();
  894. /* Enable the meta key, if this terminal has one. */
  895. if (_rl_enable_meta)
  896. _rl_enable_meta_key ();
  897. /* If the completion parser's default word break characters haven't
  898. been set yet, then do so now. */
  899. if (rl_completer_word_break_characters == (char *)NULL)
  900. rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
  901. }
  902. /* If this system allows us to look at the values of the regular
  903. input editing characters, then bind them to their readline
  904. equivalents, iff the characters are not bound to keymaps. */
  905. static void
  906. readline_default_bindings ()
  907. {
  908. if (_rl_bind_stty_chars)
  909. rl_tty_set_default_bindings (_rl_keymap);
  910. }
  911. /* Reset the default bindings for the terminal special characters we're
  912. interested in back to rl_insert and read the new ones. */
  913. static void
  914. reset_default_bindings ()
  915. {
  916. if (_rl_bind_stty_chars)
  917. {
  918. rl_tty_unset_default_bindings (_rl_keymap);
  919. rl_tty_set_default_bindings (_rl_keymap);
  920. }
  921. }
  922. /* Bind some common arrow key sequences in MAP. */
  923. static void
  924. bind_arrow_keys_internal (map)
  925. Keymap map;
  926. {
  927. Keymap xkeymap;
  928. xkeymap = _rl_keymap;
  929. _rl_keymap = map;
  930. #if defined (__MSDOS__)
  931. rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
  932. rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
  933. rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
  934. rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
  935. #endif
  936. rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
  937. rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
  938. rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
  939. rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
  940. rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
  941. rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
  942. rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
  943. rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
  944. rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
  945. rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
  946. rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
  947. rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
  948. #if defined (__MINGW32__)
  949. rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
  950. rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
  951. rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
  952. rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
  953. rl_bind_keyseq_if_unbound ("\340G", rl_beg_of_line);
  954. rl_bind_keyseq_if_unbound ("\340O", rl_end_of_line);
  955. rl_bind_keyseq_if_unbound ("\340S", rl_delete);
  956. rl_bind_keyseq_if_unbound ("\340R", rl_overwrite_mode);
  957. #endif
  958. _rl_keymap = xkeymap;
  959. }
  960. /* Try and bind the common arrow key prefixes after giving termcap and
  961. the inputrc file a chance to bind them and create `real' keymaps
  962. for the arrow key prefix. */
  963. static void
  964. bind_arrow_keys ()
  965. {
  966. bind_arrow_keys_internal (emacs_standard_keymap);
  967. #if defined (VI_MODE)
  968. bind_arrow_keys_internal (vi_movement_keymap);
  969. /* Unbind vi_movement_keymap[ESC] to allow users to repeatedly hit ESC
  970. in vi command mode while still allowing the arrow keys to work. */
  971. if (vi_movement_keymap[ESC].type == ISKMAP)
  972. rl_bind_keyseq_in_map ("\033", (rl_command_func_t *)NULL, vi_movement_keymap);
  973. bind_arrow_keys_internal (vi_insertion_keymap);
  974. #endif
  975. }
  976. /* **************************************************************** */
  977. /* */
  978. /* Saving and Restoring Readline's state */
  979. /* */
  980. /* **************************************************************** */
  981. int
  982. rl_save_state (sp)
  983. struct readline_state *sp;
  984. {
  985. if (sp == 0)
  986. return -1;
  987. sp->point = rl_point;
  988. sp->end = rl_end;
  989. sp->mark = rl_mark;
  990. sp->buffer = rl_line_buffer;
  991. sp->buflen = rl_line_buffer_len;
  992. sp->ul = rl_undo_list;
  993. sp->prompt = rl_prompt;
  994. sp->rlstate = rl_readline_state;
  995. sp->done = rl_done;
  996. sp->kmap = _rl_keymap;
  997. sp->lastfunc = rl_last_func;
  998. sp->insmode = rl_insert_mode;
  999. sp->edmode = rl_editing_mode;
  1000. sp->kseqlen = rl_key_sequence_length;
  1001. sp->inf = rl_instream;
  1002. sp->outf = rl_outstream;
  1003. sp->pendingin = rl_pending_input;
  1004. sp->macro = rl_executing_macro;
  1005. sp->catchsigs = rl_catch_signals;
  1006. sp->catchsigwinch = rl_catch_sigwinch;
  1007. return (0);
  1008. }
  1009. int
  1010. rl_restore_state (sp)
  1011. struct readline_state *sp;
  1012. {
  1013. if (sp == 0)
  1014. return -1;
  1015. rl_point = sp->point;
  1016. rl_end = sp->end;
  1017. rl_mark = sp->mark;
  1018. the_line = rl_line_buffer = sp->buffer;
  1019. rl_line_buffer_len = sp->buflen;
  1020. rl_undo_list = sp->ul;
  1021. rl_prompt = sp->prompt;
  1022. rl_readline_state = sp->rlstate;
  1023. rl_done = sp->done;
  1024. _rl_keymap = sp->kmap;
  1025. rl_last_func = sp->lastfunc;
  1026. rl_insert_mode = sp->insmode;
  1027. rl_editing_mode = sp->edmode;
  1028. rl_key_sequence_length = sp->kseqlen;
  1029. rl_instream = sp->inf;
  1030. rl_outstream = sp->outf;
  1031. rl_pending_input = sp->pendingin;
  1032. rl_executing_macro = sp->macro;
  1033. rl_catch_signals = sp->catchsigs;
  1034. rl_catch_sigwinch = sp->catchsigwinch;
  1035. return (0);
  1036. }