history.c 22 KB
Newer Older
1
/*	$NetBSD: history.c,v 1.33 2009/02/06 14:40:32 sketch Exp $	*/
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Christos Zoulas of Cornell University.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
18
 * 3. Neither the name of the University nor the names of its contributors
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

35 36 37 38 39 40 41
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
#if 0
static char sccsid[] = "@(#)history.c	8.1 (Berkeley) 6/4/93";
#else
#endif
#endif /* not lint && not SCCSID */
42 43 44 45 46 47 48

/*
 * hist.c: History access functions
 */
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
49
#ifdef HAVE_VIS_H
50
#include <vis.h>
51 52 53
#else
#include "np/vis.h"
#endif
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <sys/stat.h>

static const char hist_cookie[] = "_HiStOrY_V2_\n";

#include "histedit.h"

typedef int (*history_gfun_t)(ptr_t, HistEvent *);
typedef int (*history_efun_t)(ptr_t, HistEvent *, const char *);
typedef void (*history_vfun_t)(ptr_t, HistEvent *);
typedef int (*history_sfun_t)(ptr_t, HistEvent *, const int);

struct history {
	ptr_t h_ref;		/* Argument for history fcns	 */
	int h_ent;		/* Last entry point for history	 */
	history_gfun_t h_first;	/* Get the first element	 */
	history_gfun_t h_next;	/* Get the next element		 */
	history_gfun_t h_last;	/* Get the last element		 */
	history_gfun_t h_prev;	/* Get the previous element	 */
	history_gfun_t h_curr;	/* Get the current element	 */
	history_sfun_t h_set;	/* Set the current element	 */
74
	history_sfun_t h_del;	/* Set the given element	 */
75 76 77 78
	history_vfun_t h_clear;	/* Clear the history list	 */
	history_efun_t h_enter;	/* Add an element		 */
	history_efun_t h_add;	/* Append to an element		 */
};
79

80 81 82 83 84 85 86 87 88
#define	HNEXT(h, ev)		(*(h)->h_next)((h)->h_ref, ev)
#define	HFIRST(h, ev)		(*(h)->h_first)((h)->h_ref, ev)
#define	HPREV(h, ev)		(*(h)->h_prev)((h)->h_ref, ev)
#define	HLAST(h, ev)		(*(h)->h_last)((h)->h_ref, ev)
#define	HCURR(h, ev)		(*(h)->h_curr)((h)->h_ref, ev)
#define	HSET(h, ev, n)		(*(h)->h_set)((h)->h_ref, ev, n)
#define	HCLEAR(h, ev)		(*(h)->h_clear)((h)->h_ref, ev)
#define	HENTER(h, ev, str)	(*(h)->h_enter)((h)->h_ref, ev, str)
#define	HADD(h, ev, str)	(*(h)->h_add)((h)->h_ref, ev, str)
89
#define	HDEL(h, ev, n)		(*(h)->h_del)((h)->h_ref, ev, n)
90

91
#define	h_strdup(a)	strdup(a)
92 93 94 95
#define	h_malloc(a)	malloc(a)
#define	h_realloc(a, b)	realloc((a), (b))
#define	h_free(a)	free(a)

96 97 98 99 100 101
typedef struct {
    int		num;
    char	*str;
} HistEventPrivate;


102 103 104

private int history_setsize(History *, HistEvent *, int);
private int history_getsize(History *, HistEvent *);
105 106
private int history_setunique(History *, HistEvent *, int);
private int history_getunique(History *, HistEvent *);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
private int history_set_fun(History *, History *);
private int history_load(History *, const char *);
private int history_save(History *, const char *);
private int history_prev_event(History *, HistEvent *, int);
private int history_next_event(History *, HistEvent *, int);
private int history_next_string(History *, HistEvent *, const char *);
private int history_prev_string(History *, HistEvent *, const char *);


/***********************************************************************/

/*
 * Builtin- history implementation
 */
