Skip to content

Commit

Permalink
- Be explicit on command line checking, instead of relying on patterns,
Browse files Browse the repository at this point in the history
  which may be NULL (e.g. -e '').
- let add_pattern() decide how to deal with empty patterns, don't do
  magic in read_patterns().

This unbreaks stuff like grep -e '', and makes grep -f <file> more
POSIX compliant. Semantics for grep -f /dev/null (or any other empty
file) may be questionable, but this case isn't specified by POSIX,
and matching nothing at all seems to be sane.

Thanks to otto@, who mentioned potential problems related to the
-x option with the first patch i sent.

ok jaredy@ (some time ago), otto@, millert@
  • Loading branch information
mkilian authored and howardjp committed Nov 28, 2009
1 parent 2771f28 commit d4d4f79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
10 changes: 8 additions & 2 deletions grep.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $OpenBSD: grep.1,v 1.31 2005/06/05 19:40:22 jmc Exp $
.\" $OpenBSD: grep.1,v 1.32 2006/03/07 12:23:50 jmc Exp $
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
Expand Down Expand Up @@ -170,7 +170,11 @@ to behave as
.It Fl f Ar file
Read one or more newline separated patterns from
.Ar file .
Empty pattern lines match every input line.
Newlines are not considered part of a pattern.
If
.Ar file
is empty, nothing is matched.
.It Fl G
Interpret
.Ar pattern
Expand Down Expand Up @@ -364,7 +368,9 @@ specification.
.Pp
The flags
.Op Fl AaBbCGHhILoPRSUVwZ
are extensions to that specification.
are extensions to that specification, and the behaviour of the
.Fl f
flag when used with an empty pattern file is left undefined.
.Pp
All long options are provided for compatibility with
GNU versions of this utility.
Expand Down
27 changes: 9 additions & 18 deletions grep.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: grep.c,v 1.36 2006/09/26 15:55:17 jaredy Exp $ */
/* $OpenBSD: grep.c,v 1.37 2006/11/02 18:00:03 ray Exp $ */

/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
Expand Down Expand Up @@ -221,23 +221,11 @@ read_patterns(const char *fn)
FILE *f;
char *line;
size_t len;
int nl;

if ((f = fopen(fn, "r")) == NULL)
err(2, "%s", fn);
nl = 0;
while ((line = fgetln(f, &len)) != NULL) {
if (*line == '\n') {
++nl;
continue;
}
if (nl) {
matchall = 1;
break;
}
nl = 0;
add_pattern(line, len);
}
while ((line = fgetln(f, &len)) != NULL)
add_pattern(line, *line == '\n' ? 0 : len);
if (ferror(f))
err(2, "%s", fn);
fclose(f);
Expand All @@ -246,7 +234,7 @@ read_patterns(const char *fn)
int
main(int argc, char *argv[])
{
int c, lastc, prevoptind, newarg, i;
int c, lastc, prevoptind, newarg, i, needpattern;
struct patfile *patfile, *pf_next;
long l;
char *ep;
Expand Down Expand Up @@ -283,6 +271,7 @@ main(int argc, char *argv[])
lastc = '\0';
newarg = 1;
prevoptind = 1;
needpattern = 1;
while ((c = getopt_long(argc, argv, optstr,
long_options, NULL)) != -1) {
switch (c) {
Expand Down Expand Up @@ -372,11 +361,13 @@ main(int argc, char *argv[])
break;
case 'e':
add_patterns(optarg);
needpattern = 0;
break;
case 'f':
patfile = grep_malloc(sizeof(*patfile));
patfile->pf_file = optarg;
SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
needpattern = 0;
break;
case 'h':
oflag = 0;
Expand Down Expand Up @@ -448,10 +439,10 @@ main(int argc, char *argv[])
free(patfile);
}

if (argc == 0 && patterns == 0)
if (argc == 0 && needpattern)
usage();

if (patterns == 0) {
if (argc != 0 && needpattern) {
add_patterns(*argv);
--argc;
++argv;
Expand Down

0 comments on commit d4d4f79

Please sign in to comment.