bsdgrep(1): Slooowly peel away the chunky onion

(or peel off the band-aid, whatever floats your boat)

This addresses two separate issues:

1.) Nothing within bsdgrep actually knew whether it cared about line numbers
  or not.

2.) The file layer knew nothing about the context in which it was being
  called.

#1 is only important when we're *not* processing line-by-line. #2 is
debatably a good idea; the parsing context is only handy because that's
where we store current offset information and, as of this commit, whether or
not it needs to be line-aware.
This commit is contained in:
kevans 2018-06-08 01:25:07 +00:00
parent 511cde02d7
commit cc9b40180f
3 changed files with 27 additions and 23 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -56,20 +56,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
*/
@ -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;