📄 search.c
字号:
char *l; if (file_lline(sp, ep, &lno)) return (1); flags = *flagp; if (lno == 0) { if (LF_ISSET(SEARCH_MSG)) msgq(sp, M_INFO, EMPTYMSG); return (1); } re = &lre; if (resetup(sp, &re, BACKWARD, ptrn, eptrn, &delta, flagp)) return (1); /* If in the first column, start searching on the previous line. */ if (fm->cno == 0) { if (fm->lno == 1) { if (!O_ISSET(sp, O_WRAPSCAN)) { if (LF_ISSET(SEARCH_MSG)) msgq(sp, M_INFO, SOFMSG); return (1); } } else lno = fm->lno - 1; } else lno = fm->lno; /* Turn on busy message, interrupts. */ btear = F_ISSET(sp, S_EXSILENT) ? 0 : !busy_on(sp, "Searching..."); itear = !intr_init(sp); for (rval = 1, wrapped = 0, coff = fm->cno;; --lno, coff = 0) { if (F_ISSET(sp, S_INTERRUPTED)) { msgq(sp, M_INFO, "Interrupted."); break; } if (wrapped && lno < fm->lno || lno == 0) { if (wrapped) { if (LF_ISSET(SEARCH_MSG)) msgq(sp, M_INFO, NOTFOUND); break; } if (!O_ISSET(sp, O_WRAPSCAN)) { if (LF_ISSET(SEARCH_MSG)) msgq(sp, M_INFO, SOFMSG); break; } if (file_lline(sp, ep, &lno)) goto err; if (lno == 0) { if (LF_ISSET(SEARCH_MSG)) msgq(sp, M_INFO, EMPTYMSG); break; } ++lno; wrapped = 1; continue; } if ((l = file_gline(sp, ep, lno, &len)) == NULL) goto err; /* Set the termination. */ match[0].rm_so = 0; match[0].rm_eo = len;#if defined(DEBUG) && 0 TRACE(sp, "B search: %lu from 0 to %qu\n", lno, match[0].rm_eo);#endif /* Search the line. */ eval = regexec(re, l, 1, match, (match[0].rm_eo == len ? 0 : REG_NOTEOL) | REG_STARTEND); if (eval == REG_NOMATCH) continue; if (eval != 0) { re_error(sp, eval, re); break; } /* Check for a match starting past the cursor. */ if (coff != 0 && match[0].rm_so >= coff) continue; /* Warn if wrapped. */ if (wrapped && O_ISSET(sp, O_WARN) && LF_ISSET(SEARCH_MSG)) msgq(sp, M_VINFO, WRAPMSG); if (delta) { if (check_delta(sp, ep, delta, lno)) break; rm->lno = delta + lno; rm->cno = 0; } else {#if defined(DEBUG) && 0 TRACE(sp, "found: %qu to %qu\n", match[0].rm_so, match[0].rm_eo);#endif /* * We now have the first match on the line. Step * through the line character by character until we * find the last acceptable match. This is painful, * we need a better interface to regex to make this * work. */ for (;;) { last = match[0].rm_so++; if (match[0].rm_so >= len) break; match[0].rm_eo = len; eval = regexec(re, l, 1, match, (match[0].rm_so == 0 ? 0 : REG_NOTBOL) | REG_STARTEND); if (eval == REG_NOMATCH) break; if (eval != 0) { re_error(sp, eval, re); goto err; } if (coff && match[0].rm_so >= coff) break; } rm->lno = lno; /* See comment in f_search(). */ if (!LF_ISSET(SEARCH_EOL) && last >= len) rm->cno = len ? len - 1 : 0; else rm->cno = last; } rval = 0; break; } /* Turn off busy message, interrupts. */err: if (btear) busy_off(sp); if (itear) intr_end(sp); return (rval);}/* * re_conv -- * Convert vi's regular expressions into something that the * the POSIX 1003.2 RE functions can handle. * * There are three conversions we make to make vi's RE's (specifically * the global, search, and substitute patterns) work with POSIX RE's. * * 1: If O_MAGIC is not set, strip backslashes from the magic character * set (.[]*~) that have them, and add them to the ones that don't. * 2: If O_MAGIC is not set, the string "\~" is replaced with the text * from the last substitute command's replacement string. If O_MAGIC * is set, it's the string "~". * 3: The pattern \<ptrn\> does "word" searches, convert it to use the * new RE escapes. */intre_conv(sp, ptrnp, replacedp) SCR *sp; char **ptrnp; int *replacedp;{ size_t blen, needlen; int magic; char *bp, *p, *t; /* * First pass through, we figure out how much space we'll need. * We do it in two passes, on the grounds that most of the time * the user is doing a search and won't have magic characters. * That way we can skip the malloc and memmove's. */ for (p = *ptrnp, magic = 0, needlen = 0; *p != '\0'; ++p) switch (*p) { case '\\': switch (*++p) { case '<': magic = 1; needlen += sizeof(RE_WSTART); break; case '>': magic = 1; needlen += sizeof(RE_WSTOP); break; case '~': if (!O_ISSET(sp, O_MAGIC)) { magic = 1; needlen += sp->repl_len; } break; case '.': case '[': case ']': case '*': if (!O_ISSET(sp, O_MAGIC)) { magic = 1; needlen += 1; } break; default: needlen += 2; } break; case '~': if (O_ISSET(sp, O_MAGIC)) { magic = 1; needlen += sp->repl_len; } break; case '.': case '[': case ']': case '*': if (!O_ISSET(sp, O_MAGIC)) { magic = 1; needlen += 2; } break; default: needlen += 1; break; } if (!magic) { *replacedp = 0; return (0); } /* * Get enough memory to hold the final pattern. * * XXX * It's nul-terminated, for now. */ GET_SPACE_RET(sp, bp, blen, needlen + 1); for (p = *ptrnp, t = bp; *p != '\0'; ++p) switch (*p) { case '\\': switch (*++p) { case '<': memmove(t, RE_WSTART, sizeof(RE_WSTART) - 1); t += sizeof(RE_WSTART) - 1; break; case '>': memmove(t, RE_WSTOP, sizeof(RE_WSTOP) - 1); t += sizeof(RE_WSTOP) - 1; break; case '~': if (O_ISSET(sp, O_MAGIC)) *t++ = '~'; else { memmove(t, sp->repl, sp->repl_len); t += sp->repl_len; } break; case '.': case '[': case ']': case '*': if (O_ISSET(sp, O_MAGIC)) *t++ = '\\'; *t++ = *p; break; default: *t++ = '\\'; *t++ = *p; } break; case '~': if (O_ISSET(sp, O_MAGIC)) { memmove(t, sp->repl, sp->repl_len); t += sp->repl_len; } else *t++ = '~'; break; case '.': case '[': case ']': case '*': if (!O_ISSET(sp, O_MAGIC)) *t++ = '\\'; *t++ = *p; break; default: *t++ = *p; break; } *t = '\0'; *ptrnp = bp; *replacedp = 1; return (0);}/* * get_delta -- * Get a line delta. The trickiness is that the delta can be pretty * complicated, i.e. "+3-2+3++- ++" is allowed. * * !!! * In historic vi, if you had a delta on a search pattern which was used as * a motion command, the command became a line mode command regardless of the * cursor positions. A fairly common trick is to use a delta of "+0" to make * the command a line mode command. This is the only place that knows about * delta's, so we set the return flag information here. */static intget_delta(sp, dp, valp, flagp) SCR *sp; char **dp; long *valp; u_int *flagp;{ char *p; long val, tval; for (tval = 0, p = *dp; *p != '\0'; *flagp |= SEARCH_DELTA) { if (isblank(*p)) { ++p; continue; } if (*p == '+' || *p == '-') { if (!isdigit(*(p + 1))) { if (*p == '+') { if (tval == LONG_MAX) goto overflow; ++tval; } else { if (tval == LONG_MIN) goto underflow; --tval; } ++p; continue; } } else if (!isdigit(*p)) break; errno = 0; val = strtol(p, &p, 10); if (errno == ERANGE) { if (val == LONG_MAX)overflow: msgq(sp, M_ERR, "Delta value overflow."); else if (val == LONG_MIN)underflow: msgq(sp, M_ERR, "Delta value underflow."); else msgq(sp, M_SYSERR, NULL); return (1); } if (val >= 0) { if (LONG_MAX - val < tval) goto overflow; } else if (-(LONG_MIN - tval) > val) goto underflow; tval += val; } *dp = p; *valp = tval; return (0);}/* * check_delta -- * Check a line delta to see if it's legal. */static intcheck_delta(sp, ep, delta, lno) SCR *sp; EXF *ep; long delta; recno_t lno;{ /* A delta can overflow a record number. */ if (delta < 0) { if (lno < LONG_MAX && delta >= (long)lno) { msgq(sp, M_ERR, "Search offset before line 1."); return (1); } } else { if (ULONG_MAX - lno < delta) { msgq(sp, M_ERR, "Delta value overflow."); return (1); } if (file_gline(sp, ep, lno + delta, NULL) == NULL) { msgq(sp, M_ERR, "Search offset past end-of-file."); return (1); } } return (0);}/* * re_error -- * Report a regular expression error. */voidre_error(sp, errcode, preg) SCR *sp; int errcode; regex_t *preg;{ size_t s; char *oe; s = regerror(errcode, preg, "", 0); if ((oe = malloc(s)) == NULL) msgq(sp, M_SYSERR, NULL); else { (void)regerror(errcode, preg, oe, s); msgq(sp, M_ERR, "RE error: %s", oe); } free(oe);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -