diff options
Diffstat (limited to 'readline/history.c')
-rw-r--r-- | readline/history.c | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/readline/history.c b/readline/history.c index d7894cf..129c57a 100644 --- a/readline/history.c +++ b/readline/history.c @@ -1,6 +1,6 @@ /* history.c -- standalone history library */ -/* Copyright (C) 1989-2009 Free Software Foundation, Inc. +/* Copyright (C) 1989-2015 Free Software Foundation, Inc. This file contains the GNU History Library (History), a set of routines for managing the text of previously typed lines. @@ -43,11 +43,22 @@ # include <unistd.h> #endif +#include <errno.h> + #include "history.h" #include "histlib.h" #include "xmalloc.h" +#if !defined (errno) +extern int errno; +#endif + +/* How big to make the_history when we first allocate it. */ +#define DEFAULT_HISTORY_INITIAL_SIZE 502 + +#define MAX_HISTORY_INITIAL_SIZE 8192 + /* The number of slots to increase the_history by. */ #define DEFAULT_HISTORY_GROW_SIZE 50 @@ -236,7 +247,10 @@ 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 */ + errno = 0; + t = (time_t) strtol (ts + 1, (char **)NULL, 10); /* XXX - should use strtol() here */ + if (errno == ERANGE) + return (time_t)0; return t; } @@ -265,6 +279,7 @@ add_history (string) const char *string; { HIST_ENTRY *temp; + int new_length; if (history_stifled && (history_length == history_max_entries)) { @@ -279,19 +294,25 @@ add_history (string) if (the_history[0]) (void) free_history_entry (the_history[0]); - /* Copy the rest of the entries, moving down one slot. */ - for (i = 0; i < history_length; i++) - the_history[i] = the_history[i + 1]; + /* Copy the rest of the entries, moving down one slot. Copy includes + trailing NULL. */ + memmove (the_history, the_history + 1, history_length * sizeof (HIST_ENTRY *)); + new_length = history_length; history_base++; } else { if (history_size == 0) { - history_size = DEFAULT_HISTORY_GROW_SIZE; + if (history_stifled && history_max_entries > 0) + history_size = (history_max_entries > MAX_HISTORY_INITIAL_SIZE) + ? MAX_HISTORY_INITIAL_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; + new_length = 1; } else { @@ -301,14 +322,15 @@ add_history (string) the_history = (HIST_ENTRY **) xrealloc (the_history, history_size * sizeof (HIST_ENTRY *)); } - history_length++; + new_length = history_length + 1; } } - temp = alloc_history_entry (string, hist_inittime ()); + temp = alloc_history_entry ((char *)string, hist_inittime ()); - the_history[history_length] = (HIST_ENTRY *)NULL; - the_history[history_length - 1] = temp; + the_history[new_length] = (HIST_ENTRY *)NULL; + the_history[new_length - 1] = temp; + history_length = new_length; } /* Change the time stamp of the most recent history entry to STRING. */ @@ -318,7 +340,7 @@ add_history_time (string) { HIST_ENTRY *hs; - if (string == 0) + if (string == 0 || history_length < 1) return; hs = the_history[history_length - 1]; FREE (hs->timestamp); @@ -387,6 +409,30 @@ replace_history_entry (which, line, data) return (old_value); } +/* Append LINE to the history line at offset WHICH, adding a newline to the + end of the current line first. This can be used to construct multi-line + history entries while reading lines from the history file. */ +void +_hs_append_history_line (which, line) + int which; + const char *line; +{ + HIST_ENTRY *hent; + size_t newlen, curlen; + char *newline; + + hent = the_history[which]; + curlen = strlen (hent->line); + newlen = curlen + strlen (line) + 2; + newline = realloc (hent->line, newlen); + if (newline) + { + hent->line = newline; + hent->line[curlen++] = '\n'; + strcpy (hent->line + curlen, line); + } +} + /* 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 @@ -394,7 +440,7 @@ replace_history_entry (which, line, data) WHICH >= 0 means to replace that particular history entry's data, as long as it matches OLD. */ void -replace_history_data (which,old, new) +_hs_replace_history_data (which, old, new) int which; histdata_t *old, *new; { |