The zstd package includes a zstdgrep script, which should behave like zgrep,
however it assumes a few gnu grep behaviors we don't support.

1. --label=name prints a custom label, so you can get file.txt, not
file.txt.zst in the output.

2. It uses - for stdin instead of a missing argument.

Both are easy to address, as below. With this, I can grep my old log files
with having to do the zstdcat | grep dance myself.


Index: grep.1
===================================================================
RCS file: /home/cvs/src/usr.bin/grep/grep.1,v
retrieving revision 1.47
diff -u -p -r1.47 grep.1
--- grep.1      18 Jul 2019 15:32:50 -0000      1.47
+++ grep.1      6 Oct 2019 18:21:05 -0000
@@ -47,6 +47,7 @@
 .Op Fl m Ar num
 .Op Fl -binary-files Ns = Ns Ar value
 .Op Fl -context Ns Op = Ns Ar num
+.Op Fl -label Ar name
 .Op Fl -line-buffered
 .Op Ar pattern
 .Op Ar
@@ -283,6 +284,10 @@ do not search binary files;
 and
 .Ar text :
 treat all files as text.
+.It Fl -label Ar name
+Print
+.Ar name
+instead of the filename before lines.
 .It Fl -line-buffered
 Force output to be line buffered.
 By default, output is line buffered when standard output is a terminal
Index: grep.c
===================================================================
RCS file: /home/cvs/src/usr.bin/grep/grep.c,v
retrieving revision 1.60
diff -u -p -r1.60 grep.c
--- grep.c      18 Jul 2019 15:32:50 -0000      1.60
+++ grep.c      6 Oct 2019 18:13:02 -0000
@@ -80,6 +80,7 @@ int    vflag;         /* -v: only show non-matchi
 int     wflag;         /* -w: pattern must start and end on word boundaries */
 int     xflag;         /* -x: pattern must match entire line */
 int     lbflag;        /* --line-buffered */
+const char *labelname; /* --label=name */
 
 int binbehave = BIN_FILE_BIN;
 
@@ -87,7 +88,8 @@ enum {
        BIN_OPT = CHAR_MAX + 1,
        HELP_OPT,
        MMAP_OPT,
-       LINEBUF_OPT
+       LINEBUF_OPT,
+       LABEL_OPT,
 };
 
 /* Housekeeping */
@@ -130,6 +132,7 @@ static const struct option long_options[
        {"binary-files",        required_argument,      NULL, BIN_OPT},
        {"help",                no_argument,            NULL, HELP_OPT},
        {"mmap",                no_argument,            NULL, MMAP_OPT},
+       {"label",               required_argument,      NULL, LABEL_OPT},
        {"line-buffered",       no_argument,            NULL, LINEBUF_OPT},
        {"after-context",       required_argument,      NULL, 'A'},
        {"before-context",      required_argument,      NULL, 'B'},
@@ -427,6 +430,9 @@ main(int argc, char *argv[])
                case MMAP_OPT:
                        /* default, compatibility */
                        break;
+               case LABEL_OPT:
+                       labelname = optarg;
+                       break;
                case LINEBUF_OPT:
                        lbflag = 1;
                        break;
@@ -458,6 +464,11 @@ main(int argc, char *argv[])
 
        if (argc != 0 && needpattern) {
                add_patterns(*argv);
+               --argc;
+               ++argv;
+       }
+       if (argc == 1 && strcmp(*argv, "-") == 0) {
+               /* stdin */
                --argc;
                ++argv;
        }
Index: grep.h
===================================================================
RCS file: /home/cvs/src/usr.bin/grep/grep.h,v
retrieving revision 1.26
diff -u -p -r1.26 grep.h
--- grep.h      23 Jan 2019 23:00:54 -0000      1.26
+++ grep.h      6 Oct 2019 18:09:32 -0000
@@ -70,6 +70,7 @@ extern int     Aflag, Bflag, Eflag, Fflag, 
                 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, qflag,
                 sflag, vflag, wflag, xflag;
 extern int      binbehave;
+extern const char *labelname;
 
 extern int      first, matchall, patterns, tail, file_err;
 extern char    **pattern;
Index: util.c
===================================================================
RCS file: /home/cvs/src/usr.bin/grep/util.c,v
retrieving revision 1.60
diff -u -p -r1.60 util.c
--- util.c      17 Jul 2019 04:24:20 -0000      1.60
+++ util.c      6 Oct 2019 18:10:07 -0000
@@ -122,6 +122,8 @@ procfile(char *fn)
        }
 
        ln.file = fn;
+       if (labelname)
+               ln.file = (char *)labelname;
        ln.line_no = 0;
        ln.len = 0;
        linesqueued = 0;

Reply via email to