summary refs log tree commit diff
path: root/bin/cash/libedit/history.c
blob: 005fe2a0c0d96b4453b87672e3df266a9b93d8aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
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
586
587
588
589
590
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
/*	$NetBSD: history.c,v 1.57 2016/04/11 18:56:31 christos Exp $	*/

/*-
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    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.
 */

#include "config.h"
#if !defined(lint) && !defined(SCCSID)
#if 0
static char sccsid[] = "@(#)history.c	8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: history.c,v 1.57 2016/04/11 18:56:31 christos Exp $");
#endif
#endif /* not lint && not SCCSID */

/*
 * hist.c: TYPE(History) access functions
 */
#include <sys/file.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <vis.h>

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

#include "histedit.h"


#ifdef NARROWCHAR

#define	Char			char
#define	FUN(prefix, rest)	prefix ## _ ## rest
#define	FUNW(type)		type
#define	TYPE(type)		type
#define	STR(x)			x

#define	Strlen(s)		strlen(s)
#define	Strdup(s)		strdup(s)
#define	Strcmp(d, s)		strcmp(d, s)
#define	Strncmp(d, s, n)	strncmp(d, s, n)
#define	Strncpy(d, s, n)	strncpy(d, s, n)
#define	Strncat(d, s, n)	strncat(d, s, n)
#define	ct_decode_string(s, b)	(s)
#define	ct_encode_string(s, b)	(s)

#else
#include "chartype.h"

#define	Char			wchar_t
#define	FUN(prefix, rest)	prefix ## _w ## rest
#define	FUNW(type)		type ## _w
#define	TYPE(type)		type ## W
#define	STR(x)			L ## x

#define	Strlen(s)		wcslen(s)
#define	Strdup(s)		wcsdup(s)
#define	Strcmp(d, s)		wcscmp(d, s)
#define	Strncmp(d, s, n)	wcsncmp(d, s, n)
#define	Strncpy(d, s, n)	wcsncpy(d, s, n)
#define	Strncat(d, s, n)	wcsncat(d, s, n)

#endif


typedef int (*history_gfun_t)(void *, TYPE(HistEvent) *);
typedef int (*history_efun_t)(void *, TYPE(HistEvent) *, const Char *);
typedef void (*history_vfun_t)(void *, TYPE(HistEvent) *);
typedef int (*history_sfun_t)(void *, TYPE(HistEvent) *, const int);

struct TYPE(history) {
	void *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	 */
	history_sfun_t h_del;	/* Set the given element	 */
	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		 */
};

#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)
#define	HDEL(h, ev, n)		(*(h)->h_del)((h)->h_ref, ev, n)

#define	h_strdup(a)	Strdup(a)
#define	h_malloc(a)	malloc(a)
#define	h_realloc(a, b)	realloc((a), (b))
#define	h_free(a)	free(a)

typedef struct {
    int		num;
    Char	*str;
} HistEventPrivate;


static int history_setsize(TYPE(History) *, TYPE(HistEvent) *, int);
static int history_getsize(TYPE(History) *, TYPE(HistEvent) *);
static int history_setunique(TYPE(History) *, TYPE(HistEvent) *, int);
static int history_getunique(TYPE(History) *, TYPE(HistEvent) *);
static int history_set_fun(TYPE(History) *, TYPE(History) *);
static int history_load(TYPE(History) *, const char *);
static int history_save(TYPE(History) *, const char *);
static int history_save_fp(TYPE(History) *, FILE *);
static int history_prev_event(TYPE(History) *, TYPE(HistEvent) *, int);
static int history_next_event(TYPE(History) *, TYPE(HistEvent) *, int);
static int history_next_string(TYPE(History) *, TYPE(HistEvent) *,
    const Char *);
static int history_prev_string(TYPE(History) *, TYPE(HistEvent) *,
    const Char *);


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

/*
 * Builtin- history implementation
 */
typedef struct hentry_t {
	TYPE(HistEvent) ev;		/* What we return		 */
	void *data;		/* data				 */
	struct hentry_t *next;	/* Next entry			 */
	struct hentry_t *prev;	/* Previous entry		 */
} hentry_t;

