X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd%2Ffilter.c;h=ab608d8223ae27d249c3f4da96922918eeaf05c9;hb=39e8c944acc71c4e3deaa48d55c09279fd80d81f;hp=a150d6dbe97eb10451c43ecd5c33bafe92466c48;hpb=ae0c2eb5d2ea501fd9e458fc138696c268a14569;p=apps%2Fpfixtools.git diff --git a/postlicyd/filter.c b/postlicyd/filter.c index a150d6d..ab608d8 100644 --- a/postlicyd/filter.c +++ b/postlicyd/filter.c @@ -45,10 +45,13 @@ static bool params[FTK_count][ATK_count]; static filter_context_constructor_t ctx_constructors[FTK_count]; static filter_context_destructor_t ctx_destructors[FTK_count]; +static filter_async_handler_t async_handler = NULL; static const filter_hook_t default_hook = { .type = 0, .value = (char*)"DUNNO", + .counter = -1, + .cost = 0, .postfix = true, .async = false, .filter_id = 0 @@ -57,11 +60,15 @@ static const filter_hook_t default_hook = { static const filter_hook_t async_hook = { .type = 0, .value = NULL, + .counter = -1, + .cost = 0, .postfix = false, .async = true, .filter_id = 0 }; +uint32_t filter_running = 0; + filter_type_t filter_register(const char *type, filter_constructor_t constructor, filter_destructor_t destructor, filter_runner_t runner, filter_context_constructor_t context_constructor, @@ -101,6 +108,11 @@ filter_param_id_t filter_param_register(filter_type_t filter, return tok; } +void filter_async_handler_register(filter_async_handler_t handler) +{ + async_handler = handler; +} + bool filter_build(filter_t *filter) { bool ret = true; @@ -180,22 +192,16 @@ void filter_wipe(filter_t *filter) p_delete(&filter->name); } -const filter_hook_t *filter_run(const filter_t *filter, const query_t *query, - filter_context_t *context) +static inline const filter_hook_t *filter_hook_for_result(const filter_t *filter, + filter_result_t res) { int start = 0; int end = filter->hooks.len; - debug("running filter %s (%s)", filter->name, ftokens[filter->type]); - filter_result_t res = runners[filter->type](filter, query, context); - context->current_filter = NULL; - - debug("filter run, result is %s", htokens[res]); if (res == HTK_ABORT) { return NULL; } if (res == HTK_ASYNC) { - context->current_filter = filter; return &async_hook; } @@ -216,6 +222,24 @@ const filter_hook_t *filter_run(const filter_t *filter, const query_t *query, return &default_hook; } +const filter_hook_t *filter_run(const filter_t *filter, const query_t *query, + filter_context_t *context) +{ + debug("running filter %s (%s)", filter->name, ftokens[filter->type]); + ++filter_running; + filter_result_t res = runners[filter->type](filter, query, context); + + if (res == HTK_ASYNC) { + context->current_filter = filter; + } else { + --filter_running; + context->current_filter = NULL; + } + + debug("filter run, result is %s", htokens[res]); + return filter_hook_for_result(filter, res); +} + bool filter_test(const filter_t *filter, const query_t *query, filter_context_t *context, filter_result_t result) { @@ -257,6 +281,7 @@ bool filter_add_hook(filter_t *filter, const char *name, int name_len, const char *value, int value_len) { filter_hook_t hook; + hook.filter_id = -1; hook.type = hook_tokenize(name, name_len); if (hook.type == HTK_UNKNOWN) { err("unknown hook type %.*s", name_len, name); @@ -268,9 +293,42 @@ bool filter_add_hook(filter_t *filter, const char *name, int name_len, return false; } hook.async = false; + + /* Value format is (counter:id:incr)?(postfix:reply|filter_name) + */ + hook.value = NULL; + if (strncmp(value, "counter:", 8) == 0) { + char *end = NULL; + value += 8; + hook.counter = strtol(value, &end, 10); + if (end == value || *end != ':') { + err("hook %s, cannot read counter id", htokens[hook.type]); + return false; + } else if (hook.counter < 0 || hook.counter >= MAX_COUNTERS) { + err("hook %s, invalid counter id %d", htokens[hook.type], hook.counter); + return false; + } + value = end + 1; + hook.cost = strtol(value, &end, 10); + if (end == value || *end != ':') { + err("hook %s, cannot read counter increment", htokens[hook.type]); + return false; + } else if (hook.cost < 0) { + err("hook %s, invalid counter increment value %d", htokens[hook.type], + hook.cost); + return false; + } + value = end + 1; + } else { + hook.counter = -1; + hook.cost = 0; + } hook.postfix = (strncmp(value, "postfix:", 8) == 0); + if (hook.postfix && !query_format_check(value + 8)) { + err("invalid formatted text \"%s\"", value + 8); + return false; + } hook.value = m_strdup(hook.postfix ? value + 8 : value); - hook.filter_id = -1; array_add(filter->hooks, hook); return true; } @@ -294,3 +352,22 @@ void filter_context_wipe(filter_context_t *context) } } } + +void filter_context_clean(filter_context_t *context) +{ + p_clear(&context->counters, 1); + context->instance[0] = '\0'; +} + +void filter_post_async_result(filter_context_t *context, filter_result_t result) +{ + const filter_t *filter = context->current_filter; + const filter_hook_t *hook = NULL; + + if (result == HTK_ASYNC) { + return; + } + --filter_running; + hook = filter_hook_for_result(filter, result); + async_handler(context, hook); +}