summaryrefslogtreecommitdiff
path: root/usr.bin/grep
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2018-06-08 01:25:07 +0000
committerKyle Evans <kevans@FreeBSD.org>2018-06-08 01:25:07 +0000
commitbd60b9b4993e823fc857f03f7f11c9ad1204ba2e (patch)
tree95fcab1737e86d9c9a844ae74b669a291e3b17b5 /usr.bin/grep
parentc9ca1a70cce29057168692f5a3bdeedfee4faacc (diff)
downloadsrc-test2-bd60b9b4993e823fc857f03f7f11c9ad1204ba2e.tar.gz
src-test2-bd60b9b4993e823fc857f03f7f11c9ad1204ba2e.zip
Notes
Diffstat (limited to 'usr.bin/grep')
-rw-r--r--usr.bin/grep/file.c12
-rw-r--r--usr.bin/grep/grep.h17
-rw-r--r--usr.bin/grep/util.c21
3 files changed, 27 insertions, 23 deletions
diff --git a/usr.bin/grep/file.c b/usr.bin/grep/file.c
index 560a568879b6..4653a45941d4 100644
--- a/usr.bin/grep/file.c
+++ b/usr.bin/grep/file.c
@@ -95,7 +95,7 @@ grep_lnbufgrow(size_t newlen)
}
char *
-grep_fgetln(struct file *f, size_t *lenp)
+grep_fgetln(struct file *f, struct parsec *pc)
{
unsigned char *p;
char *ret;
@@ -109,18 +109,18 @@ grep_fgetln(struct file *f, size_t *lenp)
if (bufrem == 0) {
/* Return zero length to indicate EOF */
- *lenp = 0;
+ pc->ln.len= 0;
return (bufpos);
}
- /* Look for a newline in the remaining part of the buffer */
+ /* Look for a newline in the remaining part of the [6rbuffer */
if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
++p; /* advance over newline */
ret = bufpos;
len = p - bufpos;
bufrem -= len;
bufpos = p;
- *lenp = len;
+ pc->ln.len = len;
return (ret);
}
@@ -155,11 +155,11 @@ grep_fgetln(struct file *f, size_t *lenp)
bufpos = p;
break;
}
- *lenp = len;
+ pc->ln.len = len;
return (lnbuf);
error:
- *lenp = 0;
+ pc->ln.len = 0;
return (NULL);
}
diff --git a/usr.bin/grep/grep.h b/usr.bin/grep/grep.h
index b2c16e8767d7..6d1e87ae4d7c 100644
--- a/usr.bin/grep/grep.h
+++ b/usr.bin/grep/grep.h
@@ -97,6 +97,21 @@ struct epat {
int mode;
};
+/*
+ * Parsing context; used to hold things like matches made and
+ * other useful bits
+ */
+struct parsec {
+ regmatch_t matches[MAX_MATCHES]; /* Matches made */
+ /* XXX TODO: This should be a chunk, not a line */
+ struct str ln; /* Current line */
+ size_t lnstart; /* Position in line */
+ size_t matchidx; /* Latest match index */
+ int printed; /* Metadata printed? */
+ bool binary; /* Binary file? */
+ bool cntlines; /* Count lines? */
+};
+
/* Flags passed to regcomp() and regexec() */
extern int cflags, eflags;
@@ -141,4 +156,4 @@ void clearqueue(void);
/* file.c */
void grep_close(struct file *f);
struct file *grep_open(const char *path);
-char *grep_fgetln(struct file *f, size_t *len);
+char *grep_fgetln(struct file *f, struct parsec *pc);
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index 5278859c73e8..c92d08ef69a5 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -57,20 +57,6 @@ __FBSDID("$FreeBSD$");
static bool first_match = true;
/*
- * Parsing context; used to hold things like matches made and
- * other useful bits
- */
-struct parsec {
- regmatch_t matches[MAX_MATCHES]; /* Matches made */
- /* XXX TODO: This should be a chunk, not a line */
- struct str ln; /* Current line */
- size_t lnstart; /* Position in line */
- size_t matchidx; /* Latest match index */
- int printed; /* Metadata printed? */
- bool binary; /* Binary file? */
-};
-
-/*
* Match printing context
*/
struct mprintc {
@@ -276,7 +262,7 @@ procmatches(struct mprintc *mc, struct parsec *pc, bool matched)
if (mflag) {
/* XXX TODO: Decrement by number of matched lines */
mcount -= 1;
- if (mflag && mcount <= 0)
+ if (mcount <= 0)
return (false);
}
} else if (mc->doctx)
@@ -327,6 +313,7 @@ procfile(const char *fn)
pc.ln.boff = 0;
pc.ln.off = -1;
pc.binary = f->binary;
+ pc.cntlines = false;
memset(&mc, 0, sizeof(mc));
mc.printmatch = true;
if ((pc.binary && binbehave == BINFILE_BIN) || cflag || qflag ||
@@ -334,6 +321,8 @@ procfile(const char *fn)
mc.printmatch = false;
if (mc.printmatch && (Aflag != 0 || Bflag != 0))
mc.doctx = true;
+ if (mc.printmatch && (Aflag != 0 || Bflag != 0 || mflag || nflag))
+ pc.cntlines = true;
mcount = mlimit;
for (lines = 0; lines == 0 || !(lflag || qflag); ) {
@@ -349,7 +338,7 @@ procfile(const char *fn)
pc.ln.boff = 0;
pc.ln.off += pc.ln.len + 1;
/* XXX TODO: Grab a chunk */
- if ((pc.ln.dat = grep_fgetln(f, &pc.ln.len)) == NULL ||
+ if ((pc.ln.dat = grep_fgetln(f, &pc)) == NULL ||
pc.ln.len == 0)
break;