blob: 4174836d3a4b52bc03dfcf9c53e000595be01879 [file] [log] [blame]
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050011 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050028#include <string.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <assert.h>
Kristian Høgsberg3d890492012-08-03 21:56:41 -040032#include <ctype.h>
Ossama Othmana50e6e42013-05-14 09:48:26 -070033#include <limits.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <unistd.h>
Kristian Høgsberg73274712013-04-01 12:41:23 -040038#include <errno.h>
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050039
Kristian Høgsberg73274712013-04-01 12:41:23 -040040#include <wayland-util.h>
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050041#include "config-parser.h"
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050042
Kristian Høgsbergf73f3162013-05-26 20:50:53 -040043#define container_of(ptr, type, member) ({ \
44 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
45 (type *)( (char *)__mptr - offsetof(type,member) );})
46
Kristian Høgsberg73274712013-04-01 12:41:23 -040047struct weston_config_entry {
48 char *key;
49 char *value;
50 struct wl_list link;
51};
52
53struct weston_config_section {
54 char *name;
55 struct wl_list entry_list;
56 struct wl_list link;
57};
58
59struct weston_config {
60 struct wl_list section_list;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -070061 char path[PATH_MAX];
Kristian Høgsberg73274712013-04-01 12:41:23 -040062};
63
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -070064static int
65open_config_file(struct weston_config *c, const char *name)
66{
67 const char *config_dir = getenv("XDG_CONFIG_HOME");
68 const char *home_dir = getenv("HOME");
69 const char *config_dirs = getenv("XDG_CONFIG_DIRS");
70 const char *p, *next;
71 int fd;
72
73 if (name[0] == '/') {
74 snprintf(c->path, sizeof c->path, "%s", name);
75 return open(name, O_RDONLY | O_CLOEXEC);
76 }
77
78 /* Precedence is given to config files in the home directory,
79 * and then to directories listed in XDG_CONFIG_DIRS and
80 * finally to the current working directory. */
81
82 /* $XDG_CONFIG_HOME */
83 if (config_dir) {
84 snprintf(c->path, sizeof c->path, "%s/%s", config_dir, name);
85 fd = open(c->path, O_RDONLY | O_CLOEXEC);
86 if (fd >= 0)
87 return fd;
88 }
89
90 /* $HOME/.config */
91 if (home_dir) {
92 snprintf(c->path, sizeof c->path,
93 "%s/.config/%s", home_dir, name);
94 fd = open(c->path, O_RDONLY | O_CLOEXEC);
95 if (fd >= 0)
96 return fd;
97 }
98
99 /* For each $XDG_CONFIG_DIRS: weston/<config_file> */
100 if (!config_dirs)
101 config_dirs = "/etc/xdg"; /* See XDG base dir spec. */
102
103 for (p = config_dirs; *p != '\0'; p = next) {
104 next = strchrnul(p, ':');
105 snprintf(c->path, sizeof c->path,
106 "%.*s/weston/%s", (int)(next - p), p, name);
107 fd = open(c->path, O_RDONLY | O_CLOEXEC);
108 if (fd >= 0)
109 return fd;
110
111 if (*next == ':')
112 next++;
113 }
114
115 /* Current working directory. */
116 snprintf(c->path, sizeof c->path, "./%s", name);
117
118 return open(c->path, O_RDONLY | O_CLOEXEC);
119}
120
Kristian Høgsberg73274712013-04-01 12:41:23 -0400121static struct weston_config_entry *
122config_section_get_entry(struct weston_config_section *section,
123 const char *key)
124{
125 struct weston_config_entry *e;
126
127 if (section == NULL)
128 return NULL;
129 wl_list_for_each(e, &section->entry_list, link)
130 if (strcmp(e->key, key) == 0)
131 return e;
132
133 return NULL;
134}
135
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200136WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400137struct weston_config_section *
138weston_config_get_section(struct weston_config *config, const char *section,
139 const char *key, const char *value)
140{
141 struct weston_config_section *s;
142 struct weston_config_entry *e;
143
Mun Gwan-gyeong72a3ab72013-05-25 02:09:13 +0900144 if (config == NULL)
145 return NULL;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400146 wl_list_for_each(s, &config->section_list, link) {
147 if (strcmp(s->name, section) != 0)
148 continue;
149 if (key == NULL)
150 return s;
151 e = config_section_get_entry(s, key);
152 if (e && strcmp(e->value, value) == 0)
153 return s;
154 }
155
156 return NULL;
157}
158
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200159WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400160int
161weston_config_section_get_int(struct weston_config_section *section,
162 const char *key,
163 int32_t *value, int32_t default_value)
164{
165 struct weston_config_entry *entry;
166 char *end;
167
168 entry = config_section_get_entry(section, key);
169 if (entry == NULL) {
170 *value = default_value;
171 errno = ENOENT;
172 return -1;
173 }
174
175 *value = strtol(entry->value, &end, 0);
176 if (*end != '\0') {
177 *value = default_value;
178 errno = EINVAL;
179 return -1;
180 }
181
182 return 0;
183}
184
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200185WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400186int
187weston_config_section_get_uint(struct weston_config_section *section,
188 const char *key,
189 uint32_t *value, uint32_t default_value)
190{
191 struct weston_config_entry *entry;
192 char *end;
193
194 entry = config_section_get_entry(section, key);
195 if (entry == NULL) {
196 *value = default_value;
197 errno = ENOENT;
198 return -1;
199 }
200
201 *value = strtoul(entry->value, &end, 0);
202 if (*end != '\0') {
203 *value = default_value;
204 errno = EINVAL;
205 return -1;
206 }
207
208 return 0;
209}
210
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200211WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400212int
Armin Kb502f902013-07-31 01:41:03 +0200213weston_config_section_get_double(struct weston_config_section *section,
214 const char *key,
215 double *value, double default_value)
216{
217 struct weston_config_entry *entry;
218 char *end;
219
220 entry = config_section_get_entry(section, key);
221 if (entry == NULL) {
222 *value = default_value;
223 errno = ENOENT;
224 return -1;
225 }
226
227 *value = strtod(entry->value, &end);
228 if (*end != '\0') {
229 *value = default_value;
230 errno = EINVAL;
231 return -1;
232 }
233
234 return 0;
235}
236
237WL_EXPORT
238int
Kristian Høgsberg73274712013-04-01 12:41:23 -0400239weston_config_section_get_string(struct weston_config_section *section,
240 const char *key,
241 char **value, const char *default_value)
242{
243 struct weston_config_entry *entry;
244
245 entry = config_section_get_entry(section, key);
246 if (entry == NULL) {
247 if (default_value)
248 *value = strdup(default_value);
249 else
250 *value = NULL;
251 errno = ENOENT;
252 return -1;
253 }
254
255 *value = strdup(entry->value);
256
257 return 0;
258}
259
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200260WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400261int
262weston_config_section_get_bool(struct weston_config_section *section,
263 const char *key,
264 int *value, int default_value)
265{
266 struct weston_config_entry *entry;
267
268 entry = config_section_get_entry(section, key);
269 if (entry == NULL) {
270 *value = default_value;
271 errno = ENOENT;
272 return -1;
273 }
274
275 if (strcmp(entry->value, "false") == 0)
276 *value = 0;
277 else if (strcmp(entry->value, "true") == 0)
278 *value = 1;
279 else {
280 *value = default_value;
281 errno = EINVAL;
282 return -1;
283 }
284
285 return 0;
286}
287
Derek Foremanc7210432014-08-21 11:32:38 -0500288WL_EXPORT
289const char *
290weston_config_get_libexec_dir(void)
291{
292 const char *path = getenv("WESTON_BUILD_DIR");
293
294 if (path)
295 return path;
296
297 return LIBEXECDIR;
298}
299
Pekka Paalanen6c71aae2015-03-24 15:56:19 +0200300const char *
301weston_config_get_name_from_env(void)
302{
303 const char *name;
304
305 name = getenv(WESTON_CONFIG_FILE_ENV_VAR);
306 if (name)
307 return name;
308
309 return "weston.ini";
310}
311
Kristian Høgsberg73274712013-04-01 12:41:23 -0400312static struct weston_config_section *
313config_add_section(struct weston_config *config, const char *name)
314{
315 struct weston_config_section *section;
316
317 section = malloc(sizeof *section);
318 section->name = strdup(name);
319 wl_list_init(&section->entry_list);
320 wl_list_insert(config->section_list.prev, &section->link);
321
322 return section;
323}
324
325static struct weston_config_entry *
326section_add_entry(struct weston_config_section *section,
327 const char *key, const char *value)
328{
329 struct weston_config_entry *entry;
330
331 entry = malloc(sizeof *entry);
332 entry->key = strdup(key);
333 entry->value = strdup(value);
334 wl_list_insert(section->entry_list.prev, &entry->link);
335
336 return entry;
337}
338
339struct weston_config *
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700340weston_config_parse(const char *name)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400341{
342 FILE *fp;
343 char line[512], *p;
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200344 struct stat filestat;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400345 struct weston_config *config;
346 struct weston_config_section *section = NULL;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700347 int i, fd;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400348
349 config = malloc(sizeof *config);
350 if (config == NULL)
351 return NULL;
352
353 wl_list_init(&config->section_list);
354
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700355 fd = open_config_file(config, name);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700356 if (fd == -1) {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400357 free(config);
358 return NULL;
359 }
360
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200361 if (fstat(fd, &filestat) < 0 ||
362 !S_ISREG(filestat.st_mode)) {
363 close(fd);
364 free(config);
365 return NULL;
366 }
367
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700368 fp = fdopen(fd, "r");
369 if (fp == NULL) {
370 free(config);
371 return NULL;
372 }
Kristian Høgsberg73274712013-04-01 12:41:23 -0400373
374 while (fgets(line, sizeof line, fp)) {
375 switch (line[0]) {
376 case '#':
377 case '\n':
378 continue;
379 case '[':
380 p = strchr(&line[1], ']');
381 if (!p || p[1] != '\n') {
382 fprintf(stderr, "malformed "
383 "section header: %s\n", line);
384 fclose(fp);
385 weston_config_destroy(config);
386 return NULL;
387 }
388 p[0] = '\0';
389 section = config_add_section(config, &line[1]);
390 continue;
391 default:
392 p = strchr(line, '=');
393 if (!p || p == line || !section) {
394 fprintf(stderr, "malformed "
395 "config line: %s\n", line);
396 fclose(fp);
397 weston_config_destroy(config);
398 return NULL;
399 }
400
401 p[0] = '\0';
402 p++;
403 while (isspace(*p))
404 p++;
405 i = strlen(p);
406 while (i > 0 && isspace(p[i - 1])) {
407 p[i - 1] = '\0';
408 i--;
409 }
410 section_add_entry(section, line, p);
411 continue;
412 }
413 }
414
415 fclose(fp);
416
417 return config;
418}
419
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700420const char *
421weston_config_get_full_path(struct weston_config *config)
422{
Alexandru DAMIAN5f429302013-09-26 10:27:16 +0100423 return config == NULL ? NULL : config->path;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700424}
425
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400426int
427weston_config_next_section(struct weston_config *config,
428 struct weston_config_section **section,
429 const char **name)
430{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900431 if (config == NULL)
432 return 0;
433
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400434 if (*section == NULL)
435 *section = container_of(config->section_list.next,
436 struct weston_config_section, link);
437 else
438 *section = container_of((*section)->link.next,
439 struct weston_config_section, link);
440
441 if (&(*section)->link == &config->section_list)
442 return 0;
443
444 *name = (*section)->name;
445
446 return 1;
447}
448
Kristian Høgsberg73274712013-04-01 12:41:23 -0400449void
450weston_config_destroy(struct weston_config *config)
451{
452 struct weston_config_section *s, *next_s;
453 struct weston_config_entry *e, *next_e;
454
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900455 if (config == NULL)
456 return;
457
Kristian Høgsberg73274712013-04-01 12:41:23 -0400458 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
459 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
460 free(e->key);
461 free(e->value);
462 free(e);
463 }
464 free(s->name);
465 free(s);
466 }
467
468 free(config);
469}