From 92dd18f01ef8dafa7147b2522f223b2263950571 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 7 Oct 2019 17:47:33 +0000 Subject: two compat features to allow the zstdgrep script to work. add --label option to prefix the output instead of filename. allow using - to mean stdin. ok deraadt --- usr.bin/grep/grep.1 | 9 +++++++-- usr.bin/grep/grep.c | 15 +++++++++++++-- usr.bin/grep/grep.h | 3 ++- usr.bin/grep/util.c | 4 +++- 4 files changed, 25 insertions(+), 6 deletions(-) (limited to 'usr.bin/grep') diff --git a/usr.bin/grep/grep.1 b/usr.bin/grep/grep.1 index 6ea1ad7bd83..4f2c0482b09 100644 --- a/usr.bin/grep/grep.1 +++ b/usr.bin/grep/grep.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: grep.1,v 1.47 2019/07/18 15:32:50 schwarze Exp $ +.\" $OpenBSD: grep.1,v 1.48 2019/10/07 17:47:32 tedu Exp $ .\" Copyright (c) 1980, 1990, 1993 .\" The Regents of the University of California. All rights reserved. .\" @@ -28,7 +28,7 @@ .\" .\" @(#)grep.1 8.3 (Berkeley) 4/18/94 .\" -.Dd $Mdocdate: July 18 2019 $ +.Dd $Mdocdate: October 7 2019 $ .Dt GREP 1 .Os .Sh NAME @@ -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 diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 651a3b15002..3b2cf80b8e7 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.60 2019/07/18 15:32:50 schwarze Exp $ */ +/* $OpenBSD: grep.c,v 1.61 2019/10/07 17:47:32 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -80,6 +80,7 @@ int vflag; /* -v: only show non-matching lines */ 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; @@ -461,6 +467,11 @@ main(int argc, char *argv[]) --argc; ++argv; } + if (argc == 1 && strcmp(*argv, "-") == 0) { + /* stdin */ + --argc; + ++argv; + } if (Rflag && argc == 0) warnx("warning: recursive search of stdin"); diff --git a/usr.bin/grep/grep.h b/usr.bin/grep/grep.h index 8deacded308..b3d24ae662b 100644 --- a/usr.bin/grep/grep.h +++ b/usr.bin/grep/grep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.h,v 1.26 2019/01/23 23:00:54 tedu Exp $ */ +/* $OpenBSD: grep.h,v 1.27 2019/10/07 17:47:32 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -70,6 +70,7 @@ extern int Aflag, Bflag, Eflag, Fflag, Hflag, Lflag, 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; diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 84db8bee013..b683e8510b7 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.60 2019/07/17 04:24:20 tedu Exp $ */ +/* $OpenBSD: util.c,v 1.61 2019/10/07 17:47:32 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -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; -- cgit v1.2.3