From fdda5c19432ad7dc7f1b1823a058f6444f463372 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 3 May 2022 20:34:41 -0700 Subject: New -g option to pre-load the grep stack. --- pw.1 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pw.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/pw.1 b/pw.1 index e423baa..15f3d2f 100644 --- a/pw.1 +++ b/pw.1 @@ -588,6 +588,61 @@ that is the default, this option has no effect unless it appears after a .B -E option. +.IP "\fB-g\fP [\fB!\fP]\fIpattern\fP" + +Push +.I pattern +onto the grep stack as if using the +.B :v +or +.B :g +commands, but prior to startup, and hence prior to reading any input. + +For more detail, see the +.B :g +and +.B :v +run-time commands. + +A leading +.B ! +before the pattern indicates inversion, like what is arranged by the +.B :v +command. Similarly to the convention in the trigger command syntax, +to specify a non-inverted pattern which starts with a literal +.B ! +character, precede that +.B ! +character with a backslash. This convention is part of the +.B -g +option's argument syntax, and not a regular expression escape sequence, +and is not recognized other than before a non-inverted pattern. + +If the +.B -g +option is used one or more times, the status display will show +.B GREP +followed by the patterns enclosed in parentheses, as usual. +Patterns pushed by +.B -g +may be removed interactively with the +.B :r +command. + +Whether the +.I pattern +argument of a +.B -g +is compiled as a BRE or ERE depends on whether a +.B -B +or +.B -E +option more recently appeared before that +.B -g +option. If neither option is used, then +.I pattern +is interpreted as a BRE. + .SH ENVIRONMENT .I pw diff --git a/pw.c b/pw.c index c9d9bbf..8da30a4 100644 --- a/pw.c +++ b/pw.c @@ -245,6 +245,7 @@ static void usage(void) "-d do not quit on end-of-input\n" "-E treat regular expressions as extended\n" "-B treat regular expressions as basic (default)\n\n" + "-g [!]pattern add pattern to grep stack; ! inverts.\n\n" " represents an arbitrary command that generates the\n" "output to be monitored by %s.\n\n" "Standard input must be redirected; it cannot be the same device\n" @@ -725,7 +726,7 @@ int main(int argc, char **argv) } } - while ((opt = getopt(argc, argv, "n:i:l:dEB")) != -1) { + while ((opt = getopt(argc, argv, "n:i:l:dEBg:")) != -1) { switch (opt) { case 'n': { @@ -761,6 +762,35 @@ int main(int argc, char **argv) case 'B': regex_flags = 0; break; + case 'g': + { + grep *gr = &grepstack[ngrep]; + char *pat = optarg; + int inv = 0; + + if (ngrep >= maxgrep) { + error("too many patterns specified with -g\n"); + return EXIT_FAILURE; + } + + if (*pat == '!') { + inv = 1; + pat++; + } else if (strncmp(pat, "\\!", 2) == 0) { + pat++; + } + + if ((grinit(gr, dsdup(pat), inv, dsdrop)) != 0) { + char grmsg[cmdsize]; + grerrstr(gr, grmsg, sizeof grmsg); + error("-%c option: bad pattern %s: %s\n", opt, pat, grmsg); + return EXIT_FAILURE; + } + + ngrep++; + stat |= stat_grep; + } + break; default: usage(); } -- cgit v1.2.3