xref: /openbsd-src/lib/libedit/hist.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: hist.c,v 1.8 2009/10/27 23:59:28 deraadt Exp $	*/
2 /*	$NetBSD: hist.c,v 1.15 2003/11/01 23:36:39 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 = (char *) el_malloc(EL_BUFSIZ);
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) strlcpy(el->el_line.buffer, hp,
126 			(size_t)(el->el_line.limit - el->el_line.buffer));
127 	el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
128 
129 	if (el->el_line.lastchar > el->el_line.buffer
130 	    && el->el_line.lastchar[-1] == '\n')
131 		el->el_line.lastchar--;
132 	if (el->el_line.lastchar > el->el_line.buffer
133 	    && el->el_line.lastchar[-1] == ' ')
134 		el->el_line.lastchar--;
135 #ifdef KSHVI
136 	if (el->el_map.type == MAP_VI)
137 		el->el_line.cursor = el->el_line.buffer;
138 	else
139 #endif /* KSHVI */
140 		el->el_line.cursor = el->el_line.lastchar;
141 
142 	return (CC_REFRESH);
143 }
144 
145 
146 /* hist_command()
147  *	process a history command
148  */
149 protected int
150 hist_command(EditLine *el, int argc, const char **argv)
151 {
152 	const char *str;
153 	int num;
154 	HistEvent ev;
155 
156 	if (el->el_history.ref == NULL)
157 		return (-1);
158 
159 	if (argc == 1 || strcmp(argv[1], "list") == 0) {
160 		 /* List history entries */
161 
162 		for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
163 			(void) fprintf(el->el_outfile, "%d %s",
164 			    el->el_history.ev.num, str);
165 		return (0);
166 	}
167 
168 	if (argc != 3)
169 		return (-1);
170 
171 	num = (int)strtol(argv[2], NULL, 0);
172 
173 	if (strcmp(argv[1], "size") == 0)
174 		return history(el->el_history.ref, &ev, H_SETSIZE, num);
175 
176 	if (strcmp(argv[1], "unique") == 0)
177 		return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
178 
179 	return -1;
180 }
181 
182 /* hist_enlargebuf()
183  *	Enlarge history buffer to specified value. Called from el_enlargebufs().
184  *	Return 0 for failure, 1 for success.
185  */
186 protected int
187 /*ARGSUSED*/
188 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
189 {
190 	char *newbuf;
191 
192 	newbuf = realloc(el->el_history.buf, newsz);
193 	if (!newbuf)
194 		return 0;
195 
196 	(void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
197 
198 	el->el_history.last = newbuf +
199 				(el->el_history.last - el->el_history.buf);
200 	el->el_history.buf = newbuf;
201 	el->el_history.sz  = newsz;
202 
203 	return 1;
204 }
205