xref: /openbsd-src/lib/libedit/hist.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: hist.c,v 1.5 2003/06/02 20:18:40 millert Exp $	*/
2 /*	$NetBSD: hist.c,v 1.2 1997/01/11 06:47:55 lukem Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Christos Zoulas of Cornell University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)hist.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static const char rcsid[] = "$OpenBSD: hist.c,v 1.5 2003/06/02 20:18:40 millert Exp $";
41 #endif
42 #endif /* not lint && not SCCSID */
43 
44 /*
45  * hist.c: History access functions
46  */
47 #include "sys.h"
48 #include <stdlib.h>
49 #include "el.h"
50 
51 /* hist_init():
52  *	Initialization function.
53  */
54 protected int
55 hist_init(el)
56     EditLine *el;
57 {
58     el->el_history.fun  = NULL;
59     el->el_history.ref  = NULL;
60     el->el_history.buf   = (char *) el_malloc(EL_BUFSIZ);
61     el->el_history.last  = el->el_history.buf;
62     return 0;
63 }
64 
65 
66 /* hist_end():
67  *	clean up history;
68  */
69 protected void
70 hist_end(el)
71     EditLine *el;
72 {
73     el_free((ptr_t) el->el_history.buf);
74     el->el_history.buf   = NULL;
75 }
76 
77 
78 /* hist_set():
79  *	Set new history interface
80  */
81 protected int
82 hist_set(el, fun, ptr)
83     EditLine *el;
84     hist_fun_t fun;
85     ptr_t ptr;
86 
87 {
88     el->el_history.ref = ptr;
89     el->el_history.fun = fun;
90     return 0;
91 }
92 
93 
94 /* hist_get():
95  *	Get a history line and update it in the buffer.
96  *	eventno tells us the event to get.
97  */
98 protected el_action_t
99 hist_get(el)
100     EditLine *el;
101 {
102     const char    *hp;
103     int     h;
104 
105     if (el->el_history.eventno == 0) {	/* if really the current line */
106 	(void)strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ - 1);
107 	el->el_line.buffer[EL_BUFSIZ - 1] = '\0';
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 
118 	return CC_REFRESH;
119     }
120 
121     if (el->el_history.ref == NULL)
122 	return CC_ERROR;
123 
124     hp = HIST_FIRST(el);
125 
126     if (hp == NULL)
127 	return CC_ERROR;
128 
129     for (h = 1; h < el->el_history.eventno; h++)
130 	if ((hp = HIST_NEXT(el)) == NULL) {
131 	    el->el_history.eventno = h;
132 	    return CC_ERROR;
133 	}
134 
135     (void)strncpy(el->el_line.buffer, hp, EL_BUFSIZ - 1);
136     el->el_line.buffer[EL_BUFSIZ - 1] = '\0';
137     el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
138 
139     if (el->el_line.lastchar > el->el_line.buffer) {
140 	if (el->el_line.lastchar[-1] == '\n')
141 	    el->el_line.lastchar--;
142 	if (el->el_line.lastchar[-1] == ' ')
143 	    el->el_line.lastchar--;
144 	if (el->el_line.lastchar < el->el_line.buffer)
145 	    el->el_line.lastchar = el->el_line.buffer;
146     }
147 
148 #ifdef KSHVI
149     if (el->el_map.type == MAP_VI)
150 	el->el_line.cursor = el->el_line.buffer;
151     else
152 #endif /* KSHVI */
153 	el->el_line.cursor = el->el_line.lastchar;
154 
155     return CC_REFRESH;
156 }
157 
158 /* hist_list()
159  *	List history entries
160  */
161 protected int
162 /*ARGSUSED*/
163 hist_list(el, argc, argv)
164     EditLine *el;
165     int argc;
166     char **argv;
167 {
168     const char *str;
169 
170     if (el->el_history.ref == NULL)
171 	return -1;
172     for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
173 	(void)fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str);
174     return 0;
175 }
176