blob: 4e6cf7fb9b48dedd2fb11f5faff8db8f418c2362 [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øgsbergac3a59a2011-11-14 22:43:37 -050044static int
45handle_key(const struct config_key *key, const char *value)
46{
47 char *end, *s;
48 int i, len;
Scott Moreaufa1de692012-01-27 13:25:49 -070049 unsigned int ui;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050050
51 switch (key->type) {
52 case CONFIG_KEY_INTEGER:
53 i = strtol(value, &end, 0);
54 if (*end != '\n') {
55 fprintf(stderr, "invalid integer: %s\n", value);
56 return -1;
57 }
58 *(int *)key->data = i;
59 return 0;
60
Scott Moreaufa1de692012-01-27 13:25:49 -070061 case CONFIG_KEY_UNSIGNED_INTEGER:
62 ui = strtoul(value, &end, 0);
63 if (*end != '\n') {
64 fprintf(stderr, "invalid integer: %s\n", value);
65 return -1;
66 }
67 *(unsigned int *)key->data = ui;
68 return 0;
69
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050070 case CONFIG_KEY_STRING:
71 len = strlen(value);
Kristian Høgsberg3d890492012-08-03 21:56:41 -040072 while (len > 0 && isspace(value[len - 1]))
73 len--;
74 s = malloc(len + 1);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050075 if (s == NULL)
76 return -1;
Kristian Høgsberg3d890492012-08-03 21:56:41 -040077 memcpy(s, value, len);
78 s[len] = '\0';
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050079 *(char **)key->data = s;
80 return 0;
81
Pekka Paalanen28a20702011-12-08 09:24:24 +020082 case CONFIG_KEY_BOOLEAN:
Pekka Paalanen09d65d02011-11-15 11:45:41 +020083 if (strcmp(value, "false\n") == 0)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050084 *(int *)key->data = 0;
Pekka Paalanen09d65d02011-11-15 11:45:41 +020085 else if (strcmp(value, "true\n") == 0)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050086 *(int *)key->data = 1;
87 else {
88 fprintf(stderr, "invalid bool: %s\n", value);
89 return -1;
90 }
91 return 0;
92
93 default:
94 assert(0);
95 break;
96 }
Pekka Paalanen4ea4d1b2012-03-30 13:54:53 +030097
98 return -1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050099}
100
101int
Ossama Othmana50e6e42013-05-14 09:48:26 -0700102parse_config_file(int fd,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500103 const struct config_section *sections, int num_sections,
104 void *data)
105{
106 FILE *fp;
107 char line[512], *p;
108 const struct config_section *current = NULL;
109 int i;
110
Ossama Othmana50e6e42013-05-14 09:48:26 -0700111 if (fd == -1)
112 return -1;
113
114 fp = fdopen(dup(fd), "r");
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500115 if (fp == NULL) {
Ossama Othmana50e6e42013-05-14 09:48:26 -0700116 perror("couldn't open config file");
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500117 return -1;
118 }
119
Ossama Othmana50e6e42013-05-14 09:48:26 -0700120 rewind(fp);
121
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500122 while (fgets(line, sizeof line, fp)) {
123 if (line[0] == '#' || line[0] == '\n') {
124 continue;
125 } if (line[0] == '[') {
126 p = strchr(&line[1], ']');
127 if (!p || p[1] != '\n') {
128 fprintf(stderr, "malformed "
129 "section header: %s\n", line);
130 fclose(fp);
131 return -1;
132 }
133 if (current && current->done)
134 current->done(data);
135 p[0] = '\0';
136 for (i = 0; i < num_sections; i++) {
137 if (strcmp(sections[i].name, &line[1]) == 0) {
138 current = &sections[i];
139 break;
140 }
141 }
142 if (i == num_sections)
143 current = NULL;
144 } else if (p = strchr(line, '='), p != NULL) {
145 if (current == NULL)
146 continue;
147 p[0] = '\0';
148 for (i = 0; i < current->num_keys; i++) {
149 if (strcmp(current->keys[i].name, line) == 0) {
150 if (handle_key(&current->keys[i], &p[1]) < 0) {
151 fclose(fp);
152 return -1;
153 }
154 break;
155 }
156 }
157 } else {
158 fprintf(stderr, "malformed config line: %s\n", line);
159 fclose(fp);
160 return -1;
161 }
162 }
163
164 if (current && current->done)
165 current->done(data);
166
167 fclose(fp);
168
169 return 0;
170}
Pekka Paalanen668dd562011-11-15 11:45:40 +0200171
Ossama Othmana50e6e42013-05-14 09:48:26 -0700172int
173open_config_file(const char *name)
Pekka Paalanen668dd562011-11-15 11:45:40 +0200174{
Ossama Othmana50e6e42013-05-14 09:48:26 -0700175 const char *config_dir = getenv("XDG_CONFIG_HOME");
176 const char *home_dir = getenv("HOME");
177 const char *config_dirs = getenv("XDG_CONFIG_DIRS");
178 char path[PATH_MAX];
179 const char *p, *next;
180 int fd;
Pekka Paalanen668dd562011-11-15 11:45:40 +0200181
Ossama Othmana50e6e42013-05-14 09:48:26 -0700182 /* Precedence is given to config files in the home directory,
183 * and then to directories listed in XDG_CONFIG_DIRS and
184 * finally to the current working directory. */
Pekka Paalanen668dd562011-11-15 11:45:40 +0200185
Ossama Othmana50e6e42013-05-14 09:48:26 -0700186 /* $XDG_CONFIG_HOME */
187 if (config_dir) {
188 snprintf(path, sizeof path, "%s/%s", config_dir, name);
189 fd = open(path, O_RDONLY | O_CLOEXEC);
190 if (fd >= 0)
191 return fd;
Pekka Paalanen668dd562011-11-15 11:45:40 +0200192 }
193
Ossama Othmana50e6e42013-05-14 09:48:26 -0700194 /* $HOME/.config */
195 if (home_dir) {
196 snprintf(path, sizeof path, "%s/.config/%s", home_dir, name);
197 fd = open(path, O_RDONLY | O_CLOEXEC);
198 if (fd >= 0)
199 return fd;
200 }
Pekka Paalanen668dd562011-11-15 11:45:40 +0200201
Ossama Othmana50e6e42013-05-14 09:48:26 -0700202 /* For each $XDG_CONFIG_DIRS: weston/<config_file> */
203 if (!config_dirs)
204 config_dirs = "/etc/xdg"; /* See XDG base dir spec. */
205
206 for (p = config_dirs; *p != '\0'; p = next) {
207 next = strchrnul(p, ':');
208 snprintf(path, sizeof path,
209 "%.*s/weston/%s", (int)(next - p), p, name);
210 fd = open(path, O_RDONLY | O_CLOEXEC);
211 if (fd >= 0)
212 return fd;
213
214 if (*next == ':')
215 next++;
216 }
217
218 /* Current working directory. */
219 snprintf(path, sizeof path, "./%s", name);
220 fd = open(path, O_RDONLY | O_CLOEXEC);
221
222 if (fd >= 0)
223 fprintf(stderr,
224 "using config in current working directory: %s\n",
225 path);
226 else
227 fprintf(stderr, "config file \"%s\" not found.\n", name);
228
229 return fd;
Pekka Paalanen668dd562011-11-15 11:45:40 +0200230}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400231
232struct weston_config_entry {
233 char *key;
234 char *value;
235 struct wl_list link;
236};
237
238struct weston_config_section {
239 char *name;
240 struct wl_list entry_list;
241 struct wl_list link;
242};
243
244struct weston_config {
245 struct wl_list section_list;
246};
247
248static struct weston_config_entry *
249config_section_get_entry(struct weston_config_section *section,
250 const char *key)
251{
252 struct weston_config_entry *e;
253
254 if (section == NULL)
255 return NULL;
256 wl_list_for_each(e, &section->entry_list, link)
257 if (strcmp(e->key, key) == 0)
258 return e;
259
260 return NULL;
261}
262
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200263WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400264struct weston_config_section *
265weston_config_get_section(struct weston_config *config, const char *section,
266 const char *key, const char *value)
267{
268 struct weston_config_section *s;
269 struct weston_config_entry *e;
270
Mun Gwan-gyeong72a3ab72013-05-25 02:09:13 +0900271 if (config == NULL)
272 return NULL;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400273 wl_list_for_each(s, &config->section_list, link) {
274 if (strcmp(s->name, section) != 0)
275 continue;
276 if (key == NULL)
277 return s;
278 e = config_section_get_entry(s, key);
279 if (e && strcmp(e->value, value) == 0)
280 return s;
281 }
282
283 return NULL;
284}
285
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200286WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400287int
288weston_config_section_get_int(struct weston_config_section *section,
289 const char *key,
290 int32_t *value, int32_t default_value)
291{
292 struct weston_config_entry *entry;
293 char *end;
294
295 entry = config_section_get_entry(section, key);
296 if (entry == NULL) {
297 *value = default_value;
298 errno = ENOENT;
299 return -1;
300 }
301
302 *value = strtol(entry->value, &end, 0);
303 if (*end != '\0') {
304 *value = default_value;
305 errno = EINVAL;
306 return -1;
307 }
308
309 return 0;
310}
311
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200312WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400313int
314weston_config_section_get_uint(struct weston_config_section *section,
315 const char *key,
316 uint32_t *value, uint32_t default_value)
317{
318 struct weston_config_entry *entry;
319 char *end;
320
321 entry = config_section_get_entry(section, key);
322 if (entry == NULL) {
323 *value = default_value;
324 errno = ENOENT;
325 return -1;
326 }
327
328 *value = strtoul(entry->value, &end, 0);
329 if (*end != '\0') {
330 *value = default_value;
331 errno = EINVAL;
332 return -1;
333 }
334
335 return 0;
336}
337
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200338WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400339int
340weston_config_section_get_string(struct weston_config_section *section,
341 const char *key,
342 char **value, const char *default_value)
343{
344 struct weston_config_entry *entry;
345
346 entry = config_section_get_entry(section, key);
347 if (entry == NULL) {
348 if (default_value)
349 *value = strdup(default_value);
350 else
351 *value = NULL;
352 errno = ENOENT;
353 return -1;
354 }
355
356 *value = strdup(entry->value);
357
358 return 0;
359}
360
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200361WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400362int
363weston_config_section_get_bool(struct weston_config_section *section,
364 const char *key,
365 int *value, int default_value)
366{
367 struct weston_config_entry *entry;
368
369 entry = config_section_get_entry(section, key);
370 if (entry == NULL) {
371 *value = default_value;
372 errno = ENOENT;
373 return -1;
374 }
375
376 if (strcmp(entry->value, "false") == 0)
377 *value = 0;
378 else if (strcmp(entry->value, "true") == 0)
379 *value = 1;
380 else {
381 *value = default_value;
382 errno = EINVAL;
383 return -1;
384 }
385
386 return 0;
387}
388
389static struct weston_config_section *
390config_add_section(struct weston_config *config, const char *name)
391{
392 struct weston_config_section *section;
393
394 section = malloc(sizeof *section);
395 section->name = strdup(name);
396 wl_list_init(&section->entry_list);
397 wl_list_insert(config->section_list.prev, &section->link);
398
399 return section;
400}
401
402static struct weston_config_entry *
403section_add_entry(struct weston_config_section *section,
404 const char *key, const char *value)
405{
406 struct weston_config_entry *entry;
407
408 entry = malloc(sizeof *entry);
409 entry->key = strdup(key);
410 entry->value = strdup(value);
411 wl_list_insert(section->entry_list.prev, &entry->link);
412
413 return entry;
414}
415
416struct weston_config *
417weston_config_parse(int fd)
418{
419 FILE *fp;
420 char line[512], *p;
421 struct weston_config *config;
422 struct weston_config_section *section = NULL;
423 int i;
424
425 config = malloc(sizeof *config);
426 if (config == NULL)
427 return NULL;
428
429 wl_list_init(&config->section_list);
430
431 fp = fdopen(dup(fd), "r");
432 if (fp == NULL) {
433 free(config);
434 return NULL;
435 }
436
437 rewind(fp);
438
439 while (fgets(line, sizeof line, fp)) {
440 switch (line[0]) {
441 case '#':
442 case '\n':
443 continue;
444 case '[':
445 p = strchr(&line[1], ']');
446 if (!p || p[1] != '\n') {
447 fprintf(stderr, "malformed "
448 "section header: %s\n", line);
449 fclose(fp);
450 weston_config_destroy(config);
451 return NULL;
452 }
453 p[0] = '\0';
454 section = config_add_section(config, &line[1]);
455 continue;
456 default:
457 p = strchr(line, '=');
458 if (!p || p == line || !section) {
459 fprintf(stderr, "malformed "
460 "config line: %s\n", line);
461 fclose(fp);
462 weston_config_destroy(config);
463 return NULL;
464 }
465
466 p[0] = '\0';
467 p++;
468 while (isspace(*p))
469 p++;
470 i = strlen(p);
471 while (i > 0 && isspace(p[i - 1])) {
472 p[i - 1] = '\0';
473 i--;
474 }
475 section_add_entry(section, line, p);
476 continue;
477 }
478 }
479
480 fclose(fp);
481
482 return config;
483}
484
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400485int
486weston_config_next_section(struct weston_config *config,
487 struct weston_config_section **section,
488 const char **name)
489{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900490 if (config == NULL)
491 return 0;
492
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400493 if (*section == NULL)
494 *section = container_of(config->section_list.next,
495 struct weston_config_section, link);
496 else
497 *section = container_of((*section)->link.next,
498 struct weston_config_section, link);
499
500 if (&(*section)->link == &config->section_list)
501 return 0;
502
503 *name = (*section)->name;
504
505 return 1;
506}
507
Kristian Høgsberg73274712013-04-01 12:41:23 -0400508void
509weston_config_destroy(struct weston_config *config)
510{
511 struct weston_config_section *s, *next_s;
512 struct weston_config_entry *e, *next_e;
513
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900514 if (config == NULL)
515 return;
516
Kristian Høgsberg73274712013-04-01 12:41:23 -0400517 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
518 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
519 free(e->key);
520 free(e->value);
521 free(e);
522 }
523 free(s->name);
524 free(s);
525 }
526
527 free(config);
528}