1 /* $OpenBSD: history.c,v 1.28 2016/04/11 21:17:29 schwarze Exp $ */
2 /* $NetBSD: history.c,v 1.37 2010/01/03 18:27:10 christos Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * hist.c: TYPE(History) access functions
51 static const char hist_cookie
[] = "_HiStOrY_V2_\n";
59 #define FUN(prefix, rest) prefix ## _ ## rest
60 #define FUNW(type) type
61 #define TYPE(type) type
64 #define Strlen(s) strlen(s)
65 #define Strdup(s) strdup(s)
66 #define Strcmp(d, s) strcmp(d, s)
67 #define Strncmp(d, s, n) strncmp(d, s, n)
68 #define Strncpy(d, s, n) strncpy(d, s, n)
69 #define Strncat(d, s, n) strncat(d, s, n)
70 #define ct_decode_string(s, b) (s)
71 #define ct_encode_string(s, b) (s)
77 #define FUN(prefix, rest) prefix ## _w ## rest
78 #define FUNW(type) type ## _w
79 #define TYPE(type) type ## W
82 #define Strlen(s) wcslen(s)
83 #define Strdup(s) wcsdup(s)
84 #define Strcmp(d, s) wcscmp(d, s)
85 #define Strncmp(d, s, n) wcsncmp(d, s, n)
86 #define Strncpy(d, s, n) wcsncpy(d, s, n)
87 #define Strncat(d, s, n) wcsncat(d, s, n)
92 typedef int (*history_gfun_t
)(void *, TYPE(HistEvent
) *);
93 typedef int (*history_efun_t
)(void *, TYPE(HistEvent
) *, const Char
*);
94 typedef void (*history_vfun_t
)(void *, TYPE(HistEvent
) *);
95 typedef int (*history_sfun_t
)(void *, TYPE(HistEvent
) *, const int);
97 struct TYPE(history
) {
98 void *h_ref
; /* Argument for history fcns */
99 int h_ent
; /* Last entry point for history */
100 history_gfun_t h_first
; /* Get the first element */
101 history_gfun_t h_next
; /* Get the next element */
102 history_gfun_t h_last
; /* Get the last element */
103 history_gfun_t h_prev
; /* Get the previous element */
104 history_gfun_t h_curr
; /* Get the current element */
105 history_sfun_t h_set
; /* Set the current element */
106 history_sfun_t h_del
; /* Set the given element */
107 history_vfun_t h_clear
; /* Clear the history list */
108 history_efun_t h_enter
; /* Add an element */
109 history_efun_t h_add
; /* Append to an element */
112 #define HNEXT(h, ev) (*(h)->h_next)((h)->h_ref, ev)
113 #define HFIRST(h, ev) (*(h)->h_first)((h)->h_ref, ev)
114 #define HPREV(h, ev) (*(h)->h_prev)((h)->h_ref, ev)
115 #define HLAST(h, ev) (*(h)->h_last)((h)->h_ref, ev)
116 #define HCURR(h, ev) (*(h)->h_curr)((h)->h_ref, ev)
117 #define HSET(h, ev, n) (*(h)->h_set)((h)->h_ref, ev, n)
118 #define HCLEAR(h, ev) (*(h)->h_clear)((h)->h_ref, ev)
119 #define HENTER(h, ev, str) (*(h)->h_enter)((h)->h_ref, ev, str)
120 #define HADD(h, ev, str) (*(h)->h_add)((h)->h_ref, ev, str)
121 #define HDEL(h, ev, n) (*(h)->h_del)((h)->h_ref, ev, n)
123 #define h_strdup(a) Strdup(a)
131 static int history_setsize(TYPE(History
) *, TYPE(HistEvent
) *, int);
132 static int history_getsize(TYPE(History
) *, TYPE(HistEvent
) *);
133 static int history_setunique(TYPE(History
) *, TYPE(HistEvent
) *, int);
134 static int history_getunique(TYPE(History
) *, TYPE(HistEvent
) *);
135 static int history_set_fun(TYPE(History
) *, TYPE(History
) *);
136 static int history_load(TYPE(History
) *, const char *);
137 static int history_save(TYPE(History
) *, const char *);
138 static int history_save_fp(TYPE(History
) *, FILE *);
139 static int history_prev_event(TYPE(History
) *, TYPE(HistEvent
) *, int);
140 static int history_next_event(TYPE(History
) *, TYPE(HistEvent
) *, int);
141 static int history_next_string(TYPE(History
) *, TYPE(HistEvent
) *,
143 static int history_prev_string(TYPE(History
) *, TYPE(HistEvent
) *,
147 /***********************************************************************/
150 * Builtin- history implementation
152 typedef struct hentry_t
{
153 TYPE(HistEvent
) ev
; /* What we return */
154 void *data
; /* data */
155 struct hentry_t
*next
; /* Next entry */
156 struct hentry_t
*prev
; /* Previous entry */
159 typedef struct history_t
{
160 hentry_t list
; /* Fake list header element */
161 hentry_t
*cursor
; /* Current element in the list */
162 int max
; /* Maximum number of events */
163 int cur
; /* Current number of events */
164 int eventid
; /* For generation of unique event id */
165 int flags
; /* TYPE(History) flags */
166 #define H_UNIQUE 1 /* Store only unique elements */
169 static int history_def_next(void *, TYPE(HistEvent
) *);
170 static int history_def_first(void *, TYPE(HistEvent
) *);
171 static int history_def_prev(void *, TYPE(HistEvent
) *);
172 static int history_def_last(void *, TYPE(HistEvent
) *);
173 static int history_def_curr(void *, TYPE(HistEvent
) *);
174 static int history_def_set(void *, TYPE(HistEvent
) *, const int);
175 static void history_def_clear(void *, TYPE(HistEvent
) *);
176 static int history_def_enter(void *, TYPE(HistEvent
) *, const Char
*);
177 static int history_def_add(void *, TYPE(HistEvent
) *, const Char
*);
178 static int history_def_del(void *, TYPE(HistEvent
) *, const int);
180 static int history_def_init(void **, TYPE(HistEvent
) *, int);
181 static int history_def_insert(history_t
*, TYPE(HistEvent
) *, const Char
*);
182 static void history_def_delete(history_t
*, TYPE(HistEvent
) *, hentry_t
*);
184 static int history_deldata_nth(history_t
*, TYPE(HistEvent
) *, int, void **);
185 static int history_set_nth(void *, TYPE(HistEvent
) *, int);
187 #define history_def_setsize(p, num)(void) (((history_t *)p)->max = (num))
188 #define history_def_getsize(p) (((history_t *)p)->cur)
189 #define history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0)
190 #define history_def_setunique(p, uni) \
192 (((history_t *)p)->flags) |= H_UNIQUE; \
194 (((history_t *)p)->flags) &= ~H_UNIQUE
196 #define he_strerror(code) he_errlist[code]
197 #define he_seterrev(evp, code) {\
199 evp->str = he_strerror(code);\
203 static const Char
*const he_errlist
[] = {
205 STR("unknown error"),
206 STR("malloc() failed"),
207 STR("first event not found"),
208 STR("last event not found"),
210 STR("no next event"),
211 STR("no previous event"),
212 STR("current event is invalid"),
213 STR("event not found"),
214 STR("can't read history from file"),
215 STR("can't write history"),
216 STR("required parameter(s) not supplied"),
217 STR("history size negative"),
218 STR("function not allowed with other history-functions-set the default"),
219 STR("bad parameters")
223 #define _HE_UNKNOWN 1
224 #define _HE_MALLOC_FAILED 2
225 #define _HE_FIRST_NOTFOUND 3
226 #define _HE_LAST_NOTFOUND 4
227 #define _HE_EMPTY_LIST 5
228 #define _HE_END_REACHED 6
229 #define _HE_START_REACHED 7
230 #define _HE_CURR_INVALID 8
231 #define _HE_NOT_FOUND 9
232 #define _HE_HIST_READ 10
233 #define _HE_HIST_WRITE 11
234 #define _HE_PARAM_MISSING 12
235 #define _HE_SIZE_NEGATIVE 13
236 #define _HE_NOT_ALLOWED 14
237 #define _HE_BAD_PARAM 15
239 /* history_def_first():
240 * Default function to return the first event in the history.
243 history_def_first(void *p
, TYPE(HistEvent
) *ev
)
245 history_t
*h
= (history_t
*) p
;
247 h
->cursor
= h
->list
.next
;
248 if (h
->cursor
!= &h
->list
)
251 he_seterrev(ev
, _HE_FIRST_NOTFOUND
);
259 /* history_def_last():
260 * Default function to return the last event in the history.
263 history_def_last(void *p
, TYPE(HistEvent
) *ev
)
265 history_t
*h
= (history_t
*) p
;
267 h
->cursor
= h
->list
.prev
;
268 if (h
->cursor
!= &h
->list
)
271 he_seterrev(ev
, _HE_LAST_NOTFOUND
);
279 /* history_def_next():
280 * Default function to return the next event in the history.
283 history_def_next(void *p
, TYPE(HistEvent
) *ev
)
285 history_t
*h
= (history_t
*) p
;
287 if (h
->cursor
== &h
->list
) {
288 he_seterrev(ev
, _HE_EMPTY_LIST
);
292 if (h
->cursor
->next
== &h
->list
) {
293 he_seterrev(ev
, _HE_END_REACHED
);
297 h
->cursor
= h
->cursor
->next
;
304 /* history_def_prev():
305 * Default function to return the previous event in the history.
308 history_def_prev(void *p
, TYPE(HistEvent
) *ev
)
310 history_t
*h
= (history_t
*) p
;
312 if (h
->cursor
== &h
->list
) {
314 (h
->cur
> 0) ? _HE_END_REACHED
: _HE_EMPTY_LIST
);
318 if (h
->cursor
->prev
== &h
->list
) {
319 he_seterrev(ev
, _HE_START_REACHED
);
323 h
->cursor
= h
->cursor
->prev
;
330 /* history_def_curr():
331 * Default function to return the current event in the history.
334 history_def_curr(void *p
, TYPE(HistEvent
) *ev
)
336 history_t
*h
= (history_t
*) p
;
338 if (h
->cursor
!= &h
->list
)
342 (h
->cur
> 0) ? _HE_CURR_INVALID
: _HE_EMPTY_LIST
);
350 /* history_def_set():
351 * Default function to set the current event in the history to the
355 history_def_set(void *p
, TYPE(HistEvent
) *ev
, const int n
)
357 history_t
*h
= (history_t
*) p
;
360 he_seterrev(ev
, _HE_EMPTY_LIST
);
363 if (h
->cursor
== &h
->list
|| h
->cursor
->ev
.num
!= n
) {
364 for (h
->cursor
= h
->list
.next
; h
->cursor
!= &h
->list
;
365 h
->cursor
= h
->cursor
->next
)
366 if (h
->cursor
->ev
.num
== n
)
369 if (h
->cursor
== &h
->list
) {
370 he_seterrev(ev
, _HE_NOT_FOUND
);
377 /* history_set_nth():
378 * Default function to set the current event in the history to the
382 history_set_nth(void *p
, TYPE(HistEvent
) *ev
, int n
)
384 history_t
*h
= (history_t
*) p
;
387 he_seterrev(ev
, _HE_EMPTY_LIST
);
390 for (h
->cursor
= h
->list
.prev
; h
->cursor
!= &h
->list
;
391 h
->cursor
= h
->cursor
->prev
)
394 if (h
->cursor
== &h
->list
) {
395 he_seterrev(ev
, _HE_NOT_FOUND
);
402 /* history_def_add():
403 * Append string to element
406 history_def_add(void *p
, TYPE(HistEvent
) *ev
, const Char
*str
)
408 history_t
*h
= (history_t
*) p
;
411 HistEventPrivate
*evp
= (void *)&h
->cursor
->ev
;
413 if (h
->cursor
== &h
->list
)
414 return history_def_enter(p
, ev
, str
);
415 len
= Strlen(evp
->str
) + Strlen(str
) + 1;
416 s
= reallocarray(NULL
, len
, sizeof(*s
));
418 he_seterrev(ev
, _HE_MALLOC_FAILED
);
421 (void) Strncpy(s
, h
->cursor
->ev
.str
, len
);
423 (void) Strncat(s
, str
, len
- Strlen(s
) - 1);
432 history_deldata_nth(history_t
*h
, TYPE(HistEvent
) *ev
,
433 int num
, void **data
)
435 if (history_set_nth(h
, ev
, num
) != 0)
437 /* magic value to skip delete (just set to n-th history) */
438 if (data
== (void **)-1)
440 ev
->str
= Strdup(h
->cursor
->ev
.str
);
441 ev
->num
= h
->cursor
->ev
.num
;
443 *data
= h
->cursor
->data
;
444 history_def_delete(h
, ev
, h
->cursor
);
449 /* history_def_del():
450 * Delete element hp of the h list
454 history_def_del(void *p
, TYPE(HistEvent
) *ev
__attribute__((__unused__
)),
457 history_t
*h
= (history_t
*) p
;
458 if (history_def_set(h
, ev
, num
) != 0)
460 ev
->str
= Strdup(h
->cursor
->ev
.str
);
461 ev
->num
= h
->cursor
->ev
.num
;
462 history_def_delete(h
, ev
, h
->cursor
);
467 /* history_def_delete():
468 * Delete element hp of the h list
472 history_def_delete(history_t
*h
,
473 TYPE(HistEvent
) *ev
__attribute__((__unused__
)), hentry_t
*hp
)
475 HistEventPrivate
*evp
= (void *)&hp
->ev
;
478 if (h
->cursor
== hp
) {
479 h
->cursor
= hp
->prev
;
480 if (h
->cursor
== &h
->list
)
481 h
->cursor
= hp
->next
;
483 hp
->prev
->next
= hp
->next
;
484 hp
->next
->prev
= hp
->prev
;
491 /* history_def_insert():
492 * Insert element with string str in the h list
495 history_def_insert(history_t
*h
, TYPE(HistEvent
) *ev
, const Char
*str
)
498 h
->cursor
= (hentry_t
*) malloc(sizeof(hentry_t
));
499 if (h
->cursor
== NULL
)
501 if ((h
->cursor
->ev
.str
= h_strdup(str
)) == NULL
) {
505 h
->cursor
->data
= NULL
;
506 h
->cursor
->ev
.num
= ++h
->eventid
;
507 h
->cursor
->next
= h
->list
.next
;
508 h
->cursor
->prev
= &h
->list
;
509 h
->list
.next
->prev
= h
->cursor
;
510 h
->list
.next
= h
->cursor
;
516 he_seterrev(ev
, _HE_MALLOC_FAILED
);
521 /* history_def_enter():
522 * Default function to enter an item in the history
525 history_def_enter(void *p
, TYPE(HistEvent
) *ev
, const Char
*str
)
527 history_t
*h
= (history_t
*) p
;
529 if ((h
->flags
& H_UNIQUE
) != 0 && h
->list
.next
!= &h
->list
&&
530 Strcmp(h
->list
.next
->ev
.str
, str
) == 0)
533 if (history_def_insert(h
, ev
, str
) == -1)
534 return -1; /* error, keep error message */
537 * Always keep at least one entry.
538 * This way we don't have to check for the empty list.
540 while (h
->cur
> h
->max
&& h
->cur
> 0)
541 history_def_delete(h
, ev
, h
->list
.prev
);
547 /* history_def_init():
548 * Default history initialization function
552 history_def_init(void **p
, TYPE(HistEvent
) *ev
__attribute__((__unused__
)), int n
)
554 history_t
*h
= (history_t
*) malloc(sizeof(history_t
));
563 h
->list
.next
= h
->list
.prev
= &h
->list
;
564 h
->list
.ev
.str
= NULL
;
566 h
->cursor
= &h
->list
;
573 /* history_def_clear():
574 * Default history cleanup function
577 history_def_clear(void *p
, TYPE(HistEvent
) *ev
)
579 history_t
*h
= (history_t
*) p
;
581 while (h
->list
.prev
!= &h
->list
)
582 history_def_delete(h
, ev
, h
->list
.prev
);
590 /************************************************************************/
593 * Initialization function.
596 FUN(history
,init
)(void)
599 TYPE(History
) *h
= (TYPE(History
) *) malloc(sizeof(TYPE(History
)));
603 if (history_def_init(&h
->h_ref
, &ev
, 0) == -1) {
608 h
->h_next
= history_def_next
;
609 h
->h_first
= history_def_first
;
610 h
->h_last
= history_def_last
;
611 h
->h_prev
= history_def_prev
;
612 h
->h_curr
= history_def_curr
;
613 h
->h_set
= history_def_set
;
614 h
->h_clear
= history_def_clear
;
615 h
->h_enter
= history_def_enter
;
616 h
->h_add
= history_def_add
;
617 h
->h_del
= history_def_del
;
627 FUN(history
,end
)(TYPE(History
) *h
)
631 if (h
->h_next
== history_def_next
)
632 history_def_clear(h
->h_ref
, &ev
);
639 /* history_setsize():
640 * Set history number of events
643 history_setsize(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, int num
)
646 if (h
->h_next
!= history_def_next
) {
647 he_seterrev(ev
, _HE_NOT_ALLOWED
);
651 he_seterrev(ev
, _HE_BAD_PARAM
);
654 history_def_setsize(h
->h_ref
, num
);
659 /* history_getsize():
660 * Get number of events currently in history
663 history_getsize(TYPE(History
) *h
, TYPE(HistEvent
) *ev
)
665 if (h
->h_next
!= history_def_next
) {
666 he_seterrev(ev
, _HE_NOT_ALLOWED
);
669 ev
->num
= history_def_getsize(h
->h_ref
);
671 he_seterrev(ev
, _HE_SIZE_NEGATIVE
);
678 /* history_setunique():
679 * Set if adjacent equal events should not be entered in history.
682 history_setunique(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, int uni
)
685 if (h
->h_next
!= history_def_next
) {
686 he_seterrev(ev
, _HE_NOT_ALLOWED
);
689 history_def_setunique(h
->h_ref
, uni
);
694 /* history_getunique():
695 * Get if adjacent equal events should not be entered in history.
698 history_getunique(TYPE(History
) *h
, TYPE(HistEvent
) *ev
)
700 if (h
->h_next
!= history_def_next
) {
701 he_seterrev(ev
, _HE_NOT_ALLOWED
);
704 ev
->num
= history_def_getunique(h
->h_ref
);
709 /* history_set_fun():
710 * Set history functions
713 history_set_fun(TYPE(History
) *h
, TYPE(History
) *nh
)
717 if (nh
->h_first
== NULL
|| nh
->h_next
== NULL
|| nh
->h_last
== NULL
||
718 nh
->h_prev
== NULL
|| nh
->h_curr
== NULL
|| nh
->h_set
== NULL
||
719 nh
->h_enter
== NULL
|| nh
->h_add
== NULL
|| nh
->h_clear
== NULL
||
720 nh
->h_del
== NULL
|| nh
->h_ref
== NULL
) {
721 if (h
->h_next
!= history_def_next
) {
722 history_def_init(&h
->h_ref
, &ev
, 0);
723 h
->h_first
= history_def_first
;
724 h
->h_next
= history_def_next
;
725 h
->h_last
= history_def_last
;
726 h
->h_prev
= history_def_prev
;
727 h
->h_curr
= history_def_curr
;
728 h
->h_set
= history_def_set
;
729 h
->h_clear
= history_def_clear
;
730 h
->h_enter
= history_def_enter
;
731 h
->h_add
= history_def_add
;
732 h
->h_del
= history_def_del
;
736 if (h
->h_next
== history_def_next
)
737 history_def_clear(h
->h_ref
, &ev
);
740 h
->h_first
= nh
->h_first
;
741 h
->h_next
= nh
->h_next
;
742 h
->h_last
= nh
->h_last
;
743 h
->h_prev
= nh
->h_prev
;
744 h
->h_curr
= nh
->h_curr
;
745 h
->h_set
= nh
->h_set
;
746 h
->h_clear
= nh
->h_clear
;
747 h
->h_enter
= nh
->h_enter
;
748 h
->h_add
= nh
->h_add
;
749 h
->h_del
= nh
->h_del
;
756 * TYPE(History) load function
759 history_load(TYPE(History
) *h
, const char *fname
)
770 static ct_buffer_t conv
;
773 if ((fp
= fopen(fname
, "r")) == NULL
)
778 if ((sz
= getline(&line
, &llen
, fp
)) == -1)
781 if (strncmp(line
, hist_cookie
, sz
) != 0)
784 ptr
= malloc(max_size
= 1024);
787 for (i
= 0; (sz
= getline(&line
, &llen
, fp
)) != -1; i
++) {
788 if (sz
> 0 && line
[sz
- 1] == '\n')
792 max_size
= (sz
+ 1024) & ~1023;
793 nptr
= realloc(ptr
, max_size
);
800 (void) strunvis(ptr
, line
);
801 if (HENTER(h
, &ev
, ct_decode_string(ptr
, &conv
)) == -1) {
815 /* history_save_fp():
816 * TYPE(History) save function
819 history_save_fp(TYPE(History
) *h
, FILE *fp
)
823 size_t len
, max_size
;
826 static ct_buffer_t conv
;
829 if (fchmod(fileno(fp
), S_IRUSR
|S_IWUSR
) == -1)
831 if (fputs(hist_cookie
, fp
) == EOF
)
833 ptr
= malloc(max_size
= 1024);
836 for (i
= 0, retval
= HLAST(h
, &ev
);
838 retval
= HPREV(h
, &ev
), i
++) {
839 len
= Strlen(ev
.str
) * 4 + 1;
840 if (len
> max_size
) {
842 max_size
= (len
+ 1024) & ~1023;
843 nptr
= realloc(ptr
, max_size
);
850 (void) strnvis(ptr
, ct_encode_string(ev
.str
, &conv
), max_size
,
852 (void) fprintf(fp
, "%s\n", ptr
);
862 * History save function
865 history_save(TYPE(History
) *h
, const char *fname
)
870 if ((fp
= fopen(fname
, "w")) == NULL
)
873 i
= history_save_fp(h
, fp
);
880 /* history_prev_event():
881 * Find the previous event, with number given
884 history_prev_event(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, int num
)
888 for (retval
= HCURR(h
, ev
); retval
!= -1; retval
= HPREV(h
, ev
))
892 he_seterrev(ev
, _HE_NOT_FOUND
);
898 history_next_evdata(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, int num
, void **d
)
902 for (retval
= HCURR(h
, ev
); retval
!= -1; retval
= HPREV(h
, ev
))
903 if (ev
->num
== num
) {
905 *d
= ((history_t
*)h
->h_ref
)->cursor
->data
;
909 he_seterrev(ev
, _HE_NOT_FOUND
);
914 /* history_next_event():
915 * Find the next event, with number given
918 history_next_event(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, int num
)
922 for (retval
= HCURR(h
, ev
); retval
!= -1; retval
= HNEXT(h
, ev
))
926 he_seterrev(ev
, _HE_NOT_FOUND
);
931 /* history_prev_string():
932 * Find the previous event beginning with string
935 history_prev_string(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, const Char
*str
)
937 size_t len
= Strlen(str
);
940 for (retval
= HCURR(h
, ev
); retval
!= -1; retval
= HNEXT(h
, ev
))
941 if (Strncmp(str
, ev
->str
, len
) == 0)
944 he_seterrev(ev
, _HE_NOT_FOUND
);
949 /* history_next_string():
950 * Find the next event beginning with string
953 history_next_string(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, const Char
*str
)
955 size_t len
= Strlen(str
);
958 for (retval
= HCURR(h
, ev
); retval
!= -1; retval
= HPREV(h
, ev
))
959 if (Strncmp(str
, ev
->str
, len
) == 0)
962 he_seterrev(ev
, _HE_NOT_FOUND
);
968 * User interface to history functions.
971 FUNW(history
)(TYPE(History
) *h
, TYPE(HistEvent
) *ev
, int fun
, ...)
979 he_seterrev(ev
, _HE_OK
);
983 retval
= history_getsize(h
, ev
);
987 retval
= history_setsize(h
, ev
, va_arg(va
, int));
991 retval
= history_getunique(h
, ev
);
995 retval
= history_setunique(h
, ev
, va_arg(va
, int));
999 str
= va_arg(va
, const Char
*);
1000 retval
= HADD(h
, ev
, str
);
1004 retval
= HDEL(h
, ev
, va_arg(va
, const int));
1008 str
= va_arg(va
, const Char
*);
1009 if ((retval
= HENTER(h
, ev
, str
)) != -1)
1014 str
= va_arg(va
, const Char
*);
1015 if ((retval
= HSET(h
, ev
, h
->h_ent
)) != -1)
1016 retval
= HADD(h
, ev
, str
);
1020 retval
= HFIRST(h
, ev
);
1024 retval
= HNEXT(h
, ev
);
1028 retval
= HLAST(h
, ev
);
1032 retval
= HPREV(h
, ev
);
1036 retval
= HCURR(h
, ev
);
1040 retval
= HSET(h
, ev
, va_arg(va
, const int));
1049 retval
= history_load(h
, va_arg(va
, const char *));
1051 he_seterrev(ev
, _HE_HIST_READ
);
1055 retval
= history_save(h
, va_arg(va
, const char *));
1057 he_seterrev(ev
, _HE_HIST_WRITE
);
1061 retval
= history_save_fp(h
, va_arg(va
, FILE *));
1063 he_seterrev(ev
, _HE_HIST_WRITE
);
1067 retval
= history_prev_event(h
, ev
, va_arg(va
, int));
1071 retval
= history_next_event(h
, ev
, va_arg(va
, int));
1075 retval
= history_prev_string(h
, ev
, va_arg(va
, const Char
*));
1079 retval
= history_next_string(h
, ev
, va_arg(va
, const Char
*));
1086 hf
.h_ref
= va_arg(va
, void *);
1088 hf
.h_first
= va_arg(va
, history_gfun_t
);
1089 hf
.h_next
= va_arg(va
, history_gfun_t
);
1090 hf
.h_last
= va_arg(va
, history_gfun_t
);
1091 hf
.h_prev
= va_arg(va
, history_gfun_t
);
1092 hf
.h_curr
= va_arg(va
, history_gfun_t
);
1093 hf
.h_set
= va_arg(va
, history_sfun_t
);
1094 hf
.h_clear
= va_arg(va
, history_vfun_t
);
1095 hf
.h_enter
= va_arg(va
, history_efun_t
);
1096 hf
.h_add
= va_arg(va
, history_efun_t
);
1097 hf
.h_del
= va_arg(va
, history_sfun_t
);
1099 if ((retval
= history_set_fun(h
, &hf
)) == -1)
1100 he_seterrev(ev
, _HE_PARAM_MISSING
);
1105 FUN(history
,end
)(h
);
1111 int num
= va_arg(va
, int);
1112 void **d
= va_arg(va
, void **);
1113 retval
= history_next_evdata(h
, ev
, num
, d
);
1119 int num
= va_arg(va
, int);
1120 void **d
= va_arg(va
, void **);
1121 retval
= history_deldata_nth((history_t
*)h
->h_ref
, ev
, num
, d
);
1125 case H_REPLACE
: /* only use after H_NEXT_EVDATA */
1127 const Char
*line
= va_arg(va
, const Char
*);
1128 void *d
= va_arg(va
, void *);
1130 if(!line
|| !(s
= Strdup(line
))) {
1134 ((history_t
*)h
->h_ref
)->cursor
->ev
.str
= s
;
1135 ((history_t
*)h
->h_ref
)->cursor
->data
= d
;
1142 he_seterrev(ev
, _HE_UNKNOWN
);