xref: /minix3/usr.bin/gzip/zgrep (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
15a645f22SBen Gras#!/bin/sh
25a645f22SBen Gras#
3*0a6a1f1dSLionel Sambuc# $NetBSD: zgrep,v 1.9 2015/07/06 12:05:40 nakayama Exp $
45a645f22SBen Gras#
55a645f22SBen Gras# Copyright (c) 2003 Thomas Klausner.
65a645f22SBen Gras#
75a645f22SBen Gras# Redistribution and use in source and binary forms, with or without
85a645f22SBen Gras# modification, are permitted provided that the following conditions
95a645f22SBen Gras# are met:
105a645f22SBen Gras# 1. Redistributions of source code must retain the above copyright
115a645f22SBen Gras#    notice, this list of conditions and the following disclaimer.
125a645f22SBen Gras# 2. Redistributions in binary form must reproduce the above copyright
135a645f22SBen Gras#    notice, this list of conditions and the following disclaimer in the
145a645f22SBen Gras#    documentation and/or other materials provided with the distribution.
155a645f22SBen Gras#
165a645f22SBen Gras# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175a645f22SBen Gras# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185a645f22SBen Gras# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195a645f22SBen Gras# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205a645f22SBen Gras# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
215a645f22SBen Gras# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
225a645f22SBen Gras# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
235a645f22SBen Gras# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245a645f22SBen Gras# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255a645f22SBen Gras# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265a645f22SBen Gras
275a645f22SBen Grasgrep=/usr/bin/grep
285a645f22SBen Graszcat=/usr/bin/zcat
295a645f22SBen Gras
305a645f22SBen Grasendofopts=0
315a645f22SBen Graspattern_found=0
325a645f22SBen Grasgrep_args=""
335a645f22SBen Grashyphen=0
345a645f22SBen Grassilent=0
355a645f22SBen Gras
3684d9c625SLionel Sambucprg=$(basename $0)
375a645f22SBen Gras
385a645f22SBen Gras# handle being called 'zegrep' or 'zfgrep'
395a645f22SBen Grascase ${prg} in
405a645f22SBen Gras    *zegrep)
415a645f22SBen Gras	grep_args="-E";;
425a645f22SBen Gras    *zfgrep)
435a645f22SBen Gras	grep_args="-F";;
445a645f22SBen Grasesac
455a645f22SBen Gras
465a645f22SBen Gras# skip all options and pass them on to grep taking care of options
475a645f22SBen Gras# with arguments, and if -e was supplied
485a645f22SBen Gras
495a645f22SBen Graswhile [ $# -gt 0 -a ${endofopts} -eq 0 ]
505a645f22SBen Grasdo
515a645f22SBen Gras    case $1 in
525a645f22SBen Gras    # from GNU grep-2.5.1 -- keep in sync!
535a645f22SBen Gras	-[ABCDXdefm])
545a645f22SBen Gras	    if [ $# -lt 2 ]
555a645f22SBen Gras		then
565a645f22SBen Gras		echo "${prg}: missing argument for $1 flag" >&2
575a645f22SBen Gras		exit 1
585a645f22SBen Gras	    fi
595a645f22SBen Gras	    case $1 in
605a645f22SBen Gras		-e)
615a645f22SBen Gras		    pattern="$2"
625a645f22SBen Gras		    pattern_found=1
635a645f22SBen Gras		    shift 2
645a645f22SBen Gras		    break
655a645f22SBen Gras		    ;;
665a645f22SBen Gras		*)
675a645f22SBen Gras		    ;;
685a645f22SBen Gras	    esac
695a645f22SBen Gras	    grep_args="${grep_args} $1 $2"
705a645f22SBen Gras	    shift 2
715a645f22SBen Gras	    ;;
725a645f22SBen Gras	--)
735a645f22SBen Gras	    shift
745a645f22SBen Gras	    endofopts=1
755a645f22SBen Gras	    ;;
765a645f22SBen Gras	-)
775a645f22SBen Gras	    hyphen=1
785a645f22SBen Gras	    shift
795a645f22SBen Gras	    ;;
805a645f22SBen Gras	-h)
815a645f22SBen Gras	    silent=1
825a645f22SBen Gras	    shift
835a645f22SBen Gras	    ;;
845a645f22SBen Gras	-*)
855a645f22SBen Gras	    grep_args="${grep_args} $1"
865a645f22SBen Gras	    shift
875a645f22SBen Gras	    ;;
885a645f22SBen Gras	*)
895a645f22SBen Gras	    # pattern to grep for
905a645f22SBen Gras	    endofopts=1
915a645f22SBen Gras	    ;;
925a645f22SBen Gras    esac
935a645f22SBen Grasdone
945a645f22SBen Gras
955a645f22SBen Gras# if no -e option was found, take next argument as grep-pattern
965a645f22SBen Grasif [ ${pattern_found} -lt 1 ]
975a645f22SBen Grasthen
985a645f22SBen Gras    if [ $# -ge 1 ]; then
995a645f22SBen Gras	pattern="$1"
1005a645f22SBen Gras	shift
1015a645f22SBen Gras    elif [ ${hyphen} -gt 0 ]; then
1025a645f22SBen Gras	pattern="-"
1035a645f22SBen Gras    else
1045a645f22SBen Gras	echo "${prg}: missing pattern" >&2
1055a645f22SBen Gras	exit 1
1065a645f22SBen Gras    fi
1075a645f22SBen Grasfi
1085a645f22SBen Gras
1095a645f22SBen Gras# call grep ...
1105a645f22SBen Grasif [ $# -lt 1 ]
1115a645f22SBen Grasthen
1125a645f22SBen Gras    # ... on stdin
1135a645f22SBen Gras    ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
1145a645f22SBen Graselse
1155a645f22SBen Gras    # ... on all files given on the command line
116*0a6a1f1dSLionel Sambuc    if [ ${silent} -lt 1 -a $# -gt 1 ]; then
1175a645f22SBen Gras	grep_args="-H ${grep_args}"
1185a645f22SBen Gras    fi
1195a645f22SBen Gras    while [ $# -gt 0 ]
1205a645f22SBen Gras    do
1215a645f22SBen Gras	${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
1225a645f22SBen Gras	shift
1235a645f22SBen Gras    done
1245a645f22SBen Grasfi
1255a645f22SBen Gras
1265a645f22SBen Grasexit 0
127