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