aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc/funcvars.c
blob: 50abbb8cd4a7332e0477861b31317d9719d7303d (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
/*
 * 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 "funcvars.h"
#include "rusage.h"
#include "sysinfo.h"
#include "../common/utils.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

    {NULL, NULL},
};


char*
blogc_funcvars_lookup(const char *name, bc_trie_t *global)
{
    // protect against evaluating the same function twice in the same global
    // context
    if (NULL != bc_trie_lookup(global, name))
        return NULL;

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

    return NULL;
}