history.c revision 1.6 1 1.6 christos /* $NetBSD: history.c,v 1.6 1997/07/06 18:25:27 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.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.6 christos #include <sys/cdefs.h>
40 1.1 cgd #if !defined(lint) && !defined(SCCSID)
41 1.6 christos #if 0
42 1.1 cgd static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
43 1.6 christos #else
44 1.6 christos __RCSID("$NetBSD: history.c,v 1.6 1997/07/06 18:25:27 christos Exp $");
45 1.6 christos #endif
46 1.1 cgd #endif /* not lint && not SCCSID */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * hist.c: History access functions
50 1.1 cgd */
51 1.1 cgd #include "sys.h"
52 1.1 cgd
53 1.1 cgd #include <string.h>
54 1.1 cgd #include <stdlib.h>
55 1.5 christos #ifdef __STDC__
56 1.1 cgd #include <stdarg.h>
57 1.1 cgd #else
58 1.1 cgd #include <varargs.h>
59 1.1 cgd #endif
60 1.1 cgd
61 1.2 christos static const char hist_cookie[] = "_HiStOrY_V1_\n";
62 1.2 christos
63 1.1 cgd #include "histedit.h"
64 1.1 cgd
65 1.1 cgd typedef const HistEvent * (*history_gfun_t) __P((ptr_t));
66 1.1 cgd typedef const HistEvent * (*history_efun_t) __P((ptr_t, const char *));
67 1.2 christos typedef void (*history_vfun_t) __P((ptr_t));
68 1.1 cgd
69 1.1 cgd struct history {
70 1.1 cgd ptr_t h_ref; /* Argument for history fcns */
71 1.1 cgd history_gfun_t h_first; /* Get the first element */
72 1.1 cgd history_gfun_t h_next; /* Get the next element */
73 1.1 cgd history_gfun_t h_last; /* Get the last element */
74 1.1 cgd history_gfun_t h_prev; /* Get the previous element */
75 1.1 cgd history_gfun_t h_curr; /* Get the current element */
76 1.2 christos history_vfun_t h_clear; /* Clear the history list */
77 1.1 cgd history_efun_t h_enter; /* Add an element */
78 1.1 cgd history_efun_t h_add; /* Append to an element */
79 1.1 cgd };
80 1.1 cgd
81 1.1 cgd #define HNEXT(h) (*(h)->h_next)((h)->h_ref)
82 1.1 cgd #define HFIRST(h) (*(h)->h_first)((h)->h_ref)
83 1.1 cgd #define HPREV(h) (*(h)->h_prev)((h)->h_ref)
84 1.1 cgd #define HLAST(h) (*(h)->h_last)((h)->h_ref)
85 1.1 cgd #define HCURR(h) (*(h)->h_curr)((h)->h_ref)
86 1.2 christos #define HCLEAR(h) (*(h)->h_clear)((h)->h_ref)
87 1.1 cgd #define HENTER(h, str) (*(h)->h_enter)((h)->h_ref, str)
88 1.1 cgd #define HADD(h, str) (*(h)->h_add)((h)->h_ref, str)
89 1.1 cgd
90 1.1 cgd #define h_malloc(a) malloc(a)
91 1.1 cgd #define h_free(a) free(a)
92 1.1 cgd
93 1.1 cgd
94 1.1 cgd private int history_set_num __P((History *, int));
95 1.2 christos private int history_set_fun __P((History *, History *));
96 1.2 christos private int history_load __P((History *, const char *));
97 1.2 christos private int history_save __P((History *, const char *));
98 1.1 cgd private const HistEvent *history_prev_event __P((History *, int));
99 1.1 cgd private const HistEvent *history_next_event __P((History *, int));
100 1.1 cgd private const HistEvent *history_next_string __P((History *, const char *));
101 1.1 cgd private const HistEvent *history_prev_string __P((History *, const char *));
102 1.1 cgd
103 1.1 cgd
104 1.1 cgd /***********************************************************************/
105 1.1 cgd
106 1.1 cgd /*
107 1.1 cgd * Builtin- history implementation
108 1.1 cgd */
109 1.1 cgd typedef struct hentry_t {
110 1.1 cgd HistEvent ev; /* What we return */
111 1.1 cgd struct hentry_t *next; /* Next entry */
112 1.1 cgd struct hentry_t *prev; /* Previous entry */
113 1.1 cgd } hentry_t;
114 1.1 cgd
115 1.1 cgd typedef struct history_t {
116 1.1 cgd hentry_t list; /* Fake list header element */
117 1.1 cgd hentry_t *cursor; /* Current element in the list */
118 1.1 cgd int max; /* Maximum number of events */
119 1.1 cgd int cur; /* Current number of events */
120 1.1 cgd int eventno; /* Current event number */
121 1.1 cgd } history_t;
122 1.1 cgd
123 1.1 cgd private const HistEvent *history_def_first __P((ptr_t));
124 1.1 cgd private const HistEvent *history_def_last __P((ptr_t));
125 1.1 cgd private const HistEvent *history_def_next __P((ptr_t));
126 1.1 cgd private const HistEvent *history_def_prev __P((ptr_t));
127 1.1 cgd private const HistEvent *history_def_curr __P((ptr_t));
128 1.1 cgd private const HistEvent *history_def_enter __P((ptr_t, const char *));
129 1.1 cgd private const HistEvent *history_def_add __P((ptr_t, const char *));
130 1.1 cgd private void history_def_init __P((ptr_t *, int));
131 1.2 christos private void history_def_clear __P((ptr_t));
132 1.1 cgd private const HistEvent *history_def_insert __P((history_t *, const char *));
133 1.1 cgd private void history_def_delete __P((history_t *, hentry_t *));
134 1.1 cgd
135 1.1 cgd #define history_def_set(p, num) (void) (((history_t *) p)->max = (num))
136 1.1 cgd
137 1.1 cgd
138 1.1 cgd /* history_def_first():
139 1.1 cgd * Default function to return the first event in the history.
140 1.1 cgd */
141 1.1 cgd private const HistEvent *
142 1.1 cgd history_def_first(p)
143 1.1 cgd ptr_t p;
144 1.1 cgd {
145 1.1 cgd history_t *h = (history_t *) p;
146 1.1 cgd h->cursor = h->list.next;
147 1.1 cgd if (h->cursor != &h->list)
148 1.1 cgd return &h->cursor->ev;
149 1.1 cgd else
150 1.1 cgd return NULL;
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd /* history_def_last():
154 1.1 cgd * Default function to return the last event in the history.
155 1.1 cgd */
156 1.1 cgd private const HistEvent *
157 1.1 cgd history_def_last(p)
158 1.1 cgd ptr_t p;
159 1.1 cgd {
160 1.1 cgd history_t *h = (history_t *) p;
161 1.1 cgd h->cursor = h->list.prev;
162 1.1 cgd if (h->cursor != &h->list)
163 1.1 cgd return &h->cursor->ev;
164 1.1 cgd else
165 1.1 cgd return NULL;
166 1.1 cgd }
167 1.1 cgd
168 1.1 cgd /* history_def_next():
169 1.1 cgd * Default function to return the next event in the history.
170 1.1 cgd */
171 1.1 cgd private const HistEvent *
172 1.1 cgd history_def_next(p)
173 1.1 cgd ptr_t p;
174 1.1 cgd {
175 1.1 cgd history_t *h = (history_t *) p;
176 1.1 cgd
177 1.1 cgd if (h->cursor != &h->list)
178 1.1 cgd h->cursor = h->cursor->next;
179 1.1 cgd else
180 1.1 cgd return NULL;
181 1.1 cgd
182 1.1 cgd if (h->cursor != &h->list)
183 1.1 cgd return &h->cursor->ev;
184 1.1 cgd else
185 1.1 cgd return NULL;
186 1.1 cgd }
187 1.1 cgd
188 1.1 cgd
189 1.1 cgd /* history_def_prev():
190 1.1 cgd * Default function to return the previous event in the history.
191 1.1 cgd */
192 1.1 cgd private const HistEvent *
193 1.1 cgd history_def_prev(p)
194 1.1 cgd ptr_t p;
195 1.1 cgd {
196 1.1 cgd history_t *h = (history_t *) p;
197 1.1 cgd
198 1.1 cgd if (h->cursor != &h->list)
199 1.1 cgd h->cursor = h->cursor->prev;
200 1.1 cgd else
201 1.1 cgd return NULL;
202 1.1 cgd
203 1.1 cgd if (h->cursor != &h->list)
204 1.1 cgd return &h->cursor->ev;
205 1.1 cgd else
206 1.1 cgd return NULL;
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd
210 1.1 cgd /* history_def_curr():
211 1.1 cgd * Default function to return the current event in the history.
212 1.1 cgd */
213 1.1 cgd private const HistEvent *
214 1.1 cgd history_def_curr(p)
215 1.1 cgd ptr_t p;
216 1.1 cgd {
217 1.1 cgd history_t *h = (history_t *) p;
218 1.1 cgd
219 1.1 cgd if (h->cursor != &h->list)
220 1.1 cgd return &h->cursor->ev;
221 1.1 cgd else
222 1.1 cgd return NULL;
223 1.1 cgd }
224 1.1 cgd
225 1.1 cgd /* history_def_add():
226 1.1 cgd * Append string to element
227 1.1 cgd */
228 1.1 cgd private const HistEvent *
229 1.1 cgd history_def_add(p, str)
230 1.1 cgd ptr_t p;
231 1.1 cgd const char *str;
232 1.1 cgd {
233 1.1 cgd history_t *h = (history_t *) p;
234 1.1 cgd size_t len;
235 1.1 cgd char *s;
236 1.1 cgd
237 1.1 cgd if (h->cursor == &h->list)
238 1.1 cgd return (history_def_enter(p, str));
239 1.1 cgd len = strlen(h->cursor->ev.str) + strlen(str) + 1;
240 1.1 cgd s = (char *) h_malloc(len);
241 1.4 mrg (void)strcpy(s, h->cursor->ev.str); /* XXX strcpy is safe */
242 1.4 mrg (void)strcat(s, str); /* XXX strcat is safe */
243 1.1 cgd h_free((ptr_t) h->cursor->ev.str);
244 1.1 cgd h->cursor->ev.str = s;
245 1.1 cgd return &h->cursor->ev;
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd
249 1.1 cgd /* history_def_delete():
250 1.1 cgd * Delete element hp of the h list
251 1.1 cgd */
252 1.1 cgd private void
253 1.1 cgd history_def_delete(h, hp)
254 1.1 cgd history_t *h;
255 1.1 cgd hentry_t *hp;
256 1.1 cgd {
257 1.1 cgd if (hp == &h->list)
258 1.1 cgd abort();
259 1.1 cgd hp->prev->next = hp->next;
260 1.1 cgd hp->next->prev = hp->prev;
261 1.1 cgd h_free((ptr_t) hp->ev.str);
262 1.1 cgd h_free(hp);
263 1.1 cgd h->cur--;
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd
267 1.1 cgd /* history_def_insert():
268 1.1 cgd * Insert element with string str in the h list
269 1.1 cgd */
270 1.1 cgd private const HistEvent *
271 1.1 cgd history_def_insert(h, str)
272 1.1 cgd history_t *h;
273 1.1 cgd const char *str;
274 1.1 cgd {
275 1.1 cgd h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
276 1.1 cgd h->cursor->ev.str = strdup(str);
277 1.1 cgd h->cursor->next = h->list.next;
278 1.1 cgd h->cursor->prev = &h->list;
279 1.1 cgd h->list.next->prev = h->cursor;
280 1.1 cgd h->list.next = h->cursor;
281 1.1 cgd h->cur++;
282 1.1 cgd
283 1.1 cgd return &h->cursor->ev;
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd
287 1.1 cgd /* history_def_enter():
288 1.1 cgd * Default function to enter an item in the history
289 1.1 cgd */
290 1.1 cgd private const HistEvent *
291 1.1 cgd history_def_enter(p, str)
292 1.1 cgd ptr_t p;
293 1.1 cgd const char *str;
294 1.1 cgd {
295 1.1 cgd history_t *h = (history_t *) p;
296 1.1 cgd const HistEvent *ev;
297 1.1 cgd
298 1.1 cgd
299 1.1 cgd ev = history_def_insert(h, str);
300 1.1 cgd ((HistEvent*) ev)->num = ++h->eventno;
301 1.1 cgd
302 1.1 cgd /*
303 1.1 cgd * Always keep at least one entry.
304 1.1 cgd * This way we don't have to check for the empty list.
305 1.1 cgd */
306 1.1 cgd while (h->cur > h->max + 1)
307 1.1 cgd history_def_delete(h, h->list.prev);
308 1.1 cgd return ev;
309 1.1 cgd }
310 1.1 cgd
311 1.1 cgd
312 1.1 cgd /* history_def_init():
313 1.1 cgd * Default history initialization function
314 1.1 cgd */
315 1.1 cgd private void
316 1.1 cgd history_def_init(p, n)
317 1.1 cgd ptr_t *p;
318 1.1 cgd int n;
319 1.1 cgd {
320 1.1 cgd history_t *h = (history_t *) h_malloc(sizeof(history_t));
321 1.1 cgd if (n <= 0)
322 1.1 cgd n = 0;
323 1.1 cgd h->eventno = 0;
324 1.1 cgd h->cur = 0;
325 1.1 cgd h->max = n;
326 1.1 cgd h->list.next = h->list.prev = &h->list;
327 1.1 cgd h->list.ev.str = NULL;
328 1.1 cgd h->list.ev.num = 0;
329 1.1 cgd h->cursor = &h->list;
330 1.1 cgd *p = (ptr_t) h;
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd
334 1.2 christos /* history_def_clear():
335 1.1 cgd * Default history cleanup function
336 1.1 cgd */
337 1.1 cgd private void
338 1.2 christos history_def_clear(p)
339 1.1 cgd ptr_t p;
340 1.1 cgd {
341 1.1 cgd history_t *h = (history_t *) p;
342 1.1 cgd
343 1.1 cgd while (h->list.prev != &h->list)
344 1.1 cgd history_def_delete(h, h->list.prev);
345 1.2 christos h->eventno = 0;
346 1.2 christos h->cur = 0;
347 1.1 cgd }
348 1.1 cgd
349 1.2 christos
350 1.2 christos
351 1.2 christos
352 1.1 cgd /************************************************************************/
353 1.1 cgd
354 1.1 cgd /* history_init():
355 1.1 cgd * Initialization function.
356 1.1 cgd */
357 1.1 cgd public History *
358 1.1 cgd history_init()
359 1.1 cgd {
360 1.1 cgd History *h = (History *) h_malloc(sizeof(History));
361 1.1 cgd
362 1.1 cgd history_def_init(&h->h_ref, 0);
363 1.1 cgd
364 1.1 cgd h->h_next = history_def_next;
365 1.1 cgd h->h_first = history_def_first;
366 1.1 cgd h->h_last = history_def_last;
367 1.1 cgd h->h_prev = history_def_prev;
368 1.1 cgd h->h_curr = history_def_curr;
369 1.2 christos h->h_clear = history_def_clear;
370 1.1 cgd h->h_enter = history_def_enter;
371 1.1 cgd h->h_add = history_def_add;
372 1.1 cgd
373 1.1 cgd return h;
374 1.1 cgd }
375 1.1 cgd
376 1.1 cgd
377 1.1 cgd /* history_end():
378 1.1 cgd * clean up history;
379 1.1 cgd */
380 1.1 cgd public void
381 1.1 cgd history_end(h)
382 1.1 cgd History *h;
383 1.1 cgd {
384 1.1 cgd if (h->h_next == history_def_next)
385 1.2 christos history_def_clear(h->h_ref);
386 1.1 cgd }
387 1.1 cgd
388 1.1 cgd
389 1.1 cgd
390 1.1 cgd /* history_set_num():
391 1.1 cgd * Set history number of events
392 1.1 cgd */
393 1.1 cgd private int
394 1.1 cgd history_set_num(h, num)
395 1.1 cgd History *h;
396 1.1 cgd int num;
397 1.1 cgd {
398 1.1 cgd if (h->h_next != history_def_next || num < 0)
399 1.1 cgd return -1;
400 1.1 cgd history_def_set(h->h_ref, num);
401 1.1 cgd return 0;
402 1.1 cgd }
403 1.1 cgd
404 1.1 cgd
405 1.1 cgd /* history_set_fun():
406 1.1 cgd * Set history functions
407 1.1 cgd */
408 1.1 cgd private int
409 1.2 christos history_set_fun(h, nh)
410 1.2 christos History *h, *nh;
411 1.2 christos {
412 1.2 christos if (nh->h_first == NULL || nh->h_next == NULL ||
413 1.2 christos nh->h_last == NULL || nh->h_prev == NULL || nh->h_curr == NULL ||
414 1.2 christos nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
415 1.2 christos nh->h_ref == NULL) {
416 1.1 cgd if (h->h_next != history_def_next) {
417 1.1 cgd history_def_init(&h->h_ref, 0);
418 1.1 cgd h->h_first = history_def_first;
419 1.1 cgd h->h_next = history_def_next;
420 1.1 cgd h->h_last = history_def_last;
421 1.1 cgd h->h_prev = history_def_prev;
422 1.1 cgd h->h_curr = history_def_curr;
423 1.2 christos h->h_clear = history_def_clear;
424 1.1 cgd h->h_enter = history_def_enter;
425 1.1 cgd h->h_add = history_def_add;
426 1.1 cgd }
427 1.1 cgd return -1;
428 1.1 cgd }
429 1.1 cgd
430 1.1 cgd if (h->h_next == history_def_next)
431 1.2 christos history_def_clear(h->h_ref);
432 1.2 christos
433 1.2 christos h->h_first = nh->h_first;
434 1.2 christos h->h_next = nh->h_next;
435 1.2 christos h->h_last = nh->h_last;
436 1.2 christos h->h_prev = nh->h_prev;
437 1.2 christos h->h_curr = nh->h_curr;
438 1.2 christos h->h_clear = nh->h_clear;
439 1.2 christos h->h_enter = nh->h_enter;
440 1.2 christos h->h_add = nh->h_add;
441 1.1 cgd
442 1.1 cgd return 0;
443 1.1 cgd }
444 1.1 cgd
445 1.1 cgd
446 1.2 christos /* history_load():
447 1.2 christos * History load function
448 1.2 christos */
449 1.2 christos private int
450 1.2 christos history_load(h, fname)
451 1.2 christos History *h;
452 1.2 christos const char *fname;
453 1.2 christos {
454 1.2 christos FILE *fp;
455 1.2 christos char *line;
456 1.2 christos size_t sz;
457 1.2 christos int i = -1;
458 1.2 christos
459 1.2 christos if ((fp = fopen(fname, "r")) == NULL)
460 1.2 christos return i;
461 1.2 christos
462 1.2 christos if ((line = fgetln(fp, &sz)) == NULL)
463 1.2 christos goto done;
464 1.2 christos
465 1.2 christos if (strncmp(line, hist_cookie, sz) != 0)
466 1.2 christos goto done;
467 1.2 christos
468 1.2 christos for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
469 1.2 christos char c = line[sz];
470 1.2 christos line[sz] = '\0';
471 1.2 christos HENTER(h, line);
472 1.2 christos line[sz] = c;
473 1.2 christos }
474 1.2 christos
475 1.2 christos done:
476 1.2 christos (void) fclose(fp);
477 1.2 christos return i;
478 1.2 christos }
479 1.2 christos
480 1.2 christos
481 1.2 christos /* history_save():
482 1.2 christos * History save function
483 1.2 christos */
484 1.2 christos private int
485 1.2 christos history_save(h, fname)
486 1.2 christos History *h;
487 1.2 christos const char *fname;
488 1.2 christos {
489 1.2 christos FILE *fp;
490 1.2 christos const HistEvent *ev;
491 1.2 christos int i = 0;
492 1.2 christos
493 1.2 christos if ((fp = fopen(fname, "w")) == NULL)
494 1.2 christos return -1;
495 1.2 christos
496 1.2 christos (void) fputs(hist_cookie, fp);
497 1.2 christos for (ev = HLAST(h); ev != NULL; ev = HPREV(h), i++)
498 1.2 christos (void) fprintf(fp, "%s", ev->str);
499 1.2 christos (void) fclose(fp);
500 1.2 christos return i;
501 1.2 christos }
502 1.2 christos
503 1.2 christos
504 1.1 cgd /* history_prev_event():
505 1.1 cgd * Find the previous event, with number given
506 1.1 cgd */
507 1.1 cgd private const HistEvent *
508 1.1 cgd history_prev_event(h, num)
509 1.1 cgd History *h;
510 1.1 cgd int num;
511 1.1 cgd {
512 1.1 cgd const HistEvent *ev;
513 1.1 cgd for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
514 1.1 cgd if (ev->num == num)
515 1.1 cgd return ev;
516 1.1 cgd return NULL;
517 1.1 cgd }
518 1.1 cgd
519 1.1 cgd
520 1.1 cgd /* history_next_event():
521 1.1 cgd * Find the next event, with number given
522 1.1 cgd */
523 1.1 cgd private const HistEvent *
524 1.1 cgd history_next_event(h, num)
525 1.1 cgd History *h;
526 1.1 cgd int num;
527 1.1 cgd {
528 1.1 cgd const HistEvent *ev;
529 1.1 cgd for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
530 1.1 cgd if (ev->num == num)
531 1.1 cgd return ev;
532 1.1 cgd return NULL;
533 1.1 cgd }
534 1.1 cgd
535 1.1 cgd
536 1.1 cgd /* history_prev_string():
537 1.1 cgd * Find the previous event beginning with string
538 1.1 cgd */
539 1.1 cgd private const HistEvent *
540 1.1 cgd history_prev_string(h, str)
541 1.1 cgd History *h;
542 1.1 cgd const char* str;
543 1.1 cgd {
544 1.1 cgd const HistEvent *ev;
545 1.1 cgd size_t len = strlen(str);
546 1.1 cgd
547 1.1 cgd for (ev = HCURR(h); ev != NULL; ev = HNEXT(h))
548 1.1 cgd if (strncmp(str, ev->str, len) == 0)
549 1.1 cgd return ev;
550 1.1 cgd return NULL;
551 1.1 cgd }
552 1.1 cgd
553 1.1 cgd
554 1.2 christos
555 1.2 christos
556 1.1 cgd /* history_next_string():
557 1.1 cgd * Find the next event beginning with string
558 1.1 cgd */
559 1.1 cgd private const HistEvent *
560 1.1 cgd history_next_string(h, str)
561 1.1 cgd History *h;
562 1.1 cgd const char* str;
563 1.1 cgd {
564 1.1 cgd const HistEvent *ev;
565 1.1 cgd size_t len = strlen(str);
566 1.1 cgd
567 1.1 cgd for (ev = HCURR(h); ev != NULL; ev = HPREV(h))
568 1.1 cgd if (strncmp(str, ev->str, len) == 0)
569 1.1 cgd return ev;
570 1.1 cgd return NULL;
571 1.1 cgd }
572 1.1 cgd
573 1.1 cgd
574 1.1 cgd /* history():
575 1.1 cgd * User interface to history functions.
576 1.1 cgd */
577 1.1 cgd const HistEvent *
578 1.5 christos #ifdef __STDC__
579 1.1 cgd history(History *h, int fun, ...)
580 1.1 cgd #else
581 1.1 cgd history(va_alist)
582 1.1 cgd va_dcl
583 1.1 cgd #endif
584 1.1 cgd {
585 1.1 cgd va_list va;
586 1.1 cgd const HistEvent *ev = NULL;
587 1.1 cgd const char *str;
588 1.2 christos static HistEvent sev = { 0, "" };
589 1.1 cgd
590 1.5 christos #ifdef __STDC__
591 1.1 cgd va_start(va, fun);
592 1.1 cgd #else
593 1.1 cgd History *h;
594 1.1 cgd int fun;
595 1.1 cgd va_start(va);
596 1.1 cgd h = va_arg(va, History *);
597 1.1 cgd fun = va_arg(va, int);
598 1.1 cgd #endif
599 1.1 cgd
600 1.1 cgd switch (fun) {
601 1.1 cgd case H_ADD:
602 1.1 cgd str = va_arg(va, const char *);
603 1.1 cgd ev = HADD(h, str);
604 1.1 cgd break;
605 1.1 cgd
606 1.1 cgd case H_ENTER:
607 1.1 cgd str = va_arg(va, const char *);
608 1.1 cgd ev = HENTER(h, str);
609 1.1 cgd break;
610 1.1 cgd
611 1.1 cgd case H_FIRST:
612 1.1 cgd ev = HFIRST(h);
613 1.1 cgd break;
614 1.1 cgd
615 1.1 cgd case H_NEXT:
616 1.1 cgd ev = HNEXT(h);
617 1.1 cgd break;
618 1.1 cgd
619 1.1 cgd case H_LAST:
620 1.1 cgd ev = HLAST(h);
621 1.1 cgd break;
622 1.1 cgd
623 1.1 cgd case H_PREV:
624 1.1 cgd ev = HPREV(h);
625 1.1 cgd break;
626 1.1 cgd
627 1.1 cgd case H_CURR:
628 1.1 cgd ev = HCURR(h);
629 1.1 cgd break;
630 1.1 cgd
631 1.2 christos case H_CLEAR:
632 1.2 christos HCLEAR(h);
633 1.2 christos break;
634 1.2 christos
635 1.2 christos case H_LOAD:
636 1.2 christos sev.num = history_load(h, va_arg(va, const char *));
637 1.2 christos ev = &sev;
638 1.2 christos break;
639 1.2 christos
640 1.2 christos case H_SAVE:
641 1.2 christos sev.num = history_save(h, va_arg(va, const char *));
642 1.2 christos ev = &sev;
643 1.2 christos break;
644 1.2 christos
645 1.1 cgd case H_PREV_EVENT:
646 1.1 cgd ev = history_prev_event(h, va_arg(va, int));
647 1.1 cgd break;
648 1.1 cgd
649 1.1 cgd case H_NEXT_EVENT:
650 1.1 cgd ev = history_next_event(h, va_arg(va, int));
651 1.1 cgd break;
652 1.1 cgd
653 1.1 cgd case H_PREV_STR:
654 1.1 cgd ev = history_prev_string(h, va_arg(va, const char*));
655 1.1 cgd break;
656 1.1 cgd
657 1.1 cgd case H_NEXT_STR:
658 1.1 cgd ev = history_next_string(h, va_arg(va, const char*));
659 1.1 cgd break;
660 1.1 cgd
661 1.1 cgd case H_EVENT:
662 1.2 christos if (history_set_num(h, va_arg(va, int)) == 0) {
663 1.2 christos sev.num = -1;
664 1.1 cgd ev = &sev;
665 1.2 christos }
666 1.1 cgd break;
667 1.1 cgd
668 1.1 cgd case H_FUNC:
669 1.1 cgd {
670 1.2 christos History hf;
671 1.2 christos hf.h_ref = va_arg(va, ptr_t);
672 1.2 christos hf.h_first = va_arg(va, history_gfun_t);
673 1.2 christos hf.h_next = va_arg(va, history_gfun_t);
674 1.2 christos hf.h_last = va_arg(va, history_gfun_t);
675 1.2 christos hf.h_prev = va_arg(va, history_gfun_t);
676 1.2 christos hf.h_curr = va_arg(va, history_gfun_t);
677 1.2 christos hf.h_clear = va_arg(va, history_vfun_t);
678 1.2 christos hf.h_enter = va_arg(va, history_efun_t);
679 1.2 christos hf.h_add = va_arg(va, history_efun_t);
680 1.1 cgd
681 1.2 christos if (history_set_fun(h, &hf) == 0) {
682 1.2 christos sev.num = -1;
683 1.1 cgd ev = &sev;
684 1.2 christos }
685 1.1 cgd }
686 1.1 cgd break;
687 1.1 cgd
688 1.1 cgd case H_END:
689 1.1 cgd history_end(h);
690 1.1 cgd break;
691 1.1 cgd
692 1.1 cgd default:
693 1.1 cgd break;
694 1.1 cgd }
695 1.1 cgd va_end(va);
696 1.1 cgd return ev;
697 1.1 cgd }
698