zgrep revision 1.1
11.1Swiz#!/bin/sh
21.1Swiz
31.1Swiz# Copyright (c) 2003 Thomas Klausner.
41.1Swiz#
51.1Swiz# Redistribution and use in source and binary forms, with or without
61.1Swiz# modification, are permitted provided that the following conditions
71.1Swiz# are met:
81.1Swiz# 1. Redistributions of source code must retain the above copyright
91.1Swiz#    notice, this list of conditions and the following disclaimer.
101.1Swiz# 2. Redistributions in binary form must reproduce the above copyright
111.1Swiz#    notice, this list of conditions and the following disclaimer in the
121.1Swiz#    documentation and/or other materials provided with the distribution.
131.1Swiz# 3. The name of the author may not be used to endorse or promote products
141.1Swiz#    derived from this software without specific prior written permission.
151.1Swiz#
161.1Swiz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Swiz# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Swiz# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Swiz# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Swiz# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211.1Swiz# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221.1Swiz# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231.1Swiz# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241.1Swiz# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251.1Swiz# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261.1Swiz
271.1Swizgrep=/usr/bin/grep
281.1Swizzcat=/usr/bin/zcat
291.1Swiz
301.1Swizendofopts=0
311.1Swizpattern_found=0
321.1Swizgrep_args=""
331.1Swiz
341.1Swizprg=$0
351.1Swiz
361.1Swiz# handle being called 'zegrep' or 'zfgrep'
371.1Swizcase ${prg} in
381.1Swiz    *zegrep)
391.1Swiz	grep_args="-E";;
401.1Swiz    *zfgrep)
411.1Swiz	grep_args="-F";;
421.1Swizesac
431.1Swiz
441.1Swiz# skip all options and pass them on to grep taking care of options
451.1Swiz# with arguments, and if -e was supplied
461.1Swiz
471.1Swizwhile [ $# -gt 0 -a ${endofopts} -eq 0 ]
481.1Swizdo
491.1Swiz    case $1 in
501.1Swiz    # from GNU grep-2.5.1 -- keep in sync!
511.1Swiz	-[ABCDXdefm])
521.1Swiz	    case $1 in
531.1Swiz		-e)
541.1Swiz		    pattern_found=1;;
551.1Swiz		*)
561.1Swiz		    ;;
571.1Swiz	    esac
581.1Swiz
591.1Swiz	    if [ $# -lt 2 ]
601.1Swiz		then
611.1Swiz		echo "${prg}: missing argument for ${saved_arg} flag" >&2
621.1Swiz		exit 1
631.1Swiz	    fi
641.1Swiz	    grep_args="${grep_args} $1 $2"
651.1Swiz	    shift 2
661.1Swiz	    ;;
671.1Swiz	--)
681.1Swiz	    grep_args="${grep_args} $1"
691.1Swiz	    shift
701.1Swiz	    endofopts=1
711.1Swiz	    ;;
721.1Swiz	-)
731.1Swiz	    endofopts=1
741.1Swiz	    ;;
751.1Swiz	-*)
761.1Swiz	    grep_args="${grep_args} $1"
771.1Swiz	    shift
781.1Swiz	    ;;
791.1Swiz	*)
801.1Swiz	    # pattern to grep for
811.1Swiz	    endofopts=1
821.1Swiz	    ;;
831.1Swiz    esac
841.1Swizdone
851.1Swiz
861.1Swiz# if no -e option was found, take next argument as grep-pattern
871.1Swizif [ ${pattern_found} -lt 1 ]
881.1Swizthen
891.1Swiz    if [ $# -lt 1 ]
901.1Swiz    then
911.1Swiz	echo "${prg}: missing pattern" >&2
921.1Swiz	exit 1
931.1Swiz    fi
941.1Swiz    pattern=$1
951.1Swiz    shift
961.1Swizfi
971.1Swiz
981.1Swiz# call grep ...
991.1Swizif [ $# -lt 1 ]
1001.1Swizthen
1011.1Swiz    # ... on stdin
1021.1Swiz    ${zcat} - | ${grep} ${grep_args} ${pattern} -
1031.1Swizelse
1041.1Swiz    # ... on all files given on the command line
1051.1Swiz    while [ $# -gt 0 ]
1061.1Swiz    do
1071.1Swiz	${zcat} -- "$1" | ${grep} -H --label="${1}" ${pattern} ${grep_args} -
1081.1Swiz	shift
1091.1Swiz    done
1101.1Swizfi
1111.1Swiz
1121.1Swizexit 0
113