Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 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: |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 11 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 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. |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <assert.h> |
Kristian Høgsberg | 3d89049 | 2012-08-03 21:56:41 -0400 | [diff] [blame] | 32 | #include <ctype.h> |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 33 | #include <limits.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <unistd.h> |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 38 | #include <errno.h> |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 39 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 40 | #include <wayland-util.h> |
Kristian Høgsberg | 9b935c8 | 2011-12-08 12:44:27 -0500 | [diff] [blame] | 41 | #include "config-parser.h" |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 42 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 43 | #define container_of(ptr, type, member) ({ \ |
| 44 | const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \ |
| 45 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 46 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 47 | struct weston_config_entry { |
| 48 | char *key; |
| 49 | char *value; |
| 50 | struct wl_list link; |
| 51 | }; |
| 52 | |
| 53 | struct weston_config_section { |
| 54 | char *name; |
| 55 | struct wl_list entry_list; |
| 56 | struct wl_list link; |
| 57 | }; |
| 58 | |
| 59 | struct weston_config { |
| 60 | struct wl_list section_list; |
Kristian Høgsberg | eeefc9e | 2013-09-21 23:17:35 -0700 | [diff] [blame] | 61 | char path[PATH_MAX]; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 62 | }; |
| 63 | |
Kristian Høgsberg | eeefc9e | 2013-09-21 23:17:35 -0700 | [diff] [blame] | 64 | static int |
| 65 | open_config_file(struct weston_config *c, const char *name) |
| 66 | { |
| 67 | const char *config_dir = getenv("XDG_CONFIG_HOME"); |
| 68 | const char *home_dir = getenv("HOME"); |
| 69 | const char *config_dirs = getenv("XDG_CONFIG_DIRS"); |
| 70 | const char *p, *next; |
| 71 | int fd; |
| 72 | |
| 73 | if (name[0] == '/') { |
| 74 | snprintf(c->path, sizeof c->path, "%s", name); |
| 75 | return open(name, O_RDONLY | O_CLOEXEC); |
| 76 | } |
| 77 | |
| 78 | /* Precedence is given to config files in the home directory, |
| 79 | * and then to directories listed in XDG_CONFIG_DIRS and |
| 80 | * finally to the current working directory. */ |
| 81 | |
| 82 | /* $XDG_CONFIG_HOME */ |
| 83 | if (config_dir) { |
| 84 | snprintf(c->path, sizeof c->path, "%s/%s", config_dir, name); |
| 85 | fd = open(c->path, O_RDONLY | O_CLOEXEC); |
| 86 | if (fd >= 0) |
| 87 | return fd; |
| 88 | } |
| 89 | |
| 90 | /* $HOME/.config */ |
| 91 | if (home_dir) { |
| 92 | snprintf(c->path, sizeof c->path, |
| 93 | "%s/.config/%s", home_dir, name); |
| 94 | fd = open(c->path, O_RDONLY | O_CLOEXEC); |
| 95 | if (fd >= 0) |
| 96 | return fd; |
| 97 | } |
| 98 | |
| 99 | /* For each $XDG_CONFIG_DIRS: weston/<config_file> */ |
| 100 | if (!config_dirs) |
| 101 | config_dirs = "/etc/xdg"; /* See XDG base dir spec. */ |
| 102 | |
| 103 | for (p = config_dirs; *p != '\0'; p = next) { |
| 104 | next = strchrnul(p, ':'); |
| 105 | snprintf(c->path, sizeof c->path, |
| 106 | "%.*s/weston/%s", (int)(next - p), p, name); |
| 107 | fd = open(c->path, O_RDONLY | O_CLOEXEC); |
| 108 | if (fd >= 0) |
| 109 | return fd; |
| 110 | |
| 111 | if (*next == ':') |
| 112 | next++; |
| 113 | } |
| 114 | |
| 115 | /* Current working directory. */ |
| 116 | snprintf(c->path, sizeof c->path, "./%s", name); |
| 117 | |
| 118 | return open(c->path, O_RDONLY | O_CLOEXEC); |
| 119 | } |
| 120 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 121 | static struct weston_config_entry * |
| 122 | config_section_get_entry(struct weston_config_section *section, |
| 123 | const char *key) |
| 124 | { |
| 125 | struct weston_config_entry *e; |
| 126 | |
| 127 | if (section == NULL) |
| 128 | return NULL; |
| 129 | wl_list_for_each(e, §ion->entry_list, link) |
| 130 | if (strcmp(e->key, key) == 0) |
| 131 | return e; |
| 132 | |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 136 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 137 | struct weston_config_section * |
| 138 | weston_config_get_section(struct weston_config *config, const char *section, |
| 139 | const char *key, const char *value) |
| 140 | { |
| 141 | struct weston_config_section *s; |
| 142 | struct weston_config_entry *e; |
| 143 | |
Mun Gwan-gyeong | 72a3ab7 | 2013-05-25 02:09:13 +0900 | [diff] [blame] | 144 | if (config == NULL) |
| 145 | return NULL; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 146 | wl_list_for_each(s, &config->section_list, link) { |
| 147 | if (strcmp(s->name, section) != 0) |
| 148 | continue; |
| 149 | if (key == NULL) |
| 150 | return s; |
| 151 | e = config_section_get_entry(s, key); |
| 152 | if (e && strcmp(e->value, value) == 0) |
| 153 | return s; |
| 154 | } |
| 155 | |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 159 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 160 | int |
| 161 | weston_config_section_get_int(struct weston_config_section *section, |
| 162 | const char *key, |
| 163 | int32_t *value, int32_t default_value) |
| 164 | { |
| 165 | struct weston_config_entry *entry; |
| 166 | char *end; |
| 167 | |
| 168 | entry = config_section_get_entry(section, key); |
| 169 | if (entry == NULL) { |
| 170 | *value = default_value; |
| 171 | errno = ENOENT; |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | *value = strtol(entry->value, &end, 0); |
| 176 | if (*end != '\0') { |
| 177 | *value = default_value; |
| 178 | errno = EINVAL; |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 185 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 186 | int |
| 187 | weston_config_section_get_uint(struct weston_config_section *section, |
| 188 | const char *key, |
| 189 | uint32_t *value, uint32_t default_value) |
| 190 | { |
| 191 | struct weston_config_entry *entry; |
| 192 | char *end; |
| 193 | |
| 194 | entry = config_section_get_entry(section, key); |
| 195 | if (entry == NULL) { |
| 196 | *value = default_value; |
| 197 | errno = ENOENT; |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | *value = strtoul(entry->value, &end, 0); |
| 202 | if (*end != '\0') { |
| 203 | *value = default_value; |
| 204 | errno = EINVAL; |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 211 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 212 | int |
Armin K | b502f90 | 2013-07-31 01:41:03 +0200 | [diff] [blame] | 213 | weston_config_section_get_double(struct weston_config_section *section, |
| 214 | const char *key, |
| 215 | double *value, double default_value) |
| 216 | { |
| 217 | struct weston_config_entry *entry; |
| 218 | char *end; |
| 219 | |
| 220 | entry = config_section_get_entry(section, key); |
| 221 | if (entry == NULL) { |
| 222 | *value = default_value; |
| 223 | errno = ENOENT; |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | *value = strtod(entry->value, &end); |
| 228 | if (*end != '\0') { |
| 229 | *value = default_value; |
| 230 | errno = EINVAL; |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | WL_EXPORT |
| 238 | int |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 239 | weston_config_section_get_string(struct weston_config_section *section, |
| 240 | const char *key, |
| 241 | char **value, const char *default_value) |
| 242 | { |
| 243 | struct weston_config_entry *entry; |
| 244 | |
| 245 | entry = config_section_get_entry(section, key); |
| 246 | if (entry == NULL) { |
| 247 | if (default_value) |
| 248 | *value = strdup(default_value); |
| 249 | else |
| 250 | *value = NULL; |
| 251 | errno = ENOENT; |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | *value = strdup(entry->value); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 260 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 261 | int |
| 262 | weston_config_section_get_bool(struct weston_config_section *section, |
| 263 | const char *key, |
| 264 | int *value, int default_value) |
| 265 | { |
| 266 | struct weston_config_entry *entry; |
| 267 | |
| 268 | entry = config_section_get_entry(section, key); |
| 269 | if (entry == NULL) { |
| 270 | *value = default_value; |
| 271 | errno = ENOENT; |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | if (strcmp(entry->value, "false") == 0) |
| 276 | *value = 0; |
| 277 | else if (strcmp(entry->value, "true") == 0) |
| 278 | *value = 1; |
| 279 | else { |
| 280 | *value = default_value; |
| 281 | errno = EINVAL; |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
Derek Foreman | c721043 | 2014-08-21 11:32:38 -0500 | [diff] [blame] | 288 | WL_EXPORT |
| 289 | const char * |
| 290 | weston_config_get_libexec_dir(void) |
| 291 | { |
| 292 | const char *path = getenv("WESTON_BUILD_DIR"); |
| 293 | |
| 294 | if (path) |
| 295 | return path; |
| 296 | |
| 297 | return LIBEXECDIR; |
| 298 | } |
| 299 | |
Pekka Paalanen | 6c71aae | 2015-03-24 15:56:19 +0200 | [diff] [blame] | 300 | const char * |
| 301 | weston_config_get_name_from_env(void) |
| 302 | { |
| 303 | const char *name; |
| 304 | |
| 305 | name = getenv(WESTON_CONFIG_FILE_ENV_VAR); |
| 306 | if (name) |
| 307 | return name; |
| 308 | |
| 309 | return "weston.ini"; |
| 310 | } |
| 311 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 312 | static struct weston_config_section * |
| 313 | config_add_section(struct weston_config *config, const char *name) |
| 314 | { |
| 315 | struct weston_config_section *section; |
| 316 | |
| 317 | section = malloc(sizeof *section); |
| 318 | section->name = strdup(name); |
| 319 | wl_list_init(§ion->entry_list); |
| 320 | wl_list_insert(config->section_list.prev, §ion->link); |
| 321 | |
| 322 | return section; |
| 323 | } |
| 324 | |
| 325 | static struct weston_config_entry * |
| 326 | section_add_entry(struct weston_config_section *section, |
| 327 | const char *key, const char *value) |
| 328 | { |
| 329 | struct weston_config_entry *entry; |
| 330 | |
| 331 | entry = malloc(sizeof *entry); |
| 332 | entry->key = strdup(key); |
| 333 | entry->value = strdup(value); |
| 334 | wl_list_insert(section->entry_list.prev, &entry->link); |
| 335 | |
| 336 | return entry; |
| 337 | } |
| 338 | |
| 339 | struct weston_config * |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 340 | weston_config_parse(const char *name) |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 341 | { |
| 342 | FILE *fp; |
| 343 | char line[512], *p; |
Pekka Paalanen | 49f6d62 | 2015-03-24 15:56:17 +0200 | [diff] [blame] | 344 | struct stat filestat; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 345 | struct weston_config *config; |
| 346 | struct weston_config_section *section = NULL; |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 347 | int i, fd; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 348 | |
| 349 | config = malloc(sizeof *config); |
| 350 | if (config == NULL) |
| 351 | return NULL; |
| 352 | |
| 353 | wl_list_init(&config->section_list); |
| 354 | |
Kristian Høgsberg | eeefc9e | 2013-09-21 23:17:35 -0700 | [diff] [blame] | 355 | fd = open_config_file(config, name); |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 356 | if (fd == -1) { |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 357 | free(config); |
| 358 | return NULL; |
| 359 | } |
| 360 | |
Pekka Paalanen | 49f6d62 | 2015-03-24 15:56:17 +0200 | [diff] [blame] | 361 | if (fstat(fd, &filestat) < 0 || |
| 362 | !S_ISREG(filestat.st_mode)) { |
| 363 | close(fd); |
| 364 | free(config); |
| 365 | return NULL; |
| 366 | } |
| 367 | |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 368 | fp = fdopen(fd, "r"); |
| 369 | if (fp == NULL) { |
| 370 | free(config); |
| 371 | return NULL; |
| 372 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 373 | |
| 374 | while (fgets(line, sizeof line, fp)) { |
| 375 | switch (line[0]) { |
| 376 | case '#': |
| 377 | case '\n': |
| 378 | continue; |
| 379 | case '[': |
| 380 | p = strchr(&line[1], ']'); |
| 381 | if (!p || p[1] != '\n') { |
| 382 | fprintf(stderr, "malformed " |
| 383 | "section header: %s\n", line); |
| 384 | fclose(fp); |
| 385 | weston_config_destroy(config); |
| 386 | return NULL; |
| 387 | } |
| 388 | p[0] = '\0'; |
| 389 | section = config_add_section(config, &line[1]); |
| 390 | continue; |
| 391 | default: |
| 392 | p = strchr(line, '='); |
| 393 | if (!p || p == line || !section) { |
| 394 | fprintf(stderr, "malformed " |
| 395 | "config line: %s\n", line); |
| 396 | fclose(fp); |
| 397 | weston_config_destroy(config); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | p[0] = '\0'; |
| 402 | p++; |
| 403 | while (isspace(*p)) |
| 404 | p++; |
| 405 | i = strlen(p); |
| 406 | while (i > 0 && isspace(p[i - 1])) { |
| 407 | p[i - 1] = '\0'; |
| 408 | i--; |
| 409 | } |
| 410 | section_add_entry(section, line, p); |
| 411 | continue; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | fclose(fp); |
| 416 | |
| 417 | return config; |
| 418 | } |
| 419 | |
Kristian Høgsberg | eeefc9e | 2013-09-21 23:17:35 -0700 | [diff] [blame] | 420 | const char * |
| 421 | weston_config_get_full_path(struct weston_config *config) |
| 422 | { |
Alexandru DAMIAN | 5f42930 | 2013-09-26 10:27:16 +0100 | [diff] [blame] | 423 | return config == NULL ? NULL : config->path; |
Kristian Høgsberg | eeefc9e | 2013-09-21 23:17:35 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 426 | int |
| 427 | weston_config_next_section(struct weston_config *config, |
| 428 | struct weston_config_section **section, |
| 429 | const char **name) |
| 430 | { |
Mun Gwan-gyeong | 151a528 | 2013-05-28 00:04:26 +0900 | [diff] [blame] | 431 | if (config == NULL) |
| 432 | return 0; |
| 433 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 434 | if (*section == NULL) |
| 435 | *section = container_of(config->section_list.next, |
| 436 | struct weston_config_section, link); |
| 437 | else |
| 438 | *section = container_of((*section)->link.next, |
| 439 | struct weston_config_section, link); |
| 440 | |
| 441 | if (&(*section)->link == &config->section_list) |
| 442 | return 0; |
| 443 | |
| 444 | *name = (*section)->name; |
| 445 | |
| 446 | return 1; |
| 447 | } |
| 448 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 449 | void |
| 450 | weston_config_destroy(struct weston_config *config) |
| 451 | { |
| 452 | struct weston_config_section *s, *next_s; |
| 453 | struct weston_config_entry *e, *next_e; |
| 454 | |
Mun Gwan-gyeong | 7732540 | 2013-05-28 00:20:04 +0900 | [diff] [blame] | 455 | if (config == NULL) |
| 456 | return; |
| 457 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 458 | wl_list_for_each_safe(s, next_s, &config->section_list, link) { |
| 459 | wl_list_for_each_safe(e, next_e, &s->entry_list, link) { |
| 460 | free(e->key); |
| 461 | free(e->value); |
| 462 | free(e); |
| 463 | } |
| 464 | free(s->name); |
| 465 | free(s); |
| 466 | } |
| 467 | |
| 468 | free(config); |
| 469 | } |