xref: /openbsd-src/lib/libedit/hist.c (revision 6f05df2d9be0954bec42d51d943d77bd250fb664)
1 /*	$OpenBSD: hist.c,v 1.10 2014/10/17 06:07:50 deraadt Exp $	*/
2 /*	$NetBSD: hist.c,v 1.17 2009/12/30 23:54:52 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 "el.h"
43 
44 /* hist_init():
45  *	Initialization function.
46  */
47 protected int
48 hist_init(EditLine *el)
49 {
50 
51 	el->el_history.fun = NULL;
52 	el->el_history.ref = NULL;
53 	el->el_history.buf = reallocarray(NULL, EL_BUFSIZ,
54 	    sizeof(*el->el_history.buf));
55 	el->el_history.sz  = EL_BUFSIZ;
56 	if (el->el_history.buf == NULL)
57 		return (-1);
58 	el->el_history.last = el->el_history.buf;
59 	return (0);
60 }
61 
62 
63 /* hist_end():
64  *	clean up history;
65  */
66 protected void
67 hist_end(EditLine *el)
68 {
69 
70 	free((ptr_t) el->el_history.buf);
71 	el->el_history.buf = NULL;
72 }
73 
74 
75 /* hist_set():
76  *	Set new history interface
77  */
78 protected int
79 hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
80 {
81 
82 	el->el_history.ref = ptr;
83 	el->el_history.fun = fun;
84 	return (0);
85 }
86 
87 
88 /* hist_get():
89  *	Get a history line and update it in the buffer.
90  *	eventno tells us the event to get.
91  */
92 protected el_action_t
93 hist_get(EditLine *el)
94 {
95 	const Char *hp;
96 	int h;
97 
98 	if (el->el_history.eventno == 0) {	/* if really the current line */
99 		(void) Strncpy(el->el_line.buffer, el->el_history.buf,
100 		    el->el_history.sz);
101 		el->el_line.lastchar = el->el_line.buffer +
102 		    (el->el_history.last - el->el_history.buf);
103 
104 #ifdef KSHVI
105 		if (el->el_map.type == MAP_VI)
106 			el->el_line.cursor = el->el_line.buffer;
107 		else
108 #endif /* KSHVI */
109 			el->el_line.cursor = el->el_line.lastchar;
110 
111 		return (CC_REFRESH);
112 	}
113 	if (el->el_history.ref == NULL)
114 		return (CC_ERROR);
115 
116 	hp = HIST_FIRST(el);
117 
118 	if (hp == NULL)
119 		return (CC_ERROR);
120 
121 	for (h = 1; h < el->el_history.eventno; h++)
122 		if ((hp = HIST_NEXT(el)) == NULL) {
123 			el->el_history.eventno = h;
124 			return (CC_ERROR);
125 		}
126 	(void) Strncpy(el->el_line.buffer, hp,
127 			(size_t)(el->el_line.limit - el->el_line.buffer));
128 	el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0';
129 	el->el_line.lastchar = el->el_line.buffer + Strlen(el->el_line.buffer);
130 
131 	if (el->el_line.lastchar > el->el_line.buffer
132 	    && el->el_line.lastchar[-1] == '\n')
133 		el->el_line.lastchar--;
134 	if (el->el_line.lastchar > el->el_line.buffer
135 	    && el->el_line.lastchar[-1] == ' ')
136 		el->el_line.lastchar--;
137 #ifdef KSHVI
138 	if (el->el_map.type == MAP_VI)
139 		el->el_line.cursor = el->el_line.buffer;
140 	else
141 #endif /* KSHVI */
142 		el->el_line.cursor = el->el_line.lastchar;
143 
144 	return (CC_REFRESH);
145 }
146 
147 
148 /* hist_command()
149  *	process a history command
150  */
151 protected int
152 hist_command(EditLine *el, int argc, const Char **argv)
153 {
154 	const Char *str;
155 	int num;
156 	HistEvent ev;
157 
158 	if (el->el_history.ref == NULL)
159 		return (-1);
160 
161 	if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) {
162 		 /* List history entries */
163 
164 		for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
165 			(void) fprintf(el->el_outfile, "%d %s",
166 			    el->el_history.ev.num, ct_encode_string(str, &el->el_scratch));
167 		return (0);
168 	}
169 
170 	if (argc != 3)
171 		return (-1);
172 
173 	num = (int)Strtol(argv[2], NULL, 0);
174 
175 	if (Strcmp(argv[1], STR("size")) == 0)
176 		return history(el->el_history.ref, &ev, H_SETSIZE, num);
177 
178 	if (Strcmp(argv[1], STR("unique")) == 0)
179 		return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
180 
181 	return -1;
182 }
183 
184 /* hist_enlargebuf()
185  *	Enlarge history buffer to specified value. Called from el_enlargebufs().
186  *	Return 0 for failure, 1 for success.
187  */
188 protected int
189 /*ARGSUSED*/
190 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
191 {
192 	Char *newbuf;
193 
194 	newbuf = reallocarray(el->el_history.buf, newsz, sizeof(*newbuf));
195 	if (!newbuf)
196 		return 0;
197 
198 	(void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
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 #ifdef WIDECHAR
209 protected wchar_t *
210 hist_convert(EditLine *el, int fn, ptr_t arg)
211 {
212 	HistEventW ev;
213 	if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
214 		return NULL;
215 	return ct_decode_string((const char *)(const void *)ev.str,
216 	    &el->el_scratch);
217 }
218 #endif
219