aboutsummaryrefslogtreecommitdiffstats
path: root/src/variables.c
blob: c7cc4da5ea7bad7a575dbb31f4d278b3df5deae6 (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
/* 
 *
 */

#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);
}