Skip to content

Commit

Permalink
13037 grep could support -L
Browse files Browse the repository at this point in the history
Reviewed by: Andy Fiddaman <andy@omniosce.org>
Reviewed by: Toomas Soome <tsoome@me.com>
Approved by: Gordon Ross <gordon.w.ross@gmail.com>
  • Loading branch information
rmustacc committed Aug 25, 2020
1 parent f415aa8 commit 2e5ac46
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 10 deletions.
41 changes: 36 additions & 5 deletions usr/src/cmd/grep/grep.c
Expand Up @@ -39,6 +39,7 @@
* Copyright 2018 RackTop Systems.
* Copyright 2018 Nexenta Systems, Inc.
* Copyright 2013 Damian Bogel. All rights reserved.
* Copyright 2020 Oxide Computer Company
*/

#include <string.h>
Expand Down Expand Up @@ -94,6 +95,7 @@ static uchar_t iflag; /* Case insensitve matching */
static uchar_t Hflag; /* Precede lines by file name */
static uchar_t hflag; /* Suppress printing of filename */
static uchar_t lflag; /* Print file names of matches */
static uchar_t Lflag; /* Print file names of non-matches */
static uchar_t nflag; /* Precede lines by line number */
static uchar_t rflag; /* Search directories recursively */
static uchar_t bflag; /* Precede matches by block number */
Expand Down Expand Up @@ -201,7 +203,8 @@ main(int argc, char **argv)
}
}

