From c046e0b626491c613bb82fb81611d727bc24e0c9 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 29 Nov 2018 02:45:01 +0700 Subject: [PATCH 1/3] Fix #1428 - zstdgrep now returns 1 on unmatch --- programs/zstdgrep | 137 +++++++++++++++++++++++----------------------- 1 file changed, 70 insertions(+), 67 deletions(-) diff --git a/programs/zstdgrep b/programs/zstdgrep index 9f871c03f..7f050d04a 100755 --- a/programs/zstdgrep +++ b/programs/zstdgrep @@ -31,94 +31,97 @@ 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 - exit 1 - fi - case $1 in - -e) - pattern="$2" - pattern_found=1 - shift 2 - break - ;; - *) - ;; - esac - grep_args="${grep_args} $1 $2" - shift 2 - ;; - --) - shift - endofopts=1 - ;; - -) - hyphen=1 - shift - ;; - -h) - silent=1 - shift - ;; - -*) - grep_args="${grep_args} $1" - shift - ;; - *) - # pattern to grep for - endofopts=1 - ;; + -[ABCDXdefm]) + if [ "$#" -lt 2 ]; then + printf '%s: missing argument for %s flag\n' "${prg}" "$1" >&2 + exit 1 + fi + case "$1" in + -e) + pattern="$2" + pattern_found=1 + shift 2 + break + ;; + *) + ;; + esac + grep_args="${grep_args} $1 $2" + shift 2 + ;; + --) + shift + endofopts=1 + ;; + -) + hyphen=1 + shift + ;; + -h) + silent=1 + shift + ;; + -*) + grep_args="${grep_args} $1" + shift + ;; + *) + # pattern to grep for + endofopts=1 + ;; esac done # if no -e option was found, take next argument as grep-pattern -if [ ${pattern_found} -lt 1 ] -then - if [ $# -ge 1 ]; then - pattern="$1" - shift - elif [ ${hyphen} -gt 0 ]; then - pattern="-" +if [ "${pattern_found}" -lt 1 ]; then + if [ "$#" -ge 1 ]; then + pattern="$1" + shift + elif [ "${hyphen}" -gt 0 ]; then + pattern="-" else - echo "${prg}: missing pattern" >&2 - exit 1 + 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}" - + # shellcheck disable=SC2086 + "${zcat}" -fq - | "${grep}" ${grep_args} -- "${pattern}" - + EXIT_CODE=$? else # ... on all files given on the command line - if [ ${silent} -lt 1 -a $# -gt 1 ]; then - grep_args="-H ${grep_args}" + 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}" - - shift + CUR_EXIT_CODE=0 + EXIT_CODE=1 + 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 fi -exit 0 +exit "${EXIT_CODE}" From 3d18b4764dd3c839361c70aded843d9984410011 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 29 Nov 2018 03:04:40 +0700 Subject: [PATCH 2/3] Prevent globbing on non-quoting variable --- programs/zstdgrep | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/programs/zstdgrep b/programs/zstdgrep index 7f050d04a..a10e0710a 100755 --- a/programs/zstdgrep +++ b/programs/zstdgrep @@ -103,9 +103,11 @@ EXIT_CODE=0 # call grep ... if [ "$#" -lt 1 ]; then # ... on stdin + 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 ] && [ "$#" -gt 1 ]; then @@ -113,6 +115,7 @@ else fi 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}" - @@ -122,6 +125,7 @@ else fi shift done + set +f fi exit "${EXIT_CODE}" From d095adf9fb7fcadfaa770e2c75d14dde0950f6d0 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 29 Nov 2018 03:39:47 +0700 Subject: [PATCH 3/3] Add simple test for zstdgrep --- tests/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index f363001b1..25bd5c84e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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 -