summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Mustacchi <rm@fingolfin.org>2020-08-02 17:16:51 -0700
committerRobert Mustacchi <rm@fingolfin.org>2020-08-25 16:00:10 -0700
commit8ccd021704a4fe1b4022c110d3dd497f8cd907d5 (patch)
treea5934e2086affa2a31d2e241a91f0c46ba9a332f
parent2e5ac464a5683c5bf11439ccad74c9f279051820 (diff)
downloadillumos-joyent-8ccd021704a4fe1b4022c110d3dd497f8cd907d5.tar.gz
13038 grep could support --label
Reviewed by: Andy Fiddaman <andy@omniosce.org> Reviewed by: Toomas Soome <tsoome@me.com> Approved by: Gordon Ross <gordon.w.ross@gmail.com>
-rw-r--r--usr/src/cmd/grep/grep.c34
-rw-r--r--usr/src/man/man1/grep.125
-rw-r--r--usr/src/pkg/manifests/system-test-utiltest.mf12
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/Makefile12
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.01
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.11
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.21
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.31
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.41
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.51
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.01
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.11
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.21
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.31
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.41
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.51
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh50
17 files changed, 140 insertions, 5 deletions
diff --git a/usr/src/cmd/grep/grep.c b/usr/src/cmd/grep/grep.c
index 96f0dc4853..8848d84724 100644
--- a/usr/src/cmd/grep/grep.c
+++ b/usr/src/cmd/grep/grep.c
@@ -59,6 +59,7 @@
#include <wctype.h>
#include <ftw.h>
#include <sys/param.h>
+#include <getopt.h>
#define STDIN_FILENAME gettext("(standard input)")
@@ -109,6 +110,7 @@ static uchar_t Rflag; /* Like rflag, but follow symlinks */
static uchar_t outfn; /* Put out file name */
static uchar_t conflag; /* show context of matches */
static char *cmdname;
+static char *stdin_label; /* Optional lable for stdin */
static int use_wchar, use_bmg, mblocale;
@@ -129,6 +131,20 @@ static void process_path(const char *);
static void process_file(const char *, int);
/*
+ * These are values that we use to return from getopt_long. They start at
+ * SHRT_MAX to avoid any possible conflict with the normal options. These are
+ * used for long options that have no short option equivalent.
+ */
+enum grep_opts {
+ OPT_LABEL = SHRT_MAX + 1
+};
+
+static struct option grep_options[] = {
+ { "label", required_argument, NULL, OPT_LABEL },
+ { NULL }
+};
+
+/*
* mainline for grep
*/
int
@@ -203,8 +219,8 @@ main(int argc, char **argv)
}
}
- while ((c = getopt(argc, argv, "vwchHilLnrbse:f:qxEFIRA:B:C:")) !=
- EOF) {
+ while ((c = getopt_long(argc, argv, "+vwchHilLnrbse:f:qxEFIRA:B:C:",
+ grep_options, NULL)) != EOF) {
unsigned long tval;
switch (c) {
case 'v': /* POSIX: negate matches */
@@ -364,6 +380,10 @@ main(int argc, char **argv)
}
break;
+ case OPT_LABEL:
+ stdin_label = optarg;
+ break;
+
default:
usage();
}
@@ -469,9 +489,13 @@ main(int argc, char **argv)
*/
fixpatterns();
+ if (stdin_label == NULL) {
+ stdin_label = STDIN_FILENAME;
+ }
+
/* Process all files: stdin, or rest of arg list */
if (argc < 2) {
- matched = grep(0, STDIN_FILENAME);
+ matched = grep(0, stdin_label);
} else {
if (Hflag || (argc > 2 && hflag == 0))
outfn = 1; /* Print filename on match line */
@@ -1562,8 +1586,8 @@ usage(void)
if (!egrep && !fgrep)
(void) fprintf(stderr, gettext(" [-E|-F]"));
(void) fprintf(stderr, gettext(" [-bchHilLnqrRsvx] [-A num] [-B num] "
- "[-C num|-num]\n [-e pattern_list]... "
- "[-f pattern_file]... [pattern_list] [file]...\n"));
+ "[-C num|-num]\n [--label=name] [-e pattern_list]... "
+ "[-f pattern_file]...\n [pattern_list] [file]...\n"));
exit(2);
/* NOTREACHED */
}
diff --git a/usr/src/man/man1/grep.1 b/usr/src/man/man1/grep.1
index 1843b25c80..817616fe05 100644
--- a/usr/src/man/man1/grep.1
+++ b/usr/src/man/man1/grep.1
@@ -61,6 +61,7 @@
.Op Fl A Ar num
.Op Fl B Ar num
.Op Fl C Ar num Ns | Ns Fl Ns Ar num
+.Op Fl -label Ns = Ns Ar name
.Oo Fl e Ar pattern_list Oc Ns ...
.Oo Fl f Ar pattern_file Oc Ns ...
.Op Ar pattern_list
@@ -167,6 +168,16 @@ Used when searching multiple files.
Precedes each line by the name of the file containing the matching line.
.It Fl i
Ignores upper/lower case distinction during comparisons.
+.It Fl -label Ns = Ns Ar name
+When the name of the matching file is printed
+.Pq Fl H ,
+instead of printing the string
+.Ql (standard input)
+the string
+.Ar name
+is printed instead.
+See
+.Sx Example 5 .
.It Fl l
Prints only the names of files with matching lines, separated by NEWLINE
characters.
@@ -362,6 +373,20 @@ $ grep -E '^abc$
$ grep -F -x 'abc
def'
.Ed
+.It Sy Example 5 No Using Fl -label
+When piping standard input into
+.Nm ,
+as part of a pipeline, occasionally it can be useful override the file
+name
+.Ql (standard input)
+with something from the pipeline.
+This would output each matching line instead with the name of the input
+file.
+.Bd -literal
+$ for f in *.gz; do
+> gzcat $f | grep -H --label=$f foo
+> done
+.Ed
.El
.Sh ENVIRONMENT VARIABLES
See
diff --git a/usr/src/pkg/manifests/system-test-utiltest.mf b/usr/src/pkg/manifests/system-test-utiltest.mf
index d0251e5d89..9ad8cd1037 100644
--- a/usr/src/pkg/manifests/system-test-utiltest.mf
+++ b/usr/src/pkg/manifests/system-test-utiltest.mf
@@ -1451,6 +1451,18 @@ 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/gout.t8.0 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t8.1 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t8.2 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t8.3 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t8.4 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t8.5 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t9.0 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t9.1 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t9.2 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t9.3 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t9.4 mode=0444
+file path=opt/util-tests/tests/files/grep/gout.t9.5 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
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile b/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
index 4129aa40e5..28b48237e9 100644
--- a/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
@@ -123,6 +123,18 @@ PROGS = test0 \
gout.t6.17 \
gout.t6.18 \
gout.t6.19 \
+ gout.t8.0 \
+ gout.t8.1 \
+ gout.t8.2 \
+ gout.t8.3 \
+ gout.t8.4 \
+ gout.t8.5 \
+ gout.t9.0 \
+ gout.t9.1 \
+ gout.t9.2 \
+ gout.t9.3 \
+ gout.t9.4 \
+ gout.t9.5 \
testnl
CMDS = $(PROGS:%=$(TESTDIR)/%)
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.0 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.0
new file mode 100644
index 0000000000..1c7858c440
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.0
@@ -0,0 +1 @@
+(standard input):a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.1 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.1
new file mode 100644
index 0000000000..131d71a84b
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.1
@@ -0,0 +1 @@
+zelda:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.2 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.2
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.2
@@ -0,0 +1 @@
+a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.3 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.3
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.3
@@ -0,0 +1 @@
+a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.4 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.4
new file mode 100644
index 0000000000..594176c85c
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.4
@@ -0,0 +1 @@
+7:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.5 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.5
new file mode 100644
index 0000000000..85ea8560f8
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t8.5
@@ -0,0 +1 @@
+zelda:7:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.0 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.0
new file mode 100644
index 0000000000..f69ef4b88d
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.0
@@ -0,0 +1 @@
+test0:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.1 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.1
new file mode 100644
index 0000000000..f69ef4b88d
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.1
@@ -0,0 +1 @@
+test0:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.2 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.2
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.2
@@ -0,0 +1 @@
+a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.3 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.3
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.3
@@ -0,0 +1 @@
+a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.4 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.4
new file mode 100644
index 0000000000..594176c85c
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.4
@@ -0,0 +1 @@
+7:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.5 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.5
new file mode 100644
index 0000000000..4e73c73b77
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout.t9.5
@@ -0,0 +1 @@
+test0:7:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh b/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
index 663ae26916..d73b4e9d96 100644
--- a/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
+++ b/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
@@ -74,6 +74,41 @@ run_tests() {
done < $FLAGSFILE
}
+run_tests_stdin() {
+ i=0
+ exp=$1
+ pass=$2
+ pattern=$3
+ file=$4
+
+ echo "$FLAGS" > $FLAGSFILE
+ while read flags; do
+ difffile="gout.$pass.$i"
+ print -n "test $total: grep (stdin) $flags: "
+ ((total++))
+ ((i++))
+ $XGREP $flags $pattern < $file > $OUTFILE
+ err=$?
+ if [[ $err -ne $exp ]]; then
+ test_fail "failed on exit: $err"
+ continue
+ fi
+
+ if [[ $exp -eq 0 ]]; then
+ if [[ ! -f "$difffile" ]]; then
+ test_fail "missing output file $difffile"
+ continue
+ fi
+
+ if [[ -n "$(diff $OUTFILE $difffile)" ]]; then
+ print "$(diff $OUTFILE $difffile)"
+ test_fail "output is different"
+ continue
+ fi
+ fi
+ echo "passed"
+ done < $FLAGSFILE
+}
total=0
failures=0
@@ -203,6 +238,21 @@ FLAGS="-vL
run_tests 1 t7 foo test.lL.0 test.lL.1
#
+# Test Group 8, 9: Here we are testing the ability for --label to
+# properly replace the name of a file from standard in, but not
+# replacing that of a file that's normally specified. Set 8 verifies the
+# former while set 9 verifies the latter.
+#
+FLAGS="-H
+-H --label=zelda
+--label=zelda
+-h --label=zelda
+-n --label=zelda
+-H -n --label=zelda"
+run_tests_stdin 0 t8 a test0
+run_tests 0 t9 a test0
+
+#
# Clean up temporary files.
#
rm -f $FLAGSFILE $OUTFILE