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.4 2003/11/12 20:21:29 eirikn Exp $
41 * hist.c: History access functions
53 static const char hist_cookie[] = "_HiStOrY_V1_\n";
57 typedef const HistEvent * (*history_gfun_t) (ptr_t);
58 typedef const HistEvent * (*history_efun_t) (ptr_t, const char *);
59 typedef void (*history_vfun_t) (ptr_t);
62 ptr_t h_ref; /* Argument for history fcns */
63 history_gfun_t h_first; /* Get the first element */
64 history_gfun_t h_next; /* Get the next element */
65 history_gfun_t h_last; /* Get the last element */
66 history_gfun_t h_prev; /* Get the previous element */
67 history_gfun_t h_curr; /* Get the current element */
68 history_vfun_t h_clear; /* Clear the history list */
69 history_efun_t h_enter; /* Add an element */
70 history_efun_t h_add; /* Append to an element */
73 #define HNEXT(h) (*(h)->h_next)((h)->h_ref)
74 #define HFIRST(h) (*(h)->h_first)((h)->h_ref)
75 #define HPREV(h) (*(h)->h_prev)((h)->h_ref)
76 #define HLAST(h) (*(h)->h_last)((h)->h_ref)
77 #define HCURR(h) (*(h)->h_curr)((h)->h_ref)
78 #define HCLEAR(h) (*(h)->h_clear)((h)->h_ref)
79 #define HENTER(h, str) (*(h)->h_enter)((h)->h_ref, str)
80 #define HADD(h, str) (*(h)->h_add)((h)->h_ref, str)
82 #define h_malloc(a) malloc(a)
83 #define h_free(a) free(a)
86 private int history_set_num (History *, int);
87 private int history_set_fun (History *, History *);
88 private int history_load (History *, const char *);
89 private int history_save (History *, const char *);
90 private const HistEvent *history_prev_event (History *, int);
91 private const HistEvent *history_next_event (History *, int);
92 private const HistEvent *history_next_string (History *, const char *);
93 private const HistEvent *history_prev_string (History *, const char *);
96 /***********************************************************************/
99 * Builtin- history implementation
101 typedef struct hentry_t {
102 HistEvent ev; /* What we return */
103 struct hentry_t *next; /* Next entry */
104 struct hentry_t *prev; /* Previous entry */
107 typedef struct history_t {
108 hentry_t list; /* Fake list header element */
109 hentry_t *cursor; /* Current element in the list */
110 int max; /* Maximum number of events */
111 int cur; /* Current number of events */
112 int eventno; /* Current event number */
115 private const HistEvent *history_def_first (ptr_t);
116 private const HistEvent *history_def_last (ptr_t);
117 private const HistEvent *history_def_next (ptr_t);
118 private const HistEvent *history_def_prev (ptr_t);
119 private const HistEvent *history_def_curr (ptr_t);
120 private const HistEvent *history_def_enter (ptr_t, const char *);
121 private const HistEvent *history_def_add (ptr_t, const char *);
122 private void history_def_init (ptr_t *, int);
123 private void history_def_clear (ptr_t);
124 private const HistEvent *history_def_insert (history_t *, const char *);
125 private void history_def_delete (history_t *, hentry_t *);
127 #define history_def_set(p, num) (void) (((history_t *) p)->max = (num))
130 /* history_def_first():
131 * Default function to return the first event in the history.
133 private const HistEvent *
137 history_t *h = (history_t *) p;
138 h->cursor = h->list.next;
139 if (h->cursor != &h->list)
140 return &h->cursor->ev;
145 /* history_def_last():
146 * Default function to return the last event in the history.
148 private const HistEvent *
152 history_t *h = (history_t *) p;
153 h->cursor = h->list.prev;
154 if (h->cursor != &h->list)
155 return &h->cursor->ev;
160 /* history_def_next():
161 * Default function to return the next event in the history.
163 private const HistEvent *
167 history_t *h = (history_t *) p;
169 if (h->cursor != &h->list)
170 h->cursor = h->cursor->next;
174 if (h->cursor != &h->list)
175 return &h->cursor->ev;
181 /* history_def_prev():
182 * Default function to return the previous event in the history.
184 private const HistEvent *
188 history_t *h = (history_t *) p;
190 if (h->cursor != &h->list)
191 h->cursor = h->cursor->prev;
195 if (h->cursor != &h->list)
196 return &h->cursor->ev;
202 /* history_def_curr():
203 * Default function to return the current event in the history.
205 private const HistEvent *
209 history_t *h = (history_t *) p;
211 if (h->cursor != &h->list)
212 return &h->cursor->ev;
218 /* history_def_add():
219 * Append string to element
221 private const HistEvent *
222 history_def_add(p, str)
226 history_t *h = (history_t *) p;
230 if (h->cursor == &h->list)
231 return (history_def_enter(p, str));
232 len = strlen(h->cursor->ev.str) + strlen(str) + 1;
233 s = (char *) h_malloc(len);
234 (void)strcpy(s, h->cursor->ev.str); /* XXX strcpy is safe */
235 (void)strcat(s, str); /* XXX strcat is safe */
236 h_free((ptr_t) h->cursor->ev.str);
237 h->cursor->ev.str = s;
238 return &h->cursor->ev;
242 /* history_def_delete():
243 * Delete element hp of the h list
246 history_def_delete(h, hp)
252 hp->prev->next = hp->next;
253 hp->next->prev = hp->prev;
254 h_free((ptr_t) hp->ev.str);
260 /* history_def_insert():
261 * Insert element with string str in the h list
263 private const HistEvent *
264 history_def_insert(h, str)
268 h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
269 h->cursor->ev.str = strdup(str);
270 h->cursor->next = h->list.next;
271 h->cursor->prev = &h->list;
272 h->list.next->prev = h->cursor;
273 h->list.next = h->cursor;
276 return &h->cursor->ev;
280 /* history_def_enter():
281 * Default function to enter an item in the history
283 private const HistEvent *
284 history_def_enter(p, str)
288 history_t *h = (history_t *) p;
292 ev = history_def_insert(h, str);
293 ((HistEvent*) ev)->num = ++h->eventno;
296 * Always keep at least one entry.
297 * This way we don't have to check for the empty list.
299 while (h->cur > h->max + 1)
300 history_def_delete(h, h->list.prev);
305 /* history_def_init():
306 * Default history initialization function
309 history_def_init(p, n)
313 history_t *h = (history_t *) h_malloc(sizeof(history_t));
319 h->list.next = h->list.prev = &h->list;
320 h->list.ev.str = NULL;
322 h->cursor = &h->list;
327 /* history_def_clear():
328 * Default history cleanup function
334 history_t *h = (history_t *) p;
336 while (h->list.prev != &h->list)
337 history_def_delete(h, h->list.prev);
342 /************************************************************************/
345 * Initialization function.
350 History *h = (History *) h_malloc(sizeof(History));
352 history_def_init(&h->h_ref, 0);
354 h->h_next = history_def_next;
355 h->h_first = history_def_first;
356 h->h_last = history_def_last;
357 h->h_prev = history_def_prev;
358 h->h_curr = history_def_curr;
359 h->h_clear = history_def_clear;
360 h->h_enter = history_def_enter;
361 h->h_add = history_def_add;
374 if (h->h_next == history_def_next)
375 history_def_clear(h->h_ref);
380 /* history_set_num():
381 * Set history number of events
384 history_set_num(h, num)
388 if (h->h_next != history_def_next || num < 0)
390 history_def_set(h->h_ref, num);
395 /* history_set_fun():
396 * Set history functions
399 history_set_fun(h, nh)
402 if (nh->h_first == NULL || nh->h_next == NULL ||
403 nh->h_last == NULL || nh->h_prev == NULL || nh->h_curr == NULL ||
404 nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
406 if (h->h_next != history_def_next) {
407 history_def_init(&h->h_ref, 0);
408 h->h_first = history_def_first;
409 h->h_next = history_def_next;
410 h->h_last = history_def_last;
411 h->h_prev = history_def_prev;
412 h->h_curr = history_def_curr;
413 h->h_clear = history_def_clear;
414 h->h_enter = history_def_enter;
415 h->h_add = history_def_add;
420 if (h->h_next == history_def_next)
421 history_def_clear(h->h_ref);
423 h->h_first = nh->h_first;
424 h->h_next = nh->h_next;
425 h->h_last = nh->h_last;
426 h->h_prev = nh->h_prev;
427 h->h_curr = nh->h_curr;
428 h->h_clear = nh->h_clear;
429 h->h_enter = nh->h_enter;
430 h->h_add = nh->h_add;
436 * History load function
439 history_load(h, fname)
448 if ((fp = fopen(fname, "r")) == NULL)
451 if ((line = fgetln(fp, &sz)) == NULL)
454 if (strncmp(line, hist_cookie, sz) != 0)
457 for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
471 * History save function
474 history_save(h, fname)
482 if ((fp = fopen(fname, "w")) == NULL)
485 (void) fputs(hist_cookie, fp);
486 for (ev = HLAST(h); ev != NULL; ev = HPREV(h), i++)
487 (void) fprintf(fp, "%s", ev->str);
493 /* history_prev_event():
494 * Find the previous event, with number given
496 private const HistEvent *
497 history_prev_event(h, num)
502 for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
509 /* history_next_event():
510 * Find the next event, with number given
512 private const HistEvent *
513 history_next_event(h, num)
518 for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
525 /* history_prev_string():
526 * Find the previous event beginning with string
528 private const HistEvent *
529 history_prev_string(h, str)
534 size_t len = strlen(str);
536 for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
537 if (strncmp(str, ev->str, len) == 0)
543 /* history_next_string():
544 * Find the next event beginning with string
546 private const HistEvent *
547 history_next_string(h, str)
552 size_t len = strlen(str);
554 for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
555 if (strncmp(str, ev->str, len) == 0)
562 * User interface to history functions.
566 history(History *h, int fun, ...)
573 const HistEvent *ev = NULL;
575 static HistEvent sev = { 0, "" };
583 h = va_arg(va, History *);
584 fun = va_arg(va, int);
589 str = va_arg(va, const char *);
594 str = va_arg(va, const char *);
623 sev.num = history_load(h, va_arg(va, const char *));
628 sev.num = history_save(h, va_arg(va, const char *));
633 ev = history_prev_event(h, va_arg(va, int));
637 ev = history_next_event(h, va_arg(va, int));
641 ev = history_prev_string(h, va_arg(va, const char*));
645 ev = history_next_string(h, va_arg(va, const char*));
649 if (history_set_num(h, va_arg(va, int)) == 0) {
658 hf.h_ref = va_arg(va, ptr_t);
659 hf.h_first = va_arg(va, history_gfun_t);
660 hf.h_next = va_arg(va, history_gfun_t);
661 hf.h_last = va_arg(va, history_gfun_t);
662 hf.h_prev = va_arg(va, history_gfun_t);
663 hf.h_curr = va_arg(va, history_gfun_t);
664 hf.h_clear = va_arg(va, history_vfun_t);
665 hf.h_enter = va_arg(va, history_efun_t);
666 hf.h_add = va_arg(va, history_efun_t);
668 if (history_set_fun(h, &hf) == 0) {