PageRenderTime 233ms CodeModel.GetById 56ms RepoModel.GetById 2ms app.codeStats 1ms

/src/readline/readline.c

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