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