1
/* $NetBSD: search.c,v 1.20 2004/11/04 01:16:03 christos Exp $ */
1
/* $NetBSD: search.c,v 1.30 2011/10/04 15:27:04 christos Exp $ */
4
4
* Copyright (c) 1992, 1993
65
65
search_init(EditLine *el)
68
el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
68
el->el_search.patbuf = el_malloc(EL_BUFSIZ *
69
sizeof(*el->el_search.patbuf));
69
70
if (el->el_search.patbuf == NULL)
71
72
el->el_search.patlen = 0;
72
73
el->el_search.patdir = -1;
73
74
el->el_search.chacha = '\0';
74
75
el->el_search.chadir = CHAR_FWD;
75
76
el->el_search.chatflg = 0;
118
122
extern int re_exec(const char *);
121
if (strstr(str, pat) != NULL)
125
if (Strstr(str, pat) != 0)
124
128
#if defined(REGEX)
125
if (regcomp(&re, pat, 0) == 0) {
126
rv = regexec(&re, str, 0, NULL, 0) == 0;
129
if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
130
rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
132
137
#elif defined(REGEXP)
133
if ((re = regcomp(pat)) != NULL) {
134
rv = regexec(re, str);
138
if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
139
rv = regexec(re, ct_encode_string(str, &conv));
141
if (re_comp(pat) != NULL)
146
if (re_comp(ct_encode_string(pat, &conv)) != NULL)
144
return (re_exec(str) == 1);
149
return re_exec(ct_encode_string(str, &conv) == 1);
150
155
* return True if the pattern matches the prefix
153
c_hmatch(EditLine *el, const char *str)
158
c_hmatch(EditLine *el, const Char *str)
156
161
(void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
157
162
el->el_search.patbuf, str);
158
163
#endif /* SDEBUG */
160
return (el_match(str, el->el_search.patbuf));
165
return el_match(str, el->el_search.patbuf);
170
175
if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
171
176
el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
172
el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
177
el->el_search.patlen =
178
(size_t)(EL_CURSOR(el) - el->el_line.buffer);
173
179
if (el->el_search.patlen >= EL_BUFSIZ)
174
180
el->el_search.patlen = EL_BUFSIZ - 1;
175
181
if (el->el_search.patlen != 0) {
176
(void) strncpy(el->el_search.patbuf, el->el_line.buffer,
182
(void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
177
183
el->el_search.patlen);
178
184
el->el_search.patbuf[el->el_search.patlen] = '\0';
180
el->el_search.patlen = strlen(el->el_search.patbuf);
186
el->el_search.patlen = Strlen(el->el_search.patbuf);
183
189
(void) fprintf(el->el_errfile, "\neventno = %d\n",
198
204
protected el_action_t
199
205
ce_inc_search(EditLine *el, int dir)
201
static const char STRfwd[] = {'f', 'w', 'd', '\0'},
207
static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
202
208
STRbck[] = {'b', 'c', 'k', '\0'};
203
static char pchar = ':';/* ':' = normal, '?' = failed */
204
static char endcmd[2] = {'\0', '\0'};
205
char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
209
static Char pchar = ':';/* ':' = normal, '?' = failed */
210
static Char endcmd[2] = {'\0', '\0'};
211
Char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
208
214
el_action_t ret = CC_NORM;
210
216
int ohisteventno = el->el_history.eventno;
211
int oldpatlen = el->el_search.patlen;
217
size_t oldpatlen = el->el_search.patlen;
212
218
int newdir = dir;
215
if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
221
if (el->el_line.lastchar + sizeof(STRfwd) /
222
sizeof(*el->el_line.lastchar) + 2 +
216
223
el->el_search.patlen >= el->el_line.limit)
241
248
*el->el_line.lastchar = '\0';
244
if (el_getc(el, &ch) != 1)
245
return (ed_end_of_file(el, 0));
251
if (FUN(el,getc)(el, &ch) != 1)
252
return ed_end_of_file(el, 0);
247
254
switch (el->el_map.current[(unsigned char) ch]) {
250
257
if (el->el_search.patlen >= EL_BUFSIZ - LEN)
253
260
el->el_search.patbuf[el->el_search.patlen++] =
312
319
} else if (isglob(*cp)) {
318
325
default: /* Terminate and execute cmd */
327
FUN(el,push)(el, endcmd);
321
328
/* FALLTHROUGH */
323
330
case 0033: /* ESC: Terminate */
379
386
/* avoid c_setpat */
380
387
el->el_state.lastcmd =
381
388
(el_action_t) newdir;
382
ret = newdir == ED_SEARCH_PREV_HISTORY ?
390
(newdir == ED_SEARCH_PREV_HISTORY ?
383
391
ed_search_prev_history(el, 0) :
384
ed_search_next_history(el, 0);
392
ed_search_next_history(el, 0));
385
393
if (ret != CC_ERROR) {
386
394
el->el_line.cursor = newdir ==
387
395
ED_SEARCH_PREV_HISTORY ?
426
434
if (el->el_history.eventno != ohisteventno) {
427
435
el->el_history.eventno = ohisteventno;
428
436
if (hist_get(el) == CC_ERROR)
431
439
el->el_line.cursor = ocursor;
432
440
if (ret == CC_ERROR)
435
443
if (done || ret != CC_NORM)
457
465
el->el_search.patdir = dir;
459
467
tmplen = c_gets(el, &tmpbuf[LEN],
460
dir == ED_SEARCH_PREV_HISTORY ? "\n/" : "\n?" );
468
dir == ED_SEARCH_PREV_HISTORY ? STR("\n/") : STR("\n?") );
461
469
if (tmplen == -1)
462
470
return CC_REFRESH;
472
480
if (el->el_search.patlen == 0) {
477
485
if (el->el_search.patbuf[0] != '.' &&
478
486
el->el_search.patbuf[0] != '*') {
479
(void) strncpy(tmpbuf, el->el_search.patbuf,
487
(void) Strncpy(tmpbuf, el->el_search.patbuf,
488
sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
481
489
el->el_search.patbuf[0] = '.';
482
490
el->el_search.patbuf[1] = '*';
483
(void) strncpy(&el->el_search.patbuf[2], tmpbuf,
491
(void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
485
493
el->el_search.patlen++;
486
494
el->el_search.patbuf[el->el_search.patlen++] = '.';
494
502
tmpbuf[tmplen++] = '*';
496
504
tmpbuf[tmplen] = '\0';
497
(void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
498
el->el_search.patlen = tmplen;
505
(void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
506
el->el_search.patlen = (size_t)tmplen;
500
508
el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
501
509
el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
502
510
if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
503
511
ed_search_next_history(el, 0)) == CC_ERROR) {
507
515
if (ch == 0033) {
509
517
return ed_newline(el, 0);
518
526
protected el_action_t
519
527
ce_search_line(EditLine *el, int dir)
521
char *cp = el->el_line.cursor;
522
char *pattern = el->el_search.patbuf;
529
Char *cp = el->el_line.cursor;
530
Char *pattern = el->el_search.patbuf;
525
533
ocp = &pattern[1];
558
566
* Vi repeat search
560
568
protected el_action_t
561
cv_repeat_srch(EditLine *el, int c)
569
cv_repeat_srch(EditLine *el, Int c)
565
573
(void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
566
c, el->el_search.patlen, el->el_search.patbuf);
574
c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
569
577
el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
573
581
case ED_SEARCH_NEXT_HISTORY:
574
return (ed_search_next_history(el, 0));
582
return ed_search_next_history(el, 0);
575
583
case ED_SEARCH_PREV_HISTORY:
576
return (ed_search_prev_history(el, 0));
584
return ed_search_prev_history(el, 0);
584
592
* Vi character search
586
594
protected el_action_t
587
cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
595
cv_csearch(EditLine *el, int direction, Int ch, int count, int tflag)
596
if (el_getc(el, &c) != 1)
604
if (FUN(el,getc)(el, &c) != 1)
597
605
return ed_end_of_file(el, 0);
601
609
/* Save for ';' and ',' commands */
602
610
el->el_search.chacha = ch;
603
611
el->el_search.chadir = direction;
604
el->el_search.chatflg = tflag;
612
el->el_search.chatflg = (char)tflag;
606
614
cp = el->el_line.cursor;
607
615
while (count--) {
610
618
for (;;cp += direction) {
611
619
if (cp >= el->el_line.lastchar)
613
621
if (cp < el->el_line.buffer)