Deleted Added
full compact
hist.c (148834) hist.c (276881)
1/* $NetBSD: hist.c,v 1.20 2011/07/29 15:16:33 christos Exp $ */
2
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

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.
3/*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

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.
31 *
32 * $NetBSD: hist.c,v 1.15 2003/11/01 23:36:39 christos Exp $
33 */
34
33 */
34
35#include "config.h"
35#if !defined(lint) && !defined(SCCSID)
36#if !defined(lint) && !defined(SCCSID)
37#if 0
36static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
38static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
39#else
40__RCSID("$NetBSD: hist.c,v 1.20 2011/07/29 15:16:33 christos Exp $");
41#endif
37#endif /* not lint && not SCCSID */
38#include <sys/cdefs.h>
42#endif /* not lint && not SCCSID */
43#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libedit/hist.c 148834 2005-08-07 20:55:59Z stefanf $");
44__FBSDID("$FreeBSD: head/lib/libedit/hist.c 276881 2015-01-09 07:40:56Z bapt $");
40
41/*
42 * hist.c: History access functions
43 */
45
46/*
47 * hist.c: History access functions
48 */
44#include "sys.h"
45#include <stdlib.h>
46#include "el.h"
47
48/* hist_init():
49 * Initialization function.
50 */
51protected int
52hist_init(EditLine *el)
53{
54
55 el->el_history.fun = NULL;
56 el->el_history.ref = NULL;
49#include <stdlib.h>
50#include "el.h"
51
52/* hist_init():
53 * Initialization function.
54 */
55protected int
56hist_init(EditLine *el)
57{
58
59 el->el_history.fun = NULL;
60 el->el_history.ref = NULL;
57 el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
61 el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf));
58 el->el_history.sz = EL_BUFSIZ;
59 if (el->el_history.buf == NULL)
62 el->el_history.sz = EL_BUFSIZ;
63 if (el->el_history.buf == NULL)
60 return (-1);
64 return -1;
61 el->el_history.last = el->el_history.buf;
65 el->el_history.last = el->el_history.buf;
62 return (0);
66 return 0;
63}
64
65
66/* hist_end():
67 * clean up history;
68 */
69protected void
70hist_end(EditLine *el)
71{
72
67}
68
69
70/* hist_end():
71 * clean up history;
72 */
73protected void
74hist_end(EditLine *el)
75{
76
73 el_free((ptr_t) el->el_history.buf);
77 el_free(el->el_history.buf);
74 el->el_history.buf = NULL;
75}
76
77
78/* hist_set():
79 * Set new history interface
80 */
81protected int
78 el->el_history.buf = NULL;
79}
80
81
82/* hist_set():
83 * Set new history interface
84 */
85protected int
82hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
86hist_set(EditLine *el, hist_fun_t fun, void *ptr)
83{
84
85 el->el_history.ref = ptr;
86 el->el_history.fun = fun;
87{
88
89 el->el_history.ref = ptr;
90 el->el_history.fun = fun;
87 return (0);
91 return 0;
88}
89
90
91/* hist_get():
92 * Get a history line and update it in the buffer.
93 * eventno tells us the event to get.
94 */
95protected el_action_t
96hist_get(EditLine *el)
97{
92}
93
94
95/* hist_get():
96 * Get a history line and update it in the buffer.
97 * eventno tells us the event to get.
98 */
99protected el_action_t
100hist_get(EditLine *el)
101{
98 const char *hp;
102 const Char *hp;
99 int h;
100
101 if (el->el_history.eventno == 0) { /* if really the current line */
103 int h;
104
105 if (el->el_history.eventno == 0) { /* if really the current line */
102 (void) strncpy(el->el_line.buffer, el->el_history.buf,
106 (void) Strncpy(el->el_line.buffer, el->el_history.buf,
103 el->el_history.sz);
104 el->el_line.lastchar = el->el_line.buffer +
105 (el->el_history.last - el->el_history.buf);
106
107#ifdef KSHVI
108 if (el->el_map.type == MAP_VI)
109 el->el_line.cursor = el->el_line.buffer;
110 else
111#endif /* KSHVI */
112 el->el_line.cursor = el->el_line.lastchar;
113
107 el->el_history.sz);
108 el->el_line.lastchar = el->el_line.buffer +
109 (el->el_history.last - el->el_history.buf);
110
111#ifdef KSHVI
112 if (el->el_map.type == MAP_VI)
113 el->el_line.cursor = el->el_line.buffer;
114 else
115#endif /* KSHVI */
116 el->el_line.cursor = el->el_line.lastchar;
117
114 return (CC_REFRESH);
118 return CC_REFRESH;
115 }
116 if (el->el_history.ref == NULL)
119 }
120 if (el->el_history.ref == NULL)
117 return (CC_ERROR);
121 return CC_ERROR;
118
119 hp = HIST_FIRST(el);
120
121 if (hp == NULL)
122
123 hp = HIST_FIRST(el);
124
125 if (hp == NULL)
122 return (CC_ERROR);
126 return CC_ERROR;
123
124 for (h = 1; h < el->el_history.eventno; h++)
125 if ((hp = HIST_NEXT(el)) == NULL) {
126 el->el_history.eventno = h;
127
128 for (h = 1; h < el->el_history.eventno; h++)
129 if ((hp = HIST_NEXT(el)) == NULL) {
130 el->el_history.eventno = h;
127 return (CC_ERROR);
131 return CC_ERROR;
128 }
132 }
129 (void) strlcpy(el->el_line.buffer, hp,
133 (void) Strncpy(el->el_line.buffer, hp,
130 (size_t)(el->el_line.limit - el->el_line.buffer));
134 (size_t)(el->el_line.limit - el->el_line.buffer));
131 el->el_line.lastchar = el->el_line.buffer + strlen(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);
132
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;
145
137
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--;
144#ifdef KSHVI
145 if (el->el_map.type == MAP_VI)
146 el->el_line.cursor = el->el_line.buffer;
147 else
148#endif /* KSHVI */
149 el->el_line.cursor = el->el_line.lastchar;
150
146 return (CC_REFRESH);
151 return CC_REFRESH;
147}
148
149
150/* hist_command()
151 * process a history command
152 */
153protected int
152}
153
154
155/* hist_command()
156 * process a history command
157 */
158protected int
154hist_command(EditLine *el, int argc, const char **argv)
159hist_command(EditLine *el, int argc, const Char **argv)
155{
160{
156 const char *str;
161 const Char *str;
157 int num;
162 int num;
158 HistEvent ev;
163 TYPE(HistEvent) ev;
159
160 if (el->el_history.ref == NULL)
164
165 if (el->el_history.ref == NULL)
161 return (-1);
166 return -1;
162
167
163 if (argc == 1 || strcmp(argv[1], "list") == 0) {
168 if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) {
164 /* List history entries */
165
166 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
167 (void) fprintf(el->el_outfile, "%d %s",
169 /* List history entries */
170
171 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
172 (void) fprintf(el->el_outfile, "%d %s",
168 el->el_history.ev.num, str);
169 return (0);
173 el->el_history.ev.num, ct_encode_string(str, &el->el_scratch));
174 return 0;
170 }
171
172 if (argc != 3)
175 }
176
177 if (argc != 3)
173 return (-1);
178 return -1;
174
179
175 num = (int)strtol(argv[2], NULL, 0);
180 num = (int)Strtol(argv[2], NULL, 0);
176
181
177 if (strcmp(argv[1], "size") == 0)
178 return history(el->el_history.ref, &ev, H_SETSIZE, num);
182 if (Strcmp(argv[1], STR("size")) == 0)
183 return FUNW(history)(el->el_history.ref, &ev, H_SETSIZE, num);
179
184
180 if (strcmp(argv[1], "unique") == 0)
181 return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
185 if (Strcmp(argv[1], STR("unique")) == 0)
186 return FUNW(history)(el->el_history.ref, &ev, H_SETUNIQUE, num);
182
183 return -1;
184}
185
186/* hist_enlargebuf()
187 * Enlarge history buffer to specified value. Called from el_enlargebufs().
188 * Return 0 for failure, 1 for success.
189 */
190protected int
191/*ARGSUSED*/
192hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
193{
187
188 return -1;
189}
190
191/* hist_enlargebuf()
192 * Enlarge history buffer to specified value. Called from el_enlargebufs().
193 * Return 0 for failure, 1 for success.
194 */
195protected int
196/*ARGSUSED*/
197hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
198{
194 char *newbuf;
199 Char *newbuf;
195
200
196 newbuf = realloc(el->el_history.buf, newsz);
201 newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
197 if (!newbuf)
198 return 0;
199
202 if (!newbuf)
203 return 0;
204
200 (void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
205 (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
201
202 el->el_history.last = newbuf +
203 (el->el_history.last - el->el_history.buf);
204 el->el_history.buf = newbuf;
205 el->el_history.sz = newsz;
206
207 return 1;
208}
206
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;
211
212 return 1;
213}
214
215#ifdef WIDECHAR
216protected wchar_t *
217hist_convert(EditLine *el, int fn, void *arg)
218{
219 HistEventW ev;
220 if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
221 return NULL;
222 return ct_decode_string((const char *)(const void *)ev.str,
223 &el->el_scratch);
224}
225#endif