-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbashful-match.inc.sh
228 lines (200 loc) · 6.09 KB
/
bashful-match.inc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# Bashful is copyright 2009-2016 Dejay Clayton, all rights reserved:
# https://github.com/dejayc/bashful
# Bashful is licensed under the 2-Clause BSD License:
# http://opensource.org/licenses/BSD-2-Clause
# Declare the module name and dependencies.
declare BASHFUL_MODULE='match'
declare BASHFUL_MODULE_DEPENDENCIES='list text'
# Verify execution context and module dependencies, and register the module.
{
declare BASHFUL_MODULE_VAR="BASHFUL_LOADED_${BASHFUL_MODULE}"
[[ -z "${!BASHFUL_MODULE_VAR-}" ]] || return 0
# Ensure the module is sourced, not executed, generating an error
# otherwise.
[[ "${BASH_ARGV}" != '' ]] || {
echo "ERROR: ${BASH_SOURCE[0]##*/} must be sourced, not executed"
exit 1
} >&2
# Register the module and dependencies.
declare "${BASHFUL_MODULE_VAR}"="${BASHFUL_MODULE}"
declare "BASHFUL_DEPS_${BASHFUL_MODULE}"="${BASHFUL_MODULE_DEPENDENCIES}"
}
# function ifWildcardMatches
#
# Returns status 0 if the second passed parameter, when interpreted as a
# wildcard, matches the first passed parameter. Otherwise, returns 1.
# Wildcard characters asterisk '*' and question mark '?' are treated as
# special wildcard characters, with '*' matching any sequence of characters,
# and '?' matching any single character. All other characters are treated as
# literal characters.
#
# Examples:
#
# $ ifWildcardMatches 'tortoise' 'tortoise'; echo ${?}
# 0
#
# $ ifWildcardMatches 'tortoise' 'porpoise'; echo ${?}
# 1
#
# $ ifWildcardMatches 'tortoise' '?or?oise'; echo ${?}
# 0
#
# $ ifWildcardMatches 'tortoise' '*oise'; echo ${?}
# 0
#
# $ ifWildcardMatches 'tortoise' 'tort'; echo ${?}
# 1
function ifWildcardMatches()
{
local VALUE="${1-}"
local PATTERN="${2-}"
[[ "${VALUE}" == "${PATTERN}" ]] && return 0
PATTERN="$( escapedExtendedRegex "${PATTERN}" )" || return
PATTERN="${PATTERN//\\[*]/.*}"
PATTERN="${PATTERN//\\[?]/.}"
[[ "${VALUE}" =~ ^${PATTERN}$ ]]
}
# function valueForMatchedName
#
# Accepts a name as the first argument, and any number of name/value pairs as
# subsequent arguments, and returns the value of the first name/value pair
# that matches the specified name. If no match is found, returns the status
# code 1.
#
# -l optionally returns the value of the last name/value pair that matches the
# specified name. By default, the value of the first name/value pair that
# matches the specified name is returned.
#
# -d optionally specifies one or more value delimiter characters. The first
# occurrence of an input delimiter within a name/value pair will be used to
# split the name and value. All subsequent occurrences will be considered
# part of the value. Defaults to '='. An error is returned if null.
#
# -t optionally trims leading and trailing whitespace from the name, and each
# name and value in name/value pairs.
#
# -v optionally treats arguments without an input delimiter as a value with a
# null name. By default, such entries are treated as a name with a null
# value.
#
# -w optionally performs wildcard matching, interpreting the name within each
# name/value pair as a wildcard against which to compare the name argument.
#
# Examples:
#
# $ valueForMatchedName '3' '1=a' '2=b' '3=c'
# c
#
# $ valueForMatchedName -w 'book' 'b*=1' 'bo*=2' 'b?o*=3'
# 1
#
# $ valueForMatchedName -w 'book' 'b* = 1' 'bo*=2'
# 2
#
# $ valueForMatchedName -t -w ' book ' 'b* = 1' 'bo*=2'
# 1
#
# $ valueForMatchedName -w -l 'book' 'b*=1' 'bo*=2' 'b?o*=3'
# 3
#
# $ valueForMatchedName '' 'empty' '=value'
# value
#
# $ valueForMatchedName -v '' 'empty' '=value'
# empty
function valueForMatchedName()
{
local DELIM='='
declare -i REPORT_LAST_MATCH=0
declare -i SINGLE_IS_VALUE=0
declare -i TRIM_TEXT=0
declare -i WILDCARD_MATCHES=0
# Parse function options.
declare -i OPTIND
local OPT=''
while getopts ":d:lvtw" OPT
do
case "${OPT}" in
d)
DELIM="${OPTARG}"
[[ -z "${DELIM}" ]] && return 1
;;
l)
let REPORT_LAST_MATCH=1
;;
t)
let TRIM_TEXT=1
;;
v)
let SINGLE_IS_VALUE=1
;;
w)
let WILDCARD_MATCHES=1
;;
*)
return 2
esac
done
shift $(( OPTIND - 1 ))
# Done parsing function options.
local NAME="${1-}"
shift ||:
[[ ${TRIM_TEXT} -eq 0 ]] || {
NAME="${NAME#"${NAME%%[![:space:]]*}"}"
NAME="${NAME%"${NAME##*[![:space:]]}"}"
}
declare -i FOUND_MATCH=0
local MATCH=''
while [ $# -gt 0 ]
do
local PAIR_STR="${1}"
shift
local PAIR_LIST
PAIR_LIST="$( splitList -d "${DELIM}" "${PAIR_STR}" )" || return
unset PAIR
declare -a PAIR=() # Compatibility fix.
declare -a PAIR="( ${PAIR_LIST} )" || return
declare -i PAIR_LEN=${#PAIR[@]-}
local PATTERN=''
local VALUE=''
case ${PAIR_LEN} in
1)
if [ ${SINGLE_IS_VALUE} -ne 0 ]
then
VALUE="${PAIR[0]}"
else
PATTERN="${PAIR[0]}"
fi
;;
2)
PATTERN="${PAIR[0]}"
VALUE="${PAIR[1]}"
;;
*)
PATTERN="${PAIR[0]}"
VALUE="${PAIR_STR:$(( ${#PATTERN} + 1 ))}"
;;
esac
[[ ${TRIM_TEXT} -eq 0 ]] || {
PATTERN="${PATTERN#"${PATTERN%%[![:space:]]*}"}"
PATTERN="${PATTERN%"${PATTERN##*[![:space:]]}"}"
VALUE="${VALUE#"${VALUE%%[![:space:]]*}"}"
VALUE="${VALUE%"${VALUE##*[![:space:]]}"}"
}
declare -i IS_MATCHING=0
if [ ${WILDCARD_MATCHES} -ne 0 ]
then
ifWildcardMatches "${NAME}" "${PATTERN}" && let IS_MATCHING=1
else
[[ "${NAME}" == "${PATTERN}" ]] && let IS_MATCHING=1
fi
[[ ${IS_MATCHING} -eq 0 ]] || {
let FOUND_MATCH=1
MATCH="${VALUE}"
[[ ${REPORT_LAST_MATCH} -eq 0 ]] && break
}
done
echo -n "${MATCH}"
[[ ${FOUND_MATCH} -ne 0 ]]
}