zstdgrep 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2003 Thomas Klausner.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. grep=${GREP:-grep}
  25. zcat=${ZCAT:-zstdcat}
  26. endofopts=0
  27. pattern_found=0
  28. grep_args=""
  29. hyphen=0
  30. silent=0
  31. prog=${0##*/}
  32. # handle being called 'zegrep' or 'zfgrep'
  33. case $prog in
  34. *egrep*) prog=zegrep; grep_args='-E';;
  35. *fgrep*) prog=zfgrep; grep_args='-F';;
  36. *) prog=zstdgrep;;
  37. esac
  38. # skip all options and pass them on to grep taking care of options
  39. # with arguments, and if -e was supplied
  40. while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
  41. case "$1" in
  42. # from GNU grep-2.5.1 -- keep in sync!
  43. -[ABCDXdefm])
  44. if [ "$#" -lt 2 ]; then
  45. printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2
  46. exit 1
  47. fi
  48. case "$1" in
  49. -e)
  50. pattern="$2"
  51. pattern_found=1
  52. shift 2
  53. break
  54. ;;
  55. -f)
  56. pattern_found=2
  57. ;;
  58. *)
  59. ;;
  60. esac
  61. grep_args="${grep_args} $1 $2"
  62. shift 2
  63. ;;
  64. --)
  65. shift
  66. endofopts=1
  67. ;;
  68. -)
  69. hyphen=1
  70. shift
  71. ;;
  72. -h)
  73. silent=1
  74. shift
  75. ;;
  76. -*)
  77. grep_args="${grep_args} $1"
  78. shift
  79. ;;
  80. *)
  81. # pattern to grep for
  82. endofopts=1
  83. ;;
  84. esac
  85. done
  86. # if no -e option was found, take next argument as grep-pattern
  87. if [ "${pattern_found}" -lt 1 ]; then
  88. if [ "$#" -ge 1 ]; then
  89. pattern="$1"
  90. shift
  91. elif [ "${hyphen}" -gt 0 ]; then
  92. pattern="-"
  93. else
  94. printf '%s: missing pattern\n' "${prog}" >&2
  95. exit 1
  96. fi
  97. fi
  98. EXIT_CODE=0
  99. # call grep ...
  100. if [ "$#" -lt 1 ]; then
  101. # ... on stdin
  102. set -f # Disable file name generation (globbing).
  103. # shellcheck disable=SC2086
  104. "${zcat}" - | "${grep}" ${grep_args} -- "${pattern}" -
  105. EXIT_CODE=$?
  106. set +f
  107. else
  108. # ... on all files given on the command line
  109. if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
  110. grep_args="-H ${grep_args}"
  111. fi
  112. set -f
  113. while [ "$#" -gt 0 ]; do
  114. # shellcheck disable=SC2086
  115. if [ $pattern_found -eq 2 ]; then
  116. "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- -
  117. else
  118. "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
  119. fi
  120. [ "$?" -ne 0 ] && EXIT_CODE=1
  121. shift
  122. done
  123. set +f
  124. fi
  125. exit "${EXIT_CODE}"