aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: 93b4f53ffef8e88da2228c852c7ac9911aec86b4 (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
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>

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

#define ANY_INI_IMPLEMENT
#include "any_ini.h"

typedef enum {
    CONFIG_STRING,
    CONFIG_INT,
    CONFIG_UINT,
    CONFIG_DOUBLE,
    CONFIG_BOOL,
    CONFIG_COLOR,
} config_type_t;

typedef struct {
    const char *key;
    config_type_t type;
    size_t offset;
} config_entry_t;

static const config_entry_t config_entries[] = {
    { "width",             CONFIG_UINT,   offsetof(config_t, width) },
    { "height",            CONFIG_UINT,   offsetof(config_t, height) },
    { "font",              CONFIG_STRING, offsetof(config_t, font) },
    { "monitor",           CONFIG_STRING, offsetof(config_t, monitor) },
    { "override_redirect", CONFIG_BOOL,   offsetof(config_t, override_redirect) },
};

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)
{
    size_t lenght = strlen(value);

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

    if (lenght == 4 && !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 (!strcmp(value, "rgb")) {
        log_panic("TODO");
    }

    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_entry(config_t *config, int line, const char *section, const char *key, const char *value)
{
    const int n_entries = sizeof(config_entries) / sizeof(config_entry_t);

    const char *types[] = {
        "string",
        "integer",
        "positive integer",
        "double float",
    };

    for (int i = 0; i < n_entries; i++) {
        if (!strcmp(key, config_entries[i].key)) {
            bool nullable = config_entries[i].type == CONFIG_STRING;

            if ((value == NULL || *value == '\0') && !nullable) {
                log_value_error("Empty config entry",
                                "s:section", section,
                                "s:key", key,
                                "s:expected_type", types[config_entries[i].type],
                                "i:line", line);
                return false;
            }

            void *result = (uint8_t *)config + config_entries[i].offset;
            switch (config_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("Config '%s' set to '%s'", key, *(char **)result);
                        return true;
                    }
                    break;

                case CONFIG_INT:
                    if (config_read_int(value, (int *)result)) {
                        log_debug("Config '%s' set to '%d'", key, *(int *)result);
                        return true;
                    }
                    break;

                case CONFIG_UINT:
                    if (config_read_uint(value, (unsigned int *)result)) {
                        log_debug("Config '%s' set to '%u'", key, *(unsigned int *)result);
                        return true;
                    }
                    break;

                case CONFIG_DOUBLE:
                    if (config_read_double(value, (double *)result)) {
                        log_debug("Config '%s' set to '%lf'", key, *(double *)result);
                        return true;
                    }
                    break;

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

                case CONFIG_COLOR:
                    if (config_read_color(value, (color_t *)result)) {
                        log_debug("Config '%s' set to '%s'", key, value);
                        return true;
                    }
                    break;

                default:
                    log_panic("Unreachable");
            }

            log_value_error("Invalid config entry",
                            "s:section", section,
                            "s:key", key,
                            "s:value", value,
                            "s:expected_type", types[config_entries[i].type],
                            "i:line", line);

            return false;
        }
    }

    log_value_warn("Unknown config entry",
                   "s:section", section,
                   "s:key", key,
                   "s:value", value,
                   "i:line", line);

    return true;
}

void config_init(config_t *config)
{
    const config_t config_default = {
        .block = {
            .label = "main_block",
            .color = color_rgb(100, 100, 100),
            .type = BLOCK_GROUP,
        },
        .font = "monospace 10",
        .monitor = NULL,
        .height = 50,
        .width = 100,
        .override_redirect = false,
    };

    memcpy(config, &config_default, sizeof(config_t));
    block_copy(&config->block, &config_default.block);

    config->font = strcopy(config_default.font);
    config->monitor = strcopy(config_default.monitor);
}

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

    int errors = 0;
    char *section = NULL;

    do {
        bool unknown_section = section != NULL && strcmp(section, "bar");

        if (unknown_section)
            log_warn("Unknown section '%s'", section);

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

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

            if (!unknown_section)
                errors += !config_entry(config, ini.line, section, key, value);

            free(key);
            free(value);
        }

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

    if (errors > 0)
        log_panic("Config file contained %d errors", errors);
}

void config_free(config_t *config)
{
    free(config->font);
    free(config->monitor);
    block_free(&config->block);
}