doc: correct a diagnostic in man page to match actual
[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 bindir=@bindir@
26 case $1 in
27 --__bindir) bindir=${2?}; shift; shift;;
28 esac
29 PATH=$bindir:$PATH
30
31 grep='${GREP-'\''@GREP@'\''}'
32
33 version='zgrep (gzip) @VERSION@
34 Copyright (C) 2010-2015 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', except that the following 'grep'
46 options are not supported: --dereference-recursive (-R), --directories (-d),
47 --exclude, --exclude-from, --exclude-dir, --include, --null (-Z),
48 --null-data (-z), and --recursive (-r).
49
50 Report bugs to <bug-gzip@gnu.org>."
51
52 # sed script to escape all ' for the shell, and then (to handle trailing
53 # newlines correctly) append ' to the last line.
54 escape='
55   s/'\''/'\''\\'\'''\''/g
56   $s/$/'\''/
57 '
58 operands=
59 have_pat=0
60 files_with_matches=0
61 files_without_matches=0
62 no_filename=0
63 with_filename=0
64
65 while test $# -ne 0; do
66   option=$1
67   shift
68   optarg=
69
70   case $option in
71   (-[0123456789EFGHIKLPRTUVZabchilnoqrsuvwxyz]*[!0123456789]*)
72     arg2=-\'$(expr "X$option" : 'X-.[0-9]*\(.*\)' | sed "$escape")
73     eval "set -- $arg2 "'${1+"$@"}'
74     option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
75   (--binary-*=* | --[lm]a*=* | --reg*=*)
76     ;;
77   (-[ABCDXdefm] | binary-* | --file | --[lm]a* | --reg*)
78     case ${1?"$option option requires an argument"} in
79     (*\'*)
80       optarg=" '"$(printf '%s\n' "$1" | sed "$escape");;
81     (*)
82       optarg=" '$1'";;
83     esac
84     shift;;
85   (-f?*\'*)
86     optarg=" '"$(expr "X$option" : 'X-f\(.*\)' | sed "$escape")
87     option=-f;;
88   (-f?*)
89     optarg=" '"$(expr "X$option" : 'X-f\(.*\)')\'
90     option=-f;;
91   (--file=*\'*)
92     optarg=" '"$(expr "X$option" : 'X--file=\(.*\)' | sed "$escape")
93     option=--file;;
94   (--file=*)
95     optarg=" '"$(expr "X$option" : 'X--file=\(.*\)')\'
96     option=--file;;
97   (--)
98     break;;
99   (-?*)
100     ;;
101   (*)
102     case $option in
103     (*\'*)
104       operands="$operands '"$(printf '%s\n' "$option" | sed "$escape");;
105     (*)
106       operands="$operands '$option'";;
107     esac
108     ${POSIXLY_CORRECT+break}
109     continue;;
110   esac
111
112   case $option in
113   (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
114     printf >&2 '%s: %s: option not supported\n' "$0" "$option"
115     exit 2;;
116   (-e* | --reg*)
117     have_pat=1;;
118   (-f | --file)
119     # The pattern is coming from a file rather than the command-line.
120     # If the file is actually stdin then we need to do a little
121     # magic, since we use stdin to pass the gzip output to grep.
122     # Turn the -f option into an -e option by copying the file's
123     # contents into OPTARG.
124     case $optarg in
125     (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
126       option=-e
127       optarg=" '"$(sed "$escape") || exit 2;;
128     esac
129     have_pat=1;;
130   (--h | --he | --hel | --help)
131     echo "$usage" || exit 2
132     exit;;
133   (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
134   | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
135   | --with-filename)
136     with_filename=1
137     continue;;
138   (-l | --files-with-*)
139     files_with_matches=1;;
140   (-L | --files-witho*)
141     files_without_matches=1;;
142   (-h | --no-f*)
143     no_filename=1;;
144   (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
145     echo "$version" || exit 2
146     exit;;
147   esac
148
149   case $option in
150   (*\'?*)
151     option=\'$(printf '%s\n' "$option" | sed "$escape");;
152   (*)
153     option="'$option'";;
154   esac
155
156   grep="$grep $option$optarg"
157 done
158
159 eval "set -- $operands "'${1+"$@"}'
160
161 if test $have_pat -eq 0; then
162   case ${1?"missing pattern; try \`$0 --help' for help"} in
163   (*\'*)
164     grep="$grep -- '"$(printf '%s\n' "$1" | sed "$escape");;
165   (*)
166     grep="$grep -- '$1'";;
167   esac
168   shift
169 fi
170
171 if test $# -eq 0; then
172   set -- -
173 fi
174
175 exec 3>&1
176 res=1
177
178 for i
179 do
180   # Fail if gzip or grep (or sed) fails.
181   gzip_status=$(
182     exec 5>&1
183     (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
184     if test $files_with_matches -eq 1; then
185       eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
186     elif test $files_without_matches -eq 1; then
187       eval "$grep" >/dev/null || {
188         r=$?
189         if test $r -eq 1; then
190           printf '%s\n' "$i" || r=2
191         fi
192         exit $r
193       }
194     elif test $with_filename -eq 0 &&
195          { test $# -eq 1 || test $no_filename -eq 1; }; then
196       eval "$grep"
197     else
198       case $i in
199       (*'
200 '* | *'&'* | *'\'* | *'|'*)
201         i=$(printf '%s\n' "$i" |
202             sed '
203               $!N
204               $s/[&\|]/\\&/g
205               $s/\n/\\n/g
206             ');;
207       esac
208       sed_script="s|^|$i:|"
209
210       # Fail if grep or sed fails.
211       r=$(
212         exec 4>&1
213         (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
214       ) && exit $r
215       r=$?
216       test 1 -lt $r && exit $r || exit 2
217     fi >&3 5>&-
218   )
219   r=$?
220
221   # Ignore gzip status 2, as it is just a warning.
222   # gzip status 1 is an error, like grep status 2.
223   test $gzip_status -eq 2 && gzip_status=0
224   test $gzip_status -eq 1 && gzip_status=2
225
226   # Use the more serious of the grep and gzip statuses.
227   test $r -lt $gzip_status && r=$gzip_status
228
229   # Exit immediately on software configuration error.
230   test 126 -le $r && exit $r
231
232   # Accumulate the greatest status, except consider 0 to be greater than 1.
233   if test $r -le 1 && test $res -le 1; then
234      test $r -lt $res
235   else
236      test $res -lt $r
237   fi && res=$r
238 done
239 exit $res