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