#ifndef INCLUDED_CONFIG_H #define INCLUDED_CONFIG_H #define CONFIG_FILENAME(sym, ...) struct filename sym; #define CONFIG_UINT( sym, ...) unsigned int sym; struct config { CONFIG }; #undef CONFIG_FILENAME #undef CONFIG_UINT static bool parse_filename (const char **head, const char *end, const char *key, size_t key_len, const char *name, struct filename *filename) { if (strncmp (key, name, key_len)) return false; while (strchr (" \t", **head)) { *head += 1; if (*head == end) { WARNING ("Unexpected EOF while looking for '='"); return false; } } if (**head != '=') { WARNING ("Expected '='"); return false; } *head += 1; if (*head == end) { WARNING ("Unexpected EOF after '='"); return false; } while (strchr (" \t", **head)) { *head += 1; if (*head == end) { WARNING ("Unexpected EOF while looking for value"); return false; } } const char *value = *head; while (!strchr (" \t\n\0", **head)) { *head += 1; if (*head == end) { WARNING ("unexpected EOF while parsing value"); return false; } } size_t value_len = *head - value; filename->count = 0; DYNAMIC_ARRAY_APPEND_MANY (*filename, value, value_len); DYNAMIC_ARRAY_APPEND (*filename, '\0'); DEBUG ("Parsed filename `%.*s' from config: %s", (int) key_len, key, filename->items); return true; } static bool parse_uint (const char **head, const char *end, const char *key, size_t key_len, const char *name, unsigned int *uint) { if (strncmp (key, name, key_len)) return false; while (strchr (" \t", **head)) { *head += 1; if (*head == end) { WARNING ("unexpected EOF while looking for '='"); return false; } } if (**head != '=') { WARNING ("expected '='"); } *head += 1; if (*head == end) { WARNING ("unexpected EOF while looking for value"); return false; } while (strchr (" \t", **head)) { *head += 1; if (*head == end) { WARNING ("unexpected EOF while looking for value"); return false; } } int value_len = 0; if (sscanf (*head, "%u%n", uint, &value_len) == 0) { LOG (ERR, "unsigned int"); return false; } *head += value_len; DEBUG ("Parsed uint `%.*s' from config: %u", (int) key_len, key, *uint); return true; } bool check_config (struct config *); bool read_config (const char *filename, struct config *config) { bool success = false; char *contents = NULL; int fd = -1; #define CONFIG_FILENAME(SYM, KEY, INITIAL) { \ DYNAMIC_ARRAY_RESET (config->SYM); \ if (INITIAL) \ DYNAMIC_ARRAY_APPEND_MANY (config->SYM, INITIAL, sizeof (INITIAL)); \ else DYNAMIC_ARRAY_APPEND (config->SYM, '\0'); \ } #define CONFIG_UINT(SYM, KEY, INITIAL) config->SYM = INITIAL; CONFIG #undef CONFIG_FILENAME #undef CONFIG_UINT fd = open (filename, O_RDONLY); if (fd == -1) { ERROR ("Unable to open config file `%s' for reading: %s", filename, strerror (errno)); goto defer; } DEBUG ("Opened config file `%s' for reading", filename); struct stat st; if (fstat (fd, &st) == -1) { ERROR ("Unable to fstat config file `%s': %s", filename, strerror (errno)); goto defer; } DEBUG ("Config file size: %lu", st.st_size); contents = malloc (st.st_size + 1); contents[st.st_size] = '\0'; if (read (fd, contents, st.st_size) == -1) { ERROR ("Unable to read config from `%s': %s", filename, strerror (errno)); goto defer; } close (fd); fd = -1; DEBUG ("Read config file into buffer"); const char *end = contents + st.st_size; for (const char *head = contents; head < end; ++head) { if (strchr (" \t\n", *head)) continue; const char *key = head; while (!strchr ("= \t", *head)) { head += 1; if (head == end) { ERROR ("Unexpected EOF while reading key from config"); goto defer; } } size_t key_len = head - key; #define PARSE(parse, sym, name) \ if (parse (&head, end, key, key_len, name, &config->sym)) continue; #define CONFIG_FILENAME(sym, name, initial) PARSE (parse_filename, sym, name) #define CONFIG_UINT( sym, name, initial) PARSE (parse_uint, sym, name) CONFIG; #undef PARSE #undef CONFIG_FILENAME #undef CONFIG_UINT ERROR ("Unknown config key: %.*s", (int) key_len, key); goto defer; } success = true; defer: if (fd == -1) close (fd); if (contents) free (contents); return success && check_config (config); } #endif // INCLUDED_CONFIG_H