search.c revision 1.42 1 /* $NetBSD: search.c,v 1.42 2016/04/11 00:22:48 christos 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. 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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: search.c,v 1.42 2016/04/11 00:22:48 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
43
44 /*
45 * search.c: History and character search functions
46 */
47 #include <stdlib.h>
48 #include <string.h>
49 #if defined(REGEX)
50 #include <regex.h>
51 #elif defined(REGEXP)
52 #include <regexp.h>
53 #endif
54
55 #include "el.h"
56 #include "common.h"
57
58 /*
59 * Adjust cursor in vi mode to include the character under it
60 */
61 #define EL_CURSOR(el) \
62 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
63 ((el)->el_map.current == (el)->el_map.alt)))
64
65 /* search_init():
66 * Initialize the search stuff
67 */
68 protected int
69 search_init(EditLine *el)
70 {
71
72 el->el_search.patbuf = el_malloc(EL_BUFSIZ *
73 sizeof(*el->el_search.patbuf));
74 if (el->el_search.patbuf == NULL)
75 return -1;
76 el->el_search.patlen = 0;
77 el->el_search.patdir = -1;
78 el->el_search.chacha = '\0';
79 el->el_search.chadir = CHAR_FWD;
80 el->el_search.chatflg = 0;
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(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 static ct_buffer_t conv;
116 #if defined (REGEX)
117 regex_t re;
118 int rv;
119 #elif defined (REGEXP)
120 regexp *rp;
121 int rv;
122 #else
123 extern char *re_comp(const char *);
124 extern int re_exec(const char *);
125 #endif
126
127 if (wcsstr(str, pat) != 0)
128 return 1;
129
130 #if defined(REGEX)
131 if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
132 rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
133 0) == 0;
134 regfree(&re);
135 } else {
136 rv = 0;
137 }
138 return rv;
139 #elif defined(REGEXP)
140 if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
141 rv = regexec(re, ct_encode_string(str, &conv));
142 el_free(re);
143 } else {
144 rv = 0;
145 }
146 return rv;
147 #else
148 if (re_comp(ct_encode_string(pat, &conv)) != NULL)
149 return 0;
150 else
151 return re_exec(ct_encode_string(str, &conv)) == 1;
152 #endif
153 }
154
155
156 /* c_hmatch():
157 * return True if the pattern matches the prefix
158 */
159 protected int
160 c_hmatch(EditLine *el, const Char *str)
161 {
162 #ifdef SDEBUG
163 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
164 el->el_search.patbuf, str);
165 #endif /* SDEBUG */
166
167 return el_match(str, el->el_search.patbuf);
168 }
169
170
171 /* c_setpat():
172 * Set the history seatch pattern
173 */
174 protected void
175 c_setpat(EditLine *el)
176 {
177 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
178 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
179 el->el_search.patlen =
180 (size_t)(EL_CURSOR(el) - el->el_line.buffer);
181 if (el->el_search.patlen >= EL_BUFSIZ)
182 el->el_search.patlen = EL_BUFSIZ - 1;
183 if (el->el_search.patlen != 0) {
184 (void) wcsncpy(el->el_search.patbuf, el->el_line.buffer,
185 el->el_search.patlen);
186 el->el_search.patbuf[el->el_search.patlen] = '\0';
187 } else
188 el->el_search.patlen = wcslen(el->el_search.patbuf);
189 }
190 #ifdef SDEBUG
191 (void) fprintf(el->el_errfile, "\neventno = %d\n",
192 el->el_history.eventno);
193 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
194 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
195 el->el_search.patbuf);
196 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
197 EL_CURSOR(el) - el->el_line.buffer,
198 el->el_line.lastchar - el->el_line.buffer);
199 #endif
200 }
201
202
203 /* ce_inc_search():
204 * Emacs incremental search
205 */
206 protected el_action_t
207 ce_inc_search(EditLine *el, int dir)
208 {
209 static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
210 STRbck[] = {'b', 'c', 'k', '\0'};
211 static Char pchar = ':';/* ':' = normal, '?' = failed */
212 static Char endcmd[2] = {'\0', '\0'};
213 Char *ocursor = el->el_line.cursor, oldpchar = pchar, ch;
214 const Char *cp;
215 wchar_t wch;
216
217 el_action_t ret = CC_NORM;
218
219 int ohisteventno = el->el_history.eventno;
220 size_t oldpatlen = el->el_search.patlen;
221 int newdir = dir;
222 int done, redo;
223
224 if (el->el_line.lastchar + sizeof(STRfwd) /
225 sizeof(*el->el_line.lastchar) + 2 +
226 el->el_search.patlen >= el->el_line.limit)
227 return CC_ERROR;
228
229 for (;;) {
230
231 if (el->el_search.patlen == 0) { /* first round */
232 pchar = ':';
233 #ifdef ANCHOR
234 #define LEN 2
235 el->el_search.patbuf[el->el_search.patlen++] = '.';
236 el->el_search.patbuf[el->el_search.patlen++] = '*';
237 #else
238 #define LEN 0
239 #endif
240 }
241 done = redo = 0;
242 *el->el_line.lastchar++ = '\n';
243 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
244 *cp; *el->el_line.lastchar++ = *cp++)
245 continue;
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++)
250 continue;
251 *el->el_line.lastchar = '\0';
252 re_refresh(el);
253
254 if (el_wgetc(el, &wch) != 1)
255 return ed_end_of_file(el, 0);
256
257 ch = (Char)wch;
258
259 switch (el->el_map.current[(unsigned char) ch]) {
260 case ED_INSERT:
261 case ED_DIGIT:
262 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
263 terminal_beep(el);
264 else {
265 el->el_search.patbuf[el->el_search.patlen++] =
266 ch;
267 *el->el_line.lastchar++ = ch;
268 *el->el_line.lastchar = '\0';
269 re_refresh(el);
270 }
271 break;
272
273 case EM_INC_SEARCH_NEXT:
274 newdir = ED_SEARCH_NEXT_HISTORY;
275 redo++;
276 break;
277
278 case EM_INC_SEARCH_PREV:
279 newdir = ED_SEARCH_PREV_HISTORY;
280 redo++;
281 break;
282
283 case EM_DELETE_PREV_CHAR:
284 case ED_DELETE_PREV_CHAR:
285 if (el->el_search.patlen > LEN)
286 done++;
287 else
288 terminal_beep(el);
289 break;
290
291 default:
292 switch (ch) {
293 case 0007: /* ^G: Abort */
294 ret = CC_ERROR;
295 done++;
296 break;
297
298 case 0027: /* ^W: Append word */
299 /* No can do if globbing characters in pattern */
300 for (cp = &el->el_search.patbuf[LEN];; cp++)
301 if (cp >= &el->el_search.patbuf[
302 el->el_search.patlen]) {
303 el->el_line.cursor +=
304 el->el_search.patlen - LEN - 1;
305 cp = c__next_word(el->el_line.cursor,
306 el->el_line.lastchar, 1,
307 ce__isword);
308 while (el->el_line.cursor < cp &&
309 *el->el_line.cursor != '\n') {
310 if (el->el_search.patlen >=
311 EL_BUFSIZ - LEN) {
312 terminal_beep(el);
313 break;
314 }
315 el->el_search.patbuf[el->el_search.patlen++] =
316 *el->el_line.cursor;
317 *el->el_line.lastchar++ =
318 *el->el_line.cursor++;
319 }
320 el->el_line.cursor = ocursor;
321 *el->el_line.lastchar = '\0';
322 re_refresh(el);
323 break;
324 } else if (isglob(*cp)) {
325 terminal_beep(el);
326 break;
327 }
328 break;
329
330 default: /* Terminate and execute cmd */
331 endcmd[0] = ch;
332 el_wpush(el, endcmd);
333 /* FALLTHROUGH */
334
335 case 0033: /* ESC: Terminate */
336 ret = CC_REFRESH;
337 done++;
338 break;
339 }
340 break;
341 }
342
343 while (el->el_line.lastchar > el->el_line.buffer &&
344 *el->el_line.lastchar != '\n')
345 *el->el_line.lastchar-- = '\0';
346 *el->el_line.lastchar = '\0';
347
348 if (!done) {
349
350 /* Can't search if unmatched '[' */
351 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
352 ch = L']';
353 cp >= &el->el_search.patbuf[LEN];
354 cp--)
355 if (*cp == '[' || *cp == ']') {
356 ch = *cp;
357 break;
358 }
359 if (el->el_search.patlen > LEN && ch != L'[') {
360 if (redo && newdir == dir) {
361 if (pchar == '?') { /* wrap around */
362 el->el_history.eventno =
363 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
364 if (hist_get(el) == CC_ERROR)
365 /* el->el_history.event
366 * no was fixed by
367 * first call */
368 (void) hist_get(el);
369 el->el_line.cursor = newdir ==
370 ED_SEARCH_PREV_HISTORY ?
371 el->el_line.lastchar :
372 el->el_line.buffer;
373 } else
374 el->el_line.cursor +=
375 newdir ==
376 ED_SEARCH_PREV_HISTORY ?
377 -1 : 1;
378 }
379 #ifdef ANCHOR
380 el->el_search.patbuf[el->el_search.patlen++] =
381 '.';
382 el->el_search.patbuf[el->el_search.patlen++] =
383 '*';
384 #endif
385 el->el_search.patbuf[el->el_search.patlen] =
386 '\0';
387 if (el->el_line.cursor < el->el_line.buffer ||
388 el->el_line.cursor > el->el_line.lastchar ||
389 (ret = ce_search_line(el, newdir))
390 == CC_ERROR) {
391 /* avoid c_setpat */
392 el->el_state.lastcmd =
393 (el_action_t) newdir;
394 ret = (el_action_t)
395 (newdir == ED_SEARCH_PREV_HISTORY ?
396 ed_search_prev_history(el, 0) :
397 ed_search_next_history(el, 0));
398 if (ret != CC_ERROR) {
399 el->el_line.cursor = newdir ==
400 ED_SEARCH_PREV_HISTORY ?
401 el->el_line.lastchar :
402 el->el_line.buffer;
403 (void) ce_search_line(el,
404 newdir);
405 }
406 }
407 el->el_search.patlen -= LEN;
408 el->el_search.patbuf[el->el_search.patlen] =
409 '\0';
410 if (ret == CC_ERROR) {
411 terminal_beep(el);
412 if (el->el_history.eventno !=
413 ohisteventno) {
414 el->el_history.eventno =
415 ohisteventno;
416 if (hist_get(el) == CC_ERROR)
417 return CC_ERROR;
418 }
419 el->el_line.cursor = ocursor;
420 pchar = '?';
421 } else {
422 pchar = ':';
423 }
424 }
425 ret = ce_inc_search(el, newdir);
426
427 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
428 /*
429 * break abort of failed search at last
430 * non-failed
431 */
432 ret = CC_NORM;
433
434 }
435 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
436 /* restore on normal return or error exit */
437 pchar = oldpchar;
438 el->el_search.patlen = oldpatlen;
439 if (el->el_history.eventno != ohisteventno) {
440 el->el_history.eventno = ohisteventno;
441 if (hist_get(el) == CC_ERROR)
442 return CC_ERROR;
443 }
444 el->el_line.cursor = ocursor;
445 if (ret == CC_ERROR)
446 re_refresh(el);
447 }
448 if (done || ret != CC_NORM)
449 return ret;
450 }
451 }
452
453
454 /* cv_search():
455 * Vi search.
456 */
457 protected el_action_t
458 cv_search(EditLine *el, int dir)
459 {
460 Char ch;
461 Char tmpbuf[EL_BUFSIZ];
462 ssize_t tmplen;
463
464 #ifdef ANCHOR
465 tmpbuf[0] = '.';
466 tmpbuf[1] = '*';
467 #endif
468 tmplen = LEN;
469
470 el->el_search.patdir = dir;
471
472 tmplen = c_gets(el, &tmpbuf[LEN],
473 dir == ED_SEARCH_PREV_HISTORY ? L"\n/" : L"\n?" );
474 if (tmplen == -1)
475 return CC_REFRESH;
476
477 tmplen += LEN;
478 ch = tmpbuf[tmplen];
479 tmpbuf[tmplen] = '\0';
480
481 if (tmplen == LEN) {
482 /*
483 * Use the old pattern, but wild-card it.
484 */
485 if (el->el_search.patlen == 0) {
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) wcsncpy(tmpbuf, el->el_search.patbuf,
493 sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
494 el->el_search.patbuf[0] = '.';
495 el->el_search.patbuf[1] = '*';
496 (void) wcsncpy(&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) wcsncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
511 el->el_search.patlen = (size_t)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 }
520 if (ch == 0033) {
521 re_refresh(el);
522 return ed_newline(el, 0);
523 }
524 return CC_REFRESH;
525 }
526
527
528 /* ce_search_line():
529 * Look for a pattern inside a line
530 */
531 protected el_action_t
532 ce_search_line(EditLine *el, int dir)
533 {
534 Char *cp = el->el_line.cursor;
535 Char *pattern = el->el_search.patbuf;
536 Char oc, *ocp;
537 #ifdef ANCHOR
538 ocp = &pattern[1];
539 oc = *ocp;
540 *ocp = '^';
541 #else
542 ocp = pattern;
543 oc = *ocp;
544 #endif
545
546 if (dir == ED_SEARCH_PREV_HISTORY) {
547 for (; cp >= el->el_line.buffer; cp--) {
548 if (el_match(cp, ocp)) {
549 *ocp = oc;
550 el->el_line.cursor = cp;
551 return CC_NORM;
552 }
553 }
554 *ocp = oc;
555 return CC_ERROR;
556 } else {
557 for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
558 if (el_match(cp, ocp)) {
559 *ocp = oc;
560 el->el_line.cursor = cp;
561 return CC_NORM;
562 }
563 }
564 *ocp = oc;
565 return CC_ERROR;
566 }
567 }
568
569
570 /* cv_repeat_srch():
571 * Vi repeat search
572 */
573 protected el_action_t
574 cv_repeat_srch(EditLine *el, wint_t c)
575 {
576
577 #ifdef SDEBUG
578 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
579 c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
580 #endif
581
582 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
583 el->el_line.lastchar = el->el_line.buffer;
584
585 switch (c) {
586 case ED_SEARCH_NEXT_HISTORY:
587 return ed_search_next_history(el, 0);
588 case ED_SEARCH_PREV_HISTORY:
589 return ed_search_prev_history(el, 0);
590 default:
591 return CC_ERROR;
592 }
593 }
594
595
596 /* cv_csearch():
597 * Vi character search
598 */
599 protected el_action_t
600 cv_csearch(EditLine *el, int direction, wint_t ch, int count, int tflag)
601 {
602 Char *cp;
603
604 if (ch == 0)
605 return CC_ERROR;
606
607 if (ch == (wint_t)-1) {
608 if (el_wgetc(el, &ch) != 1)
609 return ed_end_of_file(el, 0);
610 }
611
612 /* Save for ';' and ',' commands */
613 el->el_search.chacha = (Char)ch;
614 el->el_search.chadir = direction;
615 el->el_search.chatflg = (char)tflag;
616
617 cp = el->el_line.cursor;
618 while (count--) {
619 if ((wint_t)*cp == ch)
620 cp += direction;
621 for (;;cp += direction) {
622 if (cp >= el->el_line.lastchar)
623 return CC_ERROR;
624 if (cp < el->el_line.buffer)
625 return CC_ERROR;
626 if ((wint_t)*cp == ch)
627 break;
628 }
629 }
630
631 if (tflag)
632 cp -= direction;
633
634 el->el_line.cursor = cp;
635
636 if (el->el_chared.c_vcmd.action != NOP) {
637 if (direction > 0)
638 el->el_line.cursor++;
639 cv_delfini(el);
640 return CC_REFRESH;
641 }
642 return CC_CURSOR;
643 }
644