aboutsummaryrefslogtreecommitdiff
path: root/src/action.c
blob: 81995e692daeb284456656c491f705c5e86ae5fb (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdlib.h>
#include <string.h>
#include <assert.h>

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

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;

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

            case ACTION_SET_PAIR: {
                assert(target != NULL);

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

                // Skip "set-"
                block_change(target, key + 4, value);
                break;
            }

            case ACTION_RUN_SCRIPT: {
                log_panic("TODO");
                break;
            }

            default:
                unreachable();
        }


        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);
}

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);
}