Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libedit / hist.c
blob522c0601c5edd95f3db2f5ab46e888c402e886af
1 /* $NetBSD: hist.c,v 1.16 2009/12/30 22:37:40 christos Exp $ */
3 /*-
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
12 * are met:
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
32 * SUCH DAMAGE.
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: hist.c,v 1.16 2009/12/30 22:37:40 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
45 * hist.c: History access functions
47 #include <stdlib.h>
48 #include "el.h"
50 /* hist_init():
51 * Initialization function.
53 protected int
54 hist_init(EditLine *el)
57 el->el_history.fun = NULL;
58 el->el_history.ref = NULL;
59 el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf));
60 el->el_history.sz = EL_BUFSIZ;
61 if (el->el_history.buf == NULL)
62 return (-1);
63 el->el_history.last = el->el_history.buf;
64 return (0);
68 /* hist_end():
69 * clean up history;
71 protected void
72 hist_end(EditLine *el)
75 el_free((ptr_t) el->el_history.buf);
76 el->el_history.buf = NULL;
80 /* hist_set():
81 * Set new history interface
83 protected int
84 hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
87 el->el_history.ref = ptr;
88 el->el_history.fun = fun;
89 return (0);
93 /* hist_get():
94 * Get a history line and update it in the buffer.
95 * eventno tells us the event to get.
97 protected el_action_t
98 hist_get(EditLine *el)
100 const Char *hp;
101 int h;
103 if (el->el_history.eventno == 0) { /* if really the current line */
104 (void) Strncpy(el->el_line.buffer, el->el_history.buf,
105 el->el_history.sz);
106 el->el_line.lastchar = el->el_line.buffer +
107 (el->el_history.last - el->el_history.buf);
109 #ifdef KSHVI
110 if (el->el_map.type == MAP_VI)
111 el->el_line.cursor = el->el_line.buffer;
112 else
113 #endif /* KSHVI */
114 el->el_line.cursor = el->el_line.lastchar;
116 return (CC_REFRESH);
118 if (el->el_history.ref == NULL)
119 return (CC_ERROR);
121 hp = HIST_FIRST(el);
123 if (hp == NULL)
124 return (CC_ERROR);
126 for (h = 1; h < el->el_history.eventno; h++)
127 if ((hp = HIST_NEXT(el)) == NULL) {
128 el->el_history.eventno = h;
129 return (CC_ERROR);
131 (void) Strncpy(el->el_line.buffer, hp,
132 (size_t)(el->el_line.limit - el->el_line.buffer));
133 el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0';
134 el->el_line.lastchar = el->el_line.buffer + Strlen(el->el_line.buffer);
136 if (el->el_line.lastchar > el->el_line.buffer
137 && el->el_line.lastchar[-1] == '\n')
138 el->el_line.lastchar--;
139 if (el->el_line.lastchar > el->el_line.buffer
140 && el->el_line.lastchar[-1] == ' ')
141 el->el_line.lastchar--;
142 #ifdef KSHVI
143 if (el->el_map.type == MAP_VI)
144 el->el_line.cursor = el->el_line.buffer;
145 else
146 #endif /* KSHVI */
147 el->el_line.cursor = el->el_line.lastchar;
149 return (CC_REFRESH);
153 /* hist_command()
154 * process a history command
156 protected int
157 hist_command(EditLine *el, int argc, const Char **argv)
159 const Char *str;
160 int num;
161 HistEvent ev;
163 if (el->el_history.ref == NULL)
164 return (-1);
166 if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) {
167 /* List history entries */
169 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
170 (void) fprintf(el->el_outfile, "%d %s",
171 el->el_history.ev.num, ct_encode_string(str, &el->el_scratch));
172 return (0);
175 if (argc != 3)
176 return (-1);
178 num = (int)Strtol(argv[2], NULL, 0);
180 if (Strcmp(argv[1], STR("size")) == 0)
181 return history(el->el_history.ref, &ev, H_SETSIZE, num);
183 if (Strcmp(argv[1], STR("unique")) == 0)
184 return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
186 return -1;
189 /* hist_enlargebuf()
190 * Enlarge history buffer to specified value. Called from el_enlargebufs().
191 * Return 0 for failure, 1 for success.
193 protected int
194 /*ARGSUSED*/
195 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
197 Char *newbuf;
199 newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
200 if (!newbuf)
201 return 0;
203 (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
205 el->el_history.last = newbuf +
206 (el->el_history.last - el->el_history.buf);
207 el->el_history.buf = newbuf;
208 el->el_history.sz = newsz;
210 return 1;
213 #ifdef WIDECHAR
214 protected wchar_t *
215 hist_convert(EditLine *el, int fn, ptr_t arg)
217 HistEventW ev;
218 if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
219 return NULL;
220 return ct_decode_string((const char *)(const void *)ev.str,
221 &el->el_scratch);
223 #endif