1 /* $NetBSD: search.c,v 1.30 2011/10/04 15:27:04 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.30 2011/10/04 15:27:04 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 = el_malloc(EL_BUFSIZ *
71 sizeof(*el->el_search.patbuf));
72 if (el->el_search.patbuf == NULL)
74 el->el_search.patlen = 0;
75 el->el_search.patdir = -1;
76 el->el_search.chacha = '\0';
77 el->el_search.chadir = CHAR_FWD;
78 el->el_search.chatflg = 0;
84 * Initialize the search stuff
87 search_end(EditLine *el)
90 el_free(el->el_search.patbuf);
91 el->el_search.patbuf = NULL;
97 * Handle regular expression errors
101 regerror(const char *msg)
108 * Return if string matches pattern
111 el_match(const Char *str, const Char *pat)
114 static ct_buffer_t conv;
119 #elif defined (REGEXP)
123 extern char *re_comp(const char *);
124 extern int re_exec(const char *);
127 if (Strstr(str, pat) != 0)
131 if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
132 rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
139 #elif defined(REGEXP)
140 if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
141 rv = regexec(re, ct_encode_string(str, &conv));
148 if (re_comp(ct_encode_string(pat, &conv)) != NULL)
151 return re_exec(ct_encode_string(str, &conv) == 1);
157 * return True if the pattern matches the prefix
160 c_hmatch(EditLine *el, const Char *str)
163 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
164 el->el_search.patbuf, str);
167 return el_match(str, el->el_search.patbuf);
172 * Set the history seatch pattern
175 c_setpat(EditLine *el)
177 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
178 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
179 el->el_search.patlen =
180 (size_t)(EL_CURSOR(el) - el->el_line.buffer);
181 if (el->el_search.patlen >= EL_BUFSIZ)
182 el->el_search.patlen = EL_BUFSIZ - 1;
183 if (el->el_search.patlen != 0) {
184 (void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
185 el->el_search.patlen);
186 el->el_search.patbuf[el->el_search.patlen] = '\0';
188 el->el_search.patlen = Strlen(el->el_search.patbuf);
191 (void) fprintf(el->el_errfile, "\neventno = %d\n",
192 el->el_history.eventno);
193 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
194 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
195 el->el_search.patbuf);
196 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
197 EL_CURSOR(el) - el->el_line.buffer,
198 el->el_line.lastchar - el->el_line.buffer);
204 * Emacs incremental search
206 protected el_action_t
207 ce_inc_search(EditLine *el, int dir)
209 static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
210 STRbck[] = {'b', 'c', 'k', '\0'};
211 static Char pchar = ':';/* ':' = normal, '?' = failed */
212 static Char endcmd[2] = {'\0', '\0'};
213 Char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
216 el_action_t ret = CC_NORM;
218 int ohisteventno = el->el_history.eventno;
219 size_t oldpatlen = el->el_search.patlen;
223 if (el->el_line.lastchar + sizeof(STRfwd) /
224 sizeof(*el->el_line.lastchar) + 2 +
225 el->el_search.patlen >= el->el_line.limit)
230 if (el->el_search.patlen == 0) { /* first round */
234 el->el_search.patbuf[el->el_search.patlen++] = '.';
235 el->el_search.patbuf[el->el_search.patlen++] = '*';
241 *el->el_line.lastchar++ = '\n';
242 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
243 *cp; *el->el_line.lastchar++ = *cp++)
245 *el->el_line.lastchar++ = pchar;
246 for (cp = &el->el_search.patbuf[LEN];
247 cp < &el->el_search.patbuf[el->el_search.patlen];
248 *el->el_line.lastchar++ = *cp++)
250 *el->el_line.lastchar = '\0';
253 if (FUN(el,getc)(el, &ch) != 1)
254 return ed_end_of_file(el, 0);
256 switch (el->el_map.current[(unsigned char) ch]) {
259 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
262 el->el_search.patbuf[el->el_search.patlen++] =
264 *el->el_line.lastchar++ = ch;
265 *el->el_line.lastchar = '\0';
270 case EM_INC_SEARCH_NEXT:
271 newdir = ED_SEARCH_NEXT_HISTORY;
275 case EM_INC_SEARCH_PREV:
276 newdir = ED_SEARCH_PREV_HISTORY;
280 case EM_DELETE_PREV_CHAR:
281 case ED_DELETE_PREV_CHAR:
282 if (el->el_search.patlen > LEN)
290 case 0007: /* ^G: Abort */
295 case 0027: /* ^W: Append word */
296 /* No can do if globbing characters in pattern */
297 for (cp = &el->el_search.patbuf[LEN];; cp++)
298 if (cp >= &el->el_search.patbuf[
299 el->el_search.patlen]) {
300 el->el_line.cursor +=
301 el->el_search.patlen - LEN - 1;
302 cp = c__next_word(el->el_line.cursor,
303 el->el_line.lastchar, 1,
305 while (el->el_line.cursor < cp &&
306 *el->el_line.cursor != '\n') {
307 if (el->el_search.patlen >=
312 el->el_search.patbuf[el->el_search.patlen++] =
314 *el->el_line.lastchar++ =
315 *el->el_line.cursor++;
317 el->el_line.cursor = ocursor;
318 *el->el_line.lastchar = '\0';
321 } else if (isglob(*cp)) {
327 default: /* Terminate and execute cmd */
329 FUN(el,push)(el, endcmd);
332 case 0033: /* ESC: Terminate */
340 while (el->el_line.lastchar > el->el_line.buffer &&
341 *el->el_line.lastchar != '\n')
342 *el->el_line.lastchar-- = '\0';
343 *el->el_line.lastchar = '\0';
347 /* Can't search if unmatched '[' */
348 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
350 cp >= &el->el_search.patbuf[LEN];
352 if (*cp == '[' || *cp == ']') {
356 if (el->el_search.patlen > LEN && ch != '[') {
357 if (redo && newdir == dir) {
358 if (pchar == '?') { /* wrap around */
359 el->el_history.eventno =
360 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
361 if (hist_get(el) == CC_ERROR)
362 /* el->el_history.event
366 el->el_line.cursor = newdir ==
367 ED_SEARCH_PREV_HISTORY ?
368 el->el_line.lastchar :
371 el->el_line.cursor +=
373 ED_SEARCH_PREV_HISTORY ?
377 el->el_search.patbuf[el->el_search.patlen++] =
379 el->el_search.patbuf[el->el_search.patlen++] =
382 el->el_search.patbuf[el->el_search.patlen] =
384 if (el->el_line.cursor < el->el_line.buffer ||
385 el->el_line.cursor > el->el_line.lastchar ||
386 (ret = ce_search_line(el, newdir))
389 el->el_state.lastcmd =
390 (el_action_t) newdir;
392 (newdir == ED_SEARCH_PREV_HISTORY ?
393 ed_search_prev_history(el, 0) :
394 ed_search_next_history(el, 0));
395 if (ret != CC_ERROR) {
396 el->el_line.cursor = newdir ==
397 ED_SEARCH_PREV_HISTORY ?
398 el->el_line.lastchar :
400 (void) ce_search_line(el,
404 el->el_search.patlen -= LEN;
405 el->el_search.patbuf[el->el_search.patlen] =
407 if (ret == CC_ERROR) {
409 if (el->el_history.eventno !=
411 el->el_history.eventno =
413 if (hist_get(el) == CC_ERROR)
416 el->el_line.cursor = ocursor;
422 ret = ce_inc_search(el, newdir);
424 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
426 * break abort of failed search at last
432 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
433 /* restore on normal return or error exit */
435 el->el_search.patlen = oldpatlen;
436 if (el->el_history.eventno != ohisteventno) {
437 el->el_history.eventno = ohisteventno;
438 if (hist_get(el) == CC_ERROR)
441 el->el_line.cursor = ocursor;
445 if (done || ret != CC_NORM)
454 protected el_action_t
455 cv_search(EditLine *el, int dir)
458 Char tmpbuf[EL_BUFSIZ];
467 el->el_search.patdir = dir;
469 tmplen = c_gets(el, &tmpbuf[LEN],
470 dir == ED_SEARCH_PREV_HISTORY ? STR("\n/") : STR("\n?") );
476 tmpbuf[tmplen] = '\0';
480 * Use the old pattern, but wild-card it.
482 if (el->el_search.patlen == 0) {
487 if (el->el_search.patbuf[0] != '.' &&
488 el->el_search.patbuf[0] != '*') {
489 (void) Strncpy(tmpbuf, el->el_search.patbuf,
490 sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
491 el->el_search.patbuf[0] = '.';
492 el->el_search.patbuf[1] = '*';
493 (void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
495 el->el_search.patlen++;
496 el->el_search.patbuf[el->el_search.patlen++] = '.';
497 el->el_search.patbuf[el->el_search.patlen++] = '*';
498 el->el_search.patbuf[el->el_search.patlen] = '\0';
503 tmpbuf[tmplen++] = '.';
504 tmpbuf[tmplen++] = '*';
506 tmpbuf[tmplen] = '\0';
507 (void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
508 el->el_search.patlen = (size_t)tmplen;
510 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
511 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
512 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
513 ed_search_next_history(el, 0)) == CC_ERROR) {
519 return ed_newline(el, 0);
526 * Look for a pattern inside a line
528 protected el_action_t
529 ce_search_line(EditLine *el, int dir)
531 Char *cp = el->el_line.cursor;
532 Char *pattern = el->el_search.patbuf;
543 if (dir == ED_SEARCH_PREV_HISTORY) {
544 for (; cp >= el->el_line.buffer; cp--) {
545 if (el_match(cp, ocp)) {
547 el->el_line.cursor = cp;
554 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
555 if (el_match(cp, ocp)) {
557 el->el_line.cursor = cp;
570 protected el_action_t
571 cv_repeat_srch(EditLine *el, Int c)
575 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
576 c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
579 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
580 el->el_line.lastchar = el->el_line.buffer;
583 case ED_SEARCH_NEXT_HISTORY:
584 return ed_search_next_history(el, 0);
585 case ED_SEARCH_PREV_HISTORY:
586 return ed_search_prev_history(el, 0);
594 * Vi character search
596 protected el_action_t
597 cv_csearch(EditLine *el, int direction, Int ch, int count, int tflag)
606 if (FUN(el,getc)(el, &c) != 1)
607 return ed_end_of_file(el, 0);
611 /* Save for ';' and ',' commands */
612 el->el_search.chacha = ch;
613 el->el_search.chadir = direction;
614 el->el_search.chatflg = (char)tflag;
616 cp = el->el_line.cursor;
620 for (;;cp += direction) {
621 if (cp >= el->el_line.lastchar)
623 if (cp < el->el_line.buffer)
633 el->el_line.cursor = cp;
635 if (el->el_chared.c_vcmd.action != NOP) {
637 el->el_line.cursor++;