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