blob: 5ffa466449f428c9100d895df4c8b41d0852403e [file] [log] [blame]
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001/*
2 * Copyright © 2011 Intel Corporation
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#include <string.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <assert.h>
27
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050028#include "config-parser.h"
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050029
30static int
31handle_key(const struct config_key *key, const char *value)
32{
33 char *end, *s;
34 int i, len;
Scott Moreaufa1de692012-01-27 13:25:49 -070035 unsigned int ui;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050036
37 switch (key->type) {
38 case CONFIG_KEY_INTEGER:
39 i = strtol(value, &end, 0);
40 if (*end != '\n') {
41 fprintf(stderr, "invalid integer: %s\n", value);
42 return -1;
43 }
44 *(int *)key->data = i;
45 return 0;
46
Scott Moreaufa1de692012-01-27 13:25:49 -070047 case CONFIG_KEY_UNSIGNED_INTEGER:
48 ui = strtoul(value, &end, 0);
49 if (*end != '\n') {
50 fprintf(stderr, "invalid integer: %s\n", value);
51 return -1;
52 }
53 *(unsigned int *)key->data = ui;
54 return 0;
55
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050056 case CONFIG_KEY_STRING:
57 len = strlen(value);
58 s = malloc(len);
59 if (s == NULL)
60 return -1;
61 memcpy(s, value, len - 1);
62 s[len - 1] = '\0';
63 *(char **)key->data = s;
64 return 0;
65
Pekka Paalanen28a20702011-12-08 09:24:24 +020066 case CONFIG_KEY_BOOLEAN:
Pekka Paalanen09d65d02011-11-15 11:45:41 +020067 if (strcmp(value, "false\n") == 0)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050068 *(int *)key->data = 0;
Pekka Paalanen09d65d02011-11-15 11:45:41 +020069 else if (strcmp(value, "true\n") == 0)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050070 *(int *)key->data = 1;
71 else {
72 fprintf(stderr, "invalid bool: %s\n", value);
73 return -1;
74 }
75 return 0;
76
77 default:
78 assert(0);
79 break;
80 }
Pekka Paalanen4ea4d1b2012-03-30 13:54:53 +030081
82 return -1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050083}
84
85int
86parse_config_file(const char *path,
87 const struct config_section *sections, int num_sections,
88 void *data)
89{
90 FILE *fp;
91 char line[512], *p;
92 const struct config_section *current = NULL;
93 int i;
94
95 fp = fopen(path, "r");
96 if (fp == NULL) {
97 fprintf(stderr, "couldn't open %s\n", path);
98 return -1;
99 }
100
101 while (fgets(line, sizeof line, fp)) {
102 if (line[0] == '#' || line[0] == '\n') {
103 continue;
104 } if (line[0] == '[') {
105 p = strchr(&line[1], ']');
106 if (!p || p[1] != '\n') {
107 fprintf(stderr, "malformed "
108 "section header: %s\n", line);
109 fclose(fp);
110 return -1;
111 }
112 if (current && current->done)
113 current->done(data);
114 p[0] = '\0';
115 for (i = 0; i < num_sections; i++) {
116 if (strcmp(sections[i].name, &line[1]) == 0) {
117 current = &sections[i];
118 break;
119 }
120 }
121 if (i == num_sections)
122 current = NULL;
123 } else if (p = strchr(line, '='), p != NULL) {
124 if (current == NULL)
125 continue;
126 p[0] = '\0';
127 for (i = 0; i < current->num_keys; i++) {
128 if (strcmp(current->keys[i].name, line) == 0) {
129 if (handle_key(&current->keys[i], &p[1]) < 0) {
130 fclose(fp);
131 return -1;
132 }
133 break;
134 }
135 }
136 } else {
137 fprintf(stderr, "malformed config line: %s\n", line);
138 fclose(fp);
139 return -1;
140 }
141 }
142
143 if (current && current->done)
144 current->done(data);
145
146 fclose(fp);
147
148 return 0;
149}
Pekka Paalanen668dd562011-11-15 11:45:40 +0200150
151char *
152config_file_path(const char *name)
153{
154 const char dotconf[] = "/.config/";
155 const char *config_dir;
156 const char *home_dir;
157 char *path;
158 size_t size;
159
160 config_dir = getenv("XDG_CONFIG_HOME");
161 if (!config_dir) {
Pekka Paalanen668dd562011-11-15 11:45:40 +0200162 home_dir = getenv("HOME");
163 if (!home_dir) {
164 fprintf(stderr, "HOME is not set, using cwd.\n");
165 return strdup(name);
166 }
167
168 size = strlen(home_dir) + sizeof dotconf + strlen(name);
169 path = malloc(size);
170 if (!path)
171 return NULL;
172
173 snprintf(path, size, "%s%s%s", home_dir, dotconf, name);
174 return path;
175 }
176
177 size = strlen(config_dir) + 1 + strlen(name) + 1;
178 path = malloc(size);
179 if (!path)
180 return NULL;
181
182 snprintf(path, size, "%s/%s", config_dir, name);
183 return path;
184}