36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] =
"@(#)search.c 8.1 (Berkeley) 6/4/93";
57 #define EL_CURSOR(el) \
58 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
59 ((el)->el_map.current == (el)->el_map.alt)))
68 el->el_search.patbuf = el_malloc(EL_BUFSIZ *
69 sizeof(*el->el_search.patbuf));
70 if (el->el_search.patbuf == NULL)
72 el->el_search.patlen = 0;
73 el->el_search.patdir = -1;
74 el->el_search.chacha =
'\0';
75 el->el_search.chadir = CHAR_FWD;
76 el->el_search.chatflg = 0;
88 el_free(el->el_search.patbuf);
89 el->el_search.patbuf = NULL;
99 regerror(
const char *
msg)
109 el_match(
const Char *str,
const Char *pat)
112 static ct_buffer_t conv;
117 #elif defined (REGEXP)
121 extern char *re_comp(
const char *);
122 extern int re_exec(
const char *);
125 if (Strstr(str, pat) != 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,
137 #elif defined(REGEXP)
138 if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
139 rv = regexec(re, ct_encode_string(str, &conv));
146 if (re_comp(ct_encode_string(pat, &conv)) != NULL)
149 return re_exec(ct_encode_string(str, &conv) == 1);
158 c_hmatch(
EditLine *el,
const Char *str)
161 (void) fprintf(el->el_errfile,
"match `%s' with `%s'\n",
162 el->el_search.patbuf, str);
165 return el_match(str, el->el_search.patbuf);
175 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
176 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
177 el->el_search.patlen =
178 (size_t)(EL_CURSOR(el) - el->el_line.buffer);
179 if (el->el_search.patlen >= EL_BUFSIZ)
180 el->el_search.patlen = EL_BUFSIZ - 1;
181 if (el->el_search.patlen != 0) {
182 (void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
183 el->el_search.patlen);
184 el->el_search.patbuf[el->el_search.patlen] =
'\0';
186 el->el_search.patlen = Strlen(el->el_search.patbuf);
189 (void) fprintf(el->el_errfile,
"\neventno = %d\n",
190 el->el_history.eventno);
191 (void) fprintf(el->el_errfile,
"patlen = %d\n", el->el_search.patlen);
192 (void) fprintf(el->el_errfile,
"patbuf = \"%s\"\n",
193 el->el_search.patbuf);
194 (void) fprintf(el->el_errfile,
"cursor %d lastchar %d\n",
195 EL_CURSOR(el) - el->el_line.buffer,
196 el->el_line.lastchar - el->el_line.buffer);
204 protected el_action_t
205 ce_inc_search(
EditLine *el,
int dir)
207 static const Char STRfwd[] = {
'f',
'w',
'd',
'\0'},
208 STRbck[] = {
'b',
'c',
'k',
'\0'};
209 static Char pchar =
':';
210 static Char endcmd[2] = {
'\0',
'\0'};
211 Char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
214 el_action_t
ret = CC_NORM;
216 int ohisteventno = el->el_history.eventno;
217 size_t oldpatlen = el->el_search.patlen;
221 if (el->el_line.lastchar +
sizeof(STRfwd) /
222 sizeof(*el->el_line.lastchar) + 2 +
223 el->el_search.patlen >= el->el_line.limit)
228 if (el->el_search.patlen == 0) {
232 el->el_search.patbuf[el->el_search.patlen++] =
'.';
233 el->el_search.patbuf[el->el_search.patlen++] =
'*';
239 *el->el_line.lastchar++ =
'\n';
240 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
241 *cp; *el->el_line.lastchar++ = *cp++)
243 *el->el_line.lastchar++ = pchar;
244 for (cp = &el->el_search.patbuf[LEN];
245 cp < &el->el_search.patbuf[el->el_search.patlen];
246 *el->el_line.lastchar++ = *cp++)
248 *el->el_line.lastchar =
'\0';
251 if (FUN(el,getc)(el, &ch) != 1)
252 return ed_end_of_file(el, 0);
254 switch (el->el_map.current[(
unsigned char) ch]) {
257 if (el->el_search.patlen >= EL_BUFSIZ - LEN)
260 el->el_search.patbuf[el->el_search.patlen++] =
262 *el->el_line.lastchar++ = ch;
263 *el->el_line.lastchar =
'\0';
268 case EM_INC_SEARCH_NEXT:
269 newdir = ED_SEARCH_NEXT_HISTORY;
273 case EM_INC_SEARCH_PREV:
274 newdir = ED_SEARCH_PREV_HISTORY;
278 case EM_DELETE_PREV_CHAR:
279 case ED_DELETE_PREV_CHAR:
280 if (el->el_search.patlen > LEN)
295 for (cp = &el->el_search.patbuf[LEN];; cp++)
296 if (cp >= &el->el_search.patbuf[
297 el->el_search.patlen]) {
298 el->el_line.cursor +=
299 el->el_search.patlen - LEN - 1;
300 cp = c__next_word(el->el_line.cursor,
301 el->el_line.lastchar, 1,
303 while (el->el_line.cursor < cp &&
304 *el->el_line.cursor !=
'\n') {
305 if (el->el_search.patlen >=
310 el->el_search.patbuf[el->el_search.patlen++] =
312 *el->el_line.lastchar++ =
313 *el->el_line.cursor++;
315 el->el_line.cursor = ocursor;
316 *el->el_line.lastchar =
'\0';
319 }
else if (isglob(*cp)) {
327 FUN(el,push)(el, endcmd);
338 while (el->el_line.lastchar > el->el_line.buffer &&
339 *el->el_line.lastchar !=
'\n')
340 *el->el_line.lastchar-- =
'\0';
341 *el->el_line.lastchar =
'\0';
346 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
348 cp >= &el->el_search.patbuf[LEN];
350 if (*cp ==
'[' || *cp ==
']') {
354 if (el->el_search.patlen > LEN && ch !=
'[') {
355 if (redo && newdir == dir) {
357 el->el_history.eventno =
358 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
359 if (hist_get(el) == CC_ERROR)
364 el->el_line.cursor = newdir ==
365 ED_SEARCH_PREV_HISTORY ?
366 el->el_line.lastchar :
369 el->el_line.cursor +=
371 ED_SEARCH_PREV_HISTORY ?
375 el->el_search.patbuf[el->el_search.patlen++] =
377 el->el_search.patbuf[el->el_search.patlen++] =
380 el->el_search.patbuf[el->el_search.patlen] =
382 if (el->el_line.cursor < el->el_line.buffer ||
383 el->el_line.cursor > el->el_line.lastchar ||
384 (ret = ce_search_line(el, newdir))
387 el->el_state.lastcmd =
388 (el_action_t) newdir;
390 (newdir == ED_SEARCH_PREV_HISTORY ?
391 ed_search_prev_history(el, 0) :
392 ed_search_next_history(el, 0));
393 if (ret != CC_ERROR) {
394 el->el_line.cursor = newdir ==
395 ED_SEARCH_PREV_HISTORY ?
396 el->el_line.lastchar :
398 (void) ce_search_line(el,
402 el->el_search.patlen -= LEN;
403 el->el_search.patbuf[el->el_search.patlen] =
405 if (ret == CC_ERROR) {
407 if (el->el_history.eventno !=
409 el->el_history.eventno =
411 if (hist_get(el) == CC_ERROR)
414 el->el_line.cursor = ocursor;
420 ret = ce_inc_search(el, newdir);
422 if (ret == CC_ERROR && pchar ==
'?' && oldpchar ==
':')
430 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
433 el->el_search.patlen = oldpatlen;
434 if (el->el_history.eventno != ohisteventno) {
435 el->el_history.eventno = ohisteventno;
436 if (hist_get(el) == CC_ERROR)
439 el->el_line.cursor = ocursor;
443 if (done || ret != CC_NORM)
452 protected el_action_t
456 Char tmpbuf[EL_BUFSIZ];
465 el->el_search.patdir = dir;
467 tmplen = c_gets(el, &tmpbuf[LEN],
468 dir == ED_SEARCH_PREV_HISTORY ? STR(
"\n/") : STR(
"\n?") );
474 tmpbuf[tmplen] =
'\0';
480 if (el->el_search.patlen == 0) {
485 if (el->el_search.patbuf[0] !=
'.' &&
486 el->el_search.patbuf[0] !=
'*') {
487 (void) Strncpy(tmpbuf, el->el_search.patbuf,
488 sizeof(tmpbuf) /
sizeof(*tmpbuf) - 1);
489 el->el_search.patbuf[0] =
'.';
490 el->el_search.patbuf[1] =
'*';
491 (void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
493 el->el_search.patlen++;
494 el->el_search.patbuf[el->el_search.patlen++] =
'.';
495 el->el_search.patbuf[el->el_search.patlen++] =
'*';
496 el->el_search.patbuf[el->el_search.patlen] =
'\0';
501 tmpbuf[tmplen++] =
'.';
502 tmpbuf[tmplen++] =
'*';
504 tmpbuf[tmplen] =
'\0';
505 (void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
506 el->el_search.patlen = (size_t)tmplen;
508 el->el_state.lastcmd = (el_action_t) dir;
509 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
510 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
511 ed_search_next_history(el, 0)) == CC_ERROR) {
517 return ed_newline(el, 0);
526 protected el_action_t
527 ce_search_line(
EditLine *el,
int dir)
529 Char *cp = el->el_line.cursor;
530 Char *pattern = el->el_search.patbuf;
541 if (dir == ED_SEARCH_PREV_HISTORY) {
542 for (; cp >= el->el_line.buffer; cp--) {
543 if (el_match(cp, ocp)) {
545 el->el_line.cursor = cp;
552 for (; *cp !=
'\0' && cp < el->el_line.limit; cp++) {
553 if (el_match(cp, ocp)) {
555 el->el_line.cursor = cp;
568 protected el_action_t
573 (void) fprintf(el->el_errfile,
"dir %d patlen %d patbuf %s\n",
574 c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
577 el->el_state.lastcmd = (el_action_t) c;
578 el->el_line.lastchar = el->el_line.buffer;
581 case ED_SEARCH_NEXT_HISTORY:
582 return ed_search_next_history(el, 0);
583 case ED_SEARCH_PREV_HISTORY:
584 return ed_search_prev_history(el, 0);
594 protected el_action_t
595 cv_csearch(
EditLine *el,
int direction, Int ch,
int count,
int tflag)
604 if (FUN(el,getc)(el, &c) != 1)
605 return ed_end_of_file(el, 0);
610 el->el_search.chacha = ch;
611 el->el_search.chadir = direction;
612 el->el_search.chatflg = (char)tflag;
614 cp = el->el_line.cursor;
618 for (;;cp += direction) {
619 if (cp >= el->el_line.lastchar)
621 if (cp < el->el_line.buffer)
631 el->el_line.cursor = cp;
633 if (el->el_chared.c_vcmd.action != NOP) {
635 el->el_line.cursor++;