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