zgrep revision 1.6
1#!/bin/sh
2#
3# $NetBSD: zgrep,v 1.6 2008/04/27 09:27:55 nakayama 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
29grep=/usr/bin/grep
30zcat=/usr/bin/zcat
31
32endofopts=0
33pattern_found=0
34grep_args=""
35hyphen=0
36silent=0
37
38prg=$0
39
40# handle being called 'zegrep' or 'zfgrep'
41case ${prg} in
42    *zegrep)
43	grep_args="-E";;
44    *zfgrep)
45	grep_args="-F";;
46esac
47
48# skip all options and pass them on to grep taking care of options
49# with arguments, and if -e was supplied
50
51while [ $# -gt 0 -a ${endofopts} -eq 0 ]
52do
53    case $1 in
54    # from GNU grep-2.5.1 -- keep in sync!
55	-[ABCDXdefm])
56	    if [ $# -lt 2 ]
57		then
58		echo "${prg}: missing argument for $1 flag" >&2
59		exit 1
60	    fi
61	    case $1 in
62		-e)
63		    pattern="$2"
64		    pattern_found=1
65		    shift 2
66		    break
67		    ;;
68		*)
69		    ;;
70	    esac
71	    grep_args="${grep_args} $1 $2"
72	    shift 2
73	    ;;
74	--)
75	    shift
76	    endofopts=1
77	    ;;
78	-)
79	    hyphen=1
80	    shift
81	    ;;
82	-h)
83	    silent=1
84	    shift
85	    ;;
86	-*)
87	    grep_args="${grep_args} $1"
88	    shift
89	    ;;
90	*)
91	    # pattern to grep for
92	    endofopts=1
93	    ;;
94    esac
95done
96
97# if no -e option was found, take next argument as grep-pattern
98if [ ${pattern_found} -lt 1 ]
99then
100    if [ $# -ge 1 ]; then
101	pattern="$1"
102	shift
103    elif [ ${hyphen} -gt 0 ]; then
104	pattern="-"
105    else
106	echo "${prg}: missing pattern" >&2
107	exit 1
108    fi
109fi
110
111# call grep ...
112if [ $# -lt 1 ]
113then
114    # ... on stdin
115    ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
116else
117    # ... on all files given on the command line
118    if [ ${silent} -lt 1 ]; then
119	grep_args="-H ${grep_args}"
120    fi
121    while [ $# -gt 0 ]
122    do
123	${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
124	shift
125    done
126fi
127
128exit 0
129