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