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 "config-parser.h"
31 handle_key(const struct config_key *key, const char *value)
38 case CONFIG_KEY_INTEGER:
39 i = strtol(value, &end, 0);
41 fprintf(stderr, "invalid integer: %s\n", value);
44 *(int *)key->data = i;
47 case CONFIG_KEY_UNSIGNED_INTEGER:
48 ui = strtoul(value, &end, 0);
50 fprintf(stderr, "invalid integer: %s\n", value);
53 *(unsigned int *)key->data = ui;
56 case CONFIG_KEY_STRING:
61 memcpy(s, value, len - 1);
63 *(char **)key->data = s;
66 case CONFIG_KEY_BOOLEAN:
67 if (strcmp(value, "false\n") == 0)
68 *(int *)key->data = 0;
69 else if (strcmp(value, "true\n") == 0)
70 *(int *)key->data = 1;
72 fprintf(stderr, "invalid bool: %s\n", value);
84 parse_config_file(const char *path,
85 const struct config_section *sections, int num_sections,
90 const struct config_section *current = NULL;
93 fp = fopen(path, "r");
95 fprintf(stderr, "couldn't open %s\n", path);
99 while (fgets(line, sizeof line, fp)) {
100 if (line[0] == '#' || line[0] == '\n') {
102 } if (line[0] == '[') {
103 p = strchr(&line[1], ']');
104 if (!p || p[1] != '\n') {
105 fprintf(stderr, "malformed "
106 "section header: %s\n", line);
110 if (current && current->done)
113 for (i = 0; i < num_sections; i++) {
114 if (strcmp(sections[i].name, &line[1]) == 0) {
115 current = §ions[i];
119 if (i == num_sections)
121 } else if (p = strchr(line, '='), p != NULL) {
125 for (i = 0; i < current->num_keys; i++) {
126 if (strcmp(current->keys[i].name, line) == 0) {
127 if (handle_key(¤t->keys[i], &p[1]) < 0) {
135 fprintf(stderr, "malformed config line: %s\n", line);
141 if (current && current->done)
150 config_file_path(const char *name)
152 const char dotconf[] = "/.config/";
153 const char *config_dir;
154 const char *home_dir;
158 config_dir = getenv("XDG_CONFIG_HOME");
160 home_dir = getenv("HOME");
162 fprintf(stderr, "HOME is not set, using cwd.\n");
166 size = strlen(home_dir) + sizeof dotconf + strlen(name);
171 snprintf(path, size, "%s%s%s", home_dir, dotconf, name);
175 size = strlen(config_dir) + 1 + strlen(name) + 1;
180 snprintf(path, size, "%s/%s", config_dir, name);