1 /* $NetBSD: search.c,v 1.39 2016/02/24 14:25:38 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.39 2016/02/24 14:25:38 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * search.c: History and character search functions
61 * Adjust cursor in vi mode to include the character under it
63 #define EL_CURSOR(el) \
64 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
65 ((el)->el_map.current == (el)->el_map.alt)))
68 * Initialize the search stuff
71 search_init(EditLine *el)
74 el->el_search.patbuf = el_malloc(EL_BUFSIZ *
75 sizeof(*el->el_search.patbuf));
76 if (el->el_search.patbuf == NULL)
78 el->el_search.patlen = 0;
79 el->el_search.patdir = -1;
80 el->el_search.chacha = '\0';
81 el->el_search.chadir = CHAR_FWD;
82 el->el_search.chatflg = 0;
88 * Initialize the search stuff
91 search_end(EditLine *el)
94 el_free(el->el_search.patbuf);
95 el->el_search.patbuf = NULL;
101 * Handle regular expression errors
105 regerror(const char *msg)
112 * Return if string matches pattern
115 el_match(const Char *str, const Char *pat)
118 static ct_buffer_t conv;
123 #elif defined (REGEXP)
127 extern char *re_comp(const char *);
128 extern int re_exec(const char *);
131 if (Strstr(str, pat) != 0)
135 if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
136 rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
143 #elif defined(REGEXP)
144 if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
145 rv = regexec(re, ct_encode_string(str, &conv));
152 if (re_comp(ct_encode_string(pat, &conv)) != NULL)
155 return re_exec(ct_encode_string(str, &conv)) == 1;
161 * return True if the pattern matches the prefix
164 c_hmatch(EditLine *el, const Char *str)
167 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
168 el->el_search.patbuf, str);
171 return el_match(str, el->el_search.patbuf);
176 * Set the history seatch pattern
179 c_setpat(EditLine *el)
181 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
182 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
183 el->el_search.patlen =
184 (size_t)(EL_CURSOR(el) - el->el_line.buffer);
185 if (el->el_search.patlen >= EL_BUFSIZ)
186 el->el_search.patlen = EL_BUFSIZ - 1;
187 if (el->el_search.patlen != 0) {
188 (void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
189 el->el_search.patlen);
190 el->el_search.patbuf[el->el_search.patlen] = '\0';
192 el->el_search.patlen = Strlen(el->el_search.patbuf);
195 (void) fprintf(el->el_errfile, "\neventno = %d\n",
196 el->el_history.eventno);
197 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
198 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
199 el->el_search.patbuf);
200 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
201 EL_CURSOR(el) - el->el_line.buffer,
202 el->el_line.lastchar - el->el_line.buffer);
208 * Emacs incremental search
210 protected el_action_t
211 ce_inc_search(EditLine *el, int dir)
213 static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
214 STRbck[] = {'b', 'c', 'k', '\0'};
215 static Char pchar = ':';/* ':' = normal, '?' = failed */
216 static Char endcmd[2] = {'\0', '\0'};
217 Char *ocursor = el->el_line.cursor, oldpchar = pchar, ch;
221 el_action_t ret = CC_NORM;
223 int ohisteventno = el->el_history.eventno;
224 size_t oldpatlen = el->el_search.patlen;
228 if (el->el_line.lastchar + sizeof(STRfwd) /
229 sizeof(*el->el_line.lastchar) + 2 +
230 el->el_search.patlen >= el->el_line.limit)
235 if (el->el_search.patlen == 0) { /* first round */
239 el->el_search.patbuf[el->el_search.patlen++] = '.';
240 el->el_search.patbuf[el->el_search.patlen++] = '*';
246 *el->el_line.lastchar++ = '\n';
247 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
248 *cp; *el->el_line.lastchar++ = *cp++)
250 *el->el_line.lastchar++ = pchar;
251 for (cp = &el->el_search.patbuf[LEN];
252 cp < &el->el_search.patbuf[el->el_search.patlen];
253 *el->el_line.lastchar++ = *cp++)
255 *el->el_line.lastchar = '\0';
258 if (el_wgetc(el, &wch) != 1)
259 return ed_end_of_file(el, 0);
263 switch (el->el_map.current[(unsigned char) ch]) {
266 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
269 el->el_search.patbuf[el->el_search.patlen++] =
271 *el->el_line.lastchar++ = ch;
272 *el->el_line.lastchar = '\0';
277 case EM_INC_SEARCH_NEXT:
278 newdir = ED_SEARCH_NEXT_HISTORY;
282 case EM_INC_SEARCH_PREV:
283 newdir = ED_SEARCH_PREV_HISTORY;
287 case EM_DELETE_PREV_CHAR:
288 case ED_DELETE_PREV_CHAR:
289 if (el->el_search.patlen > LEN)
297 case 0007: /* ^G: Abort */
302 case 0027: /* ^W: Append word */
303 /* No can do if globbing characters in pattern */
304 for (cp = &el->el_search.patbuf[LEN];; cp++)
305 if (cp >= &el->el_search.patbuf[
306 el->el_search.patlen]) {
307 el->el_line.cursor +=
308 el->el_search.patlen - LEN - 1;
309 cp = c__next_word(el->el_line.cursor,
310 el->el_line.lastchar, 1,
312 while (el->el_line.cursor < cp &&
313 *el->el_line.cursor != '\n') {
314 if (el->el_search.patlen >=
319 el->el_search.patbuf[el->el_search.patlen++] =
321 *el->el_line.lastchar++ =
322 *el->el_line.cursor++;
324 el->el_line.cursor = ocursor;
325 *el->el_line.lastchar = '\0';
328 } else if (isglob(*cp)) {
334 default: /* Terminate and execute cmd */
336 FUN(el,push)(el, endcmd);
339 case 0033: /* ESC: Terminate */
347 while (el->el_line.lastchar > el->el_line.buffer &&
348 *el->el_line.lastchar != '\n')
349 *el->el_line.lastchar-- = '\0';
350 *el->el_line.lastchar = '\0';
354 /* Can't search if unmatched '[' */
355 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
357 cp >= &el->el_search.patbuf[LEN];
359 if (*cp == '[' || *cp == ']') {
363 if (el->el_search.patlen > LEN && ch != L'[') {
364 if (redo && newdir == dir) {
365 if (pchar == '?') { /* wrap around */
366 el->el_history.eventno =
367 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
368 if (hist_get(el) == CC_ERROR)
369 /* el->el_history.event
373 el->el_line.cursor = newdir ==
374 ED_SEARCH_PREV_HISTORY ?
375 el->el_line.lastchar :
378 el->el_line.cursor +=
380 ED_SEARCH_PREV_HISTORY ?
384 el->el_search.patbuf[el->el_search.patlen++] =
386 el->el_search.patbuf[el->el_search.patlen++] =
389 el->el_search.patbuf[el->el_search.patlen] =
391 if (el->el_line.cursor < el->el_line.buffer ||
392 el->el_line.cursor > el->el_line.lastchar ||
393 (ret = ce_search_line(el, newdir))
396 el->el_state.lastcmd =
397 (el_action_t) newdir;
399 (newdir == ED_SEARCH_PREV_HISTORY ?
400 ed_search_prev_history(el, 0) :
401 ed_search_next_history(el, 0));
402 if (ret != CC_ERROR) {
403 el->el_line.cursor = newdir ==
404 ED_SEARCH_PREV_HISTORY ?
405 el->el_line.lastchar :
407 (void) ce_search_line(el,
411 el->el_search.patlen -= LEN;
412 el->el_search.patbuf[el->el_search.patlen] =
414 if (ret == CC_ERROR) {
416 if (el->el_history.eventno !=
418 el->el_history.eventno =
420 if (hist_get(el) == CC_ERROR)
423 el->el_line.cursor = ocursor;
429 ret = ce_inc_search(el, newdir);
431 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
433 * break abort of failed search at last
439 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
440 /* restore on normal return or error exit */
442 el->el_search.patlen = oldpatlen;
443 if (el->el_history.eventno != ohisteventno) {
444 el->el_history.eventno = ohisteventno;
445 if (hist_get(el) == CC_ERROR)
448 el->el_line.cursor = ocursor;
452 if (done || ret != CC_NORM)
461 protected el_action_t
462 cv_search(EditLine *el, int dir)
465 Char tmpbuf[EL_BUFSIZ];
474 el->el_search.patdir = dir;
476 tmplen = c_gets(el, &tmpbuf[LEN],
477 dir == ED_SEARCH_PREV_HISTORY ? STR("\n/") : STR("\n?") );
483 tmpbuf[tmplen] = '\0';
487 * Use the old pattern, but wild-card it.
489 if (el->el_search.patlen == 0) {
494 if (el->el_search.patbuf[0] != '.' &&
495 el->el_search.patbuf[0] != '*') {
496 (void) Strncpy(tmpbuf, el->el_search.patbuf,
497 sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
498 el->el_search.patbuf[0] = '.';
499 el->el_search.patbuf[1] = '*';
500 (void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
502 el->el_search.patlen++;
503 el->el_search.patbuf[el->el_search.patlen++] = '.';
504 el->el_search.patbuf[el->el_search.patlen++] = '*';
505 el->el_search.patbuf[el->el_search.patlen] = '\0';
510 tmpbuf[tmplen++] = '.';
511 tmpbuf[tmplen++] = '*';
513 tmpbuf[tmplen] = '\0';
514 (void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
515 el->el_search.patlen = (size_t)tmplen;
517 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
518 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
519 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
520 ed_search_next_history(el, 0)) == CC_ERROR) {
526 return ed_newline(el, 0);
533 * Look for a pattern inside a line
535 protected el_action_t
536 ce_search_line(EditLine *el, int dir)
538 Char *cp = el->el_line.cursor;
539 Char *pattern = el->el_search.patbuf;
550 if (dir == ED_SEARCH_PREV_HISTORY) {
551 for (; cp >= el->el_line.buffer; cp--) {
552 if (el_match(cp, ocp)) {
554 el->el_line.cursor = cp;
561 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
562 if (el_match(cp, ocp)) {
564 el->el_line.cursor = cp;
577 protected el_action_t
578 cv_repeat_srch(EditLine *el, wint_t c)
582 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
583 c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
586 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
587 el->el_line.lastchar = el->el_line.buffer;
590 case ED_SEARCH_NEXT_HISTORY:
591 return ed_search_next_history(el, 0);
592 case ED_SEARCH_PREV_HISTORY:
593 return ed_search_prev_history(el, 0);
601 * Vi character search
603 protected el_action_t
604 cv_csearch(EditLine *el, int direction, wint_t ch, int count, int tflag)
611 if (ch == (wint_t)-1) {
612 if (el_wgetc(el, &ch) != 1)
613 return ed_end_of_file(el, 0);
616 /* Save for ';' and ',' commands */
617 el->el_search.chacha = (Char)ch;
618 el->el_search.chadir = direction;
619 el->el_search.chatflg = (char)tflag;
621 cp = el->el_line.cursor;
623 if ((wint_t)*cp == ch)
625 for (;;cp += direction) {
626 if (cp >= el->el_line.lastchar)
628 if (cp < el->el_line.buffer)
630 if ((wint_t)*cp == ch)
638 el->el_line.cursor = cp;
640 if (el->el_chared.c_vcmd.action != NOP) {
642 el->el_line.cursor++;