diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-18 23:29:26 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-18 23:29:26 +0100 |
| commit | ca2cf9265e2030a9098f18315e3addde7f518192 (patch) | |
| tree | 52c4663887ad50cea1bbc44484300bb2dafacc9a | |
| parent | 38637de243c569c0d6aaff455752e617c30c5866 (diff) | |
Refactor
| -rw-r--r-- | comet.conf | 2 | ||||
| -rw-r--r-- | src/comet.c | 13 | ||||
| -rw-r--r-- | src/config.c | 17 | ||||
| -rw-r--r-- | src/config.h | 4 | ||||
| -rw-r--r-- | src/format.c | 10 |
5 files changed, 24 insertions, 22 deletions
@@ -46,7 +46,7 @@ [block.ram] type = ram - text = RAM: %{used-percentage}% + text = RAM: %{used-percentage} % text-color = #fff color = #f0b diff --git a/src/comet.c b/src/comet.c index 1522bf7..d5eb03f 100644 --- a/src/comet.c +++ b/src/comet.c @@ -71,7 +71,11 @@ int main(int argc, char **argv) if (config_file != NULL) { log_info("Reading config '%s'", config_path); - config_read(&config, config_file); + int errors = config_read(&config, config_file); + if (errors > 0) { + log_error("Config file contained %d errors", errors); + return EXIT_FAILURE; + } } signal(SIGINT, signal_quit); @@ -96,7 +100,12 @@ int main(int argc, char **argv) .height = config.height, }; - block_t *block = config_resolve(&config); + block_t *block = NULL; + if (config_resolve(&config, &block) > 0) { + log_error("Config could not be resolved"); + return EXIT_FAILURE; + } + log_debug("Starting bar"); window_resize(&window, config.width, config.height); diff --git a/src/config.c b/src/config.c index 3f7da08..615c833 100644 --- a/src/config.c +++ b/src/config.c @@ -385,7 +385,7 @@ void config_init(config_t *config) bar->spacing = 10; } -void config_read(config_t *config, FILE *file) +int config_read(config_t *config, FILE *file) { any_ini_stream_t ini; any_ini_file_init(&ini, file); @@ -531,10 +531,7 @@ skip_pair: n_errors += errors; } while ((section = any_ini_stream_next_section(&ini)) != NULL); - if (n_errors > 0) { - log_error("Config file contained %d errors", n_errors); - exit(EXIT_FAILURE); - } + return n_errors; } bool config_resolve_children(config_t *config, block_t *block) @@ -595,7 +592,7 @@ error: return false; } -block_t *config_resolve(config_t *config) +int config_resolve(config_t *config, block_t **block) { int errors = 0; block_group_t *bar = (block_group_t *)config->blocks[0]; @@ -609,12 +606,8 @@ block_t *config_resolve(config_t *config) errors += !config_resolve_children(config, config->blocks[0]); } - if (errors > 0) { - log_error("Config could not be resolved"); - exit(EXIT_FAILURE); - } - - return config->blocks[0]; + *block = config->blocks[0]; + return errors; } void config_free(config_t *config) diff --git a/src/config.h b/src/config.h index fef2cba..e65dbcf 100644 --- a/src/config.h +++ b/src/config.h @@ -45,11 +45,11 @@ typedef struct { void config_init(config_t *config); -void config_read(config_t *config, FILE *file); +int config_read(config_t *config, FILE *file); bool config_resolve_children(config_t *config, block_t *block); -block_t *config_resolve(config_t *config); +int config_resolve(config_t *config, block_t **block); void config_free(config_t *config); diff --git a/src/format.c b/src/format.c index 4657359..8f42c00 100644 --- a/src/format.c +++ b/src/format.c @@ -7,13 +7,13 @@ #include "any_log.h" #include "util.h" -#define format_grow() \ +#define format_grow(need) \ do { \ if (n + 1 >= length) { \ - length += 4; \ + length += (need); \ parts = realloc(parts, length * sizeof(char *)); \ assert(parts != NULL); \ - marks = realloc(marks, length * sizeof(bool)); \ + marks = realloc(marks, length * sizeof(uint8_t)); \ assert(marks != NULL); \ } \ } while (0) @@ -31,7 +31,7 @@ bool format_init(format_t *format, const char *string, char delim) // TODO: Escape formatting if (string[end + 1] == '{') { - format_grow(); + format_grow(2); if (start != end) { parts[n] = strslice(string, start, end); @@ -74,7 +74,7 @@ next: if (start != end || string[end] != '\0') { while (string[end] != '\0') end++; - format_grow(); + format_grow(1); parts[n] = strslice(string, start, end); marks[n] = false; n++; |
