blob: 85245686e1eef678e743e23bfde4fe2409ae5875 (
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
|
/*
* 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 "../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_cpu_time},
{"BLOGC_RUSAGE_MEMORY", blogc_rusage_memory},
#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)) {
char *val = funcs[i].func();
if (val == NULL)
return NULL;
bc_trie_insert(global, name, bc_strdup(val));
return val;
}
}
return NULL;
}
|