blob: f98209cbbe8614dff2397de039cb53173233fe45 [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
Armin Kb502f902013-07-31 01:41:03 +0200340weston_config_section_get_double(struct weston_config_section *section,
341 const char *key,
342 double *value, double default_value)
343{
344 struct weston_config_entry *entry;
345 char *end;
346
347 entry = config_section_get_entry(section, key);
348 if (entry == NULL) {
349 *value = default_value;
350 errno = ENOENT;
351 return -1;
352 }
353
354 *value = strtod(entry->value, &end);
355 if (*end != '\0') {
356 *value = default_value;
357 errno = EINVAL;
358 return -1;
359 }
360
361 return 0;
362}
363
364WL_EXPORT
365int
Kristian Høgsberg73274712013-04-01 12:41:23 -0400366weston_config_section_get_string(struct weston_config_section *section,
367 const char *key,
368 char **value, const char *default_value)
369{
370 struct weston_config_entry *entry;
371
372 entry = config_section_get_entry(section, key);
373 if (entry == NULL) {
374 if (default_value)
375 *value = strdup(default_value);
376 else
377 *value = NULL;
378 errno = ENOENT;
379 return -1;
380 }
381
382 *value = strdup(entry->value);
383
384 return 0;
385}
386
Quentin Glidic6e2c1242013-07-01 17:03:08 +0200387WL_EXPORT
Kristian Høgsberg73274712013-04-01 12:41:23 -0400388int
389weston_config_section_get_bool(struct weston_config_section *section,
390 const char *key,
391 int *value, int default_value)
392{
393 struct weston_config_entry *entry;
394
395 entry = config_section_get_entry(section, key);
396 if (entry == NULL) {
397 *value = default_value;
398 errno = ENOENT;
399 return -1;
400 }
401
402 if (strcmp(entry->value, "false") == 0)
403 *value = 0;
404 else if (strcmp(entry->value, "true") == 0)
405 *value = 1;
406 else {
407 *value = default_value;
408 errno = EINVAL;
409 return -1;
410 }
411
412 return 0;
413}
414
415static struct weston_config_section *
416config_add_section(struct weston_config *config, const char *name)
417{
418 struct weston_config_section *section;
419
420 section = malloc(sizeof *section);
421 section->name = strdup(name);
422 wl_list_init(&section->entry_list);
423 wl_list_insert(config->section_list.prev, &section->link);
424
425 return section;
426}
427
428static struct weston_config_entry *
429section_add_entry(struct weston_config_section *section,
430 const char *key, const char *value)
431{
432 struct weston_config_entry *entry;
433
434 entry = malloc(sizeof *entry);
435 entry->key = strdup(key);
436 entry->value = strdup(value);
437 wl_list_insert(section->entry_list.prev, &entry->link);
438
439 return entry;
440}
441
442struct weston_config *
443weston_config_parse(int fd)
444{
445 FILE *fp;
446 char line[512], *p;
447 struct weston_config *config;
448 struct weston_config_section *section = NULL;
449 int i;
450
451 config = malloc(sizeof *config);
452 if (config == NULL)
453 return NULL;
454
455 wl_list_init(&config->section_list);
456
457 fp = fdopen(dup(fd), "r");
458 if (fp == NULL) {
459 free(config);
460 return NULL;
461 }
462
463 rewind(fp);
464
465 while (fgets(line, sizeof line, fp)) {
466 switch (line[0]) {
467 case '#':
468 case '\n':
469 continue;
470 case '[':
471 p = strchr(&line[1], ']');
472 if (!p || p[1] != '\n') {
473 fprintf(stderr, "malformed "
474 "section header: %s\n", line);
475 fclose(fp);
476 weston_config_destroy(config);
477 return NULL;
478 }
479 p[0] = '\0';
480 section = config_add_section(config, &line[1]);
481 continue;
482 default:
483 p = strchr(line, '=');
484 if (!p || p == line || !section) {
485 fprintf(stderr, "malformed "
486 "config line: %s\n", line);
487 fclose(fp);
488 weston_config_destroy(config);
489 return NULL;
490 }
491
492 p[0] = '\0';
493 p++;
494 while (isspace(*p))
495 p++;
496 i = strlen(p);
497 while (i > 0 && isspace(p[i - 1])) {
498 p[i - 1] = '\0';
499 i--;
500 }
501 section_add_entry(section, line, p);
502 continue;
503 }
504 }
505
506 fclose(fp);
507
508 return config;
509}
510
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400511int
512weston_config_next_section(struct weston_config *config,
513 struct weston_config_section **section,
514 const char **name)
515{
Mun Gwan-gyeong151a5282013-05-28 00:04:26 +0900516 if (config == NULL)
517 return 0;
518
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400519 if (*section == NULL)
520 *section = container_of(config->section_list.next,
521 struct weston_config_section, link);
522 else
523 *section = container_of((*section)->link.next,
524 struct weston_config_section, link);
525
526 if (&(*section)->link == &config->section_list)
527 return 0;
528
529 *name = (*section)->name;
530
531 return 1;
532}
533
Kristian Høgsberg73274712013-04-01 12:41:23 -0400534void
535weston_config_destroy(struct weston_config *config)
536{
537 struct weston_config_section *s, *next_s;
538 struct weston_config_entry *e, *next_e;
539
Mun Gwan-gyeong77325402013-05-28 00:20:04 +0900540 if (config == NULL)
541 return;
542
Kristian Høgsberg73274712013-04-01 12:41:23 -0400543 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
544 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
545 free(e->key);
546 free(e->value);
547 free(e);
548 }
549 free(s->name);
550 free(s);
551 }
552
553 free(config);
554}