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 Free Software Foundation
7 # Copyright (C) 1993 Jean-loup Gailly
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 PATH="BINDIR:$PATH"; export PATH
26 (*egrep) grep='${EGREP-egrep}' ;;
27 (*fgrep) grep='${FGREP-fgrep}' ;;
28 (*) grep='${GREP-grep}' ;;
31 eval 'version="z'"$grep"' (gzip) @VERSION@
32 Copyright (C) 2006 Free Software Foundation, Inc.
33 This is free software. You may redistribute copies of it under the terms of
34 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
35 There is NO WARRANTY, to the extent permitted by law.
37 Written by Jean-loup Gailly."'
39 usage="Usage: z$grep [OPTION]... [-e] PATTERN [FILE]...
40 Look for instances of PATTERN in the input FILEs, using their
41 uncompressed contents if they are compressed.
43 OPTIONs are the same as for '$grep'.
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) turn trailing X on last line into '.
50 s/'\''/'\''\\'\'''\''/g
56 files_without_matches=0
60 while test $# -ne 0; do
66 (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
67 arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
68 eval "set -- $arg2 "'${1+"$@"}'
69 option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
70 (--binary-*=* | --[lm]a*=* | --reg*=*)
72 (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
74 case ${1?"$option option requires an argument"} in
76 optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
88 operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
90 operands="$operands '$option'";;
92 ${POSIXLY_CORRECT+break}
97 (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
98 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
100 (-[ef]* | --file | --file=* | --reg*)
102 (--h | --he | --hel | --help)
103 echo "$usage" || exit 2
105 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
106 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
110 (-l | --files-with-*)
111 files_with_matches=1;;
112 (-L | --files-witho*)
113 files_without_matches=1;;
116 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
117 echo "$version" || exit 2
123 option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
128 grep="$grep $option$optarg"
131 eval "set -- $operands "'${1+"$@"}'
133 if test $have_pat -eq 0; then
134 case ${1?"missing pattern; try \`$0 --help' for help"} in
136 grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
138 grep="$grep -- '$1'";;
143 if test $# -eq 0; then
152 # Fail if gzip or grep (or sed) fails.
155 (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
156 if test $files_with_matches -eq 1; then
157 eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
158 elif test $files_without_matches -eq 1; then
159 eval "$grep" >/dev/null || {
161 if test $r -eq 1; then
162 printf '%s\n' "$i" || r=2
166 elif test $with_filename -eq 0 &&
167 { test $# -eq 1 || test $no_filename -eq 1; }; then
172 '* | *'&'* | *'\'* | *'|'*)
173 i=$(printf '%s\n' "$i" |
180 sed_script="s|^|$i:|"
182 # Fail if grep or sed fails.
185 (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
191 test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
192 test $res -lt $r && res=$r