1 /* $NetBSD: search.c,v 1.31 2016/01/30 04:02:51 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.31 2016/01/30 04:02:51 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * search.c: History and character search functions
58 * Adjust cursor in vi mode to include the character under it
60 #define EL_CURSOR(el) \
61 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
62 ((el)->el_map.current == (el)->el_map.alt)))
65 * Initialize the search stuff
68 search_init(EditLine *el)
71 el->el_search.patbuf = el_malloc(EL_BUFSIZ *
72 sizeof(*el->el_search.patbuf));
73 if (el->el_search.patbuf == NULL)
75 el->el_search.patlen = 0;
76 el->el_search.patdir = -1;
77 el->el_search.chacha = '\0';
78 el->el_search.chadir = CHAR_FWD;
79 el->el_search.chatflg = 0;
85 * Initialize the search stuff
88 search_end(EditLine *el)
91 el_free(el->el_search.patbuf);
92 el->el_search.patbuf = NULL;
98 * Handle regular expression errors
102 regerror(const char *msg)
109 * Return if string matches pattern
112 el_match(const Char *str, const Char *pat)
115 static ct_buffer_t conv;
120 #elif defined (REGEXP)
124 extern char *re_comp(const char *);
125 extern int re_exec(const char *);
128 if (Strstr(str, pat) != 0)
132 if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
133 rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
140 #elif defined(REGEXP)
141 if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
142 rv = regexec(re, ct_encode_string(str, &conv));
149 if (re_comp(ct_encode_string(pat, &conv)) != NULL)
152 return re_exec(ct_encode_string(str, &conv)) == 1;
158 * return True if the pattern matches the prefix
161 c_hmatch(EditLine *el, const Char *str)
164 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
165 el->el_search.patbuf, str);
168 return el_match(str, el->el_search.patbuf);
173 * Set the history seatch pattern
176 c_setpat(EditLine *el)
178 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
179 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
180 el->el_search.patlen =
181 (size_t)(EL_CURSOR(el) - el->el_line.buffer);
182 if (el->el_search.patlen >= EL_BUFSIZ)
183 el->el_search.patlen = EL_BUFSIZ - 1;
184 if (el->el_search.patlen != 0) {
185 (void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
186 el->el_search.patlen);
187 el->el_search.patbuf[el->el_search.patlen] = '\0';
189 el->el_search.patlen = Strlen(el->el_search.patbuf);
192 (void) fprintf(el->el_errfile, "\neventno = %d\n",
193 el->el_history.eventno);
194 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
195 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
196 el->el_search.patbuf);
197 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
198 EL_CURSOR(el) - el->el_line.buffer,
199 el->el_line.lastchar - el->el_line.buffer);
205 * Emacs incremental search
207 protected el_action_t
208 ce_inc_search(EditLine *el, int dir)
210 static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
211 STRbck[] = {'b', 'c', 'k', '\0'};
212 static Char pchar = ':';/* ':' = normal, '?' = failed */
213 static Char endcmd[2] = {'\0', '\0'};
214 Char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
217 el_action_t ret = CC_NORM;
219 int ohisteventno = el->el_history.eventno;
220 size_t oldpatlen = el->el_search.patlen;
224 if (el->el_line.lastchar + sizeof(STRfwd) /
225 sizeof(*el->el_line.lastchar) + 2 +
226 el->el_search.patlen >= el->el_line.limit)
231 if (el->el_search.patlen == 0) { /* first round */
235 el->el_search.patbuf[el->el_search.patlen++] = '.';
236 el->el_search.patbuf[el->el_search.patlen++] = '*';
242 *el->el_line.lastchar++ = '\n';
243 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
244 *cp; *el->el_line.lastchar++ = *cp++)
246 *el->el_line.lastchar++ = pchar;
247 for (cp = &el->el_search.patbuf[LEN];
248 cp < &el->el_search.patbuf[el->el_search.patlen];
249 *el->el_line.lastchar++ = *cp++)
251 *el->el_line.lastchar = '\0';
254 if (FUN(el,getc)(el, &ch) != 1)
255 return ed_end_of_file(el, 0);
257 switch (el->el_map.current[(unsigned char) ch]) {
260 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
263 el->el_search.patbuf[el->el_search.patlen++] =
265 *el->el_line.lastchar++ = ch;
266 *el->el_line.lastchar = '\0';
271 case EM_INC_SEARCH_NEXT:
272 newdir = ED_SEARCH_NEXT_HISTORY;
276 case EM_INC_SEARCH_PREV:
277 newdir = ED_SEARCH_PREV_HISTORY;
281 case EM_DELETE_PREV_CHAR:
282 case ED_DELETE_PREV_CHAR:
283 if (el->el_search.patlen > LEN)
291 case 0007: /* ^G: Abort */
296 case 0027: /* ^W: Append word */
297 /* No can do if globbing characters in pattern */
298 for (cp = &el->el_search.patbuf[LEN];; cp++)
299 if (cp >= &el->el_search.patbuf[
300 el->el_search.patlen]) {
301 el->el_line.cursor +=
302 el->el_search.patlen - LEN - 1;
303 cp = c__next_word(el->el_line.cursor,
304 el->el_line.lastchar, 1,
306 while (el->el_line.cursor < cp &&
307 *el->el_line.cursor != '\n') {
308 if (el->el_search.patlen >=
313 el->el_search.patbuf[el->el_search.patlen++] =
315 *el->el_line.lastchar++ =
316 *el->el_line.cursor++;
318 el->el_line.cursor = ocursor;
319 *el->el_line.lastchar = '\0';
322 } else if (isglob(*cp)) {
328 default: /* Terminate and execute cmd */
330 FUN(el,push)(el, endcmd);
333 case 0033: /* ESC: Terminate */
341 while (el->el_line.lastchar > el->el_line.buffer &&
342 *el->el_line.lastchar != '\n')
343 *el->el_line.lastchar-- = '\0';
344 *el->el_line.lastchar = '\0';
348 /* Can't search if unmatched '[' */
349 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
351 cp >= &el->el_search.patbuf[LEN];
353 if (*cp == '[' || *cp == ']') {
357 if (el->el_search.patlen > LEN && ch != '[') {
358 if (redo && newdir == dir) {
359 if (pchar == '?') { /* wrap around */
360 el->el_history.eventno =
361 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
362 if (hist_get(el) == CC_ERROR)
363 /* el->el_history.event
367 el->el_line.cursor = newdir ==
368 ED_SEARCH_PREV_HISTORY ?
369 el->el_line.lastchar :
372 el->el_line.cursor +=
374 ED_SEARCH_PREV_HISTORY ?
378 el->el_search.patbuf[el->el_search.patlen++] =
380 el->el_search.patbuf[el->el_search.patlen++] =
383 el->el_search.patbuf[el->el_search.patlen] =
385 if (el->el_line.cursor < el->el_line.buffer ||
386 el->el_line.cursor > el->el_line.lastchar ||
387 (ret = ce_search_line(el, newdir))
390 el->el_state.lastcmd =
391 (el_action_t) newdir;
393 (newdir == ED_SEARCH_PREV_HISTORY ?
394 ed_search_prev_history(el, 0) :
395 ed_search_next_history(el, 0));
396 if (ret != CC_ERROR) {
397 el->el_line.cursor = newdir ==
398 ED_SEARCH_PREV_HISTORY ?
399 el->el_line.lastchar :
401 (void) ce_search_line(el,
405 el->el_search.patlen -= LEN;
406 el->el_search.patbuf[el->el_search.patlen] =
408 if (ret == CC_ERROR) {
410 if (el->el_history.eventno !=
412 el->el_history.eventno =
414 if (hist_get(el) == CC_ERROR)
417 el->el_line.cursor = ocursor;
423 ret = ce_inc_search(el, newdir);
425 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
427 * break abort of failed search at last
433 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
434 /* restore on normal return or error exit */
436 el->el_search.patlen = oldpatlen;
437 if (el->el_history.eventno != ohisteventno) {
438 el->el_history.eventno = ohisteventno;
439 if (hist_get(el) == CC_ERROR)
442 el->el_line.cursor = ocursor;
446 if (done || ret != CC_NORM)
455 protected el_action_t
456 cv_search(EditLine *el, int dir)
459 Char tmpbuf[EL_BUFSIZ];
468 el->el_search.patdir = dir;
470 tmplen = c_gets(el, &tmpbuf[LEN],
471 dir == ED_SEARCH_PREV_HISTORY ? STR("\n/") : STR("\n?") );
477 tmpbuf[tmplen] = '\0';
481 * Use the old pattern, but wild-card it.
483 if (el->el_search.patlen == 0) {
488 if (el->el_search.patbuf[0] != '.' &&
489 el->el_search.patbuf[0] != '*') {
490 (void) Strncpy(tmpbuf, el->el_search.patbuf,
491 sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
492 el->el_search.patbuf[0] = '.';
493 el->el_search.patbuf[1] = '*';
494 (void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
496 el->el_search.patlen++;
497 el->el_search.patbuf[el->el_search.patlen++] = '.';
498 el->el_search.patbuf[el->el_search.patlen++] = '*';
499 el->el_search.patbuf[el->el_search.patlen] = '\0';
504 tmpbuf[tmplen++] = '.';
505 tmpbuf[tmplen++] = '*';
507 tmpbuf[tmplen] = '\0';
508 (void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
509 el->el_search.patlen = (size_t)tmplen;
511 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
512 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
513 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
514 ed_search_next_history(el, 0)) == CC_ERROR) {
520 return ed_newline(el, 0);
527 * Look for a pattern inside a line
529 protected el_action_t
530 ce_search_line(EditLine *el, int dir)
532 Char *cp = el->el_line.cursor;
533 Char *pattern = el->el_search.patbuf;
544 if (dir == ED_SEARCH_PREV_HISTORY) {
545 for (; cp >= el->el_line.buffer; cp--) {
546 if (el_match(cp, ocp)) {
548 el->el_line.cursor = cp;
555 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
556 if (el_match(cp, ocp)) {
558 el->el_line.cursor = cp;
571 protected el_action_t
572 cv_repeat_srch(EditLine *el, Int c)
576 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
577 c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
580 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
581 el->el_line.lastchar = el->el_line.buffer;
584 case ED_SEARCH_NEXT_HISTORY:
585 return ed_search_next_history(el, 0);
586 case ED_SEARCH_PREV_HISTORY:
587 return ed_search_prev_history(el, 0);
595 * Vi character search
597 protected el_action_t
598 cv_csearch(EditLine *el, int direction, Int ch, int count, int tflag)
607 if (FUN(el,getc)(el, &c) != 1)
608 return ed_end_of_file(el, 0);
612 /* Save for ';' and ',' commands */
613 el->el_search.chacha = ch;
614 el->el_search.chadir = direction;
615 el->el_search.chatflg = (char)tflag;
617 cp = el->el_line.cursor;
621 for (;;cp += direction) {
622 if (cp >= el->el_line.lastchar)
624 if (cp < el->el_line.buffer)
634 el->el_line.cursor = cp;
636 if (el->el_chared.c_vcmd.action != NOP) {
638 el->el_line.cursor++;