aboutsummaryrefslogtreecommitdiff
path: root/src/config.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.h')
-rw-r--r--src/config.h174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..381ff30
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,174 @@
1#ifndef INCLUDED_CONFIG_H
2#define INCLUDED_CONFIG_H
3
4#define CONFIG_FILENAME(sym, ...) struct filename sym;
5#define CONFIG_UINT( sym, ...) unsigned int sym;
6struct config {
7 CONFIG
8};
9#undef CONFIG_FILENAME
10#undef CONFIG_UINT
11
12static bool
13parse_filename (const char **head, const char *end, const char *key,
14 size_t key_len, const char *name, struct filename *filename)
15{
16 if (strncmp (key, name, key_len))
17 return false;
18
19 while (strchr (" \t", **head)) {
20 *head += 1;
21 if (*head == end) {
22 WARNING ("Unexpected EOF while looking for '='");
23 return false;
24 }
25 }
26
27 if (**head != '=') {
28 WARNING ("Expected '='");
29 return false;
30 } *head += 1;
31 if (*head == end) {
32 WARNING ("Unexpected EOF after '='");
33 return false;
34 }
35
36 while (strchr (" \t", **head)) {
37 *head += 1;
38 if (*head == end) {
39 WARNING ("Unexpected EOF while looking for value");
40 return false;
41 }
42 }
43
44 const char *value = *head;
45 while (!strchr (" \t\n\0", **head)) {
46 *head += 1;
47 if (*head == end) {
48 WARNING ("unexpected EOF while parsing value");
49 return false;
50 }
51 }
52
53 size_t value_len = *head - value;
54 filename->count = 0;
55 DYNAMIC_ARRAY_APPEND_MANY (*filename, value, value_len);
56 DYNAMIC_ARRAY_APPEND (*filename, '\0');
57
58 DEBUG ("Parsed filename `%.*s' from config: %s", (int) key_len, key, filename->items);
59 return true;
60}
61
62
63static bool
64parse_uint (const char **head, const char *end, const char *key,
65 size_t key_len, const char *name, unsigned int *uint)
66{
67 if (strncmp (key, name, key_len))
68 return false;
69
70 while (strchr (" \t", **head)) {
71 *head += 1;
72 if (*head == end) {
73 WARNING ("unexpected EOF while looking for '='");
74 return false;
75 }
76 }
77
78 if (**head != '=') {
79 WARNING ("expected '='");
80 } *head += 1;
81 if (*head == end) {
82 WARNING ("unexpected EOF while looking for value");
83 return false;
84 }
85
86 while (strchr (" \t", **head)) {
87 *head += 1;
88 if (*head == end) {
89 WARNING ("unexpected EOF while looking for value");
90 return false;
91 }
92 }
93
94 int value_len = 0;
95 if (sscanf (*head, "%u%n", uint, &value_len) == 0) {
96 LOG (ERR, "unsigned int");
97 return false;
98 } *head += value_len;
99
100 DEBUG ("Parsed uint `%.*s' from config: %u", (int) key_len, key, *uint);
101 return true;
102}
103
104bool check_config (struct config *);
105
106bool
107read_config (const char *filename, struct config *config)
108{
109 bool success = false;
110 char *contents = NULL;
111 int fd = -1;
112#define CONFIG_FILENAME(SYM, KEY, INITIAL) { \
113 DYNAMIC_ARRAY_RESET (config->SYM); \
114 if (INITIAL) \
115 DYNAMIC_ARRAY_APPEND_MANY (config->SYM, INITIAL, sizeof (INITIAL)); \
116 else DYNAMIC_ARRAY_APPEND (config->SYM, '\0'); \
117 }
118#define CONFIG_UINT(SYM, KEY, INITIAL) config->SYM = INITIAL;
119 CONFIG
120#undef CONFIG_FILENAME
121#undef CONFIG_UINT
122
123 fd = open (filename, O_RDONLY);
124 if (fd == -1) {
125 ERROR ("Unable to open config file `%s' for reading: %s", filename, strerror (errno));
126 goto defer;
127 } DEBUG ("Opened config file `%s' for reading", filename);
128
129 struct stat st;
130 if (fstat (fd, &st) == -1) {
131 ERROR ("Unable to fstat config file `%s': %s", filename, strerror (errno));
132 goto defer;
133 } DEBUG ("Config file size: %lu", st.st_size);
134
135 contents = malloc (st.st_size + 1);
136 contents[st.st_size] = '\0';
137 if (read (fd, contents, st.st_size) == -1) {
138 ERROR ("Unable to read config from `%s': %s", filename, strerror (errno));
139 goto defer;
140 } close (fd); fd = -1;
141 DEBUG ("Read config file into buffer");
142
143 const char *end = contents + st.st_size;
144 for (const char *head = contents; head < end; ++head) {
145 if (strchr (" \t\n", *head)) continue;
146 const char *key = head;
147 while (!strchr ("= \t", *head)) {
148 head += 1;
149 if (head == end) {
150 ERROR ("Unexpected EOF while reading key from config");
151 goto defer;
152 }
153 }
154 size_t key_len = head - key;
155#define PARSE(parse, sym, name) \
156 if (parse (&head, end, key, key_len, name, &config->sym)) continue;
157#define CONFIG_FILENAME(sym, name, initial) PARSE (parse_filename, sym, name)
158#define CONFIG_UINT( sym, name, initial) PARSE (parse_uint, sym, name)
159 CONFIG;
160#undef PARSE
161#undef CONFIG_FILENAME
162#undef CONFIG_UINT
163
164 ERROR ("Unknown config key: %.*s", (int) key_len, key);
165 goto defer;
166 } success = true;
167
168defer:
169 if (fd == -1) close (fd);
170 if (contents) free (contents);
171 return success && check_config (config);
172}
173
174#endif // INCLUDED_CONFIG_H