1 /* $NetBSD: hist.c,v 1.32 2017/03/05 19:23:58 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: hist.c,v 1.32 2017/03/05 19:23:58 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * hist.c: History access functions
54 * Initialization function.
57 hist_init(EditLine *el)
60 el->el_history.fun = NULL;
61 el->el_history.ref = NULL;
62 el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf));
63 el->el_history.sz = EL_BUFSIZ;
64 if (el->el_history.buf == NULL)
66 el->el_history.last = el->el_history.buf;
75 hist_end(EditLine *el)
78 el_free(el->el_history.buf);
79 el->el_history.buf = NULL;
84 * Set new history interface
87 hist_set(EditLine *el, hist_fun_t fun, void *ptr)
90 el->el_history.ref = ptr;
91 el->el_history.fun = fun;
97 * Get a history line and update it in the buffer.
98 * eventno tells us the event to get.
100 libedit_private el_action_t
101 hist_get(EditLine *el)
107 if (el->el_history.eventno == 0) { /* if really the current line */
108 (void) wcsncpy(el->el_line.buffer, el->el_history.buf,
110 el->el_line.lastchar = el->el_line.buffer +
111 (el->el_history.last - el->el_history.buf);
114 if (el->el_map.type == MAP_VI)
115 el->el_line.cursor = el->el_line.buffer;
118 el->el_line.cursor = el->el_line.lastchar;
122 if (el->el_history.ref == NULL)
130 for (h = 1; h < el->el_history.eventno; h++)
131 if ((hp = HIST_NEXT(el)) == NULL)
134 hlen = wcslen(hp) + 1;
135 blen = (size_t)(el->el_line.limit - el->el_line.buffer);
136 if (hlen > blen && !ch_enlargebufs(el, hlen))
139 memcpy(el->el_line.buffer, hp, hlen * sizeof(*hp));
140 el->el_line.lastchar = el->el_line.buffer + hlen - 1;
142 if (el->el_line.lastchar > el->el_line.buffer
143 && el->el_line.lastchar[-1] == '\n')
144 el->el_line.lastchar--;
145 if (el->el_line.lastchar > el->el_line.buffer
146 && el->el_line.lastchar[-1] == ' ')
147 el->el_line.lastchar--;
149 if (el->el_map.type == MAP_VI)
150 el->el_line.cursor = el->el_line.buffer;
153 el->el_line.cursor = el->el_line.lastchar;
157 el->el_history.eventno = h;
164 * process a history command
167 hist_command(EditLine *el, int argc, const wchar_t **argv)
173 if (el->el_history.ref == NULL)
176 if (argc == 1 || wcscmp(argv[1], L"list") == 0) {
180 /* List history entries */
182 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) {
184 ct_encode_string(str, &el->el_scratch);
185 size_t len = strlen(ptr);
186 if (len > 0 && ptr[len - 1] == '\n')
191 char *nbuf = el_realloc(buf, maxlen);
198 strvis(buf, ptr, VIS_NL);
199 (void) fprintf(el->el_outfile, "%d\t%s\n",
209 num = (int)wcstol(argv[2], NULL, 0);
211 if (wcscmp(argv[1], L"size") == 0)
212 return history_w(el->el_history.ref, &ev, H_SETSIZE, num);
214 if (wcscmp(argv[1], L"unique") == 0)
215 return history_w(el->el_history.ref, &ev, H_SETUNIQUE, num);
221 * Enlarge history buffer to specified value. Called from el_enlargebufs().
222 * Return 0 for failure, 1 for success.
226 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
230 newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
234 (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
236 el->el_history.last = newbuf +
237 (el->el_history.last - el->el_history.buf);
238 el->el_history.buf = newbuf;
239 el->el_history.sz = newsz;
244 libedit_private wchar_t *
245 hist_convert(EditLine *el, int fn, void *arg)
248 if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
250 return ct_decode_string((const char *)(const void *)ev.str,