typedef struct hentry_t {
	HistEvent ev;		/* What we return		 */
	struct hentry_t *next;	/* Next entry			 */
	struct hentry_t *prev;	/* Previous entry		 */
125
} hentry_t;
126 127

typedef struct history_t {
128 129 130 131
	hentry_t list;		/* Fake list header element	*/
	hentry_t *cursor;	/* Current element in the list	*/
	int max;		/* Maximum number of events	*/
	int cur;		/* Current number of events	*/
132
	int eventid;		/* For generation of unique event id	 */
133 134 135
	int flags;		/* History flags		*/
#define H_UNIQUE	1	/* Store only unique elements	*/
} history_t;
136 137

private int history_def_next(ptr_t, HistEvent *);
138
private int history_def_first(ptr_t, HistEvent *);
139
private int history_def_prev(ptr_t, HistEvent *);
140
private int history_def_last(ptr_t, HistEvent *);
141
private int history_def_curr(ptr_t, HistEvent *);
142 143
private int history_def_set(ptr_t, HistEvent *, const int);
private void history_def_clear(ptr_t, HistEvent *);
144 145
private int history_def_enter(ptr_t, HistEvent *, const char *);
private int history_def_add(ptr_t, HistEvent *, const char *);
146 147
private int history_def_del(ptr_t, HistEvent *, const int);

148
private int history_def_init(ptr_t *, HistEvent *, int);
149 150 151
private int history_def_insert(history_t *, HistEvent *, const char *);
private void history_def_delete(history_t *, HistEvent *, hentry_t *);

152 153 154 155 156 157 158 159
#define	history_def_setsize(p, num)(void) (((history_t *)p)->max = (num))
#define	history_def_getsize(p)  (((history_t *)p)->cur)
#define	history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0)
#define	history_def_setunique(p, uni) \
    if (uni) \
	(((history_t *)p)->flags) |= H_UNIQUE; \
    else \
	(((history_t *)p)->flags) &= ~H_UNIQUE
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

#define	he_strerror(code)	he_errlist[code]
#define	he_seterrev(evp, code)	{\
				    evp->num = code;\
				    evp->str = he_strerror(code);\
				}

/* error messages */
static const char *const he_errlist[] = {
	"OK",
	"unknown error",
	"malloc() failed",
	"first event not found",
	"last event not found",
	"empty list",
	"no next event",
	"no previous event",
	"current event is invalid",
	"event not found",
	"can't read history from file",
	"can't write history",
	"required parameter(s) not supplied",
	"history size negative",
	"function not allowed with other history-functions-set the default",
	"bad parameters"
};
/* error codes */
#define	_HE_OK                   0
#define	_HE_UNKNOWN		 1
#define	_HE_MALLOC_FAILED        2
#define	_HE_FIRST_NOTFOUND       3
#define	_HE_LAST_NOTFOUND        4
#define	_HE_EMPTY_LIST           5
#define	_HE_END_REACHED          6
#define	_HE_START_REACHED	 7
#define	_HE_CURR_INVALID	 8
#define	_HE_NOT_FOUND		 9
#define	_HE_HIST_READ		10
#define	_HE_HIST_WRITE		11
#define	_HE_PARAM_MISSING	12
#define	_HE_SIZE_NEGATIVE	13
#define	_HE_NOT_ALLOWED		14
#define	_HE_BAD_PARAM		15

/* history_def_first():
 *	Default function to return the first event in the history.
 */
private int
history_def_first(ptr_t p, HistEvent *ev)
{
	history_t *h = (history_t *) p;

	h->cursor = h->list.next;
	if (h->cursor != &h->list)
		*ev = h->cursor->ev;
	else {
		he_seterrev(ev, _HE_FIRST_NOTFOUND);
		return (-1);
	}

	return (0);
}


/* history_def_last():
 *	Default function to return the last event in the history.
 */
