Debian Codegraph @

terps/scare/sxglob.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * The glob matching functions in this module are derived from an original
24 * (and somewhat hairy) glob.c posted by Arjan Kenter from the University
25 * of Twente, NL, in an assortment of minor variations between 1993 and 1997.
26 * The major modifications are:
27 *
28 * o Added checks to ensure that invalid range patterns such as "[a-" or
29 * "[-" don't cause the loops to walk off the end of the pattern string
30 * and (usually) result in SIGSEGV.
31 * o Moved from plain char to unsigned char to avoid signedness problems
32 * with range comparisons.
33 * o Skipped the leading '[' in the range checker; the original was treating
34 * it as a possible first value of 'r'.
35 * o Moved the range checker while() from the bottom of the loop to the top,
36 * to avoid problems with invalid ranges.
37 * o Gave 'l' in the range checker an initial value that ensures that it
38 * can never match until it's been re-assigned to 'r'.
39 * o Used a return value rather than multiple returns in the matcher, for
40 * better debugability.
41 * o Applied some const-correctness, and replaced some pointers by indexing.
42 * o Added scanf-like special cases, making ']' a valid part of a range if
43 * first, and '-' if last.
44 *
45 * This glob accepts * and ? wild cards, and [] ranges. It does not check
46 * whether the range string is valid (for example, terminates with ']'), but
47 * simply returns the best it can under those circumstances.
48 *
49 * Example call:
50 * glob_match ("a*b?c[A-Za-z_0-9]d*", some_string)
51 */
52
53 #include <assert.h>
54 #include <stdlib.h>
55
56 #include "scare.h"
57 #include "sxprotos.h"
58
59
60 /*
61 * glob_inrange_unsigned()
62 * glob_match_unsigned()
63 *
64 * Match a "[...]" character range, and match general glob wildcards. See
65 * above for notes on where these functions came from originally.
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 /* Skip the leading '[' on entry to a range check. */
76 index_ = 1;
77
78 /* Special-case a range that has ']' as its first character. */
79 if (pattern_[index_] == ']')
80 {
81 r = pattern_[index_++];
82 if (ch == r)
83 in_range = TRUE;
84 }
85
86 /*
87 * Check at the loop top, rather than the bottom, to avoid problems with
88 * invalid or uncompleted ranges.
89 */
90 while (pattern_[index_] && pattern_[index_] != ']')
91 {
92 r = pattern_[index_++];
93 if (r == '-')
94 {
95 /* Special-case a range that has '-' as its last character. */
96 if (pattern_[index_] == ']' || !pattern_[index_])
97 {
98 if (ch == r)
99 in_range = TRUE;
100 break;
101 }
102
103 /* Break the loop on unterminated range ending with '-'. */
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 /* Update pattern with characters consumed, return result. */
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 * After a range check, we need to see if we hit the end of the
156 * pattern before recursively matching pattern + 1.
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 /* Structures and data for the self test function. */
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 * glob_self_test()
237 *
238 * Sed quis custodiet ipsos custodes?
239 */
240 static void
241 glob_self_test (void)
242 {
243 const sx_test_data_t *test;
244 sc_int errors;
245
246 /*
247 * Run each test case and compare against expected result. To avoid a lot
248 * of ugly casting, we use the main public glob_match() function.
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 * Abort if any error. As befits our distrustful nature, we won't even
275 * trust that sx_fatal() calls abort() (though it should).
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 * glob_match()
288 *
289 * Adapter for the above globbing functions, presenting a more standard char-
290 * based interface. Here is where all the evil casting lives.
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 /* On the first call, run a self-test to verify basic glob matching. */
303 if (!initialized)
304 {
305 /*
306 * To avoid lots of icky casting, the self-test uses the core public
307 * glob_match() that we're in right here to run its tests. So set
308 * initialized _before_ the test, to avoid infinite recursion.
309 */
310 initialized = TRUE;
311 glob_self_test ();
312 }
313
314 retval = glob_match_unsigned (pattern_, string_) != 0;
315 return retval;
316 }