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