build: update gnulib to latest
[gzip.git] / zgrep.in
blobc55a4b611b8824752cc6a02acfef91cd07a9a46f
1 #!/bin/sh
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-2024 Free Software Foundation,
7 # Inc.
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@'\''}'
26 args=
28 version='zgrep (gzip) @VERSION@
29 Copyright (C) 2024 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.
49 escape='
50 s/'\''/'\''\\'\'''\''/g
51 $s/$/'\''/
53 operands=
54 have_pat=0
55 files_with_matches=0
56 files_without_matches=0
57 no_filename=0
58 with_filename=0
59 pattmp=
61 while test $# -ne 0; do
62 option=$1
63 shift
64 optarg=
66 case $option in
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
76 (*\'*)
77 optarg=" '"$(printf '%s\n' "$1" | LC_ALL=C sed "$escape");;
78 (*)
79 optarg=" '$1'";;
80 esac
81 shift;;
82 (-f?*\'*)
83 optarg=" '"$(LC_ALL=C expr "X$option" : 'X-f\(.*\)' |
84 LC_ALL=C sed "$escape")
85 option=-f;;
86 (-f?*)
87 optarg=" '"$(LC_ALL=C expr "X$option" : 'X-f\(.*\)')\'
88 option=-f;;
89 (--file=*\'*)
90 optarg=" '"$(LC_ALL=C expr "X$option" : 'X--file=\(.*\)' |
91 LC_ALL=C sed "$escape")
92 option=--file;;
93 (--file=*)
94 optarg=" '"$(LC_ALL=C expr "X$option" : 'X--file=\(.*\)')\'
95 option=--file;;
96 (--)
97 break;;
98 (-?*)
101 case $option in
102 (*\'*)
103 operands="$operands '"$(printf '%s\n' "$option" | LC_ALL=C sed "$escape")
106 operands="$operands '$option'";;
107 esac
108 ${POSIXLY_CORRECT+break}
109 continue;;
110 esac
112 case $option in
113 (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
114 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
115 exit 2;;
116 (-e* | --reg*)
117 have_pat=1;;
118 (-f | --file)
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.
126 if case $optarg in
127 (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
130 eval "test ! -f$optarg";;
131 esac
132 then
133 if test -n "$pattmp"; then
134 eval "cat --$optarg" >>"$pattmp" || exit 2
135 continue
137 trap '
138 test -n "$pattmp" && rm -f "$pattmp"
139 (exit 2); exit 2
140 ' HUP INT PIPE TERM 0
141 case $TMPDIR in
142 / | /*/) ;;
143 /*) TMPDIR=$TMPDIR/;;
144 *) TMPDIR=/tmp/;;
145 esac
146 if type mktemp >/dev/null 2>&1; then
147 pattmp=$(mktemp "${TMPDIR}zgrepXXXXXXXXX") || exit 2
148 else
149 set -C
150 pattmp=${TMPDIR}zgrep$$
152 eval "cat --$optarg" >"$pattmp" || exit 2
153 optarg=' "$pattmp"'
155 have_pat=1;;
156 (--h | --he | --hel | --help)
157 printf '%s\n' "$usage" || exit 2
158 exit;;
159 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
160 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
161 | --with-filename)
162 with_filename=1
163 continue;;
164 (-l | --files-with-*)
165 files_with_matches=1;;
166 (-L | --files-witho*)
167 files_without_matches=1;;
168 (-h | --no-f*)
169 no_filename=1;;
170 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
171 printf '%s\n' "$version" || exit 2
172 exit;;
173 esac
175 case $option in
176 (*\'?*)
177 option=\'$(printf '%s\n' "$option" | LC_ALL=C sed "$escape");;
179 option="'$option'";;
180 esac
182 args="$args $option$optarg"
183 done
185 eval "set -- $operands "'${1+"$@"}'
187 if test $have_pat -eq 0; then
188 case ${1?"missing pattern; try \`$0 --help' for help"} in
189 (*\'*)
190 args="$args -- '"$(printf '%s\n' "$1" | LC_ALL=C sed "$escape");;
192 args="$args -- '$1'";;
193 esac
194 shift
197 if test $# -eq 0; then
198 set -- -
199 elif test 1 -lt $# && test $no_filename -eq 0; then
200 with_filename=1
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
206 grep="$grep -H"
209 exec 3>&1
210 res=1
212 for i
214 # Fail if gzip or grep (or sed) fails.
215 gzip_status=$(
216 exec 5>&1
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 || {
222 r=$?
223 if test $r -eq 1; then
224 printf '%s\n' "$i" || r=2
226 test 256 -le $r && r=$(expr 128 + $r % 128)
227 exit $r
229 elif $gnuish_grep && test "$i" != -; then
230 eval "$grep --label \"\$i\"$args"
231 elif $gnuish_grep || test $with_filename -eq 0; then
232 eval "$grep$args"
233 else
234 case $i in
236 '* | *'&'* | *'\'* | *'|'*)
237 icolon=$(printf '%s\n' "$i:" |
238 LC_ALL=C sed -e 's/[&\|]/\\&/g' -e '$!s/$/\\/') ||
239 icolon='(unknown filename):';;
240 (*) icolon="$i:";;
241 esac
242 sed_script="s|^|$icolon|"
244 # Fail if grep or sed fails.
245 r=$(
246 exec 4>&1
247 (eval "$grep$args" 4>&-; echo $? >&4) 3>&- |
248 LC_ALL=C sed "$sed_script" >&3 4>&-
249 ) || { r=$?; test $r -lt 2 && r=2; }
250 test 256 -le $r && r=$(expr 128 + $r % 128)
251 exit $r
252 fi >&3 5>&-
254 r=$?
256 # Ignore gzip status 2 or 141, as it is just a warning or broken pipe.
257 # gzip status 1 is an error, like grep status 2.
258 { test $gzip_status -eq 2 || test $gzip_status -eq 141; } && gzip_status=0
259 test $gzip_status -eq 1 && gzip_status=2
261 # Use the more serious of the grep and gzip statuses.
262 test $r -lt $gzip_status && r=$gzip_status
264 # Accumulate the greatest status, except consider 0 to be greater than 1.
265 if test $r -le 1 && test $res -le 1; then
266 test $r -lt $res
267 else
268 test $res -lt $r
269 fi && res=$r
271 # Exit immediately on a serious error.
272 test 126 -le $res && break
273 done
275 if test -n "$pattmp"; then
276 rm -f "$pattmp" || {
277 r=$?
278 test $r -lt 2 && r=2
279 test $res -lt $r && res=$r
281 trap - HUP INT PIPE TERM 0
284 test 128 -le $res && kill -$(expr $res % 128) $$
285 exit $res