#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;
};
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;
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);
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:
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);
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);
}
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)
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;
}
#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,
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);
__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);