source: filter.c @ 06f5ef8

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