1*86d7f5d3SJohn Marino#!/bin/sh 2*86d7f5d3SJohn Marino# 3*86d7f5d3SJohn Marino# $NetBSD: zgrep,v 1.7 2008/05/08 15:35:23 wiz Exp $ 4*86d7f5d3SJohn Marino# $DragonFly: src/usr.bin/gzip/zgrep,v 1.1 2004/10/26 11:19:31 joerg Exp $ 5*86d7f5d3SJohn Marino# 6*86d7f5d3SJohn Marino# Copyright (c) 2003 Thomas Klausner. 7*86d7f5d3SJohn Marino# 8*86d7f5d3SJohn Marino# Redistribution and use in source and binary forms, with or without 9*86d7f5d3SJohn Marino# modification, are permitted provided that the following conditions 10*86d7f5d3SJohn Marino# are met: 11*86d7f5d3SJohn Marino# 1. Redistributions of source code must retain the above copyright 12*86d7f5d3SJohn Marino# notice, this list of conditions and the following disclaimer. 13*86d7f5d3SJohn Marino# 2. Redistributions in binary form must reproduce the above copyright 14*86d7f5d3SJohn Marino# notice, this list of conditions and the following disclaimer in the 15*86d7f5d3SJohn Marino# documentation and/or other materials provided with the distribution. 16*86d7f5d3SJohn Marino# 17*86d7f5d3SJohn Marino# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18*86d7f5d3SJohn Marino# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19*86d7f5d3SJohn Marino# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20*86d7f5d3SJohn Marino# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21*86d7f5d3SJohn Marino# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22*86d7f5d3SJohn Marino# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23*86d7f5d3SJohn Marino# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24*86d7f5d3SJohn Marino# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25*86d7f5d3SJohn Marino# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26*86d7f5d3SJohn Marino# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*86d7f5d3SJohn Marino 28*86d7f5d3SJohn Marinogrep=/usr/bin/grep 29*86d7f5d3SJohn Marinozcat=/usr/bin/zcat 30*86d7f5d3SJohn Marino 31*86d7f5d3SJohn Marinoendofopts=0 32*86d7f5d3SJohn Marinopattern_found=0 33*86d7f5d3SJohn Marinogrep_args="" 34*86d7f5d3SJohn Marinohyphen=0 35*86d7f5d3SJohn Marinosilent=0 36*86d7f5d3SJohn Marino 37*86d7f5d3SJohn Marinoprg=$0 38*86d7f5d3SJohn Marino 39*86d7f5d3SJohn Marino# handle being called 'zegrep' or 'zfgrep' 40*86d7f5d3SJohn Marinocase ${prg} in 41*86d7f5d3SJohn Marino *zegrep) 42*86d7f5d3SJohn Marino grep_args="-E";; 43*86d7f5d3SJohn Marino *zfgrep) 44*86d7f5d3SJohn Marino grep_args="-F";; 45*86d7f5d3SJohn Marinoesac 46*86d7f5d3SJohn Marino 47*86d7f5d3SJohn Marino# skip all options and pass them on to grep taking care of options 48*86d7f5d3SJohn Marino# with arguments, and if -e was supplied 49*86d7f5d3SJohn Marino 50*86d7f5d3SJohn Marinowhile [ $# -gt 0 -a ${endofopts} -eq 0 ] 51*86d7f5d3SJohn Marinodo 52*86d7f5d3SJohn Marino case $1 in 53*86d7f5d3SJohn Marino # from GNU grep-2.5.1 -- keep in sync! 54*86d7f5d3SJohn Marino -[ABCDXdefm]) 55*86d7f5d3SJohn Marino if [ $# -lt 2 ] 56*86d7f5d3SJohn Marino then 57*86d7f5d3SJohn Marino echo "${prg}: missing argument for $1 flag" >&2 58*86d7f5d3SJohn Marino exit 1 59*86d7f5d3SJohn Marino fi 60*86d7f5d3SJohn Marino case $1 in 61*86d7f5d3SJohn Marino -e) 62*86d7f5d3SJohn Marino pattern="$2" 63*86d7f5d3SJohn Marino pattern_found=1 64*86d7f5d3SJohn Marino shift 2 65*86d7f5d3SJohn Marino break 66*86d7f5d3SJohn Marino ;; 67*86d7f5d3SJohn Marino *) 68*86d7f5d3SJohn Marino ;; 69*86d7f5d3SJohn Marino esac 70*86d7f5d3SJohn Marino grep_args="${grep_args} $1 $2" 71*86d7f5d3SJohn Marino shift 2 72*86d7f5d3SJohn Marino ;; 73*86d7f5d3SJohn Marino --) 74*86d7f5d3SJohn Marino shift 75*86d7f5d3SJohn Marino endofopts=1 76*86d7f5d3SJohn Marino ;; 77*86d7f5d3SJohn Marino -) 78*86d7f5d3SJohn Marino hyphen=1 79*86d7f5d3SJohn Marino shift 80*86d7f5d3SJohn Marino ;; 81*86d7f5d3SJohn Marino -h) 82*86d7f5d3SJohn Marino silent=1 83*86d7f5d3SJohn Marino shift 84*86d7f5d3SJohn Marino ;; 85*86d7f5d3SJohn Marino -*) 86*86d7f5d3SJohn Marino grep_args="${grep_args} $1" 87*86d7f5d3SJohn Marino shift 88*86d7f5d3SJohn Marino ;; 89*86d7f5d3SJohn Marino *) 90*86d7f5d3SJohn Marino # pattern to grep for 91*86d7f5d3SJohn Marino endofopts=1 92*86d7f5d3SJohn Marino ;; 93*86d7f5d3SJohn Marino esac 94*86d7f5d3SJohn Marinodone 95*86d7f5d3SJohn Marino 96*86d7f5d3SJohn Marino# if no -e option was found, take next argument as grep-pattern 97*86d7f5d3SJohn Marinoif [ ${pattern_found} -lt 1 ] 98*86d7f5d3SJohn Marinothen 99*86d7f5d3SJohn Marino if [ $# -ge 1 ]; then 100*86d7f5d3SJohn Marino pattern="$1" 101*86d7f5d3SJohn Marino shift 102*86d7f5d3SJohn Marino elif [ ${hyphen} -gt 0 ]; then 103*86d7f5d3SJohn Marino pattern="-" 104*86d7f5d3SJohn Marino else 105*86d7f5d3SJohn Marino echo "${prg}: missing pattern" >&2 106*86d7f5d3SJohn Marino exit 1 107*86d7f5d3SJohn Marino fi 108*86d7f5d3SJohn Marinofi 109*86d7f5d3SJohn Marino 110*86d7f5d3SJohn Marino# call grep ... 111*86d7f5d3SJohn Marinoif [ $# -lt 1 ] 112*86d7f5d3SJohn Marinothen 113*86d7f5d3SJohn Marino # ... on stdin 114*86d7f5d3SJohn Marino ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" - 115*86d7f5d3SJohn Marinoelse 116*86d7f5d3SJohn Marino # ... on all files given on the command line 117*86d7f5d3SJohn Marino if [ ${silent} -lt 1 ]; then 118*86d7f5d3SJohn Marino grep_args="-H ${grep_args}" 119*86d7f5d3SJohn Marino fi 120*86d7f5d3SJohn Marino while [ $# -gt 0 ] 121*86d7f5d3SJohn Marino do 122*86d7f5d3SJohn Marino ${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" - 123*86d7f5d3SJohn Marino shift 124*86d7f5d3SJohn Marino done 125*86d7f5d3SJohn Marinofi 126*86d7f5d3SJohn Marino 127*86d7f5d3SJohn Marinoexit 0 128