3 # zgrep -- a wrapper around a grep program that decompresses files as needed
4 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
6 # Copyright (C) 1998, 2001-2002, 2006-2007, 2009-2022 Free Software Foundation,
9 # Copyright (C) 1993 Jean-loup Gailly
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 grep='${GREP-'\''@GREP@'\''}'
28 version
='zgrep (gzip) @VERSION@
29 Copyright (C) 2022 Free Software Foundation, Inc.
30 This is free software. You may redistribute copies of it under the terms of
31 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
32 There is NO WARRANTY, to the extent permitted by law.
34 Written by Jean-loup Gailly.'
36 usage
="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
37 Look for instances of PATTERN in the input FILEs, using their
38 uncompressed contents if they are compressed.
40 OPTIONs are the same as for 'grep', except that the following 'grep'
41 options are not supported: --dereference-recursive (-R), --directories (-d),
42 --exclude, --exclude-from, --exclude-dir, --include, --null (-Z),
43 --null-data (-z), and --recursive (-r).
45 Report bugs to <bug-gzip@gnu.org>."
47 # sed script to escape all ' for the shell, and then (to handle trailing
48 # newlines correctly) append ' to the last line.
50 s/'\''/'\''\\'\'''\''/g
56 files_without_matches
=0
61 while test $# -ne 0; do
67 (-[0123456789EFGHIKLPRTUVZabchilnoqrsuvwxyz
]*[!0123456789]*)
68 arg2
=-\'$
(LC_ALL
=C
expr "X$option" : 'X-.[0-9]*\(.*\)' |
69 LC_ALL
=C
sed "$escape")
70 eval "set -- $arg2 "'${1+"$@"}'
71 option
=$
(LC_ALL
=C
expr "X$option" : 'X\(-.[0-9]*\)');;
72 (--binary-*=* |
--[lm
]a
*=* |
--reg*=*)
74 (-[ABCDXdefm
] |
--binary-* |
--file |
--[lm
]a
* |
--reg*)
75 case ${1?"$option option requires an argument"} in
77 optarg
=" '"$
(printf '%s\n' "$1" | LC_ALL
=C
sed "$escape");;
83 optarg
=" '"$
(LC_ALL
=C
expr "X$option" : 'X-f\(.*\)' |
84 LC_ALL
=C
sed "$escape")
87 optarg
=" '"$
(LC_ALL
=C
expr "X$option" : 'X-f\(.*\)')\'
90 optarg
=" '"$
(LC_ALL
=C
expr "X$option" : 'X--file=\(.*\)' |
91 LC_ALL
=C
sed "$escape")
94 optarg
=" '"$
(LC_ALL
=C
expr "X$option" : 'X--file=\(.*\)')\'
103 operands
="$operands '"$
(printf '%s\n' "$option" | LC_ALL
=C
sed "$escape")
106 operands
="$operands '$option'";;
108 ${POSIXLY_CORRECT+break}
113 (-[drRzZ
] |
--di* |
--exc* |
--inc* |
--rec* |
--nu*)
114 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
119 # The pattern is coming from a file rather than the command-line.
120 # If the file is actually stdin then we need to do a little
121 # magic, since we use stdin to pass the gzip output to grep.
122 # Similarly if it is not a regular file, since it might be read repeatedly.
123 # In either of these two cases, copy the pattern into a temporary file,
124 # and use that file instead. The pattern might contain null bytes,
125 # so we cannot simply switch to -e here.
127 (" '-'" |
" '/dev/stdin'" |
" '/dev/fd/0'")
130 eval "test ! -f$optarg";;
133 if test -n "$pattmp"; then
134 eval "cat --$optarg" >>"$pattmp" ||
exit 2
138 test -n "$pattmp" && rm -f "$pattmp"
140 ' HUP INT PIPE TERM
0
143 /*) TMPDIR
=$TMPDIR/;;
146 if type mktemp
>/dev
/null
2>&1; then
147 pattmp
=$
(mktemp
"${TMPDIR}zgrepXXXXXXXXX") ||
exit 2
150 pattmp
=${TMPDIR}zgrep$$
152 eval "cat --$optarg" >"$pattmp" ||
exit 2
156 (--h |
--he |
--hel |
--help)
157 printf '%s\n' "$usage" ||
exit 2
159 (-H |
--wi |
--wit |
--with |
--with- |
--with-f |
--with-fi \
160 |
--with-fil |
--with-file |
--with-filen |
--with-filena |
--with-filenam \
164 (-l |
--files-with-*)
165 files_with_matches
=1;;
166 (-L |
--files-witho*)
167 files_without_matches
=1;;
170 (-V |
--v |
--ve |
--ver |
--vers |
--versi |
--versio |
--version)
171 printf '%s\n' "$version" ||
exit 2
177 option
=\'$
(printf '%s\n' "$option" | LC_ALL
=C
sed "$escape");;
182 args
="$args $option$optarg"
185 eval "set -- $operands "'${1+"$@"}'
187 if test $have_pat -eq 0; then
188 case ${1?"missing pattern; try \`$0 --help' for help"} in
190 args
="$args -- '"$
(printf '%s\n' "$1" | LC_ALL
=C
sed "$escape");;
192 args
="$args -- '$1'";;
197 if test $# -eq 0; then
199 elif test 1 -lt $# && test $no_filename -eq 0; then
203 l_e
=$
(eval "(echo e | $grep -H --label=l e) 2>/dev/null") && test "$l_e" = l
:e
204 gnuish_grep
="test $? -eq 0"
205 if $gnuish_grep && test $with_filename -eq 1; then
214 # Fail if gzip or grep (or sed) fails.
217 ('gzip' -cdfq -- "$i" 5>&-; echo $?
>&5) 3>&- |
218 if test $files_with_matches -eq 1; then
219 eval "$grep$args" >/dev
/null
&& { printf '%s\n' "$i" ||
exit 2; }
220 elif test $files_without_matches -eq 1; then
221 eval "$grep$args" >/dev
/null ||
{
223 if test $r -eq 1; then
224 printf '%s\n' "$i" || r
=2
226 test 256 -le $r && r
=$
(expr 128 + $r % 128)
229 elif $gnuish_grep && test "$i" != -; then
230 eval "$grep --label \"\$i\"$args"
231 elif $gnuish_grep ||
test $with_filename -eq 0; then
236 '* |
*'&'* |
*'\'* | *'|
'*)
237 icolon=$(printf '%s
\n' "$i:" |
238 LC_ALL=C sed -e 's
/[&\|
]/\\&/g
' -e '$
!s
/$
/\\/');;
241 sed_script="s|^|$icolon|"
243 # Fail if grep or sed fails.
246 (eval "$grep$args" 4>&-; echo $? >&4) 3>&- |
247 LC_ALL=C sed "$sed_script" >&3 4>&-
248 ) || { r=$?; test $r -lt 2 && r=2; }
249 test 256 -le $r && r=$(expr 128 + $r % 128)
255 # Ignore gzip status 2, as it is just a warning.
256 # gzip status 1 is an error, like grep status 2.
257 test $gzip_status -eq 2 && gzip_status=0
258 test $gzip_status -eq 1 && gzip_status=2
260 # Use the more serious of the grep and gzip statuses.
261 test $r -lt $gzip_status && r=$gzip_status
263 # Accumulate the greatest status, except consider 0 to be greater than 1.
264 if test $r -le 1 && test $res -le 1; then
270 # Exit immediately on a serious error.
271 test 126 -le $res && break
274 if test -n "$pattmp"; then
278 test $res -lt $r && res=$r
280 trap - HUP INT PIPE TERM 0
283 test 128 -le $res && kill -$(expr $res % 128) $$