1 /* $NetBSD: hist.c,v 1.14 2003/08/07 16:44:31 agc 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.14 2003/08/07 16:44:31 agc Exp $");
42 #endif /* not lint && not SCCSID */
45 * hist.c: History access functions
51 * Initialization function.
54 hist_init(EditLine
*el
)
57 el
->el_history
.fun
= NULL
;
58 el
->el_history
.ref
= NULL
;
59 el
->el_history
.buf
= (char *) el_malloc(EL_BUFSIZ
);
60 el
->el_history
.sz
= EL_BUFSIZ
;
61 if (el
->el_history
.buf
== NULL
)
63 el
->el_history
.last
= el
->el_history
.buf
;
72 hist_end(EditLine
*el
)
75 el_free((ptr_t
) el
->el_history
.buf
);
76 el
->el_history
.buf
= NULL
;
81 * Set new history interface
84 hist_set(EditLine
*el
, hist_fun_t fun
, ptr_t ptr
)
87 el
->el_history
.ref
= ptr
;
88 el
->el_history
.fun
= fun
;
94 * Get a history line and update it in the buffer.
95 * eventno tells us the event to get.
98 hist_get(EditLine
*el
)
103 if (el
->el_history
.eventno
== 0) { /* if really the current line */
104 (void) strncpy(el
->el_line
.buffer
, el
->el_history
.buf
,
106 el
->el_line
.lastchar
= el
->el_line
.buffer
+
107 (el
->el_history
.last
- el
->el_history
.buf
);
110 if (el
->el_map
.type
== MAP_VI
)
111 el
->el_line
.cursor
= el
->el_line
.buffer
;
114 el
->el_line
.cursor
= el
->el_line
.lastchar
;
118 if (el
->el_history
.ref
== NULL
)
126 for (h
= 1; h
< el
->el_history
.eventno
; h
++)
127 if ((hp
= HIST_NEXT(el
)) == NULL
) {
128 el
->el_history
.eventno
= h
;
131 (void) strlcpy(el
->el_line
.buffer
, hp
,
132 (size_t)(el
->el_line
.limit
- el
->el_line
.buffer
));
133 el
->el_line
.lastchar
= el
->el_line
.buffer
+ strlen(el
->el_line
.buffer
);
135 if (el
->el_line
.lastchar
> el
->el_line
.buffer
136 && el
->el_line
.lastchar
[-1] == '\n')
137 el
->el_line
.lastchar
--;
138 if (el
->el_line
.lastchar
> el
->el_line
.buffer
139 && el
->el_line
.lastchar
[-1] == ' ')
140 el
->el_line
.lastchar
--;
142 if (el
->el_map
.type
== MAP_VI
)
143 el
->el_line
.cursor
= el
->el_line
.buffer
;
146 el
->el_line
.cursor
= el
->el_line
.lastchar
;
153 * process a history command
156 hist_command(EditLine
*el
, int argc
, const char **argv
)
162 if (el
->el_history
.ref
== NULL
)
165 if (argc
== 1 || strcmp(argv
[1], "list") == 0) {
166 /* List history entries */
168 for (str
= HIST_LAST(el
); str
!= NULL
; str
= HIST_PREV(el
))
169 (void) fprintf(el
->el_outfile
, "%d %s",
170 el
->el_history
.ev
.num
, str
);
177 num
= (int)strtol(argv
[2], NULL
, 0);
179 if (strcmp(argv
[1], "size") == 0)
180 return history(el
->el_history
.ref
, &ev
, H_SETSIZE
, num
);
182 if (strcmp(argv
[1], "unique") == 0)
183 return history(el
->el_history
.ref
, &ev
, H_SETUNIQUE
, num
);
189 * Enlarge history buffer to specified value. Called from el_enlargebufs().
190 * Return 0 for failure, 1 for success.
194 hist_enlargebuf(EditLine
*el
, size_t oldsz
, size_t newsz
)
198 newbuf
= realloc(el
->el_history
.buf
, newsz
);
202 (void) memset(&newbuf
[oldsz
], '\0', newsz
- oldsz
);
204 el
->el_history
.last
= newbuf
+
205 (el
->el_history
.last
- el
->el_history
.buf
);
206 el
->el_history
.buf
= newbuf
;
207 el
->el_history
.sz
= newsz
;