aboutsummaryrefslogtreecommitdiff
path: root/src/action.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-11-24 16:43:35 +0100
committerFederico Angelilli <code@fedang.net>2024-11-24 16:43:35 +0100
commit30020a08392c959505bd27388564f7436a04bace (patch)
tree2cfdeb4da789c71e3400e05caf353fe0307a6836 /src/action.c
parent441c0b974d00c01fccbce4ebed61711b7b0eeec7 (diff)
Change action syntax
Diffstat (limited to 'src/action.c')
-rw-r--r--src/action.c56
1 files changed, 39 insertions, 17 deletions
diff --git a/src/action.c b/src/action.c
index 2ceae9b..81995e6 100644
--- a/src/action.c
+++ b/src/action.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include "action.h"
#include "block.h"
@@ -7,36 +8,57 @@
void action_perform(action_t *action, block_t *block, config_t *config)
{
+ block_t *target = block;
+
for (size_t i = 0; i < action->length; i++) {
const char *key = action->parts[i].key;
const char *value = action->parts[i].value;
- if (strncmp(key, "block.", 6))
- abort();
+ switch (action->parts[i].type) {
+ case ACTION_TARGET: {
+ if (strncmp(value, "block.", 6))
+ log_panic("Invalid target %s", value);
+
+ if (!strcmp(value + 6, "default")) {
+ target = block;
+ } else {
+ for (size_t i = 0; i < config->n_blocks; i++) {
+ if (!strcmp(value + 6, config->blocks[i]->label)) {
+ target = config->blocks[i];
+ break;
+ }
+ }
+ }
+ break;
+ }
- int sep = 6;
- while (key[sep] != '.' && key[sep] != '\0') sep++;
+ case ACTION_SET_PAIR: {
+ assert(target != NULL);
- if (key[sep] == '\0') abort();
+ if (target->scheme->change_fn == NULL) {
+ log_warn("Block '%s' does not support changing values", target->label);
+ continue;
+ }
- block_t *target = NULL;
- for (size_t i = 0; i < config->n_blocks; i++) {
- size_t length = strlen(config->blocks[i]->label);
- if (length == (sep - 6) && !strncmp(key + 6, config->blocks[i]->label, length)) {
- target = config->blocks[i];
+ // Skip "set-"
+ block_change(target, key + 4, value);
break;
}
- }
- if (target == NULL)
- abort();
+ case ACTION_RUN_SCRIPT: {
+ log_panic("TODO");
+ break;
+ }
- if (target->scheme->change_fn == NULL) {
- log_warn("Block '%s' does not support changing values", target->label);
- continue;
+ default:
+ unreachable();
}
- block_change(target, key, value);
+
+ log_value_trace("Performed action step",
+ "i:type", action->parts[i].type,
+ "s:key", key,
+ "s:value", value);
}
log_debug("Performed action '%s'", action->label);