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