2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
39 #endif /* not lint && not SCCSID */
42 * search.c: History and character search functions
54 * Adjust cursor in vi mode to include the character under it
56 #define EL_CURSOR(el) \
57 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
58 ((el)->el_map.current == (el)->el_map.alt)))
61 * Initialize the search stuff
67 el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
68 el->el_search.patlen = 0;
69 el->el_search.patdir = -1;
70 el->el_search.chacha = '\0';
71 el->el_search.chadir = -1;
77 * Initialize the search stuff
83 el_free((ptr_t) el->el_search.patbuf);
84 el->el_search.patbuf = NULL;
89 * Handle regular expression errors
100 * Return if string matches pattern
110 #elif defined (REGEXP)
114 extern char *re_comp __P((const char *));
115 extern int re_exec __P((const char *));
118 if (strstr(str, pat) != NULL)
122 if (regcomp(&re, pat, 0) == 0) {
123 rv = regexec(&re, str, 0, NULL, 0) == 0;
129 #elif defined(REGEXP)
130 if ((re = regcomp(pat)) != NULL) {
131 rv = regexec(re, str);
138 if (re_comp(pat) != NULL)
141 return re_exec(str) == 1;
147 * return True if the pattern matches the prefix
155 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
156 el->el_search.patbuf, str);
159 return el_match(str, el->el_search.patbuf);
164 * Set the history seatch pattern
170 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
171 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
172 el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
173 if (el->el_search.patlen >= EL_BUFSIZ)
174 el->el_search.patlen = EL_BUFSIZ -1;
175 if (el->el_search.patlen >= 0) {
176 (void) strncpy(el->el_search.patbuf, el->el_line.buffer,
177 el->el_search.patlen);
178 el->el_search.patbuf[el->el_search.patlen] = '\0';
181 el->el_search.patlen = strlen(el->el_search.patbuf);
184 (void) fprintf(el->el_errfile, "\neventno = %d\n", el->el_history.eventno);
185 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
186 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n", el->el_search.patbuf);
187 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
188 EL_CURSOR(el) - el->el_line.buffer,
189 el->el_line.lastchar - el->el_line.buffer);
195 * Emacs incremental search
197 protected el_action_t
198 ce_inc_search(el, dir)
202 static char STRfwd[] = { 'f', 'w', 'd', '\0' },
203 STRbck[] = { 'b', 'c', 'k', '\0' };
204 static char pchar = ':'; /* ':' = normal, '?' = failed */
205 static char endcmd[2] = { '\0', '\0' };
206 char ch, *cp, *ocursor = el->el_line.cursor, oldpchar = pchar;
208 el_action_t ret = CC_NORM;
210 int ohisteventno = el->el_history.eventno,
211 oldpatlen = el->el_search.patlen,
215 if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
216 el->el_search.patlen >= el->el_line.limit)
221 if (el->el_search.patlen == 0) { /* first round */
224 el->el_search.patbuf[el->el_search.patlen++] = '.';
225 el->el_search.patbuf[el->el_search.patlen++] = '*';
229 *el->el_line.lastchar++ = '\n';
230 for (cp = newdir == ED_SEARCH_PREV_HISTORY ? STRbck : STRfwd;
231 *cp; *el->el_line.lastchar++ = *cp++)
233 *el->el_line.lastchar++ = pchar;
234 for (cp = &el->el_search.patbuf[1];
235 cp < &el->el_search.patbuf[el->el_search.patlen];
236 *el->el_line.lastchar++ = *cp++)
238 *el->el_line.lastchar = '\0';
241 if (el_getc(el, &ch) != 1)
242 return ed_end_of_file(el, 0);
244 switch (el->el_map.current[(unsigned char) ch]) {
247 if (el->el_search.patlen > EL_BUFSIZ - 3)
250 el->el_search.patbuf[el->el_search.patlen++] = ch;
251 *el->el_line.lastchar++ = ch;
252 *el->el_line.lastchar = '\0';
257 case EM_INC_SEARCH_NEXT:
258 newdir = ED_SEARCH_NEXT_HISTORY;
262 case EM_INC_SEARCH_PREV:
263 newdir = ED_SEARCH_PREV_HISTORY;
267 case ED_DELETE_PREV_CHAR:
268 if (el->el_search.patlen > 1)
276 case 0007: /* ^G: Abort */
281 case 0027: /* ^W: Append word */
282 /* No can do if globbing characters in pattern */
283 for (cp = &el->el_search.patbuf[1]; ; cp++)
284 if (cp >= &el->el_search.patbuf[el->el_search.patlen]) {
285 el->el_line.cursor += el->el_search.patlen - 1;
286 cp = c__next_word(el->el_line.cursor,
287 el->el_line.lastchar, 1, ce__isword);
288 while (el->el_line.cursor < cp &&
289 *el->el_line.cursor != '\n') {
290 if (el->el_search.patlen > EL_BUFSIZ - 3) {
294 el->el_search.patbuf[el->el_search.patlen++] =
296 *el->el_line.lastchar++ = *el->el_line.cursor++;
298 el->el_line.cursor = ocursor;
299 *el->el_line.lastchar = '\0';
302 } else if (isglob(*cp)) {
308 default: /* Terminate and execute cmd */
313 case 0033: /* ESC: Terminate */
321 while (el->el_line.lastchar > el->el_line.buffer &&
322 *el->el_line.lastchar != '\n')
323 *el->el_line.lastchar-- = '\0';
324 *el->el_line.lastchar = '\0';
328 /* Can't search if unmatched '[' */
329 for (cp = &el->el_search.patbuf[el->el_search.patlen-1], ch = ']';
330 cp > el->el_search.patbuf; cp--)
331 if (*cp == '[' || *cp == ']') {
336 if (el->el_search.patlen > 1 && ch != '[') {
337 if (redo && newdir == dir) {
338 if (pchar == '?') { /* wrap around */
339 el->el_history.eventno =
340 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
341 if (hist_get(el) == CC_ERROR)
342 /* el->el_history.eventno was fixed by first call */
344 el->el_line.cursor = newdir == ED_SEARCH_PREV_HISTORY ?
345 el->el_line.lastchar : el->el_line.buffer;
347 el->el_line.cursor +=
348 newdir == ED_SEARCH_PREV_HISTORY ? -1 : 1;
351 el->el_search.patbuf[el->el_search.patlen++] = '.';
352 el->el_search.patbuf[el->el_search.patlen++] = '*';
354 el->el_search.patbuf[el->el_search.patlen] = '\0';
355 if (el->el_line.cursor < el->el_line.buffer ||
356 el->el_line.cursor > el->el_line.lastchar ||
357 (ret = ce_search_line(el, &el->el_search.patbuf[1],
358 newdir)) == CC_ERROR) {
360 el->el_state.lastcmd = (el_action_t) newdir;
361 ret = newdir == ED_SEARCH_PREV_HISTORY ?
362 ed_search_prev_history(el, 0) :
363 ed_search_next_history(el, 0);
364 if (ret != CC_ERROR) {
365 el->el_line.cursor = newdir == ED_SEARCH_PREV_HISTORY ?
366 el->el_line.lastchar : el->el_line.buffer;
367 (void) ce_search_line(el, &el->el_search.patbuf[1],
371 el->el_search.patbuf[--el->el_search.patlen] = '\0';
372 if (ret == CC_ERROR) {
374 if (el->el_history.eventno != ohisteventno) {
375 el->el_history.eventno = ohisteventno;
376 if (hist_get(el) == CC_ERROR)
379 el->el_line.cursor = ocursor;
386 ret = ce_inc_search(el, newdir);
388 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
389 /* break abort of failed search at last non-failed */
394 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
395 /* restore on normal return or error exit */
397 el->el_search.patlen = oldpatlen;
398 if (el->el_history.eventno != ohisteventno) {
399 el->el_history.eventno = ohisteventno;
400 if (hist_get(el) == CC_ERROR)
403 el->el_line.cursor = ocursor;
407 if (done || ret != CC_NORM)
416 protected el_action_t
422 char tmpbuf[EL_BUFSIZ];
427 tmpbuf[tmplen++] = '.';
428 tmpbuf[tmplen++] = '*';
431 el->el_line.buffer[0] = '\0';
432 el->el_line.lastchar = el->el_line.buffer;
433 el->el_line.cursor = el->el_line.buffer;
434 el->el_search.patdir = dir;
436 c_insert(el, 2); /* prompt + '\n' */
437 *el->el_line.cursor++ = '\n';
438 *el->el_line.cursor++ = dir == ED_SEARCH_PREV_HISTORY ? '/' : '?';
447 tmplen = c_gets(el, &tmpbuf[LEN]) + LEN;
449 tmpbuf[tmplen] = '\0';
453 * Use the old pattern, but wild-card it.
455 if (el->el_search.patlen == 0) {
456 el->el_line.buffer[0] = '\0';
457 el->el_line.lastchar = el->el_line.buffer;
458 el->el_line.cursor = el->el_line.buffer;
463 if (el->el_search.patbuf[0] != '.' && el->el_search.patbuf[0] != '*') {
464 (void)strncpy(tmpbuf, el->el_search.patbuf, sizeof(tmpbuf) - 1);
465 el->el_search.patbuf[0] = '.';
466 el->el_search.patbuf[1] = '*';
467 (void)strncpy(&el->el_search.patbuf[2], tmpbuf, EL_BUFSIZ - 3);
468 el->el_search.patlen++;
469 el->el_search.patbuf[el->el_search.patlen++] = '.';
470 el->el_search.patbuf[el->el_search.patlen++] = '*';
471 el->el_search.patbuf[el->el_search.patlen] = '\0';
477 tmpbuf[tmplen++] = '.';
478 tmpbuf[tmplen++] = '*';
480 tmpbuf[tmplen] = '\0';
481 (void)strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
482 el->el_search.patlen = tmplen;
484 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
485 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
486 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
487 ed_search_next_history(el, 0)) == CC_ERROR) {
494 *el->el_line.lastchar++ = '\n';
495 *el->el_line.lastchar = '\0';
506 * Look for a pattern inside a line
508 protected el_action_t
509 ce_search_line(el, pattern, dir)
516 if (dir == ED_SEARCH_PREV_HISTORY) {
517 for (cp = el->el_line.cursor; cp >= el->el_line.buffer; cp--)
518 if (el_match(cp, pattern)) {
519 el->el_line.cursor = cp;
524 for (cp = el->el_line.cursor; *cp != '\0' &&
525 cp < el->el_line.limit; cp++)
526 if (el_match(cp, pattern)) {
527 el->el_line.cursor = cp;
538 protected el_action_t
539 cv_repeat_srch(el, c)
544 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
545 c, el->el_search.patlen, el->el_search.patbuf);
548 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
549 el->el_line.lastchar = el->el_line.buffer;
552 case ED_SEARCH_NEXT_HISTORY:
553 return ed_search_next_history(el, 0);
554 case ED_SEARCH_PREV_HISTORY:
555 return ed_search_prev_history(el, 0);
562 /* cv_csearch_back():
563 * Vi character search reverse
565 protected el_action_t
566 cv_csearch_back(el, ch, count, tflag)
568 int ch, count, tflag;
572 cp = el->el_line.cursor;
576 while (cp > el->el_line.buffer && *cp != ch)
580 if (cp < el->el_line.buffer || (cp == el->el_line.buffer && *cp != ch))
583 if (*cp == ch && tflag)
586 el->el_line.cursor = cp;
588 if (el->el_chared.c_vcmd.action & DELETE) {
589 el->el_line.cursor++;
594 re_refresh_cursor(el);
600 * Vi character search forward
602 protected el_action_t
603 cv_csearch_fwd(el, ch, count, tflag)
605 int ch, count, tflag;
609 cp = el->el_line.cursor;
613 while (cp < el->el_line.lastchar && *cp != ch)
617 if (cp >= el->el_line.lastchar)
620 if (*cp == ch && tflag)
623 el->el_line.cursor = cp;
625 if (el->el_chared.c_vcmd.action & DELETE) {
626 el->el_line.cursor++;
630 re_refresh_cursor(el);