3 # xzgrep -- a wrapper around a grep program that decompresses files as needed
4 # Adapted to XZ Utils from gzip-1.3.3 + Red Hat's security patches
5 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
6 # Copyright (C) 1998, 2001 Free Software Foundation
7 # Copyright (C) 1993 Jean-loup Gailly
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 # Improve error handling, this is supported by bash but not all the other
25 # shells so we hide the possible error:
26 set -o pipefail > /dev/null 2> /dev/null
28 prog=`echo "$0" | sed 's|.*/||'`
30 *egrep) grep=${EGREP-egrep} ;;
31 *fgrep) grep=${FGREP-fgrep} ;;
32 *) grep=${GREP-grep} ;;
38 files_without_matches=0
42 while test $# -ne 0; do
43 case "$after_dash_dash$1" in
44 --d* | --rec*) echo >&2 "$0: $1: option not supported"; exit 1;;
45 --files-with-*) files_with_matches=1;;
46 --files-witho*) files_without_matches=1;;
47 --no-f*) no_filename=1;;
48 --wi*) with_filename=1;;
52 -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 1;;
55 -*H*) with_filename=1;;
61 -*L*) files_without_matches=1;;
64 -*l*) files_with_matches=1;;
67 case "$after_dash_dash$1" in
68 -[ef]) opt="$opt $1"; shift; pat="$1"
69 if test "$grep" = grep; then # grep is buggy with -e on SVR4
72 -[ABCdm])opt="$opt $1 $2"; shift;;
73 --) opt="$opt $1"; after_dash_dash=1;;
75 *) if test -z "$pat"; then
84 if test -z "$pat"; then
85 echo "grep through .xz and .lzma files"
86 echo "usage: $prog [grep_options] pattern [files]"
90 if test $# -eq 0; then
91 xz -dc | $grep $opt "$pat"
99 if test $files_with_matches -eq 1; then
100 $grep $opt "$pat" > /dev/null && printf "%s\n" "$i"
101 elif test $files_without_matches -eq 1; then
102 $grep $opt "$pat" > /dev/null || printf "%s\n" "$i"
103 elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
109 i=`printf "%s" "$i" | tr '\n' ' '`
110 if test $with_filename -eq 1; then
111 sed_script="s|^[^:]*:|${i}:|"
113 sed_script="s|^|${i}:|"
115 $grep $opt "$pat" | sed "$sed_script"
118 test $res -lt $r && res=$r
120 test "$r" -eq 141 && exit $res