aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc/funcvars.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/blogc/funcvars.c')
-rw-r--r--src/blogc/funcvars.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c
new file mode 100644
index 0000000..a3af274
--- /dev/null
+++ b/src/blogc/funcvars.c
@@ -0,0 +1,47 @@
+/*
+ * 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[] = {
+ {"BLOGC_RUSAGE_CPU_TIME", blogc_rusage_cpu_time},
+ {"BLOGC_RUSAGE_MEMORY", blogc_rusage_memory},
+ {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;
+}