history.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:10k
源码类别:

MySQL数据库

开发平台:

Visual C++

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