diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-05-03 20:34:41 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-05-03 20:34:41 -0700 |
commit | fdda5c19432ad7dc7f1b1823a058f6444f463372 (patch) | |
tree | 61cf6371e4ffbfc1ac6b1c638b4aff72fbee2844 | |
parent | 906d31bf059bd206ef6054c668fe10b93f33b336 (diff) | |
download | pw-fdda5c19432ad7dc7f1b1823a058f6444f463372.tar.gz pw-fdda5c19432ad7dc7f1b1823a058f6444f463372.tar.bz2 pw-fdda5c19432ad7dc7f1b1823a058f6444f463372.zip |
New -g option to pre-load the grep stack.
-rw-r--r-- | pw.1 | 55 | ||||
-rw-r--r-- | pw.c | 32 |
2 files changed, 86 insertions, 1 deletions
@@ -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 @@ -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" "<command> 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(); } |