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