aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc/funcvars.c
blob: 358e3b44cf9725ef7abfc38cc4ddf647ee96021d (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <squareball.h>

#include "rusage.h"
#include "sysinfo.h"
#include "funcvars.h"


static const struct func_map {
    const char *variable;
    const blogc_funcvars_func_t func;
} funcs[] = {

#ifdef HAVE_RUSAGE
    {"BLOGC_RUSAGE_CPU_TIME", blogc_rusage_inject},
    {"BLOGC_RUSAGE_MEMORY", blogc_rusage_inject},
#endif

#ifdef HAVE_SYSINFO_HOSTNAME
    {"BLOGC_SYSINFO_HOSTNAME", blogc_sysinfo_inject_hostname},
#endif

#ifdef HAVE_SYSINFO_DATETIME
    {"BLOGC_SYSINFO_DATETIME", blogc_sysinfo_inject_datetime},
#endif

    {"BLOGC_SYSINFO_USERNAME", blogc_sysinfo_inject_username},
    {"BLOGC_SYSINFO_INSIDE_DOCKER", blogc_sysinfo_inject_inside_docker},
    {NULL, NULL},
};


void
blogc_funcvars_eval(sb_trie_t *global, const char *name)
{
    if (global == NULL || name == NULL)
        return;

    // protect against evaluating the same function twice in the same global
    // context
    if (NULL != sb_trie_lookup(global, name))
        return;

    for (size_t i = 0; funcs[i].variable != NULL; i++) {
        if (0 == strcmp(name, funcs[i].variable)) {
            funcs[i].func(global);
            return;
        }
    }

    return;
}