diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-27 23:19:38 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-27 23:19:38 +0100 |
| commit | 6ab3b52fbde3ab2ac11e66e4d664f67e108f6aee (patch) | |
| tree | 838f5231fffc6cacdd65762443fd56bbe0f275f6 /src/config.c | |
| parent | 2582279bc1046954702311c300294c5fd9f3ae0c (diff) | |
Rework resolve functions
Diffstat (limited to 'src/config.c')
| -rw-r--r-- | src/config.c | 133 |
1 files changed, 35 insertions, 98 deletions
diff --git a/src/config.c b/src/config.c index c8b63b4..e7c47d7 100644 --- a/src/config.c +++ b/src/config.c @@ -205,8 +205,8 @@ static bool config_read_color(const char *value, color_t *result) static bool config_read_gradient(const char *value, gradient_t *result) { - // TODO: Ensure that this is correct gradient_free(result); + memset(result, 0, sizeof(gradient_t)); size_t count = 0; for (size_t i = 0; value[i] != '\0'; ++i) @@ -235,15 +235,15 @@ static bool config_read_gradient(const char *value, gradient_t *result) } free(copy); - result->colors = colors; - result->length = n; - if (n == 0) { free(colors); log_debug("Expected at least one color"); return false; } + result->colors = colors; + result->length = n; + if (n == 1) { result->pattern = cairo_pattern_create_rgba(colors->r, colors->g, colors->b, colors->a); return true; @@ -289,8 +289,8 @@ static bool config_read_time(const char *value, struct timespec *result) // TODO: Handle quotation marks static bool config_read_list(const char *value, char ***result) { - // TODO: Ensure that this is correct - strfree(*result); + strfreelist(*result); + *result = NULL; size_t count = 0; for (size_t i = 0; value[i] != '\0'; ++i) @@ -312,9 +312,9 @@ static bool config_read_list(const char *value, char ***result) *end = '\0'; if (!config_read_string(token, &list[n++])) { - free(copy); list[n] = NULL; - strfree(list); + strfreelist(list); + free(copy); return false; } } @@ -716,6 +716,12 @@ skip_pair: int config_validate(config_t *config) { int errors = 0; + block_group_t *bar = (block_group_t *)config->blocks[0]; + + if (bar->children == NULL) { + log_error("Section '%s' requires at least one child", "bar"); + errors++; + } // Validate the config itself if (config->scale < 1 && config->scale != 0) { @@ -737,7 +743,6 @@ int config_validate(config_t *config) } // Validate actions - // NOTE: Maybe move this after the config is resolved? // for (size_t i = 0; i < config->n_actions; i++) { action_t *action = &config->actions[i]; @@ -748,109 +753,41 @@ int config_validate(config_t *config) return errors; } -bool config_resolve_action(config_t *config, block_t *block) +bool config_resolve_action(config_t *config, const char *label, action_t **action) { - action_t *actions[EVENT_MAX] = { 0 }; - - for (size_t i = 0; i < EVENT_MAX; i++) { - char *label = (char *)block->actions[i]; - - if (label == NULL) - continue; - - for (size_t j = 0; j < config->n_actions; j++) { - if (!strcmp(label, config->actions[j].label)) { - log_debug("Block '%s' uses action '%s' for %s", block->label, label, event_type_to_string(i)); - - actions[i] = &config->actions[j]; - free(label); - goto next; - } + for (size_t j = 0; j < config->n_actions; j++) { + if (!strcmp(label, config->actions[j].label)) { + *action = &config->actions[j]; + return true; } - - log_error("Action '%s' not found (referenced by '%s')", label, block->label); - return false; -next: } - memcpy(block->actions, actions, sizeof(actions)); - return true; + log_error("Action '%s' not found", label); + return false; } -bool config_resolve_children(config_t *config, block_t *block) +bool config_resolve(config_t *config, block_t **block) { - block->resolved = true; - - if (!config_resolve_action(config, block)) - return false; - - if (block->type != BLOCK_GROUP) - return true; - - assert(block->type == BLOCK_GROUP); - block_group_t *group = (block_group_t *)block; - - char **children = (char **)group->children; - group->children = NULL; - group->n_children = 0; - - if (children == NULL) - return true; - - while (children[group->n_children] != NULL) - group->n_children++; - - group->children = malloc(group->n_children * sizeof(block_t *)); - - for (size_t i = 0; i < group->n_children; i++) { - for (size_t j = 0; j < config->n_blocks; j++) { - if (config->blocks[j] == block) - continue; - - if (!strcmp(children[i], config->blocks[j]->label)) { - if (config->blocks[j]->resolved) { - log_error("Block '%s' can only be referenced by one block", children[i]); - goto error; - } - - log_debug("Block '%s' is parent of '%s'", block->label, children[i]); - group->children[i] = config->blocks[j]; - - if (!config_resolve_children(config, config->blocks[j])) - goto error; - goto next; - } - } + block_group_t *bar = (block_group_t *)config->blocks[0]; + assert(bar->children != NULL); - log_error("Block '%s' not found (referenced by '%s')", children[i], block->label); - goto error; -next: + for (size_t i = 0; i < config->n_actions; i++) { + if (!action_resolve(&config->actions[i], config)) + return false; } - strfree(children); - return true; - -error: - strfree(children); - return false; -} - -int config_resolve(config_t *config, block_t **block) -{ - int errors = 0; - block_group_t *bar = (block_group_t *)config->blocks[0]; + log_debug("Resolved %zu actions", config->n_actions); - if (bar->children == NULL) { - errors++; - log_error("Section '%s' requires at least one child", "bar"); - } else { - // NOTE: The first item is the main block - bar->block.min_width = bar->block.max_width = config->width; - errors += !config_resolve_children(config, config->blocks[0]); + for (size_t i = 0; i < config->n_blocks; i++) { + if (!block_resolve(config->blocks[i], config)) + return false; } + log_debug("Resolved %zu blocks", config->n_blocks); + *block = config->blocks[0]; - return errors; + bar->block.min_width = bar->block.max_width = config->width; + return true; } void config_free(config_t *config) |
