2 * Copyright © 2011 Intel Corporation
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.
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
23 #define _GNU_SOURCE /* for stchrnul() */
30 #include <sys/types.h>
35 #include <weston/config-parser.h>
38 handle_key(const struct config_key *key, const char *value)
45 case CONFIG_KEY_INTEGER:
46 i = strtol(value, &end, 0);
48 fprintf(stderr, "invalid integer: %s\n", value);
51 *(int *)key->data = i;
54 case CONFIG_KEY_UNSIGNED_INTEGER:
55 ui = strtoul(value, &end, 0);
57 fprintf(stderr, "invalid integer: %s\n", value);
60 *(unsigned int *)key->data = ui;
63 case CONFIG_KEY_STRING:
65 while (len > 0 && isspace(value[len - 1]))
70 memcpy(s, value, len);
72 *(char **)key->data = s;
75 case CONFIG_KEY_BOOLEAN:
76 if (strcmp(value, "false\n") == 0)
77 *(int *)key->data = 0;
78 else if (strcmp(value, "true\n") == 0)
79 *(int *)key->data = 1;
81 fprintf(stderr, "invalid bool: %s\n", value);
95 parse_config_file(int fd,
96 const struct config_section *sections, int num_sections,
101 const struct config_section *current = NULL;
107 fp = fdopen(dup(fd), "r");
109 perror("couldn't open config file");
115 while (fgets(line, sizeof line, fp)) {
116 if (line[0] == '#' || line[0] == '\n') {
118 } if (line[0] == '[') {
119 p = strchr(&line[1], ']');
120 if (!p || p[1] != '\n') {
121 fprintf(stderr, "malformed "
122 "section header: %s\n", line);
126 if (current && current->done)
129 for (i = 0; i < num_sections; i++) {
130 if (strcmp(sections[i].name, &line[1]) == 0) {
131 current = §ions[i];
135 if (i == num_sections)
137 } else if (p = strchr(line, '='), p != NULL) {
141 for (i = 0; i < current->num_keys; i++) {
142 if (strcmp(current->keys[i].name, line) == 0) {
143 if (handle_key(¤t->keys[i], &p[1]) < 0) {
151 fprintf(stderr, "malformed config line: %s\n", line);
157 if (current && current->done)
166 open_config_file(const char *name)
168 const char *config_dir = getenv("XDG_CONFIG_HOME");
169 const char *home_dir = getenv("HOME");
170 const char *config_dirs = getenv("XDG_CONFIG_DIRS");
172 const char *p, *next;
175 /* Precedence is given to config files in the home directory,
176 * and then to directories listed in XDG_CONFIG_DIRS and
177 * finally to the current working directory. */
179 /* $XDG_CONFIG_HOME */
181 snprintf(path, sizeof path, "%s/%s", config_dir, name);
182 fd = open(path, O_RDONLY | O_CLOEXEC);
189 snprintf(path, sizeof path, "%s/.config/%s", home_dir, name);
190 fd = open(path, O_RDONLY | O_CLOEXEC);
195 /* For each $XDG_CONFIG_DIRS: weston/<config_file> */
197 config_dirs = "/etc/xdg"; /* See XDG base dir spec. */
199 for (p = config_dirs; *p != '\0'; p = next) {
200 next = strchrnul(p, ':');
201 snprintf(path, sizeof path,
202 "%.*s/weston/%s", (int)(next - p), p, name);
203 fd = open(path, O_RDONLY | O_CLOEXEC);
211 /* Current working directory. */
212 snprintf(path, sizeof path, "./%s", name);
213 fd = open(path, O_RDONLY | O_CLOEXEC);
217 "using config in current working directory: %s\n",
220 fprintf(stderr, "config file \"%s\" not found.\n", name);