home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / readline / readline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-19  |  168.4 KB  |  7,086 lines

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