zgrep revision 1.4
1b8e80941Smrg#!/bin/sh 2b8e80941Smrg# 3b8e80941Smrg# $NetBSD: zgrep,v 1.4 2004/05/25 07:09:09 mrg Exp $ 4b8e80941Smrg# 5b8e80941Smrg# Copyright (c) 2003 Thomas Klausner. 6b8e80941Smrg# 7b8e80941Smrg# Redistribution and use in source and binary forms, with or without 8b8e80941Smrg# modification, are permitted provided that the following conditions 9b8e80941Smrg# are met: 10b8e80941Smrg# 1. Redistributions of source code must retain the above copyright 11b8e80941Smrg# notice, this list of conditions and the following disclaimer. 12b8e80941Smrg# 2. Redistributions in binary form must reproduce the above copyright 13b8e80941Smrg# notice, this list of conditions and the following disclaimer in the 14b8e80941Smrg# documentation and/or other materials provided with the distribution. 15b8e80941Smrg# 3. The name of the author may not be used to endorse or promote products 16b8e80941Smrg# derived from this software without specific prior written permission. 17b8e80941Smrg# 18b8e80941Smrg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19b8e80941Smrg# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20b8e80941Smrg# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21b8e80941Smrg# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22b8e80941Smrg# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23b8e80941Smrg# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24b8e80941Smrg# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25b8e80941Smrg# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26b8e80941Smrg# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27b8e80941Smrg# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28b8e80941Smrg 29b8e80941Smrggrep=/usr/bin/grep 30b8e80941Smrgzcat=/usr/bin/zcat 31b8e80941Smrg 32b8e80941Smrgendofopts=0 33b8e80941Smrgpattern_found=0 34b8e80941Smrggrep_args="" 35b8e80941Smrg 36b8e80941Smrgprg=$0 37b8e80941Smrg 38b8e80941Smrg# handle being called 'zegrep' or 'zfgrep' 39b8e80941Smrgcase ${prg} in 40b8e80941Smrg *zegrep) 41b8e80941Smrg grep_args="-E";; 42b8e80941Smrg *zfgrep) 43b8e80941Smrg grep_args="-F";; 44b8e80941Smrgesac 45b8e80941Smrg 46b8e80941Smrg# skip all options and pass them on to grep taking care of options 47b8e80941Smrg# with arguments, and if -e was supplied 48b8e80941Smrg 49b8e80941Smrgwhile [ $# -gt 0 -a ${endofopts} -eq 0 ] 50b8e80941Smrgdo 51b8e80941Smrg case $1 in 52b8e80941Smrg # from GNU grep-2.5.1 -- keep in sync! 53b8e80941Smrg -[ABCDXdefm]) 54b8e80941Smrg case $1 in 55b8e80941Smrg -e) 56b8e80941Smrg pattern_found=1;; 57b8e80941Smrg *) 58b8e80941Smrg ;; 59b8e80941Smrg esac 60b8e80941Smrg 61b8e80941Smrg if [ $# -lt 2 ] 62b8e80941Smrg then 63b8e80941Smrg echo "${prg}: missing argument for ${saved_arg} flag" >&2 64b8e80941Smrg exit 1 65b8e80941Smrg fi 66b8e80941Smrg grep_args="${grep_args} $1 $2" 67b8e80941Smrg shift 2 68b8e80941Smrg ;; 69b8e80941Smrg --) 70b8e80941Smrg grep_args="${grep_args} $1" 71b8e80941Smrg shift 72b8e80941Smrg endofopts=1 73b8e80941Smrg ;; 74b8e80941Smrg -) 75b8e80941Smrg endofopts=1 76b8e80941Smrg ;; 77b8e80941Smrg -*) 78b8e80941Smrg grep_args="${grep_args} $1" 79b8e80941Smrg shift 80b8e80941Smrg ;; 81b8e80941Smrg *) 82b8e80941Smrg # pattern to grep for 83b8e80941Smrg endofopts=1 84b8e80941Smrg ;; 85b8e80941Smrg esac 86b8e80941Smrgdone 87b8e80941Smrg 88b8e80941Smrg# if no -e option was found, take next argument as grep-pattern 89b8e80941Smrgif [ ${pattern_found} -lt 1 ] 90b8e80941Smrgthen 91b8e80941Smrg if [ $# -lt 1 ] 92b8e80941Smrg then 93b8e80941Smrg echo "${prg}: missing pattern" >&2 94b8e80941Smrg exit 1 95 fi 96 pattern=$1 97 shift 98fi 99 100# call grep ... 101if [ $# -lt 1 ] 102then 103 # ... on stdin 104 ${zcat} -fq - | ${grep} ${grep_args} "${pattern}" - 105else 106 # ... on all files given on the command line 107 while [ $# -gt 0 ] 108 do 109 ${zcat} -fq -- "$1" | ${grep} -H --label="${1}" "${pattern}" ${grep_args} - 110 shift 111 done 112fi 113 114exit 0 115