Home | History | Annotate | Line # | Download | only in gzip
zgrep revision 1.5
      1 #!/bin/sh
      2 #
      3 # $NetBSD: zgrep,v 1.5 2006/05/03 16:48:29 yamt 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 hyphen=0
     36 
     37 prg=$0
     38 
     39 # handle being called 'zegrep' or 'zfgrep'
     40 case ${prg} in
     41     *zegrep)
     42 	grep_args="-E";;
     43     *zfgrep)
     44 	grep_args="-F";;
     45 esac
     46 
     47 # skip all options and pass them on to grep taking care of options
     48 # with arguments, and if -e was supplied
     49 
     50 while [ $# -gt 0 -a ${endofopts} -eq 0 ]
     51 do
     52     case $1 in
     53     # from GNU grep-2.5.1 -- keep in sync!
     54 	-[ABCDXdefm])
     55 	    if [ $# -lt 2 ]
     56 		then
     57 		echo "${prg}: missing argument for $1 flag" >&2
     58 		exit 1
     59 	    fi
     60 	    case $1 in
     61 		-e)
     62 		    pattern="$2"
     63 		    pattern_found=1
     64 		    shift 2
     65 		    break
     66 		    ;;
     67 		*)
     68 		    ;;
     69 	    esac
     70 	    grep_args="${grep_args} $1 $2"
     71 	    shift 2
     72 	    ;;
     73 	--)
     74 	    shift
     75 	    endofopts=1
     76 	    ;;
     77 	-)
     78 	    hyphen=1
     79 	    shift
     80 	    ;;
     81 	-*)
     82 	    grep_args="${grep_args} $1"
     83 	    shift
     84 	    ;;
     85 	*)
     86 	    # pattern to grep for
     87 	    endofopts=1
     88 	    ;;
     89     esac
     90 done
     91 
     92 # if no -e option was found, take next argument as grep-pattern
     93 if [ ${pattern_found} -lt 1 ]
     94 then
     95     if [ $# -ge 1 ]; then
     96 	pattern="$1"
     97 	shift
     98     elif [ ${hyphen} -gt 0 ]; then
     99 	pattern="-"
    100     else
    101 	echo "${prg}: missing pattern" >&2
    102 	exit 1
    103     fi
    104 fi
    105 
    106 # call grep ...
    107 if [ $# -lt 1 ]
    108 then
    109     # ... on stdin
    110     ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
    111 else
    112     # ... on all files given on the command line
    113     while [ $# -gt 0 ]
    114     do
    115 	${zcat} -fq -- "$1" | ${grep} -H --label="${1}" ${grep_args} -- "${pattern}" -
    116 	shift
    117     done
    118 fi
    119 
    120 exit 0
    121