blob: ff6814ce42cfe3821a4843ef5d0b9c471e7e22b3 [file] [log] [blame]
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050025#include <string.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <assert.h>
Kristian Høgsberg3d890492012-08-03 21:56:41 -040029#include <ctype.h>
Ossama Othmana50e6e42013-05-14 09:48:26 -070030#include <limits.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsberg73274712013-04-01 12:41:23 -040035#include <errno.h>
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050036
Kristian Høgsberg73274712013-04-01 12:41:23 -040037#include <wayland-util.h>
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050038#include "config-parser.h"
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050039
Kristian Høgsbergf73f3162013-05-26 20:50:53 -040040#define container_of(ptr, type, member) ({ \
41 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
42 (type *)( (char *)__mptr - offsetof(type,member) );})
43
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
172 *value = strtol(entry->value, &end, 0);
173 if (*end != '\0') {
174 *value = default_value;
175 errno = EINVAL;
176 return -1;
177 }
178
179 return 0;
180}
181
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200182WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400183int
184weston_config_section_get_uint(struct weston_config_section *section,
185 const char *key,
186 uint32_t *value, uint32_t default_value)
187{
188 struct weston_config_entry *entry;
189 char *end;
190
191 entry = config_section_get_entry(section, key);
192 if (entry == NULL) {
193 *value = default_value;
194 errno = ENOENT;
195 return -1;
196 }
197
198 *value = strtoul(entry->value, &end, 0);
199 if (*end != '\0') {
200 *value = default_value;
201 errno = EINVAL;
202 return -1;
203 }
204
205 return 0;
206}
207
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200208WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400209int
Armin Kb502f902013-07-31 01:41:03 +0200210weston_config_section_get_double(struct weston_config_section *section,
211 const char *key,
212 double *value, double default_value)
213{
214 struct weston_config_entry *entry;
215 char *end;
216
217 entry = config_section_get_entry(section, key);
218 if (entry == NULL) {
219 *value = default_value;
220 errno = ENOENT;
221 return -1;
222 }
223
224 *value = strtod(entry->value, &end);
225 if (*end != '\0') {
226 *value = default_value;
227 errno = EINVAL;
228 return -1;
229 }
230
231 return 0;
232}
233
234WL_EXPORT
235int
Kristian Høgsberg73274712013-04-01 12:41:23 -0400236weston_config_section_get_string(struct weston_config_section *section,
237 const char *key,
238 char **value, const char *default_value)
239{
240 struct weston_config_entry *entry;
241
242 entry = config_section_get_entry(section, key);
243 if (entry == NULL) {
244 if (default_value)
245 *value = strdup(default_value);
246 else
247 *value = NULL;
248 errno = ENOENT;
249 return -1;
250 }
251
252 *value = strdup(entry->value);
253
254 return 0;
255}
256
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200257WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400258int
259weston_config_section_get_bool(struct weston_config_section *section,
260 const char *key,
261 int *value, int default_value)
262{
263 struct weston_config_entry *entry;
264
265 entry = config_section_get_entry(section, key);
266 if (entry == NULL) {
267 *value = default_value;
268 errno = ENOENT;
269 return -1;
270 }
271
272 if (strcmp(entry->value, "false") == 0)
273 *value = 0;
274 else if (strcmp(entry->value, "true") == 0)
275 *value = 1;
276 else {
277 *value = default_value;
278 errno = EINVAL;
279 return -1;
280 }
281
282 return 0;
283}
284
Derek Foremanc7210432014-08-21 11:32:38 -0500285WL_EXPORT
286const char *
287weston_config_get_libexec_dir(void)
288{
289 const char *path = getenv("WESTON_BUILD_DIR");
290
291 if (path)
292 return path;
293
294 return LIBEXECDIR;
295}
296
Kristian Høgsberg73274712013-04-01 12:41:23 -0400297static struct weston_config_section *
298config_add_section(struct weston_config *config, const char *name)
299{
300 struct weston_config_section *section;
301
302 section = malloc(sizeof *section);
303 section->name = strdup(name);
304 wl_list_init(&section->entry_list);
305 wl_list_insert(config->section_list.prev, &section->link);
306
307 return section;
308}
309
310static struct weston_config_entry *
311section_add_entry(struct weston_config_section *section,
312 const char *key, const char *value)
313{
314 struct weston_config_entry *entry;
315
316 entry = malloc(sizeof *entry);
317 entry->key = strdup(key);
318 entry->value = strdup(value);
319 wl_list_insert(section->entry_list.prev, &entry->link);
320
321 return entry;
322}
323
324struct weston_config *
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700325weston_config_parse(const char *name)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400326{
327 FILE *fp;
328 char line[512], *p;
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200329 struct stat filestat;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400330 struct weston_config *config;
331 struct weston_config_section *section = NULL;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700332 int i, fd;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400333
334 config = malloc(sizeof *config);
335 if (config == NULL)
336 return NULL;
337
338 wl_list_init(&config->section_list);
339
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700340 fd = open_config_file(config, name);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700341 if (fd == -1) {
Kristian Høgsberg73274712013-04-01 12:41:23 -0400342 free(config);
343 return NULL;
344 }
345
Pekka Paalanen49f6d622015-03-24 15:56:17 +0200346 if (fstat(fd, &filestat) < 0 ||
347 !S_ISREG(filestat.st_mode)) {
348 close(fd);
349 free(config);
350 return NULL;
351 }
352
Kristian Høgsberg1abe0482013-09-21 23:02:31 -0700353 fp = fdopen(fd, "r");
354 if (fp == NULL) {
355 free(config);
356 return NULL;
357 }
Kristian Høgsberg73274712013-04-01 12:41:23 -0400358
359 while (fgets(line, sizeof line, fp)) {
360 switch (line[0]) {
361 case '#':
362 case '\n':
363 continue;
364 case '[':
365 p = strchr(&line[1], ']');
366 if (!p || p[1] != '\n') {
367 fprintf(stderr, "malformed "
368 "section header: %s\n", line);
369 fclose(fp);
370 weston_config_destroy(config);
371 return NULL;
372 }
373 p[0] = '\0';
374 section = config_add_section(config, &line[1]);
375 continue;
376 default:
377 p = strchr(line, '=');
378 if (!p || p == line || !section) {
379 fprintf(stderr, "malformed "
380 "config line: %s\n", line);
381 fclose(fp);
382 weston_config_destroy(config);
383 return NULL;
384 }
385
386 p[0] = '\0';
387 p++;
388 while (isspace(*p))
389 p++;
390 i = strlen(p);
391 while (i > 0 && isspace(p[i - 1])) {
392 p[i - 1] = '\0';
393 i--;
394 }
395 section_add_entry(section, line, p);
396 continue;
397 }
398 }
399
400 fclose(fp);
401
402 return config;
403}
404
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700405const char *
406weston_config_get_full_path(struct weston_config *config)
407{
Alexandru DAMIAN5f429302013-09-26 10:27:16 +0100408 return config == NULL ? NULL : config->path;
Kristian Høgsbergeeefc9e2013-09-21 23:17:35 -0700409}
410
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400411int
412weston_config_next_section(struct weston_config *config,
413 struct weston_config_section **section,
414 const char **name)
415{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900416 if (config == NULL)
417 return 0;
418
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400419 if (*section == NULL)
420 *section = container_of(config->section_list.next,
421 struct weston_config_section, link);
422 else
423 *section = container_of((*section)->link.next,
424 struct weston_config_section, link);
425
426 if (&(*section)->link == &config->section_list)
427 return 0;
428
429 *name = (*section)->name;
430
431 return 1;
432}
433
Kristian Høgsberg73274712013-04-01 12:41:23 -0400434void
435weston_config_destroy(struct weston_config *config)
436{
437 struct weston_config_section *s, *next_s;
438 struct weston_config_entry *e, *next_e;
439
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900440 if (config == NULL)
441 return;
442
Kristian Høgsberg73274712013-04-01 12:41:23 -0400443 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
444 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
445 free(e->key);
446 free(e->value);
447 free(e);
448 }
449 free(s->name);
450 free(s);
451 }
452
453 free(config);
454}