source: filter.c @ 130633c

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