summaryrefslogtreecommitdiffstats
path: root/zgrep.in
blob: 3d9e8f3c8359045e66a1e86d122bbe200c37fe39 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /bin/sh
# Zgrep - Grep wrapper for compressed files.
# Copyright (C) 2008, 2009 Antonio Diaz Diaz.
#
# This script is free software: you have unlimited permission
# to copy, distribute and modify it.

LC_ALL=C
export LC_ALL
invocation_name=$0
args=
default_prog=lzip
have_pat=0
list=0
no_name=0

# Loop over args until pattern is found
while [ x"$1" != x ] ; do

	case "$1" in
	--help | --he* | -h)
		echo "Zgrep - Grep wrapper for compressed files."
		echo
		echo "Zgrep is a wrapper script around the grep command that allows"
		echo "transparent search on any combination of compressed and non-compressed"
		echo "files. If any given file is compressed, its uncompressed content is"
		echo "used. If a given file does not exist, zgrep tries the compressed file"
		echo "name corresponding to the default compressor selected. The supported"
		echo "compressors are gzip, bzip2, lzip and xz."
		echo
		echo "Usage: ${invocation_name} [OPTIONS] [GREP_OPTIONS] PATTERN [FILES]"
		echo
		echo "GREP_OPTIONS are passed directly to grep."
		echo "The exit status from grep is preserved."
		echo
		echo "Options:"
		echo "  -h, --help          display this help and exit"
		echo "  -V, --version       output version information and exit"
		echo "      --gzip          use gzip as default decompressor"
		echo "      --bzip2         use bzip2 as default decompressor"
		echo "      --lzip          use lzip as default decompressor (default)"
		echo "      --xz            use xz as default decompressor"
		echo
		echo "Report bugs to lzip-bug@nongnu.org"
		echo "Lzip home page: http://www.nongnu.org/lzip/lzip.html"
		exit 0 ;;
	--version | --ve* | -V)
		echo "Zgrep VERSION"
		echo "Copyright (C) 2009 Antonio Diaz Diaz."
		echo "This script is free software: you have unlimited permission"
		echo "to copy, distribute and modify it."
		exit 0 ;;
	--gz*)
		default_prog=gzip ;;
	--bz*)
		default_prog=bzip2 ;;
	--lz*)
		default_prog=lzip ;;
	--xz*)
		default_prog=xz ;;
	-[drRzZ] | --di* | --exc* | --inc* | --nu* | --rec*)
		echo "${invocation_name}: option $1 not supported"
		exit 1 ;;
	-e?* | -f?* | --file=* | --reg*=*)
		args="${args} $1"; have_pat=1 ;;
	-e | -f | --file | --reg*)
		args="${args} $1 $2"; shift; have_pat=1 ;;
	--*=*)
		args="${args} $1" ;;
	-[ABCDm] | --af* | --be* | --binary-* | --con* | --de* | --[lm]a*)
		args="${args} $1 $2"; shift ;;
	-l | --files-with-*)
		args="${args} $1"; list=1 ;;
	-h | --no-f*)
		args="${args} $1"; no_name=1 ;;
	--)
		break ;;
	-?*)
		args="${args} $1" ;;
	 *)
	 	if test ${have_pat} = 0; then args="${args} $1"; have_pat=1
		else break
		fi ;;
	esac
	shift
done

if test ${have_pat} = 0; then
	echo "${invocation_name}: Pattern not found; use --help for usage." 1>&2
	exit 1
fi

if test $# -eq 0; then
	${default_prog} -cdfq | grep ${args}
	exit $?
fi

retval=0
for i in "$@" ; do
	prog="${default_prog} -cdfq"
	case "$i" in
	*.gz | *.tgz)
		prog="gzip -cdfq" ;;
	*.bz2 | *.tbz | *.tbz2)
		prog="bzip2 -cdfq" ;;
	*.lz | *.tlz)
		prog="lzip -cdfq" ;;
	*.xz | *.txz)
		prog="xz -cdfq" ;;
	*)
		if test -f "$i"; then prog=cat
		else
			case ${default_prog} in
			gzip)
				if test -f "$i.gz"; then i="$i.gz"; fi ;;
			bzip2)
				if test -f "$i.bz2"; then i="$i.bz2"; fi ;;
			lzip)
				if test -f "$i.lz"; then i="$i.lz"; fi ;;
			xz)
				if test -f "$i.xz"; then i="$i.xz"; fi ;;
			esac
		fi ;;
	esac
	if test ${list} -eq 1; then
		${prog} -- "$i" | grep ${args} 2>&1 > /dev/null && echo "$i"
		r=$?
	elif test $# -eq 1 -o ${no_name} -eq 1; then
		${prog} -- "$i" | grep ${args}
		r=$?
	else
		j=`printf "%s" "$i" | sed 's/\\\\/\\\\\\\\/g'`
		j=`printf "%s" "$j" | sed 's/|/\\\\|/g'`
		j=`printf "%s" "$j" | sed 's/&/\\\\&/g'`
		j=`printf "%s" "$j" | tr '\n' ' '`
		${prog} -- "$i" | grep ${args} | sed "s|^|${j}:|"
		r=$?
	fi
	test "$r" -ne 0 && retval="$r"
done

exit ${retval}