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# $FreeBSD$ 26 27set -u 28grep=grep 29zcat=zstdcat 30 31endofopts=0 32pattern_file=0 33pattern_found=0 34grep_args="" 35hyphen=0 36silent=0 37 38prg=${0##*/} 39 40# handle being called 'zegrep' or 'zfgrep' 41case ${prg} in 42*egrep) 43 grep_args="-E";; 44*fgrep) 45 grep_args="-F";; 46esac 47 48catargs="-f" 49case ${prg} in 50zstd*) 51 cattool="/usr/bin/zstdcat" 52 catargs="-fq" 53 ;; 54bz*) 55 cattool="/usr/bin/bzcat" 56 ;; 57z*) 58 cattool="/usr/bin/zcat" 59 ;; 60xz*) 61 cattool="/usr/bin/xzcat" 62 ;; 63lz*) 64 cattool="/usr/bin/lzcat" 65 ;; 66*) 67 echo "Invalid command: ${prg}" >&2 68 exit 1 69 ;; 70esac 71 72# skip all options and pass them on to grep taking care of options 73# with arguments, and if -e was supplied 74 75while [ $# -gt 0 -a ${endofopts} -eq 0 ] 76do 77 case $1 in 78 # from GNU grep-2.5.1 -- keep in sync! 79 --) 80 shift 81 endofopts=1 82 ;; 83 --file=*) 84 pattern_file=1 85 grep_args="${grep_args} ${1}" 86 shift 87 ;; 88 --regexp=*) 89 pattern="${1#--regexp=}" 90 pattern_found=1 91 shift 92 ;; 93 -h|--no-filename) 94 silent=1 95 shift 96 ;; 97 --*) 98 grep_args="${grep_args} $1" 99 shift 100 ;; 101 -*[ABCDXdefm]) 102 if [ $# -lt 2 ] 103 then 104 echo "${prg}: missing argument for $1 flag" >&2 105 exit 1 106 fi 107 case $1 in 108 -*e) 109 pattern="$2" 110 pattern_found=1 111 shift 2 112 continue 113 ;; 114 -*f) 115 pattern_file=1 116 ;; 117 *) 118 ;; 119 esac 120 grep_args="${grep_args} $1 $2" 121 shift 2 122 ;; 123 -) 124 hyphen=1 125 shift 126 ;; 127 -r|-R) 128 echo "${prg}: the ${1} flag is not currently supported" >&2 129 exit 1 130 ;; 131 -V|--version) 132 exec ${grep} -V 133 ;; 134 -*) 135 grep_args="${grep_args} $1" 136 shift 137 ;; 138 *) 139 # pattern to grep for 140 endofopts=1 141 ;; 142 esac 143done 144 145# if no -e option was found, take next argument as grep-pattern 146if [ ${pattern_file} -eq 0 -a ${pattern_found} -eq 0 ] 147then 148 if [ $# -ge 1 ]; then 149 pattern="$1" 150 shift 151 elif [ ${hyphen} -gt 0 ]; then 152 pattern="-" 153 else 154 echo "${prg}: missing pattern" >&2 155 exit 1 156 fi 157 pattern_found=1 158fi 159 160# call grep ... 161if [ $# -lt 1 ] 162then 163 # ... on stdin 164 if [ ${pattern_file} -eq 0 ]; then 165 ${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - 166 else 167 ${cattool} ${catargs} - | ${grep} ${grep_args} -- - 168 fi 169 ret=$? 170else 171 # ... on all files given on the command line 172 if [ ${silent} -lt 1 -a $# -gt 1 ]; then 173 grep_args="-H ${grep_args}" 174 fi 175 # Succeed if any file matches. First assume no match. 176 ret=1 177 for file; do 178 if [ ${pattern_file} -eq 0 ]; then 179 ${cattool} ${catargs} -- "${file}" | 180 ${grep} --label="${file}" ${grep_args} -- "${pattern}" - 181 else 182 ${cattool} ${catargs} -- "${file}" | 183 ${grep} --label="${file}" ${grep_args} -- - 184 fi 185 this_ret=$? 186 # A match (0) overrides a no-match (1). An error (>=2) overrides all. 187 if [ ${this_ret} -eq 0 -a ${ret} -eq 1 ] || [ ${this_ret} -ge 2 ]; then 188 ret=${this_ret} 189 fi 190 done 191fi 192 193exit ${ret} 194