From d13fe587519626f0b5501dbac0b40de9dad4c513 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Fri, 11 Mar 2016 21:16:41 +1000 Subject: [PATCH] * config process includes now --- src/backend-test.c | 2 +- src/config.c | 19 +++++++++++++++++-- src/config.h | 2 +- src/main.c | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/backend-test.c b/src/backend-test.c index f44c29a..820250a 100644 --- a/src/backend-test.c +++ b/src/backend-test.c @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) { usage(); memset(&config, 0x0, sizeof(config)); - if (f2b_config_load(&config, argv[1]) != true) { + if (f2b_config_load(&config, argv[1], false) != true) { f2b_log_msg(log_error, "can't load config"); return EXIT_FAILURE; } diff --git a/src/config.c b/src/config.c index ceeb68a..ff1f497 100644 --- a/src/config.c +++ b/src/config.c @@ -2,6 +2,8 @@ #include "config.h" #include "log.h" +#include + f2b_config_param_t * f2b_config_param_create(const char *src) { f2b_config_param_t *param = NULL; @@ -164,7 +166,7 @@ f2b_config_section_find(f2b_config_section_t *section, const char *name) { } bool -f2b_config_load(f2b_config_t *config, const char *path) { +f2b_config_load(f2b_config_t *config, const char *path, bool recursion) { f2b_config_section_t *section = NULL; /* always points to current section */ f2b_config_param_t *param = NULL; /* temp pointer */ FILE *f = NULL; /* config file fd */ @@ -233,7 +235,20 @@ f2b_config_load(f2b_config_t *config, const char *path) { } /* while */ fclose(f); - /* TODO: process includes */ + if (recursion && config->main && (param = f2b_config_param_find(config->main->param, "includes"))) { + char pattern[PATH_MAX] = ""; + glob_t globbuf; + snprintf(pattern, sizeof(pattern), "%s/*.conf", param->value); + if (glob(pattern, 0, NULL, &globbuf) != 0) { + f2b_log_msg(log_error, "glob on 'includes' dir failed"); + return false; + } + for (size_t i = 0; i < globbuf.gl_pathc; i++) { + f2b_config_load(config, globbuf.gl_pathv[i], false); + /* TODO: includes processing are not fatal? hmm... good question */ + } + globfree(&globbuf); + } return true; } diff --git a/src/config.h b/src/config.h index 64545c1..6c367c3 100644 --- a/src/config.h +++ b/src/config.h @@ -45,6 +45,6 @@ f2b_config_section_t * f2b_config_section_create(const char *line); f2b_config_section_t * f2b_config_section_find (f2b_config_section_t *s, const char *name); f2b_config_section_t * f2b_config_section_append(f2b_config_t *c, f2b_config_section_t *s); -bool f2b_config_load(f2b_config_t *c, const char *path); +bool f2b_config_load(f2b_config_t *c, const char *path, bool recursion); void f2b_config_free(f2b_config_t *c); #endif /* CONFIG_H_ */ diff --git a/src/main.c b/src/main.c index ff6c8d4..2fd02c1 100644 --- a/src/main.c +++ b/src/main.c @@ -64,7 +64,7 @@ int main(int argc, char *argv[]) { if (!config_file) usage(EXIT_FAILURE); memset(&config, 0x0, sizeof(config)); - if (f2b_config_load(&config, config_file) != true) { + if (f2b_config_load(&config, config_file, true) != true) { f2b_log_msg(log_error, "can't load config from '%s'", config_file); return EXIT_FAILURE; }