2 * Copyright © 2011 Intel Corporation
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.
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
29 #include "config-parser.h"
32 handle_key(const struct config_key *key, const char *value)
39 case CONFIG_KEY_INTEGER:
40 i = strtol(value, &end, 0);
42 fprintf(stderr, "invalid integer: %s\n", value);
45 *(int *)key->data = i;
48 case CONFIG_KEY_UNSIGNED_INTEGER:
49 ui = strtoul(value, &end, 0);
51 fprintf(stderr, "invalid integer: %s\n", value);
54 *(unsigned int *)key->data = ui;
57 case CONFIG_KEY_STRING:
59 while (len > 0 && isspace(value[len - 1]))
64 memcpy(s, value, len);
66 *(char **)key->data = s;
69 case CONFIG_KEY_BOOLEAN:
70 if (strcmp(value, "false\n") == 0)
71 *(int *)key->data = 0;
72 else if (strcmp(value, "true\n") == 0)
73 *(int *)key->data = 1;
75 fprintf(stderr, "invalid bool: %s\n", value);
89 parse_config_file(const char *path,
90 const struct config_section *sections, int num_sections,
95 const struct config_section *current = NULL;
98 fp = fopen(path, "r");
100 fprintf(stderr, "couldn't open %s\n", path);
104 while (fgets(line, sizeof line, fp)) {
105 if (line[0] == '#' || line[0] == '\n') {
107 } if (line[0] == '[') {
108 p = strchr(&line[1], ']');
109 if (!p || p[1] != '\n') {
110 fprintf(stderr, "malformed "
111 "section header: %s\n", line);
115 if (current && current->done)
118 for (i = 0; i < num_sections; i++) {
119 if (strcmp(sections[i].name, &line[1]) == 0) {
120 current = §ions[i];
124 if (i == num_sections)
126 } else if (p = strchr(line, '='), p != NULL) {
130 for (i = 0; i < current->num_keys; i++) {
131 if (strcmp(current->keys[i].name, line) == 0) {
132 if (handle_key(¤t->keys[i], &p[1]) < 0) {
140 fprintf(stderr, "malformed config line: %s\n", line);
146 if (current && current->done)
155 config_file_path(const char *name)
157 const char dotconf[] = "/.config/";
158 const char *config_dir;
159 const char *home_dir;
163 config_dir = getenv("XDG_CONFIG_HOME");
165 home_dir = getenv("HOME");
167 fprintf(stderr, "HOME is not set, using cwd.\n");
171 size = strlen(home_dir) + sizeof dotconf + strlen(name);
176 snprintf(path, size, "%s%s%s", home_dir, dotconf, name);
180 size = strlen(config_dir) + 1 + strlen(name) + 1;
185 snprintf(path, size, "%s/%s", config_dir, name);