blob: 1e08759e7e968d465e4c96c9a0fdcfe9fdd2fbe2 [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"
Jon Cruz867d50e2015-06-15 15:37:10 -070042#include "helpers.h"
Kristian Høgsbergf73f3162013-05-26 20:50:53 -040043
Kristian Høgsberg73274712013-04-01 12:41:23 -040044struct weston_config_entry {
45 char *key;
46 char *value;
47 struct wl_list link;
48};
49
50struct weston_config_section {
51 char *name;
52 struct wl_list entry_list;
53 struct wl_list link;
54};
55
56struct weston_config {
57 struct wl_list section_list;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -070058 char path[PATH_MAX];
Kristian Høgsberg73274712013-04-01 12:41:23 -040059};
60
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -070061static int
62open_config_file(struct weston_config *c, const char *name)
63{
64 const char *config_dir = getenv("XDG_CONFIG_HOME");
65 const char *home_dir = getenv("HOME");
66 const char *config_dirs = getenv("XDG_CONFIG_DIRS");
67 const char *p, *next;
68 int fd;
69
70 if (name[0] == '/') {
71 snprintf(c->path, sizeof c->path, "%s", name);
72 return open(name, O_RDONLY | O_CLOEXEC);
73 }
74
75 /* Precedence is given to config files in the home directory,
76 * and then to directories listed in XDG_CONFIG_DIRS and
77 * finally to the current working directory. */
78
79 /* $XDG_CONFIG_HOME */
80 if (config_dir) {
81 snprintf(c->path, sizeof c->path, "%s/%s", config_dir, name);
82 fd = open(c->path, O_RDONLY | O_CLOEXEC);
83 if (fd >= 0)
84 return fd;
85 }
86
87 /* $HOME/.config */
88 if (home_dir) {
89 snprintf(c->path, sizeof c->path,
90 "%s/.config/%s", home_dir, name);
91 fd = open(c->path, O_RDONLY | O_CLOEXEC);
92 if (fd >= 0)
93 return fd;
94 }
95
96 /* For each $XDG_CONFIG_DIRS: weston/<config_file> */
97 if (!config_dirs)
98 config_dirs = "/etc/xdg"; /* See XDG base dir spec. */
99
100 for (p = config_dirs; *p != '\0'; p = next) {
101 next = strchrnul(p, ':');
102 snprintf(c->path, sizeof c->path,
103 "%.*s/weston/%s", (int)(next - p), p, name);
104 fd = open(c->path, O_RDONLY | O_CLOEXEC);
105 if (fd >= 0)
106 return fd;
107
108 if (*next == ':')
109 next++;
110 }
111
112 /* Current working directory. */
113 snprintf(c->path, sizeof c->path, "./%s", name);
114
115 return open(c->path, O_RDONLY | O_CLOEXEC);
116}
117
Kristian Høgsberg73274712013-04-01 12:41:23 -0400118static struct weston_config_entry *
119config_section_get_entry(struct weston_config_section *section,
120 const char *key)
121{
122 struct weston_config_entry *e;
123
124 if (section == NULL)
125 return NULL;
126 wl_list_for_each(e, &section->entry_list, link)
127 if (strcmp(e->key, key) == 0)
128 return e;
129
130 return NULL;
131}
132
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200133WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400134struct weston_config_section *
135weston_config_get_section(struct weston_config *config, const char *section,
136 const char *key, const char *value)
137{
138 struct weston_config_section *s;
139 struct weston_config_entry *e;
140
Mun Gwan-gyeong72a3ab72013-05-25 02:09:13 +0900141 if (config == NULL)
142 return NULL;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400143 wl_list_for_each(s, &config->section_list, link) {
144 if (strcmp(s->name, section) != 0)
145 continue;
146 if (key == NULL)
147 return s;
148 e = config_section_get_entry(s, key);
149 if (e && strcmp(e->value, value) == 0)
150 return s;
151 }
152
153 return NULL;
154}
155
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200156WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400157int
158weston_config_section_get_int(struct weston_config_section *section,
159 const char *key,
160 int32_t *value, int32_t default_value)
161{
162 struct weston_config_entry *entry;
163 char *end;
164
165 entry = config_section_get_entry(section, key);
166 if (entry == NULL) {
167 *value = default_value;
168 errno = ENOENT;
169 return -1;
170 }
171
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700172 errno = 0;
Bryce Harrington375759e2016-07-12 16:51:27 -0700173 *value = strtol(entry->value, &end, 10);
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700174 if (errno != 0 || end == entry->value || *end != '\0') {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400175 *value = default_value;
176 errno = EINVAL;
177 return -1;
178 }
179
180 return 0;
181}
182
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200183WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400184int
185weston_config_section_get_uint(struct weston_config_section *section,
186 const char *key,
187 uint32_t *value, uint32_t default_value)
188{
189 struct weston_config_entry *entry;
190 char *end;
191
192 entry = config_section_get_entry(section, key);
193 if (entry == NULL) {
194 *value = default_value;
195 errno = ENOENT;
196 return -1;
197 }
198
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700199 errno = 0;
Bryce Harrington03793e32016-07-13 13:27:29 -0700200 *value = strtoul(entry->value, &end, 0);
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700201 if (errno != 0 || end == entry->value || *end != '\0') {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400202 *value = default_value;
203 errno = EINVAL;
204 return -1;
205 }
206
207 return 0;
208}
209
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200210WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400211int
Armin Kb502f902013-07-31 01:41:03 +0200212weston_config_section_get_double(struct weston_config_section *section,
213 const char *key,
214 double *value, double default_value)
215{
216 struct weston_config_entry *entry;
217 char *end;
218
219 entry = config_section_get_entry(section, key);
220 if (entry == NULL) {
221 *value = default_value;
222 errno = ENOENT;
223 return -1;
224 }
225
226 *value = strtod(entry->value, &end);
227 if (*end != '\0') {
228 *value = default_value;
229 errno = EINVAL;
230 return -1;
231 }
232
233 return 0;
234}
235
236WL_EXPORT
237int
Kristian Høgsberg73274712013-04-01 12:41:23 -0400238weston_config_section_get_string(struct weston_config_section *section,
239 const char *key,
240 char **value, const char *default_value)
241{
242 struct weston_config_entry *entry;
243
244 entry = config_section_get_entry(section, key);
245 if (entry == NULL) {
246 if (default_value)
247 *value = strdup(default_value);
248 else
249 *value = NULL;
250 errno = ENOENT;
251 return -1;
252 }
253
254 *value = strdup(entry->value);
255
256 return 0;
257}
258
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200259WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400260int
261weston_config_section_get_bool(struct weston_config_section *section,
262 const char *key,
263 int *value, int default_value)
264{
265 struct weston_config_entry *entry;
266
267 entry = config_section_get_entry(section, key);
268 if (entry == NULL) {
269 *value = default_value;
270 errno = ENOENT;
271 return -1;
272 }
273
274 if (strcmp(entry->value, "false") == 0)
275 *value = 0;
276 else if (strcmp(entry->value, "true") == 0)
277 *value = 1;
278 else {
279 *value = default_value;
280 errno = EINVAL;
281 return -1;
282 }
283
284 return 0;
285}
286
Derek Foremanc7210432014-08-21 11:32:38 -0500287WL_EXPORT
288const char *
289weston_config_get_libexec_dir(void)
290{
291 const char *path = getenv("WESTON_BUILD_DIR");
292
293 if (path)
294 return path;
295
296 return LIBEXECDIR;
297}
298
Pekka Paalanen6c71aae2015-03-24 15:56:19 +0200299const char *
300weston_config_get_name_from_env(void)
301{
302 const char *name;
303
304 name = getenv(WESTON_CONFIG_FILE_ENV_VAR);
305 if (name)
306 return name;
307
308 return "weston.ini";
309}
310
Kristian Høgsberg73274712013-04-01 12:41:23 -0400311static struct weston_config_section *
312config_add_section(struct weston_config *config, const char *name)
313{
314 struct weston_config_section *section;
315
316 section = malloc(sizeof *section);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800317 if (section == NULL)
318 return NULL;
319
Kristian Høgsberg73274712013-04-01 12:41:23 -0400320 section->name = strdup(name);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800321 if (section->name == NULL) {
322 free(section);
323 return NULL;
324 }
325
Kristian Høgsberg73274712013-04-01 12:41:23 -0400326 wl_list_init(&section->entry_list);
327 wl_list_insert(config->section_list.prev, &section->link);
328
329 return section;
330}
331
332static struct weston_config_entry *
333section_add_entry(struct weston_config_section *section,
334 const char *key, const char *value)
335{
336 struct weston_config_entry *entry;
337
338 entry = malloc(sizeof *entry);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800339 if (entry == NULL)
340 return NULL;
341
Kristian Høgsberg73274712013-04-01 12:41:23 -0400342 entry->key = strdup(key);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800343 if (entry->key == NULL) {
344 free(entry);
345 return NULL;
346 }
347
Kristian Høgsberg73274712013-04-01 12:41:23 -0400348 entry->value = strdup(value);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800349 if (entry->value == NULL) {
350 free(entry->key);
351 free(entry);
352 return NULL;
353 }
354
Kristian Høgsberg73274712013-04-01 12:41:23 -0400355 wl_list_insert(section->entry_list.prev, &entry->link);
356
357 return entry;
358}
359
360struct weston_config *
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700361weston_config_parse(const char *name)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400362{
363 FILE *fp;
364 char line[512], *p;
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200365 struct stat filestat;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400366 struct weston_config *config;
367 struct weston_config_section *section = NULL;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700368 int i, fd;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400369
370 config = malloc(sizeof *config);
371 if (config == NULL)
372 return NULL;
373
374 wl_list_init(&config->section_list);
375
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700376 fd = open_config_file(config, name);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700377 if (fd == -1) {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400378 free(config);
379 return NULL;
380 }
381
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200382 if (fstat(fd, &filestat) < 0 ||
383 !S_ISREG(filestat.st_mode)) {
384 close(fd);
385 free(config);
386 return NULL;
387 }
388
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700389 fp = fdopen(fd, "r");
390 if (fp == NULL) {
391 free(config);
392 return NULL;
393 }
Kristian Høgsberg73274712013-04-01 12:41:23 -0400394
395 while (fgets(line, sizeof line, fp)) {
396 switch (line[0]) {
397 case '#':
398 case '\n':
399 continue;
400 case '[':
401 p = strchr(&line[1], ']');
402 if (!p || p[1] != '\n') {
403 fprintf(stderr, "malformed "
404 "section header: %s\n", line);
405 fclose(fp);
406 weston_config_destroy(config);
407 return NULL;
408 }
409 p[0] = '\0';
410 section = config_add_section(config, &line[1]);
411 continue;
412 default:
413 p = strchr(line, '=');
414 if (!p || p == line || !section) {
415 fprintf(stderr, "malformed "
416 "config line: %s\n", line);
417 fclose(fp);
418 weston_config_destroy(config);
419 return NULL;
420 }
421
422 p[0] = '\0';
423 p++;
424 while (isspace(*p))
425 p++;
426 i = strlen(p);
427 while (i > 0 && isspace(p[i - 1])) {
428 p[i - 1] = '\0';
429 i--;
430 }
431 section_add_entry(section, line, p);
432 continue;
433 }
434 }
435
436 fclose(fp);
437
438 return config;
439}
440
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700441const char *
442weston_config_get_full_path(struct weston_config *config)
443{
Alexandru DAMIAN5f429302013-09-26 10:27:16 +0100444 return config == NULL ? NULL : config->path;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700445}
446
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400447int
448weston_config_next_section(struct weston_config *config,
449 struct weston_config_section **section,
450 const char **name)
451{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900452 if (config == NULL)
453 return 0;
454
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400455 if (*section == NULL)
456 *section = container_of(config->section_list.next,
457 struct weston_config_section, link);
458 else
459 *section = container_of((*section)->link.next,
460 struct weston_config_section, link);
461
462 if (&(*section)->link == &config->section_list)
463 return 0;
464
465 *name = (*section)->name;
466
467 return 1;
468}
469
Kristian Høgsberg73274712013-04-01 12:41:23 -0400470void
471weston_config_destroy(struct weston_config *config)
472{
473 struct weston_config_section *s, *next_s;
474 struct weston_config_entry *e, *next_e;
475
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900476 if (config == NULL)
477 return;
478
Kristian Høgsberg73274712013-04-01 12:41:23 -0400479 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
480 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
481 free(e->key);
482 free(e->value);
483 free(e);
484 }
485 free(s->name);
486 free(s);
487 }
488
489 free(config);
490}