1114db65bSPeter Avalos#!@POSIX_SHELL@ 2114db65bSPeter Avalos 3114db65bSPeter Avalos# xzgrep -- a wrapper around a grep program that decompresses files as needed 4114db65bSPeter Avalos# Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca> 5114db65bSPeter Avalos 6114db65bSPeter Avalos# Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation 7114db65bSPeter Avalos# Copyright (C) 1993 Jean-loup Gailly 8114db65bSPeter Avalos 9114db65bSPeter Avalos# Modified for XZ Utils by Andrew Dudman and Lasse Collin. 10114db65bSPeter Avalos 11114db65bSPeter Avalos# This program is free software; you can redistribute it and/or modify 12114db65bSPeter Avalos# it under the terms of the GNU General Public License as published by 13114db65bSPeter Avalos# the Free Software Foundation; either version 2 of the License, or 14114db65bSPeter Avalos# (at your option) any later version. 15114db65bSPeter Avalos 16114db65bSPeter Avalos# This program is distributed in the hope that it will be useful, 17114db65bSPeter Avalos# but WITHOUT ANY WARRANTY; without even the implied warranty of 18114db65bSPeter Avalos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19114db65bSPeter Avalos# GNU General Public License for more details. 20114db65bSPeter Avalos 21*e151908bSDaniel Fojt@enable_path_for_scripts@ 22114db65bSPeter Avalos#SET_PATH - This line is a placeholder to ease patching this script. 23114db65bSPeter Avalos 24114db65bSPeter Avalos# Instead of unsetting XZ_OPT, just make sure that xz will use file format 25114db65bSPeter Avalos# autodetection. This way memory usage limit and thread limit can be 2615ab8c86SJohn Marino# specified via XZ_OPT. With gzip, bzip2, and lzop it's OK to just unset the 27114db65bSPeter Avalos# environment variables. 28114db65bSPeter Avalosxz='@xz@ --format=auto' 2915ab8c86SJohn Marinounset GZIP BZIP BZIP2 LZOP 30114db65bSPeter Avalos 31114db65bSPeter Avaloscase ${0##*/} in 32114db65bSPeter Avalos *egrep*) prog=xzegrep; grep=${GREP:-egrep};; 33114db65bSPeter Avalos *fgrep*) prog=xzfgrep; grep=${GREP:-fgrep};; 34114db65bSPeter Avalos *) prog=xzgrep; grep=${GREP:-grep};; 35114db65bSPeter Avalosesac 36114db65bSPeter Avalos 37114db65bSPeter Avalosversion="$prog (@PACKAGE_NAME@) @VERSION@" 38114db65bSPeter Avalos 39114db65bSPeter Avalosusage="Usage: ${0##*/} [OPTION]... [-e] PATTERN [FILE]... 40114db65bSPeter AvalosLook for instances of PATTERN in the input FILEs, using their 41114db65bSPeter Avalosuncompressed contents if they are compressed. 42114db65bSPeter Avalos 43114db65bSPeter AvalosOPTIONs are the same as for '$grep'. 44114db65bSPeter Avalos 45114db65bSPeter AvalosReport bugs to <@PACKAGE_BUGREPORT@>." 46114db65bSPeter Avalos 47114db65bSPeter Avalos# sed script to escape all ' for the shell, and then (to handle trailing 48114db65bSPeter Avalos# newlines correctly) turn trailing X on last line into '. 49114db65bSPeter Avalosescape=' 50114db65bSPeter Avalos s/'\''/'\''\\'\'''\''/g 51114db65bSPeter Avalos $s/X$/'\''/ 52114db65bSPeter Avalos' 53114db65bSPeter Avalosoperands= 54114db65bSPeter Avaloshave_pat=0 55114db65bSPeter Avalosfiles_with_matches=0 56114db65bSPeter Avalosfiles_without_matches=0 57114db65bSPeter Avalosno_filename=0 58114db65bSPeter Avaloswith_filename=0 59114db65bSPeter Avalos 60114db65bSPeter Avaloswhile test $# -ne 0; do 61114db65bSPeter Avalos option=$1 62114db65bSPeter Avalos shift 63114db65bSPeter Avalos optarg= 64114db65bSPeter Avalos 65114db65bSPeter Avalos case $option in 66114db65bSPeter Avalos (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*) 67114db65bSPeter Avalos arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape") 68114db65bSPeter Avalos eval "set -- $arg2 "'${1+"$@"}' 69114db65bSPeter Avalos option=$(expr "X$option" : 'X\(-.[0-9]*\)');; 70114db65bSPeter Avalos (--binary-*=* | --[lm]a*=* | --reg*=*) 71114db65bSPeter Avalos ;; 72114db65bSPeter Avalos (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*) 73114db65bSPeter Avalos case ${1?"$option option requires an argument"} in 74114db65bSPeter Avalos (*\'*) 75114db65bSPeter Avalos optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");; 76114db65bSPeter Avalos (*) 77114db65bSPeter Avalos optarg=" '$1'";; 78114db65bSPeter Avalos esac 79114db65bSPeter Avalos shift;; 80114db65bSPeter Avalos (--) 81114db65bSPeter Avalos break;; 82114db65bSPeter Avalos (-?*) 83114db65bSPeter Avalos ;; 84114db65bSPeter Avalos (*) 85114db65bSPeter Avalos case $option in 86114db65bSPeter Avalos (*\'*) 87114db65bSPeter Avalos operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");; 88114db65bSPeter Avalos (*) 89114db65bSPeter Avalos operands="$operands '$option'";; 90114db65bSPeter Avalos esac 91114db65bSPeter Avalos ${POSIXLY_CORRECT+break} 92114db65bSPeter Avalos continue;; 93114db65bSPeter Avalos esac 94114db65bSPeter Avalos 95114db65bSPeter Avalos case $option in 96114db65bSPeter Avalos (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*) 97114db65bSPeter Avalos printf >&2 '%s: %s: Option not supported\n' "$0" "$option" 98114db65bSPeter Avalos exit 2;; 99114db65bSPeter Avalos (-[ef]* | --file | --file=* | --reg*) 100114db65bSPeter Avalos have_pat=1;; 101114db65bSPeter Avalos (--h | --he | --hel | --help) 102114db65bSPeter Avalos echo "$usage" || exit 2 103114db65bSPeter Avalos exit;; 104114db65bSPeter Avalos (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \ 105114db65bSPeter Avalos | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \ 106114db65bSPeter Avalos | --with-filename) 107114db65bSPeter Avalos with_filename=1 108114db65bSPeter Avalos continue;; 109114db65bSPeter Avalos (-l | --files-with-*) 11015ab8c86SJohn Marino files_with_matches=1 11115ab8c86SJohn Marino continue;; 112114db65bSPeter Avalos (-L | --files-witho*) 11315ab8c86SJohn Marino files_without_matches=1 11415ab8c86SJohn Marino continue;; 115a530a267SJohn Marino (-h | --no-f*) 116114db65bSPeter Avalos no_filename=1;; 117114db65bSPeter Avalos (-V | --v | --ve | --ver | --vers | --versi | --versio | --version) 118114db65bSPeter Avalos echo "$version" || exit 2 119114db65bSPeter Avalos exit;; 120114db65bSPeter Avalos esac 121114db65bSPeter Avalos 122114db65bSPeter Avalos case $option in 123114db65bSPeter Avalos (*\'?*) 124114db65bSPeter Avalos option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");; 125114db65bSPeter Avalos (*) 126114db65bSPeter Avalos option="'$option'";; 127114db65bSPeter Avalos esac 128114db65bSPeter Avalos 129114db65bSPeter Avalos grep="$grep $option$optarg" 130114db65bSPeter Avalosdone 131114db65bSPeter Avalos 132114db65bSPeter Avalosif test $files_with_matches -eq 1 || test $files_without_matches -eq 1; then 133114db65bSPeter Avalos grep="$grep -q" 134114db65bSPeter Avalosfi 135114db65bSPeter Avalos 136114db65bSPeter Avaloseval "set -- $operands "'${1+"$@"}' 137114db65bSPeter Avalos 138114db65bSPeter Avalosif test $have_pat -eq 0; then 139114db65bSPeter Avalos case ${1?"Missing pattern; try \`${0##*/} --help' for help"} in 140114db65bSPeter Avalos (*\'*) 141114db65bSPeter Avalos grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");; 142114db65bSPeter Avalos (*) 143114db65bSPeter Avalos grep="$grep -- '$1'";; 144114db65bSPeter Avalos esac 145114db65bSPeter Avalos shift 146114db65bSPeter Avalosfi 147114db65bSPeter Avalos 148114db65bSPeter Avalosif test $# -eq 0; then 149114db65bSPeter Avalos set -- - 150114db65bSPeter Avalosfi 151114db65bSPeter Avalos 152114db65bSPeter Avalosexec 3>&1 153a530a267SJohn Marino 154a530a267SJohn Marino# res=1 means that no file matched yet 155a530a267SJohn Marinores=1 156114db65bSPeter Avalos 157114db65bSPeter Avalosfor i; do 158114db65bSPeter Avalos case $i in 159114db65bSPeter Avalos *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) uncompress="gzip -cdfq";; 160114db65bSPeter Avalos *[-.]bz2 | *[-.]tbz | *.tbz2) uncompress="bzip2 -cdfq";; 16115ab8c86SJohn Marino *[-.]lzo | *[-.]tzo) uncompress="lzop -cdfq";; 162114db65bSPeter Avalos *) uncompress="$xz -cdfq";; 163114db65bSPeter Avalos esac 164114db65bSPeter Avalos # Fail if xz or grep (or sed) fails. 165114db65bSPeter Avalos xz_status=$( 166114db65bSPeter Avalos exec 5>&1 167114db65bSPeter Avalos ($uncompress -- "$i" 5>&-; echo $? >&5) 3>&- | 168114db65bSPeter Avalos if test $files_with_matches -eq 1; then 169114db65bSPeter Avalos eval "$grep" && { printf '%s\n' "$i" || exit 2; } 170114db65bSPeter Avalos elif test $files_without_matches -eq 1; then 171114db65bSPeter Avalos eval "$grep" || { 172114db65bSPeter Avalos r=$? 173114db65bSPeter Avalos if test $r -eq 1; then 174114db65bSPeter Avalos printf '%s\n' "$i" || r=2 175114db65bSPeter Avalos fi 176114db65bSPeter Avalos exit $r 177114db65bSPeter Avalos } 178114db65bSPeter Avalos elif test $with_filename -eq 0 && 179114db65bSPeter Avalos { test $# -eq 1 || test $no_filename -eq 1; }; then 180114db65bSPeter Avalos eval "$grep" 181114db65bSPeter Avalos else 182114db65bSPeter Avalos case $i in 183114db65bSPeter Avalos (*' 184114db65bSPeter Avalos'* | *'&'* | *'\'* | *'|'*) 185114db65bSPeter Avalos i=$(printf '%s\n' "$i" | 186114db65bSPeter Avalos sed ' 187114db65bSPeter Avalos $!N 188114db65bSPeter Avalos $s/[&\|]/\\&/g 189114db65bSPeter Avalos $s/\n/\\n/g 190114db65bSPeter Avalos ');; 191114db65bSPeter Avalos esac 192114db65bSPeter Avalos sed_script="s|^|$i:|" 193114db65bSPeter Avalos 194114db65bSPeter Avalos # Fail if grep or sed fails. 195114db65bSPeter Avalos r=$( 196114db65bSPeter Avalos exec 4>&1 197114db65bSPeter Avalos (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&- 198114db65bSPeter Avalos ) || r=2 199114db65bSPeter Avalos exit $r 200114db65bSPeter Avalos fi >&3 5>&- 201114db65bSPeter Avalos ) 202114db65bSPeter Avalos r=$? 203a530a267SJohn Marino 204*e151908bSDaniel Fojt # fail occurred previously, nothing worse can happen 205a530a267SJohn Marino test $res -gt 1 && continue 206a530a267SJohn Marino 207b892b6baSPeter Avalos test "$xz_status" -eq 0 || test "$xz_status" -eq 2 \ 208b892b6baSPeter Avalos || test "$(kill -l "$xz_status" 2> /dev/null)" = "PIPE" || r=2 209a530a267SJohn Marino 210a530a267SJohn Marino # still no match 211a530a267SJohn Marino test $r -eq 1 && continue 212a530a267SJohn Marino 213a530a267SJohn Marino # 0 == match, >=2 == fail 214a530a267SJohn Marino res=$r 215114db65bSPeter Avalosdone 216114db65bSPeter Avalosexit $res 217