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