aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: 615c833cbd873ad38cdde7bcace677e48b5ecb9c (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

#include "any_log.h"
#include "config.h"
#include "util.h"
#include "block.h"
#include "block.h"

#define ANY_INI_IMPLEMENT
#include "any_ini.h"

typedef enum {
    CONFIG_SUCCESS,
    CONFIG_INVALID,
    CONFIG_UNKNOWN,
} config_status_t;

static const config_entry_t bar_entries[] = {
    { "width",             CONFIG_UINT,   NULL, offsetof(config_t, width) },
    { "height",            CONFIG_UINT,   NULL, offsetof(config_t, height) },
    { "font",              CONFIG_STRING, NULL, offsetof(config_t, font) },
    { "monitor",           CONFIG_STRING, NULL, offsetof(config_t, monitor) },
    { "override-redirect", CONFIG_BOOL,   NULL, offsetof(config_t, override_redirect) },
    { "background",        CONFIG_COLOR,  NULL, offsetof(config_t, background) },
    { 0 },
};

static const config_entry_t block_entries[] = {
    { "hidden",     CONFIG_BOOL,  NULL, offsetof(block_t, hidden) },
    { "color",      CONFIG_COLOR, NULL, offsetof(block_t, color) },
    { "line-color", CONFIG_COLOR, NULL, offsetof(block_t, line_color) },
    { "line-width", CONFIG_UINT,  NULL, offsetof(block_t, line_width) },
    { "x-padding",  CONFIG_UINT,  NULL, offsetof(block_t, x_padding) },
    { "y-padding",  CONFIG_UINT,  NULL, offsetof(block_t, y_padding) },
    { "min-width",  CONFIG_UINT,  NULL, offsetof(block_t, min_width) },
    { "max-width",  CONFIG_UINT,  NULL, offsetof(block_t, max_width) },
    { "interval",   CONFIG_TIME,  NULL, offsetof(block_t, update_interval) },
    { 0 },
};

static const config_entry_t block_group_entries[] = {
    { "spacing",  CONFIG_UINT, NULL, offsetof(block_group_t, spacing) },
    { "blocks",   CONFIG_LIST, NULL, offsetof(block_group_t, children) },
    { "collapse", CONFIG_BOOL, NULL, offsetof(block_group_t, collapse) },
    { 0 },
};

static config_enum_t text_align_enum[] = {
    { "left",   ALIGN_LEFT },
    { "center", ALIGN_CENTER },
    { "right",  ALIGN_RIGHT },
    { 0 },
};

static const config_entry_t block_text_entries[] = {
    { "text",       CONFIG_STRING, NULL,            offsetof(block_text_t, text) },
    { "text-color", CONFIG_COLOR,  NULL,            offsetof(block_text_t, text_color) },
    { "text-size",  CONFIG_UINT,   NULL,            offsetof(block_text_t, text_size) },
    { "text-align", CONFIG_ENUM,   text_align_enum, offsetof(block_text_t, text_size) },
    { 0 },
};

static bool config_read_string(const char *value, char **result)
{
    if (value == NULL)
        value = "";

    size_t start = 0, end = strlen(value);
    if (value[0] == '"' && value[end - 1] == '"') {
        start++;
        end--;
    }

    free(*result);
    *result = strslice(value, start, end);
    return true;
}

static bool config_read_int(const char *value, int *result)
{
    char *end;
    long n = strtol(value, &end, 0);
    *result = n;

    if (n > INT_MAX || n < INT_MIN)
        log_debug("Integer %ld is out of range", n);

    return *end == '\0' && n != LONG_MIN && n != LONG_MAX;
}

static bool config_read_uint(const char *value, unsigned int *result)
{
    char *end;
    unsigned long n = strtoul(value, &end, 0);
    *result = n;

    if (n > UINT_MAX)
        log_debug("Integer %lu is out of range", n);

    return *end == '\0' && n != ULONG_MAX;
}

static bool config_read_double(const char *value, double *result)
{
    char *end;
    *result = strtod(value, &end);
    return *end == '\0';
}

static bool config_read_bool(const char *value, bool *result)
{
    if (!strcmp(value, "false")) {
        *result = false;
        return true;
    }

    if (!strcmp(value, "true")) {
        *result = true;
        return true;
    }

    log_debug("Invalid bool value");
    return false;
}

static bool config_read_color(const char *value, color_t *result)
{
    if (value[0] == '#') {
        char *end;
        unsigned long n = strtoul(value + 1, &end, 16);

        if (end[0] != '\0')
            return false;

        int bpc = 0;
        switch (end - (value + 1)) {
                case 3:
                    bpc = 4;
                    n = (n << 4) | 0x0f;
                    break;

                case 6:
                    bpc = 8;
                    n = (n << 8) | 0xff;
                    break;

                case 4:
                    bpc = 4;
                    break;

                case 8:
                    bpc = 8;
                    break;

                default:
                    return false;
        }

        const unsigned int single_max = (1 << bpc) - 1;
        result->r = ((n >> 3 * bpc) & single_max) / (double)single_max;
        result->g = ((n >> 2 * bpc) & single_max) / (double)single_max;
        result->b = ((n >> 1 * bpc) & single_max) / (double)single_max;
        result->a = ((n >> 0 * bpc) & single_max) / (double)single_max;
        return true;
    }

    log_debug("Invalid color '%s'", value);
    return false;
}

static bool config_read_enum(const char *value, config_enum_t *data, int *result)
{
    for (int i = 0; data[i].label != NULL; i++) {
        if (!strcmp(value, data[i].label)) {
            *result = data[i].value;
            return true;
        }
    }

    log_debug("Invalid enum value '%s'", value);
    return false;
}

// TODO: More robust parsing
static bool config_read_time(const char *value, struct timespec *result)
{
    unsigned int ms;
    if (config_read_uint(value, &ms)) {
        result->tv_sec = ms / 1000;
        result->tv_nsec = (ms % 1000) * 1000000;
        return true;
    }

    log_debug("Invalid time '%s'", value);
    return false;
}

// TODO: Handle quotation marks
static bool config_read_list(const char *value, char ***result)
{
    size_t count = 0;
    for (size_t i = 0; value[i] != '\0'; ++i)
        count += value[i] == ',';

    char **list = calloc(count + 2, sizeof(char *));
    size_t n = 0;

    char *state, *copy = strcopy(value);
    for (char *string = copy; ; string = NULL) {
        char *token = strtok_r(string, ",", &state);

        if (token == NULL)
            break;

        while (isspace(*token)) token++;
        char *end = token + strlen(token);
        while (isspace(*end)) end--;
        *end = '\0';

        if (!config_read_string(token, &list[n++])) {
            free(copy);
            free(list);
            return false;
        }
    }

    free(copy);
    list[n] = NULL;
    *result = list;
    return true;
}

static config_status_t config_read_entry(const config_entry_t *entries, void *result, const char **type,
                                         const char *section, const char *key, const char *value)
{
    static const char *types[] = {
        "string",
        "integer",
        "unsigned integer",
        "double float",
        "boolean",
        "color",
        "enum",
        "time",
        "list",
    };

    for (int i = 0; entries[i].key != NULL; i++) {
        if (!strcmp(key, entries[i].key)) {
            if (type != NULL)
                *type = types[entries[i].type];

            result += entries[i].offset;
            bool nullable = type == CONFIG_STRING;
            if ((value == NULL || *value == '\0') && !nullable)
                return CONFIG_INVALID;

            switch (entries[i].type) {
                case CONFIG_STRING:
                    // NOTE: The previous string is freed by config_read_string
                    if (config_read_string(value, (char **)result)) {
                        log_debug("Set '%s.%s' to '%s'", section, key, *(char **)result);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_INT:
                    if (config_read_int(value, (int *)result)) {
                        log_debug("Set '%s.%s' to '%d'", section, key, *(int *)result);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_UINT:
                    if (config_read_uint(value, (unsigned int *)result)) {
                        log_debug("Set '%s.%s' to '%u'", section, key, *(unsigned int *)result);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_DOUBLE:
                    if (config_read_double(value, (double *)result)) {
                        log_debug("Set '%s.%s' to '%lf'", section, key, *(double *)result);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_BOOL:
                    if (config_read_bool(value, (bool *)result)) {
                        log_debug("Set '%s.%s' to '%s'", section, key, *(bool *)result ? "true" : "false");
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_COLOR:
                    if (config_read_color(value, (color_t *)result)) {
                        char *color = color_to_string((color_t *)result);
                        log_debug("Set '%s.%s' to '%s'", section, key, color);
                        free(color);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_ENUM:
                    if (config_read_enum(value, (config_enum_t *)entries[i].data, (int *)result)) {
                        log_debug("Set '%s.%s' to '%d'", section, key, *(int *)result);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_TIME:
                    if (config_read_time(value, (struct timespec *)result)) {
                        log_debug("Set '%s.%s' to '%s'", section, key, value);
                        return CONFIG_SUCCESS;
                    }
                    break;

                case CONFIG_LIST:
                    if (config_read_list(value, (char ***)result)) {
                        log_debug("Set '%s.%s' to '%s'", section, key, value);
                        return CONFIG_SUCCESS;
                    }
                    break;

                default:
                    unreachable();
            }

            return CONFIG_INVALID;
        }
    }

    return CONFIG_UNKNOWN;
}

static config_status_t config_read_block(const block_scheme_t *scheme, block_t *block, const char **type,
                                         const char *section, const char *key, const char *value)
{
    config_status_t status = config_read_entry(block_entries, block, type, section, key, value);
    if (status != CONFIG_UNKNOWN) return status;

    status = config_read_entry(block->type == BLOCK_GROUP ? block_group_entries : block_text_entries,
                               block, type, section, key, value);

    return status != CONFIG_UNKNOWN || scheme == NULL || scheme->entries == NULL
        ? status
        : config_read_entry(scheme->entries, block, type, section, key, value);
}

static block_t *config_alloc_block(config_t *config, const block_scheme_t *scheme, char *label)
{
    config->blocks = realloc(config->blocks, ++config->n_blocks * sizeof(block_t *));
    assert(config->blocks != NULL);

    config->blocks[config->n_blocks - 1] = scheme->alloc_fn(scheme);
    block_t *block = config->blocks[config->n_blocks - 1];
    assert(block != NULL);

    block->label = label;
    block->scheme = scheme;
    return block;
}

// TODO: More robust way to define defaults
void config_init(config_t *config)
{
    const config_t config_default = {
        .font = "monospace 10",
        .monitor = NULL,
        .width = 100,
        .height = 50,
        .override_redirect = false,
    };

    memcpy(config, &config_default, sizeof(config_t));
    config->font = strcopy(config_default.font);

    extern const block_scheme_t block_group_scheme;
    block_group_t *bar = (block_group_t *)config_alloc_block(config, &block_group_scheme, strcopy("bar"));
    bar->spacing = 10;
}

int config_read(config_t *config, FILE *file)
{
    any_ini_stream_t ini;
    any_ini_file_init(&ini, file);

    int n_errors = 0;
    char *section = NULL;

    do {
        char *key = NULL, *value = NULL;
        int errors = 0;

        bool bar_section = false;
        const block_scheme_t *scheme = NULL;
        block_t *block = NULL;

        if (section != NULL) {
            if (!strncmp(section, "block.", 6)) {
                char *label = strcopy(section + 6);
                if (label == NULL || *label == '\0' || !strcmp(label, "bar")) {
                    ++errors;
                    log_value_error("Block section must have a valid label",
                                    "s:section", section,
                                    "i:line", ini.line);
                } else {
                    for (size_t i = 0; i < config->n_blocks; i++) {
                        if (!strcmp(label, config->blocks[i]->label)) {
                            ++errors;
                            log_value_error("Block section must have a unique label",
                                            "s:section", section,
                                            "i:line", ini.line);
                        }
                    }
                }

                if ((key = any_ini_stream_next_key(&ini)) == NULL || strcmp(key, "type")) {
                    ++errors;
                    log_value_error("Block section must start with a 'type' pair",
                                    "s:section", section,
                                    "s:wrong_key", key,
                                    "i:line", ini.line);
                    goto skip_pair;
                }

                value = any_ini_stream_next_value(&ini);
                if (value == NULL) {
                    ++errors;
                    log_value_error("Block type must be non-empty",
                                    "s:section", section,
                                    "i:line", ini.line);
                    goto skip_pair;
                }

                for (int i = 0; block_schemes[i] != NULL; i++) {
                    if (strcmp(block_schemes[i]->name, value))
                        continue;

                    scheme = block_schemes[i];
                    block = config_alloc_block(config, scheme, label);
                    goto skip_pair;
                }

                ++errors;
                log_value_error("Block type not supported",
                                "s:section", section,
                                "s:type", value,
                                "i:line", ini.line);

                goto skip_pair;
            } else if (!strcmp(section, "bar")) {
                bar_section = true;
            } else
                log_warn("Unknown section '%s'", section);
        }

        while ((key = any_ini_stream_next_key(&ini)) != NULL) {
            value = any_ini_stream_next_value(&ini);

            log_value_trace("Reading entry",
                            "s:section", section,
                            "s:key", key,
                            "s:value", value,
                            "i:line", ini.line);

            if (!bar_section && block == NULL)
                 goto skip_pair;

            const char *type;
            config_status_t status;

            if (bar_section) {
                status = config_read_entry(bar_entries, config, &type, section, key, value);

                // Try the block entries
                if (status == CONFIG_UNKNOWN)
                    block = config->blocks[0];
            }

            if (block != NULL) {
                status = config_read_block(scheme, block, &type, section, key, value);
            }

            switch (status) {
                case CONFIG_SUCCESS:
                    break;

                case CONFIG_INVALID:
                    errors++;
                    log_value_error("Invalid entry",
                                    "s:section", section,
                                    "s:key", key,
                                    "s:value", value,
                                    "s:type", type,
                                    "i:line", ini.line);
                    break;

                case CONFIG_UNKNOWN:
                    log_value_warn("Unknown entry",
                                   "s:section", section,
                                   "s:key", key,
                                   "s:value", value,
                                   "i:line", ini.line);
                    break;

                default:
                    unreachable();
            }

skip_pair:
            free(key);
            free(value);
        }

        if (block != NULL && scheme != NULL && scheme->validate_fn != NULL) {
            if (errors != 0)
                log_trace("Skipped validation for block '%s'", section);
            else {
                block->validated = scheme->validate_fn(block, scheme);
                errors += !block->validated;
            }
        }

        free(section);
        n_errors += errors;
    } while ((section = any_ini_stream_next_section(&ini)) != NULL);

    return n_errors;
}

bool config_resolve_children(config_t *config, block_t *block)
{
    block->resolved = true;

    if (block->scheme != NULL && block->scheme->resolve_fn != NULL)
        return block->scheme->resolve_fn(block, config);

    if (block->type != BLOCK_GROUP)
        return true;

    assert(block->type == BLOCK_GROUP);
    block_group_t *group = (block_group_t *)block;

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

    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]->resolved) {
                    log_error("Block '%s' can only be referenced by one block", config->blocks[j]->label);
                    goto error;
                }

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

                if (!config_resolve_children(config, config->blocks[j]))
                    goto error;
                goto next;
            }
        }

        log_error("Block '%s' not found", children[i]);
        goto error;
next:
    }

    strfree(children);
    return true;

error:
    strfree(children);
    return false;
}

int config_resolve(config_t *config, block_t **block)
{
    int errors = 0;
    block_group_t *bar = (block_group_t *)config->blocks[0];

    if (bar->children == NULL) {
        errors++;
        log_error("Section '%s' requires at least one child", "bar");
    } else {
        // NOTE: The first item is the main block
        bar->block.min_width = bar->block.max_width = config->width;
        errors += !config_resolve_children(config, config->blocks[0]);
    }

    *block = config->blocks[0];
    return errors;
}

void config_free(config_t *config)
{
    for (int i = 0; i < config->n_blocks; i++)
        block_free(config->blocks[i]);

    free(config->blocks);
    free(config->font);
    free(config->monitor);
}