forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadconfig.c
328 lines (277 loc) · 7.84 KB
/
loadconfig.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <alloca.h>
#include <assert.h>
#include <basedir.h>
#include <basedir_fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "loadconfig.h"
#include "log.h"
#include "item.h"
#include "names.h"
#include "planet_type.h"
#include "port_type.h"
#include "ship_type.h"
#include "star.h"
#include "stringtree.h"
struct file_list {
char *name;
struct list_head list;
};
struct config_type {
const char key[16];
struct list_head head;
int (*func)(const char * const file, struct universe * const);
};
static xdgHandle xdg_handle;
#define CONFIG_PREFIX "yastg/"
static char *get_conf_file(const char * const name)
{
char *path, *file;
if (!name || !*name)
return NULL;
path = malloc(sizeof(CONFIG_PREFIX) + strlen(name) + 1);
if (!path)
goto mem_err;
memcpy(path, CONFIG_PREFIX, sizeof(CONFIG_PREFIX));
strcat(path, name);
file = xdgConfigFind(path, &xdg_handle);
if (!*file)
goto file_err;
free(path);
return file;
file_err:
log_printfn(LOG_CONFIG, "file not found: \"%s\"\n", path);
free(path);
mem_err:
return NULL;
}
static int build_list_of_file_names(struct config_type configs[], const size_t len, const struct list_head *conf_root)
{
struct list_head cmd_root = LIST_HEAD_INIT(cmd_root);
for (size_t i = 0; i < len; i++) {
if (st_add_string(&cmd_root, configs[i].key, &configs[i].head))
goto err;
}
struct config *conf;
list_for_each_entry(conf, conf_root, list) {
struct list_head *head = st_lookup_string(&cmd_root, conf->key);
if (!head) {
log_printfn(LOG_CONFIG, "invalid keyword \"%s\"", conf->key);
continue;
}
if (!conf->str) {
log_printfn(LOG_CONFIG, "keyword \"%s\" is empty, ignored", conf->key);
continue;
}
char *file = get_conf_file(conf->str);
if (!file) {
log_printfn(LOG_CONFIG, "cannot find configuration file \"%s\"", conf->str);
continue;
}
struct file_list *list = malloc(sizeof(*list));
if (!list)
goto err;
list->name = file;
list_add_tail(&list->list, head);
}
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return 0;
err:
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return -1;
}
static int do_load_from_files(const struct config_type configs[], const size_t len,
struct universe * const universe)
{
struct file_list *f;
int r;
log_printfn(LOG_CONFIG, "the following configuration files will be loaded");
for (size_t i = 0; i < len; i++) {
list_for_each_entry(f, &configs[i].head, list)
log_printfn(LOG_CONFIG, "%s: %s ", configs[i].key, f->name);
list_for_each_entry(f, &configs[i].head, list) {
if (configs[i].func) {
r = configs[i].func(f->name, universe);
if (r)
return r;
}
}
}
return 0;
}
/*
* Names are a special case and can't be treated like all other files,
* as we need to pass multiple file names to load_all_names(), and exactly
* which ones depend on the keys. That's why they are handled in a separate
* function and not in do_load_from_files() above.
*/
static int load_names_from_files(const struct config_type configs[], const size_t len)
{
struct list_head cmd_root = LIST_HEAD_INIT(cmd_root);
const char *constellations = NULL;
const char *first = NULL;
const char *sur = NULL;
const char *place = NULL;
const char *prefix = NULL;
const char *suffix = NULL;
struct key_val {
char *key;
const char **val;
};
struct key_val key_vals[] = {
{ .key = "constellations", .val = &constellations },
{ .key = "firstnames", .val = &first, },
{ .key = "surnames", .val = &sur, },
{ .key = "placenames", .val = &place },
{ .key = "placeprefix", .val = &prefix },
{ .key = "placesuffix", .val = &suffix },
};
for (size_t i = 0; i < ARRAY_SIZE(key_vals); i++) {
if (st_add_string(&cmd_root, key_vals[i].key, key_vals[i].val))
goto err;
}
const char **s;
for (size_t i = 0; i < len; i++) {
s = st_lookup_string(&cmd_root, configs[i].key);
if (!s)
continue;
assert(!list_empty(&configs[i].head));
*s = list_first_entry(&configs[i].head, struct file_list, list)->name;
assert(*s);
}
if (constellations)
names_load(&univ.avail_constellations, NULL, constellations, NULL, NULL);
if (first || sur) {
if (first && sur)
names_load(&univ.avail_player_names, NULL, first, sur, NULL);
else
log_printfn(LOG_CONFIG, "need both first names and surnames");
}
if (place || prefix || suffix) {
if (place && prefix && suffix)
names_load(&univ.avail_port_names, prefix, place, NULL, suffix);
else
log_printfn(LOG_CONFIG, "need all of placenames, placeprefix and placesuffix");
}
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return 0;
err:
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return -1;
}
static int load_config(struct universe * const universe, struct list_head * const config_root)
{
struct file_list *f, *_f;
int r = 0;
/*
* The files will be loaded in the order they are listed here.
* It is very important this is correct, as some of them (e.g. items)
* must be loaded before others (e.g. planets).
*/
struct config_type configs[] = {
{ .key = "civilizations", .func = load_civs_from_file, },
{ .key = "constellations", .func = NULL, },
{ .key = "firstnames", .func = NULL, },
{ .key = "surnames", .func = NULL, },
{ .key = "placenames", .func = NULL, },
{ .key = "placeprefix", .func = NULL, },
{ .key = "placesuffix", .func = NULL, },
{ .key = "items", .func = load_items_from_file, },
{ .key = "ships", .func = load_ships_from_file, },
{ .key = "ports", .func = load_ports_from_file, },
{ .key = "planets", .func = load_planets_from_file, },
};
for (size_t i = 0; i < ARRAY_SIZE(configs); i++)
INIT_LIST_HEAD(&configs[i].head);
r = build_list_of_file_names(configs, ARRAY_SIZE(configs), config_root);
if (r)
goto cleanup;
r = do_load_from_files(configs, ARRAY_SIZE(configs), universe);
if (r)
goto cleanup;
r = load_names_from_files(configs, ARRAY_SIZE(configs));
if (r)
goto cleanup;
cleanup:
for (size_t i = 0; i < ARRAY_SIZE(configs); i++) {
list_for_each_entry_safe(f, _f, &configs[i].head, list) {
list_del(&f->list);
free(f->name);
free(f);
}
}
return r;
}
static int is_config_sane(struct universe * const universe)
{
int r = 1;
if (list_empty(&universe->items)) {
log_printfn(LOG_CONFIG, "error: no items loaded");
r = 0;
} else {
assert(!list_empty(&universe->item_names));
}
if (list_empty(&universe->port_types)) {
log_printfn(LOG_CONFIG, "error: no ports loaded");
r = 0;
} else {
assert(!list_empty(&universe->port_type_names));
}
if (list_empty(&universe->planet_types)) {
log_printfn(LOG_CONFIG, "error: no planet types loaded");
r = 0;
}
if (list_empty(&universe->ship_types)) {
log_printfn(LOG_CONFIG, "error: no ship types loaded");
r = 0;
} else {
assert(!list_empty(&universe->ship_type_names));
}
if (list_empty(&universe->civs)) {
log_printfn(LOG_CONFIG, "error: no civilizations loaded");
r = 0;
}
if (!is_names_loaded(&universe->avail_constellations)) {
log_printfn(LOG_CONFIG, "error: no constellations loaded");
r = 0;
}
if (!is_names_loaded(&universe->avail_port_names)) {
log_printfn(LOG_CONFIG, "error: no port names loaded");
r = 0;
}
if (!is_names_loaded(&universe->avail_player_names)) {
log_printfn(LOG_CONFIG, "error: no player names loaded");
r = 0;
}
return r;
}
#define CONFIG_FILE "yastg/yastg.conf"
int parse_config_files(struct universe * const universe)
{
struct list_head conf = LIST_HEAD_INIT(conf);
char *c;
printf("Parsing configuration files\n");
xdgInitHandle(&xdg_handle);
c = xdgConfigFind(CONFIG_FILE, &xdg_handle);
if (!c) {
log_printfn(LOG_CONFIG, CONFIG_FILE "not found\n");
return -1;
}
log_printfn(LOG_CONFIG, "parsing \"%s\"\n", c);
if (parse_config_file(c, &conf)) {
destroy_config(&conf);
return -1;
}
if (load_config(universe, &conf)) {
destroy_config(&conf);
return -1;
}
destroy_config(&conf);
free(c);
xdgWipeHandle(&xdg_handle);
if (!is_config_sane(universe))
return -1;
return 0;
}