blob: d5bbb8dbc78676e3b66795b598327ddcc50c042a [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 Harringtond0716f42016-07-14 18:28:04 -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 Harringtond0716f42016-07-14 18:28:04 -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 Harringtond0716f42016-07-14 18:28:04 -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
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700222weston_config_section_get_color(struct weston_config_section *section,
223 const char *key,
224 uint32_t *color, uint32_t default_color)
225{
226 struct weston_config_entry *entry;
227 int len;
228 char *end;
229
230 entry = config_section_get_entry(section, key);
231 if (entry == NULL) {
232 *color = default_color;
233 errno = ENOENT;
234 return -1;
235 }
236
237 len = strlen(entry->value);
238 if (len == 1 && entry->value[0] == '0') {
239 *color = 0;
240 return 0;
241 } else if (len != 8 && len != 10) {
242 fprintf(stderr, "string '%s' is length %d\n", entry->value, len);
243 *color = default_color;
244 errno = EINVAL;
245 return -1;
246 }
247
248 errno = 0;
249 *color = strtoul(entry->value, &end, 16);
250 if (errno != 0 || end == entry->value || *end != '\0') {
251 *color = default_color;
252 errno = EINVAL;
253 return -1;
254 }
255
256 return 0;
257}
258
259WL_EXPORT
260int
Armin Kb502f902013-07-31 01:41:03 +0200261weston_config_section_get_double(struct weston_config_section *section,
262 const char *key,
263 double *value, double default_value)
264{
265 struct weston_config_entry *entry;
266 char *end;
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 *value = strtod(entry->value, &end);
276 if (*end != '\0') {
277 *value = default_value;
278 errno = EINVAL;
279 return -1;
280 }
281
282 return 0;
283}
284
285WL_EXPORT
286int
Kristian Høgsberg73274712013-04-01 12:41:23 -0400287weston_config_section_get_string(struct weston_config_section *section,
288 const char *key,
289 char **value, const char *default_value)
290{
291 struct weston_config_entry *entry;
292
293 entry = config_section_get_entry(section, key);
294 if (entry == NULL) {
295 if (default_value)
296 *value = strdup(default_value);
297 else
298 *value = NULL;
299 errno = ENOENT;
300 return -1;
301 }
302
303 *value = strdup(entry->value);
304
305 return 0;
306}
307
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200308WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400309int
310weston_config_section_get_bool(struct weston_config_section *section,
311 const char *key,
312 int *value, int default_value)
313{
314 struct weston_config_entry *entry;
315
316 entry = config_section_get_entry(section, key);
317 if (entry == NULL) {
318 *value = default_value;
319 errno = ENOENT;
320 return -1;
321 }
322
323 if (strcmp(entry->value, "false") == 0)
324 *value = 0;
325 else if (strcmp(entry->value, "true") == 0)
326 *value = 1;
327 else {
328 *value = default_value;
329 errno = EINVAL;
330 return -1;
331 }
332
333 return 0;
334}
335
Derek Foremanc7210432014-08-21 11:32:38 -0500336WL_EXPORT
337const char *
338weston_config_get_libexec_dir(void)
339{
340 const char *path = getenv("WESTON_BUILD_DIR");
341
342 if (path)
343 return path;
344
345 return LIBEXECDIR;
346}
347
Pekka Paalanen6c71aae2015-03-24 15:56:19 +0200348const char *
349weston_config_get_name_from_env(void)
350{
351 const char *name;
352
353 name = getenv(WESTON_CONFIG_FILE_ENV_VAR);
354 if (name)
355 return name;
356
357 return "weston.ini";
358}
359
Kristian Høgsberg73274712013-04-01 12:41:23 -0400360static struct weston_config_section *
361config_add_section(struct weston_config *config, const char *name)
362{
363 struct weston_config_section *section;
364
365 section = malloc(sizeof *section);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800366 if (section == NULL)
367 return NULL;
368
Kristian Høgsberg73274712013-04-01 12:41:23 -0400369 section->name = strdup(name);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800370 if (section->name == NULL) {
371 free(section);
372 return NULL;
373 }
374
Kristian Høgsberg73274712013-04-01 12:41:23 -0400375 wl_list_init(&section->entry_list);
376 wl_list_insert(config->section_list.prev, &section->link);
377
378 return section;
379}
380
381static struct weston_config_entry *
382section_add_entry(struct weston_config_section *section,
383 const char *key, const char *value)
384{
385 struct weston_config_entry *entry;
386
387 entry = malloc(sizeof *entry);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800388 if (entry == NULL)
389 return NULL;
390
Kristian Høgsberg73274712013-04-01 12:41:23 -0400391 entry->key = strdup(key);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800392 if (entry->key == NULL) {
393 free(entry);
394 return NULL;
395 }
396
Kristian Høgsberg73274712013-04-01 12:41:23 -0400397 entry->value = strdup(value);
Bryce Harrington3f2062c2016-02-17 20:46:01 -0800398 if (entry->value == NULL) {
399 free(entry->key);
400 free(entry);
401 return NULL;
402 }
403
Kristian Høgsberg73274712013-04-01 12:41:23 -0400404 wl_list_insert(section->entry_list.prev, &entry->link);
405
406 return entry;
407}
408
409struct weston_config *
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700410weston_config_parse(const char *name)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400411{
412 FILE *fp;
413 char line[512], *p;
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200414 struct stat filestat;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400415 struct weston_config *config;
416 struct weston_config_section *section = NULL;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700417 int i, fd;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400418
419 config = malloc(sizeof *config);
420 if (config == NULL)
421 return NULL;
422
423 wl_list_init(&config->section_list);
424
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700425 fd = open_config_file(config, name);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700426 if (fd == -1) {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400427 free(config);
428 return NULL;
429 }
430
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200431 if (fstat(fd, &filestat) < 0 ||
432 !S_ISREG(filestat.st_mode)) {
433 close(fd);
434 free(config);
435 return NULL;
436 }
437
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700438 fp = fdopen(fd, "r");
439 if (fp == NULL) {
440 free(config);
441 return NULL;
442 }
Kristian Høgsberg73274712013-04-01 12:41:23 -0400443
444 while (fgets(line, sizeof line, fp)) {
445 switch (line[0]) {
446 case '#':
447 case '\n':
448 continue;
449 case '[':
450 p = strchr(&line[1], ']');
451 if (!p || p[1] != '\n') {
452 fprintf(stderr, "malformed "
453 "section header: %s\n", line);
454 fclose(fp);
455 weston_config_destroy(config);
456 return NULL;
457 }
458 p[0] = '\0';
459 section = config_add_section(config, &line[1]);
460 continue;
461 default:
462 p = strchr(line, '=');
463 if (!p || p == line || !section) {
464 fprintf(stderr, "malformed "
465 "config line: %s\n", line);
466 fclose(fp);
467 weston_config_destroy(config);
468 return NULL;
469 }
470
471 p[0] = '\0';
472 p++;
473 while (isspace(*p))
474 p++;
475 i = strlen(p);
476 while (i > 0 && isspace(p[i - 1])) {
477 p[i - 1] = '\0';
478 i--;
479 }
480 section_add_entry(section, line, p);
481 continue;
482 }
483 }
484
485 fclose(fp);
486
487 return config;
488}
489
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700490const char *
491weston_config_get_full_path(struct weston_config *config)
492{
Alexandru DAMIAN5f429302013-09-26 10:27:16 +0100493 return config == NULL ? NULL : config->path;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700494}
495
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400496int
497weston_config_next_section(struct weston_config *config,
498 struct weston_config_section **section,
499 const char **name)
500{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900501 if (config == NULL)
502 return 0;
503
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400504 if (*section == NULL)
505 *section = container_of(config->section_list.next,
506 struct weston_config_section, link);
507 else
508 *section = container_of((*section)->link.next,
509 struct weston_config_section, link);
510
511 if (&(*section)->link == &config->section_list)
512 return 0;
513
514 *name = (*section)->name;
515
516 return 1;
517}
518
Kristian Høgsberg73274712013-04-01 12:41:23 -0400519void
520weston_config_destroy(struct weston_config *config)
521{
522 struct weston_config_section *s, *next_s;
523 struct weston_config_entry *e, *next_e;
524
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900525 if (config == NULL)
526 return;
527
Kristian Høgsberg73274712013-04-01 12:41:23 -0400528 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
529 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
530 free(e->key);
531 free(e->value);
532 free(e);
533 }
534 free(s->name);
535 free(s);
536 }
537
538 free(config);
539}