Changeset 23fddadbb7a375688e9be9627f1f5ca39aaa54d0
- Timestamp:
- 10/23/09 23:01:30 (5 weeks ago)
- Author:
- Karl Ramm <kcr@1ts.org>
- git-author:
- Karl Ramm <kcr@1ts.org> / 2009-09-30T10:12:27Z-0400
- Parents:
- 52761cc7346cb35d2d70160f25573df691427ef2
- Children:
- 859077469433dd85e1c9ebff365cb2cc6d237c6a
- git-committer:
- Karl Ramm <kcr@1ts.org> / 2009-10-23T23:01:30Z-0400
- Message:
-
refactor & rename filter creation & storage management
filter_init_fromstring -> filter_new_fromstring
filter_init -> filter_new
filter_free -> filter_delete
Every time owl_filter_init and owl_filter_init_fromstring were called,
there was a call to malloc right above them; Conversely, after every
owl_filter_free there needed to be a call to owl_free (and sometimes
there wasn't).
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r73faa26
|
r23fddad
|
|
| 2426 | 2426 | f = owl_list_get_element(fl, i); |
| 2427 | 2427 | owl_list_remove_element(fl, i); |
| 2428 | | owl_filter_free(f); |
| | 2428 | owl_filter_delete(f); |
| 2429 | 2429 | return; |
| 2430 | 2430 | } else { |
-
|
r8bce750
|
r23fddad
|
|
| 2 | 2 | #include "owl.h" |
| 3 | 3 | |
| 4 | | int owl_filter_init_fromstring(owl_filter *f, const char *name, const char *string) |
| 5 | | { |
| | 4 | owl_filter *owl_filter_new_fromstring(const char *name, const char *string) |
| | 5 | { |
| | 6 | owl_filter *f; |
| 6 | 7 | char **argv; |
| 7 | | int argc, out; |
| 8 | | |
| 9 | | argv=owl_parseline(string, &argc); |
| 10 | | out=owl_filter_init(f, name, argc, strs(argv)); |
| | 8 | int argc; |
| | 9 | |
| | 10 | argv = owl_parseline(string, &argc); |
| | 11 | f = owl_filter_new(name, argc, strs(argv)); |
| 11 | 12 | owl_parsefree(argv, argc); |
| 12 | | return(out); |
| 13 | | } |
| 14 | | |
| 15 | | int owl_filter_init(owl_filter *f, const char *name, int argc, const char *const *argv) |
| 16 | | { |
| | 13 | |
| | 14 | return f; |
| | 15 | } |
| | 16 | |
| | 17 | owl_filter *owl_filter_new(const char *name, int argc, const char *const *argv) |
| | 18 | { |
| | 19 | owl_filter *f; |
| | 20 | |
| | 21 | f = owl_malloc(sizeof(owl_filter)); |
| | 22 | |
| 17 | 23 | f->name=owl_strdup(name); |
| 18 | 24 | f->fgcolor=OWL_COLOR_DEFAULT; |
| … |
… |
|
| 40 | 46 | } |
| 41 | 47 | |
| 42 | | if(!(f->root = owl_filter_parse_expression(argc, argv, NULL))) |
| 43 | | return(-1); |
| | 48 | if (!(f->root = owl_filter_parse_expression(argc, argv, NULL))) { |
| | 49 | owl_filter_delete(f); |
| | 50 | return NULL; |
| | 51 | } |
| 44 | 52 | |
| 45 | 53 | /* Now check for recursion. */ |
| 46 | 54 | if (owl_filter_is_toodeep(f)) { |
| 47 | 55 | owl_function_error("Filter loop!"); |
| 48 | | owl_filter_free(f); |
| 49 | | return(-1); |
| 50 | | } |
| 51 | | |
| 52 | | return(0); |
| | 56 | owl_filter_delete(f); |
| | 57 | return NULL; |
| | 58 | } |
| | 59 | |
| | 60 | return f; |
| 53 | 61 | } |
| 54 | 62 | |
| … |
… |
|
| 255 | 263 | } |
| 256 | 264 | |
| 257 | | void owl_filter_free(owl_filter *f) |
| 258 | | { |
| 259 | | if(f->root) { |
| | 265 | void owl_filter_delete(owl_filter *f) |
| | 266 | { |
| | 267 | if (f == NULL) |
| | 268 | return; |
| | 269 | if (f->root) { |
| 260 | 270 | owl_filterelement_free(f->root); |
| 261 | 271 | owl_free(f->root); |
| 262 | 272 | } |
| 263 | | if (f->name) owl_free(f->name); |
| 264 | | } |
| | 273 | if (f->name) |
| | 274 | owl_free(f->name); |
| | 275 | owl_free(f); |
| | 276 | } |
-
|
r52761cc
|
r23fddad
|
|
| 2133 | 2133 | owl_filter *f; |
| 2134 | 2134 | const owl_view *v; |
| 2135 | | int ret, inuse=0; |
| | 2135 | int inuse = 0; |
| 2136 | 2136 | |
| 2137 | 2137 | if (argc < 2) { |
| … |
… |
|
| 2183 | 2183 | |
| 2184 | 2184 | /* create the filter and check for errors */ |
| 2185 | | f=owl_malloc(sizeof(owl_filter)); |
| 2186 | | ret=owl_filter_init(f, argv[1], argc-2, argv+2); |
| 2187 | | if (ret==-1) { |
| 2188 | | owl_free(f); |
| | 2185 | f = owl_filter_new(argv[1], argc-2, argv+2); |
| | 2186 | if (f == NULL) { |
| 2189 | 2187 | owl_function_error("Invalid filter"); |
| 2190 | 2188 | return; |
| … |
… |
|
| 2382 | 2380 | if (tmpinstance) owl_free(tmpinstance); |
| 2383 | 2381 | |
| 2384 | | f=owl_malloc(sizeof(owl_filter)); |
| 2385 | | owl_filter_init_fromstring(f, filtname, argbuff); |
| | 2382 | f = owl_filter_new_fromstring(filtname, argbuff); |
| 2386 | 2383 | |
| 2387 | 2384 | /* add it to the global list */ |
| … |
… |
|
| 2421 | 2418 | |
| 2422 | 2419 | /* create the new-internal filter */ |
| 2423 | | f=owl_malloc(sizeof(owl_filter)); |
| 2424 | | |
| 2425 | 2420 | esclonguser = owl_text_quote(longuser, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
| 2426 | 2421 | |
| … |
… |
|
| 2430 | 2425 | esclonguser); |
| 2431 | 2426 | |
| 2432 | | owl_filter_init_fromstring(f, filtname, argbuff); |
| | 2427 | f = owl_filter_new_fromstring(filtname, argbuff); |
| 2433 | 2428 | |
| 2434 | 2429 | /* add it to the global list */ |
| … |
… |
|
| 2465 | 2460 | |
| 2466 | 2461 | /* create the new-internal filter */ |
| 2467 | | f=owl_malloc(sizeof(owl_filter)); |
| 2468 | | |
| 2469 | 2462 | escuser = owl_text_quote(user, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
| 2470 | 2463 | |
| … |
… |
|
| 2474 | 2467 | escuser, owl_global_get_aim_screenname_for_filters(&g)); |
| 2475 | 2468 | |
| 2476 | | owl_filter_init_fromstring(f, filtname, argbuff); |
| | 2469 | f = owl_filter_new_fromstring(filtname, argbuff); |
| 2477 | 2470 | |
| 2478 | 2471 | /* add it to the global list */ |
| … |
… |
|
| 2506 | 2499 | argbuff = owl_sprintf("type ^%s$", esctype); |
| 2507 | 2500 | |
| 2508 | | owl_filter_init_fromstring(f, filtname, argbuff); |
| | 2501 | f = owl_filter_new_fromstring(filtname, argbuff); |
| 2509 | 2502 | |
| 2510 | 2503 | /* add it to the global list */ |
| … |
… |
|
| 2825 | 2818 | owl_filter *f; |
| 2826 | 2819 | owl_list *fl; |
| 2827 | | int ret, i, j; |
| | 2820 | int i, j; |
| 2828 | 2821 | fl=owl_global_get_puntlist(&g); |
| 2829 | 2822 | |
| 2830 | 2823 | /* first, create the filter */ |
| 2831 | | f=owl_malloc(sizeof(owl_filter)); |
| 2832 | | |
| 2833 | 2824 | owl_function_debugmsg("About to filter %s", filter); |
| 2834 | | ret=owl_filter_init_fromstring(f, "punt-filter", filter); |
| 2835 | | if (ret) { |
| | 2825 | f = owl_filter_new_fromstring("punt-filter", filter); |
| | 2826 | if (f == NULL) { |
| 2836 | 2827 | owl_function_error("Error creating filter for zpunt"); |
| 2837 | | owl_filter_free(f); |
| 2838 | | owl_free(f); |
| 2839 | 2828 | return; |
| 2840 | 2829 | } |
| … |
… |
|
| 2847 | 2836 | /* if we're punting, then just silently bow out on this duplicate */ |
| 2848 | 2837 | if (direction==0) { |
| 2849 | | owl_filter_free(f); |
| 2850 | | owl_free(f); |
| | 2838 | owl_filter_delete(f); |
| 2851 | 2839 | return; |
| 2852 | 2840 | } |
| … |
… |
|
| 2854 | 2842 | /* if we're unpunting, then remove this filter from the puntlist */ |
| 2855 | 2843 | if (direction==1) { |
| 2856 | | owl_filter_free(owl_list_get_element(fl, i)); |
| | 2844 | owl_filter_delete(owl_list_get_element(fl, i)); |
| 2857 | 2845 | owl_list_remove_element(fl, i); |
| 2858 | | owl_filter_free(f); |
| 2859 | | owl_free(f); |
| | 2846 | owl_filter_delete(f); |
| 2860 | 2847 | return; |
| 2861 | 2848 | } |
-
|
r52761cc
|
r23fddad
|
|
| 635 | 635 | f=owl_list_get_element(&(g->filterlist), i); |
| 636 | 636 | if (!strcmp(name, owl_filter_get_name(f))) { |
| 637 | | owl_filter_free(f); |
| | 637 | owl_filter_delete(f); |
| 638 | 638 | owl_list_remove_element(&(g->filterlist), i); |
| 639 | 639 | break; |
-
|
r84ea53c
|
r23fddad
|
|
| 151 | 151 | static void owl_setup_default_filters(void) |
| 152 | 152 | { |
| 153 | | owl_filter *f; |
| 154 | 153 | int i; |
| 155 | 154 | static const struct { |
| … |
… |
|
| 176 | 175 | owl_function_debugmsg("startup: creating default filters"); |
| 177 | 176 | |
| 178 | | for (i = 0; filters[i].name != NULL; i++) { |
| 179 | | f = owl_malloc(sizeof(owl_filter)); |
| 180 | | owl_filter_init_fromstring(f, filters[i].name, filters[i].desc); |
| 181 | | owl_list_append_element(owl_global_get_filterlist(&g), f); |
| 182 | | } |
| | 177 | for (i = 0; filters[i].name != NULL; i++) |
| | 178 | owl_list_append_element(owl_global_get_filterlist(&g), |
| | 179 | owl_filter_new_fromstring(filters[i].name, |
| | 180 | filters[i].desc)); |
| 183 | 181 | } |
| 184 | 182 | |
-
|
rdd6af02
|
r23fddad
|
|
| 194 | 194 | |
| 195 | 195 | int owl_filter_test_string(const char * filt, const owl_message *m, int shouldmatch) /* noproto */ { |
| 196 | | owl_filter f; |
| | 196 | owl_filter *f; |
| 197 | 197 | int ok; |
| 198 | 198 | int failed = 0; |
| 199 | | if(owl_filter_init_fromstring(&f, "test-filter", filt)) { |
| | 199 | if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) { |
| 200 | 200 | printf("not ok can't parse %s\n", filt); |
| 201 | 201 | failed = 1; |
| 202 | 202 | goto out; |
| 203 | 203 | } |
| 204 | | ok = owl_filter_message_match(&f, m); |
| | 204 | ok = owl_filter_message_match(f, m); |
| 205 | 205 | if((shouldmatch && !ok) || (!shouldmatch && ok)) { |
| 206 | 206 | printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch); |
| … |
… |
|
| 208 | 208 | } |
| 209 | 209 | out: |
| 210 | | owl_filter_free(&f); |
| | 210 | owl_filter_delete(f); |
| 211 | 211 | if(!failed) { |
| 212 | 212 | printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt); |
| … |
… |
|
| 218 | 218 | int numfailed=0; |
| 219 | 219 | owl_message m; |
| 220 | | owl_filter f1, f2, f3, f4, f5; |
| | 220 | owl_filter *f1, *f2, *f3, *f4, *f5; |
| 221 | 221 | |
| 222 | 222 | owl_list_create(&(g.filterlist)); |
| … |
… |
|
| 260 | 260 | TEST_FILTER("true and false or false", 0); |
| 261 | 261 | |
| 262 | | owl_filter_init_fromstring(&f1, "f1", "class owl"); |
| 263 | | owl_global_add_filter(&g, &f1); |
| | 262 | f1 = owl_filter_new_fromstring("f1", "class owl"); |
| | 263 | owl_global_add_filter(&g, f1); |
| 264 | 264 | TEST_FILTER("filter f1", 1); |
| | 265 | owl_global_remove_filter(&g, "f1"); |
| 265 | 266 | |
| 266 | 267 | /* Test recursion prevention */ |
| 267 | | FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test")); |
| | 268 | FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL); |
| | 269 | owl_filter_delete(f2); |
| 268 | 270 | |
| 269 | 271 | /* mutual recursion */ |
| 270 | | owl_filter_init_fromstring(&f3, "f3", "filter f4"); |
| 271 | | owl_global_add_filter(&g, &f3); |
| 272 | | FAIL_UNLESS("mutual recursion", owl_filter_init_fromstring(&f4, "f4", "filter f3")); |
| | 272 | f3 = owl_filter_new_fromstring("f3", "filter f4"); |
| | 273 | owl_global_add_filter(&g, f3); |
| | 274 | FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL); |
| | 275 | owl_global_remove_filter(&g, "f3"); |
| | 276 | owl_filter_delete(f4); |
| 273 | 277 | |
| 274 | 278 | /* support referencing a filter several times */ |
| 275 | | FAIL_UNLESS("DAG", !owl_filter_init_fromstring(&f5, "dag", "filter f1 or filter f1")); |
| | 279 | FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL); |
| | 280 | owl_filter_delete(f5); |
| 276 | 281 | |
| 277 | 282 | return 0; |