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