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