source: Daodan/MinGW/msys/1.0/bin/bzegrep@ 1098

Last change on this file since 1098 was 1046, checked in by alloc, 8 years ago

Daodan: Added Windows MinGW and build batch file

File size: 3.6 KB
RevLine 
[1046]1#!/bin/sh
2
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>
6
7# Copyright (C) 1998, 2001, 2002 Free Software Foundation
8# Copyright (C) 1993 Jean-loup Gailly
9
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)
13# any later version.
14
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.
19
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
23# 02111-1307, USA.
24
25PATH="/usr/bin:$PATH"; export PATH
26
27prog=`echo "$0" | sed 's|.*/||'`
28case "$prog" in
29 *egrep) grep=${EGREP-egrep} ;;
30 *fgrep) grep=${FGREP-fgrep} ;;
31 *) grep=${GREP-grep} ;;
32esac
33
34pat=""
35after_dash_dash=""
36files_with_matches=0
37files_without_matches=0
38no_filename=0
39with_filename=0
40
41while 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;;
48 --*) ;;
49 -*)
50 case "$1" in
51 -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
52 esac
53 case "$1" in
54 -*H*) with_filename=1;;
55 esac
56 case "$1" in
57 -*h*) no_filename=1;;
58 esac
59 case "$1" in
60 -*L*) files_without_matches=1;;
61 esac
62 case "$1" in
63 -*l*) files_with_matches=1;;
64 esac;;
65 esac
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
69 grep=egrep
70 fi;;
71 -[ABCdm])opt="$opt $1 $2"; shift;;
72 --) opt="$opt $1"; after_dash_dash=1;;
73 -*) opt="$opt $1";;
74 *) if test -z "$pat"; then
75 pat="$1"
76 else
77 break;
78 fi;;
79 esac
80 shift
81done
82
83if test -z "$pat"; then
84 echo "grep through bzip2 files"
85 echo "usage: $prog [grep_options] pattern [files]"
86 exit 2
87fi
88
89if test $# -eq 0; then
90 bzip2 -cdfq | $grep $opt "$pat"
91 exit $?
92fi
93
94res=0
95for i do
96 bzip2 -cdfq -- "$i" |
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
102 $grep $opt "$pat"
103 else
104 i=$(echo "$i" | sed -e 's/[\\|&]/\\&/g')
105 if test $with_filename -eq 1; then
106 sed_script="s|^[^:]*:|${i}:|"
107 else
108 sed_script="s|^|${i}:|"
109 fi
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:
113 #
114 # $grep $opt "$pat" | sed "$sed_script"
115 # exit ${PIPESTATUS[0]}
116 #
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
120 exec 3>&1
121 eval `
122 exec 4>&1 >&3 3>&-
123 {
124 $grep $opt "$pat" 4>&-; echo "r=$?;" >&4
125 } | sed "$sed_script"
126 `
127 exit $r
128 fi
129 r=$?
130 test $res -lt $r && res=$r
131done
132exit $res
Note: See TracBrowser for help on using the repository browser.