source: filter.c @ d54035d

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since d54035d 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
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
[1aee7d9]4static const char fileIdent[] = "$Id$";
5
[e187445]6int owl_filter_init_fromstring(owl_filter *f, char *name, char *string)
7{
[7d4fbcd]8  char **argv;
9  int argc, out;
10
11  argv=owl_parseline(string, &argc);
12  out=owl_filter_init(f, name, argc, argv);
[1716fed]13  owl_parsefree(argv, argc);
[7d4fbcd]14  return(out);
15}
16
[e187445]17int owl_filter_init(owl_filter *f, char *name, int argc, char **argv)
18{
[7d4fbcd]19  f->name=owl_strdup(name);
20  f->polarity=0;
[8fa9562]21  f->fgcolor=OWL_COLOR_DEFAULT;
22  f->bgcolor=OWL_COLOR_DEFAULT;
[59cf91c]23  f->cachedmsgid=-1;
[cb769bb]24
[7d4fbcd]25  /* first take arguments that have to come first */
26  /* set the color */
[8fa9562]27  while ( argc>=2 && ( !strcmp(argv[0], "-c") ||
28                       !strcmp(argv[0], "-b") ) ) {
[601733d]29    if (owl_util_string_to_color(argv[1])==OWL_COLOR_INVALID) {
[12c35df]30      owl_function_error("The color '%s' is not available, using default.", argv[1]);
31    } else {
[8fa9562]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      }
[12c35df]40    }
[7d4fbcd]41    argc-=2;
42    argv+=2;
43  }
44
[cb769bb]45  if(!(f->root = owl_filter_parse_expression(argc, argv, NULL)))
46    return(-1);
[7d4fbcd]47
[cb769bb]48  /* Now check for recursion. */
[40458b9]49  if (owl_filter_is_toodeep(f)) {
[9711a6e]50    owl_function_error("Filter loop!");
[40458b9]51    owl_filter_free(f);
52    return(-1);
[7d4fbcd]53  }
[cb769bb]54
[7d4fbcd]55  return(0);
56}
57
[cb769bb]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{
[ad15610]63  owl_filterelement *fe, *op;
64  int i = 0, skip;
[cb769bb]65
[ad15610]66  if(!argc) return NULL;
[cb769bb]67
[ad15610]68  fe = owl_malloc(sizeof(owl_filterelement));
[cb769bb]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;
[d791cdb]76    if(i >= argc) goto err;
[cb769bb]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;
[ad15610]118  owl_filterelement * op1 = NULL, * op2 = NULL, *tmp;
[cb769bb]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;
[ad15610]129    tmp = owl_malloc(sizeof(owl_filterelement));
[cb769bb]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
[e187445]154char *owl_filter_get_name(owl_filter *f)
155{
[7d4fbcd]156  return(f->name);
157}
158
[e187445]159void owl_filter_set_polarity_match(owl_filter *f)
160{
[7d4fbcd]161  f->polarity=0;
162}
163
[e187445]164void owl_filter_set_polarity_unmatch(owl_filter *f)
165{
[7d4fbcd]166  f->polarity=1;
167}
168
[8fa9562]169void owl_filter_set_fgcolor(owl_filter *f, int color)
[e187445]170{
[8fa9562]171  f->fgcolor=color;
[7d4fbcd]172}
173
[8fa9562]174int owl_filter_get_fgcolor(owl_filter *f)
[e187445]175{
[8fa9562]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);
[7d4fbcd]187}
188
[e187445]189void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid)
190{
[59cf91c]191  f->cachedmsgid=cachedmsgid;
192}
193
[e187445]194int owl_filter_get_cachedmsgid(owl_filter *f)
195{
[59cf91c]196  return(f->cachedmsgid);
197}
198
[15b34fd]199/* return 1 if the message matches the given filter, otherwise
200 * return 0.
201 */
[e187445]202int owl_filter_message_match(owl_filter *f, owl_message *m)
203{
[ad15610]204  int ret;
[cb769bb]205  if(!f->root) return 0;
[ad15610]206  ret = owl_filterelement_match(f->root, m);
[cb769bb]207  if(f->polarity) ret = !ret;
208  return ret;
[7d4fbcd]209}
210
211
[0504f63]212char* owl_filter_print(owl_filter *f)
[e187445]213{
[cdc6ff1]214  GString *out = g_string_new("");
[446aa2b]215
[8fa9562]216  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
[0504f63]217    g_string_append(out, "-c ");
[c2c5c77]218    if (f->fgcolor < 8) {
[0504f63]219      g_string_append(out, owl_util_color_to_string(f->fgcolor));
[c2c5c77]220    }
221    else {
[0504f63]222      g_string_append_printf(out, "%i",f->fgcolor);
[c2c5c77]223    }
[0504f63]224    g_string_append(out, " ");
[8fa9562]225  }
226  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
[0504f63]227    g_string_append(out, "-b ");
[c2c5c77]228    if (f->bgcolor < 8) {
[0504f63]229      g_string_append(out, owl_util_color_to_string(f->bgcolor));
[c2c5c77]230    }
231    else {
[0504f63]232      g_string_append_printf(out, "%i",f->fgcolor);
[c2c5c77]233    }
[0504f63]234    g_string_append(out, " ");
235  }
236  if(f->root) {
237    owl_filterelement_print(f->root, out);
238    g_string_append(out, "\n");
[446aa2b]239  }
[0504f63]240
241  return g_string_free(out, 0);
[7d4fbcd]242}
243
[40458b9]244/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
[e187445]245int owl_filter_equiv(owl_filter *a, owl_filter *b)
246{
[0504f63]247  char *buffa, *buffb;
248  int ret;
[7d4fbcd]249
[0504f63]250  buffa = owl_filter_print(a);
251  buffb = owl_filter_print(b);
[7d4fbcd]252
[0504f63]253  ret = !strcmp(buffa, buffb);
[cdc6ff1]254  ret = ret && !strcmp(owl_filter_get_name(a),
255                       owl_filter_get_name(b));
[0504f63]256
257  owl_free(buffa);
258  owl_free(buffb);
259
260  return ret;
[7d4fbcd]261}
262
[cb769bb]263
264int owl_filter_is_toodeep(owl_filter *f)
[40458b9]265{
[cb769bb]266  return owl_filterelement_is_toodeep(f, f->root);
[40458b9]267}
268
[cb769bb]269void owl_filter_free(owl_filter *f)
[40458b9]270{
[cb769bb]271  if(f->root) {
272    owl_filterelement_free(f->root);
273    owl_free(f->root);
[40458b9]274  }
[cb769bb]275  if (f->name) owl_free(f->name);
276}
[40458b9]277
[cb769bb]278/**************************************************************************/
279/************************* REGRESSION TESTS *******************************/
280/**************************************************************************/
[40458b9]281
[cb769bb]282#ifdef OWL_INCLUDE_REG_TESTS
[40458b9]283
[cb769bb]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)) {
[535d68b]289    printf("not ok can't parse %s\n", filt);
[cb769bb]290    failed = 1;
291    goto out;
292  }
293  ok = owl_filter_message_match(&f, m);
294  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
[535d68b]295    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
[cb769bb]296    failed = 1;
297  }
298 out:
299  owl_filter_free(&f);
300  if(!failed) {
[535d68b]301    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
[cb769bb]302  }
303  return failed;
304}
[40458b9]305
306
[cb769bb]307#include "test.h"
[40458b9]308
309
[cb769bb]310int owl_filter_regtest(void) {
311  int numfailed=0;
312  owl_message m;
[9711a6e]313  owl_filter f1, f2, f3, f4, f5;
[ad15610]314
315  owl_list_create(&(g.filterlist));
[cb769bb]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");
[40458b9]324
[cb769bb]325#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
[40458b9]326
[7d4fbcd]327
[cb769bb]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
[af1920fd]346  /* Order of operations and precedence */
[cb769bb]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
[af1920fd]357  /* Test recursion prevention */
[cb769bb]358  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
359
[af1920fd]360  /* mutual recursion */
[cb769bb]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
[9711a6e]365  /* support referencing a filter several times */
366  FAIL_UNLESS("DAG", !owl_filter_init_fromstring(&f5, "dag", "filter f1 or filter f1"));
367
[cb769bb]368  return 0;
[7d4fbcd]369}
[cb769bb]370
371
372#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.