home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / readline / readline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-30  |  72.9 KB  |  3,296 lines

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