2025-01-16 07:46:27 +03:00
|
|
|
#include <ctorm/ctorm.h>
|
|
|
|
|
2025-01-10 00:16:06 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2025-01-16 07:46:27 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2025-01-10 00:16:06 +03:00
|
|
|
#include <errno.h>
|
2025-01-16 07:46:27 +03:00
|
|
|
|
2025-01-10 00:16:06 +03:00
|
|
|
#include "config.h"
|
2025-01-16 07:46:27 +03:00
|
|
|
#include "util.h"
|
2025-01-10 00:16:06 +03:00
|
|
|
|
|
|
|
option_t options[] = {
|
2025-01-16 07:46:27 +03:00
|
|
|
{"host", "0.0.0.0:7003", true }, // host the server should listen on
|
|
|
|
{"docs_dir", "./docs", true }, // documentation directory
|
|
|
|
{"", NULL, false},
|
2025-01-10 00:16:06 +03:00
|
|
|
};
|
|
|
|
|
2025-01-16 07:46:27 +03:00
|
|
|
bool config_load(config_t *conf) {
|
2025-01-10 00:16:06 +03:00
|
|
|
bzero(conf, sizeof(*conf));
|
|
|
|
|
2025-01-16 07:46:27 +03:00
|
|
|
char name_env[OPT_NAME_MAX + 5], name_copy[OPT_NAME_MAX], *value = NULL;
|
2025-01-10 00:16:06 +03:00
|
|
|
conf->options = options;
|
|
|
|
|
2025-01-16 07:46:27 +03:00
|
|
|
for (option_t *opt = conf->options; opt->value != NULL; opt++, conf->count++) {
|
|
|
|
strcpy(name_copy, opt->name);
|
|
|
|
util_toupper(name_copy);
|
|
|
|
snprintf(name_env, sizeof(name_env), "DOC_%s", name_copy);
|
|
|
|
|
|
|
|
if ((value = getenv(name_env)) != NULL)
|
2025-01-10 00:16:06 +03:00
|
|
|
opt->value = value;
|
|
|
|
|
2025-01-16 07:46:27 +03:00
|
|
|
if (*opt->value == 0)
|
|
|
|
opt->value = NULL;
|
|
|
|
|
|
|
|
if (!opt->required || NULL != opt->value)
|
|
|
|
continue;
|
2025-01-10 00:16:06 +03:00
|
|
|
|
2025-01-16 07:46:27 +03:00
|
|
|
ctorm_fail("please specify a value for the required config option: %s (%s)", opt->name, name_env);
|
|
|
|
errno = EFAULT;
|
|
|
|
return false;
|
2025-01-10 00:16:06 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 07:46:27 +03:00
|
|
|
return true;
|
2025-01-10 00:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *config_get(config_t *conf, const char *name) {
|
|
|
|
for (int32_t i = 0; i < conf->count; i++)
|
|
|
|
if (strcmp(conf->options[i].name, name) == 0)
|
|
|
|
return conf->options[i].value;
|
|
|
|
return NULL;
|
|
|
|
}
|