home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / readline / history.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  10KB  |  391 lines

  1. /* Modified by Klaus Gebhardt, October 1996 */
  2. /* History.c -- standalone history library */
  3.  
  4. /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
  5.  
  6.    This file contains the GNU History Library (the Library), a set of
  7.    routines for managing the text of previously typed lines.
  8.  
  9.    The Library is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 1, or (at your option)
  12.    any later version.
  13.  
  14.    The Library is distributed in the hope that it will be useful, but
  15.    WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.    General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23.  
  24. /* 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. #define READLINE_LIBRARY
  28.  
  29. #if defined (HAVE_CONFIG_H)
  30. #  include <config.h>
  31. #endif
  32.  
  33. #include <stdio.h>
  34.  
  35. #if defined (HAVE_STDLIB_H)
  36. #  include <stdlib.h>
  37. #else
  38. #  include "ansi_stdlib.h"
  39. #endif /* HAVE_STDLIB_H */
  40.  
  41. #if defined (HAVE_UNISTD_H)
  42. #  include <unistd.h>
  43. #endif
  44.  
  45. #if defined (HAVE_STRING_H)
  46. #  include <string.h>
  47. #else
  48. #  include <strings.h>
  49. #endif /* !HAVE_STRING_H */
  50.  
  51. #include "history.h"
  52. #include "histlib.h"
  53.  
  54. extern char *xmalloc (), *xrealloc ();
  55.  
  56. /* The number of slots to increase the_history by. */
  57. #define DEFAULT_HISTORY_GROW_SIZE 50
  58.  
  59. /* **************************************************************** */
  60. /*                                    */
  61. /*            History Functions                */
  62. /*                                    */
  63. /* **************************************************************** */
  64.  
  65. /* An array of HIST_ENTRY.  This is where we store the history. */
  66. static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
  67.  
  68. /* Non-zero means that we have enforced a limit on the amount of
  69.    history that we save. */
  70. #if defined (__EMX__) && defined (OS2)
  71. int history_stifled = 0;
  72. #else
  73. static int history_stifled;
  74. #endif
  75.  
  76. /* If HISTORY_STIFLED is non-zero, then this is the maximum number of
  77.    entries to remember. */
  78. int max_input_history;
  79.  
  80. /* The current location of the interactive history pointer.  Just makes
  81.    life easier for outside callers. */
  82. int history_offset;
  83.  
  84. /* The number of strings currently stored in the history list. */
  85. int history_length;
  86.  
  87. /* The current number of slots allocated to the input_history. */
  88. static int history_size;
  89.  
  90. /* The logical `base' of the history array.  It defaults to 1. */
  91. int history_base = 1;
  92.  
  93. /* Return the current HISTORY_STATE of the history. */
  94. HISTORY_STATE *
  95. history_get_history_state ()
  96. {
  97.   HISTORY_STATE *state;
  98.  
  99.   state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
  100.   state->entries = the_history;
  101.   state->offset = history_offset;
  102.   state->length = history_length;
  103.   state->size = history_size;
  104.   state->flags = 0;
  105.   if (history_stifled)
  106.     state->flags |= HS_STIFLED;
  107.  
  108.   return (state);
  109. }
  110.  
  111. /* Set the state of the current history array to STATE. */
  112. void
  113. history_set_history_state (state)
  114.      HISTORY_STATE *state;
  115. {
  116.   the_history = state->entries;
  117.   history_offset = state->offset;
  118.   history_length = state->length;
  119.   history_size = state->size;
  120.   if (state->flags & HS_STIFLED)
  121.     history_stifled = 1;
  122. }
  123.  
  124. /* Begin a session in which the history functions might be used.  This
  125.    initializes interactive variables. */
  126. void
  127. using_history ()
  128. {
  129.   history_offset = history_length;
  130. }
  131.  
  132. /* Return the number of bytes that the primary history entries are using.
  133.    This just adds up the lengths of the_history->lines. */
  134. int
  135. history_total_bytes ()
  136. {
  137.   register int i, result;
  138.  
  139.   result = 0;
  140.  
  141.   for (i = 0; the_history && the_history[i]; i++)
  142.     result += strlen (the_history[i]->line);
  143.  
  144.   return (result);
  145. }
  146.  
  147. /* Returns the magic number which says what history element we are
  148.    looking at now.  In this implementation, it returns history_offset. */
  149. int
  150. where_history ()
  151. {
  152.   return (history_offset);
  153. }
  154.  
  155. /* Make the current history item be the one at POS, an absolute index.
  156.    Returns zero if POS is out of range, else non-zero. */
  157. int
  158. history_set_pos (pos)
  159.      int pos;
  160. {
  161.   if (pos > history_length || pos < 0 || !the_history)
  162.     return (0);
  163.   history_offset = pos;
  164.   return (1);
  165. }
  166.  
  167. /* Return the current history array.  The caller has to be carefull, since this
  168.    is the actual array of data, and could be bashed or made corrupt easily.
  169.    The array is terminated with a NULL pointer. */
  170. HIST_ENTRY **
  171. history_list ()
  172. {
  173.   return (the_history);
  174. }
  175.  
  176. /* Return the history entry at the current position, as determined by
  177.    history_offset.  If there is no entry there, return a NULL pointer. */
  178. HIST_ENTRY *
  179. current_history ()
  180. {
  181.   return ((history_offset == history_length) || the_history == 0)
  182.         ? (HIST_ENTRY *)NULL
  183.         : the_history[history_offset];
  184. }
  185.  
  186. /* Back up history_offset to the previous history entry, and return
  187.    a pointer to that entry.  If there is no previous entry then return
  188.    a NULL pointer. */
  189. HIST_ENTRY *
  190. previous_history ()
  191. {
  192.   return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
  193. }
  194.  
  195. /* Move history_offset forward to the next history entry, and return
  196.    a pointer to that entry.  If there is no next entry then return a
  197.    NULL pointer. */
  198. HIST_ENTRY *
  199. next_history ()
  200. {
  201.   return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
  202. }
  203.  
  204. /* Return the history entry which is logically at OFFSET in the history array.
  205.    OFFSET is relative to history_base. */
  206. HIST_ENTRY *
  207. history_get (offset)
  208.      int offset;
  209. {
  210.   int local_index;
  211.  
  212.   local_index = offset - history_base;
  213.   return (local_index >= history_length || local_index < 0 || !the_history)
  214.         ? (HIST_ENTRY *)NULL
  215.         : the_history[local_index];
  216. }
  217.  
  218. /* Place STRING at the end of the history list.  The data field
  219.    is  set to NULL. */
  220. void
  221. add_history (string)
  222.      char *string;
  223. {
  224.   HIST_ENTRY *temp;
  225.  
  226.   if (history_stifled && (history_length == max_input_history))
  227.     {
  228.       register int i;
  229.  
  230.       /* If the history is stifled, and history_length is zero,
  231.      and it equals max_input_history, we don't save items. */
  232.       if (history_length == 0)
  233.     return;
  234.  
  235.       /* If there is something in the slot, then remove it. */
  236.       if (the_history[0])
  237.     {
  238.       free (the_history[0]->line);
  239.       free (the_history[0]);
  240.     }
  241.  
  242.       /* Copy the rest of the entries, moving down one slot. */
  243.       for (i = 0; i < history_length; i++)
  244.     the_history[i] = the_history[i + 1];
  245.  
  246.       history_base++;
  247.     }
  248.   else
  249.     {
  250.       if (history_size == 0)
  251.     {
  252.       history_size = DEFAULT_HISTORY_GROW_SIZE;
  253.       the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
  254.       history_length = 1;
  255.     }
  256.       else
  257.     {
  258.       if (history_length == (history_size - 1))
  259.         {
  260.           history_size += DEFAULT_HISTORY_GROW_SIZE;
  261.           the_history = (HIST_ENTRY **)
  262.         xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
  263.         }
  264.       history_length++;
  265.     }
  266.     }
  267.  
  268.   temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  269.   temp->line = savestring (string);
  270.   temp->data = (char *)NULL;
  271.  
  272.   the_history[history_length] = (HIST_ENTRY *)NULL;
  273.   the_history[history_length - 1] = temp;
  274. }
  275.  
  276. /* Make the history entry at WHICH have LINE and DATA.  This returns
  277.    the old entry so you can dispose of the data.  In the case of an
  278.    invalid WHICH, a NULL pointer is returned. */
  279. HIST_ENTRY *
  280. replace_history_entry (which, line, data)
  281.      int which;
  282.      char *line;
  283.      char *data;
  284. {
  285.   HIST_ENTRY *temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  286.   HIST_ENTRY *old_value;
  287.  
  288.   if (which >= history_length)
  289.     return ((HIST_ENTRY *)NULL);
  290.  
  291.   old_value = the_history[which];
  292.  
  293.   temp->line = savestring (line);
  294.   temp->data = data;
  295.   the_history[which] = temp;
  296.  
  297.   return (old_value);
  298. }
  299.  
  300. /* Remove history element WHICH from the history.  The removed
  301.    element is returned to you so you can free the line, data,
  302.    and containing structure. */
  303. HIST_ENTRY *
  304. remove_history (which)
  305.      int which;
  306. {
  307.   HIST_ENTRY *return_value;
  308.  
  309.   if (which >= history_length || !history_length)
  310.     return_value = (HIST_ENTRY *)NULL;
  311.   else
  312.     {
  313.       register int i;
  314.       return_value = the_history[which];
  315.  
  316.       for (i = which; i < history_length; i++)
  317.     the_history[i] = the_history[i + 1];
  318.  
  319.       history_length--;
  320.     }
  321.  
  322.   return (return_value);
  323. }
  324.  
  325. /* Stifle the history list, remembering only MAX number of lines. */
  326. void
  327. stifle_history (max)
  328.      int max;
  329. {
  330.   if (max < 0)
  331.     max = 0;
  332.  
  333.   if (history_length > max)
  334.     {
  335.       register int i, j;
  336.  
  337.       /* This loses because we cannot free the data. */
  338.       for (i = 0, j = history_length - max; i < j; i++)
  339.     {
  340.       free (the_history[i]->line);
  341.       free (the_history[i]);
  342.     }
  343.  
  344.       history_base = i;
  345.       for (j = 0, i = history_length - max; j < max; i++, j++)
  346.     the_history[j] = the_history[i];
  347.       the_history[j] = (HIST_ENTRY *)NULL;
  348.       history_length = j;
  349.     }
  350.  
  351.   history_stifled = 1;
  352.   max_input_history = max;
  353. }
  354.  
  355. /* Stop stifling the history.  This returns the previous amount the 
  356.    history was stifled by.  The value is positive if the history was
  357.    stifled,  negative if it wasn't. */
  358. int
  359. unstifle_history ()
  360. {
  361.   if (history_stifled)
  362.     {
  363.       history_stifled = 0;
  364.       return (-max_input_history);
  365.     }
  366.  
  367.   return (max_input_history);
  368. }
  369.  
  370. int
  371. history_is_stifled ()
  372. {
  373.   return (history_stifled);
  374. }
  375.  
  376. void
  377. clear_history ()
  378. {
  379.   register int i;
  380.  
  381.   /* This loses because we cannot free the data. */
  382.   for (i = 0; i < history_length; i++)
  383.     {
  384.       free (the_history[i]->line);
  385.       free (the_history[i]);
  386.       the_history[i] = (HIST_ENTRY *)NULL;
  387.     }
  388.  
  389.   history_offset = history_length = 0;
  390. }
  391.