Resurrect z*grep script to support gzip'ped files.
[dragonfly.git] / usr.bin / gzip / zgrep
1 #!/bin/sh
2 #
3 # $NetBSD: zgrep,v 1.7 2008/05/08 15:35:23 wiz Exp $
4 # $DragonFly: src/usr.bin/gzip/zgrep,v 1.1 2004/10/26 11:19:31 joerg Exp $
5 #
6 # Copyright (c) 2003 Thomas Klausner.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 grep=/usr/bin/grep
29 zcat=/usr/bin/zcat
30
31 endofopts=0
32 pattern_found=0
33 grep_args=""
34 hyphen=0
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             if [ $# -lt 2 ]
56                 then
57                 echo "${prg}: missing argument for $1 flag" >&2
58                 exit 1
59             fi
60             case $1 in
61                 -e)
62                     pattern="$2"
63                     pattern_found=1
64                     shift 2
65                     break
66                     ;;
67                 *)
68                     ;;
69             esac
70             grep_args="${grep_args} $1 $2"
71             shift 2
72             ;;
73         --)
74             shift
75             endofopts=1
76             ;;
77         -)
78             hyphen=1
79             shift
80             ;;
81         -h)
82             silent=1
83             shift
84             ;;
85         -*)
86             grep_args="${grep_args} $1"
87             shift
88             ;;
89         *)
90             # pattern to grep for
91             endofopts=1
92             ;;
93     esac
94 done
95
96 # if no -e option was found, take next argument as grep-pattern
97 if [ ${pattern_found} -lt 1 ]
98 then
99     if [ $# -ge 1 ]; then
100         pattern="$1"
101         shift
102     elif [ ${hyphen} -gt 0 ]; then
103         pattern="-"
104     else
105         echo "${prg}: missing pattern" >&2
106         exit 1
107     fi
108 fi
109
110 # call grep ...
111 if [ $# -lt 1 ]
112 then
113     # ... on stdin
114     ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
115 else
116     # ... on all files given on the command line
117     if [ ${silent} -lt 1 ]; then
118         grep_args="-H ${grep_args}"
119     fi
120     while [ $# -gt 0 ]
121     do
122         ${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
123         shift
124     done
125 fi
126
127 exit 0