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