Merge commit '9992e6a682b1c35b4385c3b512db329ec8ab9ede'
[unleashed.git] / lib / libedit / hist.c
blobc26ff93c12f0cacb277ee5121408725d5027a8c0
1 /* $OpenBSD: hist.c,v 1.18 2017/04/12 18:24:37 tb Exp $ */
2 /* $NetBSD: hist.c,v 1.28 2016/04/11 00:50:13 christos Exp $ */
4 /*-
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
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.
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.
36 #include "config.h"
39 * hist.c: History access functions
41 #include <stdlib.h>
42 #include <string.h>
44 #include "el.h"
46 /* hist_init():
47 * Initialization function.
49 protected int
50 hist_init(EditLine *el)
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;
65 /* hist_end():
66 * clean up history;
68 protected void
69 hist_end(EditLine *el)
72 free(el->el_history.buf);
73 el->el_history.buf = NULL;
77 /* hist_set():
78 * Set new history interface
80 protected int
81 hist_set(EditLine *el, hist_fun_t fun, void *ptr)
84 el->el_history.ref = ptr;
85 el->el_history.fun = fun;
86 return 0;
90 /* hist_get():
91 * Get a history line and update it in the buffer.
92 * eventno tells us the event to get.
94 protected el_action_t
95 hist_get(EditLine *el)
97 const wchar_t *hp;
98 int h;
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);
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;
113 return CC_REFRESH;
115 if (el->el_history.ref == NULL)
116 return CC_ERROR;
118 hp = HIST_FIRST(el);
120 if (hp == NULL)
121 return CC_ERROR;
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;
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);
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;
146 return CC_REFRESH;
150 /* hist_command()
151 * process a history command
153 protected int
154 hist_command(EditLine *el, int argc, const wchar_t **argv)
156 const wchar_t *str;
157 int num;
158 HistEvent ev;
160 if (el->el_history.ref == NULL)
161 return -1;
163 if (argc == 1 || wcscmp(argv[1], L"list") == 0) {
164 /* List history entries */
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;
172 if (argc != 3)
173 return -1;
175 num = (int)wcstol(argv[2], NULL, 0);
177 if (wcscmp(argv[1], L"size") == 0)
178 return history(el->el_history.ref, &ev, H_SETSIZE, num);
180 if (wcscmp(argv[1], L"unique") == 0)
181 return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
183 return -1;
186 /* hist_enlargebuf()
187 * Enlarge history buffer to specified value. Called from el_enlargebufs().
188 * Return 0 for failure, 1 for success.
190 protected int
191 /*ARGSUSED*/
192 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
194 wchar_t *newbuf;
196 newbuf = recallocarray(el->el_history.buf, oldsz, newsz,
197 sizeof(*newbuf));
198 if (!newbuf)
199 return 0;
201 el->el_history.last = newbuf +
202 (el->el_history.last - el->el_history.buf);
203 el->el_history.buf = newbuf;
204 el->el_history.sz = newsz;
206 return 1;
209 protected wchar_t *
210 hist_convert(EditLine *el, int fn, void *arg)
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);