1 /* $NetBSD: search.c,v 1.48 2018/02/26 17:36:14 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.48 2018/02/26 17:36:14 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * search.c: History and character search functions
48 #include <sys/types.h>
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.patbuf[0] = L'\0';
79 el->el_search.patlen = 0;
80 el->el_search.patdir = -1;
81 el->el_search.chacha = L'\0';
82 el->el_search.chadir = CHAR_FWD;
83 el->el_search.chatflg = 0;
89 * Initialize the search stuff
92 search_end(EditLine *el)
95 el_free(el->el_search.patbuf);
96 el->el_search.patbuf = NULL;
102 * Handle regular expression errors
106 regerror(const char *msg)
113 * Return if string matches pattern
116 el_match(const wchar_t *str, const wchar_t *pat)
118 static ct_buffer_t conv;
122 #elif defined (REGEXP)
126 extern char *re_comp(const char *);
127 extern int re_exec(const char *);
130 if (wcsstr(str, pat) != 0)
134 if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
135 rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
142 #elif defined(REGEXP)
143 if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
144 rv = regexec(re, ct_encode_string(str, &conv));
151 if (re_comp(ct_encode_string(pat, &conv)) != NULL)
154 return re_exec(ct_encode_string(str, &conv)) == 1;
160 * return True if the pattern matches the prefix
163 c_hmatch(EditLine *el, const wchar_t *str)
166 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
167 el->el_search.patbuf, str);
170 return el_match(str, el->el_search.patbuf);
175 * Set the history seatch pattern
178 c_setpat(EditLine *el)
180 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
181 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
182 el->el_search.patlen =
183 (size_t)(EL_CURSOR(el) - el->el_line.buffer);
184 if (el->el_search.patlen >= EL_BUFSIZ)
185 el->el_search.patlen = EL_BUFSIZ - 1;
186 if (el->el_search.patlen != 0) {
187 (void) wcsncpy(el->el_search.patbuf, el->el_line.buffer,
188 el->el_search.patlen);
189 el->el_search.patbuf[el->el_search.patlen] = '\0';
191 el->el_search.patlen = wcslen(el->el_search.patbuf);
194 (void) fprintf(el->el_errfile, "\neventno = %d\n",
195 el->el_history.eventno);
196 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
197 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
198 el->el_search.patbuf);
199 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
200 EL_CURSOR(el) - el->el_line.buffer,
201 el->el_line.lastchar - el->el_line.buffer);
207 * Emacs incremental search
209 libedit_private el_action_t
210 ce_inc_search(EditLine *el, int dir)
212 static const wchar_t STRfwd[] = L"fwd", STRbck[] = L"bck";
213 static wchar_t pchar = L':'; /* ':' = normal, '?' = failed */
214 static wchar_t endcmd[2] = {'\0', '\0'};
215 wchar_t *ocursor = el->el_line.cursor, oldpchar = pchar, ch;
218 el_action_t ret = CC_NORM;
220 int ohisteventno = el->el_history.eventno;
221 size_t oldpatlen = el->el_search.patlen;
225 if (el->el_line.lastchar + sizeof(STRfwd) /
226 sizeof(*el->el_line.lastchar) + 2 +
227 el->el_search.patlen >= el->el_line.limit)
232 if (el->el_search.patlen == 0) { /* first round */
236 el->el_search.patbuf[el->el_search.patlen++] = '.';
237 el->el_search.patbuf[el->el_search.patlen++] = '*';
243 *el->el_line.lastchar++ = '\n';
244 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
245 *cp; *el->el_line.lastchar++ = *cp++)
247 *el->el_line.lastchar++ = pchar;
248 for (cp = &el->el_search.patbuf[LEN];
249 cp < &el->el_search.patbuf[el->el_search.patlen];
250 *el->el_line.lastchar++ = *cp++)
252 *el->el_line.lastchar = '\0';
255 if (el_wgetc(el, &ch) != 1)
256 return ed_end_of_file(el, 0);
258 switch (el->el_map.current[(unsigned char) ch]) {
261 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
264 el->el_search.patbuf[el->el_search.patlen++] =
266 *el->el_line.lastchar++ = ch;
267 *el->el_line.lastchar = '\0';
272 case EM_INC_SEARCH_NEXT:
273 newdir = ED_SEARCH_NEXT_HISTORY;
277 case EM_INC_SEARCH_PREV:
278 newdir = ED_SEARCH_PREV_HISTORY;
282 case EM_DELETE_PREV_CHAR:
283 case ED_DELETE_PREV_CHAR:
284 if (el->el_search.patlen > LEN)
292 case 0007: /* ^G: Abort */
297 case 0027: /* ^W: Append word */
298 /* No can do if globbing characters in pattern */
299 for (cp = &el->el_search.patbuf[LEN];; cp++)
300 if (cp >= &el->el_search.patbuf[
301 el->el_search.patlen]) {
302 el->el_line.cursor +=
303 el->el_search.patlen - LEN - 1;
304 cp = c__next_word(el->el_line.cursor,
305 el->el_line.lastchar, 1,
307 while (el->el_line.cursor < cp &&
308 *el->el_line.cursor != '\n') {
309 if (el->el_search.patlen >=
314 el->el_search.patbuf[el->el_search.patlen++] =
316 *el->el_line.lastchar++ =
317 *el->el_line.cursor++;
319 el->el_line.cursor = ocursor;
320 *el->el_line.lastchar = '\0';
323 } else if (isglob(*cp)) {
329 default: /* Terminate and execute cmd */
331 el_wpush(el, endcmd);
334 case 0033: /* ESC: Terminate */
342 while (el->el_line.lastchar > el->el_line.buffer &&
343 *el->el_line.lastchar != '\n')
344 *el->el_line.lastchar-- = '\0';
345 *el->el_line.lastchar = '\0';
349 /* Can't search if unmatched '[' */
350 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
352 cp >= &el->el_search.patbuf[LEN];
354 if (*cp == '[' || *cp == ']') {
358 if (el->el_search.patlen > LEN && ch != L'[') {
359 if (redo && newdir == dir) {
360 if (pchar == '?') { /* wrap around */
361 el->el_history.eventno =
362 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
363 if (hist_get(el) == CC_ERROR)
364 /* el->el_history.event
368 el->el_line.cursor = newdir ==
369 ED_SEARCH_PREV_HISTORY ?
370 el->el_line.lastchar :
373 el->el_line.cursor +=
375 ED_SEARCH_PREV_HISTORY ?
379 el->el_search.patbuf[el->el_search.patlen++] =
381 el->el_search.patbuf[el->el_search.patlen++] =
384 el->el_search.patbuf[el->el_search.patlen] =
386 if (el->el_line.cursor < el->el_line.buffer ||
387 el->el_line.cursor > el->el_line.lastchar ||
388 (ret = ce_search_line(el, newdir))
391 el->el_state.lastcmd =
392 (el_action_t) newdir;
394 (newdir == ED_SEARCH_PREV_HISTORY ?
395 ed_search_prev_history(el, 0) :
396 ed_search_next_history(el, 0));
397 if (ret != CC_ERROR) {
398 el->el_line.cursor = newdir ==
399 ED_SEARCH_PREV_HISTORY ?
400 el->el_line.lastchar :
402 (void) ce_search_line(el,
406 el->el_search.patlen -= LEN;
407 el->el_search.patbuf[el->el_search.patlen] =
409 if (ret == CC_ERROR) {
411 if (el->el_history.eventno !=
413 el->el_history.eventno =
415 if (hist_get(el) == CC_ERROR)
418 el->el_line.cursor = ocursor;
424 ret = ce_inc_search(el, newdir);
426 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
428 * break abort of failed search at last
434 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
435 /* restore on normal return or error exit */
437 el->el_search.patlen = oldpatlen;
438 if (el->el_history.eventno != ohisteventno) {
439 el->el_history.eventno = ohisteventno;
440 if (hist_get(el) == CC_ERROR)
443 el->el_line.cursor = ocursor;
447 if (done || ret != CC_NORM)
456 libedit_private el_action_t
457 cv_search(EditLine *el, int dir)
460 wchar_t tmpbuf[EL_BUFSIZ];
469 el->el_search.patdir = dir;
471 tmplen = c_gets(el, &tmpbuf[LEN],
472 dir == ED_SEARCH_PREV_HISTORY ? L"\n/" : L"\n?" );
478 tmpbuf[tmplen] = '\0';
482 * Use the old pattern, but wild-card it.
484 if (el->el_search.patlen == 0) {
489 if (el->el_search.patbuf[0] != '.' &&
490 el->el_search.patbuf[0] != '*') {
491 (void) wcsncpy(tmpbuf, el->el_search.patbuf,
492 sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
493 el->el_search.patbuf[0] = '.';
494 el->el_search.patbuf[1] = '*';
495 (void) wcsncpy(&el->el_search.patbuf[2], tmpbuf,
497 el->el_search.patlen++;
498 el->el_search.patbuf[el->el_search.patlen++] = '.';
499 el->el_search.patbuf[el->el_search.patlen++] = '*';
500 el->el_search.patbuf[el->el_search.patlen] = '\0';
505 tmpbuf[tmplen++] = '.';
506 tmpbuf[tmplen++] = '*';
508 tmpbuf[tmplen] = '\0';
509 (void) wcsncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
510 el->el_search.patlen = (size_t)tmplen;
512 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
513 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
514 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
515 ed_search_next_history(el, 0)) == CC_ERROR) {
521 return ed_newline(el, 0);
528 * Look for a pattern inside a line
530 libedit_private el_action_t
531 ce_search_line(EditLine *el, int dir)
533 wchar_t *cp = el->el_line.cursor;
534 wchar_t *pattern = el->el_search.patbuf;
545 if (dir == ED_SEARCH_PREV_HISTORY) {
546 for (; cp >= el->el_line.buffer; cp--) {
547 if (el_match(cp, ocp)) {
549 el->el_line.cursor = cp;
556 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
557 if (el_match(cp, ocp)) {
559 el->el_line.cursor = cp;
572 libedit_private el_action_t
573 cv_repeat_srch(EditLine *el, wint_t c)
577 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
578 c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
581 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
582 el->el_line.lastchar = el->el_line.buffer;
585 case ED_SEARCH_NEXT_HISTORY:
586 return ed_search_next_history(el, 0);
587 case ED_SEARCH_PREV_HISTORY:
588 return ed_search_prev_history(el, 0);
596 * Vi character search
598 libedit_private el_action_t
599 cv_csearch(EditLine *el, int direction, wint_t ch, int count, int tflag)
606 if (ch == (wint_t)-1) {
608 if (el_wgetc(el, &c) != 1)
609 return ed_end_of_file(el, 0);
613 /* Save for ';' and ',' commands */
614 el->el_search.chacha = ch;
615 el->el_search.chadir = direction;
616 el->el_search.chatflg = (char)tflag;
618 cp = el->el_line.cursor;
620 if ((wint_t)*cp == ch)
622 for (;;cp += direction) {
623 if (cp >= el->el_line.lastchar)
625 if (cp < el->el_line.buffer)
627 if ((wint_t)*cp == ch)
635 el->el_line.cursor = cp;
637 if (el->el_chared.c_vcmd.action != NOP) {
639 el->el_line.cursor++;