From 150de5c8a15de6797f2844293891621efefff40b Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Fri, 12 Sep 2008 22:37:36 +0200 Subject: [PATCH] Switch to array_t and feed the filters from config. Signed-off-by: Florent Bruneau --- postlicyd/config.c | 43 ++++++++++++++++++++++++++++--------------- postlicyd/filter.c | 18 ++++++++++++++---- postlicyd/filter.h | 41 ++++++++++++++++++++++++++++++++++------- 3 files changed, 76 insertions(+), 26 deletions(-) diff --git a/postlicyd/config.c b/postlicyd/config.c index 82f80f0..2d818f2 100644 --- a/postlicyd/config.c +++ b/postlicyd/config.c @@ -38,10 +38,8 @@ #include "config.h" struct config_t { - filter_t *filters; - int filters_len; - int filters_size; - + A(filter_t) filters; + A(filter_params_t) params; int entry_point; }; @@ -55,17 +53,16 @@ static inline config_t *config_new(void) void config_delete(config_t **config) { if (*config) { - for (int i = 0 ; i < (*config)->filters_len ; ++i) { - filter_wipe((*config)->filters + i); - } - p_delete(&(*config)->filters); + array_deep_wipe((*config)->filters, filter_wipe); + array_deep_wipe((*config)->params, filter_params_wipe); + p_delete(config); } } config_t *config_read(const char *file) { config_t *config; - //filter_t *filter = NULL; + filter_t filter; file_map_t map; const char *p; int line = 0; @@ -217,8 +214,8 @@ read_param_value: goto read_section; read_filter: - /* TODO: Create a filter with the given name. - */ + filter_init(&filter); + filter_set_name(&filter, key, key_len); READ_BLANK(goto badeof); while (*p != '}') { READ_TOKEN("filter parameter name", key, key_len); @@ -230,12 +227,28 @@ read_filter: READ_BLANK(goto badeof); READ_STRING("filter parameter value", value, value_len, goto badeof); READ_BLANK(goto badeof); - /* TODO: Insert parameter in the filter. - */ + if (strcmp(key, "type") == 0) { + if (!filter_set_type(&filter, value, value_len)) { + READ_ERROR("unknow filter type (%s) for filter %s", + value, filter.name); + } + } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) { + if (!filter_add_hook(&filter, key + 3, key_len - 3, + value, value_len)) { + READ_ERROR("hook %s not supported by filter %s", + key + 3, filter.name); + } + } else { + if (!filter_add_param(&filter, key, key_len, value, value_len)) { + goto error; + } + } } READ_NEXT(;); - /* TODO: Check the filter. - */ + if (!filter_build(&filter)) { + READ_ERROR("invalid filter %s", filter.name); + } + array_add(config->filters, filter); goto read_section; ok: diff --git a/postlicyd/filter.c b/postlicyd/filter.c index a98d29d..5c4f93e 100644 --- a/postlicyd/filter.c +++ b/postlicyd/filter.c @@ -53,6 +53,9 @@ void filter_register(const char *type, filter_constructor_t constructor, bool filter_build(filter_t *filter) { + if (filter->type == FTK_UNKNOWN || filter->name == NULL) { + return false; + } filter_constructor_t constructor = constructors[filter->type]; if (constructor) { return constructor(filter); @@ -66,8 +69,8 @@ void filter_wipe(filter_t *filter) if (destructor) { destructor(filter); } - p_delete(&filter->hooks); - p_delete(&filter->params); + array_deep_wipe(filter->hooks, filter_hook_wipe); + array_deep_wipe(filter->params, filter_params_wipe); p_delete(&filter->name); } @@ -76,12 +79,11 @@ filter_result_t filter_run(const filter_t *filter, const query_t *query) return runners[filter->type](filter, query); } -bool filter_set_name(filter_t *filter, const char *name, ssize_t len) +void filter_set_name(filter_t *filter, const char *name, ssize_t len) { filter->name = p_new(char, len + 1); memcpy(filter->name, name, len); filter->name[len] = '\0'; - return true; } bool filter_set_type(filter_t *filter, const char *type, ssize_t len) @@ -93,11 +95,19 @@ bool filter_set_type(filter_t *filter, const char *type, ssize_t len) bool filter_add_param(filter_t *filter, const char *name, ssize_t name_len, const char *value, ssize_t value_len) { + filter_params_t param; + param.name = strdup(name); + param.value = strdup(value); + array_add(filter->params, param); return true; } bool filter_add_hook(filter_t *filter, const char *name, ssize_t name_len, const char *value, ssize_t value_len) { + filter_hook_t hook; + hook.name = strdup(name); + hook.value = strdup(value); + array_add(filter->hooks, hook); return true; } diff --git a/postlicyd/filter.h b/postlicyd/filter.h index f532d1a..a08ec45 100644 --- a/postlicyd/filter.h +++ b/postlicyd/filter.h @@ -39,27 +39,33 @@ #include "common.h" #include "filter_tokens.h" #include "query.h" +#include "array.h" typedef filter_token filter_type_t; typedef struct filter_hook_t { - const char *name; - const char *value; + char *name; + char *value; } filter_hook_t; +ARRAY(filter_hook_t) typedef struct filter_params_t { - const char *name; - const char *value; + char *name; + char *value; } filter_params_t; +ARRAY(filter_params_t) typedef struct filter_t { char *name; filter_type_t type; - filter_hook_t *hooks; - filter_params_t *params; + A(filter_hook_t) hooks; + A(filter_params_t) params; void *data; } filter_t; +ARRAY(filter_t) + +#define FILTER_INIT { NULL, FTK_UNKNOWN, ARRAY_INIT, ARRAY_INIT, NULL } typedef const char *filter_result_t; typedef filter_result_t (*filter_runner_t)(const filter_t *filter, @@ -71,8 +77,15 @@ __attribute__((nonnull(1,4))) void filter_register(const char *type, filter_constructor_t constructor, filter_destructor_t destructor, filter_runner_t runner); +__attribute__((nonnull(1))) +static inline void filter_init(filter_t *filter) +{ + const filter_t f = FILTER_INIT; + *filter = f; +} + __attribute__((nonnull(1,2))) -bool filter_set_name(filter_t *filter, const char *name, ssize_t len); +void filter_set_name(filter_t *filter, const char *name, ssize_t len); __attribute__((nonnull(1,2))) bool filter_set_type(filter_t *filter, const char *type, ssize_t len); @@ -88,6 +101,20 @@ bool filter_add_hook(filter_t *filter, const char *name, ssize_t name_len, __attribute__((nonnull(1))) bool filter_build(filter_t *filter); +__attribute__((nonnull(1))) +static inline void filter_hook_wipe(filter_hook_t *hook) +{ + p_delete(&hook->name); + p_delete(&hook->value); +} + +__attribute__((nonnull(1))) +static inline void filter_params_wipe(filter_params_t *param) +{ + p_delete(¶m->name); + p_delete(¶m->value); +} + __attribute__((nonnull(1))) void filter_wipe(filter_t *filter); -- 2.20.1