zgrep revision 1.4.6.1 1 #!/bin/sh
2 #
3 # $NetBSD: zgrep,v 1.4.6.1 2008/05/11 07:17:39 jdc Exp $
4 #
5 # Copyright (c) 2003 Thomas Klausner.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # 3. The name of the author may not be used to endorse or promote products
16 # derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 grep=/usr/bin/grep
30 zcat=/usr/bin/zcat
31
32 endofopts=0
33 pattern_found=0
34 grep_args=""
35 silent=0
36
37 prg=$0
38
39 # handle being called 'zegrep' or 'zfgrep'
40 case ${prg} in
41 *zegrep)
42 grep_args="-E";;
43 *zfgrep)
44 grep_args="-F";;
45 esac
46
47 # skip all options and pass them on to grep taking care of options
48 # with arguments, and if -e was supplied
49
50 while [ $# -gt 0 -a ${endofopts} -eq 0 ]
51 do
52 case $1 in
53 # from GNU grep-2.5.1 -- keep in sync!
54 -[ABCDXdefm])
55 case $1 in
56 -e)
57 pattern_found=1;;
58 *)
59 ;;
60 esac
61
62 if [ $# -lt 2 ]
63 then
64 echo "${prg}: missing argument for ${saved_arg} flag" >&2
65 exit 1
66 fi
67 grep_args="${grep_args} $1 $2"
68 shift 2
69 ;;
70 --)
71 grep_args="${grep_args} $1"
72 shift
73 endofopts=1
74 ;;
75 -)
76 endofopts=1
77 ;;
78 -h)
79 silent=1
80 shift
81 ;;
82 -*)
83 grep_args="${grep_args} $1"
84 shift
85 ;;
86 *)
87 # pattern to grep for
88 endofopts=1
89 ;;
90 esac
91 done
92
93 # if no -e option was found, take next argument as grep-pattern
94 if [ ${pattern_found} -lt 1 ]
95 then
96 if [ $# -lt 1 ]
97 then
98 echo "${prg}: missing pattern" >&2
99 exit 1
100 fi
101 pattern=$1
102 shift
103 fi
104
105 # call grep ...
106 if [ $# -lt 1 ]
107 then
108 # ... on stdin
109 ${zcat} -fq - | ${grep} ${grep_args} "${pattern}" -
110 else
111 # ... on all files given on the command line
112 if [ ${silent} -lt 1 ]; then
113 grep_args="-H ${grep_args}"
114 fi
115 while [ $# -gt 0 ]
116 do
117 ${zcat} -fq -- "$1" | ${grep} --label="${1}" "${pattern}" ${grep_args} -
118 shift
119 done
120 fi
121
122 exit 0
123