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