typedef struct history_t {
	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	*/
	int eventid;		/* For generation of unique event id	 */
	int flags;		/* TYPE(History) flags		*/
#define H_UNIQUE	1	/* Store only unique elements	*/
} history_t;

static int history_def_next(void *, TYPE(HistEvent) *);
static int history_def_first(void *, TYPE(HistEvent) *);
static int history_def_prev(void *, TYPE(HistEvent) *);
static int history_def_last(void *, TYPE(HistEvent) *);
static int history_def_curr(void *, TYPE(HistEvent) *);
static int history_def_set(void *, TYPE(HistEvent) *, const int);
static void history_def_clear(void *, TYPE(HistEvent) *);
static int history_def_enter(void *, TYPE(HistEvent) *, const Char *);
static int history_def_add(void *, TYPE(HistEvent) *, const Char *);
static int history_def_del(void *, TYPE(HistEvent) *, const int);

static int history_def_init(void **, TYPE(HistEvent) *, int);
static int history_def_insert(history_t *, TYPE(HistEvent) *, const Char *);
static void history_def_delete(history_t *, TYPE(HistEvent) *, hentry_t *);

static int history_deldata_nth(history_t *, TYPE(HistEvent) *, int, void **);
static int history_set_nth(void *, TYPE(HistEvent) *, int);

#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

