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:-grep} 26*0c16b537SWarner Loshzcat=${ZCAT:-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 Loshprog=${0##*/} 35*0c16b537SWarner Losh 36*0c16b537SWarner Losh# handle being called 'zegrep' or 'zfgrep' 37*0c16b537SWarner Loshcase $prog in 38*0c16b537SWarner Losh *egrep*) prog=zegrep; grep_args='-E';; 39*0c16b537SWarner Losh *fgrep*) prog=zfgrep; grep_args='-F';; 40*0c16b537SWarner Losh *) prog=zstdgrep;; 41*0c16b537SWarner Loshesac 42*0c16b537SWarner Losh 43*0c16b537SWarner Losh# skip all options and pass them on to grep taking care of options 44*0c16b537SWarner Losh# with arguments, and if -e was supplied 45*0c16b537SWarner Losh 46*0c16b537SWarner Loshwhile [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do 47*0c16b537SWarner Losh case "$1" in 48*0c16b537SWarner Losh # from GNU grep-2.5.1 -- keep in sync! 49*0c16b537SWarner Losh -[ABCDXdefm]) 50*0c16b537SWarner Losh if [ "$#" -lt 2 ]; then 51*0c16b537SWarner Losh printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2 52*0c16b537SWarner Losh exit 1 53*0c16b537SWarner Losh fi 54*0c16b537SWarner Losh case "$1" in 55*0c16b537SWarner Losh -e) 56*0c16b537SWarner Losh pattern="$2" 57*0c16b537SWarner Losh pattern_found=1 58*0c16b537SWarner Losh shift 2 59*0c16b537SWarner Losh break 60*0c16b537SWarner Losh ;; 61*0c16b537SWarner Losh -f) 62*0c16b537SWarner Losh pattern_found=2 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 ]; then 95*0c16b537SWarner Losh if [ "$#" -ge 1 ]; then 96*0c16b537SWarner Losh pattern="$1" 97*0c16b537SWarner Losh shift 98*0c16b537SWarner Losh elif [ "${hyphen}" -gt 0 ]; then 99*0c16b537SWarner Losh pattern="-" 100*0c16b537SWarner Losh else 101*0c16b537SWarner Losh printf '%s: missing pattern\n' "${prog}" >&2 102*0c16b537SWarner Losh exit 1 103*0c16b537SWarner Losh fi 104*0c16b537SWarner Loshfi 105*0c16b537SWarner Losh 106*0c16b537SWarner LoshEXIT_CODE=0 107*0c16b537SWarner Losh# call grep ... 108*0c16b537SWarner Loshif [ "$#" -lt 1 ]; then 109*0c16b537SWarner Losh # ... on stdin 110*0c16b537SWarner Losh set -f # Disable file name generation (globbing). 111*0c16b537SWarner Losh # shellcheck disable=SC2086 112*0c16b537SWarner Losh "${zcat}" - | "${grep}" ${grep_args} -- "${pattern}" - 113*0c16b537SWarner Losh EXIT_CODE=$? 114*0c16b537SWarner Losh set +f 115*0c16b537SWarner Loshelse 116*0c16b537SWarner Losh # ... on all files given on the command line 117*0c16b537SWarner Losh if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then 118*0c16b537SWarner Losh grep_args="-H ${grep_args}" 119*0c16b537SWarner Losh fi 120*0c16b537SWarner Losh set -f 121*0c16b537SWarner Losh while [ "$#" -gt 0 ]; do 122*0c16b537SWarner Losh # shellcheck disable=SC2086 123*0c16b537SWarner Losh if [ $pattern_found -eq 2 ]; then 124*0c16b537SWarner Losh "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- - 125 else 126 "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" - 127 fi 128 [ "$?" -ne 0 ] && EXIT_CODE=1 129 shift 130 done 131 set +f 132fi 133 134exit "${EXIT_CODE}" 135