| 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 |
#include <assert.h> |
| 54 |
#include <stdlib.h> |
| 55 |
|
| 56 |
#include "scare.h" |
| 57 |
#include "sxprotos.h" |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static int |
| 68 |
glob_inrange_unsigned (const unsigned char **const pattern, |
| 69 |
unsigned char ch) |
| 70 |
{ |
| 71 |
const unsigned char *const pattern_ = *pattern; |
| 72 |
int in_range = FALSE; |
| 73 |
unsigned int l = 256, r = 0, index_; |
| 74 |
|
| 75 |
|
| 76 |
index_ = 1; |
| 77 |
|
| 78 |
|
| 79 |
if (pattern_[index_] == ']') |
| 80 |
{ |
| 81 |
r = pattern_[index_++]; |
| 82 |
if (ch == r) |
| 83 |
in_range = TRUE; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
while (pattern_[index_] && pattern_[index_] != ']') |
| 91 |
{ |
| 92 |
r = pattern_[index_++]; |
| 93 |
if (r == '-') |
| 94 |
{ |
| 95 |
|
| 96 |
if (pattern_[index_] == ']' || !pattern_[index_]) |
| 97 |
{ |
| 98 |
if (ch == r) |
| 99 |
in_range = TRUE; |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if (!pattern_[index_]) |
| 105 |
break; |
| 106 |
|
| 107 |
r = pattern_[index_++]; |
| 108 |
if (l <= ch && ch <= r) |
| 109 |
in_range = TRUE; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
l = r; |
| 114 |
if (ch == r) |
| 115 |
in_range = TRUE; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
*pattern += index_; |
| 121 |
return in_range; |
| 122 |
} |
| 123 |
|
| 124 |
static int |
| 125 |
glob_match_unsigned (const unsigned char *pattern, |
| 126 |
const unsigned char *string) |
| 127 |
{ |
| 128 |
int is_match = FALSE; |
| 129 |
|
| 130 |
if (!*string) |
| 131 |
{ |
| 132 |
if (*pattern == '*') |
| 133 |
is_match = glob_match_unsigned (pattern + 1, string); |
| 134 |
else |
| 135 |
is_match = !*pattern; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
switch (*pattern) |
| 140 |
{ |
| 141 |
case '\0': |
| 142 |
is_match = !*string; |
| 143 |
break; |
| 144 |
case '*': |
| 145 |
if (glob_match_unsigned (pattern + 1, string)) |
| 146 |
is_match = TRUE; |
| 147 |
else |
| 148 |
is_match = glob_match_unsigned (pattern, string + 1); |
| 149 |
break; |
| 150 |
case '?': |
| 151 |
is_match = glob_match_unsigned (pattern + 1, string + 1); |
| 152 |
break; |
| 153 |
case '[': |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
is_match = glob_inrange_unsigned (&pattern, *string) |
| 159 |
&& (!*pattern |
| 160 |
|| glob_match_unsigned (pattern + 1, string + 1)); |
| 161 |
break; |
| 162 |
default: |
| 163 |
is_match = *pattern == *string |
| 164 |
&& glob_match_unsigned (pattern + 1, string + 1); |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return is_match; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
typedef struct |
| 175 |
{ |
| 176 |
const sc_char *const pattern; |
| 177 |
const sc_char *const string; |
| 178 |
} sx_test_data_t; |
| 179 |
|
| 180 |
static const sx_test_data_t SHOULD_MATCH[] = { |
| 181 |
{"a", "a"}, {"abc", "abc"}, {"", ""}, |
| 182 |
{"*", ""}, {"*", "abc"}, {"*", "cba"}, |
| 183 |
{"*c", "c"}, {"*c", "abc"}, {"*c", "cbac"}, |
| 184 |
{"a*", "a"}, {"a*", "abc"}, {"a*", "abca"}, |
| 185 |
{"a*c", "ac"}, {"a*c", "abc"}, {"a*c", "abcbcbc"}, |
| 186 |
{"a**c", "ac"}, {"a**c", "abc"}, {"a**c", "abcbcbc"}, |
| 187 |
{"*b*", "b"}, {"*b*", "abc"}, {"*b*", "ab"}, {"*b*", "bc"}, |
| 188 |
{"?", "a"}, {"?", "z"}, {"?", "?"}, {"[?]", "?"}, |
| 189 |
{"a?", "aa"}, {"a?", "az"}, {"a?", "a?"}, |
| 190 |
{"?c", "ac"}, {"?c", "zc"}, {"?c", "?c"}, |
| 191 |
{"[abz]", "a"}, {"[abz]", "b"}, {"[abz]", "z"}, |
| 192 |
{"[a-c]", "a"}, {"[a-c]", "b"}, {"[a-c]", "c"}, |
| 193 |
{"[ac]b[ac]", "abc"}, {"[ac]b[ac]", "cba"}, |
| 194 |
|
| 195 |
{"[]]", "]"}, {"[]a-c]", "a"}, {"[]a-c]", "b"}, {"[]a-c]", "c"}, |
| 196 |
{"[?]", "?" }, {"[-]", "-"}, {"[z-]", "z"}, {"[z-]", "-"}, |
| 197 |
{"[][-]", "]"}, {"[][-]", "["}, {"[][-]", "-"}, |
| 198 |
{"[a-c-]", "a"}, {"[a-c-]", "b"}, {"[a-c-]", "c"}, {"[a-c-]", "-"}, |
| 199 |
|
| 200 |
{"*[a-z]*abc?xyz", "a<star>abcQxyz"}, {"*[a-z]*abc?xyz", "<star>aabcQxyz"}, |
| 201 |
{"*[a-z]*abc?xyz", "aabcQxyz"}, {"*[a-z]*abc?xyz", "<star>a<star>abcQxyz"}, |
| 202 |
|
| 203 |
{"???]", "abc]"}, {"[z-a]", "z"}, |
| 204 |
{"[a-z", "a"}, {"[a-", "a"}, {"[a", "a"}, {"[[", "["}, |
| 205 |
{NULL, NULL} |
| 206 |
}; |
| 207 |
|
| 208 |
static const sx_test_data_t SHOULD_NOT_MATCH[] = { |
| 209 |
{"a", "b"}, {"abc", "abd"}, {"a", ""}, {"", "a"}, |
| 210 |
{"*c", "a"}, {"*c", "ab"}, {"*c", "abca"}, |
| 211 |
{"a*", "c"}, {"a*", "cba"}, {"a*", "cbac"}, |
| 212 |
{"a*c", "ca"}, {"a*c", "cba"}, {"a*c", "cbababa"}, |
| 213 |
{"a**c", "ca"}, {"a**c", "cba"}, {"a**c", "cbababa"}, |
| 214 |
{"*b*", ""}, {"*b*", "z"}, {"*b*", "ac"}, {"*b*", "azc"}, |
| 215 |
{"?", ""}, {"?", "ab"}, {"?", "abc"}, {"[?]", "a"}, |
| 216 |
{"a?", "ca"}, {"a?", "cz"}, {"a?", "??"}, |
| 217 |
{"?c", "ab"}, {"?c", "zb"}, {"?c", "??"}, |
| 218 |
{"[bcy]", "a"}, {"[bcy]", "d"}, {"[bcy]", "z"}, |
| 219 |
{"[b-d]", "a"}, {"[b-d]", "e"}, {"[b-d]", ""}, {"[b-d]", "bc"}, |
| 220 |
{"[ac]b[ac]", "aaa"}, {"[ac]b[ac]", "bbb"}, {"[ac]b[ac]", "ccc"}, |
| 221 |
|
| 222 |
{"[]]", "["}, {"[]]", "a"}, {"[]a-c]", "z"}, |
| 223 |
{"[?]", "a" }, {"[-]", "a"}, {"[z-]", "a"}, |
| 224 |
{"[][-]", "a"}, {"[][-]", "z"}, |
| 225 |
{"[a-c-]", "z"}, |
| 226 |
|
| 227 |
{"*[a-z]*abc?xyz", "A<STAR>abcQxyz"}, {"*[a-z]*abc?xyz", "<STAR>AabcQxyz"}, |
| 228 |
{"*[a-z]*abc?xyz", "AabcQxyz"}, {"*[a-z]*abc?xyz", "aabcxyz"}, |
| 229 |
|
| 230 |
{"[z-a]", "a"}, {"[z-a]", "b"}, {"[", "a"}, {"[[", "a"}, |
| 231 |
{NULL, NULL} |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
static void |
| 241 |
glob_self_test (void) |
| 242 |
{ |
| 243 |
const sx_test_data_t *test; |
| 244 |
sc_int errors; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
errors = 0; |
| 251 |
for (test = SHOULD_MATCH; test->pattern; test++) |
| 252 |
{ |
| 253 |
if (!glob_match (test->pattern, test->string)) |
| 254 |
{ |
| 255 |
sx_error ("glob_self_test: \"%s\", \"%s\"" |
| 256 |
" did not match, and should have matched\n", |
| 257 |
test->pattern, test->string); |
| 258 |
errors++; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
for (test = SHOULD_NOT_MATCH; test->pattern; test++) |
| 263 |
{ |
| 264 |
if (glob_match (test->pattern, test->string)) |
| 265 |
{ |
| 266 |
sx_error ("glob_self_test: \"%s\", \"%s\"" |
| 267 |
" matched, and should not have matched\n", |
| 268 |
test->pattern, test->string); |
| 269 |
errors++; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
if (errors > 0) |
| 278 |
{ |
| 279 |
sx_fatal ("glob_self_test: %ld self-test error%s found, aborting\n", |
| 280 |
errors, (errors == 1) ? "" : "s"); |
| 281 |
abort (); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
sc_bool |
| 293 |
glob_match (const sc_char *pattern, const sc_char *string) |
| 294 |
{ |
| 295 |
static sc_bool initialized = FALSE; |
| 296 |
|
| 297 |
const unsigned char *pattern_ = (const unsigned char *) pattern; |
| 298 |
const unsigned char *string_ = (const unsigned char *) string; |
| 299 |
sc_bool retval; |
| 300 |
assert (pattern && string); |
| 301 |
|
| 302 |
|
| 303 |
if (!initialized) |
| 304 |
{ |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
initialized = TRUE; |
| 311 |
glob_self_test (); |
| 312 |
} |
| 313 |
|
| 314 |
retval = glob_match_unsigned (pattern_, string_) != 0; |
| 315 |
return retval; |
| 316 |
} |