src/server/config/initconf.c

changeset 418
b7dcc9c4f270
parent 415
d938228c382e
child 459
f21b4ff81c01
--- a/src/server/config/initconf.c	Sun Nov 06 17:41:39 2022 +0100
+++ b/src/server/config/initconf.c	Mon Nov 07 17:59:44 2022 +0100
@@ -32,53 +32,61 @@
 
 #include "initconf.h"
 
-InitConfig *load_init_config(char *file) {
-    FILE *in = fopen(file, "r");
-    if(in == NULL) {
+InitConfig *initconfig_load(const char *file) {
+    CxMempool *mp = cxBasicMempoolCreate(512);
+    if(!mp) {
         return NULL;
     }
     
-    InitConfig *conf = malloc(sizeof(InitConfig));
-    conf->parser.parse = initconf_parse;
-    conf->file = file;
-    conf->directives = NULL;
-
-    int r = cfg_parse_basic_file((ConfigParser*)conf, in);
-    if(r != 0) {
-        free_init_config(conf);
+    // setup parser
+    ConfigParser2 parser;
+    memset(&parser, 0, sizeof(ConfigParser2));
+    parser.mp = mp;
+    parser.validateDirective = initconfig_validate_directive;
+    parser.allow_hierarchy = 0;
+    
+    ConfigNode *init_config = serverconfig_load_file(&parser, file);
+    if(!init_config) {
+        cxMempoolDestroy(mp);
         return NULL;
     }
-    fclose(in);
+    
+    InitConfig *conf = cxMalloc(mp->allocator, sizeof(InitConfig));
+    if(!conf) {
+        cxMempoolDestroy(mp);
+        return NULL;
+    }
+    
+    conf->mp = mp;
+    conf->root = init_config;
 
     return conf;
 }
 
-void free_init_config(InitConfig *conf) {
-    // TODO: fix
-    //ucx_mempool_destroy(conf->parser.mp->pool);
-    free(conf);
+int initconfig_validate_directive(ConfigParser2 *parser, ConfigNode *node) {
+    // check directive type
+    // currently only Init is supported
+    const char *types[] = { "Init" };
+    size_t typeindex;
+    if(serverconfig_validate_directive_name(node, types, 1, &typeindex)) {
+        return 1;
+    }
+    
+    // init directives must have parameter names
+    ConfigParam *param_err;
+    if(serverconfig_check_param_names(node, &param_err)) {
+        return 1;
+    }
+    
+    // check if the fn parameter exists
+    cxstring fn = serverconfig_directive_get_arg(node, cx_str("fn"));
+    if(fn.length == 0) {
+        return 1;
+    }
+    
+    return 0;
 }
 
-int initconf_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
-    InitConfig *conf = p;
-    
-    // parse directive
-    ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp);
-    if(d == NULL) {
-        log_ereport(LOG_FAILURE, "initconf_parse: directive is null");
-        return 0;
-    }
-    d->begin = begin;
-    d->end = end;
-    if(d->type_num == INIT_DIRECTIVE) {
-        //conf->directives = ucx_list_append(conf->directives, d);
-        ConfigDirectiveList *dir_entry = cxMalloc(conf->parser.mp, sizeof(ConfigDirectiveList));
-        dir_entry->directive = d;
-        dir_entry->next = NULL;
-        CFG_DIRECTIVES_ADD(&conf->directives, dir_entry);
-    } else {
-        log_ereport(LOG_WARN, "Non Init directive in init.conf");
-    }
-
-    return 0;
+void initconfig_free(InitConfig *conf) {
+    cxMempoolDestroy(conf->mp);
 }

mercurial