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