aboutsummaryrefslogtreecommitdiff
path: root/src/config.h
blob: 381ff306ad74cdbf31b870bfad20d555d71f5978 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef INCLUDED_CONFIG_H
#define INCLUDED_CONFIG_H

#define CONFIG_FILENAME(sym, ...) struct filename sym;
#define CONFIG_UINT(    sym, ...) unsigned int    sym;
struct config {
  CONFIG
};
#undef CONFIG_FILENAME
#undef CONFIG_UINT

static bool
parse_filename (const char **head, const char *end, const char *key,
                size_t key_len, const char *name, struct filename *filename)
{
  if (strncmp (key, name, key_len))
    return false;

  while (strchr (" \t", **head)) {
    *head += 1;
    if (*head == end) {
      WARNING ("Unexpected EOF while looking for '='");
      return false;
    }
  }

  if (**head != '=') {
    WARNING ("Expected '='");
    return false;
  } *head += 1;
  if (*head == end) {
    WARNING ("Unexpected EOF after '='");
    return false;
  }

  while (strchr (" \t", **head)) {
    *head += 1;
    if (*head == end) {
      WARNING ("Unexpected EOF while looking for value");
      return false;
    }
  }

  const char *value = *head;
  while (!strchr (" \t\n\0", **head))  {
    *head += 1;
    if (*head == end) {
      WARNING ("unexpected EOF while parsing value");
      return false;
    }
  }

  size_t value_len = *head - value;
  filename->count = 0;
  DYNAMIC_ARRAY_APPEND_MANY (*filename, value, value_len);
  DYNAMIC_ARRAY_APPEND (*filename, '\0');

  DEBUG ("Parsed filename `%.*s' from config: %s", (int) key_len, key, filename->items);
  return true;
}


static bool
parse_uint (const char **head, const char *end, const char *key,
            size_t key_len, const char *name, unsigned int *uint)
{
  if (strncmp (key, name, key_len))
    return false;

  while (strchr (" \t", **head)) {
    *head += 1;
    if (*head == end) {
      WARNING ("unexpected EOF while looking for '='");
      return false;
    }
  }

  if (**head != '=') {
    WARNING ("expected '='");
  } *head += 1;
  if (*head == end) {
    WARNING ("unexpected EOF while looking for value");
    return false;
  }

  while (strchr (" \t", **head))  {
    *head += 1;
    if (*head == end) {
      WARNING ("unexpected EOF while looking for value");
      return false;
    }
  }

  int value_len = 0;
  if (sscanf (*head, "%u%n", uint, &value_len) == 0) {
    LOG (ERR, "unsigned int");
    return false;
  } *head += value_len;

  DEBUG ("Parsed uint `%.*s' from config: %u", (int) key_len, key, *uint);
  return true;
}

bool check_config (struct config *);

bool
read_config (const char *filename, struct config *config)
{
  bool success = false;
  char *contents = NULL;
  int fd = -1;
#define CONFIG_FILENAME(SYM, KEY, INITIAL) {                                   \
    DYNAMIC_ARRAY_RESET (config->SYM);                                         \
    if (INITIAL)                                                               \
      DYNAMIC_ARRAY_APPEND_MANY (config->SYM, INITIAL, sizeof (INITIAL));      \
    else DYNAMIC_ARRAY_APPEND (config->SYM, '\0');                             \
  }
#define CONFIG_UINT(SYM, KEY, INITIAL) config->SYM = INITIAL;
  CONFIG
#undef CONFIG_FILENAME
#undef CONFIG_UINT

  fd = open (filename, O_RDONLY);
  if (fd == -1) {
    ERROR ("Unable to open config file `%s' for reading: %s", filename, strerror (errno));
    goto defer;
  } DEBUG ("Opened config file `%s' for reading", filename);

  struct stat st;
  if (fstat (fd, &st) == -1) {
    ERROR ("Unable to fstat config file `%s': %s", filename, strerror (errno));
    goto defer;
  } DEBUG ("Config file size: %lu", st.st_size);

  contents = malloc (st.st_size + 1);
  contents[st.st_size] = '\0';
  if (read (fd, contents, st.st_size) == -1) {
    ERROR ("Unable to read config from `%s': %s", filename, strerror (errno));
    goto defer;
  } close (fd); fd = -1;
  DEBUG ("Read config file into buffer");

  const char *end = contents + st.st_size;
  for (const char *head = contents; head < end; ++head) {
    if (strchr (" \t\n", *head)) continue;
    const char *key = head;
    while (!strchr ("= \t", *head)) {
      head += 1;
      if (head == end) {
        ERROR ("Unexpected EOF while reading key from config");
        goto defer;
      }
    }
    size_t key_len = head - key;
#define PARSE(parse, sym, name) \
    if (parse (&head, end, key, key_len, name, &config->sym)) continue;
#define CONFIG_FILENAME(sym, name, initial) PARSE (parse_filename, sym, name)
#define CONFIG_UINT(    sym, name, initial) PARSE (parse_uint,     sym, name)
    CONFIG;
#undef PARSE
#undef CONFIG_FILENAME
#undef CONFIG_UINT

    ERROR ("Unknown config key: %.*s", (int) key_len, key);
    goto defer;
  } success = true;

defer:
  if (fd == -1) close (fd);
  if (contents) free (contents);
  return success && check_config (config);
}

#endif // INCLUDED_CONFIG_H