/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Christos Zoulas of Cornell University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
typedef int (*history_gfun_t)(void *, TYPE(HistEvent) *);
typedef int (*history_efun_t)(void *, TYPE(HistEvent) *, const Char *);
typedef void (*history_vfun_t)(void *, TYPE(HistEvent) *);
typedef int (*history_sfun_t)(void *, TYPE(HistEvent) *, const int);
struct TYPE(history) {
void *h_ref; /* Argument for history fcns */
int h_ent; /* Last entry point for history */
history_gfun_t h_first; /* Get the first element */
history_gfun_t h_next; /* Get the next element */
history_gfun_t h_last; /* Get the last element */
history_gfun_t h_prev; /* Get the previous element */
history_gfun_t h_curr; /* Get the current element */
history_sfun_t h_set; /* Set the current element */
history_sfun_t h_del; /* Set the given element */
history_vfun_t h_clear; /* Clear the history list */
history_efun_t h_enter; /* Add an element */
history_efun_t h_add; /* Append to an element */
};
/*
* Builtin- history implementation
*/
typedef struct hentry_t {
TYPE(HistEvent) ev; /* What we return */
void *data; /* data */
struct hentry_t *next; /* Next entry */
struct hentry_t *prev; /* Previous entry */
} 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 */
int eventid; /* For generation of unique event id */
int flags; /* TYPE(History) flags */
#define H_UNIQUE 1 /* Store only unique elements */
} history_t;
static int history_def_next(void *, TYPE(HistEvent) *);
static int history_def_first(void *, TYPE(HistEvent) *);
static int history_def_prev(void *, TYPE(HistEvent) *);
static int history_def_last(void *, TYPE(HistEvent) *);
static int history_def_curr(void *, TYPE(HistEvent) *);
static int history_def_set(void *, TYPE(HistEvent) *, const int);
static void history_def_clear(void *, TYPE(HistEvent) *);
static int history_def_enter(void *, TYPE(HistEvent) *, const Char *);
static int history_def_add(void *, TYPE(HistEvent) *, const Char *);
static int history_def_del(void *, TYPE(HistEvent) *, const int);
/* error messages */
static const Char *const he_errlist[] = {
STR("OK"),
STR("unknown error"),
STR("malloc() failed"),
STR("first event not found"),
STR("last event not found"),
STR("empty list"),
STR("no next event"),
STR("no previous event"),
STR("current event is invalid"),
STR("event not found"),
STR("can't read history from file"),
STR("can't write history"),
STR("required parameter(s) not supplied"),
STR("history size negative"),
STR("function not allowed with other history-functions-set the default"),
STR("bad parameters")
};
/* error codes */
#define _HE_OK 0
#define _HE_UNKNOWN 1
#define _HE_MALLOC_FAILED 2
#define _HE_FIRST_NOTFOUND 3
#define _HE_LAST_NOTFOUND 4
#define _HE_EMPTY_LIST 5
#define _HE_END_REACHED 6
#define _HE_START_REACHED 7
#define _HE_CURR_INVALID 8
#define _HE_NOT_FOUND 9
#define _HE_HIST_READ 10
#define _HE_HIST_WRITE 11
#define _HE_PARAM_MISSING 12
#define _HE_SIZE_NEGATIVE 13
#define _HE_NOT_ALLOWED 14
#define _HE_BAD_PARAM 15
/* history_def_first():
* Default function to return the first event in the history.
*/
static int
history_def_first(void *p, TYPE(HistEvent) *ev)
{
history_t *h = (history_t *) p;
/* history_def_last():
* Default function to return the last event in the history.
*/
static int
history_def_last(void *p, TYPE(HistEvent) *ev)
{
history_t *h = (history_t *) p;
/* history_def_next():
* Default function to return the next event in the history.
*/
static int
history_def_next(void *p, TYPE(HistEvent) *ev)
{
history_t *h = (history_t *) p;
if (h->cursor == &h->list) {
he_seterrev(ev, _HE_EMPTY_LIST);
return -1;
}
if (h->cursor->next == &h->list) {
he_seterrev(ev, _HE_END_REACHED);
return -1;
}
h->cursor = h->cursor->next;
*ev = h->cursor->ev;
return 0;
}
/* history_def_prev():
* Default function to return the previous event in the history.
*/
static int
history_def_prev(void *p, TYPE(HistEvent) *ev)
{
history_t *h = (history_t *) p;
if (h->cursor->prev == &h->list) {
he_seterrev(ev, _HE_START_REACHED);
return -1;
}
h->cursor = h->cursor->prev;
*ev = h->cursor->ev;
return 0;
}
/* history_def_curr():
* Default function to return the current event in the history.
*/
static int
history_def_curr(void *p, TYPE(HistEvent) *ev)
{
history_t *h = (history_t *) p;
/* history_def_set():
* Default function to set the current event in the history to the
* given one.
*/
static int
history_def_set(void *p, TYPE(HistEvent) *ev, const int n)
{
history_t *h = (history_t *) p;
if (h->cur == 0) {
he_seterrev(ev, _HE_EMPTY_LIST);
return -1;
}
if (h->cursor == &h->list || h->cursor->ev.num != n) {
for (h->cursor = h->list.next; h->cursor != &h->list;
h->cursor = h->cursor->next)
if (h->cursor->ev.num == n)
break;
}
if (h->cursor == &h->list) {
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
return 0;
}
/* history_set_nth():
* Default function to set the current event in the history to the
* n-th one.
*/
static int
history_set_nth(void *p, TYPE(HistEvent) *ev, int n)
{
history_t *h = (history_t *) p;
if (h->cur == 0) {
he_seterrev(ev, _HE_EMPTY_LIST);
return -1;
}
for (h->cursor = h->list.prev; h->cursor != &h->list;
h->cursor = h->cursor->prev)
if (n-- <= 0)
break;
if (h->cursor == &h->list) {
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
return 0;
}
static int
history_deldata_nth(history_t *h, TYPE(HistEvent) *ev,
int num, void **data)
{
if (history_set_nth(h, ev, num) != 0)
return -1;
/* magic value to skip delete (just set to n-th history) */
if (data == (void **)-1)
return 0;
ev->str = Strdup(h->cursor->ev.str);
ev->num = h->cursor->ev.num;
if (data)
*data = h->cursor->data;
history_def_delete(h, ev, h->cursor);
return 0;
}
/* history_def_del():
* Delete element hp of the h list
*/
/* ARGSUSED */
static int
history_def_del(void *p, TYPE(HistEvent) *ev __attribute__((__unused__)),
const int num)
{
history_t *h = (history_t *) p;
if (history_def_set(h, ev, num) != 0)
return -1;
ev->str = Strdup(h->cursor->ev.str);
ev->num = h->cursor->ev.num;
history_def_delete(h, ev, h->cursor);
return 0;
}
/* history_def_delete():
* Delete element hp of the h list
*/
/* ARGSUSED */
static void
history_def_delete(history_t *h,
TYPE(HistEvent) *ev __attribute__((__unused__)), hentry_t *hp)
{
HistEventPrivate *evp = (void *)&hp->ev;
if (hp == &h->list)
abort();
if (h->cursor == hp) {
h->cursor = hp->prev;
if (h->cursor == &h->list)
h->cursor = hp->next;
}
hp->prev->next = hp->next;
hp->next->prev = hp->prev;
h_free(evp->str);
h_free(hp);
h->cur--;
}
/* history_def_insert():
* Insert element with string str in the h list
*/
static int
history_def_insert(history_t *h, TYPE(HistEvent) *ev, const Char *str)
{
hentry_t *c;
/* history_def_enter():
* Default function to enter an item in the history
*/
static int
history_def_enter(void *p, TYPE(HistEvent) *ev, const Char *str)
{
history_t *h = (history_t *) p;
/*
* Always keep at least one entry.
* This way we don't have to check for the empty list.
*/
while (h->cur > h->max && h->cur > 0)
history_def_delete(h, ev, h->list.prev);
return 1;
}
/* history_def_init():
* Default history initialization function
*/
/* ARGSUSED */
static int
history_def_init(void **p, TYPE(HistEvent) *ev __attribute__((__unused__)), int n)
{
history_t *h = (history_t *) h_malloc(sizeof(*h));
if (h == NULL)
return -1;
/* history_getsize():
* Get number of events currently in history
*/
static int
history_getsize(TYPE(History) *h, TYPE(HistEvent) *ev)
{
if (h->h_next != history_def_next) {
he_seterrev(ev, _HE_NOT_ALLOWED);
return -1;
}
ev->num = history_def_getsize(h->h_ref);
if (ev->num < -1) {
he_seterrev(ev, _HE_SIZE_NEGATIVE);
return -1;
}
return 0;
}
/* history_setunique():
* Set if adjacent equal events should not be entered in history.
*/
static int
history_setunique(TYPE(History) *h, TYPE(HistEvent) *ev, int uni)
{
/* history_getunique():
* Get if adjacent equal events should not be entered in history.
*/
static int
history_getunique(TYPE(History) *h, TYPE(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;
}
/* history_set_fun():
* Set history functions
*/
static int
history_set_fun(TYPE(History) *h, TYPE(History) *nh)
{
TYPE(HistEvent) ev;
/* history_save():
* History save function
*/
static int
history_save(TYPE(History) *h, const char *fname)
{
FILE *fp;
int i;
if ((i = open(fname, O_WRONLY|O_CREAT|O_TRUNC,
S_IRUSR|S_IWUSR)) == -1)
return -1;
if ((fp = fdopen(i, "w")) == NULL)
return -1;
i = history_save_fp(h, (size_t)-1, fp);
(void) fclose(fp);
return i;
}
/* history_prev_event():
* Find the previous event, with number given
*/
static int
history_prev_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
{
int retval;
for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
if (ev->num == num)
return 0;
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
static int
history_next_evdata(TYPE(History) *h, TYPE(HistEvent) *ev, int num, void **d)
{
int retval;
for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
if (ev->num == num) {
if (d)
*d = ((history_t *)h->h_ref)->cursor->data;
return 0;
}
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
/* history_next_event():
* Find the next event, with number given
*/
static int
history_next_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
{
int retval;
for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
if (ev->num == num)
return 0;
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
/* history_prev_string():
* Find the previous event beginning with string
*/
static int
history_prev_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
{
size_t len = Strlen(str);
int retval;
for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
if (Strncmp(str, ev->str, len) == 0)
return 0;
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
/* history_next_string():
* Find the next event beginning with string
*/
static int
history_next_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
{
size_t len = Strlen(str);
int retval;
for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
if (Strncmp(str, ev->str, len) == 0)
return 0;
he_seterrev(ev, _HE_NOT_FOUND);
return -1;
}
/* history():
* User interface to history functions.
*/
int
FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...)
{
va_list va;
const Char *str;
int retval;
va_start(va, fun);
he_seterrev(ev, _HE_OK);
switch (fun) {
case H_GETSIZE:
retval = history_getsize(h, ev);
break;
case H_SETSIZE:
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;