1 /* $NetBSD: search.c,v 1.21 2009/02/15 21:55:23 christos 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[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: search.c,v 1.21 2009/02/15 21:55:23 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * search.c: History and character search functions
48 #include <sys/types.h>
57 * Adjust cursor in vi mode to include the character under it
59 #define EL_CURSOR(el) \
60 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
61 ((el)->el_map.current == (el)->el_map.alt)))
64 * Initialize the search stuff
67 search_init(EditLine *el)
70 el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
71 if (el->el_search.patbuf == NULL)
73 el->el_search.patlen = 0;
74 el->el_search.patdir = -1;
75 el->el_search.chacha = '\0';
76 el->el_search.chadir = CHAR_FWD;
77 el->el_search.chatflg = 0;
83 * Initialize the search stuff
86 search_end(EditLine *el)
89 el_free((ptr_t) el->el_search.patbuf);
90 el->el_search.patbuf = NULL;
96 * Handle regular expression errors
100 regerror(const char *msg)
107 * Return if string matches pattern
110 el_match(const char *str, const char *pat)
115 #elif defined (REGEXP)
119 extern char *re_comp(const char *);
120 extern int re_exec(const char *);
123 if (strstr(str, pat) != NULL)
127 if (regcomp(&re, pat, 0) == 0) {
128 rv = regexec(&re, str, 0, NULL, 0) == 0;
134 #elif defined(REGEXP)
135 if ((re = regcomp(pat)) != NULL) {
136 rv = regexec(re, str);
143 if (re_comp(pat) != NULL)
146 return (re_exec(str) == 1);
152 * return True if the pattern matches the prefix
155 c_hmatch(EditLine *el, const char *str)
158 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
159 el->el_search.patbuf, str);
162 return (el_match(str, el->el_search.patbuf));
167 * Set the history seatch pattern
170 c_setpat(EditLine *el)
172 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
173 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
174 el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
175 if (el->el_search.patlen >= EL_BUFSIZ)
176 el->el_search.patlen = EL_BUFSIZ - 1;
177 if (el->el_search.patlen != 0) {
178 (void) strncpy(el->el_search.patbuf, el->el_line.buffer,
179 el->el_search.patlen);
180 el->el_search.patbuf[el->el_search.patlen] = '\0';
182 el->el_search.patlen = strlen(el->el_search.patbuf);
185 (void) fprintf(el->el_errfile, "\neventno = %d\n",
186 el->el_history.eventno);
187 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
188 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
189 el->el_search.patbuf);
190 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
191 EL_CURSOR(el) - el->el_line.buffer,
192 el->el_line.lastchar - el->el_line.buffer);
198 * Emacs incremental search
200 protected el_action_t
201 ce_inc_search(EditLine *el, int dir)
203 static const char STRfwd[] = {'f', 'w', 'd', '\0'},
204 STRbck[] = {'b', 'c', 'k', '\0'};
205 static char pchar = ':';/* ':' = normal, '?' = failed */
206 static char endcmd[2] = {'\0', '\0'};
207 char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
210 el_action_t ret = CC_NORM;
212 int ohisteventno = el->el_history.eventno;
213 size_t oldpatlen = el->el_search.patlen;
217 if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
218 el->el_search.patlen >= el->el_line.limit)
223 if (el->el_search.patlen == 0) { /* first round */
227 el->el_search.patbuf[el->el_search.patlen++] = '.';
228 el->el_search.patbuf[el->el_search.patlen++] = '*';
234 *el->el_line.lastchar++ = '\n';
235 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
236 *cp; *el->el_line.lastchar++ = *cp++)
238 *el->el_line.lastchar++ = pchar;
239 for (cp = &el->el_search.patbuf[LEN];
240 cp < &el->el_search.patbuf[el->el_search.patlen];
241 *el->el_line.lastchar++ = *cp++)
243 *el->el_line.lastchar = '\0';
246 if (el_getc(el, &ch) != 1)
247 return (ed_end_of_file(el, 0));
249 switch (el->el_map.current[(unsigned char) ch]) {
252 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
255 el->el_search.patbuf[el->el_search.patlen++] =
257 *el->el_line.lastchar++ = ch;
258 *el->el_line.lastchar = '\0';
263 case EM_INC_SEARCH_NEXT:
264 newdir = ED_SEARCH_NEXT_HISTORY;
268 case EM_INC_SEARCH_PREV:
269 newdir = ED_SEARCH_PREV_HISTORY;
273 case EM_DELETE_PREV_CHAR:
274 case ED_DELETE_PREV_CHAR:
275 if (el->el_search.patlen > LEN)
283 case 0007: /* ^G: Abort */
288 case 0027: /* ^W: Append word */
289 /* No can do if globbing characters in pattern */
290 for (cp = &el->el_search.patbuf[LEN];; cp++)
291 if (cp >= &el->el_search.patbuf[
292 el->el_search.patlen]) {
293 el->el_line.cursor +=
294 el->el_search.patlen - LEN - 1;
295 cp = c__next_word(el->el_line.cursor,
296 el->el_line.lastchar, 1,
298 while (el->el_line.cursor < cp &&
299 *el->el_line.cursor != '\n') {
300 if (el->el_search.patlen >=
305 el->el_search.patbuf[el->el_search.patlen++] =
307 *el->el_line.lastchar++ =
308 *el->el_line.cursor++;
310 el->el_line.cursor = ocursor;
311 *el->el_line.lastchar = '\0';
314 } else if (isglob(*cp)) {
320 default: /* Terminate and execute cmd */
325 case 0033: /* ESC: Terminate */
333 while (el->el_line.lastchar > el->el_line.buffer &&
334 *el->el_line.lastchar != '\n')
335 *el->el_line.lastchar-- = '\0';
336 *el->el_line.lastchar = '\0';
340 /* Can't search if unmatched '[' */
341 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
343 cp >= &el->el_search.patbuf[LEN];
345 if (*cp == '[' || *cp == ']') {
349 if (el->el_search.patlen > LEN && ch != '[') {
350 if (redo && newdir == dir) {
351 if (pchar == '?') { /* wrap around */
352 el->el_history.eventno =
353 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
354 if (hist_get(el) == CC_ERROR)
355 /* el->el_history.event
359 el->el_line.cursor = newdir ==
360 ED_SEARCH_PREV_HISTORY ?
361 el->el_line.lastchar :
364 el->el_line.cursor +=
366 ED_SEARCH_PREV_HISTORY ?
370 el->el_search.patbuf[el->el_search.patlen++] =
372 el->el_search.patbuf[el->el_search.patlen++] =
375 el->el_search.patbuf[el->el_search.patlen] =
377 if (el->el_line.cursor < el->el_line.buffer ||
378 el->el_line.cursor > el->el_line.lastchar ||
379 (ret = ce_search_line(el, newdir))
382 el->el_state.lastcmd =
383 (el_action_t) newdir;
384 ret = newdir == ED_SEARCH_PREV_HISTORY ?
385 ed_search_prev_history(el, 0) :
386 ed_search_next_history(el, 0);
387 if (ret != CC_ERROR) {
388 el->el_line.cursor = newdir ==
389 ED_SEARCH_PREV_HISTORY ?
390 el->el_line.lastchar :
392 (void) ce_search_line(el,
396 el->el_search.patlen -= LEN;
397 el->el_search.patbuf[el->el_search.patlen] =
399 if (ret == CC_ERROR) {
401 if (el->el_history.eventno !=
403 el->el_history.eventno =
405 if (hist_get(el) == CC_ERROR)
408 el->el_line.cursor = ocursor;
414 ret = ce_inc_search(el, newdir);
416 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
418 * break abort of failed search at last
424 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
425 /* restore on normal return or error exit */
427 el->el_search.patlen = oldpatlen;
428 if (el->el_history.eventno != ohisteventno) {
429 el->el_history.eventno = ohisteventno;
430 if (hist_get(el) == CC_ERROR)
433 el->el_line.cursor = ocursor;
437 if (done || ret != CC_NORM)
446 protected el_action_t
447 cv_search(EditLine *el, int dir)
450 char tmpbuf[EL_BUFSIZ];
459 el->el_search.patdir = dir;
461 tmplen = c_gets(el, &tmpbuf[LEN],
462 dir == ED_SEARCH_PREV_HISTORY ? "\n/" : "\n?" );
468 tmpbuf[tmplen] = '\0';
472 * Use the old pattern, but wild-card it.
474 if (el->el_search.patlen == 0) {
479 if (el->el_search.patbuf[0] != '.' &&
480 el->el_search.patbuf[0] != '*') {
481 (void) strncpy(tmpbuf, el->el_search.patbuf,
483 el->el_search.patbuf[0] = '.';
484 el->el_search.patbuf[1] = '*';
485 (void) strncpy(&el->el_search.patbuf[2], tmpbuf,
487 el->el_search.patlen++;
488 el->el_search.patbuf[el->el_search.patlen++] = '.';
489 el->el_search.patbuf[el->el_search.patlen++] = '*';
490 el->el_search.patbuf[el->el_search.patlen] = '\0';
495 tmpbuf[tmplen++] = '.';
496 tmpbuf[tmplen++] = '*';
498 tmpbuf[tmplen] = '\0';
499 (void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
500 el->el_search.patlen = tmplen;
502 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
503 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
504 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
505 ed_search_next_history(el, 0)) == CC_ERROR) {
511 return ed_newline(el, 0);
518 * Look for a pattern inside a line
520 protected el_action_t
521 ce_search_line(EditLine *el, int dir)
523 char *cp = el->el_line.cursor;
524 char *pattern = el->el_search.patbuf;
535 if (dir == ED_SEARCH_PREV_HISTORY) {
536 for (; cp >= el->el_line.buffer; cp--) {
537 if (el_match(cp, ocp)) {
539 el->el_line.cursor = cp;
546 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
547 if (el_match(cp, ocp)) {
549 el->el_line.cursor = cp;
562 protected el_action_t
563 cv_repeat_srch(EditLine *el, int c)
567 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
568 c, el->el_search.patlen, el->el_search.patbuf);
571 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
572 el->el_line.lastchar = el->el_line.buffer;
575 case ED_SEARCH_NEXT_HISTORY:
576 return (ed_search_next_history(el, 0));
577 case ED_SEARCH_PREV_HISTORY:
578 return (ed_search_prev_history(el, 0));
586 * Vi character search
588 protected el_action_t
589 cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
598 if (el_getc(el, &c) != 1)
599 return ed_end_of_file(el, 0);
603 /* Save for ';' and ',' commands */
604 el->el_search.chacha = ch;
605 el->el_search.chadir = direction;
606 el->el_search.chatflg = tflag;
608 cp = el->el_line.cursor;
612 for (;;cp += direction) {
613 if (cp >= el->el_line.lastchar)
615 if (cp < el->el_line.buffer)
625 el->el_line.cursor = cp;
627 if (el->el_chared.c_vcmd.action != NOP) {
629 el->el_line.cursor++;