2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)history.c 8.1 (Berkeley) 6/4/93
40 * hist.c: History access functions
52 static const char hist_cookie[] = "_HiStOrY_V1_\n";
56 typedef const HistEvent * (*history_gfun_t) __P((ptr_t));
57 typedef const HistEvent * (*history_efun_t) __P((ptr_t, const char *));
58 typedef void (*history_vfun_t) __P((ptr_t));
61 ptr_t h_ref; /* Argument for history fcns */
62 history_gfun_t h_first; /* Get the first element */
63 history_gfun_t h_next; /* Get the next element */
64 history_gfun_t h_last; /* Get the last element */
65 history_gfun_t h_prev; /* Get the previous element */
66 history_gfun_t h_curr; /* Get the current element */
67 history_vfun_t h_clear; /* Clear the history list */
68 history_efun_t h_enter; /* Add an element */
69 history_efun_t h_add; /* Append to an element */
72 #define HNEXT(h) (*(h)->h_next)((h)->h_ref)
73 #define HFIRST(h) (*(h)->h_first)((h)->h_ref)
74 #define HPREV(h) (*(h)->h_prev)((h)->h_ref)
75 #define HLAST(h) (*(h)->h_last)((h)->h_ref)
76 #define HCURR(h) (*(h)->h_curr)((h)->h_ref)
77 #define HCLEAR(h) (*(h)->h_clear)((h)->h_ref)
78 #define HENTER(h, str) (*(h)->h_enter)((h)->h_ref, str)
79 #define HADD(h, str) (*(h)->h_add)((h)->h_ref, str)
81 #define h_malloc(a) malloc(a)
82 #define h_free(a) free(a)
85 private int history_set_num __P((History *, int));
86 private int history_set_fun __P((History *, History *));
87 private int history_load __P((History *, const char *));
88 private int history_save __P((History *, const char *));
89 private const HistEvent *history_prev_event __P((History *, int));
90 private const HistEvent *history_next_event __P((History *, int));
91 private const HistEvent *history_next_string __P((History *, const char *));
92 private const HistEvent *history_prev_string __P((History *, const char *));
95 /***********************************************************************/
98 * Builtin- history implementation
100 typedef struct hentry_t {
101 HistEvent ev; /* What we return */
102 struct hentry_t *next; /* Next entry */
103 struct hentry_t *prev; /* Previous entry */
106 typedef struct history_t {
107 hentry_t list; /* Fake list header element */
108 hentry_t *cursor; /* Current element in the list */
109 int max; /* Maximum number of events */
110 int cur; /* Current number of events */
111 int eventno; /* Current event number */
114 private const HistEvent *history_def_first __P((ptr_t));
115 private const HistEvent *history_def_last __P((ptr_t));
116 private const HistEvent *history_def_next __P((ptr_t));
117 private const HistEvent *history_def_prev __P((ptr_t));
118 private const HistEvent *history_def_curr __P((ptr_t));
119 private const HistEvent *history_def_enter __P((ptr_t, const char *));
120 private const HistEvent *history_def_add __P((ptr_t, const char *));
121 private void history_def_init __P((ptr_t *, int));
122 private void history_def_clear __P((ptr_t));
123 private const HistEvent *history_def_insert __P((history_t *, const char *));
124 private void history_def_delete __P((history_t *, hentry_t *));
126 #define history_def_set(p, num) (void) (((history_t *) p)->max = (num))
129 /* history_def_first():
130 * Default function to return the first event in the history.
132 private const HistEvent *
136 history_t *h = (history_t *) p;
137 h->cursor = h->list.next;
138 if (h->cursor != &h->list)
139 return &h->cursor->ev;
144 /* history_def_last():
145 * Default function to return the last event in the history.
147 private const HistEvent *
151 history_t *h = (history_t *) p;
152 h->cursor = h->list.prev;
153 if (h->cursor != &h->list)
154 return &h->cursor->ev;
159 /* history_def_next():
160 * Default function to return the next event in the history.
162 private const HistEvent *
166 history_t *h = (history_t *) p;
168 if (h->cursor != &h->list)
169 h->cursor = h->cursor->next;
173 if (h->cursor != &h->list)
174 return &h->cursor->ev;
180 /* history_def_prev():
181 * Default function to return the previous event in the history.
183 private const HistEvent *
187 history_t *h = (history_t *) p;
189 if (h->cursor != &h->list)
190 h->cursor = h->cursor->prev;
194 if (h->cursor != &h->list)
195 return &h->cursor->ev;
201 /* history_def_curr():
202 * Default function to return the current event in the history.
204 private const HistEvent *
208 history_t *h = (history_t *) p;
210 if (h->cursor != &h->list)
211 return &h->cursor->ev;
217 /* history_def_add():
218 * Append string to element
220 private const HistEvent *
221 history_def_add(p, str)
225 history_t *h = (history_t *) p;
229 if (h->cursor == &h->list)
230 return (history_def_enter(p, str));
231 len = strlen(h->cursor->ev.str) + strlen(str) + 1;
232 s = (char *) h_malloc(len);
233 (void)strcpy(s, h->cursor->ev.str); /* XXX strcpy is safe */
234 (void)strcat(s, str); /* XXX strcat is safe */
235 h_free((ptr_t) h->cursor->ev.str);
236 h->cursor->ev.str = s;
237 return &h->cursor->ev;
241 /* history_def_delete():
242 * Delete element hp of the h list
245 history_def_delete(h, hp)
251 hp->prev->next = hp->next;
252 hp->next->prev = hp->prev;
253 h_free((ptr_t) hp->ev.str);
259 /* history_def_insert():
260 * Insert element with string str in the h list
262 private const HistEvent *
263 history_def_insert(h, str)
267 h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
268 h->cursor->ev.str = strdup(str);
269 h->cursor->next = h->list.next;
270 h->cursor->prev = &h->list;
271 h->list.next->prev = h->cursor;
272 h->list.next = h->cursor;
275 return &h->cursor->ev;
279 /* history_def_enter():
280 * Default function to enter an item in the history
282 private const HistEvent *
283 history_def_enter(p, str)
287 history_t *h = (history_t *) p;
291 ev = history_def_insert(h, str);
292 ((HistEvent*) ev)->num = ++h->eventno;
295 * Always keep at least one entry.
296 * This way we don't have to check for the empty list.
298 while (h->cur > h->max + 1)
299 history_def_delete(h, h->list.prev);
304 /* history_def_init():
305 * Default history initialization function
308 history_def_init(p, n)
312 history_t *h = (history_t *) h_malloc(sizeof(history_t));
318 h->list.next = h->list.prev = &h->list;
319 h->list.ev.str = NULL;
321 h->cursor = &h->list;
326 /* history_def_clear():
327 * Default history cleanup function
333 history_t *h = (history_t *) p;
335 while (h->list.prev != &h->list)
336 history_def_delete(h, h->list.prev);
341 /************************************************************************/
344 * Initialization function.
349 History *h = (History *) h_malloc(sizeof(History));
351 history_def_init(&h->h_ref, 0);
353 h->h_next = history_def_next;
354 h->h_first = history_def_first;
355 h->h_last = history_def_last;
356 h->h_prev = history_def_prev;
357 h->h_curr = history_def_curr;
358 h->h_clear = history_def_clear;
359 h->h_enter = history_def_enter;
360 h->h_add = history_def_add;
373 if (h->h_next == history_def_next)
374 history_def_clear(h->h_ref);
379 /* history_set_num():
380 * Set history number of events
383 history_set_num(h, num)
387 if (h->h_next != history_def_next || num < 0)
389 history_def_set(h->h_ref, num);
394 /* history_set_fun():
395 * Set history functions
398 history_set_fun(h, nh)
401 if (nh->h_first == NULL || nh->h_next == NULL ||
402 nh->h_last == NULL || nh->h_prev == NULL || nh->h_curr == NULL ||
403 nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
405 if (h->h_next != history_def_next) {
406 history_def_init(&h->h_ref, 0);
407 h->h_first = history_def_first;
408 h->h_next = history_def_next;
409 h->h_last = history_def_last;
410 h->h_prev = history_def_prev;
411 h->h_curr = history_def_curr;
412 h->h_clear = history_def_clear;
413 h->h_enter = history_def_enter;
414 h->h_add = history_def_add;
419 if (h->h_next == history_def_next)
420 history_def_clear(h->h_ref);
422 h->h_first = nh->h_first;
423 h->h_next = nh->h_next;
424 h->h_last = nh->h_last;
425 h->h_prev = nh->h_prev;
426 h->h_curr = nh->h_curr;
427 h->h_clear = nh->h_clear;
428 h->h_enter = nh->h_enter;
429 h->h_add = nh->h_add;
435 * History load function
438 history_load(h, fname)
447 if ((fp = fopen(fname, "r")) == NULL)
450 if ((line = fgetln(fp, &sz)) == NULL)
453 if (strncmp(line, hist_cookie, sz) != 0)
456 for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
470 * History save function
473 history_save(h, fname)
481 if ((fp = fopen(fname, "w")) == NULL)
484 (void) fputs(hist_cookie, fp);
485 for (ev = HLAST(h); ev != NULL; ev = HPREV(h), i++)
486 (void) fprintf(fp, "%s", ev->str);
492 /* history_prev_event():
493 * Find the previous event, with number given
495 private const HistEvent *
496 history_prev_event(h, num)
501 for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
508 /* history_next_event():
509 * Find the next event, with number given
511 private const HistEvent *
512 history_next_event(h, num)
517 for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
524 /* history_prev_string():
525 * Find the previous event beginning with string
527 private const HistEvent *
528 history_prev_string(h, str)
533 size_t len = strlen(str);
535 for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
536 if (strncmp(str, ev->str, len) == 0)
542 /* history_next_string():
543 * Find the next event beginning with string
545 private const HistEvent *
546 history_next_string(h, str)
551 size_t len = strlen(str);
553 for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
554 if (strncmp(str, ev->str, len) == 0)
561 * User interface to history functions.
565 history(History *h, int fun, ...)
572 const HistEvent *ev = NULL;
574 static HistEvent sev = { 0, "" };
582 h = va_arg(va, History *);
583 fun = va_arg(va, int);
588 str = va_arg(va, const char *);
593 str = va_arg(va, const char *);
622 sev.num = history_load(h, va_arg(va, const char *));
627 sev.num = history_save(h, va_arg(va, const char *));
632 ev = history_prev_event(h, va_arg(va, int));
636 ev = history_next_event(h, va_arg(va, int));
640 ev = history_prev_string(h, va_arg(va, const char*));
644 ev = history_next_string(h, va_arg(va, const char*));
648 if (history_set_num(h, va_arg(va, int)) == 0) {
657 hf.h_ref = va_arg(va, ptr_t);
658 hf.h_first = va_arg(va, history_gfun_t);
659 hf.h_next = va_arg(va, history_gfun_t);
660 hf.h_last = va_arg(va, history_gfun_t);
661 hf.h_prev = va_arg(va, history_gfun_t);
662 hf.h_curr = va_arg(va, history_gfun_t);
663 hf.h_clear = va_arg(va, history_vfun_t);
664 hf.h_enter = va_arg(va, history_efun_t);
665 hf.h_add = va_arg(va, history_efun_t);
667 if (history_set_fun(h, &hf) == 0) {