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