diff options
Diffstat (limited to 'cmd-line-utils/libedit/history.c')
-rw-r--r-- | cmd-line-utils/libedit/history.c | 201 |
1 files changed, 145 insertions, 56 deletions
diff --git a/cmd-line-utils/libedit/history.c b/cmd-line-utils/libedit/history.c index 90d94e7fc18..53648203bf0 100644 --- a/cmd-line-utils/libedit/history.c +++ b/cmd-line-utils/libedit/history.c @@ -1,4 +1,4 @@ -/* $NetBSD: history.c,v 1.17 2001/03/20 00:08:31 christos Exp $ */ +/* $NetBSD: history.c,v 1.22 2003/01/21 18:40:24 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -36,18 +36,25 @@ * SUCH DAMAGE. */ -#include "compat.h" +#include "config.h" +#if !defined(lint) && !defined(SCCSID) +#if 0 +static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: history.c,v 1.22 2003/01/21 18:40:24 christos Exp $"); +#endif +#endif /* not lint && not SCCSID */ /* * hist.c: History access functions */ -#include "sys.h" - #include <string.h> #include <stdlib.h> #include <stdarg.h> #ifdef HAVE_VIS_H #include <vis.h> +#else +#include "np/vis.h" #endif #include <sys/stat.h> @@ -73,6 +80,7 @@ struct history { history_efun_t h_enter; /* Add an element */ history_efun_t h_add; /* Append to an element */ }; + #define HNEXT(h, ev) (*(h)->h_next)((h)->h_ref, ev) #define HFIRST(h, ev) (*(h)->h_first)((h)->h_ref, ev) #define HPREV(h, ev) (*(h)->h_prev)((h)->h_ref, ev) @@ -87,9 +95,17 @@ struct history { #define h_realloc(a, b) realloc((a), (b)) #define h_free(a) free(a) +typedef struct { + int num; + char *str; +} HistEventPrivate; + + private int history_setsize(History *, HistEvent *, int); private int history_getsize(History *, HistEvent *); +private int history_setunique(History *, HistEvent *, int); +private int history_getunique(History *, HistEvent *); private int history_set_fun(History *, History *); private int history_load(History *, const char *); private int history_save(History *, const char *); @@ -108,15 +124,17 @@ typedef struct hentry_t { HistEvent ev; /* What we return */ struct hentry_t *next; /* Next entry */ struct hentry_t *prev; /* Previous entry */ -} hentry_t; +} hentry_t; typedef struct history_t { - hentry_t list; /* Fake list header element */ - hentry_t *cursor; /* Current element in the list */ - int max; /* Maximum number of events */ - int cur; /* Current number of events */ + hentry_t list; /* Fake list header element */ + hentry_t *cursor; /* Current element in the list */ + int max; /* Maximum number of events */ + int cur; /* Current number of events */ int eventid; /* For generation of unique event id */ -} history_t; + int flags; /* History flags */ +#define H_UNIQUE 1 /* Store only unique elements */ +} history_t; private int history_def_first(ptr_t, HistEvent *); private int history_def_last(ptr_t, HistEvent *); @@ -126,13 +144,19 @@ private int history_def_curr(ptr_t, HistEvent *); private int history_def_set(ptr_t, HistEvent *, const int n); private int history_def_enter(ptr_t, HistEvent *, const char *); private int history_def_add(ptr_t, HistEvent *, const char *); -private void history_def_init(ptr_t *, HistEvent *, int); +private int history_def_init(ptr_t *, HistEvent *, int); private void history_def_clear(ptr_t, HistEvent *); private int history_def_insert(history_t *, HistEvent *, const char *); private void history_def_delete(history_t *, HistEvent *, hentry_t *); -#define history_def_setsize(p, num)(void) (((history_t *) p)->max = (num)) -#define history_def_getsize(p) (((history_t *) p)->cur) +#define history_def_setsize(p, num)(void) (((history_t *)p)->max = (num)) +#define history_def_getsize(p) (((history_t *)p)->cur) +#define history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0) +#define history_def_setunique(p, uni) \ + if (uni) \ + (((history_t *)p)->flags) |= H_UNIQUE; \ + else \ + (((history_t *)p)->flags) &= ~H_UNIQUE #define he_strerror(code) he_errlist[code] #define he_seterrev(evp, code) {\ @@ -326,20 +350,20 @@ history_def_add(ptr_t p, HistEvent *ev, const char *str) history_t *h = (history_t *) p; size_t len; char *s; + HistEventPrivate *evp = (void *)&h->cursor->ev; if (h->cursor == &h->list) return (history_def_enter(p, ev, str)); - len = strlen(h->cursor->ev.str) + strlen(str) + 1; + len = strlen(evp->str) + strlen(str) + 1; s = (char *) h_malloc(len); - if (!s) { + if (s == NULL) { he_seterrev(ev, _HE_MALLOC_FAILED); return (-1); } (void) strlcpy(s, h->cursor->ev.str, len); (void) strlcat(s, str, len); - /* LINTED const cast */ - h_free((ptr_t) h->cursor->ev.str); - h->cursor->ev.str = s; + h_free((ptr_t)evp->str); + evp->str = s; *ev = h->cursor->ev; return (0); } @@ -350,16 +374,14 @@ history_def_add(ptr_t p, HistEvent *ev, const char *str) */ /* ARGSUSED */ private void -history_def_delete(history_t *h, - HistEvent *ev __attribute__((unused)), hentry_t *hp) +history_def_delete(history_t *h, HistEvent *ev __attribute__((unused)), hentry_t *hp) { - + HistEventPrivate *evp = (void *)&hp->ev; if (hp == &h->list) abort(); hp->prev->next = hp->next; hp->next->prev = hp->prev; - /* LINTED const cast */ - h_free((ptr_t) hp->ev.str); + h_free((ptr_t) evp->str); h_free(hp); h->cur--; } @@ -373,11 +395,11 @@ history_def_insert(history_t *h, HistEvent *ev, const char *str) { h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t)); - if (h->cursor) - h->cursor->ev.str = strdup(str); - if (!h->cursor || !h->cursor->ev.str) { - he_seterrev(ev, _HE_MALLOC_FAILED); - return (-1); + if (h->cursor == NULL) + goto oomem; + if ((h->cursor->ev.str = strdup(str)) == NULL) { + h_free((ptr_t)h->cursor); + goto oomem; } h->cursor->ev.num = ++h->eventid; h->cursor->next = h->list.next; @@ -388,6 +410,9 @@ history_def_insert(history_t *h, HistEvent *ev, const char *str) *ev = h->cursor->ev; return (0); +oomem: + he_seterrev(ev, _HE_MALLOC_FAILED); + return (-1); } @@ -399,6 +424,10 @@ history_def_enter(ptr_t p, HistEvent *ev, const char *str) { history_t *h = (history_t *) p; + if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list && + strcmp(h->list.next->ev.str, str) == 0) + return (0); + if (history_def_insert(h, ev, str) == -1) return (-1); /* error, keep error message */ @@ -406,10 +435,10 @@ history_def_enter(ptr_t p, HistEvent *ev, const char *str) * Always keep at least one entry. * This way we don't have to check for the empty list. */ - while (h->cur - 1 > h->max) + while (h->cur > h->max && h->cur > 0) history_def_delete(h, ev, h->list.prev); - return (0); + return (1); } @@ -417,10 +446,12 @@ history_def_enter(ptr_t p, HistEvent *ev, const char *str) * Default history initialization function */ /* ARGSUSED */ -private void +private int history_def_init(ptr_t *p, HistEvent *ev __attribute__((unused)), int n) { history_t *h = (history_t *) h_malloc(sizeof(history_t)); + if (h == NULL) + return -1; if (n <= 0) n = 0; @@ -431,7 +462,9 @@ history_def_init(ptr_t *p, HistEvent *ev __attribute__((unused)), int n) h->list.ev.str = NULL; h->list.ev.num = 0; h->cursor = &h->list; + h->flags = 0; *p = (ptr_t) h; + return 0; } @@ -460,10 +493,15 @@ history_def_clear(ptr_t p, HistEvent *ev) public History * history_init(void) { - History *h = (History *) h_malloc(sizeof(History)); HistEvent ev; + History *h = (History *) h_malloc(sizeof(History)); + if (h == NULL) + return NULL; - history_def_init(&h->h_ref, &ev, 0); + if (history_def_init(&h->h_ref, &ev, 0) == -1) { + h_free((ptr_t)h); + return NULL; + } h->h_ent = -1; h->h_next = history_def_next; h->h_first = history_def_first; @@ -519,18 +557,46 @@ history_setsize(History *h, HistEvent *ev, int num) private int history_getsize(History *h, HistEvent *ev) { - int retval = 0; - if (h->h_next != history_def_next) { he_seterrev(ev, _HE_NOT_ALLOWED); return (-1); } - retval = history_def_getsize(h->h_ref); - if (retval < -1) { + ev->num = history_def_getsize(h->h_ref); + if (ev->num < -1) { he_seterrev(ev, _HE_SIZE_NEGATIVE); return (-1); } - ev->num = retval; + return (0); +} + + +/* history_setunique(): + * Set if adjacent equal events should not be entered in history. + */ +private int +history_setunique(History *h, HistEvent *ev, int uni) +{ + + if (h->h_next != history_def_next) { + he_seterrev(ev, _HE_NOT_ALLOWED); + return (-1); + } + history_def_setunique(h->h_ref, uni); + return (0); +} + + +/* history_getunique(): + * Get if adjacent equal events should not be entered in history. + */ +private int +history_getunique(History *h, HistEvent *ev) +{ + if (h->h_next != history_def_next) { + he_seterrev(ev, _HE_NOT_ALLOWED); + return (-1); + } + ev->num = history_def_getunique(h->h_ref); return (0); } @@ -595,13 +661,9 @@ history_load(History *h, const char *fname) if ((fp = fopen(fname, "r")) == NULL) return (i); - if ((line = fgetln(fp, &sz)) == NULL) - goto done; - - if (strncmp(line, hist_cookie, sz) != 0) - goto done; - ptr = h_malloc(max_size = 1024); + if (ptr == NULL) + goto done; for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) { char c = line[sz]; @@ -611,15 +673,24 @@ history_load(History *h, const char *fname) line[sz] = '\0'; if (max_size < sz) { + char *nptr; max_size = (sz + 1023) & ~1023; - ptr = h_realloc(ptr, max_size); + nptr = h_realloc(ptr, max_size); + if (nptr == NULL) { + i = -1; + goto oomem; + } + ptr = nptr; } (void) strunvis(ptr, line); line[sz] = c; - HENTER(h, &ev, ptr); + if (HENTER(h, &ev, ptr) == -1) { + h_free((ptr_t)ptr); + return -1; + } } - h_free(ptr); - +oomem: + h_free((ptr_t)ptr); done: (void) fclose(fp); return (i); @@ -634,28 +705,38 @@ history_save(History *h, const char *fname) { FILE *fp; HistEvent ev; - int i = 0, retval; + int i = -1, retval; size_t len, max_size; char *ptr; if ((fp = fopen(fname, "w")) == NULL) return (-1); - (void) fchmod(fileno(fp), S_IRUSR|S_IWUSR); - (void) fputs(hist_cookie, fp); + if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1) + goto done; ptr = h_malloc(max_size = 1024); - for (retval = HLAST(h, &ev); + if (ptr == NULL) + goto done; + for (i = 0, retval = HLAST(h, &ev); retval != -1; retval = HPREV(h, &ev), i++) { - len = strlen(ev.str) * 4; + len = strlen(ev.str) * 4 + 1; if (len >= max_size) { - max_size = (len + 1023) & 1023; - ptr = h_realloc(ptr, max_size); + char *nptr; + max_size = (len + 1023) & ~1023; + nptr = h_realloc(ptr, max_size); + if (nptr == NULL) { + i = -1; + goto oomem; + } + ptr = nptr; } (void) strvis(ptr, ev.str, VIS_WHITE); (void) fprintf(fp, "%s\n", ev.str); } - h_free(ptr); +oomem: + h_free((ptr_t)ptr); +done: (void) fclose(fp); return (i); } @@ -754,6 +835,14 @@ history(History *h, HistEvent *ev, int fun, ...) retval = history_setsize(h, ev, va_arg(va, int)); break; + case H_GETUNIQUE: + retval = history_getunique(h, ev); + break; + + case H_SETUNIQUE: + retval = history_setunique(h, ev, va_arg(va, int)); + break; + case H_ADD: str = va_arg(va, const char *); retval = HADD(h, ev, str); |