From 62c1d2bc2da66e182c52bb1e3aa791f6d8a9e099 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sun, 17 Jul 2022 20:55:16 +0300 Subject: [PATCH] xzgrep: Fix parsing of certain options. Fix handling of "xzgrep -25 foo" (in GNU grep "grep -25 foo" is an alias for "grep -C25 foo"). xzgrep would treat "foo" as filename instead of as a pattern. This bug was fixed in zgrep in gzip in 2012. Add -E, -F, -G, and -P to the "no argument required" list. Add -X to "argument required" list. It is an intentionally-undocumented GNU grep option so this isn't an important option for xzgrep but it seems that other grep implementations (well, those that I checked) don't support -X so I hope this change is an improvement still. grep -d (grep --directories=ACTION) requires an argument. In contrast to zgrep, I kept -d in the "no argument required" list because it's not supported in xzgrep (or zgrep). This way "xzgrep -d" gives an error about option being unsupported instead of telling that it requires an argument. Both zgrep and xzgrep tell that it's unsupported if an argument is specified. Add comments. --- src/scripts/xzgrep.in | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/scripts/xzgrep.in b/src/scripts/xzgrep.in index e5186baf..fce7940a 100644 --- a/src/scripts/xzgrep.in +++ b/src/scripts/xzgrep.in @@ -63,13 +63,28 @@ while test $# -ne 0; do optarg= case $option in - (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*) + (-[0123456789abcdEFGhHiIKlLnoPqrRsTuUvVwxyzZ]*[!0123456789]*) + # Something like -Fiv was specified, that is, $option contains more + # than one option of which the first option (in this example -F) + # doesn't take an argument. Split the first option into a standalone + # argument and continue parsing the rest of the options (in this example, + # replace -Fiv with -iv in the argument list and set option=-F). + # + # If there are digits [0-9] they are treated as if they were a single + # option character because this syntax is an alias for -C for GNU grep. + # For example, "grep -25F" is equivalent to "grep -C25 -F". If only + # digits are specified like "grep -25" we don't get here because the + # above pattern in the case-statement doesn't match such strings. arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape") eval "set -- $arg2 "'${1+"$@"}' option=$(expr "X$option" : 'X\(-.[0-9]*\)');; (--binary-*=* | --[lm]a*=* | --reg*=*) + # These options require an argument and an argument has been provided + # with the --foo=argument syntax. All is good. ;; - (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*) + (-[ABCDefmX] | --binary-* | --file | --[lm]a* | --reg*) + # These options require an argument which should now be in $1. + # If it isn't, display an error and exit. case ${1?"$option option requires an argument"} in (*\'*) optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;