Deleted Added
full compact
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 37 unchanged lines hidden (view full) ---

46#include <string.h>
47#include <stdlib.h>
48#if __STDC__
49#include <stdarg.h>
50#else
51#include <varargs.h>
52#endif
53
54#include "histedit.h"
55
56typedef const HistEvent * (*history_gfun_t) __P((ptr_t));
57typedef const HistEvent * (*history_efun_t) __P((ptr_t, const char *));
58
59struct history {
60 ptr_t h_ref; /* Argument for history fcns */
61 history_gfun_t h_first; /* Get the first element */
62 history_gfun_t h_next; /* Get the next element */
63 history_gfun_t h_last; /* Get the last element */
64 history_gfun_t h_prev; /* Get the previous element */
65 history_gfun_t h_curr; /* Get the current element */
66 history_efun_t h_enter; /* Add an element */
67 history_efun_t h_add; /* Append to an element */
68};
69
70#define HNEXT(h) (*(h)->h_next)((h)->h_ref)
71#define HFIRST(h) (*(h)->h_first)((h)->h_ref)
72#define HPREV(h) (*(h)->h_prev)((h)->h_ref)
73#define HLAST(h) (*(h)->h_last)((h)->h_ref)
74#define HCURR(h) (*(h)->h_curr)((h)->h_ref)
75#define HENTER(h, str) (*(h)->h_enter)((h)->h_ref, str)
76#define HADD(h, str) (*(h)->h_add)((h)->h_ref, str)
77
78#define h_malloc(a) malloc(a)
79#define h_free(a) free(a)
80
81
82private int history_set_num __P((History *, int));
83private int history_set_fun __P((History *, history_gfun_t,
84 history_gfun_t,
85 history_gfun_t,
86 history_gfun_t,
87 history_gfun_t,
88 history_efun_t,
89 history_efun_t, ptr_t));
90private const HistEvent *history_prev_event __P((History *, int));
91private const HistEvent *history_next_event __P((History *, int));
92private const HistEvent *history_next_string __P((History *, const char *));
93private const HistEvent *history_prev_string __P((History *, const char *));
94
95
96/***********************************************************************/
97

--- 17 unchanged lines hidden (view full) ---

115private const HistEvent *history_def_first __P((ptr_t));
116private const HistEvent *history_def_last __P((ptr_t));
117private const HistEvent *history_def_next __P((ptr_t));
118private const HistEvent *history_def_prev __P((ptr_t));
119private const HistEvent *history_def_curr __P((ptr_t));
120private const HistEvent *history_def_enter __P((ptr_t, const char *));
121private const HistEvent *history_def_add __P((ptr_t, const char *));
122private void history_def_init __P((ptr_t *, int));
123private void history_def_end __P((ptr_t));
124private const HistEvent *history_def_insert __P((history_t *, const char *));
125private void history_def_delete __P((history_t *, hentry_t *));
126
127#define history_def_set(p, num) (void) (((history_t *) p)->max = (num))
128
129
130/* history_def_first():
131 * Default function to return the first event in the history.

--- 94 unchanged lines hidden (view full) ---

226 history_t *h = (history_t *) p;
227 size_t len;
228 char *s;
229
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);
235 (void) strcat(s, str);
236 h_free((ptr_t) h->cursor->ev.str);
237 h->cursor->ev.str = s;
238 return &h->cursor->ev;
239}
240
241
242/* history_def_delete():
243 * Delete element hp of the h list

--- 75 unchanged lines hidden (view full) ---

319 h->list.next = h->list.prev = &h->list;
320 h->list.ev.str = NULL;
321 h->list.ev.num = 0;
322 h->cursor = &h->list;
323 *p = (ptr_t) h;
324}
325
326
327/* history_def_end():
328 * Default history cleanup function
329 */
330private void
331history_def_end(p)
332 ptr_t p;
333{
334 history_t *h = (history_t *) p;
335
336 while (h->list.prev != &h->list)
337 history_def_delete(h, h->list.prev);
338}
339
340/************************************************************************/
341
342/* history_init():
343 * Initialization function.
344 */
345public History *
346history_init()
347{
348 History *h = (History *) h_malloc(sizeof(History));
349
350 history_def_init(&h->h_ref, 0);
351
352 h->h_next = history_def_next;
353 h->h_first = history_def_first;
354 h->h_last = history_def_last;
355 h->h_prev = history_def_prev;
356 h->h_curr = history_def_curr;
357 h->h_enter = history_def_enter;
358 h->h_add = history_def_add;
359
360 return h;
361}
362
363
364/* history_end():
365 * clean up history;
366 */
367public void
368history_end(h)
369 History *h;
370{
371 if (h->h_next == history_def_next)
372 history_def_end(h->h_ref);
373}
374
375
376
377/* history_set_num():
378 * Set history number of events
379 */
380private int

