#include #include #include #include #include #include "any_log.h" #include "config.h" #include "util.h" #define ANY_INI_IMPLEMENT #include "any_ini.h" typedef enum { CONFIG_STRING, CONFIG_INT, CONFIG_UINT, CONFIG_DOUBLE, CONFIG_BOOL, } config_type_t; typedef struct { const char *key; config_type_t type; size_t offset; } config_entry_t; static const config_entry_t config_entries[] = { { "width", CONFIG_UINT, offsetof(config_t, width) }, { "height", CONFIG_UINT, offsetof(config_t, height) }, { "font", CONFIG_STRING, offsetof(config_t, font) }, { "monitor", CONFIG_STRING, offsetof(config_t, monitor) }, { "override_redirect", CONFIG_BOOL, offsetof(config_t, override_redirect) }, }; static bool config_read_string(const char *value, char **result) { if (value == NULL) value = ""; size_t start = 0, end = strlen(value); if (value[0] == '"' && value[end - 1] == '"') { start++; end--; } free(*result); *result = strslice(value, start, end); return true; } static bool config_read_int(const char *value, int *result) { char *end; long n = strtol(value, &end, 0); *result = n; if (n > INT_MAX || n < INT_MIN) log_debug("Integer %ld is out of range", n); return *end == '\0' && n != LONG_MIN && n != LONG_MAX; } static bool config_read_uint(const char *value, unsigned int *result) { char *end; unsigned long n = strtoul(value, &end, 0); *result = n; if (n > UINT_MAX) log_debug("Integer %lu is out of range", n); return *end == '\0' && n != ULONG_MAX; } static bool config_read_bool(const char *value, bool *result) { size_t lenght = strlen(value); if (lenght == 5 && !strcmp(value, "false")) { *result = false; return true; } if (lenght == 4 && !strcmp(value, "true")) { *result = true; return true; } log_debug("Invalid bool value"); return false; } static bool config_read_double(const char *value, double *result) { char *end; *result = strtod(value, &end); return *end == '\0'; } static bool config_entry(config_t *config, int line, const char *section, const char *key, const char *value) { const int n_entries = sizeof(config_entries) / sizeof(config_entry_t); const char *types[] = { "string", "integer", "positive integer", "double float", }; for (int i = 0; i < n_entries; i++) { if (!strcmp(key, config_entries[i].key)) { bool nullable = config_entries[i].type == CONFIG_STRING; if ((value == NULL || *value == '\0') && !nullable) { log_value_error("Empty config entry", "s:section", section, "s:key", key, "s:expected_type", types[config_entries[i].type], "i:line", line); return false; } void *result = (uint8_t *)config + config_entries[i].offset; switch (config_entries[i].type) { case CONFIG_STRING: // NOTE: The previous string is freed by config_read_string if (config_read_string(value, (char **)result)) { log_debug("Config '%s' set to '%s'", key, *(char **)result); return true; } break; case CONFIG_INT: if (config_read_int(value, (int *)result)) { log_debug("Config '%s' set to '%d'", key, *(int *)result); return true; } break; case CONFIG_UINT: if (config_read_uint(value, (unsigned int *)result)) { log_debug("Config '%s' set to '%u'", key, *(unsigned int *)result); return true; } break; case CONFIG_DOUBLE: if (config_read_double(value, (double *)result)) { log_debug("Config '%s' set to '%lf'", key, *(double *)result); return true; } break; case CONFIG_BOOL: if (config_read_bool(value, (bool *)result)) { log_debug("Config '%s' set to '%s'", key, *(bool *)result ? "true" : "false"); return true; } break; default: log_panic("Unreachable"); } log_value_error("Invalid config entry", "s:section", section, "s:key", key, "s:value", value, "s:expected_type", types[config_entries[i].type], "i:line", line); return false; } } log_value_warn("Unknown config entry", "s:section", section, "s:key", key, "s:value", value, "i:line", line); return true; } void config_init(config_t *config) { const config_t config_default = { .block = { .label = "main_block", .color = color_rgb(100, 100, 100), .type = BLOCK_GROUP, }, .font = "monospace 10", .monitor = NULL, .height = 50, .width = 100, .override_redirect = false, }; memcpy(config, &config_default, sizeof(config_t)); block_copy(&config->block, &config_default.block); config->font = strcopy(config_default.font); config->monitor = strcopy(config_default.monitor); } void config_read(config_t *config, FILE *file) { any_ini_stream_t ini; any_ini_file_init(&ini, file); int errors = 0; char *section = NULL; do { bool unknown_section = section != NULL && strcmp(section, "bar"); if (unknown_section) log_warn("Unknown section '%s'", section); char *key; while ((key = any_ini_stream_next_key(&ini)) != NULL) { char *value = any_ini_stream_next_value(&ini); log_value_trace("Reading config entry", "s:section", section, "s:key", key, "s:value", value, "i:line", ini.line); if (!unknown_section) errors += !config_entry(config, ini.line, section, key, value); free(key); free(value); } free(section); } while ((section = any_ini_stream_next_section(&ini)) != NULL); if (errors > 0) log_panic("Config file contained %d errors", errors); } void config_free(config_t *config) { free(config->font); free(config->monitor); block_free(&config->block); }