2015-01-09 07:40:56 +00:00
|
|
|
/* $NetBSD: history.c,v 1.47 2014/05/11 01:05:17 christos Exp $ */
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
2005-08-07 20:55:59 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
#include "config.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
2015-01-09 07:40:56 +00:00
|
|
|
#if 0
|
1994-05-27 05:00:24 +00:00
|
|
|
static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
|
2015-01-09 07:40:56 +00:00
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: history.c,v 1.47 2014/05/11 01:05:17 christos Exp $");
|
|
|
|
#endif
|
1994-05-27 05:00:24 +00:00
|
|
|
#endif /* not lint && not SCCSID */
|
2001-10-01 08:41:27 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-09 07:40:56 +00:00
|
|
|
* hist.c: TYPE(History) access functions
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2001-10-01 08:41:27 +00:00
|
|
|
#include <vis.h>
|
|
|
|
#include <sys/stat.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
static const char hist_cookie[] = "_HiStOrY_V2_\n";
|
1997-06-25 08:14:24 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
#include "histedit.h"
|
2015-01-09 07:40:56 +00:00
|
|
|
#include "chartype.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
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);
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
struct TYPE(history) {
|
|
|
|
void *h_ref; /* Argument for history fcns */
|
2001-10-01 08:41:27 +00:00
|
|
|
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 */
|
2005-08-07 20:55:59 +00:00
|
|
|
history_sfun_t h_del; /* Set the given element */
|
2001-10-01 08:41:27 +00:00
|
|
|
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 */
|
1994-05-27 05:00:24 +00:00
|
|
|
};
|
2005-08-07 20:55:59 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
#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)
|
|
|
|
#define HLAST(h, ev) (*(h)->h_last)((h)->h_ref, ev)
|
|
|
|
#define HCURR(h, ev) (*(h)->h_curr)((h)->h_ref, ev)
|
|
|
|
#define HSET(h, ev, n) (*(h)->h_set)((h)->h_ref, ev, n)
|
|
|
|
#define HCLEAR(h, ev) (*(h)->h_clear)((h)->h_ref, ev)
|
|
|
|
#define HENTER(h, ev, str) (*(h)->h_enter)((h)->h_ref, ev, str)
|
|
|
|
#define HADD(h, ev, str) (*(h)->h_add)((h)->h_ref, ev, str)
|
2005-08-07 20:55:59 +00:00
|
|
|
#define HDEL(h, ev, n) (*(h)->h_del)((h)->h_ref, ev, n)
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
#define h_strdup(a) Strdup(a)
|
2001-10-01 08:41:27 +00:00
|
|
|
#define h_malloc(a) malloc(a)
|
|
|
|
#define h_realloc(a, b) realloc((a), (b))
|
|
|
|
#define h_free(a) free(a)
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
typedef struct {
|
|
|
|
int num;
|
2015-01-09 07:40:56 +00:00
|
|
|
Char *str;
|
2005-08-07 20:55:59 +00:00
|
|
|
} HistEventPrivate;
|
|
|
|
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
private int history_setsize(TYPE(History) *, TYPE(HistEvent) *, int);
|
|
|
|
private int history_getsize(TYPE(History) *, TYPE(HistEvent) *);
|
|
|
|
private int history_setunique(TYPE(History) *, TYPE(HistEvent) *, int);
|
|
|
|
private int history_getunique(TYPE(History) *, TYPE(HistEvent) *);
|
|
|
|
private int history_set_fun(TYPE(History) *, TYPE(History) *);
|
|
|
|
private int history_load(TYPE(History) *, const char *);
|
|
|
|
private int history_save(TYPE(History) *, const char *);
|
|
|
|
private int history_save_fp(TYPE(History) *, FILE *);
|
|
|
|
private int history_prev_event(TYPE(History) *, TYPE(HistEvent) *, int);
|
|
|
|
private int history_next_event(TYPE(History) *, TYPE(HistEvent) *, int);
|
|
|
|
private int history_next_string(TYPE(History) *, TYPE(HistEvent) *, const Char *);
|
|
|
|
private int history_prev_string(TYPE(History) *, TYPE(HistEvent) *, const Char *);
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builtin- history implementation
|
|
|
|
*/
|
|
|
|
typedef struct hentry_t {
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) ev; /* What we return */
|
2012-06-22 18:01:22 +00:00
|
|
|
void *data; /* data */
|
2001-10-01 08:41:27 +00:00
|
|
|
struct hentry_t *next; /* Next entry */
|
|
|
|
struct hentry_t *prev; /* Previous entry */
|
2005-08-07 20:55:59 +00:00
|
|
|
} hentry_t;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
typedef struct history_t {
|
2005-08-07 20:55:59 +00:00
|
|
|
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 */
|
2001-10-01 08:41:27 +00:00
|
|
|
int eventid; /* For generation of unique event id */
|
2015-01-09 07:40:56 +00:00
|
|
|
int flags; /* TYPE(History) flags */
|
2005-08-07 20:55:59 +00:00
|
|
|
#define H_UNIQUE 1 /* Store only unique elements */
|
|
|
|
} history_t;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
private int history_def_next(void *, TYPE(HistEvent) *);
|
|
|
|
private int history_def_first(void *, TYPE(HistEvent) *);
|
|
|
|
private int history_def_prev(void *, TYPE(HistEvent) *);
|
|
|
|
private int history_def_last(void *, TYPE(HistEvent) *);
|
|
|
|
private int history_def_curr(void *, TYPE(HistEvent) *);
|
|
|
|
private int history_def_set(void *, TYPE(HistEvent) *, const int);
|
|
|
|
private void history_def_clear(void *, TYPE(HistEvent) *);
|
|
|
|
private int history_def_enter(void *, TYPE(HistEvent) *, const Char *);
|
|
|
|
private int history_def_add(void *, TYPE(HistEvent) *, const Char *);
|
|
|
|
private int history_def_del(void *, TYPE(HistEvent) *, const int);
|
2005-08-07 20:55:59 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
private int history_def_init(void **, TYPE(HistEvent) *, int);
|
|
|
|
private int history_def_insert(history_t *, TYPE(HistEvent) *, const Char *);
|
|
|
|
private void history_def_delete(history_t *, TYPE(HistEvent) *, hentry_t *);
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
private int history_deldata_nth(history_t *, TYPE(HistEvent) *, int, void **);
|
|
|
|
private int history_set_nth(void *, TYPE(HistEvent) *, int);
|
2012-06-22 18:01:22 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
#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
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
#define he_strerror(code) he_errlist[code]
|
|
|
|
#define he_seterrev(evp, code) {\
|
|
|
|
evp->num = code;\
|
|
|
|
evp->str = he_strerror(code);\
|
|
|
|
}
|
|
|
|
|
|
|
|
/* error messages */
|
2015-01-09 07:40:56 +00:00
|
|
|
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")
|
2001-10-01 08:41:27 +00:00
|
|
|
};
|
|
|
|
/* 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
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
/* history_def_first():
|
|
|
|
* Default function to return the first event in the history.
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_first(void *p, TYPE(HistEvent) *ev)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
|
|
|
|
h->cursor = h->list.next;
|
|
|
|
if (h->cursor != &h->list)
|
|
|
|
*ev = h->cursor->ev;
|
|
|
|
else {
|
|
|
|
he_seterrev(ev, _HE_FIRST_NOTFOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* history_def_last():
|
|
|
|
* Default function to return the last event in the history.
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_last(void *p, TYPE(HistEvent) *ev)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
|
|
|
|
h->cursor = h->list.prev;
|
|
|
|
if (h->cursor != &h->list)
|
|
|
|
*ev = h->cursor->ev;
|
|
|
|
else {
|
|
|
|
he_seterrev(ev, _HE_LAST_NOTFOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* history_def_next():
|
|
|
|
* Default function to return the next event in the history.
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_next(void *p, TYPE(HistEvent) *ev)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if (h->cursor == &h->list) {
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_EMPTY_LIST);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if (h->cursor->next == &h->list) {
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_END_REACHED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
h->cursor = h->cursor->next;
|
|
|
|
*ev = h->cursor->ev;
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_def_prev():
|
|
|
|
* Default function to return the previous event in the history.
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_prev(void *p, TYPE(HistEvent) *ev)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if (h->cursor == &h->list) {
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev,
|
|
|
|
(h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if (h->cursor->prev == &h->list) {
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_START_REACHED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
h->cursor = h->cursor->prev;
|
|
|
|
*ev = h->cursor->ev;
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_def_curr():
|
|
|
|
* Default function to return the current event in the history.
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_curr(void *p, TYPE(HistEvent) *ev)
|
2001-10-01 08:41:27 +00:00
|
|
|
{
|
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
|
|
|
|
if (h->cursor != &h->list)
|
|
|
|
*ev = h->cursor->ev;
|
|
|
|
else {
|
|
|
|
he_seterrev(ev,
|
|
|
|
(h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_def_set():
|
|
|
|
* Default function to set the current event in the history to the
|
|
|
|
* given one.
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_set(void *p, TYPE(HistEvent) *ev, const int n)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (h->cur == 0) {
|
|
|
|
he_seterrev(ev, _HE_EMPTY_LIST);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
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);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-22 18:01:22 +00:00
|
|
|
/* history_set_nth():
|
|
|
|
* Default function to set the current event in the history to the
|
|
|
|
* n-th one.
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_set_nth(void *p, TYPE(HistEvent) *ev, int n)
|
2012-06-22 18:01:22 +00:00
|
|
|
{
|
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
|
|
|
|
if (h->cur == 0) {
|
|
|
|
he_seterrev(ev, _HE_EMPTY_LIST);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2012-06-22 18:01:22 +00:00
|
|
|
}
|
|
|
|
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);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2012-06-22 18:01:22 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2012-06-22 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* history_def_add():
|
|
|
|
* Append string to element
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_add(void *p, TYPE(HistEvent) *ev, const Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
size_t len;
|
2015-01-09 07:40:56 +00:00
|
|
|
Char *s;
|
2005-08-07 20:55:59 +00:00
|
|
|
HistEventPrivate *evp = (void *)&h->cursor->ev;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if (h->cursor == &h->list)
|
2015-01-09 07:40:56 +00:00
|
|
|
return history_def_enter(p, ev, str);
|
|
|
|
len = Strlen(evp->str) + Strlen(str) + 1;
|
|
|
|
s = h_malloc(len * sizeof(*s));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (s == NULL) {
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_MALLOC_FAILED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) Strncpy(s, h->cursor->ev.str, len);
|
|
|
|
s[len - 1] = '\0';
|
|
|
|
(void) Strncat(s, str, len - Strlen(s) - 1);
|
|
|
|
h_free(evp->str);
|
2005-08-07 20:55:59 +00:00
|
|
|
evp->str = s;
|
2001-10-01 08:41:27 +00:00
|
|
|
*ev = h->cursor->ev;
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-22 18:01:22 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_deldata_nth(history_t *h, TYPE(HistEvent) *ev,
|
2012-06-22 18:01:22 +00:00
|
|
|
int num, void **data)
|
|
|
|
{
|
|
|
|
if (history_set_nth(h, ev, num) != 0)
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2012-06-22 18:01:22 +00:00
|
|
|
/* magic value to skip delete (just set to n-th history) */
|
|
|
|
if (data == (void **)-1)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
|
|
|
ev->str = Strdup(h->cursor->ev.str);
|
2012-06-22 18:01:22 +00:00
|
|
|
ev->num = h->cursor->ev.num;
|
|
|
|
if (data)
|
|
|
|
*data = h->cursor->data;
|
|
|
|
history_def_delete(h, ev, h->cursor);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2012-06-22 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
/* history_def_del():
|
|
|
|
* Delete element hp of the h list
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_del(void *p, TYPE(HistEvent) *ev __attribute__((__unused__)),
|
2005-08-07 20:55:59 +00:00
|
|
|
const int num)
|
|
|
|
{
|
|
|
|
history_t *h = (history_t *) p;
|
|
|
|
if (history_def_set(h, ev, num) != 0)
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
|
|
|
ev->str = Strdup(h->cursor->ev.str);
|
2005-08-07 20:55:59 +00:00
|
|
|
ev->num = h->cursor->ev.num;
|
|
|
|
history_def_delete(h, ev, h->cursor);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* history_def_delete():
|
|
|
|
* Delete element hp of the h list
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 05:00:24 +00:00
|
|
|
private void
|
2005-08-07 20:55:59 +00:00
|
|
|
history_def_delete(history_t *h,
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) *ev __attribute__((__unused__)), hentry_t *hp)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2005-08-07 20:55:59 +00:00
|
|
|
HistEventPrivate *evp = (void *)&hp->ev;
|
2001-10-01 08:41:27 +00:00
|
|
|
if (hp == &h->list)
|
|
|
|
abort();
|
2012-06-22 18:01:22 +00:00
|
|
|
if (h->cursor == hp) {
|
2005-08-07 20:55:59 +00:00
|
|
|
h->cursor = hp->prev;
|
2012-06-22 18:01:22 +00:00
|
|
|
if (h->cursor == &h->list)
|
|
|
|
h->cursor = hp->next;
|
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
hp->prev->next = hp->next;
|
|
|
|
hp->next->prev = hp->prev;
|
2015-01-09 07:40:56 +00:00
|
|
|
h_free(evp->str);
|
2001-10-01 08:41:27 +00:00
|
|
|
h_free(hp);
|
|
|
|
h->cur--;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_def_insert():
|
|
|
|
* Insert element with string str in the h list
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_insert(history_t *h, TYPE(HistEvent) *ev, const Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
hentry_t *c;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
c = h_malloc(sizeof(*c));
|
|
|
|
if (c == NULL)
|
2005-08-07 20:55:59 +00:00
|
|
|
goto oomem;
|
2015-01-09 07:40:56 +00:00
|
|
|
if ((c->ev.str = h_strdup(str)) == NULL) {
|
|
|
|
h_free(c);
|
2005-08-07 20:55:59 +00:00
|
|
|
goto oomem;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
c->data = NULL;
|
|
|
|
c->ev.num = ++h->eventid;
|
|
|
|
c->next = h->list.next;
|
|
|
|
c->prev = &h->list;
|
|
|
|
h->list.next->prev = c;
|
|
|
|
h->list.next = c;
|
2001-10-01 08:41:27 +00:00
|
|
|
h->cur++;
|
2015-01-09 07:40:56 +00:00
|
|
|
h->cursor = c;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
*ev = c->ev;
|
|
|
|
return 0;
|
2005-08-07 20:55:59 +00:00
|
|
|
oomem:
|
|
|
|
he_seterrev(ev, _HE_MALLOC_FAILED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_def_enter():
|
|
|
|
* Default function to enter an item in the history
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_enter(void *p, TYPE(HistEvent) *ev, const Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
|
2015-01-09 07:40:56 +00:00
|
|
|
Strcmp(h->list.next->ev.str, str) == 0)
|
|
|
|
return 0;
|
2005-08-07 20:55:59 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (history_def_insert(h, ev, str) == -1)
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1; /* error, keep error message */
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
/*
|
|
|
|
* Always keep at least one entry.
|
|
|
|
* This way we don't have to check for the empty list.
|
|
|
|
*/
|
2005-08-07 20:55:59 +00:00
|
|
|
while (h->cur > h->max && h->cur > 0)
|
2001-10-01 08:41:27 +00:00
|
|
|
history_def_delete(h, ev, h->list.prev);
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_def_init():
|
|
|
|
* Default history initialization function
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
/* ARGSUSED */
|
2005-08-07 20:55:59 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_init(void **p, TYPE(HistEvent) *ev __attribute__((__unused__)), int n)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
history_t *h = (history_t *) h_malloc(sizeof(*h));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (h == NULL)
|
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
n = 0;
|
|
|
|
h->eventid = 0;
|
|
|
|
h->cur = 0;
|
|
|
|
h->max = n;
|
|
|
|
h->list.next = h->list.prev = &h->list;
|
|
|
|
h->list.ev.str = NULL;
|
|
|
|
h->list.ev.num = 0;
|
|
|
|
h->cursor = &h->list;
|
2005-08-07 20:55:59 +00:00
|
|
|
h->flags = 0;
|
2015-01-09 07:40:56 +00:00
|
|
|
*p = h;
|
2005-08-07 20:55:59 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-06-25 08:14:24 +00:00
|
|
|
/* history_def_clear():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Default history cleanup function
|
|
|
|
*/
|
|
|
|
private void
|
2015-01-09 07:40:56 +00:00
|
|
|
history_def_clear(void *p, TYPE(HistEvent) *ev)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
history_t *h = (history_t *) p;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
while (h->list.prev != &h->list)
|
|
|
|
history_def_delete(h, ev, h->list.prev);
|
2015-01-09 07:40:56 +00:00
|
|
|
h->cursor = &h->list;
|
2001-10-01 08:41:27 +00:00
|
|
|
h->eventid = 0;
|
|
|
|
h->cur = 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/* history_init():
|
|
|
|
* Initialization function.
|
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
public TYPE(History) *
|
|
|
|
FUN(history,init)(void)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) ev;
|
|
|
|
TYPE(History) *h = (TYPE(History) *) h_malloc(sizeof(*h));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (h == NULL)
|
|
|
|
return NULL;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if (history_def_init(&h->h_ref, &ev, 0) == -1) {
|
2015-01-09 07:40:56 +00:00
|
|
|
h_free(h);
|
2005-08-07 20:55:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
h->h_ent = -1;
|
|
|
|
h->h_next = history_def_next;
|
|
|
|
h->h_first = history_def_first;
|
|
|
|
h->h_last = history_def_last;
|
|
|
|
h->h_prev = history_def_prev;
|
|
|
|
h->h_curr = history_def_curr;
|
|
|
|
h->h_set = history_def_set;
|
|
|
|
h->h_clear = history_def_clear;
|
|
|
|
h->h_enter = history_def_enter;
|
|
|
|
h->h_add = history_def_add;
|
2005-08-07 20:55:59 +00:00
|
|
|
h->h_del = history_def_del;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return h;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_end():
|
|
|
|
* clean up history;
|
|
|
|
*/
|
|
|
|
public void
|
2015-01-09 07:40:56 +00:00
|
|
|
FUN(history,end)(TYPE(History) *h)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) ev;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if (h->h_next == history_def_next)
|
|
|
|
history_def_clear(h->h_ref, &ev);
|
2007-03-11 21:47:40 +00:00
|
|
|
h_free(h->h_ref);
|
2005-08-07 20:55:59 +00:00
|
|
|
h_free(h);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
/* history_setsize():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Set history number of events
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_setsize(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
|
2001-10-01 08:41:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (h->h_next != history_def_next) {
|
|
|
|
he_seterrev(ev, _HE_NOT_ALLOWED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
if (num < 0) {
|
|
|
|
he_seterrev(ev, _HE_BAD_PARAM);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
history_def_setsize(h->h_ref, num);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_getsize():
|
|
|
|
* Get number of events currently in history
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_getsize(TYPE(History) *h, TYPE(HistEvent) *ev)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
if (h->h_next != history_def_next) {
|
|
|
|
he_seterrev(ev, _HE_NOT_ALLOWED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2005-08-07 20:55:59 +00:00
|
|
|
ev->num = history_def_getsize(h->h_ref);
|
|
|
|
if (ev->num < -1) {
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_SIZE_NEGATIVE);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_setunique():
|
|
|
|
* Set if adjacent equal events should not be entered in history.
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_setunique(TYPE(History) *h, TYPE(HistEvent) *ev, int uni)
|
2005-08-07 20:55:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (h->h_next != history_def_next) {
|
|
|
|
he_seterrev(ev, _HE_NOT_ALLOWED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
|
|
|
history_def_setunique(h->h_ref, uni);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_getunique():
|
|
|
|
* Get if adjacent equal events should not be entered in history.
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_getunique(TYPE(History) *h, TYPE(HistEvent) *ev)
|
2005-08-07 20:55:59 +00:00
|
|
|
{
|
|
|
|
if (h->h_next != history_def_next) {
|
|
|
|
he_seterrev(ev, _HE_NOT_ALLOWED);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
|
|
|
ev->num = history_def_getunique(h->h_ref);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_set_fun():
|
|
|
|
* Set history functions
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_set_fun(TYPE(History) *h, TYPE(History) *nh)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) ev;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL ||
|
|
|
|
nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL ||
|
|
|
|
nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
|
2005-08-07 20:55:59 +00:00
|
|
|
nh->h_del == NULL || nh->h_ref == NULL) {
|
2001-10-01 08:41:27 +00:00
|
|
|
if (h->h_next != history_def_next) {
|
2015-01-09 07:40:56 +00:00
|
|
|
if (history_def_init(&h->h_ref, &ev, 0) == -1)
|
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
h->h_first = history_def_first;
|
|
|
|
h->h_next = history_def_next;
|
|
|
|
h->h_last = history_def_last;
|
|
|
|
h->h_prev = history_def_prev;
|
|
|
|
h->h_curr = history_def_curr;
|
|
|
|
h->h_set = history_def_set;
|
|
|
|
h->h_clear = history_def_clear;
|
|
|
|
h->h_enter = history_def_enter;
|
|
|
|
h->h_add = history_def_add;
|
2005-08-07 20:55:59 +00:00
|
|
|
h->h_del = history_def_del;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
if (h->h_next == history_def_next)
|
|
|
|
history_def_clear(h->h_ref, &ev);
|
|
|
|
|
|
|
|
h->h_ent = -1;
|
|
|
|
h->h_first = nh->h_first;
|
|
|
|
h->h_next = nh->h_next;
|
|
|
|
h->h_last = nh->h_last;
|
|
|
|
h->h_prev = nh->h_prev;
|
|
|
|
h->h_curr = nh->h_curr;
|
|
|
|
h->h_set = nh->h_set;
|
|
|
|
h->h_clear = nh->h_clear;
|
|
|
|
h->h_enter = nh->h_enter;
|
|
|
|
h->h_add = nh->h_add;
|
2005-08-07 20:55:59 +00:00
|
|
|
h->h_del = nh->h_del;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-06-25 08:14:24 +00:00
|
|
|
/* history_load():
|
2015-01-09 07:40:56 +00:00
|
|
|
* TYPE(History) load function
|
1997-06-25 08:14:24 +00:00
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_load(TYPE(History) *h, const char *fname)
|
1997-06-25 08:14:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *line;
|
|
|
|
size_t sz, max_size;
|
|
|
|
char *ptr;
|
|
|
|
int i = -1;
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) ev;
|
|
|
|
#ifdef WIDECHAR
|
|
|
|
static ct_buffer_t conv;
|
|
|
|
#endif
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if ((fp = fopen(fname, "r")) == NULL)
|
2015-01-09 07:40:56 +00:00
|
|
|
return i;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if ((line = fgetln(fp, &sz)) == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (strncmp(line, hist_cookie, sz) != 0)
|
|
|
|
goto done;
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
goto done;
|
2001-10-01 08:41:27 +00:00
|
|
|
for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
|
|
|
|
char c = line[sz];
|
|
|
|
|
|
|
|
if (sz != 0 && line[sz - 1] == '\n')
|
|
|
|
line[--sz] = '\0';
|
|
|
|
else
|
|
|
|
line[sz] = '\0';
|
|
|
|
|
|
|
|
if (max_size < sz) {
|
2005-08-07 20:55:59 +00:00
|
|
|
char *nptr;
|
2015-01-09 07:40:56 +00:00
|
|
|
max_size = (sz + 1024) & (size_t)~1023;
|
|
|
|
nptr = h_realloc(ptr, max_size * sizeof(*ptr));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (nptr == NULL) {
|
|
|
|
i = -1;
|
|
|
|
goto oomem;
|
|
|
|
}
|
|
|
|
ptr = nptr;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
(void) strunvis(ptr, line);
|
|
|
|
line[sz] = c;
|
2015-01-09 07:40:56 +00:00
|
|
|
if (HENTER(h, &ev, ct_decode_string(ptr, &conv)) == -1) {
|
2012-06-22 18:01:22 +00:00
|
|
|
i = -1;
|
|
|
|
goto oomem;
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2005-08-07 20:55:59 +00:00
|
|
|
oomem:
|
2015-01-09 07:40:56 +00:00
|
|
|
h_free(ptr);
|
1997-06-25 08:14:24 +00:00
|
|
|
done:
|
2001-10-01 08:41:27 +00:00
|
|
|
(void) fclose(fp);
|
2015-01-09 07:40:56 +00:00
|
|
|
return i;
|
1997-06-25 08:14:24 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
|
2014-05-11 01:44:11 +00:00
|
|
|
/* history_save_fp():
|
2015-01-09 07:40:56 +00:00
|
|
|
* TYPE(History) save function
|
1997-06-25 08:14:24 +00:00
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
private int
|
|
|
|
history_save_fp(TYPE(History) *h, FILE *fp)
|
1997-06-25 08:14:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(HistEvent) ev;
|
2005-08-07 20:55:59 +00:00
|
|
|
int i = -1, retval;
|
2001-10-01 08:41:27 +00:00
|
|
|
size_t len, max_size;
|
|
|
|
char *ptr;
|
2015-01-09 07:40:56 +00:00
|
|
|
const char *str;
|
|
|
|
#ifdef WIDECHAR
|
|
|
|
static ct_buffer_t conv;
|
|
|
|
#endif
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
|
|
|
|
goto done;
|
|
|
|
if (fputs(hist_cookie, fp) == EOF)
|
|
|
|
goto done;
|
2015-01-09 07:40:56 +00:00
|
|
|
ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
goto done;
|
|
|
|
for (i = 0, retval = HLAST(h, &ev);
|
2001-10-01 08:41:27 +00:00
|
|
|
retval != -1;
|
|
|
|
retval = HPREV(h, &ev), i++) {
|
2015-01-09 07:40:56 +00:00
|
|
|
str = ct_encode_string(ev.str, &conv);
|
|
|
|
len = strlen(str) * 4;
|
2001-10-01 08:41:27 +00:00
|
|
|
if (len >= max_size) {
|
2005-08-07 20:55:59 +00:00
|
|
|
char *nptr;
|
2015-01-09 07:40:56 +00:00
|
|
|
max_size = (len + 1024) & (size_t)~1023;
|
|
|
|
nptr = h_realloc(ptr, max_size * sizeof(*ptr));
|
2005-08-07 20:55:59 +00:00
|
|
|
if (nptr == NULL) {
|
|
|
|
i = -1;
|
|
|
|
goto oomem;
|
|
|
|
}
|
|
|
|
ptr = nptr;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) strvis(ptr, str, VIS_WHITE);
|
2002-10-14 10:42:38 +00:00
|
|
|
(void) fprintf(fp, "%s\n", ptr);
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2005-08-07 20:55:59 +00:00
|
|
|
oomem:
|
2015-01-09 07:40:56 +00:00
|
|
|
h_free(ptr);
|
2014-05-11 01:44:11 +00:00
|
|
|
done:
|
2015-01-09 07:40:56 +00:00
|
|
|
return i;
|
2014-05-11 01:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_save():
|
2015-01-09 07:40:56 +00:00
|
|
|
* History save function
|
2014-05-11 01:44:11 +00:00
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_save(TYPE(History) *h, const char *fname)
|
2014-05-11 01:44:11 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
FILE *fp;
|
|
|
|
int i;
|
2014-05-11 01:44:11 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
if ((fp = fopen(fname, "w")) == NULL)
|
|
|
|
return -1;
|
2014-05-11 01:44:11 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
i = history_save_fp(h, fp);
|
2014-05-11 01:44:11 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) fclose(fp);
|
|
|
|
return i;
|
1997-06-25 08:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* history_prev_event():
|
|
|
|
* Find the previous event, with number given
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_prev_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
|
|
|
|
if (ev->num == num)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
he_seterrev(ev, _HE_NOT_FOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-22 18:01:22 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_next_evdata(TYPE(History) *h, TYPE(HistEvent) *ev, int num, void **d)
|
2012-06-22 18:01:22 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
|
2015-01-09 07:40:56 +00:00
|
|
|
if (ev->num == num) {
|
2012-06-22 18:01:22 +00:00
|
|
|
if (d)
|
|
|
|
*d = ((history_t *)h->h_ref)->cursor->data;
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2012-06-22 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
he_seterrev(ev, _HE_NOT_FOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2012-06-22 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* history_next_event():
|
|
|
|
* Find the next event, with number given
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_next_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
|
|
|
|
if (ev->num == num)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
he_seterrev(ev, _HE_NOT_FOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_prev_string():
|
|
|
|
* Find the previous event beginning with string
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_prev_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
size_t len = Strlen(str);
|
2001-10-01 08:41:27 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
|
2015-01-09 07:40:56 +00:00
|
|
|
if (Strncmp(str, ev->str, len) == 0)
|
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_NOT_FOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history_next_string():
|
|
|
|
* Find the next event beginning with string
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
history_next_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
size_t len = Strlen(str);
|
2001-10-01 08:41:27 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
|
2015-01-09 07:40:56 +00:00
|
|
|
if (Strncmp(str, ev->str, len) == 0)
|
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
he_seterrev(ev, _HE_NOT_FOUND);
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* history():
|
|
|
|
* User interface to history functions.
|
|
|
|
*/
|
2001-10-01 08:41:27 +00:00
|
|
|
int
|
2015-01-09 07:40:56 +00:00
|
|
|
FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
va_list va;
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *str;
|
2001-10-01 08:41:27 +00:00
|
|
|
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;
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
case H_GETUNIQUE:
|
|
|
|
retval = history_getunique(h, ev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_SETUNIQUE:
|
|
|
|
retval = history_setunique(h, ev, va_arg(va, int));
|
|
|
|
break;
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
case H_ADD:
|
2015-01-09 07:40:56 +00:00
|
|
|
str = va_arg(va, const Char *);
|
2001-10-01 08:41:27 +00:00
|
|
|
retval = HADD(h, ev, str);
|
|
|
|
break;
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
case H_DEL:
|
|
|
|
retval = HDEL(h, ev, va_arg(va, const int));
|
|
|
|
break;
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
case H_ENTER:
|
2015-01-09 07:40:56 +00:00
|
|
|
str = va_arg(va, const Char *);
|
2001-10-01 08:41:27 +00:00
|
|
|
if ((retval = HENTER(h, ev, str)) != -1)
|
|
|
|
h->h_ent = ev->num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_APPEND:
|
2015-01-09 07:40:56 +00:00
|
|
|
str = va_arg(va, const Char *);
|
2001-10-01 08:41:27 +00:00
|
|
|
if ((retval = HSET(h, ev, h->h_ent)) != -1)
|
|
|
|
retval = HADD(h, ev, str);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_FIRST:
|
|
|
|
retval = HFIRST(h, ev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_NEXT:
|
|
|
|
retval = HNEXT(h, ev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_LAST:
|
|
|
|
retval = HLAST(h, ev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_PREV:
|
|
|
|
retval = HPREV(h, ev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_CURR:
|
|
|
|
retval = HCURR(h, ev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_SET:
|
|
|
|
retval = HSET(h, ev, va_arg(va, const int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_CLEAR:
|
|
|
|
HCLEAR(h, ev);
|
|
|
|
retval = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_LOAD:
|
|
|
|
retval = history_load(h, va_arg(va, const char *));
|
|
|
|
if (retval == -1)
|
|
|
|
he_seterrev(ev, _HE_HIST_READ);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_SAVE:
|
|
|
|
retval = history_save(h, va_arg(va, const char *));
|
|
|
|
if (retval == -1)
|
|
|
|
he_seterrev(ev, _HE_HIST_WRITE);
|
|
|
|
break;
|
|
|
|
|
2014-05-11 01:44:11 +00:00
|
|
|
case H_SAVE_FP:
|
2015-01-09 07:40:56 +00:00
|
|
|
retval = history_save_fp(h, va_arg(va, FILE *));
|
2014-05-11 01:44:11 +00:00
|
|
|
if (retval == -1)
|
2015-01-09 07:40:56 +00:00
|
|
|
he_seterrev(ev, _HE_HIST_WRITE);
|
2014-05-11 01:44:11 +00:00
|
|
|
break;
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
case H_PREV_EVENT:
|
|
|
|
retval = history_prev_event(h, ev, va_arg(va, int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_NEXT_EVENT:
|
|
|
|
retval = history_next_event(h, ev, va_arg(va, int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H_PREV_STR:
|
2015-01-09 07:40:56 +00:00
|
|
|
retval = history_prev_string(h, ev, va_arg(va, const Char *));
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case H_NEXT_STR:
|
2015-01-09 07:40:56 +00:00
|
|
|
retval = history_next_string(h, ev, va_arg(va, const Char *));
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case H_FUNC:
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
TYPE(History) hf;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
hf.h_ref = va_arg(va, void *);
|
2001-10-01 08:41:27 +00:00
|
|
|
h->h_ent = -1;
|
|
|
|
hf.h_first = va_arg(va, history_gfun_t);
|
|
|
|
hf.h_next = va_arg(va, history_gfun_t);
|
|
|
|
hf.h_last = va_arg(va, history_gfun_t);
|
|
|
|
hf.h_prev = va_arg(va, history_gfun_t);
|
|
|
|
hf.h_curr = va_arg(va, history_gfun_t);
|
|
|
|
hf.h_set = va_arg(va, history_sfun_t);
|
|
|
|
hf.h_clear = va_arg(va, history_vfun_t);
|
|
|
|
hf.h_enter = va_arg(va, history_efun_t);
|
|
|
|
hf.h_add = va_arg(va, history_efun_t);
|
2005-08-07 20:55:59 +00:00
|
|
|
hf.h_del = va_arg(va, history_sfun_t);
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if ((retval = history_set_fun(h, &hf)) == -1)
|
|
|
|
he_seterrev(ev, _HE_PARAM_MISSING);
|
|
|
|
break;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
case H_END:
|
2015-01-09 07:40:56 +00:00
|
|
|
FUN(history,end)(h);
|
2001-10-01 08:41:27 +00:00
|
|
|
retval = 0;
|
|
|
|
break;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2012-06-22 18:01:22 +00:00
|
|
|
case H_NEXT_EVDATA:
|
|
|
|
{
|
|
|
|
int num = va_arg(va, int);
|
|
|
|
void **d = va_arg(va, void **);
|
|
|
|
retval = history_next_evdata(h, ev, num, d);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case H_DELDATA:
|
|
|
|
{
|
|
|
|
int num = va_arg(va, int);
|
|
|
|
void **d = va_arg(va, void **);
|
|
|
|
retval = history_deldata_nth((history_t *)h->h_ref, ev, num, d);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case H_REPLACE: /* only use after H_NEXT_EVDATA */
|
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *line = va_arg(va, const Char *);
|
2012-06-22 18:01:22 +00:00
|
|
|
void *d = va_arg(va, void *);
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *s;
|
|
|
|
if(!line || !(s = Strdup(line))) {
|
2012-06-22 18:01:22 +00:00
|
|
|
retval = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
((history_t *)h->h_ref)->cursor->ev.str = s;
|
|
|
|
((history_t *)h->h_ref)->cursor->data = d;
|
|
|
|
retval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
default:
|
|
|
|
retval = -1;
|
|
|
|
he_seterrev(ev, _HE_UNKNOWN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end(va);
|
2012-06-22 18:01:22 +00:00
|
|
|
return retval;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|