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
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$NetBSD: hist.c,v 1.15 2003/11/01 23:36:39 christos Exp $
33 */
34
35/*
36 * hist.c: History access functions
37 */
38#include "sys.h"
39#include <stdlib.h>
40#include "el.h"
41
42/* hist_init():
43 *	Initialization function.
44 */
45int
46hist_init(EditLine *el)
47{
48
49	el->el_history.fun = NULL;
50	el->el_history.ref = NULL;
51	el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
52	el->el_history.sz  = EL_BUFSIZ;
53	if (el->el_history.buf == NULL)
54		return (-1);
55	el->el_history.last = el->el_history.buf;
56	return (0);
57}
58
59
60/* hist_end():
61 *	clean up history;
62 */
63void
64hist_end(EditLine *el)
65{
66
67	el_free((ptr_t) el->el_history.buf);
68	el->el_history.buf = NULL;
69}
70
71
72/* hist_set():
73 *	Set new history interface
74 */
75int
76hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
77{
78
79	el->el_history.ref = ptr;
80	el->el_history.fun = fun;
81	return (0);
82}
83
84
85/* hist_get():
86 *	Get a history line and update it in the buffer.
87 *	eventno tells us the event to get.
88 */
89el_action_t
90hist_get(EditLine *el)
91{
92	const char *hp;
93	int h;
94
95	if (el->el_history.eventno == 0) {	/* if really the current line */
96		(void) strncpy(el->el_line.buffer, el->el_history.buf,
97		    el->el_history.sz);
98		el->el_line.lastchar = el->el_line.buffer +
99		    (el->el_history.last - el->el_history.buf);
100
101#ifdef KSHVI
102		if (el->el_map.type == MAP_VI)
103			el->el_line.cursor = el->el_line.buffer;
104		else
105#endif /* KSHVI */
106			el->el_line.cursor = el->el_line.lastchar;
107
108		return (CC_REFRESH);
109	}
110	if (el->el_history.ref == NULL)
111		return (CC_ERROR);
112
113	hp = HIST_FIRST(el);
114
115	if (hp == NULL)
116		return (CC_ERROR);
117
118	for (h = 1; h < el->el_history.eventno; h++)
119		if ((hp = HIST_NEXT(el)) == NULL) {
120			el->el_history.eventno = h;
121			return (CC_ERROR);
122		}
123	(void) strlcpy(el->el_line.buffer, hp,
124			(size_t)(el->el_line.limit - el->el_line.buffer));
125	el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
126
127	if (el->el_line.lastchar > el->el_line.buffer
128	    && el->el_line.lastchar[-1] == '\n')
129		el->el_line.lastchar--;
130	if (el->el_line.lastchar > el->el_line.buffer
131	    && el->el_line.lastchar[-1] == ' ')
132		el->el_line.lastchar--;
133#ifdef KSHVI
134	if (el->el_map.type == MAP_VI)
135		el->el_line.cursor = el->el_line.buffer;
136	else
137#endif /* KSHVI */
138		el->el_line.cursor = el->el_line.lastchar;
139
140	return (CC_REFRESH);
141}
142
143
144/* hist_command()
145 *	process a history command
146 */
147int
148hist_command(EditLine *el, int argc, const char **argv)
149{
150	const char *str;
151	int num;
152	HistEvent ev;
153
154	if (el->el_history.ref == NULL)
155		return (-1);
156
157	if (argc == 1 || strcmp(argv[1], "list") == 0) {
158		 /* List history entries */
159
160		for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
161			(void) fprintf(el->el_outfile, "%d %s",
162			    el->el_history.ev.num, str);
163		return (0);
164	}
165
166	if (argc != 3)
167		return (-1);
168
169	num = (int)strtol(argv[2], NULL, 0);
170
171	if (strcmp(argv[1], "size") == 0)
172		return history(el->el_history.ref, &ev, H_SETSIZE, num);
173
174	if (strcmp(argv[1], "unique") == 0)
175		return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
176
177	return -1;
178}
179
180/* hist_enlargebuf()
181 *	Enlarge history buffer to specified value. Called from el_enlargebufs().
182 *	Return 0 for failure, 1 for success.
183 */
184int
185/*ARGSUSED*/
186hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
187{
188	char *newbuf;
189
190	newbuf = realloc(el->el_history.buf, newsz);
191	if (!newbuf)
192		return 0;
193
194	(void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
195
196	el->el_history.last = newbuf +
197				(el->el_history.last - el->el_history.buf);
198	el->el_history.buf = newbuf;
199	el->el_history.sz  = newsz;
200
201	return 1;
202}
203