source: filter.c @ 2209f16

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2209f16 was 9711a6e, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Add a test case to filter.c for referencing a filter multiple times.
  • Property mode set to 100644
File size: 8.7 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4static const char fileIdent[] = "$Id$";
5
6int owl_filter_init_fromstring(owl_filter *f, char *name, char *string)
7{
8  char **argv;
9  int argc, out;
10
11  argv=owl_parseline(string, &argc);
12  out=owl_filter_init(f, name, argc, argv);
13  owl_parsefree(argv, argc);
14  return(out);
15}
16
17int owl_filter_init(owl_filter *f, char *name, int argc, char **argv)
18{
19  f->name=owl_strdup(name);
20  f->polarity=0;
21  f->fgcolor=OWL_COLOR_DEFAULT;
22  f->bgcolor=OWL_COLOR_DEFAULT;
23  f->cachedmsgid=-1;
24
25  /* first take arguments that have to come first */
26  /* set the color */
27  while ( argc>=2 && ( !strcmp(argv[0], "-c") ||
28                       !strcmp(argv[0], "-b") ) ) {
29    if (owl_util_string_to_color(argv[1])==OWL_COLOR_INVALID) {
30      owl_function_error("The color '%s' is not available, using default.", argv[1]);
31    } else {
32      switch (argv[0][1]) {
33      case 'c':
34        f->fgcolor=owl_util_string_to_color(argv[1]);
35        break;
36      case 'b':
37        f->bgcolor=owl_util_string_to_color(argv[1]);
38        break;
39      }
40    }
41    argc-=2;
42    argv+=2;
43  }
44
45  if(!(f->root = owl_filter_parse_expression(argc, argv, NULL)))
46    return(-1);
47
48  /* Now check for recursion. */
49  if (owl_filter_is_toodeep(f)) {
50    owl_function_error("Filter loop!");
51    owl_filter_free(f);
52    return(-1);
53  }
54
55  return(0);
56}
57
58
59/* A primitive expression is one without any toplevel ``and'' or ``or''s*/
60
61static owl_filterelement * owl_filter_parse_primitive_expression(int argc, char **argv, int *next)
62{
63  owl_filterelement *fe, *op;
64  int i = 0, skip;
65
66  if(!argc) return NULL;
67
68  fe = owl_malloc(sizeof(owl_filterelement));
69  owl_filterelement_create(fe);
70
71  if(!strcasecmp(argv[i], "(")) {
72    i++;
73    op = owl_filter_parse_expression(argc-i, argv+i, &skip);
74    if(!op) goto err;
75    i += skip;
76    if(i >= argc) goto err;
77    if(strcasecmp(argv[i++], ")")) goto err;
78    owl_filterelement_create_group(fe, op);
79  } else if(!strcasecmp(argv[i], "not")) {
80    i++;
81    op = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip);
82    if(!op) goto err;
83    i += skip;
84    owl_filterelement_create_not(fe, op);
85  } else if(!strcasecmp(argv[i], "true")) {
86    i++;
87    owl_filterelement_create_true(fe);
88  } else if(!strcasecmp(argv[i], "false")) {
89    i++;
90    owl_filterelement_create_false(fe);
91  } else {
92    if(argc == 1) goto err;
93    if(!strcasecmp(*argv, "filter")) {
94      owl_filterelement_create_filter(fe, *(argv+1));
95    } else if(!strcasecmp(*argv, "perl")) {
96      owl_filterelement_create_perl(fe, *(argv+1));
97    } else {
98      owl_filterelement_create_re(fe, *argv, *(argv+1));
99    }
100    i += 2;
101  }
102
103  if(next) {
104    *next = i;
105  } else if(i != argc) {
106    goto err;
107  }
108  return fe;
109err:
110  owl_filterelement_free(fe);
111  owl_free(fe);
112  return NULL;
113}
114
115owl_filterelement * owl_filter_parse_expression(int argc, char **argv, int *next)
116{
117  int i = 0, skip;
118  owl_filterelement * op1 = NULL, * op2 = NULL, *tmp;
119
120  op1 = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip);
121  i += skip;
122  if(!op1) goto err;
123
124  while(i < argc) {
125    if(strcasecmp(argv[i], "and") &&
126       strcasecmp(argv[i], "or")) break;
127    op2 = owl_filter_parse_primitive_expression(argc-i-1, argv+i+1, &skip);
128    if(!op2) goto err;
129    tmp = owl_malloc(sizeof(owl_filterelement));
130    if(!strcasecmp(argv[i], "and")) {
131      owl_filterelement_create_and(tmp, op1, op2);
132    } else {
133      owl_filterelement_create_or(tmp, op1, op2);
134    }
135    op1 = tmp;
136    op2 = NULL;
137    i += skip+1;
138  }
139
140  if(next) {
141    *next = i;
142  } else if(i != argc) {
143    goto err;
144  }
145  return op1;
146err:
147  if(op1) {
148    owl_filterelement_free(op1);
149    owl_free(op1);
150  }
151  return NULL;
152}
153
154char *owl_filter_get_name(owl_filter *f)
155{
156  return(f->name);
157}
158
159void owl_filter_set_polarity_match(owl_filter *f)
160{
161  f->polarity=0;
162}
163
164void owl_filter_set_polarity_unmatch(owl_filter *f)
165{
166  f->polarity=1;
167}
168
169void owl_filter_set_fgcolor(owl_filter *f, int color)
170{
171  f->fgcolor=color;
172}
173
174int owl_filter_get_fgcolor(owl_filter *f)
175{
176  return(f->fgcolor);
177}
178
179void owl_filter_set_bgcolor(owl_filter *f, int color)
180{
181  f->bgcolor=color;
182}
183
184int owl_filter_get_bgcolor(owl_filter *f)
185{
186  return(f->bgcolor);
187}
188
189void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid)
190{
191  f->cachedmsgid=cachedmsgid;
192}
193
194int owl_filter_get_cachedmsgid(owl_filter *f)
195{
196  return(f->cachedmsgid);
197}
198
199/* return 1 if the message matches the given filter, otherwise
200 * return 0.
201 */
202int owl_filter_message_match(owl_filter *f, owl_message *m)
203{
204  int ret;
205  if(!f->root) return 0;
206  ret = owl_filterelement_match(f->root, m);
207  if(f->polarity) ret = !ret;
208  return ret;
209}
210
211
212char* owl_filter_print(owl_filter *f)
213{
214  GString *out = g_string_new("");
215
216  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
217    g_string_append(out, "-c ");
218    if (f->fgcolor < 8) {
219      g_string_append(out, owl_util_color_to_string(f->fgcolor));
220    }
221    else {
222      g_string_append_printf(out, "%i",f->fgcolor);
223    }
224    g_string_append(out, " ");
225  }
226  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
227    g_string_append(out, "-b ");
228    if (f->bgcolor < 8) {
229      g_string_append(out, owl_util_color_to_string(f->bgcolor));
230    }
231    else {
232      g_string_append_printf(out, "%i",f->fgcolor);
233    }
234    g_string_append(out, " ");
235  }
236  if(f->root) {
237    owl_filterelement_print(f->root, out);
238    g_string_append(out, "\n");
239  }
240
241  return g_string_free(out, 0);
242}
243
244/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
245int owl_filter_equiv(owl_filter *a, owl_filter *b)
246{
247  char *buffa, *buffb;
248  int ret;
249
250  buffa = owl_filter_print(a);
251  buffb = owl_filter_print(b);
252
253  ret = !strcmp(buffa, buffb);
254  ret = ret && !strcmp(owl_filter_get_name(a),
255                       owl_filter_get_name(b));
256
257  owl_free(buffa);
258  owl_free(buffb);
259
260  return ret;
261}
262
263
264int owl_filter_is_toodeep(owl_filter *f)
265{
266  return owl_filterelement_is_toodeep(f, f->root);
267}
268
269void owl_filter_free(owl_filter *f)
270{
271  if(f->root) {
272    owl_filterelement_free(f->root);
273    owl_free(f->root);
274  }
275  if (f->name) owl_free(f->name);
276}
277
278/**************************************************************************/
279/************************* REGRESSION TESTS *******************************/
280/**************************************************************************/
281
282#ifdef OWL_INCLUDE_REG_TESTS
283
284int owl_filter_test_string(char * filt, owl_message *m, int shouldmatch) /* noproto */ {
285  owl_filter f;
286  int ok;
287  int failed = 0;
288  if(owl_filter_init_fromstring(&f, "test-filter", filt)) {
289    printf("not ok can't parse %s\n", filt);
290    failed = 1;
291    goto out;
292  }
293  ok = owl_filter_message_match(&f, m);
294  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
295    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
296    failed = 1;
297  }
298 out:
299  owl_filter_free(&f);
300  if(!failed) {
301    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
302  }
303  return failed;
304}
305
306
307#include "test.h"
308
309
310int owl_filter_regtest(void) {
311  int numfailed=0;
312  owl_message m;
313  owl_filter f1, f2, f3, f4, f5;
314
315  owl_list_create(&(g.filterlist));
316  owl_message_init(&m);
317  owl_message_set_type_zephyr(&m);
318  owl_message_set_direction_in(&m);
319  owl_message_set_class(&m, "owl");
320  owl_message_set_instance(&m, "tester");
321  owl_message_set_sender(&m, "owl-user");
322  owl_message_set_recipient(&m, "joe");
323  owl_message_set_attribute(&m, "foo", "bar");
324
325#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
326
327
328  TEST_FILTER("true", 1);
329  TEST_FILTER("false", 0);
330  TEST_FILTER("( true )", 1);
331  TEST_FILTER("not false", 1);
332  TEST_FILTER("( true ) or ( false )", 1);
333  TEST_FILTER("true and false", 0);
334  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
335
336  TEST_FILTER("class owl", 1);
337  TEST_FILTER("class ^owl$", 1);
338  TEST_FILTER("instance test", 1);
339  TEST_FILTER("instance ^test$", 0);
340  TEST_FILTER("instance ^tester$", 1);
341
342  TEST_FILTER("foo bar", 1);
343  TEST_FILTER("class owl and instance tester", 1);
344  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
345
346  /* Order of operations and precedence */
347  TEST_FILTER("not true or false", 0);
348  TEST_FILTER("true or true and false", 0);
349  TEST_FILTER("true and true and false or true", 1);
350  TEST_FILTER("false and false or true", 1);
351  TEST_FILTER("true and false or false", 0);
352
353  owl_filter_init_fromstring(&f1, "f1", "class owl");
354  owl_global_add_filter(&g, &f1);
355  TEST_FILTER("filter f1", 1);
356
357  /* Test recursion prevention */
358  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
359
360  /* mutual recursion */
361  owl_filter_init_fromstring(&f3, "f3", "filter f4");
362  owl_global_add_filter(&g, &f3);
363  FAIL_UNLESS("mutual recursion",   owl_filter_init_fromstring(&f4, "f4", "filter f3"));
364
365  /* support referencing a filter several times */
366  FAIL_UNLESS("DAG", !owl_filter_init_fromstring(&f5, "dag", "filter f1 or filter f1"));
367
368  return 0;
369}
370
371
372#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.