#include #include #include #include #include #include "config.h" #include "macros.h" #include "misc.h" void cleanup_config(config *conf) { for (int i = 0; config_keywords[i] != NULL; ++i) { /* Get the pointer to the config option. */ void *cfg = get_keyword_offset_ptr(config_keywords[i], conf); /* Is this config option's type a string, and is the string of that config option in conf not NULL? */ if (config_keywords[i]->type == TYPE_STRING && *(void **)cfg != NULL) { free(*(void **)cfg); } } free(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); } } 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. */ config *cfg; /* Buffer of the file contents. */ char *buf = read_file(filename, &filesize); char *tmp = buf; /* Return NULL, if the buffer wasn't allocated */ if (buf == NULL) { return NULL; } cfg = calloc(1, sizeof(config)); /* Return NULL, if cfg wasn't allocated. */ if (cfg == NULL) { free(buf); return NULL; } /* 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 > 7)) { cleanup_config(cfg); free(buf); return NULL; } } free(buf); return cfg; }