private int
history_def_last(ptr_t p, HistEvent *ev)
{
	history_t *h = (history_t *) p;

	h->cursor = h->list.prev;
	if (h->cursor != &h->list)
		*ev = h->cursor->ev;
	else {
		he_seterrev(ev, _HE_LAST_NOTFOUND);
		return (-1);
	}

	return (0);
}


/* history_def_next():
 *	Default function to return the next event in the history.
 */
private int
history_def_next(ptr_t p, HistEvent *ev)
{
	history_t *h = (history_t *) p;

252
	if (h->cursor == &h->list) {
253 254 255 256
		he_seterrev(ev, _HE_EMPTY_LIST);
		return (-1);
	}

257
	if (h->cursor->next == &h->list) {
258 259 260 261
		he_seterrev(ev, _HE_END_REACHED);
		return (-1);
	}

262 263 264
        h->cursor = h->cursor->next;
        *ev = h->cursor->ev;

265 266 267 268 269 270 271 272 273 274 275 276
	return (0);
}


/* history_def_prev():
 *	Default function to return the previous event in the history.
 */
private int
history_def_prev(ptr_t p, HistEvent *ev)
{
	history_t *h = (history_t *) p;

277
	if (h->cursor == &h->list) {
278 279 280 281 282
		he_seterrev(ev,
		    (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
		return (-1);
	}

283
	if (h->cursor->prev == &h->list) {
284 285 286 287
		he_seterrev(ev, _HE_START_REACHED);
		return (-1);
	}

288 289 290
        h->cursor = h->cursor->prev;
        *ev = h->cursor->ev;

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	return (0);
}


/* history_def_curr():
 *	Default function to return the current event in the history.
 */
private int
history_def_curr(ptr_t p, HistEvent *ev)
{
	history_t *h = (history_t *) p;

	if (h->cursor != &h->list)
		*ev = h->cursor->ev;
	else {
		he_seterrev(ev,
		    (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
		return (-1);
	}

	return (0);
}


/* history_def_set():
 *	Default function to set the current event in the history to the
 *	given one.
 */
private int
history_def_set(ptr_t p, HistEvent *ev, const int n)
{
	history_t *h = (history_t *) p;

	if (h->cur == 0) {
		he_seterrev(ev, _HE_EMPTY_LIST);
		return (-1);
	}
	if (h->cursor == &h->list || h->cursor->ev.num != n) {
		for (h->cursor = h->list.next; h->cursor != &h->list;
		    h->cursor = h->cursor->next)
			if (h->cursor->ev.num == n)
				break;
	}
	if (h->cursor == &h->list) {
		he_seterrev(ev, _HE_NOT_FOUND);
		return (-1);
	}
	return (0);
}


/* history_def_add():
 *	Append string to element
 */
private int
history_def_add(ptr_t p, HistEvent *ev, const char *str)
{
	history_t *h = (history_t *) p;
	size_t len;
	char *s;
351
	HistEventPrivate *evp = (void *)&h->cursor->ev;
352 353 354

	if (h->cursor == &h->list)
		return (history_def_enter(p, ev, str));
355
	len = strlen(evp->str) + strlen(str) + 1;
356
	s = (char *) h_malloc(len);
357
	if (s == NULL) {
358 359 360 361 362
		he_seterrev(ev, _HE_MALLOC_FAILED);
		return (-1);
	}
	(void) strlcpy(s, h->cursor->ev.str, len);
	(void) strlcat(s, str, len);
363 364
	h_free((ptr_t)evp->str);
	evp->str = s;
365 366 367 368 369
	*ev = h->cursor->ev;
	return (0);
}


370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
/* history_def_del():
 *	Delete element hp of the h list
 */
/* ARGSUSED */
private int
history_def_del(ptr_t p, HistEvent *ev __attribute__((__unused__)),
    const int num)
{
	history_t *h = (history_t *) p;
	if (history_def_set(h, ev, num) != 0)
		return (-1);
	ev->str = strdup(h->cursor->ev.str);
	ev->num = h->cursor->ev.num;
	history_def_delete(h, ev, h->cursor);
	return (0);
}


388 389 390 391 392
/* history_def_delete():
 *	Delete element hp of the h list
 */
/* ARGSUSED */
private void
393 394
history_def_delete(history_t *h, 
		   HistEvent *ev __attribute__((__unused__)), hentry_t *hp)
395
{
396
	HistEventPrivate *evp = (void *)&hp->ev;
397 398
	if (hp == &h->list)
		abort();
399 400
	if (h->cursor == hp)
		h->cursor = hp->prev;
401 402
	hp->prev->next = hp->next;
	hp->next->prev = hp->prev;
403
	h_free((ptr_t) evp->str);
404 405 406 407 408 409 410 411 412 413 414 415 416
	h_free(hp);
	h->cur--;
}


/* history_def_insert():
 *	Insert element with string str in the h list
 */
private int
history_def_insert(history_t *h, HistEvent *ev, const char *str)
{

	h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
417 418
	if (h->cursor == NULL)
		goto oomem;
419
	if ((h->cursor->ev.str = h_strdup(str)) == NULL) {
420 421
		h_free((ptr_t)h->cursor);
		goto oomem;
422 423 424 425 426 427 428 429 430 431
	}
	h->cursor->ev.num = ++h->eventid;
	h->cursor->next = h->list.next;
	h->cursor->prev = &h->list;
	h->list.next->prev = h->cursor;
	h->list.next = h->cursor;
	h->cur++;

	*ev = h->cursor->ev;
	return (0);
432 433 434
oomem:
	he_seterrev(ev, _HE_MALLOC_FAILED);
	return (-1);
435 436 437 438 439 440 441 442 443 444 445
}


/* history_def_enter():
 *	Default function to enter an item in the history
 */
private int
history_def_enter(ptr_t p, HistEvent *ev, const char *str)
{
	history_t *h = (history_t *) p;

446 447 448 449
	if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
	    strcmp(h->list.next->ev.str, str) == 0)
	    return (0); 

450 451 452 453 454 455 456
	if (history_def_insert(h, ev, str) == -1)
		return (-1);	/* error, keep error message */

	/*
         * Always keep at least one entry.
         * This way we don't have to check for the empty list.
         */
457
	while (h->cur > h->max && h->cur > 0)
458 459
		history_def_delete(h, ev, h->list.prev);

460
	return (1);
461 462 463 464 465 466 467
}


/* history_def_init():
 *	Default history initialization function
 */
/* ARGSUSED */
468
private int
469
history_def_init(ptr_t *p, HistEvent *ev __attribute__((__unused__)), int n)
470 471
{
	history_t *h = (history_t *) h_malloc(sizeof(history_t));
472 473
	if (h == NULL)
		return -1;
474 475 476 477 478 479 480 481 482 483

	if (n <= 0)
		n = 0;
	h->eventid = 0;
	h->cur = 0;
	h->max = n;
	h->list.next = h->list.prev = &h->list;
	h->list.ev.str = NULL;
	h->list.ev.num = 0;
	h->cursor = &h->list;
484
	h->flags = 0;
485
	*p = (ptr_t) h;
486
	return 0;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
}


/* history_def_clear():
 *	Default history cleanup function
 */
private void
history_def_clear(ptr_t p, HistEvent *ev)
{
	history_t *h = (history_t *) p;

	while (h->list.prev != &h->list)
		history_def_delete(h, ev, h->list.prev);
	h->eventid = 0;
	h->cur = 0;
}




/************************************************************************/

/* history_init():
 *	Initialization function.
 */
public History *
history_init(void)
{
	HistEvent ev;
516 517 518
	History *h = (History *) h_malloc(sizeof(History));
	if (h == NULL)
		return NULL;
519

520 521 522 523
	if (history_def_init(&h->h_ref, &ev, 0) == -1) {
		h_free((ptr_t)h);
		return NULL;
	}
524 525 526 527 528 529 530 531 532 533
	h->h_ent = -1;
	h->h_next = history_def_next;
	h->h_first = history_def_first;
	h->h_last = history_def_last;
	h->h_prev = history_def_prev;
	h->h_curr = history_def_curr;
	h->h_set = history_def_set;
	h->h_clear = history_def_clear;
	h->h_enter = history_def_enter;
	h->h_add = history_def_add;
534
	h->h_del = history_def_del;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

	return (h);
}


/* history_end():
 *	clean up history;
 */
public void
history_end(History *h)
{
	HistEvent ev;

	if (h->h_next == history_def_next)
		history_def_clear(h->h_ref, &ev);
550 551
	h_free(h->h_ref);
	h_free(h);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
}



/* history_setsize():
 *	Set history number of events
 */
private int
history_setsize(History *h, HistEvent *ev, int num)
{

	if (h->h_next != history_def_next) {
		he_seterrev(ev, _HE_NOT_ALLOWED);
		return (-1);
	}
	if (num < 0) {
		he_seterrev(ev, _HE_BAD_PARAM);
		return (-1);
	}
	history_def_setsize(h->h_ref, num);
	return (0);
}


/* history_getsize():
 *      Get number of events currently in history
 */
private int
history_getsize(History *h, HistEvent *ev)
{
	if (h->h_next != history_def_next) {
		he_seterrev(ev, _HE_NOT_ALLOWED);
		return (-1);
	}
586 587
	ev->num = history_def_getsize(h->h_ref);
	if (ev->num < -1) {
588 589 590
		he_seterrev(ev, _HE_SIZE_NEGATIVE);
		return (-1);
	}
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	return (0);
}


/* history_setunique():
 *	Set if adjacent equal events should not be entered in history.
 */
private int
history_setunique(History *h, HistEvent *ev, int uni)
{

	if (h->h_next != history_def_next) {
		he_seterrev(ev, _HE_NOT_ALLOWED);
		return (-1);
	}
	history_def_setunique(h->h_ref, uni);
	return (0);
}


/* history_getunique():
 *	Get if adjacent equal events should not be entered in history.
 */
private int
history_getunique(History *h, HistEvent *ev)
{
	if (h->h_next != history_def_next) {
		he_seterrev(ev, _HE_NOT_ALLOWED);
		return (-1);
	}
	ev->num = history_def_getunique(h->h_ref);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	return (0);
}


/* history_set_fun():
 *	Set history functions
 */
private int
history_set_fun(History *h, History *nh)
{
	HistEvent ev;

	if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL ||
	    nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL ||
	    nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
637
	    nh->h_del == NULL || nh->h_ref == NULL) {
638 639 640 641 642 643 644 645 646 647 648
		if (h->h_next != history_def_next) {
			history_def_init(&h->h_ref, &ev, 0);
			h->h_first = history_def_first;
			h->h_next = history_def_next;
			h->h_last = history_def_last;
			h->h_prev = history_def_prev;
			h->h_curr = history_def_curr;
			h->h_set = history_def_set;
			h->h_clear = history_def_clear;
			h->h_enter = history_def_enter;
			h->h_add = history_def_add;
649
			h->h_del = history_def_del;
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		}
		return (-1);
	}
	if (h->h_next == history_def_next)
		history_def_clear(h->h_ref, &ev);

	h->h_ent = -1;
	h->h_first = nh->h_first;
	h->h_next = nh->h_next;
	h->h_last = nh->h_last;
	h->h_prev = nh->h_prev;
	h->h_curr = nh->h_curr;
	h->h_set = nh->h_set;
	h->h_clear = nh->h_clear;
	h->h_enter = nh->h_enter;
	h->h_add = nh->h_add;
666
	h->h_del = nh->h_del;
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

	return (0);
}


/* history_load():
 *	History load function
 */
private int
history_load(History *h, const char *fname)
{
	FILE *fp;
	char *line;
	size_t sz, max_size;
	char *ptr;
	int i = -1;
	HistEvent ev;

	if ((fp = fopen(fname, "r")) == NULL)
		return (i);

688 689 690 691 692 693
	if ((line = fgetln(fp, &sz)) == NULL)
		goto done;

	if (strncmp(line, hist_cookie, sz) != 0)
		goto done;

694
	ptr = h_malloc(max_size = 1024);
695 696
	if (ptr == NULL)
		goto done;
697 698 699 700 701 702 703 704 705
	for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
		char c = line[sz];

		if (sz != 0 && line[sz - 1] == '\n')
			line[--sz] = '\0';
		else
			line[sz] = '\0';

		if (max_size < sz) {
706
			char *nptr;
707
			max_size = (sz + 1024) & ~1023;
708 709 710 711 712 713
			nptr = h_realloc(ptr, max_size);
			if (nptr == NULL) {
				i = -1;
				goto oomem;
			}
			ptr = nptr;
714 715 716
		}
		(void) strunvis(ptr, line);
		line[sz] = c;
717
		if (HENTER(h, &ev, ptr) == -1) {
718 719
			i = -1;
			goto oomem;
720
		}
721
	}
722 723
oomem:
	h_free((ptr_t)ptr);
724 725 726 727 728 729 730 731 732 733 734 735 736 737
done:
	(void) fclose(fp);
	return (i);
}


/* history_save():
 *	History save function
 */
private int
history_save(History *h, const char *fname)
{
	FILE *fp;
	HistEvent ev;
738
	int i = -1, retval;
739 740 741 742 743 744
	size_t len, max_size;
	char *ptr;

	if ((fp = fopen(fname, "w")) == NULL)
		return (-1);

745 746
	if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
		goto done;
747 748
	if (fputs(hist_cookie, fp) == EOF)
		goto done;
749
	ptr = h_malloc(max_size = 1024);
750 751 752
	if (ptr == NULL)
		goto done;
	for (i = 0, retval = HLAST(h, &ev);
753 754
	    retval != -1;
	    retval = HPREV(h, &ev), i++) {
755
		len = strlen(ev.str) * 4;
756
		if (len >= max_size) {
757
			char *nptr;
758
			max_size = (len + 1024) & ~1023;
759 760 761 762 763 764
			nptr = h_realloc(ptr, max_size);
			if (nptr == NULL) {
				i = -1;
				goto oomem;
			}
			ptr = nptr;
765 766
		}
		(void) strvis(ptr, ev.str, VIS_WHITE);
767
		(void) fprintf(fp, "%s\n", ptr);
768
	}
769 770 771
oomem:
	h_free((ptr_t)ptr);
done:
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
	(void) fclose(fp);
	return (i);
}


/* history_prev_event():
 *	Find the previous event, with number given
 */
private int
history_prev_event(History *h, HistEvent *ev, int num)
{
	int retval;

	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
		if (ev->num == num)
			return (0);

	he_seterrev(ev, _HE_NOT_FOUND);
	return (-1);
}


/* history_next_event():
 *	Find the next event, with number given
 */
private int
history_next_event(History *h, HistEvent *ev, int num)
{
	int retval;

	for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
		if (ev->num == num)
			return (0);

	he_seterrev(ev, _HE_NOT_FOUND);
	return (-1);
}


/* history_prev_string():
 *	Find the previous event beginning with string
 */
private int
history_prev_string(History *h, HistEvent *ev, const char *str)
{
	size_t len = strlen(str);
	int retval;

	for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
		if (strncmp(str, ev->str, len) == 0)
			return (0);

	he_seterrev(ev, _HE_NOT_FOUND);
	return (-1);
}


/* history_next_string():
 *	Find the next event beginning with string
 */
private int
history_next_string(History *h, HistEvent *ev, const char *str)
{
	size_t len = strlen(str);
	int retval;

	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
		if (strncmp(str, ev->str, len) == 0)
			return (0);

	he_seterrev(ev, _HE_NOT_FOUND);
	return (-1);
}


/* history():
 *	User interface to history functions.
 */
int
history(History *h, HistEvent *ev, int fun, ...)
{
	va_list va;
	const char *str;
	int retval;

	va_start(va, fun);

	he_seterrev(ev, _HE_OK);

	switch (fun) {
	case H_GETSIZE:
		retval = history_getsize(h, ev);
		break;

	case H_SETSIZE:
		retval = history_setsize(h, ev, va_arg(va, int));
		break;

870 871 872 873 874 875 876 877
	case H_GETUNIQUE:
		retval = history_getunique(h, ev);
		break;

	case H_SETUNIQUE:
		retval = history_setunique(h, ev, va_arg(va, int));
		break;

878 879 880 881 882
	case H_ADD:
		str = va_arg(va, const char *);
		retval = HADD(h, ev, str);
		break;

883 884 885 886
	case H_DEL:
		retval = HDEL(h, ev, va_arg(va, const int));
		break;

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	case H_ENTER:
		str = va_arg(va, const char *);
		if ((retval = HENTER(h, ev, str)) != -1)
			h->h_ent = ev->num;
		break;

	case H_APPEND:
		str = va_arg(va, const char *);
		if ((retval = HSET(h, ev, h->h_ent)) != -1)
			retval = HADD(h, ev, str);
		break;

	case H_FIRST:
		retval = HFIRST(h, ev);
		break;

	case H_NEXT:
		retval = HNEXT(h, ev);
		break;

	case H_LAST:
		retval = HLAST(h, ev);
		break;

	case H_PREV:
		retval = HPREV(h, ev);
		break;

	case H_CURR:
		retval = HCURR(h, ev);
		break;

	case H_SET:
		retval = HSET(h, ev, va_arg(va, const int));
		break;

	case H_CLEAR:
		HCLEAR(h, ev);
		retval = 0;
		break;

	case H_LOAD:
		retval = history_load(h, va_arg(va, const char *));
		if (retval == -1)
			he_seterrev(ev, _HE_HIST_READ);
		break;

	case H_SAVE:
		retval = history_save(h, va_arg(va, const char *));
		if (retval == -1)
			he_seterrev(ev, _HE_HIST_WRITE);
		break;

	case H_PREV_EVENT:
		retval = history_prev_event(h, ev, va_arg(va, int));
		break;

	case H_NEXT_EVENT:
		retval = history_next_event(h, ev, va_arg(va, int));
		break;

	case H_PREV_STR:
		retval = history_prev_string(h, ev, va_arg(va, const char *));
		break;

	case H_NEXT_STR:
		retval = history_next_string(h, ev, va_arg(va, const char *));
		break;

	case H_FUNC:
	{
		History hf;

		hf.h_ref = va_arg(va, ptr_t);
		h->h_ent = -1;
		hf.h_first = va_arg(va, history_gfun_t);
		hf.h_next = va_arg(va, history_gfun_t);
		hf.h_last = va_arg(va, history_gfun_t);
		hf.h_prev = va_arg(va, history_gfun_t);
		hf.h_curr = va_arg(va, history_gfun_t);
		hf.h_set = va_arg(va, history_sfun_t);
		hf.h_clear = va_arg(va, history_vfun_t);
		hf.h_enter = va_arg(va, history_efun_t);
		hf.h_add = va_arg(va, history_efun_t);
971
		hf.h_del = va_arg(va, history_sfun_t);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

		if ((retval = history_set_fun(h, &hf)) == -1)
			he_seterrev(ev, _HE_PARAM_MISSING);
		break;
	}

	case H_END:
		history_end(h);
		retval = 0;
		break;

	default:
		retval = -1;
		he_seterrev(ev, _HE_UNKNOWN);
		break;
	}
	va_end(va);
	return (retval);
}