#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[] = {
	STR("OK"),
	STR("unknown error"),
	STR("malloc() failed"),
	STR("first event not found"),
	STR("last event not found"),
	STR("empty list"),
	STR("no next event"),
	STR("no previous event"),
	STR("current event is invalid"),
	STR("event not found"),
	STR("can't read history from file"),
	STR("can't write history"),
	STR("required parameter(s) not supplied"),
	STR("history size negative"),
	STR("function not allowed with other history-functions-set the default"),
	STR("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.
 */
static int
history_def_first(void *p, TYPE(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.
 */
static int
history_def_last(void *p, TYPE(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.
 */
static int
history_def_next(void *p, TYPE(HistEvent) *ev)
{
	history_t *h = (history_t *) p;

	if (h->cursor == &h->list) {
		he_seterrev(ev, _HE_EMPTY_LIST);
		return -1;
	}

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

        h->cursor = h->cursor->next;
        *ev = h->cursor->ev;

	return 0;
}


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

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

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

        h->cursor = h->cursor->prev;
        *ev = h->cursor->ev;

	return 0;
}


/* history_def_curr():
 *	Default function to return the current event in the history.
 */
static int
history_def_curr(void *p, TYPE(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.
 */
static int
history_def_set(void *p, TYPE(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_set_nth():
 *	Default function to set the current event in the history to the
 *	n-th one.
 */
static int
history_set_nth(void *p, TYPE(HistEvent) *ev, int n)
{
	history_t *h = (history_t *) p;

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


/* history_def_add():
 *	Append string to element
 */
static int
history_def_add(void *p, TYPE(HistEvent) *ev, const Char *str)
{
	history_t *h = (history_t *) p;
	size_t len;
	Char *s;
	HistEventPrivate *evp = (void *)&h->cursor->ev;

	if (h->cursor == &h->list)
		return history_def_enter(p, ev, str);
	len = Strlen(evp->str) + Strlen(str) + 1;
	s = h_malloc(len * sizeof(*s));
	if (s == NULL) {
		he_seterrev(ev, _HE_MALLOC_FAILED);
		return -1;
	}
	(void) Strncpy(s, h->cursor->ev.str, len);
        s[len - 1] = '\0';
	(void) Strncat(s, str, len - Strlen(s) - 1);
	h_free(evp->str);
	evp->str = s;
	*ev = h->cursor->ev;
	return 0;
}


static int
history_deldata_nth(history_t *h, TYPE(HistEvent) *ev,
    int num, void **data)
{
	if (history_set_nth(h, ev, num) != 0)
		return -1;
	/* magic value to skip delete (just set to n-th history) */
	if (data == (void **)-1)
		return 0;
	ev->str = Strdup(h->cursor->ev.str);
	ev->num = h->cursor->ev.num;
	if (data)
		*data = h->cursor->data;
	history_def_delete(h, ev, h->cursor);
	return 0;
}


/* history_def_del():
 *	Delete element hp of the h list
 */
/* ARGSUSED */
static int
history_def_del(void *p, TYPE(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;
}


/* history_def_delete():
 *	Delete element hp of the h list
 */
/* ARGSUSED */
static void
history_def_delete(history_t *h,
		   TYPE(HistEvent) *ev __attribute__((__unused__)), hentry_t *hp)
{
	HistEventPrivate *evp = (void *)&hp->ev;
	if (hp == &h->list)
		abort();
	if (h->cursor == hp) {
		h->cursor = hp->prev;
		if (h->cursor == &h->list)
			h->cursor = hp->next;
	}
	hp->prev->next = hp->next;
	hp->next->prev = hp->prev;
	h_free(evp->str);
	h_free(hp);
	h->cur--;
}


/* history_def_insert():
 *	Insert element with string str in the h list
 */
static int
history_def_insert(history_t *h, TYPE(HistEvent) *ev, const Char *str)
{
	hentry_t *c;

	c = h_malloc(sizeof(*c));
	if (c == NULL)
		goto oomem;
	if ((c->ev.str = h_strdup(str)) == NULL) {
		h_free(c);
		goto oomem;
	}
	c->data = NULL;
	c->ev.num = ++h->eventid;
	c->next = h->list.next;
	c->prev = &h->list;
	h->list.next->prev = c;
	h->list.next = c;
	h->cur++;
	h->cursor = c;

	*ev = c->ev;
	return 0;
oomem:
	he_seterrev(ev, _HE_MALLOC_FAILED);
	return -1;
}


/* history_def_enter():
 *	Default function to enter an item in the history
 */
static int
history_def_enter(void *p, TYPE(HistEvent) *ev, const Char *str)
{
	history_t *h = (history_t *) p;

	if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
	    Strcmp(h->list.next->ev.str, str) == 0)
	    return 0;

	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.
         */
	while (h->cur > h->max && h->cur > 0)
		history_def_delete(h, ev, h->list.prev);

	return 1;
}


/* history_def_init():
 *	Default history initialization function
 */
/* ARGSUSED */
static int
history_def_init(void **p, TYPE(HistEvent) *ev __attribute__((__unused__)), int n)
{
	history_t *h = (history_t *) h_malloc(sizeof(*h));
	if (h == NULL)
		return -1;

	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;
	h->flags = 0;
	*p = h;
	return 0;
}


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

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




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

/* history_init():
 *	Initialization function.
 */
TYPE(History) *
FUN(history,init)(void)
{
	TYPE(HistEvent) ev;
	TYPE(History) *h = (TYPE(History) *) h_malloc(sizeof(*h));
	if (h == NULL)
		return NULL;

	if (history_def_init(&h->h_ref, &ev, 0) == -1) {
		h_free(h);
		return NULL;
	}
	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;
	h->h_del = history_def_del;

	return h;
}


/* history_end():
 *	clean up history;
 */
void
FUN(history,end)(TYPE(History) *h)
{
	TYPE(HistEvent) ev;

	if (h->h_next == history_def_next)
		history_def_clear(h->h_ref, &ev);
	h_free(h->h_ref);
	h_free(h);
}



/* history_setsize():
 *	Set history number of events
 */
static int
history_setsize(TYPE(History) *h, TYPE(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
 */
static int
history_getsize(TYPE(History) *h, TYPE(HistEvent) *ev)
{
	if (h->h_next != history_def_next) {
		he_seterrev(ev, _HE_NOT_ALLOWED);
		return -1;
	}
	ev->num = history_def_getsize(h->h_ref);
	if (ev->num < -1) {
		he_seterrev(ev, _HE_SIZE_NEGATIVE);
		return -1;
	}
	return 0;
}


/* history_setunique():
 *	Set if adjacent equal events should not be entered in history.
 */
static int
history_setunique(TYPE(History) *h, TYPE(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.
 */
static int
history_getunique(TYPE(History) *h, TYPE(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);
	return 0;
}


/* history_set_fun():
 *	Set history functions
 */
static int
history_set_fun(TYPE(History) *h, TYPE(History) *nh)
{
	TYPE(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 ||
	    nh->h_del == NULL || nh->h_ref == NULL) {
		if (h->h_next != history_def_next) {
			if (history_def_init(&h->h_ref, &ev, 0) == -1)
				return -1;
			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;
			h->h_del = history_def_del;
		}
		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;
	h->h_del = nh->h_del;

	return 0;
}


/* history_load():
 *	TYPE(History) load function
 */
static int
history_load(TYPE(History) *h, const char *fname)
{
	FILE *fp;
	char *line = NULL;
	size_t llen;
	ssize_t sz;
	size_t max_size;
	char *ptr;
	int i = -1;
	TYPE(HistEvent) ev;
#ifndef NARROWCHAR
	static ct_buffer_t conv;
#endif

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

	if (flock(fileno(fp), LOCK_SH) == -1)
		goto done;

	llen = 0;
	if ((sz = getline(&line, &llen, fp)) == -1)
		goto done;

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

	ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
	if (ptr == NULL)
		goto done;
	for (i = 0; (sz = getline(&line, &llen, fp)) != -1; i++) {
		if (sz > 0 && line[sz - 1] == '\n')
			line[--sz] = '\0';
		if (max_size < (size_t)sz) {
			char *nptr;
			max_size = ((size_t)sz + 1024) & (size_t)~1023;
			nptr = h_realloc(ptr, max_size * sizeof(*ptr));
			if (nptr == NULL) {
				i = -1;
				goto oomem;
			}
			ptr = nptr;
		}
		(void) strunvis(ptr, line);
		if (HENTER(h, &ev, ct_decode_string(ptr, &conv)) == -1) {
			i = -1;
			goto oomem;
		}
	}
oomem:
	h_free(ptr);
done:
	free(line);
	(void) fclose(fp);
	return i;
}


/* history_save_fp():
 *	TYPE(History) save function
 */
static int
history_save_fp(TYPE(History) *h, FILE *fp)
{
	TYPE(HistEvent) ev;
	int i = -1, retval;
	size_t len, max_size;
	char *ptr;
	const char *str;
#ifndef NARROWCHAR
	static ct_buffer_t conv;
#endif

	if (flock(fileno(fp), LOCK_EX) == -1)
		goto done;
	if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
		goto done;
	if (fputs(hist_cookie, fp) == EOF)
		goto done;
	ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
	if (ptr == NULL)
		goto done;
	for (i = 0, retval = HLAST(h, &ev);
	    retval != -1;
	    retval = HPREV(h, &ev), i++) {
		str = ct_encode_string(ev.str, &conv);
		len = strlen(str) * 4 + 1;
		if (len > max_size) {
			char *nptr;
			max_size = (len + 1024) & (size_t)~1023;
			nptr = h_realloc(ptr, max_size * sizeof(*ptr));
			if (nptr == NULL) {
				i = -1;
				goto oomem;
			}
			ptr = nptr;
		}
		(void) strvis(ptr, str, VIS_WHITE);
		(void) fprintf(fp, "%s\n", ptr);
	}
oomem:
	h_free(ptr);
done:
	(void) flock(fileno(fp), LOCK_UN);
	return i;
}


/* history_save():
 *    History save function
 */
static int
history_save(TYPE(History) *h, const char *fname)
{
    FILE *fp;
    int i;

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

    i = history_save_fp(h, fp);

    (void) fclose(fp);
    return i;
}


static int
history_save_fp_incr(TYPE(History) *h, FILE *fp, int num)
{
	TYPE(HistEvent) ev;
	char *line = NULL;
	char *ptr;
	const char *str;
	int i = -1, retval;
	size_t len, max_size;
	size_t llen = 0;
	ssize_t sz;
#ifndef NARROWCHAR
	static ct_buffer_t conv;
#endif

	for (retval = HCURR(h, &ev); retval != -1; retval = HNEXT(h, &ev))
		if (ev.num == num)
			break;
	if (retval == -1)
		goto done;

	if (flock(fileno(fp), LOCK_EX) == -1)
		goto done;
	if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
		goto done;

	if (fseek(fp, 0, SEEK_SET) == -1)
		goto done;
	if ((sz = getline(&line, &llen, fp)) == -1) {
		if (ferror(fp))
			goto done;
		else
			sz = 0;
	}
	if (strncmp(line, hist_cookie, (size_t)sz) != 0)
		goto done;
	if (fseek(fp, 0, SEEK_END) == -1)
		goto done;
	if (sz == 0)
		if (fputs(hist_cookie, fp) == EOF)
			goto done;

	ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
	if (ptr == NULL)
		goto done;
	for (i = 0, retval = 0;
		retval != -1;
		retval = HPREV(h, &ev), i++) {
		str = ct_encode_string(ev.str, &conv);
		len = strlen(str) * 4 + 1;
		if (len > max_size) {
			char *nptr;
			max_size = (len + 1024) & (size_t)~1023;
			nptr = h_realloc(ptr, max_size * sizeof(*ptr));
			if (nptr == NULL) {
				i = -1;
				goto oomem;
			}
			ptr = nptr;
		}
		(void) strvis(ptr, str, VIS_WHITE);
		(void) fprintf(fp, "%s\n", ptr);
	}
oomem:
	h_free(ptr);
done:
	free(line);
	(void) flock(fileno(fp), LOCK_UN);
	return i;
}


static int
history_save_incr(TYPE(History) *h, const char *fname, int num)
{
	FILE *fp;
	int i;

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

	i = history_save_fp_incr(h, fp, num);

	(void) fclose(fp);
	return i;
}


/* history_prev_event():
 *	Find the previous event, with number given
 */
static int
history_prev_event(TYPE(History) *h, TYPE(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;
}


static int
history_next_evdata(TYPE(History) *h, TYPE(HistEvent) *ev, int num, void **d)
{
	int retval;

	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
		if (ev->num == num) {
			if (d)
				*d = ((history_t *)h->h_ref)->cursor->data;
			return 0;
		}

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


/* history_next_event():
 *	Find the next event, with number given
 */
static int
history_next_event(TYPE(History) *h, TYPE(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
 */
static int
history_prev_string(TYPE(History) *h, TYPE(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
 */
static int
history_next_string(TYPE(History) *h, TYPE(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
FUNW(history)(TYPE(History) *h, TYPE(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;

	case H_GETUNIQUE:
		retval = history_getunique(h, ev);
		break;

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

	case H_ADD:
		str = va_arg(va, const Char *);
		retval = HADD(h, ev, str);
		break;

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

	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_SAVE_INCR:
	{
		const char *fname = va_arg(va, const char *);
		int num = va_arg(va, int);
		retval = history_save_incr(h, fname, num);
		if (retval == -1)
			he_seterrev(ev, _HE_HIST_WRITE);
		break;
	}

	case H_SAVE_FP:
		retval = history_save_fp(h, va_arg(va, FILE *));
		if (retval == -1)
		    he_seterrev(ev, _HE_HIST_WRITE);
		break;

	case H_SAVE_FP_INCR:
	{
		FILE *fp = va_arg(va, FILE *);
		int num = va_arg(va, int);
		retval = history_save_fp_incr(h, fp, num);
		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:
	{
		TYPE(History) hf;

		hf.h_ref = va_arg(va, void *);
		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);
		hf.h_del = va_arg(va, history_sfun_t);

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

	case H_END:
		FUN(history,end)(h);
		retval = 0;
		break;

	case H_NEXT_EVDATA:
	{
		int num = va_arg(va, int);
		void **d = va_arg(va, void **);
		retval = history_next_evdata(h, ev, num, d);
		break;
	}

	case H_DELDATA:
	{
		int num = va_arg(va, int);
		void **d = va_arg(va, void **);
		retval = history_deldata_nth((history_t *)h->h_ref, ev, num, d);
		break;
	}

	case H_REPLACE: /* only use after H_NEXT_EVDATA */
	{
		const Char *line = va_arg(va, const Char *);
		void *d = va_arg(va, void *);
		const Char *s;
		if(!line || !(s = Strdup(line))) {
			retval = -1;
			break;
		}
		((history_t *)h->h_ref)->cursor->ev.str = s;
		((history_t *)h->h_ref)->cursor->data = d;
		retval = 0;
		break;
	}

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