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