* NEWS: gzip -q now exits with status 2 (not 1) on SIGPIPE.
[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 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"; export PATH
24
25 case "$0" in
26 (*egrep)        grep='${EGREP-egrep}'   ;;
27 (*fgrep)        grep='${FGREP-fgrep}'   ;;
28 (*)             grep='${GREP-grep}'     ;;
29 esac
30
31 version="z$grep (gzip) @VERSION@
32 Copyright (C) 2006 Free Software Foundation, Inc.
33 This is free software.  You may redistribute copies of it under the terms of
34 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
35 There is NO WARRANTY, to the extent permitted by law.
36
37 Written by Jean-loup Gailly."
38
39 usage="Usage: z$grep [OPTION]... [-e] PATTERN [FILE]...
40 Look for instances of PATTERN in the input FILEs, using their
41 uncompressed contents if they are compressed.
42
43 OPTIONs are the same as for '$grep'.
44
45 Report bugs to <bug-gzip@gnu.org>."
46
47 # sed script to escape all ' for the shell, and then (to handle trailing
48 # newlines correctly) turn trailing X on last line into '.
49 escape='
50   s/'\''/'\''\\'\'''\''/g
51   $s/X$/'\''/
52 '
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   optarg=
62
63   case $1 in
64   (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
65     option=$(expr "X$1" : 'X\(-.[0-9]*\)')
66     arg2=-\'$(expr "X$1X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
67     shift
68     eval "set X $arg2 "'${1+"$@"}';;
69   (--binary-*=* | --[lm]a*=* | --reg*=*)
70     ;;
71   (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
72     have_optarg=1
73     case ${2?"$1 option requires an argument"} in
74     (*\'*)
75       optarg=" '"$(printf '%sX\n' "$2" | sed "$escape");;
76     (*)
77       optarg=" '$2'";;
78     esac
79     shift;;
80   (--)
81     shift
82     break;;
83   (-*)
84     ;;
85   (*)
86     break;;
87   esac
88   shift
89
90   case $option in
91   (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
92     printf >&2 '%s: %s: option not supported\n' "$0" "$option"
93     exit 2;;
94   (-[ef]* | --file | --file=* | --reg*)
95     have_pat=1;;
96   (--h | --he | --hel | --help)
97     echo "$usage" || exit 2
98     exit;;
99   (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
100   | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
101   | --with-filename)
102     with_filename=1
103     continue;;
104   (-l | --files-with-*)
105     files_with_matches=1;;
106   (-L | --files-witho*)
107     files_without_matches=1;;
108   (--no-f*)
109     no_filename=1;;
110   (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
111     echo "$version" || exit 2
112     exit;;
113   esac
114
115   case $option in
116   (*\'?*)
117     option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
118   (*)
119     option="'$option'";;
120   esac
121
122   grep="$grep $option$optarg"
123 done
124
125 if test $have_pat -eq 0; then
126   case ${1?"missing pattern; try \`$0 --help' for help"} in
127   (*\'*)
128     grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
129   (*)
130     grep="$grep -- '$1'";;
131   esac
132   shift
133 fi
134
135 if test $# -eq 0; then
136   set X -
137   shift
138 fi
139
140 exec 3>&1
141 res=0
142
143 for i
144 do
145   # Fail if gzip or grep (or sed) fails.
146   gzip_status=$(
147     exec 5>&1
148     (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
149     if test $files_with_matches -eq 1; then
150       eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
151     elif test $files_without_matches -eq 1; then
152       eval "$grep" >/dev/null || {
153         r=$?
154         if test $r -eq 1; then
155           printf '%s\n' "$i" || r=2
156         fi
157         exit $r
158       }
159     elif test $with_filename -eq 0 &&
160          { test $# -eq 1 || test $no_filename -eq 1; }; then
161       eval "$grep"
162     else
163       case $i in
164       (*'
165 '* | *'&'* | *'\'* | *'|'*)
166         i=$(printf '%s\n' "$i" |
167             sed '
168               $!N
169               $s/[&\|]/\\&/g
170               $s/\n/\\n/g
171             ');;
172       esac
173       sed_script="s|^|$i:|"
174
175       # Fail if grep or sed fails.
176       r=$(
177         exec 4>&1
178         (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
179       ) || r=2
180       exit $r
181     fi >&3 5>&-
182   )
183   r=$?
184   test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
185   test $res -lt $r && res=$r
186 done
187 exit $res