summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/gzip/zgrep.getopt
blob: f6aa343f5c38d67c6dd5f511d0fe859a247b7cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
#
# Copyright (c) April 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# zgrep - search possibly compressed files for a regular expression
#
# $Id: zgrep.getopt,v 1.1 1996/05/12 16:20:26 wosch Exp $


PATH=/bin:/usr/bin:$PATH; export PATH

# grep variant
case "$0" in
	*egrep)	grep=${EGREP-egrep}	;;
	*fgrep)	grep=${FGREP-fgrep}	;;
	*)	grep=${GREP-grep}	;;
esac

gzip=gzip gzipopt="-cdfq" pattern= grepopt= files= line= header=
set -- `getopt "0123456789A:B:CEFGVX:bce:f:hiLlnqsvwxy" "$@"`

# check options
while :
do
	case "$1" in
		--) 	shift; break;;			# EOF options
		-e|-f) 	pattern="$1 $2"; shift;;	# -f file
		-l|-L) 	line=$1; 	grepopt="$grepopt $1";;
		-h|-q) 	header=$1; 	grepopt="$grepopt $1";;
  		*) 			grepopt="$grepopt $1";;	# rest
	esac
	shift
done

# check pattern
case X"$pattern" in
	X) case $# in 
		0) echo "usage: $0 [grep_options] pattern [files]"; exit 1;; 
		*) pattern=$1; shift;;
	   esac
esac


files="$@"	
# no shell loop neccessary for option -q or -h 
# and a single file or reading from stdin
case "$header"X"$#" in 
	-h*|-q*|X0|X1) 	$gzip $gzipopt $files | $grep $grepopt $pattern
			exit $?;; 
esac


exit=1
for f in $files
do
	case "$line" in
		-l|-L) 	if $gzip $gzipopt $f | 
				$grep $grepopt $pattern >/dev/null
			then
				echo $f
			fi
			;;
		*) $gzip $gzipopt $f | $grep $grepopt $pattern | 
			sed "s%^%${f}:%";;
	esac

	case $? in 
		2) exit=$?;; 
		0) case "$exit" in 2);; 1) exit=0;; esac;;
	esac
done

exit $exit
OpenPOWER on IntegriCloud