summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/grep/util.c')
-rw-r--r--usr.bin/grep/util.c144
1 files changed, 69 insertions, 75 deletions
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index 780f0fc..e209968 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -49,6 +49,7 @@ __FBSDID("$FreeBSD$");
#include <wchar.h>
#include <wctype.h>
+#include "fastmatch.h"
#include "grep.h"
static int linesqueued;
@@ -232,13 +233,8 @@ procfile(const char *fn)
linesqueued++;
}
c += t;
-
- /* Count the matches if we have a match limit */
- if (mflag) {
- mcount -= t;
- if (mcount <= 0)
- break;
- }
+ if (mflag && mcount < 0)
+ break;
}
if (Bflag > 0)
clearqueue();
@@ -280,79 +276,77 @@ procline(struct str *l, int nottext)
unsigned int i;
int c = 0, m = 0, r = 0;
- if (!matchall) {
- /* Loop to process the whole line */
- while (st <= l->len) {
- pmatch.rm_so = st;
- pmatch.rm_eo = l->len;
+ /* Loop to process the whole line */
+ while (st <= l->len) {
+ pmatch.rm_so = st;
+ pmatch.rm_eo = l->len;
- /* Loop to compare with all the patterns */
- for (i = 0; i < patterns; i++) {
-/*
- * XXX: grep_search() is a workaround for speed up and should be
- * removed in the future. See fastgrep.c.
- */
- if (fg_pattern[i].pattern)
- r = grep_search(&fg_pattern[i],
- (unsigned char *)l->dat,
- l->len, &pmatch);
- else
- r = regexec(&r_pattern[i], l->dat, 1,
- &pmatch, eflags);
- r = (r == 0) ? 0 : REG_NOMATCH;
- st = (cflags & REG_NOSUB)
- ? (size_t)l->len
- : (size_t)pmatch.rm_eo;
- if (r == REG_NOMATCH)
- continue;
- /* Check for full match */
- if (r == 0 && xflag)
- if (pmatch.rm_so != 0 ||
- (size_t)pmatch.rm_eo != l->len)
- r = REG_NOMATCH;
- /* Check for whole word match */
- if (r == 0 && (wflag || fg_pattern[i].word)) {
- wint_t wbegin, wend;
-
- wbegin = wend = L' ';
- if (pmatch.rm_so != 0 &&
- sscanf(&l->dat[pmatch.rm_so - 1],
- "%lc", &wbegin) != 1)
- r = REG_NOMATCH;
- else if ((size_t)pmatch.rm_eo !=
- l->len &&
- sscanf(&l->dat[pmatch.rm_eo],
- "%lc", &wend) != 1)
- r = REG_NOMATCH;
- else if (iswword(wbegin) ||
- iswword(wend))
- r = REG_NOMATCH;
- }
- if (r == 0) {
- if (m == 0)
- c++;
- if (m < MAX_LINE_MATCHES)
- matches[m++] = pmatch;
- /* matches - skip further patterns */
- if ((color == NULL && !oflag) ||
- qflag || lflag)
- break;
- }
+ /* Loop to compare with all the patterns */
+ for (i = 0; i < patterns; i++) {
+ if (fg_pattern[i].pattern)
+ r = fastexec(&fg_pattern[i],
+ l->dat, 1, &pmatch, eflags);
+ else
+ r = regexec(&r_pattern[i], l->dat, 1,
+ &pmatch, eflags);
+ r = (r == 0) ? 0 : REG_NOMATCH;
+ st = (cflags & REG_NOSUB)
+ ? (size_t)l->len
+ : (size_t)pmatch.rm_eo;
+ if (r == REG_NOMATCH)
+ continue;
+ /* Check for full match */
+ if (r == 0 && xflag)
+ if (pmatch.rm_so != 0 ||
+ (size_t)pmatch.rm_eo != l->len)
+ r = REG_NOMATCH;
+ /* Check for whole word match */
+ if (r == 0 && (wflag || fg_pattern[i].word)) {
+ wint_t wbegin, wend;
+
+ wbegin = wend = L' ';
+ if (pmatch.rm_so != 0 &&
+ sscanf(&l->dat[pmatch.rm_so - 1],
+ "%lc", &wbegin) != 1)
+ r = REG_NOMATCH;
+ else if ((size_t)pmatch.rm_eo !=
+ l->len &&
+ sscanf(&l->dat[pmatch.rm_eo],
+ "%lc", &wend) != 1)
+ r = REG_NOMATCH;
+ else if (iswword(wbegin) ||
+ iswword(wend))
+ r = REG_NOMATCH;
}
-
- if (vflag) {
- c = !c;
- break;
+ if (r == 0) {
+ if (m == 0)
+ c++;
+ if (m < MAX_LINE_MATCHES)
+ matches[m++] = pmatch;
+ /* matches - skip further patterns */
+ if ((color == NULL && !oflag) ||
+ qflag || lflag)
+ break;
}
- /* One pass if we are not recording matches */
- if ((color == NULL && !oflag) || qflag || lflag)
- break;
+ }
- if (st == (size_t)pmatch.rm_so)
- break; /* No matches */
+ if (vflag) {
+ c = !c;
+ break;
}
- } else
- c = !vflag;
+
+ /* One pass if we are not recording matches */
+ if ((color == NULL && !oflag) || qflag || lflag)
+ break;
+
+ if (st == (size_t)pmatch.rm_so)
+ break; /* No matches */
+ }
+
+
+ /* Count the matches if we have a match limit */
+ if (mflag)
+ mcount -= c;
if (c && binbehave == BINFILE_BIN && nottext)
return (c); /* Binary file */
OpenPOWER on IntegriCloud