home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / READLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-20  |  77.9 KB  |  3,528 lines

  1. /* readline.c -- a general facility for reading lines of input
  2.    with emacs style editing and completion. */
  3.  
  4. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  5.  
  6.    This file is part of the GNU Readline Library, a library for
  7.    reading lines of text with interactive input and history editing.
  8.  
  9.    The GNU Readline Library is free software; you can redistribute it
  10.    and/or modify it under the terms of the GNU General Public License
  11.    as published by the Free Software Foundation; either version 1, or
  12.    (at your option) any later version.
  13.  
  14.    The GNU Readline Library is distributed in the hope that it will be
  15.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  16.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23. #define READLINE_LIBRARY
  24.  
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #if !defined (NO_SYS_FILE)
  29. #  include <sys/file.h>
  30. #endif /* !NO_SYS_FILE */
  31. #include <signal.h>
  32.  
  33. #if defined (HAVE_UNISTD_H)
  34. #  include <unistd.h>
  35. #endif /* HAVE_UNISTD_H */
  36.  
  37. #if defined (HAVE_STDLIB_H)
  38. #  include <stdlib.h>
  39. #else
  40. #  include "ansi_stdlib.h"
  41. #endif /* HAVE_STDLIB_H */
  42.  
  43. #include <errno.h>
  44. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  45. #if !defined (errno)
  46. extern int errno;
  47. #endif /* !errno */
  48.  
  49. #include <setjmp.h>
  50.  
  51. #include "posixstat.h"
  52.  
  53. /* System-specific feature definitions and include files. */
  54. #include "rldefs.h"
  55.  
  56. #if defined (GWINSZ_IN_SYS_IOCTL) || (defined (VSTATUS) && !defined (SunOS4))
  57. #  include <sys/ioctl.h>
  58. #endif /* GWINSZ_IN_SYS_IOCTL || VSTATUS */
  59.  
  60. /* Some standard library routines. */
  61. #include "readline.h"
  62. #include "history.h"
  63.  
  64. /* NOTE: Functions and variables prefixed with `_rl_' are
  65.    pseudo-global: they are global so they can be shared
  66.    between files in the readline library, but are not intended
  67.    to be visible to readline callers. */
  68.  
  69. /* Functions imported from other files in the library. */
  70. extern char *tgetstr ();
  71. extern void rl_prep_terminal (), rl_deprep_terminal ();
  72.  
  73. extern void _rl_bind_if_unbound ();
  74.  
  75. /* External redisplay functions and variables from display.c */
  76. extern void _rl_move_vert ();
  77. extern void _rl_update_final ();
  78.  
  79. extern void _rl_erase_at_end_of_line ();
  80. extern void _rl_move_cursor_relative ();
  81.  
  82. extern int _rl_vis_botlin;
  83. extern int _rl_last_c_pos;
  84. extern int _rl_horizontal_scroll_mode;
  85. extern int rl_display_fixed;
  86. extern char *rl_display_prompt;
  87.  
  88. /* Variables imported from complete.c. */
  89. extern char *rl_completer_word_break_characters;
  90. extern char *rl_basic_word_break_characters;
  91. extern int rl_completion_query_items;
  92. extern int rl_complete_with_tilde_expansion;
  93.  
  94. #if defined (VI_MODE)
  95. extern void _rl_vi_set_last ();
  96. extern void _rl_vi_reset_last ();
  97. extern void _rl_vi_done_inserting ();
  98. #endif /* VI_MODE */
  99.  
  100. /* Forward declarations used in this file. */
  101. void _rl_free_history_entry ();
  102.  
  103. int _rl_dispatch ();
  104. void _rl_set_screen_size ();
  105. int _rl_output_character_function ();
  106.  
  107. static char *readline_internal ();
  108. static void readline_initialize_everything ();
  109. static int init_terminal_io ();
  110. static void start_using_history ();
  111. static void bind_arrow_keys ();
  112.  
  113. #if !defined (__GO32__)
  114. static void readline_default_bindings ();
  115. #endif /* !__GO32__ */
  116.  
  117. #if defined (__GO32__)
  118. #  include <sys/pc.h>
  119. #  undef HANDLE_SIGNALS
  120. #endif /* __GO32__ */
  121.  
  122. #if defined (STATIC_MALLOC)
  123. static char *xmalloc (), *xrealloc ();
  124. #else
  125. extern char *xmalloc (), *xrealloc ();
  126. #endif /* STATIC_MALLOC */
  127.  
  128.  
  129. /* **************************************************************** */
  130. /*                                    */
  131. /*            Line editing input utility            */
  132. /*                                    */
  133. /* **************************************************************** */
  134.  
  135. static char *LibraryVersion = "2.0";
  136.  
  137. /* A pointer to the keymap that is currently in use.
  138.    By default, it is the standard emacs keymap. */
  139. Keymap _rl_keymap = emacs_standard_keymap;
  140.  
  141. /* The current style of editing. */
  142. int rl_editing_mode = emacs_mode;
  143.  
  144. /* Non-zero if the previous command was a kill command. */
  145. static int last_command_was_kill = 0;
  146.  
  147. /* The current value of the numeric argument specified by the user. */
  148. int rl_numeric_arg = 1;
  149.  
  150. /* Non-zero if an argument was typed. */
  151. int rl_explicit_arg = 0;
  152.  
  153. /* Temporary value used while generating the argument. */
  154. int rl_arg_sign = 1;
  155.  
  156. /* Non-zero means we have been called at least once before. */
  157. static int rl_initialized = 0;
  158.  
  159. /* If non-zero, this program is running in an EMACS buffer. */
  160. static int running_in_emacs = 0;
  161.  
  162. /* The current offset in the current input line. */
  163. int rl_point;
  164.  
  165. /* Mark in the current input line. */
  166. int rl_mark;
  167.  
  168. /* Length of the current input line. */
  169. int rl_end;
  170.  
  171. /* Make this non-zero to return the current input_line. */
  172. int rl_done;
  173.  
  174. /* The last function executed by readline. */
  175. Function *rl_last_func = (Function *)NULL;
  176.  
  177. /* Top level environment for readline_internal (). */
  178. static jmp_buf readline_top_level;
  179.  
  180. /* The streams we interact with. */
  181. static FILE *in_stream, *out_stream;
  182.  
  183. /* The names of the streams that we do input and output to. */
  184. FILE *rl_instream = (FILE *)NULL;
  185. FILE *rl_outstream = (FILE *)NULL;
  186.  
  187. /* Non-zero means echo characters as they are read. */
  188. int readline_echoing_p = 1;
  189.  
  190. /* Current prompt. */
  191. char *rl_prompt;
  192. int rl_visible_prompt_length = 0;
  193.  
  194. /* The number of characters read in order to type this complete command. */
  195. int rl_key_sequence_length = 0;
  196.  
  197. /* If non-zero, then this is the address of a function to call just
  198.    before readline_internal () prints the first prompt. */
  199. Function *rl_startup_hook = (Function *)NULL;
  200.  
  201. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  202. static char *the_line;
  203.  
  204. /* The character that can generate an EOF.  Really read from
  205.    the terminal driver... just defaulted here. */
  206. int _rl_eof_char = CTRL ('D');
  207.  
  208. /* Non-zero makes this the next keystroke to read. */
  209. int rl_pending_input = 0;
  210.  
  211. /* Pointer to a useful terminal name. */
  212. char *rl_terminal_name = (char *)NULL;
  213.  
  214. /* Non-zero means to always use horizontal scrolling in line display. */
  215. int _rl_horizontal_scroll_mode = 0;
  216.  
  217. /* Non-zero means to display an asterisk at the starts of history lines
  218.    which have been modified. */
  219. int _rl_mark_modified_lines = 0;  
  220.  
  221. /* The style of `bell' notification preferred.  This can be set to NO_BELL,
  222.    AUDIBLE_BELL, or VISIBLE_BELL. */
  223. int _rl_bell_preference = AUDIBLE_BELL;
  224.      
  225. /* Line buffer and maintenence. */
  226. char *rl_line_buffer = (char *)NULL;
  227. int rl_line_buffer_len = 0;
  228. #define DEFAULT_BUFFER_SIZE 256
  229.  
  230. /* Forward declarations used by the display and termcap code. */
  231. int term_xn;
  232. int screenwidth, screenheight, screenchars;
  233.  
  234.  
  235. /* **************************************************************** */
  236. /*                                    */
  237. /*            `Forward' declarations              */
  238. /*                                    */
  239. /* **************************************************************** */
  240.  
  241. /* Non-zero means do not parse any lines other than comments and
  242.    parser directives. */
  243. unsigned char _rl_parsing_conditionalized_out = 0;
  244.  
  245. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  246. static int defining_kbd_macro = 0;
  247.  
  248. /* Non-zero means to convert characters with the meta bit set to
  249.    escape-prefixed characters so we can indirect through
  250.    emacs_meta_keymap or vi_escape_keymap. */
  251. int _rl_convert_meta_chars_to_ascii = 1;
  252.  
  253. /* Non-zero means to output characters with the meta bit set directly
  254.    rather than as a meta-prefixed escape sequence. */
  255. int _rl_output_meta_chars = 0;
  256.  
  257. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  258.    the undo list. */
  259. static int doing_an_undo = 0;
  260.  
  261. /* **************************************************************** */
  262. /*                                    */
  263. /*            Top Level Functions                */
  264. /*                                    */
  265. /* **************************************************************** */
  266.  
  267. /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
  268. int _rl_meta_flag = 0;    /* Forward declaration */
  269.  
  270. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  271.    none.  A return value of NULL means that EOF was encountered. */
  272. char *
  273. readline (prompt)
  274.      char *prompt;
  275. {
  276.   char *value;
  277.  
  278.   rl_prompt = prompt;
  279.  
  280.   /* If we are at EOF return a NULL string. */
  281.   if (rl_pending_input == EOF)
  282.     {
  283.       rl_pending_input = 0;
  284.       return ((char *)NULL);
  285.     }
  286.  
  287.   rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
  288.  
  289.   rl_initialize ();
  290.   rl_prep_terminal (_rl_meta_flag);
  291.  
  292. #if defined (HANDLE_SIGNALS)
  293.   rl_set_signals ();
  294. #endif
  295.  
  296.   value = readline_internal ();
  297.   rl_deprep_terminal ();
  298.  
  299. #if defined (HANDLE_SIGNALS)
  300.   rl_clear_signals ();
  301. #endif
  302.  
  303.   return (value);
  304. }
  305.  
  306. /* Read a line of input from the global rl_instream, doing output on
  307.    the global rl_outstream.
  308.    If rl_prompt is non-null, then that is our prompt. */
  309. static char *
  310. readline_internal ()
  311. {
  312.   int lastc, c, eof_found;
  313.  
  314.   in_stream  = rl_instream;
  315.   out_stream = rl_outstream;
  316.  
  317.   lastc = -1;
  318.   eof_found = 0;
  319.  
  320.   if (rl_startup_hook)
  321.     (*rl_startup_hook) ();
  322.  
  323.   if (!readline_echoing_p)
  324.     {
  325.       if (rl_prompt)
  326.     {
  327.       fprintf (out_stream, "%s", rl_prompt);
  328.       fflush (out_stream);
  329.     }
  330.     }
  331.   else
  332.     {
  333.       rl_on_new_line ();
  334.       rl_redisplay ();
  335. #if defined (VI_MODE)
  336.       if (rl_editing_mode == vi_mode)
  337.     rl_vi_insertion_mode ();
  338. #endif /* VI_MODE */
  339.     }
  340.  
  341.   while (!rl_done)
  342.     {
  343.       int lk = last_command_was_kill;
  344.       int code;
  345.  
  346.       code = setjmp (readline_top_level);
  347.  
  348.       if (code)
  349.     rl_redisplay ();
  350.  
  351.       if (!rl_pending_input)
  352.     {
  353.       /* Then initialize the argument and number of keys read. */
  354.       rl_init_argument ();
  355.       rl_key_sequence_length = 0;
  356.     }
  357.  
  358.       c = rl_read_key ();
  359.  
  360.       /* EOF typed to a non-blank line is a <NL>. */
  361.       if (c == EOF && rl_end)
  362.     c = NEWLINE;
  363.  
  364.       /* The character _rl_eof_char typed to blank line, and not as the
  365.      previous character is interpreted as EOF. */
  366.       if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
  367.     {
  368.       eof_found = 1;
  369.       break;
  370.     }
  371.  
  372.       lastc = c;
  373.       _rl_dispatch (c, _rl_keymap);
  374.  
  375.       /* If there was no change in last_command_was_kill, then no kill
  376.      has taken place.  Note that if input is pending we are reading
  377.      a prefix command, so nothing has changed yet. */
  378.       if (!rl_pending_input)
  379.     {
  380.       if (lk == last_command_was_kill)
  381.         last_command_was_kill = 0;
  382.     }
  383.  
  384. #if defined (VI_MODE)
  385.       /* In vi mode, when you exit insert mode, the cursor moves back
  386.      over the previous character.  We explicitly check for that here. */
  387.       if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
  388.     rl_vi_check ();
  389. #endif /* VI_MODE */
  390.  
  391.       if (!rl_done)
  392.     rl_redisplay ();
  393.     }
  394.  
  395.   /* Restore the original of this history line, iff the line that we
  396.      are editing was originally in the history, AND the line has changed. */
  397.   {
  398.     HIST_ENTRY *entry = current_history ();
  399.  
  400.     if (entry && rl_undo_list)
  401.       {
  402.     char *temp = savestring (the_line);
  403.     rl_revert_line ();
  404.     entry = replace_history_entry (where_history (), the_line,
  405.                        (HIST_ENTRY *)NULL);
  406.     _rl_free_history_entry (entry);
  407.  
  408.     strcpy (the_line, temp);
  409.     free (temp);
  410.       }
  411.   }
  412.  
  413.   /* At any rate, it is highly likely that this line has an undo list.  Get
  414.      rid of it now. */
  415.   if (rl_undo_list)
  416.     free_undo_list ();
  417.  
  418.   if (eof_found)
  419.     return (char *)NULL;
  420.   else
  421.     return (savestring (the_line));
  422. }
  423.  
  424. /* **************************************************************** */
  425. /*                                    */
  426. /*            Character Input Buffering               */
  427. /*                                    */
  428. /* **************************************************************** */
  429.  
  430. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  431. static unsigned char ibuffer[512];
  432.  
  433. /* Non-null means it is a pointer to a function to run while waiting for
  434.    character input. */
  435. Function *rl_event_hook = (Function *)NULL;
  436.  
  437. #define any_typein (push_index != pop_index)
  438.  
  439. /* Add KEY to the buffer of characters to be read. */
  440. rl_stuff_char (key)
  441.      int key;
  442. {
  443.   if (key == EOF)
  444.     {
  445.       key = NEWLINE;
  446.       rl_pending_input = EOF;
  447.     }
  448.   ibuffer[push_index++] = key;
  449.   if (push_index >= ibuffer_len)
  450.     push_index = 0;
  451.   return push_index;
  452. }
  453.  
  454. /* Return the amount of space available in the
  455.    buffer for stuffing characters. */
  456. int
  457. ibuffer_space ()
  458. {
  459.   if (pop_index > push_index)
  460.     return (pop_index - push_index);
  461.   else
  462.     return (ibuffer_len - (push_index - pop_index));
  463. }
  464.  
  465. /* Get a key from the buffer of characters to be read.
  466.    Return the key in KEY.
  467.    Result is KEY if there was a key, or 0 if there wasn't. */
  468. int
  469. rl_get_char (key)
  470.      int *key;
  471. {
  472.   if (push_index == pop_index)
  473.     return (0);
  474.  
  475.   *key = ibuffer[pop_index++];
  476.  
  477.   if (pop_index >= ibuffer_len)
  478.     pop_index = 0;
  479.  
  480.   return (1);
  481. }
  482.  
  483. /* Stuff KEY into the *front* of the input buffer.
  484.    Returns non-zero if successful, zero if there is
  485.    no space left in the buffer. */
  486. int
  487. rl_unget_char (key)
  488.      int key;
  489. {
  490.   if (ibuffer_space ())
  491.     {
  492.       pop_index--;
  493.       if (pop_index < 0)
  494.     pop_index = ibuffer_len - 1;
  495.       ibuffer[pop_index] = key;
  496.       return (1);
  497.     }
  498.   return (0);
  499. }
  500.  
  501. /* If a character is available to be read, then read it
  502.    and stuff it into IBUFFER.  Otherwise, just return. */
  503. void
  504. rl_gather_tyi ()
  505. {
  506. #if defined (__GO32__)
  507.   char input;
  508.  
  509.   if (isatty (0))
  510.     {
  511.       int i = rl_getc ();
  512.  
  513.       if (i != EOF)
  514.     rl_stuff_char (i);
  515.     }
  516.   else if (kbhit () && ibuffer_space ())
  517.     rl_stuff_char (getkey ());
  518. #else /* !__GO32__ */
  519.  
  520.   int tty = fileno (in_stream);
  521.   register int tem, result = -1;
  522.   int chars_avail;
  523.   char input;
  524.  
  525. #if defined (FIONREAD)
  526.   result = ioctl (tty, FIONREAD, &chars_avail);
  527. #endif
  528.  
  529. #if defined (O_NDELAY)
  530.   if (result == -1)
  531.     {
  532.       int flags;
  533.  
  534.       flags = fcntl (tty, F_GETFL, 0);
  535.  
  536.       fcntl (tty, F_SETFL, (flags | O_NDELAY));
  537.       chars_avail = read (tty, &input, 1);
  538.  
  539.       fcntl (tty, F_SETFL, flags);
  540.       if (chars_avail == -1 && errno == EAGAIN)
  541.     return;
  542.     }
  543. #endif /* O_NDELAY */
  544.  
  545.   /* If there's nothing available, don't waste time trying to read
  546.      something. */
  547.   if (chars_avail == 0)
  548.     return;
  549.  
  550.   tem = ibuffer_space ();
  551.  
  552.   if (chars_avail > tem)
  553.     chars_avail = tem;
  554.  
  555.   /* One cannot read all of the available input.  I can only read a single
  556.      character at a time, or else programs which require input can be
  557.      thwarted.  If the buffer is larger than one character, I lose.
  558.      Damn! */
  559.   if (tem < ibuffer_len)
  560.     chars_avail = 0;
  561.  
  562.   if (result != -1)
  563.     {
  564.       while (chars_avail--)
  565.     rl_stuff_char (rl_getc (in_stream));
  566.     }
  567.   else
  568.     {
  569.       if (chars_avail)
  570.     rl_stuff_char (input);
  571.     }
  572. #endif /* !__GO32__ */
  573. }
  574.  
  575. static int next_macro_key ();
  576. /* Read a key, including pending input. */
  577. int
  578. rl_read_key ()
  579. {
  580.   int c;
  581.  
  582.   rl_key_sequence_length++;
  583.  
  584.   if (rl_pending_input)
  585.     {
  586.       c = rl_pending_input;
  587.       rl_pending_input = 0;
  588.     }
  589.   else
  590.     {
  591.       /* If input is coming from a macro, then use that. */
  592.       if (c = next_macro_key ())
  593.     return (c);
  594.  
  595.       /* If the user has an event function, then call it periodically. */
  596.       if (rl_event_hook)
  597.     {
  598.       while (rl_event_hook && !rl_get_char (&c))
  599.         {
  600.           (*rl_event_hook) ();
  601.           rl_gather_tyi ();
  602.         }
  603.     }
  604.       else
  605.     {
  606.       if (!rl_get_char (&c))
  607.         c = rl_getc (in_stream);
  608.     }
  609.     }
  610.  
  611.   return (c);
  612. }
  613.  
  614. /* Found later in this file. */
  615. static void add_macro_char (), with_macro_input ();
  616.  
  617. /* Do the command associated with KEY in MAP.
  618.    If the associated command is really a keymap, then read
  619.    another key, and dispatch into that map. */
  620. int
  621. _rl_dispatch (key, map)
  622.      register int key;
  623.      Keymap map;
  624. {
  625.   int r = 0;
  626.  
  627.   if (defining_kbd_macro)
  628.     add_macro_char (key);
  629.  
  630.   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
  631.     {
  632.       if (map[ESC].type == ISKMAP)
  633.     {
  634.       map = FUNCTION_TO_KEYMAP (map, ESC);
  635.       key = UNMETA (key);
  636.       rl_key_sequence_length += 2;
  637.       return (_rl_dispatch (key, map));
  638.     }
  639.       else
  640.     ding ();
  641.       return 0;
  642.     }
  643.  
  644.   switch (map[key].type)
  645.     {
  646.     case ISFUNC:
  647.       {
  648.     Function *func = map[key].function;
  649.  
  650.     if (func != (Function *)NULL)
  651.       {
  652.         /* Special case rl_do_lowercase_version (). */
  653.         if (func == rl_do_lowercase_version)
  654.           return (_rl_dispatch (to_lower (key), map));
  655.  
  656.         r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  657.  
  658.         /* If we have input pending, then the last command was a prefix
  659.            command.  Don't change the state of rl_last_func.  Otherwise,
  660.            remember the last command executed in this variable. */
  661.         if (!rl_pending_input)
  662.           rl_last_func = map[key].function;
  663.       }
  664.     else
  665.       {
  666.         rl_abort ();
  667.         return -1;
  668.       }
  669.       }
  670.       break;
  671.  
  672.     case ISKMAP:
  673.       if (map[key].function != (Function *)NULL)
  674.     {
  675.       int newkey;
  676.  
  677.       rl_key_sequence_length++;
  678.       newkey = rl_read_key ();
  679.       r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
  680.     }
  681.       else
  682.     {
  683.       rl_abort ();
  684.       return -1;
  685.     }
  686.       break;
  687.  
  688.     case ISMACR:
  689.       if (map[key].function != (Function *)NULL)
  690.     {
  691.       char *macro;
  692.  
  693.       macro = savestring ((char *)map[key].function);
  694.       with_macro_input (macro);
  695.       return 0;
  696.     }
  697.       break;
  698.     }
  699. #if defined (VI_MODE)
  700.   if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
  701.       rl_vi_textmod_command (key))
  702.     _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
  703. #endif
  704.   return (r);
  705. }
  706.  
  707.  
  708. /* **************************************************************** */
  709. /*                                    */
  710. /*            Hacking Keyboard Macros             */
  711. /*                                    */
  712. /* **************************************************************** */
  713.  
  714. /* The currently executing macro string.  If this is non-zero,
  715.    then it is a malloc ()'ed string where input is coming from. */
  716. static char *executing_macro = (char *)NULL;
  717.  
  718. /* The offset in the above string to the next character to be read. */
  719. static int executing_macro_index = 0;
  720.  
  721. /* The current macro string being built.  Characters get stuffed
  722.    in here by add_macro_char (). */
  723. static char *current_macro = (char *)NULL;
  724.  
  725. /* The size of the buffer allocated to current_macro. */
  726. static int current_macro_size = 0;
  727.  
  728. /* The index at which characters are being added to current_macro. */
  729. static int current_macro_index = 0;
  730.  
  731. /* A structure used to save nested macro strings.
  732.    It is a linked list of string/index for each saved macro. */
  733. struct saved_macro {
  734.   struct saved_macro *next;
  735.   char *string;
  736.   int sindex;
  737. };
  738.  
  739. /* The list of saved macros. */
  740. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  741.  
  742. /* Forward declarations of static functions.  Thank you C. */
  743. static void push_executing_macro (), pop_executing_macro ();
  744.  
  745. /* This one has to be declared earlier in the file. */
  746. /* static void add_macro_char (); */
  747.  
  748. /* Set up to read subsequent input from STRING.
  749.    STRING is free ()'ed when we are done with it. */
  750. static void
  751. with_macro_input (string)
  752.      char *string;
  753. {
  754.   push_executing_macro ();
  755.   executing_macro = string;
  756.   executing_macro_index = 0;
  757. }
  758.  
  759. /* Return the next character available from a macro, or 0 if
  760.    there are no macro characters. */
  761. static int
  762. next_macro_key ()
  763. {
  764.   if (!executing_macro)
  765.     return (0);
  766.  
  767.   if (!executing_macro[executing_macro_index])
  768.     {
  769.       pop_executing_macro ();
  770.       return (next_macro_key ());
  771.     }
  772.  
  773.   return (executing_macro[executing_macro_index++]);
  774. }
  775.  
  776. /* Save the currently executing macro on a stack of saved macros. */
  777. static void
  778. push_executing_macro ()
  779. {
  780.   struct saved_macro *saver;
  781.  
  782.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  783.   saver->next = macro_list;
  784.   saver->sindex = executing_macro_index;
  785.   saver->string = executing_macro;
  786.  
  787.   macro_list = saver;
  788. }
  789.  
  790. /* Discard the current macro, replacing it with the one
  791.    on the top of the stack of saved macros. */
  792. static void
  793. pop_executing_macro ()
  794. {
  795.   if (executing_macro)
  796.     free (executing_macro);
  797.  
  798.   executing_macro = (char *)NULL;
  799.   executing_macro_index = 0;
  800.  
  801.   if (macro_list)
  802.     {
  803.       struct saved_macro *disposer = macro_list;
  804.       executing_macro = macro_list->string;
  805.       executing_macro_index = macro_list->sindex;
  806.       macro_list = macro_list->next;
  807.       free (disposer);
  808.     }
  809. }
  810.  
  811. /* Add a character to the macro being built. */
  812. static void
  813. add_macro_char (c)
  814.      int c;
  815. {
  816.   if (current_macro_index + 1 >= current_macro_size)
  817.     {
  818.       if (!current_macro)
  819.     current_macro = xmalloc (current_macro_size = 25);
  820.       else
  821.     current_macro = xrealloc (current_macro, current_macro_size += 25);
  822.     }
  823.  
  824.   current_macro[current_macro_index++] = c;
  825.   current_macro[current_macro_index] = '\0';
  826. }
  827.  
  828. /* Begin defining a keyboard macro.
  829.    Keystrokes are recorded as they are executed.
  830.    End the definition with rl_end_kbd_macro ().
  831.    If a numeric argument was explicitly typed, then append this
  832.    definition to the end of the existing macro, and start by
  833.    re-executing the existing macro. */
  834. rl_start_kbd_macro (ignore1, ignore2)
  835.      int ignore1, ignore2;
  836. {
  837.   if (defining_kbd_macro)
  838.     {
  839.       rl_abort ();
  840.       return -1;
  841.     }
  842.  
  843.   if (rl_explicit_arg)
  844.     {
  845.       if (current_macro)
  846.     with_macro_input (savestring (current_macro));
  847.     }
  848.   else
  849.     current_macro_index = 0;
  850.  
  851.   defining_kbd_macro = 1;
  852.   return 0;
  853. }
  854.  
  855. /* Stop defining a keyboard macro.
  856.    A numeric argument says to execute the macro right now,
  857.    that many times, counting the definition as the first time. */
  858. rl_end_kbd_macro (count, ignore)
  859.      int count, ignore;
  860. {
  861.   if (!defining_kbd_macro)
  862.     {
  863.       rl_abort ();
  864.       return -1;
  865.     }
  866.  
  867.   current_macro_index -= (rl_key_sequence_length - 1);
  868.   current_macro[current_macro_index] = '\0';
  869.  
  870.   defining_kbd_macro = 0;
  871.  
  872.   return (rl_call_last_kbd_macro (--count, 0));
  873. }
  874.  
  875. /* Execute the most recently defined keyboard macro.
  876.    COUNT says how many times to execute it. */
  877. rl_call_last_kbd_macro (count, ignore)
  878.      int count, ignore;
  879. {
  880.   if (!current_macro)
  881.     rl_abort ();
  882.  
  883.   if (defining_kbd_macro)
  884.     {
  885.       ding ();        /* no recursive macros */
  886.       current_macro[--current_macro_index] = '\0';    /* erase this char */
  887.       return 0;
  888.     }
  889.  
  890.   while (count--)
  891.     with_macro_input (savestring (current_macro));
  892.   return 0;
  893. }
  894.  
  895. void
  896. _rl_kill_kbd_macro ()
  897. {
  898.   if (current_macro)
  899.     {
  900.       free (current_macro);
  901.       current_macro = (char *) NULL;
  902.     }
  903.   current_macro_size = current_macro_index = 0;
  904.  
  905.   if (executing_macro)
  906.     {
  907.       free (executing_macro);
  908.       executing_macro = (char *) NULL;
  909.     }
  910.   executing_macro_index = 0;
  911.  
  912.   defining_kbd_macro = 0;
  913. }
  914.  
  915. /* **************************************************************** */
  916. /*                                    */
  917. /*            Initializations                 */
  918. /*                                    */
  919. /* **************************************************************** */
  920.  
  921. /* Initliaze readline (and terminal if not already). */
  922. rl_initialize ()
  923. {
  924.   char *t;
  925.  
  926.   /* If we have never been called before, initialize the
  927.      terminal and data structures. */
  928.   if (!rl_initialized)
  929.     {
  930.       readline_initialize_everything ();
  931.       rl_initialized++;
  932.     }
  933.  
  934.   /* Initalize the current line information. */
  935.   rl_point = rl_end = 0;
  936.   the_line = rl_line_buffer;
  937.   the_line[0] = 0;
  938.  
  939.   /* We aren't done yet.  We haven't even gotten started yet! */
  940.   rl_done = 0;
  941.  
  942.   /* Check for LC_CTYPE and use its value to decide the defaults for
  943.      8-bit character input and output. */
  944.   t = getenv ("LC_CTYPE");
  945.   if (t && (strcmp (t, "iso-8859-1") == 0 || strcmp (t, "iso_8859_1") == 0))
  946.     {
  947.       _rl_meta_flag = 1;
  948.       _rl_convert_meta_chars_to_ascii = 0;
  949.       _rl_output_meta_chars = 1;
  950.     }
  951.       
  952.   /* Tell the history routines what is going on. */
  953.   start_using_history ();
  954.  
  955.   /* Make the display buffer match the state of the line. */
  956.   rl_reset_line_state ();
  957.  
  958.   /* No such function typed yet. */
  959.   rl_last_func = (Function *)NULL;
  960.  
  961.   /* Parsing of key-bindings begins in an enabled state. */
  962.   _rl_parsing_conditionalized_out = 0;
  963.  
  964.   return 0;
  965. }
  966.  
  967. /* Initialize the entire state of the world. */
  968. static void
  969. readline_initialize_everything ()
  970. {
  971.   /* Find out if we are running in Emacs. */
  972.   running_in_emacs = getenv ("EMACS") != (char *)0;
  973.  
  974.   /* Set up input and output if they are not already set up. */
  975.   if (!rl_instream)
  976.     rl_instream = stdin;
  977.  
  978.   if (!rl_outstream)
  979.     rl_outstream = stdout;
  980.  
  981.   /* Bind in_stream and out_stream immediately.  These values may change,
  982.      but they may also be used before readline_internal () is called. */
  983.   in_stream = rl_instream;
  984.   out_stream = rl_outstream;
  985.  
  986.   /* Allocate data structures. */
  987.   if (!rl_line_buffer)
  988.     rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  989.  
  990.   /* Initialize the terminal interface. */
  991.   init_terminal_io ((char *)NULL);
  992.  
  993. #if !defined (__GO32__)
  994.   /* Bind tty characters to readline functions. */
  995.   readline_default_bindings ();
  996. #endif /* !__GO32__ */
  997.  
  998.   /* Initialize the function names. */
  999.   rl_initialize_funmap ();
  1000.  
  1001.   /* Read in the init file. */
  1002.   rl_read_init_file ((char *)NULL);
  1003.  
  1004.   /* XXX */
  1005.   if (_rl_horizontal_scroll_mode && term_xn)
  1006.     {
  1007.       screenwidth--;
  1008.       screenchars -= screenheight;
  1009.     }
  1010.  
  1011.   /* Override the effect of any `set keymap' assignments in the
  1012.      inputrc file. */
  1013.   rl_set_keymap_from_edit_mode ();
  1014.  
  1015.   /* Try to bind a common arrow key prefix, if not already bound. */
  1016.   bind_arrow_keys ();
  1017.  
  1018.   /* If the completion parser's default word break characters haven't
  1019.      been set yet, then do so now. */
  1020.   if (rl_completer_word_break_characters == (char *)NULL)
  1021.     rl_completer_word_break_characters = rl_basic_word_break_characters;
  1022. }
  1023.  
  1024. /* If this system allows us to look at the values of the regular
  1025.    input editing characters, then bind them to their readline
  1026.    equivalents, iff the characters are not bound to keymaps. */
  1027. static void
  1028. readline_default_bindings ()
  1029. {
  1030.   rltty_set_default_bindings (_rl_keymap);
  1031. }
  1032.  
  1033. static void
  1034. bind_arrow_keys_internal ()
  1035. {
  1036.   Function *f;
  1037.  
  1038.   f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
  1039.   if (!f || f == rl_do_lowercase_version)
  1040.     {
  1041.       _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
  1042.       _rl_bind_if_unbound ("\033[B", rl_get_next_history);
  1043.       _rl_bind_if_unbound ("\033[C", rl_forward);
  1044.       _rl_bind_if_unbound ("\033[D", rl_backward);
  1045.     }
  1046.  
  1047.   f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
  1048.   if (!f || f == rl_do_lowercase_version)
  1049.     {
  1050.       _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
  1051.       _rl_bind_if_unbound ("\033OB", rl_get_next_history);
  1052.       _rl_bind_if_unbound ("\033OC", rl_forward);
  1053.       _rl_bind_if_unbound ("\033OD", rl_backward);
  1054.     }
  1055. }
  1056.  
  1057. /* Try and bind the common arrow key prefix after giving termcap and
  1058.    the inputrc file a chance to bind them and create `real' keymaps
  1059.    for the arrow key prefix. */
  1060. static void
  1061. bind_arrow_keys ()
  1062. {
  1063.   Keymap xkeymap;
  1064.  
  1065.   xkeymap = _rl_keymap;
  1066.  
  1067.   _rl_keymap = emacs_standard_keymap;
  1068.   bind_arrow_keys_internal ();
  1069.  
  1070. #if defined (VI_MODE)
  1071.   _rl_keymap = vi_movement_keymap;
  1072.   bind_arrow_keys_internal ();
  1073. #endif
  1074.  
  1075.   _rl_keymap = xkeymap;
  1076. }
  1077.  
  1078.  
  1079. /* **************************************************************** */
  1080. /*                                    */
  1081. /*            Numeric Arguments                */
  1082. /*                                    */
  1083. /* **************************************************************** */
  1084.  
  1085. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1086. static int
  1087. rl_digit_loop ()
  1088. {
  1089.   int key, c;
  1090.  
  1091.   while (1)
  1092.     {
  1093.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
  1094.       key = c = rl_read_key ();
  1095.  
  1096.       if (_rl_keymap[c].type == ISFUNC &&
  1097.       _rl_keymap[c].function == rl_universal_argument)
  1098.     {
  1099.       rl_numeric_arg *= 4;
  1100.       continue;
  1101.     }
  1102.       c = UNMETA (c);
  1103.       if (digit_p (c))
  1104.     {
  1105.       if (rl_explicit_arg)
  1106.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1107.       else
  1108.         rl_numeric_arg = (c - '0');
  1109.       rl_explicit_arg = 1;
  1110.     }
  1111.       else
  1112.     {
  1113.       if (c == '-' && !rl_explicit_arg)
  1114.         {
  1115.           rl_numeric_arg = 1;
  1116.           rl_arg_sign = -1;
  1117.         }
  1118.       else
  1119.         {
  1120.           rl_clear_message ();
  1121.           return (_rl_dispatch (key, _rl_keymap));
  1122.         }
  1123.     }
  1124.     }
  1125.   return 0;
  1126. }
  1127.  
  1128. /* Add the current digit to the argument in progress. */
  1129. rl_digit_argument (ignore, key)
  1130.      int ignore, key;
  1131. {
  1132.   rl_pending_input = key;
  1133.   return (rl_digit_loop ());
  1134. }
  1135.  
  1136. /* What to do when you abort reading an argument. */
  1137. rl_discard_argument ()
  1138. {
  1139.   ding ();
  1140.   rl_clear_message ();
  1141.   rl_init_argument ();
  1142.   return 0;
  1143. }
  1144.  
  1145. /* Create a default argument. */
  1146. rl_init_argument ()
  1147. {
  1148.   rl_numeric_arg = rl_arg_sign = 1;
  1149.   rl_explicit_arg = 0;
  1150.   return 0;
  1151. }
  1152.  
  1153. /* C-u, universal argument.  Multiply the current argument by 4.
  1154.    Read a key.  If the key has nothing to do with arguments, then
  1155.    dispatch on it.  If the key is the abort character then abort. */
  1156. rl_universal_argument ()
  1157. {
  1158.   rl_numeric_arg *= 4;
  1159.   return (rl_digit_loop ());
  1160. }
  1161.  
  1162. /* **************************************************************** */
  1163. /*                                    */
  1164. /*            Terminal and Termcap                */
  1165. /*                                    */
  1166. /* **************************************************************** */
  1167.  
  1168. static char *term_buffer = (char *)NULL;
  1169. static char *term_string_buffer = (char *)NULL;
  1170.  
  1171. static int tcap_initialized = 0;
  1172.  
  1173. /* Non-zero means this terminal can't really do anything. */
  1174. int dumb_term = 0;
  1175. /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
  1176.    Unfortunately, PC is a global variable used by the termcap library. */
  1177. #undef PC
  1178.  
  1179. #if !defined (__linux__)
  1180. /* If this causes problems, remove the `extern'. */
  1181. extern char PC, *BC, *UP;
  1182. #endif /* __linux__ */
  1183.  
  1184. /* Some strings to control terminal actions.  These are output by tputs (). */
  1185. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1186. char *term_pc;
  1187.  
  1188. /* Non-zero if we determine that the terminal can do character insertion. */
  1189. int terminal_can_insert = 0;
  1190.  
  1191. /* How to insert characters. */
  1192. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  1193.  
  1194. /* How to delete characters. */
  1195. char *term_dc, *term_DC;
  1196.  
  1197. #if defined (HACK_TERMCAP_MOTION)
  1198. char *term_forward_char;
  1199. #endif  /* HACK_TERMCAP_MOTION */
  1200.  
  1201. /* How to go up a line. */
  1202. char *term_up;
  1203.  
  1204. /* A visible bell, if the terminal can be made to flash the screen. */
  1205. char *visible_bell;
  1206.  
  1207. /* Non-zero means that this terminal has a meta key. */
  1208. int term_has_meta;
  1209.  
  1210. /* The string to write to turn on the meta key, if this term has one. */
  1211. char *term_mm;
  1212.  
  1213. /* The string to write to turn off the meta key, if this term has one. */
  1214. char *term_mo;
  1215.  
  1216. /* The key sequences output by the arrow keys, if this terminal has any. */
  1217. char *term_ku, *term_kd, *term_kr, *term_kl;
  1218.  
  1219. /* How to initialize and reset the arrow keys, if this terminal has any. */
  1220. char *term_ks, *term_ke;
  1221.  
  1222. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  1223.    has changed. */
  1224. rl_reset_terminal (terminal_name)
  1225.      char *terminal_name;
  1226. {
  1227.   init_terminal_io (terminal_name);
  1228.   return 0;
  1229. }
  1230.  
  1231. /* Set readline's idea of the screen size.  TTY is a file descriptor open
  1232.    to the terminal.  If IGNORE_ENV is true, we do not pay attention to the
  1233.    values of $LINES and $COLUMNS.  The tests for TERM_STRING_BUFFER being
  1234.    non-null serve to check whether or not we have initialized termcap. */
  1235. void
  1236. _rl_set_screen_size (tty, ignore_env)
  1237.      int tty, ignore_env;
  1238. {
  1239. #if defined (TIOCGWINSZ)
  1240.   struct winsize window_size;
  1241. #endif /* TIOCGWINSZ */
  1242.  
  1243. #if defined (TIOCGWINSZ)
  1244.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  1245.     {
  1246.       screenwidth = (int) window_size.ws_col;
  1247.       screenheight = (int) window_size.ws_row;
  1248.     }
  1249. #endif /* TIOCGWINSZ */
  1250.  
  1251.   /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
  1252.      is unset. */
  1253.   if (screenwidth <= 0)
  1254.     {
  1255.       char *sw;
  1256.  
  1257.       if (!ignore_env && (sw = getenv ("COLUMNS")))
  1258.     screenwidth = atoi (sw);
  1259.  
  1260.       if (screenwidth <= 0 && term_string_buffer)
  1261.     screenwidth = tgetnum ("co");
  1262.     }
  1263.  
  1264.   /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
  1265.      is unset. */
  1266.   if (screenheight <= 0)
  1267.     {
  1268.       char *sh;
  1269.  
  1270.       if (!ignore_env && (sh = getenv ("LINES")))
  1271.     screenheight = atoi (sh);
  1272.  
  1273.       if (screenheight <= 0 && term_string_buffer)
  1274.     screenheight = tgetnum ("li");
  1275.     }
  1276.  
  1277.   /* If all else fails, default to 80x24 terminal. */
  1278.   if (screenwidth <= 1)
  1279.     screenwidth = 80;
  1280.  
  1281.   if (screenheight <= 0)
  1282.     screenheight = 24;
  1283.  
  1284. #if defined (SHELL)
  1285.   /* If we're being compiled as part of bash, set the environment
  1286.      variables $LINES and $COLUMNS to new values. */
  1287.   set_lines_and_columns (screenheight, screenwidth);
  1288. #endif
  1289.  
  1290.   if (!term_xn)
  1291.     screenwidth--;
  1292.  
  1293.   screenchars = screenwidth * screenheight;
  1294. }
  1295.  
  1296. struct _tc_string {
  1297.      char *tc_var;
  1298.      char **tc_value;
  1299. };
  1300.  
  1301. /* This should be kept sorted, just in case we decide to change the
  1302.    search algorithm to something smarter. */
  1303. static struct _tc_string tc_strings[] =
  1304. {
  1305.   "DC", &term_DC,
  1306.   "IC", &term_IC,
  1307.   "ce", &term_clreol,
  1308.   "cl", &term_clrpag,
  1309.   "cr", &term_cr,
  1310.   "dc", &term_dc,
  1311.   "ei", &term_ei,
  1312.   "ic", &term_ic,
  1313.   "im", &term_im,
  1314.   "kd", &term_kd,
  1315.   "kl", &term_kl,
  1316.   "kr", &term_kr,
  1317.   "ku", &term_ku,
  1318.   "ks", &term_ks,
  1319.   "ke", &term_ke,
  1320.   "le", &term_backspace,
  1321.   "mm", &term_mm,
  1322.   "mo", &term_mo,
  1323. #if defined (HACK_TERMCAP_MOTION)
  1324.   "nd", &term_forward_char,
  1325. #endif
  1326.   "pc", &term_pc,
  1327.   "up", &term_up,
  1328.   "vb", &visible_bell,
  1329. };
  1330.  
  1331. #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
  1332.  
  1333. /* Read the desired terminal capability strings into BP.  The capabilities
  1334.    are described in the TC_STRINGS table. */
  1335. static void
  1336. get_term_capabilities (bp)
  1337.      char **bp;
  1338. {
  1339.   register int i;
  1340.  
  1341.   for (i = 0; i < NUM_TC_STRINGS; i++)
  1342.     *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
  1343.   tcap_initialized = 1;
  1344. }
  1345.  
  1346. static int
  1347. init_terminal_io (terminal_name)
  1348.      char *terminal_name;
  1349. {
  1350. #if defined (__GO32__)
  1351.   screenwidth = ScreenCols ();
  1352.   screenheight = ScreenRows ();
  1353.   screenchars = screenwidth * screenheight;
  1354.   term_cr = "\r";
  1355.   term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1356.   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1357.  
  1358.   /* Does the __GO32__ have a meta key?  I don't know. */
  1359.   term_has_meta = 0;
  1360.   term_mm = term_mo = (char *)NULL;
  1361.  
  1362.   /* It probably has arrow keys, but I don't know what they are. */
  1363.   term_ku = term_kd = term_kr = term_kl = (char *)NULL;
  1364.  
  1365. #if defined (HACK_TERMCAP_MOTION)
  1366.   term_forward_char = (char *)NULL;
  1367. #endif /* HACK_TERMCAP_MOTION */
  1368.   terminal_can_insert = term_xn = 0;
  1369.   return;
  1370. #else /* !__GO32__ */
  1371.  
  1372.   char *term, *buffer;
  1373.   int tty;
  1374.   Keymap xkeymap;
  1375.  
  1376.   term = terminal_name ? terminal_name : getenv ("TERM");
  1377.  
  1378.   if (!term_string_buffer)
  1379.     term_string_buffer = xmalloc (2048);
  1380.  
  1381.   if (!term_buffer)
  1382.     term_buffer = xmalloc (2048);
  1383.  
  1384.   buffer = term_string_buffer;
  1385.  
  1386.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  1387.  
  1388.   if (!term)
  1389.     term = "dumb";
  1390.  
  1391.   if (tgetent (term_buffer, term) <= 0)
  1392.     {
  1393.       dumb_term = 1;
  1394.       screenwidth = 79;
  1395.       screenheight = 24;
  1396.       screenchars = 79 * 24;
  1397.       term_cr = "\r";
  1398.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1399.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1400.       term_ku = term_kd = term_kl = term_kr = (char *)NULL;
  1401. #if defined (HACK_TERMCAP_MOTION)
  1402.       term_forward_char = (char *)NULL;
  1403. #endif
  1404.       terminal_can_insert = 0;
  1405.       return 0;
  1406.     }
  1407.  
  1408.   get_term_capabilities (&buffer);
  1409.  
  1410.   /* Set up the variables that the termcap library expects the application
  1411.      to provide. */
  1412.   PC = term_pc ? *term_pc : 0;
  1413.   BC = term_backspace;
  1414.   UP = term_up;
  1415.  
  1416.   if (!term_cr)
  1417.     term_cr =  "\r";
  1418.  
  1419.   if (rl_instream)
  1420.     tty = fileno (rl_instream);
  1421.   else
  1422.     tty = 0;
  1423.  
  1424.   screenwidth = screenheight = 0;
  1425.  
  1426.   term_xn = tgetflag ("am") && tgetflag ("xn");
  1427.  
  1428.   _rl_set_screen_size (tty, 0);
  1429.  
  1430.   /* "An application program can assume that the terminal can do
  1431.       character insertion if *any one of* the capabilities `IC',
  1432.       `im', `ic' or `ip' is provided."  But we can't do anything if
  1433.       only `ip' is provided, so... */
  1434.   terminal_can_insert = (term_IC || term_im || term_ic);
  1435.  
  1436.   /* Check to see if this terminal has a meta key and clear the capability
  1437.      variables if there is none. */
  1438.   term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
  1439.   if (!term_has_meta)
  1440.     {
  1441.       term_mm = (char *)NULL;
  1442.       term_mo = (char *)NULL;
  1443.     }
  1444.  
  1445.   /* Attempt to find and bind the arrow keys.  Do not override already
  1446.      bound keys in an overzealous attempt, however. */
  1447.   xkeymap = _rl_keymap;
  1448.  
  1449.   _rl_keymap = emacs_standard_keymap;
  1450.   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
  1451.   _rl_bind_if_unbound (term_kd, rl_get_next_history);
  1452.   _rl_bind_if_unbound (term_kr, rl_forward);
  1453.   _rl_bind_if_unbound (term_kl, rl_backward);
  1454.  
  1455. #if defined (VI_MODE)
  1456.   _rl_keymap = vi_movement_keymap;
  1457.   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
  1458.   _rl_bind_if_unbound (term_kd, rl_get_next_history);
  1459.   _rl_bind_if_unbound (term_kr, rl_forward);
  1460.   _rl_bind_if_unbound (term_kl, rl_backward);
  1461. #endif /* VI_MODE */
  1462.  
  1463.   _rl_keymap = xkeymap;
  1464.  
  1465. #endif /* !__GO32__ */
  1466.   return 0;
  1467. }
  1468.  
  1469. char *
  1470. rl_get_termcap (cap)
  1471.      char *cap;
  1472. {
  1473.   register int i;
  1474.  
  1475.   if (tcap_initialized == 0)
  1476.     return ((char *)NULL);
  1477.   for (i = 0; i < NUM_TC_STRINGS; i++)
  1478.     {
  1479.       if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
  1480.         return *(tc_strings[i].tc_value);
  1481.     }
  1482.   return ((char *)NULL);
  1483. }
  1484.  
  1485. /* A function for the use of tputs () */
  1486. int
  1487. _rl_output_character_function (c)
  1488.      int c;
  1489. {
  1490.   return putc (c, out_stream);
  1491. }
  1492.  
  1493. /* Write COUNT characters from STRING to the output stream. */
  1494. void
  1495. _rl_output_some_chars (string, count)
  1496.      char *string;
  1497.      int count;
  1498. {
  1499.   fwrite (string, 1, count, out_stream);
  1500. }
  1501.  
  1502. /* Move the cursor back. */
  1503. backspace (count)
  1504.      int count;
  1505. {
  1506.   register int i;
  1507.  
  1508. #if !defined (__GO32__)
  1509.   if (term_backspace)
  1510.     for (i = 0; i < count; i++)
  1511.       tputs (term_backspace, 1, _rl_output_character_function);
  1512.   else
  1513. #endif /* !__GO32__ */
  1514.     for (i = 0; i < count; i++)
  1515.       putc ('\b', out_stream);
  1516.   return 0;
  1517. }
  1518.  
  1519. /* Move to the start of the next line. */
  1520. crlf ()
  1521. {
  1522. #if defined (NEW_TTY_DRIVER)
  1523.   tputs (term_cr, 1, _rl_output_character_function);
  1524. #endif /* NEW_TTY_DRIVER */
  1525.   putc ('\n', out_stream);
  1526.   return 0;
  1527. }
  1528.  
  1529. rl_tty_status (count, key)
  1530.      int count, key;
  1531. {
  1532. #if defined (TIOCSTAT)
  1533.   ioctl (1, TIOCSTAT, (char *)0);
  1534.   rl_refresh_line ();
  1535. #else
  1536.   ding ();
  1537. #endif
  1538.   return 0;
  1539. }
  1540.  
  1541.  
  1542. /* **************************************************************** */
  1543. /*                                    */
  1544. /*            Utility Functions                */
  1545. /*                                    */
  1546. /* **************************************************************** */
  1547.  
  1548. /* Return 0 if C is not a member of the class of characters that belong
  1549.    in words, or 1 if it is. */
  1550.  
  1551. int allow_pathname_alphabetic_chars = 0;
  1552. char *pathname_alphabetic_chars = "/-_=~.#$";
  1553.  
  1554. int
  1555. alphabetic (c)
  1556.      int c;
  1557. {
  1558.   if (pure_alphabetic (c) || (digit_p (c)))
  1559.     return (1);
  1560.  
  1561.   if (allow_pathname_alphabetic_chars)
  1562.     return (strchr (pathname_alphabetic_chars, c) != NULL);
  1563.   else
  1564.     return (0);
  1565. }
  1566.  
  1567. /* Ring the terminal bell. */
  1568. int
  1569. ding ()
  1570. {
  1571.   if (readline_echoing_p)
  1572.     {
  1573. #if !defined (__GO32__)
  1574.       switch (_rl_bell_preference)
  1575.         {
  1576.     case NO_BELL:
  1577.     default:
  1578.       break;
  1579.     case VISIBLE_BELL:
  1580.       if (visible_bell)
  1581.         {
  1582.           tputs (visible_bell, 1, _rl_output_character_function);
  1583.           break;
  1584.         }
  1585.       /* FALLTHROUGH */
  1586.     case AUDIBLE_BELL:
  1587.       fprintf (stderr, "\007");
  1588.       fflush (stderr);
  1589.       break;
  1590.         }
  1591. #else /* __GO32__ */
  1592.       fprintf (stderr, "\007");
  1593.       fflush (stderr);
  1594. #endif /* __GO32__ */
  1595.       return (0);
  1596.     }
  1597.   return (-1);
  1598. }
  1599.  
  1600. /* How to abort things. */
  1601. rl_abort (count, key)
  1602.      int count, key;
  1603. {
  1604.   ding ();
  1605.   rl_clear_message ();
  1606.   rl_init_argument ();
  1607.   rl_pending_input = 0;
  1608.  
  1609.   defining_kbd_macro = 0;
  1610.   while (executing_macro)
  1611.     pop_executing_macro ();
  1612.  
  1613.   rl_last_func = (Function *)NULL;
  1614.   longjmp (readline_top_level, 1);
  1615. }
  1616.  
  1617. /* Return a copy of the string between FROM and TO.
  1618.    FROM is inclusive, TO is not. */
  1619. char *
  1620. rl_copy_text (from, to)
  1621.      int from, to;
  1622. {
  1623.   register int length;
  1624.   char *copy;
  1625.  
  1626.   /* Fix it if the caller is confused. */
  1627.   if (from > to)
  1628.     {
  1629.       int t = from;
  1630.       from = to;
  1631.       to = t;
  1632.     }
  1633.  
  1634.   length = to - from;
  1635.   copy = xmalloc (1 + length);
  1636.   strncpy (copy, the_line + from, length);
  1637.   copy[length] = '\0';
  1638.   return (copy);
  1639. }
  1640.  
  1641. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  1642.    LEN characters. */
  1643. void
  1644. rl_extend_line_buffer (len)
  1645.      int len;
  1646. {
  1647.   while (len >= rl_line_buffer_len)
  1648.     {
  1649.       rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
  1650.       rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
  1651.     }
  1652.  
  1653.   the_line = rl_line_buffer;
  1654. }
  1655.  
  1656.  
  1657. /* **************************************************************** */
  1658. /*                                    */
  1659. /*            Insert and Delete                */
  1660. /*                                    */
  1661. /* **************************************************************** */
  1662.  
  1663. /* Insert a string of text into the line at point.  This is the only
  1664.    way that you should do insertion.  rl_insert () calls this
  1665.    function. */
  1666. rl_insert_text (string)
  1667.      char *string;
  1668. {
  1669.   register int i, l = strlen (string);
  1670.  
  1671.   if (rl_end + l >= rl_line_buffer_len)
  1672.     rl_extend_line_buffer (rl_end + l);
  1673.  
  1674.   for (i = rl_end; i >= rl_point; i--)
  1675.     the_line[i + l] = the_line[i];
  1676.   strncpy (the_line + rl_point, string, l);
  1677.  
  1678.   /* Remember how to undo this if we aren't undoing something. */
  1679.   if (!doing_an_undo)
  1680.     {
  1681.       /* If possible and desirable, concatenate the undos. */
  1682.       if ((l == 1) &&
  1683.       rl_undo_list &&
  1684.       (rl_undo_list->what == UNDO_INSERT) &&
  1685.       (rl_undo_list->end == rl_point) &&
  1686.       (rl_undo_list->end - rl_undo_list->start < 20))
  1687.     rl_undo_list->end++;
  1688.       else
  1689.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  1690.     }
  1691.   rl_point += l;
  1692.   rl_end += l;
  1693.   the_line[rl_end] = '\0';
  1694.   return l;
  1695. }
  1696.  
  1697. /* Delete the string between FROM and TO.  FROM is
  1698.    inclusive, TO is not. */
  1699. rl_delete_text (from, to)
  1700.      int from, to;
  1701. {
  1702.   register char *text;
  1703.   register int diff, i;
  1704.  
  1705.   /* Fix it if the caller is confused. */
  1706.   if (from > to)
  1707.     {
  1708.       int t = from;
  1709.       from = to;
  1710.       to = t;
  1711.     }
  1712.   text = rl_copy_text (from, to);
  1713.  
  1714.   /* Some versions of strncpy() can't handle overlapping arguments. */
  1715.   diff = to - from;
  1716.   for (i = from; i < rl_end - diff; i++)
  1717.     the_line[i] = the_line[i + diff];
  1718.  
  1719.   /* Remember how to undo this delete. */
  1720.   if (!doing_an_undo)
  1721.     rl_add_undo (UNDO_DELETE, from, to, text);
  1722.   else
  1723.     free (text);
  1724.  
  1725.   rl_end -= diff;
  1726.   the_line[rl_end] = '\0';
  1727.   return (diff);
  1728. }
  1729.  
  1730.  
  1731. /* **************************************************************** */
  1732. /*                                    */
  1733. /*            Readline character functions            */
  1734. /*                                    */
  1735. /* **************************************************************** */
  1736.  
  1737. /* This is not a gap editor, just a stupid line input routine.  No hair
  1738.    is involved in writing any of the functions, and none should be. */
  1739.  
  1740. /* Note that:
  1741.  
  1742.    rl_end is the place in the string that we would place '\0';
  1743.    i.e., it is always safe to place '\0' there.
  1744.  
  1745.    rl_point is the place in the string where the cursor is.  Sometimes
  1746.    this is the same as rl_end.
  1747.  
  1748.    Any command that is called interactively receives two arguments.
  1749.    The first is a count: the numeric arg pased to this command.
  1750.    The second is the key which invoked this command.
  1751. */
  1752.  
  1753.  
  1754. /* **************************************************************** */
  1755. /*                                    */
  1756. /*            Movement Commands                */
  1757. /*                                    */
  1758. /* **************************************************************** */
  1759.  
  1760. /* Note that if you `optimize' the display for these functions, you cannot
  1761.    use said functions in other functions which do not do optimizing display.
  1762.    I.e., you will have to update the data base for rl_redisplay, and you
  1763.    might as well let rl_redisplay do that job. */
  1764.  
  1765. /* Move forward COUNT characters. */
  1766. rl_forward (count, key)
  1767.      int count, key;
  1768. {
  1769.   if (count < 0)
  1770.     rl_backward (-count);
  1771.   else if (count > 0)
  1772.     {
  1773.       int end = rl_point + count;
  1774. #if defined (VI_MODE)
  1775.       int lend = rl_end - (rl_editing_mode == vi_mode);
  1776. #else
  1777.       int lend = rl_end;
  1778. #endif
  1779.  
  1780.       if (end > lend)
  1781.     {
  1782.       rl_point = lend;
  1783.       ding ();
  1784.     }
  1785.       else
  1786.     rl_point = end;
  1787.     }
  1788.   return 0;
  1789. }
  1790.  
  1791. /* Move backward COUNT characters. */
  1792. rl_backward (count, key)
  1793.      int count, key;
  1794. {
  1795.   if (count < 0)
  1796.     rl_forward (-count);
  1797.   else if (count > 0)
  1798.     {
  1799.       if (rl_point < count)
  1800.     {
  1801.       rl_point = 0;
  1802.       ding ();
  1803.     }
  1804.       else
  1805.         rl_point -= count;
  1806.     }
  1807.   return 0;
  1808. }
  1809.  
  1810. /* Move to the beginning of the line. */
  1811. rl_beg_of_line (count, key)
  1812.      int count, key;
  1813. {
  1814.   rl_point = 0;
  1815.   return 0;
  1816. }
  1817.  
  1818. /* Move to the end of the line. */
  1819. rl_end_of_line (count, key)
  1820.      int count, key;
  1821. {
  1822.   rl_point = rl_end;
  1823.   return 0;
  1824. }
  1825.  
  1826. /* Move forward a word.  We do what Emacs does. */
  1827. rl_forward_word (count, key)
  1828.      int count, key;
  1829. {
  1830.   int c;
  1831.  
  1832.   if (count < 0)
  1833.     {
  1834.       rl_backward_word (-count);
  1835.       return 0;
  1836.     }
  1837.  
  1838.   while (count)
  1839.     {
  1840.       if (rl_point == rl_end)
  1841.     return 0;
  1842.  
  1843.       /* If we are not in a word, move forward until we are in one.
  1844.      Then, move forward until we hit a non-alphabetic character. */
  1845.       c = the_line[rl_point];
  1846.       if (!alphabetic (c))
  1847.     {
  1848.       while (++rl_point < rl_end)
  1849.         {
  1850.           c = the_line[rl_point];
  1851.           if (alphabetic (c))
  1852.         break;
  1853.         }
  1854.     }
  1855.       if (rl_point == rl_end)
  1856.     return 0;
  1857.       while (++rl_point < rl_end)
  1858.     {
  1859.       c = the_line[rl_point];
  1860.       if (!alphabetic (c))
  1861.         break;
  1862.     }
  1863.       --count;
  1864.     }
  1865.   return 0;
  1866. }
  1867.  
  1868. /* Move backward a word.  We do what Emacs does. */
  1869. rl_backward_word (count, key)
  1870.      int count, key;
  1871. {
  1872.   int c;
  1873.  
  1874.   if (count < 0)
  1875.     {
  1876.       rl_forward_word (-count);
  1877.       return 0;
  1878.     }
  1879.  
  1880.   while (count)
  1881.     {
  1882.       if (!rl_point)
  1883.     return 0;
  1884.  
  1885.       /* Like rl_forward_word (), except that we look at the characters
  1886.      just before point. */
  1887.  
  1888.       c = the_line[rl_point - 1];
  1889.       if (!alphabetic (c))
  1890.     {
  1891.       while (--rl_point)
  1892.         {
  1893.           c = the_line[rl_point - 1];
  1894.           if (alphabetic (c))
  1895.         break;
  1896.         }
  1897.     }
  1898.  
  1899.       while (rl_point)
  1900.     {
  1901.       c = the_line[rl_point - 1];
  1902.       if (!alphabetic (c))
  1903.         break;
  1904.       else
  1905.         --rl_point;
  1906.     }
  1907.       --count;
  1908.     }
  1909.   return 0;
  1910. }
  1911.  
  1912. /* Clear the current line.  Numeric argument to C-l does this. */
  1913. rl_refresh_line ()
  1914. {
  1915.   int curr_line, nleft;
  1916.  
  1917.   /* Find out whether or not there might be invisible characters in the
  1918.      editing buffer. */
  1919.   if (rl_display_prompt == rl_prompt)
  1920.     nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
  1921.   else
  1922.     nleft = _rl_last_c_pos - screenwidth;
  1923.  
  1924.   if (nleft > 0)
  1925.     curr_line = 1 + nleft / screenwidth;
  1926.   else
  1927.     curr_line = 0;
  1928.  
  1929.   _rl_move_vert (curr_line);
  1930.   _rl_move_cursor_relative (0, the_line);   /* XXX is this right */
  1931.  
  1932. #if defined (__GO32__)
  1933.   {
  1934.     int row, col, width, row_start;
  1935.  
  1936.     ScreenGetCursor (&row, &col);
  1937.     width = ScreenCols ();
  1938.     row_start = ScreenPrimary + (row * width);
  1939.     memset (row_start + col, 0, (width - col) * 2);
  1940.   }
  1941. #else /* !__GO32__ */
  1942.   if (term_clreol)
  1943.     tputs (term_clreol, 1, _rl_output_character_function);
  1944. #endif /* !__GO32__ */
  1945.  
  1946.   rl_forced_update_display ();
  1947.   rl_display_fixed = 1;
  1948.  
  1949.   return 0;
  1950. }
  1951.  
  1952. /* C-l typed to a line without quoting clears the screen, and then reprints
  1953.    the prompt and the current input line.  Given a numeric arg, redraw only
  1954.    the current line. */
  1955. rl_clear_screen (count, key)
  1956.      int count, key;
  1957. {
  1958.   if (rl_explicit_arg)
  1959.     {
  1960.       rl_refresh_line ();
  1961.       return 0;
  1962.     }
  1963.  
  1964. #if !defined (__GO32__)
  1965.   if (term_clrpag)
  1966.     tputs (term_clrpag, 1, _rl_output_character_function);
  1967.   else
  1968. #endif /* !__GO32__ */
  1969.     crlf ();
  1970.  
  1971.   rl_forced_update_display ();
  1972.   rl_display_fixed = 1;
  1973.  
  1974.   return 0;
  1975. }
  1976.  
  1977. rl_arrow_keys (count, c)
  1978.      int count, c;
  1979. {
  1980.   int ch;
  1981.  
  1982.   ch = rl_read_key ();
  1983.  
  1984.   switch (to_upper (ch))
  1985.     {
  1986.     case 'A':
  1987.       rl_get_previous_history (count);
  1988.       break;
  1989.  
  1990.     case 'B':
  1991.       rl_get_next_history (count);
  1992.       break;
  1993.  
  1994.     case 'C':
  1995.       rl_forward (count);
  1996.       break;
  1997.  
  1998.     case 'D':
  1999.       rl_backward (count);
  2000.       break;
  2001.  
  2002.     default:
  2003.       ding ();
  2004.     }
  2005.   return 0;
  2006. }
  2007.  
  2008.  
  2009. /* **************************************************************** */
  2010. /*                                    */
  2011. /*            Text commands                    */
  2012. /*                                    */
  2013. /* **************************************************************** */
  2014.  
  2015. /* Insert the character C at the current location, moving point forward. */
  2016. rl_insert (count, c)
  2017.      int count, c;
  2018. {
  2019.   register int i;
  2020.   char *string;
  2021.  
  2022.   if (count <= 0)
  2023.     return 0;
  2024.  
  2025.   /* If we can optimize, then do it.  But don't let people crash
  2026.      readline because of extra large arguments. */
  2027.   if (count > 1 && count < 1024)
  2028.     {
  2029.       string = xmalloc (1 + count);
  2030.  
  2031.       for (i = 0; i < count; i++)
  2032.     string[i] = c;
  2033.  
  2034.       string[i] = '\0';
  2035.       rl_insert_text (string);
  2036.       free (string);
  2037.  
  2038.       return 0;
  2039.     }
  2040.  
  2041.   if (count > 1024)
  2042.     {
  2043.       int decreaser;
  2044.       char str[1024+1];
  2045.  
  2046.       for (i = 0; i < 1024; i++)
  2047.     str[i] = c;
  2048.  
  2049.       while (count)
  2050.     {
  2051.       decreaser = (count > 1024 ? 1024 : count);
  2052.       str[decreaser] = '\0';
  2053.       rl_insert_text (str);
  2054.       count -= decreaser;
  2055.     }
  2056.  
  2057.       return 0;
  2058.     }
  2059.  
  2060.   /* We are inserting a single character.
  2061.      If there is pending input, then make a string of all of the
  2062.      pending characters that are bound to rl_insert, and insert
  2063.      them all. */
  2064.   if (any_typein)
  2065.     {
  2066.       int key = 0, t;
  2067.  
  2068.       i = 0;
  2069.       string = xmalloc (ibuffer_len + 1);
  2070.       string[i++] = c;
  2071.  
  2072.       while ((t = rl_get_char (&key)) &&
  2073.          (_rl_keymap[key].type == ISFUNC &&
  2074.           _rl_keymap[key].function == rl_insert))
  2075.     string[i++] = key;
  2076.  
  2077.       if (t)
  2078.     rl_unget_char (key);
  2079.  
  2080.       string[i] = '\0';
  2081.       rl_insert_text (string);
  2082.       free (string);
  2083.     }
  2084.   else
  2085.     {
  2086.       /* Inserting a single character. */
  2087.       char str[2];
  2088.  
  2089.       str[1] = '\0';
  2090.       str[0] = c;
  2091.       rl_insert_text (str);
  2092.     }
  2093.   return 0;
  2094. }
  2095.  
  2096. /* Insert the next typed character verbatim. */
  2097. rl_quoted_insert (count, key)
  2098.      int count, key;
  2099. {
  2100.   int c;
  2101.  
  2102.   c = rl_read_key ();
  2103.   return (rl_insert (count, c));  
  2104. }
  2105.  
  2106. /* Insert a tab character. */
  2107. rl_tab_insert (count, key)
  2108.      int count, key;
  2109. {
  2110.   return (rl_insert (count, '\t'));
  2111. }
  2112.  
  2113. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  2114.    KEY is the key that invoked this command.  I guess it could have
  2115.    meaning in the future. */
  2116. rl_newline (count, key)
  2117.      int count, key;
  2118. {
  2119.   rl_done = 1;
  2120.  
  2121. #if defined (VI_MODE)
  2122.   _rl_vi_done_inserting ();
  2123.   _rl_vi_reset_last ();
  2124.  
  2125. #endif /* VI_MODE */
  2126.  
  2127.   if (readline_echoing_p)
  2128.     _rl_update_final ();
  2129.   return 0;
  2130. }
  2131.  
  2132. rl_clean_up_for_exit ()
  2133. {
  2134.   if (readline_echoing_p)
  2135.     {
  2136.       _rl_move_vert (_rl_vis_botlin);
  2137.       _rl_vis_botlin = 0;
  2138.       fflush (out_stream);
  2139.       rl_restart_output ();
  2140.     }
  2141.   return 0;
  2142. }
  2143.  
  2144. /* What to do for some uppercase characters, like meta characters,
  2145.    and some characters appearing in emacs_ctlx_keymap.  This function
  2146.    is just a stub, you bind keys to it and the code in _rl_dispatch ()
  2147.    is special cased. */
  2148. rl_do_lowercase_version (ignore1, ignore2)
  2149.      int ignore1, ignore2;
  2150. {
  2151.   return 0;
  2152. }
  2153.  
  2154. /* Rubout the character behind point. */
  2155. rl_rubout (count, key)
  2156.      int count, key;
  2157. {
  2158.   if (count < 0)
  2159.     {
  2160.       rl_delete (-count);
  2161.       return 0;
  2162.     }
  2163.  
  2164.   if (!rl_point)
  2165.     {
  2166.       ding ();
  2167.       return -1;
  2168.     }
  2169.  
  2170.   if (count > 1 || rl_explicit_arg)
  2171.     {
  2172.       int orig_point = rl_point;
  2173.       rl_backward (count);
  2174.       rl_kill_text (orig_point, rl_point);
  2175.     }
  2176.   else
  2177.     {
  2178.       int c = the_line[--rl_point];
  2179.       rl_delete_text (rl_point, rl_point + 1);
  2180.  
  2181.       if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
  2182.     {
  2183.       int l;
  2184.       l = rl_character_len (c, rl_point);
  2185.       _rl_erase_at_end_of_line (l);
  2186.     }
  2187.     }
  2188.   return 0;
  2189. }
  2190.  
  2191. /* Delete the character under the cursor.  Given a numeric argument,
  2192.    kill that many characters instead. */
  2193. rl_delete (count, invoking_key)
  2194.      int count, invoking_key;
  2195. {
  2196.   if (count < 0)
  2197.     {
  2198.       return (rl_rubout (-count));
  2199.     }
  2200.  
  2201.   if (rl_point == rl_end)
  2202.     {
  2203.       ding ();
  2204.       return -1;
  2205.     }
  2206.  
  2207.   if (count > 1 || rl_explicit_arg)
  2208.     {
  2209.       int orig_point = rl_point;
  2210.       rl_forward (count);
  2211.       rl_kill_text (orig_point, rl_point);
  2212.       rl_point = orig_point;
  2213.       return 0;
  2214.     }
  2215.   else
  2216.     return (rl_delete_text (rl_point, rl_point + 1));
  2217.   
  2218. }
  2219.  
  2220. /* Delete all spaces and tabs around point. */
  2221. rl_delete_horizontal_space (count, ignore)
  2222.      int count, ignore;
  2223. {
  2224.   int start = rl_point;
  2225.  
  2226.   while (rl_point && whitespace (the_line[rl_point - 1]))
  2227.     rl_point--;
  2228.  
  2229.   start = rl_point;
  2230.  
  2231.   while (rl_point < rl_end && whitespace (the_line[rl_point]))
  2232.     rl_point++;
  2233.  
  2234.   if (start != rl_point)
  2235.     {
  2236.       rl_delete_text (start, rl_point);
  2237.       rl_point = start;
  2238.     }
  2239.   return 0;
  2240. }
  2241.  
  2242.  
  2243. /* **************************************************************** */
  2244. /*                                    */
  2245. /*            Kill commands                    */
  2246. /*                                    */
  2247. /* **************************************************************** */
  2248.  
  2249. /* The next two functions mimic unix line editing behaviour, except they
  2250.    save the deleted text on the kill ring.  This is safer than not saving
  2251.    it, and since we have a ring, nobody should get screwed. */
  2252.  
  2253. /* This does what C-w does in Unix.  We can't prevent people from
  2254.    using behaviour that they expect. */
  2255. rl_unix_word_rubout (count, key)
  2256.      int count, key;
  2257. {
  2258.   if (!rl_point)
  2259.     ding ();
  2260.   else
  2261.     {
  2262.       int orig_point = rl_point;
  2263.  
  2264.       while (rl_point && whitespace (the_line[rl_point - 1]))
  2265.     rl_point--;
  2266.  
  2267.       while (rl_point && !whitespace (the_line[rl_point - 1]))
  2268.     rl_point--;
  2269.  
  2270.       rl_kill_text (orig_point, rl_point);
  2271.     }
  2272.   return 0;
  2273. }
  2274.  
  2275. /* Here is C-u doing what Unix does.  You don't *have* to use these
  2276.    key-bindings.  We have a choice of killing the entire line, or
  2277.    killing from where we are to the start of the line.  We choose the
  2278.    latter, because if you are a Unix weenie, then you haven't backspaced
  2279.    into the line at all, and if you aren't, then you know what you are
  2280.    doing. */
  2281. rl_unix_line_discard (count, key)
  2282.      int count, key;
  2283. {
  2284.   if (!rl_point)
  2285.     ding ();
  2286.   else
  2287.     {
  2288.       rl_kill_text (rl_point, 0);
  2289.       rl_point = 0;
  2290.     }
  2291.   return 0;
  2292. }
  2293.  
  2294.  
  2295. /* **************************************************************** */
  2296. /*                                    */
  2297. /*            Commands For Typos                */
  2298. /*                                    */
  2299. /* **************************************************************** */
  2300.  
  2301. /* Random and interesting things in here.  */
  2302.  
  2303. /* **************************************************************** */
  2304. /*                                    */
  2305. /*            Changing Case                    */
  2306. /*                                    */
  2307. /* **************************************************************** */
  2308.  
  2309. /* The three kinds of things that we know how to do. */
  2310. #define UpCase 1
  2311. #define DownCase 2
  2312. #define CapCase 3
  2313.  
  2314. static int rl_change_case ();
  2315.  
  2316. /* Uppercase the word at point. */
  2317. rl_upcase_word (count, key)
  2318.      int count, key;
  2319. {
  2320.   return (rl_change_case (count, UpCase));
  2321. }
  2322.  
  2323. /* Lowercase the word at point. */
  2324. rl_downcase_word (count, key)
  2325.      int count, key;
  2326. {
  2327.   return (rl_change_case (count, DownCase));
  2328. }
  2329.  
  2330. /* Upcase the first letter, downcase the rest. */
  2331. rl_capitalize_word (count, key)
  2332.      int count, key;
  2333. {
  2334.  return (rl_change_case (count, CapCase));
  2335. }
  2336.  
  2337. /* The meaty function.
  2338.    Change the case of COUNT words, performing OP on them.
  2339.    OP is one of UpCase, DownCase, or CapCase.
  2340.    If a negative argument is given, leave point where it started,
  2341.    otherwise, leave it where it moves to. */
  2342. static int
  2343. rl_change_case (count, op)
  2344.      int count, op;
  2345. {
  2346.   register int start = rl_point, end;
  2347.   int state = 0;
  2348.  
  2349.   rl_forward_word (count);
  2350.   end = rl_point;
  2351.  
  2352.   if (count < 0)
  2353.     {
  2354.       int temp = start;
  2355.       start = end;
  2356.       end = temp;
  2357.     }
  2358.  
  2359.   /* We are going to modify some text, so let's prepare to undo it. */
  2360.   rl_modifying (start, end);
  2361.  
  2362.   for (; start < end; start++)
  2363.     {
  2364.       switch (op)
  2365.     {
  2366.     case UpCase:
  2367.       the_line[start] = to_upper (the_line[start]);
  2368.       break;
  2369.  
  2370.     case DownCase:
  2371.       the_line[start] = to_lower (the_line[start]);
  2372.       break;
  2373.  
  2374.     case CapCase:
  2375.       if (state == 0)
  2376.         {
  2377.           the_line[start] = to_upper (the_line[start]);
  2378.           state = 1;
  2379.         }
  2380.       else
  2381.         {
  2382.           the_line[start] = to_lower (the_line[start]);
  2383.         }
  2384.       if (!pure_alphabetic (the_line[start]))
  2385.         state = 0;
  2386.       break;
  2387.  
  2388.     default:
  2389.       abort ();
  2390.       return -1;
  2391.     }
  2392.     }
  2393.   rl_point = end;
  2394.   return 0;
  2395. }
  2396.  
  2397. /* **************************************************************** */
  2398. /*                                    */
  2399. /*            Transposition                    */
  2400. /*                                    */
  2401. /* **************************************************************** */
  2402.  
  2403. /* Transpose the words at point. */
  2404. rl_transpose_words (count, key)
  2405.      int count, key;
  2406. {
  2407.   char *word1, *word2;
  2408.   int w1_beg, w1_end, w2_beg, w2_end;
  2409.   int orig_point = rl_point;
  2410.  
  2411.   if (!count)
  2412.     return 0;
  2413.  
  2414.   /* Find the two words. */
  2415.   rl_forward_word (count);
  2416.   w2_end = rl_point;
  2417.   rl_backward_word (1);
  2418.   w2_beg = rl_point;
  2419.   rl_backward_word (count);
  2420.   w1_beg = rl_point;
  2421.   rl_forward_word (1);
  2422.   w1_end = rl_point;
  2423.  
  2424.   /* Do some check to make sure that there really are two words. */
  2425.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  2426.     {
  2427.       ding ();
  2428.       rl_point = orig_point;
  2429.       return -1;
  2430.     }
  2431.  
  2432.   /* Get the text of the words. */
  2433.   word1 = rl_copy_text (w1_beg, w1_end);
  2434.   word2 = rl_copy_text (w2_beg, w2_end);
  2435.  
  2436.   /* We are about to do many insertions and deletions.  Remember them
  2437.      as one operation. */
  2438.   rl_begin_undo_group ();
  2439.  
  2440.   /* Do the stuff at word2 first, so that we don't have to worry
  2441.      about word1 moving. */
  2442.   rl_point = w2_beg;
  2443.   rl_delete_text (w2_beg, w2_end);
  2444.   rl_insert_text (word1);
  2445.  
  2446.   rl_point = w1_beg;
  2447.   rl_delete_text (w1_beg, w1_end);
  2448.   rl_insert_text (word2);
  2449.  
  2450.   /* This is exactly correct since the text before this point has not
  2451.      changed in length. */
  2452.   rl_point = w2_end;
  2453.  
  2454.   /* I think that does it. */
  2455.   rl_end_undo_group ();
  2456.   free (word1);
  2457.   free (word2);
  2458.  
  2459.   return 0;
  2460. }
  2461.  
  2462. /* Transpose the characters at point.  If point is at the end of the line,
  2463.    then transpose the characters before point. */
  2464. rl_transpose_chars (count, key)
  2465.      int count, key;
  2466. {
  2467.   char dummy[2];
  2468.  
  2469.   if (!count)
  2470.     return 0;
  2471.  
  2472.   if (!rl_point || rl_end < 2)
  2473.     {
  2474.       ding ();
  2475.       return -1;
  2476.     }
  2477.  
  2478.   rl_begin_undo_group ();
  2479.  
  2480.   if (rl_point == rl_end)
  2481.     {
  2482.       --rl_point;
  2483.       count = 1;
  2484.     }
  2485.   rl_point--;
  2486.  
  2487.   dummy[0] = the_line[rl_point];
  2488.   dummy[1] = '\0';
  2489.  
  2490.   rl_delete_text (rl_point, rl_point + 1);
  2491.  
  2492.   rl_point += count;
  2493.   if (rl_point > rl_end)
  2494.     rl_point = rl_end;
  2495.   else if (rl_point < 0)
  2496.     rl_point = 0;
  2497.   rl_insert_text (dummy);
  2498.  
  2499.   rl_end_undo_group ();
  2500.   return 0;
  2501. }
  2502.  
  2503. /* **************************************************************** */
  2504. /*                                    */
  2505. /*            Undo, and Undoing                */
  2506. /*                                    */
  2507. /* **************************************************************** */
  2508.  
  2509. /* The current undo list for THE_LINE. */
  2510. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  2511.  
  2512. /* Remember how to undo something.  Concatenate some undos if that
  2513.    seems right. */
  2514. void
  2515. rl_add_undo (what, start, end, text)
  2516.      enum undo_code what;
  2517.      int start, end;
  2518.      char *text;
  2519. {
  2520.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  2521.   temp->what = what;
  2522.   temp->start = start;
  2523.   temp->end = end;
  2524.   temp->text = text;
  2525.   temp->next = rl_undo_list;
  2526.   rl_undo_list = temp;
  2527. }
  2528.  
  2529. /* Free the existing undo list. */
  2530. void
  2531. free_undo_list ()
  2532. {
  2533.   while (rl_undo_list)
  2534.     {
  2535.       UNDO_LIST *release = rl_undo_list;
  2536.       rl_undo_list = rl_undo_list->next;
  2537.  
  2538.       if (release->what == UNDO_DELETE)
  2539.     free (release->text);
  2540.  
  2541.       free (release);
  2542.     }
  2543.   rl_undo_list = (UNDO_LIST *)NULL;
  2544. }
  2545.  
  2546. /* Undo the next thing in the list.  Return 0 if there
  2547.    is nothing to undo, or non-zero if there was. */
  2548. int
  2549. rl_do_undo ()
  2550. {
  2551.   UNDO_LIST *release;
  2552.   int waiting_for_begin = 0;
  2553.  
  2554. undo_thing:
  2555.   if (!rl_undo_list)
  2556.     return (0);
  2557.  
  2558.   doing_an_undo = 1;
  2559.  
  2560.   switch (rl_undo_list->what) {
  2561.  
  2562.     /* Undoing deletes means inserting some text. */
  2563.   case UNDO_DELETE:
  2564.     rl_point = rl_undo_list->start;
  2565.     rl_insert_text (rl_undo_list->text);
  2566.     free (rl_undo_list->text);
  2567.     break;
  2568.  
  2569.     /* Undoing inserts means deleting some text. */
  2570.   case UNDO_INSERT:
  2571.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  2572.     rl_point = rl_undo_list->start;
  2573.     break;
  2574.  
  2575.     /* Undoing an END means undoing everything 'til we get to
  2576.        a BEGIN. */
  2577.   case UNDO_END:
  2578.     waiting_for_begin++;
  2579.     break;
  2580.  
  2581.     /* Undoing a BEGIN means that we are done with this group. */
  2582.   case UNDO_BEGIN:
  2583.     if (waiting_for_begin)
  2584.       waiting_for_begin--;
  2585.     else
  2586.       ding ();
  2587.     break;
  2588.   }
  2589.  
  2590.   doing_an_undo = 0;
  2591.  
  2592.   release = rl_undo_list;
  2593.   rl_undo_list = rl_undo_list->next;
  2594.   free (release);
  2595.  
  2596.   if (waiting_for_begin)
  2597.     goto undo_thing;
  2598.  
  2599.   return (1);
  2600. }
  2601.  
  2602. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  2603. int
  2604. rl_begin_undo_group ()
  2605. {
  2606.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  2607.   return 0;
  2608. }
  2609.  
  2610. /* End an undo group started with rl_begin_undo_group (). */
  2611. int
  2612. rl_end_undo_group ()
  2613. {
  2614.   rl_add_undo (UNDO_END, 0, 0, 0);
  2615.   return 0;
  2616. }
  2617.  
  2618. /* Save an undo entry for the text from START to END. */
  2619. rl_modifying (start, end)
  2620.      int start, end;
  2621. {
  2622.   if (start > end)
  2623.     {
  2624.       int t = start;
  2625.       start = end;
  2626.       end = t;
  2627.     }
  2628.  
  2629.   if (start != end)
  2630.     {
  2631.       char *temp = rl_copy_text (start, end);
  2632.       rl_begin_undo_group ();
  2633.       rl_add_undo (UNDO_DELETE, start, end, temp);
  2634.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  2635.       rl_end_undo_group ();
  2636.     }
  2637.   return 0;
  2638. }
  2639.  
  2640. /* Revert the current line to its previous state. */
  2641. int
  2642. rl_revert_line (count, key)
  2643.      int count, key;
  2644. {
  2645.   if (!rl_undo_list)
  2646.     ding ();
  2647.   else
  2648.     {
  2649.       while (rl_undo_list)
  2650.     rl_do_undo ();
  2651.     }
  2652.   return 0;
  2653. }
  2654.  
  2655. /* Do some undoing of things that were done. */
  2656. int
  2657. rl_undo_command (count, key)
  2658.      int count, key;
  2659. {
  2660.   if (count < 0)
  2661.     return 0;    /* Nothing to do. */
  2662.  
  2663.   while (count)
  2664.     {
  2665.       if (rl_do_undo ())
  2666.     count--;
  2667.       else
  2668.     {
  2669.       ding ();
  2670.       break;
  2671.     }
  2672.     }
  2673.   return 0;
  2674. }
  2675.  
  2676. /* **************************************************************** */
  2677. /*                                    */
  2678. /*            History Utilities                */
  2679. /*                                    */
  2680. /* **************************************************************** */
  2681.  
  2682. /* We already have a history library, and that is what we use to control
  2683.    the history features of readline.  However, this is our local interface
  2684.    to the history mechanism. */
  2685.  
  2686. /* While we are editing the history, this is the saved
  2687.    version of the original line. */
  2688. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  2689.  
  2690. /* Set the history pointer back to the last entry in the history. */
  2691. static void
  2692. start_using_history ()
  2693. {
  2694.   using_history ();
  2695.   if (saved_line_for_history)
  2696.     _rl_free_history_entry (saved_line_for_history);
  2697.  
  2698.   saved_line_for_history = (HIST_ENTRY *)NULL;
  2699. }
  2700.  
  2701. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  2702. void
  2703. _rl_free_history_entry (entry)
  2704.      HIST_ENTRY *entry;
  2705. {
  2706.   if (!entry)
  2707.     return;
  2708.   if (entry->line)
  2709.     free (entry->line);
  2710.   free (entry);
  2711. }
  2712.  
  2713. /* Perhaps put back the current line if it has changed. */
  2714. maybe_replace_line ()
  2715. {
  2716.   HIST_ENTRY *temp = current_history ();
  2717.  
  2718.   /* If the current line has changed, save the changes. */
  2719.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  2720.     {
  2721.       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  2722.       free (temp->line);
  2723.       free (temp);
  2724.     }
  2725.   return 0;
  2726. }
  2727.  
  2728. /* Put back the saved_line_for_history if there is one. */
  2729. maybe_unsave_line ()
  2730. {
  2731.   if (saved_line_for_history)
  2732.     {
  2733.       int line_len;
  2734.  
  2735.       line_len = strlen (saved_line_for_history->line);
  2736.  
  2737.       if (line_len >= rl_line_buffer_len)
  2738.     rl_extend_line_buffer (line_len);
  2739.  
  2740.       strcpy (the_line, saved_line_for_history->line);
  2741.       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  2742.       _rl_free_history_entry (saved_line_for_history);
  2743.       saved_line_for_history = (HIST_ENTRY *)NULL;
  2744.       rl_end = rl_point = strlen (the_line);
  2745.     }
  2746.   else
  2747.     ding ();
  2748.   return 0;
  2749. }
  2750.  
  2751. /* Save the current line in saved_line_for_history. */
  2752. maybe_save_line ()
  2753. {
  2754.   if (!saved_line_for_history)
  2755.     {
  2756.       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  2757.       saved_line_for_history->line = savestring (the_line);
  2758.       saved_line_for_history->data = (char *)rl_undo_list;
  2759.     }
  2760.   return 0;
  2761. }
  2762.  
  2763. /* **************************************************************** */
  2764. /*                                    */
  2765. /*            History Commands                */
  2766. /*                                    */
  2767. /* **************************************************************** */
  2768.  
  2769. /* Meta-< goes to the start of the history. */
  2770. rl_beginning_of_history (count, key)
  2771.      int count, key;
  2772. {
  2773.   return (rl_get_previous_history (1 + where_history ()));
  2774. }
  2775.  
  2776. /* Meta-> goes to the end of the history.  (The current line). */
  2777. rl_end_of_history (count, key)
  2778.      int count, key;
  2779. {
  2780.   maybe_replace_line ();
  2781.   using_history ();
  2782.   maybe_unsave_line ();
  2783.   return 0;
  2784. }
  2785.  
  2786. /* Move down to the next history line. */
  2787. rl_get_next_history (count, key)
  2788.      int count, key;
  2789. {
  2790.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  2791.  
  2792.   if (count < 0)
  2793.     return (rl_get_previous_history (-count));
  2794.  
  2795.   if (!count)
  2796.     return 0;
  2797.  
  2798.   maybe_replace_line ();
  2799.  
  2800.   while (count)
  2801.     {
  2802.       temp = next_history ();
  2803.       if (!temp)
  2804.     break;
  2805.       --count;
  2806.     }
  2807.  
  2808.   if (!temp)
  2809.     maybe_unsave_line ();
  2810.   else
  2811.     {
  2812.       int line_len;
  2813.  
  2814.       line_len = strlen (temp->line);
  2815.  
  2816.       if (line_len >= rl_line_buffer_len)
  2817.     rl_extend_line_buffer (line_len);
  2818.  
  2819.       strcpy (the_line, temp->line);
  2820.       rl_undo_list = (UNDO_LIST *)temp->data;
  2821.       rl_end = rl_point = strlen (the_line);
  2822. #if defined (VI_MODE)
  2823.       if (rl_editing_mode == vi_mode)
  2824.     rl_point = 0;
  2825. #endif /* VI_MODE */
  2826.     }
  2827.   return 0;
  2828. }
  2829.  
  2830. /* Get the previous item out of our interactive history, making it the current
  2831.    line.  If there is no previous history, just ding. */
  2832. rl_get_previous_history (count, key)
  2833.      int count, key;
  2834. {
  2835.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  2836.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  2837.  
  2838.   if (count < 0)
  2839.     return (rl_get_next_history (-count));
  2840.  
  2841.   if (!count)
  2842.     return 0;
  2843.  
  2844.   /* If we don't have a line saved, then save this one. */
  2845.   maybe_save_line ();
  2846.  
  2847.   /* If the current line has changed, save the changes. */
  2848.   maybe_replace_line ();
  2849.  
  2850.   while (count)
  2851.     {
  2852.       temp = previous_history ();
  2853.       if (!temp)
  2854.     break;
  2855.       else
  2856.     old_temp = temp;
  2857.       --count;
  2858.     }
  2859.  
  2860.   /* If there was a large argument, and we moved back to the start of the
  2861.      history, that is not an error.  So use the last value found. */
  2862.   if (!temp && old_temp)
  2863.     temp = old_temp;
  2864.  
  2865.   if (!temp)
  2866.     ding ();
  2867.   else
  2868.     {
  2869.       int line_len;
  2870.  
  2871.       line_len = strlen (temp->line);
  2872.  
  2873.       if (line_len >= rl_line_buffer_len)
  2874.     rl_extend_line_buffer (line_len);
  2875.  
  2876.       strcpy (the_line, temp->line);
  2877.       rl_undo_list = (UNDO_LIST *)temp->data;
  2878.       rl_end = rl_point = line_len;
  2879.  
  2880. #if defined (VI_MODE)
  2881.       if (rl_editing_mode == vi_mode)
  2882.     rl_point = 0;
  2883. #endif /* VI_MODE */
  2884.     }
  2885.   return 0;
  2886. }
  2887.  
  2888. /* Make C be the next command to be executed. */
  2889. rl_execute_next (c)
  2890.      int c;
  2891. {
  2892.   rl_pending_input = c;
  2893.   return 0;
  2894. }
  2895.  
  2896. /* **************************************************************** */
  2897. /*                                    */
  2898. /*           The Mark and the Region.                */
  2899. /*                                    */
  2900. /* **************************************************************** */
  2901.  
  2902. /* Set the mark at POSITION. */
  2903. rl_set_mark (position)
  2904.      int position;
  2905. {
  2906.   if (position > rl_end)
  2907.     return -1;
  2908.  
  2909.   rl_mark = position;
  2910.   return 0;
  2911. }
  2912.  
  2913. /* Exchange the position of mark and point. */
  2914. rl_exchange_mark_and_point (count, key)
  2915.      int count, key;
  2916. {
  2917.   if (rl_mark > rl_end)
  2918.     rl_mark = -1;
  2919.  
  2920.   if (rl_mark == -1)
  2921.     {
  2922.       ding ();
  2923.       return -1;
  2924.     }
  2925.   else
  2926.     {
  2927.       int temp = rl_point;
  2928.  
  2929.       rl_point = rl_mark;
  2930.       rl_mark = temp;
  2931.     }
  2932.   return 0;
  2933. }
  2934.  
  2935.  
  2936. /* **************************************************************** */
  2937. /*                                    */
  2938. /*            Killing Mechanism                */
  2939. /*                                    */
  2940. /* **************************************************************** */
  2941.  
  2942. /* What we assume for a max number of kills. */
  2943. #define DEFAULT_MAX_KILLS 10
  2944.  
  2945. /* The real variable to look at to find out when to flush kills. */
  2946. int rl_max_kills =  DEFAULT_MAX_KILLS;
  2947.  
  2948. /* Where to store killed text. */
  2949. char **rl_kill_ring = (char **)NULL;
  2950.  
  2951. /* Where we are in the kill ring. */
  2952. int rl_kill_index = 0;
  2953.  
  2954. /* How many slots we have in the kill ring. */
  2955. int rl_kill_ring_length = 0;
  2956.  
  2957. /* How to say that you only want to save a certain amount
  2958.    of kill material. */
  2959. rl_set_retained_kills (num)
  2960.      int num;
  2961. {
  2962.   return 0;
  2963. }
  2964.  
  2965. /* The way to kill something.  This appends or prepends to the last
  2966.    kill, if the last command was a kill command.  if FROM is less
  2967.    than TO, then the text is appended, otherwise prepended.  If the
  2968.    last command was not a kill command, then a new slot is made for
  2969.    this kill. */
  2970. rl_kill_text (from, to)
  2971.      int from, to;
  2972. {
  2973.   int slot;
  2974.   char *text;
  2975.  
  2976.   /* Is there anything to kill? */
  2977.   if (from == to)
  2978.     {
  2979.       last_command_was_kill++;
  2980.       return 0;
  2981.     }
  2982.  
  2983.   text = rl_copy_text (from, to);
  2984.  
  2985.   /* Delete the copied text from the line. */
  2986.   rl_delete_text (from, to);
  2987.  
  2988.   /* First, find the slot to work with. */
  2989.   if (!last_command_was_kill)
  2990.     {
  2991.       /* Get a new slot.  */
  2992.       if (!rl_kill_ring)
  2993.     {
  2994.       /* If we don't have any defined, then make one. */
  2995.       rl_kill_ring = (char **)
  2996.         xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  2997.       rl_kill_ring[slot = 0] = (char *)NULL;
  2998.     }
  2999.       else
  3000.     {
  3001.       /* We have to add a new slot on the end, unless we have
  3002.          exceeded the max limit for remembering kills. */
  3003.       slot = rl_kill_ring_length;
  3004.       if (slot == rl_max_kills)
  3005.         {
  3006.           register int i;
  3007.           free (rl_kill_ring[0]);
  3008.           for (i = 0; i < slot; i++)
  3009.         rl_kill_ring[i] = rl_kill_ring[i + 1];
  3010.         }
  3011.       else
  3012.         {
  3013.           slot = rl_kill_ring_length += 1;
  3014.           rl_kill_ring = (char **)xrealloc (rl_kill_ring, slot * sizeof (char *));
  3015.         }
  3016.       rl_kill_ring[--slot] = (char *)NULL;
  3017.     }
  3018.     }
  3019.   else
  3020.     slot = rl_kill_ring_length - 1;
  3021.  
  3022.   /* If the last command was a kill, prepend or append. */
  3023.   if (last_command_was_kill && rl_editing_mode != vi_mode)
  3024.     {
  3025.       char *old = rl_kill_ring[slot];
  3026.       char *new = xmalloc (1 + strlen (old) + strlen (text));
  3027.  
  3028.       if (from < to)
  3029.     {
  3030.       strcpy (new, old);
  3031.       strcat (new, text);
  3032.     }
  3033.       else
  3034.     {
  3035.       strcpy (new, text);
  3036.       strcat (new, old);
  3037.     }
  3038.       free (old);
  3039.       free (text);
  3040.       rl_kill_ring[slot] = new;
  3041.     }
  3042.   else
  3043.     {
  3044.       rl_kill_ring[slot] = text;
  3045.     }
  3046.   rl_kill_index = slot;
  3047.   last_command_was_kill++;
  3048.   return 0;
  3049. }
  3050.  
  3051. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  3052.    commands always make rl_point's original position be the FROM argument,
  3053.    and rl_point's extent be the TO argument. */
  3054.  
  3055. /* **************************************************************** */
  3056. /*                                    */
  3057. /*            Killing Commands                */
  3058. /*                                    */
  3059. /* **************************************************************** */
  3060.  
  3061. /* Delete the word at point, saving the text in the kill ring. */
  3062. rl_kill_word (count, key)
  3063.      int count, key;
  3064. {
  3065.   int orig_point = rl_point;
  3066.  
  3067.   if (count < 0)
  3068.     return (rl_backward_kill_word (-count));
  3069.   else
  3070.     {
  3071.       rl_forward_word (count);
  3072.  
  3073.       if (rl_point != orig_point)
  3074.     rl_kill_text (orig_point, rl_point);
  3075.  
  3076.       rl_point = orig_point;
  3077.     }
  3078.   return 0;
  3079. }
  3080.  
  3081. /* Rubout the word before point, placing it on the kill ring. */
  3082. rl_backward_kill_word (count, ignore)
  3083.      int count, ignore;
  3084. {
  3085.   int orig_point = rl_point;
  3086.  
  3087.   if (count < 0)
  3088.     return (rl_kill_word (-count));
  3089.   else
  3090.     {
  3091.       rl_backward_word (count);
  3092.  
  3093.       if (rl_point != orig_point)
  3094.     rl_kill_text (orig_point, rl_point);
  3095.     }
  3096.   return 0;
  3097. }
  3098.  
  3099. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  3100.    back to the line start instead. */
  3101. rl_kill_line (direction, ignore)
  3102.      int direction, ignore;
  3103. {
  3104.   int orig_point = rl_point;
  3105.  
  3106.   if (direction < 0)
  3107.     return (rl_backward_kill_line (1));
  3108.   else
  3109.     {
  3110.       rl_end_of_line ();
  3111.       if (orig_point != rl_point)
  3112.     rl_kill_text (orig_point, rl_point);
  3113.       rl_point = orig_point;
  3114.     }
  3115.   return 0;
  3116. }
  3117.  
  3118. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  3119.    forwards to the line end instead. */
  3120. rl_backward_kill_line (direction, ignore)
  3121.      int direction, ignore;
  3122. {
  3123.   int orig_point = rl_point;
  3124.  
  3125.   if (direction < 0)
  3126.     return (rl_kill_line (1));
  3127.   else
  3128.     {
  3129.       if (!rl_point)
  3130.     ding ();
  3131.       else
  3132.     {
  3133.       rl_beg_of_line ();
  3134.       rl_kill_text (orig_point, rl_point);
  3135.     }
  3136.     }
  3137.   return 0;
  3138. }
  3139.  
  3140. /* Kill the whole line, no matter where point is. */
  3141. rl_kill_full_line (count, ignore)
  3142.      int count, ignore;
  3143. {
  3144.   rl_begin_undo_group ();
  3145.   rl_point = 0;
  3146.   rl_kill_text (rl_point, rl_end);
  3147.   rl_end_undo_group ();
  3148.   return 0;
  3149. }
  3150.  
  3151. /* Yank back the last killed text.  This ignores arguments. */
  3152. rl_yank (count, ignore)
  3153.      int count, ignore;
  3154. {
  3155.   if (!rl_kill_ring)
  3156.     {
  3157.       rl_abort ();
  3158.       return -1;
  3159.     }
  3160.  
  3161.   rl_set_mark (rl_point);
  3162.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  3163.   return 0;
  3164. }
  3165.  
  3166. /* If the last command was yank, or yank_pop, and the text just
  3167.    before point is identical to the current kill item, then
  3168.    delete that text from the line, rotate the index down, and
  3169.    yank back some other text. */
  3170. rl_yank_pop (count, key)
  3171.      int count, key;
  3172. {
  3173.   int l;
  3174.  
  3175.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  3176.       !rl_kill_ring)
  3177.     {
  3178.       rl_abort ();
  3179.       return -1;
  3180.     }
  3181.  
  3182.   l = strlen (rl_kill_ring[rl_kill_index]);
  3183.   if (((rl_point - l) >= 0) &&
  3184.       (strncmp (the_line + (rl_point - l),
  3185.         rl_kill_ring[rl_kill_index], l) == 0))
  3186.     {
  3187.       rl_delete_text ((rl_point - l), rl_point);
  3188.       rl_point -= l;
  3189.       rl_kill_index--;
  3190.       if (rl_kill_index < 0)
  3191.     rl_kill_index = rl_kill_ring_length - 1;
  3192.       rl_yank (1, 0);
  3193.       return 0;
  3194.     }
  3195.   else
  3196.     {
  3197.       rl_abort ();
  3198.       return -1;
  3199.     }
  3200. }
  3201.  
  3202. /* Yank the COUNTth argument from the previous history line. */
  3203. rl_yank_nth_arg (count, ignore)
  3204.      int count, ignore;
  3205. {
  3206.   register HIST_ENTRY *entry = previous_history ();
  3207.   char *arg;
  3208.  
  3209.   if (entry)
  3210.     next_history ();
  3211.   else
  3212.     {
  3213.       ding ();
  3214.       return -1;
  3215.     }
  3216.  
  3217.   arg = history_arg_extract (count, count, entry->line);
  3218.   if (!arg || !*arg)
  3219.     {
  3220.       ding ();
  3221.       return -1;
  3222.     }
  3223.  
  3224.   rl_begin_undo_group ();
  3225.  
  3226. #if defined (VI_MODE)
  3227.   /* Vi mode always inserts a space before yanking the argument, and it
  3228.      inserts it right *after* rl_point. */
  3229.   if (rl_editing_mode == vi_mode)
  3230.     {
  3231.       rl_vi_append_mode ();
  3232.       rl_insert_text (" ");
  3233.     }
  3234. #endif /* VI_MODE */
  3235.  
  3236.   rl_insert_text (arg);
  3237.   free (arg);
  3238.  
  3239.   rl_end_undo_group ();
  3240.   return 0;
  3241. }
  3242.  
  3243. /* Yank the last argument from the previous history line.  This `knows'
  3244.    how rl_yank_nth_arg treats a count of `$'.  With an argument, this
  3245.    behaves the same as rl_yank_nth_arg. */
  3246. int
  3247. rl_yank_last_arg (count, key)
  3248.      int count, key;
  3249. {
  3250.   if (rl_explicit_arg)
  3251.     return (rl_yank_nth_arg (count, key));
  3252.   else
  3253.     return (rl_yank_nth_arg ('$', key));
  3254. }
  3255.  
  3256. /* How to toggle back and forth between editing modes. */
  3257. rl_vi_editing_mode (count, key)
  3258.      int count, key;
  3259. {
  3260. #if defined (VI_MODE)
  3261.   rl_editing_mode = vi_mode;
  3262.   rl_vi_insertion_mode ();
  3263.   return 0;
  3264. #endif /* VI_MODE */
  3265. }
  3266.  
  3267. rl_emacs_editing_mode (count, key)
  3268.      int count, key;
  3269. {
  3270.   rl_editing_mode = emacs_mode;
  3271.   _rl_keymap = emacs_standard_keymap;
  3272.   return 0;
  3273. }
  3274.  
  3275.  
  3276. /* **************************************************************** */
  3277. /*                                    */
  3278. /*            USG (System V) Support                */
  3279. /*                                    */
  3280. /* **************************************************************** */
  3281.  
  3282. int
  3283. rl_getc (stream)
  3284.      FILE *stream;
  3285. {
  3286.   int result;
  3287.   unsigned char c;
  3288.  
  3289. #if defined (__GO32__)
  3290.   if (isatty (0))
  3291.     return (getkey () & 0x7F);
  3292. #endif /* __GO32__ */
  3293.  
  3294.   while (1)
  3295.     {
  3296.       result = read (fileno (stream), &c, sizeof (unsigned char));
  3297.  
  3298.       if (result == sizeof (unsigned char))
  3299.     return (c);
  3300.  
  3301.       /* If zero characters are returned, then the file that we are
  3302.      reading from is empty!  Return EOF in that case. */
  3303.       if (result == 0)
  3304.     return (EOF);
  3305.  
  3306. #if defined (EWOULDBLOCK)
  3307.       if (errno == EWOULDBLOCK)
  3308.     {
  3309.       int flags;
  3310.  
  3311.       if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
  3312.         return (EOF);
  3313.       if (flags & O_NDELAY)
  3314.         {
  3315.           flags &= ~O_NDELAY;
  3316.           fcntl (fileno (stream), F_SETFL, flags);
  3317.           continue;
  3318.         }
  3319.       continue;
  3320.     }
  3321. #endif /* EWOULDBLOCK */
  3322.  
  3323. #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
  3324.       if (errno == EAGAIN)
  3325.     {
  3326.       int flags;
  3327.  
  3328.       if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
  3329.         return (EOF);
  3330.       if (flags & O_NONBLOCK)
  3331.         {
  3332.           flags &= ~O_NONBLOCK;
  3333.           fcntl (fileno (stream), F_SETFL, flags);
  3334.           continue;
  3335.         }
  3336.     }
  3337. #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
  3338.  
  3339. #if !defined (__GO32__)
  3340.       /* If the error that we received was SIGINT, then try again,
  3341.      this is simply an interrupted system call to read ().
  3342.      Otherwise, some error ocurred, also signifying EOF. */
  3343.       if (errno != EINTR)
  3344.     return (EOF);
  3345. #endif /* !__GO32__ */
  3346.     }
  3347. }
  3348.  
  3349. #if !defined (SHELL)
  3350. #ifdef savestring
  3351. #undef savestring
  3352. #endif
  3353. /* Backwards compatibilty, now that savestring has been removed from
  3354.    all `public' readline header files. */
  3355. char *
  3356. savestring (s)
  3357.      char *s;
  3358. {
  3359.   return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
  3360. }
  3361. #endif
  3362.  
  3363. /* Function equivalents for the macros defined in chartypes.h. */
  3364. #undef uppercase_p
  3365. int
  3366. uppercase_p (c)
  3367.      int c;
  3368. {
  3369.   return (isupper (c));
  3370. }
  3371.  
  3372. #undef lowercase_p
  3373. int
  3374. lowercase_p (c)
  3375.      int c;
  3376. {
  3377.   return (islower (c));
  3378. }
  3379.  
  3380. #undef pure_alphabetic
  3381. int
  3382. pure_alphabetic (c)
  3383.      int c;
  3384. {
  3385.   return (isupper (c) || islower (c));
  3386. }
  3387.  
  3388. #undef digit_p
  3389. int
  3390. digit_p (c)
  3391.      int c;
  3392. {
  3393.   return (isdigit (c));
  3394. }
  3395.  
  3396. #undef to_lower
  3397. int
  3398. to_lower (c)
  3399.      int c;
  3400. {
  3401.   return (isupper (c) ? tolower (c) : c);
  3402. }
  3403.  
  3404. #undef to_upper
  3405. int
  3406. to_upper (c)
  3407.      int c;
  3408. {
  3409.   return (islower (c) ? toupper (c) : c);
  3410. }
  3411.  
  3412. #undef digit_value
  3413. int
  3414. digit_value (c)
  3415.      int c;
  3416. {
  3417.   return (isdigit (c) ? c - '0' : c);
  3418. }
  3419.  
  3420. #if defined (STATIC_MALLOC)
  3421.  
  3422. /* **************************************************************** */
  3423. /*                                    */
  3424. /*            xmalloc and xrealloc ()                     */
  3425. /*                                    */
  3426. /* **************************************************************** */
  3427.  
  3428. static void memory_error_and_abort ();
  3429.  
  3430. static char *
  3431. xmalloc (bytes)
  3432.      int bytes;
  3433. {
  3434.   char *temp = (char *)malloc (bytes);
  3435.  
  3436.   if (!temp)
  3437.     memory_error_and_abort ();
  3438.   return (temp);
  3439. }
  3440.  
  3441. static char *
  3442. xrealloc (pointer, bytes)
  3443.      char *pointer;
  3444.      int bytes;
  3445. {
  3446.   char *temp;
  3447.  
  3448.   if (!pointer)
  3449.     temp = (char *)malloc (bytes);
  3450.   else
  3451.     temp = (char *)realloc (pointer, bytes);
  3452.  
  3453.   if (!temp)
  3454.     memory_error_and_abort ();
  3455.  
  3456.   return (temp);
  3457. }
  3458.  
  3459. static void
  3460. memory_error_and_abort ()
  3461. {
  3462.   fprintf (stderr, "readline: Out of virtual memory!\n");
  3463.   abort ();
  3464. }
  3465. #endif /* STATIC_MALLOC */
  3466.  
  3467.  
  3468. /* **************************************************************** */
  3469. /*                                    */
  3470. /*            Testing Readline                */
  3471. /*                                    */
  3472. /* **************************************************************** */
  3473.  
  3474. #if defined (TEST)
  3475.  
  3476. main ()
  3477. {
  3478.   HIST_ENTRY **history_list ();
  3479.   char *temp = (char *)NULL;
  3480.   char *prompt = "readline% ";
  3481.   int done = 0;
  3482.  
  3483.   while (!done)
  3484.     {
  3485.       temp = readline (prompt);
  3486.  
  3487.       /* Test for EOF. */
  3488.       if (!temp)
  3489.     exit (1);
  3490.  
  3491.       /* If there is anything on the line, print it and remember it. */
  3492.       if (*temp)
  3493.     {
  3494.       fprintf (stderr, "%s\r\n", temp);
  3495.       add_history (temp);
  3496.     }
  3497.  
  3498.       /* Check for `command' that we handle. */
  3499.       if (strcmp (temp, "quit") == 0)
  3500.     done = 1;
  3501.  
  3502.       if (strcmp (temp, "list") == 0)
  3503.     {
  3504.       HIST_ENTRY **list = history_list ();
  3505.       register int i;
  3506.       if (list)
  3507.         {
  3508.           for (i = 0; list[i]; i++)
  3509.         {
  3510.           fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  3511.           free (list[i]->line);
  3512.         }
  3513.           free (list);
  3514.         }
  3515.     }
  3516.       free (temp);
  3517.     }
  3518. }
  3519.  
  3520. #endif /* TEST */
  3521.  
  3522.  
  3523. /*
  3524.  * Local variables:
  3525.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  3526.  * end:
  3527.  */
  3528.