Fix config string handling
This commit is contained in:
parent
b7d3f2ad0b
commit
ead52a0f47
3 changed files with 95 additions and 33 deletions
|
@ -15,25 +15,35 @@ typedef enum
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char *file;
|
char *file;
|
||||||
char database[256];
|
char *include;
|
||||||
char user[256];
|
char *database;
|
||||||
char group[256];
|
char *user;
|
||||||
|
char *group;
|
||||||
int log_level;
|
int log_level;
|
||||||
FILE *log_file;
|
FILE *log_file;
|
||||||
run_type_t run_type;
|
run_type_t run_type;
|
||||||
uint16_t server_port;
|
uint16_t server_port;
|
||||||
uint16_t discovery_port;
|
uint16_t discovery_port;
|
||||||
uint16_t mqtt_port;
|
uint16_t mqtt_port;
|
||||||
char content_dir[1024];
|
char *content_dir;
|
||||||
char not_found_file[256];
|
char *not_found_file;
|
||||||
char not_found_file_type[256];
|
char *not_found_file_type;
|
||||||
char not_found_content[256];
|
char *not_found_content;
|
||||||
char not_found_content_type[256];
|
char *not_found_content_type;
|
||||||
struct mg_serve_http_opts http_server_opts;
|
struct mg_serve_http_opts http_server_opts;
|
||||||
} config_t;
|
} config_t;
|
||||||
|
|
||||||
extern config_t global_config;
|
extern config_t global_config;
|
||||||
|
|
||||||
|
void
|
||||||
|
config_init();
|
||||||
|
|
||||||
|
void
|
||||||
|
config_free();
|
||||||
|
|
||||||
|
void
|
||||||
|
config_try_ini_files(config_t *config);
|
||||||
|
|
||||||
int
|
int
|
||||||
config_load(IniDispatch *disp, void *config_void);
|
config_load(IniDispatch *disp, void *config_void);
|
||||||
|
|
||||||
|
|
82
src/config.c
82
src/config.c
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
config_t global_config;
|
config_t global_config;
|
||||||
|
|
||||||
|
|
||||||
#define CONFINI_IS_KEY(SECTION, KEY) \
|
#define CONFINI_IS_KEY(SECTION, KEY) \
|
||||||
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
|
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
|
||||||
ini_string_match_ii(KEY, disp->data, disp->format))
|
ini_string_match_ii(KEY, disp->data, disp->format))
|
||||||
|
@ -76,6 +77,66 @@ config_load_log_file(IniDispatch *disp, config_t *config)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
config_load_string(char **holder, char *value)
|
||||||
|
{
|
||||||
|
if(*holder)
|
||||||
|
{
|
||||||
|
free(*holder);
|
||||||
|
}
|
||||||
|
size_t value_len = strlen(value);
|
||||||
|
|
||||||
|
char *new_holder = malloc(sizeof(char) * (value_len + 1));
|
||||||
|
strcpy(new_holder, value);
|
||||||
|
new_holder[value_len] = '\0';
|
||||||
|
|
||||||
|
*holder = new_holder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
config_init()
|
||||||
|
{
|
||||||
|
memset(&global_config, 0, sizeof(config_t));
|
||||||
|
|
||||||
|
global_config.discovery_port = 4421;
|
||||||
|
global_config.mqtt_port = 1885;
|
||||||
|
global_config.server_port = 5000;
|
||||||
|
|
||||||
|
global_config.log_level = LOG_INFO;
|
||||||
|
global_config.log_file = stdout;
|
||||||
|
|
||||||
|
global_config.user = NULL;
|
||||||
|
global_config.group = NULL;
|
||||||
|
|
||||||
|
config_load_string(&global_config.content_dir, ".");
|
||||||
|
config_load_string(&global_config.not_found_file, "404.html");
|
||||||
|
config_load_string(&global_config.not_found_file_type, "text/html");
|
||||||
|
config_load_string(&global_config.not_found_content, "404 - NOT FOUND");
|
||||||
|
config_load_string(&global_config.not_found_content_type, "text/plain");
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
config_free()
|
||||||
|
{
|
||||||
|
free(global_config.file);
|
||||||
|
free(global_config.include);
|
||||||
|
free(global_config.database);
|
||||||
|
free(global_config.user);
|
||||||
|
free(global_config.group);
|
||||||
|
free(global_config.content_dir);
|
||||||
|
free(global_config.not_found_file);
|
||||||
|
free(global_config.not_found_file_type);
|
||||||
|
free(global_config.not_found_content);
|
||||||
|
free(global_config.not_found_content_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
config_try_ini_files(config_t *config)
|
||||||
|
{
|
||||||
|
(void)config;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
config_load(IniDispatch *disp, void *config_void)
|
config_load(IniDispatch *disp, void *config_void)
|
||||||
{
|
{
|
||||||
|
@ -83,44 +144,49 @@ config_load(IniDispatch *disp, void *config_void)
|
||||||
|
|
||||||
if(disp->type == INI_KEY)
|
if(disp->type == INI_KEY)
|
||||||
{
|
{
|
||||||
|
if(CONFINI_IS_KEY("core", "include"))
|
||||||
|
{
|
||||||
|
config_load_string(&config->include, disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if(CONFINI_IS_KEY("core", "database"))
|
if(CONFINI_IS_KEY("core", "database"))
|
||||||
{
|
{
|
||||||
strcpy(config->database, disp->value);
|
config_load_string(&config->database, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "user"))
|
if(CONFINI_IS_KEY("core", "user"))
|
||||||
{
|
{
|
||||||
strcpy(config->user, disp->value);
|
config_load_string(&config->user, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "group"))
|
if(CONFINI_IS_KEY("core", "group"))
|
||||||
{
|
{
|
||||||
strcpy(config->group, disp->value);
|
config_load_string(&config->group, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "content-dir"))
|
if(CONFINI_IS_KEY("core", "content-dir"))
|
||||||
{
|
{
|
||||||
strcpy(config->content_dir, disp->value);
|
config_load_string(&config->content_dir, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "not-found-file"))
|
if(CONFINI_IS_KEY("core", "not-found-file"))
|
||||||
{
|
{
|
||||||
strcpy(config->not_found_file, disp->value);
|
config_load_string(&config->not_found_file, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "not-found-file-type"))
|
if(CONFINI_IS_KEY("core", "not-found-file-type"))
|
||||||
{
|
{
|
||||||
strcpy(config->not_found_file_type, disp->value);
|
config_load_string(&config->not_found_file_type, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "not-found-content"))
|
if(CONFINI_IS_KEY("core", "not-found-content"))
|
||||||
{
|
{
|
||||||
strcpy(config->not_found_content, disp->value);
|
config_load_string(&config->not_found_content, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "not-found-content-type"))
|
if(CONFINI_IS_KEY("core", "not-found-content-type"))
|
||||||
{
|
{
|
||||||
strcpy(config->not_found_content_type, disp->value);
|
config_load_string(&config->not_found_content_type, disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(CONFINI_IS_KEY("core", "log-level"))
|
if(CONFINI_IS_KEY("core", "log-level"))
|
||||||
|
|
20
src/main.c
20
src/main.c
|
@ -30,6 +30,7 @@ terminate(int signum)
|
||||||
router_free();
|
router_free();
|
||||||
status_free();
|
status_free();
|
||||||
cache_free();
|
cache_free();
|
||||||
|
config_free();
|
||||||
|
|
||||||
closelog();
|
closelog();
|
||||||
|
|
||||||
|
@ -56,22 +57,7 @@ main(int argc, const char** argv)
|
||||||
|
|
||||||
/******************** LOAD CONFIG ********************/
|
/******************** LOAD CONFIG ********************/
|
||||||
|
|
||||||
global_config.file = "core.ini";
|
config_init();
|
||||||
global_config.discovery_port = 4421;
|
|
||||||
global_config.mqtt_port = 1885;
|
|
||||||
global_config.server_port = 5000;
|
|
||||||
|
|
||||||
global_config.log_level = LOG_INFO;
|
|
||||||
global_config.log_file = stdout;
|
|
||||||
|
|
||||||
strcpy(global_config.user, "");
|
|
||||||
strcpy(global_config.group, "");
|
|
||||||
|
|
||||||
strcpy(global_config.content_dir, ".");
|
|
||||||
strcpy(global_config.not_found_file, "404.html");
|
|
||||||
strcpy(global_config.not_found_file_type, "text/html");
|
|
||||||
strcpy(global_config.not_found_content, "404 - NOT FOUND");
|
|
||||||
strcpy(global_config.not_found_content_type, "text/plain");
|
|
||||||
|
|
||||||
helper_parse_cli(argc, argv, &global_config);
|
helper_parse_cli(argc, argv, &global_config);
|
||||||
|
|
||||||
|
@ -81,7 +67,7 @@ main(int argc, const char** argv)
|
||||||
LOGGER_CRIT("config file '%s' was not found\n", global_config.file);
|
LOGGER_CRIT("config file '%s' was not found\n", global_config.file);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if(load_ini_file( ini_file, INI_DEFAULT_FORMAT, NULL, config_load, &global_config))
|
if(load_ini_file(ini_file, INI_DEFAULT_FORMAT, NULL, config_load, &global_config))
|
||||||
{
|
{
|
||||||
LOGGER_CRIT("unable to parse ini file\n");
|
LOGGER_CRIT("unable to parse ini file\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
Loading…
Reference in a new issue