MFC:
[dragonfly.git] / lib / libedit / hist.c
blobdaaa58c259e066c8c0a0e058e1fa4e71d11c718e
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)hist.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: hist.c,v 1.15 2003/11/01 23:36:39 christos Exp $"
34 * $DragonFly: src/lib/libedit/hist.c,v 1.3 2005/11/13 11:58:30 corecode Exp $
37 #include "config.h"
40 * hist.c: History access functions
42 #include <stdlib.h>
43 #include "el.h"
45 /* hist_init():
46 * Initialization function.
48 protected int
49 hist_init(EditLine *el)
52 el->el_history.fun = NULL;
53 el->el_history.ref = NULL;
54 el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
55 el->el_history.sz = EL_BUFSIZ;
56 if (el->el_history.buf == NULL)
57 return (-1);
58 el->el_history.last = el->el_history.buf;
59 return (0);
63 /* hist_end():
64 * clean up history;
66 protected void
67 hist_end(EditLine *el)
70 el_free((ptr_t) el->el_history.buf);
71 el->el_history.buf = NULL;
75 /* hist_set():
76 * Set new history interface
78 protected int
79 hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
82 el->el_history.ref = ptr;
83 el->el_history.fun = fun;
84 return (0);
88 /* hist_get():
89 * Get a history line and update it in the buffer.
90 * eventno tells us the event to get.
92 protected el_action_t
93 hist_get(EditLine *el)
95 const char *hp;
96 int h;
98 if (el->el_history.eventno == 0) { /* if really the current line */
99 (void) strncpy(el->el_line.buffer, el->el_history.buf,
100 el->el_history.sz);
101 el->el_line.lastchar = el->el_line.buffer +
102 (el->el_history.last - el->el_history.buf);
104 #ifdef KSHVI
105 if (el->el_map.type == MAP_VI)
106 el->el_line.cursor = el->el_line.buffer;
107 else
108 #endif /* KSHVI */
109 el->el_line.cursor = el->el_line.lastchar;
111 return (CC_REFRESH);
113 if (el->el_history.ref == NULL)
114 return (CC_ERROR);
116 hp = HIST_FIRST(el);
118 if (hp == NULL)
119 return (CC_ERROR);
121 for (h = 1; h < el->el_history.eventno; h++)
122 if ((hp = HIST_NEXT(el)) == NULL) {
123 el->el_history.eventno = h;
124 return (CC_ERROR);
126 (void) strlcpy(el->el_line.buffer, hp,
127 (size_t)(el->el_line.limit - el->el_line.buffer));
128 el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
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;
143 return (CC_REFRESH);
147 /* hist_command()
148 * process a history command
150 protected int
151 hist_command(EditLine *el, int argc, const char **argv)
153 const char *str;
154 int num;
155 HistEvent ev;
157 if (el->el_history.ref == NULL)
158 return (-1);
160 if (argc == 1 || strcmp(argv[1], "list") == 0) {
161 /* List history entries */
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, str);
166 return (0);
169 if (argc != 3)
170 return (-1);
172 num = (int)strtol(argv[2], NULL, 0);
174 if (strcmp(argv[1], "size") == 0)
175 return history(el->el_history.ref, &ev, H_SETSIZE, num);
177 if (strcmp(argv[1], "unique") == 0)
178 return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
180 return -1;
183 /* hist_enlargebuf()
184 * Enlarge history buffer to specified value. Called from el_enlargebufs().
185 * Return 0 for failure, 1 for success.
187 protected int
188 /*ARGSUSED*/
189 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
191 char *newbuf;
193 newbuf = realloc(el->el_history.buf, newsz);
194 if (!newbuf)
195 return 0;
197 (void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
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;
204 return 1;