home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / readline / readline.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  158KB  |  6,653 lines

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