1 /* $OpenBSD: hist.c,v 1.18 2017/04/12 18:24:37 tb Exp $ */
2 /* $NetBSD: hist.c,v 1.28 2016/04/11 00:50:13 christos Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * hist.c: History access functions
47 * Initialization function.
50 hist_init(EditLine
*el
)
53 el
->el_history
.fun
= NULL
;
54 el
->el_history
.ref
= NULL
;
55 el
->el_history
.buf
= reallocarray(NULL
, EL_BUFSIZ
,
56 sizeof(*el
->el_history
.buf
));
57 el
->el_history
.sz
= EL_BUFSIZ
;
58 if (el
->el_history
.buf
== NULL
)
60 el
->el_history
.last
= el
->el_history
.buf
;
69 hist_end(EditLine
*el
)
72 free(el
->el_history
.buf
);
73 el
->el_history
.buf
= NULL
;
78 * Set new history interface
81 hist_set(EditLine
*el
, hist_fun_t fun
, void *ptr
)
84 el
->el_history
.ref
= ptr
;
85 el
->el_history
.fun
= fun
;
91 * Get a history line and update it in the buffer.
92 * eventno tells us the event to get.
95 hist_get(EditLine
*el
)
100 if (el
->el_history
.eventno
== 0) { /* if really the current line */
101 (void) wcsncpy(el
->el_line
.buffer
, el
->el_history
.buf
,
103 el
->el_line
.lastchar
= el
->el_line
.buffer
+
104 (el
->el_history
.last
- el
->el_history
.buf
);
107 if (el
->el_map
.type
== MAP_VI
)
108 el
->el_line
.cursor
= el
->el_line
.buffer
;
111 el
->el_line
.cursor
= el
->el_line
.lastchar
;
115 if (el
->el_history
.ref
== NULL
)
123 for (h
= 1; h
< el
->el_history
.eventno
; h
++)
124 if ((hp
= HIST_NEXT(el
)) == NULL
) {
125 el
->el_history
.eventno
= h
;
128 (void) wcsncpy(el
->el_line
.buffer
, hp
,
129 (size_t)(el
->el_line
.limit
- el
->el_line
.buffer
));
130 el
->el_line
.buffer
[el
->el_line
.limit
- el
->el_line
.buffer
- 1] = '\0';
131 el
->el_line
.lastchar
= el
->el_line
.buffer
+ wcslen(el
->el_line
.buffer
);
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
--;
140 if (el
->el_map
.type
== MAP_VI
)
141 el
->el_line
.cursor
= el
->el_line
.buffer
;
144 el
->el_line
.cursor
= el
->el_line
.lastchar
;
151 * process a history command
154 hist_command(EditLine
*el
, int argc
, const wchar_t **argv
)
160 if (el
->el_history
.ref
== NULL
)
163 if (argc
== 1 || wcscmp(argv
[1], L
"list") == 0) {
164 /* List history entries */
166 for (str
= HIST_LAST(el
); str
!= NULL
; str
= HIST_PREV(el
))
167 (void) fprintf(el
->el_outfile
, "%d %s",
168 el
->el_history
.ev
.num
, ct_encode_string(str
, &el
->el_scratch
));
175 num
= (int)wcstol(argv
[2], NULL
, 0);
177 if (wcscmp(argv
[1], L
"size") == 0)
178 return history(el
->el_history
.ref
, &ev
, H_SETSIZE
, num
);
180 if (wcscmp(argv
[1], L
"unique") == 0)
181 return history(el
->el_history
.ref
, &ev
, H_SETUNIQUE
, num
);
187 * Enlarge history buffer to specified value. Called from el_enlargebufs().
188 * Return 0 for failure, 1 for success.
192 hist_enlargebuf(EditLine
*el
, size_t oldsz
, size_t newsz
)
196 newbuf
= recallocarray(el
->el_history
.buf
, oldsz
, newsz
,
201 el
->el_history
.last
= newbuf
+
202 (el
->el_history
.last
- el
->el_history
.buf
);
203 el
->el_history
.buf
= newbuf
;
204 el
->el_history
.sz
= newsz
;
210 hist_convert(EditLine
*el
, int fn
, void *arg
)
213 if ((*(el
)->el_history
.fun
)((el
)->el_history
.ref
, &ev
, fn
, arg
) == -1)
215 return ct_decode_string((const char *)(const void *)ev
.str
,