2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: search.c,v 1.21 2009/02/15 21:55:23 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * search.c: History and character search functions
54 * Adjust cursor in vi mode to include the character under it
56 #define EL_CURSOR(el) \
57 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
58 ((el)->el_map.current == (el)->el_map.alt)))
61 * Initialize the search stuff
64 search_init(EditLine *el)
67 el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
68 if (el->el_search.patbuf == NULL)
70 el->el_search.patlen = 0;
71 el->el_search.patdir = -1;
72 el->el_search.chacha = '\0';
73 el->el_search.chadir = CHAR_FWD;
74 el->el_search.chatflg = 0;
80 * Initialize the search stuff
83 search_end(EditLine *el)
86 el_free((ptr_t) el->el_search.patbuf);
87 el->el_search.patbuf = NULL;
93 * Handle regular expression errors
97 regerror(const char *msg)
104 * Return if string matches pattern
107 el_match(const char *str, const char *pat)
112 #elif defined (REGEXP)
116 extern char *re_comp(const char *);
117 extern int re_exec(const char *);
120 if (strstr(str, pat) != NULL)
124 if (regcomp(&re, pat, 0) == 0) {
125 rv = regexec(&re, str, 0, NULL, 0) == 0;
131 #elif defined(REGEXP)
132 if ((re = regcomp(pat)) != NULL) {
133 rv = regexec(re, str);
140 if (re_comp(pat) != NULL)
143 return (re_exec(str) == 1);
149 * return True if the pattern matches the prefix
152 c_hmatch(EditLine *el, const char *str)
155 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
156 el->el_search.patbuf, str);
159 return (el_match(str, el->el_search.patbuf));
164 * Set the history seatch pattern
167 c_setpat(EditLine *el)
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';
179 el->el_search.patlen = strlen(el->el_search.patbuf);
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);
195 * Emacs incremental search
197 protected el_action_t
198 ce_inc_search(EditLine *el, int dir)
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;
207 el_action_t ret = CC_NORM;
209 int ohisteventno = el->el_history.eventno;
210 size_t oldpatlen = el->el_search.patlen;
214 if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
215 el->el_search.patlen >= el->el_line.limit)
220 if (el->el_search.patlen == 0) { /* first round */
224 el->el_search.patbuf[el->el_search.patlen++] = '.';
225 el->el_search.patbuf[el->el_search.patlen++] = '*';
231 *el->el_line.lastchar++ = '\n';
232 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
233 *cp; *el->el_line.lastchar++ = *cp++)
235 *el->el_line.lastchar++ = pchar;
236 for (cp = &el->el_search.patbuf[LEN];
237 cp < &el->el_search.patbuf[el->el_search.patlen];
238 *el->el_line.lastchar++ = *cp++)
240 *el->el_line.lastchar = '\0';
243 if (el_getc(el, &ch) != 1)
244 return (ed_end_of_file(el, 0));
246 switch (el->el_map.current[(unsigned char) ch]) {
249 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
252 el->el_search.patbuf[el->el_search.patlen++] =
254 *el->el_line.lastchar++ = ch;
255 *el->el_line.lastchar = '\0';
260 case EM_INC_SEARCH_NEXT:
261 newdir = ED_SEARCH_NEXT_HISTORY;
265 case EM_INC_SEARCH_PREV:
266 newdir = ED_SEARCH_PREV_HISTORY;
270 case EM_DELETE_PREV_CHAR:
271 case ED_DELETE_PREV_CHAR:
272 if (el->el_search.patlen > LEN)
280 case 0007: /* ^G: Abort */
285 case 0027: /* ^W: Append word */
286 /* No can do if globbing characters in pattern */
287 for (cp = &el->el_search.patbuf[LEN];; cp++)
288 if (cp >= &el->el_search.patbuf[
289 el->el_search.patlen]) {
290 el->el_line.cursor +=
291 el->el_search.patlen - LEN - 1;
292 cp = c__next_word(el->el_line.cursor,
293 el->el_line.lastchar, 1,
295 while (el->el_line.cursor < cp &&
296 *el->el_line.cursor != '\n') {
297 if (el->el_search.patlen >=
302 el->el_search.patbuf[el->el_search.patlen++] =
304 *el->el_line.lastchar++ =
305 *el->el_line.cursor++;
307 el->el_line.cursor = ocursor;
308 *el->el_line.lastchar = '\0';
311 } else if (isglob(*cp)) {
317 default: /* Terminate and execute cmd */
322 case 0033: /* ESC: Terminate */
330 while (el->el_line.lastchar > el->el_line.buffer &&
331 *el->el_line.lastchar != '\n')
332 *el->el_line.lastchar-- = '\0';
333 *el->el_line.lastchar = '\0';
337 /* Can't search if unmatched '[' */
338 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
340 cp >= &el->el_search.patbuf[LEN];
342 if (*cp == '[' || *cp == ']') {
346 if (el->el_search.patlen > LEN && ch != '[') {
347 if (redo && newdir == dir) {
348 if (pchar == '?') { /* wrap around */
349 el->el_history.eventno =
350 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
351 if (hist_get(el) == CC_ERROR)
352 /* el->el_history.event
356 el->el_line.cursor = newdir ==
357 ED_SEARCH_PREV_HISTORY ?
358 el->el_line.lastchar :
361 el->el_line.cursor +=
363 ED_SEARCH_PREV_HISTORY ?
367 el->el_search.patbuf[el->el_search.patlen++] =
369 el->el_search.patbuf[el->el_search.patlen++] =
372 el->el_search.patbuf[el->el_search.patlen] =
374 if (el->el_line.cursor < el->el_line.buffer ||
375 el->el_line.cursor > el->el_line.lastchar ||
376 (ret = ce_search_line(el, newdir))
379 el->el_state.lastcmd =
380 (el_action_t) newdir;
381 ret = newdir == ED_SEARCH_PREV_HISTORY ?
382 ed_search_prev_history(el, 0) :
383 ed_search_next_history(el, 0);
384 if (ret != CC_ERROR) {
385 el->el_line.cursor = newdir ==
386 ED_SEARCH_PREV_HISTORY ?
387 el->el_line.lastchar :
389 (void) ce_search_line(el,
393 el->el_search.patlen -= LEN;
394 el->el_search.patbuf[el->el_search.patlen] =
396 if (ret == CC_ERROR) {
398 if (el->el_history.eventno !=
400 el->el_history.eventno =
402 if (hist_get(el) == CC_ERROR)
405 el->el_line.cursor = ocursor;
411 ret = ce_inc_search(el, newdir);
413 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
415 * break abort of failed search at last
421 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
422 /* restore on normal return or error exit */
424 el->el_search.patlen = oldpatlen;
425 if (el->el_history.eventno != ohisteventno) {
426 el->el_history.eventno = ohisteventno;
427 if (hist_get(el) == CC_ERROR)
430 el->el_line.cursor = ocursor;
434 if (done || ret != CC_NORM)
443 protected el_action_t
444 cv_search(EditLine *el, int dir)
447 char tmpbuf[EL_BUFSIZ];
456 el->el_search.patdir = dir;
458 tmplen = c_gets(el, &tmpbuf[LEN],
459 dir == ED_SEARCH_PREV_HISTORY ? "\n/" : "\n?" );
465 tmpbuf[tmplen] = '\0';
469 * Use the old pattern, but wild-card it.
471 if (el->el_search.patlen == 0) {
476 if (el->el_search.patbuf[0] != '.' &&
477 el->el_search.patbuf[0] != '*') {
478 (void) strncpy(tmpbuf, el->el_search.patbuf,
480 el->el_search.patbuf[0] = '.';
481 el->el_search.patbuf[1] = '*';
482 (void) strncpy(&el->el_search.patbuf[2], tmpbuf,
484 el->el_search.patlen++;
485 el->el_search.patbuf[el->el_search.patlen++] = '.';
486 el->el_search.patbuf[el->el_search.patlen++] = '*';
487 el->el_search.patbuf[el->el_search.patlen] = '\0';
492 tmpbuf[tmplen++] = '.';
493 tmpbuf[tmplen++] = '*';
495 tmpbuf[tmplen] = '\0';
496 (void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
497 el->el_search.patlen = tmplen;
499 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
500 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
501 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
502 ed_search_next_history(el, 0)) == CC_ERROR) {
508 return ed_newline(el, 0);
515 * Look for a pattern inside a line
517 protected el_action_t
518 ce_search_line(EditLine *el, int dir)
520 char *cp = el->el_line.cursor;
521 char *pattern = el->el_search.patbuf;
532 if (dir == ED_SEARCH_PREV_HISTORY) {
533 for (; cp >= el->el_line.buffer; cp--) {
534 if (el_match(cp, ocp)) {
536 el->el_line.cursor = cp;
543 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
544 if (el_match(cp, ocp)) {
546 el->el_line.cursor = cp;
559 protected el_action_t
560 cv_repeat_srch(EditLine *el, int c)
564 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
565 c, el->el_search.patlen, el->el_search.patbuf);
568 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
569 el->el_line.lastchar = el->el_line.buffer;
572 case ED_SEARCH_NEXT_HISTORY:
573 return (ed_search_next_history(el, 0));
574 case ED_SEARCH_PREV_HISTORY:
575 return (ed_search_prev_history(el, 0));
583 * Vi character search
585 protected el_action_t
586 cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
595 if (el_getc(el, &c) != 1)
596 return ed_end_of_file(el, 0);
600 /* Save for ';' and ',' commands */
601 el->el_search.chacha = ch;
602 el->el_search.chadir = direction;
603 el->el_search.chatflg = tflag;
605 cp = el->el_line.cursor;
609 for (;;cp += direction) {
610 if (cp >= el->el_line.lastchar)
612 if (cp < el->el_line.buffer)
622 el->el_line.cursor = cp;
624 if (el->el_chared.c_vcmd.action != NOP) {
626 el->el_line.cursor++;