history.c revision 1.25 1 1.25 christos /* $NetBSD: history.c,v 1.25 2003/10/18 23:48:42 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.24 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include "config.h"
36 1.1 cgd #if !defined(lint) && !defined(SCCSID)
37 1.6 christos #if 0
38 1.1 cgd static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
39 1.6 christos #else
40 1.25 christos __RCSID("$NetBSD: history.c,v 1.25 2003/10/18 23:48:42 christos Exp $");
41 1.6 christos #endif
42 1.1 cgd #endif /* not lint && not SCCSID */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * hist.c: History access functions
46 1.1 cgd */
47 1.1 cgd #include <string.h>
48 1.1 cgd #include <stdlib.h>
49 1.1 cgd #include <stdarg.h>
50 1.19 christos #ifdef HAVE_VIS_H
51 1.12 christos #include <vis.h>
52 1.19 christos #else
53 1.19 christos #include "np/vis.h"
54 1.19 christos #endif
55 1.17 christos #include <sys/stat.h>
56 1.1 cgd
57 1.12 christos static const char hist_cookie[] = "_HiStOrY_V2_\n";
58 1.2 christos
59 1.1 cgd #include "histedit.h"
60 1.1 cgd
61 1.16 lukem typedef int (*history_gfun_t)(ptr_t, HistEvent *);
62 1.16 lukem typedef int (*history_efun_t)(ptr_t, HistEvent *, const char *);
63 1.16 lukem typedef void (*history_vfun_t)(ptr_t, HistEvent *);
64 1.16 lukem typedef int (*history_sfun_t)(ptr_t, HistEvent *, const int);
65 1.1 cgd
66 1.1 cgd struct history {
67 1.16 lukem ptr_t h_ref; /* Argument for history fcns */
68 1.16 lukem int h_ent; /* Last entry point for history */
69 1.16 lukem history_gfun_t h_first; /* Get the first element */
70 1.16 lukem history_gfun_t h_next; /* Get the next element */
71 1.16 lukem history_gfun_t h_last; /* Get the last element */
72 1.16 lukem history_gfun_t h_prev; /* Get the previous element */
73 1.16 lukem history_gfun_t h_curr; /* Get the current element */
74 1.16 lukem history_sfun_t h_set; /* Set the current element */
75 1.16 lukem history_vfun_t h_clear; /* Clear the history list */
76 1.16 lukem history_efun_t h_enter; /* Add an element */
77 1.16 lukem history_efun_t h_add; /* Append to an element */
78 1.1 cgd };
79 1.22 christos
80 1.16 lukem #define HNEXT(h, ev) (*(h)->h_next)((h)->h_ref, ev)
81 1.16 lukem #define HFIRST(h, ev) (*(h)->h_first)((h)->h_ref, ev)
82 1.16 lukem #define HPREV(h, ev) (*(h)->h_prev)((h)->h_ref, ev)
83 1.16 lukem #define HLAST(h, ev) (*(h)->h_last)((h)->h_ref, ev)
84 1.16 lukem #define HCURR(h, ev) (*(h)->h_curr)((h)->h_ref, ev)
85 1.16 lukem #define HSET(h, ev, n) (*(h)->h_set)((h)->h_ref, ev, n)
86 1.16 lukem #define HCLEAR(h, ev) (*(h)->h_clear)((h)->h_ref, ev)
87 1.7 christos #define HENTER(h, ev, str) (*(h)->h_enter)((h)->h_ref, ev, str)
88 1.7 christos #define HADD(h, ev, str) (*(h)->h_add)((h)->h_ref, ev, str)
89 1.1 cgd
90 1.25 christos #define h_strdup(a) strdup(a)
91 1.16 lukem #define h_malloc(a) malloc(a)
92 1.16 lukem #define h_realloc(a, b) realloc((a), (b))
93 1.16 lukem #define h_free(a) free(a)
94 1.1 cgd
95 1.19 christos typedef struct {
96 1.19 christos int num;
97 1.19 christos char *str;
98 1.19 christos } HistEventPrivate;
99 1.19 christos
100 1.19 christos
101 1.1 cgd
102 1.16 lukem private int history_setsize(History *, HistEvent *, int);
103 1.16 lukem private int history_getsize(History *, HistEvent *);
104 1.22 christos private int history_setunique(History *, HistEvent *, int);
105 1.22 christos private int history_getunique(History *, HistEvent *);
106 1.16 lukem private int history_set_fun(History *, History *);
107 1.16 lukem private int history_load(History *, const char *);
108 1.16 lukem private int history_save(History *, const char *);
109 1.16 lukem private int history_prev_event(History *, HistEvent *, int);
110 1.16 lukem private int history_next_event(History *, HistEvent *, int);
111 1.16 lukem private int history_next_string(History *, HistEvent *, const char *);
112 1.16 lukem private int history_prev_string(History *, HistEvent *, const char *);
113 1.1 cgd
114 1.1 cgd
115 1.1 cgd /***********************************************************************/
116 1.1 cgd
117 1.1 cgd /*
118 1.1 cgd * Builtin- history implementation
119 1.1 cgd */
120 1.1 cgd typedef struct hentry_t {
121 1.16 lukem HistEvent ev; /* What we return */
122 1.16 lukem struct hentry_t *next; /* Next entry */
123 1.16 lukem struct hentry_t *prev; /* Previous entry */
124 1.22 christos } hentry_t;
125 1.1 cgd
126 1.1 cgd typedef struct history_t {
127 1.22 christos hentry_t list; /* Fake list header element */
128 1.22 christos hentry_t *cursor; /* Current element in the list */
129 1.22 christos int max; /* Maximum number of events */
130 1.22 christos int cur; /* Current number of events */
131 1.16 lukem int eventid; /* For generation of unique event id */
132 1.22 christos int flags; /* History flags */
133 1.22 christos #define H_UNIQUE 1 /* Store only unique elements */
134 1.22 christos } history_t;
135 1.16 lukem
136 1.16 lukem private int history_def_first(ptr_t, HistEvent *);
137 1.16 lukem private int history_def_last(ptr_t, HistEvent *);
138 1.16 lukem private int history_def_next(ptr_t, HistEvent *);
139 1.16 lukem private int history_def_prev(ptr_t, HistEvent *);
140 1.16 lukem private int history_def_curr(ptr_t, HistEvent *);
141 1.16 lukem private int history_def_set(ptr_t, HistEvent *, const int n);
142 1.16 lukem private int history_def_enter(ptr_t, HistEvent *, const char *);
143 1.16 lukem private int history_def_add(ptr_t, HistEvent *, const char *);
144 1.21 christos private int history_def_init(ptr_t *, HistEvent *, int);
145 1.16 lukem private void history_def_clear(ptr_t, HistEvent *);
146 1.16 lukem private int history_def_insert(history_t *, HistEvent *, const char *);
147 1.16 lukem private void history_def_delete(history_t *, HistEvent *, hentry_t *);
148 1.1 cgd
149 1.22 christos #define history_def_setsize(p, num)(void) (((history_t *)p)->max = (num))
150 1.22 christos #define history_def_getsize(p) (((history_t *)p)->cur)
151 1.22 christos #define history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0)
152 1.22 christos #define history_def_setunique(p, uni) \
153 1.22 christos if (uni) \
154 1.22 christos (((history_t *)p)->flags) |= H_UNIQUE; \
155 1.22 christos else \
156 1.22 christos (((history_t *)p)->flags) &= ~H_UNIQUE
157 1.1 cgd
158 1.16 lukem #define he_strerror(code) he_errlist[code]
159 1.16 lukem #define he_seterrev(evp, code) {\
160 1.7 christos evp->num = code;\
161 1.7 christos evp->str = he_strerror(code);\
162 1.7 christos }
163 1.14 simonb
164 1.7 christos /* error messages */
165 1.7 christos static const char *const he_errlist[] = {
166 1.16 lukem "OK",
167 1.16 lukem "unknown error",
168 1.16 lukem "malloc() failed",
169 1.16 lukem "first event not found",
170 1.16 lukem "last event not found",
171 1.16 lukem "empty list",
172 1.16 lukem "no next event",
173 1.16 lukem "no previous event",
174 1.16 lukem "current event is invalid",
175 1.16 lukem "event not found",
176 1.16 lukem "can't read history from file",
177 1.16 lukem "can't write history",
178 1.16 lukem "required parameter(s) not supplied",
179 1.16 lukem "history size negative",
180 1.16 lukem "function not allowed with other history-functions-set the default",
181 1.16 lukem "bad parameters"
182 1.7 christos };
183 1.7 christos /* error codes */
184 1.16 lukem #define _HE_OK 0
185 1.16 lukem #define _HE_UNKNOWN 1
186 1.16 lukem #define _HE_MALLOC_FAILED 2
187 1.16 lukem #define _HE_FIRST_NOTFOUND 3
188 1.16 lukem #define _HE_LAST_NOTFOUND 4
189 1.16 lukem #define _HE_EMPTY_LIST 5
190 1.16 lukem #define _HE_END_REACHED 6
191 1.16 lukem #define _HE_START_REACHED 7
192 1.16 lukem #define _HE_CURR_INVALID 8
193 1.16 lukem #define _HE_NOT_FOUND 9
194 1.16 lukem #define _HE_HIST_READ 10
195 1.16 lukem #define _HE_HIST_WRITE 11
196 1.16 lukem #define _HE_PARAM_MISSING 12
197 1.16 lukem #define _HE_SIZE_NEGATIVE 13
198 1.16 lukem #define _HE_NOT_ALLOWED 14
199 1.16 lukem #define _HE_BAD_PARAM 15
200 1.1 cgd
201 1.1 cgd /* history_def_first():
202 1.1 cgd * Default function to return the first event in the history.
203 1.1 cgd */
204 1.7 christos private int
205 1.16 lukem history_def_first(ptr_t p, HistEvent *ev)
206 1.1 cgd {
207 1.16 lukem history_t *h = (history_t *) p;
208 1.7 christos
209 1.16 lukem h->cursor = h->list.next;
210 1.16 lukem if (h->cursor != &h->list)
211 1.16 lukem *ev = h->cursor->ev;
212 1.16 lukem else {
213 1.16 lukem he_seterrev(ev, _HE_FIRST_NOTFOUND);
214 1.16 lukem return (-1);
215 1.16 lukem }
216 1.7 christos
217 1.16 lukem return (0);
218 1.1 cgd }
219 1.1 cgd
220 1.8 christos
221 1.1 cgd /* history_def_last():
222 1.1 cgd * Default function to return the last event in the history.
223 1.1 cgd */
224 1.7 christos private int
225 1.16 lukem history_def_last(ptr_t p, HistEvent *ev)
226 1.16 lukem {
227 1.16 lukem history_t *h = (history_t *) p;
228 1.16 lukem
229 1.16 lukem h->cursor = h->list.prev;
230 1.16 lukem if (h->cursor != &h->list)
231 1.16 lukem *ev = h->cursor->ev;
232 1.16 lukem else {
233 1.16 lukem he_seterrev(ev, _HE_LAST_NOTFOUND);
234 1.16 lukem return (-1);
235 1.16 lukem }
236 1.7 christos
237 1.16 lukem return (0);
238 1.1 cgd }
239 1.1 cgd
240 1.8 christos
241 1.1 cgd /* history_def_next():
242 1.1 cgd * Default function to return the next event in the history.
243 1.1 cgd */
244 1.7 christos private int
245 1.16 lukem history_def_next(ptr_t p, HistEvent *ev)
246 1.16 lukem {
247 1.16 lukem history_t *h = (history_t *) p;
248 1.16 lukem
249 1.16 lukem if (h->cursor != &h->list)
250 1.16 lukem h->cursor = h->cursor->next;
251 1.16 lukem else {
252 1.16 lukem he_seterrev(ev, _HE_EMPTY_LIST);
253 1.16 lukem return (-1);
254 1.16 lukem }
255 1.1 cgd
256 1.16 lukem if (h->cursor != &h->list)
257 1.16 lukem *ev = h->cursor->ev;
258 1.16 lukem else {
259 1.16 lukem he_seterrev(ev, _HE_END_REACHED);
260 1.16 lukem return (-1);
261 1.16 lukem }
262 1.7 christos
263 1.16 lukem return (0);
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd
267 1.1 cgd /* history_def_prev():
268 1.1 cgd * Default function to return the previous event in the history.
269 1.1 cgd */
270 1.7 christos private int
271 1.16 lukem history_def_prev(ptr_t p, HistEvent *ev)
272 1.16 lukem {
273 1.16 lukem history_t *h = (history_t *) p;
274 1.16 lukem
275 1.16 lukem if (h->cursor != &h->list)
276 1.16 lukem h->cursor = h->cursor->prev;
277 1.16 lukem else {
278 1.16 lukem he_seterrev(ev,
279 1.16 lukem (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
280 1.16 lukem return (-1);
281 1.16 lukem }
282 1.1 cgd
283 1.16 lukem if (h->cursor != &h->list)
284 1.16 lukem *ev = h->cursor->ev;
285 1.16 lukem else {
286 1.16 lukem he_seterrev(ev, _HE_START_REACHED);
287 1.16 lukem return (-1);
288 1.16 lukem }
289 1.7 christos
290 1.16 lukem return (0);
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd
294 1.1 cgd /* history_def_curr():
295 1.1 cgd * Default function to return the current event in the history.
296 1.1 cgd */
297 1.7 christos private int
298 1.16 lukem history_def_curr(ptr_t p, HistEvent *ev)
299 1.1 cgd {
300 1.16 lukem history_t *h = (history_t *) p;
301 1.1 cgd
302 1.16 lukem if (h->cursor != &h->list)
303 1.16 lukem *ev = h->cursor->ev;
304 1.16 lukem else {
305 1.16 lukem he_seterrev(ev,
306 1.16 lukem (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
307 1.16 lukem return (-1);
308 1.16 lukem }
309 1.7 christos
310 1.16 lukem return (0);
311 1.1 cgd }
312 1.1 cgd
313 1.8 christos
314 1.8 christos /* history_def_set():
315 1.8 christos * Default function to set the current event in the history to the
316 1.8 christos * given one.
317 1.8 christos */
318 1.8 christos private int
319 1.16 lukem history_def_set(ptr_t p, HistEvent *ev, const int n)
320 1.16 lukem {
321 1.16 lukem history_t *h = (history_t *) p;
322 1.8 christos
323 1.16 lukem if (h->cur == 0) {
324 1.16 lukem he_seterrev(ev, _HE_EMPTY_LIST);
325 1.16 lukem return (-1);
326 1.16 lukem }
327 1.16 lukem if (h->cursor == &h->list || h->cursor->ev.num != n) {
328 1.16 lukem for (h->cursor = h->list.next; h->cursor != &h->list;
329 1.16 lukem h->cursor = h->cursor->next)
330 1.16 lukem if (h->cursor->ev.num == n)
331 1.16 lukem break;
332 1.16 lukem }
333 1.16 lukem if (h->cursor == &h->list) {
334 1.16 lukem he_seterrev(ev, _HE_NOT_FOUND);
335 1.16 lukem return (-1);
336 1.16 lukem }
337 1.16 lukem return (0);
338 1.8 christos }
339 1.8 christos
340 1.8 christos
341 1.1 cgd /* history_def_add():
342 1.1 cgd * Append string to element
343 1.1 cgd */
344 1.7 christos private int
345 1.16 lukem history_def_add(ptr_t p, HistEvent *ev, const char *str)
346 1.16 lukem {
347 1.16 lukem history_t *h = (history_t *) p;
348 1.16 lukem size_t len;
349 1.16 lukem char *s;
350 1.19 christos HistEventPrivate *evp = (void *)&h->cursor->ev;
351 1.16 lukem
352 1.16 lukem if (h->cursor == &h->list)
353 1.16 lukem return (history_def_enter(p, ev, str));
354 1.19 christos len = strlen(evp->str) + strlen(str) + 1;
355 1.16 lukem s = (char *) h_malloc(len);
356 1.21 christos if (s == NULL) {
357 1.16 lukem he_seterrev(ev, _HE_MALLOC_FAILED);
358 1.16 lukem return (-1);
359 1.16 lukem }
360 1.16 lukem (void) strlcpy(s, h->cursor->ev.str, len);
361 1.16 lukem (void) strlcat(s, str, len);
362 1.21 christos h_free((ptr_t)evp->str);
363 1.19 christos evp->str = s;
364 1.16 lukem *ev = h->cursor->ev;
365 1.16 lukem return (0);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd
369 1.1 cgd /* history_def_delete():
370 1.1 cgd * Delete element hp of the h list
371 1.1 cgd */
372 1.11 christos /* ARGSUSED */
373 1.1 cgd private void
374 1.23 christos history_def_delete(history_t *h,
375 1.23 christos HistEvent *ev __attribute__((__unused__)), hentry_t *hp)
376 1.16 lukem {
377 1.19 christos HistEventPrivate *evp = (void *)&hp->ev;
378 1.16 lukem if (hp == &h->list)
379 1.16 lukem abort();
380 1.16 lukem hp->prev->next = hp->next;
381 1.16 lukem hp->next->prev = hp->prev;
382 1.19 christos h_free((ptr_t) evp->str);
383 1.16 lukem h_free(hp);
384 1.16 lukem h->cur--;
385 1.1 cgd }
386 1.1 cgd
387 1.1 cgd
388 1.1 cgd /* history_def_insert():
389 1.1 cgd * Insert element with string str in the h list
390 1.1 cgd */
391 1.7 christos private int
392 1.16 lukem history_def_insert(history_t *h, HistEvent *ev, const char *str)
393 1.16 lukem {
394 1.16 lukem
395 1.16 lukem h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
396 1.21 christos if (h->cursor == NULL)
397 1.21 christos goto oomem;
398 1.25 christos if ((h->cursor->ev.str = h_strdup(str)) == NULL) {
399 1.21 christos h_free((ptr_t)h->cursor);
400 1.21 christos goto oomem;
401 1.16 lukem }
402 1.16 lukem h->cursor->ev.num = ++h->eventid;
403 1.16 lukem h->cursor->next = h->list.next;
404 1.16 lukem h->cursor->prev = &h->list;
405 1.16 lukem h->list.next->prev = h->cursor;
406 1.16 lukem h->list.next = h->cursor;
407 1.16 lukem h->cur++;
408 1.1 cgd
409 1.16 lukem *ev = h->cursor->ev;
410 1.16 lukem return (0);
411 1.21 christos oomem:
412 1.21 christos he_seterrev(ev, _HE_MALLOC_FAILED);
413 1.21 christos return (-1);
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd
417 1.1 cgd /* history_def_enter():
418 1.1 cgd * Default function to enter an item in the history
419 1.1 cgd */
420 1.7 christos private int
421 1.16 lukem history_def_enter(ptr_t p, HistEvent *ev, const char *str)
422 1.16 lukem {
423 1.16 lukem history_t *h = (history_t *) p;
424 1.16 lukem
425 1.22 christos if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
426 1.22 christos strcmp(h->list.next->ev.str, str) == 0)
427 1.22 christos return (0);
428 1.22 christos
429 1.16 lukem if (history_def_insert(h, ev, str) == -1)
430 1.16 lukem return (-1); /* error, keep error message */
431 1.16 lukem
432 1.16 lukem /*
433 1.16 lukem * Always keep at least one entry.
434 1.16 lukem * This way we don't have to check for the empty list.
435 1.16 lukem */
436 1.18 jdolecek while (h->cur > h->max && h->cur > 0)
437 1.16 lukem history_def_delete(h, ev, h->list.prev);
438 1.7 christos
439 1.22 christos return (1);
440 1.1 cgd }
441 1.1 cgd
442 1.1 cgd
443 1.1 cgd /* history_def_init():
444 1.1 cgd * Default history initialization function
445 1.1 cgd */
446 1.11 christos /* ARGSUSED */
447 1.21 christos private int
448 1.23 christos history_def_init(ptr_t *p, HistEvent *ev __attribute__((__unused__)), int n)
449 1.16 lukem {
450 1.16 lukem history_t *h = (history_t *) h_malloc(sizeof(history_t));
451 1.21 christos if (h == NULL)
452 1.21 christos return -1;
453 1.16 lukem
454 1.16 lukem if (n <= 0)
455 1.16 lukem n = 0;
456 1.16 lukem h->eventid = 0;
457 1.16 lukem h->cur = 0;
458 1.16 lukem h->max = n;
459 1.16 lukem h->list.next = h->list.prev = &h->list;
460 1.16 lukem h->list.ev.str = NULL;
461 1.16 lukem h->list.ev.num = 0;
462 1.16 lukem h->cursor = &h->list;
463 1.22 christos h->flags = 0;
464 1.16 lukem *p = (ptr_t) h;
465 1.21 christos return 0;
466 1.1 cgd }
467 1.1 cgd
468 1.1 cgd
469 1.2 christos /* history_def_clear():
470 1.1 cgd * Default history cleanup function
471 1.1 cgd */
472 1.1 cgd private void
473 1.16 lukem history_def_clear(ptr_t p, HistEvent *ev)
474 1.16 lukem {
475 1.16 lukem history_t *h = (history_t *) p;
476 1.16 lukem
477 1.16 lukem while (h->list.prev != &h->list)
478 1.16 lukem history_def_delete(h, ev, h->list.prev);
479 1.16 lukem h->eventid = 0;
480 1.16 lukem h->cur = 0;
481 1.1 cgd }
482 1.1 cgd
483 1.2 christos
484 1.2 christos
485 1.2 christos
486 1.1 cgd /************************************************************************/
487 1.1 cgd
488 1.1 cgd /* history_init():
489 1.1 cgd * Initialization function.
490 1.1 cgd */
491 1.1 cgd public History *
492 1.16 lukem history_init(void)
493 1.1 cgd {
494 1.21 christos HistEvent ev;
495 1.16 lukem History *h = (History *) h_malloc(sizeof(History));
496 1.21 christos if (h == NULL)
497 1.21 christos return NULL;
498 1.1 cgd
499 1.21 christos if (history_def_init(&h->h_ref, &ev, 0) == -1) {
500 1.21 christos h_free((ptr_t)h);
501 1.21 christos return NULL;
502 1.21 christos }
503 1.16 lukem h->h_ent = -1;
504 1.16 lukem h->h_next = history_def_next;
505 1.16 lukem h->h_first = history_def_first;
506 1.16 lukem h->h_last = history_def_last;
507 1.16 lukem h->h_prev = history_def_prev;
508 1.16 lukem h->h_curr = history_def_curr;
509 1.16 lukem h->h_set = history_def_set;
510 1.16 lukem h->h_clear = history_def_clear;
511 1.16 lukem h->h_enter = history_def_enter;
512 1.16 lukem h->h_add = history_def_add;
513 1.1 cgd
514 1.16 lukem return (h);
515 1.1 cgd }
516 1.1 cgd
517 1.1 cgd
518 1.1 cgd /* history_end():
519 1.1 cgd * clean up history;
520 1.1 cgd */
521 1.1 cgd public void
522 1.16 lukem history_end(History *h)
523 1.1 cgd {
524 1.16 lukem HistEvent ev;
525 1.16 lukem
526 1.16 lukem if (h->h_next == history_def_next)
527 1.16 lukem history_def_clear(h->h_ref, &ev);
528 1.1 cgd }
529 1.1 cgd
530 1.1 cgd
531 1.1 cgd
532 1.12 christos /* history_setsize():
533 1.1 cgd * Set history number of events
534 1.1 cgd */
535 1.1 cgd private int
536 1.16 lukem history_setsize(History *h, HistEvent *ev, int num)
537 1.16 lukem {
538 1.7 christos
539 1.16 lukem if (h->h_next != history_def_next) {
540 1.16 lukem he_seterrev(ev, _HE_NOT_ALLOWED);
541 1.16 lukem return (-1);
542 1.16 lukem }
543 1.16 lukem if (num < 0) {
544 1.16 lukem he_seterrev(ev, _HE_BAD_PARAM);
545 1.16 lukem return (-1);
546 1.16 lukem }
547 1.16 lukem history_def_setsize(h->h_ref, num);
548 1.16 lukem return (0);
549 1.1 cgd }
550 1.1 cgd
551 1.16 lukem
552 1.12 christos /* history_getsize():
553 1.7 christos * Get number of events currently in history
554 1.7 christos */
555 1.7 christos private int
556 1.16 lukem history_getsize(History *h, HistEvent *ev)
557 1.16 lukem {
558 1.22 christos if (h->h_next != history_def_next) {
559 1.22 christos he_seterrev(ev, _HE_NOT_ALLOWED);
560 1.22 christos return (-1);
561 1.22 christos }
562 1.22 christos ev->num = history_def_getsize(h->h_ref);
563 1.22 christos if (ev->num < -1) {
564 1.22 christos he_seterrev(ev, _HE_SIZE_NEGATIVE);
565 1.22 christos return (-1);
566 1.22 christos }
567 1.22 christos return (0);
568 1.22 christos }
569 1.22 christos
570 1.22 christos
571 1.22 christos /* history_setunique():
572 1.22 christos * Set if adjacent equal events should not be entered in history.
573 1.22 christos */
574 1.22 christos private int
575 1.22 christos history_setunique(History *h, HistEvent *ev, int uni)
576 1.22 christos {
577 1.7 christos
578 1.16 lukem if (h->h_next != history_def_next) {
579 1.16 lukem he_seterrev(ev, _HE_NOT_ALLOWED);
580 1.16 lukem return (-1);
581 1.16 lukem }
582 1.22 christos history_def_setunique(h->h_ref, uni);
583 1.22 christos return (0);
584 1.22 christos }
585 1.22 christos
586 1.22 christos
587 1.22 christos /* history_getunique():
588 1.22 christos * Get if adjacent equal events should not be entered in history.
589 1.22 christos */
590 1.22 christos private int
591 1.22 christos history_getunique(History *h, HistEvent *ev)
592 1.22 christos {
593 1.22 christos if (h->h_next != history_def_next) {
594 1.22 christos he_seterrev(ev, _HE_NOT_ALLOWED);
595 1.16 lukem return (-1);
596 1.16 lukem }
597 1.22 christos ev->num = history_def_getunique(h->h_ref);
598 1.16 lukem return (0);
599 1.7 christos }
600 1.1 cgd
601 1.16 lukem
602 1.1 cgd /* history_set_fun():
603 1.1 cgd * Set history functions
604 1.1 cgd */
605 1.1 cgd private int
606 1.16 lukem history_set_fun(History *h, History *nh)
607 1.16 lukem {
608 1.16 lukem HistEvent ev;
609 1.16 lukem
610 1.16 lukem if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL ||
611 1.16 lukem nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL ||
612 1.16 lukem nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
613 1.16 lukem nh->h_ref == NULL) {
614 1.16 lukem if (h->h_next != history_def_next) {
615 1.16 lukem history_def_init(&h->h_ref, &ev, 0);
616 1.16 lukem h->h_first = history_def_first;
617 1.16 lukem h->h_next = history_def_next;
618 1.16 lukem h->h_last = history_def_last;
619 1.16 lukem h->h_prev = history_def_prev;
620 1.16 lukem h->h_curr = history_def_curr;
621 1.16 lukem h->h_set = history_def_set;
622 1.16 lukem h->h_clear = history_def_clear;
623 1.16 lukem h->h_enter = history_def_enter;
624 1.16 lukem h->h_add = history_def_add;
625 1.16 lukem }
626 1.16 lukem return (-1);
627 1.16 lukem }
628 1.16 lukem if (h->h_next == history_def_next)
629 1.16 lukem history_def_clear(h->h_ref, &ev);
630 1.16 lukem
631 1.16 lukem h->h_ent = -1;
632 1.16 lukem h->h_first = nh->h_first;
633 1.16 lukem h->h_next = nh->h_next;
634 1.16 lukem h->h_last = nh->h_last;
635 1.16 lukem h->h_prev = nh->h_prev;
636 1.16 lukem h->h_curr = nh->h_curr;
637 1.16 lukem h->h_set = nh->h_set;
638 1.16 lukem h->h_clear = nh->h_clear;
639 1.16 lukem h->h_enter = nh->h_enter;
640 1.16 lukem h->h_add = nh->h_add;
641 1.1 cgd
642 1.16 lukem return (0);
643 1.1 cgd }
644 1.1 cgd
645 1.1 cgd
646 1.2 christos /* history_load():
647 1.2 christos * History load function
648 1.2 christos */
649 1.2 christos private int
650 1.16 lukem history_load(History *h, const char *fname)
651 1.16 lukem {
652 1.16 lukem FILE *fp;
653 1.16 lukem char *line;
654 1.16 lukem size_t sz, max_size;
655 1.16 lukem char *ptr;
656 1.16 lukem int i = -1;
657 1.16 lukem HistEvent ev;
658 1.16 lukem
659 1.16 lukem if ((fp = fopen(fname, "r")) == NULL)
660 1.16 lukem return (i);
661 1.16 lukem
662 1.16 lukem if ((line = fgetln(fp, &sz)) == NULL)
663 1.16 lukem goto done;
664 1.16 lukem
665 1.16 lukem if (strncmp(line, hist_cookie, sz) != 0)
666 1.16 lukem goto done;
667 1.16 lukem
668 1.16 lukem ptr = h_malloc(max_size = 1024);
669 1.21 christos if (ptr == NULL)
670 1.21 christos goto done;
671 1.16 lukem for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
672 1.16 lukem char c = line[sz];
673 1.16 lukem
674 1.16 lukem if (sz != 0 && line[sz - 1] == '\n')
675 1.16 lukem line[--sz] = '\0';
676 1.16 lukem else
677 1.16 lukem line[sz] = '\0';
678 1.16 lukem
679 1.16 lukem if (max_size < sz) {
680 1.21 christos char *nptr;
681 1.16 lukem max_size = (sz + 1023) & ~1023;
682 1.21 christos nptr = h_realloc(ptr, max_size);
683 1.21 christos if (nptr == NULL) {
684 1.21 christos i = -1;
685 1.21 christos goto oomem;
686 1.21 christos }
687 1.21 christos ptr = nptr;
688 1.16 lukem }
689 1.16 lukem (void) strunvis(ptr, line);
690 1.16 lukem line[sz] = c;
691 1.21 christos if (HENTER(h, &ev, ptr) == -1) {
692 1.21 christos h_free((ptr_t)ptr);
693 1.21 christos return -1;
694 1.21 christos }
695 1.16 lukem }
696 1.21 christos oomem:
697 1.21 christos h_free((ptr_t)ptr);
698 1.2 christos done:
699 1.16 lukem (void) fclose(fp);
700 1.16 lukem return (i);
701 1.2 christos }
702 1.2 christos
703 1.2 christos
704 1.2 christos /* history_save():
705 1.2 christos * History save function
706 1.2 christos */
707 1.2 christos private int
708 1.16 lukem history_save(History *h, const char *fname)
709 1.16 lukem {
710 1.16 lukem FILE *fp;
711 1.16 lukem HistEvent ev;
712 1.21 christos int i = -1, retval;
713 1.16 lukem size_t len, max_size;
714 1.16 lukem char *ptr;
715 1.16 lukem
716 1.16 lukem if ((fp = fopen(fname, "w")) == NULL)
717 1.16 lukem return (-1);
718 1.16 lukem
719 1.21 christos if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
720 1.21 christos goto done;
721 1.21 christos if (fputs(hist_cookie, fp) == EOF)
722 1.21 christos goto done;
723 1.16 lukem ptr = h_malloc(max_size = 1024);
724 1.21 christos if (ptr == NULL)
725 1.21 christos goto done;
726 1.21 christos for (i = 0, retval = HLAST(h, &ev);
727 1.16 lukem retval != -1;
728 1.16 lukem retval = HPREV(h, &ev), i++) {
729 1.16 lukem len = strlen(ev.str) * 4;
730 1.16 lukem if (len >= max_size) {
731 1.21 christos char *nptr;
732 1.16 lukem max_size = (len + 1023) & 1023;
733 1.21 christos nptr = h_realloc(ptr, max_size);
734 1.21 christos if (nptr == NULL) {
735 1.21 christos i = -1;
736 1.21 christos goto oomem;
737 1.21 christos }
738 1.21 christos ptr = nptr;
739 1.16 lukem }
740 1.16 lukem (void) strvis(ptr, ev.str, VIS_WHITE);
741 1.20 christos (void) fprintf(fp, "%s\n", ptr);
742 1.16 lukem }
743 1.21 christos oomem:
744 1.21 christos h_free((ptr_t)ptr);
745 1.21 christos done:
746 1.16 lukem (void) fclose(fp);
747 1.16 lukem return (i);
748 1.2 christos }
749 1.2 christos
750 1.2 christos
751 1.1 cgd /* history_prev_event():
752 1.1 cgd * Find the previous event, with number given
753 1.1 cgd */
754 1.7 christos private int
755 1.16 lukem history_prev_event(History *h, HistEvent *ev, int num)
756 1.16 lukem {
757 1.16 lukem int retval;
758 1.7 christos
759 1.16 lukem for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
760 1.16 lukem if (ev->num == num)
761 1.16 lukem return (0);
762 1.16 lukem
763 1.16 lukem he_seterrev(ev, _HE_NOT_FOUND);
764 1.16 lukem return (-1);
765 1.1 cgd }
766 1.1 cgd
767 1.1 cgd
768 1.1 cgd /* history_next_event():
769 1.1 cgd * Find the next event, with number given
770 1.1 cgd */
771 1.7 christos private int
772 1.16 lukem history_next_event(History *h, HistEvent *ev, int num)
773 1.16 lukem {
774 1.16 lukem int retval;
775 1.16 lukem
776 1.16 lukem for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
777 1.16 lukem if (ev->num == num)
778 1.16 lukem return (0);
779 1.7 christos
780 1.16 lukem he_seterrev(ev, _HE_NOT_FOUND);
781 1.16 lukem return (-1);
782 1.1 cgd }
783 1.1 cgd
784 1.1 cgd
785 1.1 cgd /* history_prev_string():
786 1.1 cgd * Find the previous event beginning with string
787 1.1 cgd */
788 1.7 christos private int
789 1.16 lukem history_prev_string(History *h, HistEvent *ev, const char *str)
790 1.16 lukem {
791 1.16 lukem size_t len = strlen(str);
792 1.16 lukem int retval;
793 1.7 christos
794 1.16 lukem for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
795 1.16 lukem if (strncmp(str, ev->str, len) == 0)
796 1.16 lukem return (0);
797 1.16 lukem
798 1.16 lukem he_seterrev(ev, _HE_NOT_FOUND);
799 1.16 lukem return (-1);
800 1.1 cgd }
801 1.1 cgd
802 1.1 cgd
803 1.1 cgd /* history_next_string():
804 1.1 cgd * Find the next event beginning with string
805 1.1 cgd */
806 1.7 christos private int
807 1.16 lukem history_next_string(History *h, HistEvent *ev, const char *str)
808 1.16 lukem {
809 1.16 lukem size_t len = strlen(str);
810 1.16 lukem int retval;
811 1.16 lukem
812 1.16 lukem for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
813 1.16 lukem if (strncmp(str, ev->str, len) == 0)
814 1.16 lukem return (0);
815 1.7 christos
816 1.16 lukem he_seterrev(ev, _HE_NOT_FOUND);
817 1.16 lukem return (-1);
818 1.1 cgd }
819 1.1 cgd
820 1.1 cgd
821 1.1 cgd /* history():
822 1.1 cgd * User interface to history functions.
823 1.1 cgd */
824 1.7 christos int
825 1.7 christos history(History *h, HistEvent *ev, int fun, ...)
826 1.1 cgd {
827 1.16 lukem va_list va;
828 1.16 lukem const char *str;
829 1.16 lukem int retval;
830 1.16 lukem
831 1.16 lukem va_start(va, fun);
832 1.16 lukem
833 1.16 lukem he_seterrev(ev, _HE_OK);
834 1.16 lukem
835 1.16 lukem switch (fun) {
836 1.16 lukem case H_GETSIZE:
837 1.16 lukem retval = history_getsize(h, ev);
838 1.16 lukem break;
839 1.16 lukem
840 1.16 lukem case H_SETSIZE:
841 1.16 lukem retval = history_setsize(h, ev, va_arg(va, int));
842 1.22 christos break;
843 1.22 christos
844 1.22 christos case H_GETUNIQUE:
845 1.22 christos retval = history_getunique(h, ev);
846 1.22 christos break;
847 1.22 christos
848 1.22 christos case H_SETUNIQUE:
849 1.22 christos retval = history_setunique(h, ev, va_arg(va, int));
850 1.16 lukem break;
851 1.16 lukem
852 1.16 lukem case H_ADD:
853 1.16 lukem str = va_arg(va, const char *);
854 1.16 lukem retval = HADD(h, ev, str);
855 1.16 lukem break;
856 1.16 lukem
857 1.16 lukem case H_ENTER:
858 1.16 lukem str = va_arg(va, const char *);
859 1.16 lukem if ((retval = HENTER(h, ev, str)) != -1)
860 1.16 lukem h->h_ent = ev->num;
861 1.16 lukem break;
862 1.16 lukem
863 1.16 lukem case H_APPEND:
864 1.16 lukem str = va_arg(va, const char *);
865 1.16 lukem if ((retval = HSET(h, ev, h->h_ent)) != -1)
866 1.16 lukem retval = HADD(h, ev, str);
867 1.16 lukem break;
868 1.16 lukem
869 1.16 lukem case H_FIRST:
870 1.16 lukem retval = HFIRST(h, ev);
871 1.16 lukem break;
872 1.1 cgd
873 1.16 lukem case H_NEXT:
874 1.16 lukem retval = HNEXT(h, ev);
875 1.16 lukem break;
876 1.16 lukem
877 1.16 lukem case H_LAST:
878 1.16 lukem retval = HLAST(h, ev);
879 1.16 lukem break;
880 1.16 lukem
881 1.16 lukem case H_PREV:
882 1.16 lukem retval = HPREV(h, ev);
883 1.16 lukem break;
884 1.16 lukem
885 1.16 lukem case H_CURR:
886 1.16 lukem retval = HCURR(h, ev);
887 1.16 lukem break;
888 1.16 lukem
889 1.16 lukem case H_SET:
890 1.16 lukem retval = HSET(h, ev, va_arg(va, const int));
891 1.16 lukem break;
892 1.16 lukem
893 1.16 lukem case H_CLEAR:
894 1.16 lukem HCLEAR(h, ev);
895 1.16 lukem retval = 0;
896 1.16 lukem break;
897 1.16 lukem
898 1.16 lukem case H_LOAD:
899 1.16 lukem retval = history_load(h, va_arg(va, const char *));
900 1.16 lukem if (retval == -1)
901 1.16 lukem he_seterrev(ev, _HE_HIST_READ);
902 1.16 lukem break;
903 1.16 lukem
904 1.16 lukem case H_SAVE:
905 1.16 lukem retval = history_save(h, va_arg(va, const char *));
906 1.16 lukem if (retval == -1)
907 1.16 lukem he_seterrev(ev, _HE_HIST_WRITE);
908 1.16 lukem break;
909 1.16 lukem
910 1.16 lukem case H_PREV_EVENT:
911 1.16 lukem retval = history_prev_event(h, ev, va_arg(va, int));
912 1.16 lukem break;
913 1.16 lukem
914 1.16 lukem case H_NEXT_EVENT:
915 1.16 lukem retval = history_next_event(h, ev, va_arg(va, int));
916 1.16 lukem break;
917 1.1 cgd
918 1.16 lukem case H_PREV_STR:
919 1.16 lukem retval = history_prev_string(h, ev, va_arg(va, const char *));
920 1.16 lukem break;
921 1.7 christos
922 1.16 lukem case H_NEXT_STR:
923 1.16 lukem retval = history_next_string(h, ev, va_arg(va, const char *));
924 1.16 lukem break;
925 1.1 cgd
926 1.16 lukem case H_FUNC:
927 1.1 cgd {
928 1.16 lukem History hf;
929 1.16 lukem
930 1.16 lukem hf.h_ref = va_arg(va, ptr_t);
931 1.16 lukem h->h_ent = -1;
932 1.16 lukem hf.h_first = va_arg(va, history_gfun_t);
933 1.16 lukem hf.h_next = va_arg(va, history_gfun_t);
934 1.16 lukem hf.h_last = va_arg(va, history_gfun_t);
935 1.16 lukem hf.h_prev = va_arg(va, history_gfun_t);
936 1.16 lukem hf.h_curr = va_arg(va, history_gfun_t);
937 1.16 lukem hf.h_set = va_arg(va, history_sfun_t);
938 1.16 lukem hf.h_clear = va_arg(va, history_vfun_t);
939 1.16 lukem hf.h_enter = va_arg(va, history_efun_t);
940 1.16 lukem hf.h_add = va_arg(va, history_efun_t);
941 1.8 christos
942 1.16 lukem if ((retval = history_set_fun(h, &hf)) == -1)
943 1.16 lukem he_seterrev(ev, _HE_PARAM_MISSING);
944 1.16 lukem break;
945 1.16 lukem }
946 1.16 lukem
947 1.16 lukem case H_END:
948 1.16 lukem history_end(h);
949 1.16 lukem retval = 0;
950 1.16 lukem break;
951 1.16 lukem
952 1.16 lukem default:
953 1.16 lukem retval = -1;
954 1.16 lukem he_seterrev(ev, _HE_UNKNOWN);
955 1.16 lukem break;
956 1.16 lukem }
957 1.16 lukem va_end(va);
958 1.16 lukem return (retval);
959 1.1 cgd }
960