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
37 * $DragonFly: src/lib/libedit/history.c,v 1.5 2004/07/27 11:22:34 asmodai Exp $
41 * hist.c: History access functions
49 static const char hist_cookie[] = "_HiStOrY_V1_\n";
53 typedef const HistEvent * (*history_gfun_t) (ptr_t);
54 typedef const HistEvent * (*history_efun_t) (ptr_t, const char *);
55 typedef void (*history_vfun_t) (ptr_t);
58 ptr_t h_ref; /* Argument for history fcns */
59 history_gfun_t h_first; /* Get the first element */
60 history_gfun_t h_next; /* Get the next element */
61 history_gfun_t h_last; /* Get the last element */
62 history_gfun_t h_prev; /* Get the previous element */
63 history_gfun_t h_curr; /* Get the current element */
64 history_vfun_t h_clear; /* Clear the history list */
65 history_efun_t h_enter; /* Add an element */
66 history_efun_t h_add; /* Append to an element */
69 #define HNEXT(h) (*(h)->h_next)((h)->h_ref)
70 #define HFIRST(h) (*(h)->h_first)((h)->h_ref)
71 #define HPREV(h) (*(h)->h_prev)((h)->h_ref)
72 #define HLAST(h) (*(h)->h_last)((h)->h_ref)
73 #define HCURR(h) (*(h)->h_curr)((h)->h_ref)
74 #define HCLEAR(h) (*(h)->h_clear)((h)->h_ref)
75 #define HENTER(h, str) (*(h)->h_enter)((h)->h_ref, str)
76 #define HADD(h, str) (*(h)->h_add)((h)->h_ref, str)
78 #define h_malloc(a) malloc(a)
79 #define h_free(a) free(a)
82 private int history_set_num (History *, int);
83 private int history_set_fun (History *, History *);
84 private int history_load (History *, const char *);
85 private int history_save (History *, const char *);
86 private const HistEvent *history_prev_event (History *, int);
87 private const HistEvent *history_next_event (History *, int);
88 private const HistEvent *history_next_string (History *, const char *);
89 private const HistEvent *history_prev_string (History *, const char *);
92 /***********************************************************************/
95 * Builtin- history implementation
97 typedef struct hentry_t {
98 HistEvent ev; /* What we return */
99 struct hentry_t *next; /* Next entry */
100 struct hentry_t *prev; /* Previous entry */
103 typedef struct history_t {
104 hentry_t list; /* Fake list header element */
105 hentry_t *cursor; /* Current element in the list */
106 int max; /* Maximum number of events */
107 int cur; /* Current number of events */
108 int eventno; /* Current event number */
111 private const HistEvent *history_def_first (ptr_t);
112 private const HistEvent *history_def_last (ptr_t);
113 private const HistEvent *history_def_next (ptr_t);
114 private const HistEvent *history_def_prev (ptr_t);
115 private const HistEvent *history_def_curr (ptr_t);
116 private const HistEvent *history_def_enter (ptr_t, const char *);
117 private const HistEvent *history_def_add (ptr_t, const char *);
118 private void history_def_init (ptr_t *, int);
119 private void history_def_clear (ptr_t);
120 private const HistEvent *history_def_insert (history_t *, const char *);
121 private void history_def_delete (history_t *, hentry_t *);
123 #define history_def_set(p, num) (void) (((history_t *) p)->max = (num))
126 /* history_def_first():
127 * Default function to return the first event in the history.
129 private const HistEvent *
133 history_t *h = (history_t *) p;
134 h->cursor = h->list.next;
135 if (h->cursor != &h->list)
136 return &h->cursor->ev;
141 /* history_def_last():
142 * Default function to return the last event in the history.
144 private const HistEvent *
148 history_t *h = (history_t *) p;
149 h->cursor = h->list.prev;
150 if (h->cursor != &h->list)
151 return &h->cursor->ev;
156 /* history_def_next():
157 * Default function to return the next event in the history.
159 private const HistEvent *
163 history_t *h = (history_t *) p;
165 if (h->cursor != &h->list)
166 h->cursor = h->cursor->next;
170 if (h->cursor != &h->list)
171 return &h->cursor->ev;
177 /* history_def_prev():
178 * Default function to return the previous event in the history.
180 private const HistEvent *
184 history_t *h = (history_t *) p;
186 if (h->cursor != &h->list)
187 h->cursor = h->cursor->prev;
191 if (h->cursor != &h->list)
192 return &h->cursor->ev;
198 /* history_def_curr():
199 * Default function to return the current event in the history.
201 private const HistEvent *
205 history_t *h = (history_t *) p;
207 if (h->cursor != &h->list)
208 return &h->cursor->ev;
214 /* history_def_add():
215 * Append string to element
217 private const HistEvent *
218 history_def_add(p, str)
222 history_t *h = (history_t *) p;
226 if (h->cursor == &h->list)
227 return (history_def_enter(p, str));
228 len = strlen(h->cursor->ev.str) + strlen(str) + 1;
229 s = (char *) h_malloc(len);
230 (void)strcpy(s, h->cursor->ev.str); /* XXX strcpy is safe */
231 (void)strcat(s, str); /* XXX strcat is safe */
232 h_free((ptr_t) h->cursor->ev.str);
233 h->cursor->ev.str = s;
234 return &h->cursor->ev;
238 /* history_def_delete():
239 * Delete element hp of the h list
242 history_def_delete(h, hp)
248 hp->prev->next = hp->next;
249 hp->next->prev = hp->prev;
250 h_free((ptr_t) hp->ev.str);
256 /* history_def_insert():
257 * Insert element with string str in the h list
259 private const HistEvent *
260 history_def_insert(h, str)
264 h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
265 h->cursor->ev.str = strdup(str);
266 h->cursor->next = h->list.next;
267 h->cursor->prev = &h->list;
268 h->list.next->prev = h->cursor;
269 h->list.next = h->cursor;
272 return &h->cursor->ev;
276 /* history_def_enter():
277 * Default function to enter an item in the history
279 private const HistEvent *
280 history_def_enter(p, str)
284 history_t *h = (history_t *) p;
288 ev = history_def_insert(h, str);
289 ((HistEvent*) ev)->num = ++h->eventno;
292 * Always keep at least one entry.
293 * This way we don't have to check for the empty list.
295 while (h->cur > h->max + 1)
296 history_def_delete(h, h->list.prev);
301 /* history_def_init():
302 * Default history initialization function
305 history_def_init(p, n)
309 history_t *h = (history_t *) h_malloc(sizeof(history_t));
315 h->list.next = h->list.prev = &h->list;
316 h->list.ev.str = NULL;
318 h->cursor = &h->list;
323 /* history_def_clear():
324 * Default history cleanup function
330 history_t *h = (history_t *) p;
332 while (h->list.prev != &h->list)
333 history_def_delete(h, h->list.prev);
338 /************************************************************************/
341 * Initialization function.
346 History *h = (History *) h_malloc(sizeof(History));
348 history_def_init(&h->h_ref, 0);
350 h->h_next = history_def_next;
351 h->h_first = history_def_first;
352 h->h_last = history_def_last;
353 h->h_prev = history_def_prev;
354 h->h_curr = history_def_curr;
355 h->h_clear = history_def_clear;
356 h->h_enter = history_def_enter;
357 h->h_add = history_def_add;
370 if (h->h_next == history_def_next)
371 history_def_clear(h->h_ref);
376 /* history_set_num():
377 * Set history number of events
380 history_set_num(h, num)
384 if (h->h_next != history_def_next || num < 0)
386 history_def_set(h->h_ref, num);
391 /* history_set_fun():
392 * Set history functions
395 history_set_fun(h, nh)
398 if (nh->h_first == NULL || nh->h_next == NULL ||
399 nh->h_last == NULL || nh->h_prev == NULL || nh->h_curr == NULL ||
400 nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
402 if (h->h_next != history_def_next) {
403 history_def_init(&h->h_ref, 0);
404 h->h_first = history_def_first;
405 h->h_next = history_def_next;
406 h->h_last = history_def_last;
407 h->h_prev = history_def_prev;
408 h->h_curr = history_def_curr;
409 h->h_clear = history_def_clear;
410 h->h_enter = history_def_enter;
411 h->h_add = history_def_add;
416 if (h->h_next == history_def_next)
417 history_def_clear(h->h_ref);
419 h->h_first = nh->h_first;
420 h->h_next = nh->h_next;
421 h->h_last = nh->h_last;
422 h->h_prev = nh->h_prev;
423 h->h_curr = nh->h_curr;
424 h->h_clear = nh->h_clear;
425 h->h_enter = nh->h_enter;
426 h->h_add = nh->h_add;
432 * History load function
435 history_load(h, fname)
444 if ((fp = fopen(fname, "r")) == NULL)
447 if ((line = fgetln(fp, &sz)) == NULL)
450 if (strncmp(line, hist_cookie, sz) != 0)
453 for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
467 * History save function
470 history_save(h, fname)
478 if ((fp = fopen(fname, "w")) == NULL)
481 (void) fputs(hist_cookie, fp);
482 for (ev = HLAST(h); ev != NULL; ev = HPREV(h), i++)
483 (void) fprintf(fp, "%s", ev->str);
489 /* history_prev_event():
490 * Find the previous event, with number given
492 private const HistEvent *
493 history_prev_event(h, num)
498 for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
505 /* history_next_event():
506 * Find the next event, with number given
508 private const HistEvent *
509 history_next_event(h, num)
514 for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
521 /* history_prev_string():
522 * Find the previous event beginning with string
524 private const HistEvent *
525 history_prev_string(h, str)
530 size_t len = strlen(str);
532 for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
533 if (strncmp(str, ev->str, len) == 0)
539 /* history_next_string():
540 * Find the next event beginning with string
542 private const HistEvent *
543 history_next_string(h, str)
548 size_t len = strlen(str);
550 for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
551 if (strncmp(str, ev->str, len) == 0)
558 * User interface to history functions.
561 history(History *h, int fun, ...)
564 const HistEvent *ev = NULL;
566 static HistEvent sev = { 0, "" };
572 str = va_arg(va, const char *);
577 str = va_arg(va, const char *);
606 sev.num = history_load(h, va_arg(va, const char *));
611 sev.num = history_save(h, va_arg(va, const char *));
616 ev = history_prev_event(h, va_arg(va, int));
620 ev = history_next_event(h, va_arg(va, int));
624 ev = history_prev_string(h, va_arg(va, const char*));
628 ev = history_next_string(h, va_arg(va, const char*));
632 if (history_set_num(h, va_arg(va, int)) == 0) {
641 hf.h_ref = va_arg(va, ptr_t);
642 hf.h_first = va_arg(va, history_gfun_t);
643 hf.h_next = va_arg(va, history_gfun_t);
644 hf.h_last = va_arg(va, history_gfun_t);
645 hf.h_prev = va_arg(va, history_gfun_t);
646 hf.h_curr = va_arg(va, history_gfun_t);
647 hf.h_clear = va_arg(va, history_vfun_t);
648 hf.h_enter = va_arg(va, history_efun_t);
649 hf.h_add = va_arg(va, history_efun_t);
651 if (history_set_fun(h, &hf) == 0) {