Merge pull request #1430 from lzutao/fix-zstdgrep

Fix  #1428 - zstdgrep now returns 1 on unmatch
dev
Yann Collet 2018-11-28 14:07:40 -08:00 committed by GitHub
commit f6591e4a04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 68 deletions

View File

@ -31,30 +31,26 @@ grep_args=""
hyphen=0
silent=0
prg=$(basename $0)
prg=$(basename "$0")
# handle being called 'zegrep' or 'zfgrep'
case ${prg} in
*zegrep)
grep_args="-E";;
*zfgrep)
grep_args="-F";;
case "${prg}" in
*zegrep) grep_args="-E";;
*zfgrep) grep_args="-F";;
esac
# skip all options and pass them on to grep taking care of options
# with arguments, and if -e was supplied
while [ $# -gt 0 -a ${endofopts} -eq 0 ]
do
case $1 in
while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
case "$1" in
# from GNU grep-2.5.1 -- keep in sync!
-[ABCDXdefm])
if [ $# -lt 2 ]
then
echo "${prg}: missing argument for $1 flag" >&2
if [ "$#" -lt 2 ]; then
printf '%s: missing argument for %s flag\n' "${prg}" "$1" >&2
exit 1
fi
case $1 in
case "$1" in
-e)
pattern="$2"
pattern_found=1
@ -91,34 +87,45 @@ do
done
# if no -e option was found, take next argument as grep-pattern
if [ ${pattern_found} -lt 1 ]
then
if [ $# -ge 1 ]; then
if [ "${pattern_found}" -lt 1 ]; then
if [ "$#" -ge 1 ]; then
pattern="$1"
shift
elif [ ${hyphen} -gt 0 ]; then
elif [ "${hyphen}" -gt 0 ]; then
pattern="-"
else
echo "${prg}: missing pattern" >&2
printf '%s: missing pattern\n' "${prg}" >&2
exit 1
fi
fi
EXIT_CODE=0
# call grep ...
if [ $# -lt 1 ]
then
if [ "$#" -lt 1 ]; then
# ... on stdin
${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
set -f # Disable file name generation (globbing).
# shellcheck disable=SC2086
"${zcat}" -fq - | "${grep}" ${grep_args} -- "${pattern}" -
EXIT_CODE=$?
set +f
else
# ... on all files given on the command line
if [ ${silent} -lt 1 -a $# -gt 1 ]; then
if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
grep_args="-H ${grep_args}"
fi
while [ $# -gt 0 ]
do
${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
CUR_EXIT_CODE=0
EXIT_CODE=1
set -f
while [ "$#" -gt 0 ]; do
# shellcheck disable=SC2086
"${zcat}" -fq -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
CUR_EXIT_CODE=$?
if [ "${CUR_EXIT_CODE}" -eq 0 ] && [ "${EXIT_CODE}" -ne 1 ]; then
EXIT_CODE=0
fi
shift
done
set +f
fi
exit 0
exit "${EXIT_CODE}"

View File

@ -320,7 +320,7 @@ test32: test-zstd32 test-fullbench32 test-fuzzer32 test-zstream32
test-all: test test32 valgrindTest test-decodecorpus-cli
.PHONY: test-zstd test-zstd32 test-zstd-nolegacy
.PHONY: test-zstd test-zstd32 test-zstd-nolegacy test-zstdgrep
test-zstd: ZSTD = $(PRGDIR)/zstd
test-zstd: zstd
@ -352,6 +352,10 @@ test-gzstd: gzstd
$(PRGDIR)/zstd -dcf - <hello_zst_gz_txt.gz
$(RM) *.gz *.zst README2.md gz_zstd zstd_gz hello.txt
test-zstdgrep: gzstd
@echo a | $(PRGDIR)/zstd | $(PRGDIR)/zstdgrep a
@echo a | $(PRGDIR)/zstd | $(PRGDIR)/zstdgrep b && return 1 || return 0
test-fullbench: fullbench datagen
$(QEMU_SYS) ./fullbench -i1
$(QEMU_SYS) ./fullbench -i1 -P0