xref: /minix/lib/libedit/search.c (revision 3e1db26a)
1*3e1db26aSLionel Sambuc /*	$NetBSD: search.c,v 1.30 2011/10/04 15:27:04 christos Exp $	*/
2*3e1db26aSLionel Sambuc 
3*3e1db26aSLionel Sambuc /*-
4*3e1db26aSLionel Sambuc  * Copyright (c) 1992, 1993
5*3e1db26aSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*3e1db26aSLionel Sambuc  *
7*3e1db26aSLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*3e1db26aSLionel Sambuc  * Christos Zoulas of Cornell University.
9*3e1db26aSLionel Sambuc  *
10*3e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*3e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
12*3e1db26aSLionel Sambuc  * are met:
13*3e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*3e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*3e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*3e1db26aSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*3e1db26aSLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*3e1db26aSLionel Sambuc  *    without specific prior written permission.
21*3e1db26aSLionel Sambuc  *
22*3e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*3e1db26aSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*3e1db26aSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*3e1db26aSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*3e1db26aSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*3e1db26aSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*3e1db26aSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*3e1db26aSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*3e1db26aSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*3e1db26aSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*3e1db26aSLionel Sambuc  * SUCH DAMAGE.
33*3e1db26aSLionel Sambuc  */
34*3e1db26aSLionel Sambuc 
35*3e1db26aSLionel Sambuc #include "config.h"
36*3e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*3e1db26aSLionel Sambuc #if 0
38*3e1db26aSLionel Sambuc static char sccsid[] = "@(#)search.c	8.1 (Berkeley) 6/4/93";
39*3e1db26aSLionel Sambuc #else
40*3e1db26aSLionel Sambuc __RCSID("$NetBSD: search.c,v 1.30 2011/10/04 15:27:04 christos Exp $");
41*3e1db26aSLionel Sambuc #endif
42*3e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
43*3e1db26aSLionel Sambuc 
44*3e1db26aSLionel Sambuc /*
45*3e1db26aSLionel Sambuc  * search.c: History and character search functions
46*3e1db26aSLionel Sambuc  */
47*3e1db26aSLionel Sambuc #include <stdlib.h>
48*3e1db26aSLionel Sambuc #if defined(REGEX)
49*3e1db26aSLionel Sambuc #include <regex.h>
50*3e1db26aSLionel Sambuc #elif defined(REGEXP)
51*3e1db26aSLionel Sambuc #include <regexp.h>
52*3e1db26aSLionel Sambuc #endif
53*3e1db26aSLionel Sambuc #include "el.h"
54*3e1db26aSLionel Sambuc 
55*3e1db26aSLionel Sambuc /*
56*3e1db26aSLionel Sambuc  * Adjust cursor in vi mode to include the character under it
57*3e1db26aSLionel Sambuc  */
58*3e1db26aSLionel Sambuc #define	EL_CURSOR(el) \
59*3e1db26aSLionel Sambuc     ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
60*3e1db26aSLionel Sambuc 			    ((el)->el_map.current == (el)->el_map.alt)))
61*3e1db26aSLionel Sambuc 
62*3e1db26aSLionel Sambuc /* search_init():
63*3e1db26aSLionel Sambuc  *	Initialize the search stuff
64*3e1db26aSLionel Sambuc  */
65*3e1db26aSLionel Sambuc protected int
search_init(EditLine * el)66*3e1db26aSLionel Sambuc search_init(EditLine *el)
67*3e1db26aSLionel Sambuc {
68*3e1db26aSLionel Sambuc 
69*3e1db26aSLionel Sambuc 	el->el_search.patbuf = el_malloc(EL_BUFSIZ *
70*3e1db26aSLionel Sambuc 	    sizeof(*el->el_search.patbuf));
71*3e1db26aSLionel Sambuc 	if (el->el_search.patbuf == NULL)
72*3e1db26aSLionel Sambuc 		return -1;
73*3e1db26aSLionel Sambuc 	el->el_search.patlen = 0;
74*3e1db26aSLionel Sambuc 	el->el_search.patdir = -1;
75*3e1db26aSLionel Sambuc 	el->el_search.chacha = '\0';
76*3e1db26aSLionel Sambuc 	el->el_search.chadir = CHAR_FWD;
77*3e1db26aSLionel Sambuc 	el->el_search.chatflg = 0;
78*3e1db26aSLionel Sambuc 	return 0;
79*3e1db26aSLionel Sambuc }
80*3e1db26aSLionel Sambuc 
81*3e1db26aSLionel Sambuc 
82*3e1db26aSLionel Sambuc /* search_end():
83*3e1db26aSLionel Sambuc  *	Initialize the search stuff
84*3e1db26aSLionel Sambuc  */
85*3e1db26aSLionel Sambuc protected void
search_end(EditLine * el)86*3e1db26aSLionel Sambuc search_end(EditLine *el)
87*3e1db26aSLionel Sambuc {
88*3e1db26aSLionel Sambuc 
89*3e1db26aSLionel Sambuc 	el_free(el->el_search.patbuf);
90*3e1db26aSLionel Sambuc 	el->el_search.patbuf = NULL;
91*3e1db26aSLionel Sambuc }
92*3e1db26aSLionel Sambuc 
93*3e1db26aSLionel Sambuc 
94*3e1db26aSLionel Sambuc #ifdef REGEXP
95*3e1db26aSLionel Sambuc /* regerror():
96*3e1db26aSLionel Sambuc  *	Handle regular expression errors
97*3e1db26aSLionel Sambuc  */
98*3e1db26aSLionel Sambuc public void
99*3e1db26aSLionel Sambuc /*ARGSUSED*/
regerror(const char * msg)100*3e1db26aSLionel Sambuc regerror(const char *msg)
101*3e1db26aSLionel Sambuc {
102*3e1db26aSLionel Sambuc }
103*3e1db26aSLionel Sambuc #endif
104*3e1db26aSLionel Sambuc 
105*3e1db26aSLionel Sambuc 
106*3e1db26aSLionel Sambuc /* el_match():
107*3e1db26aSLionel Sambuc  *	Return if string matches pattern
108*3e1db26aSLionel Sambuc  */
109*3e1db26aSLionel Sambuc protected int
el_match(const Char * str,const Char * pat)110*3e1db26aSLionel Sambuc el_match(const Char *str, const Char *pat)
111*3e1db26aSLionel Sambuc {
112*3e1db26aSLionel Sambuc #ifdef WIDECHAR
113*3e1db26aSLionel Sambuc 	static ct_buffer_t conv;
114*3e1db26aSLionel Sambuc #endif
115*3e1db26aSLionel Sambuc #if defined (REGEX)
116*3e1db26aSLionel Sambuc 	regex_t re;
117*3e1db26aSLionel Sambuc 	int rv;
118*3e1db26aSLionel Sambuc #elif defined (REGEXP)
119*3e1db26aSLionel Sambuc 	regexp *rp;
120*3e1db26aSLionel Sambuc 	int rv;
121*3e1db26aSLionel Sambuc #else
122*3e1db26aSLionel Sambuc 	extern char	*re_comp(const char *);
123*3e1db26aSLionel Sambuc 	extern int	 re_exec(const char *);
124*3e1db26aSLionel Sambuc #endif
125*3e1db26aSLionel Sambuc 
126*3e1db26aSLionel Sambuc 	if (Strstr(str, pat) != 0)
127*3e1db26aSLionel Sambuc 		return 1;
128*3e1db26aSLionel Sambuc 
129*3e1db26aSLionel Sambuc #if defined(REGEX)
130*3e1db26aSLionel Sambuc 	if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
131*3e1db26aSLionel Sambuc 		rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
132*3e1db26aSLionel Sambuc 		    0) == 0;
133*3e1db26aSLionel Sambuc 		regfree(&re);
134*3e1db26aSLionel Sambuc 	} else {
135*3e1db26aSLionel Sambuc 		rv = 0;
136*3e1db26aSLionel Sambuc 	}
137*3e1db26aSLionel Sambuc 	return rv;
138*3e1db26aSLionel Sambuc #elif defined(REGEXP)
139*3e1db26aSLionel Sambuc 	if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
140*3e1db26aSLionel Sambuc 		rv = regexec(re, ct_encode_string(str, &conv));
141*3e1db26aSLionel Sambuc 		el_free(re);
142*3e1db26aSLionel Sambuc 	} else {
143*3e1db26aSLionel Sambuc 		rv = 0;
144*3e1db26aSLionel Sambuc 	}
145*3e1db26aSLionel Sambuc 	return rv;
146*3e1db26aSLionel Sambuc #else
147*3e1db26aSLionel Sambuc 	if (re_comp(ct_encode_string(pat, &conv)) != NULL)
148*3e1db26aSLionel Sambuc 		return 0;
149*3e1db26aSLionel Sambuc 	else
150*3e1db26aSLionel Sambuc 		return re_exec(ct_encode_string(str, &conv) == 1);
151*3e1db26aSLionel Sambuc #endif
152*3e1db26aSLionel Sambuc }
153*3e1db26aSLionel Sambuc 
154*3e1db26aSLionel Sambuc 
155*3e1db26aSLionel Sambuc /* c_hmatch():
156*3e1db26aSLionel Sambuc  *	 return True if the pattern matches the prefix
157*3e1db26aSLionel Sambuc  */
158*3e1db26aSLionel Sambuc protected int
c_hmatch(EditLine * el,const Char * str)159*3e1db26aSLionel Sambuc c_hmatch(EditLine *el, const Char *str)
160*3e1db26aSLionel Sambuc {
161*3e1db26aSLionel Sambuc #ifdef SDEBUG
162*3e1db26aSLionel Sambuc 	(void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
163*3e1db26aSLionel Sambuc 	    el->el_search.patbuf, str);
164*3e1db26aSLionel Sambuc #endif /* SDEBUG */
165*3e1db26aSLionel Sambuc 
166*3e1db26aSLionel Sambuc 	return el_match(str, el->el_search.patbuf);
167*3e1db26aSLionel Sambuc }
168*3e1db26aSLionel Sambuc 
169*3e1db26aSLionel Sambuc 
170*3e1db26aSLionel Sambuc /* c_setpat():
171*3e1db26aSLionel Sambuc  *	Set the history seatch pattern
172*3e1db26aSLionel Sambuc  */
173*3e1db26aSLionel Sambuc protected void
c_setpat(EditLine * el)174*3e1db26aSLionel Sambuc c_setpat(EditLine *el)
175*3e1db26aSLionel Sambuc {
176*3e1db26aSLionel Sambuc 	if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
177*3e1db26aSLionel Sambuc 	    el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
178*3e1db26aSLionel Sambuc 		el->el_search.patlen =
179*3e1db26aSLionel Sambuc 		    (size_t)(EL_CURSOR(el) - el->el_line.buffer);
180*3e1db26aSLionel Sambuc 		if (el->el_search.patlen >= EL_BUFSIZ)
181*3e1db26aSLionel Sambuc 			el->el_search.patlen = EL_BUFSIZ - 1;
182*3e1db26aSLionel Sambuc 		if (el->el_search.patlen != 0) {
183*3e1db26aSLionel Sambuc 			(void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
184*3e1db26aSLionel Sambuc 			    el->el_search.patlen);
185*3e1db26aSLionel Sambuc 			el->el_search.patbuf[el->el_search.patlen] = '\0';
186*3e1db26aSLionel Sambuc 		} else
187*3e1db26aSLionel Sambuc 			el->el_search.patlen = Strlen(el->el_search.patbuf);
188*3e1db26aSLionel Sambuc 	}
189*3e1db26aSLionel Sambuc #ifdef SDEBUG
190*3e1db26aSLionel Sambuc 	(void) fprintf(el->el_errfile, "\neventno = %d\n",
191*3e1db26aSLionel Sambuc 	    el->el_history.eventno);
192*3e1db26aSLionel Sambuc 	(void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
193*3e1db26aSLionel Sambuc 	(void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
194*3e1db26aSLionel Sambuc 	    el->el_search.patbuf);
195*3e1db26aSLionel Sambuc 	(void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
196*3e1db26aSLionel Sambuc 	    EL_CURSOR(el) - el->el_line.buffer,
197*3e1db26aSLionel Sambuc 	    el->el_line.lastchar - el->el_line.buffer);
198*3e1db26aSLionel Sambuc #endif
199*3e1db26aSLionel Sambuc }
200*3e1db26aSLionel Sambuc 
201*3e1db26aSLionel Sambuc 
202*3e1db26aSLionel Sambuc /* ce_inc_search():
203*3e1db26aSLionel Sambuc  *	Emacs incremental search
204*3e1db26aSLionel Sambuc  */
205*3e1db26aSLionel Sambuc protected el_action_t
ce_inc_search(EditLine * el,int dir)206*3e1db26aSLionel Sambuc ce_inc_search(EditLine *el, int dir)
207*3e1db26aSLionel Sambuc {
208*3e1db26aSLionel Sambuc 	static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
209*3e1db26aSLionel Sambuc 	     STRbck[] = {'b', 'c', 'k', '\0'};
210*3e1db26aSLionel Sambuc 	static Char pchar = ':';/* ':' = normal, '?' = failed */
211*3e1db26aSLionel Sambuc 	static Char endcmd[2] = {'\0', '\0'};
212*3e1db26aSLionel Sambuc 	Char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
213*3e1db26aSLionel Sambuc 	const Char *cp;
214*3e1db26aSLionel Sambuc 
215*3e1db26aSLionel Sambuc 	el_action_t ret = CC_NORM;
216*3e1db26aSLionel Sambuc 
217*3e1db26aSLionel Sambuc 	int ohisteventno = el->el_history.eventno;
218*3e1db26aSLionel Sambuc 	size_t oldpatlen = el->el_search.patlen;
219*3e1db26aSLionel Sambuc 	int newdir = dir;
220*3e1db26aSLionel Sambuc 	int done, redo;
221*3e1db26aSLionel Sambuc 
222*3e1db26aSLionel Sambuc 	if (el->el_line.lastchar + sizeof(STRfwd) /
223*3e1db26aSLionel Sambuc 	    sizeof(*el->el_line.lastchar) + 2 +
224*3e1db26aSLionel Sambuc 	    el->el_search.patlen >= el->el_line.limit)
225*3e1db26aSLionel Sambuc 		return CC_ERROR;
226*3e1db26aSLionel Sambuc 
227*3e1db26aSLionel Sambuc 	for (;;) {
228*3e1db26aSLionel Sambuc 
229*3e1db26aSLionel Sambuc 		if (el->el_search.patlen == 0) {	/* first round */
230*3e1db26aSLionel Sambuc 			pchar = ':';
231*3e1db26aSLionel Sambuc #ifdef ANCHOR
232*3e1db26aSLionel Sambuc #define	LEN	2
233*3e1db26aSLionel Sambuc 			el->el_search.patbuf[el->el_search.patlen++] = '.';
234*3e1db26aSLionel Sambuc 			el->el_search.patbuf[el->el_search.patlen++] = '*';
235*3e1db26aSLionel Sambuc #else
236*3e1db26aSLionel Sambuc #define	LEN	0
237*3e1db26aSLionel Sambuc #endif
238*3e1db26aSLionel Sambuc 		}
239*3e1db26aSLionel Sambuc 		done = redo = 0;
240*3e1db26aSLionel Sambuc 		*el->el_line.lastchar++ = '\n';
241*3e1db26aSLionel Sambuc 		for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
242*3e1db26aSLionel Sambuc 		    *cp; *el->el_line.lastchar++ = *cp++)
243*3e1db26aSLionel Sambuc 			continue;
244*3e1db26aSLionel Sambuc 		*el->el_line.lastchar++ = pchar;
245*3e1db26aSLionel Sambuc 		for (cp = &el->el_search.patbuf[LEN];
246*3e1db26aSLionel Sambuc 		    cp < &el->el_search.patbuf[el->el_search.patlen];
247*3e1db26aSLionel Sambuc 		    *el->el_line.lastchar++ = *cp++)
248*3e1db26aSLionel Sambuc 			continue;
249*3e1db26aSLionel Sambuc 		*el->el_line.lastchar = '\0';
250*3e1db26aSLionel Sambuc 		re_refresh(el);
251*3e1db26aSLionel Sambuc 
252*3e1db26aSLionel Sambuc 		if (FUN(el,getc)(el, &ch) != 1)
253*3e1db26aSLionel Sambuc 			return ed_end_of_file(el, 0);
254*3e1db26aSLionel Sambuc 
255*3e1db26aSLionel Sambuc 		switch (el->el_map.current[(unsigned char) ch]) {
256*3e1db26aSLionel Sambuc 		case ED_INSERT:
257*3e1db26aSLionel Sambuc 		case ED_DIGIT:
258*3e1db26aSLionel Sambuc 			if (el->el_search.patlen >= EL_BUFSIZ - LEN)
259*3e1db26aSLionel Sambuc 				terminal_beep(el);
260*3e1db26aSLionel Sambuc 			else {
261*3e1db26aSLionel Sambuc 				el->el_search.patbuf[el->el_search.patlen++] =
262*3e1db26aSLionel Sambuc 				    ch;
263*3e1db26aSLionel Sambuc 				*el->el_line.lastchar++ = ch;
264*3e1db26aSLionel Sambuc 				*el->el_line.lastchar = '\0';
265*3e1db26aSLionel Sambuc 				re_refresh(el);
266*3e1db26aSLionel Sambuc 			}
267*3e1db26aSLionel Sambuc 			break;
268*3e1db26aSLionel Sambuc 
269*3e1db26aSLionel Sambuc 		case EM_INC_SEARCH_NEXT:
270*3e1db26aSLionel Sambuc 			newdir = ED_SEARCH_NEXT_HISTORY;
271*3e1db26aSLionel Sambuc 			redo++;
272*3e1db26aSLionel Sambuc 			break;
273*3e1db26aSLionel Sambuc 
274*3e1db26aSLionel Sambuc 		case EM_INC_SEARCH_PREV:
275*3e1db26aSLionel Sambuc 			newdir = ED_SEARCH_PREV_HISTORY;
276*3e1db26aSLionel Sambuc 			redo++;
277*3e1db26aSLionel Sambuc 			break;
278*3e1db26aSLionel Sambuc 
279*3e1db26aSLionel Sambuc 		case EM_DELETE_PREV_CHAR:
280*3e1db26aSLionel Sambuc 		case ED_DELETE_PREV_CHAR:
281*3e1db26aSLionel Sambuc 			if (el->el_search.patlen > LEN)
282*3e1db26aSLionel Sambuc 				done++;
283*3e1db26aSLionel Sambuc 			else
284*3e1db26aSLionel Sambuc 				terminal_beep(el);
285*3e1db26aSLionel Sambuc 			break;
286*3e1db26aSLionel Sambuc 
287*3e1db26aSLionel Sambuc 		default:
288*3e1db26aSLionel Sambuc 			switch (ch) {
289*3e1db26aSLionel Sambuc 			case 0007:	/* ^G: Abort */
290*3e1db26aSLionel Sambuc 				ret = CC_ERROR;
291*3e1db26aSLionel Sambuc 				done++;
292*3e1db26aSLionel Sambuc 				break;
293*3e1db26aSLionel Sambuc 
294*3e1db26aSLionel Sambuc 			case 0027:	/* ^W: Append word */
295*3e1db26aSLionel Sambuc 			/* No can do if globbing characters in pattern */
296*3e1db26aSLionel Sambuc 				for (cp = &el->el_search.patbuf[LEN];; cp++)
297*3e1db26aSLionel Sambuc 				    if (cp >= &el->el_search.patbuf[
298*3e1db26aSLionel Sambuc 					el->el_search.patlen]) {
299*3e1db26aSLionel Sambuc 					el->el_line.cursor +=
300*3e1db26aSLionel Sambuc 					    el->el_search.patlen - LEN - 1;
301*3e1db26aSLionel Sambuc 					cp = c__next_word(el->el_line.cursor,
302*3e1db26aSLionel Sambuc 					    el->el_line.lastchar, 1,
303*3e1db26aSLionel Sambuc 					    ce__isword);
304*3e1db26aSLionel Sambuc 					while (el->el_line.cursor < cp &&
305*3e1db26aSLionel Sambuc 					    *el->el_line.cursor != '\n') {
306*3e1db26aSLionel Sambuc 						if (el->el_search.patlen >=
307*3e1db26aSLionel Sambuc 						    EL_BUFSIZ - LEN) {
308*3e1db26aSLionel Sambuc 							terminal_beep(el);
309*3e1db26aSLionel Sambuc 							break;
310*3e1db26aSLionel Sambuc 						}
311*3e1db26aSLionel Sambuc 						el->el_search.patbuf[el->el_search.patlen++] =
312*3e1db26aSLionel Sambuc 						    *el->el_line.cursor;
313*3e1db26aSLionel Sambuc 						*el->el_line.lastchar++ =
314*3e1db26aSLionel Sambuc 						    *el->el_line.cursor++;
315*3e1db26aSLionel Sambuc 					}
316*3e1db26aSLionel Sambuc 					el->el_line.cursor = ocursor;
317*3e1db26aSLionel Sambuc 					*el->el_line.lastchar = '\0';
318*3e1db26aSLionel Sambuc 					re_refresh(el);
319*3e1db26aSLionel Sambuc 					break;
320*3e1db26aSLionel Sambuc 				    } else if (isglob(*cp)) {
321*3e1db26aSLionel Sambuc 					    terminal_beep(el);
322*3e1db26aSLionel Sambuc 					    break;
323*3e1db26aSLionel Sambuc 				    }
324*3e1db26aSLionel Sambuc 				break;
325*3e1db26aSLionel Sambuc 
326*3e1db26aSLionel Sambuc 			default:	/* Terminate and execute cmd */
327*3e1db26aSLionel Sambuc 				endcmd[0] = ch;
328*3e1db26aSLionel Sambuc 				FUN(el,push)(el, endcmd);
329*3e1db26aSLionel Sambuc 				/* FALLTHROUGH */
330*3e1db26aSLionel Sambuc 
331*3e1db26aSLionel Sambuc 			case 0033:	/* ESC: Terminate */
332*3e1db26aSLionel Sambuc 				ret = CC_REFRESH;
333*3e1db26aSLionel Sambuc 				done++;
334*3e1db26aSLionel Sambuc 				break;
335*3e1db26aSLionel Sambuc 			}
336*3e1db26aSLionel Sambuc 			break;
337*3e1db26aSLionel Sambuc 		}
338*3e1db26aSLionel Sambuc 
339*3e1db26aSLionel Sambuc 		while (el->el_line.lastchar > el->el_line.buffer &&
340*3e1db26aSLionel Sambuc 		    *el->el_line.lastchar != '\n')
341*3e1db26aSLionel Sambuc 			*el->el_line.lastchar-- = '\0';
342*3e1db26aSLionel Sambuc 		*el->el_line.lastchar = '\0';
343*3e1db26aSLionel Sambuc 
344*3e1db26aSLionel Sambuc 		if (!done) {
345*3e1db26aSLionel Sambuc 
346*3e1db26aSLionel Sambuc 			/* Can't search if unmatched '[' */
347*3e1db26aSLionel Sambuc 			for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
348*3e1db26aSLionel Sambuc 			    ch = ']';
349*3e1db26aSLionel Sambuc 			    cp >= &el->el_search.patbuf[LEN];
350*3e1db26aSLionel Sambuc 			    cp--)
351*3e1db26aSLionel Sambuc 				if (*cp == '[' || *cp == ']') {
352*3e1db26aSLionel Sambuc 					ch = *cp;
353*3e1db26aSLionel Sambuc 					break;
354*3e1db26aSLionel Sambuc 				}
355*3e1db26aSLionel Sambuc 			if (el->el_search.patlen > LEN && ch != '[') {
356*3e1db26aSLionel Sambuc 				if (redo && newdir == dir) {
357*3e1db26aSLionel Sambuc 					if (pchar == '?') { /* wrap around */
358*3e1db26aSLionel Sambuc 						el->el_history.eventno =
359*3e1db26aSLionel Sambuc 						    newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
360*3e1db26aSLionel Sambuc 						if (hist_get(el) == CC_ERROR)
361*3e1db26aSLionel Sambuc 							/* el->el_history.event
362*3e1db26aSLionel Sambuc 							 * no was fixed by
363*3e1db26aSLionel Sambuc 							 * first call */
364*3e1db26aSLionel Sambuc 							(void) hist_get(el);
365*3e1db26aSLionel Sambuc 						el->el_line.cursor = newdir ==
366*3e1db26aSLionel Sambuc 						    ED_SEARCH_PREV_HISTORY ?
367*3e1db26aSLionel Sambuc 						    el->el_line.lastchar :
368*3e1db26aSLionel Sambuc 						    el->el_line.buffer;
369*3e1db26aSLionel Sambuc 					} else
370*3e1db26aSLionel Sambuc 						el->el_line.cursor +=
371*3e1db26aSLionel Sambuc 						    newdir ==
372*3e1db26aSLionel Sambuc 						    ED_SEARCH_PREV_HISTORY ?
373*3e1db26aSLionel Sambuc 						    -1 : 1;
374*3e1db26aSLionel Sambuc 				}
375*3e1db26aSLionel Sambuc #ifdef ANCHOR
376*3e1db26aSLionel Sambuc 				el->el_search.patbuf[el->el_search.patlen++] =
377*3e1db26aSLionel Sambuc 				    '.';
378*3e1db26aSLionel Sambuc 				el->el_search.patbuf[el->el_search.patlen++] =
379*3e1db26aSLionel Sambuc 				    '*';
380*3e1db26aSLionel Sambuc #endif
381*3e1db26aSLionel Sambuc 				el->el_search.patbuf[el->el_search.patlen] =
382*3e1db26aSLionel Sambuc 				    '\0';
383*3e1db26aSLionel Sambuc 				if (el->el_line.cursor < el->el_line.buffer ||
384*3e1db26aSLionel Sambuc 				    el->el_line.cursor > el->el_line.lastchar ||
385*3e1db26aSLionel Sambuc 				    (ret = ce_search_line(el, newdir))
386*3e1db26aSLionel Sambuc 				    == CC_ERROR) {
387*3e1db26aSLionel Sambuc 					/* avoid c_setpat */
388*3e1db26aSLionel Sambuc 					el->el_state.lastcmd =
389*3e1db26aSLionel Sambuc 					    (el_action_t) newdir;
390*3e1db26aSLionel Sambuc 					ret = (el_action_t)
391*3e1db26aSLionel Sambuc 					    (newdir == ED_SEARCH_PREV_HISTORY ?
392*3e1db26aSLionel Sambuc 					    ed_search_prev_history(el, 0) :
393*3e1db26aSLionel Sambuc 					    ed_search_next_history(el, 0));
394*3e1db26aSLionel Sambuc 					if (ret != CC_ERROR) {
395*3e1db26aSLionel Sambuc 						el->el_line.cursor = newdir ==
396*3e1db26aSLionel Sambuc 						    ED_SEARCH_PREV_HISTORY ?
397*3e1db26aSLionel Sambuc 						    el->el_line.lastchar :
398*3e1db26aSLionel Sambuc 						    el->el_line.buffer;
399*3e1db26aSLionel Sambuc 						(void) ce_search_line(el,
400*3e1db26aSLionel Sambuc 						    newdir);
401*3e1db26aSLionel Sambuc 					}
402*3e1db26aSLionel Sambuc 				}
403*3e1db26aSLionel Sambuc 				el->el_search.patlen -= LEN;
404*3e1db26aSLionel Sambuc 				el->el_search.patbuf[el->el_search.patlen] =
405*3e1db26aSLionel Sambuc 				    '\0';
406*3e1db26aSLionel Sambuc 				if (ret == CC_ERROR) {
407*3e1db26aSLionel Sambuc 					terminal_beep(el);
408*3e1db26aSLionel Sambuc 					if (el->el_history.eventno !=
409*3e1db26aSLionel Sambuc 					    ohisteventno) {
410*3e1db26aSLionel Sambuc 						el->el_history.eventno =
411*3e1db26aSLionel Sambuc 						    ohisteventno;
412*3e1db26aSLionel Sambuc 						if (hist_get(el) == CC_ERROR)
413*3e1db26aSLionel Sambuc 							return CC_ERROR;
414*3e1db26aSLionel Sambuc 					}
415*3e1db26aSLionel Sambuc 					el->el_line.cursor = ocursor;
416*3e1db26aSLionel Sambuc 					pchar = '?';
417*3e1db26aSLionel Sambuc 				} else {
418*3e1db26aSLionel Sambuc 					pchar = ':';
419*3e1db26aSLionel Sambuc 				}
420*3e1db26aSLionel Sambuc 			}
421*3e1db26aSLionel Sambuc 			ret = ce_inc_search(el, newdir);
422*3e1db26aSLionel Sambuc 
423*3e1db26aSLionel Sambuc 			if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
424*3e1db26aSLionel Sambuc 				/*
425*3e1db26aSLionel Sambuc 				 * break abort of failed search at last
426*3e1db26aSLionel Sambuc 				 * non-failed
427*3e1db26aSLionel Sambuc 				 */
428*3e1db26aSLionel Sambuc 				ret = CC_NORM;
429*3e1db26aSLionel Sambuc 
430*3e1db26aSLionel Sambuc 		}
431*3e1db26aSLionel Sambuc 		if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
432*3e1db26aSLionel Sambuc 			/* restore on normal return or error exit */
433*3e1db26aSLionel Sambuc 			pchar = oldpchar;
434*3e1db26aSLionel Sambuc 			el->el_search.patlen = oldpatlen;
435*3e1db26aSLionel Sambuc 			if (el->el_history.eventno != ohisteventno) {
436*3e1db26aSLionel Sambuc 				el->el_history.eventno = ohisteventno;
437*3e1db26aSLionel Sambuc 				if (hist_get(el) == CC_ERROR)
438*3e1db26aSLionel Sambuc 					return CC_ERROR;
439*3e1db26aSLionel Sambuc 			}
440*3e1db26aSLionel Sambuc 			el->el_line.cursor = ocursor;
441*3e1db26aSLionel Sambuc 			if (ret == CC_ERROR)
442*3e1db26aSLionel Sambuc 				re_refresh(el);
443*3e1db26aSLionel Sambuc 		}
444*3e1db26aSLionel Sambuc 		if (done || ret != CC_NORM)
445*3e1db26aSLionel Sambuc 			return ret;
446*3e1db26aSLionel Sambuc 	}
447*3e1db26aSLionel Sambuc }
448*3e1db26aSLionel Sambuc 
449*3e1db26aSLionel Sambuc 
450*3e1db26aSLionel Sambuc /* cv_search():
451*3e1db26aSLionel Sambuc  *	Vi search.
452*3e1db26aSLionel Sambuc  */
453*3e1db26aSLionel Sambuc protected el_action_t
cv_search(EditLine * el,int dir)454*3e1db26aSLionel Sambuc cv_search(EditLine *el, int dir)
455*3e1db26aSLionel Sambuc {
456*3e1db26aSLionel Sambuc 	Char ch;
457*3e1db26aSLionel Sambuc 	Char tmpbuf[EL_BUFSIZ];
458*3e1db26aSLionel Sambuc 	ssize_t tmplen;
459*3e1db26aSLionel Sambuc 
460*3e1db26aSLionel Sambuc #ifdef ANCHOR
461*3e1db26aSLionel Sambuc 	tmpbuf[0] = '.';
462*3e1db26aSLionel Sambuc 	tmpbuf[1] = '*';
463*3e1db26aSLionel Sambuc #endif
464*3e1db26aSLionel Sambuc 	tmplen = LEN;
465*3e1db26aSLionel Sambuc 
466*3e1db26aSLionel Sambuc 	el->el_search.patdir = dir;
467*3e1db26aSLionel Sambuc 
468*3e1db26aSLionel Sambuc 	tmplen = c_gets(el, &tmpbuf[LEN],
469*3e1db26aSLionel Sambuc 		dir == ED_SEARCH_PREV_HISTORY ? STR("\n/") : STR("\n?") );
470*3e1db26aSLionel Sambuc 	if (tmplen == -1)
471*3e1db26aSLionel Sambuc 		return CC_REFRESH;
472*3e1db26aSLionel Sambuc 
473*3e1db26aSLionel Sambuc 	tmplen += LEN;
474*3e1db26aSLionel Sambuc 	ch = tmpbuf[tmplen];
475*3e1db26aSLionel Sambuc 	tmpbuf[tmplen] = '\0';
476*3e1db26aSLionel Sambuc 
477*3e1db26aSLionel Sambuc 	if (tmplen == LEN) {
478*3e1db26aSLionel Sambuc 		/*
479*3e1db26aSLionel Sambuc 		 * Use the old pattern, but wild-card it.
480*3e1db26aSLionel Sambuc 		 */
481*3e1db26aSLionel Sambuc 		if (el->el_search.patlen == 0) {
482*3e1db26aSLionel Sambuc 			re_refresh(el);
483*3e1db26aSLionel Sambuc 			return CC_ERROR;
484*3e1db26aSLionel Sambuc 		}
485*3e1db26aSLionel Sambuc #ifdef ANCHOR
486*3e1db26aSLionel Sambuc 		if (el->el_search.patbuf[0] != '.' &&
487*3e1db26aSLionel Sambuc 		    el->el_search.patbuf[0] != '*') {
488*3e1db26aSLionel Sambuc 			(void) Strncpy(tmpbuf, el->el_search.patbuf,
489*3e1db26aSLionel Sambuc 			    sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
490*3e1db26aSLionel Sambuc 			el->el_search.patbuf[0] = '.';
491*3e1db26aSLionel Sambuc 			el->el_search.patbuf[1] = '*';
492*3e1db26aSLionel Sambuc 			(void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
493*3e1db26aSLionel Sambuc 			    EL_BUFSIZ - 3);
494*3e1db26aSLionel Sambuc 			el->el_search.patlen++;
495*3e1db26aSLionel Sambuc 			el->el_search.patbuf[el->el_search.patlen++] = '.';
496*3e1db26aSLionel Sambuc 			el->el_search.patbuf[el->el_search.patlen++] = '*';
497*3e1db26aSLionel Sambuc 			el->el_search.patbuf[el->el_search.patlen] = '\0';
498*3e1db26aSLionel Sambuc 		}
499*3e1db26aSLionel Sambuc #endif
500*3e1db26aSLionel Sambuc 	} else {
501*3e1db26aSLionel Sambuc #ifdef ANCHOR
502*3e1db26aSLionel Sambuc 		tmpbuf[tmplen++] = '.';
503*3e1db26aSLionel Sambuc 		tmpbuf[tmplen++] = '*';
504*3e1db26aSLionel Sambuc #endif
505*3e1db26aSLionel Sambuc 		tmpbuf[tmplen] = '\0';
506*3e1db26aSLionel Sambuc 		(void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
507*3e1db26aSLionel Sambuc 		el->el_search.patlen = (size_t)tmplen;
508*3e1db26aSLionel Sambuc 	}
509*3e1db26aSLionel Sambuc 	el->el_state.lastcmd = (el_action_t) dir;	/* avoid c_setpat */
510*3e1db26aSLionel Sambuc 	el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
511*3e1db26aSLionel Sambuc 	if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
512*3e1db26aSLionel Sambuc 	    ed_search_next_history(el, 0)) == CC_ERROR) {
513*3e1db26aSLionel Sambuc 		re_refresh(el);
514*3e1db26aSLionel Sambuc 		return CC_ERROR;
515*3e1db26aSLionel Sambuc 	}
516*3e1db26aSLionel Sambuc 	if (ch == 0033) {
517*3e1db26aSLionel Sambuc 		re_refresh(el);
518*3e1db26aSLionel Sambuc 		return ed_newline(el, 0);
519*3e1db26aSLionel Sambuc 	}
520*3e1db26aSLionel Sambuc 	return CC_REFRESH;
521*3e1db26aSLionel Sambuc }
522*3e1db26aSLionel Sambuc 
523*3e1db26aSLionel Sambuc 
524*3e1db26aSLionel Sambuc /* ce_search_line():
525*3e1db26aSLionel Sambuc  *	Look for a pattern inside a line
526*3e1db26aSLionel Sambuc  */
527*3e1db26aSLionel Sambuc protected el_action_t
ce_search_line(EditLine * el,int dir)528*3e1db26aSLionel Sambuc ce_search_line(EditLine *el, int dir)
529*3e1db26aSLionel Sambuc {
530*3e1db26aSLionel Sambuc 	Char *cp = el->el_line.cursor;
531*3e1db26aSLionel Sambuc 	Char *pattern = el->el_search.patbuf;
532*3e1db26aSLionel Sambuc 	Char oc, *ocp;
533*3e1db26aSLionel Sambuc #ifdef ANCHOR
534*3e1db26aSLionel Sambuc 	ocp = &pattern[1];
535*3e1db26aSLionel Sambuc 	oc = *ocp;
536*3e1db26aSLionel Sambuc 	*ocp = '^';
537*3e1db26aSLionel Sambuc #else
538*3e1db26aSLionel Sambuc 	ocp = pattern;
539*3e1db26aSLionel Sambuc 	oc = *ocp;
540*3e1db26aSLionel Sambuc #endif
541*3e1db26aSLionel Sambuc 
542*3e1db26aSLionel Sambuc 	if (dir == ED_SEARCH_PREV_HISTORY) {
543*3e1db26aSLionel Sambuc 		for (; cp >= el->el_line.buffer; cp--) {
544*3e1db26aSLionel Sambuc 			if (el_match(cp, ocp)) {
545*3e1db26aSLionel Sambuc 				*ocp = oc;
546*3e1db26aSLionel Sambuc 				el->el_line.cursor = cp;
547*3e1db26aSLionel Sambuc 				return CC_NORM;
548*3e1db26aSLionel Sambuc 			}
549*3e1db26aSLionel Sambuc 		}
550*3e1db26aSLionel Sambuc 		*ocp = oc;
551*3e1db26aSLionel Sambuc 		return CC_ERROR;
552*3e1db26aSLionel Sambuc 	} else {
553*3e1db26aSLionel Sambuc 		for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
554*3e1db26aSLionel Sambuc 			if (el_match(cp, ocp)) {
555*3e1db26aSLionel Sambuc 				*ocp = oc;
556*3e1db26aSLionel Sambuc 				el->el_line.cursor = cp;
557*3e1db26aSLionel Sambuc 				return CC_NORM;
558*3e1db26aSLionel Sambuc 			}
559*3e1db26aSLionel Sambuc 		}
560*3e1db26aSLionel Sambuc 		*ocp = oc;
561*3e1db26aSLionel Sambuc 		return CC_ERROR;
562*3e1db26aSLionel Sambuc 	}
563*3e1db26aSLionel Sambuc }
564*3e1db26aSLionel Sambuc 
565*3e1db26aSLionel Sambuc 
566*3e1db26aSLionel Sambuc /* cv_repeat_srch():
567*3e1db26aSLionel Sambuc  *	Vi repeat search
568*3e1db26aSLionel Sambuc  */
569*3e1db26aSLionel Sambuc protected el_action_t
cv_repeat_srch(EditLine * el,Int c)570*3e1db26aSLionel Sambuc cv_repeat_srch(EditLine *el, Int c)
571*3e1db26aSLionel Sambuc {
572*3e1db26aSLionel Sambuc 
573*3e1db26aSLionel Sambuc #ifdef SDEBUG
574*3e1db26aSLionel Sambuc 	(void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
575*3e1db26aSLionel Sambuc 	    c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
576*3e1db26aSLionel Sambuc #endif
577*3e1db26aSLionel Sambuc 
578*3e1db26aSLionel Sambuc 	el->el_state.lastcmd = (el_action_t) c;	/* Hack to stop c_setpat */
579*3e1db26aSLionel Sambuc 	el->el_line.lastchar = el->el_line.buffer;
580*3e1db26aSLionel Sambuc 
581*3e1db26aSLionel Sambuc 	switch (c) {
582*3e1db26aSLionel Sambuc 	case ED_SEARCH_NEXT_HISTORY:
583*3e1db26aSLionel Sambuc 		return ed_search_next_history(el, 0);
584*3e1db26aSLionel Sambuc 	case ED_SEARCH_PREV_HISTORY:
585*3e1db26aSLionel Sambuc 		return ed_search_prev_history(el, 0);
586*3e1db26aSLionel Sambuc 	default:
587*3e1db26aSLionel Sambuc 		return CC_ERROR;
588*3e1db26aSLionel Sambuc 	}
589*3e1db26aSLionel Sambuc }
590*3e1db26aSLionel Sambuc 
591*3e1db26aSLionel Sambuc 
592*3e1db26aSLionel Sambuc /* cv_csearch():
593*3e1db26aSLionel Sambuc  *	Vi character search
594*3e1db26aSLionel Sambuc  */
595*3e1db26aSLionel Sambuc protected el_action_t
cv_csearch(EditLine * el,int direction,Int ch,int count,int tflag)596*3e1db26aSLionel Sambuc cv_csearch(EditLine *el, int direction, Int ch, int count, int tflag)
597*3e1db26aSLionel Sambuc {
598*3e1db26aSLionel Sambuc 	Char *cp;
599*3e1db26aSLionel Sambuc 
600*3e1db26aSLionel Sambuc 	if (ch == 0)
601*3e1db26aSLionel Sambuc 		return CC_ERROR;
602*3e1db26aSLionel Sambuc 
603*3e1db26aSLionel Sambuc 	if (ch == (Int)-1) {
604*3e1db26aSLionel Sambuc 		Char c;
605*3e1db26aSLionel Sambuc 		if (FUN(el,getc)(el, &c) != 1)
606*3e1db26aSLionel Sambuc 			return ed_end_of_file(el, 0);
607*3e1db26aSLionel Sambuc 		ch = c;
608*3e1db26aSLionel Sambuc 	}
609*3e1db26aSLionel Sambuc 
610*3e1db26aSLionel Sambuc 	/* Save for ';' and ',' commands */
611*3e1db26aSLionel Sambuc 	el->el_search.chacha = ch;
612*3e1db26aSLionel Sambuc 	el->el_search.chadir = direction;
613*3e1db26aSLionel Sambuc 	el->el_search.chatflg = (char)tflag;
614*3e1db26aSLionel Sambuc 
615*3e1db26aSLionel Sambuc 	cp = el->el_line.cursor;
616*3e1db26aSLionel Sambuc 	while (count--) {
617*3e1db26aSLionel Sambuc 		if ((Int)*cp == ch)
618*3e1db26aSLionel Sambuc 			cp += direction;
619*3e1db26aSLionel Sambuc 		for (;;cp += direction) {
620*3e1db26aSLionel Sambuc 			if (cp >= el->el_line.lastchar)
621*3e1db26aSLionel Sambuc 				return CC_ERROR;
622*3e1db26aSLionel Sambuc 			if (cp < el->el_line.buffer)
623*3e1db26aSLionel Sambuc 				return CC_ERROR;
624*3e1db26aSLionel Sambuc 			if ((Int)*cp == ch)
625*3e1db26aSLionel Sambuc 				break;
626*3e1db26aSLionel Sambuc 		}
627*3e1db26aSLionel Sambuc 	}
628*3e1db26aSLionel Sambuc 
629*3e1db26aSLionel Sambuc 	if (tflag)
630*3e1db26aSLionel Sambuc 		cp -= direction;
631*3e1db26aSLionel Sambuc 
632*3e1db26aSLionel Sambuc 	el->el_line.cursor = cp;
633*3e1db26aSLionel Sambuc 
634*3e1db26aSLionel Sambuc 	if (el->el_chared.c_vcmd.action != NOP) {
635*3e1db26aSLionel Sambuc 		if (direction > 0)
636*3e1db26aSLionel Sambuc 			el->el_line.cursor++;
637*3e1db26aSLionel Sambuc 		cv_delfini(el);
638*3e1db26aSLionel Sambuc 		return CC_REFRESH;
639*3e1db26aSLionel Sambuc 	}
640*3e1db26aSLionel Sambuc 	return CC_CURSOR;
641*3e1db26aSLionel Sambuc }
642