1 /* $NetBSD: hist.c,v 1.20 2011/07/29 15:16:33 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.20 2011/07/29 15:16:33 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * hist.c: History access functions
53 * Initialization function.
56 hist_init(EditLine *el)
59 el->el_history.fun = NULL;
60 el->el_history.ref = NULL;
61 el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf));
62 el->el_history.sz = EL_BUFSIZ;
63 if (el->el_history.buf == NULL)
65 el->el_history.last = el->el_history.buf;
74 hist_end(EditLine *el)
77 el_free(el->el_history.buf);
78 el->el_history.buf = NULL;
83 * Set new history interface
86 hist_set(EditLine *el, hist_fun_t fun, void *ptr)
89 el->el_history.ref = ptr;
90 el->el_history.fun = fun;
96 * Get a history line and update it in the buffer.
97 * eventno tells us the event to get.
100 hist_get(EditLine *el)
105 if (el->el_history.eventno == 0) { /* if really the current line */
106 (void) Strncpy(el->el_line.buffer, el->el_history.buf,
108 el->el_line.lastchar = el->el_line.buffer +
109 (el->el_history.last - el->el_history.buf);
112 if (el->el_map.type == MAP_VI)
113 el->el_line.cursor = el->el_line.buffer;
116 el->el_line.cursor = el->el_line.lastchar;
120 if (el->el_history.ref == NULL)
128 for (h = 1; h < el->el_history.eventno; h++)
129 if ((hp = HIST_NEXT(el)) == NULL) {
130 el->el_history.eventno = h;
133 (void) Strncpy(el->el_line.buffer, hp,
134 (size_t)(el->el_line.limit - el->el_line.buffer));
135 el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0';
136 el->el_line.lastchar = el->el_line.buffer + Strlen(el->el_line.buffer);
138 if (el->el_line.lastchar > el->el_line.buffer
139 && el->el_line.lastchar[-1] == '\n')
140 el->el_line.lastchar--;
141 if (el->el_line.lastchar > el->el_line.buffer
142 && el->el_line.lastchar[-1] == ' ')
143 el->el_line.lastchar--;
145 if (el->el_map.type == MAP_VI)
146 el->el_line.cursor = el->el_line.buffer;
149 el->el_line.cursor = el->el_line.lastchar;
156 * process a history command
159 hist_command(EditLine *el, int argc, const Char **argv)
165 if (el->el_history.ref == NULL)
168 if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) {
169 /* List history entries */
171 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
172 (void) fprintf(el->el_outfile, "%d %s",
173 el->el_history.ev.num, ct_encode_string(str, &el->el_scratch));
180 num = (int)Strtol(argv[2], NULL, 0);
182 if (Strcmp(argv[1], STR("size")) == 0)
183 return FUNW(history)(el->el_history.ref, &ev, H_SETSIZE, num);
185 if (Strcmp(argv[1], STR("unique")) == 0)
186 return FUNW(history)(el->el_history.ref, &ev, H_SETUNIQUE, num);
192 * Enlarge history buffer to specified value. Called from el_enlargebufs().
193 * Return 0 for failure, 1 for success.
197 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
201 newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
205 (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
207 el->el_history.last = newbuf +
208 (el->el_history.last - el->el_history.buf);
209 el->el_history.buf = newbuf;
210 el->el_history.sz = newsz;
217 hist_convert(EditLine *el, int fn, void *arg)
220 if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
222 return ct_decode_string((const char *)(const void *)ev.str,