maint: pull in two release-related modules from gnulib
[gzip.git] / zgrep.in
blob590aea2549bfb522ef60b3e7690de4a44b388f20
1 #!/bin/sh
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>
6 # Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
7 # Copyright (C) 1993 Jean-loup Gailly
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 3 of the License, or
12 # (at your option) any later version.
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.
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.
23 bindir=@bindir@
24 case $1 in
25 --__bindir) bindir=${2?}; shift; shift;;
26 esac
27 PATH=$bindir:$PATH
29 grep='${GREP-grep}'
31 version='zgrep (gzip) @VERSION@
32 Copyright (C) 2007, 2009 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.
37 Written by Jean-loup Gailly.'
39 usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
40 Look for instances of PATTERN in the input FILEs, using their
41 uncompressed contents if they are compressed.
43 OPTIONs are the same as for 'grep'.
45 Report bugs to <bug-gzip@gnu.org>."
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$/'\''/
53 operands=
54 have_pat=0
55 files_with_matches=0
56 files_without_matches=0
57 no_filename=0
58 with_filename=0
60 while test $# -ne 0; do
61 option=$1
62 shift
63 optarg=
65 case $option in
66 (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
67 arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
68 eval "set -- $arg2 "'${1+"$@"}'
69 option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
70 (--binary-*=* | --[lm]a*=* | --reg*=*)
72 (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
73 case ${1?"$option option requires an argument"} in
74 (*\'*)
75 optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
76 (*)
77 optarg=" '$1'";;
78 esac
79 shift;;
80 (--)
81 break;;
82 (-?*)
84 (*)
85 case $option in
86 (*\'*)
87 operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
88 (*)
89 operands="$operands '$option'";;
90 esac
91 ${POSIXLY_CORRECT+break}
92 continue;;
93 esac
95 case $option in
96 (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
97 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
98 exit 2;;
99 (-[ef]* | --file | --file=* | --reg*)
100 have_pat=1;;
101 (--h | --he | --hel | --help)
102 echo "$usage" || exit 2
103 exit;;
104 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
105 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
106 | --with-filename)
107 with_filename=1
108 continue;;
109 (-l | --files-with-*)
110 files_with_matches=1;;
111 (-L | --files-witho*)
112 files_without_matches=1;;
113 (--no-f*)
114 no_filename=1;;
115 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
116 echo "$version" || exit 2
117 exit;;
118 esac
120 case $option in
121 (*\'?*)
122 option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
124 option="'$option'";;
125 esac
127 grep="$grep $option$optarg"
128 done
130 eval "set -- $operands "'${1+"$@"}'
132 if test $have_pat -eq 0; then
133 case ${1?"missing pattern; try \`$0 --help' for help"} in
134 (*\'*)
135 grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
137 grep="$grep -- '$1'";;
138 esac
139 shift
142 if test $# -eq 0; then
143 set -- -
146 exec 3>&1
147 res=0
149 for i
151 # Fail if gzip or grep (or sed) fails.
152 gzip_status=$(
153 exec 5>&1
154 (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
155 if test $files_with_matches -eq 1; then
156 eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
157 elif test $files_without_matches -eq 1; then
158 eval "$grep" >/dev/null || {
159 r=$?
160 if test $r -eq 1; then
161 printf '%s\n' "$i" || r=2
163 exit $r
165 elif test $with_filename -eq 0 &&
166 { test $# -eq 1 || test $no_filename -eq 1; }; then
167 eval "$grep"
168 else
169 case $i in
171 '* | *'&'* | *'\'* | *'|'*)
172 i=$(printf '%s\n' "$i" |
173 sed '
175 $s/[&\|]/\\&/g
176 $s/\n/\\n/g
177 ');;
178 esac
179 sed_script="s|^|$i:|"
181 # Fail if grep or sed fails.
182 r=$(
183 exec 4>&1
184 (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
185 ) || r=2
186 exit $r
187 fi >&3 5>&-
189 r=$?
190 test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
191 test $res -lt $r && res=$r
192 done
193 exit $res