aboutsummaryrefslogtreecommitdiff
path: root/src/blocks/group.c
blob: 23c75439bd95096516cbf8a502c6306b17115b9c (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
83
84
85
86
87
88
89
90
91
#include <assert.h>

#include "../block.h"
#include "../any_log.h"

static void block_group_init(block_t *block)
{
    block->type = BLOCK_GROUP;
}

static void block_group_clean(block_t *block)
{
    block_group_t *group = (block_group_t *)block;
    free(group->children);
}

static bool block_group_resolve(block_t *block, config_t *config)
{
    block_group_t *group = (block_group_t *)block;
    assert(block->type == BLOCK_GROUP);

    char **children = (char **)group->children;
    group->children = NULL;
    group->n_children = 0;

    if (children == NULL)
        return true;

    while (children[group->n_children] != NULL)
        group->n_children++;

    group->children = malloc(group->n_children * sizeof(block_t *));
    bool result = true;

    for (size_t i = 0; i < group->n_children; i++) {
        for (size_t j = 0; j < config->n_blocks; j++) {
            if (config->blocks[j] == block)
                continue;

            if (!strcmp(children[i], config->blocks[j]->label)) {
                if (config->blocks[j]->grouped) {
                    log_error("Block '%s' can only be in one group", children[i]);
                    result = false;
                    goto end;
                }

                log_debug("Block '%s' is parent of '%s'", block->label, children[i]);
                group->children[i] = config->blocks[j];
                group->children[i]->grouped = true;

                if (!block_resolve(group->children[i], config)) {
                    result = false;
                    goto end;
                }

                goto next;
            }
        }

        log_error("Block '%s' not found (referenced by '%s')", children[i], block->label);
        result = false;
        goto end;
next:
    }

end:
    strfreelist(children);
    return result;
}

static config_status_t block_group_change(block_t *block, config_t *config, const char *key, const char *value)
{
    if (!strcmp(key, "blocks")) {
        log_error("Block '%s' option '%s' cannot be changed", block->label, "blocks");
        return CONFIG_INVALID;
    }

    extern const config_entry_t block_group_entries[];
    return config_read_entry(block_group_entries, block, NULL, key, value);
}

const block_scheme_t block_group_scheme = {
    .name = "group",
    .entries = NULL,
    .size = sizeof(block_group_t),
    .init_fn = block_group_init,
    .clean_fn = block_group_clean,
    .validate_fn = NULL,
    .resolve_fn = block_group_resolve,
    .change_fn = block_group_change,
};