1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * 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:
11 *
12 * 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.
24 */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <assert.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <errno.h>
40
41 #include <wayland-util.h>
42 #include <libweston/config-parser.h>
43 #include "helpers.h"
44 #include "string-helpers.h"
45
46 struct weston_config_entry {
47 char *key;
48 char *value;
49 struct wl_list link;
50 };
51
52 struct weston_config_section {
53 char *name;
54 struct wl_list entry_list;
55 struct wl_list link;
56 };
57
58 struct weston_config {
59 struct wl_list section_list;
60 char path[PATH_MAX];
61 };
62
63 static int
open_config_file(struct weston_config * c,const char * name)64 open_config_file(struct weston_config *c, const char *name)
65 {
66 const char *config_dir = getenv("XDG_CONFIG_HOME");
67 const char *home_dir = getenv("HOME");
68 const char *config_dirs = getenv("XDG_CONFIG_DIRS");
69 const char *p, *next;
70 int fd;
71
72 if (name[0] == '/') {
73 snprintf(c->path, sizeof c->path, "%s", name);
74 return open(name, O_RDONLY | O_CLOEXEC);
75 }
76
77 /* Precedence is given to config files in the home directory,
78 * then to directories listed in XDG_CONFIG_DIRS. */
79
80 /* $XDG_CONFIG_HOME */
81 if (config_dir) {
82 snprintf(c->path, sizeof c->path, "%s/%s", config_dir, name);
83 fd = open(c->path, O_RDONLY | O_CLOEXEC);
84 if (fd >= 0)
85 return fd;
86 }
87
88 /* $HOME/.config */
89 if (home_dir) {
90 snprintf(c->path, sizeof c->path,
91 "%s/.config/%s", home_dir, name);
92 fd = open(c->path, O_RDONLY | O_CLOEXEC);
93 if (fd >= 0)
94 return fd;
95 }
96
97 /* For each $XDG_CONFIG_DIRS: weston/<config_file> */
98 if (!config_dirs)
99 config_dirs = "/etc/xdg"; /* See XDG base dir spec. */
100
101 for (p = config_dirs; *p != '\0'; p = next) {
102 next = strchrnul(p, ':');
103 snprintf(c->path, sizeof c->path,
104 "%.*s/weston/%s", (int)(next - p), p, name);
105 fd = open(c->path, O_RDONLY | O_CLOEXEC);
106 if (fd >= 0)
107 return fd;
108
109 if (*next == ':')
110 next++;
111 }
112
113 return -1;
114 }
115
116 static struct weston_config_entry *
config_section_get_entry(struct weston_config_section * section,const char * key)117 config_section_get_entry(struct weston_config_section *section,
118 const char *key)
119 {
120 struct weston_config_entry *e;
121
122 if (section == NULL)
123 return NULL;
124 wl_list_for_each(e, §ion->entry_list, link)
125 if (strcmp(e->key, key) == 0)
126 return e;
127
128 return NULL;
129 }
130
131 WL_EXPORT
132 struct weston_config_section *
weston_config_get_section(struct weston_config * config,const char * section,const char * key,const char * value)133 weston_config_get_section(struct weston_config *config, const char *section,
134 const char *key, const char *value)
135 {
136 struct weston_config_section *s;
137 struct weston_config_entry *e;
138
139 if (config == NULL)
140 return NULL;
141 wl_list_for_each(s, &config->section_list, link) {
142 if (strcmp(s->name, section) != 0)
143 continue;
144 if (key == NULL)
145 return s;
146 e = config_section_get_entry(s, key);
147 if (e && strcmp(e->value, value) == 0)
148 return s;
149 }
150
151 return NULL;
152 }
153
154 WL_EXPORT
155 int
weston_config_section_get_int(struct weston_config_section * section,const char * key,int32_t * value,int32_t default_value)156 weston_config_section_get_int(struct weston_config_section *section,
157 const char *key,
158 int32_t *value, int32_t default_value)
159 {
160 struct weston_config_entry *entry;
161
162 entry = config_section_get_entry(section, key);
163 if (entry == NULL) {
164 *value = default_value;
165 errno = ENOENT;
166 return -1;
167 }
168
169 if (!safe_strtoint(entry->value, value)) {
170 *value = default_value;
171 return -1;
172 }
173
174 return 0;
175 }
176
177 WL_EXPORT
178 int
weston_config_section_get_uint(struct weston_config_section * section,const char * key,uint32_t * value,uint32_t default_value)179 weston_config_section_get_uint(struct weston_config_section *section,
180 const char *key,
181 uint32_t *value, uint32_t default_value)
182 {
183 long int ret;
184 struct weston_config_entry *entry;
185 char *end;
186
187 entry = config_section_get_entry(section, key);
188 if (entry == NULL) {
189 *value = default_value;
190 errno = ENOENT;
191 return -1;
192 }
193
194 errno = 0;
195 ret = strtol(entry->value, &end, 0);
196 if (errno != 0 || end == entry->value || *end != '\0') {
197 *value = default_value;
198 errno = EINVAL;
199 return -1;
200 }
201
202 /* check range */
203 if (ret < 0 || ret > INT_MAX) {
204 *value = default_value;
205 errno = ERANGE;
206 return -1;
207 }
208
209 *value = ret;
210
211 return 0;
212 }
213
214 WL_EXPORT
215 int
weston_config_section_get_color(struct weston_config_section * section,const char * key,uint32_t * color,uint32_t default_color)216 weston_config_section_get_color(struct weston_config_section *section,
217 const char *key,
218 uint32_t *color, uint32_t default_color)
219 {
220 struct weston_config_entry *entry;
221 int len;
222 char *end;
223
224 entry = config_section_get_entry(section, key);
225 if (entry == NULL) {
226 *color = default_color;
227 errno = ENOENT;
228 return -1;
229 }
230
231 len = strlen(entry->value);
232 if (len == 1 && entry->value[0] == '0') {
233 *color = 0;
234 return 0;
235 } else if (len != 8 && len != 10) {
236 *color = default_color;
237 errno = EINVAL;
238 return -1;
239 }
240
241 errno = 0;
242 *color = strtoul(entry->value, &end, 16);
243 if (errno != 0 || end == entry->value || *end != '\0') {
244 *color = default_color;
245 errno = EINVAL;
246 return -1;
247 }
248
249 return 0;
250 }
251
252 WL_EXPORT
253 int
weston_config_section_get_double(struct weston_config_section * section,const char * key,double * value,double default_value)254 weston_config_section_get_double(struct weston_config_section *section,
255 const char *key,
256 double *value, double default_value)
257 {
258 struct weston_config_entry *entry;
259 char *end;
260
261 entry = config_section_get_entry(section, key);
262 if (entry == NULL) {
263 *value = default_value;
264 errno = ENOENT;
265 return -1;
266 }
267
268 *value = strtod(entry->value, &end);
269 if (*end != '\0') {
270 *value = default_value;
271 errno = EINVAL;
272 return -1;
273 }
274
275 return 0;
276 }
277
278 WL_EXPORT
279 int
weston_config_section_get_string(struct weston_config_section * section,const char * key,char ** value,const char * default_value)280 weston_config_section_get_string(struct weston_config_section *section,
281 const char *key,
282 char **value, const char *default_value)
283 {
284 struct weston_config_entry *entry;
285
286 entry = config_section_get_entry(section, key);
287 if (entry == NULL) {
288 if (default_value)
289 *value = strdup(default_value);
290 else
291 *value = NULL;
292 errno = ENOENT;
293 return -1;
294 }
295
296 *value = strdup(entry->value);
297
298 return 0;
299 }
300
301 WL_EXPORT
302 int
weston_config_section_get_bool(struct weston_config_section * section,const char * key,bool * value,bool default_value)303 weston_config_section_get_bool(struct weston_config_section *section,
304 const char *key,
305 bool *value, bool default_value)
306 {
307 struct weston_config_entry *entry;
308
309 entry = config_section_get_entry(section, key);
310 if (entry == NULL) {
311 *value = default_value;
312 errno = ENOENT;
313 return -1;
314 }
315
316 if (strcmp(entry->value, "false") == 0)
317 *value = false;
318 else if (strcmp(entry->value, "true") == 0)
319 *value = true;
320 else {
321 *value = default_value;
322 errno = EINVAL;
323 return -1;
324 }
325
326 return 0;
327 }
328
329 WL_EXPORT
330 const char *
weston_config_get_name_from_env(void)331 weston_config_get_name_from_env(void)
332 {
333 const char *name;
334
335 name = getenv(WESTON_CONFIG_FILE_ENV_VAR);
336 if (name)
337 return name;
338
339 return "weston.ini";
340 }
341
342 static struct weston_config_section *
config_add_section(struct weston_config * config,const char * name)343 config_add_section(struct weston_config *config, const char *name)
344 {
345 struct weston_config_section *section;
346
347 section = malloc(sizeof *section);
348 if (section == NULL)
349 return NULL;
350
351 section->name = strdup(name);
352 if (section->name == NULL) {
353 free(section);
354 return NULL;
355 }
356
357 wl_list_init(§ion->entry_list);
358 wl_list_insert(config->section_list.prev, §ion->link);
359
360 return section;
361 }
362
363 static struct weston_config_entry *
section_add_entry(struct weston_config_section * section,const char * key,const char * value)364 section_add_entry(struct weston_config_section *section,
365 const char *key, const char *value)
366 {
367 struct weston_config_entry *entry;
368
369 entry = malloc(sizeof *entry);
370 if (entry == NULL)
371 return NULL;
372
373 entry->key = strdup(key);
374 if (entry->key == NULL) {
375 free(entry);
376 return NULL;
377 }
378
379 entry->value = strdup(value);
380 if (entry->value == NULL) {
381 free(entry->key);
382 free(entry);
383 return NULL;
384 }
385
386 wl_list_insert(section->entry_list.prev, &entry->link);
387
388 return entry;
389 }
390
391 WL_EXPORT
392 struct weston_config *
weston_config_parse(const char * name)393 weston_config_parse(const char *name)
394 {
395 FILE *fp;
396 char line[512], *p;
397 struct stat filestat;
398 struct weston_config *config;
399 struct weston_config_section *section = NULL;
400 int i, fd;
401
402 config = malloc(sizeof *config);
403 if (config == NULL)
404 return NULL;
405
406 wl_list_init(&config->section_list);
407
408 fd = open_config_file(config, name);
409 if (fd == -1) {
410 free(config);
411 return NULL;
412 }
413
414 if (fstat(fd, &filestat) < 0 ||
415 !S_ISREG(filestat.st_mode)) {
416 close(fd);
417 free(config);
418 return NULL;
419 }
420
421 fp = fdopen(fd, "r");
422 if (fp == NULL) {
423 free(config);
424 return NULL;
425 }
426
427 while (fgets(line, sizeof line, fp)) {
428 switch (line[0]) {
429 case '#':
430 case '\n':
431 continue;
432 case '[':
433 p = strchr(&line[1], ']');
434 if (!p || p[1] != '\n') {
435 fprintf(stderr, "malformed "
436 "section header: %s\n", line);
437 fclose(fp);
438 weston_config_destroy(config);
439 return NULL;
440 }
441 p[0] = '\0';
442 section = config_add_section(config, &line[1]);
443 continue;
444 default:
445 p = strchr(line, '=');
446 if (!p || p == line || !section) {
447 fprintf(stderr, "malformed "
448 "config line: %s\n", line);
449 fclose(fp);
450 weston_config_destroy(config);
451 return NULL;
452 }
453
454 p[0] = '\0';
455 p++;
456 while (isspace(*p))
457 p++;
458 i = strlen(p);
459 while (i > 0 && isspace(p[i - 1])) {
460 p[i - 1] = '\0';
461 i--;
462 }
463 section_add_entry(section, line, p);
464 continue;
465 }
466 }
467
468 fclose(fp);
469
470 return config;
471 }
472
473 WL_EXPORT
474 const char *
weston_config_get_full_path(struct weston_config * config)475 weston_config_get_full_path(struct weston_config *config)
476 {
477 return config == NULL ? NULL : config->path;
478 }
479
480 WL_EXPORT
481 int
weston_config_next_section(struct weston_config * config,struct weston_config_section ** section,const char ** name)482 weston_config_next_section(struct weston_config *config,
483 struct weston_config_section **section,
484 const char **name)
485 {
486 if (config == NULL)
487 return 0;
488
489 if (*section == NULL)
490 *section = container_of(config->section_list.next,
491 struct weston_config_section, link);
492 else
493 *section = container_of((*section)->link.next,
494 struct weston_config_section, link);
495
496 if (&(*section)->link == &config->section_list)
497 return 0;
498
499 *name = (*section)->name;
500
501 return 1;
502 }
503
504 WL_EXPORT
505 void
weston_config_destroy(struct weston_config * config)506 weston_config_destroy(struct weston_config *config)
507 {
508 struct weston_config_section *s, *next_s;
509 struct weston_config_entry *e, *next_e;
510
511 if (config == NULL)
512 return;
513
514 wl_list_for_each_safe(s, next_s, &config->section_list, link) {
515 wl_list_for_each_safe(e, next_e, &s->entry_list, link) {
516 free(e->key);
517 free(e->value);
518 free(e);
519 }
520 free(s->name);
521 free(s);
522 }
523
524 free(config);
525 }
526