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.
This commit is contained in:
Lasse Collin 2022-07-17 20:55:16 +03:00
parent 2d2201bc63
commit b48f9d615f
1 changed files with 17 additions and 2 deletions

View File

@ -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");;