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