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-2016 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@'\''}'
27 version='zgrep (gzip) @VERSION@
28 Copyright (C) 2010-2016 Free Software Foundation, Inc.
29 This is free software. You may redistribute copies of it under the terms of
30 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
31 There is NO WARRANTY, to the extent permitted by law.
33 Written by Jean-loup Gailly.'
35 usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
36 Look for instances of PATTERN in the input FILEs, using their
37 uncompressed contents if they are compressed.
39 OPTIONs are the same as for 'grep', except that the following 'grep'
40 options are not supported: --dereference-recursive (-R), --directories (-d),
41 --exclude, --exclude-from, --exclude-dir, --include, --null (-Z),
42 --null-data (-z), and --recursive (-r).
44 Report bugs to <bug-gzip@gnu.org>."
46 # sed script to escape all ' for the shell, and then (to handle trailing
47 # newlines correctly) append ' to the last line.
49 s/'\''/'\''\\'\'''\''/g
55 files_without_matches=0
60 while test $# -ne 0; do
66 (-[0123456789EFGHIKLPRTUVZabchilnoqrsuvwxyz]*[!0123456789]*)
67 arg2=-\'$(expr "X$option" : 'X-.[0-9]*\(.*\)' | sed "$escape")
68 eval "set -- $arg2 "'${1+"$@"}'
69 option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
70 (--binary-*=* | --[lm]a*=* | --reg*=*)
72 (-[ABCDXdefm] | binary-* | --file | --[lm]a* | --reg*)
73 case ${1?"$option option requires an argument"} in
75 optarg=" '"$(printf '%s\n' "$1" | sed "$escape");;
81 optarg=" '"$(expr "X$option" : 'X-f\(.*\)' | sed "$escape")
84 optarg=" '"$(expr "X$option" : 'X-f\(.*\)')\'
87 optarg=" '"$(expr "X$option" : 'X--file=\(.*\)' | sed "$escape")
90 optarg=" '"$(expr "X$option" : 'X--file=\(.*\)')\'
99 operands="$operands '"$(printf '%s\n' "$option" | sed "$escape");;
101 operands="$operands '$option'";;
103 ${POSIXLY_CORRECT+break}
108 (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
109 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
114 # The pattern is coming from a file rather than the command-line.
115 # If the file is actually stdin then we need to do a little
116 # magic, since we use stdin to pass the gzip output to grep.
117 # Similarly if it is not a regular file, since it might be read repeatedly.
118 # In either of these two cases, copy the pattern into a temporary file,
119 # and use that file instead. The pattern might contain null bytes,
120 # so we cannot simply switch to -e here.
122 (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
125 eval "test ! -f$optarg";;
128 if test -n "$pattmp"; then
129 eval "cat --$optarg" >>"$pattmp" || exit 2
133 test -n "$pattmp" && rm -f "$pattmp"
135 ' HUP INT PIPE TERM 0
136 if type mktemp >/dev/null 2>&1; then
137 pattmp=$(mktemp) || exit 2
140 pattmp=${TMPDIR-/tmp}/zgrep.$$
142 eval "cat --$optarg" >"$pattmp" || exit 2
146 (--h | --he | --hel | --help)
147 echo "$usage" || exit 2
149 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
150 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
154 (-l | --files-with-*)
155 files_with_matches=1;;
156 (-L | --files-witho*)
157 files_without_matches=1;;
160 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
161 echo "$version" || exit 2
167 option=\'$(printf '%s\n' "$option" | sed "$escape");;
172 grep="$grep $option$optarg"
175 eval "set -- $operands "'${1+"$@"}'
177 if test $have_pat -eq 0; then
178 case ${1?"missing pattern; try \`$0 --help' for help"} in
180 grep="$grep -- '"$(printf '%s\n' "$1" | sed "$escape");;
182 grep="$grep -- '$1'";;
187 if test $# -eq 0; then
196 # Fail if gzip or grep (or sed) fails.
199 (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
200 if test $files_with_matches -eq 1; then
201 eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
202 elif test $files_without_matches -eq 1; then
203 eval "$grep" >/dev/null || {
205 if test $r -eq 1; then
206 printf '%s\n' "$i" || r=2
208 test 256 -le $r && r=$(expr 128 + $r % 128)
211 elif test $with_filename -eq 0 &&
212 { test $# -eq 1 || test $no_filename -eq 1; }; then
217 '* | *'&'* | *'\'* | *'|'*)
218 i=$(printf '%s\n' "$i" |
225 sed_script="s|^|$i:|"
227 # Fail if grep or sed fails.
230 (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
231 ) || { r=$?; test $r -lt 2 && r=2; }
232 test 256 -le $r && r=$(expr 128 + $r % 128)
238 # Ignore gzip status 2, as it is just a warning.
239 # gzip status 1 is an error, like grep status 2.
240 test $gzip_status -eq 2 && gzip_status=0
241 test $gzip_status -eq 1 && gzip_status=2
243 # Use the more serious of the grep and gzip statuses.
244 test $r -lt $gzip_status && r=$gzip_status
246 # Accumulate the greatest status, except consider 0 to be greater than 1.
247 if test $r -le 1 && test $res -le 1; then
253 # Exit immediately on a serious error.
254 test 126 -le $res && break
257 if test -n "$pattmp"; then
261 test $res -lt $r && res=$r
263 trap - HUP INT PIPE TERM 0
266 test 128 -le $res && kill -$(expr $res % 128) $$