diff options
author | Kyle Evans <kevans@FreeBSD.org> | 2019-09-25 17:14:43 +0000 |
---|---|---|
committer | Kyle Evans <kevans@FreeBSD.org> | 2019-09-25 17:14:43 +0000 |
commit | 38325e2ab8f0c7823e31df13bfc52e38e7f6d616 (patch) | |
tree | c072427d77503a8c6e447ee6e9de504e85d978ff /usr.bin/grep/util.c | |
parent | 2b93f779d22332da86a5398e59b57a5e27b9ffb6 (diff) | |
download | src-38325e2ab8f0c7823e31df13bfc52e38e7f6d616.tar.gz src-38325e2ab8f0c7823e31df13bfc52e38e7f6d616.zip |
bsdgrep(1): various fixes of empty pattern/exit code/-c behavior
When an empty pattern is encountered in the pattern list, I had previously
broken bsdgrep to count that as a "match all" and ignore any other patterns
in the list. This commit rectifies that mistake, among others:
- The -v flag semantics were not quite right; lines matched should have been
counted differently based on whether the -v flag was set or not. procline
now definitively returns whether it's matched or not, and interpreting
that result has been kicked up a level.
- Empty patterns with the -x flag was broken similarly to empty patterns
with the -w flag. The former is a whole-line match and should be more
strict, only matching blank lines. No -x and no -w will will match the
empty string at the beginning of each line.
- The exit code with -L was broken, w.r.t. modern grep. Modern grap will
exit(0) if any file that didn't match was output, so our interpretation
was simply backwards. The new interpretation makes sense to me.
Tests updated and added to try and catch some of this.
This misbehavior was found by autoconf while fixing ports found in PR 229925
expecting either a more sane or a more GNU-like sed.
MFC after: 1 week
Notes
Notes:
svn path=/head/; revision=352691
Diffstat (limited to 'usr.bin/grep/util.c')
-rw-r--r-- | usr.bin/grep/util.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 07d9b40cbdcd..33afe4d6b030 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -210,7 +210,7 @@ procmatch_match(struct mprintc *mc, struct parsec *pc) while (pc->matchidx >= MAX_MATCHES) { /* Reset matchidx and try again */ pc->matchidx = 0; - if (procline(pc)) + if (procline(pc) == !vflag) printline(pc, ':'); else break; @@ -355,7 +355,7 @@ procfile(const char *fn) return (0); } - line_matched = procline(&pc); + line_matched = procline(&pc) == !vflag; if (line_matched) ++lines; @@ -469,17 +469,32 @@ procline(struct parsec *pc) matchidx = pc->matchidx; - /* Special case: empty pattern with -w flag, check first character */ - if (matchall && wflag) { + /* + * With matchall (empty pattern), we can try to take some shortcuts. + * Emtpy patterns trivially match every line except in the -w and -x + * cases. For -w (whole-word) cases, we only match if the first + * character isn't a word-character. For -x (whole-line) cases, we only + * match if the line is empty. + */ + if (matchall) { if (pc->ln.len == 0) return (true); - wend = L' '; - if (sscanf(&pc->ln.dat[0], "%lc", &wend) != 1 || iswword(wend)) - return (false); - else + if (wflag) { + wend = L' '; + if (sscanf(&pc->ln.dat[0], "%lc", &wend) == 1 && + !iswword(wend)) + return (true); + } else if (!xflag) return (true); - } else if (matchall) - return (true); + + /* + * If we don't have any other patterns, we really don't match. + * If we do have other patterns, we must fall through and check + * them. + */ + if (patterns == 0) + return (false); + } matched = false; st = pc->lnstart; @@ -609,8 +624,6 @@ procline(struct parsec *pc) /* Reflect the new matchidx in the context */ pc->matchidx = matchidx; - if (vflag) - matched = !matched; return matched; } |