PageRenderTime 73ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/src/mswin/rdln/readline.c

https://bitbucket.org/nrnhines/nrn
C | 6803 lines | 4776 code | 973 blank | 1054 comment | 896 complexity | 8f48c6c0d52777e3efdce5f1bec6d038 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0

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

  1. /* readline.c -- a general facility for reading lines of input
  2. with emacs style editing and completion. */
  3. /* Copyright 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
  4. This file contains the Readline Library (the Library), a set of
  5. routines for providing Emacs style line input to programs that ask
  6. for it.
  7. The Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. The Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. /* Remove these declarations when we have a complete libgnu.a. */
  19. #if defined(__MSC_VER)
  20. #include <../../nrnconf.h>
  21. #endif
  22. #ifdef WIN32
  23. #define STATIC_MALLOC
  24. #define rindex strrchr
  25. #endif
  26. /* #define STATIC_MALLOC */
  27. #if defined(_MSC_VER) || defined(__MWERKS__)
  28. #include <unistd.h>
  29. #include <malloc.h>
  30. #include <string.h>
  31. #endif
  32. /* Caseless strcmp (). */
  33. #if defined(__MWERKS__)
  34. static int stricmp (), strnicmp ();
  35. #endif
  36. #if !defined (STATIC_MALLOC)
  37. extern char *xmalloc (), *xrealloc ();
  38. #else
  39. static char *xmalloc (), *xrealloc ();
  40. #endif /* STATIC_MALLOC */
  41. #include "sysdep.h"
  42. #include <stdio.h>
  43. #include <fcntl.h>
  44. #ifdef WIN32
  45. #define NO_SYS_FILE
  46. #endif
  47. #ifndef NO_SYS_FILE
  48. #include <sys/file.h>
  49. #endif
  50. #include <signal.h>
  51. #if defined (HAVE_UNISTD_H)
  52. # include <unistd.h>
  53. #endif
  54. #ifndef WIN32
  55. #define NEW_TTY_DRIVER
  56. #endif
  57. #define HAVE_BSD_SIGNALS
  58. /* #define USE_XON_XOFF */
  59. #if __GO32__
  60. #define GRX 1
  61. #endif
  62. #if GRX
  63. extern int egagrph;
  64. #endif
  65. #ifdef __MSDOS__
  66. #undef NEW_TTY_DRIVER
  67. #undef HAVE_BSD_SIGNALS
  68. #endif
  69. /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
  70. #if defined (USG) && !defined (hpux)
  71. #undef HAVE_BSD_SIGNALS
  72. #endif
  73. /* System V machines use termio. */
  74. #if !defined (_POSIX_VERSION)
  75. # if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX) || defined (__H3050R) || defined (__H3050RX)
  76. # undef NEW_TTY_DRIVER
  77. # define TERMIO_TTY_DRIVER
  78. # include <termio.h>
  79. # if !defined (TCOON)
  80. # define TCOON 1
  81. # endif
  82. # endif /* USG || hpux || Xenix || sgi || DUGX */
  83. #endif /* !_POSIX_VERSION */
  84. /* Posix systems use termios and the Posix signal functions. */
  85. #if defined (_POSIX_VERSION)
  86. # if !defined (TERMIOS_MISSING)
  87. # undef NEW_TTY_DRIVER
  88. # define TERMIOS_TTY_DRIVER
  89. # include <termios.h>
  90. # endif /* !TERMIOS_MISSING */
  91. # define HAVE_POSIX_SIGNALS
  92. # if !defined (O_NDELAY)
  93. # define O_NDELAY O_NONBLOCK /* Posix-style non-blocking i/o */
  94. # endif /* O_NDELAY */
  95. #endif /* _POSIX_VERSION */
  96. /* Other (BSD) machines use sgtty. */
  97. #if defined (NEW_TTY_DRIVER)
  98. #include <sgtty.h>
  99. #endif
  100. /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
  101. it is not already defined. It is used both to determine if a
  102. special character is disabled and to disable certain special
  103. characters. Posix systems should set to 0, USG systems to -1. */
  104. #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
  105. # if defined (_POSIX_VERSION)
  106. # define _POSIX_VDISABLE 0
  107. # else /* !_POSIX_VERSION */
  108. # define _POSIX_VDISABLE -1
  109. # endif /* !_POSIX_VERSION */
  110. #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
  111. /* Define some macros for dealing with assorted signalling disciplines.
  112. These macros provide a way to use signal blocking and disabling
  113. without smothering your code in a pile of #ifdef's.
  114. SIGNALS_UNBLOCK; Stop blocking all signals.
  115. {
  116. SIGNALS_DECLARE_SAVED (name); Declare a variable to save the
  117. signal blocking state.
  118. ...
  119. SIGNALS_BLOCK (SIGSTOP, name); Block a signal, and save the previous
  120. state for restoration later.
  121. ...
  122. SIGNALS_RESTORE (name); Restore previous signals.
  123. }
  124. */
  125. #ifdef HAVE_POSIX_SIGNALS
  126. /* POSIX signals */
  127. #define SIGNALS_UNBLOCK \
  128. do { sigset_t set; \
  129. sigemptyset (&set); \
  130. sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); \
  131. } while (0)
  132. #define SIGNALS_DECLARE_SAVED(name) sigset_t name
  133. #define SIGNALS_BLOCK(SIG, saved) \
  134. do { sigset_t set; \
  135. sigemptyset (&set); \
  136. sigaddset (&set, SIG); \
  137. sigprocmask (SIG_BLOCK, &set, &saved); \
  138. } while (0)
  139. #define SIGNALS_RESTORE(saved) \
  140. sigprocmask (SIG_SETMASK, &saved, (sigset_t *)NULL)
  141. #else /* HAVE_POSIX_SIGNALS */
  142. #ifdef HAVE_BSD_SIGNALS
  143. /* BSD signals */
  144. #define SIGNALS_UNBLOCK sigsetmask (0)
  145. #define SIGNALS_DECLARE_SAVED(name) int name
  146. #define SIGNALS_BLOCK(SIG, saved) saved = sigblock (sigmask (SIG))
  147. #define SIGNALS_RESTORE(saved) sigsetmask (saved)
  148. #else /* HAVE_BSD_SIGNALS */
  149. /* None of the Above */
  150. #define SIGNALS_UNBLOCK /* nothing */
  151. #define SIGNALS_DECLARE_SAVED(name) /* nothing */
  152. #define SIGNALS_BLOCK(SIG, saved) /* nothing */
  153. #define SIGNALS_RESTORE(saved) /* nothing */
  154. #endif /* HAVE_BSD_SIGNALS */
  155. #endif /* HAVE_POSIX_SIGNALS */
  156. /* End of signal handling definitions. */
  157. #include <errno.h>
  158. #include <setjmp.h>
  159. #include <sys/stat.h>
  160. /* Posix macro to check file in statbuf for directory-ness. */
  161. #if defined (S_IFDIR) && !defined (S_ISDIR)
  162. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  163. #endif
  164. #if !defined(__MSDOS__) && !defined(WIN32)
  165. /* These next are for filename completion. Perhaps this belongs
  166. in a different place. */
  167. #include <pwd.h>
  168. #endif /* __MSDOS__ */
  169. #if defined (USG) && !defined (isc386) && !defined (sgi)
  170. struct passwd *getpwuid (), *getpwent ();
  171. #endif
  172. /* #define HACK_TERMCAP_MOTION */
  173. /* Some standard library routines. */
  174. #include "readline.h"
  175. #include "history.h"
  176. #ifndef digit
  177. #define digit(c) ((c) >= '0' && (c) <= '9')
  178. #endif
  179. #ifndef isletter
  180. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  181. #endif
  182. #ifndef digit_value
  183. #define digit_value(c) ((c) - '0')
  184. #endif
  185. #ifndef member
  186. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  187. #endif
  188. #ifndef isident
  189. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  190. #endif
  191. #ifndef exchange
  192. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  193. #endif
  194. #if !defined (rindex)
  195. extern char *rindex ();
  196. #endif /* rindex */
  197. #if !defined (index)
  198. extern char *index ();
  199. #endif /* index */
  200. extern char *getenv ();
  201. extern char *tilde_expand ();
  202. char* tilde_expand() { return savestring("/");}
  203. static update_line ();
  204. static void output_character_function ();
  205. static delete_chars ();
  206. static void insert_some_chars ();
  207. #if defined (VOID_SIGHANDLER)
  208. # define sighandler void
  209. #else
  210. # define sighandler int
  211. #endif /* VOID_SIGHANDLER */
  212. /* This typedef is equivalant to the one for Function; it allows us
  213. to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  214. typedef sighandler SigHandler ();
  215. /* If on, then readline handles signals in a way that doesn't screw. */
  216. #ifndef WIN32
  217. #define HANDLE_SIGNALS
  218. #endif
  219. #if defined(__GO32__)
  220. #ifdef RSX
  221. #include <termio.h>
  222. #else
  223. #include <pc.h>
  224. #endif
  225. #undef HANDLE_SIGNALS
  226. #endif
  227. /* **************************************************************** */
  228. /* */
  229. /* Line editing input utility */
  230. /* */
  231. /* **************************************************************** */
  232. /* A pointer to the keymap that is currently in use.
  233. By default, it is the standard emacs keymap. */
  234. Keymap keymap = emacs_standard_keymap;
  235. #define no_mode -1
  236. #define vi_mode 0
  237. #define emacs_mode 1
  238. /* The current style of editing. */
  239. int rl_editing_mode = emacs_mode;
  240. /* Non-zero if the previous command was a kill command. */
  241. static int last_command_was_kill = 0;
  242. /* The current value of the numeric argument specified by the user. */
  243. int rl_numeric_arg = 1;
  244. /* Non-zero if an argument was typed. */
  245. int rl_explicit_arg = 0;
  246. /* Temporary value used while generating the argument. */
  247. int rl_arg_sign = 1;
  248. /* Non-zero means we have been called at least once before. */
  249. static int rl_initialized = 0;
  250. /* If non-zero, this program is running in an EMACS buffer. */
  251. static char *running_in_emacs = (char *)NULL;
  252. /* The current offset in the current input line. */
  253. int rl_point;
  254. /* Mark in the current input line. */
  255. int rl_mark;
  256. /* Length of the current input line. */
  257. int rl_end;
  258. /* Make this non-zero to return the current input_line. */
  259. int rl_done;
  260. /* The last function executed by readline. */
  261. Function *rl_last_func = (Function *)NULL;
  262. /* Top level environment for readline_internal (). */
  263. static jmp_buf readline_top_level;
  264. /* The streams we interact with. */
  265. static FILE *in_stream, *out_stream;
  266. /* The names of the streams that we do input and output to. */
  267. FILE *rl_instream, *rl_outstream;
  268. /* Non-zero means echo characters as they are read. */
  269. int readline_echoing_p = 1;
  270. /* Current prompt. */
  271. char *rl_prompt;
  272. /* The number of characters read in order to type this complete command. */
  273. int rl_key_sequence_length = 0;
  274. /* If non-zero, then this is the address of a function to call just
  275. before readline_internal () prints the first prompt. */
  276. Function *rl_startup_hook = (Function *)NULL;
  277. /* If non-zero, then this is the address of a function to call when
  278. completing on a directory name. The function is called with
  279. the address of a string (the current directory name) as an arg. */
  280. Function *rl_symbolic_link_hook = (Function *)NULL;
  281. /* What we use internally. You should always refer to RL_LINE_BUFFER. */
  282. static char *the_line;
  283. /* The character that can generate an EOF. Really read from
  284. the terminal driver... just defaulted here. */
  285. static int eof_char = CTRL ('D');
  286. /* Non-zero makes this the next keystroke to read. */
  287. int rl_pending_input = 0;
  288. /* Pointer to a useful terminal name. */
  289. char *rl_terminal_name = (char *)NULL;
  290. /* Line buffer and maintenence. */
  291. char *rl_line_buffer = (char *)NULL;
  292. int rl_line_buffer_len = 0;
  293. #define DEFAULT_BUFFER_SIZE 256
  294. /* **************************************************************** */
  295. /* */
  296. /* `Forward' declarations */
  297. /* */
  298. /* **************************************************************** */
  299. /* Non-zero means do not parse any lines other than comments and
  300. parser directives. */
  301. static unsigned char parsing_conditionalized_out = 0;
  302. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  303. static int defining_kbd_macro = 0;
  304. /* **************************************************************** */
  305. /* */
  306. /* Top Level Functions */
  307. /* */
  308. /* **************************************************************** */
  309. static void rl_prep_terminal (), rl_deprep_terminal ();
  310. static void clear_to_eol (), rl_generic_bind ();
  311. /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
  312. none. A return value of NULL means that EOF was encountered. */
  313. char *
  314. readline (prompt)
  315. char *prompt;
  316. {
  317. char *readline_internal ();
  318. char *value;
  319. rl_prompt = prompt;
  320. /* If we are at EOF return a NULL string. */
  321. if (rl_pending_input == EOF)
  322. {
  323. rl_pending_input = 0;
  324. return ((char *)NULL);
  325. }
  326. rl_initialize ();
  327. rl_prep_terminal ();
  328. #if defined (HANDLE_SIGNALS)
  329. rl_set_signals ();
  330. #endif
  331. value = readline_internal ();
  332. rl_deprep_terminal ();
  333. #if defined (HANDLE_SIGNALS)
  334. rl_clear_signals ();
  335. #endif
  336. return (value);
  337. }
  338. /* Read a line of input from the global rl_instream, doing output on
  339. the global rl_outstream.
  340. If rl_prompt is non-null, then that is our prompt. */
  341. char *
  342. readline_internal ()
  343. {
  344. int lastc, c, eof_found;
  345. in_stream = rl_instream;
  346. out_stream = rl_outstream;
  347. lastc = -1;
  348. eof_found = 0;
  349. if (rl_startup_hook)
  350. (*rl_startup_hook) ();
  351. if (!readline_echoing_p)
  352. {
  353. if (rl_prompt)
  354. {
  355. fprintf (out_stream, "%s", rl_prompt);
  356. fflush (out_stream);
  357. }
  358. }
  359. else
  360. {
  361. rl_on_new_line ();
  362. rl_redisplay ();
  363. #if defined (VI_MODE)
  364. if (rl_editing_mode == vi_mode)
  365. rl_vi_insertion_mode ();
  366. #endif /* VI_MODE */
  367. }
  368. while (!rl_done)
  369. {
  370. int lk = last_command_was_kill;
  371. int code = setjmp (readline_top_level);
  372. if (code)
  373. rl_redisplay ();
  374. if (!rl_pending_input)
  375. {
  376. /* Then initialize the argument and number of keys read. */
  377. rl_init_argument ();
  378. rl_key_sequence_length = 0;
  379. }
  380. c = rl_read_key ();
  381. #ifdef WIN32
  382. if (!winio_exists()) {
  383. eof_found = 1;
  384. break;
  385. }
  386. #endif
  387. /* EOF typed to a non-blank line is a <NL>. */
  388. if (c == EOF && rl_end)
  389. c = NEWLINE;
  390. /* The character eof_char typed to blank line, and not as the
  391. previous character is interpreted as EOF. */
  392. if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  393. {
  394. eof_found = 1;
  395. break;
  396. }
  397. lastc = c;
  398. rl_dispatch (c, keymap);
  399. /* If there was no change in last_command_was_kill, then no kill
  400. has taken place. Note that if input is pending we are reading
  401. a prefix command, so nothing has changed yet. */
  402. if (!rl_pending_input)
  403. {
  404. if (lk == last_command_was_kill)
  405. last_command_was_kill = 0;
  406. }
  407. #if defined (VI_MODE)
  408. /* In vi mode, when you exit insert mode, the cursor moves back
  409. over the previous character. We explicitly check for that here. */
  410. if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  411. rl_vi_check ();
  412. #endif /* VI_MODE */
  413. if (!rl_done)
  414. rl_redisplay ();
  415. }
  416. /* Restore the original of this history line, iff the line that we
  417. are editing was originally in the history, AND the line has changed. */
  418. {
  419. HIST_ENTRY *entry = current_history ();
  420. if (entry && rl_undo_list)
  421. {
  422. char *temp = savestring (the_line);
  423. rl_revert_line ();
  424. entry = replace_history_entry (where_history (), the_line,
  425. (HIST_ENTRY *)NULL);
  426. free_history_entry (entry);
  427. strcpy (the_line, temp);
  428. free (temp);
  429. }
  430. }
  431. /* At any rate, it is highly likely that this line has an undo list. Get
  432. rid of it now. */
  433. if (rl_undo_list)
  434. free_undo_list ();
  435. if (eof_found)
  436. return (char *)NULL;
  437. else
  438. return (savestring (the_line));
  439. }
  440. /* **************************************************************** */
  441. /* */
  442. /* Signal Handling */
  443. /* */
  444. /* **************************************************************** */
  445. #if defined (SIGWINCH)
  446. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  447. static sighandler
  448. rl_handle_sigwinch (sig)
  449. int sig;
  450. {
  451. char *term;
  452. term = rl_terminal_name;
  453. if (readline_echoing_p)
  454. {
  455. if (!term)
  456. term = getenv ("TERM");
  457. if (!term)
  458. term = "dumb";
  459. rl_reset_terminal (term);
  460. #if defined (NOTDEF)
  461. crlf ();
  462. rl_forced_update_display ();
  463. #endif /* NOTDEF */
  464. }
  465. if (old_sigwinch &&
  466. old_sigwinch != (SigHandler *)SIG_IGN &&
  467. old_sigwinch != (SigHandler *)SIG_DFL)
  468. (*old_sigwinch) (sig);
  469. #if !defined (VOID_SIGHANDLER)
  470. return (0);
  471. #endif /* VOID_SIGHANDLER */
  472. }
  473. #endif /* SIGWINCH */
  474. #if defined (HANDLE_SIGNALS)
  475. /* Interrupt handling. */
  476. static SigHandler
  477. *old_int = (SigHandler *)NULL,
  478. *old_tstp = (SigHandler *)NULL,
  479. *old_ttou = (SigHandler *)NULL,
  480. *old_ttin = (SigHandler *)NULL,
  481. *old_cont = (SigHandler *)NULL,
  482. *old_alrm = (SigHandler *)NULL;
  483. /* Handle an interrupt character. */
  484. static sighandler
  485. rl_signal_handler (sig)
  486. int sig;
  487. {
  488. #if !defined (HAVE_BSD_SIGNALS)
  489. /* Since the signal will not be blocked while we are in the signal
  490. handler, ignore it until rl_clear_signals resets the catcher. */
  491. if (sig == SIGINT)
  492. signal (sig, SIG_IGN);
  493. #endif /* !HAVE_BSD_SIGNALS */
  494. switch (sig)
  495. {
  496. case SIGINT:
  497. free_undo_list ();
  498. rl_clear_message ();
  499. rl_init_argument ();
  500. #if defined (SIGTSTP)
  501. case SIGTSTP:
  502. case SIGTTOU:
  503. case SIGTTIN:
  504. #endif /* SIGTSTP */
  505. case SIGALRM:
  506. rl_clean_up_for_exit ();
  507. rl_deprep_terminal ();
  508. rl_clear_signals ();
  509. rl_pending_input = 0;
  510. kill (getpid (), sig);
  511. SIGNALS_UNBLOCK;
  512. rl_prep_terminal ();
  513. rl_set_signals ();
  514. }
  515. #if !defined (VOID_SIGHANDLER)
  516. return (0);
  517. #endif /* !VOID_SIGHANDLER */
  518. }
  519. rl_set_signals ()
  520. {
  521. old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  522. if (old_int == (SigHandler *)SIG_IGN)
  523. signal (SIGINT, SIG_IGN);
  524. old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
  525. if (old_alrm == (SigHandler *)SIG_IGN)
  526. signal (SIGALRM, SIG_IGN);
  527. #if defined (SIGTSTP)
  528. old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  529. if (old_tstp == (SigHandler *)SIG_IGN)
  530. signal (SIGTSTP, SIG_IGN);
  531. #endif
  532. #if defined (SIGTTOU)
  533. old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  534. old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  535. if (old_tstp == (SigHandler *)SIG_IGN)
  536. {
  537. signal (SIGTTOU, SIG_IGN);
  538. signal (SIGTTIN, SIG_IGN);
  539. }
  540. #endif
  541. #if defined (SIGWINCH)
  542. old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  543. #endif
  544. }
  545. rl_clear_signals ()
  546. {
  547. signal (SIGINT, old_int);
  548. signal (SIGALRM, old_alrm);
  549. #if defined (SIGTSTP)
  550. signal (SIGTSTP, old_tstp);
  551. #endif
  552. #if defined (SIGTTOU)
  553. signal (SIGTTOU, old_ttou);
  554. signal (SIGTTIN, old_ttin);
  555. #endif
  556. #if defined (SIGWINCH)
  557. signal (SIGWINCH, old_sigwinch);
  558. #endif
  559. }
  560. #endif /* HANDLE_SIGNALS */
  561. /* **************************************************************** */
  562. /* */
  563. /* Character Input Buffering */
  564. /* */
  565. /* **************************************************************** */
  566. #if defined (USE_XON_XOFF)
  567. /* If the terminal was in xoff state when we got to it, then xon_char
  568. contains the character that is supposed to start it again. */
  569. static int xon_char, xoff_state;
  570. #endif /* USE_XON_XOFF */
  571. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  572. static unsigned char ibuffer[512];
  573. /* Non-null means it is a pointer to a function to run while waiting for
  574. character input. */
  575. Function *rl_event_hook = (Function *)NULL;
  576. #define any_typein (push_index != pop_index)
  577. /* Add KEY to the buffer of characters to be read. */
  578. rl_stuff_char (key)
  579. int key;
  580. {
  581. if (key == EOF)
  582. {
  583. key = NEWLINE;
  584. rl_pending_input = EOF;
  585. }
  586. ibuffer[push_index++] = key;
  587. if (push_index >= ibuffer_len)
  588. push_index = 0;
  589. }
  590. /* Return the amount of space available in the
  591. buffer for stuffing characters. */
  592. int
  593. ibuffer_space ()
  594. {
  595. if (pop_index > push_index)
  596. return (pop_index - push_index);
  597. else
  598. return (ibuffer_len - (push_index - pop_index));
  599. }
  600. /* Get a key from the buffer of characters to be read.
  601. Return the key in KEY.
  602. Result is KEY if there was a key, or 0 if there wasn't. */
  603. int
  604. rl_get_char (key)
  605. int *key;
  606. {
  607. if (push_index == pop_index)
  608. return (0);
  609. *key = ibuffer[pop_index++];
  610. if (pop_index >= ibuffer_len)
  611. pop_index = 0;
  612. return (1);
  613. }
  614. /* Stuff KEY into the *front* of the input buffer.
  615. Returns non-zero if successful, zero if there is
  616. no space left in the buffer. */
  617. int
  618. rl_unget_char (key)
  619. int key;
  620. {
  621. if (ibuffer_space ())
  622. {
  623. pop_index--;
  624. if (pop_index < 0)
  625. pop_index = ibuffer_len - 1;
  626. ibuffer[pop_index] = key;
  627. return (1);
  628. }
  629. return (0);
  630. }
  631. /* If a character is available to be read, then read it
  632. and stuff it into IBUFFER. Otherwise, just return. */
  633. rl_gather_tyi ()
  634. {
  635. #ifdef WIN32
  636. while (kbhit() && ibuffer_space()) {
  637. rl_stuff_char(winio_getc());
  638. }
  639. #else
  640. #if defined(__GO32__)
  641. char input;
  642. if (isatty(0))
  643. {
  644. int i = rl_getc();
  645. if (i != EOF)
  646. rl_stuff_char(i);
  647. }
  648. else
  649. if (kbhit() && ibuffer_space())
  650. rl_stuff_char(getkey());
  651. #else
  652. int tty = fileno (in_stream);
  653. register int tem, result = -1;
  654. long chars_avail;
  655. char input;
  656. #if defined (FIONREAD)
  657. result = ioctl (tty, FIONREAD, &chars_avail);
  658. #endif
  659. if (result == -1)
  660. {
  661. int flags;
  662. flags = fcntl (tty, F_GETFL, 0);
  663. fcntl (tty, F_SETFL, (flags | O_NDELAY));
  664. chars_avail = read (tty, &input, 1);
  665. fcntl (tty, F_SETFL, flags);
  666. if (chars_avail == -1 && errno == EAGAIN)
  667. return;
  668. }
  669. /* If there's nothing available, don't waste time trying to read
  670. something. */
  671. if (chars_avail == 0)
  672. return;
  673. tem = ibuffer_space ();
  674. if (chars_avail > tem)
  675. chars_avail = tem;
  676. /* One cannot read all of the available input. I can only read a single
  677. character at a time, or else programs which require input can be
  678. thwarted. If the buffer is larger than one character, I lose.
  679. Damn! */
  680. if (tem < ibuffer_len)
  681. chars_avail = 0;
  682. if (result != -1)
  683. {
  684. while (chars_avail--)
  685. rl_stuff_char (rl_getc (in_stream));
  686. }
  687. else
  688. {
  689. if (chars_avail)
  690. rl_stuff_char (input);
  691. }
  692. #endif /* def __GO32__/else */
  693. #endif
  694. }
  695. static int next_macro_key ();
  696. /* Read a key, including pending input. */
  697. int
  698. rl_read_key ()
  699. {
  700. int c;
  701. rl_key_sequence_length++;
  702. if (rl_pending_input)
  703. {
  704. c = rl_pending_input;
  705. rl_pending_input = 0;
  706. }
  707. else
  708. {
  709. /* If input is coming from a macro, then use that. */
  710. if (c = next_macro_key ())
  711. return (c);
  712. /* If the user has an event function, then call it periodically. */
  713. if (rl_event_hook)
  714. {
  715. while (rl_event_hook && !rl_get_char (&c))
  716. {
  717. (*rl_event_hook) ();
  718. if (!winio_rdln_ok()) {
  719. printf("%s", rl_prompt);
  720. printf("%s", the_line);
  721. }
  722. rl_gather_tyi ();
  723. }
  724. }
  725. else
  726. {
  727. if (!rl_get_char (&c))
  728. c = rl_getc (in_stream);
  729. }
  730. }
  731. return (c);
  732. }
  733. /* I'm beginning to hate the declaration rules for various compilers. */
  734. static void add_macro_char (), with_macro_input ();
  735. /* Do the command associated with KEY in MAP.
  736. If the associated command is really a keymap, then read
  737. another key, and dispatch into that map. */
  738. rl_dispatch (key, map)
  739. register int key;
  740. Keymap map;
  741. {
  742. if (defining_kbd_macro)
  743. add_macro_char (key);
  744. if (key > 127 && key < 256)
  745. {
  746. if (map[ESC].type == ISKMAP)
  747. {
  748. map = (Keymap)map[ESC].function;
  749. key -= 128;
  750. rl_dispatch (key, map);
  751. }
  752. else
  753. ding ();
  754. return;
  755. }
  756. switch (map[key].type)
  757. {
  758. case ISFUNC:
  759. {
  760. Function *func = map[key].function;
  761. if (func != (Function *)NULL)
  762. {
  763. /* Special case rl_do_lowercase_version (). */
  764. if (func == rl_do_lowercase_version)
  765. {
  766. rl_dispatch (to_lower (key), map);
  767. return;
  768. }
  769. (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  770. /* If we have input pending, then the last command was a prefix
  771. command. Don't change the state of rl_last_func. Otherwise,
  772. remember the last command executed in this variable. */
  773. if (!rl_pending_input)
  774. rl_last_func = map[key].function;
  775. }
  776. else
  777. {
  778. rl_abort ();
  779. return;
  780. }
  781. }
  782. break;
  783. case ISKMAP:
  784. if (map[key].function != (Function *)NULL)
  785. {
  786. int newkey;
  787. rl_key_sequence_length++;
  788. newkey = rl_read_key ();
  789. rl_dispatch (newkey, (Keymap)map[key].function);
  790. }
  791. else
  792. {
  793. rl_abort ();
  794. return;
  795. }
  796. break;
  797. case ISMACR:
  798. if (map[key].function != (Function *)NULL)
  799. {
  800. char *macro;
  801. macro = savestring ((char *)map[key].function);
  802. with_macro_input (macro);
  803. return;
  804. }
  805. break;
  806. }
  807. }
  808. /* **************************************************************** */
  809. /* */
  810. /* Hacking Keyboard Macros */
  811. /* */
  812. /* **************************************************************** */
  813. /* The currently executing macro string. If this is non-zero,
  814. then it is a malloc ()'ed string where input is coming from. */
  815. static char *executing_macro = (char *)NULL;
  816. /* The offset in the above string to the next character to be read. */
  817. static int executing_macro_index = 0;
  818. /* The current macro string being built. Characters get stuffed
  819. in here by add_macro_char (). */
  820. static char *current_macro = (char *)NULL;
  821. /* The size of the buffer allocated to current_macro. */
  822. static int current_macro_size = 0;
  823. /* The index at which characters are being added to current_macro. */
  824. static int current_macro_index = 0;
  825. /* A structure used to save nested macro strings.
  826. It is a linked list of string/index for each saved macro. */
  827. struct saved_macro {
  828. struct saved_macro *next;
  829. char *string;
  830. int index;
  831. };
  832. /* The list of saved macros. */
  833. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  834. /* Forward declarations of static functions. Thank you C. */
  835. static void push_executing_macro (), pop_executing_macro ();
  836. /* This one has to be declared earlier in the file. */
  837. /* static void add_macro_char (); */
  838. /* Set up to read subsequent input from STRING.
  839. STRING is free ()'ed when we are done with it. */
  840. static void
  841. with_macro_input (string)
  842. char *string;
  843. {
  844. push_executing_macro ();
  845. executing_macro = string;
  846. executing_macro_index = 0;
  847. }
  848. /* Return the next character available from a macro, or 0 if
  849. there are no macro characters. */
  850. static int
  851. next_macro_key ()
  852. {
  853. if (!executing_macro)
  854. return (0);
  855. if (!executing_macro[executing_macro_index])
  856. {
  857. pop_executing_macro ();
  858. return (next_macro_key ());
  859. }
  860. return (executing_macro[executing_macro_index++]);
  861. }
  862. /* Save the currently executing macro on a stack of saved macros. */
  863. static void
  864. push_executing_macro ()
  865. {
  866. struct saved_macro *saver;
  867. saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  868. saver->next = macro_list;
  869. saver->index = executing_macro_index;
  870. saver->string = executing_macro;
  871. macro_list = saver;
  872. }
  873. /* Discard the current macro, replacing it with the one
  874. on the top of the stack of saved macros. */
  875. static void
  876. pop_executing_macro ()
  877. {
  878. if (executing_macro)
  879. free (executing_macro);
  880. executing_macro = (char *)NULL;
  881. executing_macro_index = 0;
  882. if (macro_list)
  883. {
  884. struct saved_macro *disposer = macro_list;
  885. executing_macro = macro_list->string;
  886. executing_macro_index = macro_list->index;
  887. macro_list = macro_list->next;
  888. free (disposer);
  889. }
  890. }
  891. /* Add a character to the macro being built. */
  892. static void
  893. add_macro_char (c)
  894. int c;
  895. {
  896. if (current_macro_index + 1 >= current_macro_size)
  897. {
  898. if (!current_macro)
  899. current_macro = (char *)xmalloc (current_macro_size = 25);
  900. else
  901. current_macro =
  902. (char *)xrealloc (current_macro, current_macro_size += 25);
  903. }
  904. current_macro[current_macro_index++] = c;
  905. current_macro[current_macro_index] = '\0';
  906. }
  907. /* Begin defining a keyboard macro.
  908. Keystrokes are recorded as they are executed.
  909. End the definition with rl_end_kbd_macro ().
  910. If a numeric argument was explicitly typed, then append this
  911. definition to the end of the existing macro, and start by
  912. re-executing the existing macro. */
  913. rl_start_kbd_macro (ignore1, ignore2)
  914. int ignore1, ignore2;
  915. {
  916. if (defining_kbd_macro)
  917. rl_abort ();
  918. if (rl_explicit_arg)
  919. {
  920. if (current_macro)
  921. with_macro_input (savestring (current_macro));
  922. }
  923. else
  924. current_macro_index = 0;
  925. defining_kbd_macro = 1;
  926. }
  927. /* Stop defining a keyboard macro.
  928. A numeric argument says to execute the macro right now,
  929. that many times, counting the definition as the first time. */
  930. rl_end_kbd_macro (count, ignore)
  931. int count, ignore;
  932. {
  933. if (!defining_kbd_macro)
  934. rl_abort ();
  935. current_macro_index -= (rl_key_sequence_length - 1);
  936. current_macro[current_macro_index] = '\0';
  937. defining_kbd_macro = 0;
  938. rl_call_last_kbd_macro (--count, 0);
  939. }
  940. /* Execute the most recently defined keyboard macro.
  941. COUNT says how many times to execute it. */
  942. rl_call_last_kbd_macro (count, ignore)
  943. int count, ignore;
  944. {
  945. if (!current_macro)
  946. rl_abort ();
  947. while (count--)
  948. with_macro_input (savestring (current_macro));
  949. }
  950. /* **************************************************************** */
  951. /* */
  952. /* Initializations */
  953. /* */
  954. /* **************************************************************** */
  955. /* Initliaze readline (and terminal if not already). */
  956. rl_initialize ()
  957. {
  958. extern char *rl_display_prompt;
  959. /* If we have never been called before, initialize the
  960. terminal and data structures. */
  961. if (!rl_initialized)
  962. {
  963. readline_initialize_everything ();
  964. rl_initialized++;
  965. }
  966. /* Initalize the current line information. */
  967. rl_point = rl_end = 0;
  968. the_line = rl_line_buffer;
  969. the_line[0] = 0;
  970. /* We aren't done yet. We haven't even gotten started yet! */
  971. rl_done = 0;
  972. /* Tell the history routines what is going on. */
  973. start_using_history ();
  974. /* Make the display buffer match the state of the line. */
  975. {
  976. extern char *rl_display_prompt;
  977. extern int forced_display;
  978. rl_on_new_line ();
  979. rl_display_prompt = rl_prompt ? rl_prompt : "";
  980. forced_display = 1;
  981. }
  982. /* No such function typed yet. */
  983. rl_last_func = (Function *)NULL;
  984. /* Parsing of key-bindings begins in an enabled state. */
  985. parsing_conditionalized_out = 0;
  986. }
  987. /* Initialize the entire state of the world. */
  988. readline_initialize_everything ()
  989. {
  990. /* Find out if we are running in Emacs. */
  991. running_in_emacs = getenv ("EMACS");
  992. /* Set up input and output if they aren't already. */
  993. if (!rl_instream)
  994. rl_instream = stdin;
  995. if (!rl_outstream)
  996. rl_outstream = stdout;
  997. /* Allocate data structures. */
  998. if (!rl_line_buffer)
  999. rl_line_buffer =
  1000. (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1001. /* Initialize the terminal interface. */
  1002. init_terminal_io ((char *)NULL);
  1003. /* Bind tty characters to readline functions. */
  1004. readline_default_bindings ();
  1005. /* Initialize the function names. */
  1006. rl_initialize_funmap ();
  1007. /* Read in the init file. */
  1008. rl_read_init_file ((char *)NULL);
  1009. /* If the completion parser's default word break characters haven't
  1010. been set yet, then do so now. */
  1011. {
  1012. extern char *rl_completer_word_break_characters;
  1013. extern char *rl_basic_word_break_characters;
  1014. if (rl_completer_word_break_characters == (char *)NULL)
  1015. rl_completer_word_break_characters = rl_basic_word_break_characters;
  1016. }
  1017. }
  1018. /* If this system allows us to look at the values of the regular
  1019. input editing characters, then bind them to their readline
  1020. equivalents, iff the characters are not bound to keymaps. */
  1021. readline_default_bindings ()
  1022. {
  1023. #if !defined(__GO32__) && !defined(WIN32)
  1024. #if defined (NEW_TTY_DRIVER)
  1025. struct sgttyb ttybuff;
  1026. int tty = fileno (rl_instream);
  1027. if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1028. {
  1029. int erase, kill;
  1030. erase = ttybuff.sg_erase;
  1031. kill = ttybuff.sg_kill;
  1032. if (erase != -1 && keymap[erase].type == ISFUNC)
  1033. keymap[erase].function = rl_rubout;
  1034. if (kill != -1 && keymap[kill].type == ISFUNC)
  1035. keymap[kill].function = rl_unix_line_discard;
  1036. }
  1037. #if defined (TIOCGLTC)
  1038. {
  1039. struct ltchars lt;
  1040. if (ioctl (tty, TIOCGLTC, &lt) != -1)
  1041. {
  1042. int erase, nextc;
  1043. erase = lt.t_werasc;
  1044. nextc = lt.t_lnextc;
  1045. if (erase != -1 && keymap[erase].type == ISFUNC)
  1046. keymap[erase].function = rl_unix_word_rubout;
  1047. if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1048. keymap[nextc].function = rl_quoted_insert;
  1049. }
  1050. }
  1051. #endif /* TIOCGLTC */
  1052. #else /* not NEW_TTY_DRIVER */
  1053. #if defined (TERMIOS_TTY_DRIVER)
  1054. struct termios ttybuff;
  1055. #else
  1056. struct termio ttybuff;
  1057. #endif /* TERMIOS_TTY_DRIVER */
  1058. int tty = fileno (rl_instream);
  1059. #if defined (TERMIOS_TTY_DRIVER)
  1060. if (tcgetattr (tty, &ttybuff) != -1)
  1061. #else
  1062. if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1063. #endif /* !TERMIOS_TTY_DRIVER */
  1064. {
  1065. int erase, kill;
  1066. erase = ttybuff.c_cc[VERASE];
  1067. kill = ttybuff.c_cc[VKILL];
  1068. if (erase != _POSIX_VDISABLE &&
  1069. keymap[(unsigned char)erase].type == ISFUNC)
  1070. keymap[(unsigned char)erase].function = rl_rubout;
  1071. if (kill != _POSIX_VDISABLE &&
  1072. keymap[(unsigned char)kill].type == ISFUNC)
  1073. keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1074. #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  1075. {
  1076. int nextc;
  1077. nextc = ttybuff.c_cc[VLNEXT];
  1078. if (nextc != _POSIX_VDISABLE &&
  1079. keymap[(unsigned char)nextc].type == ISFUNC)
  1080. keymap[(unsigned char)nextc].function = rl_quoted_insert;
  1081. }
  1082. #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  1083. #if defined (VWERASE)
  1084. {
  1085. int werase;
  1086. werase = ttybuff.c_cc[VWERASE];
  1087. if (werase != _POSIX_VDISABLE &&
  1088. keymap[(unsigned char)werase].type == ISFUNC)
  1089. keymap[(unsigned char)werase].function = rl_unix_word_rubout;
  1090. }
  1091. #endif /* VWERASE */
  1092. }
  1093. #endif /* !NEW_TTY_DRIVER */
  1094. #endif /* def __GO32__ */
  1095. }
  1096. /* **************************************************************** */
  1097. /* */
  1098. /* Numeric Arguments */
  1099. /* */
  1100. /* **************************************************************** */
  1101. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1102. /* Add the current digit to the argument in progress. */
  1103. rl_digit_argument (ignore, key)
  1104. int ignore, key;
  1105. {
  1106. rl_pending_input = key;
  1107. rl_digit_loop ();
  1108. }
  1109. /* What to do when you abort reading an argument. */
  1110. rl_discard_argument ()
  1111. {
  1112. ding ();
  1113. rl_clear_message ();
  1114. rl_init_argument ();
  1115. }
  1116. /* Create a default argument. */
  1117. rl_init_argument ()
  1118. {
  1119. rl_numeric_arg = rl_arg_sign = 1;
  1120. rl_explicit_arg = 0;
  1121. }
  1122. /* C-u, universal argument. Multiply the current argument by 4.
  1123. Read a key. If the key has nothing to do with arguments, then
  1124. dispatch on it. If the key is the abort character then abort. */
  1125. rl_universal_argument ()
  1126. {
  1127. rl_numeric_arg *= 4;
  1128. rl_digit_loop ();
  1129. }
  1130. rl_digit_loop ()
  1131. {
  1132. int key, c;
  1133. while (1)
  1134. {
  1135. rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
  1136. key = c = rl_read_key ();
  1137. if (keymap[c].type == ISFUNC &&
  1138. keymap[c].function == rl_universal_argument)
  1139. {
  1140. rl_numeric_arg *= 4;
  1141. continue;
  1142. }
  1143. c = UNMETA (c);
  1144. if (numeric (c))
  1145. {
  1146. if (rl_explicit_arg)
  1147. rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1148. else
  1149. rl_numeric_arg = (c - '0');
  1150. rl_explicit_arg = 1;
  1151. }
  1152. else
  1153. {
  1154. if (c == '-' && !rl_explicit_arg)
  1155. {
  1156. rl_numeric_arg = 1;
  1157. rl_arg_sign = -1;
  1158. }
  1159. else
  1160. {
  1161. rl_clear_message ();
  1162. rl_dispatch (key, keymap);
  1163. return;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. /* **************************************************************** */
  1169. /* */
  1170. /* Display stuff */
  1171. /* */
  1172. /* **************************************************************** */
  1173. /* This is the stuff that is hard for me. I never seem to write good
  1174. display routines in C. Let's see how I do this time. */
  1175. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1176. the problems of input lines longer than the screen width.
  1177. update_line and the code that calls it makes a multiple line,
  1178. automatically wrapping line update. Carefull attention needs
  1179. to be paid to the vertical position variables.
  1180. handling of terminals with autowrap on (incl. DEC braindamage)
  1181. could be improved a bit. Right now I just cheat and decrement
  1182. screenwidth by one. */
  1183. /* Keep two buffers; one which reflects the current contents of the
  1184. screen, and the other to draw what we think the new contents should
  1185. be. Then compare the buffers, and make whatever changes to the
  1186. screen itself that we should. Finally, make the buffer that we
  1187. just drew into be the one which reflects the current contents of the
  1188. screen, and place the cursor where it belongs.
  1189. Commands that want to can fix the display themselves, and then let
  1190. this function know that the display has been fixed by setting the
  1191. RL_DISPLAY_FIXED variable. This is good for efficiency. */
  1192. /* Termcap variables: */
  1193. extern char *term_up, *term_dc, *term_cr;
  1194. extern int screenheight, screenwidth, terminal_can_insert;
  1195. /* What YOU turn on when you have handled all redisplay yourself. */
  1196. int rl_display_fixed = 0;
  1197. /* The visible cursor position. If you print some text, adjust this. */
  1198. int last_c_pos = 0;
  1199. int last_v_pos = 0;
  1200. /* The last left edge of text that was displayed. This is used when
  1201. doing horizontal scrolling. It shifts in thirds of a screenwidth. */
  1202. static int last_lmargin = 0;
  1203. /* The line display buffers. One is the line currently displayed on
  1204. the screen. The other is the line about to be displayed. */
  1205. static char *visible_line = (char *)NULL;
  1206. static char *invisible_line = (char *)NULL;
  1207. /* Number of lines currently on screen minus 1. */
  1208. int vis_botlin = 0;
  1209. /* A buffer for `modeline' messages. */
  1210. char msg_buf[128];
  1211. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1212. int forced_display = 0;
  1213. /* The stuff that gets printed out before the actual text of the line.
  1214. This is usually pointing to rl_prompt. */
  1215. char *rl_display_prompt = (char *)NULL;
  1216. /* Default and initial buffer size. Can grow. */
  1217. static int line_size = 1024;
  1218. /* Non-zero means to always use horizontal scrolling in line display. */
  1219. static int horizontal_scroll_mode = 0;
  1220. /* Non-zero means to display an asterisk at the starts of history lines
  1221. which have been modified. */
  1222. static int mark_modified_lines = 0;
  1223. /* Non-zero means to use a visible bell if one is available rather than
  1224. simply ringing the terminal bell. */
  1225. static int prefer_visible_bell = 0;
  1226. /* I really disagree with this, but my boss (among others) insists that we
  1227. support compilers that don't work. I don't think we are gaining by doing
  1228. so; what is the advantage in producing better code if we can't use it? */
  1229. /* The following two declarations belong inside the
  1230. function block, not here. */
  1231. static void move_cursor_relative ();
  1232. static void output_some_chars ();
  1233. static void output_character_function ();
  1234. static int compare_strings ();
  1235. /* Basic redisplay algorithm. */
  1236. rl_redisplay ()
  1237. {
  1238. register int in, out, c, linenum;
  1239. register char *line = invisible_line;
  1240. char *prompt_this_line;
  1241. int c_pos = 0;
  1242. int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
  1243. extern int readline_echoing_p;
  1244. if (!readline_echoing_p)
  1245. return;
  1246. if (!rl_display_prompt)
  1247. rl_display_prompt = "";
  1248. if (!invisible_line)
  1249. {
  1250. visible_line = (char *)xmalloc (line_size);
  1251. invisible_line = (char *)xmalloc (line_size);
  1252. line = invisible_line;
  1253. for (in = 0; in < line_size; in++)
  1254. {
  1255. visible_line[in] = 0;
  1256. invisible_line[in] = 1;
  1257. }
  1258. rl_on_new_line ();
  1259. }
  1260. /* Draw the line into the buffer. */
  1261. c_pos = -1;
  1262. /* Mark the line as modified or not. We only do this for history
  1263. lines. */
  1264. out = 0;
  1265. if (mark_modified_lines && current_history () && rl_undo_list)
  1266. {
  1267. line[out++] = '*';
  1268. line[out] = '\0';
  1269. }
  1270. /* If someone thought that the redisplay was handled, but the currently
  1271. visible line has a different modification state than the one about
  1272. to become visible, then correct the callers misconception. */
  1273. if (visible_line[0] != invisible_line[0])
  1274. rl_display_fixed = 0;
  1275. prompt_this_line = rindex (rl_display_prompt, '\n');
  1276. if (!prompt_this_line)
  1277. prompt_this_line = rl_display_prompt;
  1278. else
  1279. {
  1280. prompt_this_line++;
  1281. if (forced_display)
  1282. output_some_chars (rl_display_prompt,
  1283. prompt_this_line - rl_display_prompt);
  1284. }
  1285. strncpy (line + out, prompt_this_line, strlen (prompt_this_line));
  1286. out += strlen (prompt_this_line);
  1287. line[out] = '\0';
  1288. for (in = 0; in < rl_end; in++)
  1289. {
  1290. c = (unsigned char)the_line[in];
  1291. if (out + 1 >= line_size)
  1292. {
  1293. line_size *= 2;
  1294. visible_line = (char *)xrealloc (visible_line, line_size);
  1295. invisible_line = (char *)xrealloc (invisible_line, line_size);
  1296. line = invisible_line;
  1297. }
  1298. if (in == rl_point)
  1299. c_pos = out;
  1300. if (c > 127)
  1301. {
  1302. line[out++] = 'M';
  1303. line[out++] = '-';
  1304. line[out++] = c - 128;
  1305. }
  1306. #define DISPLAY_TABS
  1307. #if defined (DISPLAY_TABS)
  1308. else if (c == '\t')
  1309. {
  1310. register int newout = (out | (int)7) + 1;
  1311. while (out < newout)
  1312. line[out++] = ' ';
  1313. }
  1314. #endif
  1315. else if (c < 32)
  1316. {
  1317. line[out++] = 'C';
  1318. line[out++] = '-';
  1319. line[out++] = c + 64;
  1320. }
  1321. else if (c == 127)
  1322. {
  1323. line[out++] = 'C';
  1324. line[out++] = '-';
  1325. line[out++] = '?';
  1326. }
  1327. else
  1328. line[out++] = c;
  1329. }
  1330. line[out] = '\0';
  1331. if (c_pos < 0)
  1332. c_pos = out;
  1333. /* PWP: now is when things get a bit hairy. The visible and invisible
  1334. line buffers are really multiple lines, which would wrap every
  1335. (screenwidth - 1) characters. Go through each in turn, finding
  1336. the changed region and updating it. The line order is top to bottom. */
  1337. /* If we can move the cursor up and down, then use multiple lines,
  1338. otherwise, let long lines display in a single terminal line, and
  1339. horizontally scroll it. */
  1340. if (!horizontal_scroll_mode && term_up && *term_up)
  1341. {
  1342. int total_screen_chars = (screenwidth * screenheight);
  1343. if (!rl_display_fixed || forced_display)
  1344. {
  1345. forced_display = 0;
  1346. /* If we have more than a screenful of material to display, then
  1347. only display a screenful. We should display the last screen,
  1348. not the first. I'll fix this in a minute. */
  1349. if (out >= total_screen_chars)
  1350. out = total_screen_chars - 1;
  1351. /* Number of screen lines to display. */
  1352. inv_botlin = out / screenwidth;
  1353. /* For each line in the buffer, do the updating display. */
  1354. for (linenum = 0; linenum <= inv_botlin; linenum++)
  1355. update_line (linenum > vis_botlin ? ""
  1356. : &visible_line[linenum * screenwidth],
  1357. &invisible_line[linenum * screenwidth],
  1358. linenum);
  1359. /* We may have deleted some lines. If so, clear the left over
  1360. blank ones at the bottom out. */
  1361. if (vis_botlin > inv_botlin)
  1362. {
  1363. char *tt;
  1364. for (; linenum <= vis_botlin; linenum++)
  1365. {
  1366. tt = &visible_line[linenum * screenwidth];
  1367. move_vert (linenum);
  1368. move_cursor_relative (0, tt);
  1369. clear_to_eol ((linenum == vis_botlin)?
  1370. strlen (tt) : screenwidth);
  1371. }
  1372. }
  1373. vis_botlin = inv_botlin;
  1374. /* Move the cursor where it should be. */
  1375. move_vert (c_pos / screenwidth);
  1376. move_cursor_relative (c_pos % screenwidth,
  1377. &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1378. }
  1379. }
  1380. else /* Do horizontal scrolling. */
  1381. {
  1382. int lmargin;
  1383. /* Always at top line. */
  1384. last_v_pos = 0;
  1385. /* If the display position of the cursor would be off the edge
  1386. of the screen, start the display of this line at an offset that
  1387. leaves the cursor on the screen. */
  1388. if (c_pos - last_lmargin > screenwidth - 2)
  1389. lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1390. else if (c_pos - last_lmargin < 1)
  1391. lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1392. else
  1393. lmargin = last_lmargin;
  1394. /* If the first character on the screen isn't the first character
  1395. in the display line, indicate this with a special character. */
  1396. if (lmargin > 0)
  1397. line[lmargin] = '<';
  1398. if (lmargin + screenwidth < out)
  1399. line[lmargin + screenwidth - 1] = '>';
  1400. if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1401. {
  1402. forced_display = 0;
  1403. update_line (&visible_line[last_lmargin],
  1404. &invisible_line[lmargin], 0);
  1405. move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1406. last_lmargin = lmargin;
  1407. }
  1408. }
  1409. fflush (out_stream);
  1410. /* Swap visible and non-visible lines. */
  1411. {
  1412. char *temp = visible_line;
  1413. visible_line = invisible_line;
  1414. invisible_line = temp;
  1415. rl_display_fixed = 0;
  1416. }
  1417. }
  1418. /* PWP: update_line() is based on finding the middle difference of each
  1419. line on the screen; vis:
  1420. /old first difference
  1421. /beginning of line | /old last same /old EOL
  1422. v v v v
  1423. old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1424. new: eddie> Oh, my little buggy says to me, as lurgid as
  1425. ^ ^ ^ ^
  1426. \beginning of line | \new last same \new end of line
  1427. \new first difference
  1428. All are character pointers for the sake of speed. Special cases for
  1429. no differences, as well as for end of line additions must be handeled.
  1430. Could be made even smarter, but this works well enough */
  1431. static
  1432. update_line (old, new, current_line)
  1433. register char *old, *new;
  1434. int current_line;
  1435. {
  1436. register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1437. int lendiff, wsatend;
  1438. /* Find first difference. */
  1439. for (ofd = old, nfd = new;
  1440. (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1441. ofd++, nfd++)
  1442. ;
  1443. /* Move to the end of the screen line. */
  1444. for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1445. for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1446. /* If no difference, continue to next line. */
  1447. if (ofd == oe && nfd == ne)
  1448. return;
  1449. wsatend = 1; /* flag for trailing whitespace */
  1450. ols = oe - 1; /* find last same */
  1451. nls = ne - 1;
  1452. while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
  1453. {
  1454. if (*ols != ' ')
  1455. wsatend = 0;
  1456. ols--;
  1457. nls--;
  1458. }
  1459. if (wsatend)
  1460. {
  1461. ols = oe;
  1462. nls = ne;
  1463. }
  1464. else if (*ols != *nls)
  1465. {
  1466. if (*ols) /* don't step past the NUL */
  1467. ols++;
  1468. if (*nls)
  1469. nls++;
  1470. }
  1471. move_vert (current_line);
  1472. move_cursor_relative (ofd - old, old);
  1473. /* if (len (new) > len (old)) */
  1474. lendiff = (nls - nfd) - (ols - ofd);
  1475. /* Insert (diff(len(old),len(new)) ch */
  1476. if (lendiff > 0)
  1477. {
  1478. if (terminal_can_insert)
  1479. {
  1480. extern char *term_IC;
  1481. /* Sometimes it is cheaper to print the characters rather than
  1482. use the terminal's capabilities. */
  1483. if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1484. {
  1485. output_some_chars (nfd, (ne - nfd));
  1486. last_c_pos += (ne - nfd);
  1487. }
  1488. else
  1489. {
  1490. if (*ols)
  1491. {
  1492. insert_some_chars (nfd, lendiff);
  1493. last_c_pos += lendiff;
  1494. }
  1495. else
  1496. {
  1497. /* At the end of a line the characters do not have to
  1498. be "inserted". They can just be placed on the screen. */
  1499. output_some_chars (nfd, lendiff);
  1500. last_c_pos += lendiff;
  1501. }
  1502. /* Copy (new) chars to screen from first diff to last match. */
  1503. if (((nls - nfd) - lendiff) > 0)
  1504. {
  1505. output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1506. last_c_pos += ((nls - nfd) - lendiff);
  1507. }
  1508. }
  1509. }
  1510. else
  1511. { /* cannot insert chars, write to EOL */
  1512. output_some_chars (nfd, (ne - nfd));
  1513. last_c_pos += (ne - nfd);
  1514. }
  1515. }
  1516. else /* Delete characters from line. */
  1517. {
  1518. /* If possible and inexpensive to use terminal deletion, then do so. */
  1519. if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1520. {
  1521. if (lendiff)
  1522. delete_chars (-lendiff); /* delete (diff) characters */
  1523. /* Copy (new) chars to screen from first diff to last match */
  1524. if ((nls - nfd) > 0)
  1525. {
  1526. output_some_chars (nfd, (nls - nfd));
  1527. last_c_pos += (nls - nfd);
  1528. }
  1529. }
  1530. /* Otherwise, print over the existing material. */
  1531. else
  1532. {
  1533. output_some_chars (nfd, (ne - nfd));
  1534. last_c_pos += (ne - nfd);
  1535. clear_to_eol ((oe - old) - (ne - new));
  1536. }
  1537. }
  1538. }
  1539. /* (PWP) tell the update routines that we have moved onto a
  1540. new (empty) line. */
  1541. rl_on_new_line ()
  1542. {
  1543. if (visible_line)
  1544. visible_line[0] = '\0';
  1545. last_c_pos = last_v_pos = 0;
  1546. vis_botlin = last_lmargin = 0;
  1547. }
  1548. /* Actually update the display, period. */
  1549. rl_forced_update_display ()
  1550. {
  1551. if (visible_line)
  1552. {
  1553. register char *temp = visible_line;
  1554. while (*temp) *temp++ = '\0';
  1555. }
  1556. rl_on_new_line ();
  1557. forced_display++;
  1558. rl_redisplay ();
  1559. }
  1560. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1561. DATA is the contents of the screen line of interest; i.e., where
  1562. the movement is being done. */
  1563. static void
  1564. move_cursor_relative (new, data)
  1565. int new;
  1566. char *data;
  1567. {
  1568. #ifdef WIN32
  1569. winio_rel_move(new);
  1570. last_c_pos = new;
  1571. return;
  1572. #else
  1573. register int i;
  1574. #if GRX
  1575. if (egagrph) {
  1576. grx_rel_move(new);
  1577. last_c_pos = new;
  1578. return;
  1579. }
  1580. #endif
  1581. /* It may be faster to output a CR, and then move forwards instead
  1582. of moving backwards. */
  1583. if (new + 1 < last_c_pos - new)
  1584. {
  1585. #ifdef __MSDOS__
  1586. putc('\r', out_stream);
  1587. #else
  1588. tputs (term_cr, 1, output_character_function);
  1589. #endif
  1590. last_c_pos = 0;
  1591. }
  1592. if (last_c_pos == new) return;
  1593. if (last_c_pos < new)
  1594. {
  1595. /* Move the cursor forward. We do it by printing the command
  1596. to move the cursor forward if there is one, else print that
  1597. portion of the output buffer again. Which is cheaper? */
  1598. /* The above comment is left here for posterity. It is faster
  1599. to print one character (non-control) than to print a control
  1600. sequence telling the terminal to move forward one character.
  1601. That kind of control is for people who don't know what the
  1602. data is underneath the cursor. */
  1603. #if defined (HACK_TERMCAP_MOTION)
  1604. extern char *term_forward_char;
  1605. if (term_forward_char)
  1606. for (i = last_c_pos; i < new; i++)
  1607. tputs (term_forward_char, 1, output_character_function);
  1608. else
  1609. for (i = last_c_pos; i < new; i++)
  1610. putc (data[i], out_stream);
  1611. #else
  1612. for (i = last_c_pos; i < new; i++)
  1613. putc (data[i], out_stream);
  1614. #endif /* HACK_TERMCAP_MOTION */
  1615. }
  1616. else
  1617. backspace (last_c_pos - new);
  1618. last_c_pos = new;
  1619. #endif
  1620. }
  1621. /* PWP: move the cursor up or down. */
  1622. move_vert (to)
  1623. int to;
  1624. {
  1625. #ifdef WIN32
  1626. #else
  1627. void output_character_function ();
  1628. register int delta, i;
  1629. if (last_v_pos == to) return;
  1630. if (to > screenheight)
  1631. return;
  1632. #if defined(__GO32__)
  1633. {
  1634. int cur_r, cur_c;
  1635. ScreenGetCursor(&cur_r, &cur_c);
  1636. ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
  1637. }
  1638. #else /* __GO32__ */
  1639. if ((delta = to - last_v_pos) > 0)
  1640. {
  1641. for (i = 0; i < delta; i++)
  1642. putc ('\n', out_stream);
  1643. tputs (term_cr, 1, output_character_function);
  1644. last_c_pos = 0;
  1645. }
  1646. else
  1647. { /* delta < 0 */
  1648. if (term_up && *term_up)
  1649. for (i = 0; i < -delta; i++)
  1650. tputs (term_up, 1, output_

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