PageRenderTime 69ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/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

Large files files are truncated, but you can click here to view the full file

  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 i…

Large files files are truncated, but you can click here to view the full file