3
# xzgrep -- a wrapper around a grep program that decompresses files as needed
4
# Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
6
# Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
7
# Copyright (C) 1993 Jean-loup Gailly
9
# Modified for XZ Utils by Andrew Dudman and Lasse Collin.
11
# This program is free software; you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation; either version 2 of the License, or
14
# (at your option) any later version.
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
# GNU General Public License for more details.
21
#SET_PATH - This line is a placeholder to ease patching this script.
23
# Instead of unsetting XZ_OPT, just make sure that xz will use file format
24
# autodetection. This way memory usage limit and thread limit can be
25
# specified via XZ_OPT. With gzip and bzip2 it's OK to just unset the
26
# environment variables.
27
xz='@xz@ --format=auto'
31
*egrep*) prog=xzegrep; grep=${GREP:-egrep};;
32
*fgrep*) prog=xzfgrep; grep=${GREP:-fgrep};;
33
*) prog=xzgrep; grep=${GREP:-grep};;
36
version="$prog (@PACKAGE_NAME@) @VERSION@"
38
usage="Usage: ${0##/*} [OPTION]... [-e] PATTERN [FILE]...
39
Look for instances of PATTERN in the input FILEs, using their
40
uncompressed contents if they are compressed.
42
OPTIONs are the same as for '$grep'.
44
Report bugs to <@PACKAGE_BUGREPORT@>."
46
# sed script to escape all ' for the shell, and then (to handle trailing
47
# newlines correctly) turn trailing X on last line into '.
49
s/'\''/'\''\\'\'''\''/g
55
files_without_matches=0
59
while test $# -ne 0; do
65
(-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
66
arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
67
eval "set -- $arg2 "'${1+"$@"}'
68
option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
69
(--binary-*=* | --[lm]a*=* | --reg*=*)
71
(-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
72
case ${1?"$option option requires an argument"} in
74
optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
86
operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
88
operands="$operands '$option'";;
90
${POSIXLY_CORRECT+break}
95
(-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
96
printf >&2 '%s: %s: Option not supported\n' "$0" "$option"
98
(-[ef]* | --file | --file=* | --reg*)
100
(--h | --he | --hel | --help)
101
echo "$usage" || exit 2
103
(-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
104
| --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
108
(-l | --files-with-*)
109
files_with_matches=1;;
110
(-L | --files-witho*)
111
files_without_matches=1;;
114
(-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
115
echo "$version" || exit 2
121
option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
126
grep="$grep $option$optarg"
129
eval "set -- $operands "'${1+"$@"}'
131
if test $have_pat -eq 0; then
132
case ${1?"Missing pattern; try \`${0##*/} --help' for help"} in
134
grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
136
grep="$grep -- '$1'";;
141
if test $# -eq 0; then
150
*[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) uncompress="gzip -cdfq";;
151
*[-.]bz2 | *[-.]tbz | *.tbz2) uncompress="bzip2 -cdfq";;
152
*) uncompress="$xz -cdfq";;
154
# Fail if xz or grep (or sed) fails.
157
(eval "$uncompress" -- "$i" 5>&-; echo $? >&5) 3>&- |
158
if test $files_with_matches -eq 1; then
159
eval "$grep" -q && { printf '%s\n' "$i" || exit 2; }
160
elif test $files_without_matches -eq 1; then
163
if test $r -eq 1; then
164
printf '%s\n' "$i" || r=2
168
elif test $with_filename -eq 0 &&
169
{ test $# -eq 1 || test $no_filename -eq 1; }; then
174
'* | *'&'* | *'\'* | *'|'*)
175
i=$(printf '%s\n' "$i" |
182
sed_script="s|^|$i:|"
184
# Fail if grep or sed fails.
187
(eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
193
test "$xz_status" -eq 0 || test "$xz_status" -eq 2 || r=2
194
test $res -lt $r && res=$r