home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / readline / history.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  39.7 KB  |  1,707 lines

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