--- 7 unchanged lines hidden (view full) ---

388 return 0;
389}
390
391
392/* history_set_fun():
393 * Set history functions
394 */
395private int
396history_set_fun(h, first, next, last, prev, curr, enter, add, ptr)
397 History *h;
398 history_gfun_t first, next, last, prev, curr;
399 history_efun_t enter, add;
400 ptr_t ptr;
401{
402 if (first == NULL || next == NULL ||
403 last == NULL || prev == NULL || curr == NULL ||
404 enter == NULL || add == NULL ||
405 ptr == 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_enter = history_def_enter;
414 h->h_add = history_def_add;
415 }
416 return -1;
417 }
418
419 if (h->h_next == history_def_next)
420 history_def_end(h->h_ref);
421
422 h->h_next = next;
423 h->h_first = first;
424 h->h_enter = enter;
425 h->h_add = add;
426 return 0;
427}
428
429
430/* history_prev_event():
431 * Find the previous event, with number given
432 */
433private const HistEvent *
434history_prev_event(h, num)
435 History *h;
436 int num;
437{

--- 66 unchanged lines hidden (view full) ---

504#else
505history(va_alist)
506 va_dcl
507#endif
508{
509 va_list va;
510 const HistEvent *ev = NULL;
511 const char *str;
512 static const HistEvent sev = { 0, "" };
513
514#if __STDC__
515 va_start(va, fun);
516#else
517 History *h;
518 int fun;
519 va_start(va);
520 h = va_arg(va, History *);

--- 26 unchanged lines hidden (view full) ---

547 case H_PREV:
548 ev = HPREV(h);
549 break;
550
551 case H_CURR:
552 ev = HCURR(h);
553 break;
554
555 case H_PREV_EVENT:
556 ev = history_prev_event(h, va_arg(va, int));
557 break;
558
559 case H_NEXT_EVENT:
560 ev = history_next_event(h, va_arg(va, int));
561 break;
562
563 case H_PREV_STR:
564 ev = history_prev_string(h, va_arg(va, const char*));
565 break;
566
567 case H_NEXT_STR:
568 ev = history_next_string(h, va_arg(va, const char*));
569 break;
570
571 case H_EVENT:
572 if (history_set_num(h, va_arg(va, int)) == 0)
573 ev = &sev;
574 break;
575
576 case H_FUNC:
577 {
578 history_gfun_t first = va_arg(va, history_gfun_t);
579 history_gfun_t next = va_arg(va, history_gfun_t);
580 history_gfun_t last = va_arg(va, history_gfun_t);
581 history_gfun_t prev = va_arg(va, history_gfun_t);
582 history_gfun_t curr = va_arg(va, history_gfun_t);
583 history_efun_t enter = va_arg(va, history_efun_t);
584 history_efun_t add = va_arg(va, history_efun_t);
585 ptr_t ptr = va_arg(va, ptr_t);
586
587 if (history_set_fun(h, first, next, last, prev,
588 curr, enter, add, ptr) == 0)
589 ev = &sev;
590 }
591 break;
592
593 case H_END:
594 history_end(h);
595 break;
596
597 default:
598 break;
599 }
600 va_end(va);
601 return ev;
602}