search.c revision 1.11 1 /* $NetBSD: search.c,v 1.11 2001/01/23 15:55:31 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: search.c,v 1.11 2001/01/23 15:55:31 jdolecek Exp $");
45 #endif
46 #endif /* not lint && not SCCSID */
47
48 /*
49 * search.c: History and character search functions
50 */
51 #include "sys.h"
52 #include <stdlib.h>
53 #if defined(REGEX)
54 #include <regex.h>
55 #elif defined(REGEXP)
56 #include <regexp.h>
57 #endif
58 #include "el.h"
59
60 /*
61 * Adjust cursor in vi mode to include the character under it
62 */
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)))
66
67 /* search_init():
68 * Initialize the search stuff
69 */
70 protected int
71 search_init(EditLine *el)
72 {
73
74 el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
75 if (el->el_search.patbuf == NULL)
76 return (-1);
77 el->el_search.patlen = 0;
78 el->el_search.patdir = -1;
79 el->el_search.chacha = '\0';
80 el->el_search.chadir = -1;
81 return (0);
82 }
83
84
85 /* search_end():
86 * Initialize the search stuff
87 */
88 protected void
89 search_end(EditLine *el)
90 {
91
92 el_free((ptr_t) el->el_search.patbuf);
93 el->el_search.patbuf = NULL;
94 }
95
96
97 #ifdef REGEXP
98 /* regerror():
99 * Handle regular expression errors
100 */
101 public void
102 /*ARGSUSED*/
103 regerror(const char *msg)
104 {
105 }
106 #endif
107
108
109 /* el_match():
110 * Return if string matches pattern
111 */
112 protected int
113 el_match(const char *str, const char *pat)
114 {
115 #if defined (REGEX)
116 regex_t re;
117 int rv;
118 #elif defined (REGEXP)
119 regexp *rp;
120 int rv;
121 #else
122 extern char *re_comp(const char *);
123 extern int re_exec(const char *);
124 #endif
125
126 if (strstr(str, pat) != NULL)
127 return (1);
128
129 #if defined(REGEX)
130 if (regcomp(&re, pat, 0) == 0) {
131 rv = regexec(&re, str, 0, NULL, 0) == 0;
132 regfree(&re);
133 } else {
134 rv = 0;
135 }
136 return (rv);
137 #elif defined(REGEXP)
138 if ((re = regcomp(pat)) != NULL) {
139 rv = regexec(re, str);
140 free((ptr_t) re);
141 } else {
142 rv = 0;
143 }
144 return (rv);
145 #else
146 if (re_comp(pat) != NULL)
147 return (0);
148 else
149 return (re_exec(str) == 1);
150 #endif
151 }
152
153
154 /* c_hmatch():
155 * return True if the pattern matches the prefix
156 */
157 protected int
158 c_hmatch(EditLine *el, const char *str)
159 {
160 #ifdef SDEBUG
161 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
162 el->el_search.patbuf, str);
163 #endif /* SDEBUG */
164
165 return (el_match(str, el->el_search.patbuf));
166 }
167
168
169 /* c_setpat():
170 * Set the history seatch pattern
171 */
172 protected void
173 c_setpat(EditLine *el)
174 {
175 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
176 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
177 el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
178 if (el->el_search.patlen >= EL_BUFSIZ)
179 el->el_search.patlen = EL_BUFSIZ - 1;
180 if (el->el_search.patlen != 0) {
181 (void) strncpy(el->el_search.patbuf, el->el_line.buffer,
182 el->el_search.patlen);
183 el->el_search.patbuf[el->el_search.patlen] = '\0';
184 } else
185 el->el_search.patlen = strlen(el->el_search.patbuf);
186 }
187 #ifdef SDEBUG
188 (void) fprintf(el->el_errfile, "\neventno = %d\n",
189 el->el_history.eventno);
190 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
191 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
192 el->el_search.patbuf);
193 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
194 EL_CURSOR(el) - el->el_line.buffer,
195 el->el_line.lastchar - el->el_line.buffer);
196 #endif
197 }
198
199
200 /* ce_inc_search():
201 * Emacs incremental search
202 */
203 protected el_action_t
204 ce_inc_search(EditLine *el, int dir)
205 {
206 static const char STRfwd[] = {'f', 'w', 'd', '\0'},
207 STRbck[] = {'b', 'c', 'k', '\0'};
208 static char pchar = ':';/* ':' = normal, '?' = failed */
209 static char endcmd[2] = {'\0', '\0'};
210 char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
211 const char *cp;
212
213 el_action_t ret = CC_NORM;
214
215 int ohisteventno = el->el_history.eventno;
216 int oldpatlen = el->el_search.patlen;
217 int newdir = dir;
218 int done, redo;
219
220 if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
221 el->el_search.patlen >= el->el_line.limit)
222 return (CC_ERROR);
223
224 for (;;) {
225
226 if (el->el_search.patlen == 0) { /* first round */
227 pchar = ':';
228 #ifdef ANCHOR
229 el->el_search.patbuf[el->el_search.patlen++] = '.';
230 el->el_search.patbuf[el->el_search.patlen++] = '*';
231 #endif
232 }
233 done = redo = 0;
234 *el->el_line.lastchar++ = '\n';
235 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
236 *cp; *el->el_line.lastchar++ = *cp++)
237 continue;
238 *el->el_line.lastchar++ = pchar;
239 for (cp = &el->el_search.patbuf[1];
240 cp < &el->el_search.patbuf[el->el_search.patlen];
241 *el->el_line.lastchar++ = *cp++)
242 continue;
243 *el->el_line.lastchar = '\0';
244 re_refresh(el);
245
246 if (el_getc(el, &ch) != 1)
247 return (ed_end_of_file(el, 0));
248
249 switch (el->el_map.current[(unsigned char) ch]) {
250 case ED_INSERT:
251 case ED_DIGIT:
252 if (el->el_search.patlen > EL_BUFSIZ - 3)
253 term_beep(el);
254 else {
255 el->el_search.patbuf[el->el_search.patlen++] =
256 ch;
257 *el->el_line.lastchar++ = ch;
258 *el->el_line.lastchar = '\0';
259 re_refresh(el);
260 }
261 break;
262
263 case EM_INC_SEARCH_NEXT:
264 newdir = ED_SEARCH_NEXT_HISTORY;
265 redo++;
266 break;
267
268 case EM_INC_SEARCH_PREV:
269 newdir = ED_SEARCH_PREV_HISTORY;
270 redo++;
271 break;
272
273 case ED_DELETE_PREV_CHAR:
274 if (el->el_search.patlen > 1)
275 done++;
276 else
277 term_beep(el);
278 break;
279
280 default:
281 switch (ch) {
282 case 0007: /* ^G: Abort */
283 ret = CC_ERROR;
284 done++;
285 break;
286
287 case 0027: /* ^W: Append word */
288 /* No can do if globbing characters in pattern */
289 for (cp = &el->el_search.patbuf[1];; cp++)
290 if (cp >= &el->el_search.patbuf[el->el_search.patlen]) {
291 el->el_line.cursor +=
292 el->el_search.patlen - 1;
293 cp = c__next_word(el->el_line.cursor,
294 el->el_line.lastchar, 1,
295 ce__isword);
296 while (el->el_line.cursor < cp &&
297 *el->el_line.cursor != '\n') {
298 if (el->el_search.patlen >
299 EL_BUFSIZ - 3) {
300 term_beep(el);
301 break;
302 }
303 el->el_search.patbuf[el->el_search.patlen++] =
304 *el->el_line.cursor;
305 *el->el_line.lastchar++ =
306 *el->el_line.cursor++;
307 }
308 el->el_line.cursor = ocursor;
309 *el->el_line.lastchar = '\0';
310 re_refresh(el);
311 break;
312 } else if (isglob(*cp)) {
313 term_beep(el);
314 break;
315 }
316 break;
317
318 default: /* Terminate and execute cmd */
319 endcmd[0] = ch;
320 el_push(el, endcmd);
321 /* FALLTHROUGH */
322
323 case 0033: /* ESC: Terminate */
324 ret = CC_REFRESH;
325 done++;
326 break;
327 }
328 break;
329 }
330
331 while (el->el_line.lastchar > el->el_line.buffer &&
332 *el->el_line.lastchar != '\n')
333 *el->el_line.lastchar-- = '\0';
334 *el->el_line.lastchar = '\0';
335
336 if (!done) {
337
338 /* Can't search if unmatched '[' */
339 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
340 ch = ']';
341 cp > el->el_search.patbuf;
342 cp--)
343 if (*cp == '[' || *cp == ']') {
344 ch = *cp;
345 break;
346 }
347 if (el->el_search.patlen > 1 && ch != '[') {
348 if (redo && newdir == dir) {
349 if (pchar == '?') { /* wrap around */
350 el->el_history.eventno =
351 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
352 if (hist_get(el) == CC_ERROR)
353 /* el->el_history.event
354 * no was fixed by
355 * first call */
356 (void) hist_get(el);
357 el->el_line.cursor = newdir ==
358 ED_SEARCH_PREV_HISTORY ?
359 el->el_line.lastchar :
360 el->el_line.buffer;
361 } else
362 el->el_line.cursor +=
363 newdir ==
364 ED_SEARCH_PREV_HISTORY ?
365 -1 : 1;
366 }
367 #ifdef ANCHOR
368 el->el_search.patbuf[el->el_search.patlen++] =
369 '.';
370 el->el_search.patbuf[el->el_search.patlen++] =
371 '*';
372 #endif
373 el->el_search.patbuf[el->el_search.patlen] =
374 '\0';
375 if (el->el_line.cursor < el->el_line.buffer ||
376 el->el_line.cursor > el->el_line.lastchar ||
377 (ret = ce_search_line(el,
378 &el->el_search.patbuf[1],
379 newdir)) == CC_ERROR) {
380 /* avoid c_setpat */
381 el->el_state.lastcmd =
382 (el_action_t) newdir;
383 ret = newdir == ED_SEARCH_PREV_HISTORY ?
384 ed_search_prev_history(el, 0) :
385 ed_search_next_history(el, 0);
386 if (ret != CC_ERROR) {
387 el->el_line.cursor = newdir ==
388 ED_SEARCH_PREV_HISTORY ?
389 el->el_line.lastchar :
390 el->el_line.buffer;
391 (void) ce_search_line(el,
392 &el->el_search.patbuf[1],
393 newdir);
394 }
395 }
396 el->el_search.patbuf[--el->el_search.patlen] =
397 '\0';
398 if (ret == CC_ERROR) {
399 term_beep(el);
400 if (el->el_history.eventno !=
401 ohisteventno) {
402 el->el_history.eventno =
403 ohisteventno;
404 if (hist_get(el) == CC_ERROR)
405 return (CC_ERROR);
406 }
407 el->el_line.cursor = ocursor;
408 pchar = '?';
409 } else {
410 pchar = ':';
411 }
412 }
413 ret = ce_inc_search(el, newdir);
414
415 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
416 /*
417 * break abort of failed search at last
418 * non-failed
419 */
420 ret = CC_NORM;
421
422 }
423 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
424 /* restore on normal return or error exit */
425 pchar = oldpchar;
426 el->el_search.patlen = oldpatlen;
427 if (el->el_history.eventno != ohisteventno) {
428 el->el_history.eventno = ohisteventno;
429 if (hist_get(el) == CC_ERROR)
430 return (CC_ERROR);
431 }
432 el->el_line.cursor = ocursor;
433 if (ret == CC_ERROR)
434 re_refresh(el);
435 }
436 if (done || ret != CC_NORM)
437 return (ret);
438 }
439 }
440
441
442 /* cv_search():
443 * Vi search.
444 */
445 protected el_action_t
446 cv_search(EditLine *el, int dir)
447 {
448 char ch;
449 char tmpbuf[EL_BUFSIZ];
450 int tmplen;
451
452 tmplen = 0;
453 #ifdef ANCHOR
454 tmpbuf[tmplen++] = '.';
455 tmpbuf[tmplen++] = '*';
456 #endif
457
458 el->el_line.buffer[0] = '\0';
459 el->el_line.lastchar = el->el_line.buffer;
460 el->el_line.cursor = el->el_line.buffer;
461 el->el_search.patdir = dir;
462
463 c_insert(el, 2); /* prompt + '\n' */
464 *el->el_line.cursor++ = '\n';
465 *el->el_line.cursor++ = dir == ED_SEARCH_PREV_HISTORY ? '/' : '?';
466 re_refresh(el);
467
468 #ifdef ANCHOR
469 #define LEN 2
470 #else
471 #define LEN 0
472 #endif
473
474 tmplen = c_gets(el, &tmpbuf[LEN]) + LEN;
475 ch = tmpbuf[tmplen];
476 tmpbuf[tmplen] = '\0';
477
478 if (tmplen == LEN) {
479 /*
480 * Use the old pattern, but wild-card it.
481 */
482 if (el->el_search.patlen == 0) {
483 el->el_line.buffer[0] = '\0';
484 el->el_line.lastchar = el->el_line.buffer;
485 el->el_line.cursor = el->el_line.buffer;
486 re_refresh(el);
487 return (CC_ERROR);
488 }
489 #ifdef ANCHOR
490 if (el->el_search.patbuf[0] != '.' &&
491 el->el_search.patbuf[0] != '*') {
492 (void) strncpy(tmpbuf, el->el_search.patbuf,
493 sizeof(tmpbuf) - 1);
494 el->el_search.patbuf[0] = '.';
495 el->el_search.patbuf[1] = '*';
496 (void) strncpy(&el->el_search.patbuf[2], tmpbuf,
497 EL_BUFSIZ - 3);
498 el->el_search.patlen++;
499 el->el_search.patbuf[el->el_search.patlen++] = '.';
500 el->el_search.patbuf[el->el_search.patlen++] = '*';
501 el->el_search.patbuf[el->el_search.patlen] = '\0';
502 }
503 #endif
504 } else {
505 #ifdef ANCHOR
506 tmpbuf[tmplen++] = '.';
507 tmpbuf[tmplen++] = '*';
508 #endif
509 tmpbuf[tmplen] = '\0';
510 (void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
511 el->el_search.patlen = tmplen;
512 }
513 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
514 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
515 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
516 ed_search_next_history(el, 0)) == CC_ERROR) {
517 re_refresh(el);
518 return (CC_ERROR);
519 } else {
520 if (ch == 0033) {
521 re_refresh(el);
522 *el->el_line.lastchar++ = '\n';
523 *el->el_line.lastchar = '\0';
524 re_goto_bottom(el);
525 return (CC_NEWLINE);
526 } else
527 return (CC_REFRESH);
528 }
529 }
530
531
532 /* ce_search_line():
533 * Look for a pattern inside a line
534 */
535 protected el_action_t
536 ce_search_line(EditLine *el, char *pattern, int dir)
537 {
538 char *cp;
539
540 if (dir == ED_SEARCH_PREV_HISTORY) {
541 for (cp = el->el_line.cursor; cp >= el->el_line.buffer; cp--)
542 if (el_match(cp, pattern)) {
543 el->el_line.cursor = cp;
544 return (CC_NORM);
545 }
546 return (CC_ERROR);
547 } else {
548 for (cp = el->el_line.cursor; *cp != '\0' &&
549 cp < el->el_line.limit; cp++)
550 if (el_match(cp, pattern)) {
551 el->el_line.cursor = cp;
552 return (CC_NORM);
553 }
554 return (CC_ERROR);
555 }
556 }
557
558
559 /* cv_repeat_srch():
560 * Vi repeat search
561 */
562 protected el_action_t
563 cv_repeat_srch(EditLine *el, int c)
564 {
565
566 #ifdef SDEBUG
567 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
568 c, el->el_search.patlen, el->el_search.patbuf);
569 #endif
570
571 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
572 el->el_line.lastchar = el->el_line.buffer;
573
574 switch (c) {
575 case ED_SEARCH_NEXT_HISTORY:
576 return (ed_search_next_history(el, 0));
577 case ED_SEARCH_PREV_HISTORY:
578 return (ed_search_prev_history(el, 0));
579 default:
580 return (CC_ERROR);
581 }
582 }
583
584
585 /* cv_csearch_back():
586 * Vi character search reverse
587 */
588 protected el_action_t
589 cv_csearch_back(EditLine *el, int ch, int count, int tflag)
590 {
591 char *cp;
592
593 cp = el->el_line.cursor;
594 while (count--) {
595 if (*cp == ch)
596 cp--;
597 while (cp > el->el_line.buffer && *cp != ch)
598 cp--;
599 }
600
601 if (cp < el->el_line.buffer || (cp == el->el_line.buffer && *cp != ch))
602 return (CC_ERROR);
603
604 if (*cp == ch && tflag)
605 cp++;
606
607 el->el_line.cursor = cp;
608
609 if (el->el_chared.c_vcmd.action & DELETE) {
610 el->el_line.cursor++;
611 cv_delfini(el);
612 return (CC_REFRESH);
613 }
614 re_refresh_cursor(el);
615 return (CC_NORM);
616 }
617
618
619 /* cv_csearch_fwd():
620 * Vi character search forward
621 */
622 protected el_action_t
623 cv_csearch_fwd(EditLine *el, int ch, int count, int tflag)
624 {
625 char *cp;
626
627 cp = el->el_line.cursor;
628 while (count--) {
629 if (*cp == ch)
630 cp++;
631 while (cp < el->el_line.lastchar && *cp != ch)
632 cp++;
633 }
634
635 if (cp >= el->el_line.lastchar)
636 return (CC_ERROR);
637
638 if (*cp == ch && tflag)
639 cp--;
640
641 el->el_line.cursor = cp;
642
643 if (el->el_chared.c_vcmd.action & DELETE) {
644 el->el_line.cursor++;
645 cv_delfini(el);
646 return (CC_REFRESH);
647 }
648 re_refresh_cursor(el);
649 return (CC_NORM);
650 }
651