Sync readline/ to version 7.0 alpha
[deliverable/binutils-gdb.git] / readline / history.c
index a538f91c0d7b387c088705e785e85c36de62f37e..14696934b843f9d9f7a2be6b6dc01b7c8ea1e1e7 100644 (file)
@@ -1,24 +1,23 @@
 /* history.c -- standalone history library */
 
-/* Copyright (C) 1989-2005 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2011 Free Software Foundation, Inc.
 
-   This file contains the GNU History Library (the Library), a set of
+   This file contains the GNU History Library (History), a set of
    routines for managing the text of previously typed lines.
 
-   The Library is free software; you can redistribute it and/or modify
+   History is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   The Library is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   History is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-   The GNU General Public License is often shipped with GNU software, and
-   is generally kept in a file called COPYING or LICENSE.  If you do not
-   have a copy of the license, write to the Free Software Foundation,
-   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with History.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 /* The goal is to make the implementation transparent, so that you
    don't have to know what data types are used, just what functions
@@ -49,6 +48,9 @@
 
 #include "xmalloc.h"
 
+/* How big to make the_history when we first allocate it. */
+#define DEFAULT_HISTORY_INITIAL_SIZE   502
+
 /* The number of slots to increase the_history by. */
 #define DEFAULT_HISTORY_GROW_SIZE 50
 
@@ -158,7 +160,7 @@ history_set_pos (pos)
   return (1);
 }
  
-/* Return the current history array.  The caller has to be carefull, since this
+/* Return the current history array.  The caller has to be careful, since this
    is the actual array of data, and could be bashed or made corrupt easily.
    The array is terminated with a NULL pointer. */
 HIST_ENTRY **
@@ -209,6 +211,22 @@ history_get (offset)
                : the_history[local_index];
 }
 
+HIST_ENTRY *
+alloc_history_entry (string, ts)
+     char *string;
+     char *ts;
+{
+  HIST_ENTRY *temp;
+
+  temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
+
+  temp->line = string ? savestring (string) : string;
+  temp->data = (char *)NULL;
+  temp->timestamp = ts;
+
+  return temp;
+}
+
 time_t
 history_get_time (hist)
      HIST_ENTRY *hist;
@@ -221,7 +239,7 @@ history_get_time (hist)
   ts = hist->timestamp;
   if (ts[0] != history_comment_char)
     return 0;
-  t = (time_t) atol (ts + 1);          /* XXX - should use strtol() here */
+  t = (time_t) strtol (ts + 1, (char **)NULL, 10);             /* XXX - should use strtol() here */
   return t;
 }
 
@@ -264,9 +282,14 @@ add_history (string)
       if (the_history[0])
        (void) free_history_entry (the_history[0]);
 
-      /* Copy the rest of the entries, moving down one slot. */
+      /* Copy the rest of the entries, moving down one slot.  Copy includes
+        trailing NULL.  */
+#if 0
       for (i = 0; i < history_length; i++)
        the_history[i] = the_history[i + 1];
+#else
+      memmove (the_history, the_history + 1, history_length * sizeof (HIST_ENTRY *));
+#endif
 
       history_base++;
     }
@@ -274,7 +297,10 @@ add_history (string)
     {
       if (history_size == 0)
        {
-         history_size = DEFAULT_HISTORY_GROW_SIZE;
+         if (history_stifled && history_max_entries > 0)
+           history_size = history_max_entries + 2;
+         else
+           history_size = DEFAULT_HISTORY_INITIAL_SIZE;
          the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
          history_length = 1;
        }
@@ -290,11 +316,7 @@ add_history (string)
        }
     }
 
-  temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
-  temp->line = savestring (string);
-  temp->data = (char *)NULL;
-
-  temp->timestamp = hist_inittime ();
+  temp = alloc_history_entry (string, hist_inittime ());
 
   the_history[history_length] = (HIST_ENTRY *)NULL;
   the_history[history_length - 1] = temp;
@@ -307,6 +329,8 @@ add_history_time (string)
 {
   HIST_ENTRY *hs;
 
+  if (string == 0 || history_length < 1)
+    return;
   hs = the_history[history_length - 1];
   FREE (hs->timestamp);
   hs->timestamp = savestring (string);
@@ -325,9 +349,29 @@ free_history_entry (hist)
   FREE (hist->line);
   FREE (hist->timestamp);
   x = hist->data;
-  free (hist);
+  xfree (hist);
   return (x);
 }
+
+HIST_ENTRY *
+copy_history_entry (hist)
+     HIST_ENTRY *hist;
+{
+  HIST_ENTRY *ret;
+  char *ts;
+
+  if (hist == 0)
+    return hist;
+
+  ret = alloc_history_entry (hist->line, (char *)NULL);
+
+  ts = hist->timestamp ? savestring (hist->timestamp) : hist->timestamp;
+  ret->timestamp = ts;
+
+  ret->data = hist->data;
+
+  return ret;
+}
   
 /* Make the history entry at WHICH have LINE and DATA.  This returns
    the old entry so you can dispose of the data.  In the case of an
@@ -354,6 +398,51 @@ replace_history_entry (which, line, data)
   return (old_value);
 }
 
+/* Replace the DATA in the specified history entries, replacing OLD with
+   NEW.  WHICH says which one(s) to replace:  WHICH == -1 means to replace
+   all of the history entries where entry->data == OLD; WHICH == -2 means
+   to replace the `newest' history entry where entry->data == OLD; and
+   WHICH >= 0 means to replace that particular history entry's data, as
+   long as it matches OLD. */
+void
+replace_history_data (which, old, new)
+     int which;
+     histdata_t *old, *new;
+{
+  HIST_ENTRY *entry;
+  register int i, last;
+
+  if (which < -2 || which >= history_length || history_length == 0 || the_history == 0)
+    return;
+
+  if (which >= 0)
+    {
+      entry = the_history[which];
+      if (entry && entry->data == old)
+       entry->data = new;
+      return;
+    }
+
+  last = -1;
+  for (i = 0; i < history_length; i++)
+    {
+      entry = the_history[i];
+      if (entry == 0)
+       continue;
+      if (entry->data == old)
+       {
+         last = i;
+         if (which == -1)
+           entry->data = new;
+       }
+    }
+  if (which == -2 && last >= 0)
+    {
+      entry = the_history[last];
+      entry->data = new;       /* XXX - we don't check entry->old */
+    }
+}      
+  
 /* Remove history element WHICH from the history.  The removed
    element is returned to you so you can free the line, data,
    and containing structure. */
@@ -406,7 +495,7 @@ stifle_history (max)
 
 /* Stop stifling the history.  This returns the previous maximum
    number of history entries.  The value is positive if the history
-   was stifled,  negative if it wasn't. */
+   was stifled, negative if it wasn't. */
 int
 unstifle_history ()
 {
This page took 0.029458 seconds and 4 git commands to generate.