zgrep revision 1.6
11.1Swiz#!/bin/sh
21.2Swiz#
31.6Snakayama# $NetBSD: zgrep,v 1.6 2008/04/27 09:27:55 nakayama Exp $
41.2Swiz#
51.1Swiz# Copyright (c) 2003 Thomas Klausner.
61.1Swiz#
71.1Swiz# Redistribution and use in source and binary forms, with or without
81.1Swiz# modification, are permitted provided that the following conditions
91.1Swiz# are met:
101.1Swiz# 1. Redistributions of source code must retain the above copyright
111.1Swiz#    notice, this list of conditions and the following disclaimer.
121.1Swiz# 2. Redistributions in binary form must reproduce the above copyright
131.1Swiz#    notice, this list of conditions and the following disclaimer in the
141.1Swiz#    documentation and/or other materials provided with the distribution.
151.1Swiz# 3. The name of the author may not be used to endorse or promote products
161.1Swiz#    derived from this software without specific prior written permission.
171.1Swiz#
181.1Swiz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191.1Swiz# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201.1Swiz# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211.1Swiz# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221.1Swiz# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231.1Swiz# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241.1Swiz# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251.1Swiz# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261.1Swiz# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271.1Swiz# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281.1Swiz
291.1Swizgrep=/usr/bin/grep
301.1Swizzcat=/usr/bin/zcat
311.1Swiz
321.1Swizendofopts=0
331.1Swizpattern_found=0
341.1Swizgrep_args=""
351.5Syamthyphen=0
361.6Snakayamasilent=0
371.1Swiz
381.1Swizprg=$0
391.1Swiz
401.1Swiz# handle being called 'zegrep' or 'zfgrep'
411.1Swizcase ${prg} in
421.1Swiz    *zegrep)
431.1Swiz	grep_args="-E";;
441.1Swiz    *zfgrep)
451.1Swiz	grep_args="-F";;
461.1Swizesac
471.1Swiz
481.1Swiz# skip all options and pass them on to grep taking care of options
491.1Swiz# with arguments, and if -e was supplied
501.1Swiz
511.1Swizwhile [ $# -gt 0 -a ${endofopts} -eq 0 ]
521.1Swizdo
531.1Swiz    case $1 in
541.1Swiz    # from GNU grep-2.5.1 -- keep in sync!
551.1Swiz	-[ABCDXdefm])
561.5Syamt	    if [ $# -lt 2 ]
571.5Syamt		then
581.5Syamt		echo "${prg}: missing argument for $1 flag" >&2
591.5Syamt		exit 1
601.5Syamt	    fi
611.1Swiz	    case $1 in
621.1Swiz		-e)
631.5Syamt		    pattern="$2"
641.5Syamt		    pattern_found=1
651.5Syamt		    shift 2
661.5Syamt		    break
671.5Syamt		    ;;
681.1Swiz		*)
691.1Swiz		    ;;
701.1Swiz	    esac
711.1Swiz	    grep_args="${grep_args} $1 $2"
721.1Swiz	    shift 2
731.1Swiz	    ;;
741.1Swiz	--)
751.1Swiz	    shift
761.1Swiz	    endofopts=1
771.1Swiz	    ;;
781.1Swiz	-)
791.5Syamt	    hyphen=1
801.5Syamt	    shift
811.1Swiz	    ;;
821.6Snakayama	-h)
831.6Snakayama	    silent=1
841.6Snakayama	    shift
851.6Snakayama	    ;;
861.1Swiz	-*)
871.1Swiz	    grep_args="${grep_args} $1"
881.1Swiz	    shift
891.1Swiz	    ;;
901.1Swiz	*)
911.1Swiz	    # pattern to grep for
921.1Swiz	    endofopts=1
931.1Swiz	    ;;
941.1Swiz    esac
951.1Swizdone
961.1Swiz
971.1Swiz# if no -e option was found, take next argument as grep-pattern
981.1Swizif [ ${pattern_found} -lt 1 ]
991.1Swizthen
1001.5Syamt    if [ $# -ge 1 ]; then
1011.5Syamt	pattern="$1"
1021.5Syamt	shift
1031.5Syamt    elif [ ${hyphen} -gt 0 ]; then
1041.5Syamt	pattern="-"
1051.5Syamt    else
1061.1Swiz	echo "${prg}: missing pattern" >&2
1071.1Swiz	exit 1
1081.1Swiz    fi
1091.1Swizfi
1101.1Swiz
1111.1Swiz# call grep ...
1121.1Swizif [ $# -lt 1 ]
1131.1Swizthen
1141.1Swiz    # ... on stdin
1151.5Syamt    ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
1161.1Swizelse
1171.1Swiz    # ... on all files given on the command line
1181.6Snakayama    if [ ${silent} -lt 1 ]; then
1191.6Snakayama	grep_args="-H ${grep_args}"
1201.6Snakayama    fi
1211.1Swiz    while [ $# -gt 0 ]
1221.1Swiz    do
1231.6Snakayama	${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
1241.1Swiz	shift
1251.1Swiz    done
1261.1Swizfi
1271.1Swiz
1281.1Swizexit 0
129