]> icculus.org git repositories - icculus/xz.git/blob - src/scripts/lzgrep
Imported to git.
[icculus/xz.git] / src / scripts / lzgrep
1 #!/bin/sh
2
3 # lzgrep -- a wrapper around a grep program that decompresses files as needed
4 # Adapted to LZMA 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
8
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)
12 # any later version.
13
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.
18
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
22 # 02111-1307, USA.
23
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
27
28 prog=`echo "$0" | sed 's|.*/||'`
29 case "$prog" in
30         *egrep) grep=${EGREP-egrep}     ;;
31         *fgrep) grep=${FGREP-fgrep}     ;;
32         *)      grep=${GREP-grep}       ;;
33 esac
34
35 pat=""
36 after_dash_dash=""
37 files_with_matches=0
38 files_without_matches=0
39 no_filename=0
40 with_filename=0
41
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;;
49   --*)  ;;
50   -*)
51         case "$1" in
52         -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 1;;
53         esac
54         case "$1" in
55         -*H*)   with_filename=1;;
56         esac
57         case "$1" in
58         -*h*)   no_filename=1;;
59         esac
60         case "$1" in
61         -*L*)   files_without_matches=1;;
62         esac
63         case "$1" in
64         -*l*)   files_with_matches=1;;
65         esac;;
66   esac
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
70              grep=egrep
71            fi;;
72   -[ABCdm])opt="$opt $1 $2"; shift;;
73   --)      opt="$opt $1"; after_dash_dash=1;;
74   -*)      opt="$opt $1";;
75    *)      if test -z "$pat"; then
76              pat="$1"
77            else
78              break;
79            fi;;
80   esac
81   shift
82 done
83
84 if test -z "$pat"; then
85   echo "grep through lzma files"
86   echo "usage: $prog [grep_options] pattern [files]"
87   exit 1
88 fi
89
90 if test $# -eq 0; then
91   lzma -dc | $grep $opt "$pat"
92   exit $?
93 fi
94
95 res=0
96 trap break SIGPIPE
97 for i do
98   lzma -dc "$i" |
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
104       $grep $opt "$pat"
105     else
106       i=${i//\\/\\\\}
107       i=${i//|/\\|}
108       i=${i//&/\\&}
109       i=`printf "%s" "$i" | tr '\n' ' '`
110       if test $with_filename -eq 1; then
111         sed_script="s|^[^:]*:|${i}:|"
112       else
113         sed_script="s|^|${i}:|"
114       fi
115       $grep $opt "$pat" | sed "$sed_script"
116     fi
117   r=$?
118   test $res -lt $r && res=$r
119   # SIGPIPE + 128
120   test "$r" -eq 141 && exit $res
121 done
122 trap - SIGPIPE
123 exit $res