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_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.5.1 -- keep in sync! 78 -[ABCDXdefm]) 79 if [ $# -lt 2 ] 80 then 81 echo "${prg}: missing argument for $1 flag" >&2 82 exit 1 83 fi 84 case $1 in 85 -e) 86 pattern="$2" 87 pattern_found=1 88 shift 2 89 break 90 ;; 91 *) 92 ;; 93 esac 94 grep_args="${grep_args} $1 $2" 95 shift 2 96 ;; 97 --) 98 shift 99 endofopts=1 100 ;; 101 -) 102 hyphen=1 103 shift 104 ;; 105 -h) 106 silent=1 107 shift 108 ;; 109 -V|--version) 110 exec ${grep} -V 111 ;; 112 -*) 113 grep_args="${grep_args} $1" 114 shift 115 ;; 116 *) 117 # pattern to grep for 118 endofopts=1 119 ;; 120 esac 121done 122 123# if no -e option was found, take next argument as grep-pattern 124if [ ${pattern_found} -lt 1 ] 125then 126 if [ $# -ge 1 ]; then 127 pattern="$1" 128 shift 129 elif [ ${hyphen} -gt 0 ]; then 130 pattern="-" 131 else 132 echo "${prg}: missing pattern" >&2 133 exit 1 134 fi 135fi 136 137# call grep ... 138if [ $# -lt 1 ] 139then 140 # ... on stdin 141 ${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - 142else 143 # ... on all files given on the command line 144 if [ ${silent} -lt 1 -a $# -gt 1 ]; then 145 grep_args="-H ${grep_args}" 146 fi 147 for file; do 148 ${cattool} ${catargs} -- "${file}" | ${grep} --label="${file}" ${grep_args} -- "${pattern}" - 149 done 150fi 151 152exit 0 153