source: Daodan/MinGW/msys/1.0/bin/zgrep@ 1098

Last change on this file since 1098 was 1046, checked in by alloc, 8 years ago

Daodan: Added Windows MinGW and build batch file

File size: 5.5 KB
RevLine 
[1046]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 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
23PATH=${GZIP_BINDIR-'/usr/bin'}:$PATH
24grep='${GREP-grep}'
25
26version='z$grep (gzip) 1.3.12
27Copyright (C) 2007 Free Software Foundation, Inc.
28This is free software. You may redistribute copies of it under the terms of
29the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
30There is NO WARRANTY, to the extent permitted by law.
31
32Written by Jean-loup Gailly.'
33
34usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
35Look for instances of PATTERN in the input FILEs, using their
36uncompressed contents if they are compressed.
37
38OPTIONs are the same as for 'grep'.
39
40Report bugs to <bug-gzip@gnu.org>."
41
42# sed script to escape all ' for the shell, and then (to handle trailing
43# newlines correctly) turn trailing X on last line into '.
44escape='
45 s/'\''/'\''\\'\'''\''/g
46 $s/X$/'\''/
47'
48operands=
49have_pat=0
50pat_on_stdin=0
51files_with_matches=0
52files_without_matches=0
53no_filename=0
54with_filename=0
55
56while 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 # The pattern is coming from a file rather than the command-line.
97 # If the file is actually stdin then we need to do a little
98 # magic, (since we use stdin to pass the gzip output to grep).
99 # So find a free fd and change the argument to then use this
100 # file descriptor for the pattern.
101 case $optarg in
102 (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
103 pat_on_stdin=1
104 # Start search from 6 since the script already uses 3 and 5
105 for fd in $(seq 6 254); do
106 if test ! -e /dev/fd/$fd; then
107 pat_fd=$fd
108 break;
109 fi
110 done
111 optarg=/dev/fd/$pat_fd;
112 esac
113 have_pat=1;;
114 (--h | --he | --hel | --help)
115 echo "$usage" || exit 2
116 exit;;
117 (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
118 | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
119 | --with-filename)
120 with_filename=1
121 continue;;
122 (-l | --files-with-*)
123 files_with_matches=1;;
124 (-L | --files-witho*)
125 files_without_matches=1;;
126 (-h | --no-f*)
127 no_filename=1;;
128 (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
129 echo "$version" || exit 2
130 exit;;
131 esac
132
133 case $option in
134 (*\'?*)
135 option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
136 (*)
137 option="'$option'";;
138 esac
139
140 grep="$grep $option$optarg"
141done
142
143eval "set -- $operands "'${1+"$@"}'
144
145if test $have_pat -eq 0; then
146 case ${1?"missing pattern; try \`$0 --help' for help"} in
147 (*\'*)
148 grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
149 (*)
150 grep="$grep -- '$1'";;
151 esac
152 shift
153fi
154
155if test $# -eq 0; then
156 set -- -
157fi
158
159exec 3>&1
160res=0
161
162for i
163do
164 # Fail if gzip or grep (or sed) fails.
165 gzip_status=$(
166 exec 5>&1
167 if test $pat_on_stdin -eq 1; then
168 eval "exec $pat_fd<&0"
169 fi
170 (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
171 if test $files_with_matches -eq 1; then
172 eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
173 elif test $files_without_matches -eq 1; then
174 eval "$grep" >/dev/null || {
175 r=$?
176 if test $r -eq 1; then
177 printf '%s\n' "$i" || r=2
178 fi
179 exit $r
180 }
181 elif test $with_filename -eq 0 &&
182 { test $# -eq 1 || test $no_filename -eq 1; }; then
183 eval "$grep"
184 else
185 case $i in
186 (*'
187'* | *'&'* | *'\'* | *'|'*)
188 i=$(printf '%s\n' "$i" |
189 sed '
190 $!N
191 $s/[&\|]/\\&/g
192 $s/\n/\\n/g
193 ');;
194 esac
195 sed_script="s|^|$i:|"
196
197 # Fail if grep or sed fails.
198 r=$(
199 exec 4>&1
200 (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
201 ) || r=2
202 exit $r
203 fi >&3 5>&-
204 )
205 r=$?
206 test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
207 test $res -lt $r && res=$r
208done
209exit $res
Note: See TracBrowser for help on using the repository browser.