1#!/bin/sh 2# 3# Copyright (c) 2003 Thomas Klausner. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions 7# are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24# 25 26set -u 27grep=grep 28zcat=zstdcat 29 30endofopts=0 31pattern_file=0 32pattern_found=0 33grep_args="" 34hyphen=0 35silent=0 36 37prg=${0##*/} 38 39# handle being called 'zegrep' or 'zfgrep' 40case ${prg} in 41*egrep) 42 grep_args="-E";; 43*fgrep) 44 grep_args="-F";; 45esac 46 47catargs="-f" 48case ${prg} in 49zstd*) 50 cattool="/usr/bin/zstdcat" 51 catargs="-fq" 52 ;; 53bz*) 54 cattool="/usr/bin/bzcat" 55 ;; 56z*) 57 cattool="/usr/bin/zcat" 58 ;; 59xz*) 60 cattool="/usr/bin/xzcat" 61 ;; 62lz*) 63 cattool="/usr/bin/lzcat" 64 ;; 65*) 66 echo "Invalid command: ${prg}" >&2 67 exit 1 68 ;; 69esac 70 71# skip all options and pass them on to grep taking care of options 72# with arguments, and if -e was supplied 73 74while [ $# -gt 0 -a ${endofopts} -eq 0 ] 75do 76 case $1 in 77 # from GNU grep-2.6.0 -- keep in sync! 78 --) 79 shift 80 endofopts=1 81 ;; 82 --file=*) 83 pattern_file=1 84 grep_args="${grep_args} ${1}" 85 shift 86 ;; 87 --regexp=*) 88 if [ ${pattern_found} -ne 0 ]; then 89 grep_args="${grep_args} -e ${pattern}" 90 fi 91 pattern="${1#--regexp=}" 92 pattern_found=1 93 shift 94 ;; 95 -h|--no-filename) 96 silent=1 97 shift 98 ;; 99 -V|--version) 100 exec ${grep} -V 101 ;; 102 --*) 103 grep_args="${grep_args} $1" 104 shift 105 ;; 106 -[EFGHILOSUVabchilnopqsuvwxyz]*) 107 post="${1#-?}" 108 pre=${1%${post}} 109 grep_args="${grep_args} ${pre}" 110 shift 111 # Put back partial arg 112 set -- "-${post}" $* 113 ;; 114 -[ABCDdefm]) 115 if [ $# -lt 2 ] 116 then 117 echo "${prg}: missing argument for $1 flag" >&2 118 exit 1 119 fi 120 case $1 in 121 -e) 122 if [ ${pattern_found} -ne 0 ]; then 123 grep_args="${grep_args} -e ${pattern}" 124 fi 125 pattern="$2" 126 pattern_found=1 127 shift 2 128 continue 129 ;; 130 -f) 131 pattern_file=1 132 ;; 133 *) 134 ;; 135 esac 136 grep_args="${grep_args} $1 $2" 137 shift 2 138 ;; 139 -[ABCDdefm]*) 140 post="${1#-e}" 141 case ${1} in 142 -e*) 143 if [ ${pattern_found} -ne 0 ]; then 144 grep_args="${grep_args} -e ${pattern}" 145 fi 146 pattern="${post}" 147 pattern_found=1 148 shift 149 continue 150 ;; 151 -f*) 152 pattern_file=1 153 ;; 154 *) 155 ;; 156 esac 157 grep_args="${grep_args} ${post}" 158 shift 159 ;; 160 -) 161 hyphen=1 162 shift 163 ;; 164 -r|-R) 165 echo "${prg}: the ${1} flag is not currently supported" >&2 166 exit 1 167 ;; 168 -?) 169 grep_args="${grep_args} $1" 170 shift 171 ;; 172 *) 173 # pattern to grep for 174 endofopts=1 175 ;; 176 esac 177done 178 179# if no -e option was found, take next argument as grep-pattern 180if [ ${pattern_file} -eq 0 -a ${pattern_found} -eq 0 ] 181then 182 if [ $# -ge 1 ]; then 183 pattern="$1" 184 shift 185 elif [ ${hyphen} -gt 0 ]; then 186 pattern="-" 187 else 188 echo "${prg}: missing pattern" >&2 189 exit 1 190 fi 191 pattern_found=1 192fi 193 194# Clean up possible leading blank 195grep_args="${grep_args# }" 196 197# call grep ... 198if [ $# -lt 1 ] 199then 200 # ... on stdin 201 if [ ${pattern_file} -eq 0 ]; then 202 ${cattool} ${catargs} - | ${grep} ${grep_args} -e "${pattern}" -- - 203 else 204 ${cattool} ${catargs} - | ${grep} ${grep_args} -- - 205 fi 206 ret=$? 207else 208 # ... on all files given on the command line 209 if [ ${silent} -lt 1 -a $# -gt 1 ]; then 210 grep_args="-H ${grep_args}" 211 fi 212 # Succeed if any file matches. First assume no match. 213 ret=1 214 for file; do 215 if [ ${pattern_file} -eq 0 ]; then 216 ${cattool} ${catargs} -- "${file}" | 217 ${grep} --label="${file}" ${grep_args} -e "${pattern}" -- - 218 else 219 ${cattool} ${catargs} -- "${file}" | 220 ${grep} --label="${file}" ${grep_args} -- - 221 fi 222 this_ret=$? 223 # A match (0) overrides a no-match (1). An error (>=2) overrides all. 224 if [ ${this_ret} -eq 0 -a ${ret} -eq 1 ] || [ ${this_ret} -ge 2 ]; then 225 ret=${this_ret} 226 fi 227 done 228fi 229 230exit ${ret} 231