summaryrefslogtreecommitdiffstats
path: root/zgrep.in
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 16:10:15 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 16:10:15 +0000
commitaf501f6c192faeb810b4dfb587ef6206a95b2446 (patch)
tree536cc0b864f878fec3812d3f46aa41f00933a504 /zgrep.in
parentAdding upstream version 0.2. (diff)
downloadzutils-af501f6c192faeb810b4dfb587ef6206a95b2446.tar.xz
zutils-af501f6c192faeb810b4dfb587ef6206a95b2446.zip
Adding upstream version 0.3.upstream/0.3
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'zgrep.in')
-rw-r--r--zgrep.in116
1 files changed, 49 insertions, 67 deletions
diff --git a/zgrep.in b/zgrep.in
index 3d9e8f3..7321bdb 100644
--- a/zgrep.in
+++ b/zgrep.in
@@ -7,12 +7,11 @@
LC_ALL=C
export LC_ALL
-invocation_name=$0
args=
-default_prog=lzip
have_pat=0
list=0
no_name=0
+two_hyphens=0
# Loop over args until pattern is found
while [ x"$1" != x ] ; do
@@ -25,10 +24,10 @@ while [ x"$1" != x ] ; do
echo "transparent search on any combination of compressed and non-compressed"
echo "files. If any given file is compressed, its uncompressed content is"
echo "used. If a given file does not exist, zgrep tries the compressed file"
- echo "name corresponding to the default compressor selected. The supported"
- echo "compressors are gzip, bzip2, lzip and xz."
+ echo "names corresponding to the supported compressors."
+ echo "The supported compressors are gzip, bzip2, lzip and xz."
echo
- echo "Usage: ${invocation_name} [OPTIONS] [GREP_OPTIONS] PATTERN [FILES]"
+ echo "Usage: $0 [OPTIONS] [GREP_OPTIONS] PATTERN [FILES]"
echo
echo "GREP_OPTIONS are passed directly to grep."
echo "The exit status from grep is preserved."
@@ -36,13 +35,9 @@ while [ x"$1" != x ] ; do
echo "Options:"
echo " -h, --help display this help and exit"
echo " -V, --version output version information and exit"
- echo " --gzip use gzip as default decompressor"
- echo " --bzip2 use bzip2 as default decompressor"
- echo " --lzip use lzip as default decompressor (default)"
- echo " --xz use xz as default decompressor"
echo
- echo "Report bugs to lzip-bug@nongnu.org"
- echo "Lzip home page: http://www.nongnu.org/lzip/lzip.html"
+ echo "Report bugs to zutils-bug@nongnu.org"
+ echo "Zutils home page: http://www.nongnu.org/zutils/zutils.html"
exit 0 ;;
--version | --ve* | -V)
echo "Zgrep VERSION"
@@ -50,16 +45,8 @@ while [ x"$1" != x ] ; do
echo "This script is free software: you have unlimited permission"
echo "to copy, distribute and modify it."
exit 0 ;;
- --gz*)
- default_prog=gzip ;;
- --bz*)
- default_prog=bzip2 ;;
- --lz*)
- default_prog=lzip ;;
- --xz*)
- default_prog=xz ;;
-[drRzZ] | --di* | --exc* | --inc* | --nu* | --rec*)
- echo "${invocation_name}: option $1 not supported"
+ echo "$0: option $1 not supported"
exit 1 ;;
-e?* | -f?* | --file=* | --reg*=*)
args="${args} $1"; have_pat=1 ;;
@@ -74,69 +61,64 @@ while [ x"$1" != x ] ; do
-h | --no-f*)
args="${args} $1"; no_name=1 ;;
--)
- break ;;
+ shift ; two_hyphens=1 ; break ;;
-?*)
args="${args} $1" ;;
*)
- if test ${have_pat} = 0; then args="${args} $1"; have_pat=1
+ if [ ${have_pat} = 0 ]; then args="${args} $1"; have_pat=1
else break
fi ;;
esac
shift
done
-if test ${have_pat} = 0; then
- echo "${invocation_name}: Pattern not found; use --help for usage." 1>&2
+if [ ${have_pat} = 0 ]; then
+ echo "$0: Pattern not found; use --help for usage." 1>&2
exit 1
fi
-if test $# -eq 0; then
- ${default_prog} -cdfq | grep ${args}
- exit $?
-fi
-
retval=0
for i in "$@" ; do
- prog="${default_prog} -cdfq"
- case "$i" in
- *.gz | *.tgz)
- prog="gzip -cdfq" ;;
- *.bz2 | *.tbz | *.tbz2)
- prog="bzip2 -cdfq" ;;
- *.lz | *.tlz)
- prog="lzip -cdfq" ;;
- *.xz | *.txz)
- prog="xz -cdfq" ;;
- *)
- if test -f "$i"; then prog=cat
- else
- case ${default_prog} in
- gzip)
- if test -f "$i.gz"; then i="$i.gz"; fi ;;
- bzip2)
- if test -f "$i.bz2"; then i="$i.bz2"; fi ;;
- lzip)
- if test -f "$i.lz"; then i="$i.lz"; fi ;;
- xz)
- if test -f "$i.xz"; then i="$i.xz"; fi ;;
- esac
- fi ;;
- esac
- if test ${list} -eq 1; then
- ${prog} -- "$i" | grep ${args} 2>&1 > /dev/null && echo "$i"
- r=$?
- elif test $# -eq 1 -o ${no_name} -eq 1; then
- ${prog} -- "$i" | grep ${args}
- r=$?
+ if [ "$i" = "--" ] && [ ${two_hyphens} = 0 ] ; then two_hyphens=1
else
- j=`printf "%s" "$i" | sed 's/\\\\/\\\\\\\\/g'`
- j=`printf "%s" "$j" | sed 's/|/\\\\|/g'`
- j=`printf "%s" "$j" | sed 's/&/\\\\&/g'`
- j=`printf "%s" "$j" | tr '\n' ' '`
- ${prog} -- "$i" | grep ${args} | sed "s|^|${j}:|"
- r=$?
+ if [ -f "$i" ]; then
+ case "$i" in
+ *.gz | *.tgz)
+ prog="gzip -cdfq" ;;
+ *.bz2 | *.tbz | *.tbz2)
+ prog="bzip2 -cdfq" ;;
+ *.lz | *.tlz)
+ prog="lzip -cdfq" ;;
+ *.xz | *.txz)
+ prog="xz -cdfq" ;;
+ *)
+ prog=cat ;;
+ esac
+ elif [ -f "$i.gz" ]; then i="$i.gz" ; prog="gzip -cdfq"
+ elif [ -f "$i.bz2" ]; then i="$i.bz2" ; prog="bzip2 -cdfq"
+ elif [ -f "$i.lz" ]; then i="$i.lz" ; prog="lzip -cdfq"
+ elif [ -f "$i.xz" ]; then i="$i.xz" ; prog="xz -cdfq"
+ else
+ echo "$0: File \"$i\" not found or not a regular file" 1>&2
+ if [ ${retval} = 0 ]; then retval=1 ; fi
+ continue
+ fi
+ if [ ${list} = 1 ]; then
+ ${prog} -- "$i" | grep ${args} 2>&1 > /dev/null && echo "$i"
+ r=$?
+ elif [ $# = 1 ] || [ ${no_name} = 1 ]; then
+ ${prog} -- "$i" | grep ${args}
+ r=$?
+ else
+ j=`printf "%s" "$i" | sed 's/\\\\/\\\\\\\\/g'`
+ j=`printf "%s" "$j" | sed 's/|/\\\\|/g'`
+ j=`printf "%s" "$j" | sed 's/&/\\\\&/g'`
+ j=`printf "%s" "$j" | tr '\n' ' '`
+ ${prog} -- "$i" | grep ${args} | sed "s|^|${j}:|"
+ r=$?
+ fi
+ [ $r != 0 ] && retval="$r"
fi
- test "$r" -ne 0 && retval="$r"
done
exit ${retval}