summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-01 12:06:18 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-01 12:06:18 -0300
commite7514d7c60a0f54f0c79695ccdf8fb6a6e871332 (patch)
tree0a0231be5d3564e9686e71728c3afb249a52b633 /config.c
parent80585e40cba367159789fd4c52123288068510b4 (diff)
config: Use `parse_key_value_file()`, rather than doing it manually in
`parse_config()` This greatly simplifies the code, and makes it more readable.
Diffstat (limited to 'config.c')
-rw-r--r--config.c29
1 files changed, 11 insertions, 18 deletions
diff --git a/config.c b/config.c
index 30e3e95..559fc55 100644
--- a/config.c
+++ b/config.c
@@ -20,9 +20,11 @@ void cleanup_config(config *conf) {
}
int check_port(void *ctx, void *ret, const keyword *key, keyword_val val) {
+ int *error = (int *)ctx;
if (key->type == TYPE_STRING) {
int port = strtol(val.str, NULL, 0);
if (port > 0 || port <= 65535) {
+ *error = 0;
return 0;
} else {
log(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", port);
@@ -30,10 +32,13 @@ int check_port(void *ctx, void *ret, const keyword *key, keyword_val val) {
} else {
log(LOG_ERR, "Keyword \"%s\" doesn't return a string.", key->key);
}
+ *error = 1;
return -1;
}
config *parse_config(const char *filename) {
+ int error = 0;
+ int type_error = 0;
/* Size of the file, in bytes. */
long filesize = 0;
/* Config settings. */
@@ -55,25 +60,13 @@ config *parse_config(const char *filename) {
return NULL;
}
- while (*tmp != '\0') {
- char *line = get_line(&tmp);
- char *value;
- char *name = strtok_r(line, "=", &value);
- int error;
-
- /* Did we fail to parse the config option? */
- if (error = parse_keywords(config_keywords, name, value, cfg, NULL)) {
- if (error == 5) {
- cleanup_config(cfg);
- free(line);
- free(buf);
- return NULL;
- } else {
- log(LOG_WARNING, "Failed to parse config option \"%s\". Error code: %i", name, error);
- }
+ /* Did we fail to parse the config file? */
+ if (error = parse_key_value_file(cfg, &type_error, config_keywords, buf, "=", NULL)) {
+ if (type_error || (error >= 3 && error != 6)) {
+ cleanup_config(cfg);
+ free(buf);
+ return NULL;
}
-
- free(line);
}
free(buf);