-
Notifications
You must be signed in to change notification settings - Fork 0
/
globtree.c
342 lines (307 loc) · 8.48 KB
/
globtree.c
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*-
* Copyright (c) 2006, Maxime Henrion <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <assert.h>
#include <regex.h>
#include <stdlib.h>
#include "fnmatch.h"
#include "globtree.h"
#include "misc.h"
#include "queue.h"
/*
* The "GlobTree" interface allows one to construct arbitrarily complex
* boolean expressions for evaluating whether to accept or reject a
* filename. The globtree_test() function returns true or false
* according to whether the name is accepted or rejected by the
* expression.
*
* Expressions are trees constructed from nodes representing either
* primitive matching operations (primaries) or operators that are
* applied to their subexpressions. The simplest primitives are
* globtree_false(), which matches nothing, and globtree_true(), which
* matches everything.
*
* A more useful primitive is the matching operation, constructed with
* globtree_match(). It will call fnmatch() with the supplied
* shell-style pattern to determine if the filename matches.
*
* Expressions can be combined with the boolean operators AND, OR, and
* NOT, to form more complex expressions.
*/
/* Node types. */
#define GLOBTREE_NOT 0
#define GLOBTREE_AND 1
#define GLOBTREE_OR 2
#define GLOBTREE_MATCH 3
#define GLOBTREE_REGEX 4
#define GLOBTREE_TRUE 5
#define GLOBTREE_FALSE 6
typedef enum {
STATE_DOINGLEFT,
STATE_DOINGRIGHT
} walkstate_t;
/* A node. */
struct globtree {
int type;
struct globtree *left;
struct globtree *right;
/* The "data" field points to the text pattern for GLOBTREE_MATCH
nodes, and to the regex_t for GLOBTREE_REGEX nodes. For any
other node, it is set to NULL. */
void *data;
/* The "flags" field contains the flags to pass to fnmatch() for
GLOBTREE_MATCH nodes. */
int flags;
/* These two fields are used in globtree_test() where we need to use a
stack to avoid recursion when evaluating the tree. Having those two
fields directly in the globtree structure allows us to do this
without having to allocate any extra memory. However, this only
works because there are no calls to globtree_test() on the same
globtree from two different threads; otherwise we'd need locking. */
walkstate_t state;
SLIST_ENTRY(globtree) stack_next;
};
static struct globtree *globtree_new(int);
static int globtree_eval(struct globtree *, const char *);
static struct globtree *
globtree_new(int type)
{
struct globtree *gt;
gt = xmalloc(sizeof(struct globtree));
gt->type = type;
gt->data = NULL;
gt->flags = 0;
gt->left = NULL;
gt->right = NULL;
gt->state = -1;
return (gt);
}
struct globtree *
globtree_true(void)
{
struct globtree *gt;
gt = globtree_new(GLOBTREE_TRUE);
return (gt);
}
struct globtree *
globtree_false(void)
{
struct globtree *gt;
gt = globtree_new(GLOBTREE_FALSE);
return (gt);
}
struct globtree *
globtree_match(const char *pattern, int flags)
{
struct globtree *gt;
gt = globtree_new(GLOBTREE_MATCH);
gt->data = xstrdup(pattern);
gt->flags = flags;
return (gt);
}
struct globtree *
globtree_regex(const char *pattern)
{
struct globtree *gt;
int error;
gt = globtree_new(GLOBTREE_REGEX);
gt->data = xmalloc(sizeof(regex_t));
error = regcomp(gt->data, pattern, REG_NOSUB);
assert(!error);
return (gt);
}
struct globtree *
globtree_and(struct globtree *left, struct globtree *right)
{
struct globtree *gt;
if (left->type == GLOBTREE_FALSE || right->type == GLOBTREE_FALSE) {
globtree_free(left);
globtree_free(right);
gt = globtree_false();
return (gt);
}
if (left->type == GLOBTREE_TRUE) {
globtree_free(left);
return (right);
}
if (right->type == GLOBTREE_TRUE) {
globtree_free(right);
return (left);
}
gt = globtree_new(GLOBTREE_AND);
gt->left = left;
gt->right = right;
return (gt);
}
struct globtree *
globtree_or(struct globtree *left, struct globtree *right)
{
struct globtree *gt;
if (left->type == GLOBTREE_TRUE || right->type == GLOBTREE_TRUE) {
globtree_free(left);
globtree_free(right);
gt = globtree_true();
return (gt);
}
if (left->type == GLOBTREE_FALSE) {
globtree_free(left);
return (right);
}
if (right->type == GLOBTREE_FALSE) {
globtree_free(right);
return (left);
}
gt = globtree_new(GLOBTREE_OR);
gt->left = left;
gt->right = right;
return (gt);
}
struct globtree *
globtree_not(struct globtree *child)
{
struct globtree *gt;
if (child->type == GLOBTREE_TRUE) {
globtree_free(child);
gt = globtree_new(GLOBTREE_FALSE);
return (gt);
}
if (child->type == GLOBTREE_FALSE) {
globtree_free(child);
gt = globtree_new(GLOBTREE_TRUE);
return (gt);
}
gt = globtree_new(GLOBTREE_NOT);
gt->left = child;
return (gt);
}
/* Evaluate one node (must be a leaf node). */
static int
globtree_eval(struct globtree *gt, const char *path)
{
int rv;
switch (gt->type) {
case GLOBTREE_TRUE:
return (1);
case GLOBTREE_FALSE:
return (0);
case GLOBTREE_MATCH:
assert(gt->data != NULL);
rv = fnmatch(gt->data, path, gt->flags);
if (rv == 0)
return (1);
assert(rv == FNM_NOMATCH);
return (0);
case GLOBTREE_REGEX:
assert(gt->data != NULL);
rv = regexec(gt->data, path, 0, NULL, 0);
if (rv == 0)
return (1);
assert(rv == REG_NOMATCH);
return (0);
}
assert(0);
return (-1);
}
/* Tests if the supplied filename matches. */
int
globtree_test(struct globtree *gt, const char *path)
{
SLIST_HEAD(, globtree) stack;
int val;
SLIST_INIT(&stack);
for (;;) {
doleft:
/* Descend to the left until we hit bottom. */
while (gt->left != NULL) {
gt->state = STATE_DOINGLEFT;
SLIST_INSERT_HEAD(&stack, gt, stack_next);
gt = gt->left;
}
/* Now we're at a leaf node. Evaluate it. */
val = globtree_eval(gt, path);
/* Ascend, propagating the value through operator nodes. */
while (!SLIST_EMPTY(&stack)) {
gt = SLIST_FIRST(&stack);
SLIST_REMOVE_HEAD(&stack, stack_next);
switch (gt->type) {
case GLOBTREE_NOT:
val = !val;
break;
case GLOBTREE_AND:
/* If we haven't yet evaluated the right subtree
and the partial result is true, descend to
the right. Otherwise the result is already
determined to be val. */
if (gt->state == STATE_DOINGLEFT && val) {
gt->state = STATE_DOINGRIGHT;
SLIST_INSERT_HEAD(&stack, gt,
stack_next);
gt = gt->right;
goto doleft;
}
break;
case GLOBTREE_OR:
/* If we haven't yet evaluated the right subtree
and the partial result is false, descend to
the right. Otherwise the result is already
determined to be val. */
if (gt->state == STATE_DOINGLEFT && !val) {
gt->state = STATE_DOINGRIGHT;
SLIST_INSERT_HEAD(&stack, gt,
stack_next);
gt = gt->right;
goto doleft;
}
break;
default:
/* We only push nodes that have children. */
assert(0);
return (-1);
}
}
return (val);
}
}
/*
* We could de-recursify this function using a stack, but it would be
* overkill since it is never called from a thread context with a
* limited stack size nor used in a critical path, so I think we can
* afford keeping it recursive.
*/
void
globtree_free(struct globtree *gt)
{
if (gt->data != NULL) {
if (gt->type == GLOBTREE_REGEX)
regfree(gt->data);
free(gt->data);
}
if (gt->left != NULL)
globtree_free(gt->left);
if (gt->right != NULL)
globtree_free(gt->right);
free(gt);
}