New upstream version 1.8
[debian/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, 2009-2016 Free Software Foundation,
7 # Inc.
8
9 # Copyright (C) 1993 Jean-loup Gailly
10
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.
15
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.
20
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.
24
25 grep='${GREP-'\''@GREP@'\''}'
26
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.
32
33 Written by Jean-loup Gailly.'
34
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.
38
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).
43
44 Report bugs to <bug-gzip@gnu.org>."
45
46 # sed script to escape all ' for the shell, and then (to handle trailing
47 # newlines correctly) append ' to the last line.
48 escape='
49   s/'\''/'\''\\'\'''\''/g
50   $s/$/'\''/
51 '
52 operands=
53 have_pat=0
54 files_with_matches=0
55 files_without_matches=0
56 no_filename=0
57 with_filename=0
58 pattmp=
59
60 while test $# -ne 0; do
61   option=$1
62   shift
63   optarg=
64
65   case $option in
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*=*)
71     ;;
72   (-[ABCDXdefm] | binary-* | --file | --[lm]a* | --reg*)
73     case ${1?"$option option requires an argument"} in
74     (*\'*)
75       optarg=" '"$(printf '%s\n' "$1" | sed "$escape");;
76     (*)
77       optarg=" '$1'";;
78     esac
79     shift;;
80   (-f?*\'*)
81     optarg=" '"$(expr "X$option" : 'X-f\(.*\)' | sed "$escape")
82     option=-f;;
83   (-f?*)
84     optarg=" '"$(expr "X$option" : 'X-f\(.*\)')\'
85     option=-f;;
86   (--file=*\'*)
87     optarg=" '"$(expr "X$option" : 'X--file=\(.*\)' | sed "$escape")
88     option=--file;;
89   (--file=*)
90     optarg=" '"$(expr "X$option" : 'X--file=\(.*\)')\'
91     option=--file;;
92   (--)
93     break;;
94   (-?*)
95     ;;
96   (*)
97     case $option in
98     (*\'*)
99       operands="$operands '"$(printf '%s\n' "$option" | sed "$escape");;
100     (*)
101       operands="$operands '$option'";;
102     esac
103     ${POSIXLY_CORRECT+break}
104     continue;;
105   esac
106
107   case $option in
108   (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
109     printf >&2 '%s: %s: option not supported\n' "$0" "$option"
110     exit 2;;
111   (-e* | --reg*)
112     have_pat=1;;
113   (-f | --file)
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.
121     if case $optarg in
122        (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
123          :;;
124        (*)
125          eval "test ! -f$optarg";;
126        esac
127     then
128       if test -n "$pattmp"; then
129         eval "cat --$optarg" >>"$pattmp" || exit 2
130         continue
131       fi
132       trap '
133         test -n "$pattmp" && rm -f "$pattmp"
134         (exit 2); exit 2
135       ' HUP INT PIPE TERM 0
136       if type mktemp >/dev/null 2>&1; then
137         pattmp=$(mktemp) || exit 2
138       else
139         set -C
140         pattmp=${TMPDIR-/tmp}/zgrep.$$
141       fi
142       eval "cat --$optarg" >"$pattmp" || exit 2
143       optarg=' "$pattmp"'
144     fi
145     have_pat=1;;
146   (--h | --he | --hel | --help)
147     echo "$usage" || exit 2
148     exit;;
149   (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
150   | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
151   | --with-filename)
152     with_filename=1
153     continue;;
154   (-l | --files-with-*)
155     files_with_matches=1;;
156   (-L | --files-witho*)
157     files_without_matches=1;;
158   (-h | --no-f*)
159     no_filename=1;;
160   (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
161     echo "$version" || exit 2
162     exit;;
163   esac
164
165   case $option in
166   (*\'?*)
167     option=\'$(printf '%s\n' "$option" | sed "$escape");;
168   (*)
169     option="'$option'";;
170   esac
171
172   grep="$grep $option$optarg"
173 done
174
175 eval "set -- $operands "'${1+"$@"}'
176
177 if test $have_pat -eq 0; then
178   case ${1?"missing pattern; try \`$0 --help' for help"} in
179   (*\'*)
180     grep="$grep -- '"$(printf '%s\n' "$1" | sed "$escape");;
181   (*)
182     grep="$grep -- '$1'";;
183   esac
184   shift
185 fi
186
187 if test $# -eq 0; then
188   set -- -
189 fi
190
191 exec 3>&1
192 res=1
193
194 for i
195 do
196   # Fail if gzip or grep (or sed) fails.
197   gzip_status=$(
198     exec 5>&1
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 || {
204         r=$?
205         if test $r -eq 1; then
206           printf '%s\n' "$i" || r=2
207         fi
208         test 256 -le $r && r=$(expr 128 + $r % 128)
209         exit $r
210       }
211     elif test $with_filename -eq 0 &&
212          { test $# -eq 1 || test $no_filename -eq 1; }; then
213       eval "$grep"
214     else
215       case $i in
216       (*'
217 '* | *'&'* | *'\'* | *'|'*)
218         i=$(printf '%s\n' "$i" |
219             sed '
220               $!N
221               $s/[&\|]/\\&/g
222               $s/\n/\\n/g
223             ');;
224       esac
225       sed_script="s|^|$i:|"
226
227       # Fail if grep or sed fails.
228       r=$(
229         exec 4>&1
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)
233       exit $r
234     fi >&3 5>&-
235   )
236   r=$?
237
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
242
243   # Use the more serious of the grep and gzip statuses.
244   test $r -lt $gzip_status && r=$gzip_status
245
246   # Accumulate the greatest status, except consider 0 to be greater than 1.
247   if test $r -le 1 && test $res -le 1; then
248      test $r -lt $res
249   else
250      test $res -lt $r
251   fi && res=$r
252
253   # Exit immediately on a serious error.
254   test 126 -le $res && break
255 done
256
257 if test -n "$pattmp"; then
258   rm -f "$pattmp" || {
259     r=$?
260     test $r -lt 2 && r=2
261     test $res -lt $r && res=$r
262   }
263   trap - HUP INT PIPE TERM 0
264 fi
265
266 test 128 -le $res && kill -$(expr $res % 128) $$
267 exit $res