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
28 #include "configparser.h"
31 handle_key(const struct config_key *key, const char *value)
37 case CONFIG_KEY_INTEGER:
38 i = strtol(value, &end, 0);
40 fprintf(stderr, "invalid integer: %s\n", value);
43 *(int *)key->data = i;
46 case CONFIG_KEY_STRING:
51 memcpy(s, value, len - 1);
53 *(char **)key->data = s;
56 case CONFIG_KEY_BOOLEAN:
57 if (strcmp(value, "false\n") == 0)
58 *(int *)key->data = 0;
59 else if (strcmp(value, "true\n") == 0)
60 *(int *)key->data = 1;
62 fprintf(stderr, "invalid bool: %s\n", value);
74 parse_config_file(const char *path,
75 const struct config_section *sections, int num_sections,
80 const struct config_section *current = NULL;
83 fp = fopen(path, "r");
85 fprintf(stderr, "couldn't open %s\n", path);
89 while (fgets(line, sizeof line, fp)) {
90 if (line[0] == '#' || line[0] == '\n') {
92 } if (line[0] == '[') {
93 p = strchr(&line[1], ']');
94 if (!p || p[1] != '\n') {
95 fprintf(stderr, "malformed "
96 "section header: %s\n", line);
100 if (current && current->done)
103 for (i = 0; i < num_sections; i++) {
104 if (strcmp(sections[i].name, &line[1]) == 0) {
105 current = §ions[i];
109 if (i == num_sections)
111 } else if (p = strchr(line, '='), p != NULL) {
115 for (i = 0; i < current->num_keys; i++) {
116 if (strcmp(current->keys[i].name, line) == 0) {
117 if (handle_key(¤t->keys[i], &p[1]) < 0) {
125 fprintf(stderr, "malformed config line: %s\n", line);
131 if (current && current->done)
140 config_file_path(const char *name)
142 const char dotconf[] = "/.config/";
143 const char *config_dir;
144 const char *home_dir;
148 config_dir = getenv("XDG_CONFIG_HOME");
150 home_dir = getenv("HOME");
152 fprintf(stderr, "HOME is not set, using cwd.\n");
156 size = strlen(home_dir) + sizeof dotconf + strlen(name);
161 snprintf(path, size, "%s%s%s", home_dir, dotconf, name);
165 size = strlen(config_dir) + 1 + strlen(name) + 1;
170 snprintf(path, size, "%s/%s", config_dir, name);