while ((c = getopt(argc, argv, "vwchHilnrbse:f:qxEFIRA:B:C:")) != EOF) {
while ((c = getopt(argc, argv, "vwchHilLnrbse:f:qxEFIRA:B:C:")) !=
EOF) {
unsigned long tval;
switch (c) {
case 'v': /* POSIX: negate matches */
Expand All @@ -217,8 +220,17 @@ main(int argc, char **argv)
regflags |= REG_ICASE;
break;

/*
* The last of -l and -L are honored.
*/
case 'l': /* POSIX: Write filenames only */
lflag++;
Lflag = 0;
break;

case 'L': /* Write non-matching filenames */
Lflag++;
lflag = 0;
break;

case 'n': /* POSIX: Write line numbers */
Expand Down Expand Up @@ -399,18 +411,20 @@ main(int argc, char **argv)
usage();

/*
* -l overrides -H like in GNU grep
* -l or -L overrides -H like in GNU grep
*/
if (lflag)
if (lflag || Lflag)
Hflag = 0;

/*
* -c, -l and -q flags are mutually exclusive
* We have -c override -l like in Solaris.
* -q overrides -l & -c programmatically in grep() function.
*/
if (cflag && lflag)
if (cflag && (lflag || Lflag)) {
lflag = 0;
Lflag = 0;
}

argv += optind - 1;
argc -= optind - 1;
Expand Down Expand Up @@ -1446,6 +1460,9 @@ grep(int fd, const char *fn)
(void) printf("%s\n", fn);
goto out;
}
if (Lflag) {
goto out;
}
if (!cflag) {
if (Hflag || outfn) {
(void) printf("%s%c", fn, separate);
Expand Down Expand Up @@ -1518,6 +1535,20 @@ grep(int fd, const char *fn)
(void) printf("%lld\n", matches);
}
}

/*
* -L tells us to print the filename only when it doesn't match. So we
* run through the normal operationa and then invert it.
*/
if (Lflag) {
if (matches == 0) {
(void) printf("%s\n", fn);
matches = 1;
} else {
matches = 0;
}
}

return (matches != 0);
}

Expand All @@ -1530,7 +1561,7 @@ usage(void)
(void) fprintf(stderr, gettext("usage: %5s"), cmdname);
if (!egrep && !fgrep)
(void) fprintf(stderr, gettext(" [-E|-F]"));
(void) fprintf(stderr, gettext(" [-bchHilnqrRsvx] [-A num] [-B num] "
(void) fprintf(stderr, gettext(" [-bchHilLnqrRsvx] [-A num] [-B num] "
"[-C num|-num]\n [-e pattern_list]... "
"[-f pattern_file]... [pattern_list] [file]...\n"));
exit(2);
Expand Down
31 changes: 26 additions & 5 deletions usr/src/man/man1/grep.1
Expand Up @@ -44,8 +44,9 @@
.\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
.\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved
.\" Copyright 2018 Nexenta Systems, Inc.
.\" Copyright 2020 Oxide Computer Company
.\"
.Dd February 10, 2018
.Dd August 02, 2020
.Dt GREP 1
.Os
.Sh NAME
Expand All @@ -56,7 +57,7 @@
.Sh SYNOPSIS
.Nm grep
.Op Fl E Ns | Ns Fl F
.Op Fl bchHilnrRsqvwx
.Op Fl bchHilLnrRsqvwx
.Op Fl A Ar num
.Op Fl B Ar num
.Op Fl C Ar num Ns | Ns Fl Ns Ar num
Expand Down Expand Up @@ -107,7 +108,9 @@ delimiter line.
.It Fl c
Prints only a count of the lines that contain the pattern.
Overrides
.Fl l .
.Fl l
and
.Fl L .
.It Fl C Ar num Ns \&, Fl Ns Ar num
Prints
.Ar num
Expand Down Expand Up @@ -168,6 +171,23 @@ Ignores upper/lower case distinction during comparisons.
Prints only the names of files with matching lines, separated by NEWLINE
characters.
Does not repeat the names of files when the pattern is found more than once.
If both
.Fl l
and
.Fl L
are specified, only the last specified takes effect.
Overrides
.Fl H .
.It Fl L
The opposite of the
.Fl l
flag.
Prints only the names of files without matching lines.
If both
.Fl l
and
.Fl L
are specified, only the last specified takes effect.
Overrides
.Fl H .
.It Fl n
Expand All @@ -177,9 +197,10 @@ Quiet.
Does not write anything to the standard output, regardless of matching lines.
Exits with zero status if an input line is selected.
Overrides
.Fl c
.Fl c ,
.Fl l ,
and
.Fl l .
.Fl L .
.It Fl r
Read all files under each directory, recursively.
Follow symbolic links on the command line, but skip symlinks that are
Expand Down
22 changes: 22 additions & 0 deletions usr/src/pkg/manifests/system-test-utiltest.mf
Expand Up @@ -1431,6 +1431,28 @@ file path=opt/util-tests/tests/files/grep/gout.t4.0 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t4.1 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t5.0 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t5.1 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.0 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.1 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.10 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.11 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.12 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.13 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.14 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.15 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.16 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.17 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.18 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.19 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.2 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.3 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.4 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.5 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.6 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.7 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.8 mode=0444
file path=opt/util-tests/tests/files/grep/gout.t6.9 mode=0444
file path=opt/util-tests/tests/files/grep/test.lL.0 mode=0444
file path=opt/util-tests/tests/files/grep/test.lL.1 mode=0444
file path=opt/util-tests/tests/files/grep/test0 mode=0444
file path=opt/util-tests/tests/files/grep/test1 mode=0444
file path=opt/util-tests/tests/files/grep/test2 mode=0444
Expand Down
22 changes: 22 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
Expand Up @@ -30,6 +30,8 @@ PROGS = test0 \
test5 \
test6 \
test7 \
test.lL.0 \
test.lL.1 \
gout.t1.0 \
gout.t1.1 \
gout.t1.2 \
Expand Down Expand Up @@ -101,6 +103,26 @@ PROGS = test0 \
gout.t4.1 \
gout.t5.0 \
gout.t5.1 \
gout.t6.0 \
gout.t6.1 \
gout.t6.2 \
gout.t6.3 \
gout.t6.4 \
gout.t6.5 \
gout.t6.6 \
gout.t6.7 \
gout.t6.8 \
gout.t6.9 \
gout.t6.10 \
gout.t6.11 \
gout.t6.12 \
gout.t6.13 \
gout.t6.14 \
gout.t6.15 \
gout.t6.16 \
gout.t6.17 \
gout.t6.18 \
gout.t6.19 \
testnl

CMDS = $(PROGS:%=$(TESTDIR)/%)
Expand Down
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.0
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.1
@@ -0,0 +1 @@
test.lL.1
2 changes: 2 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.10
@@ -0,0 +1,2 @@
test.lL.0:1
test.lL.1:0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.11
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.12
@@ -0,0 +1 @@
test.lL.1
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.13
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.14
@@ -0,0 +1 @@
test.lL.1
2 changes: 2 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.15
@@ -0,0 +1,2 @@
test.lL.0
test.lL.1
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.16
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.17
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.18
@@ -0,0 +1 @@
test.lL.1
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.19
@@ -0,0 +1 @@
test.lL.1
2 changes: 2 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.2
@@ -0,0 +1,2 @@
test.lL.0
test.lL.1
2 changes: 2 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.3
@@ -0,0 +1,2 @@
test.lL.0
test.lL.1
2 changes: 2 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.4
@@ -0,0 +1,2 @@
test.lL.0
test.lL.1
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.5
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.6
@@ -0,0 +1 @@
test.lL.1
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.7
@@ -0,0 +1 @@
test.lL.0
1 change: 1 addition & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.8
@@ -0,0 +1 @@
test.lL.1
2 changes: 2 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/gout.t6.9
@@ -0,0 +1,2 @@
test.lL.0:1
test.lL.1:0
3 changes: 3 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/test.lL.0
@@ -0,0 +1,3 @@
foo
bar
baz
3 changes: 3 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/files/test.lL.1
@@ -0,0 +1,3 @@
FoO
bAr
BAZ
36 changes: 36 additions & 0 deletions usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
Expand Up @@ -166,6 +166,42 @@ FLAGS="-r
run_tests 0 t5 a /tmp/test0
rm -rf /tmp/test0

#
# Test Group 6: Test -l and -L which are supposed to match the file or
# not. We break this into cases that should always pass and those that
# should fail. The first group should always pass.
#
FLAGS="-l
-L
-il
-vl
-vil
-hl
-hL
-Hl
-HL
-cl
-cL
-nl
-nL
-l -A5 -B5
-L -C5
-nHvl
-l -L -l
-L -l
-l -L
-L -l -L"
run_tests 0 t6 foo test.lL.0 test.lL.1

#
# Test Group 7: -l and -L variants that should cause us to not match or
# fail for another reason.
#
FLAGS="-vL
-viL
-nHvL"
run_tests 1 t7 foo test.lL.0 test.lL.1

#
# Clean up temporary files.
#
Expand Down

0 comments on commit 2e5ac46

Please sign in to comment.