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