home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / readline / readline.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  154KB  |  6,495 lines

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