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, 2010 Free Software
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.
27 --__bindir) bindir=${2?}; shift; shift;;
33 version='zgrep (gzip) @VERSION@
34 Copyright (C) 2010-2012 Free Software Foundation, Inc.
35 This is free software. You may redistribute copies of it under the terms of
36 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
37 There is NO WARRANTY, to the extent permitted by law.
39 Written by Jean-loup Gailly.'
41 usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
42 Look for instances of PATTERN in the input FILEs, using their
43 uncompressed contents if they are compressed.
45 OPTIONs are the same as for 'grep'.
47 Report bugs to <bug-gzip@gnu.org>."
49 # sed script to escape all ' for the shell, and then (to handle trailing
50 # newlines correctly) append ' to the last line.
52 s/'\''/'\''\\'\'''\''/g
58 files_without_matches=0
62 while test $# -ne 0; do
68 (-[0123456789EFGHIKLPRTUVZabchilnoqrsuvwxyz]?*)
69 if expr "X$option" : 'X-[0-9]\+$' > /dev/null; then
70 : # Let a multi-digit, digit-only option like -10 fall through.
72 arg2=-\'$(expr "X$option" : 'X-.[0-9]*\(.*\)' | sed "$escape")
73 eval "set -- $arg2 "'${1+"$@"}'
74 option=$(expr "X$option" : 'X\(-.[0-9]*\)')
76 (--binary-*=* | --[lm]a*=* | --reg*=*)
78 (-[ABCDXdefm] | binary-* | --file | --[lm]a* | --reg*)
79 case ${1?"$option option requires an argument"} in
81 optarg=" '"$(printf '%s\n' "$1" | sed "$escape");;
87 optarg=" '"$(expr "X$option" : 'X-f\(.*\)' | sed "$escape")
90 optarg=" '"$(expr "X$option" : 'X-f\(.*\)')\'
93 optarg=" '"$(expr "X$option" : 'X--file=\(.*\)' | sed "$escape")
96 optarg=" '"$(expr "X$option" : 'X--file=\(.*\)')\'
105 operands="$operands '"$(printf '%s\n' "$option" | sed "$escape");;
107 operands="$operands '$option'";;
109 ${POSIXLY_CORRECT+break}
114 (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
115 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
120 # The pattern is coming from a file rather than the command-line.
121 # If the file is actually stdin then we need to do a little
122 # magic, since we use stdin to pass the gzip output to grep.
123 # Turn the -f option into an -e option by copying the file's
124 # contents into OPTARG.
126 (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
128 optarg=" '"$(sed "$escape") || exit 2;;
131 (--h | --he | --hel | --help)
132 echo "$usage" || exit 2
134 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
135 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
139 (-l | --files-with-*)
140 files_with_matches=1;;
141 (-L | --files-witho*)
142 files_without_matches=1;;
145 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
146 echo "$version" || exit 2
152 option=\'$(printf '%s\n' "$option" | sed "$escape");;
157 grep="$grep $option$optarg"
160 eval "set -- $operands "'${1+"$@"}'
162 if test $have_pat -eq 0; then
163 case ${1?"missing pattern; try \`$0 --help' for help"} in
165 grep="$grep -- '"$(printf '%s\n' "$1" | sed "$escape");;
167 grep="$grep -- '$1'";;
172 if test $# -eq 0; then
181 # Fail if gzip or grep (or sed) fails.
184 (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
185 if test $files_with_matches -eq 1; then
186 eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
187 elif test $files_without_matches -eq 1; then
188 eval "$grep" >/dev/null || {
190 if test $r -eq 1; then
191 printf '%s\n' "$i" || r=2
195 elif test $with_filename -eq 0 &&
196 { test $# -eq 1 || test $no_filename -eq 1; }; then
201 '* | *'&'* | *'\'* | *'|'*)
202 i=$(printf '%s\n' "$i" |
209 sed_script="s|^|$i:|"
211 # Fail if grep or sed fails.
214 (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
217 test 1 -lt $r && exit $r || exit 2
221 test 128 -lt $r && exit $r
222 test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
223 test $res -lt $r && res=$r