Debian Codegraph @

bz3grep

raw
1 #!/usr/bin/env 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 #
25 # Adapted for bz3cat.
26
27 grep=${GREP:-grep}
28 zcat=${ZCAT:-bz3cat}
29
30 endofopts=0
31 pattern_found=0
32 grep_args=""
33 hyphen=0
34 silent=0
35
36 prog=bz3grep
37
38 # skip all options and pass them on to grep taking care of options
39 # with arguments, and if -e was supplied
40
41 while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
42 case "$1" in
43 # from GNU grep-2.5.1 -- keep in sync!
44 -[ABCDXdefm])
45 if [ "$#" -lt 2 ]; then
46 printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2
47 exit 1
48 fi
49 case "$1" in
50 -e)
51 pattern="$2"
52 pattern_found=1
53 shift 2
54 break
55 ;;
56 -f)
57 pattern_found=2
58 ;;
59 *)
60 ;;
61 esac
62 grep_args="${grep_args} $1 $2"
63 shift 2
64 ;;
65 --)
66 shift
67 endofopts=1
68 ;;
69 -)
70 hyphen=1
71 shift
72 ;;
73 -h)
74 silent=1
75 shift
76 ;;
77 -*)
78 grep_args="${grep_args} $1"
79 shift
80 ;;
81 *)
82 # pattern to grep for
83 endofopts=1
84 ;;
85 esac
86 done
87
88 # if no -e option was found, take next argument as grep-pattern
89 if [ "${pattern_found}" -lt 1 ]; then
90 if [ "$#" -ge 1 ]; then
91 pattern="$1"
92 shift
93 elif [ "${hyphen}" -gt 0 ]; then
94 pattern="-"
95 else
96 printf '%s: missing pattern\n' "${prog}" >&2
97 exit 1
98 fi
99 fi
100
101 EXIT_CODE=0
102 # call grep ...
103 if [ "$#" -lt 1 ]; then
104 # ... on stdin
105 set -f # Disable file name generation (globbing).
106 # shellcheck disable=SC2086
107 "${zcat}" - | "${grep}" ${grep_args} -- "${pattern}" -
108 EXIT_CODE=$?
109 set +f
110 else
111 # ... on all files given on the command line
112 if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
113 grep_args="-H ${grep_args}"
114 fi
115 set -f
116 while [ "$#" -gt 0 ]; do
117 # shellcheck disable=SC2086
118 if [ $pattern_found -eq 2 ]; then
119 "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- -
120 else
121 "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
122 fi
123 [ "$?" -ne 0 ] && EXIT_CODE=1
124 shift
125 done
126 set +f
127 fi
128
129 exit "${EXIT_CODE}"