3 # bzgrep -- a wrapper around a grep program that decompresses files as needed
4 # Adapted from zgrep of the Debian gzip package by Anibal Monsalve Salazar.
5 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
7 # Copyright (C) 1998, 2001, 2002 Free Software Foundation
8 # Copyright (C) 1993 Jean-loup Gailly
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 PATH="/usr/bin:$PATH"; export PATH
27 prog=`echo "$0" | sed 's|.*/||'`
29 *egrep) grep=${EGREP-egrep} ;;
30 *fgrep) grep=${FGREP-fgrep} ;;
31 *) grep=${GREP-grep} ;;
37 files_without_matches=0
41 while test $# -ne 0; do
42 case "$after_dash_dash$1" in
43 --d* | --rec*) echo >&2 "$0: $1: option not supported"; exit 2;;
44 --files-with-*) files_with_matches=1;;
45 --files-witho*) files_without_matches=1;;
46 --no-f*) no_filename=1;;
47 --wi*) with_filename=1;;
51 -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
54 -*H*) with_filename=1;;
60 -*L*) files_without_matches=1;;
63 -*l*) files_with_matches=1;;
66 case "$after_dash_dash$1" in
67 -[ef]) opt="$opt $1"; shift; pat="$1"
68 if test "$grep" = grep; then # grep is buggy with -e on SVR4
71 -[ABCdm])opt="$opt $1 $2"; shift;;
72 --) opt="$opt $1"; after_dash_dash=1;;
74 *) if test -z "$pat"; then
83 if test -z "$pat"; then
84 echo "grep through bzip2 files"
85 echo "usage: $prog [grep_options] pattern [files]"
89 if test $# -eq 0; then
90 bzip2 -cdfq | $grep $opt "$pat"
97 if test $files_with_matches -eq 1; then
98 $grep $opt "$pat" > /dev/null && printf "%s\n" "$i"
99 elif test $files_without_matches -eq 1; then
100 $grep $opt "$pat" > /dev/null || printf "%s\n" "$i"
101 elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
104 i=$(echo "$i" | sed -e 's/[\\|&]/\\&/g')
105 if test $with_filename -eq 1; then
106 sed_script="s|^[^:]*:|${i}:|"
108 sed_script="s|^|${i}:|"
110 # Hack adapted from GPLed code at
111 # http://home.comcast.net/~j.p.h/cus-faq-2
112 # Has the same effect as the following two lines of bash:
114 # $grep $opt "$pat" | sed "$sed_script"
115 # exit ${PIPESTATUS[0]}
117 # Inside the `...`, fd4 goes to the pipe whose other end is read
118 # and passed to eval; fd1 is the normal standard output
119 # preserved the line before with exec 3>&1
124 $grep $opt "$pat" 4>&-; echo "r=$?;" >&4
125 } | sed "$sed_script"
130 test $res -lt $r && res=$r