xref: /dragonfly/contrib/xz/src/scripts/xzgrep.in (revision 1975d09e)
1#!@POSIX_SHELL@
2
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>
5
6# Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
7# Copyright (C) 1993 Jean-loup Gailly
8
9# Modified for XZ Utils by Andrew Dudman and Lasse Collin.
10
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.
15
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.
20
21@enable_path_for_scripts@
22#SET_PATH - This line is a placeholder to ease patching this script.
23
24# Instead of unsetting XZ_OPT, just make sure that xz will use file format
25# autodetection. This way memory usage limit and thread limit can be
26# specified via XZ_OPT. With gzip, bzip2, and lzop it's OK to just unset the
27# environment variables.
28xz='@xz@ --format=auto'
29unset GZIP BZIP BZIP2 LZOP
30
31case ${0##*/} in
32  *egrep*) prog=xzegrep; grep=${GREP:-egrep};;
33  *fgrep*) prog=xzfgrep; grep=${GREP:-fgrep};;
34  *)       prog=xzgrep; grep=${GREP:-grep};;
35esac
36
37version="$prog (@PACKAGE_NAME@) @VERSION@"
38
39usage="Usage: ${0##*/} [OPTION]... [-e] PATTERN [FILE]...
40Look for instances of PATTERN in the input FILEs, using their
41uncompressed contents if they are compressed.
42
43OPTIONs are the same as for '$grep'.
44
45Report bugs to <@PACKAGE_BUGREPORT@>."
46
47# sed script to escape all ' for the shell, and then (to handle trailing
48# newlines correctly) turn trailing X on last line into '.
49escape='
50  s/'\''/'\''\\'\'''\''/g
51  $s/X$/'\''/
52'
53operands=
54have_pat=0
55files_with_matches=0
56files_without_matches=0
57no_filename=0
58with_filename=0
59
60while test $# -ne 0; do
61  option=$1
62  shift
63  optarg=
64
65  case $option in
66  (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
67    arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
68    eval "set -- $arg2 "'${1+"$@"}'
69    option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
70  (--binary-*=* | --[lm]a*=* | --reg*=*)
71    ;;
72  (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
73    case ${1?"$option option requires an argument"} in
74    (*\'*)
75      optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
76    (*)
77      optarg=" '$1'";;
78    esac
79    shift;;
80  (--)
81    break;;
82  (-?*)
83    ;;
84  (*)
85    case $option in
86    (*\'*)
87      operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
88    (*)
89      operands="$operands '$option'";;
90    esac
91    ${POSIXLY_CORRECT+break}
92    continue;;
93  esac
94
95  case $option in
96  (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
97    printf >&2 '%s: %s: Option not supported\n' "$0" "$option"
98    exit 2;;
99  (-[ef]* | --file | --file=* | --reg*)
100    have_pat=1;;
101  (--h | --he | --hel | --help)
102    echo "$usage" || exit 2
103    exit;;
104  (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
105  | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
106  | --with-filename)
107    with_filename=1
108    continue;;
109  (-l | --files-with-*)
110    files_with_matches=1
111    continue;;
112  (-L | --files-witho*)
113    files_without_matches=1
114    continue;;
115  (-h | --no-f*)
116    no_filename=1;;
117  (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
118    echo "$version" || exit 2
119    exit;;
120  esac
121
122  case $option in
123  (*\'?*)
124    option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
125  (*)
126    option="'$option'";;
127  esac
128
129  grep="$grep $option$optarg"
130done
131
132if test $files_with_matches -eq 1 || test $files_without_matches -eq 1; then
133  grep="$grep -q"
134fi
135
136eval "set -- $operands "'${1+"$@"}'
137
138if test $have_pat -eq 0; then
139  case ${1?"Missing pattern; try \`${0##*/} --help' for help"} in
140  (*\'*)
141    grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
142  (*)
143    grep="$grep -- '$1'";;
144  esac
145  shift
146fi
147
148if test $# -eq 0; then
149  set -- -
150fi
151
152exec 3>&1
153
154# res=1 means that no file matched yet
155res=1
156
157for i; do
158  case $i in
159    *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) uncompress="gzip -cdfq";;
160    *[-.]bz2 | *[-.]tbz | *.tbz2) uncompress="bzip2 -cdfq";;
161    *[-.]lzo | *[-.]tzo) uncompress="lzop -cdfq";;
162    *) uncompress="$xz -cdfq";;
163  esac
164  # Fail if xz or grep (or sed) fails.
165  xz_status=$(
166    exec 5>&1
167    ($uncompress -- "$i" 5>&-; echo $? >&5) 3>&- |
168    if test $files_with_matches -eq 1; then
169      eval "$grep" && { printf '%s\n' "$i" || exit 2; }
170    elif test $files_without_matches -eq 1; then
171      eval "$grep" || {
172        r=$?
173        if test $r -eq 1; then
174          printf '%s\n' "$i" || r=2
175        fi
176        exit $r
177      }
178    elif test $with_filename -eq 0 &&
179         { test $# -eq 1 || test $no_filename -eq 1; }; then
180      eval "$grep"
181    else
182      case $i in
183      (*'
184'* | *'&'* | *'\'* | *'|'*)
185        i=$(printf '%s\n' "$i" |
186            sed '
187              $!N
188              $s/[&\|]/\\&/g
189              $s/\n/\\n/g
190            ');;
191      esac
192      sed_script="s|^|$i:|"
193
194      # Fail if grep or sed fails.
195      r=$(
196        exec 4>&1
197        (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
198      ) || r=2
199      exit $r
200    fi >&3 5>&-
201  )
202  r=$?
203
204  # fail occurred previously, nothing worse can happen
205  test $res -gt 1 && continue
206
207  test "$xz_status" -eq 0 || test "$xz_status" -eq 2 \
208      || test "$(kill -l "$xz_status" 2> /dev/null)" = "PIPE" || r=2
209
210  # still no match
211  test $r -eq 1 && continue
212
213  # 0 == match, >=2 == fail
214  res=$r
215done
216exit $res
217