xref: /minix3/external/bsd/bzip2/dist/bzgrep (revision 4a711bea63dc53acce03198b5fbfaa103fe328d6)
1*4a711beaSLionel Sambuc#!/bin/sh
2*4a711beaSLionel Sambuc
3*4a711beaSLionel Sambuc# Bzgrep wrapped for bzip2,
4*4a711beaSLionel Sambuc# adapted from zgrep by Philippe Troin <phil@fifi.org> for Debian GNU/Linux.
5*4a711beaSLionel Sambuc## zgrep notice:
6*4a711beaSLionel Sambuc## zgrep -- a wrapper around a grep program that decompresses files as needed
7*4a711beaSLionel Sambuc## Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
8*4a711beaSLionel Sambuc
9*4a711beaSLionel SambucPATH="/usr/bin:$PATH"; export PATH
10*4a711beaSLionel Sambuc
11*4a711beaSLionel Sambucprog=`echo $0 | sed 's|.*/||'`
12*4a711beaSLionel Sambuccase "$prog" in
13*4a711beaSLionel Sambuc	*egrep)	grep=${EGREP-egrep}	;;
14*4a711beaSLionel Sambuc	*fgrep)	grep=${FGREP-fgrep}	;;
15*4a711beaSLionel Sambuc	*)	grep=${GREP-grep}	;;
16*4a711beaSLionel Sambucesac
17*4a711beaSLionel Sambucpat=""
18*4a711beaSLionel Sambucwhile test $# -ne 0; do
19*4a711beaSLionel Sambuc  case "$1" in
20*4a711beaSLionel Sambuc  -e | -f) opt="$opt $1"; shift; pat="$1"
21*4a711beaSLionel Sambuc           if test "$grep" = grep; then  # grep is buggy with -e on SVR4
22*4a711beaSLionel Sambuc             grep=egrep
23*4a711beaSLionel Sambuc           fi;;
24*4a711beaSLionel Sambuc  -A | -B) opt="$opt $1 $2"; shift;;
25*4a711beaSLionel Sambuc  -*)	   opt="$opt $1";;
26*4a711beaSLionel Sambuc   *)      if test -z "$pat"; then
27*4a711beaSLionel Sambuc	     pat="$1"
28*4a711beaSLionel Sambuc	   else
29*4a711beaSLionel Sambuc	     break;
30*4a711beaSLionel Sambuc           fi;;
31*4a711beaSLionel Sambuc  esac
32*4a711beaSLionel Sambuc  shift
33*4a711beaSLionel Sambucdone
34*4a711beaSLionel Sambuc
35*4a711beaSLionel Sambucif test -z "$pat"; then
36*4a711beaSLionel Sambuc  echo "grep through bzip2 files"
37*4a711beaSLionel Sambuc  echo "usage: $prog [grep_options] pattern [files]"
38*4a711beaSLionel Sambuc  exit 1
39*4a711beaSLionel Sambucfi
40*4a711beaSLionel Sambuc
41*4a711beaSLionel Sambuclist=0
42*4a711beaSLionel Sambucsilent=0
43*4a711beaSLionel Sambucop=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
44*4a711beaSLionel Sambuccase "$op" in
45*4a711beaSLionel Sambuc  *l*) list=1
46*4a711beaSLionel Sambucesac
47*4a711beaSLionel Sambuccase "$op" in
48*4a711beaSLionel Sambuc  *h*) silent=1
49*4a711beaSLionel Sambucesac
50*4a711beaSLionel Sambuc
51*4a711beaSLionel Sambucif test $# -eq 0; then
52*4a711beaSLionel Sambuc  bzip2 -cdfq | $grep $opt "$pat"
53*4a711beaSLionel Sambuc  exit $?
54*4a711beaSLionel Sambucfi
55*4a711beaSLionel Sambuc
56*4a711beaSLionel Sambucres=0
57*4a711beaSLionel Sambucfor i do
58*4a711beaSLionel Sambuc  if test -f "$i"; then :; else if test -f "$i.bz2"; then i="$i.bz2"; fi; fi
59*4a711beaSLionel Sambuc  if test $list -eq 1; then
60*4a711beaSLionel Sambuc    bzip2 -cdfq "$i" | $grep $opt "$pat" 2>&1 > /dev/null && echo $i
61*4a711beaSLionel Sambuc    r=$?
62*4a711beaSLionel Sambuc  elif test $# -eq 1 -o $silent -eq 1; then
63*4a711beaSLionel Sambuc    bzip2 -cdfq "$i" | $grep $opt "$pat"
64*4a711beaSLionel Sambuc    r=$?
65*4a711beaSLionel Sambuc  else
66*4a711beaSLionel Sambuc    j=${i//\\/\\\\}
67*4a711beaSLionel Sambuc    j=${j//|/\\|}
68*4a711beaSLionel Sambuc    j=${j//&/\\&}
69*4a711beaSLionel Sambuc    j=`printf "%s" "$j" | tr '\n' ' '`
70*4a711beaSLionel Sambuc    bzip2 -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${j}:|"
71*4a711beaSLionel Sambuc    r=$?
72*4a711beaSLionel Sambuc  fi
73*4a711beaSLionel Sambuc  test "$r" -ne 0 && res="$r"
74*4a711beaSLionel Sambucdone
75*4a711beaSLionel Sambucexit $res
76