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