aboutsummaryrefslogtreecommitdiff
path: root/src/action.c
blob: 2ceae9ba2a3cf0b3fef8e979d7de16049422ed21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdlib.h>
#include <string.h>

#include "action.h"
#include "block.h"
#include "any_log.h"

void action_perform(action_t *action, block_t *block, config_t *config)
{
    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();

        int sep = 6;
        while (key[sep] != '.' && key[sep] != '\0') sep++;

        if (key[sep] == '\0') abort();

        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];
                break;
            }
        }

        if (target == NULL)
            abort();

        if (target->scheme->change_fn == NULL) {
            log_warn("Block '%s' does not support changing values", target->label);
            continue;
        }

        block_change(target, key, value);
    }

    log_debug("Performed action '%s'", action->label);
}

int action_validate(action_t *action, config_t *config)
{
    // TODO
    return 0;
}

void action_free(action_t *action)
{
    for (size_t i = 0; i < action->length; i++) {
        free(action->parts[i].key);
        free(action->parts[i].value);
    }

    free(action->parts);
    free(action->label);
}