diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-12 01:44:14 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-12 01:44:14 +0100 |
| commit | e85ad7414c063e42296f7ef4e202829567484ac0 (patch) | |
| tree | 25454de6b83a2dd91142c97b4a28add01331b3ea /src/blocks | |
| parent | 686802c2a472746a1b40fab30cbb0351e536c9ba (diff) | |
Fix config and blocks
Diffstat (limited to 'src/blocks')
| -rw-r--r-- | src/blocks/date.c | 34 | ||||
| -rw-r--r-- | src/blocks/fs.c | 50 | ||||
| -rw-r--r-- | src/blocks/group.c | 10 | ||||
| -rw-r--r-- | src/blocks/ram.c | 36 | ||||
| -rw-r--r-- | src/blocks/scheme.h | 1 | ||||
| -rw-r--r-- | src/blocks/text.c | 13 |
6 files changed, 92 insertions, 52 deletions
diff --git a/src/blocks/date.c b/src/blocks/date.c index 01c62c3..93747c3 100644 --- a/src/blocks/date.c +++ b/src/blocks/date.c @@ -6,33 +6,43 @@ #include "../any_log.h" +typedef struct { + block_text_t block; + char *format; +} block_date_t; + static void block_date_update(block_t *block) { + block_date_t *date = (block_date_t *)block; + time_t epoch = time(NULL); struct tm *loc_time = localtime(&epoch); char buffer[64]; - strftime(buffer, sizeof(buffer), block->state, loc_time); + strftime(buffer, sizeof(buffer), date->format, loc_time); - free(block->text.text); - block->text.text = strcopy(buffer); - assert(block->text.text != NULL); + free(date->block.text); + date->block.text = strcopy(buffer); + assert(date->block.text != NULL); } -static void block_date_finalize(block_t *block) +static void block_date_clean(block_t *block) { - free(block->state); + block_date_t *date = (block_date_t *)block; + free(date->format); } static bool block_date_validate(block_t *block, const block_scheme_t *scheme) { - if (block->text.text == NULL) { + block_date_t *date = (block_date_t *)block; + + if (date->block.text == NULL) { log_error("Block '%s' requires key '%s'", block->label, "text"); return false; } - block->state = block->text.text; - block->text.text = NULL; + date->format = date->block.text; + date->block.text = NULL; return true; } @@ -44,10 +54,10 @@ const block_scheme_t block_date_scheme = { .tv_sec = 1, .tv_nsec = 0, }, - .update_cb = block_date_update, - .finalize_cb = block_date_finalize, + .update_fn = block_date_update, + .clean_fn = block_date_clean, }, - .size = sizeof(char *), + .size = sizeof(block_date_t), .entries = NULL, .validate = block_date_validate, }; diff --git a/src/blocks/fs.c b/src/blocks/fs.c index 65e8fe2..9bb4452 100644 --- a/src/blocks/fs.c +++ b/src/blocks/fs.c @@ -10,13 +10,18 @@ #include "../any_log.h" +typedef struct { + block_text_t block; + char *format; + char *path; +} block_fs_t; + static void block_fs_update(block_t *block) { - const char *path = ((char **)block->state)[0]; - const char *text = ((char **)block->state)[1]; + block_fs_t *fs = (block_fs_t *)block; struct statvfs sbuf; - assert(statvfs(path, &sbuf) == 0); + assert(statvfs(fs->path, &sbuf) == 0); static const char *fs_formats[] = { "total", @@ -38,47 +43,48 @@ static void block_fs_update(block_t *block) buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], NULL, }; - free(block->text.text); - block->text.text = strformat(text, '%', fs_formats, fs_values); - assert(block->text.text != NULL); + free(fs->block.text); + fs->block.text = strformat(fs->format, '%', fs_formats, fs_values); + assert(fs->block.text != NULL); } -static void block_fs_finalize(block_t *block) +static void block_fs_clean(block_t *block) { - free(block->state); + block_fs_t *fs = (block_fs_t *)block; + free(fs->format); + free(fs->path); } static bool block_fs_validate(block_t *block, const block_scheme_t *scheme) { - if (block->text.text == NULL) { + block_fs_t *fs = (block_fs_t *)block; + + if (fs->block.text == NULL) { log_error("Block '%s' requires key '%s'", block->label, "text"); return false; } - if (strstr(block->text.text, "%{") == NULL) { + if (strstr(fs->block.text, "%{") == NULL) { log_warn("Block '%s' does not use any fs variable", block->label); - block->update_cb = NULL; + block->update_fn = NULL; log_debug("Disabled updates for block '%s'", block->label); return true; } - const char *path = ((char **)block->state)[0]; struct stat sbuf; - - if (path == NULL || stat(path, &sbuf) < 0) { - log_error("Block '%s' was given an invalid path '%s'", block->label, path); + if (fs->path == NULL || stat(fs->path, &sbuf) < 0) { + log_error("Block '%s' was given an invalid path '%s'", block->label, fs->path); return false; } - ((char **)block->state)[1] = block->text.text; - block->text.text = NULL; - + fs->format = fs->block.text; + fs->block.text = NULL; return true; } static const config_entry_t block_fs_entries[] = { - { "path", CONFIG_STRING, NULL, 0 }, + { "path", CONFIG_STRING, NULL, offsetof(block_fs_t, path) }, { 0 }, }; @@ -90,10 +96,10 @@ const block_scheme_t block_fs_scheme = { .tv_sec = 20, .tv_nsec = 0, }, - .update_cb = block_fs_update, - .finalize_cb = block_fs_finalize, + .update_fn = block_fs_update, + .clean_fn = block_fs_clean, }, - .size = 2 * sizeof(char *), + .size = sizeof(block_fs_t), .entries = block_fs_entries, .validate = block_fs_validate, }; diff --git a/src/blocks/group.c b/src/blocks/group.c index 9843afa..ae8f981 100644 --- a/src/blocks/group.c +++ b/src/blocks/group.c @@ -1,12 +1,18 @@ #include "scheme.h" +static void block_group_clean(block_t *block) +{ + block_group_t *group = (block_group_t *)block; + free(group->children); +} + const block_scheme_t block_group_scheme = { .name = "group", .block = { .type = BLOCK_GROUP, - .group.collapse = false, + .clean_fn = block_group_clean, }, - .size = 0, + .size = sizeof(block_group_t), .entries = NULL, .validate = NULL, }; diff --git a/src/blocks/ram.c b/src/blocks/ram.c index 5a95013..8c21b21 100644 --- a/src/blocks/ram.c +++ b/src/blocks/ram.c @@ -7,6 +7,11 @@ #include "../any_log.h" +typedef struct { + block_text_t block; + char *format; +} block_ram_t; + static void block_ram_update(block_t *block) { FILE *meminfo = fopen("/proc/meminfo", "rb"); @@ -47,33 +52,38 @@ static void block_ram_update(block_t *block) NULL, }; - free(block->text.text); - block->text.text = strformat(block->state, '%', ram_formats, ram_values); - assert(block->text.text != NULL); + block_ram_t *ram = (block_ram_t *)block; + free(ram->block.text); + + ram->block.text = strformat(ram->format, '%', ram_formats, ram_values); + assert(ram->block.text != NULL); } -static void block_ram_finalize(block_t *block) +static void block_ram_clean(block_t *block) { - free(block->state); + block_ram_t *ram = (block_ram_t *)block; + free(ram->format); } static bool block_ram_validate(block_t *block, const block_scheme_t *scheme) { - if (block->text.text == NULL) { + block_ram_t *ram = (block_ram_t *)block; + + if (ram->block.text == NULL) { log_error("Block '%s' requires key '%s'", block->label, "text"); return false; } - if (strstr(block->text.text, "%{") == NULL) { + if (strstr(ram->block.text, "%{") == NULL) { log_warn("Block '%s' does not use any ram variable", block->label); - block->update_cb = NULL; + block->update_fn = NULL; log_debug("Disabled updates for block '%s'", block->label); return true; } - block->state = block->text.text; - block->text.text = NULL; + ram->format = ram->block.text; + ram->block.text = NULL; return true; } @@ -85,10 +95,10 @@ const block_scheme_t block_ram_scheme = { .tv_sec = 1, .tv_nsec = 0, }, - .update_cb = block_ram_update, - .finalize_cb = block_ram_finalize, + .update_fn = block_ram_update, + .clean_fn = block_ram_clean, }, - .size = sizeof(char *), + .size = sizeof(block_ram_t), .entries = NULL, .validate = block_ram_validate, }; diff --git a/src/blocks/scheme.h b/src/blocks/scheme.h index b726afe..a3dc9c2 100644 --- a/src/blocks/scheme.h +++ b/src/blocks/scheme.h @@ -13,7 +13,6 @@ struct block_scheme { size_t size; const config_entry_t *entries; block_validate_t validate; - //block_resolve_t resolve; }; extern const block_scheme_t *block_schemes[]; diff --git a/src/blocks/text.c b/src/blocks/text.c index f2c1651..270b197 100644 --- a/src/blocks/text.c +++ b/src/blocks/text.c @@ -2,9 +2,17 @@ #include "../any_log.h" +static void block_text_clean(block_t *block) +{ + block_text_t *text = (block_text_t *)block; + free(text->text); +} + static bool block_text_validate(block_t *block, const block_scheme_t *scheme) { - if (block->text.text == NULL) { + block_text_t *text = (block_text_t *)block; + + if (text->text == NULL) { log_error("Block '%s' requires key '%s'", block->label, "text"); return false; } @@ -16,8 +24,9 @@ const block_scheme_t block_text_scheme = { .name = "text", .block = { .type = BLOCK_TEXT, + .clean_fn = block_text_clean, }, - .size = 0, + .size = sizeof(block_text_t), .entries = NULL, .validate = block_text_validate, }; |
