2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # xzgrep -- a wrapper around a grep program that decompresses files as needed
5 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
7 # Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
8 # Copyright (C) 1993 Jean-loup Gailly
10 # Modified for XZ Utils by Andrew Dudman and Lasse Collin.
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 @enable_path_for_scripts@
23 #SET_PATH - This line is a placeholder to ease patching this script.
25 # Instead of unsetting XZ_OPT, just make sure that xz will use file format
26 # autodetection. This way memory usage limit and thread limit can be
27 # specified via XZ_OPT. With gzip, bzip2, and lzop it's OK to just unset the
28 # environment variables.
29 xz='@xz@ --format=auto'
30 unset GZIP BZIP BZIP2 LZOP
33 *egrep*) prog=xzegrep; grep=${GREP:-grep -E};;
34 *fgrep*) prog=xzfgrep; grep=${GREP:-grep -F};;
35 *) prog=xzgrep; grep=${GREP:-grep};;
38 version="$prog (@PACKAGE_NAME@) @PACKAGE_VERSION@"
40 usage="Usage: ${0##*/} [OPTION]... [-e] PATTERN [FILE]...
41 Look for instances of PATTERN in the input FILEs, using their
42 uncompressed contents if they are compressed.
44 OPTIONs are the same as for '$grep'.
46 Report bugs to <@PACKAGE_BUGREPORT@>."
48 # sed script to escape all ' for the shell, and then (to handle trailing
49 # newlines correctly) turn trailing X on last line into '.
51 s/'\''/'\''\\'\'''\''/g
57 files_without_matches=0
61 # See if -H and --label options are supported (GNU and *BSDs).
62 if test f:x = "$(eval "echo x | $grep -H --label=f x 2> /dev/null")"; then
68 while test $# -ne 0; do
74 (-[0123456789abcdEFGhHiIKlLnoPqrRsTuUvVwxyzZ]*[!0123456789]*)
75 # Something like -Fiv was specified, that is, $option contains more
76 # than one option of which the first option (in this example -F)
77 # doesn't take an argument. Split the first option into a standalone
78 # argument and continue parsing the rest of the options (in this example,
79 # replace -Fiv with -iv in the argument list and set option=-F).
81 # If there are digits [0-9] they are treated as if they were a single
82 # option character because this syntax is an alias for -C for GNU grep.
83 # For example, "grep -25F" is equivalent to "grep -C25 -F". If only
84 # digits are specified like "grep -25" we don't get here because the
85 # above pattern in the case-statement doesn't match such strings.
86 arg2=-\'$(LC_ALL=C expr "X${option}X" : 'X-.[0-9]*\(.*\)' |
87 LC_ALL=C sed "$escape")
88 eval "set -- $arg2 "'${1+"$@"}'
89 option=$(LC_ALL=C expr "X$option" : 'X\(-.[0-9]*\)');;
90 (--binary-*=* | --[lm]a*=* | --reg*=*)
91 # These options require an argument and an argument has been provided
92 # with the --foo=argument syntax. All is good.
94 (-[ABCDefmX] | --binary-* | --file | --[lm]a* | --reg*)
95 # These options require an argument which should now be in $1.
96 # If it isn't, display an error and exit.
97 case ${1?"$option option requires an argument"} in
99 optarg=" '"$(printf '%sX\n' "$1" | LC_ALL=C sed "$escape");;
111 operands="$operands '"$(printf '%sX\n' "$option" |
112 LC_ALL=C sed "$escape");;
114 operands="$operands '$option'";;
116 ${POSIXLY_CORRECT+break}
121 (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
122 printf >&2 '%s: %s: Option not supported\n' "$0" "$option"
124 (-[ef]* | --file | --file=* | --reg*)
126 (--h | --he | --hel | --help)
127 printf '%s\n' "$usage" || exit 2
129 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
130 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
134 (-l | --files-with-*)
137 (-L | --files-witho*)
138 files_without_matches=1
142 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
143 printf '%s\n' "$version" || exit 2
149 option=\'$(printf '%sX\n' "$option" | LC_ALL=C sed "$escape");;
154 grep="$grep $option$optarg"
157 eval "set -- $operands "'${1+"$@"}'
159 if test $have_pat -eq 0; then
160 case ${1?"Missing pattern; try '${0##*/} --help' for help"} in
162 grep="$grep -e '"$(printf '%sX\n' "$1" | LC_ALL=C sed "$escape");;
164 grep="$grep -e '$1'";;
169 if test $# -eq 0; then
175 # res=1 means that no file matched yet
180 *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) uncompress="gzip -cdf";;
181 *[-.]bz2 | *[-.]tbz | *.tbz2) uncompress="bzip2 -cdf";;
182 *[-.]lzo | *[-.]tzo) uncompress="lzop -cdf";;
183 *[-.]zst | *[-.]tzst) uncompress="zstd -cdfq";; # zstd needs -q.
184 *[-.]lz4) uncompress="lz4 -cdf";;
185 *) uncompress="$xz -cdfqQ";; # -qQ to ignore warnings like unsupp. check.
187 # xz_status will hold the decompressor's exit status.
188 # Exit status of grep (and in rare cases, printf or sed) is
189 # available as the exit status of this assignment command.
192 ($uncompress -- "$i" 5>&-; echo $? >&5) 3>&- |
193 if test $files_with_matches -eq 1; then
194 eval "$grep -q" && { printf '%s\n' "$i" || exit 2; }
195 elif test $files_without_matches -eq 1; then
198 if test $r -eq 1; then
199 printf '%s\n' "$i" || r=2
203 elif test $with_filename -eq 0 &&
204 { test $# -eq 1 || test $no_filename -eq 1; }; then
206 elif test $grep_supports_label -eq 1; then
207 # The grep implementation in use allows us to specify the filename
208 # that grep will prefix to the output lines. This is faster and
209 # less prone to security bugs than the fallback method that uses sed.
210 # This also avoids confusing output with GNU grep >= 3.5 (2020-09-27)
211 # which prints "binary file matches" to stderr instead of stdout.
213 # If reading from stdin, let grep use whatever name it prefers for
214 # stdin. With GNU grep it is a locale-specific translated string.
215 if test "x$i" = "x-"; then
218 eval "$grep -H --label \"\$i\""
221 # Append a colon so that the last character will never be a newline
222 # which would otherwise get lost in shell command substitution.
225 # Escape & \ | and newlines only if such characters are present
226 # (speed optimization).
229 '* | *'&'* | *'\'* | *'|'*)
230 # If sed fails, set i to a known safe string to ensure that
231 # failing sed did not create a half-escaped dangerous string.
232 i=$(printf '%s\n' "$i" | LC_ALL=C sed 's/[&\|]/\\&/g; $!s/$/\\/') ||
233 i='(unknown filename):';;
236 # $i already ends with a colon so do not add it here.
239 # If grep or sed fails, pick the larger value of the two exit statuses.
240 # If sed fails, use at least 2 since we use >= 2 to indicate errors.
243 (eval "$grep" 4>&-; echo $? >&4) 3>&- |
244 LC_ALL=C sed "$sed_script" >&3 4>&-
247 test "$sed_status" -lt 2 && sed_status=2
248 test "$r" -lt "$sed_status" && r=$sed_status
255 # If grep or sed or other non-decompression command failed with a signal,
256 # exit immediately and ignore the possible remaining files.
258 # NOTE: Instead of 128 + signal_number, some shells use
259 # 256 + signal_number (ksh) or 384 + signal_number (yash).
260 # This is fine for us since their "exit" and "kill -l" commands take
261 # this into account. (At least the versions I tried do but there is
262 # a report of an old ksh variant whose "exit" truncates the exit status
263 # to 8 bits without any special handling for values indicating a signal.)
264 test "$r" -ge 128 && exit "$r"
266 if test -z "$xz_status"; then
267 # Something unusual happened, for example, we got a signal and
268 # the exit status of the decompressor was never echoed and thus
269 # $xz_status is empty. Exit immediately and ignore the possible
272 elif test "$xz_status" -ge 128; then
273 # The decompressor died due to a signal. SIGPIPE is ignored since it can
274 # occur if grep exits before the whole file has been decompressed (grep -q
275 # can do that). If the decompressor died with some other signal, exit
276 # immediately and ignore the possible remaining files.
277 test "$(kill -l "$xz_status" 2> /dev/null)" != "PIPE" && exit "$xz_status"
278 elif test "$xz_status" -gt 0; then
279 # Decompression failed but we will continue with the remaining
280 # files anyway. Set exit status to at least 2 to indicate an error.
281 test "$r" -lt 2 && r=2
284 # Since res=1 is the initial value, we only need to care about
285 # matches (r == 0) and errors (r >= 2) here; r == 1 can be ignored.
286 if test "$r" -ge 2; then
287 # An error occurred in decompressor, grep, or some other command. Update
288 # res unless a larger error code has been seen with an earlier file.
289 test "$res" -lt "$r" && res=$r
290 elif test "$r" -eq 0; then
291 # grep found a match and no errors occurred. Update res if no errors have
292 # occurred with earlier files.
293 test "$res" -eq 1 && res=0
297 # 0: At least one file matched and no errors occurred.
298 # 1: No matches were found and no errors occurred.
299 # >=2: Error. It's unknown if matches were found.