Skip to content

Commit

Permalink
bsdgrep(1): Slooowly peel away the chunky onion
Browse files Browse the repository at this point in the history
(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.
  • Loading branch information
kevans91 committed Jun 8, 2018
1 parent 511cde0 commit cc9b401
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
12 changes: 6 additions & 6 deletions usr.bin/grep/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

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

Expand Down
17 changes: 16 additions & 1 deletion usr.bin/grep/grep.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
21 changes: 5 additions & 16 deletions usr.bin/grep/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -327,13 +313,16 @@ 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 ||
lflag || Lflag)
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); ) {
Expand All @@ -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;

Expand Down

0 comments on commit cc9b401

Please sign in to comment.