blob: 4c6722061d2b293ed270e930f22a2bcbab7b25ef [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{
Bryce Harrington6351fb02016-07-11 17:55:15 -0700189 long int ret;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400190 struct weston_config_entry *entry;
191 char *end;
192
193 entry = config_section_get_entry(section, key);
194 if (entry == NULL) {
195 *value = default_value;
196 errno = ENOENT;
197 return -1;
198 }
199
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700200 errno = 0;
Bryce Harrington6351fb02016-07-11 17:55:15 -0700201 ret = strtol(entry->value, &end, 0);
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700202 if (errno != 0 || end == entry->value || *end != '\0') {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400203 *value = default_value;
204 errno = EINVAL;
205 return -1;
206 }
207
Bryce Harrington6351fb02016-07-11 17:55:15 -0700208 /* check range */
209 if (ret < 0 || ret > INT_MAX) {
210 *value = default_value;
211 errno = ERANGE;
212 return -1;
213 }
214
215 *value = ret;
216
Kristian Høgsberg73274712013-04-01 12:41:23 -0400217 return 0;
218}
219
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200220WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400221int
Armin Kb502f902013-07-31 01:41:03 +0200222weston_config_section_get_double(struct weston_config_section *section,
223 const char *key,
224 double *value, double default_value)
225{
226 struct weston_config_entry *entry;
227 char *end;
228
229 entry = config_section_get_entry(section, key);
230 if (entry == NULL) {
231 *value = default_value;
232 errno = ENOENT;
233 return -1;
234 }
235
236 *value = strtod(entry->value, &end);
237 if (*end != '\0') {
238 *value = default_value;
239 errno = EINVAL;
240 return -1;
241 }
242
243 return 0;
244}
245
246WL_EXPORT
247int
Kristian Høgsberg73274712013-04-01 12:41:23 -0400248weston_config_section_get_string(struct weston_config_section *section,
249 const char *key,
250 char **value, const char *default_value)
251{
252 struct weston_config_entry *entry;
253
254 entry = config_section_get_entry(section, key);
255 if (entry == NULL) {
256 if (default_value)
257 *value = strdup(default_value);
258 else
259 *value = NULL;
260 errno = ENOENT;
261 return -1;
262 }
263
264 *value = strdup(entry->value);
265
266 return 0;
267}
268
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200269WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400270int
271weston_config_section_get_bool(struct weston_config_section *section,
272 const char *key,
273 int *value, int default_value)
274{
275 struct weston_config_entry *entry;
276
277 entry = config_section_get_entry(section, key);
278 if (entry == NULL) {
279 *value = default_value;
280 errno = ENOENT;
281 return -1;
282 }
283
284 if (strcmp(entry->value, "false") == 0)
285 *value = 0;
286 else if (strcmp(entry->value, "true") == 0)
287 *value = 1;
288 else {
289 *value = default_value;
290 errno = EINVAL;
291 return -1;
292 }
293
294 return 0;
295}
296
Derek Foremanc7210432014-08-21 11:32:38 -0500297WL_EXPORT
298const char *
299weston_config_get_libexec_dir(void)
300{
301 const char *path = getenv("WESTON_BUILD_DIR");
302
303 if (path)
304 return path;
305
306 return LIBEXECDIR;
307}
308
Pekka Paalanen6c71aae2015-03-24 15:56:19 +0200309const char *
310weston_config_get_name_from_env(void)
311{
312 const char *name;
313
314 name = getenv(WESTON_CONFIG_FILE_ENV_VAR);
315 if (name)
316 return name;
317
318 return "weston.ini";
319}
320
Kristian Høgsberg73274712013-04-01 12:41:23 -0400321static struct weston_config_section *
322config_add_section(struct weston_config *config, const char *name)
323{
324 struct weston_config_section *section;
325
326 section = malloc(sizeof *section);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800327 if (section == NULL)
328 return NULL;
329
Kristian Høgsberg73274712013-04-01 12:41:23 -0400330 section->name = strdup(name);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800331 if (section->name == NULL) {
332 free(section);
333 return NULL;
334 }
335
Kristian Høgsberg73274712013-04-01 12:41:23 -0400336 wl_list_init(&section->entry_list);
337 wl_list_insert(config->section_list.prev, &section->link);
338
339 return section;
340}
341
342static struct weston_config_entry *
343section_add_entry(struct weston_config_section *section,
344 const char *key, const char *value)
345{
346 struct weston_config_entry *entry;
347
348 entry = malloc(sizeof *entry);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800349 if (entry == NULL)
350 return NULL;
351
Kristian Høgsberg73274712013-04-01 12:41:23 -0400352 entry->key = strdup(key);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800353 if (entry->key == NULL) {
354 free(entry);
355 return NULL;
356 }
357
Kristian Høgsberg73274712013-04-01 12:41:23 -0400358 entry->value = strdup(value);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800359 if (entry->value == NULL) {
360 free(entry->key);
361 free(entry);
362 return NULL;
363 }
364
Kristian Høgsberg73274712013-04-01 12:41:23 -0400365 wl_list_insert(section->entry_list.prev, &entry->link);
366
367 return entry;
368}
369
370struct weston_config *
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700371weston_config_parse(const char *name)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400372{
373 FILE *fp;
374 char line[512], *p;
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200375 struct stat filestat;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400376 struct weston_config *config;
377 struct weston_config_section *section = NULL;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700378 int i, fd;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400379
380 config = malloc(sizeof *config);
381 if (config == NULL)
382 return NULL;
383
384 wl_list_init(&config->section_list);
385
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700386 fd = open_config_file(config, name);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700387 if (fd == -1) {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400388 free(config);
389 return NULL;
390 }
391
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200392 if (fstat(fd, &filestat) < 0 ||
393 !S_ISREG(filestat.st_mode)) {
394 close(fd);
395 free(config);
396 return NULL;
397 }
398
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700399 fp = fdopen(fd, "r");
400 if (fp == NULL) {
401 free(config);
402 return NULL;
403 }
Kristian Høgsberg73274712013-04-01 12:41:23 -0400404
405 while (fgets(line, sizeof line, fp)) {
406 switch (line[0]) {
407 case '#':
408 case '\n':
409 continue;
410 case '[':
411 p = strchr(&line[1], ']');
412 if (!p || p[1] != '\n') {
413 fprintf(stderr, "malformed "
414 "section header: %s\n", line);
415 fclose(fp);
416 weston_config_destroy(config);
417 return NULL;
418 }
419 p[0] = '\0';
420 section = config_add_section(config, &line[1]);
421 continue;
422 default:
423 p = strchr(line, '=');
424 if (!p || p == line || !section) {
425 fprintf(stderr, "malformed "
426 "config line: %s\n", line);
427 fclose(fp);
428 weston_config_destroy(config);
429 return NULL;
430 }
431
432 p[0] = '\0';
433 p++;
434 while (isspace(*p))
435 p++;
436 i = strlen(p);
437 while (i > 0 && isspace(p[i - 1])) {
438 p[i - 1] = '\0';
439 i--;
440 }
441 section_add_entry(section, line, p);
442 continue;
443 }
444 }
445
446 fclose(fp);
447
448 return config;
449}
450
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700451const char *
452weston_config_get_full_path(struct weston_config *config)
453{
Alexandru DAMIAN5f429302013-09-26 10:27:16 +0100454 return config == NULL ? NULL : config->path;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700455}
456
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400457int
458weston_config_next_section(struct weston_config *config,
459 struct weston_config_section **section,
460 const char **name)
461{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900462 if (config == NULL)
463 return 0;
464
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400465 if (*section == NULL)
466 *section = container_of(config->section_list.next,
467 struct weston_config_section, link);
468 else
469 *section = container_of((*section)->link.next,
470 struct weston_config_section, link);
471
472 if (&(*section)->link == &config->section_list)
473 return 0;
474
475 *name = (*section)->name;
476
477 return 1;
478}
479
Kristian Høgsberg73274712013-04-01 12:41:23 -0400480void
481weston_config_destroy(struct weston_config *config)
482{
483 struct weston_config_section *s, *next_s;
484 struct weston_config_entry *e, *next_e;
485
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900486 if (config == NULL)
487 return;
488
Kristian Høgsberg73274712013-04-01 12:41:23 -0400489 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
490 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
491 free(e->key);
492 free(e->value);
493 free(e);
494 }
495 free(s->name);
496 free(s);
497 }
498
499 free(config);
500}