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

  1. /* History.c -- standalone history library */
  2.  
  3. /* Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file contains the GNU History Library (the Library), a set of
  6.    routines for managing the text of previously typed lines.
  7.  
  8.    The Library is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    The Library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* The goal is to make the implementation transparent, so that you
  23.    don't have to know what data types are used, just what functions
  24.    you can call.  I think I have done that. */
  25.  
  26. /* Remove these declarations when we have a complete libgnu.a. */
  27. #if !defined (STATIC_MALLOC)
  28. extern char *xmalloc (), *xrealloc ();
  29. #else
  30. static char *xmalloc (), *xrealloc ();
  31. #endif
  32.  
  33. #include "sysdep.h"
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #ifndef    NO_SYS_FILE
  37. #include <sys/file.h>
  38. #endif
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41.  
  42. #include "history.h"
  43.  
  44. #ifndef savestring
  45. #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
  46. #endif
  47.  
  48. #ifndef whitespace
  49. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  50. #endif
  51.  
  52. #ifndef digit
  53. #define digit(c)  ((c) >= '0' && (c) <= '9')
  54. #endif
  55.  
  56. #ifndef member
  57. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  58. #endif
  59.  
  60. /* **************************************************************** */
  61. /*                                    */
  62. /*            History Functions                */
  63. /*                                    */
  64. /* **************************************************************** */
  65.  
  66. /* An array of HIST_ENTRY.  This is where we store the history. */
  67. static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
  68.  
  69. /* Non-zero means that we have enforced a limit on the amount of
  70.    history that we save. */
  71. int history_stifled = 0;
  72.  
  73. /* If HISTORY_STIFLED is non-zero, then this is the maximum number of
  74.    entries to remember. */
  75. int max_input_history;
  76.  
  77. /* The current location of the interactive history pointer.  Just makes
  78.    life easier for outside callers. */
  79. static int history_offset = 0;
  80.  
  81. /* The number of strings currently stored in the input_history list. */
  82. int history_length = 0;
  83.  
  84. /* The current number of slots allocated to the input_history. */
  85. static int history_size = 0;
  86.  
  87. /* The number of slots to increase the_history by. */
  88. #define DEFAULT_HISTORY_GROW_SIZE 50
  89.  
  90. /* The character that represents the start of a history expansion
  91.    request.  This is usually `!'. */
  92. char history_expansion_char = '!';
  93.  
  94. /* The character that invokes word substitution if found at the start of
  95.    a line.  This is usually `^'. */
  96. char history_subst_char = '^';
  97.  
  98. /* During tokenization, if this character is seen as the first character
  99.    of a word, then it, and all subsequent characters upto a newline are
  100.    ignored.  For a Bourne shell, this should be '#'.  Bash special cases
  101.    the interactive comment character to not be a comment delimiter. */
  102. char history_comment_char = '\0';
  103.  
  104. /* The list of characters which inhibit the expansion of text if found
  105.    immediately following history_expansion_char. */
  106. char *history_no_expand_chars = " \t\n\r=";
  107.  
  108. /* The logical `base' of the history array.  It defaults to 1. */
  109. int history_base = 1;
  110.  
  111. /* Begin a session in which the history functions might be used.  This
  112.    initializes interactive variables. */
  113. void
  114. using_history ()
  115. {
  116.   history_offset = history_length;
  117. }
  118.  
  119. /* Return the number of bytes that the primary history entries are using.
  120.    This just adds up the lengths of the_history->lines. */
  121. int
  122. history_total_bytes ()
  123. {
  124.   register int i, result;
  125.  
  126.   result = 0;
  127.  
  128.   for (i = 0; the_history && the_history[i]; i++)
  129.     result += strlen (the_history[i]->line);
  130.  
  131.   return (result);
  132. }
  133.  
  134. /* Place STRING at the end of the history list.  The data field
  135.    is  set to NULL. */
  136. void
  137. add_history (string)
  138.      char *string;
  139. {
  140.   HIST_ENTRY *temp;
  141.  
  142.   if (history_stifled && (history_length == max_input_history))
  143.     {
  144.       register int i;
  145.  
  146.       /* If the history is stifled, and history_length is zero,
  147.      and it equals max_input_history, we don't save items. */
  148.       if (!history_length)
  149.     return;
  150.  
  151.       /* If there is something in the slot, then remove it. */
  152.       if (the_history[0])
  153.     {
  154.       free (the_history[0]->line);
  155.       free (the_history[0]);
  156.     }
  157.  
  158.       for (i = 0; i < history_length; i++)
  159.     the_history[i] = the_history[i + 1];
  160.  
  161.       history_base++;
  162.  
  163.     }
  164.   else
  165.     {
  166.       if (!history_size)
  167.     {
  168.       the_history = (HIST_ENTRY **)
  169.         xmalloc ((history_size = DEFAULT_HISTORY_GROW_SIZE)
  170.              * sizeof (HIST_ENTRY *));
  171.       history_length = 1;
  172.  
  173.     }
  174.       else
  175.     {
  176.       if (history_length == (history_size - 1))
  177.         {
  178.           the_history = (HIST_ENTRY **)
  179.         xrealloc (the_history,
  180.               ((history_size += DEFAULT_HISTORY_GROW_SIZE)
  181.                * sizeof (HIST_ENTRY *)));
  182.       }
  183.       history_length++;
  184.     }
  185.     }
  186.  
  187.   temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  188.   temp->line = savestring (string);
  189.   temp->data = (char *)NULL;
  190.  
  191.   the_history[history_length] = (HIST_ENTRY *)NULL;
  192.   the_history[history_length - 1] = temp;
  193. }
  194.  
  195. /* Make the history entry at WHICH have LINE and DATA.  This returns
  196.    the old entry so you can dispose of the data.  In the case of an
  197.    invalid WHICH, a NULL pointer is returned. */
  198. HIST_ENTRY *
  199. replace_history_entry (which, line, data)
  200.      int which;
  201.      char *line;
  202.      char *data;
  203. {
  204.   HIST_ENTRY *temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  205.   HIST_ENTRY *old_value;
  206.  
  207.   if (which >= history_length)
  208.     return ((HIST_ENTRY *)NULL);
  209.  
  210.   old_value = the_history[which];
  211.  
  212.   temp->line = savestring (line);
  213.   temp->data = data;
  214.   the_history[which] = temp;
  215.  
  216.   return (old_value);
  217. }
  218.  
  219. /* Returns the magic number which says what history element we are
  220.    looking at now.  In this implementation, it returns history_offset. */
  221. int
  222. where_history ()
  223. {
  224.   return (history_offset);
  225. }
  226.  
  227. /* Search the history for STRING, starting at history_offset.
  228.    If DIRECTION < 0, then the search is through previous entries, else
  229.    through subsequent.  If ANCHORED is non-zero, the string must
  230.    appear at the beginning of a history line, otherwise, the string
  231.    may appear anywhere in the line.  If the string is found, then
  232.    current_history () is the history entry, and the value of this
  233.    function is the offset in the line of that history entry that the
  234.    string was found in.  Otherwise, nothing is changed, and a -1 is
  235.    returned. */
  236.  
  237. #define ANCHORED_SEARCH 1
  238. #define NON_ANCHORED_SEARCH 0
  239.  
  240. static int
  241. history_search_internal (string, direction, anchored)
  242.      char *string;
  243.      int direction, anchored;
  244. {
  245.   register int i = history_offset;
  246.   register int reverse = (direction < 0);
  247.   register char *line;
  248.   register int index;
  249.   int string_len = strlen (string);
  250.  
  251.   /* Take care of trivial cases first. */
  252.  
  253.   if (!history_length || ((i == history_length) && !reverse))
  254.     return (-1);
  255.  
  256.   if (reverse && (i == history_length))
  257.     i--;
  258.  
  259.   while (1)
  260.     {
  261.       /* Search each line in the history list for STRING. */
  262.  
  263.       /* At limit for direction? */
  264.       if ((reverse && i < 0) ||
  265.       (!reverse && i == history_length))
  266.     return (-1);
  267.  
  268.       line = the_history[i]->line;
  269.       index = strlen (line);
  270.  
  271.       /* If STRING is longer than line, no match. */
  272.       if (string_len > index)
  273.     goto next_line;
  274.  
  275.       /* Handle anchored searches first. */
  276.       if (anchored == ANCHORED_SEARCH)
  277.     {
  278.       if (strncmp (string, line, string_len) == 0)
  279.         {
  280.           history_offset = i;
  281.           return (0);
  282.         }
  283.  
  284.       goto next_line;
  285.     }
  286.  
  287.       /* Do substring search. */
  288.       if (reverse)
  289.     {
  290.       index -= string_len;
  291.  
  292.       while (index >= 0)
  293.         {
  294.           if (strncmp (string, line + index, string_len) == 0)
  295.         {
  296.           history_offset = i;
  297.           return (index);
  298.         }
  299.           index--;
  300.         }
  301.     }
  302.       else
  303.     {
  304.       register int limit = index - string_len + 1;
  305.       index = 0;
  306.  
  307.       while (index < limit)
  308.         {
  309.           if (strncmp (string, line + index, string_len) == 0)
  310.         {
  311.           history_offset = i;
  312.           return (index);
  313.         }
  314.           index++;
  315.         }
  316.     }
  317.     next_line:
  318.       if (reverse)
  319.     i--;
  320.       else
  321.     i++;
  322.     }
  323. }
  324.  
  325. /* Do a non-anchored search for STRING through the history in DIRECTION. */
  326. int
  327. history_search (string, direction)
  328.      char *string;
  329.      int direction;
  330. {
  331.   return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
  332. }
  333.  
  334. /* Do an anchored search for string through the history in DIRECTION. */
  335. int
  336. history_search_prefix (string, direction)
  337.      char *string;
  338.      int direction;
  339. {
  340.   return (history_search_internal (string, direction, ANCHORED_SEARCH));
  341. }
  342.  
  343. /* Remove history element WHICH from the history.  The removed
  344.    element is returned to you so you can free the line, data,
  345.    and containing structure. */
  346. HIST_ENTRY *
  347. remove_history (which)
  348.      int which;
  349. {
  350.   HIST_ENTRY *return_value;
  351.  
  352.   if (which >= history_length || !history_length)
  353.     return_value = (HIST_ENTRY *)NULL;
  354.   else
  355.     {
  356.       register int i;
  357.       return_value = the_history[which];
  358.  
  359.       for (i = which; i < history_length; i++)
  360.     the_history[i] = the_history[i + 1];
  361.  
  362.       history_length--;
  363.     }
  364.  
  365.   return (return_value);
  366. }
  367.  
  368. /* Stifle the history list, remembering only MAX number of lines. */
  369. void
  370. stifle_history (max)
  371.      int max;
  372. {
  373.   if (max < 0)
  374.     max = 0;
  375.   if (history_length > max)
  376.     {
  377.       register int i, j;
  378.  
  379.       /* This loses because we cannot free the data. */
  380.       for (i = 0; i < (history_length - max); i++)
  381.     {
  382.       free (the_history[i]->line);
  383.       free (the_history[i]);
  384.     }
  385.       history_base = i;
  386.       for (j = 0, i = history_length - max; j < max; i++, j++)
  387.     the_history[j] = the_history[i];
  388.       the_history[j] = (HIST_ENTRY *)NULL;
  389.       history_length = j;
  390.     }
  391.   history_stifled = 1;
  392.   max_input_history = max;
  393. }
  394.  
  395. /* Stop stifling the history.  This returns the previous amount the history
  396.  was stifled by.  The value is positive if the history was stifled, negative
  397.  if it wasn't. */
  398. int
  399. unstifle_history ()
  400. {
  401.   int result = max_input_history;
  402.   if (history_stifled)
  403.     {
  404.       result = - result;
  405.       history_stifled = 0;
  406.     }
  407.   return (result);
  408. }
  409.  
  410. /* Return the string that should be used in the place of this
  411.    filename.  This only matters when you don't specify the
  412.    filename to read_history (), or write_history (). */
  413. static char *
  414. history_filename (filename)
  415.      char *filename;
  416. {
  417.   char *return_val = filename ? savestring (filename) : (char *)NULL;
  418.  
  419.   if (!return_val)
  420.     {
  421.       char *home = (char *)getenv ("HOME");
  422.       if (!home) home = ".";
  423.       return_val = (char *)xmalloc (2 + strlen (home) + strlen (".history"));
  424.       sprintf (return_val, "%s/.history", home);
  425.     }
  426.   return (return_val);
  427. }
  428.  
  429. /* Add the contents of FILENAME to the history list, a line at a time.
  430.    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
  431.    successful, or errno if not. */
  432. int
  433. read_history (filename)
  434.      char *filename;
  435. {
  436.   return (read_history_range (filename, 0, -1));
  437. }
  438.  
  439. /* Read a range of lines from FILENAME, adding them to the history list.
  440.    Start reading at the FROM'th line and end at the TO'th.  If FROM
  441.    is zero, start at the beginning.  If TO is less than FROM, read
  442.    until the end of the file.  If FILENAME is NULL, then read from
  443.    ~/.history.  Returns 0 if successful, or errno if not. */
  444. int
  445. read_history_range (filename, from, to)
  446.      char *filename;
  447.      int from, to;
  448. {
  449.   register int line_start, line_end;
  450.   char *input, *buffer = (char *)NULL;
  451.   int file, current_line;
  452.   struct stat finfo;
  453.   extern int errno;
  454.  
  455.   input = history_filename (filename);
  456.   file = open (input, O_RDONLY, 0666);
  457.  
  458.   if ((file < 0) ||
  459.       (stat (input, &finfo) == -1))
  460.     goto error_and_exit;
  461.  
  462.   buffer = (char *)xmalloc (finfo.st_size + 1);
  463.  
  464.   if (read (file, buffer, finfo.st_size) != finfo.st_size)
  465.   error_and_exit:
  466.     {
  467.       if (file >= 0)
  468.     close (file);
  469.  
  470.       if (buffer)
  471.     free (buffer);
  472.  
  473.       return (errno);
  474.     }
  475.  
  476.   close (file);
  477.  
  478.   /* Set TO to larger than end of file if negative. */
  479.   if (to < 0)
  480.     to = finfo.st_size;
  481.  
  482.   /* Start at beginning of file, work to end. */
  483.   line_start = line_end = current_line = 0;
  484.  
  485.   /* Skip lines until we are at FROM. */
  486.   while (line_start < finfo.st_size && current_line < from)
  487.     {
  488.       for (line_end = line_start; line_end < finfo.st_size; line_end++)
  489.     if (buffer[line_end] == '\n')
  490.       {
  491.         current_line++;
  492.         line_start = line_end + 1;
  493.         if (current_line == from)
  494.           break;
  495.       }
  496.     }
  497.  
  498.   /* If there are lines left to gobble, then gobble them now. */
  499.   for (line_end = line_start; line_end < finfo.st_size; line_end++)
  500.     if (buffer[line_end] == '\n')
  501.       {
  502.     buffer[line_end] = '\0';
  503.  
  504.     if (buffer[line_start])
  505.       add_history (buffer + line_start);
  506.  
  507.     current_line++;
  508.  
  509.     if (current_line >= to)
  510.       break;
  511.  
  512.     line_start = line_end + 1;
  513.       }
  514.   return (0);
  515. }
  516.  
  517. /* Truncate the history file FNAME, leaving only LINES trailing lines.
  518.    If FNAME is NULL, then use ~/.history. */
  519. history_truncate_file (fname, lines)
  520.      char *fname;
  521.      register int lines;
  522. {
  523.   register int i;
  524.   int file;
  525.   char *buffer = (char *)NULL, *filename;
  526.   struct stat finfo;
  527.  
  528.   filename = history_filename (fname);
  529.   if (stat (filename, &finfo) == -1)
  530.     goto truncate_exit;
  531.  
  532.   file = open (filename, O_RDONLY, 0666);
  533.  
  534.   if (file == -1)
  535.     goto truncate_exit;
  536.  
  537.   buffer = (char *)xmalloc (finfo.st_size + 1);
  538.   read (file, buffer, finfo.st_size);
  539.   close (file);
  540.  
  541.   /* Count backwards from the end of buffer until we have passed
  542.      LINES lines. */
  543.   for (i = finfo.st_size; lines && i; i--)
  544.     {
  545.       if (buffer[i] == '\n')
  546.     lines--;
  547.     }
  548.  
  549.   /* If there are fewer lines in the file than we want to truncate to,
  550.      then we are all done. */
  551.   if (!i)
  552.     goto truncate_exit;
  553.  
  554.   /* Otherwise, write from the start of this line until the end of the
  555.      buffer. */
  556.   for (--i; i; i--)
  557.     if (buffer[i] == '\n')
  558.       {
  559.     i++;
  560.     break;
  561.       }
  562.  
  563.   file = open (filename, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  564.   if (file == -1)
  565.     goto truncate_exit;
  566.  
  567.   write (file, buffer + i, finfo.st_size - i);
  568.   close (file);
  569.  
  570.  truncate_exit:
  571.   if (buffer)
  572.     free (buffer);
  573.  
  574.   free (filename);
  575. }
  576.  
  577. #define HISTORY_APPEND 0
  578. #define HISTORY_OVERWRITE 1
  579.  
  580. /* Workhorse function for writing history.  Writes NELEMENT entries
  581.    from the history list to FILENAME.  OVERWRITE is non-zero if you
  582.    wish to replace FILENAME with the entries. */
  583. static int
  584. history_do_write (filename, nelements, overwrite)
  585.      char *filename;
  586.      int nelements, overwrite;
  587. {
  588.   extern int errno;
  589.   register int i, j;
  590.   char *output = history_filename (filename);
  591.   int file, mode;
  592.  
  593.   if (overwrite)
  594.     mode = O_WRONLY | O_CREAT | O_TRUNC;
  595.   else
  596.     mode = O_WRONLY | O_APPEND;
  597.  
  598.   if ((file = open (output, mode, 0666)) == -1)
  599.     return (errno);
  600.  
  601.   if (nelements > history_length)
  602.     nelements = history_length;
  603.  
  604.   /* Build a buffer of all the lines to write, and write them in one syscall.
  605.      Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */
  606.   {
  607.     register int j = 0;
  608.     int buffer_size = 0;
  609.     char *buffer;
  610.  
  611.     /* Calculate the total number of bytes to write. */
  612.     for (i = history_length - nelements; i < history_length; i++)
  613.       buffer_size += 1 + strlen (the_history[i]->line);
  614.  
  615.     /* Allocate the buffer, and fill it. */
  616.     buffer = (char *)xmalloc (buffer_size);
  617.  
  618.     for (i = history_length - nelements; i < history_length; i++)
  619.       {
  620.     strcpy (buffer + j, the_history[i]->line);
  621.     j += strlen (the_history[i]->line);
  622.     buffer[j++] = '\n';
  623.       }
  624.  
  625.     write (file, buffer, buffer_size);
  626.     free (buffer);
  627.   }
  628.  
  629.   close (file);
  630.   return (0);
  631. }
  632.   
  633. /* Append NELEMENT entries to FILENAME.  The entries appended are from
  634.    the end of the list minus NELEMENTs up to the end of the list. */
  635. int
  636. append_history (nelements, filename)
  637.      int nelements;
  638.      char *filename;
  639. {
  640.   return (history_do_write (filename, nelements, HISTORY_APPEND));
  641. }
  642.  
  643. /* Overwrite FILENAME with the current history.  If FILENAME is NULL,
  644.    then write the history list to ~/.history.  Values returned
  645.    are as in read_history ().*/
  646. int
  647. write_history (filename)
  648.      char *filename;
  649. {
  650.   return (history_do_write (filename, history_length, HISTORY_OVERWRITE));
  651. }
  652.  
  653. /* Return the history entry at the current position, as determined by
  654.    history_offset.  If there is no entry there, return a NULL pointer. */
  655. HIST_ENTRY *
  656. current_history ()
  657. {
  658.   if ((history_offset == history_length) || !the_history)
  659.     return ((HIST_ENTRY *)NULL);
  660.   else
  661.     return (the_history[history_offset]);
  662. }
  663.  
  664. /* Back up history_offset to the previous history entry, and return
  665.    a pointer to that entry.  If there is no previous entry then return
  666.    a NULL pointer. */
  667. HIST_ENTRY *
  668. previous_history ()
  669. {
  670.   if (!history_offset)
  671.     return ((HIST_ENTRY *)NULL);
  672.   else
  673.     return (the_history[--history_offset]);
  674. }
  675.  
  676. /* Move history_offset forward to the next history entry, and return
  677.    a pointer to that entry.  If there is no next entry then return a
  678.    NULL pointer. */
  679. HIST_ENTRY *
  680. next_history ()
  681. {
  682.   if (history_offset == history_length)
  683.     return ((HIST_ENTRY *)NULL);
  684.   else
  685.     return (the_history[++history_offset]);
  686. }
  687.  
  688. /* Return the current history array.  The caller has to be carefull, since this
  689.    is the actual array of data, and could be bashed or made corrupt easily.
  690.    The array is terminated with a NULL pointer. */
  691. HIST_ENTRY **
  692. history_list ()
  693. {
  694.   return (the_history);
  695. }
  696.  
  697. /* Return the history entry which is logically at OFFSET in the history array.
  698.    OFFSET is relative to history_base. */
  699. HIST_ENTRY *
  700. history_get (offset)
  701.      int offset;
  702. {
  703.   int index = offset - history_base;
  704.  
  705.   if (index >= history_length ||
  706.       index < 0 ||
  707.       !the_history)
  708.     return ((HIST_ENTRY *)NULL);
  709.   return (the_history[index]);
  710. }
  711.  
  712. /* Search for STRING in the history list.  DIR is < 0 for searching
  713.    backwards.  POS is an absolute index into the history list at
  714.    which point to begin searching. */
  715. int
  716. history_search_pos (string, dir, pos)
  717.      char *string;
  718.      int dir, pos;
  719. {
  720.   int ret, old = where_history ();
  721.   history_set_pos (pos);
  722.   if (history_search (string, dir) == -1)
  723.     {
  724.       history_set_pos (old);
  725.       return (-1);
  726.     }
  727.   ret = where_history ();
  728.   history_set_pos (old);
  729.   return ret;
  730. }
  731.  
  732. /* Make the current history item be the one at POS, an absolute index.
  733.    Returns zero if POS is out of range, else non-zero. */
  734. int
  735. history_set_pos (pos)
  736.      int pos;
  737. {
  738.   if (pos > history_length || pos < 0 || !the_history)
  739.     return (0);
  740.   history_offset = pos;
  741.   return (1);
  742. }
  743.  
  744.  
  745. /* **************************************************************** */
  746. /*                                    */
  747. /*            History Expansion                */
  748. /*                                    */
  749. /* **************************************************************** */
  750.  
  751. /* Hairy history expansion on text, not tokens.  This is of general
  752.    use, and thus belongs in this library. */
  753.  
  754. /* The last string searched for in a !?string? search. */
  755. static char *search_string = (char *)NULL;
  756.  
  757. /* Return the event specified at TEXT + OFFSET modifying OFFSET to
  758.    point to after the event specifier.  Just a pointer to the history
  759.    line is returned; NULL is returned in the event of a bad specifier.
  760.    You pass STRING with *INDEX equal to the history_expansion_char that
  761.    begins this specification.
  762.    DELIMITING_QUOTE is a character that is allowed to end the string
  763.    specification for what to search for in addition to the normal
  764.    characters `:', ` ', `\t', `\n', and sometimes `?'.
  765.    So you might call this function like:
  766.    line = get_history_event ("!echo:p", &index, 0);  */
  767. char *
  768. get_history_event (string, caller_index, delimiting_quote)
  769.      char *string;
  770.      int *caller_index;
  771.      int delimiting_quote;
  772. {
  773.   register int i = *caller_index;
  774.   int which, sign = 1;
  775.   HIST_ENTRY *entry;
  776.  
  777.   /* The event can be specified in a number of ways.
  778.  
  779.      !!   the previous command
  780.      !n   command line N
  781.      !-n  current command-line minus N
  782.      !str the most recent command starting with STR
  783.      !?str[?]
  784.       the most recent command containing STR
  785.  
  786.      All values N are determined via HISTORY_BASE. */
  787.  
  788.   if (string[i] != history_expansion_char)
  789.     return ((char *)NULL);
  790.  
  791.   /* Move on to the specification. */
  792.   i++;
  793.  
  794.   /* Handle !! case. */
  795.   if (string[i] == history_expansion_char)
  796.     {
  797.       i++;
  798.       which = history_base + (history_length - 1);
  799.       *caller_index = i;
  800.       goto get_which;
  801.     }
  802.  
  803.   /* Hack case of numeric line specification. */
  804.  read_which:
  805.   if (string[i] == '-')
  806.     {
  807.       sign = -1;
  808.       i++;
  809.     }
  810.  
  811.   if (digit (string[i]))
  812.     {
  813.       int start = i;
  814.  
  815.       /* Get the extent of the digits. */
  816.       for (; digit (string[i]); i++);
  817.  
  818.       /* Get the digit value. */
  819.       sscanf (string + start, "%d", &which);
  820.  
  821.       *caller_index = i;
  822.  
  823.       if (sign < 0)
  824.     which = (history_length + history_base) - which;
  825.  
  826.     get_which:
  827.       if (entry = history_get (which))
  828.     return (entry->line);
  829.  
  830.       return ((char *)NULL);
  831.     }
  832.  
  833.   /* This must be something to search for.  If the spec begins with
  834.      a '?', then the string may be anywhere on the line.  Otherwise,
  835.      the string must be found at the start of a line. */
  836.   {
  837.     int index;
  838.     char *temp;
  839.     int substring_okay = 0;
  840.  
  841.     if (string[i] == '?')
  842.       {
  843.     substring_okay++;
  844.     i++;
  845.       }
  846.  
  847.     for (index = i; string[i]; i++)
  848.       if (whitespace (string[i]) ||
  849.       string[i] == '\n' ||
  850.       string[i] == ':' ||
  851.       (substring_okay && string[i] == '?') ||
  852.       string[i] == delimiting_quote)
  853.     break;
  854.  
  855.     temp = (char *)alloca (1 + (i - index));
  856.     strncpy (temp, &string[index], (i - index));
  857.     temp[i - index] = '\0';
  858.  
  859.     if (string[i] == '?')
  860.       i++;
  861.  
  862.     *caller_index = i;
  863.  
  864.   search_again:
  865.  
  866.     index = history_search_internal
  867.       (temp, -1, substring_okay ? NON_ANCHORED_SEARCH : ANCHORED_SEARCH);
  868.  
  869.     if (index < 0)
  870.     search_lost:
  871.       {
  872.     history_offset = history_length;
  873.     return ((char *)NULL);
  874.       }
  875.  
  876.     if (index == 0)
  877.       {
  878.       search_won:
  879.     entry = current_history ();
  880.     history_offset = history_length;
  881.     
  882.     /* If this was a substring search, then remember the string that
  883.        we matched for word substitution. */
  884.     if (substring_okay)
  885.       {
  886.         if (search_string)
  887.           free (search_string);
  888.         search_string = savestring (temp);
  889.       }
  890.  
  891.     return (entry->line);
  892.       }
  893.  
  894.     if (history_offset)
  895.       history_offset--;
  896.     else
  897.       goto search_lost;
  898.     
  899.     goto search_again;
  900.   }
  901. }
  902.  
  903. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  904.    to a string.  Returns:
  905.  
  906.    0) If no expansions took place (or, if the only change in
  907.       the text was the de-slashifying of the history expansion
  908.       character)
  909.    1) If expansions did take place
  910.   -1) If there was an error in expansion.
  911.  
  912.   If an error ocurred in expansion, then OUTPUT contains a descriptive
  913.   error message. */
  914. int
  915. history_expand (string, output)
  916.      char *string;
  917.      char **output;
  918. {
  919.   register int j, l = strlen (string);
  920.   int i, word_spec_error = 0;
  921.   int cc, modified = 0;
  922.   char *word_spec, *event;
  923.   int starting_index, only_printing = 0, substitute_globally = 0;
  924.  
  925.   char *get_history_word_specifier (), *rindex ();
  926.  
  927.   /* The output string, and its length. */
  928.   int len = 0;
  929.   char *result = (char *)NULL;
  930.  
  931.   /* Used in add_string; */
  932.   char *temp, tt[2], tbl[3];
  933.  
  934.   /* Prepare the buffer for printing error messages. */
  935.   result = (char *)xmalloc (len = 255);
  936.  
  937.   result[0] = tt[1] = tbl[2] = '\0';
  938.   tbl[0] = '\\';
  939.   tbl[1] = history_expansion_char;
  940.  
  941.   /* Grovel the string.  Only backslash can quote the history escape
  942.      character.  We also handle arg specifiers. */
  943.  
  944.   /* Before we grovel forever, see if the history_expansion_char appears
  945.      anywhere within the text. */
  946.  
  947.   /* The quick substitution character is a history expansion all right.  That
  948.      is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
  949.      that is the substitution that we do. */
  950.   if (string[0] == history_subst_char)
  951.     {
  952.       char *format_string = (char *)alloca (10 + strlen (string));
  953.  
  954.       sprintf (format_string, "%c%c:s%s",
  955.            history_expansion_char, history_expansion_char,
  956.            string);
  957.       string = format_string;
  958.       l += 4;
  959.       goto grovel;
  960.     }
  961.  
  962.   /* If not quick substitution, still maybe have to do expansion. */
  963.  
  964.   /* `!' followed by one of the characters in history_no_expand_chars
  965.      is NOT an expansion. */
  966.   for (i = 0; string[i]; i++)
  967.     if (string[i] == history_expansion_char)
  968.       if (!string[i + 1] || member (string[i + 1], history_no_expand_chars))
  969.     continue;
  970.       else
  971.     goto grovel;
  972.  
  973.   free (result);
  974.   *output = savestring (string);
  975.   return (0);
  976.  
  977.  grovel:
  978.  
  979.   for (i = j = 0; i < l; i++)
  980.     {
  981.       int tchar = string[i];
  982.       if (tchar == history_expansion_char)
  983.     tchar = -3;
  984.  
  985.       switch (tchar)
  986.     {
  987.     case '\\':
  988.       if (string[i + 1] == history_expansion_char)
  989.         {
  990.           i++;
  991.           temp = tbl;
  992.           goto do_add;
  993.         }
  994.       else
  995.         goto add_char;
  996.  
  997.       /* case history_expansion_char: */
  998.     case -3:
  999.       starting_index = i + 1;
  1000.       cc = string[i + 1];
  1001.  
  1002.       /* If the history_expansion_char is followed by one of the
  1003.          characters in history_no_expand_chars, then it is not a
  1004.          candidate for expansion of any kind. */
  1005.       if (member (cc, history_no_expand_chars))
  1006.         goto add_char;
  1007.  
  1008.       /* There is something that is listed as a `word specifier' in csh
  1009.          documentation which means `the expanded text to this point'.
  1010.          That is not a word specifier, it is an event specifier. */
  1011.  
  1012.       if (cc == '#')
  1013.         goto hack_pound_sign;
  1014.  
  1015.       /* If it is followed by something that starts a word specifier,
  1016.          then !! is implied as the event specifier. */
  1017.  
  1018.       if (member (cc, ":$*%^"))
  1019.         {
  1020.           char fake_s[3];
  1021.           int fake_i = 0;
  1022.           i++;
  1023.           fake_s[0] = fake_s[1] = history_expansion_char;
  1024.           fake_s[2] = '\0';
  1025.           event = get_history_event (fake_s, &fake_i, 0);
  1026.         }
  1027.       else
  1028.         {
  1029.           int quoted_search_delimiter = 0;
  1030.  
  1031.           /* If the character before this `!' is a double or single
  1032.          quote, then this expansion takes place inside of the
  1033.          quoted string.  If we have to search for some text ("!foo"),
  1034.          allow the delimiter to end the search string. */
  1035.           if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
  1036.         quoted_search_delimiter = string[i - 1];
  1037.  
  1038.           event = get_history_event (string, &i, quoted_search_delimiter);
  1039.         }
  1040.       
  1041.       if (!event)
  1042.       event_not_found:
  1043.         {
  1044.         int l = 1 + (i - starting_index);
  1045.  
  1046.         temp = (char *)alloca (1 + l);
  1047.         strncpy (temp, string + starting_index, l);
  1048.         temp[l - 1] = 0;
  1049.         sprintf (result, "%s: %s.", temp,
  1050.              word_spec_error ? "Bad word specifier" : "Event not found");
  1051.       error_exit:
  1052.         *output = result;
  1053.         return (-1);
  1054.       }
  1055.  
  1056.       /* If a word specifier is found, then do what that requires. */
  1057.       starting_index = i;
  1058.  
  1059.       word_spec = get_history_word_specifier (string, event, &i);
  1060.  
  1061.       /* There is no such thing as a `malformed word specifier'.  However,
  1062.          it is possible for a specifier that has no match.  In that case,
  1063.          we complain. */
  1064.       if (word_spec == (char *)-1)
  1065.       bad_word_spec:
  1066.       {
  1067.         word_spec_error++;
  1068.         goto event_not_found;
  1069.       }
  1070.  
  1071.       /* If no word specifier, than the thing of interest was the event. */
  1072.       if (!word_spec)
  1073.         temp = event;
  1074.       else
  1075.         {
  1076.           temp = (char *)alloca (1 + strlen (word_spec));
  1077.           strcpy (temp, word_spec);
  1078.           free (word_spec);
  1079.         }
  1080.  
  1081.       /* Perhaps there are other modifiers involved.  Do what they say. */
  1082.  
  1083.     hack_specials:
  1084.  
  1085.       if (string[i] == ':')
  1086.         {
  1087.           char *tstr;
  1088.  
  1089.           switch (string[i + 1])
  1090.         {
  1091.           /* :p means make this the last executed line.  So we
  1092.              return an error state after adding this line to the
  1093.              history. */
  1094.         case 'p':
  1095.           only_printing++;
  1096.           goto next_special;
  1097.  
  1098.           /* :t discards all but the last part of the pathname. */
  1099.         case 't':
  1100.           tstr = rindex (temp, '/');
  1101.           if (tstr)
  1102.             temp = ++tstr;
  1103.           goto next_special;
  1104.  
  1105.           /* :h discards the last part of a pathname. */
  1106.         case 'h':
  1107.           tstr = rindex (temp, '/');
  1108.           if (tstr)
  1109.             *tstr = '\0';
  1110.           goto next_special;
  1111.  
  1112.           /* :r discards the suffix. */
  1113.         case 'r':
  1114.           tstr = rindex (temp, '.');
  1115.           if (tstr)
  1116.             *tstr = '\0';
  1117.           goto next_special;
  1118.  
  1119.           /* :e discards everything but the suffix. */
  1120.         case 'e':
  1121.           tstr = rindex (temp, '.');
  1122.           if (tstr)
  1123.             temp = tstr;
  1124.           goto next_special;
  1125.  
  1126.           /* :s/this/that substitutes `this' for `that'. */
  1127.           /* :gs/this/that substitutes `this' for `that' globally. */
  1128.         case 'g':
  1129.           if (string[i + 2] == 's')
  1130.             {
  1131.               i++;
  1132.               substitute_globally = 1;
  1133.               goto substitute;
  1134.             }
  1135.           else
  1136.             
  1137.         case 's':
  1138.           substitute:
  1139.           {
  1140.             char *this, *that, *new_event;
  1141.             int delimiter = 0;
  1142.             int si, l_this, l_that, l_temp = strlen (temp);
  1143.  
  1144.             if (i + 2 < strlen (string))
  1145.               delimiter = string[i + 2];
  1146.  
  1147.             if (!delimiter)
  1148.               break;
  1149.  
  1150.             i += 3;
  1151.  
  1152.             /* Get THIS. */
  1153.             for (si = i; string[si] && string[si] != delimiter; si++);
  1154.             l_this = (si - i);
  1155.             this = (char *)alloca (1 + l_this);
  1156.             strncpy (this, string + i, l_this);
  1157.             this[l_this] = '\0';
  1158.  
  1159.             i = si;
  1160.             if (string[si])
  1161.               i++;
  1162.  
  1163.             /* Get THAT. */
  1164.             for (si = i; string[si] && string[si] != delimiter; si++);
  1165.             l_that = (si - i);
  1166.             that = (char *)alloca (1 + l_that);
  1167.             strncpy (that, string + i, l_that);
  1168.             that[l_that] = '\0';
  1169.  
  1170.             i = si;
  1171.             if (string[si]) i++;
  1172.  
  1173.             /* Ignore impossible cases. */
  1174.             if (l_this > l_temp)
  1175.               goto cant_substitute;
  1176.  
  1177.             /* Find the first occurrence of THIS in TEMP. */
  1178.             si = 0;
  1179.             for (; (si + l_this) <= l_temp; si++)
  1180.               if (strncmp (temp + si, this, l_this) == 0)
  1181.             {
  1182.               new_event =
  1183.                 (char *)alloca (1 + (l_that - l_this) + l_temp);
  1184.               strncpy (new_event, temp, si);
  1185.               strncpy (new_event + si, that, l_that);
  1186.               strncpy (new_event + si + l_that,
  1187.                    temp + si + l_this,
  1188.                    l_temp - (si + l_this));
  1189.               new_event[(l_that - l_this) + l_temp] = '\0';
  1190.               temp = new_event;
  1191.  
  1192.               if (substitute_globally)
  1193.                 {
  1194.                   si += l_that;
  1195.                   l_temp = strlen (temp);
  1196.                   substitute_globally++;
  1197.                   continue;
  1198.                 }
  1199.  
  1200.               goto hack_specials;
  1201.             }
  1202.  
  1203.           cant_substitute:
  1204.  
  1205.             if (substitute_globally > 1)
  1206.               {
  1207.             substitute_globally = 0;
  1208.             goto hack_specials;
  1209.               }
  1210.  
  1211.             goto event_not_found;
  1212.           }
  1213.  
  1214.           /* :# is the line so far.  Note that we have to
  1215.              alloca () it since RESULT could be realloc ()'ed
  1216.              below in add_string. */
  1217.         case '#':
  1218.         hack_pound_sign:
  1219.           if (result)
  1220.             {
  1221.               temp = (char *)alloca (1 + strlen (result));
  1222.               strcpy (temp, result);
  1223.             }
  1224.           else
  1225.             temp = "";
  1226.  
  1227.         next_special:
  1228.           i += 2;
  1229.           goto hack_specials;
  1230.         }
  1231.  
  1232.         }
  1233.       /* Believe it or not, we have to back the pointer up by one. */
  1234.       --i;
  1235.       goto add_string;
  1236.  
  1237.       /* A regular character.  Just add it to the output string. */
  1238.     default:
  1239.     add_char:
  1240.       tt[0] = string[i];
  1241.       temp = tt;
  1242.       goto do_add;
  1243.  
  1244.     add_string:
  1245.       modified++;
  1246.  
  1247.     do_add:
  1248.       j += strlen (temp);
  1249.       while (j > len)
  1250.         result = (char *)xrealloc (result, (len += 255));
  1251.  
  1252.       strcpy (result + (j - strlen (temp)), temp);
  1253.     }
  1254.     }
  1255.  
  1256.   *output = result;
  1257.  
  1258.   if (only_printing)
  1259.     {
  1260.       add_history (result);
  1261.       return (-1);
  1262.     }
  1263.  
  1264.   return (modified != 0);
  1265. }
  1266.  
  1267. /* Return a consed string which is the word specified in SPEC, and found
  1268.    in FROM.  NULL is returned if there is no spec.  -1 is returned if
  1269.    the word specified cannot be found.  CALLER_INDEX is the offset in
  1270.    SPEC to start looking; it is updated to point to just after the last
  1271.    character parsed. */
  1272. char *
  1273. get_history_word_specifier (spec, from, caller_index)
  1274.      char *spec, *from;
  1275.      int *caller_index;
  1276. {
  1277.   register int i = *caller_index;
  1278.   int first, last;
  1279.   int expecting_word_spec = 0;
  1280.   char *history_arg_extract ();
  1281.  
  1282.   /* The range of words to return doesn't exist yet. */
  1283.   first = last = 0;
  1284.  
  1285.   /* If we found a colon, then this *must* be a word specification.  If
  1286.      it isn't, then it is an error. */
  1287.   if (spec[i] == ':')
  1288.     i++, expecting_word_spec++;
  1289.  
  1290.   /* Handle special cases first. */
  1291.  
  1292.   /* `%' is the word last searched for. */
  1293.   if (spec[i] == '%')
  1294.     {
  1295.       *caller_index = i + 1;
  1296.       if (search_string)
  1297.     return (savestring (search_string));
  1298.       else
  1299.     return (savestring (""));
  1300.     }
  1301.  
  1302.   /* `*' matches all of the arguments, but not the command. */
  1303.   if (spec[i] == '*')
  1304.     {
  1305.       char *star_result;
  1306.  
  1307.       *caller_index = i + 1;
  1308.       star_result = history_arg_extract (1, '$', from);
  1309.  
  1310.       if (!star_result)
  1311.     star_result = savestring ("");
  1312.  
  1313.       return (star_result);
  1314.     }
  1315.  
  1316.   /* `$' is last arg. */
  1317.   if (spec[i] == '$')
  1318.     {
  1319.       *caller_index = i + 1;
  1320.       return (history_arg_extract ('$', '$', from));
  1321.     }
  1322.  
  1323.   /* Try to get FIRST and LAST figured out. */
  1324.   if (spec[i] == '-' || spec[i] == '^')
  1325.     {
  1326.       first = 1;
  1327.       goto get_last;
  1328.     }
  1329.  
  1330.  get_first:
  1331.   if (digit (spec[i]) && expecting_word_spec)
  1332.     {
  1333.       sscanf (spec + i, "%d", &first);
  1334.       for (; digit (spec[i]); i++);
  1335.     }
  1336.   else
  1337.     return ((char *)NULL);
  1338.  
  1339.  get_last:
  1340.   if (spec[i] == '^')
  1341.     {
  1342.       i++;
  1343.       last = 1;
  1344.       goto get_args;
  1345.     }
  1346.  
  1347.   if (spec[i] != '-')
  1348.     {
  1349.       last = first;
  1350.       goto get_args;
  1351.     }
  1352.  
  1353.   i++;
  1354.  
  1355.   if (digit (spec[i]))
  1356.     {
  1357.       sscanf (spec + i, "%d", &last);
  1358.       for (; digit (spec[i]); i++);
  1359.     }
  1360.   else
  1361.     if (spec[i] == '$')
  1362.       {
  1363.     i++;
  1364.     last = '$';
  1365.       }
  1366.  
  1367.  get_args:
  1368.   {
  1369.     char *result = (char *)NULL;
  1370.  
  1371.     *caller_index = i;
  1372.  
  1373.     if (last >= first)
  1374.       result = history_arg_extract (first, last, from);
  1375.  
  1376.     if (result)
  1377.       return (result);
  1378.     else
  1379.       return ((char *)-1);
  1380.   }
  1381. }
  1382.  
  1383. /* Extract the args specified, starting at FIRST, and ending at LAST.
  1384.    The args are taken from STRING.  If either FIRST or LAST is < 0,
  1385.    then make that arg count from the right (subtract from the number of
  1386.    tokens, so that FIRST = -1 means the next to last token on the line). */
  1387. char *
  1388. history_arg_extract (first, last, string)
  1389.      int first, last;
  1390.      char *string;
  1391. {
  1392.   register int i, len;
  1393.   char *result = (char *)NULL;
  1394.   int size = 0, offset = 0;
  1395.  
  1396.   char **history_tokenize (), **list;
  1397.  
  1398.   if (!(list = history_tokenize (string)))
  1399.     return ((char *)NULL);
  1400.  
  1401.   for (len = 0; list[len]; len++);
  1402.  
  1403.   if (last < 0)
  1404.     last = len + last - 1;
  1405.  
  1406.   if (first < 0)
  1407.     first = len + first - 1;
  1408.  
  1409.   if (last == '$')
  1410.     last = len - 1;
  1411.  
  1412.   if (first == '$')
  1413.     first = len - 1;
  1414.  
  1415.   last++;
  1416.  
  1417.   if (first > len || last > len || first < 0 || last < 0)
  1418.     result = ((char *)NULL);
  1419.   else
  1420.     {
  1421.       for (i = first; i < last; i++)
  1422.     {
  1423.       int l = strlen (list[i]);
  1424.  
  1425.       if (!result)
  1426.         result = (char *)xmalloc ((size = (2 + l)));
  1427.       else
  1428.         result = (char *)xrealloc (result, (size += (2 + l)));
  1429.       strcpy (result + offset, list[i]);
  1430.       offset += l;
  1431.       if (i + 1 < last)
  1432.         {
  1433.           strcpy (result + offset, " ");
  1434.           offset++;
  1435.         }
  1436.     }
  1437.     }
  1438.  
  1439.   for (i = 0; i < len; i++)
  1440.     free (list[i]);
  1441.  
  1442.   free (list);
  1443.  
  1444.   return (result);
  1445. }
  1446.  
  1447. #define slashify_in_quotes "\\`\"$"
  1448.  
  1449. /* Return an array of tokens, much as the shell might.  The tokens are
  1450.    parsed out of STRING. */
  1451. char **
  1452. history_tokenize (string)
  1453.      char *string;
  1454. {
  1455.   char **result = (char **)NULL;
  1456.   register int i, start, result_index, size;
  1457.   int len;
  1458.  
  1459.   i = result_index = size = 0;
  1460.  
  1461.   /* Get a token, and stuff it into RESULT.  The tokens are split
  1462.      exactly where the shell would split them. */
  1463.  get_token:
  1464.  
  1465.   /* Skip leading whitespace. */
  1466.   for (; string[i] && whitespace(string[i]); i++);
  1467.  
  1468.   start = i;
  1469.  
  1470.   if (!string[i] || string[i] == history_comment_char)
  1471.     return (result);
  1472.  
  1473.   if (member (string[i], "()\n")) {
  1474.     i++;
  1475.     goto got_token;
  1476.   }
  1477.  
  1478.   if (member (string[i], "<>;&|")) {
  1479.     int peek = string[i + 1];
  1480.  
  1481.     if (peek == string[i]) {
  1482.       if (peek ==  '<') {
  1483.     if (string[1 + 2] == '-')
  1484.       i++;
  1485.     i += 2;
  1486.     goto got_token;
  1487.       }
  1488.  
  1489.       if (member (peek, ">:&|")) {
  1490.     i += 2;
  1491.     goto got_token;
  1492.       }
  1493.     } else {
  1494.       if ((peek == '&' &&
  1495.       (string[i] == '>' || string[i] == '<')) ||
  1496.       ((peek == '>') &&
  1497.       (string[i] == '&'))) {
  1498.     i += 2;
  1499.     goto got_token;
  1500.       }
  1501.     }
  1502.     i++;
  1503.     goto got_token;
  1504.   }
  1505.  
  1506.   /* Get word from string + i; */
  1507.   {
  1508.     int delimiter = 0;
  1509.  
  1510.     if (member (string[i], "\"'`"))
  1511.       delimiter = string[i++];
  1512.  
  1513.     for (;string[i]; i++) {
  1514.  
  1515.       if (string[i] == '\\') {
  1516.  
  1517.     if (string[i + 1] == '\n') {
  1518.       i++;
  1519.       continue;
  1520.     } else {
  1521.       if (delimiter != '\'')
  1522.         if ((delimiter != '"') ||
  1523.         (member (string[i], slashify_in_quotes))) {
  1524.           i++;
  1525.           continue;
  1526.         }
  1527.     }
  1528.       }
  1529.  
  1530.       if (delimiter && string[i] == delimiter) {
  1531.     delimiter = 0;
  1532.     continue;
  1533.       }
  1534.  
  1535.       if (!delimiter && (member (string[i], " \t\n;&()|<>")))
  1536.     goto got_token;
  1537.  
  1538.       if (!delimiter && member (string[i], "\"'`")) {
  1539.     delimiter = string[i];
  1540.     continue;
  1541.       }
  1542.     }
  1543.     got_token:
  1544.  
  1545.     len = i - start;
  1546.     if (result_index + 2 >= size) {
  1547.       if (!size)
  1548.     result = (char **)xmalloc ((size = 10) * (sizeof (char *)));
  1549.       else
  1550.     result =
  1551.       (char **)xrealloc (result, ((size += 10) * (sizeof (char *))));
  1552.     }
  1553.     result[result_index] = (char *)xmalloc (1 + len);
  1554.     strncpy (result[result_index], string + start, len);
  1555.     result[result_index][len] = '\0';
  1556.     result_index++;
  1557.     result[result_index] = (char *)NULL;
  1558.   }
  1559.   if (string[i])
  1560.     goto get_token;
  1561.  
  1562.   return (result);
  1563. }
  1564.  
  1565. #if defined (STATIC_MALLOC)
  1566.  
  1567. /* **************************************************************** */
  1568. /*                                    */
  1569. /*            xmalloc and xrealloc ()                     */
  1570. /*                                    */
  1571. /* **************************************************************** */
  1572.  
  1573. static void memory_error_and_abort ();
  1574.  
  1575. static char *
  1576. xmalloc (bytes)
  1577.      int bytes;
  1578. {
  1579.   char *temp = (char *)malloc (bytes);
  1580.  
  1581.   if (!temp)
  1582.     memory_error_and_abort ();
  1583.   return (temp);
  1584. }
  1585.  
  1586. static char *
  1587. xrealloc (pointer, bytes)
  1588.      char *pointer;
  1589.      int bytes;
  1590. {
  1591.   char *temp;
  1592.  
  1593.   if (!pointer)
  1594.     temp = (char *)xmalloc (bytes);
  1595.   else
  1596.     temp = (char *)realloc (pointer, bytes);
  1597.  
  1598.   if (!temp)
  1599.     memory_error_and_abort ();
  1600.  
  1601.   return (temp);
  1602. }
  1603.  
  1604. static void
  1605. memory_error_and_abort ()
  1606. {
  1607.   fprintf (stderr, "history: Out of virtual memory!\n");
  1608.   abort ();
  1609. }
  1610. #endif /* STATIC_MALLOC */
  1611.  
  1612.  
  1613. /* **************************************************************** */
  1614. /*                                    */
  1615. /*                Test Code                */
  1616. /*                                    */
  1617. /* **************************************************************** */
  1618. #ifdef TEST
  1619. main ()
  1620. {
  1621.   char line[1024], *t;
  1622.   int done = 0;
  1623.  
  1624.   line[0] = 0;
  1625.  
  1626.   while (!done)
  1627.     {
  1628.       fprintf (stdout, "history%% ");
  1629.       t = gets (line);
  1630.  
  1631.       if (!t)
  1632.     strcpy (line, "quit");
  1633.  
  1634.       if (line[0])
  1635.     {
  1636.       char *expansion;
  1637.       int result;
  1638.  
  1639.       using_history ();
  1640.  
  1641.       result = history_expand (line, &expansion);
  1642.       strcpy (line, expansion);
  1643.       free (expansion);
  1644.       if (result)
  1645.         fprintf (stderr, "%s\n", line);
  1646.  
  1647.       if (result < 0)
  1648.         continue;
  1649.  
  1650.       add_history (line);
  1651.     }
  1652.  
  1653.       if (strcmp (line, "quit") == 0) done = 1;
  1654.       if (strcmp (line, "save") == 0) write_history (0);
  1655.       if (strcmp (line, "read") == 0) read_history (0);
  1656.       if (strcmp (line, "list") == 0)
  1657.     {
  1658.       register HIST_ENTRY **the_list = history_list ();
  1659.       register int i;
  1660.  
  1661.       if (the_list)
  1662.         for (i = 0; the_list[i]; i++)
  1663.           fprintf (stdout, "%d: %s\n", i + history_base, the_list[i]->line);
  1664.     }
  1665.       if (strncmp (line, "delete", strlen ("delete")) == 0)
  1666.     {
  1667.       int which;
  1668.       if ((sscanf (line + strlen ("delete"), "%d", &which)) == 1)
  1669.         {
  1670.           HIST_ENTRY *entry = remove_history (which);
  1671.           if (!entry)
  1672.         fprintf (stderr, "No such entry %d\n", which);
  1673.           else
  1674.         {
  1675.           free (entry->line);
  1676.           free (entry);
  1677.         }
  1678.         }
  1679.       else
  1680.         {
  1681.           fprintf (stderr, "non-numeric arg given to `delete'\n");
  1682.         }
  1683.     }
  1684.     }
  1685. }
  1686.  
  1687. #endif                /* TEST */
  1688.  
  1689. /*
  1690. * Local variables:
  1691. * compile-command: "gcc -g -DTEST -o history history.c"
  1692. * end:
  1693. */
  1694.