1 | #!/bin/bash
|
---|
2 | ## !/bin/sh - stupid stupid bashisms.
|
---|
3 |
|
---|
4 | # zgrep -- a wrapper around a grep program that decompresses files as needed
|
---|
5 | # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
|
---|
6 |
|
---|
7 | # Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
|
---|
8 | # Copyright (C) 1993 Jean-loup Gailly
|
---|
9 |
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 |
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 |
|
---|
20 | # You should have received a copy of the GNU General Public License along
|
---|
21 | # with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
22 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
---|
23 |
|
---|
24 | PATH="BINDIR;$PATH"
|
---|
25 | grep='${GREP-grep}'
|
---|
26 |
|
---|
27 | version='zgrep (gzip) @VERSION@
|
---|
28 | Copyright (C) 2007 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'.
|
---|
40 |
|
---|
41 | Report bugs to <bug-gzip@gnu.org>."
|
---|
42 |
|
---|
43 | # sed script to escape all ' for the shell, and then (to handle trailing
|
---|
44 | # newlines correctly) turn trailing X on last line into '.
|
---|
45 | escape='
|
---|
46 | s/'\''/'\''\\'\'''\''/g
|
---|
47 | $s/X$/'\''/
|
---|
48 | '
|
---|
49 | operands=
|
---|
50 | have_pat=0
|
---|
51 | files_with_matches=0
|
---|
52 | files_without_matches=0
|
---|
53 | no_filename=0
|
---|
54 | with_filename=0
|
---|
55 |
|
---|
56 | while test $# -ne 0; do
|
---|
57 | option=$1
|
---|
58 | shift
|
---|
59 | optarg=
|
---|
60 |
|
---|
61 | case $option in
|
---|
62 | (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
|
---|
63 | arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
|
---|
64 | eval "set -- $arg2 "'${1+"$@"}'
|
---|
65 | option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
|
---|
66 | (--binary-*=* | --[lm]a*=* | --reg*=*)
|
---|
67 | ;;
|
---|
68 | (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
|
---|
69 | case ${1?"$option option requires an argument"} in
|
---|
70 | (*\'*)
|
---|
71 | optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
|
---|
72 | (*)
|
---|
73 | optarg=" '$1'";;
|
---|
74 | esac
|
---|
75 | shift;;
|
---|
76 | (--)
|
---|
77 | break;;
|
---|
78 | (-?*)
|
---|
79 | ;;
|
---|
80 | (*)
|
---|
81 | case $option in
|
---|
82 | (*\'*)
|
---|
83 | operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
|
---|
84 | (*)
|
---|
85 | operands="$operands '$option'";;
|
---|
86 | esac
|
---|
87 | ${POSIXLY_CORRECT+break}
|
---|
88 | continue;;
|
---|
89 | esac
|
---|
90 |
|
---|
91 | case $option in
|
---|
92 | (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
|
---|
93 | printf >&2 '%s: %s: option not supported\n' "$0" "$option"
|
---|
94 | exit 2;;
|
---|
95 | (-[ef]* | --file | --file=* | --reg*)
|
---|
96 | have_pat=1;;
|
---|
97 | (--h | --he | --hel | --help)
|
---|
98 | echo "$usage" || exit 2
|
---|
99 | exit;;
|
---|
100 | (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
|
---|
101 | | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
|
---|
102 | | --with-filename)
|
---|
103 | with_filename=1
|
---|
104 | continue;;
|
---|
105 | (-l | --files-with-*)
|
---|
106 | files_with_matches=1;;
|
---|
107 | (-L | --files-witho*)
|
---|
108 | files_without_matches=1;;
|
---|
109 | (--no-f*)
|
---|
110 | no_filename=1;;
|
---|
111 | (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
|
---|
112 | echo "$version" || exit 2
|
---|
113 | exit;;
|
---|
114 | esac
|
---|
115 |
|
---|
116 | case $option in
|
---|
117 | (*\'?*)
|
---|
118 | option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
|
---|
119 | (*)
|
---|
120 | option="'$option'";;
|
---|
121 | esac
|
---|
122 |
|
---|
123 | grep="$grep $option$optarg"
|
---|
124 | done
|
---|
125 |
|
---|
126 | eval "set -- $operands "'${1+"$@"}'
|
---|
127 |
|
---|
128 | if test $have_pat -eq 0; then
|
---|
129 | case ${1?"missing pattern; try \`$0 --help' for help"} in
|
---|
130 | (*\'*)
|
---|
131 | grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
|
---|
132 | (*)
|
---|
133 | grep="$grep -- '$1'";;
|
---|
134 | esac
|
---|
135 | shift
|
---|
136 | fi
|
---|
137 |
|
---|
138 | if test $# -eq 0; then
|
---|
139 | set -- -
|
---|
140 | fi
|
---|
141 |
|
---|
142 | exec 3>&1
|
---|
143 | res=0
|
---|
144 |
|
---|
145 | for i
|
---|
146 | do
|
---|
147 | # Fail if gzip or grep (or sed) fails.
|
---|
148 | gzip_status=$(
|
---|
149 | exec 5>&1
|
---|
150 | (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
|
---|
151 | if test $files_with_matches -eq 1; then
|
---|
152 | eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
|
---|
153 | elif test $files_without_matches -eq 1; then
|
---|
154 | eval "$grep" >/dev/null || {
|
---|
155 | r=$?
|
---|
156 | if test $r -eq 1; then
|
---|
157 | printf '%s\n' "$i" || r=2
|
---|
158 | fi
|
---|
159 | exit $r
|
---|
160 | }
|
---|
161 | elif test $with_filename -eq 0 &&
|
---|
162 | { test $# -eq 1 || test $no_filename -eq 1; }; then
|
---|
163 | eval "$grep"
|
---|
164 | else
|
---|
165 | case $i in
|
---|
166 | (*'
|
---|
167 | '* | *'&'* | *'\'* | *'|'*)
|
---|
168 | i=$(printf '%s\n' "$i" |
|
---|
169 | sed '
|
---|
170 | $!N
|
---|
171 | $s/[&\|]/\\&/g
|
---|
172 | $s/\n/\\n/g
|
---|
173 | ');;
|
---|
174 | esac
|
---|
175 | sed_script="s|^|$i:|"
|
---|
176 |
|
---|
177 | # Fail if grep or sed fails.
|
---|
178 | r=$(
|
---|
179 | exec 4>&1
|
---|
180 | (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
|
---|
181 | ) || r=2
|
---|
182 | exit $r
|
---|
183 | fi >&3 5>&-
|
---|
184 | )
|
---|
185 | r=$?
|
---|
186 | test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
|
---|
187 | test $res -lt $r && res=$r
|
---|
188 | done
|
---|
189 | exit $res
|
---|