PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/src/mswin/rdln/readline.c

https://bitbucket.org/nrnhines/nrn
C | 6803 lines | 4776 code | 973 blank | 1054 comment | 896 complexity | 8f48c6c0d52777e3efdce5f1bec6d038 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. /* readline.c -- a general facility for reading lines of input
  2. with emacs style editing and completion. */
  3. /* Copyright 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
  4. This file contains the Readline Library (the Library), a set of
  5. routines for providing Emacs style line input to programs that ask
  6. for it.
  7. The Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. The Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. /* Remove these declarations when we have a complete libgnu.a. */
  19. #if defined(__MSC_VER)
  20. #include <../../nrnconf.h>
  21. #endif
  22. #ifdef WIN32
  23. #define STATIC_MALLOC
  24. #define rindex strrchr
  25. #endif
  26. /* #define STATIC_MALLOC */
  27. #if defined(_MSC_VER) || defined(__MWERKS__)
  28. #include <unistd.h>
  29. #include <malloc.h>
  30. #include <string.h>
  31. #endif
  32. /* Caseless strcmp (). */
  33. #if defined(__MWERKS__)
  34. static int stricmp (), strnicmp ();
  35. #endif
  36. #if !defined (STATIC_MALLOC)
  37. extern char *xmalloc (), *xrealloc ();
  38. #else
  39. static char *xmalloc (), *xrealloc ();
  40. #endif /* STATIC_MALLOC */
  41. #include "sysdep.h"
  42. #include <stdio.h>
  43. #include <fcntl.h>
  44. #ifdef WIN32
  45. #define NO_SYS_FILE
  46. #endif
  47. #ifndef NO_SYS_FILE
  48. #include <sys/file.h>
  49. #endif
  50. #include <signal.h>
  51. #if defined (HAVE_UNISTD_H)
  52. # include <unistd.h>
  53. #endif
  54. #ifndef WIN32
  55. #define NEW_TTY_DRIVER
  56. #endif
  57. #define HAVE_BSD_SIGNALS
  58. /* #define USE_XON_XOFF */
  59. #if __GO32__
  60. #define GRX 1
  61. #endif
  62. #if GRX
  63. extern int egagrph;
  64. #endif
  65. #ifdef __MSDOS__
  66. #undef NEW_TTY_DRIVER
  67. #undef HAVE_BSD_SIGNALS
  68. #endif
  69. /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
  70. #if defined (USG) && !defined (hpux)
  71. #undef HAVE_BSD_SIGNALS
  72. #endif
  73. /* System V machines use termio. */
  74. #if !defined (_POSIX_VERSION)
  75. # if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX) || defined (__H3050R) || defined (__H3050RX)
  76. # undef NEW_TTY_DRIVER
  77. # define TERMIO_TTY_DRIVER
  78. # include <termio.h>
  79. # if !defined (TCOON)
  80. # define TCOON 1
  81. # endif
  82. # endif /* USG || hpux || Xenix || sgi || DUGX */
  83. #endif /* !_POSIX_VERSION */
  84. /* Posix systems use termios and the Posix signal functions. */
  85. #if defined (_POSIX_VERSION)
  86. # if !defined (TERMIOS_MISSING)
  87. # undef NEW_TTY_DRIVER
  88. # define TERMIOS_TTY_DRIVER
  89. # include <termios.h>
  90. # endif /* !TERMIOS_MISSING */
  91. # define HAVE_POSIX_SIGNALS
  92. # if !defined (O_NDELAY)
  93. # define O_NDELAY O_NONBLOCK /* Posix-style non-blocking i/o */
  94. # endif /* O_NDELAY */
  95. #endif /* _POSIX_VERSION */
  96. /* Other (BSD) machines use sgtty. */
  97. #if defined (NEW_TTY_DRIVER)
  98. #include <sgtty.h>
  99. #endif
  100. /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
  101. it is not already defined. It is used both to determine if a
  102. special character is disabled and to disable certain special
  103. characters. Posix systems should set to 0, USG systems to -1. */
  104. #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
  105. # if defined (_POSIX_VERSION)
  106. # define _POSIX_VDISABLE 0
  107. # else /* !_POSIX_VERSION */
  108. # define _POSIX_VDISABLE -1
  109. # endif /* !_POSIX_VERSION */
  110. #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
  111. /* Define some macros for dealing with assorted signalling disciplines.
  112. These macros provide a way to use signal blocking and disabling
  113. without smothering your code in a pile of #ifdef's.
  114. SIGNALS_UNBLOCK; Stop blocking all signals.
  115. {
  116. SIGNALS_DECLARE_SAVED (name); Declare a variable to save the
  117. signal blocking state.
  118. ...
  119. SIGNALS_BLOCK (SIGSTOP, name); Block a signal, and save the previous
  120. state for restoration later.
  121. ...
  122. SIGNALS_RESTORE (name); Restore previous signals.
  123. }
  124. */
  125. #ifdef HAVE_POSIX_SIGNALS
  126. /* POSIX signals */
  127. #define SIGNALS_UNBLOCK \
  128. do { sigset_t set; \
  129. sigemptyset (&set); \
  130. sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); \
  131. } while (0)
  132. #define SIGNALS_DECLARE_SAVED(name) sigset_t name
  133. #define SIGNALS_BLOCK(SIG, saved) \
  134. do { sigset_t set; \
  135. sigemptyset (&set); \
  136. sigaddset (&set, SIG); \
  137. sigprocmask (SIG_BLOCK, &set, &saved); \
  138. } while (0)
  139. #define SIGNALS_RESTORE(saved) \
  140. sigprocmask (SIG_SETMASK, &saved, (sigset_t *)NULL)
  141. #else /* HAVE_POSIX_SIGNALS */
  142. #ifdef HAVE_BSD_SIGNALS
  143. /* BSD signals */
  144. #define SIGNALS_UNBLOCK sigsetmask (0)
  145. #define SIGNALS_DECLARE_SAVED(name) int name
  146. #define SIGNALS_BLOCK(SIG, saved) saved = sigblock (sigmask (SIG))
  147. #define SIGNALS_RESTORE(saved) sigsetmask (saved)
  148. #else /* HAVE_BSD_SIGNALS */
  149. /* None of the Above */
  150. #define SIGNALS_UNBLOCK /* nothing */
  151. #define SIGNALS_DECLARE_SAVED(name) /* nothing */
  152. #define SIGNALS_BLOCK(SIG, saved) /* nothing */
  153. #define SIGNALS_RESTORE(saved) /* nothing */
  154. #endif /* HAVE_BSD_SIGNALS */
  155. #endif /* HAVE_POSIX_SIGNALS */
  156. /* End of signal handling definitions. */
  157. #include <errno.h>
  158. #include <setjmp.h>
  159. #include <sys/stat.h>
  160. /* Posix macro to check file in statbuf for directory-ness. */
  161. #if defined (S_IFDIR) && !defined (S_ISDIR)
  162. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  163. #endif
  164. #if !defined(__MSDOS__) && !defined(WIN32)
  165. /* These next are for filename completion. Perhaps this belongs
  166. in a different place. */
  167. #include <pwd.h>
  168. #endif /* __MSDOS__ */
  169. #if defined (USG) && !defined (isc386) && !defined (sgi)
  170. struct passwd *getpwuid (), *getpwent ();
  171. #endif
  172. /* #define HACK_TERMCAP_MOTION */
  173. /* Some standard library routines. */
  174. #include "readline.h"
  175. #include "history.h"
  176. #ifndef digit
  177. #define digit(c) ((c) >= '0' && (c) <= '9')
  178. #endif
  179. #ifndef isletter
  180. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  181. #endif
  182. #ifndef digit_value
  183. #define digit_value(c) ((c) - '0')
  184. #endif
  185. #ifndef member
  186. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  187. #endif
  188. #ifndef isident
  189. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  190. #endif
  191. #ifndef exchange
  192. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  193. #endif
  194. #if !defined (rindex)
  195. extern char *rindex ();
  196. #endif /* rindex */
  197. #if !defined (index)
  198. extern char *index ();
  199. #endif /* index */
  200. extern char *getenv ();
  201. extern char *tilde_expand ();
  202. char* tilde_expand() { return savestring("/");}
  203. static update_line ();
  204. static void output_character_function ();
  205. static delete_chars ();
  206. static void insert_some_chars ();
  207. #if defined (VOID_SIGHANDLER)
  208. # define sighandler void
  209. #else
  210. # define sighandler int
  211. #endif /* VOID_SIGHANDLER */
  212. /* This typedef is equivalant to the one for Function; it allows us
  213. to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  214. typedef sighandler SigHandler ();
  215. /* If on, then readline handles signals in a way that doesn't screw. */
  216. #ifndef WIN32
  217. #define HANDLE_SIGNALS
  218. #endif
  219. #if defined(__GO32__)
  220. #ifdef RSX
  221. #include <termio.h>
  222. #else
  223. #include <pc.h>
  224. #endif
  225. #undef HANDLE_SIGNALS
  226. #endif
  227. /* **************************************************************** */
  228. /* */
  229. /* Line editing input utility */
  230. /* */
  231. /* **************************************************************** */
  232. /* A pointer to the keymap that is currently in use.
  233. By default, it is the standard emacs keymap. */
  234. Keymap keymap = emacs_standard_keymap;
  235. #define no_mode -1
  236. #define vi_mode 0
  237. #define emacs_mode 1
  238. /* The current style of editing. */
  239. int rl_editing_mode = emacs_mode;
  240. /* Non-zero if the previous command was a kill command. */
  241. static int last_command_was_kill = 0;
  242. /* The current value of the numeric argument specified by the user. */
  243. int rl_numeric_arg = 1;
  244. /* Non-zero if an argument was typed. */
  245. int rl_explicit_arg = 0;
  246. /* Temporary value used while generating the argument. */
  247. int rl_arg_sign = 1;
  248. /* Non-zero means we have been called at least once before. */
  249. static int rl_initialized = 0;
  250. /* If non-zero, this program is running in an EMACS buffer. */
  251. static char *running_in_emacs = (char *)NULL;
  252. /* The current offset in the current input line. */
  253. int rl_point;
  254. /* Mark in the current input line. */
  255. int rl_mark;
  256. /* Length of the current input line. */
  257. int rl_end;
  258. /* Make this non-zero to return the current input_line. */
  259. int rl_done;
  260. /* The last function executed by readline. */
  261. Function *rl_last_func = (Function *)NULL;
  262. /* Top level environment for readline_internal (). */
  263. static jmp_buf readline_top_level;
  264. /* The streams we interact with. */
  265. static FILE *in_stream, *out_stream;
  266. /* The names of the streams that we do input and output to. */
  267. FILE *rl_instream, *rl_outstream;
  268. /* Non-zero means echo characters as they are read. */
  269. int readline_echoing_p = 1;
  270. /* Current prompt. */
  271. char *rl_prompt;
  272. /* The number of characters read in order to type this complete command. */
  273. int rl_key_sequence_length = 0;
  274. /* If non-zero, then this is the address of a function to call just
  275. before readline_internal () prints the first prompt. */
  276. Function *rl_startup_hook = (Function *)NULL;
  277. /* If non-zero, then this is the address of a function to call when
  278. completing on a directory name. The function is called with
  279. the address of a string (the current directory name) as an arg. */
  280. Function *rl_symbolic_link_hook = (Function *)NULL;
  281. /* What we use internally. You should always refer to RL_LINE_BUFFER. */
  282. static char *the_line;
  283. /* The character that can generate an EOF. Really read from
  284. the terminal driver... just defaulted here. */
  285. static int eof_char = CTRL ('D');
  286. /* Non-zero makes this the next keystroke to read. */
  287. int rl_pending_input = 0;
  288. /* Pointer to a useful terminal name. */
  289. char *rl_terminal_name = (char *)NULL;
  290. /* Line buffer and maintenence. */
  291. char *rl_line_buffer = (char *)NULL;
  292. int rl_line_buffer_len = 0;
  293. #define DEFAULT_BUFFER_SIZE 256
  294. /* **************************************************************** */
  295. /* */
  296. /* `Forward' declarations */
  297. /* */
  298. /* **************************************************************** */
  299. /* Non-zero means do not parse any lines other than comments and
  300. parser directives. */
  301. static unsigned char parsing_conditionalized_out = 0;
  302. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  303. static int defining_kbd_macro = 0;
  304. /* **************************************************************** */
  305. /* */
  306. /* Top Level Functions */
  307. /* */
  308. /* **************************************************************** */
  309. static void rl_prep_terminal (), rl_deprep_terminal ();
  310. static void clear_to_eol (), rl_generic_bind ();
  311. /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
  312. none. A return value of NULL means that EOF was encountered. */
  313. char *
  314. readline (prompt)
  315. char *prompt;
  316. {
  317. char *readline_internal ();
  318. char *value;
  319. rl_prompt = prompt;
  320. /* If we are at EOF return a NULL string. */
  321. if (rl_pending_input == EOF)
  322. {
  323. rl_pending_input = 0;
  324. return ((char *)NULL);
  325. }
  326. rl_initialize ();
  327. rl_prep_terminal ();
  328. #if defined (HANDLE_SIGNALS)
  329. rl_set_signals ();
  330. #endif
  331. value = readline_internal ();
  332. rl_deprep_terminal ();
  333. #if defined (HANDLE_SIGNALS)
  334. rl_clear_signals ();
  335. #endif
  336. return (value);
  337. }
  338. /* Read a line of input from the global rl_instream, doing output on
  339. the global rl_outstream.
  340. If rl_prompt is non-null, then that is our prompt. */
  341. char *
  342. readline_internal ()
  343. {
  344. int lastc, c, eof_found;
  345. in_stream = rl_instream;
  346. out_stream = rl_outstream;
  347. lastc = -1;
  348. eof_found = 0;
  349. if (rl_startup_hook)
  350. (*rl_startup_hook) ();
  351. if (!readline_echoing_p)
  352. {
  353. if (rl_prompt)
  354. {
  355. fprintf (out_stream, "%s", rl_prompt);
  356. fflush (out_stream);
  357. }
  358. }
  359. else
  360. {
  361. rl_on_new_line ();
  362. rl_redisplay ();
  363. #if defined (VI_MODE)
  364. if (rl_editing_mode == vi_mode)
  365. rl_vi_insertion_mode ();
  366. #endif /* VI_MODE */
  367. }
  368. while (!rl_done)
  369. {
  370. int lk = last_command_was_kill;
  371. int code = setjmp (readline_top_level);
  372. if (code)
  373. rl_redisplay ();
  374. if (!rl_pending_input)
  375. {
  376. /* Then initialize the argument and number of keys read. */
  377. rl_init_argument ();
  378. rl_key_sequence_length = 0;
  379. }
  380. c = rl_read_key ();
  381. #ifdef WIN32
  382. if (!winio_exists()) {
  383. eof_found = 1;
  384. break;
  385. }
  386. #endif
  387. /* EOF typed to a non-blank line is a <NL>. */
  388. if (c == EOF && rl_end)
  389. c = NEWLINE;
  390. /* The character eof_char typed to blank line, and not as the
  391. previous character is interpreted as EOF. */
  392. if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  393. {
  394. eof_found = 1;
  395. break;
  396. }
  397. lastc = c;
  398. rl_dispatch (c, keymap);
  399. /* If there was no change in last_command_was_kill, then no kill
  400. has taken place. Note that if input is pending we are reading
  401. a prefix command, so nothing has changed yet. */
  402. if (!rl_pending_input)
  403. {
  404. if (lk == last_command_was_kill)
  405. last_command_was_kill = 0;
  406. }
  407. #if defined (VI_MODE)
  408. /* In vi mode, when you exit insert mode, the cursor moves back
  409. over the previous character. We explicitly check for that here. */
  410. if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  411. rl_vi_check ();
  412. #endif /* VI_MODE */
  413. if (!rl_done)
  414. rl_redisplay ();
  415. }
  416. /* Restore the original of this history line, iff the line that we
  417. are editing was originally in the history, AND the line has changed. */
  418. {
  419. HIST_ENTRY *entry = current_history ();
  420. if (entry && rl_undo_list)
  421. {
  422. char *temp = savestring (the_line);
  423. rl_revert_line ();
  424. entry = replace_history_entry (where_history (), the_line,
  425. (HIST_ENTRY *)NULL);
  426. free_history_entry (entry);
  427. strcpy (the_line, temp);
  428. free (temp);
  429. }
  430. }
  431. /* At any rate, it is highly likely that this line has an undo list. Get
  432. rid of it now. */
  433. if (rl_undo_list)
  434. free_undo_list ();
  435. if (eof_found)
  436. return (char *)NULL;
  437. else
  438. return (savestring (the_line));
  439. }
  440. /* **************************************************************** */
  441. /* */
  442. /* Signal Handling */
  443. /* */
  444. /* **************************************************************** */
  445. #if defined (SIGWINCH)
  446. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  447. static sighandler
  448. rl_handle_sigwinch (sig)
  449. int sig;
  450. {
  451. char *term;
  452. term = rl_terminal_name;
  453. if (readline_echoing_p)
  454. {
  455. if (!term)
  456. term = getenv ("TERM");
  457. if (!term)
  458. term = "dumb";
  459. rl_reset_terminal (term);
  460. #if defined (NOTDEF)
  461. crlf ();
  462. rl_forced_update_display ();
  463. #endif /* NOTDEF */
  464. }
  465. if (old_sigwinch &&
  466. old_sigwinch != (SigHandler *)SIG_IGN &&
  467. old_sigwinch != (SigHandler *)SIG_DFL)
  468. (*old_sigwinch) (sig);
  469. #if !defined (VOID_SIGHANDLER)
  470. return (0);
  471. #endif /* VOID_SIGHANDLER */
  472. }
  473. #endif /* SIGWINCH */
  474. #if defined (HANDLE_SIGNALS)
  475. /* Interrupt handling. */
  476. static SigHandler
  477. *old_int = (SigHandler *)NULL,
  478. *old_tstp = (SigHandler *)NULL,
  479. *old_ttou = (SigHandler *)NULL,
  480. *old_ttin = (SigHandler *)NULL,
  481. *old_cont = (SigHandler *)NULL,
  482. *old_alrm = (SigHandler *)NULL;
  483. /* Handle an interrupt character. */
  484. static sighandler
  485. rl_signal_handler (sig)
  486. int sig;
  487. {
  488. #if !defined (HAVE_BSD_SIGNALS)
  489. /* Since the signal will not be blocked while we are in the signal
  490. handler, ignore it until rl_clear_signals resets the catcher. */
  491. if (sig == SIGINT)
  492. signal (sig, SIG_IGN);
  493. #endif /* !HAVE_BSD_SIGNALS */
  494. switch (sig)
  495. {
  496. case SIGINT:
  497. free_undo_list ();
  498. rl_clear_message ();
  499. rl_init_argument ();
  500. #if defined (SIGTSTP)
  501. case SIGTSTP:
  502. case SIGTTOU:
  503. case SIGTTIN:
  504. #endif /* SIGTSTP */
  505. case SIGALRM:
  506. rl_clean_up_for_exit ();
  507. rl_deprep_terminal ();
  508. rl_clear_signals ();
  509. rl_pending_input = 0;
  510. kill (getpid (), sig);
  511. SIGNALS_UNBLOCK;
  512. rl_prep_terminal ();
  513. rl_set_signals ();
  514. }
  515. #if !defined (VOID_SIGHANDLER)
  516. return (0);
  517. #endif /* !VOID_SIGHANDLER */
  518. }
  519. rl_set_signals ()
  520. {
  521. old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  522. if (old_int == (SigHandler *)SIG_IGN)
  523. signal (SIGINT, SIG_IGN);
  524. old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
  525. if (old_alrm == (SigHandler *)SIG_IGN)
  526. signal (SIGALRM, SIG_IGN);
  527. #if defined (SIGTSTP)
  528. old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  529. if (old_tstp == (SigHandler *)SIG_IGN)
  530. signal (SIGTSTP, SIG_IGN);
  531. #endif
  532. #if defined (SIGTTOU)
  533. old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  534. old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  535. if (old_tstp == (SigHandler *)SIG_IGN)
  536. {
  537. signal (SIGTTOU, SIG_IGN);
  538. signal (SIGTTIN, SIG_IGN);
  539. }
  540. #endif
  541. #if defined (SIGWINCH)
  542. old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  543. #endif
  544. }
  545. rl_clear_signals ()
  546. {
  547. signal (SIGINT, old_int);
  548. signal (SIGALRM, old_alrm);
  549. #if defined (SIGTSTP)
  550. signal (SIGTSTP, old_tstp);
  551. #endif
  552. #if defined (SIGTTOU)
  553. signal (SIGTTOU, old_ttou);
  554. signal (SIGTTIN, old_ttin);
  555. #endif
  556. #if defined (SIGWINCH)
  557. signal (SIGWINCH, old_sigwinch);
  558. #endif
  559. }
  560. #endif /* HANDLE_SIGNALS */
  561. /* **************************************************************** */
  562. /* */
  563. /* Character Input Buffering */
  564. /* */
  565. /* **************************************************************** */
  566. #if defined (USE_XON_XOFF)
  567. /* If the terminal was in xoff state when we got to it, then xon_char
  568. contains the character that is supposed to start it again. */
  569. static int xon_char, xoff_state;
  570. #endif /* USE_XON_XOFF */
  571. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  572. static unsigned char ibuffer[512];
  573. /* Non-null means it is a pointer to a function to run while waiting for
  574. character input. */
  575. Function *rl_event_hook = (Function *)NULL;
  576. #define any_typein (push_index != pop_index)
  577. /* Add KEY to the buffer of characters to be read. */
  578. rl_stuff_char (key)
  579. int key;
  580. {
  581. if (key == EOF)
  582. {
  583. key = NEWLINE;
  584. rl_pending_input = EOF;
  585. }
  586. ibuffer[push_index++] = key;
  587. if (push_index >= ibuffer_len)
  588. push_index = 0;
  589. }
  590. /* Return the amount of space available in the
  591. buffer for stuffing characters. */
  592. int
  593. ibuffer_space ()
  594. {
  595. if (pop_index > push_index)
  596. return (pop_index - push_index);
  597. else
  598. return (ibuffer_len - (push_index - pop_index));
  599. }
  600. /* Get a key from the buffer of characters to be read.
  601. Return the key in KEY.
  602. Result is KEY if there was a key, or 0 if there wasn't. */
  603. int
  604. rl_get_char (key)
  605. int *key;
  606. {
  607. if (push_index == pop_index)
  608. return (0);
  609. *key = ibuffer[pop_index++];
  610. if (pop_index >= ibuffer_len)
  611. pop_index = 0;
  612. return (1);
  613. }
  614. /* Stuff KEY into the *front* of the input buffer.
  615. Returns non-zero if successful, zero if there is
  616. no space left in the buffer. */
  617. int
  618. rl_unget_char (key)
  619. int key;
  620. {
  621. if (ibuffer_space ())
  622. {
  623. pop_index--;
  624. if (pop_index < 0)
  625. pop_index = ibuffer_len - 1;
  626. ibuffer[pop_index] = key;
  627. return (1);
  628. }
  629. return (0);
  630. }
  631. /* If a character is available to be read, then read it
  632. and stuff it into IBUFFER. Otherwise, just return. */
  633. rl_gather_tyi ()
  634. {
  635. #ifdef WIN32
  636. while (kbhit() && ibuffer_space()) {
  637. rl_stuff_char(winio_getc());
  638. }
  639. #else
  640. #if defined(__GO32__)
  641. char input;
  642. if (isatty(0))
  643. {
  644. int i = rl_getc();
  645. if (i != EOF)
  646. rl_stuff_char(i);
  647. }
  648. else
  649. if (kbhit() && ibuffer_space())
  650. rl_stuff_char(getkey());
  651. #else
  652. int tty = fileno (in_stream);
  653. register int tem, result = -1;
  654. long chars_avail;
  655. char input;
  656. #if defined (FIONREAD)
  657. result = ioctl (tty, FIONREAD, &chars_avail);
  658. #endif
  659. if (result == -1)
  660. {
  661. int flags;
  662. flags = fcntl (tty, F_GETFL, 0);
  663. fcntl (tty, F_SETFL, (flags | O_NDELAY));
  664. chars_avail = read (tty, &input, 1);
  665. fcntl (tty, F_SETFL, flags);
  666. if (chars_avail == -1 && errno == EAGAIN)
  667. return;
  668. }
  669. /* If there's nothing available, don't waste time trying to read
  670. something. */
  671. if (chars_avail == 0)
  672. return;
  673. tem = ibuffer_space ();
  674. if (chars_avail > tem)
  675. chars_avail = tem;
  676. /* One cannot read all of the available input. I can only read a single
  677. character at a time, or else programs which require input can be
  678. thwarted. If the buffer is larger than one character, I lose.
  679. Damn! */
  680. if (tem < ibuffer_len)
  681. chars_avail = 0;
  682. if (result != -1)
  683. {
  684. while (chars_avail--)
  685. rl_stuff_char (rl_getc (in_stream));
  686. }
  687. else
  688. {
  689. if (chars_avail)
  690. rl_stuff_char (input);
  691. }
  692. #endif /* def __GO32__/else */
  693. #endif
  694. }
  695. static int next_macro_key ();
  696. /* Read a key, including pending input. */
  697. int
  698. rl_read_key ()
  699. {
  700. int c;
  701. rl_key_sequence_length++;
  702. if (rl_pending_input)
  703. {
  704. c = rl_pending_input;
  705. rl_pending_input = 0;
  706. }
  707. else
  708. {
  709. /* If input is coming from a macro, then use that. */
  710. if (c = next_macro_key ())
  711. return (c);
  712. /* If the user has an event function, then call it periodically. */
  713. if (rl_event_hook)
  714. {
  715. while (rl_event_hook && !rl_get_char (&c))
  716. {
  717. (*rl_event_hook) ();
  718. if (!winio_rdln_ok()) {
  719. printf("%s", rl_prompt);
  720. printf("%s", the_line);
  721. }
  722. rl_gather_tyi ();
  723. }
  724. }
  725. else
  726. {
  727. if (!rl_get_char (&c))
  728. c = rl_getc (in_stream);
  729. }
  730. }
  731. return (c);
  732. }
  733. /* I'm beginning to hate the declaration rules for various compilers. */
  734. static void add_macro_char (), with_macro_input ();
  735. /* Do the command associated with KEY in MAP.
  736. If the associated command is really a keymap, then read
  737. another key, and dispatch into that map. */
  738. rl_dispatch (key, map)
  739. register int key;
  740. Keymap map;
  741. {
  742. if (defining_kbd_macro)
  743. add_macro_char (key);
  744. if (key > 127 && key < 256)
  745. {
  746. if (map[ESC].type == ISKMAP)
  747. {
  748. map = (Keymap)map[ESC].function;
  749. key -= 128;
  750. rl_dispatch (key, map);
  751. }
  752. else
  753. ding ();
  754. return;
  755. }
  756. switch (map[key].type)
  757. {
  758. case ISFUNC:
  759. {
  760. Function *func = map[key].function;
  761. if (func != (Function *)NULL)
  762. {
  763. /* Special case rl_do_lowercase_version (). */
  764. if (func == rl_do_lowercase_version)
  765. {
  766. rl_dispatch (to_lower (key), map);
  767. return;
  768. }
  769. (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  770. /* If we have input pending, then the last command was a prefix
  771. command. Don't change the state of rl_last_func. Otherwise,
  772. remember the last command executed in this variable. */
  773. if (!rl_pending_input)
  774. rl_last_func = map[key].function;
  775. }
  776. else
  777. {
  778. rl_abort ();
  779. return;
  780. }
  781. }
  782. break;
  783. case ISKMAP:
  784. if (map[key].function != (Function *)NULL)
  785. {
  786. int newkey;
  787. rl_key_sequence_length++;
  788. newkey = rl_read_key ();
  789. rl_dispatch (newkey, (Keymap)map[key].function);
  790. }
  791. else
  792. {
  793. rl_abort ();
  794. return;
  795. }
  796. break;
  797. case ISMACR:
  798. if (map[key].function != (Function *)NULL)
  799. {
  800. char *macro;
  801. macro = savestring ((char *)map[key].function);
  802. with_macro_input (macro);
  803. return;
  804. }
  805. break;
  806. }
  807. }
  808. /* **************************************************************** */
  809. /* */
  810. /* Hacking Keyboard Macros */
  811. /* */
  812. /* **************************************************************** */
  813. /* The currently executing macro string. If this is non-zero,
  814. then it is a malloc ()'ed string where input is coming from. */
  815. static char *executing_macro = (char *)NULL;
  816. /* The offset in the above string to the next character to be read. */
  817. static int executing_macro_index = 0;
  818. /* The current macro string being built. Characters get stuffed
  819. in here by add_macro_char (). */
  820. static char *current_macro = (char *)NULL;
  821. /* The size of the buffer allocated to current_macro. */
  822. static int current_macro_size = 0;
  823. /* The index at which characters are being added to current_macro. */
  824. static int current_macro_index = 0;
  825. /* A structure used to save nested macro strings.
  826. It is a linked list of string/index for each saved macro. */
  827. struct saved_macro {
  828. struct saved_macro *next;
  829. char *string;
  830. int index;
  831. };
  832. /* The list of saved macros. */
  833. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  834. /* Forward declarations of static functions. Thank you C. */
  835. static void push_executing_macro (), pop_executing_macro ();
  836. /* This one has to be declared earlier in the file. */
  837. /* static void add_macro_char (); */
  838. /* Set up to read subsequent input from STRING.
  839. STRING is free ()'ed when we are done with it. */
  840. static void
  841. with_macro_input (string)
  842. char *string;
  843. {
  844. push_executing_macro ();
  845. executing_macro = string;
  846. executing_macro_index = 0;
  847. }
  848. /* Return the next character available from a macro, or 0 if
  849. there are no macro characters. */
  850. static int
  851. next_macro_key ()
  852. {
  853. if (!executing_macro)
  854. return (0);
  855. if (!executing_macro[executing_macro_index])
  856. {
  857. pop_executing_macro ();
  858. return (next_macro_key ());
  859. }
  860. return (executing_macro[executing_macro_index++]);
  861. }
  862. /* Save the currently executing macro on a stack of saved macros. */
  863. static void
  864. push_executing_macro ()
  865. {
  866. struct saved_macro *saver;
  867. saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  868. saver->next = macro_list;
  869. saver->index = executing_macro_index;
  870. saver->string = executing_macro;
  871. macro_list = saver;
  872. }
  873. /* Discard the current macro, replacing it with the one
  874. on the top of the stack of saved macros. */
  875. static void
  876. pop_executing_macro ()
  877. {
  878. if (executing_macro)
  879. free (executing_macro);
  880. executing_macro = (char *)NULL;
  881. executing_macro_index = 0;
  882. if (macro_list)
  883. {
  884. struct saved_macro *disposer = macro_list;
  885. executing_macro = macro_list->string;
  886. executing_macro_index = macro_list->index;
  887. macro_list = macro_list->next;
  888. free (disposer);
  889. }
  890. }
  891. /* Add a character to the macro being built. */
  892. static void
  893. add_macro_char (c)
  894. int c;
  895. {
  896. if (current_macro_index + 1 >= current_macro_size)
  897. {
  898. if (!current_macro)
  899. current_macro = (char *)xmalloc (current_macro_size = 25);
  900. else
  901. current_macro =
  902. (char *)xrealloc (current_macro, current_macro_size += 25);
  903. }
  904. current_macro[current_macro_index++] = c;
  905. current_macro[current_macro_index] = '\0';
  906. }
  907. /* Begin defining a keyboard macro.
  908. Keystrokes are recorded as they are executed.
  909. End the definition with rl_end_kbd_macro ().
  910. If a numeric argument was explicitly typed, then append this
  911. definition to the end of the existing macro, and start by
  912. re-executing the existing macro. */
  913. rl_start_kbd_macro (ignore1, ignore2)
  914. int ignore1, ignore2;
  915. {
  916. if (defining_kbd_macro)
  917. rl_abort ();
  918. if (rl_explicit_arg)
  919. {
  920. if (current_macro)
  921. with_macro_input (savestring (current_macro));
  922. }
  923. else
  924. current_macro_index = 0;
  925. defining_kbd_macro = 1;
  926. }
  927. /* Stop defining a keyboard macro.
  928. A numeric argument says to execute the macro right now,
  929. that many times, counting the definition as the first time. */
  930. rl_end_kbd_macro (count, ignore)
  931. int count, ignore;
  932. {
  933. if (!defining_kbd_macro)
  934. rl_abort ();
  935. current_macro_index -= (rl_key_sequence_length - 1);
  936. current_macro[current_macro_index] = '\0';
  937. defining_kbd_macro = 0;
  938. rl_call_last_kbd_macro (--count, 0);
  939. }
  940. /* Execute the most recently defined keyboard macro.
  941. COUNT says how many times to execute it. */
  942. rl_call_last_kbd_macro (count, ignore)
  943. int count, ignore;
  944. {
  945. if (!current_macro)
  946. rl_abort ();
  947. while (count--)
  948. with_macro_input (savestring (current_macro));
  949. }
  950. /* **************************************************************** */
  951. /* */
  952. /* Initializations */
  953. /* */
  954. /* **************************************************************** */
  955. /* Initliaze readline (and terminal if not already). */
  956. rl_initialize ()
  957. {
  958. extern char *rl_display_prompt;
  959. /* If we have never been called before, initialize the
  960. terminal and data structures. */
  961. if (!rl_initialized)
  962. {
  963. readline_initialize_everything ();
  964. rl_initialized++;
  965. }
  966. /* Initalize the current line information. */
  967. rl_point = rl_end = 0;
  968. the_line = rl_line_buffer;
  969. the_line[0] = 0;
  970. /* We aren't done yet. We haven't even gotten started yet! */
  971. rl_done = 0;
  972. /* Tell the history routines what is going on. */
  973. start_using_history ();
  974. /* Make the display buffer match the state of the line. */
  975. {
  976. extern char *rl_display_prompt;
  977. extern int forced_display;
  978. rl_on_new_line ();
  979. rl_display_prompt = rl_prompt ? rl_prompt : "";
  980. forced_display = 1;
  981. }
  982. /* No such function typed yet. */
  983. rl_last_func = (Function *)NULL;
  984. /* Parsing of key-bindings begins in an enabled state. */
  985. parsing_conditionalized_out = 0;
  986. }
  987. /* Initialize the entire state of the world. */
  988. readline_initialize_everything ()
  989. {
  990. /* Find out if we are running in Emacs. */
  991. running_in_emacs = getenv ("EMACS");
  992. /* Set up input and output if they aren't already. */
  993. if (!rl_instream)
  994. rl_instream = stdin;
  995. if (!rl_outstream)
  996. rl_outstream = stdout;
  997. /* Allocate data structures. */
  998. if (!rl_line_buffer)
  999. rl_line_buffer =
  1000. (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1001. /* Initialize the terminal interface. */
  1002. init_terminal_io ((char *)NULL);
  1003. /* Bind tty characters to readline functions. */
  1004. readline_default_bindings ();
  1005. /* Initialize the function names. */
  1006. rl_initialize_funmap ();
  1007. /* Read in the init file. */
  1008. rl_read_init_file ((char *)NULL);
  1009. /* If the completion parser's default word break characters haven't
  1010. been set yet, then do so now. */
  1011. {
  1012. extern char *rl_completer_word_break_characters;
  1013. extern char *rl_basic_word_break_characters;
  1014. if (rl_completer_word_break_characters == (char *)NULL)
  1015. rl_completer_word_break_characters = rl_basic_word_break_characters;
  1016. }
  1017. }
  1018. /* If this system allows us to look at the values of the regular
  1019. input editing characters, then bind them to their readline
  1020. equivalents, iff the characters are not bound to keymaps. */
  1021. readline_default_bindings ()
  1022. {
  1023. #if !defined(__GO32__) && !defined(WIN32)
  1024. #if defined (NEW_TTY_DRIVER)
  1025. struct sgttyb ttybuff;
  1026. int tty = fileno (rl_instream);
  1027. if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1028. {
  1029. int erase, kill;
  1030. erase = ttybuff.sg_erase;
  1031. kill = ttybuff.sg_kill;
  1032. if (erase != -1 && keymap[erase].type == ISFUNC)
  1033. keymap[erase].function = rl_rubout;
  1034. if (kill != -1 && keymap[kill].type == ISFUNC)
  1035. keymap[kill].function = rl_unix_line_discard;
  1036. }
  1037. #if defined (TIOCGLTC)
  1038. {
  1039. struct ltchars lt;
  1040. if (ioctl (tty, TIOCGLTC, &lt) != -1)
  1041. {
  1042. int erase, nextc;
  1043. erase = lt.t_werasc;
  1044. nextc = lt.t_lnextc;
  1045. if (erase != -1 && keymap[erase].type == ISFUNC)
  1046. keymap[erase].function = rl_unix_word_rubout;
  1047. if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1048. keymap[nextc].function = rl_quoted_insert;
  1049. }
  1050. }
  1051. #endif /* TIOCGLTC */
  1052. #else /* not NEW_TTY_DRIVER */
  1053. #if defined (TERMIOS_TTY_DRIVER)
  1054. struct termios ttybuff;
  1055. #else
  1056. struct termio ttybuff;
  1057. #endif /* TERMIOS_TTY_DRIVER */
  1058. int tty = fileno (rl_instream);
  1059. #if defined (TERMIOS_TTY_DRIVER)
  1060. if (tcgetattr (tty, &ttybuff) != -1)
  1061. #else
  1062. if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1063. #endif /* !TERMIOS_TTY_DRIVER */
  1064. {
  1065. int erase, kill;
  1066. erase = ttybuff.c_cc[VERASE];
  1067. kill = ttybuff.c_cc[VKILL];
  1068. if (erase != _POSIX_VDISABLE &&
  1069. keymap[(unsigned char)erase].type == ISFUNC)
  1070. keymap[(unsigned char)erase].function = rl_rubout;
  1071. if (kill != _POSIX_VDISABLE &&
  1072. keymap[(unsigned char)kill].type == ISFUNC)
  1073. keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1074. #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  1075. {
  1076. int nextc;
  1077. nextc = ttybuff.c_cc[VLNEXT];
  1078. if (nextc != _POSIX_VDISABLE &&
  1079. keymap[(unsigned char)nextc].type == ISFUNC)
  1080. keymap[(unsigned char)nextc].function = rl_quoted_insert;
  1081. }
  1082. #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  1083. #if defined (VWERASE)
  1084. {
  1085. int werase;
  1086. werase = ttybuff.c_cc[VWERASE];
  1087. if (werase != _POSIX_VDISABLE &&
  1088. keymap[(unsigned char)werase].type == ISFUNC)
  1089. keymap[(unsigned char)werase].function = rl_unix_word_rubout;
  1090. }
  1091. #endif /* VWERASE */
  1092. }
  1093. #endif /* !NEW_TTY_DRIVER */
  1094. #endif /* def __GO32__ */
  1095. }
  1096. /* **************************************************************** */
  1097. /* */
  1098. /* Numeric Arguments */
  1099. /* */
  1100. /* **************************************************************** */
  1101. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1102. /* Add the current digit to the argument in progress. */
  1103. rl_digit_argument (ignore, key)
  1104. int ignore, key;
  1105. {
  1106. rl_pending_input = key;
  1107. rl_digit_loop ();
  1108. }
  1109. /* What to do when you abort reading an argument. */
  1110. rl_discard_argument ()
  1111. {
  1112. ding ();
  1113. rl_clear_message ();
  1114. rl_init_argument ();
  1115. }
  1116. /* Create a default argument. */
  1117. rl_init_argument ()
  1118. {
  1119. rl_numeric_arg = rl_arg_sign = 1;
  1120. rl_explicit_arg = 0;
  1121. }
  1122. /* C-u, universal argument. Multiply the current argument by 4.
  1123. Read a key. If the key has nothing to do with arguments, then
  1124. dispatch on it. If the key is the abort character then abort. */
  1125. rl_universal_argument ()
  1126. {
  1127. rl_numeric_arg *= 4;
  1128. rl_digit_loop ();
  1129. }
  1130. rl_digit_loop ()
  1131. {
  1132. int key, c;
  1133. while (1)
  1134. {
  1135. rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
  1136. key = c = rl_read_key ();
  1137. if (keymap[c].type == ISFUNC &&
  1138. keymap[c].function == rl_universal_argument)
  1139. {
  1140. rl_numeric_arg *= 4;
  1141. continue;
  1142. }
  1143. c = UNMETA (c);
  1144. if (numeric (c))
  1145. {
  1146. if (rl_explicit_arg)
  1147. rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1148. else
  1149. rl_numeric_arg = (c - '0');
  1150. rl_explicit_arg = 1;
  1151. }
  1152. else
  1153. {
  1154. if (c == '-' && !rl_explicit_arg)
  1155. {
  1156. rl_numeric_arg = 1;
  1157. rl_arg_sign = -1;
  1158. }
  1159. else
  1160. {
  1161. rl_clear_message ();
  1162. rl_dispatch (key, keymap);
  1163. return;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. /* **************************************************************** */
  1169. /* */
  1170. /* Display stuff */
  1171. /* */
  1172. /* **************************************************************** */
  1173. /* This is the stuff that is hard for me. I never seem to write good
  1174. display routines in C. Let's see how I do this time. */
  1175. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1176. the problems of input lines longer than the screen width.
  1177. update_line and the code that calls it makes a multiple line,
  1178. automatically wrapping line update. Carefull attention needs
  1179. to be paid to the vertical position variables.
  1180. handling of terminals with autowrap on (incl. DEC braindamage)
  1181. could be improved a bit. Right now I just cheat and decrement
  1182. screenwidth by one. */
  1183. /* Keep two buffers; one which reflects the current contents of the
  1184. screen, and the other to draw what we think the new contents should
  1185. be. Then compare the buffers, and make whatever changes to the
  1186. screen itself that we should. Finally, make the buffer that we
  1187. just drew into be the one which reflects the current contents of the
  1188. screen, and place the cursor where it belongs.
  1189. Commands that want to can fix the display themselves, and then let
  1190. this function know that the display has been fixed by setting the
  1191. RL_DISPLAY_FIXED variable. This is good for efficiency. */
  1192. /* Termcap variables: */
  1193. extern char *term_up, *term_dc, *term_cr;
  1194. extern int screenheight, screenwidth, terminal_can_insert;
  1195. /* What YOU turn on when you have handled all redisplay yourself. */
  1196. int rl_display_fixed = 0;
  1197. /* The visible cursor position. If you print some text, adjust this. */
  1198. int last_c_pos = 0;
  1199. int last_v_pos = 0;
  1200. /* The last left edge of text that was displayed. This is used when
  1201. doing horizontal scrolling. It shifts in thirds of a screenwidth. */
  1202. static int last_lmargin = 0;
  1203. /* The line display buffers. One is the line currently displayed on
  1204. the screen. The other is the line about to be displayed. */
  1205. static char *visible_line = (char *)NULL;
  1206. static char *invisible_line = (char *)NULL;
  1207. /* Number of lines currently on screen minus 1. */
  1208. int vis_botlin = 0;
  1209. /* A buffer for `modeline' messages. */
  1210. char msg_buf[128];
  1211. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1212. int forced_display = 0;
  1213. /* The stuff that gets printed out before the actual text of the line.
  1214. This is usually pointing to rl_prompt. */
  1215. char *rl_display_prompt = (char *)NULL;
  1216. /* Default and initial buffer size. Can grow. */
  1217. static int line_size = 1024;
  1218. /* Non-zero means to always use horizontal scrolling in line display. */
  1219. static int horizontal_scroll_mode = 0;
  1220. /* Non-zero means to display an asterisk at the starts of history lines
  1221. which have been modified. */
  1222. static int mark_modified_lines = 0;
  1223. /* Non-zero means to use a visible bell if one is available rather than
  1224. simply ringing the terminal bell. */
  1225. static int prefer_visible_bell = 0;
  1226. /* I really disagree with this, but my boss (among others) insists that we
  1227. support compilers that don't work. I don't think we are gaining by doing
  1228. so; what is the advantage in producing better code if we can't use it? */
  1229. /* The following two declarations belong inside the
  1230. function block, not here. */
  1231. static void move_cursor_relative ();
  1232. static void output_some_chars ();
  1233. static void output_character_function ();
  1234. static int compare_strings ();
  1235. /* Basic redisplay algorithm. */
  1236. rl_redisplay ()
  1237. {
  1238. register int in, out, c, linenum;
  1239. register char *line = invisible_line;
  1240. char *prompt_this_line;
  1241. int c_pos = 0;
  1242. int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
  1243. extern int readline_echoing_p;
  1244. if (!readline_echoing_p)
  1245. return;
  1246. if (!rl_display_prompt)
  1247. rl_display_prompt = "";
  1248. if (!invisible_line)
  1249. {
  1250. visible_line = (char *)xmalloc (line_size);
  1251. invisible_line = (char *)xmalloc (line_size);
  1252. line = invisible_line;
  1253. for (in = 0; in < line_size; in++)
  1254. {
  1255. visible_line[in] = 0;
  1256. invisible_line[in] = 1;
  1257. }
  1258. rl_on_new_line ();
  1259. }
  1260. /* Draw the line into the buffer. */
  1261. c_pos = -1;
  1262. /* Mark the line as modified or not. We only do this for history
  1263. lines. */
  1264. out = 0;
  1265. if (mark_modified_lines && current_history () && rl_undo_list)
  1266. {
  1267. line[out++] = '*';
  1268. line[out] = '\0';
  1269. }
  1270. /* If someone thought that the redisplay was handled, but the currently
  1271. visible line has a different modification state than the one about
  1272. to become visible, then correct the callers misconception. */
  1273. if (visible_line[0] != invisible_line[0])
  1274. rl_display_fixed = 0;
  1275. prompt_this_line = rindex (rl_display_prompt, '\n');
  1276. if (!prompt_this_line)
  1277. prompt_this_line = rl_display_prompt;
  1278. else
  1279. {
  1280. prompt_this_line++;
  1281. if (forced_display)
  1282. output_some_chars (rl_display_prompt,
  1283. prompt_this_line - rl_display_prompt);
  1284. }
  1285. strncpy (line + out, prompt_this_line, strlen (prompt_this_line));
  1286. out += strlen (prompt_this_line);
  1287. line[out] = '\0';
  1288. for (in = 0; in < rl_end; in++)
  1289. {
  1290. c = (unsigned char)the_line[in];
  1291. if (out + 1 >= line_size)
  1292. {
  1293. line_size *= 2;
  1294. visible_line = (char *)xrealloc (visible_line, line_size);
  1295. invisible_line = (char *)xrealloc (invisible_line, line_size);
  1296. line = invisible_line;
  1297. }
  1298. if (in == rl_point)
  1299. c_pos = out;
  1300. if (c > 127)
  1301. {
  1302. line[out++] = 'M';
  1303. line[out++] = '-';
  1304. line[out++] = c - 128;
  1305. }
  1306. #define DISPLAY_TABS
  1307. #if defined (DISPLAY_TABS)
  1308. else if (c == '\t')
  1309. {
  1310. register int newout = (out | (int)7) + 1;
  1311. while (out < newout)
  1312. line[out++] = ' ';
  1313. }
  1314. #endif
  1315. else if (c < 32)
  1316. {
  1317. line[out++] = 'C';
  1318. line[out++] = '-';
  1319. line[out++] = c + 64;
  1320. }
  1321. else if (c == 127)
  1322. {
  1323. line[out++] = 'C';
  1324. line[out++] = '-';
  1325. line[out++] = '?';
  1326. }
  1327. else
  1328. line[out++] = c;
  1329. }
  1330. line[out] = '\0';
  1331. if (c_pos < 0)
  1332. c_pos = out;
  1333. /* PWP: now is when things get a bit hairy. The visible and invisible
  1334. line buffers are really multiple lines, which would wrap every
  1335. (screenwidth - 1) characters. Go through each in turn, finding
  1336. the changed region and updating it. The line order is top to bottom. */
  1337. /* If we can move the cursor up and down, then use multiple lines,
  1338. otherwise, let long lines display in a single terminal line, and
  1339. horizontally scroll it. */
  1340. if (!horizontal_scroll_mode && term_up && *term_up)
  1341. {
  1342. int total_screen_chars = (screenwidth * screenheight);
  1343. if (!rl_display_fixed || forced_display)
  1344. {
  1345. forced_display = 0;
  1346. /* If we have more than a screenful of material to display, then
  1347. only display a screenful. We should display the last screen,
  1348. not the first. I'll fix this in a minute. */
  1349. if (out >= total_screen_chars)
  1350. out = total_screen_chars - 1;
  1351. /* Number of screen lines to display. */
  1352. inv_botlin = out / screenwidth;
  1353. /* For each line in the buffer, do the updating display. */
  1354. for (linenum = 0; linenum <= inv_botlin; linenum++)
  1355. update_line (linenum > vis_botlin ? ""
  1356. : &visible_line[linenum * screenwidth],
  1357. &invisible_line[linenum * screenwidth],
  1358. linenum);
  1359. /* We may have deleted some lines. If so, clear the left over
  1360. blank ones at the bottom out. */
  1361. if (vis_botlin > inv_botlin)
  1362. {
  1363. char *tt;
  1364. for (; linenum <= vis_botlin; linenum++)
  1365. {
  1366. tt = &visible_line[linenum * screenwidth];
  1367. move_vert (linenum);
  1368. move_cursor_relative (0, tt);
  1369. clear_to_eol ((linenum == vis_botlin)?
  1370. strlen (tt) : screenwidth);
  1371. }
  1372. }
  1373. vis_botlin = inv_botlin;
  1374. /* Move the cursor where it should be. */
  1375. move_vert (c_pos / screenwidth);
  1376. move_cursor_relative (c_pos % screenwidth,
  1377. &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1378. }
  1379. }
  1380. else /* Do horizontal scrolling. */
  1381. {
  1382. int lmargin;
  1383. /* Always at top line. */
  1384. last_v_pos = 0;
  1385. /* If the display position of the cursor would be off the edge
  1386. of the screen, start the display of this line at an offset that
  1387. leaves the cursor on the screen. */
  1388. if (c_pos - last_lmargin > screenwidth - 2)
  1389. lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1390. else if (c_pos - last_lmargin < 1)
  1391. lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1392. else
  1393. lmargin = last_lmargin;
  1394. /* If the first character on the screen isn't the first character
  1395. in the display line, indicate this with a special character. */
  1396. if (lmargin > 0)
  1397. line[lmargin] = '<';
  1398. if (lmargin + screenwidth < out)
  1399. line[lmargin + screenwidth - 1] = '>';
  1400. if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1401. {
  1402. forced_display = 0;
  1403. update_line (&visible_line[last_lmargin],
  1404. &invisible_line[lmargin], 0);
  1405. move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1406. last_lmargin = lmargin;
  1407. }
  1408. }
  1409. fflush (out_stream);
  1410. /* Swap visible and non-visible lines. */
  1411. {
  1412. char *temp = visible_line;
  1413. visible_line = invisible_line;
  1414. invisible_line = temp;
  1415. rl_display_fixed = 0;
  1416. }
  1417. }
  1418. /* PWP: update_line() is based on finding the middle difference of each
  1419. line on the screen; vis:
  1420. /old first difference
  1421. /beginning of line | /old last same /old EOL
  1422. v v v v
  1423. old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1424. new: eddie> Oh, my little buggy says to me, as lurgid as
  1425. ^ ^ ^ ^
  1426. \beginning of line | \new last same \new end of line
  1427. \new first difference
  1428. All are character pointers for the sake of speed. Special cases for
  1429. no differences, as well as for end of line additions must be handeled.
  1430. Could be made even smarter, but this works well enough */
  1431. static
  1432. update_line (old, new, current_line)
  1433. register char *old, *new;
  1434. int current_line;
  1435. {
  1436. register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1437. int lendiff, wsatend;
  1438. /* Find first difference. */
  1439. for (ofd = old, nfd = new;
  1440. (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1441. ofd++, nfd++)
  1442. ;
  1443. /* Move to the end of the screen line. */
  1444. for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1445. for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1446. /* If no difference, continue to next line. */
  1447. if (ofd == oe && nfd == ne)
  1448. return;
  1449. wsatend = 1; /* flag for trailing whitespace */
  1450. ols = oe - 1; /* find last same */
  1451. nls = ne - 1;
  1452. while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
  1453. {
  1454. if (*ols != ' ')
  1455. wsatend = 0;
  1456. ols--;
  1457. nls--;
  1458. }
  1459. if (wsatend)
  1460. {
  1461. ols = oe;
  1462. nls = ne;
  1463. }
  1464. else if (*ols != *nls)
  1465. {
  1466. if (*ols) /* don't step past the NUL */
  1467. ols++;
  1468. if (*nls)
  1469. nls++;
  1470. }
  1471. move_vert (current_line);
  1472. move_cursor_relative (ofd - old, old);
  1473. /* if (len (new) > len (old)) */
  1474. lendiff = (nls - nfd) - (ols - ofd);
  1475. /* Insert (diff(len(old),len(new)) ch */
  1476. if (lendiff > 0)
  1477. {
  1478. if (terminal_can_insert)
  1479. {
  1480. extern char *term_IC;
  1481. /* Sometimes it is cheaper to print the characters rather than
  1482. use the terminal's capabilities. */
  1483. if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1484. {
  1485. output_some_chars (nfd, (ne - nfd));
  1486. last_c_pos += (ne - nfd);
  1487. }
  1488. else
  1489. {
  1490. if (*ols)
  1491. {
  1492. insert_some_chars (nfd, lendiff);
  1493. last_c_pos += lendiff;
  1494. }
  1495. else
  1496. {
  1497. /* At the end of a line the characters do not have to
  1498. be "inserted". They can just be placed on the screen. */
  1499. output_some_chars (nfd, lendiff);
  1500. last_c_pos += lendiff;
  1501. }
  1502. /* Copy (new) chars to screen from first diff to last match. */
  1503. if (((nls - nfd) - lendiff) > 0)
  1504. {
  1505. output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1506. last_c_pos += ((nls - nfd) - lendiff);
  1507. }
  1508. }
  1509. }
  1510. else
  1511. { /* cannot insert chars, write to EOL */
  1512. output_some_chars (nfd, (ne - nfd));
  1513. last_c_pos += (ne - nfd);
  1514. }
  1515. }
  1516. else /* Delete characters from line. */
  1517. {
  1518. /* If possible and inexpensive to use terminal deletion, then do so. */
  1519. if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1520. {
  1521. if (lendiff)
  1522. delete_chars (-lendiff); /* delete (diff) characters */
  1523. /* Copy (new) chars to screen from first diff to last match */
  1524. if ((nls - nfd) > 0)
  1525. {
  1526. output_some_chars (nfd, (nls - nfd));
  1527. last_c_pos += (nls - nfd);
  1528. }
  1529. }
  1530. /* Otherwise, print over the existing material. */
  1531. else
  1532. {
  1533. output_some_chars (nfd, (ne - nfd));
  1534. last_c_pos += (ne - nfd);
  1535. clear_to_eol ((oe - old) - (ne - new));
  1536. }
  1537. }
  1538. }
  1539. /* (PWP) tell the update routines that we have moved onto a
  1540. new (empty) line. */
  1541. rl_on_new_line ()
  1542. {
  1543. if (visible_line)
  1544. visible_line[0] = '\0';
  1545. last_c_pos = last_v_pos = 0;
  1546. vis_botlin = last_lmargin = 0;
  1547. }
  1548. /* Actually update the display, period. */
  1549. rl_forced_update_display ()
  1550. {
  1551. if (visible_line)
  1552. {
  1553. register char *temp = visible_line;
  1554. while (*temp) *temp++ = '\0';
  1555. }
  1556. rl_on_new_line ();
  1557. forced_display++;
  1558. rl_redisplay ();
  1559. }
  1560. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1561. DATA is the contents of the screen line of interest; i.e., where
  1562. the movement is being done. */
  1563. static void
  1564. move_cursor_relative (new, data)
  1565. int new;
  1566. char *data;
  1567. {
  1568. #ifdef WIN32
  1569. winio_rel_move(new);
  1570. last_c_pos = new;
  1571. return;
  1572. #else
  1573. register int i;
  1574. #if GRX
  1575. if (egagrph) {
  1576. grx_rel_move(new);
  1577. last_c_pos = new;
  1578. return;
  1579. }
  1580. #endif
  1581. /* It may be faster to output a CR, and then move forwards instead
  1582. of moving backwards. */
  1583. if (new + 1 < last_c_pos - new)
  1584. {
  1585. #ifdef __MSDOS__
  1586. putc('\r', out_stream);
  1587. #else
  1588. tputs (term_cr, 1, output_character_function);
  1589. #endif
  1590. last_c_pos = 0;
  1591. }
  1592. if (last_c_pos == new) return;
  1593. if (last_c_pos < new)
  1594. {
  1595. /* Move the cursor forward. We do it by printing the command
  1596. to move the cursor forward if there is one, else print that
  1597. portion of the output buffer again. Which is cheaper? */
  1598. /* The above comment is left here for posterity. It is faster
  1599. to print one character (non-control) than to print a control
  1600. sequence telling the terminal to move forward one character.
  1601. That kind of control is for people who don't know what the
  1602. data is underneath the cursor. */
  1603. #if defined (HACK_TERMCAP_MOTION)
  1604. extern char *term_forward_char;
  1605. if (term_forward_char)
  1606. for (i = last_c_pos; i < new; i++)
  1607. tputs (term_forward_char, 1, output_character_function);
  1608. else
  1609. for (i = last_c_pos; i < new; i++)
  1610. putc (data[i], out_stream);
  1611. #else
  1612. for (i = last_c_pos; i < new; i++)
  1613. putc (data[i], out_stream);
  1614. #endif /* HACK_TERMCAP_MOTION */
  1615. }
  1616. else
  1617. backspace (last_c_pos - new);
  1618. last_c_pos = new;
  1619. #endif
  1620. }
  1621. /* PWP: move the cursor up or down. */
  1622. move_vert (to)
  1623. int to;
  1624. {
  1625. #ifdef WIN32
  1626. #else
  1627. void output_character_function ();
  1628. register int delta, i;
  1629. if (last_v_pos == to) return;
  1630. if (to > screenheight)
  1631. return;
  1632. #if defined(__GO32__)
  1633. {
  1634. int cur_r, cur_c;
  1635. ScreenGetCursor(&cur_r, &cur_c);
  1636. ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
  1637. }
  1638. #else /* __GO32__ */
  1639. if ((delta = to - last_v_pos) > 0)
  1640. {
  1641. for (i = 0; i < delta; i++)
  1642. putc ('\n', out_stream);
  1643. tputs (term_cr, 1, output_character_function);
  1644. last_c_pos = 0;
  1645. }
  1646. else
  1647. { /* delta < 0 */
  1648. if (term_up && *term_up)
  1649. for (i = 0; i < -delta; i++)
  1650. tputs (term_up, 1, output_character_function);
  1651. }
  1652. #endif /* __GO32__ */
  1653. #endif
  1654. last_v_pos = to; /* now to is here */
  1655. }
  1656. /* Physically print C on out_stream. This is for functions which know
  1657. how to optimize the display. */
  1658. rl_show_char (c)
  1659. int c;
  1660. {
  1661. if (c > 127)
  1662. {
  1663. fprintf (out_stream, "M-");
  1664. c -= 128;
  1665. }
  1666. #if defined (DISPLAY_TABS)
  1667. if (c < 32 && c != '\t')
  1668. #else
  1669. if (c < 32)
  1670. #endif
  1671. {
  1672. c += 64;
  1673. }
  1674. putc (c, out_stream);
  1675. fflush (out_stream);
  1676. }
  1677. #if defined (DISPLAY_TABS)
  1678. int
  1679. rl_character_len (c, pos)
  1680. register int c, pos;
  1681. {
  1682. if (c < ' ' || c > 126)
  1683. {
  1684. if (c == '\t')
  1685. return (((pos | (int)7) + 1) - pos);
  1686. else
  1687. return (3);
  1688. }
  1689. else
  1690. return (1);
  1691. }
  1692. #else
  1693. int
  1694. rl_character_len (c)
  1695. int c;
  1696. {
  1697. if (c < ' ' || c > 126)
  1698. return (3);
  1699. else
  1700. return (1);
  1701. }
  1702. #endif /* DISPLAY_TAB */
  1703. /* How to print things in the "echo-area". The prompt is treated as a
  1704. mini-modeline. */
  1705. rl_message (string, arg1, arg2)
  1706. char *string;
  1707. {
  1708. sprintf (msg_buf, string, arg1, arg2);
  1709. rl_display_prompt = msg_buf;
  1710. rl_redisplay ();
  1711. }
  1712. /* How to clear things from the "echo-area". */
  1713. rl_clear_message ()
  1714. {
  1715. rl_display_prompt = rl_prompt;
  1716. rl_redisplay ();
  1717. }
  1718. /* **************************************************************** */
  1719. /* */
  1720. /* Terminal and Termcap */
  1721. /* */
  1722. /* **************************************************************** */
  1723. static char *term_buffer = (char *)NULL;
  1724. static char *term_string_buffer = (char *)NULL;
  1725. /* Non-zero means this terminal can't really do anything. */
  1726. int dumb_term = 0;
  1727. /* On Solaris2, sys/types.h brings in sys/reg.h,
  1728. which screws up the Termcap variable PC, used below. */
  1729. #undef PC
  1730. char PC;
  1731. char *BC, *UP;
  1732. /* Some strings to control terminal actions. These are output by tputs (). */
  1733. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1734. int screenwidth, screenheight;
  1735. /* Non-zero if we determine that the terminal can do character insertion. */
  1736. int terminal_can_insert = 0;
  1737. /* How to insert characters. */
  1738. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  1739. /* How to delete characters. */
  1740. char *term_dc, *term_DC;
  1741. #if defined (HACK_TERMCAP_MOTION)
  1742. char *term_forward_char;
  1743. #endif /* HACK_TERMCAP_MOTION */
  1744. /* How to go up a line. */
  1745. char *term_up;
  1746. /* A visible bell, if the terminal can be made to flash the screen. */
  1747. char *visible_bell;
  1748. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  1749. has changed. */
  1750. rl_reset_terminal (terminal_name)
  1751. char *terminal_name;
  1752. {
  1753. init_terminal_io (terminal_name);
  1754. }
  1755. init_terminal_io (terminal_name)
  1756. char *terminal_name;
  1757. {
  1758. #ifdef WIN32
  1759. winio_size(&screenheight, &screenwidth);
  1760. #else
  1761. #if defined(__GO32__)
  1762. screenwidth = ScreenCols();
  1763. screenheight = ScreenRows();
  1764. term_cr = "\r";
  1765. term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1766. term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1767. #if defined (HACK_TERMCAP_MOTION)
  1768. term_forward_char = (char *)NULL;
  1769. #endif
  1770. terminal_can_insert = 0;
  1771. return;
  1772. #else
  1773. extern char *tgetstr ();
  1774. char *term, *buffer;
  1775. #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
  1776. struct winsize window_size;
  1777. #endif
  1778. int tty;
  1779. term = terminal_name ? terminal_name : getenv ("TERM");
  1780. if (!term_string_buffer)
  1781. term_string_buffer = (char *)xmalloc (2048);
  1782. if (!term_buffer)
  1783. term_buffer = (char *)xmalloc (2048);
  1784. buffer = term_string_buffer;
  1785. term_clrpag = term_cr = term_clreol = (char *)NULL;
  1786. if (!term)
  1787. term = "dumb";
  1788. if (tgetent (term_buffer, term) <= 0)
  1789. {
  1790. dumb_term = 1;
  1791. screenwidth = 79;
  1792. screenheight = 24;
  1793. term_cr = "\r";
  1794. term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1795. term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1796. #if defined (HACK_TERMCAP_MOTION)
  1797. term_forward_char = (char *)NULL;
  1798. #endif
  1799. terminal_can_insert = 0;
  1800. return;
  1801. }
  1802. BC = tgetstr ("pc", &buffer);
  1803. PC = buffer ? *buffer : 0;
  1804. term_backspace = tgetstr ("le", &buffer);
  1805. term_cr = tgetstr ("cr", &buffer);
  1806. term_clreol = tgetstr ("ce", &buffer);
  1807. term_clrpag = tgetstr ("cl", &buffer);
  1808. if (!term_cr)
  1809. term_cr = "\r";
  1810. #if defined (HACK_TERMCAP_MOTION)
  1811. term_forward_char = tgetstr ("nd", &buffer);
  1812. #endif /* HACK_TERMCAP_MOTION */
  1813. if (rl_instream)
  1814. tty = fileno (rl_instream);
  1815. else
  1816. tty = 0;
  1817. screenwidth = screenheight = 0;
  1818. #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
  1819. if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  1820. {
  1821. screenwidth = (int) window_size.ws_col;
  1822. screenheight = (int) window_size.ws_row;
  1823. }
  1824. #endif
  1825. if (screenwidth <= 0 || screenheight <= 0)
  1826. {
  1827. screenwidth = tgetnum ("co");
  1828. screenheight = tgetnum ("li");
  1829. }
  1830. screenwidth--;
  1831. if (screenwidth <= 0)
  1832. screenwidth = 79;
  1833. if (screenheight <= 0)
  1834. screenheight = 24;
  1835. term_im = tgetstr ("im", &buffer);
  1836. term_ei = tgetstr ("ei", &buffer);
  1837. term_IC = tgetstr ("IC", &buffer);
  1838. term_ic = tgetstr ("ic", &buffer);
  1839. /* "An application program can assume that the terminal can do
  1840. character insertion if *any one of* the capabilities `IC',
  1841. `im', `ic' or `ip' is provided." But we can't do anything if
  1842. only `ip' is provided, so... */
  1843. terminal_can_insert = (term_IC || term_im || term_ic);
  1844. term_up = tgetstr ("up", &buffer);
  1845. term_dc = tgetstr ("dc", &buffer);
  1846. term_DC = tgetstr ("DC", &buffer);
  1847. visible_bell = tgetstr ("vb", &buffer);
  1848. #endif /* !__GO32__ */
  1849. #endif
  1850. }
  1851. /* A function for the use of tputs () */
  1852. static void
  1853. output_character_function (c)
  1854. int c;
  1855. {
  1856. putc (c, out_stream);
  1857. }
  1858. /* Write COUNT characters from STRING to the output stream. */
  1859. static void
  1860. output_some_chars (string, count)
  1861. char *string;
  1862. int count;
  1863. {
  1864. #ifdef WIN32
  1865. winio_output_some_chars(string, count);
  1866. #else
  1867. #if GRX
  1868. if (egagrph) {
  1869. grx_output_some_chars(string, count);
  1870. }else
  1871. #endif
  1872. {
  1873. fwrite (string, 1, count, out_stream);
  1874. }
  1875. #endif
  1876. }
  1877. /* Delete COUNT characters from the display line. */
  1878. static
  1879. delete_chars (count)
  1880. int count;
  1881. {
  1882. #ifdef WIN32
  1883. winio_delete_chars(count);
  1884. #else
  1885. #if defined(__GO32__)
  1886. int r, c, w;
  1887. #if GRX
  1888. if (egagrph) {
  1889. grx_delete_chars(count);
  1890. }else
  1891. #endif
  1892. {
  1893. ScreenGetCursor(&r, &c);
  1894. w = ScreenCols();
  1895. memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
  1896. memset(ScreenPrimary+r*w+w-count, 0, count*2);
  1897. }
  1898. #else /* __GO32__ */
  1899. if (count > screenwidth)
  1900. return;
  1901. if (term_DC && *term_DC)
  1902. {
  1903. char *tgoto (), *buffer;
  1904. buffer = tgoto (term_DC, 0, count);
  1905. tputs (buffer, 1, output_character_function);
  1906. }
  1907. else
  1908. {
  1909. if (term_dc && *term_dc)
  1910. while (count--)
  1911. tputs (term_dc, 1, output_character_function);
  1912. }
  1913. #endif /* __GO32__ */
  1914. #endif
  1915. }
  1916. /* Insert COUNT characters from STRING to the output stream. */
  1917. static void
  1918. insert_some_chars (string, count)
  1919. char *string;
  1920. int count;
  1921. {
  1922. #ifdef WIN32
  1923. winio_insert_some_chars(string, count);
  1924. #else
  1925. #if defined(__GO32__)
  1926. int r, c, w;
  1927. #if GRX
  1928. if (egagrph) {
  1929. grx_insert_some_chars(string, count);
  1930. }else
  1931. #endif
  1932. {
  1933. ScreenGetCursor(&r, &c);
  1934. w = ScreenCols();
  1935. memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
  1936. /* Print the text. */
  1937. output_some_chars (string, count);
  1938. }
  1939. #else /* __GO32__ */
  1940. /* If IC is defined, then we do not have to "enter" insert mode. */
  1941. if (term_IC)
  1942. {
  1943. char *tgoto (), *buffer;
  1944. buffer = tgoto (term_IC, 0, count);
  1945. tputs (buffer, 1, output_character_function);
  1946. output_some_chars (string, count);
  1947. }
  1948. else
  1949. {
  1950. register int i;
  1951. /* If we have to turn on insert-mode, then do so. */
  1952. if (term_im && *term_im)
  1953. tputs (term_im, 1, output_character_function);
  1954. /* If there is a special command for inserting characters, then
  1955. use that first to open up the space. */
  1956. if (term_ic && *term_ic)
  1957. {
  1958. for (i = count; i--; )
  1959. tputs (term_ic, 1, output_character_function);
  1960. }
  1961. /* Print the text. */
  1962. output_some_chars (string, count);
  1963. /* If there is a string to turn off insert mode, we had best use
  1964. it now. */
  1965. if (term_ei && *term_ei)
  1966. tputs (term_ei, 1, output_character_function);
  1967. }
  1968. #endif /* __GO32__ */
  1969. #endif
  1970. }
  1971. /* Move the cursor back. */
  1972. backspace (count)
  1973. int count;
  1974. {
  1975. #ifdef WIN32
  1976. winio_backspace(count);
  1977. #else
  1978. register int i;
  1979. #ifndef __GO32__
  1980. if (term_backspace)
  1981. for (i = 0; i < count; i++)
  1982. tputs (term_backspace, 1, output_character_function);
  1983. else
  1984. #endif /* !__GO32__ */
  1985. #if GRX
  1986. if (egagrph) {
  1987. grx_backspace(count);
  1988. }else
  1989. #endif
  1990. {
  1991. for (i = 0; i < count; i++)
  1992. putc ('\b', out_stream);
  1993. }
  1994. #endif
  1995. }
  1996. /* Move to the start of the next line. */
  1997. crlf ()
  1998. {
  1999. #ifdef WIN32
  2000. printf("\n");
  2001. #else
  2002. #if defined (NEW_TTY_DRIVER)
  2003. tputs (term_cr, 1, output_character_function);
  2004. #endif /* NEW_TTY_DRIVER */
  2005. #if GRX
  2006. if (egagrph) {
  2007. grx_output_some_chars("\n", 1);
  2008. }else
  2009. #endif
  2010. {
  2011. putc ('\n', out_stream);
  2012. }
  2013. #endif
  2014. }
  2015. /* Clear to the end of the line. COUNT is the minimum
  2016. number of character spaces to clear, */
  2017. static void
  2018. clear_to_eol (count)
  2019. int count;
  2020. {
  2021. #ifdef WIN32
  2022. winio_clear_to_eol();
  2023. #else
  2024. #ifndef __GO32__
  2025. if (term_clreol)
  2026. {
  2027. tputs (term_clreol, 1, output_character_function);
  2028. }
  2029. else
  2030. #endif /* !__GO32__ */
  2031. {
  2032. register int i;
  2033. /* Do one more character space. */
  2034. count++;
  2035. #if GRX
  2036. if (egagrph) {
  2037. grx_clear_to_eol();
  2038. }else
  2039. #endif
  2040. {
  2041. for (i = 0; i < count; i++)
  2042. putc (' ', out_stream);
  2043. backspace (count);
  2044. }
  2045. }
  2046. #endif
  2047. }
  2048. /* **************************************************************** */
  2049. /* */
  2050. /* Saving and Restoring the TTY */
  2051. /* */
  2052. /* **************************************************************** */
  2053. /* Non-zero means that the terminal is in a prepped state. */
  2054. static int terminal_prepped = 0;
  2055. #if defined (NEW_TTY_DRIVER)
  2056. /* Standard flags, including ECHO. */
  2057. static int original_tty_flags = 0;
  2058. /* Local mode flags, like LPASS8. */
  2059. static int local_mode_flags = 0;
  2060. /* Terminal characters. This has C-s and C-q in it. */
  2061. static struct tchars original_tchars;
  2062. /* Local special characters. This has the interrupt characters in it. */
  2063. #if defined (TIOCGLTC)
  2064. static struct ltchars original_ltchars;
  2065. #endif
  2066. /* We use this to get and set the tty_flags. */
  2067. static struct sgttyb the_ttybuff;
  2068. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  2069. static void
  2070. rl_prep_terminal ()
  2071. {
  2072. #if !defined(__GO32__) && !defined(WIN32)
  2073. int tty = fileno (rl_instream);
  2074. SIGNALS_DECLARE_SAVED (saved_signals);
  2075. if (terminal_prepped)
  2076. return;
  2077. SIGNALS_BLOCK (SIGINT, saved_signals);
  2078. /* We always get the latest tty values. Maybe stty changed them. */
  2079. ioctl (tty, TIOCGETP, &the_ttybuff);
  2080. original_tty_flags = the_ttybuff.sg_flags;
  2081. readline_echoing_p = (original_tty_flags & ECHO);
  2082. #if defined (TIOCLGET)
  2083. ioctl (tty, TIOCLGET, &local_mode_flags);
  2084. #endif
  2085. #if !defined (ANYP)
  2086. # define ANYP (EVENP | ODDP)
  2087. #endif
  2088. /* If this terminal doesn't care how the 8th bit is used,
  2089. then we can use it for the meta-key. We check by seeing
  2090. if BOTH odd and even parity are allowed. */
  2091. if (the_ttybuff.sg_flags & ANYP)
  2092. {
  2093. #if defined (PASS8)
  2094. the_ttybuff.sg_flags |= PASS8;
  2095. #endif
  2096. /* Hack on local mode flags if we can. */
  2097. #if defined (TIOCLGET) && defined (LPASS8)
  2098. {
  2099. int flags;
  2100. flags = local_mode_flags | LPASS8;
  2101. ioctl (tty, TIOCLSET, &flags);
  2102. }
  2103. #endif /* TIOCLGET && LPASS8 */
  2104. }
  2105. #if defined (TIOCGETC)
  2106. {
  2107. struct tchars temp;
  2108. ioctl (tty, TIOCGETC, &original_tchars);
  2109. temp = original_tchars;
  2110. #if defined (USE_XON_XOFF)
  2111. /* Get rid of C-s and C-q.
  2112. We remember the value of startc (C-q) so that if the terminal is in
  2113. xoff state, the user can xon it by pressing that character. */
  2114. xon_char = temp.t_startc;
  2115. temp.t_stopc = -1;
  2116. temp.t_startc = -1;
  2117. /* If there is an XON character, bind it to restart the output. */
  2118. if (xon_char != -1)
  2119. rl_bind_key (xon_char, rl_restart_output);
  2120. #endif /* USE_XON_XOFF */
  2121. /* If there is an EOF char, bind eof_char to it. */
  2122. if (temp.t_eofc != -1)
  2123. eof_char = temp.t_eofc;
  2124. #if defined (NO_KILL_INTR)
  2125. /* Get rid of C-\ and C-c. */
  2126. temp.t_intrc = temp.t_quitc = -1;
  2127. #endif /* NO_KILL_INTR */
  2128. ioctl (tty, TIOCSETC, &temp);
  2129. }
  2130. #endif /* TIOCGETC */
  2131. #if defined (TIOCGLTC)
  2132. {
  2133. struct ltchars temp;
  2134. ioctl (tty, TIOCGLTC, &original_ltchars);
  2135. temp = original_ltchars;
  2136. /* Make the interrupt keys go away. Just enough to make people
  2137. happy. */
  2138. temp.t_dsuspc = -1; /* C-y */
  2139. temp.t_lnextc = -1; /* C-v */
  2140. ioctl (tty, TIOCSLTC, &temp);
  2141. }
  2142. #endif /* TIOCGLTC */
  2143. the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
  2144. the_ttybuff.sg_flags |= CBREAK;
  2145. ioctl (tty, TIOCSETN, &the_ttybuff);
  2146. terminal_prepped = 1;
  2147. SIGNALS_RESTORE (saved_signals);
  2148. #endif /* !__GO32__ */
  2149. }
  2150. /* Restore the terminal to its original state. */
  2151. static void
  2152. rl_deprep_terminal ()
  2153. {
  2154. #if !defined(__GO32s__) && !defined(WIN32)
  2155. int tty = fileno (rl_instream);
  2156. SIGNALS_DECLARE_SAVED (saved_signals);
  2157. if (!terminal_prepped)
  2158. return;
  2159. SIGNALS_BLOCK (SIGINT, saved_signals);
  2160. the_ttybuff.sg_flags = original_tty_flags;
  2161. ioctl (tty, TIOCSETN, &the_ttybuff);
  2162. readline_echoing_p = 1;
  2163. #if defined (TIOCLGET)
  2164. ioctl (tty, TIOCLSET, &local_mode_flags);
  2165. #endif
  2166. #if defined (TIOCSLTC)
  2167. ioctl (tty, TIOCSLTC, &original_ltchars);
  2168. #endif
  2169. #if defined (TIOCSETC)
  2170. ioctl (tty, TIOCSETC, &original_tchars);
  2171. #endif
  2172. terminal_prepped = 0;
  2173. SIGNALS_RESTORE (saved_signals);
  2174. #endif /* !__GO32 */
  2175. }
  2176. #else /* !defined (NEW_TTY_DRIVER) */
  2177. #if !defined (VMIN)
  2178. #define VMIN VEOF
  2179. #endif
  2180. #if !defined (VTIME)
  2181. #define VTIME VEOL
  2182. #endif
  2183. #if !defined(__GO32__) && !defined(WIN32)
  2184. #if defined (TERMIOS_TTY_DRIVER)
  2185. static struct termios otio;
  2186. #else
  2187. static struct termio otio;
  2188. #endif /* !TERMIOS_TTY_DRIVER */
  2189. #endif /* __GO32__ */
  2190. static void
  2191. rl_prep_terminal ()
  2192. {
  2193. #ifdef WIN32
  2194. winio_rdln_on();
  2195. #endif
  2196. #if !defined(__GO32__) && !defined(WIN32)
  2197. int tty = fileno (rl_instream);
  2198. #if defined (TERMIOS_TTY_DRIVER)
  2199. struct termios tio;
  2200. #else
  2201. struct termio tio;
  2202. #endif /* !TERMIOS_TTY_DRIVER */
  2203. SIGNALS_DECLARE_SAVED (saved_signals);
  2204. if (terminal_prepped)
  2205. return;
  2206. /* Try to keep this function from being INTerrupted. We can do it
  2207. on POSIX and systems with BSD-like signal handling. */
  2208. SIGNALS_BLOCK (SIGINT, saved_signals);
  2209. #if defined (TERMIOS_TTY_DRIVER)
  2210. tcgetattr (tty, &tio);
  2211. #else
  2212. ioctl (tty, TCGETA, &tio);
  2213. #endif /* !TERMIOS_TTY_DRIVER */
  2214. otio = tio;
  2215. readline_echoing_p = (tio.c_lflag & ECHO);
  2216. tio.c_lflag &= ~(ICANON|ECHO);
  2217. if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
  2218. eof_char = otio.c_cc[VEOF];
  2219. #if defined (USE_XON_XOFF)
  2220. #if defined (IXANY)
  2221. tio.c_iflag &= ~(IXON|IXOFF|IXANY);
  2222. #else
  2223. /* `strict' Posix systems do not define IXANY. */
  2224. tio.c_iflag &= ~(IXON|IXOFF);
  2225. #endif /* IXANY */
  2226. #endif /* USE_XON_XOFF */
  2227. /* Only turn this off if we are using all 8 bits. */
  2228. /* |ISTRIP|INPCK */
  2229. tio.c_iflag &= ~(ISTRIP | INPCK);
  2230. /* Make sure we differentiate between CR and NL on input. */
  2231. tio.c_iflag &= ~(ICRNL | INLCR);
  2232. #if !defined (HANDLE_SIGNALS)
  2233. tio.c_lflag &= ~ISIG;
  2234. #else
  2235. tio.c_lflag |= ISIG;
  2236. #endif
  2237. tio.c_cc[VMIN] = 1;
  2238. tio.c_cc[VTIME] = 0;
  2239. /* Turn off characters that we need on Posix systems with job control,
  2240. just to be sure. This includes ^Y and ^V. This should not really
  2241. be necessary. */
  2242. #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
  2243. #if defined (VLNEXT)
  2244. tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
  2245. #endif
  2246. #if defined (VDSUSP)
  2247. tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
  2248. #endif
  2249. #endif /* POSIX && JOB_CONTROL */
  2250. #if defined (TERMIOS_TTY_DRIVER)
  2251. tcsetattr (tty, TCSADRAIN, &tio);
  2252. tcflow (tty, TCOON); /* Simulate a ^Q. */
  2253. #else
  2254. ioctl (tty, TCSETAW, &tio);
  2255. ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
  2256. #endif /* !TERMIOS_TTY_DRIVER */
  2257. terminal_prepped = 1;
  2258. SIGNALS_RESTORE (saved_signals);
  2259. #endif /* !__GO32__ */
  2260. }
  2261. static void
  2262. rl_deprep_terminal ()
  2263. {
  2264. #ifdef WIN32
  2265. winio_rdln_off();
  2266. #endif
  2267. #if !defined(__GO32__) && !defined(WIN32)
  2268. int tty = fileno (rl_instream);
  2269. /* Try to keep this function from being INTerrupted. We can do it
  2270. on POSIX and systems with BSD-like signal handling. */
  2271. SIGNALS_DECLARE_SAVED (saved_signals);
  2272. if (!terminal_prepped)
  2273. return;
  2274. SIGNALS_BLOCK (SIGINT, saved_signals);
  2275. #if defined (TERMIOS_TTY_DRIVER)
  2276. tcsetattr (tty, TCSADRAIN, &otio);
  2277. tcflow (tty, TCOON); /* Simulate a ^Q. */
  2278. #else /* TERMIOS_TTY_DRIVER */
  2279. ioctl (tty, TCSETAW, &otio);
  2280. ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
  2281. #endif /* !TERMIOS_TTY_DRIVER */
  2282. terminal_prepped = 0;
  2283. SIGNALS_RESTORE (saved_signals);
  2284. #endif /* !__GO32__ */
  2285. }
  2286. #endif /* NEW_TTY_DRIVER */
  2287. /* **************************************************************** */
  2288. /* */
  2289. /* Utility Functions */
  2290. /* */
  2291. /* **************************************************************** */
  2292. /* Return 0 if C is not a member of the class of characters that belong
  2293. in words, or 1 if it is. */
  2294. int allow_pathname_alphabetic_chars = 0;
  2295. char *pathname_alphabetic_chars = "/-_=~.#$";
  2296. int
  2297. alphabetic (c)
  2298. int c;
  2299. {
  2300. if (pure_alphabetic (c) || (numeric (c)))
  2301. return (1);
  2302. if (allow_pathname_alphabetic_chars)
  2303. return ((int)rindex (pathname_alphabetic_chars, c));
  2304. else
  2305. return (0);
  2306. }
  2307. /* Return non-zero if C is a numeric character. */
  2308. int
  2309. numeric (c)
  2310. int c;
  2311. {
  2312. return (c >= '0' && c <= '9');
  2313. }
  2314. /* Ring the terminal bell. */
  2315. int
  2316. ding ()
  2317. {
  2318. #ifdef WIN32
  2319. printf("\007");
  2320. #else
  2321. if (readline_echoing_p)
  2322. {
  2323. #ifndef __GO32__
  2324. if (prefer_visible_bell && visible_bell)
  2325. tputs (visible_bell, 1, output_character_function);
  2326. else
  2327. #endif /* !__GO32__ */
  2328. {
  2329. fprintf (stderr, "\007");
  2330. fflush (stderr);
  2331. }
  2332. }
  2333. #endif
  2334. return (-1);
  2335. }
  2336. /* How to abort things. */
  2337. rl_abort ()
  2338. {
  2339. ding ();
  2340. rl_clear_message ();
  2341. rl_init_argument ();
  2342. rl_pending_input = 0;
  2343. defining_kbd_macro = 0;
  2344. while (executing_macro)
  2345. pop_executing_macro ();
  2346. rl_last_func = (Function *)NULL;
  2347. longjmp (readline_top_level, 1);
  2348. }
  2349. /* Return a copy of the string between FROM and TO.
  2350. FROM is inclusive, TO is not. */
  2351. #if defined (sun) /* Yes, that's right, some crufty function in sunview is
  2352. called rl_copy (). */
  2353. static
  2354. #endif
  2355. char *
  2356. rl_copy (from, to)
  2357. int from, to;
  2358. {
  2359. register int length;
  2360. char *copy;
  2361. /* Fix it if the caller is confused. */
  2362. if (from > to)
  2363. {
  2364. int t = from;
  2365. from = to;
  2366. to = t;
  2367. }
  2368. length = to - from;
  2369. copy = (char *)xmalloc (1 + length);
  2370. strncpy (copy, the_line + from, length);
  2371. copy[length] = '\0';
  2372. return (copy);
  2373. }
  2374. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  2375. LEN characters. */
  2376. void
  2377. rl_extend_line_buffer (len)
  2378. int len;
  2379. {
  2380. while (len >= rl_line_buffer_len)
  2381. rl_line_buffer =
  2382. (char *)xrealloc
  2383. (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
  2384. the_line = rl_line_buffer;
  2385. }
  2386. /* **************************************************************** */
  2387. /* */
  2388. /* Insert and Delete */
  2389. /* */
  2390. /* **************************************************************** */
  2391. /* Insert a string of text into the line at point. This is the only
  2392. way that you should do insertion. rl_insert () calls this
  2393. function. */
  2394. rl_insert_text (string)
  2395. char *string;
  2396. {
  2397. extern int doing_an_undo;
  2398. register int i, l = strlen (string);
  2399. if (rl_end + l >= rl_line_buffer_len)
  2400. rl_extend_line_buffer (rl_end + l);
  2401. for (i = rl_end; i >= rl_point; i--)
  2402. the_line[i + l] = the_line[i];
  2403. strncpy (the_line + rl_point, string, l);
  2404. /* Remember how to undo this if we aren't undoing something. */
  2405. if (!doing_an_undo)
  2406. {
  2407. /* If possible and desirable, concatenate the undos. */
  2408. if ((strlen (string) == 1) &&
  2409. rl_undo_list &&
  2410. (rl_undo_list->what == UNDO_INSERT) &&
  2411. (rl_undo_list->end == rl_point) &&
  2412. (rl_undo_list->end - rl_undo_list->start < 20))
  2413. rl_undo_list->end++;
  2414. else
  2415. rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  2416. }
  2417. rl_point += l;
  2418. rl_end += l;
  2419. the_line[rl_end] = '\0';
  2420. }
  2421. /* Delete the string between FROM and TO. FROM is
  2422. inclusive, TO is not. */
  2423. rl_delete_text (from, to)
  2424. int from, to;
  2425. {
  2426. extern int doing_an_undo;
  2427. register char *text;
  2428. /* Fix it if the caller is confused. */
  2429. if (from > to)
  2430. {
  2431. int t = from;
  2432. from = to;
  2433. to = t;
  2434. }
  2435. text = rl_copy (from, to);
  2436. strncpy (the_line + from, the_line + to, rl_end - to);
  2437. /* Remember how to undo this delete. */
  2438. if (!doing_an_undo)
  2439. rl_add_undo (UNDO_DELETE, from, to, text);
  2440. else
  2441. free (text);
  2442. rl_end -= (to - from);
  2443. the_line[rl_end] = '\0';
  2444. }
  2445. /* **************************************************************** */
  2446. /* */
  2447. /* Readline character functions */
  2448. /* */
  2449. /* **************************************************************** */
  2450. /* This is not a gap editor, just a stupid line input routine. No hair
  2451. is involved in writing any of the functions, and none should be. */
  2452. /* Note that:
  2453. rl_end is the place in the string that we would place '\0';
  2454. i.e., it is always safe to place '\0' there.
  2455. rl_point is the place in the string where the cursor is. Sometimes
  2456. this is the same as rl_end.
  2457. Any command that is called interactively receives two arguments.
  2458. The first is a count: the numeric arg pased to this command.
  2459. The second is the key which invoked this command.
  2460. */
  2461. /* **************************************************************** */
  2462. /* */
  2463. /* Movement Commands */
  2464. /* */
  2465. /* **************************************************************** */
  2466. /* Note that if you `optimize' the display for these functions, you cannot
  2467. use said functions in other functions which do not do optimizing display.
  2468. I.e., you will have to update the data base for rl_redisplay, and you
  2469. might as well let rl_redisplay do that job. */
  2470. /* Move forward COUNT characters. */
  2471. rl_forward (count)
  2472. int count;
  2473. {
  2474. if (count < 0)
  2475. rl_backward (-count);
  2476. else
  2477. while (count)
  2478. {
  2479. #if defined (VI_MODE)
  2480. if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
  2481. #else
  2482. if (rl_point == rl_end)
  2483. #endif /* VI_MODE */
  2484. {
  2485. ding ();
  2486. return;
  2487. }
  2488. else
  2489. rl_point++;
  2490. --count;
  2491. }
  2492. }
  2493. /* Move backward COUNT characters. */
  2494. rl_backward (count)
  2495. int count;
  2496. {
  2497. if (count < 0)
  2498. rl_forward (-count);
  2499. else
  2500. while (count)
  2501. {
  2502. if (!rl_point)
  2503. {
  2504. ding ();
  2505. return;
  2506. }
  2507. else
  2508. --rl_point;
  2509. --count;
  2510. }
  2511. }
  2512. /* Move to the beginning of the line. */
  2513. rl_beg_of_line ()
  2514. {
  2515. rl_point = 0;
  2516. }
  2517. /* Move to the end of the line. */
  2518. rl_end_of_line ()
  2519. {
  2520. rl_point = rl_end;
  2521. }
  2522. /* Move forward a word. We do what Emacs does. */
  2523. rl_forward_word (count)
  2524. int count;
  2525. {
  2526. int c;
  2527. if (count < 0)
  2528. {
  2529. rl_backward_word (-count);
  2530. return;
  2531. }
  2532. while (count)
  2533. {
  2534. if (rl_point == rl_end)
  2535. return;
  2536. /* If we are not in a word, move forward until we are in one.
  2537. Then, move forward until we hit a non-alphabetic character. */
  2538. c = the_line[rl_point];
  2539. if (!alphabetic (c))
  2540. {
  2541. while (++rl_point < rl_end)
  2542. {
  2543. c = the_line[rl_point];
  2544. if (alphabetic (c)) break;
  2545. }
  2546. }
  2547. if (rl_point == rl_end) return;
  2548. while (++rl_point < rl_end)
  2549. {
  2550. c = the_line[rl_point];
  2551. if (!alphabetic (c)) break;
  2552. }
  2553. --count;
  2554. }
  2555. }
  2556. /* Move backward a word. We do what Emacs does. */
  2557. rl_backward_word (count)
  2558. int count;
  2559. {
  2560. int c;
  2561. if (count < 0)
  2562. {
  2563. rl_forward_word (-count);
  2564. return;
  2565. }
  2566. while (count)
  2567. {
  2568. if (!rl_point)
  2569. return;
  2570. /* Like rl_forward_word (), except that we look at the characters
  2571. just before point. */
  2572. c = the_line[rl_point - 1];
  2573. if (!alphabetic (c))
  2574. {
  2575. while (--rl_point)
  2576. {
  2577. c = the_line[rl_point - 1];
  2578. if (alphabetic (c)) break;
  2579. }
  2580. }
  2581. while (rl_point)
  2582. {
  2583. c = the_line[rl_point - 1];
  2584. if (!alphabetic (c))
  2585. break;
  2586. else --rl_point;
  2587. }
  2588. --count;
  2589. }
  2590. }
  2591. /* Clear the current line. Numeric argument to C-l does this. */
  2592. rl_refresh_line ()
  2593. {
  2594. #ifdef WIN32
  2595. printf("implement rl_refresh_line\n");
  2596. #else
  2597. int curr_line = last_c_pos / screenwidth;
  2598. extern char *term_clreol;
  2599. move_vert(curr_line);
  2600. move_cursor_relative (0, the_line); /* XXX is this right */
  2601. #if defined(__GO32__)
  2602. {
  2603. int r, c, w;
  2604. ScreenGetCursor(&r, &c);
  2605. w = ScreenCols();
  2606. memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
  2607. }
  2608. #else /* __GO32__ */
  2609. if (term_clreol)
  2610. tputs (term_clreol, 1, output_character_function);
  2611. #endif /* __GO32__/else */
  2612. rl_forced_update_display ();
  2613. rl_display_fixed = 1;
  2614. #endif
  2615. }
  2616. /* C-l typed to a line without quoting clears the screen, and then reprints
  2617. the prompt and the current input line. Given a numeric arg, redraw only
  2618. the current line. */
  2619. rl_clear_screen ()
  2620. {
  2621. #ifdef WIN32
  2622. printf("implement rl_clear_screen\n");
  2623. #else
  2624. extern char *term_clrpag;
  2625. if (rl_explicit_arg)
  2626. {
  2627. rl_refresh_line ();
  2628. return;
  2629. }
  2630. #ifndef __GO32__
  2631. if (term_clrpag)
  2632. tputs (term_clrpag, 1, output_character_function);
  2633. else
  2634. #endif /* !__GO32__ */
  2635. crlf ();
  2636. rl_forced_update_display ();
  2637. rl_display_fixed = 1;
  2638. #endif
  2639. }
  2640. rl_arrow_keys (count, c)
  2641. int count, c;
  2642. {
  2643. int ch;
  2644. ch = rl_read_key ();
  2645. switch (to_upper (ch))
  2646. {
  2647. case 'A':
  2648. rl_get_previous_history (count);
  2649. break;
  2650. case 'B':
  2651. rl_get_next_history (count);
  2652. break;
  2653. case 'C':
  2654. rl_forward (count);
  2655. break;
  2656. case 'D':
  2657. rl_backward (count);
  2658. break;
  2659. default:
  2660. ding ();
  2661. }
  2662. }
  2663. /* **************************************************************** */
  2664. /* */
  2665. /* Text commands */
  2666. /* */
  2667. /* **************************************************************** */
  2668. /* Insert the character C at the current location, moving point forward. */
  2669. rl_insert (count, c)
  2670. int count, c;
  2671. {
  2672. register int i;
  2673. char *string;
  2674. #ifdef WIN32
  2675. char dummy[5];
  2676. dummy[0] = 0;
  2677. #endif
  2678. if (count <= 0)
  2679. return;
  2680. /* If we can optimize, then do it. But don't let people crash
  2681. readline because of extra large arguments. */
  2682. if (count > 1 && count < 1024)
  2683. {
  2684. string = (char *)alloca (1 + count);
  2685. for (i = 0; i < count; i++)
  2686. string[i] = c;
  2687. string[i] = '\0';
  2688. rl_insert_text (string);
  2689. return;
  2690. }
  2691. if (count > 1024)
  2692. {
  2693. int decreaser;
  2694. string = (char *)alloca (1024 + 1);
  2695. for (i = 0; i < 1024; i++)
  2696. string[i] = c;
  2697. while (count)
  2698. {
  2699. decreaser = (count > 1024 ? 1024 : count);
  2700. string[decreaser] = '\0';
  2701. rl_insert_text (string);
  2702. count -= decreaser;
  2703. }
  2704. return;
  2705. }
  2706. /* We are inserting a single character.
  2707. If there is pending input, then make a string of all of the
  2708. pending characters that are bound to rl_insert, and insert
  2709. them all. */
  2710. if (any_typein)
  2711. {
  2712. int key = 0, t;
  2713. i = 0;
  2714. string = (char *)alloca (ibuffer_len + 1);
  2715. string[i++] = c;
  2716. while ((t = rl_get_char (&key)) &&
  2717. (keymap[key].type == ISFUNC &&
  2718. keymap[key].function == rl_insert))
  2719. string[i++] = key;
  2720. if (t)
  2721. rl_unget_char (key);
  2722. string[i] = '\0';
  2723. rl_insert_text (string);
  2724. return;
  2725. }
  2726. else
  2727. {
  2728. /* Inserting a single character. */
  2729. string = (char*)alloca (2);
  2730. string[1] = '\0';
  2731. string[0] = c;
  2732. rl_insert_text (string);
  2733. }
  2734. }
  2735. /* Insert the next typed character verbatim. */
  2736. rl_quoted_insert (count)
  2737. int count;
  2738. {
  2739. int c = rl_read_key ();
  2740. rl_insert (count, c);
  2741. }
  2742. /* Insert a tab character. */
  2743. rl_tab_insert (count)
  2744. int count;
  2745. {
  2746. rl_insert (count, '\t');
  2747. }
  2748. /* What to do when a NEWLINE is pressed. We accept the whole line.
  2749. KEY is the key that invoked this command. I guess it could have
  2750. meaning in the future. */
  2751. rl_newline (count, key)
  2752. int count, key;
  2753. {
  2754. rl_done = 1;
  2755. #if defined (VI_MODE)
  2756. {
  2757. extern int vi_doing_insert;
  2758. if (vi_doing_insert)
  2759. {
  2760. rl_end_undo_group ();
  2761. vi_doing_insert = 0;
  2762. }
  2763. }
  2764. #endif /* VI_MODE */
  2765. if (readline_echoing_p)
  2766. {
  2767. move_vert (vis_botlin);
  2768. vis_botlin = 0;
  2769. crlf ();
  2770. #ifndef WIN32
  2771. fflush (out_stream);
  2772. #endif
  2773. rl_display_fixed++;
  2774. }
  2775. }
  2776. rl_clean_up_for_exit ()
  2777. {
  2778. if (readline_echoing_p)
  2779. {
  2780. move_vert (vis_botlin);
  2781. vis_botlin = 0;
  2782. fflush (out_stream);
  2783. rl_restart_output ();
  2784. }
  2785. }
  2786. /* What to do for some uppercase characters, like meta characters,
  2787. and some characters appearing in emacs_ctlx_keymap. This function
  2788. is just a stub, you bind keys to it and the code in rl_dispatch ()
  2789. is special cased. */
  2790. rl_do_lowercase_version (ignore1, ignore2)
  2791. int ignore1, ignore2;
  2792. {
  2793. }
  2794. /* Rubout the character behind point. */
  2795. rl_rubout (count)
  2796. int count;
  2797. {
  2798. if (count < 0)
  2799. {
  2800. rl_delete (-count);
  2801. return;
  2802. }
  2803. if (!rl_point)
  2804. {
  2805. ding ();
  2806. return;
  2807. }
  2808. if (count > 1)
  2809. {
  2810. int orig_point = rl_point;
  2811. rl_backward (count);
  2812. rl_kill_text (orig_point, rl_point);
  2813. }
  2814. else
  2815. {
  2816. int c = the_line[--rl_point];
  2817. rl_delete_text (rl_point, rl_point + 1);
  2818. if (rl_point == rl_end && alphabetic (c) && last_c_pos)
  2819. {
  2820. backspace (1);
  2821. #ifndef WIN32
  2822. #if GRX
  2823. if (egagrph) {
  2824. grx_output_some_chars(" ", 1);
  2825. }else
  2826. #endif
  2827. {
  2828. putc (' ', out_stream);
  2829. }
  2830. backspace (1);
  2831. #endif
  2832. last_c_pos--;
  2833. visible_line[last_c_pos] = '\0';
  2834. rl_display_fixed++;
  2835. }
  2836. }
  2837. }
  2838. /* Delete the character under the cursor. Given a numeric argument,
  2839. kill that many characters instead. */
  2840. rl_delete (count, invoking_key)
  2841. int count, invoking_key;
  2842. {
  2843. if (count < 0)
  2844. {
  2845. rl_rubout (-count);
  2846. return;
  2847. }
  2848. if (rl_point == rl_end)
  2849. {
  2850. ding ();
  2851. return;
  2852. }
  2853. if (count > 1)
  2854. {
  2855. int orig_point = rl_point;
  2856. rl_forward (count);
  2857. rl_kill_text (orig_point, rl_point);
  2858. rl_point = orig_point;
  2859. }
  2860. else
  2861. rl_delete_text (rl_point, rl_point + 1);
  2862. }
  2863. /* **************************************************************** */
  2864. /* */
  2865. /* Kill commands */
  2866. /* */
  2867. /* **************************************************************** */
  2868. /* The next two functions mimic unix line editing behaviour, except they
  2869. save the deleted text on the kill ring. This is safer than not saving
  2870. it, and since we have a ring, nobody should get screwed. */
  2871. /* This does what C-w does in Unix. We can't prevent people from
  2872. using behaviour that they expect. */
  2873. rl_unix_word_rubout ()
  2874. {
  2875. if (!rl_point) ding ();
  2876. else {
  2877. int orig_point = rl_point;
  2878. while (rl_point && whitespace (the_line[rl_point - 1]))
  2879. rl_point--;
  2880. while (rl_point && !whitespace (the_line[rl_point - 1]))
  2881. rl_point--;
  2882. rl_kill_text (rl_point, orig_point);
  2883. }
  2884. }
  2885. /* Here is C-u doing what Unix does. You don't *have* to use these
  2886. key-bindings. We have a choice of killing the entire line, or
  2887. killing from where we are to the start of the line. We choose the
  2888. latter, because if you are a Unix weenie, then you haven't backspaced
  2889. into the line at all, and if you aren't, then you know what you are
  2890. doing. */
  2891. rl_unix_line_discard ()
  2892. {
  2893. if (!rl_point) ding ();
  2894. else {
  2895. rl_kill_text (rl_point, 0);
  2896. rl_point = 0;
  2897. }
  2898. }
  2899. /* **************************************************************** */
  2900. /* */
  2901. /* Commands For Typos */
  2902. /* */
  2903. /* **************************************************************** */
  2904. /* Random and interesting things in here. */
  2905. /* **************************************************************** */
  2906. /* */
  2907. /* Changing Case */
  2908. /* */
  2909. /* **************************************************************** */
  2910. /* The three kinds of things that we know how to do. */
  2911. #define UpCase 1
  2912. #define DownCase 2
  2913. #define CapCase 3
  2914. /* Uppercase the word at point. */
  2915. rl_upcase_word (count)
  2916. int count;
  2917. {
  2918. rl_change_case (count, UpCase);
  2919. }
  2920. /* Lowercase the word at point. */
  2921. rl_downcase_word (count)
  2922. int count;
  2923. {
  2924. rl_change_case (count, DownCase);
  2925. }
  2926. /* Upcase the first letter, downcase the rest. */
  2927. rl_capitalize_word (count)
  2928. int count;
  2929. {
  2930. rl_change_case (count, CapCase);
  2931. }
  2932. /* The meaty function.
  2933. Change the case of COUNT words, performing OP on them.
  2934. OP is one of UpCase, DownCase, or CapCase.
  2935. If a negative argument is given, leave point where it started,
  2936. otherwise, leave it where it moves to. */
  2937. rl_change_case (count, op)
  2938. int count, op;
  2939. {
  2940. register int start = rl_point, end;
  2941. int state = 0;
  2942. rl_forward_word (count);
  2943. end = rl_point;
  2944. if (count < 0)
  2945. {
  2946. int temp = start;
  2947. start = end;
  2948. end = temp;
  2949. }
  2950. /* We are going to modify some text, so let's prepare to undo it. */
  2951. rl_modifying (start, end);
  2952. for (; start < end; start++)
  2953. {
  2954. switch (op)
  2955. {
  2956. case UpCase:
  2957. the_line[start] = to_upper (the_line[start]);
  2958. break;
  2959. case DownCase:
  2960. the_line[start] = to_lower (the_line[start]);
  2961. break;
  2962. case CapCase:
  2963. if (state == 0)
  2964. {
  2965. the_line[start] = to_upper (the_line[start]);
  2966. state = 1;
  2967. }
  2968. else
  2969. {
  2970. the_line[start] = to_lower (the_line[start]);
  2971. }
  2972. if (!pure_alphabetic (the_line[start]))
  2973. state = 0;
  2974. break;
  2975. default:
  2976. abort ();
  2977. }
  2978. }
  2979. rl_point = end;
  2980. }
  2981. /* **************************************************************** */
  2982. /* */
  2983. /* Transposition */
  2984. /* */
  2985. /* **************************************************************** */
  2986. /* Transpose the words at point. */
  2987. rl_transpose_words (count)
  2988. int count;
  2989. {
  2990. char *word1, *word2;
  2991. int w1_beg, w1_end, w2_beg, w2_end;
  2992. int orig_point = rl_point;
  2993. if (!count) return;
  2994. /* Find the two words. */
  2995. rl_forward_word (count);
  2996. w2_end = rl_point;
  2997. rl_backward_word (1);
  2998. w2_beg = rl_point;
  2999. rl_backward_word (count);
  3000. w1_beg = rl_point;
  3001. rl_forward_word (1);
  3002. w1_end = rl_point;
  3003. /* Do some check to make sure that there really are two words. */
  3004. if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  3005. {
  3006. ding ();
  3007. rl_point = orig_point;
  3008. return;
  3009. }
  3010. /* Get the text of the words. */
  3011. word1 = rl_copy (w1_beg, w1_end);
  3012. word2 = rl_copy (w2_beg, w2_end);
  3013. /* We are about to do many insertions and deletions. Remember them
  3014. as one operation. */
  3015. rl_begin_undo_group ();
  3016. /* Do the stuff at word2 first, so that we don't have to worry
  3017. about word1 moving. */
  3018. rl_point = w2_beg;
  3019. rl_delete_text (w2_beg, w2_end);
  3020. rl_insert_text (word1);
  3021. rl_point = w1_beg;
  3022. rl_delete_text (w1_beg, w1_end);
  3023. rl_insert_text (word2);
  3024. /* This is exactly correct since the text before this point has not
  3025. changed in length. */
  3026. rl_point = w2_end;
  3027. /* I think that does it. */
  3028. rl_end_undo_group ();
  3029. free (word1); free (word2);
  3030. }
  3031. /* Transpose the characters at point. If point is at the end of the line,
  3032. then transpose the characters before point. */
  3033. rl_transpose_chars (count)
  3034. int count;
  3035. {
  3036. if (!count)
  3037. return;
  3038. if (!rl_point || rl_end < 2) {
  3039. ding ();
  3040. return;
  3041. }
  3042. while (count)
  3043. {
  3044. if (rl_point == rl_end)
  3045. {
  3046. int t = the_line[rl_point - 1];
  3047. the_line[rl_point - 1] = the_line[rl_point - 2];
  3048. the_line[rl_point - 2] = t;
  3049. }
  3050. else
  3051. {
  3052. int t = the_line[rl_point];
  3053. the_line[rl_point] = the_line[rl_point - 1];
  3054. the_line[rl_point - 1] = t;
  3055. if (count < 0 && rl_point)
  3056. rl_point--;
  3057. else
  3058. rl_point++;
  3059. }
  3060. if (count < 0)
  3061. count++;
  3062. else
  3063. count--;
  3064. }
  3065. }
  3066. /* **************************************************************** */
  3067. /* */
  3068. /* Bogus Flow Control */
  3069. /* */
  3070. /* **************************************************************** */
  3071. rl_restart_output (count, key)
  3072. int count, key;
  3073. {
  3074. int fildes = fileno (rl_outstream);
  3075. #if defined (TIOCSTART)
  3076. #if defined (apollo)
  3077. ioctl (&fildes, TIOCSTART, 0);
  3078. #else
  3079. ioctl (fildes, TIOCSTART, 0);
  3080. #endif /* apollo */
  3081. #else
  3082. # if defined (TERMIOS_TTY_DRIVER)
  3083. tcflow (fildes, TCOON);
  3084. # else
  3085. # if defined (TCXONC)
  3086. ioctl (fildes, TCXONC, TCOON);
  3087. # endif /* TCXONC */
  3088. # endif /* !TERMIOS_TTY_DRIVER */
  3089. #endif /* TIOCSTART */
  3090. }
  3091. rl_stop_output (count, key)
  3092. int count, key;
  3093. {
  3094. int fildes = fileno (rl_instream);
  3095. #if defined (TIOCSTOP)
  3096. # if defined (apollo)
  3097. ioctl (&fildes, TIOCSTOP, 0);
  3098. # else
  3099. ioctl (fildes, TIOCSTOP, 0);
  3100. # endif /* apollo */
  3101. #else
  3102. # if defined (TERMIOS_TTY_DRIVER)
  3103. tcflow (fildes, TCOOFF);
  3104. # else
  3105. # if defined (TCXONC)
  3106. ioctl (fildes, TCXONC, TCOON);
  3107. # endif /* TCXONC */
  3108. # endif /* !TERMIOS_TTY_DRIVER */
  3109. #endif /* TIOCSTOP */
  3110. }
  3111. /* **************************************************************** */
  3112. /* */
  3113. /* Completion matching, from readline's point of view. */
  3114. /* */
  3115. /* **************************************************************** */
  3116. /* Pointer to the generator function for completion_matches ().
  3117. NULL means to use filename_entry_function (), the default filename
  3118. completer. */
  3119. Function *rl_completion_entry_function = (Function *)NULL;
  3120. /* Pointer to alternative function to create matches.
  3121. Function is called with TEXT, START, and END.
  3122. START and END are indices in RL_LINE_BUFFER saying what the boundaries
  3123. of TEXT are.
  3124. If this function exists and returns NULL then call the value of
  3125. rl_completion_entry_function to try to match, otherwise use the
  3126. array of strings returned. */
  3127. Function *rl_attempted_completion_function = (Function *)NULL;
  3128. /* Local variable states what happened during the last completion attempt. */
  3129. static int completion_changed_buffer = 0;
  3130. /* Complete the word at or before point. You have supplied the function
  3131. that does the initial simple matching selection algorithm (see
  3132. completion_matches ()). The default is to do filename completion. */
  3133. rl_complete (ignore, invoking_key)
  3134. int ignore, invoking_key;
  3135. {
  3136. if (rl_last_func == rl_complete && !completion_changed_buffer)
  3137. rl_complete_internal ('?');
  3138. else
  3139. rl_complete_internal (TAB);
  3140. }
  3141. /* List the possible completions. See description of rl_complete (). */
  3142. rl_possible_completions ()
  3143. {
  3144. rl_complete_internal ('?');
  3145. }
  3146. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  3147. get_y_or_n ()
  3148. {
  3149. int c;
  3150. loop:
  3151. c = rl_read_key ();
  3152. if (c == 'y' || c == 'Y') return (1);
  3153. if (c == 'n' || c == 'N') return (0);
  3154. if (c == ABORT_CHAR) rl_abort ();
  3155. ding (); goto loop;
  3156. }
  3157. /* Up to this many items will be displayed in response to a
  3158. possible-completions call. After that, we ask the user if
  3159. she is sure she wants to see them all. */
  3160. int rl_completion_query_items = 100;
  3161. /* The basic list of characters that signal a break between words for the
  3162. completer routine. The contents of this variable is what breaks words
  3163. in the shell, i.e. " \t\n\"\\'`@$><=" */
  3164. char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
  3165. /* The list of characters that signal a break between words for
  3166. rl_complete_internal. The default list is the contents of
  3167. rl_basic_word_break_characters. */
  3168. char *rl_completer_word_break_characters = (char *)NULL;
  3169. /* The list of characters which are used to quote a substring of the command
  3170. line. Command completion occurs on the entire substring, and within the
  3171. substring rl_completer_word_break_characters are treated as any other
  3172. character, unless they also appear within this list. */
  3173. char *rl_completer_quote_characters = (char *)NULL;
  3174. /* List of characters that are word break characters, but should be left
  3175. in TEXT when it is passed to the completion function. The shell uses
  3176. this to help determine what kind of completing to do. */
  3177. char *rl_special_prefixes = (char *)NULL;
  3178. /* If non-zero, then disallow duplicates in the matches. */
  3179. int rl_ignore_completion_duplicates = 1;
  3180. /* Non-zero means that the results of the matches are to be treated
  3181. as filenames. This is ALWAYS zero on entry, and can only be changed
  3182. within a completion entry finder function. */
  3183. int rl_filename_completion_desired = 0;
  3184. /* This function, if defined, is called by the completer when real
  3185. filename completion is done, after all the matching names have been
  3186. generated. It is passed a (char**) known as matches in the code below.
  3187. It consists of a NULL-terminated array of pointers to potential
  3188. matching strings. The 1st element (matches[0]) is the maximal
  3189. substring that is common to all matches. This function can re-arrange
  3190. the list of matches as required, but all elements of the array must be
  3191. free()'d if they are deleted. The main intent of this function is
  3192. to implement FIGNORE a la SunOS csh. */
  3193. Function *rl_ignore_some_completions_function = (Function *)NULL;
  3194. /* Complete the word at or before point.
  3195. WHAT_TO_DO says what to do with the completion.
  3196. `?' means list the possible completions.
  3197. TAB means do standard completion.
  3198. `*' means insert all of the possible completions. */
  3199. rl_complete_internal (what_to_do)
  3200. int what_to_do;
  3201. {
  3202. char *filename_completion_function ();
  3203. char **completion_matches (), **matches;
  3204. Function *our_func;
  3205. int start, scan, end, delimiter = 0;
  3206. char *text, *saved_line_buffer;
  3207. char quote_char = '\0';
  3208. char *replacement;
  3209. if (the_line)
  3210. saved_line_buffer = savestring (the_line);
  3211. else
  3212. saved_line_buffer = (char *)NULL;
  3213. if (rl_completion_entry_function)
  3214. our_func = rl_completion_entry_function;
  3215. else
  3216. our_func = (int (*)())filename_completion_function;
  3217. /* Only the completion entry function can change this. */
  3218. rl_filename_completion_desired = 0;
  3219. /* We now look backwards for the start of a filename/variable word. */
  3220. end = rl_point;
  3221. if (rl_point)
  3222. {
  3223. if (rl_completer_quote_characters)
  3224. {
  3225. /* We have a list of characters which can be used in pairs to quote
  3226. substrings for completion. Try to find the start of an unclosed
  3227. quoted substring.
  3228. FIXME: Doesn't yet handle '\' escapes to hid embedded quotes */
  3229. for (scan = 0; scan < end; scan++)
  3230. {
  3231. if (quote_char != '\0')
  3232. {
  3233. /* Ignore everything until the matching close quote char */
  3234. if (the_line[scan] == quote_char)
  3235. {
  3236. /* Found matching close quote. Abandon this substring. */
  3237. quote_char = '\0';
  3238. rl_point = end;
  3239. }
  3240. }
  3241. else if (rindex (rl_completer_quote_characters, the_line[scan]))
  3242. {
  3243. /* Found start of a quoted substring. */
  3244. quote_char = the_line[scan];
  3245. rl_point = scan + 1;
  3246. }
  3247. }
  3248. }
  3249. if (rl_point == end)
  3250. {
  3251. /* We didn't find an unclosed quoted substring upon which to do
  3252. completion, so use the word break characters to find the
  3253. substring on which to do completion. */
  3254. while (--rl_point &&
  3255. !rindex (rl_completer_word_break_characters,
  3256. the_line[rl_point])) {;}
  3257. }
  3258. /* If we are at a word break, then advance past it. */
  3259. if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
  3260. {
  3261. /* If the character that caused the word break was a quoting
  3262. character, then remember it as the delimiter. */
  3263. if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
  3264. delimiter = the_line[rl_point];
  3265. /* If the character isn't needed to determine something special
  3266. about what kind of completion to perform, then advance past it. */
  3267. if (!rl_special_prefixes ||
  3268. !rindex (rl_special_prefixes, the_line[rl_point]))
  3269. rl_point++;
  3270. }
  3271. }
  3272. start = rl_point;
  3273. rl_point = end;
  3274. text = rl_copy (start, end);
  3275. /* If the user wants to TRY to complete, but then wants to give
  3276. up and use the default completion function, they set the
  3277. variable rl_attempted_completion_function. */
  3278. if (rl_attempted_completion_function)
  3279. {
  3280. matches =
  3281. (char **)(*rl_attempted_completion_function) (text, start, end);
  3282. if (matches)
  3283. {
  3284. our_func = (Function *)NULL;
  3285. goto after_usual_completion;
  3286. }
  3287. }
  3288. matches = completion_matches (text, our_func);
  3289. after_usual_completion:
  3290. free (text);
  3291. if (!matches)
  3292. ding ();
  3293. else
  3294. {
  3295. register int i;
  3296. some_matches:
  3297. /* It seems to me that in all the cases we handle we would like
  3298. to ignore duplicate possibilities. Scan for the text to
  3299. insert being identical to the other completions. */
  3300. if (rl_ignore_completion_duplicates)
  3301. {
  3302. char *lowest_common;
  3303. int j, newlen = 0;
  3304. /* Sort the items. */
  3305. /* It is safe to sort this array, because the lowest common
  3306. denominator found in matches[0] will remain in place. */
  3307. for (i = 0; matches[i]; i++);
  3308. qsort (matches, i, sizeof (char *), compare_strings);
  3309. /* Remember the lowest common denominator for it may be unique. */
  3310. lowest_common = savestring (matches[0]);
  3311. for (i = 0; matches[i + 1]; i++)
  3312. {
  3313. if (strcmp (matches[i], matches[i + 1]) == 0)
  3314. {
  3315. free (matches[i]);
  3316. matches[i] = (char *)-1;
  3317. }
  3318. else
  3319. newlen++;
  3320. }
  3321. /* We have marked all the dead slots with (char *)-1.
  3322. Copy all the non-dead entries into a new array. */
  3323. {
  3324. char **temp_array =
  3325. (char **)malloc ((3 + newlen) * sizeof (char *));
  3326. for (i = 1, j = 1; matches[i]; i++)
  3327. {
  3328. if (matches[i] != (char *)-1)
  3329. temp_array[j++] = matches[i];
  3330. }
  3331. temp_array[j] = (char *)NULL;
  3332. if (matches[0] != (char *)-1)
  3333. free (matches[0]);
  3334. free (matches);
  3335. matches = temp_array;
  3336. }
  3337. /* Place the lowest common denominator back in [0]. */
  3338. matches[0] = lowest_common;
  3339. /* If there is one string left, and it is identical to the
  3340. lowest common denominator, then the LCD is the string to
  3341. insert. */
  3342. if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  3343. {
  3344. free (matches[1]);
  3345. matches[1] = (char *)NULL;
  3346. }
  3347. }
  3348. switch (what_to_do)
  3349. {
  3350. case TAB:
  3351. /* If we are matching filenames, then here is our chance to
  3352. do clever processing by re-examining the list. Call the
  3353. ignore function with the array as a parameter. It can
  3354. munge the array, deleting matches as it desires. */
  3355. if (rl_ignore_some_completions_function &&
  3356. our_func == (int (*)())filename_completion_function)
  3357. (void)(*rl_ignore_some_completions_function)(matches);
  3358. /* If we are doing completions on quoted substrings, and any matches
  3359. contain any of the completer word break characters, then auto-
  3360. matically prepend the substring with a quote character (just
  3361. pick the first one from the list of such) if it does not already
  3362. begin with a quote string. FIXME: Need to remove any such
  3363. automatically inserted quote character when it no longer is
  3364. necessary, such as if we change the string we are completing on
  3365. and the new set of matches don't require a quoted substring? */
  3366. replacement = matches[0];
  3367. if (matches[0] != NULL
  3368. && rl_completer_quote_characters != NULL
  3369. && (quote_char == '\0'))
  3370. {
  3371. for (i = 1; matches[i] != NULL; i++)
  3372. {
  3373. if (strpbrk (matches[i], rl_completer_word_break_characters))
  3374. {
  3375. /* Found an embedded word break character in a potential
  3376. match, so need to prepend a quote character if we are
  3377. replacing the completion string. */
  3378. replacement = (char *)alloca (strlen (matches[0]) + 2);
  3379. quote_char = *rl_completer_quote_characters;
  3380. *replacement = quote_char;
  3381. strcpy (replacement + 1, matches[0]);
  3382. break;
  3383. }
  3384. }
  3385. }
  3386. if (replacement)
  3387. {
  3388. rl_delete_text (start, rl_point);
  3389. rl_point = start;
  3390. rl_insert_text (replacement);
  3391. }
  3392. /* If there are more matches, ring the bell to indicate.
  3393. If this was the only match, and we are hacking files,
  3394. check the file to see if it was a directory. If so,
  3395. add a '/' to the name. If not, and we are at the end
  3396. of the line, then add a space. */
  3397. if (matches[1])
  3398. {
  3399. ding (); /* There are other matches remaining. */
  3400. }
  3401. else
  3402. {
  3403. char temp_string[16];
  3404. int temp_index = 0;
  3405. if (quote_char)
  3406. {
  3407. temp_string[temp_index++] = quote_char;
  3408. }
  3409. temp_string[temp_index++] = delimiter ? delimiter : ' ';
  3410. temp_string[temp_index++] = '\0';
  3411. if (rl_filename_completion_desired)
  3412. {
  3413. struct stat finfo;
  3414. char *filename = tilde_expand (matches[0]);
  3415. if ((stat (filename, &finfo) == 0) &&
  3416. S_ISDIR (finfo.st_mode))
  3417. {
  3418. if (the_line[rl_point] != '/')
  3419. rl_insert_text ("/");
  3420. }
  3421. else
  3422. {
  3423. if (rl_point == rl_end)
  3424. rl_insert_text (temp_string);
  3425. }
  3426. free (filename);
  3427. }
  3428. else
  3429. {
  3430. if (rl_point == rl_end)
  3431. rl_insert_text (temp_string);
  3432. }
  3433. }
  3434. break;
  3435. case '*':
  3436. {
  3437. int i = 1;
  3438. rl_delete_text (start, rl_point);
  3439. rl_point = start;
  3440. rl_begin_undo_group ();
  3441. if (matches[1])
  3442. {
  3443. while (matches[i])
  3444. {
  3445. rl_insert_text (matches[i++]);
  3446. rl_insert_text (" ");
  3447. }
  3448. }
  3449. else
  3450. {
  3451. rl_insert_text (matches[0]);
  3452. rl_insert_text (" ");
  3453. }
  3454. rl_end_undo_group ();
  3455. }
  3456. break;
  3457. case '?':
  3458. {
  3459. int len, count, limit, max = 0;
  3460. int j, k, l;
  3461. /* Handle simple case first. What if there is only one answer? */
  3462. if (!matches[1])
  3463. {
  3464. char *temp;
  3465. if (rl_filename_completion_desired)
  3466. temp = rindex (matches[0], '/');
  3467. else
  3468. temp = (char *)NULL;
  3469. if (!temp)
  3470. temp = matches[0];
  3471. else
  3472. temp++;
  3473. crlf ();
  3474. fprintf (out_stream, "%s", temp);
  3475. crlf ();
  3476. goto restart;
  3477. }
  3478. /* There is more than one answer. Find out how many there are,
  3479. and find out what the maximum printed length of a single entry
  3480. is. */
  3481. for (i = 1; matches[i]; i++)
  3482. {
  3483. char *temp = (char *)NULL;
  3484. /* If we are hacking filenames, then only count the characters
  3485. after the last slash in the pathname. */
  3486. if (rl_filename_completion_desired)
  3487. temp = rindex (matches[i], '/');
  3488. else
  3489. temp = (char *)NULL;
  3490. if (!temp)
  3491. temp = matches[i];
  3492. else
  3493. temp++;
  3494. if (strlen (temp) > max)
  3495. max = strlen (temp);
  3496. }
  3497. len = i;
  3498. /* If there are many items, then ask the user if she
  3499. really wants to see them all. */
  3500. if (len >= rl_completion_query_items)
  3501. {
  3502. crlf ();
  3503. fprintf (out_stream,
  3504. "There are %d possibilities. Do you really", len);
  3505. crlf ();
  3506. fprintf (out_stream, "wish to see them all? (y or n)");
  3507. fflush (out_stream);
  3508. if (!get_y_or_n ())
  3509. {
  3510. crlf ();
  3511. goto restart;
  3512. }
  3513. }
  3514. /* How many items of MAX length can we fit in the screen window? */
  3515. max += 2;
  3516. limit = screenwidth / max;
  3517. if (limit != 1 && (limit * max == screenwidth))
  3518. limit--;
  3519. /* Avoid a possible floating exception. If max > screenwidth,
  3520. limit will be 0 and a divide-by-zero fault will result. */
  3521. if (limit == 0)
  3522. limit = 1;
  3523. /* How many iterations of the printing loop? */
  3524. count = (len + (limit - 1)) / limit;
  3525. /* Watch out for special case. If LEN is less than LIMIT, then
  3526. just do the inner printing loop. */
  3527. if (len < limit) count = 1;
  3528. /* Sort the items if they are not already sorted. */
  3529. if (!rl_ignore_completion_duplicates)
  3530. qsort (matches, len, sizeof (char *), compare_strings);
  3531. /* Print the sorted items, up-and-down alphabetically, like
  3532. ls might. */
  3533. crlf ();
  3534. for (i = 1; i < count + 1; i++)
  3535. {
  3536. for (j = 0, l = i; j < limit; j++)
  3537. {
  3538. if (l > len || !matches[l])
  3539. {
  3540. break;
  3541. }
  3542. else
  3543. {
  3544. char *temp = (char *)NULL;
  3545. if (rl_filename_completion_desired)
  3546. temp = rindex (matches[l], '/');
  3547. else
  3548. temp = (char *)NULL;
  3549. if (!temp)
  3550. temp = matches[l];
  3551. else
  3552. temp++;
  3553. fprintf (out_stream, "%s", temp);
  3554. for (k = 0; k < max - strlen (temp); k++)
  3555. putc (' ', out_stream);
  3556. }
  3557. l += count;
  3558. }
  3559. crlf ();
  3560. }
  3561. restart:
  3562. rl_on_new_line ();
  3563. }
  3564. break;
  3565. default:
  3566. abort ();
  3567. }
  3568. for (i = 0; matches[i]; i++)
  3569. free (matches[i]);
  3570. free (matches);
  3571. }
  3572. /* Check to see if the line has changed through all of this manipulation. */
  3573. if (saved_line_buffer)
  3574. {
  3575. if (strcmp (the_line, saved_line_buffer) != 0)
  3576. completion_changed_buffer = 1;
  3577. else
  3578. completion_changed_buffer = 0;
  3579. free (saved_line_buffer);
  3580. }
  3581. }
  3582. /* Stupid comparison routine for qsort () ing strings. */
  3583. static int
  3584. compare_strings (s1, s2)
  3585. char **s1, **s2;
  3586. {
  3587. return (strcmp (*s1, *s2));
  3588. }
  3589. /* A completion function for usernames.
  3590. TEXT contains a partial username preceded by a random
  3591. character (usually `~'). */
  3592. char *
  3593. username_completion_function (text, state)
  3594. int state;
  3595. char *text;
  3596. {
  3597. #if defined(__GO32__) || defined(WIN32)
  3598. return (char *)NULL;
  3599. #else /* !__GO32__ */
  3600. static char *username = (char *)NULL;
  3601. static struct passwd *entry;
  3602. static int namelen, first_char, first_char_loc;
  3603. if (!state)
  3604. {
  3605. if (username)
  3606. free (username);
  3607. first_char = *text;
  3608. if (first_char == '~')
  3609. first_char_loc = 1;
  3610. else
  3611. first_char_loc = 0;
  3612. username = savestring (&text[first_char_loc]);
  3613. namelen = strlen (username);
  3614. setpwent ();
  3615. }
  3616. while (entry = getpwent ())
  3617. {
  3618. if (strncmp (username, entry->pw_name, namelen) == 0)
  3619. break;
  3620. }
  3621. if (!entry)
  3622. {
  3623. endpwent ();
  3624. return ((char *)NULL);
  3625. }
  3626. else
  3627. {
  3628. char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
  3629. *value = *text;
  3630. strcpy (value + first_char_loc, entry->pw_name);
  3631. if (first_char == '~')
  3632. rl_filename_completion_desired = 1;
  3633. return (value);
  3634. }
  3635. #endif /* !__GO32__ */
  3636. }
  3637. /* **************************************************************** */
  3638. /* */
  3639. /* Undo, and Undoing */
  3640. /* */
  3641. /* **************************************************************** */
  3642. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  3643. the undo list. */
  3644. int doing_an_undo = 0;
  3645. /* The current undo list for THE_LINE. */
  3646. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  3647. /* Remember how to undo something. Concatenate some undos if that
  3648. seems right. */
  3649. rl_add_undo (what, start, end, text)
  3650. enum undo_code what;
  3651. int start, end;
  3652. char *text;
  3653. {
  3654. UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  3655. temp->what = what;
  3656. temp->start = start;
  3657. temp->end = end;
  3658. temp->text = text;
  3659. temp->next = rl_undo_list;
  3660. rl_undo_list = temp;
  3661. }
  3662. /* Free the existing undo list. */
  3663. free_undo_list ()
  3664. {
  3665. while (rl_undo_list) {
  3666. UNDO_LIST *release = rl_undo_list;
  3667. rl_undo_list = rl_undo_list->next;
  3668. if (release->what == UNDO_DELETE)
  3669. free (release->text);
  3670. free (release);
  3671. }
  3672. }
  3673. /* Undo the next thing in the list. Return 0 if there
  3674. is nothing to undo, or non-zero if there was. */
  3675. int
  3676. rl_do_undo ()
  3677. {
  3678. UNDO_LIST *release;
  3679. int waiting_for_begin = 0;
  3680. undo_thing:
  3681. if (!rl_undo_list)
  3682. return (0);
  3683. doing_an_undo = 1;
  3684. switch (rl_undo_list->what) {
  3685. /* Undoing deletes means inserting some text. */
  3686. case UNDO_DELETE:
  3687. rl_point = rl_undo_list->start;
  3688. rl_insert_text (rl_undo_list->text);
  3689. free (rl_undo_list->text);
  3690. break;
  3691. /* Undoing inserts means deleting some text. */
  3692. case UNDO_INSERT:
  3693. rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  3694. rl_point = rl_undo_list->start;
  3695. break;
  3696. /* Undoing an END means undoing everything 'til we get to
  3697. a BEGIN. */
  3698. case UNDO_END:
  3699. waiting_for_begin++;
  3700. break;
  3701. /* Undoing a BEGIN means that we are done with this group. */
  3702. case UNDO_BEGIN:
  3703. if (waiting_for_begin)
  3704. waiting_for_begin--;
  3705. else
  3706. abort ();
  3707. break;
  3708. }
  3709. doing_an_undo = 0;
  3710. release = rl_undo_list;
  3711. rl_undo_list = rl_undo_list->next;
  3712. free (release);
  3713. if (waiting_for_begin)
  3714. goto undo_thing;
  3715. return (1);
  3716. }
  3717. /* Begin a group. Subsequent undos are undone as an atomic operation. */
  3718. rl_begin_undo_group ()
  3719. {
  3720. rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  3721. }
  3722. /* End an undo group started with rl_begin_undo_group (). */
  3723. rl_end_undo_group ()
  3724. {
  3725. rl_add_undo (UNDO_END, 0, 0, 0);
  3726. }
  3727. /* Save an undo entry for the text from START to END. */
  3728. rl_modifying (start, end)
  3729. int start, end;
  3730. {
  3731. if (start > end)
  3732. {
  3733. int t = start;
  3734. start = end;
  3735. end = t;
  3736. }
  3737. if (start != end)
  3738. {
  3739. char *temp = rl_copy (start, end);
  3740. rl_begin_undo_group ();
  3741. rl_add_undo (UNDO_DELETE, start, end, temp);
  3742. rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  3743. rl_end_undo_group ();
  3744. }
  3745. }
  3746. /* Revert the current line to its previous state. */
  3747. rl_revert_line ()
  3748. {
  3749. if (!rl_undo_list) ding ();
  3750. else {
  3751. while (rl_undo_list)
  3752. rl_do_undo ();
  3753. }
  3754. }
  3755. /* Do some undoing of things that were done. */
  3756. rl_undo_command (count)
  3757. {
  3758. if (count < 0) return; /* Nothing to do. */
  3759. while (count)
  3760. {
  3761. if (rl_do_undo ())
  3762. {
  3763. count--;
  3764. }
  3765. else
  3766. {
  3767. ding ();
  3768. break;
  3769. }
  3770. }
  3771. }
  3772. /* **************************************************************** */
  3773. /* */
  3774. /* History Utilities */
  3775. /* */
  3776. /* **************************************************************** */
  3777. /* We already have a history library, and that is what we use to control
  3778. the history features of readline. However, this is our local interface
  3779. to the history mechanism. */
  3780. /* While we are editing the history, this is the saved
  3781. version of the original line. */
  3782. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  3783. /* Set the history pointer back to the last entry in the history. */
  3784. start_using_history ()
  3785. {
  3786. using_history ();
  3787. if (saved_line_for_history)
  3788. free_history_entry (saved_line_for_history);
  3789. saved_line_for_history = (HIST_ENTRY *)NULL;
  3790. }
  3791. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  3792. free_history_entry (entry)
  3793. HIST_ENTRY *entry;
  3794. {
  3795. if (!entry) return;
  3796. if (entry->line)
  3797. free (entry->line);
  3798. free (entry);
  3799. }
  3800. /* Perhaps put back the current line if it has changed. */
  3801. maybe_replace_line ()
  3802. {
  3803. HIST_ENTRY *temp = current_history ();
  3804. /* If the current line has changed, save the changes. */
  3805. if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  3806. {
  3807. temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  3808. free (temp->line);
  3809. free (temp);
  3810. }
  3811. }
  3812. /* Put back the saved_line_for_history if there is one. */
  3813. maybe_unsave_line ()
  3814. {
  3815. if (saved_line_for_history)
  3816. {
  3817. int line_len;
  3818. line_len = strlen (saved_line_for_history->line);
  3819. if (line_len >= rl_line_buffer_len)
  3820. rl_extend_line_buffer (line_len);
  3821. strcpy (the_line, saved_line_for_history->line);
  3822. rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  3823. free_history_entry (saved_line_for_history);
  3824. saved_line_for_history = (HIST_ENTRY *)NULL;
  3825. rl_end = rl_point = strlen (the_line);
  3826. }
  3827. else
  3828. ding ();
  3829. }
  3830. /* Save the current line in saved_line_for_history. */
  3831. maybe_save_line ()
  3832. {
  3833. if (!saved_line_for_history)
  3834. {
  3835. saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  3836. saved_line_for_history->line = savestring (the_line);
  3837. saved_line_for_history->data = (char *)rl_undo_list;
  3838. }
  3839. }
  3840. /* **************************************************************** */
  3841. /* */
  3842. /* History Commands */
  3843. /* */
  3844. /* **************************************************************** */
  3845. /* Meta-< goes to the start of the history. */
  3846. rl_beginning_of_history ()
  3847. {
  3848. rl_get_previous_history (1 + where_history ());
  3849. }
  3850. /* Meta-> goes to the end of the history. (The current line). */
  3851. rl_end_of_history ()
  3852. {
  3853. maybe_replace_line ();
  3854. using_history ();
  3855. maybe_unsave_line ();
  3856. }
  3857. /* Move down to the next history line. */
  3858. rl_get_next_history (count)
  3859. int count;
  3860. {
  3861. HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  3862. if (count < 0)
  3863. {
  3864. rl_get_previous_history (-count);
  3865. return;
  3866. }
  3867. if (!count)
  3868. return;
  3869. maybe_replace_line ();
  3870. while (count)
  3871. {
  3872. temp = next_history ();
  3873. if (!temp)
  3874. break;
  3875. --count;
  3876. }
  3877. if (!temp)
  3878. maybe_unsave_line ();
  3879. else
  3880. {
  3881. int line_len;
  3882. line_len = strlen (temp->line);
  3883. if (line_len >= rl_line_buffer_len)
  3884. rl_extend_line_buffer (line_len);
  3885. strcpy (the_line, temp->line);
  3886. rl_undo_list = (UNDO_LIST *)temp->data;
  3887. rl_end = rl_point = strlen (the_line);
  3888. #if defined (VI_MODE)
  3889. if (rl_editing_mode == vi_mode)
  3890. rl_point = 0;
  3891. #endif /* VI_MODE */
  3892. }
  3893. }
  3894. /* Get the previous item out of our interactive history, making it the current
  3895. line. If there is no previous history, just ding. */
  3896. rl_get_previous_history (count)
  3897. int count;
  3898. {
  3899. HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  3900. HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  3901. if (count < 0)
  3902. {
  3903. rl_get_next_history (-count);
  3904. return;
  3905. }
  3906. if (!count)
  3907. return;
  3908. /* If we don't have a line saved, then save this one. */
  3909. maybe_save_line ();
  3910. /* If the current line has changed, save the changes. */
  3911. maybe_replace_line ();
  3912. while (count)
  3913. {
  3914. temp = previous_history ();
  3915. if (!temp)
  3916. break;
  3917. else
  3918. old_temp = temp;
  3919. --count;
  3920. }
  3921. /* If there was a large argument, and we moved back to the start of the
  3922. history, that is not an error. So use the last value found. */
  3923. if (!temp && old_temp)
  3924. temp = old_temp;
  3925. if (!temp)
  3926. ding ();
  3927. else
  3928. {
  3929. int line_len;
  3930. line_len = strlen (temp->line);
  3931. if (line_len >= rl_line_buffer_len)
  3932. rl_extend_line_buffer (line_len);
  3933. strcpy (the_line, temp->line);
  3934. rl_undo_list = (UNDO_LIST *)temp->data;
  3935. rl_end = rl_point = line_len;
  3936. #if defined (VI_MODE)
  3937. if (rl_editing_mode == vi_mode)
  3938. rl_point = 0;
  3939. #endif /* VI_MODE */
  3940. }
  3941. }
  3942. /* **************************************************************** */
  3943. /* */
  3944. /* I-Search and Searching */
  3945. /* */
  3946. /* **************************************************************** */
  3947. /* Search backwards through the history looking for a string which is typed
  3948. interactively. Start with the current line. */
  3949. rl_reverse_search_history (sign, key)
  3950. int sign;
  3951. int key;
  3952. {
  3953. rl_search_history (-sign, key);
  3954. }
  3955. /* Search forwards through the history looking for a string which is typed
  3956. interactively. Start with the current line. */
  3957. rl_forward_search_history (sign, key)
  3958. int sign;
  3959. int key;
  3960. {
  3961. rl_search_history (sign, key);
  3962. }
  3963. /* Display the current state of the search in the echo-area.
  3964. SEARCH_STRING contains the string that is being searched for,
  3965. DIRECTION is zero for forward, or 1 for reverse,
  3966. WHERE is the history list number of the current line. If it is
  3967. -1, then this line is the starting one. */
  3968. rl_display_search (search_string, reverse_p, where)
  3969. char *search_string;
  3970. int reverse_p, where;
  3971. {
  3972. char *message = (char *)NULL;
  3973. #ifdef WIN32
  3974. char dummy[5];
  3975. dummy[0] = 0;
  3976. #endif
  3977. message =
  3978. (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
  3979. *message = '\0';
  3980. #if defined (NOTDEF)
  3981. if (where != -1)
  3982. sprintf (message, "[%d]", where + history_base);
  3983. #endif /* NOTDEF */
  3984. strcat (message, "(");
  3985. if (reverse_p)
  3986. strcat (message, "reverse-");
  3987. strcat (message, "i-search)`");
  3988. if (search_string)
  3989. strcat (message, search_string);
  3990. strcat (message, "': ");
  3991. rl_message (message, 0, 0);
  3992. rl_redisplay ();
  3993. }
  3994. /* Search through the history looking for an interactively typed string.
  3995. This is analogous to i-search. We start the search in the current line.
  3996. DIRECTION is which direction to search; >= 0 means forward, < 0 means
  3997. backwards. */
  3998. rl_search_history (direction, invoking_key)
  3999. int direction;
  4000. int invoking_key;
  4001. {
  4002. /* The string that the user types in to search for. */
  4003. char *search_string = (char *)alloca (128);
  4004. /* The current length of SEARCH_STRING. */
  4005. int search_string_index;
  4006. /* The list of lines to search through. */
  4007. char **lines;
  4008. /* The length of LINES. */
  4009. int hlen;
  4010. /* Where we get LINES from. */
  4011. HIST_ENTRY **hlist = history_list ();
  4012. register int i = 0;
  4013. int orig_point = rl_point;
  4014. int orig_line = where_history ();
  4015. int last_found_line = orig_line;
  4016. int c, done = 0;
  4017. /* The line currently being searched. */
  4018. char *sline;
  4019. /* Offset in that line. */
  4020. int index;
  4021. /* Non-zero if we are doing a reverse search. */
  4022. int reverse = (direction < 0);
  4023. /* Create an arrary of pointers to the lines that we want to search. */
  4024. maybe_replace_line ();
  4025. if (hlist)
  4026. for (i = 0; hlist[i]; i++);
  4027. /* Allocate space for this many lines, +1 for the current input line,
  4028. and remember those lines. */
  4029. lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
  4030. for (i = 0; i < hlen; i++)
  4031. lines[i] = hlist[i]->line;
  4032. if (saved_line_for_history)
  4033. lines[i] = saved_line_for_history->line;
  4034. else
  4035. /* So I have to type it in this way instead. */
  4036. {
  4037. char *alloced_line;
  4038. /* Keep that mips alloca happy. */
  4039. alloced_line = (char *)alloca (1 + strlen (the_line));
  4040. lines[i] = alloced_line;
  4041. strcpy (lines[i], &the_line[0]);
  4042. }
  4043. hlen++;
  4044. /* The line where we start the search. */
  4045. i = orig_line;
  4046. /* Initialize search parameters. */
  4047. *search_string = '\0';
  4048. search_string_index = 0;
  4049. /* Normalize DIRECTION into 1 or -1. */
  4050. if (direction >= 0)
  4051. direction = 1;
  4052. else
  4053. direction = -1;
  4054. rl_display_search (search_string, reverse, -1);
  4055. sline = the_line;
  4056. index = rl_point;
  4057. while (!done)
  4058. {
  4059. c = rl_read_key ();
  4060. /* Hack C to Do What I Mean. */
  4061. {
  4062. Function *f = (Function *)NULL;
  4063. if (keymap[c].type == ISFUNC)
  4064. {
  4065. f = keymap[c].function;
  4066. if (f == rl_reverse_search_history)
  4067. c = reverse ? -1 : -2;
  4068. else if (f == rl_forward_search_history)
  4069. c = !reverse ? -1 : -2;
  4070. }
  4071. }
  4072. switch (c)
  4073. {
  4074. case ESC:
  4075. done = 1;
  4076. continue;
  4077. /* case invoking_key: */
  4078. case -1:
  4079. goto search_again;
  4080. /* switch directions */
  4081. case -2:
  4082. direction = -direction;
  4083. reverse = (direction < 0);
  4084. goto do_search;
  4085. case CTRL ('G'):
  4086. strcpy (the_line, lines[orig_line]);
  4087. rl_point = orig_point;
  4088. rl_end = strlen (the_line);
  4089. rl_clear_message ();
  4090. return;
  4091. default:
  4092. if (c < 32 || c > 126)
  4093. {
  4094. rl_execute_next (c);
  4095. done = 1;
  4096. continue;
  4097. }
  4098. else
  4099. {
  4100. search_string[search_string_index++] = c;
  4101. search_string[search_string_index] = '\0';
  4102. goto do_search;
  4103. search_again:
  4104. if (!search_string_index)
  4105. continue;
  4106. else
  4107. {
  4108. if (reverse)
  4109. --index;
  4110. else
  4111. if (index != strlen (sline))
  4112. ++index;
  4113. else
  4114. ding ();
  4115. }
  4116. do_search:
  4117. while (1)
  4118. {
  4119. if (reverse)
  4120. {
  4121. while (index >= 0)
  4122. if (strncmp
  4123. (search_string, sline + index, search_string_index)
  4124. == 0)
  4125. goto string_found;
  4126. else
  4127. index--;
  4128. }
  4129. else
  4130. {
  4131. register int limit =
  4132. (strlen (sline) - search_string_index) + 1;
  4133. while (index < limit)
  4134. {
  4135. if (strncmp (search_string,
  4136. sline + index,
  4137. search_string_index) == 0)
  4138. goto string_found;
  4139. index++;
  4140. }
  4141. }
  4142. next_line:
  4143. i += direction;
  4144. /* At limit for direction? */
  4145. if ((reverse && i < 0) ||
  4146. (!reverse && i == hlen))
  4147. goto search_failed;
  4148. sline = lines[i];
  4149. if (reverse)
  4150. index = strlen (sline);
  4151. else
  4152. index = 0;
  4153. /* If the search string is longer than the current
  4154. line, no match. */
  4155. if (search_string_index > strlen (sline))
  4156. goto next_line;
  4157. /* Start actually searching. */
  4158. if (reverse)
  4159. index -= search_string_index;
  4160. }
  4161. search_failed:
  4162. /* We cannot find the search string. Ding the bell. */
  4163. ding ();
  4164. i = last_found_line;
  4165. break;
  4166. string_found:
  4167. /* We have found the search string. Just display it. But don't
  4168. actually move there in the history list until the user accepts
  4169. the location. */
  4170. {
  4171. int line_len;
  4172. line_len = strlen (lines[i]);
  4173. if (line_len >= rl_line_buffer_len)
  4174. rl_extend_line_buffer (line_len);
  4175. strcpy (the_line, lines[i]);
  4176. rl_point = index;
  4177. rl_end = line_len;
  4178. last_found_line = i;
  4179. rl_display_search
  4180. (search_string, reverse, (i == orig_line) ? -1 : i);
  4181. }
  4182. }
  4183. }
  4184. continue;
  4185. }
  4186. /* The searching is over. The user may have found the string that she
  4187. was looking for, or else she may have exited a failing search. If
  4188. INDEX is -1, then that shows that the string searched for was not
  4189. found. We use this to determine where to place rl_point. */
  4190. {
  4191. int now = last_found_line;
  4192. /* First put back the original state. */
  4193. strcpy (the_line, lines[orig_line]);
  4194. if (now < orig_line)
  4195. rl_get_previous_history (orig_line - now);
  4196. else
  4197. rl_get_next_history (now - orig_line);
  4198. /* If the index of the "matched" string is less than zero, then the
  4199. final search string was never matched, so put point somewhere
  4200. reasonable. */
  4201. if (index < 0)
  4202. index = strlen (the_line);
  4203. rl_point = index;
  4204. rl_clear_message ();
  4205. }
  4206. }
  4207. /* Make C be the next command to be executed. */
  4208. rl_execute_next (c)
  4209. int c;
  4210. {
  4211. rl_pending_input = c;
  4212. }
  4213. /* **************************************************************** */
  4214. /* */
  4215. /* Killing Mechanism */
  4216. /* */
  4217. /* **************************************************************** */
  4218. /* What we assume for a max number of kills. */
  4219. #define DEFAULT_MAX_KILLS 10
  4220. /* The real variable to look at to find out when to flush kills. */
  4221. int rl_max_kills = DEFAULT_MAX_KILLS;
  4222. /* Where to store killed text. */
  4223. char **rl_kill_ring = (char **)NULL;
  4224. /* Where we are in the kill ring. */
  4225. int rl_kill_index = 0;
  4226. /* How many slots we have in the kill ring. */
  4227. int rl_kill_ring_length = 0;
  4228. /* How to say that you only want to save a certain amount
  4229. of kill material. */
  4230. rl_set_retained_kills (num)
  4231. int num;
  4232. {}
  4233. /* The way to kill something. This appends or prepends to the last
  4234. kill, if the last command was a kill command. if FROM is less
  4235. than TO, then the text is appended, otherwise prepended. If the
  4236. last command was not a kill command, then a new slot is made for
  4237. this kill. */
  4238. rl_kill_text (from, to)
  4239. int from, to;
  4240. {
  4241. int slot;
  4242. char *text = rl_copy (from, to);
  4243. /* Is there anything to kill? */
  4244. if (from == to)
  4245. {
  4246. free (text);
  4247. last_command_was_kill++;
  4248. return;
  4249. }
  4250. /* Delete the copied text from the line. */
  4251. rl_delete_text (from, to);
  4252. /* First, find the slot to work with. */
  4253. if (!last_command_was_kill)
  4254. {
  4255. /* Get a new slot. */
  4256. if (!rl_kill_ring)
  4257. {
  4258. /* If we don't have any defined, then make one. */
  4259. rl_kill_ring = (char **)
  4260. xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  4261. slot = 1;
  4262. }
  4263. else
  4264. {
  4265. /* We have to add a new slot on the end, unless we have
  4266. exceeded the max limit for remembering kills. */
  4267. slot = rl_kill_ring_length;
  4268. if (slot == rl_max_kills)
  4269. {
  4270. register int i;
  4271. free (rl_kill_ring[0]);
  4272. for (i = 0; i < slot; i++)
  4273. rl_kill_ring[i] = rl_kill_ring[i + 1];
  4274. }
  4275. else
  4276. {
  4277. rl_kill_ring =
  4278. (char **)
  4279. xrealloc (rl_kill_ring,
  4280. ((slot = (rl_kill_ring_length += 1)) + 1)
  4281. * sizeof (char *));
  4282. }
  4283. }
  4284. slot--;
  4285. }
  4286. else
  4287. {
  4288. slot = rl_kill_ring_length - 1;
  4289. }
  4290. /* If the last command was a kill, prepend or append. */
  4291. if (last_command_was_kill && rl_editing_mode != vi_mode)
  4292. {
  4293. char *old = rl_kill_ring[slot];
  4294. char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
  4295. if (from < to)
  4296. {
  4297. strcpy (new, old);
  4298. strcat (new, text);
  4299. }
  4300. else
  4301. {
  4302. strcpy (new, text);
  4303. strcat (new, old);
  4304. }
  4305. free (old);
  4306. free (text);
  4307. rl_kill_ring[slot] = new;
  4308. }
  4309. else
  4310. {
  4311. rl_kill_ring[slot] = text;
  4312. }
  4313. rl_kill_index = slot;
  4314. last_command_was_kill++;
  4315. }
  4316. /* Now REMEMBER! In order to do prepending or appending correctly, kill
  4317. commands always make rl_point's original position be the FROM argument,
  4318. and rl_point's extent be the TO argument. */
  4319. /* **************************************************************** */
  4320. /* */
  4321. /* Killing Commands */
  4322. /* */
  4323. /* **************************************************************** */
  4324. /* Delete the word at point, saving the text in the kill ring. */
  4325. rl_kill_word (count)
  4326. int count;
  4327. {
  4328. int orig_point = rl_point;
  4329. if (count < 0)
  4330. rl_backward_kill_word (-count);
  4331. else
  4332. {
  4333. rl_forward_word (count);
  4334. if (rl_point != orig_point)
  4335. rl_kill_text (orig_point, rl_point);
  4336. rl_point = orig_point;
  4337. }
  4338. }
  4339. /* Rubout the word before point, placing it on the kill ring. */
  4340. rl_backward_kill_word (count)
  4341. int count;
  4342. {
  4343. int orig_point = rl_point;
  4344. if (count < 0)
  4345. rl_kill_word (-count);
  4346. else
  4347. {
  4348. rl_backward_word (count);
  4349. if (rl_point != orig_point)
  4350. rl_kill_text (orig_point, rl_point);
  4351. }
  4352. }
  4353. /* Kill from here to the end of the line. If DIRECTION is negative, kill
  4354. back to the line start instead. */
  4355. rl_kill_line (direction)
  4356. int direction;
  4357. {
  4358. int orig_point = rl_point;
  4359. if (direction < 0)
  4360. rl_backward_kill_line (1);
  4361. else
  4362. {
  4363. rl_end_of_line ();
  4364. if (orig_point != rl_point)
  4365. rl_kill_text (orig_point, rl_point);
  4366. rl_point = orig_point;
  4367. }
  4368. }
  4369. /* Kill backwards to the start of the line. If DIRECTION is negative, kill
  4370. forwards to the line end instead. */
  4371. rl_backward_kill_line (direction)
  4372. int direction;
  4373. {
  4374. int orig_point = rl_point;
  4375. if (direction < 0)
  4376. rl_kill_line (1);
  4377. else
  4378. {
  4379. if (!rl_point)
  4380. ding ();
  4381. else
  4382. {
  4383. rl_beg_of_line ();
  4384. rl_kill_text (orig_point, rl_point);
  4385. }
  4386. }
  4387. }
  4388. /* Yank back the last killed text. This ignores arguments. */
  4389. rl_yank ()
  4390. {
  4391. if (!rl_kill_ring) rl_abort ();
  4392. rl_insert_text (rl_kill_ring[rl_kill_index]);
  4393. }
  4394. /* If the last command was yank, or yank_pop, and the text just
  4395. before point is identical to the current kill item, then
  4396. delete that text from the line, rotate the index down, and
  4397. yank back some other text. */
  4398. rl_yank_pop ()
  4399. {
  4400. int l;
  4401. if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  4402. !rl_kill_ring)
  4403. {
  4404. rl_abort ();
  4405. }
  4406. l = strlen (rl_kill_ring[rl_kill_index]);
  4407. if (((rl_point - l) >= 0) &&
  4408. (strncmp (the_line + (rl_point - l),
  4409. rl_kill_ring[rl_kill_index], l) == 0))
  4410. {
  4411. rl_delete_text ((rl_point - l), rl_point);
  4412. rl_point -= l;
  4413. rl_kill_index--;
  4414. if (rl_kill_index < 0)
  4415. rl_kill_index = rl_kill_ring_length - 1;
  4416. rl_yank ();
  4417. }
  4418. else
  4419. rl_abort ();
  4420. }
  4421. /* Yank the COUNTth argument from the previous history line. */
  4422. rl_yank_nth_arg (count, ignore)
  4423. int count;
  4424. {
  4425. register HIST_ENTRY *entry = previous_history ();
  4426. char *arg;
  4427. if (entry)
  4428. next_history ();
  4429. else
  4430. {
  4431. ding ();
  4432. return;
  4433. }
  4434. arg = history_arg_extract (count, count, entry->line);
  4435. if (!arg || !*arg)
  4436. {
  4437. ding ();
  4438. return;
  4439. }
  4440. rl_begin_undo_group ();
  4441. #if defined (VI_MODE)
  4442. /* Vi mode always inserts a space befoe yanking the argument, and it
  4443. inserts it right *after* rl_point. */
  4444. if (rl_editing_mode == vi_mode)
  4445. rl_point++;
  4446. #endif /* VI_MODE */
  4447. if (rl_point && the_line[rl_point - 1] != ' ')
  4448. rl_insert_text (" ");
  4449. rl_insert_text (arg);
  4450. free (arg);
  4451. rl_end_undo_group ();
  4452. }
  4453. /* How to toggle back and forth between editing modes. */
  4454. rl_vi_editing_mode ()
  4455. {
  4456. #if defined (VI_MODE)
  4457. rl_editing_mode = vi_mode;
  4458. rl_vi_insertion_mode ();
  4459. #endif /* VI_MODE */
  4460. }
  4461. rl_emacs_editing_mode ()
  4462. {
  4463. rl_editing_mode = emacs_mode;
  4464. keymap = emacs_standard_keymap;
  4465. }
  4466. /* **************************************************************** */
  4467. /* */
  4468. /* Completion */
  4469. /* */
  4470. /* **************************************************************** */
  4471. /* Non-zero means that case is not significant in completion. */
  4472. int completion_case_fold = 0;
  4473. /* Return an array of (char *) which is a list of completions for TEXT.
  4474. If there are no completions, return a NULL pointer.
  4475. The first entry in the returned array is the substitution for TEXT.
  4476. The remaining entries are the possible completions.
  4477. The array is terminated with a NULL pointer.
  4478. ENTRY_FUNCTION is a function of two args, and returns a (char *).
  4479. The first argument is TEXT.
  4480. The second is a state argument; it should be zero on the first call, and
  4481. non-zero on subsequent calls. It returns a NULL pointer to the caller
  4482. when there are no more matches.
  4483. */
  4484. char **
  4485. completion_matches (text, entry_function)
  4486. char *text;
  4487. char *(*entry_function) ();
  4488. {
  4489. /* Number of slots in match_list. */
  4490. int match_list_size;
  4491. /* The list of matches. */
  4492. char **match_list =
  4493. (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  4494. /* Number of matches actually found. */
  4495. int matches = 0;
  4496. /* Temporary string binder. */
  4497. char *string;
  4498. match_list[1] = (char *)NULL;
  4499. while (string = (*entry_function) (text, matches))
  4500. {
  4501. if (matches + 1 == match_list_size)
  4502. match_list = (char **)xrealloc
  4503. (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  4504. match_list[++matches] = string;
  4505. match_list[matches + 1] = (char *)NULL;
  4506. }
  4507. /* If there were any matches, then look through them finding out the
  4508. lowest common denominator. That then becomes match_list[0]. */
  4509. if (matches)
  4510. {
  4511. register int i = 1;
  4512. int low = 100000; /* Count of max-matched characters. */
  4513. /* If only one match, just use that. */
  4514. if (matches == 1)
  4515. {
  4516. match_list[0] = match_list[1];
  4517. match_list[1] = (char *)NULL;
  4518. }
  4519. else
  4520. {
  4521. /* Otherwise, compare each member of the list with
  4522. the next, finding out where they stop matching. */
  4523. while (i < matches)
  4524. {
  4525. register int c1, c2, si;
  4526. if (completion_case_fold)
  4527. {
  4528. for (si = 0;
  4529. (c1 = to_lower(match_list[i][si])) &&
  4530. (c2 = to_lower(match_list[i + 1][si]));
  4531. si++)
  4532. if (c1 != c2) break;
  4533. }
  4534. else
  4535. {
  4536. for (si = 0;
  4537. (c1 = match_list[i][si]) &&
  4538. (c2 = match_list[i + 1][si]);
  4539. si++)
  4540. if (c1 != c2) break;
  4541. }
  4542. if (low > si) low = si;
  4543. i++;
  4544. }
  4545. match_list[0] = (char *)xmalloc (low + 1);
  4546. strncpy (match_list[0], match_list[1], low);
  4547. match_list[0][low] = '\0';
  4548. }
  4549. }
  4550. else /* There were no matches. */
  4551. {
  4552. free (match_list);
  4553. match_list = (char **)NULL;
  4554. }
  4555. return (match_list);
  4556. }
  4557. /* Okay, now we write the entry_function for filename completion. In the
  4558. general case. Note that completion in the shell is a little different
  4559. because of all the pathnames that must be followed when looking up the
  4560. completion for a command. */
  4561. char *
  4562. filename_completion_function (text, state)
  4563. int state;
  4564. char *text;
  4565. {
  4566. #if !defined(__MWERKS__) && !defined(_MSC_VER)
  4567. static DIR *directory;
  4568. static char *filename = (char *)NULL;
  4569. static char *dirname = (char *)NULL;
  4570. static char *users_dirname = (char *)NULL;
  4571. static int filename_len;
  4572. dirent *entry = (dirent *)NULL;
  4573. /* If we don't have any state, then do some initialization. */
  4574. if (!state)
  4575. {
  4576. char *temp;
  4577. if (dirname) free (dirname);
  4578. if (filename) free (filename);
  4579. if (users_dirname) free (users_dirname);
  4580. filename = savestring (text);
  4581. if (!*text) text = ".";
  4582. dirname = savestring (text);
  4583. temp = rindex (dirname, '/');
  4584. if (temp)
  4585. {
  4586. strcpy (filename, ++temp);
  4587. *temp = '\0';
  4588. }
  4589. else
  4590. strcpy (dirname, ".");
  4591. /* We aren't done yet. We also support the "~user" syntax. */
  4592. /* Save the version of the directory that the user typed. */
  4593. users_dirname = savestring (dirname);
  4594. {
  4595. char *temp_dirname;
  4596. temp_dirname = tilde_expand (dirname);
  4597. free (dirname);
  4598. dirname = temp_dirname;
  4599. if (rl_symbolic_link_hook)
  4600. (*rl_symbolic_link_hook) (&dirname);
  4601. }
  4602. directory = opendir (dirname);
  4603. filename_len = strlen (filename);
  4604. rl_filename_completion_desired = 1;
  4605. }
  4606. /* At this point we should entertain the possibility of hacking wildcarded
  4607. filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
  4608. contains globbing characters, then build an array of directories to
  4609. glob on, and glob on the first one. */
  4610. /* Now that we have some state, we can read the directory. */
  4611. while (directory && (entry = readdir (directory)))
  4612. {
  4613. /* Special case for no filename.
  4614. All entries except "." and ".." match. */
  4615. if (!filename_len)
  4616. {
  4617. if ((strcmp (entry->d_name, ".") != 0) &&
  4618. (strcmp (entry->d_name, "..") != 0))
  4619. break;
  4620. }
  4621. else
  4622. {
  4623. /* Otherwise, if these match upto the length of filename, then
  4624. it is a match. */
  4625. if (entry->d_name[0] == filename[0] && /* Quick test */
  4626. (strncmp (filename, entry->d_name, filename_len) == 0))
  4627. {
  4628. break;
  4629. }
  4630. }
  4631. }
  4632. if (!entry)
  4633. {
  4634. if (directory)
  4635. {
  4636. closedir (directory);
  4637. directory = (DIR *)NULL;
  4638. }
  4639. return (char *)NULL;
  4640. }
  4641. else
  4642. {
  4643. char *temp;
  4644. if (dirname && (strcmp (dirname, ".") != 0))
  4645. {
  4646. temp = (char *)
  4647. xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
  4648. strcpy (temp, users_dirname);
  4649. strcat (temp, entry->d_name);
  4650. }
  4651. else
  4652. {
  4653. temp = (savestring (entry->d_name));
  4654. }
  4655. return (temp);
  4656. }
  4657. #endif
  4658. return 0;
  4659. }
  4660. /* **************************************************************** */
  4661. /* */
  4662. /* Binding keys */
  4663. /* */
  4664. /* **************************************************************** */
  4665. /* rl_add_defun (char *name, Function *function, int key)
  4666. Add NAME to the list of named functions. Make FUNCTION
  4667. be the function that gets called.
  4668. If KEY is not -1, then bind it. */
  4669. rl_add_defun (name, function, key)
  4670. char *name;
  4671. Function *function;
  4672. int key;
  4673. {
  4674. if (key != -1)
  4675. rl_bind_key (key, function);
  4676. rl_add_funmap_entry (name, function);
  4677. }
  4678. /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
  4679. int
  4680. rl_bind_key (key, function)
  4681. int key;
  4682. Function *function;
  4683. {
  4684. if (key < 0)
  4685. return (key);
  4686. if (key > 127 && key < 256)
  4687. {
  4688. if (keymap[ESC].type == ISKMAP)
  4689. {
  4690. Keymap escmap = (Keymap)keymap[ESC].function;
  4691. key -= 128;
  4692. escmap[key].type = ISFUNC;
  4693. escmap[key].function = function;
  4694. return (0);
  4695. }
  4696. return (key);
  4697. }
  4698. keymap[key].type = ISFUNC;
  4699. keymap[key].function = function;
  4700. return (0);
  4701. }
  4702. /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
  4703. KEY. */
  4704. int
  4705. rl_bind_key_in_map (key, function, map)
  4706. int key;
  4707. Function *function;
  4708. Keymap map;
  4709. {
  4710. int result;
  4711. Keymap oldmap = keymap;
  4712. keymap = map;
  4713. result = rl_bind_key (key, function);
  4714. keymap = oldmap;
  4715. return (result);
  4716. }
  4717. /* Make KEY do nothing in the currently selected keymap.
  4718. Returns non-zero in case of error. */
  4719. int
  4720. rl_unbind_key (key)
  4721. int key;
  4722. {
  4723. return (rl_bind_key (key, (Function *)NULL));
  4724. }
  4725. /* Make KEY do nothing in MAP.
  4726. Returns non-zero in case of error. */
  4727. int
  4728. rl_unbind_key_in_map (key, map)
  4729. int key;
  4730. Keymap map;
  4731. {
  4732. return (rl_bind_key_in_map (key, (Function *)NULL, map));
  4733. }
  4734. /* Bind the key sequence represented by the string KEYSEQ to
  4735. FUNCTION. This makes new keymaps as necessary. The initial
  4736. place to do bindings is in MAP. */
  4737. rl_set_key (keyseq, function, map)
  4738. char *keyseq;
  4739. Function *function;
  4740. Keymap map;
  4741. {
  4742. rl_generic_bind (ISFUNC, keyseq, function, map);
  4743. }
  4744. /* Bind the key sequence represented by the string KEYSEQ to
  4745. the string of characters MACRO. This makes new keymaps as
  4746. necessary. The initial place to do bindings is in MAP. */
  4747. rl_macro_bind (keyseq, macro, map)
  4748. char *keyseq, *macro;
  4749. Keymap map;
  4750. {
  4751. char *macro_keys;
  4752. int macro_keys_len;
  4753. macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
  4754. if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
  4755. {
  4756. free (macro_keys);
  4757. return;
  4758. }
  4759. rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  4760. }
  4761. /* Bind the key sequence represented by the string KEYSEQ to
  4762. the arbitrary pointer DATA. TYPE says what kind of data is
  4763. pointed to by DATA, right now this can be a function (ISFUNC),
  4764. a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
  4765. as necessary. The initial place to do bindings is in MAP. */
  4766. static void
  4767. rl_generic_bind (type, keyseq, data, map)
  4768. int type;
  4769. char *keyseq, *data;
  4770. Keymap map;
  4771. {
  4772. char *keys;
  4773. int keys_len;
  4774. register int i;
  4775. /* If no keys to bind to, exit right away. */
  4776. if (!keyseq || !*keyseq)
  4777. {
  4778. if (type == ISMACR)
  4779. free (data);
  4780. return;
  4781. }
  4782. keys = (char *)alloca (1 + (2 * strlen (keyseq)));
  4783. /* Translate the ASCII representation of KEYSEQ into an array
  4784. of characters. Stuff the characters into ARRAY, and the
  4785. length of ARRAY into LENGTH. */
  4786. if (rl_translate_keyseq (keyseq, keys, &keys_len))
  4787. return;
  4788. /* Bind keys, making new keymaps as necessary. */
  4789. for (i = 0; i < keys_len; i++)
  4790. {
  4791. if (i + 1 < keys_len)
  4792. {
  4793. if (map[keys[i]].type != ISKMAP)
  4794. {
  4795. if (map[i].type == ISMACR)
  4796. free ((char *)map[i].function);
  4797. map[keys[i]].type = ISKMAP;
  4798. map[keys[i]].function = (Function *)rl_make_bare_keymap ();
  4799. }
  4800. map = (Keymap)map[keys[i]].function;
  4801. }
  4802. else
  4803. {
  4804. if (map[keys[i]].type == ISMACR)
  4805. free ((char *)map[keys[i]].function);
  4806. map[keys[i]].function = (Function *)data;
  4807. map[keys[i]].type = type;
  4808. }
  4809. }
  4810. }
  4811. /* Translate the ASCII representation of SEQ, stuffing the
  4812. values into ARRAY, an array of characters. LEN gets the
  4813. final length of ARRAY. Return non-zero if there was an
  4814. error parsing SEQ. */
  4815. rl_translate_keyseq (seq, array, len)
  4816. char *seq, *array;
  4817. int *len;
  4818. {
  4819. register int i, c, l = 0;
  4820. for (i = 0; c = seq[i]; i++)
  4821. {
  4822. if (c == '\\')
  4823. {
  4824. c = seq[++i];
  4825. if (!c)
  4826. break;
  4827. if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
  4828. (c == 'e'))
  4829. {
  4830. /* Handle special case of backwards define. */
  4831. if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  4832. {
  4833. array[l++] = ESC;
  4834. i += 5;
  4835. array[l++] = CTRL (to_upper (seq[i]));
  4836. if (!seq[i])
  4837. i--;
  4838. continue;
  4839. }
  4840. switch (c)
  4841. {
  4842. case 'M':
  4843. i++;
  4844. array[l++] = ESC;
  4845. break;
  4846. case 'C':
  4847. i += 2;
  4848. /* Special hack for C-?... */
  4849. if (seq[i] == '?')
  4850. array[l++] = RUBOUT;
  4851. else
  4852. array[l++] = CTRL (to_upper (seq[i]));
  4853. break;
  4854. case 'e':
  4855. array[l++] = ESC;
  4856. }
  4857. continue;
  4858. }
  4859. }
  4860. array[l++] = c;
  4861. }
  4862. *len = l;
  4863. array[l] = '\0';
  4864. return (0);
  4865. }
  4866. /* Return a pointer to the function that STRING represents.
  4867. If STRING doesn't have a matching function, then a NULL pointer
  4868. is returned. */
  4869. Function *
  4870. rl_named_function (string)
  4871. char *string;
  4872. {
  4873. register int i;
  4874. for (i = 0; funmap[i]; i++)
  4875. if (stricmp (funmap[i]->name, string) == 0)
  4876. return (funmap[i]->function);
  4877. return ((Function *)NULL);
  4878. }
  4879. /* The last key bindings file read. */
  4880. #ifdef __MSDOS__
  4881. /* Don't know what to do, but this is a guess */
  4882. static char *last_readline_init_file = "/INPUTRC";
  4883. #else
  4884. static char *last_readline_init_file = "~/.inputrc";
  4885. #endif
  4886. /* Re-read the current keybindings file. */
  4887. rl_re_read_init_file (count, ignore)
  4888. int count, ignore;
  4889. {
  4890. rl_read_init_file ((char *)NULL);
  4891. }
  4892. /* Do key bindings from a file. If FILENAME is NULL it defaults
  4893. to `~/.inputrc'. If the file existed and could be opened and
  4894. read, 0 is returned, otherwise errno is returned. */
  4895. int
  4896. rl_read_init_file (filename)
  4897. char *filename;
  4898. {
  4899. register int i;
  4900. char *buffer, *openname, *line, *end;
  4901. struct stat finfo;
  4902. int file;
  4903. /* Default the filename. */
  4904. if (!filename)
  4905. filename = last_readline_init_file;
  4906. openname = tilde_expand (filename);
  4907. if (!openname || *openname == '\000')
  4908. return ENOENT;
  4909. if ((stat (openname, &finfo) < 0) ||
  4910. (file = open (openname, O_RDONLY, 0666)) < 0)
  4911. {
  4912. free (openname);
  4913. return (errno);
  4914. }
  4915. else
  4916. free (openname);
  4917. last_readline_init_file = filename;
  4918. /* Read the file into BUFFER. */
  4919. buffer = (char *)xmalloc (finfo.st_size + 1);
  4920. i = read (file, buffer, finfo.st_size);
  4921. close (file);
  4922. if (i != finfo.st_size)
  4923. return (errno);
  4924. /* Loop over the lines in the file. Lines that start with `#' are
  4925. comments; all other lines are commands for readline initialization. */
  4926. line = buffer;
  4927. end = buffer + finfo.st_size;
  4928. while (line < end)
  4929. {
  4930. /* Find the end of this line. */
  4931. for (i = 0; line + i != end && line[i] != '\n'; i++);
  4932. /* Mark end of line. */
  4933. line[i] = '\0';
  4934. /* If the line is not a comment, then parse it. */
  4935. if (*line != '#')
  4936. rl_parse_and_bind (line);
  4937. /* Move to the next line. */
  4938. line += i + 1;
  4939. }
  4940. return (0);
  4941. }
  4942. /* **************************************************************** */
  4943. /* */
  4944. /* Parser Directives */
  4945. /* */
  4946. /* **************************************************************** */
  4947. /* Conditionals. */
  4948. /* Calling programs set this to have their argv[0]. */
  4949. char *rl_readline_name = "other";
  4950. /* Stack of previous values of parsing_conditionalized_out. */
  4951. static unsigned char *if_stack = (unsigned char *)NULL;
  4952. static int if_stack_depth = 0;
  4953. static int if_stack_size = 0;
  4954. /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
  4955. parser_if (args)
  4956. char *args;
  4957. {
  4958. register int i;
  4959. /* Push parser state. */
  4960. if (if_stack_depth + 1 >= if_stack_size)
  4961. {
  4962. if (!if_stack)
  4963. if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  4964. else
  4965. if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  4966. }
  4967. if_stack[if_stack_depth++] = parsing_conditionalized_out;
  4968. /* If parsing is turned off, then nothing can turn it back on except
  4969. for finding the matching endif. In that case, return right now. */
  4970. if (parsing_conditionalized_out)
  4971. return;
  4972. /* Isolate first argument. */
  4973. for (i = 0; args[i] && !whitespace (args[i]); i++);
  4974. if (args[i])
  4975. args[i++] = '\0';
  4976. /* Handle "if term=foo" and "if mode=emacs" constructs. If this
  4977. isn't term=foo, or mode=emacs, then check to see if the first
  4978. word in ARGS is the same as the value stored in rl_readline_name. */
  4979. if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
  4980. {
  4981. char *tem, *tname;
  4982. /* Terminals like "aaa-60" are equivalent to "aaa". */
  4983. tname = savestring (rl_terminal_name);
  4984. tem = rindex (tname, '-');
  4985. if (tem)
  4986. *tem = '\0';
  4987. if (stricmp (args + 5, tname) == 0)
  4988. parsing_conditionalized_out = 0;
  4989. else
  4990. parsing_conditionalized_out = 1;
  4991. free (tname);
  4992. }
  4993. #if defined (VI_MODE)
  4994. else if (strnicmp (args, "mode=", 5) == 0)
  4995. {
  4996. int mode;
  4997. if (stricmp (args + 5, "emacs") == 0)
  4998. mode = emacs_mode;
  4999. else if (stricmp (args + 5, "vi") == 0)
  5000. mode = vi_mode;
  5001. else
  5002. mode = no_mode;
  5003. if (mode == rl_editing_mode)
  5004. parsing_conditionalized_out = 0;
  5005. else
  5006. parsing_conditionalized_out = 1;
  5007. }
  5008. #endif /* VI_MODE */
  5009. /* Check to see if the first word in ARGS is the same as the
  5010. value stored in rl_readline_name. */
  5011. else if (stricmp (args, rl_readline_name) == 0)
  5012. parsing_conditionalized_out = 0;
  5013. else
  5014. parsing_conditionalized_out = 1;
  5015. }
  5016. /* Invert the current parser state if there is anything on the stack. */
  5017. parser_else (args)
  5018. char *args;
  5019. {
  5020. register int i;
  5021. if (!if_stack_depth)
  5022. {
  5023. /* Error message? */
  5024. return;
  5025. }
  5026. /* Check the previous (n - 1) levels of the stack to make sure that
  5027. we haven't previously turned off parsing. */
  5028. for (i = 0; i < if_stack_depth - 1; i++)
  5029. if (if_stack[i] == 1)
  5030. return;
  5031. /* Invert the state of parsing if at top level. */
  5032. parsing_conditionalized_out = !parsing_conditionalized_out;
  5033. }
  5034. /* Terminate a conditional, popping the value of
  5035. parsing_conditionalized_out from the stack. */
  5036. parser_endif (args)
  5037. char *args;
  5038. {
  5039. if (if_stack_depth)
  5040. parsing_conditionalized_out = if_stack[--if_stack_depth];
  5041. else
  5042. {
  5043. /* *** What, no error message? *** */
  5044. }
  5045. }
  5046. /* Associate textual names with actual functions. */
  5047. static struct {
  5048. char *name;
  5049. Function *function;
  5050. } parser_directives [] = {
  5051. { "if", parser_if },
  5052. { "endif", parser_endif },
  5053. { "else", parser_else },
  5054. { (char *)0x0, (Function *)0x0 }
  5055. };
  5056. /* Handle a parser directive. STATEMENT is the line of the directive
  5057. without any leading `$'. */
  5058. static int
  5059. handle_parser_directive (statement)
  5060. char *statement;
  5061. {
  5062. register int i;
  5063. char *directive, *args;
  5064. /* Isolate the actual directive. */
  5065. /* Skip whitespace. */
  5066. for (i = 0; whitespace (statement[i]); i++);
  5067. directive = &statement[i];
  5068. for (; statement[i] && !whitespace (statement[i]); i++);
  5069. if (statement[i])
  5070. statement[i++] = '\0';
  5071. for (; statement[i] && whitespace (statement[i]); i++);
  5072. args = &statement[i];
  5073. /* Lookup the command, and act on it. */
  5074. for (i = 0; parser_directives[i].name; i++)
  5075. if (stricmp (directive, parser_directives[i].name) == 0)
  5076. {
  5077. (*parser_directives[i].function) (args);
  5078. return (0);
  5079. }
  5080. /* *** Should an error message be output? */
  5081. return (1);
  5082. }
  5083. /* Ugly but working hack for binding prefix meta. */
  5084. #define PREFIX_META_HACK
  5085. static int substring_member_of_array ();
  5086. /* Read the binding command from STRING and perform it.
  5087. A key binding command looks like: Keyname: function-name\0,
  5088. a variable binding command looks like: set variable value.
  5089. A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  5090. rl_parse_and_bind (string)
  5091. char *string;
  5092. {
  5093. extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  5094. char *funname, *kname;
  5095. register int c;
  5096. int key, i;
  5097. while (string && whitespace (*string))
  5098. string++;
  5099. if (!string || !*string || *string == '#')
  5100. return;
  5101. /* If this is a parser directive, act on it. */
  5102. if (*string == '$')
  5103. {
  5104. handle_parser_directive (&string[1]);
  5105. return;
  5106. }
  5107. /* If we are supposed to be skipping parsing right now, then do it. */
  5108. if (parsing_conditionalized_out)
  5109. return;
  5110. i = 0;
  5111. /* If this keyname is a complex key expression surrounded by quotes,
  5112. advance to after the matching close quote. */
  5113. if (*string == '"')
  5114. {
  5115. for (i = 1; c = string[i]; i++)
  5116. {
  5117. if (c == '"' && string[i - 1] != '\\')
  5118. break;
  5119. }
  5120. }
  5121. /* Advance to the colon (:) or whitespace which separates the two objects. */
  5122. for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  5123. /* Mark the end of the command (or keyname). */
  5124. if (string[i])
  5125. string[i++] = '\0';
  5126. /* If this is a command to set a variable, then do that. */
  5127. if (stricmp (string, "set") == 0)
  5128. {
  5129. char *var = string + i;
  5130. char *value;
  5131. /* Make VAR point to start of variable name. */
  5132. while (*var && whitespace (*var)) var++;
  5133. /* Make value point to start of value string. */
  5134. value = var;
  5135. while (*value && !whitespace (*value)) value++;
  5136. if (*value)
  5137. *value++ = '\0';
  5138. while (*value && whitespace (*value)) value++;
  5139. rl_variable_bind (var, value);
  5140. return;
  5141. }
  5142. /* Skip any whitespace between keyname and funname. */
  5143. for (; string[i] && whitespace (string[i]); i++);
  5144. funname = &string[i];
  5145. /* Now isolate funname.
  5146. For straight function names just look for whitespace, since
  5147. that will signify the end of the string. But this could be a
  5148. macro definition. In that case, the string is quoted, so skip
  5149. to the matching delimiter. */
  5150. if (*funname == '\'' || *funname == '"')
  5151. {
  5152. int delimiter = string[i++];
  5153. for (; c = string[i]; i++)
  5154. {
  5155. if (c == delimiter && string[i - 1] != '\\')
  5156. break;
  5157. }
  5158. if (c)
  5159. i++;
  5160. }
  5161. /* Advance to the end of the string. */
  5162. for (; string[i] && !whitespace (string[i]); i++);
  5163. /* No extra whitespace at the end of the string. */
  5164. string[i] = '\0';
  5165. /* If this is a new-style key-binding, then do the binding with
  5166. rl_set_key (). Otherwise, let the older code deal with it. */
  5167. if (*string == '"')
  5168. {
  5169. char *seq = (char *)alloca (1 + strlen (string));
  5170. register int j, k = 0;
  5171. for (j = 1; string[j]; j++)
  5172. {
  5173. if (string[j] == '"' && string[j - 1] != '\\')
  5174. break;
  5175. seq[k++] = string[j];
  5176. }
  5177. seq[k] = '\0';
  5178. /* Binding macro? */
  5179. if (*funname == '\'' || *funname == '"')
  5180. {
  5181. j = strlen (funname);
  5182. if (j && funname[j - 1] == *funname)
  5183. funname[j - 1] = '\0';
  5184. rl_macro_bind (seq, &funname[1], keymap);
  5185. }
  5186. else
  5187. rl_set_key (seq, rl_named_function (funname), keymap);
  5188. return;
  5189. }
  5190. /* Get the actual character we want to deal with. */
  5191. kname = rindex (string, '-');
  5192. if (!kname)
  5193. kname = string;
  5194. else
  5195. kname++;
  5196. key = glean_key_from_name (kname);
  5197. /* Add in control and meta bits. */
  5198. if (substring_member_of_array (string, possible_control_prefixes))
  5199. key = CTRL (to_upper (key));
  5200. if (substring_member_of_array (string, possible_meta_prefixes))
  5201. key = META (key);
  5202. /* Temporary. Handle old-style keyname with macro-binding. */
  5203. if (*funname == '\'' || *funname == '"')
  5204. {
  5205. char seq[2];
  5206. int fl = strlen (funname);
  5207. seq[0] = key; seq[1] = '\0';
  5208. if (fl && funname[fl - 1] == *funname)
  5209. funname[fl - 1] = '\0';
  5210. rl_macro_bind (seq, &funname[1], keymap);
  5211. }
  5212. #if defined (PREFIX_META_HACK)
  5213. /* Ugly, but working hack to keep prefix-meta around. */
  5214. else if (stricmp (funname, "prefix-meta") == 0)
  5215. {
  5216. char seq[2];
  5217. seq[0] = key;
  5218. seq[1] = '\0';
  5219. rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
  5220. }
  5221. #endif /* PREFIX_META_HACK */
  5222. else
  5223. rl_bind_key (key, rl_named_function (funname));
  5224. }
  5225. rl_variable_bind (name, value)
  5226. char *name, *value;
  5227. {
  5228. if (stricmp (name, "editing-mode") == 0)
  5229. {
  5230. if (strnicmp (value, "vi", 2) == 0)
  5231. {
  5232. #if defined (VI_MODE)
  5233. keymap = vi_insertion_keymap;
  5234. rl_editing_mode = vi_mode;
  5235. #else
  5236. #if defined (NOTDEF)
  5237. /* What state is the terminal in? I'll tell you:
  5238. non-determinate! That means we cannot do any output. */
  5239. ding ();
  5240. #endif /* NOTDEF */
  5241. #endif /* VI_MODE */
  5242. }
  5243. else if (strnicmp (value, "emacs", 5) == 0)
  5244. {
  5245. keymap = emacs_standard_keymap;
  5246. rl_editing_mode = emacs_mode;
  5247. }
  5248. }
  5249. else if (stricmp (name, "horizontal-scroll-mode") == 0)
  5250. {
  5251. if (!*value || stricmp (value, "On") == 0)
  5252. horizontal_scroll_mode = 1;
  5253. else
  5254. horizontal_scroll_mode = 0;
  5255. }
  5256. else if (stricmp (name, "mark-modified-lines") == 0)
  5257. {
  5258. if (!*value || stricmp (value, "On") == 0)
  5259. mark_modified_lines = 1;
  5260. else
  5261. mark_modified_lines = 0;
  5262. }
  5263. else if (stricmp (name, "prefer-visible-bell") == 0)
  5264. {
  5265. if (!*value || stricmp (value, "On") == 0)
  5266. prefer_visible_bell = 1;
  5267. else
  5268. prefer_visible_bell = 0;
  5269. }
  5270. else if (stricmp (name, "comment-begin") == 0)
  5271. {
  5272. #if defined (VI_MODE)
  5273. extern char *rl_vi_comment_begin;
  5274. if (*value)
  5275. {
  5276. if (rl_vi_comment_begin)
  5277. free (rl_vi_comment_begin);
  5278. rl_vi_comment_begin = savestring (value);
  5279. }
  5280. #endif /* VI_MODE */
  5281. }
  5282. }
  5283. /* Return the character which matches NAME.
  5284. For example, `Space' returns ' '. */
  5285. typedef struct {
  5286. char *name;
  5287. int value;
  5288. } assoc_list;
  5289. assoc_list name_key_alist[] = {
  5290. { "DEL", 0x7f },
  5291. { "ESC", '\033' },
  5292. { "Escape", '\033' },
  5293. { "LFD", '\n' },
  5294. { "Newline", '\n' },
  5295. { "RET", '\r' },
  5296. { "Return", '\r' },
  5297. { "Rubout", 0x7f },
  5298. { "SPC", ' ' },
  5299. { "Space", ' ' },
  5300. { "Tab", 0x09 },
  5301. { (char *)0x0, 0 }
  5302. };
  5303. int
  5304. glean_key_from_name (name)
  5305. char *name;
  5306. {
  5307. register int i;
  5308. for (i = 0; name_key_alist[i].name; i++)
  5309. if (stricmp (name, name_key_alist[i].name) == 0)
  5310. return (name_key_alist[i].value);
  5311. return (*name);
  5312. }
  5313. /* **************************************************************** */
  5314. /* */
  5315. /* Key Binding and Function Information */
  5316. /* */
  5317. /* **************************************************************** */
  5318. /* Each of the following functions produces information about the
  5319. state of keybindings and functions known to Readline. The info
  5320. is always printed to rl_outstream, and in such a way that it can
  5321. be read back in (i.e., passed to rl_parse_and_bind (). */
  5322. /* Print the names of functions known to Readline. */
  5323. void
  5324. rl_list_funmap_names (ignore)
  5325. int ignore;
  5326. {
  5327. register int i;
  5328. char **funmap_names;
  5329. extern char **rl_funmap_names ();
  5330. funmap_names = rl_funmap_names ();
  5331. if (!funmap_names)
  5332. return;
  5333. for (i = 0; funmap_names[i]; i++)
  5334. fprintf (rl_outstream, "%s\n", funmap_names[i]);
  5335. free (funmap_names);
  5336. }
  5337. /* Return a NULL terminated array of strings which represent the key
  5338. sequences that are used to invoke FUNCTION in MAP. */
  5339. static char **
  5340. invoking_keyseqs_in_map (function, map)
  5341. Function *function;
  5342. Keymap map;
  5343. {
  5344. register int key;
  5345. char **result;
  5346. int result_index, result_size;
  5347. result = (char **)NULL;
  5348. result_index = result_size = 0;
  5349. for (key = 0; key < 128; key++)
  5350. {
  5351. switch (map[key].type)
  5352. {
  5353. case ISMACR:
  5354. /* Macros match, if, and only if, the pointers are identical.
  5355. Thus, they are treated exactly like functions in here. */
  5356. case ISFUNC:
  5357. /* If the function in the keymap is the one we are looking for,
  5358. then add the current KEY to the list of invoking keys. */
  5359. if (map[key].function == function)
  5360. {
  5361. char *keyname = (char *)xmalloc (5);
  5362. if (CTRL_P (key))
  5363. sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  5364. else if (key == RUBOUT)
  5365. sprintf (keyname, "\\C-?");
  5366. else
  5367. sprintf (keyname, "%c", key);
  5368. if (result_index + 2 > result_size)
  5369. {
  5370. if (!result)
  5371. result = (char **) xmalloc
  5372. ((result_size = 10) * sizeof (char *));
  5373. else
  5374. result = (char **) xrealloc
  5375. (result, (result_size += 10) * sizeof (char *));
  5376. }
  5377. result[result_index++] = keyname;
  5378. result[result_index] = (char *)NULL;
  5379. }
  5380. break;
  5381. case ISKMAP:
  5382. {
  5383. char **seqs = (char **)NULL;
  5384. /* Find the list of keyseqs in this map which have FUNCTION as
  5385. their target. Add the key sequences found to RESULT. */
  5386. if (map[key].function)
  5387. seqs =
  5388. invoking_keyseqs_in_map (function, (Keymap)map[key].function);
  5389. if (seqs)
  5390. {
  5391. register int i;
  5392. for (i = 0; seqs[i]; i++)
  5393. {
  5394. char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
  5395. if (key == ESC)
  5396. sprintf (keyname, "\\e");
  5397. else if (CTRL_P (key))
  5398. sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  5399. else if (key == RUBOUT)
  5400. sprintf (keyname, "\\C-?");
  5401. else
  5402. sprintf (keyname, "%c", key);
  5403. strcat (keyname, seqs[i]);
  5404. if (result_index + 2 > result_size)
  5405. {
  5406. if (!result)
  5407. result = (char **)
  5408. xmalloc ((result_size = 10) * sizeof (char *));
  5409. else
  5410. result = (char **)
  5411. xrealloc (result,
  5412. (result_size += 10) * sizeof (char *));
  5413. }
  5414. result[result_index++] = keyname;
  5415. result[result_index] = (char *)NULL;
  5416. }
  5417. }
  5418. }
  5419. break;
  5420. }
  5421. }
  5422. return (result);
  5423. }
  5424. /* Return a NULL terminated array of strings which represent the key
  5425. sequences that can be used to invoke FUNCTION using the current keymap. */
  5426. char **
  5427. rl_invoking_keyseqs (function)
  5428. Function *function;
  5429. {
  5430. return (invoking_keyseqs_in_map (function, keymap));
  5431. }
  5432. /* Print all of the current functions and their bindings to
  5433. rl_outstream. If an explicit argument is given, then print
  5434. the output in such a way that it can be read back in. */
  5435. int
  5436. rl_dump_functions (count)
  5437. int count;
  5438. {
  5439. void rl_function_dumper ();
  5440. rl_function_dumper (rl_explicit_arg);
  5441. rl_on_new_line ();
  5442. return (0);
  5443. }
  5444. /* Print all of the functions and their bindings to rl_outstream. If
  5445. PRINT_READABLY is non-zero, then print the output in such a way
  5446. that it can be read back in. */
  5447. void
  5448. rl_function_dumper (print_readably)
  5449. int print_readably;
  5450. {
  5451. register int i;
  5452. char **rl_funmap_names (), **names;
  5453. char *name;
  5454. names = rl_funmap_names ();
  5455. fprintf (rl_outstream, "\n");
  5456. for (i = 0; name = names[i]; i++)
  5457. {
  5458. Function *function;
  5459. char **invokers;
  5460. function = rl_named_function (name);
  5461. invokers = invoking_keyseqs_in_map (function, keymap);
  5462. if (print_readably)
  5463. {
  5464. if (!invokers)
  5465. fprintf (rl_outstream, "# %s (not bound)\n", name);
  5466. else
  5467. {
  5468. register int j;
  5469. for (j = 0; invokers[j]; j++)
  5470. {
  5471. fprintf (rl_outstream, "\"%s\": %s\n",
  5472. invokers[j], name);
  5473. free (invokers[j]);
  5474. }
  5475. free (invokers);
  5476. }
  5477. }
  5478. else
  5479. {
  5480. if (!invokers)
  5481. fprintf (rl_outstream, "%s is not bound to any keys\n",
  5482. name);
  5483. else
  5484. {
  5485. register int j;
  5486. fprintf (rl_outstream, "%s can be found on ", name);
  5487. for (j = 0; invokers[j] && j < 5; j++)
  5488. {
  5489. fprintf (rl_outstream, "\"%s\"%s", invokers[j],
  5490. invokers[j + 1] ? ", " : ".\n");
  5491. }
  5492. if (j == 5 && invokers[j])
  5493. fprintf (rl_outstream, "...\n");
  5494. for (j = 0; invokers[j]; j++)
  5495. free (invokers[j]);
  5496. free (invokers);
  5497. }
  5498. }
  5499. }
  5500. }
  5501. /* **************************************************************** */
  5502. /* */
  5503. /* String Utility Functions */
  5504. /* */
  5505. /* **************************************************************** */
  5506. static char* strindex();
  5507. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  5508. static int
  5509. substring_member_of_array (string, array)
  5510. char *string, **array;
  5511. {
  5512. while (*array)
  5513. {
  5514. if (strindex (string, *array))
  5515. return (1);
  5516. array++;
  5517. }
  5518. return (0);
  5519. }
  5520. /* Whoops, Unix doesn't have strnicmp. */
  5521. /* Determine if s2 occurs in s1. If so, return a pointer to the
  5522. match in s1. The compare is case insensitive. */
  5523. static char *
  5524. strindex (s1, s2)
  5525. register char *s1, *s2;
  5526. {
  5527. register int i, l = strlen (s2);
  5528. register int len = strlen (s1);
  5529. for (i = 0; (len - i) >= l; i++)
  5530. if (strnicmp (&s1[i], s2, l) == 0)
  5531. return (s1 + i);
  5532. return ((char *)NULL);
  5533. }
  5534. /* **************************************************************** */
  5535. /* */
  5536. /* USG (System V) Support */
  5537. /* */
  5538. /* **************************************************************** */
  5539. /* When compiling and running in the `Posix' environment, Ultrix does
  5540. not restart system calls, so this needs to do it. */
  5541. int
  5542. rl_getc (stream)
  5543. FILE *stream;
  5544. {
  5545. #ifdef WIN32
  5546. return winio_getc();
  5547. #else
  5548. int result;
  5549. unsigned char c;
  5550. #if defined(__GO32__)
  5551. if (isatty(0))
  5552. return (getkey() & 0x7f);
  5553. #endif /* __GO32__ */
  5554. while (1)
  5555. {
  5556. result = read (fileno (stream), &c, sizeof (char));
  5557. if (result == sizeof (char))
  5558. return (c);
  5559. /* If zero characters are returned, then the file that we are
  5560. reading from is empty! Return EOF in that case. */
  5561. if (result == 0)
  5562. return (EOF);
  5563. #ifndef __GO32__
  5564. /* If the error that we received was SIGINT, then try again,
  5565. this is simply an interrupted system call to read ().
  5566. Otherwise, some error ocurred, also signifying EOF. */
  5567. if (errno != EINTR)
  5568. return (EOF);
  5569. #endif /* !__GO32__ */
  5570. }
  5571. #endif
  5572. }
  5573. #if defined (STATIC_MALLOC)
  5574. /* **************************************************************** */
  5575. /* */
  5576. /* xmalloc and xrealloc () */
  5577. /* */
  5578. /* **************************************************************** */
  5579. static void memory_error_and_abort ();
  5580. static char *
  5581. xmalloc (bytes)
  5582. int bytes;
  5583. {
  5584. char *temp = (char *)malloc (bytes);
  5585. if (!temp)
  5586. memory_error_and_abort ();
  5587. return (temp);
  5588. }
  5589. static char *
  5590. xrealloc (pointer, bytes)
  5591. char *pointer;
  5592. int bytes;
  5593. {
  5594. char *temp;
  5595. if (!pointer)
  5596. temp = (char *)malloc (bytes);
  5597. else
  5598. temp = (char *)realloc (pointer, bytes);
  5599. if (!temp)
  5600. memory_error_and_abort ();
  5601. return (temp);
  5602. }
  5603. static void
  5604. memory_error_and_abort ()
  5605. {
  5606. fprintf (stderr, "readline: Out of virtual memory!\n");
  5607. abort ();
  5608. }
  5609. #endif /* STATIC_MALLOC */
  5610. /* **************************************************************** */
  5611. /* */
  5612. /* Testing Readline */
  5613. /* */
  5614. /* **************************************************************** */
  5615. #if defined (TEST)
  5616. main ()
  5617. {
  5618. HIST_ENTRY **history_list ();
  5619. char *temp = (char *)NULL;
  5620. char *prompt = "readline% ";
  5621. int done = 0;
  5622. while (!done)
  5623. {
  5624. temp = readline (prompt);
  5625. /* Test for EOF. */
  5626. if (!temp)
  5627. exit (1);
  5628. /* If there is anything on the line, print it and remember it. */
  5629. if (*temp)
  5630. {
  5631. fprintf (stderr, "%s\r\n", temp);
  5632. add_history (temp);
  5633. }
  5634. /* Check for `command' that we handle. */
  5635. if (strcmp (temp, "quit") == 0)
  5636. done = 1;
  5637. if (strcmp (temp, "list") == 0)
  5638. {
  5639. HIST_ENTRY **list = history_list ();
  5640. register int i;
  5641. if (list)
  5642. {
  5643. for (i = 0; list[i]; i++)
  5644. {
  5645. fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  5646. free (list[i]->line);
  5647. }
  5648. free (list);
  5649. }
  5650. }
  5651. free (temp);
  5652. }
  5653. }
  5654. #endif /* TEST */
  5655. /*
  5656. * Local variables:
  5657. * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  5658. * end:
  5659. */
  5660. #if defined(__MWERKS__)
  5661. /* strcmp (), but caseless. */
  5662. static int
  5663. stricmp (string1, string2)
  5664. char *string1, *string2;
  5665. {
  5666. register char ch1, ch2;
  5667. while (*string1 && *string2)
  5668. {
  5669. ch1 = *string1++;
  5670. ch2 = *string2++;
  5671. if (to_upper(ch1) != to_upper(ch2))
  5672. return (1);
  5673. }
  5674. return (*string1 | *string2);
  5675. }
  5676. /* Compare at most COUNT characters from string1 to string2. Case
  5677. doesn't matter. */
  5678. static int
  5679. strnicmp (string1, string2, count)
  5680. char *string1, *string2;
  5681. {
  5682. register char ch1, ch2;
  5683. while (count)
  5684. {
  5685. ch1 = *string1++;
  5686. ch2 = *string2++;
  5687. if (to_upper(ch1) == to_upper(ch2))
  5688. count--;
  5689. else break;
  5690. }
  5691. return (count);
  5692. }
  5693. #endif