X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd%2Fconfig.c;h=ec634804aa1240eb1b8ddfda2568f1ab2dab5f3c;hb=5fc57f7c291b99db643dc22a814087b34b9f4b59;hp=82f80f000ccdc037235b6a1c8f1aeb7c3c544f1f;hpb=6dcf855b7add63be72fbed3af6590f00532e1c76;p=apps%2Fpfixtools.git diff --git a/postlicyd/config.c b/postlicyd/config.c index 82f80f0..ec63480 100644 --- a/postlicyd/config.c +++ b/postlicyd/config.c @@ -34,16 +34,12 @@ */ #include "file.h" -#include "filter.h" #include "config.h" +#include "str.h" -struct config_t { - filter_t *filters; - int filters_len; - int filters_size; +#define config_param_register(Param) - int entry_point; -}; +config_param_register("first_filter"); static inline config_t *config_new(void) { @@ -55,17 +51,55 @@ 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); + array_deep_wipe((*config)->filters, filter_wipe); + array_deep_wipe((*config)->params, filter_params_wipe); + p_delete(config); + } +} + + +static bool config_second_pass(config_t *config) +{ + bool ok = true; + if (config->filters.len > 0) { +# define QSORT_TYPE filter_t +# define QSORT_BASE config->filters.data +# define QSORT_NELT config->filters.len +# define QSORT_LT(a,b) strcmp(a->name, b->name) < 0 +# include "qsort.c" + } + + foreach (filter_t *filter, config->filters) { + if (!filter_update_references(filter, &config->filters)) { + ok = false; + break; } - p_delete(&(*config)->filters); + }} + + config->entry_point = -1; + foreach (filter_param_t *param, config->params) { + switch (param->type) { + case ATK_FIRST_FILTER: + config->entry_point = filter_find_with_name(&config->filters, + param->value); + break; + default: break; + } + }} + array_deep_wipe(config->params, filter_params_wipe); + + if (config->entry_point == -1) { + ok = false; + syslog(LOG_ERR, "no entry point defined"); } + + return ok; } 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; @@ -80,6 +114,7 @@ config_t *config_read(const char *file) } config = config_new(); + filter_init(&filter); linep = p = map.map; #define READ_ERROR(Fmt, ...) \ @@ -212,13 +247,18 @@ read_section: read_param_value: READ_BLANK(goto badeof); READ_STRING("parameter value", value, value_len, ;); - /* TODO: Insert parameter in the configuration. - */ + { + filter_param_t param; + param.type = param_tokenize(key, key_len); + if (param.type != ATK_UNKNOWN) { + param.value = m_strdup(value); + array_add(config->params, param); + } + } goto read_section; read_filter: - /* TODO: Create a filter with the given name. - */ + filter_set_name(&filter, key, key_len); READ_BLANK(goto badeof); while (*p != '}') { READ_TOKEN("filter parameter name", key, key_len); @@ -230,21 +270,45 @@ 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 { + /* filter_add_param failure mean unknown type or unsupported type. + * this are non-fatal errors. + */ + (void)filter_add_param(&filter, key, key_len, value, value_len); + } } READ_NEXT(;); - /* TODO: Check the filter. - */ + if (!filter_build(&filter)) { + READ_ERROR("invalid filter %s", filter.name); + } + array_add(config->filters, filter); + filter_init(&filter); goto read_section; ok: + if (!config_second_pass(config)) { + goto error; + } + file_map_close(&map); return config; badeof: syslog(LOG_ERR, "Unexpected end of file"); error: + filter_wipe(&filter); config_delete(&config); + file_map_close(&map); return NULL; }