Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 1 | /* |
| 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 Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <assert.h> |
Kristian Høgsberg | 3d89049 | 2012-08-03 21:56:41 -0400 | [diff] [blame] | 29 | #include <ctype.h> |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 30 | #include <limits.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 35 | #include <errno.h> |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 36 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 37 | #include <wayland-util.h> |
Kristian Høgsberg | 9b935c8 | 2011-12-08 12:44:27 -0500 | [diff] [blame] | 38 | #include "config-parser.h" |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 39 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 40 | #define container_of(ptr, type, member) ({ \ |
| 41 | const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \ |
| 42 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 43 | |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 44 | static int |
| 45 | handle_key(const struct config_key *key, const char *value) |
| 46 | { |
| 47 | char *end, *s; |
| 48 | int i, len; |
Scott Moreau | fa1de69 | 2012-01-27 13:25:49 -0700 | [diff] [blame] | 49 | unsigned int ui; |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 50 | |
| 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 Moreau | fa1de69 | 2012-01-27 13:25:49 -0700 | [diff] [blame] | 61 | 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øgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 70 | case CONFIG_KEY_STRING: |
| 71 | len = strlen(value); |
Kristian Høgsberg | 3d89049 | 2012-08-03 21:56:41 -0400 | [diff] [blame] | 72 | while (len > 0 && isspace(value[len - 1])) |
| 73 | len--; |
| 74 | s = malloc(len + 1); |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 75 | if (s == NULL) |
| 76 | return -1; |
Kristian Høgsberg | 3d89049 | 2012-08-03 21:56:41 -0400 | [diff] [blame] | 77 | memcpy(s, value, len); |
| 78 | s[len] = '\0'; |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 79 | *(char **)key->data = s; |
| 80 | return 0; |
| 81 | |
Pekka Paalanen | 28a2070 | 2011-12-08 09:24:24 +0200 | [diff] [blame] | 82 | case CONFIG_KEY_BOOLEAN: |
Pekka Paalanen | 09d65d0 | 2011-11-15 11:45:41 +0200 | [diff] [blame] | 83 | if (strcmp(value, "false\n") == 0) |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 84 | *(int *)key->data = 0; |
Pekka Paalanen | 09d65d0 | 2011-11-15 11:45:41 +0200 | [diff] [blame] | 85 | else if (strcmp(value, "true\n") == 0) |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 86 | *(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 Paalanen | 4ea4d1b | 2012-03-30 13:54:53 +0300 | [diff] [blame] | 97 | |
| 98 | return -1; |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | int |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 102 | parse_config_file(int fd, |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 103 | 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 Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 111 | if (fd == -1) |
| 112 | return -1; |
| 113 | |
| 114 | fp = fdopen(dup(fd), "r"); |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 115 | if (fp == NULL) { |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 116 | perror("couldn't open config file"); |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 117 | return -1; |
| 118 | } |
| 119 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 120 | rewind(fp); |
| 121 | |
Kristian Høgsberg | ac3a59a | 2011-11-14 22:43:37 -0500 | [diff] [blame] | 122 | 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 = §ions[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(¤t->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 Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 171 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 172 | int |
| 173 | open_config_file(const char *name) |
Pekka Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 174 | { |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 175 | 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 Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 181 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 182 | /* 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 Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 185 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 186 | /* $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 Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 194 | /* $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 Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 201 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 202 | /* 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 Paalanen | 668dd56 | 2011-11-15 11:45:40 +0200 | [diff] [blame] | 230 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 231 | |
| 232 | struct weston_config_entry { |
| 233 | char *key; |
| 234 | char *value; |
| 235 | struct wl_list link; |
| 236 | }; |
| 237 | |
| 238 | struct weston_config_section { |
| 239 | char *name; |
| 240 | struct wl_list entry_list; |
| 241 | struct wl_list link; |
| 242 | }; |
| 243 | |
| 244 | struct weston_config { |
| 245 | struct wl_list section_list; |
| 246 | }; |
| 247 | |
| 248 | static struct weston_config_entry * |
| 249 | config_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, §ion->entry_list, link) |
| 257 | if (strcmp(e->key, key) == 0) |
| 258 | return e; |
| 259 | |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 263 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 264 | struct weston_config_section * |
| 265 | weston_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-gyeong | 72a3ab7 | 2013-05-25 02:09:13 +0900 | [diff] [blame] | 271 | if (config == NULL) |
| 272 | return NULL; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 273 | 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 Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 286 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 287 | int |
| 288 | weston_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 Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 312 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 313 | int |
| 314 | weston_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 Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 338 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 339 | int |
| 340 | weston_config_section_get_string(struct weston_config_section *section, |
| 341 | const char *key, |
| 342 | char **value, const char *default_value) |
| 343 | { |
| 344 | struct weston_config_entry *entry; |
| 345 | |
| 346 | entry = config_section_get_entry(section, key); |
| 347 | if (entry == NULL) { |
| 348 | if (default_value) |
| 349 | *value = strdup(default_value); |
| 350 | else |
| 351 | *value = NULL; |
| 352 | errno = ENOENT; |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | *value = strdup(entry->value); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Quentin Glidic | 6e2c124 | 2013-07-01 17:03:08 +0200 | [diff] [blame] | 361 | WL_EXPORT |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 362 | int |
| 363 | weston_config_section_get_bool(struct weston_config_section *section, |
| 364 | const char *key, |
| 365 | int *value, int default_value) |
| 366 | { |
| 367 | struct weston_config_entry *entry; |
| 368 | |
| 369 | entry = config_section_get_entry(section, key); |
| 370 | if (entry == NULL) { |
| 371 | *value = default_value; |
| 372 | errno = ENOENT; |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | if (strcmp(entry->value, "false") == 0) |
| 377 | *value = 0; |
| 378 | else if (strcmp(entry->value, "true") == 0) |
| 379 | *value = 1; |
| 380 | else { |
| 381 | *value = default_value; |
| 382 | errno = EINVAL; |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static struct weston_config_section * |
| 390 | config_add_section(struct weston_config *config, const char *name) |
| 391 | { |
| 392 | struct weston_config_section *section; |
| 393 | |
| 394 | section = malloc(sizeof *section); |
| 395 | section->name = strdup(name); |
| 396 | wl_list_init(§ion->entry_list); |
| 397 | wl_list_insert(config->section_list.prev, §ion->link); |
| 398 | |
| 399 | return section; |
| 400 | } |
| 401 | |
| 402 | static struct weston_config_entry * |
| 403 | section_add_entry(struct weston_config_section *section, |
| 404 | const char *key, const char *value) |
| 405 | { |
| 406 | struct weston_config_entry *entry; |
| 407 | |
| 408 | entry = malloc(sizeof *entry); |
| 409 | entry->key = strdup(key); |
| 410 | entry->value = strdup(value); |
| 411 | wl_list_insert(section->entry_list.prev, &entry->link); |
| 412 | |
| 413 | return entry; |
| 414 | } |
| 415 | |
| 416 | struct weston_config * |
| 417 | weston_config_parse(int fd) |
| 418 | { |
| 419 | FILE *fp; |
| 420 | char line[512], *p; |
| 421 | struct weston_config *config; |
| 422 | struct weston_config_section *section = NULL; |
| 423 | int i; |
| 424 | |
| 425 | config = malloc(sizeof *config); |
| 426 | if (config == NULL) |
| 427 | return NULL; |
| 428 | |
| 429 | wl_list_init(&config->section_list); |
| 430 | |
| 431 | fp = fdopen(dup(fd), "r"); |
| 432 | if (fp == NULL) { |
| 433 | free(config); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | rewind(fp); |
| 438 | |
| 439 | while (fgets(line, sizeof line, fp)) { |
| 440 | switch (line[0]) { |
| 441 | case '#': |
| 442 | case '\n': |
| 443 | continue; |
| 444 | case '[': |
| 445 | p = strchr(&line[1], ']'); |
| 446 | if (!p || p[1] != '\n') { |
| 447 | fprintf(stderr, "malformed " |
| 448 | "section header: %s\n", line); |
| 449 | fclose(fp); |
| 450 | weston_config_destroy(config); |
| 451 | return NULL; |
| 452 | } |
| 453 | p[0] = '\0'; |
| 454 | section = config_add_section(config, &line[1]); |
| 455 | continue; |
| 456 | default: |
| 457 | p = strchr(line, '='); |
| 458 | if (!p || p == line || !section) { |
| 459 | fprintf(stderr, "malformed " |
| 460 | "config line: %s\n", line); |
| 461 | fclose(fp); |
| 462 | weston_config_destroy(config); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | p[0] = '\0'; |
| 467 | p++; |
| 468 | while (isspace(*p)) |
| 469 | p++; |
| 470 | i = strlen(p); |
| 471 | while (i > 0 && isspace(p[i - 1])) { |
| 472 | p[i - 1] = '\0'; |
| 473 | i--; |
| 474 | } |
| 475 | section_add_entry(section, line, p); |
| 476 | continue; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | fclose(fp); |
| 481 | |
| 482 | return config; |
| 483 | } |
| 484 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 485 | int |
| 486 | weston_config_next_section(struct weston_config *config, |
| 487 | struct weston_config_section **section, |
| 488 | const char **name) |
| 489 | { |
Mun Gwan-gyeong | 151a528 | 2013-05-28 00:04:26 +0900 | [diff] [blame] | 490 | if (config == NULL) |
| 491 | return 0; |
| 492 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 493 | if (*section == NULL) |
| 494 | *section = container_of(config->section_list.next, |
| 495 | struct weston_config_section, link); |
| 496 | else |
| 497 | *section = container_of((*section)->link.next, |
| 498 | struct weston_config_section, link); |
| 499 | |
| 500 | if (&(*section)->link == &config->section_list) |
| 501 | return 0; |
| 502 | |
| 503 | *name = (*section)->name; |
| 504 | |
| 505 | return 1; |
| 506 | } |
| 507 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 508 | void |
| 509 | weston_config_destroy(struct weston_config *config) |
| 510 | { |
| 511 | struct weston_config_section *s, *next_s; |
| 512 | struct weston_config_entry *e, *next_e; |
| 513 | |
Mun Gwan-gyeong | 7732540 | 2013-05-28 00:20:04 +0900 | [diff] [blame] | 514 | if (config == NULL) |
| 515 | return; |
| 516 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 517 | wl_list_for_each_safe(s, next_s, &config->section_list, link) { |
| 518 | wl_list_for_each_safe(e, next_e, &s->entry_list, link) { |
| 519 | free(e->key); |
| 520 | free(e->value); |
| 521 | free(e); |
| 522 | } |
| 523 | free(s->name); |
| 524 | free(s); |
| 525 | } |
| 526 | |
| 527 | free(config); |
| 528 | } |