6 static const char *OP_and = "&"; /* Logical AND */
7 static const char *OP_or = "|"; /* Logical OR */
8 static const char *OP_not = "!"; /* Logical NOT */
10 #define is_operator(c) ((c) == '|' || (c) == '&' || (c) == '!')
11 #define is_separator(c) (is_operator(c) || (c) == '(' || (c) == ')')
13 static void strfilter_node__delete(struct strfilter_node *node)
16 if (node->p && !is_operator(*node->p))
17 zfree((char **)&node->p);
18 strfilter_node__delete(node->l);
19 strfilter_node__delete(node->r);
24 void strfilter__delete(struct strfilter *filter)
27 strfilter_node__delete(filter->root);
32 static const char *get_token(const char *s, const char **e)
36 while (isspace(*s)) /* Skip spaces */
45 if (!is_separator(*s)) {
48 while (*p && !is_separator(*p) && !isspace(*p))
50 /* Escape and special case: '!' is also used in glob pattern */
51 if (*(p - 1) == '\\' || (*p == '!' && *(p - 1) == '[')) {
61 static struct strfilter_node *strfilter_node__alloc(const char *op,
62 struct strfilter_node *l,
63 struct strfilter_node *r)
65 struct strfilter_node *node = zalloc(sizeof(*node));
76 static struct strfilter_node *strfilter_node__new(const char *s,
79 struct strfilter_node root, *cur, *last_op;
85 memset(&root, 0, sizeof(root));
86 last_op = cur = &root;
89 while (*s != '\0' && *s != ')') {
91 case '&': /* Exchg last OP->r with AND */
92 if (!cur->r || !last_op->r)
94 cur = strfilter_node__alloc(OP_and, last_op->r, NULL);
100 case '|': /* Exchg the root with OR */
101 if (!cur->r || !root.r)
103 cur = strfilter_node__alloc(OP_or, root.r, NULL);
109 case '!': /* Add NOT as a leaf node */
112 cur->r = strfilter_node__alloc(OP_not, NULL, NULL);
117 case '(': /* Recursively parses inside the parenthesis */
120 cur->r = strfilter_node__new(s + 1, &s);
123 if (!cur->r || *s != ')')
130 cur->r = strfilter_node__alloc(NULL, NULL, NULL);
133 cur->r->p = strndup(s, e - s);
137 s = get_token(e, &e);
147 strfilter_node__delete(root.r);
152 * Parse filter rule and return new strfilter.
153 * Return NULL if fail, and *ep == NULL if memory allocation failed.
155 struct strfilter *strfilter__new(const char *rules, const char **err)
157 struct strfilter *filter = zalloc(sizeof(*filter));
158 const char *ep = NULL;
161 filter->root = strfilter_node__new(rules, &ep);
163 if (!filter || !filter->root || *ep != '\0') {
166 strfilter__delete(filter);
173 static int strfilter__append(struct strfilter *filter, bool _or,
174 const char *rules, const char **err)
176 struct strfilter_node *right, *root;
177 const char *ep = NULL;
179 if (!filter || !rules)
182 right = strfilter_node__new(rules, &ep);
183 if (!right || *ep != '\0') {
188 root = strfilter_node__alloc(_or ? OP_or : OP_and, filter->root, right);
198 strfilter_node__delete(right);
199 return ep ? -EINVAL : -ENOMEM;
202 int strfilter__or(struct strfilter *filter, const char *rules, const char **err)
204 return strfilter__append(filter, true, rules, err);
207 int strfilter__and(struct strfilter *filter, const char *rules,
210 return strfilter__append(filter, false, rules, err);
213 static bool strfilter_node__compare(struct strfilter_node *node,
216 if (!node || !node->p)
221 return strfilter_node__compare(node->l, str) ||
222 strfilter_node__compare(node->r, str);
224 return strfilter_node__compare(node->l, str) &&
225 strfilter_node__compare(node->r, str);
227 return !strfilter_node__compare(node->r, str);
229 return strglobmatch(str, node->p);
233 /* Return true if STR matches the filter rules */
234 bool strfilter__compare(struct strfilter *filter, const char *str)
238 return strfilter_node__compare(filter->root, str);
241 static int strfilter_node__sprint(struct strfilter_node *node, char *buf);
243 /* sprint node in parenthesis if needed */
244 static int strfilter_node__sprint_pt(struct strfilter_node *node, char *buf)
247 int pt = node->r ? 2 : 0; /* don't need to check node->l */
251 len = strfilter_node__sprint(node, buf);
259 static int strfilter_node__sprint(struct strfilter_node *node, char *buf)
263 if (!node || !node->p)
269 len = strfilter_node__sprint_pt(node->l, buf);
274 *(buf + len++) = *node->p;
278 rlen = strfilter_node__sprint_pt(node->r, buf);
284 len = strlen(node->p);
286 strcpy(buf, node->p);
292 char *strfilter__string(struct strfilter *filter)
297 len = strfilter_node__sprint(filter->root, NULL);
301 ret = malloc(len + 1);
303 strfilter_node__sprint(filter->root, ret);