16ac2a4f4Swiz#!/bin/sh 2e000293cSwiz# 3*dafd5f44Snakayama# $NetBSD: zgrep,v 1.9 2015/07/06 12:05:40 nakayama Exp $ 4e000293cSwiz# 56ac2a4f4Swiz# Copyright (c) 2003 Thomas Klausner. 66ac2a4f4Swiz# 76ac2a4f4Swiz# Redistribution and use in source and binary forms, with or without 86ac2a4f4Swiz# modification, are permitted provided that the following conditions 96ac2a4f4Swiz# are met: 106ac2a4f4Swiz# 1. Redistributions of source code must retain the above copyright 116ac2a4f4Swiz# notice, this list of conditions and the following disclaimer. 126ac2a4f4Swiz# 2. Redistributions in binary form must reproduce the above copyright 136ac2a4f4Swiz# notice, this list of conditions and the following disclaimer in the 146ac2a4f4Swiz# documentation and/or other materials provided with the distribution. 156ac2a4f4Swiz# 166ac2a4f4Swiz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 176ac2a4f4Swiz# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 186ac2a4f4Swiz# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 196ac2a4f4Swiz# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 206ac2a4f4Swiz# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 216ac2a4f4Swiz# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 226ac2a4f4Swiz# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 236ac2a4f4Swiz# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 246ac2a4f4Swiz# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 256ac2a4f4Swiz# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 266ac2a4f4Swiz 276ac2a4f4Swizgrep=/usr/bin/grep 286ac2a4f4Swizzcat=/usr/bin/zcat 296ac2a4f4Swiz 306ac2a4f4Swizendofopts=0 316ac2a4f4Swizpattern_found=0 326ac2a4f4Swizgrep_args="" 3325539809Syamthyphen=0 3466f1c31aSnakayamasilent=0 356ac2a4f4Swiz 36d96eb157Spgoyetteprg=$(basename $0) 376ac2a4f4Swiz 386ac2a4f4Swiz# handle being called 'zegrep' or 'zfgrep' 396ac2a4f4Swizcase ${prg} in 406ac2a4f4Swiz *zegrep) 416ac2a4f4Swiz grep_args="-E";; 426ac2a4f4Swiz *zfgrep) 436ac2a4f4Swiz grep_args="-F";; 446ac2a4f4Swizesac 456ac2a4f4Swiz 466ac2a4f4Swiz# skip all options and pass them on to grep taking care of options 476ac2a4f4Swiz# with arguments, and if -e was supplied 486ac2a4f4Swiz 496ac2a4f4Swizwhile [ $# -gt 0 -a ${endofopts} -eq 0 ] 506ac2a4f4Swizdo 516ac2a4f4Swiz case $1 in 526ac2a4f4Swiz # from GNU grep-2.5.1 -- keep in sync! 536ac2a4f4Swiz -[ABCDXdefm]) 5425539809Syamt if [ $# -lt 2 ] 5525539809Syamt then 5625539809Syamt echo "${prg}: missing argument for $1 flag" >&2 5725539809Syamt exit 1 5825539809Syamt fi 596ac2a4f4Swiz case $1 in 606ac2a4f4Swiz -e) 6125539809Syamt pattern="$2" 6225539809Syamt pattern_found=1 6325539809Syamt shift 2 6425539809Syamt break 6525539809Syamt ;; 666ac2a4f4Swiz *) 676ac2a4f4Swiz ;; 686ac2a4f4Swiz esac 696ac2a4f4Swiz grep_args="${grep_args} $1 $2" 706ac2a4f4Swiz shift 2 716ac2a4f4Swiz ;; 726ac2a4f4Swiz --) 736ac2a4f4Swiz shift 746ac2a4f4Swiz endofopts=1 756ac2a4f4Swiz ;; 766ac2a4f4Swiz -) 7725539809Syamt hyphen=1 7825539809Syamt shift 796ac2a4f4Swiz ;; 8066f1c31aSnakayama -h) 8166f1c31aSnakayama silent=1 8266f1c31aSnakayama shift 8366f1c31aSnakayama ;; 846ac2a4f4Swiz -*) 856ac2a4f4Swiz grep_args="${grep_args} $1" 866ac2a4f4Swiz shift 876ac2a4f4Swiz ;; 886ac2a4f4Swiz *) 896ac2a4f4Swiz # pattern to grep for 906ac2a4f4Swiz endofopts=1 916ac2a4f4Swiz ;; 926ac2a4f4Swiz esac 936ac2a4f4Swizdone 946ac2a4f4Swiz 956ac2a4f4Swiz# if no -e option was found, take next argument as grep-pattern 966ac2a4f4Swizif [ ${pattern_found} -lt 1 ] 976ac2a4f4Swizthen 9825539809Syamt if [ $# -ge 1 ]; then 9925539809Syamt pattern="$1" 10025539809Syamt shift 10125539809Syamt elif [ ${hyphen} -gt 0 ]; then 10225539809Syamt pattern="-" 10325539809Syamt else 1046ac2a4f4Swiz echo "${prg}: missing pattern" >&2 1056ac2a4f4Swiz exit 1 1066ac2a4f4Swiz fi 1076ac2a4f4Swizfi 1086ac2a4f4Swiz 1096ac2a4f4Swiz# call grep ... 1106ac2a4f4Swizif [ $# -lt 1 ] 1116ac2a4f4Swizthen 1126ac2a4f4Swiz # ... on stdin 11325539809Syamt ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" - 1146ac2a4f4Swizelse 1156ac2a4f4Swiz # ... on all files given on the command line 116*dafd5f44Snakayama if [ ${silent} -lt 1 -a $# -gt 1 ]; then 11766f1c31aSnakayama grep_args="-H ${grep_args}" 11866f1c31aSnakayama fi 1196ac2a4f4Swiz while [ $# -gt 0 ] 1206ac2a4f4Swiz do 12166f1c31aSnakayama ${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" - 1226ac2a4f4Swiz shift 1236ac2a4f4Swiz done 1246ac2a4f4Swizfi 1256ac2a4f4Swiz 1266ac2a4f4Swizexit 0 127