Skip to content

Commit cc9b401

Browse files
committed
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.
1 parent 511cde0 commit cc9b401

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

usr.bin/grep/file.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ grep_lnbufgrow(size_t newlen)
9595
}
9696

9797
char *
98-
grep_fgetln(struct file *f, size_t *lenp)
98+
grep_fgetln(struct file *f, struct parsec *pc)
9999
{
100100
unsigned char *p;
101101
char *ret;
@@ -109,18 +109,18 @@ grep_fgetln(struct file *f, size_t *lenp)
109109

110110
if (bufrem == 0) {
111111
/* Return zero length to indicate EOF */
112-
*lenp = 0;
112+
pc->ln.len= 0;
113113
return (bufpos);
114114
}
115115

116-
/* Look for a newline in the remaining part of the buffer */
116+
/* Look for a newline in the remaining part of the [6rbuffer */
117117
if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
118118
++p; /* advance over newline */
119119
ret = bufpos;
120120
len = p - bufpos;
121121
bufrem -= len;
122122
bufpos = p;
123-
*lenp = len;
123+
pc->ln.len = len;
124124
return (ret);
125125
}
126126

@@ -155,11 +155,11 @@ grep_fgetln(struct file *f, size_t *lenp)
155155
bufpos = p;
156156
break;
157157
}
158-
*lenp = len;
158+
pc->ln.len = len;
159159
return (lnbuf);
160160

161161
error:
162-
*lenp = 0;
162+
pc->ln.len = 0;
163163
return (NULL);
164164
}
165165

usr.bin/grep/grep.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ struct epat {
9797
int mode;
9898
};
9999

100+
/*
101+
* Parsing context; used to hold things like matches made and
102+
* other useful bits
103+
*/
104+
struct parsec {
105+
regmatch_t matches[MAX_MATCHES]; /* Matches made */
106+
/* XXX TODO: This should be a chunk, not a line */
107+
struct str ln; /* Current line */
108+
size_t lnstart; /* Position in line */
109+
size_t matchidx; /* Latest match index */
110+
int printed; /* Metadata printed? */
111+
bool binary; /* Binary file? */
112+
bool cntlines; /* Count lines? */
113+
};
114+
100115
/* Flags passed to regcomp() and regexec() */
101116
extern int cflags, eflags;
102117

@@ -141,4 +156,4 @@ void clearqueue(void);
141156
/* file.c */
142157
void grep_close(struct file *f);
143158
struct file *grep_open(const char *path);
144-
char *grep_fgetln(struct file *f, size_t *len);
159+
char *grep_fgetln(struct file *f, struct parsec *pc);

usr.bin/grep/util.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ __FBSDID("$FreeBSD$");
5656

5757
static bool first_match = true;
5858

59-
/*
60-
* Parsing context; used to hold things like matches made and
61-
* other useful bits
62-
*/
63-
struct parsec {
64-
regmatch_t matches[MAX_MATCHES]; /* Matches made */
65-
/* XXX TODO: This should be a chunk, not a line */
66-
struct str ln; /* Current line */
67-
size_t lnstart; /* Position in line */
68-
size_t matchidx; /* Latest match index */
69-
int printed; /* Metadata printed? */
70-
bool binary; /* Binary file? */
71-
};
72-
7359
/*
7460
* Match printing context
7561
*/
@@ -276,7 +262,7 @@ procmatches(struct mprintc *mc, struct parsec *pc, bool matched)
276262
if (mflag) {
277263
/* XXX TODO: Decrement by number of matched lines */
278264
mcount -= 1;
279-
if (mflag && mcount <= 0)
265+
if (mcount <= 0)
280266
return (false);
281267
}
282268
} else if (mc->doctx)
@@ -327,13 +313,16 @@ procfile(const char *fn)
327313
pc.ln.boff = 0;
328314
pc.ln.off = -1;
329315
pc.binary = f->binary;
316+
pc.cntlines = false;
330317
memset(&mc, 0, sizeof(mc));
331318
mc.printmatch = true;
332319
if ((pc.binary && binbehave == BINFILE_BIN) || cflag || qflag ||
333320
lflag || Lflag)
334321
mc.printmatch = false;
335322
if (mc.printmatch && (Aflag != 0 || Bflag != 0))
336323
mc.doctx = true;
324+
if (mc.printmatch && (Aflag != 0 || Bflag != 0 || mflag || nflag))
325+
pc.cntlines = true;
337326
mcount = mlimit;
338327

339328
for (lines = 0; lines == 0 || !(lflag || qflag); ) {
@@ -349,7 +338,7 @@ procfile(const char *fn)
349338
pc.ln.boff = 0;
350339
pc.ln.off += pc.ln.len + 1;
351340
/* XXX TODO: Grab a chunk */
352-
if ((pc.ln.dat = grep_fgetln(f, &pc.ln.len)) == NULL ||
341+
if ((pc.ln.dat = grep_fgetln(f, &pc)) == NULL ||
353342
pc.ln.len == 0)
354343
break;
355344

0 commit comments

Comments
 (0)