zgrep revision 1.9
11.1Swiz#!/bin/sh
21.2Swiz#
31.9Snakayama# $NetBSD: zgrep,v 1.9 2015/07/06 12:05:40 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#
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.5Syamthyphen=0
341.6Snakayamasilent=0
351.1Swiz
361.8Spgoyetteprg=$(basename $0)
371.1Swiz
381.1Swiz# handle being called 'zegrep' or 'zfgrep'
391.1Swizcase ${prg} in
401.1Swiz    *zegrep)
411.1Swiz	grep_args="-E";;
421.1Swiz    *zfgrep)
431.1Swiz	grep_args="-F";;
441.1Swizesac
451.1Swiz
461.1Swiz# skip all options and pass them on to grep taking care of options
471.1Swiz# with arguments, and if -e was supplied
481.1Swiz
491.1Swizwhile [ $# -gt 0 -a ${endofopts} -eq 0 ]
501.1Swizdo
511.1Swiz    case $1 in
521.1Swiz    # from GNU grep-2.5.1 -- keep in sync!
531.1Swiz	-[ABCDXdefm])
541.5Syamt	    if [ $# -lt 2 ]
551.5Syamt		then
561.5Syamt		echo "${prg}: missing argument for $1 flag" >&2
571.5Syamt		exit 1
581.5Syamt	    fi
591.1Swiz	    case $1 in
601.1Swiz		-e)
611.5Syamt		    pattern="$2"
621.5Syamt		    pattern_found=1
631.5Syamt		    shift 2
641.5Syamt		    break
651.5Syamt		    ;;
661.1Swiz		*)
671.1Swiz		    ;;
681.1Swiz	    esac
691.1Swiz	    grep_args="${grep_args} $1 $2"
701.1Swiz	    shift 2
711.1Swiz	    ;;
721.1Swiz	--)
731.1Swiz	    shift
741.1Swiz	    endofopts=1
751.1Swiz	    ;;
761.1Swiz	-)
771.5Syamt	    hyphen=1
781.5Syamt	    shift
791.1Swiz	    ;;
801.6Snakayama	-h)
811.6Snakayama	    silent=1
821.6Snakayama	    shift
831.6Snakayama	    ;;
841.1Swiz	-*)
851.1Swiz	    grep_args="${grep_args} $1"
861.1Swiz	    shift
871.1Swiz	    ;;
881.1Swiz	*)
891.1Swiz	    # pattern to grep for
901.1Swiz	    endofopts=1
911.1Swiz	    ;;
921.1Swiz    esac
931.1Swizdone
941.1Swiz
951.1Swiz# if no -e option was found, take next argument as grep-pattern
961.1Swizif [ ${pattern_found} -lt 1 ]
971.1Swizthen
981.5Syamt    if [ $# -ge 1 ]; then
991.5Syamt	pattern="$1"
1001.5Syamt	shift
1011.5Syamt    elif [ ${hyphen} -gt 0 ]; then
1021.5Syamt	pattern="-"
1031.5Syamt    else
1041.1Swiz	echo "${prg}: missing pattern" >&2
1051.1Swiz	exit 1
1061.1Swiz    fi
1071.1Swizfi
1081.1Swiz
1091.1Swiz# call grep ...
1101.1Swizif [ $# -lt 1 ]
1111.1Swizthen
1121.1Swiz    # ... on stdin
1131.5Syamt    ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
1141.1Swizelse
1151.1Swiz    # ... on all files given on the command line
1161.9Snakayama    if [ ${silent} -lt 1 -a $# -gt 1 ]; then
1171.6Snakayama	grep_args="-H ${grep_args}"
1181.6Snakayama    fi
1191.1Swiz    while [ $# -gt 0 ]
1201.1Swiz    do
1211.6Snakayama	${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
1221.1Swiz	shift
1231.1Swiz    done
1241.1Swizfi
1251.1Swiz
1261.1Swizexit 0
127