Home | History | Annotate | Line # | Download | only in libedit
history.c revision 1.46
      1  1.46  christos /*	$NetBSD: history.c,v 1.46 2011/11/18 20:39:18 christos Exp $	*/
      2   1.3     lukem 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1992, 1993
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Christos Zoulas of Cornell University.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.24       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19  christos #include "config.h"
     36   1.1       cgd #if !defined(lint) && !defined(SCCSID)
     37   1.6  christos #if 0
     38   1.1       cgd static char sccsid[] = "@(#)history.c	8.1 (Berkeley) 6/4/93";
     39   1.6  christos #else
     40  1.46  christos __RCSID("$NetBSD: history.c,v 1.46 2011/11/18 20:39:18 christos Exp $");
     41   1.6  christos #endif
     42   1.1       cgd #endif /* not lint && not SCCSID */
     43   1.1       cgd 
     44   1.1       cgd /*
     45  1.35  christos  * hist.c: TYPE(History) access functions
     46   1.1       cgd  */
     47   1.1       cgd #include <string.h>
     48   1.1       cgd #include <stdlib.h>
     49   1.1       cgd #include <stdarg.h>
     50  1.12  christos #include <vis.h>
     51  1.17  christos #include <sys/stat.h>
     52   1.1       cgd 
     53  1.12  christos static const char hist_cookie[] = "_HiStOrY_V2_\n";
     54   1.2  christos 
     55   1.1       cgd #include "histedit.h"
     56  1.35  christos #include "chartype.h"
     57   1.1       cgd 
     58  1.42  christos typedef int (*history_gfun_t)(void *, TYPE(HistEvent) *);
     59  1.42  christos typedef int (*history_efun_t)(void *, TYPE(HistEvent) *, const Char *);
     60  1.42  christos typedef void (*history_vfun_t)(void *, TYPE(HistEvent) *);
     61  1.42  christos typedef int (*history_sfun_t)(void *, TYPE(HistEvent) *, const int);
     62   1.1       cgd 
     63  1.37  christos struct TYPE(history) {
     64  1.42  christos 	void *h_ref;		/* Argument for history fcns	 */
     65  1.16     lukem 	int h_ent;		/* Last entry point for history	 */
     66  1.16     lukem 	history_gfun_t h_first;	/* Get the first element	 */
     67  1.16     lukem 	history_gfun_t h_next;	/* Get the next element		 */
     68  1.16     lukem 	history_gfun_t h_last;	/* Get the last element		 */
     69  1.16     lukem 	history_gfun_t h_prev;	/* Get the previous element	 */
     70  1.16     lukem 	history_gfun_t h_curr;	/* Get the current element	 */
     71  1.16     lukem 	history_sfun_t h_set;	/* Set the current element	 */
     72  1.30  christos 	history_sfun_t h_del;	/* Set the given element	 */
     73  1.16     lukem 	history_vfun_t h_clear;	/* Clear the history list	 */
     74  1.16     lukem 	history_efun_t h_enter;	/* Add an element		 */
     75  1.16     lukem 	history_efun_t h_add;	/* Append to an element		 */
     76   1.1       cgd };
     77  1.22  christos 
     78  1.16     lukem #define	HNEXT(h, ev)		(*(h)->h_next)((h)->h_ref, ev)
     79  1.16     lukem #define	HFIRST(h, ev)		(*(h)->h_first)((h)->h_ref, ev)
     80  1.16     lukem #define	HPREV(h, ev)		(*(h)->h_prev)((h)->h_ref, ev)
     81  1.16     lukem #define	HLAST(h, ev)		(*(h)->h_last)((h)->h_ref, ev)
     82  1.16     lukem #define	HCURR(h, ev)		(*(h)->h_curr)((h)->h_ref, ev)
     83  1.16     lukem #define	HSET(h, ev, n)		(*(h)->h_set)((h)->h_ref, ev, n)
     84  1.16     lukem #define	HCLEAR(h, ev)		(*(h)->h_clear)((h)->h_ref, ev)
     85   1.7  christos #define	HENTER(h, ev, str)	(*(h)->h_enter)((h)->h_ref, ev, str)
     86   1.7  christos #define	HADD(h, ev, str)	(*(h)->h_add)((h)->h_ref, ev, str)
     87  1.30  christos #define	HDEL(h, ev, n)		(*(h)->h_del)((h)->h_ref, ev, n)
     88   1.1       cgd 
     89  1.35  christos #define	h_strdup(a)	Strdup(a)
     90  1.16     lukem #define	h_malloc(a)	malloc(a)
     91  1.16     lukem #define	h_realloc(a, b)	realloc((a), (b))
     92  1.16     lukem #define	h_free(a)	free(a)
     93   1.1       cgd 
     94  1.19  christos typedef struct {
     95  1.19  christos     int		num;
     96  1.35  christos     Char	*str;
     97  1.19  christos } HistEventPrivate;
     98  1.19  christos 
     99  1.19  christos 
    100   1.1       cgd 
    101  1.35  christos private int history_setsize(TYPE(History) *, TYPE(HistEvent) *, int);
    102  1.35  christos private int history_getsize(TYPE(History) *, TYPE(HistEvent) *);
    103  1.35  christos private int history_setunique(TYPE(History) *, TYPE(HistEvent) *, int);
    104  1.35  christos private int history_getunique(TYPE(History) *, TYPE(HistEvent) *);
    105  1.35  christos private int history_set_fun(TYPE(History) *, TYPE(History) *);
    106  1.35  christos private int history_load(TYPE(History) *, const char *);
    107  1.35  christos private int history_save(TYPE(History) *, const char *);
    108  1.35  christos private int history_prev_event(TYPE(History) *, TYPE(HistEvent) *, int);
    109  1.35  christos private int history_next_event(TYPE(History) *, TYPE(HistEvent) *, int);
    110  1.35  christos private int history_next_string(TYPE(History) *, TYPE(HistEvent) *, const Char *);
    111  1.35  christos private int history_prev_string(TYPE(History) *, TYPE(HistEvent) *, const Char *);
    112   1.1       cgd 
    113   1.1       cgd 
    114   1.1       cgd /***********************************************************************/
    115   1.1       cgd 
    116   1.1       cgd /*
    117   1.1       cgd  * Builtin- history implementation
    118   1.1       cgd  */
    119   1.1       cgd typedef struct hentry_t {
    120  1.35  christos 	TYPE(HistEvent) ev;		/* What we return		 */
    121  1.34  christos 	void *data;		/* data				 */
    122  1.16     lukem 	struct hentry_t *next;	/* Next entry			 */
    123  1.16     lukem 	struct hentry_t *prev;	/* Previous entry		 */
    124  1.22  christos } hentry_t;
    125   1.1       cgd 
    126   1.1       cgd typedef struct history_t {
    127  1.22  christos 	hentry_t list;		/* Fake list header element	*/
    128  1.22  christos 	hentry_t *cursor;	/* Current element in the list	*/
    129  1.22  christos 	int max;		/* Maximum number of events	*/
    130  1.22  christos 	int cur;		/* Current number of events	*/
    131  1.16     lukem 	int eventid;		/* For generation of unique event id	 */
    132  1.35  christos 	int flags;		/* TYPE(History) flags		*/
    133  1.22  christos #define H_UNIQUE	1	/* Store only unique elements	*/
    134  1.22  christos } history_t;
    135  1.16     lukem 
    136  1.42  christos private int history_def_next(void *, TYPE(HistEvent) *);
    137  1.42  christos private int history_def_first(void *, TYPE(HistEvent) *);
    138  1.42  christos private int history_def_prev(void *, TYPE(HistEvent) *);
    139  1.42  christos private int history_def_last(void *, TYPE(HistEvent) *);
    140  1.42  christos private int history_def_curr(void *, TYPE(HistEvent) *);
    141  1.42  christos private int history_def_set(void *, TYPE(HistEvent) *, const int);
    142  1.42  christos private void history_def_clear(void *, TYPE(HistEvent) *);
    143  1.42  christos private int history_def_enter(void *, TYPE(HistEvent) *, const Char *);
    144  1.42  christos private int history_def_add(void *, TYPE(HistEvent) *, const Char *);
    145  1.42  christos private int history_def_del(void *, TYPE(HistEvent) *, const int);
    146  1.35  christos 
    147  1.42  christos private int history_def_init(void **, TYPE(HistEvent) *, int);
    148  1.35  christos private int history_def_insert(history_t *, TYPE(HistEvent) *, const Char *);
    149  1.35  christos private void history_def_delete(history_t *, TYPE(HistEvent) *, hentry_t *);
    150   1.1       cgd 
    151  1.35  christos private int history_deldata_nth(history_t *, TYPE(HistEvent) *, int, void **);
    152  1.42  christos private int history_set_nth(void *, TYPE(HistEvent) *, int);
    153  1.34  christos 
    154  1.22  christos #define	history_def_setsize(p, num)(void) (((history_t *)p)->max = (num))
    155  1.22  christos #define	history_def_getsize(p)  (((history_t *)p)->cur)
    156  1.22  christos #define	history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0)
    157  1.22  christos #define	history_def_setunique(p, uni) \
    158  1.22  christos     if (uni) \
    159  1.22  christos 	(((history_t *)p)->flags) |= H_UNIQUE; \
    160  1.22  christos     else \
    161  1.22  christos 	(((history_t *)p)->flags) &= ~H_UNIQUE
    162   1.1       cgd 
    163  1.16     lukem #define	he_strerror(code)	he_errlist[code]
    164  1.16     lukem #define	he_seterrev(evp, code)	{\
    165   1.7  christos 				    evp->num = code;\
    166   1.7  christos 				    evp->str = he_strerror(code);\
    167   1.7  christos 				}
    168  1.14    simonb 
    169   1.7  christos /* error messages */
    170  1.35  christos static const Char *const he_errlist[] = {
    171  1.35  christos 	STR("OK"),
    172  1.35  christos 	STR("unknown error"),
    173  1.35  christos 	STR("malloc() failed"),
    174  1.35  christos 	STR("first event not found"),
    175  1.35  christos 	STR("last event not found"),
    176  1.35  christos 	STR("empty list"),
    177  1.35  christos 	STR("no next event"),
    178  1.35  christos 	STR("no previous event"),
    179  1.35  christos 	STR("current event is invalid"),
    180  1.35  christos 	STR("event not found"),
    181  1.35  christos 	STR("can't read history from file"),
    182  1.35  christos 	STR("can't write history"),
    183  1.35  christos 	STR("required parameter(s) not supplied"),
    184  1.35  christos 	STR("history size negative"),
    185  1.35  christos 	STR("function not allowed with other history-functions-set the default"),
    186  1.35  christos 	STR("bad parameters")
    187   1.7  christos };
    188   1.7  christos /* error codes */
    189  1.16     lukem #define	_HE_OK                   0
    190  1.16     lukem #define	_HE_UNKNOWN		 1
    191  1.16     lukem #define	_HE_MALLOC_FAILED        2
    192  1.16     lukem #define	_HE_FIRST_NOTFOUND       3
    193  1.16     lukem #define	_HE_LAST_NOTFOUND        4
    194  1.16     lukem #define	_HE_EMPTY_LIST           5
    195  1.16     lukem #define	_HE_END_REACHED          6
    196  1.16     lukem #define	_HE_START_REACHED	 7
    197  1.16     lukem #define	_HE_CURR_INVALID	 8
    198  1.16     lukem #define	_HE_NOT_FOUND		 9
    199  1.16     lukem #define	_HE_HIST_READ		10
    200  1.16     lukem #define	_HE_HIST_WRITE		11
    201  1.16     lukem #define	_HE_PARAM_MISSING	12
    202  1.16     lukem #define	_HE_SIZE_NEGATIVE	13
    203  1.16     lukem #define	_HE_NOT_ALLOWED		14
    204  1.16     lukem #define	_HE_BAD_PARAM		15
    205   1.1       cgd 
    206   1.1       cgd /* history_def_first():
    207   1.1       cgd  *	Default function to return the first event in the history.
    208   1.1       cgd  */
    209   1.7  christos private int
    210  1.42  christos history_def_first(void *p, TYPE(HistEvent) *ev)
    211   1.1       cgd {
    212  1.16     lukem 	history_t *h = (history_t *) p;
    213   1.7  christos 
    214  1.16     lukem 	h->cursor = h->list.next;
    215  1.16     lukem 	if (h->cursor != &h->list)
    216  1.16     lukem 		*ev = h->cursor->ev;
    217  1.16     lukem 	else {
    218  1.16     lukem 		he_seterrev(ev, _HE_FIRST_NOTFOUND);
    219  1.43  christos 		return -1;
    220  1.16     lukem 	}
    221   1.7  christos 
    222  1.43  christos 	return 0;
    223   1.1       cgd }
    224   1.1       cgd 
    225   1.8  christos 
    226   1.1       cgd /* history_def_last():
    227   1.1       cgd  *	Default function to return the last event in the history.
    228   1.1       cgd  */
    229   1.7  christos private int
    230  1.42  christos history_def_last(void *p, TYPE(HistEvent) *ev)
    231  1.16     lukem {
    232  1.16     lukem 	history_t *h = (history_t *) p;
    233  1.16     lukem 
    234  1.16     lukem 	h->cursor = h->list.prev;
    235  1.16     lukem 	if (h->cursor != &h->list)
    236  1.16     lukem 		*ev = h->cursor->ev;
    237  1.16     lukem 	else {
    238  1.16     lukem 		he_seterrev(ev, _HE_LAST_NOTFOUND);
    239  1.43  christos 		return -1;
    240  1.16     lukem 	}
    241   1.7  christos 
    242  1.43  christos 	return 0;
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.8  christos 
    246   1.1       cgd /* history_def_next():
    247   1.1       cgd  *	Default function to return the next event in the history.
    248   1.1       cgd  */
    249   1.7  christos private int
    250  1.42  christos history_def_next(void *p, TYPE(HistEvent) *ev)
    251  1.16     lukem {
    252  1.16     lukem 	history_t *h = (history_t *) p;
    253  1.16     lukem 
    254  1.28  christos 	if (h->cursor == &h->list) {
    255  1.16     lukem 		he_seterrev(ev, _HE_EMPTY_LIST);
    256  1.43  christos 		return -1;
    257  1.16     lukem 	}
    258   1.1       cgd 
    259  1.28  christos 	if (h->cursor->next == &h->list) {
    260  1.16     lukem 		he_seterrev(ev, _HE_END_REACHED);
    261  1.43  christos 		return -1;
    262  1.16     lukem 	}
    263   1.7  christos 
    264  1.28  christos         h->cursor = h->cursor->next;
    265  1.28  christos         *ev = h->cursor->ev;
    266  1.28  christos 
    267  1.43  christos 	return 0;
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.1       cgd 
    271   1.1       cgd /* history_def_prev():
    272   1.1       cgd  *	Default function to return the previous event in the history.
    273   1.1       cgd  */
    274   1.7  christos private int
    275  1.42  christos history_def_prev(void *p, TYPE(HistEvent) *ev)
    276  1.16     lukem {
    277  1.16     lukem 	history_t *h = (history_t *) p;
    278  1.16     lukem 
    279  1.28  christos 	if (h->cursor == &h->list) {
    280  1.16     lukem 		he_seterrev(ev,
    281  1.16     lukem 		    (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
    282  1.43  christos 		return -1;
    283  1.16     lukem 	}
    284   1.1       cgd 
    285  1.28  christos 	if (h->cursor->prev == &h->list) {
    286  1.16     lukem 		he_seterrev(ev, _HE_START_REACHED);
    287  1.43  christos 		return -1;
    288  1.16     lukem 	}
    289   1.7  christos 
    290  1.28  christos         h->cursor = h->cursor->prev;
    291  1.28  christos         *ev = h->cursor->ev;
    292  1.28  christos 
    293  1.43  christos 	return 0;
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.1       cgd 
    297   1.1       cgd /* history_def_curr():
    298   1.1       cgd  *	Default function to return the current event in the history.
    299   1.1       cgd  */
    300   1.7  christos private int
    301  1.42  christos history_def_curr(void *p, TYPE(HistEvent) *ev)
    302   1.1       cgd {
    303  1.16     lukem 	history_t *h = (history_t *) p;
    304   1.1       cgd 
    305  1.16     lukem 	if (h->cursor != &h->list)
    306  1.16     lukem 		*ev = h->cursor->ev;
    307  1.16     lukem 	else {
    308  1.16     lukem 		he_seterrev(ev,
    309  1.16     lukem 		    (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
    310  1.43  christos 		return -1;
    311  1.16     lukem 	}
    312   1.7  christos 
    313  1.43  christos 	return 0;
    314   1.1       cgd }
    315   1.1       cgd 
    316   1.8  christos 
    317   1.8  christos /* history_def_set():
    318   1.8  christos  *	Default function to set the current event in the history to the
    319   1.8  christos  *	given one.
    320   1.8  christos  */
    321   1.8  christos private int
    322  1.42  christos history_def_set(void *p, TYPE(HistEvent) *ev, const int n)
    323  1.16     lukem {
    324  1.16     lukem 	history_t *h = (history_t *) p;
    325   1.8  christos 
    326  1.16     lukem 	if (h->cur == 0) {
    327  1.16     lukem 		he_seterrev(ev, _HE_EMPTY_LIST);
    328  1.43  christos 		return -1;
    329  1.16     lukem 	}
    330  1.16     lukem 	if (h->cursor == &h->list || h->cursor->ev.num != n) {
    331  1.16     lukem 		for (h->cursor = h->list.next; h->cursor != &h->list;
    332  1.16     lukem 		    h->cursor = h->cursor->next)
    333  1.16     lukem 			if (h->cursor->ev.num == n)
    334  1.16     lukem 				break;
    335  1.16     lukem 	}
    336  1.16     lukem 	if (h->cursor == &h->list) {
    337  1.16     lukem 		he_seterrev(ev, _HE_NOT_FOUND);
    338  1.43  christos 		return -1;
    339  1.16     lukem 	}
    340  1.43  christos 	return 0;
    341   1.8  christos }
    342   1.8  christos 
    343   1.8  christos 
    344  1.34  christos /* history_set_nth():
    345  1.34  christos  *	Default function to set the current event in the history to the
    346  1.34  christos  *	n-th one.
    347  1.34  christos  */
    348  1.34  christos private int
    349  1.42  christos history_set_nth(void *p, TYPE(HistEvent) *ev, int n)
    350  1.34  christos {
    351  1.34  christos 	history_t *h = (history_t *) p;
    352  1.34  christos 
    353  1.34  christos 	if (h->cur == 0) {
    354  1.34  christos 		he_seterrev(ev, _HE_EMPTY_LIST);
    355  1.43  christos 		return -1;
    356  1.34  christos 	}
    357  1.34  christos 	for (h->cursor = h->list.prev; h->cursor != &h->list;
    358  1.34  christos 	    h->cursor = h->cursor->prev)
    359  1.34  christos 		if (n-- <= 0)
    360  1.34  christos 			break;
    361  1.34  christos 	if (h->cursor == &h->list) {
    362  1.34  christos 		he_seterrev(ev, _HE_NOT_FOUND);
    363  1.43  christos 		return -1;
    364  1.34  christos 	}
    365  1.43  christos 	return 0;
    366  1.34  christos }
    367  1.34  christos 
    368  1.34  christos 
    369   1.1       cgd /* history_def_add():
    370   1.1       cgd  *	Append string to element
    371   1.1       cgd  */
    372   1.7  christos private int
    373  1.42  christos history_def_add(void *p, TYPE(HistEvent) *ev, const Char *str)
    374  1.16     lukem {
    375  1.16     lukem 	history_t *h = (history_t *) p;
    376  1.16     lukem 	size_t len;
    377  1.35  christos 	Char *s;
    378  1.19  christos 	HistEventPrivate *evp = (void *)&h->cursor->ev;
    379  1.16     lukem 
    380  1.16     lukem 	if (h->cursor == &h->list)
    381  1.43  christos 		return history_def_enter(p, ev, str);
    382  1.35  christos 	len = Strlen(evp->str) + Strlen(str) + 1;
    383  1.35  christos 	s = h_malloc(len * sizeof(*s));
    384  1.21  christos 	if (s == NULL) {
    385  1.16     lukem 		he_seterrev(ev, _HE_MALLOC_FAILED);
    386  1.43  christos 		return -1;
    387  1.16     lukem 	}
    388  1.35  christos 	(void) Strncpy(s, h->cursor->ev.str, len);
    389  1.35  christos         s[len - 1] = '\0';
    390  1.35  christos 	(void) Strncat(s, str, len - Strlen(s) - 1);
    391  1.42  christos 	h_free(evp->str);
    392  1.19  christos 	evp->str = s;
    393  1.16     lukem 	*ev = h->cursor->ev;
    394  1.43  christos 	return 0;
    395   1.1       cgd }
    396   1.1       cgd 
    397   1.1       cgd 
    398  1.34  christos private int
    399  1.35  christos history_deldata_nth(history_t *h, TYPE(HistEvent) *ev,
    400  1.34  christos     int num, void **data)
    401  1.34  christos {
    402  1.34  christos 	if (history_set_nth(h, ev, num) != 0)
    403  1.43  christos 		return -1;
    404  1.34  christos 	/* magic value to skip delete (just set to n-th history) */
    405  1.34  christos 	if (data == (void **)-1)
    406  1.43  christos 		return 0;
    407  1.35  christos 	ev->str = Strdup(h->cursor->ev.str);
    408  1.34  christos 	ev->num = h->cursor->ev.num;
    409  1.34  christos 	if (data)
    410  1.34  christos 		*data = h->cursor->data;
    411  1.34  christos 	history_def_delete(h, ev, h->cursor);
    412  1.43  christos 	return 0;
    413  1.34  christos }
    414  1.34  christos 
    415  1.34  christos 
    416  1.30  christos /* history_def_del():
    417  1.30  christos  *	Delete element hp of the h list
    418  1.30  christos  */
    419  1.30  christos /* ARGSUSED */
    420  1.30  christos private int
    421  1.42  christos history_def_del(void *p, TYPE(HistEvent) *ev __attribute__((__unused__)),
    422  1.30  christos     const int num)
    423  1.30  christos {
    424  1.30  christos 	history_t *h = (history_t *) p;
    425  1.30  christos 	if (history_def_set(h, ev, num) != 0)
    426  1.43  christos 		return -1;
    427  1.35  christos 	ev->str = Strdup(h->cursor->ev.str);
    428  1.30  christos 	ev->num = h->cursor->ev.num;
    429  1.30  christos 	history_def_delete(h, ev, h->cursor);
    430  1.43  christos 	return 0;
    431  1.30  christos }
    432  1.30  christos 
    433  1.30  christos 
    434   1.1       cgd /* history_def_delete():
    435   1.1       cgd  *	Delete element hp of the h list
    436   1.1       cgd  */
    437  1.11  christos /* ARGSUSED */
    438   1.1       cgd private void
    439  1.23  christos history_def_delete(history_t *h,
    440  1.35  christos 		   TYPE(HistEvent) *ev __attribute__((__unused__)), hentry_t *hp)
    441  1.16     lukem {
    442  1.19  christos 	HistEventPrivate *evp = (void *)&hp->ev;
    443  1.16     lukem 	if (hp == &h->list)
    444  1.16     lukem 		abort();
    445  1.34  christos 	if (h->cursor == hp) {
    446  1.30  christos 		h->cursor = hp->prev;
    447  1.34  christos 		if (h->cursor == &h->list)
    448  1.34  christos 			h->cursor = hp->next;
    449  1.34  christos 	}
    450  1.16     lukem 	hp->prev->next = hp->next;
    451  1.16     lukem 	hp->next->prev = hp->prev;
    452  1.42  christos 	h_free(evp->str);
    453  1.16     lukem 	h_free(hp);
    454  1.16     lukem 	h->cur--;
    455   1.1       cgd }
    456   1.1       cgd 
    457   1.1       cgd 
    458   1.1       cgd /* history_def_insert():
    459   1.1       cgd  *	Insert element with string str in the h list
    460   1.1       cgd  */
    461   1.7  christos private int
    462  1.35  christos history_def_insert(history_t *h, TYPE(HistEvent) *ev, const Char *str)
    463  1.16     lukem {
    464  1.39  christos 	hentry_t *c;
    465  1.16     lukem 
    466  1.39  christos 	c = h_malloc(sizeof(*c));
    467  1.39  christos 	if (c == NULL)
    468  1.21  christos 		goto oomem;
    469  1.39  christos 	if ((c->ev.str = h_strdup(str)) == NULL) {
    470  1.42  christos 		h_free(c);
    471  1.21  christos 		goto oomem;
    472  1.16     lukem 	}
    473  1.39  christos 	c->data = NULL;
    474  1.39  christos 	c->ev.num = ++h->eventid;
    475  1.39  christos 	c->next = h->list.next;
    476  1.39  christos 	c->prev = &h->list;
    477  1.39  christos 	h->list.next->prev = c;
    478  1.39  christos 	h->list.next = c;
    479  1.16     lukem 	h->cur++;
    480  1.39  christos 	h->cursor = c;
    481   1.1       cgd 
    482  1.39  christos 	*ev = c->ev;
    483  1.43  christos 	return 0;
    484  1.21  christos oomem:
    485  1.21  christos 	he_seterrev(ev, _HE_MALLOC_FAILED);
    486  1.43  christos 	return -1;
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd 
    490   1.1       cgd /* history_def_enter():
    491   1.1       cgd  *	Default function to enter an item in the history
    492   1.1       cgd  */
    493   1.7  christos private int
    494  1.42  christos history_def_enter(void *p, TYPE(HistEvent) *ev, const Char *str)
    495  1.16     lukem {
    496  1.16     lukem 	history_t *h = (history_t *) p;
    497  1.16     lukem 
    498  1.22  christos 	if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
    499  1.35  christos 	    Strcmp(h->list.next->ev.str, str) == 0)
    500  1.43  christos 	    return 0;
    501  1.22  christos 
    502  1.16     lukem 	if (history_def_insert(h, ev, str) == -1)
    503  1.43  christos 		return -1;	/* error, keep error message */
    504  1.16     lukem 
    505  1.16     lukem 	/*
    506  1.16     lukem          * Always keep at least one entry.
    507  1.16     lukem          * This way we don't have to check for the empty list.
    508  1.16     lukem          */
    509  1.18  jdolecek 	while (h->cur > h->max && h->cur > 0)
    510  1.16     lukem 		history_def_delete(h, ev, h->list.prev);
    511   1.7  christos 
    512  1.43  christos 	return 1;
    513   1.1       cgd }
    514   1.1       cgd 
    515   1.1       cgd 
    516   1.1       cgd /* history_def_init():
    517   1.1       cgd  *	Default history initialization function
    518   1.1       cgd  */
    519  1.11  christos /* ARGSUSED */
    520  1.21  christos private int
    521  1.42  christos history_def_init(void **p, TYPE(HistEvent) *ev __attribute__((__unused__)), int n)
    522  1.16     lukem {
    523  1.42  christos 	history_t *h = (history_t *) h_malloc(sizeof(*h));
    524  1.21  christos 	if (h == NULL)
    525  1.21  christos 		return -1;
    526  1.16     lukem 
    527  1.16     lukem 	if (n <= 0)
    528  1.16     lukem 		n = 0;
    529  1.16     lukem 	h->eventid = 0;
    530  1.16     lukem 	h->cur = 0;
    531  1.16     lukem 	h->max = n;
    532  1.16     lukem 	h->list.next = h->list.prev = &h->list;
    533  1.16     lukem 	h->list.ev.str = NULL;
    534  1.16     lukem 	h->list.ev.num = 0;
    535  1.16     lukem 	h->cursor = &h->list;
    536  1.22  christos 	h->flags = 0;
    537  1.42  christos 	*p = h;
    538  1.21  christos 	return 0;
    539   1.1       cgd }
    540   1.1       cgd 
    541   1.1       cgd 
    542   1.2  christos /* history_def_clear():
    543   1.1       cgd  *	Default history cleanup function
    544   1.1       cgd  */
    545   1.1       cgd private void
    546  1.42  christos history_def_clear(void *p, TYPE(HistEvent) *ev)
    547  1.16     lukem {
    548  1.16     lukem 	history_t *h = (history_t *) p;
    549  1.16     lukem 
    550  1.16     lukem 	while (h->list.prev != &h->list)
    551  1.16     lukem 		history_def_delete(h, ev, h->list.prev);
    552  1.39  christos 	h->cursor = &h->list;
    553  1.16     lukem 	h->eventid = 0;
    554  1.16     lukem 	h->cur = 0;
    555   1.1       cgd }
    556   1.1       cgd 
    557   1.2  christos 
    558   1.2  christos 
    559   1.2  christos 
    560   1.1       cgd /************************************************************************/
    561   1.1       cgd 
    562   1.1       cgd /* history_init():
    563   1.1       cgd  *	Initialization function.
    564   1.1       cgd  */
    565  1.36  christos public TYPE(History) *
    566  1.35  christos FUN(history,init)(void)
    567   1.1       cgd {
    568  1.35  christos 	TYPE(HistEvent) ev;
    569  1.42  christos 	TYPE(History) *h = (TYPE(History) *) h_malloc(sizeof(*h));
    570  1.21  christos 	if (h == NULL)
    571  1.21  christos 		return NULL;
    572   1.1       cgd 
    573  1.21  christos 	if (history_def_init(&h->h_ref, &ev, 0) == -1) {
    574  1.42  christos 		h_free(h);
    575  1.21  christos 		return NULL;
    576  1.21  christos 	}
    577  1.16     lukem 	h->h_ent = -1;
    578  1.16     lukem 	h->h_next = history_def_next;
    579  1.16     lukem 	h->h_first = history_def_first;
    580  1.16     lukem 	h->h_last = history_def_last;
    581  1.16     lukem 	h->h_prev = history_def_prev;
    582  1.16     lukem 	h->h_curr = history_def_curr;
    583  1.16     lukem 	h->h_set = history_def_set;
    584  1.16     lukem 	h->h_clear = history_def_clear;
    585  1.16     lukem 	h->h_enter = history_def_enter;
    586  1.16     lukem 	h->h_add = history_def_add;
    587  1.31  christos 	h->h_del = history_def_del;
    588   1.1       cgd 
    589  1.43  christos 	return h;
    590   1.1       cgd }
    591   1.1       cgd 
    592   1.1       cgd 
    593   1.1       cgd /* history_end():
    594   1.1       cgd  *	clean up history;
    595   1.1       cgd  */
    596   1.1       cgd public void
    597  1.35  christos FUN(history,end)(TYPE(History) *h)
    598   1.1       cgd {
    599  1.35  christos 	TYPE(HistEvent) ev;
    600  1.16     lukem 
    601  1.16     lukem 	if (h->h_next == history_def_next)
    602  1.16     lukem 		history_def_clear(h->h_ref, &ev);
    603  1.32  christos 	h_free(h->h_ref);
    604  1.29  christos 	h_free(h);
    605   1.1       cgd }
    606   1.1       cgd 
    607   1.1       cgd 
    608   1.1       cgd 
    609  1.12  christos /* history_setsize():
    610   1.1       cgd  *	Set history number of events
    611   1.1       cgd  */
    612   1.1       cgd private int
    613  1.35  christos history_setsize(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
    614  1.16     lukem {
    615   1.7  christos 
    616  1.16     lukem 	if (h->h_next != history_def_next) {
    617  1.16     lukem 		he_seterrev(ev, _HE_NOT_ALLOWED);
    618  1.43  christos 		return -1;
    619  1.16     lukem 	}
    620  1.16     lukem 	if (num < 0) {
    621  1.16     lukem 		he_seterrev(ev, _HE_BAD_PARAM);
    622  1.43  christos 		return -1;
    623  1.16     lukem 	}
    624  1.16     lukem 	history_def_setsize(h->h_ref, num);
    625  1.43  christos 	return 0;
    626   1.1       cgd }
    627   1.1       cgd 
    628  1.16     lukem 
    629  1.12  christos /* history_getsize():
    630   1.7  christos  *      Get number of events currently in history
    631   1.7  christos  */
    632   1.7  christos private int
    633  1.35  christos history_getsize(TYPE(History) *h, TYPE(HistEvent) *ev)
    634  1.16     lukem {
    635  1.22  christos 	if (h->h_next != history_def_next) {
    636  1.22  christos 		he_seterrev(ev, _HE_NOT_ALLOWED);
    637  1.43  christos 		return -1;
    638  1.22  christos 	}
    639  1.22  christos 	ev->num = history_def_getsize(h->h_ref);
    640  1.22  christos 	if (ev->num < -1) {
    641  1.22  christos 		he_seterrev(ev, _HE_SIZE_NEGATIVE);
    642  1.43  christos 		return -1;
    643  1.22  christos 	}
    644  1.43  christos 	return 0;
    645  1.22  christos }
    646  1.22  christos 
    647  1.22  christos 
    648  1.22  christos /* history_setunique():
    649  1.22  christos  *	Set if adjacent equal events should not be entered in history.
    650  1.22  christos  */
    651  1.22  christos private int
    652  1.35  christos history_setunique(TYPE(History) *h, TYPE(HistEvent) *ev, int uni)
    653  1.22  christos {
    654   1.7  christos 
    655  1.16     lukem 	if (h->h_next != history_def_next) {
    656  1.16     lukem 		he_seterrev(ev, _HE_NOT_ALLOWED);
    657  1.43  christos 		return -1;
    658  1.16     lukem 	}
    659  1.22  christos 	history_def_setunique(h->h_ref, uni);
    660  1.43  christos 	return 0;
    661  1.22  christos }
    662  1.22  christos 
    663  1.22  christos 
    664  1.22  christos /* history_getunique():
    665  1.22  christos  *	Get if adjacent equal events should not be entered in history.
    666  1.22  christos  */
    667  1.22  christos private int
    668  1.35  christos history_getunique(TYPE(History) *h, TYPE(HistEvent) *ev)
    669  1.22  christos {
    670  1.22  christos 	if (h->h_next != history_def_next) {
    671  1.22  christos 		he_seterrev(ev, _HE_NOT_ALLOWED);
    672  1.43  christos 		return -1;
    673  1.16     lukem 	}
    674  1.22  christos 	ev->num = history_def_getunique(h->h_ref);
    675  1.43  christos 	return 0;
    676   1.7  christos }
    677   1.1       cgd 
    678  1.16     lukem 
    679   1.1       cgd /* history_set_fun():
    680   1.1       cgd  *	Set history functions
    681   1.1       cgd  */
    682   1.1       cgd private int
    683  1.35  christos history_set_fun(TYPE(History) *h, TYPE(History) *nh)
    684  1.16     lukem {
    685  1.35  christos 	TYPE(HistEvent) ev;
    686  1.16     lukem 
    687  1.16     lukem 	if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL ||
    688  1.16     lukem 	    nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL ||
    689  1.16     lukem 	    nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
    690  1.30  christos 	    nh->h_del == NULL || nh->h_ref == NULL) {
    691  1.16     lukem 		if (h->h_next != history_def_next) {
    692  1.46  christos 			if (history_def_init(&h->h_ref, &ev, 0) == -1)
    693  1.46  christos 				return -1;
    694  1.16     lukem 			h->h_first = history_def_first;
    695  1.16     lukem 			h->h_next = history_def_next;
    696  1.16     lukem 			h->h_last = history_def_last;
    697  1.16     lukem 			h->h_prev = history_def_prev;
    698  1.16     lukem 			h->h_curr = history_def_curr;
    699  1.16     lukem 			h->h_set = history_def_set;
    700  1.16     lukem 			h->h_clear = history_def_clear;
    701  1.16     lukem 			h->h_enter = history_def_enter;
    702  1.16     lukem 			h->h_add = history_def_add;
    703  1.30  christos 			h->h_del = history_def_del;
    704  1.16     lukem 		}
    705  1.43  christos 		return -1;
    706  1.16     lukem 	}
    707  1.16     lukem 	if (h->h_next == history_def_next)
    708  1.16     lukem 		history_def_clear(h->h_ref, &ev);
    709  1.16     lukem 
    710  1.16     lukem 	h->h_ent = -1;
    711  1.16     lukem 	h->h_first = nh->h_first;
    712  1.16     lukem 	h->h_next = nh->h_next;
    713  1.16     lukem 	h->h_last = nh->h_last;
    714  1.16     lukem 	h->h_prev = nh->h_prev;
    715  1.16     lukem 	h->h_curr = nh->h_curr;
    716  1.16     lukem 	h->h_set = nh->h_set;
    717  1.16     lukem 	h->h_clear = nh->h_clear;
    718  1.16     lukem 	h->h_enter = nh->h_enter;
    719  1.16     lukem 	h->h_add = nh->h_add;
    720  1.30  christos 	h->h_del = nh->h_del;
    721   1.1       cgd 
    722  1.43  christos 	return 0;
    723   1.1       cgd }
    724   1.1       cgd 
    725   1.1       cgd 
    726   1.2  christos /* history_load():
    727  1.35  christos  *	TYPE(History) load function
    728   1.2  christos  */
    729   1.2  christos private int
    730  1.35  christos history_load(TYPE(History) *h, const char *fname)
    731  1.16     lukem {
    732  1.16     lukem 	FILE *fp;
    733  1.16     lukem 	char *line;
    734  1.16     lukem 	size_t sz, max_size;
    735  1.16     lukem 	char *ptr;
    736  1.16     lukem 	int i = -1;
    737  1.35  christos 	TYPE(HistEvent) ev;
    738  1.35  christos #ifdef WIDECHAR
    739  1.36  christos 	static ct_buffer_t conv;
    740  1.35  christos #endif
    741  1.16     lukem 
    742  1.16     lukem 	if ((fp = fopen(fname, "r")) == NULL)
    743  1.43  christos 		return i;
    744  1.16     lukem 
    745  1.16     lukem 	if ((line = fgetln(fp, &sz)) == NULL)
    746  1.16     lukem 		goto done;
    747  1.16     lukem 
    748  1.16     lukem 	if (strncmp(line, hist_cookie, sz) != 0)
    749  1.16     lukem 		goto done;
    750  1.16     lukem 
    751  1.42  christos 	ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
    752  1.21  christos 	if (ptr == NULL)
    753  1.21  christos 		goto done;
    754  1.16     lukem 	for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
    755  1.16     lukem 		char c = line[sz];
    756  1.16     lukem 
    757  1.16     lukem 		if (sz != 0 && line[sz - 1] == '\n')
    758  1.16     lukem 			line[--sz] = '\0';
    759  1.16     lukem 		else
    760  1.16     lukem 			line[sz] = '\0';
    761  1.16     lukem 
    762  1.16     lukem 		if (max_size < sz) {
    763  1.21  christos 			char *nptr;
    764  1.45  christos 			max_size = (sz + 1024) & (size_t)~1023;
    765  1.42  christos 			nptr = h_realloc(ptr, max_size * sizeof(*ptr));
    766  1.21  christos 			if (nptr == NULL) {
    767  1.21  christos 				i = -1;
    768  1.21  christos 				goto oomem;
    769  1.21  christos 			}
    770  1.21  christos 			ptr = nptr;
    771  1.16     lukem 		}
    772  1.16     lukem 		(void) strunvis(ptr, line);
    773  1.16     lukem 		line[sz] = c;
    774  1.35  christos 		if (HENTER(h, &ev, ct_decode_string(ptr, &conv)) == -1) {
    775  1.33    sketch 			i = -1;
    776  1.33    sketch 			goto oomem;
    777  1.21  christos 		}
    778  1.16     lukem 	}
    779  1.21  christos oomem:
    780  1.42  christos 	h_free(ptr);
    781   1.2  christos done:
    782  1.16     lukem 	(void) fclose(fp);
    783  1.43  christos 	return i;
    784   1.2  christos }
    785   1.2  christos 
    786   1.2  christos 
    787   1.2  christos /* history_save():
    788  1.35  christos  *	TYPE(History) save function
    789   1.2  christos  */
    790   1.2  christos private int
    791  1.35  christos history_save(TYPE(History) *h, const char *fname)
    792  1.16     lukem {
    793  1.16     lukem 	FILE *fp;
    794  1.35  christos 	TYPE(HistEvent) ev;
    795  1.21  christos 	int i = -1, retval;
    796  1.16     lukem 	size_t len, max_size;
    797  1.40  christos 	char *ptr;
    798  1.40  christos 	const char *str;
    799  1.35  christos #ifdef WIDECHAR
    800  1.36  christos 	static ct_buffer_t conv;
    801  1.35  christos #endif
    802  1.16     lukem 
    803  1.16     lukem 	if ((fp = fopen(fname, "w")) == NULL)
    804  1.43  christos 		return -1;
    805  1.16     lukem 
    806  1.21  christos 	if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
    807  1.21  christos 		goto done;
    808  1.21  christos 	if (fputs(hist_cookie, fp) == EOF)
    809  1.21  christos 		goto done;
    810  1.42  christos 	ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
    811  1.21  christos 	if (ptr == NULL)
    812  1.21  christos 		goto done;
    813  1.21  christos 	for (i = 0, retval = HLAST(h, &ev);
    814  1.16     lukem 	    retval != -1;
    815  1.16     lukem 	    retval = HPREV(h, &ev), i++) {
    816  1.39  christos 		str = ct_encode_string(ev.str, &conv);
    817  1.39  christos 		len = strlen(str) * 4;
    818  1.16     lukem 		if (len >= max_size) {
    819  1.21  christos 			char *nptr;
    820  1.45  christos 			max_size = (len + 1024) & (size_t)~1023;
    821  1.42  christos 			nptr = h_realloc(ptr, max_size * sizeof(*ptr));
    822  1.21  christos 			if (nptr == NULL) {
    823  1.21  christos 				i = -1;
    824  1.21  christos 				goto oomem;
    825  1.21  christos 			}
    826  1.21  christos 			ptr = nptr;
    827  1.16     lukem 		}
    828  1.39  christos 		(void) strvis(ptr, str, VIS_WHITE);
    829  1.20  christos 		(void) fprintf(fp, "%s\n", ptr);
    830  1.16     lukem 	}
    831  1.21  christos oomem:
    832  1.42  christos 	h_free(ptr);
    833  1.21  christos done:
    834  1.16     lukem 	(void) fclose(fp);
    835  1.43  christos 	return i;
    836   1.2  christos }
    837   1.2  christos 
    838   1.2  christos 
    839   1.1       cgd /* history_prev_event():
    840   1.1       cgd  *	Find the previous event, with number given
    841   1.1       cgd  */
    842   1.7  christos private int
    843  1.35  christos history_prev_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
    844  1.16     lukem {
    845  1.16     lukem 	int retval;
    846   1.7  christos 
    847  1.16     lukem 	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
    848  1.16     lukem 		if (ev->num == num)
    849  1.43  christos 			return 0;
    850  1.16     lukem 
    851  1.16     lukem 	he_seterrev(ev, _HE_NOT_FOUND);
    852  1.43  christos 	return -1;
    853   1.1       cgd }
    854   1.1       cgd 
    855   1.1       cgd 
    856  1.34  christos private int
    857  1.35  christos history_next_evdata(TYPE(History) *h, TYPE(HistEvent) *ev, int num, void **d)
    858  1.34  christos {
    859  1.34  christos 	int retval;
    860  1.34  christos 
    861  1.34  christos 	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
    862  1.38  christos 		if (ev->num == num) {
    863  1.34  christos 			if (d)
    864  1.34  christos 				*d = ((history_t *)h->h_ref)->cursor->data;
    865  1.43  christos 			return 0;
    866  1.34  christos 		}
    867  1.34  christos 
    868  1.34  christos 	he_seterrev(ev, _HE_NOT_FOUND);
    869  1.43  christos 	return -1;
    870  1.34  christos }
    871  1.34  christos 
    872  1.34  christos 
    873   1.1       cgd /* history_next_event():
    874   1.1       cgd  *	Find the next event, with number given
    875   1.1       cgd  */
    876   1.7  christos private int
    877  1.35  christos history_next_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
    878  1.16     lukem {
    879  1.16     lukem 	int retval;
    880  1.16     lukem 
    881  1.16     lukem 	for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
    882  1.16     lukem 		if (ev->num == num)
    883  1.43  christos 			return 0;
    884   1.7  christos 
    885  1.16     lukem 	he_seterrev(ev, _HE_NOT_FOUND);
    886  1.43  christos 	return -1;
    887   1.1       cgd }
    888   1.1       cgd 
    889   1.1       cgd 
    890   1.1       cgd /* history_prev_string():
    891   1.1       cgd  *	Find the previous event beginning with string
    892   1.1       cgd  */
    893   1.7  christos private int
    894  1.35  christos history_prev_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
    895  1.16     lukem {
    896  1.35  christos 	size_t len = Strlen(str);
    897  1.16     lukem 	int retval;
    898   1.7  christos 
    899  1.16     lukem 	for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
    900  1.35  christos 		if (Strncmp(str, ev->str, len) == 0)
    901  1.43  christos 			return 0;
    902  1.16     lukem 
    903  1.16     lukem 	he_seterrev(ev, _HE_NOT_FOUND);
    904  1.43  christos 	return -1;
    905   1.1       cgd }
    906   1.1       cgd 
    907   1.1       cgd 
    908   1.1       cgd /* history_next_string():
    909   1.1       cgd  *	Find the next event beginning with string
    910   1.1       cgd  */
    911   1.7  christos private int
    912  1.35  christos history_next_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
    913  1.16     lukem {
    914  1.35  christos 	size_t len = Strlen(str);
    915  1.16     lukem 	int retval;
    916  1.16     lukem 
    917  1.16     lukem 	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
    918  1.35  christos 		if (Strncmp(str, ev->str, len) == 0)
    919  1.43  christos 			return 0;
    920   1.7  christos 
    921  1.16     lukem 	he_seterrev(ev, _HE_NOT_FOUND);
    922  1.43  christos 	return -1;
    923   1.1       cgd }
    924   1.1       cgd 
    925   1.1       cgd 
    926   1.1       cgd /* history():
    927   1.1       cgd  *	User interface to history functions.
    928   1.1       cgd  */
    929   1.7  christos int
    930  1.35  christos FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...)
    931   1.1       cgd {
    932  1.16     lukem 	va_list va;
    933  1.35  christos 	const Char *str;
    934  1.16     lukem 	int retval;
    935  1.16     lukem 
    936  1.16     lukem 	va_start(va, fun);
    937  1.16     lukem 
    938  1.16     lukem 	he_seterrev(ev, _HE_OK);
    939  1.16     lukem 
    940  1.16     lukem 	switch (fun) {
    941  1.16     lukem 	case H_GETSIZE:
    942  1.16     lukem 		retval = history_getsize(h, ev);
    943  1.16     lukem 		break;
    944  1.16     lukem 
    945  1.16     lukem 	case H_SETSIZE:
    946  1.16     lukem 		retval = history_setsize(h, ev, va_arg(va, int));
    947  1.22  christos 		break;
    948  1.22  christos 
    949  1.22  christos 	case H_GETUNIQUE:
    950  1.22  christos 		retval = history_getunique(h, ev);
    951  1.22  christos 		break;
    952  1.22  christos 
    953  1.22  christos 	case H_SETUNIQUE:
    954  1.22  christos 		retval = history_setunique(h, ev, va_arg(va, int));
    955  1.16     lukem 		break;
    956  1.16     lukem 
    957  1.16     lukem 	case H_ADD:
    958  1.35  christos 		str = va_arg(va, const Char *);
    959  1.16     lukem 		retval = HADD(h, ev, str);
    960  1.16     lukem 		break;
    961  1.16     lukem 
    962  1.30  christos 	case H_DEL:
    963  1.30  christos 		retval = HDEL(h, ev, va_arg(va, const int));
    964  1.30  christos 		break;
    965  1.30  christos 
    966  1.16     lukem 	case H_ENTER:
    967  1.35  christos 		str = va_arg(va, const Char *);
    968  1.16     lukem 		if ((retval = HENTER(h, ev, str)) != -1)
    969  1.16     lukem 			h->h_ent = ev->num;
    970  1.16     lukem 		break;
    971  1.16     lukem 
    972  1.16     lukem 	case H_APPEND:
    973  1.35  christos 		str = va_arg(va, const Char *);
    974  1.16     lukem 		if ((retval = HSET(h, ev, h->h_ent)) != -1)
    975  1.16     lukem 			retval = HADD(h, ev, str);
    976  1.16     lukem 		break;
    977  1.16     lukem 
    978  1.16     lukem 	case H_FIRST:
    979  1.16     lukem 		retval = HFIRST(h, ev);
    980  1.16     lukem 		break;
    981   1.1       cgd 
    982  1.16     lukem 	case H_NEXT:
    983  1.16     lukem 		retval = HNEXT(h, ev);
    984  1.16     lukem 		break;
    985  1.16     lukem 
    986  1.16     lukem 	case H_LAST:
    987  1.16     lukem 		retval = HLAST(h, ev);
    988  1.16     lukem 		break;
    989  1.16     lukem 
    990  1.16     lukem 	case H_PREV:
    991  1.16     lukem 		retval = HPREV(h, ev);
    992  1.16     lukem 		break;
    993  1.16     lukem 
    994  1.16     lukem 	case H_CURR:
    995  1.16     lukem 		retval = HCURR(h, ev);
    996  1.16     lukem 		break;
    997  1.16     lukem 
    998  1.16     lukem 	case H_SET:
    999  1.16     lukem 		retval = HSET(h, ev, va_arg(va, const int));
   1000  1.16     lukem 		break;
   1001  1.16     lukem 
   1002  1.16     lukem 	case H_CLEAR:
   1003  1.16     lukem 		HCLEAR(h, ev);
   1004  1.16     lukem 		retval = 0;
   1005  1.16     lukem 		break;
   1006  1.16     lukem 
   1007  1.16     lukem 	case H_LOAD:
   1008  1.16     lukem 		retval = history_load(h, va_arg(va, const char *));
   1009  1.16     lukem 		if (retval == -1)
   1010  1.16     lukem 			he_seterrev(ev, _HE_HIST_READ);
   1011  1.16     lukem 		break;
   1012  1.16     lukem 
   1013  1.16     lukem 	case H_SAVE:
   1014  1.16     lukem 		retval = history_save(h, va_arg(va, const char *));
   1015  1.16     lukem 		if (retval == -1)
   1016  1.16     lukem 			he_seterrev(ev, _HE_HIST_WRITE);
   1017  1.16     lukem 		break;
   1018  1.16     lukem 
   1019  1.16     lukem 	case H_PREV_EVENT:
   1020  1.16     lukem 		retval = history_prev_event(h, ev, va_arg(va, int));
   1021  1.16     lukem 		break;
   1022  1.16     lukem 
   1023  1.16     lukem 	case H_NEXT_EVENT:
   1024  1.16     lukem 		retval = history_next_event(h, ev, va_arg(va, int));
   1025  1.16     lukem 		break;
   1026   1.1       cgd 
   1027  1.16     lukem 	case H_PREV_STR:
   1028  1.35  christos 		retval = history_prev_string(h, ev, va_arg(va, const Char *));
   1029  1.16     lukem 		break;
   1030   1.7  christos 
   1031  1.16     lukem 	case H_NEXT_STR:
   1032  1.35  christos 		retval = history_next_string(h, ev, va_arg(va, const Char *));
   1033  1.16     lukem 		break;
   1034   1.1       cgd 
   1035  1.16     lukem 	case H_FUNC:
   1036   1.1       cgd 	{
   1037  1.35  christos 		TYPE(History) hf;
   1038  1.16     lukem 
   1039  1.42  christos 		hf.h_ref = va_arg(va, void *);
   1040  1.16     lukem 		h->h_ent = -1;
   1041  1.16     lukem 		hf.h_first = va_arg(va, history_gfun_t);
   1042  1.16     lukem 		hf.h_next = va_arg(va, history_gfun_t);
   1043  1.16     lukem 		hf.h_last = va_arg(va, history_gfun_t);
   1044  1.16     lukem 		hf.h_prev = va_arg(va, history_gfun_t);
   1045  1.16     lukem 		hf.h_curr = va_arg(va, history_gfun_t);
   1046  1.16     lukem 		hf.h_set = va_arg(va, history_sfun_t);
   1047  1.16     lukem 		hf.h_clear = va_arg(va, history_vfun_t);
   1048  1.16     lukem 		hf.h_enter = va_arg(va, history_efun_t);
   1049  1.16     lukem 		hf.h_add = va_arg(va, history_efun_t);
   1050  1.30  christos 		hf.h_del = va_arg(va, history_sfun_t);
   1051   1.8  christos 
   1052  1.16     lukem 		if ((retval = history_set_fun(h, &hf)) == -1)
   1053  1.16     lukem 			he_seterrev(ev, _HE_PARAM_MISSING);
   1054  1.16     lukem 		break;
   1055  1.16     lukem 	}
   1056  1.16     lukem 
   1057  1.16     lukem 	case H_END:
   1058  1.36  christos 		FUN(history,end)(h);
   1059  1.16     lukem 		retval = 0;
   1060  1.16     lukem 		break;
   1061  1.16     lukem 
   1062  1.34  christos 	case H_NEXT_EVDATA:
   1063  1.34  christos 	{
   1064  1.34  christos 		int num = va_arg(va, int);
   1065  1.34  christos 		void **d = va_arg(va, void **);
   1066  1.34  christos 		retval = history_next_evdata(h, ev, num, d);
   1067  1.34  christos 		break;
   1068  1.34  christos 	}
   1069  1.34  christos 
   1070  1.34  christos 	case H_DELDATA:
   1071  1.34  christos 	{
   1072  1.34  christos 		int num = va_arg(va, int);
   1073  1.34  christos 		void **d = va_arg(va, void **);
   1074  1.34  christos 		retval = history_deldata_nth((history_t *)h->h_ref, ev, num, d);
   1075  1.34  christos 		break;
   1076  1.34  christos 	}
   1077  1.34  christos 
   1078  1.34  christos 	case H_REPLACE: /* only use after H_NEXT_EVDATA */
   1079  1.34  christos 	{
   1080  1.35  christos 		const Char *line = va_arg(va, const Char *);
   1081  1.34  christos 		void *d = va_arg(va, void *);
   1082  1.35  christos 		const Char *s;
   1083  1.35  christos 		if(!line || !(s = Strdup(line))) {
   1084  1.34  christos 			retval = -1;
   1085  1.34  christos 			break;
   1086  1.34  christos 		}
   1087  1.34  christos 		((history_t *)h->h_ref)->cursor->ev.str = s;
   1088  1.34  christos 		((history_t *)h->h_ref)->cursor->data = d;
   1089  1.34  christos 		retval = 0;
   1090  1.34  christos 		break;
   1091  1.34  christos 	}
   1092  1.34  christos 
   1093  1.16     lukem 	default:
   1094  1.16     lukem 		retval = -1;
   1095  1.16     lukem 		he_seterrev(ev, _HE_UNKNOWN);
   1096  1.16     lukem 		break;
   1097  1.16     lukem 	}
   1098  1.16     lukem 	va_end(va);
   1099  1.34  christos 	return retval;
   1100   1.1       cgd }
   1101