2001-03-31 15:37:44 +04:00
|
|
|
/* $NetBSD: search.c,v 1.6 2001/03/31 11:37:47 aymeric Exp $ */
|
1998-01-09 11:03:16 +03:00
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-05-20 06:01:36 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-17 20:17:19 +04:00
|
|
|
*
|
1996-05-20 06:01:36 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
#ifndef lint
|
2001-03-31 15:37:44 +04:00
|
|
|
static const char sccsid[] = "@(#)search.c 10.25 (Berkeley) 6/30/96";
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#include "common.h"
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
typedef enum { S_EMPTY, S_EOF, S_NOPREV, S_NOTFOUND, S_SOF, S_WRAP } smsg_t;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
static void search_msg __P((SCR *, smsg_t));
|
2001-03-31 15:37:44 +04:00
|
|
|
static int search_init __P((SCR *, dir_t, char *, size_t, char **, u_int));
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/*
|
2001-03-31 15:37:44 +04:00
|
|
|
* search_init --
|
1996-05-20 06:01:36 +04:00
|
|
|
* Set up a search.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
|
|
|
static int
|
2001-03-31 15:37:44 +04:00
|
|
|
search_init(sp, dir, ptrn, plen, epp, flags)
|
1994-08-17 20:17:19 +04:00
|
|
|
SCR *sp;
|
1996-05-20 06:01:36 +04:00
|
|
|
dir_t dir;
|
1994-08-17 20:17:19 +04:00
|
|
|
char *ptrn, **epp;
|
2001-03-31 15:37:44 +04:00
|
|
|
size_t plen;
|
1994-08-17 20:17:19 +04:00
|
|
|
u_int flags;
|
1996-05-20 06:01:36 +04:00
|
|
|
{
|
|
|
|
recno_t lno;
|
|
|
|
int delim;
|
1994-08-17 20:17:19 +04:00
|
|
|
char *p, *t;
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* If the file is empty, it's a fast search. */
|
|
|
|
if (sp->lno <= 1) {
|
|
|
|
if (db_last(sp, &lno))
|
|
|
|
return (1);
|
|
|
|
if (lno == 0) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
|
|
|
search_msg(sp, S_EMPTY);
|
1994-08-17 20:17:19 +04:00
|
|
|
return (1);
|
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
if (LF_ISSET(SEARCH_PARSE)) { /* Parse the string. */
|
|
|
|
/*
|
2001-03-31 15:37:44 +04:00
|
|
|
* Use the saved pattern if no pattern specified, or if only
|
|
|
|
* one or two delimiter characters specified.
|
1996-05-20 06:01:36 +04:00
|
|
|
*
|
|
|
|
* !!!
|
2001-03-31 15:37:44 +04:00
|
|
|
* Historically, only the pattern itself was saved, vi didn't
|
|
|
|
* preserve addressing or delta information.
|
1996-05-20 06:01:36 +04:00
|
|
|
*/
|
|
|
|
if (ptrn == NULL)
|
|
|
|
goto prev;
|
2001-03-31 15:37:44 +04:00
|
|
|
if (plen == 1) {
|
1996-05-20 06:01:36 +04:00
|
|
|
if (epp != NULL)
|
|
|
|
*epp = ptrn + 1;
|
|
|
|
goto prev;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
if (ptrn[0] == ptrn[1]) {
|
|
|
|
if (epp != NULL)
|
|
|
|
*epp = ptrn + 2;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Complain if we don't have a previous pattern. */
|
|
|
|
prev: if (sp->re == NULL) {
|
|
|
|
search_msg(sp, S_NOPREV);
|
|
|
|
return (1);
|
|
|
|
}
|
2001-03-31 15:37:44 +04:00
|
|
|
/* Re-compile the search pattern if necessary. */
|
|
|
|
if (!F_ISSET(sp, SC_RE_SEARCH) && re_compile(sp,
|
|
|
|
sp->re, sp->re_len, NULL, NULL, &sp->re_c,
|
1996-05-20 06:01:36 +04:00
|
|
|
RE_C_SEARCH |
|
|
|
|
(LF_ISSET(SEARCH_MSG) ? 0 : RE_C_SILENT)))
|
|
|
|
return (1);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Set the search direction. */
|
|
|
|
if (LF_ISSET(SEARCH_SET))
|
|
|
|
sp->searchdir = dir;
|
|
|
|
return (0);
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* Set the delimiter, and move forward to the terminating
|
|
|
|
* delimiter, handling escaped delimiters.
|
|
|
|
*
|
|
|
|
* QUOTING NOTE:
|
|
|
|
* Only discard an escape character if it escapes a delimiter.
|
|
|
|
*/
|
|
|
|
for (delim = *ptrn, p = t = ++ptrn;; *t++ = *p++) {
|
2001-03-31 15:37:44 +04:00
|
|
|
if (--plen == 0 || p[0] == delim) {
|
|
|
|
if (plen != 0)
|
1994-08-17 20:17:19 +04:00
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
2001-03-31 15:37:44 +04:00
|
|
|
if (plen > 1 && p[0] == '\\' && p[1] == delim) {
|
1994-08-17 20:17:19 +04:00
|
|
|
++p;
|
2001-03-31 15:37:44 +04:00
|
|
|
--plen;
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
if (epp != NULL)
|
|
|
|
*epp = p;
|
2001-03-31 15:37:44 +04:00
|
|
|
|
|
|
|
plen = t - ptrn;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile the RE. */
|
2001-03-31 15:37:44 +04:00
|
|
|
if (re_compile(sp, ptrn, plen, &sp->re, &sp->re_len, &sp->re_c,
|
1996-05-20 06:01:36 +04:00
|
|
|
RE_C_SEARCH |
|
|
|
|
(LF_ISSET(SEARCH_MSG) ? 0 : RE_C_SILENT) |
|
|
|
|
(LF_ISSET(SEARCH_TAG) ? RE_C_TAG : 0) |
|
|
|
|
(LF_ISSET(SEARCH_CSCOPE) ? RE_C_CSCOPE : 0)))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* Set the search direction. */
|
|
|
|
if (LF_ISSET(SEARCH_SET))
|
1994-08-17 20:17:19 +04:00
|
|
|
sp->searchdir = dir;
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
return (0);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* f_search --
|
|
|
|
* Do a forward search.
|
|
|
|
*
|
2001-03-31 15:37:44 +04:00
|
|
|
* PUBLIC: int f_search __P((SCR *,
|
|
|
|
* PUBLIC: MARK *, MARK *, char *, size_t, char **, u_int));
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
|
|
|
int
|
2001-03-31 15:37:44 +04:00
|
|
|
f_search(sp, fm, rm, ptrn, plen, eptrn, flags)
|
1994-08-17 20:17:19 +04:00
|
|
|
SCR *sp;
|
|
|
|
MARK *fm, *rm;
|
|
|
|
char *ptrn, **eptrn;
|
2001-03-31 15:37:44 +04:00
|
|
|
size_t plen;
|
1996-05-20 06:01:36 +04:00
|
|
|
u_int flags;
|
1994-08-17 20:17:19 +04:00
|
|
|
{
|
1996-05-20 06:01:36 +04:00
|
|
|
busy_t btype;
|
1994-08-17 20:17:19 +04:00
|
|
|
recno_t lno;
|
1996-05-20 06:01:36 +04:00
|
|
|
regmatch_t match[1];
|
1994-08-17 20:17:19 +04:00
|
|
|
size_t coff, len;
|
1996-05-20 06:01:36 +04:00
|
|
|
int cnt, eval, rval, wrapped;
|
1994-08-17 20:17:19 +04:00
|
|
|
char *l;
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifndef REG_STARTEND
|
|
|
|
char c;
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
if (search_init(sp, FORWARD, ptrn, plen, eptrn, flags))
|
1994-08-17 20:17:19 +04:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (LF_ISSET(SEARCH_FILE)) {
|
|
|
|
lno = 1;
|
|
|
|
coff = 0;
|
|
|
|
} else {
|
1996-05-20 06:01:36 +04:00
|
|
|
if (db_get(sp, fm->lno, DBG_FATAL, &l, &len))
|
1994-08-17 20:17:19 +04:00
|
|
|
return (1);
|
1996-05-20 06:01:36 +04:00
|
|
|
lno = fm->lno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If doing incremental search, start searching at the previous
|
|
|
|
* column, so that we search a minimal distance and still match
|
|
|
|
* special patterns, e.g., \< for beginning of a word.
|
|
|
|
*
|
|
|
|
* Otherwise, start searching immediately after the cursor. If
|
|
|
|
* at the end of the line, start searching on the next line.
|
|
|
|
* This is incompatible (read bug fix) with the historic vi --
|
|
|
|
* searches for the '$' pattern never moved forward, and the
|
|
|
|
* "-t foo" didn't work if the 'f' was the first character in
|
|
|
|
* the file.
|
|
|
|
*/
|
|
|
|
if (LF_ISSET(SEARCH_INCR)) {
|
|
|
|
if ((coff = fm->cno) != 0)
|
|
|
|
--coff;
|
|
|
|
} else if (fm->cno + 1 >= len) {
|
|
|
|
coff = 0;
|
|
|
|
lno = fm->lno + 1;
|
|
|
|
if (db_get(sp, lno, 0, &l, &len)) {
|
1994-08-17 20:17:19 +04:00
|
|
|
if (!O_ISSET(sp, O_WRAPSCAN)) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_EOF);
|
1994-08-17 20:17:19 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
lno = 1;
|
1996-05-20 06:01:36 +04:00
|
|
|
}
|
|
|
|
} else
|
1994-08-17 20:17:19 +04:00
|
|
|
coff = fm->cno + 1;
|
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
btype = BUSY_ON;
|
|
|
|
for (cnt = INTERRUPT_CHECK, rval = 1, wrapped = 0;; ++lno, coff = 0) {
|
|
|
|
if (cnt-- == 0) {
|
|
|
|
if (INTERRUPTED(sp))
|
|
|
|
break;
|
|
|
|
if (LF_ISSET(SEARCH_MSG)) {
|
|
|
|
search_busy(sp, btype);
|
|
|
|
btype = BUSY_UPDATE;
|
|
|
|
}
|
|
|
|
cnt = INTERRUPT_CHECK;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
if (wrapped && lno > fm->lno || db_get(sp, lno, 0, &l, &len)) {
|
1994-08-17 20:17:19 +04:00
|
|
|
if (wrapped) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_NOTFOUND);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!O_ISSET(sp, O_WRAPSCAN)) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_EOF);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
lno = 0;
|
|
|
|
wrapped = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If already at EOL, just keep going. */
|
1996-05-20 06:01:36 +04:00
|
|
|
if (len != 0 && coff == len)
|
1994-08-17 20:17:19 +04:00
|
|
|
continue;
|
|
|
|
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifdef REG_STARTEND
|
1994-08-17 20:17:19 +04:00
|
|
|
/* Set the termination. */
|
|
|
|
match[0].rm_so = coff;
|
|
|
|
match[0].rm_eo = len;
|
2000-03-17 05:23:17 +03:00
|
|
|
#else
|
|
|
|
c = l[len];
|
|
|
|
l[len] = '\0';
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
#if defined(DEBUG) && 0
|
|
|
|
TRACE(sp, "F search: %lu from %u to %u\n",
|
1996-05-20 06:01:36 +04:00
|
|
|
lno, coff, len != 0 ? len - 1 : len);
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif
|
|
|
|
/* Search the line. */
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifdef REG_STARTEND
|
1996-05-20 06:01:36 +04:00
|
|
|
eval = regexec(&sp->re_c, l, 1, match,
|
1994-08-17 20:17:19 +04:00
|
|
|
(match[0].rm_so == 0 ? 0 : REG_NOTBOL) | REG_STARTEND);
|
2000-03-17 05:23:17 +03:00
|
|
|
#else
|
|
|
|
eval = regexec(&sp->re_c, &l[coff], 1, match,
|
|
|
|
(match[0].rm_so == 0 ? 0 : REG_NOTBOL));
|
|
|
|
l[len] = c;
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
if (eval == REG_NOMATCH)
|
|
|
|
continue;
|
|
|
|
if (eval != 0) {
|
1996-05-20 06:01:36 +04:00
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
|
|
|
re_error(sp, eval, &sp->re_c);
|
|
|
|
else
|
|
|
|
(void)sp->gp->scr_bell(sp);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
/* Warn if the search wrapped. */
|
|
|
|
if (wrapped && LF_ISSET(SEARCH_WMSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_WRAP);
|
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
#if defined(DEBUG) && 0
|
2001-01-05 02:05:54 +03:00
|
|
|
TRACE(sp, "F search: %llu to %llu\n",
|
2001-03-31 15:37:44 +04:00
|
|
|
match[0].rm_so, match[0].rm_eo);
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif
|
1996-05-20 06:01:36 +04:00
|
|
|
rm->lno = lno;
|
|
|
|
rm->cno = match[0].rm_so;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a change command, it's possible to move beyond the end
|
|
|
|
* of a line. Historic vi generally got this wrong (e.g. try
|
|
|
|
* "c?$<cr>"). Not all that sure this gets it right, there
|
|
|
|
* are lots of strange cases.
|
|
|
|
*/
|
|
|
|
if (!LF_ISSET(SEARCH_EOL) && rm->cno >= len)
|
|
|
|
rm->cno = len != 0 ? len - 1 : 0;
|
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
rval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
|
|
|
search_busy(sp, BUSY_OFF);
|
1994-08-17 20:17:19 +04:00
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* b_search --
|
|
|
|
* Do a backward search.
|
|
|
|
*
|
2001-03-31 15:37:44 +04:00
|
|
|
* PUBLIC: int b_search __P((SCR *,
|
|
|
|
* PUBLIC: MARK *, MARK *, char *, size_t, char **, u_int));
|
1996-05-20 06:01:36 +04:00
|
|
|
*/
|
1994-08-17 20:17:19 +04:00
|
|
|
int
|
2001-03-31 15:37:44 +04:00
|
|
|
b_search(sp, fm, rm, ptrn, plen, eptrn, flags)
|
1994-08-17 20:17:19 +04:00
|
|
|
SCR *sp;
|
|
|
|
MARK *fm, *rm;
|
|
|
|
char *ptrn, **eptrn;
|
2001-03-31 15:37:44 +04:00
|
|
|
size_t plen;
|
1996-05-20 06:01:36 +04:00
|
|
|
u_int flags;
|
1994-08-17 20:17:19 +04:00
|
|
|
{
|
1996-05-20 06:01:36 +04:00
|
|
|
busy_t btype;
|
1994-08-17 20:17:19 +04:00
|
|
|
recno_t lno;
|
1996-05-20 06:01:36 +04:00
|
|
|
regmatch_t match[1];
|
|
|
|
size_t coff, last, len;
|
|
|
|
int cnt, eval, rval, wrapped;
|
1994-08-17 20:17:19 +04:00
|
|
|
char *l;
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifndef REG_STARTEND
|
|
|
|
char c;
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
if (search_init(sp, BACKWARD, ptrn, plen, eptrn, flags))
|
1994-08-17 20:17:19 +04:00
|
|
|
return (1);
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* If doing incremental search, set the "starting" position past the
|
|
|
|
* current column, so that we search a minimal distance and still
|
|
|
|
* match special patterns, e.g., \> for the end of a word. This is
|
|
|
|
* safe when the cursor is at the end of a line because we only use
|
|
|
|
* it for comparison with the location of the match.
|
|
|
|
*
|
|
|
|
* Otherwise, start searching immediately before the cursor. If in
|
|
|
|
* the first column, start search on the previous line.
|
|
|
|
*/
|
|
|
|
if (LF_ISSET(SEARCH_INCR)) {
|
|
|
|
lno = fm->lno;
|
|
|
|
coff = fm->cno + 1;
|
|
|
|
} else {
|
|
|
|
if (fm->cno == 0) {
|
|
|
|
if (fm->lno == 1 && !O_ISSET(sp, O_WRAPSCAN)) {
|
1994-08-17 20:17:19 +04:00
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_SOF);
|
1994-08-17 20:17:19 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
lno = fm->lno - 1;
|
1996-05-20 06:01:36 +04:00
|
|
|
} else
|
|
|
|
lno = fm->lno;
|
|
|
|
coff = fm->cno;
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
btype = BUSY_ON;
|
|
|
|
for (cnt = INTERRUPT_CHECK, rval = 1, wrapped = 0;; --lno, coff = 0) {
|
|
|
|
if (cnt-- == 0) {
|
|
|
|
if (INTERRUPTED(sp))
|
|
|
|
break;
|
|
|
|
if (LF_ISSET(SEARCH_MSG)) {
|
|
|
|
search_busy(sp, btype);
|
|
|
|
btype = BUSY_UPDATE;
|
|
|
|
}
|
|
|
|
cnt = INTERRUPT_CHECK;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
if (wrapped && lno < fm->lno || lno == 0) {
|
|
|
|
if (wrapped) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_NOTFOUND);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!O_ISSET(sp, O_WRAPSCAN)) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_SOF);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
if (db_last(sp, &lno))
|
|
|
|
break;
|
1994-08-17 20:17:19 +04:00
|
|
|
if (lno == 0) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_EMPTY);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++lno;
|
|
|
|
wrapped = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
if (db_get(sp, lno, 0, &l, &len))
|
|
|
|
break;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/* Set the termination. */
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifdef REG_STARTEND
|
1994-08-17 20:17:19 +04:00
|
|
|
match[0].rm_so = 0;
|
|
|
|
match[0].rm_eo = len;
|
2000-03-17 05:23:17 +03:00
|
|
|
#else
|
|
|
|
c = l[len];
|
|
|
|
l[len] = '\0';
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
#if defined(DEBUG) && 0
|
2001-01-05 02:05:54 +03:00
|
|
|
TRACE(sp, "B search: %lu from 0 to %llu\n", lno,
|
2001-03-31 15:37:44 +04:00
|
|
|
(long long) match[0].rm_eo);
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif
|
|
|
|
/* Search the line. */
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifdef REG_STARTEND
|
1996-05-20 06:01:36 +04:00
|
|
|
eval = regexec(&sp->re_c, l, 1, match,
|
1994-08-17 20:17:19 +04:00
|
|
|
(match[0].rm_eo == len ? 0 : REG_NOTEOL) | REG_STARTEND);
|
2000-03-17 05:23:17 +03:00
|
|
|
#else
|
|
|
|
eval = regexec(&sp->re_c, l, 1, match,
|
|
|
|
(match[0].rm_eo == len ? 0 : REG_NOTEOL));
|
|
|
|
l[len] = c;
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
if (eval == REG_NOMATCH)
|
|
|
|
continue;
|
|
|
|
if (eval != 0) {
|
1996-05-20 06:01:36 +04:00
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
|
|
|
re_error(sp, eval, &sp->re_c);
|
|
|
|
else
|
|
|
|
(void)sp->gp->scr_bell(sp);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a match starting past the cursor. */
|
|
|
|
if (coff != 0 && match[0].rm_so >= coff)
|
|
|
|
continue;
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
/* Warn if the search wrapped. */
|
|
|
|
if (wrapped && LF_ISSET(SEARCH_WMSG))
|
1996-05-20 06:01:36 +04:00
|
|
|
search_msg(sp, S_WRAP);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
#if defined(DEBUG) && 0
|
2001-01-05 02:05:54 +03:00
|
|
|
TRACE(sp, "B found: %llu to %llu\n",
|
2001-03-31 15:37:44 +04:00
|
|
|
(long long) match[0].rm_so, (long long) match[0].rm_eo);
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* We now have the first match on the line. Step through the
|
|
|
|
* line character by character until 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;
|
2000-03-17 05:23:17 +03:00
|
|
|
#ifdef REG_STARTEND
|
1996-05-20 06:01:36 +04:00
|
|
|
match[0].rm_eo = len;
|
|
|
|
eval = regexec(&sp->re_c, l, 1, match,
|
|
|
|
(match[0].rm_so == 0 ? 0 : REG_NOTBOL) |
|
|
|
|
REG_STARTEND);
|
2000-03-17 05:23:17 +03:00
|
|
|
#else
|
|
|
|
c = l[len];
|
|
|
|
l[len] = '\0';
|
|
|
|
eval = regexec(&sp->re_c, &l[last + 1], 1, match,
|
|
|
|
(match[0].rm_so == 0 ? 0 : REG_NOTBOL));
|
|
|
|
l[len] = c;
|
|
|
|
#endif
|
1996-05-20 06:01:36 +04:00
|
|
|
if (eval == REG_NOMATCH)
|
|
|
|
break;
|
|
|
|
if (eval != 0) {
|
|
|
|
if (LF_ISSET(SEARCH_MSG))
|
|
|
|
re_error(sp, eval, &sp->re_c);
|
|
|
|
else
|
|
|
|
(void)sp->gp->scr_bell(sp);
|
|
|
|
goto err;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
if (coff && match[0].rm_so >= coff)
|
|
|
|
break;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
rm->lno = lno;
|
|
|
|
|
|
|
|
/* See comment in f_search(). */
|
|
|
|
if (!LF_ISSET(SEARCH_EOL) && last >= len)
|
|
|
|
rm->cno = len != 0 ? len - 1 : 0;
|
|
|
|
else
|
|
|
|
rm->cno = last;
|
1994-08-17 20:17:19 +04:00
|
|
|
rval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
err: if (LF_ISSET(SEARCH_MSG))
|
|
|
|
search_busy(sp, BUSY_OFF);
|
1994-08-17 20:17:19 +04:00
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* search_msg --
|
|
|
|
* Display one of the search messages.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
static void
|
|
|
|
search_msg(sp, msg)
|
1994-08-17 20:17:19 +04:00
|
|
|
SCR *sp;
|
1996-05-20 06:01:36 +04:00
|
|
|
smsg_t msg;
|
1994-08-17 20:17:19 +04:00
|
|
|
{
|
1996-05-20 06:01:36 +04:00
|
|
|
switch (msg) {
|
|
|
|
case S_EMPTY:
|
|
|
|
msgq(sp, M_ERR, "072|File empty; nothing to search");
|
|
|
|
break;
|
|
|
|
case S_EOF:
|
|
|
|
msgq(sp, M_ERR,
|
|
|
|
"073|Reached end-of-file without finding the pattern");
|
|
|
|
break;
|
|
|
|
case S_NOPREV:
|
|
|
|
msgq(sp, M_ERR, "074|No previous search pattern");
|
|
|
|
break;
|
|
|
|
case S_NOTFOUND:
|
|
|
|
msgq(sp, M_ERR, "075|Pattern not found");
|
|
|
|
break;
|
|
|
|
case S_SOF:
|
|
|
|
msgq(sp, M_ERR,
|
|
|
|
"076|Reached top-of-file without finding the pattern");
|
|
|
|
break;
|
|
|
|
case S_WRAP:
|
|
|
|
msgq(sp, M_ERR, "077|Search wrapped");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* search_busy --
|
|
|
|
* Put up the busy searching message.
|
1994-08-17 20:17:19 +04:00
|
|
|
*
|
1996-05-20 06:01:36 +04:00
|
|
|
* PUBLIC: void search_busy __P((SCR *, busy_t));
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
|
|
|
void
|
1996-05-20 06:01:36 +04:00
|
|
|
search_busy(sp, btype)
|
1994-08-17 20:17:19 +04:00
|
|
|
SCR *sp;
|
1996-05-20 06:01:36 +04:00
|
|
|
busy_t btype;
|
1994-08-17 20:17:19 +04:00
|
|
|
{
|
1996-05-20 06:01:36 +04:00
|
|
|
sp->gp->scr_busy(sp, "078|Searching...", btype);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|