186d7f5d3SJohn Marino#!/bin/sh 286d7f5d3SJohn Marino# 386d7f5d3SJohn Marino# $NetBSD: zgrep,v 1.7 2008/05/08 15:35:23 wiz Exp $ 486d7f5d3SJohn Marino# $DragonFly: src/usr.bin/gzip/zgrep,v 1.1 2004/10/26 11:19:31 joerg Exp $ 586d7f5d3SJohn Marino# 686d7f5d3SJohn Marino# Copyright (c) 2003 Thomas Klausner. 786d7f5d3SJohn Marino# 886d7f5d3SJohn Marino# Redistribution and use in source and binary forms, with or without 986d7f5d3SJohn Marino# modification, are permitted provided that the following conditions 1086d7f5d3SJohn Marino# are met: 1186d7f5d3SJohn Marino# 1. Redistributions of source code must retain the above copyright 1286d7f5d3SJohn Marino# notice, this list of conditions and the following disclaimer. 1386d7f5d3SJohn Marino# 2. Redistributions in binary form must reproduce the above copyright 1486d7f5d3SJohn Marino# notice, this list of conditions and the following disclaimer in the 1586d7f5d3SJohn Marino# documentation and/or other materials provided with the distribution. 1686d7f5d3SJohn Marino# 1786d7f5d3SJohn Marino# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1886d7f5d3SJohn Marino# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1986d7f5d3SJohn Marino# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2086d7f5d3SJohn Marino# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2186d7f5d3SJohn Marino# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2286d7f5d3SJohn Marino# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2386d7f5d3SJohn Marino# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2486d7f5d3SJohn Marino# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2586d7f5d3SJohn Marino# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2686d7f5d3SJohn Marino# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2786d7f5d3SJohn Marino 2886d7f5d3SJohn Marinogrep=/usr/bin/grep 2986d7f5d3SJohn Marinozcat=/usr/bin/zcat 3086d7f5d3SJohn Marino 3186d7f5d3SJohn Marinoendofopts=0 3286d7f5d3SJohn Marinopattern_found=0 3386d7f5d3SJohn Marinogrep_args="" 3486d7f5d3SJohn Marinohyphen=0 3586d7f5d3SJohn Marinosilent=0 3686d7f5d3SJohn Marino 3786d7f5d3SJohn Marinoprg=$0 3886d7f5d3SJohn Marino 3986d7f5d3SJohn Marino# handle being called 'zegrep' or 'zfgrep' 4086d7f5d3SJohn Marinocase ${prg} in 4186d7f5d3SJohn Marino *zegrep) 4286d7f5d3SJohn Marino grep_args="-E";; 4386d7f5d3SJohn Marino *zfgrep) 4486d7f5d3SJohn Marino grep_args="-F";; 4586d7f5d3SJohn Marinoesac 4686d7f5d3SJohn Marino 4786d7f5d3SJohn Marino# skip all options and pass them on to grep taking care of options 4886d7f5d3SJohn Marino# with arguments, and if -e was supplied 4986d7f5d3SJohn Marino 5086d7f5d3SJohn Marinowhile [ $# -gt 0 -a ${endofopts} -eq 0 ] 5186d7f5d3SJohn Marinodo 5286d7f5d3SJohn Marino case $1 in 5386d7f5d3SJohn Marino # from GNU grep-2.5.1 -- keep in sync! 5486d7f5d3SJohn Marino -[ABCDXdefm]) 5586d7f5d3SJohn Marino if [ $# -lt 2 ] 5686d7f5d3SJohn Marino then 5786d7f5d3SJohn Marino echo "${prg}: missing argument for $1 flag" >&2 5886d7f5d3SJohn Marino exit 1 5986d7f5d3SJohn Marino fi 6086d7f5d3SJohn Marino case $1 in 6186d7f5d3SJohn Marino -e) 6286d7f5d3SJohn Marino pattern="$2" 6386d7f5d3SJohn Marino pattern_found=1 6486d7f5d3SJohn Marino shift 2 6586d7f5d3SJohn Marino break 6686d7f5d3SJohn Marino ;; 6786d7f5d3SJohn Marino *) 6886d7f5d3SJohn Marino ;; 6986d7f5d3SJohn Marino esac 7086d7f5d3SJohn Marino grep_args="${grep_args} $1 $2" 7186d7f5d3SJohn Marino shift 2 7286d7f5d3SJohn Marino ;; 7386d7f5d3SJohn Marino --) 7486d7f5d3SJohn Marino shift 7586d7f5d3SJohn Marino endofopts=1 7686d7f5d3SJohn Marino ;; 7786d7f5d3SJohn Marino -) 7886d7f5d3SJohn Marino hyphen=1 7986d7f5d3SJohn Marino shift 8086d7f5d3SJohn Marino ;; 8186d7f5d3SJohn Marino -h) 8286d7f5d3SJohn Marino silent=1 8386d7f5d3SJohn Marino shift 8486d7f5d3SJohn Marino ;; 8586d7f5d3SJohn Marino -*) 8686d7f5d3SJohn Marino grep_args="${grep_args} $1" 8786d7f5d3SJohn Marino shift 8886d7f5d3SJohn Marino ;; 8986d7f5d3SJohn Marino *) 9086d7f5d3SJohn Marino # pattern to grep for 9186d7f5d3SJohn Marino endofopts=1 9286d7f5d3SJohn Marino ;; 9386d7f5d3SJohn Marino esac 9486d7f5d3SJohn Marinodone 9586d7f5d3SJohn Marino 9686d7f5d3SJohn Marino# if no -e option was found, take next argument as grep-pattern 9786d7f5d3SJohn Marinoif [ ${pattern_found} -lt 1 ] 9886d7f5d3SJohn Marinothen 9986d7f5d3SJohn Marino if [ $# -ge 1 ]; then 10086d7f5d3SJohn Marino pattern="$1" 10186d7f5d3SJohn Marino shift 10286d7f5d3SJohn Marino elif [ ${hyphen} -gt 0 ]; then 10386d7f5d3SJohn Marino pattern="-" 10486d7f5d3SJohn Marino else 10586d7f5d3SJohn Marino echo "${prg}: missing pattern" >&2 10686d7f5d3SJohn Marino exit 1 10786d7f5d3SJohn Marino fi 10886d7f5d3SJohn Marinofi 10986d7f5d3SJohn Marino 11086d7f5d3SJohn Marino# call grep ... 11186d7f5d3SJohn Marinoif [ $# -lt 1 ] 11286d7f5d3SJohn Marinothen 11386d7f5d3SJohn Marino # ... on stdin 11486d7f5d3SJohn Marino ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" - 11586d7f5d3SJohn Marinoelse 11686d7f5d3SJohn Marino # ... on all files given on the command line 11786d7f5d3SJohn Marino if [ ${silent} -lt 1 ]; then 11886d7f5d3SJohn Marino grep_args="-H ${grep_args}" 11986d7f5d3SJohn Marino fi 12086d7f5d3SJohn Marino while [ $# -gt 0 ] 12186d7f5d3SJohn Marino do 12286d7f5d3SJohn Marino ${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" - 12386d7f5d3SJohn Marino shift 12486d7f5d3SJohn Marino done 12586d7f5d3SJohn Marinofi 12686d7f5d3SJohn Marino 12786d7f5d3SJohn Marinoexit 0 128