aboutsummaryrefslogtreecommitdiffstats
path: root/src/variables.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/variables.c')
-rw-r--r--src/variables.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/variables.c b/src/variables.c
new file mode 100644
index 0000000..c7cc4da
--- /dev/null
+++ b/src/variables.c
@@ -0,0 +1,57 @@
+/*
+ *
+ */
+
+#include "variables.h"
+#include "strmap.h"
+
+static strmap *map;
+
+bool init_variable_store(void) {
+ map = strmap_create(hash_key);
+ return map;
+}
+
+bool add_variable(const char *key, const char *val) {
+ if (strmap_put(map, key, val) != STRMAP_OK) {
+ return false;
+ }
+
+ return true;
+}
+
+const char *get_variable(const char *key) {
+ fprintf(stderr, "Key is: %s\n", key);
+
+ key += (key[0] == '$');
+ const char *val;
+ strmap_status stat = strmap_get(map, key, &val);
+ switch (stat) {
+ case STRMAP_OK:
+ break;
+ case STRMAP_NOTFOUND:
+ val = getenv(key);
+ break;
+ default:
+ val = NULL;
+ break;
+ }
+
+ val = (val) ? val : "";
+ fprintf(stderr, "Value is: %s\n", val);
+
+ return val;
+}
+
+bool promote_variable_to_env(const char *key) {
+ const char *val = get_variable(key);
+ if (!val) {
+ val = "";
+ }
+
+ return !setenv(key, val, 1);
+}
+
+void destroy_variable_store() {
+ strmap_destroy(map);
+}