]> git.cworth.org Git - gzip/blob - zgrep.in
4b1b50940cbaa00b0a65ad885974846573445599
[gzip] / zgrep.in
1 #!/bin/sh
2
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>
5
6 # Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
7 # Copyright (C) 1993 Jean-loup Gailly
8
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.
13
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.
18
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.
22
23 PATH=BINDIR:$PATH
24 grep='${GREP-grep}'
25
26 prog=`echo "$0" | sed 's|.*/||'`
27 case "$prog" in
28         *egrep) grep=${EGREP-egrep}     ;;
29         *fgrep) grep=${FGREP-fgrep}     ;;
30         *)      grep=${GREP-grep}       ;;
31 esac
32
33 version="z$grep (gzip) @VERSION@
34 Copyright (C) 2007 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.
38
39 Written by Jean-loup Gailly."
40
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.
44
45 OPTIONs are the same as for 'grep'.
46
47 Report bugs to <bug-gzip@gnu.org>."
48
49 # sed script to escape all ' for the shell, and then (to handle trailing
50 # newlines correctly) turn trailing X on last line into '.
51 escape='
52   s/'\''/'\''\\'\'''\''/g
53   $s/X$/'\''/
54 '
55 operands=
56 have_pat=0
57 files_with_matches=0
58 files_without_matches=0
59 no_filename=0
60 with_filename=0
61
62 while test $# -ne 0; do
63   option=$1
64   shift
65   optarg=
66
67   case $option in
68   (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
69     arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
70     eval "set -- $arg2 "'${1+"$@"}'
71     option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
72   (--binary-*=* | --[lm]a*=* | --reg*=*)
73     ;;
74   (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
75     case ${1?"$option option requires an argument"} in
76     (*\'*)
77       optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
78     (*)
79       optarg=" '$1'";;
80     esac
81     shift;;
82   (--)
83     break;;
84   (-?*)
85     ;;
86   (*)
87     case $option in
88     (*\'*)
89       operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
90     (*)
91       operands="$operands '$option'";;
92     esac
93     ${POSIXLY_CORRECT+break}
94     continue;;
95   esac
96
97   case $option in
98   (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
99     printf >&2 '%s: %s: option not supported\n' "$0" "$option"
100     exit 2;;
101   (-[ef]* | --file | --file=* | --reg*)
102     have_pat=1;;
103   (--h | --he | --hel | --help)
104     echo "$usage" || exit 2
105     exit;;
106   (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
107   | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
108   | --with-filename)
109     with_filename=1
110     continue;;
111   (-l | --files-with-*)
112     files_with_matches=1;;
113   (-L | --files-witho*)
114     files_without_matches=1;;
115   (--no-f*)
116     no_filename=1;;
117   (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
118     echo "$version" || exit 2
119     exit;;
120   esac
121
122   case $option in
123   (*\'?*)
124     option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
125   (*)
126     option="'$option'";;
127   esac
128
129   grep="$grep $option$optarg"
130 done
131
132 eval "set -- $operands "'${1+"$@"}'
133
134 if test $have_pat -eq 0; then
135   case ${1?"missing pattern; try \`$0 --help' for help"} in
136   (*\'*)
137     grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
138   (*)
139     grep="$grep -- '$1'";;
140   esac
141   shift
142 fi
143
144 if test $# -eq 0; then
145   set -- -
146 fi
147
148 exec 3>&1
149 res=0
150
151 for i
152 do
153   # Fail if gzip or grep (or sed) fails.
154   gzip_status=$(
155     exec 5>&1
156     (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
157     if test $files_with_matches -eq 1; then
158       eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
159     elif test $files_without_matches -eq 1; then
160       eval "$grep" >/dev/null || {
161         r=$?
162         if test $r -eq 1; then
163           printf '%s\n' "$i" || r=2
164         fi
165         exit $r
166       }
167     elif test $with_filename -eq 0 &&
168          { test $# -eq 1 || test $no_filename -eq 1; }; then
169       eval "$grep"
170     else
171       case $i in
172       (*'
173 '* | *'&'* | *'\'* | *'|'*)
174         i=$(printf '%s\n' "$i" |
175             sed '
176               $!N
177               $s/[&\|]/\\&/g
178               $s/\n/\\n/g
179             ');;
180       esac
181       sed_script="s|^|$i:|"
182
183       # Fail if grep or sed fails.
184       r=$(
185         exec 4>&1
186         (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
187       ) || r=2
188       exit $r
189     fi >&3 5>&-
190   )
191   r=$?
192   test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
193   test $res -lt $r && res=$r
194 done
195 exit $res