Imported Upstream version 1.3.9
[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 pat=""
48 after_dash_dash=""
49 files_with_matches=0
50 files_without_matches=0
51 no_filename=0
52 with_filename=0
53
54 while test $# -ne 0; do
55   case "$after_dash_dash$1" in
56   --d* | --rec*)        echo >&2 "$0: $1: option not supported"; exit 2;;
57   --h*)                 echo "$usage" || exit 2; exit;;
58   --files-with-*)       files_with_matches=1;;
59   --files-witho*)       files_without_matches=1;;
60   --no-f*)      no_filename=1;;
61   --v*)         echo "$version" || exit 2; exit;;
62   --wi*)        with_filename=1;;
63   --*)  ;;
64   -*)
65         case "$1" in
66         -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
67         esac
68         case "$1" in
69         -*H*)   with_filename=1;;
70         esac
71         case "$1" in
72         -*h*)   no_filename=1;;
73         esac
74         case "$1" in
75         -*L*)   files_without_matches=1;;
76         esac
77         case "$1" in
78         -*l*)   files_with_matches=1;;
79         esac;;
80   esac
81   case "$after_dash_dash$1" in
82   -[ef])   opt="$opt $1"; shift; pat="$1"
83            if test "$grep" = grep; then  # grep is buggy with -e on SVR4
84              grep=egrep
85            fi;;
86   -[ABCdm])opt="$opt $1 $2"; shift;;
87   --)      opt="$opt $1"; after_dash_dash=1;;
88   -*)      opt="$opt $1";;
89    *)      if test -z "$pat"; then
90              pat="$1"
91            else
92              break;
93            fi;;
94   esac
95   shift
96 done
97
98 if test -z "$pat"; then
99   echo "$usage"
100   exit 2
101 fi
102
103 if test $# -eq 0; then
104   gzip -cdfq | $grep $opt "$pat"
105   exit $?
106 fi
107
108 res=0
109 for i do
110   gzip -cdfq -- "$i" |
111     if test $files_with_matches -eq 1; then
112       $grep $opt "$pat" > /dev/null && printf '%s\n' "$i"
113     elif test $files_without_matches -eq 1; then
114       $grep $opt "$pat" > /dev/null || printf '%s\n' "$i"
115     elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
116       $grep $opt "$pat"
117     else
118       escaped=
119       while :; do
120         case $i in
121         *'
122 '*)
123           char='
124 '         repl='\\n';;
125         *'&'*) char='&' repl='\&';;
126         *'\'*) char='\\' repl='\\';;
127         *'|'*) char='|' repl='\|';;
128         *) break;;
129         esac
130         up_to_first_char="\\([^$char]*\\)"
131         after_first_char="[^$char]*$char\\(.*\\)"
132         escaped=$escaped`expr "X$i" : "X$up_to_first_char"`$repl
133         i=`expr "X$i" : "$after_first_char"`
134       done
135       if test $with_filename -eq 1; then
136         sed_script="s|[^:]*|$escaped$i|"
137       else
138         sed_script="s|^|$escaped$i:|"
139       fi
140
141       # Fail if either grep or sed fails.
142       # Bash has ${PIPESTATUS[0]}, but that's not portable.
143       exec 3>&1
144       r=`
145         exec 4>&1
146         ($grep $opt "$pat" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
147       ` &&
148       exit $r
149     fi
150   r=$?
151   test $res -lt $r && res=$r
152 done
153 exit $res