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