src/server/config/initconf.c

changeset 418
b7dcc9c4f270
parent 415
d938228c382e
child 459
f21b4ff81c01
equal deleted inserted replaced
417:90805bb9fbd6 418:b7dcc9c4f270
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 32
33 #include "initconf.h" 33 #include "initconf.h"
34 34
35 InitConfig *load_init_config(char *file) { 35 InitConfig *initconfig_load(const char *file) {
36 FILE *in = fopen(file, "r"); 36 CxMempool *mp = cxBasicMempoolCreate(512);
37 if(in == NULL) { 37 if(!mp) {
38 return NULL; 38 return NULL;
39 } 39 }
40 40
41 InitConfig *conf = malloc(sizeof(InitConfig)); 41 // setup parser
42 conf->parser.parse = initconf_parse; 42 ConfigParser2 parser;
43 conf->file = file; 43 memset(&parser, 0, sizeof(ConfigParser2));
44 conf->directives = NULL; 44 parser.mp = mp;
45 45 parser.validateDirective = initconfig_validate_directive;
46 int r = cfg_parse_basic_file((ConfigParser*)conf, in); 46 parser.allow_hierarchy = 0;
47 if(r != 0) { 47
48 free_init_config(conf); 48 ConfigNode *init_config = serverconfig_load_file(&parser, file);
49 if(!init_config) {
50 cxMempoolDestroy(mp);
49 return NULL; 51 return NULL;
50 } 52 }
51 fclose(in); 53
54 InitConfig *conf = cxMalloc(mp->allocator, sizeof(InitConfig));
55 if(!conf) {
56 cxMempoolDestroy(mp);
57 return NULL;
58 }
59
60 conf->mp = mp;
61 conf->root = init_config;
52 62
53 return conf; 63 return conf;
54 } 64 }
55 65
56 void free_init_config(InitConfig *conf) { 66 int initconfig_validate_directive(ConfigParser2 *parser, ConfigNode *node) {
57 // TODO: fix 67 // check directive type
58 //ucx_mempool_destroy(conf->parser.mp->pool); 68 // currently only Init is supported
59 free(conf); 69 const char *types[] = { "Init" };
70 size_t typeindex;
71 if(serverconfig_validate_directive_name(node, types, 1, &typeindex)) {
72 return 1;
73 }
74
75 // init directives must have parameter names
76 ConfigParam *param_err;
77 if(serverconfig_check_param_names(node, &param_err)) {
78 return 1;
79 }
80
81 // check if the fn parameter exists
82 cxstring fn = serverconfig_directive_get_arg(node, cx_str("fn"));
83 if(fn.length == 0) {
84 return 1;
85 }
86
87 return 0;
60 } 88 }
61 89
62 int initconf_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) { 90 void initconfig_free(InitConfig *conf) {
63 InitConfig *conf = p; 91 cxMempoolDestroy(conf->mp);
64
65 // parse directive
66 ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp);
67 if(d == NULL) {
68 log_ereport(LOG_FAILURE, "initconf_parse: directive is null");
69 return 0;
70 }
71 d->begin = begin;
72 d->end = end;
73 if(d->type_num == INIT_DIRECTIVE) {
74 //conf->directives = ucx_list_append(conf->directives, d);
75 ConfigDirectiveList *dir_entry = cxMalloc(conf->parser.mp, sizeof(ConfigDirectiveList));
76 dir_entry->directive = d;
77 dir_entry->next = NULL;
78 CFG_DIRECTIVES_ADD(&conf->directives, dir_entry);
79 } else {
80 log_ereport(LOG_WARN, "Non Init directive in init.conf");
81 }
82
83 return 0;
84 } 92 }

mercurial