summaryrefslogtreecommitdiffstats
path: root/backend/conf.c
blob: 365ceb08c62896913ba7251915afdd3ffac9329f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "conf.h"
#include "log.h"
#include "util.h"
#include "fileaccess.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>


static inline uint_fast8_t conf_hash(const char *str)
{
	return tiny_hash(str) & CONF_HASHTAB_MASK;
}

#define hashtab_add(tab, element)					\
	do {								\
		unsigned int _hash = conf_hash((element)->name);	\
		typeof(*(element)) *a, *b;				\
		(element)->next = NULL;					\
		if (tab[_hash]) {					\
			b = tab[_hash];					\
			for (; b; a = b, b = b->next);			\
			a->next = (element);				\
		} else							\
			tab[_hash] = (element);				\
	} while (0)

#define hashtab_get(tab, element_name)					\
	({								\
		typeof(*(tab[0])) *_e;					\
		for (_e = tab[conf_hash(element_name)];			\
		     _e && strcmp(_e->name, (element_name)) != 0;	\
		     _e = _e->next);					\
		_e;							\
	})

const char * config_get(struct config_file *f,
			const char *section,
			const char *item,
			const char *_default)
{
	struct config_section *s;
	struct config_item *i;

	if (!f || !section || !item)
		return _default;

	s = hashtab_get(f->section_hashtab, section);
	if (s) {
		i = hashtab_get(s->item_hashtab, item);
		if (i)
			return i->value;
	}

	return _default;
}

static int string_to_int(const char *string, int *i)
{
	char *tailptr;
	long retval;

	retval = strtol(string, &tailptr, 0);
	if (tailptr == string || tailptr[0] != '\0')
		return -EINVAL;
	*i = retval;

	return 0;
}

int config_get_int(struct config_file *f,
		   const char *section,
		   const char *item,
		   int _default)
{
	const char *value;
	int i;

	value = config_get(f, section, item, NULL);
	if (!value)
		return _default;
	if (string_to_int(value, &i))
		return _default;

	return i;
}

int config_get_bool(struct config_file *f,
		    const char *section,
		    const char *item,
		    int _default)
{
	const char *value;
	int i;

	value = config_get(f, section, item, NULL);
	if (!value)
		return _default;
	if (!string_to_int(value, &i))
		return !!i;
	if (strcasecmp(value, "yes") == 0 ||
	    strcasecmp(value, "true") == 0 ||
	    strcasecmp(value, "on") == 0)
		return 1;
	if (strcasecmp(value, "no") == 0 ||
	    strcasecmp(value, "false") == 0 ||
	    strcasecmp(value, "off") == 0)
		return 0;

	return _default;
}

static void dump_config_tree(struct config_file *f)
{
	struct config_section *s;
	struct config_item *i;
	unsigned int j, k;
	int sect_coll, item_coll;
	const char *collstr = "\t\t<== ### COLLISION ###";

	logverbose("Parsed config file:\n");
	for (j = 0; j < CONF_HASHTAB_SIZE; j++) {
		sect_coll = 0;
		for (s = f->section_hashtab[j]; s; s = s->next) {
			logverbose("\t[%02X] Section '%s'%s\n",
				   j & 0xFF, s->name,
				   sect_coll ? collstr : "");
			for (k = 0; k < CONF_HASHTAB_SIZE; k++) {
				item_coll = 0;
				for (i = s->item_hashtab[k]; i; i = i->next) {
					logverbose("\t\t[%02X] Item '%s'%s\n",
						   k & 0xFF, i->name,
						   item_coll ? collstr : "");
					item_coll = 1;
				}
			}
			sect_coll = 1;
		}
	}
}

struct config_file * config_file_parse(const char *path)
{
	struct config_file *retval = NULL, *c;
	struct config_section *s = NULL;
	struct config_item *i;
	struct fileaccess *fa = NULL;
	LIST_HEAD(lines);
	struct text_line *l;
	char *line, *name, *value;
	int err;
	size_t len;
	unsigned int lineno = 0;

	c = zalloc(sizeof(*c));
	if (!c)
		goto out_error;

	fa = file_open(O_RDONLY, path);
	if (!fa)
		goto out_ok;
	err = file_read_text_lines(fa, &lines, 1);
	if (err) {
		logerr("Failed to read config file %s: %s\n",
		       path, strerror(-err));
		goto out_error;
	}

	list_for_each_entry(l, &lines, list) {
		lineno++;
		line = l->text;
		len = strlen(line);
		if (!len)
			continue;
		if (line[0] == '#') /* comment */
			continue;
		if (len >= 3 && line[0] == '[' && line[len - 1] == ']') {
			/* New section */
			s = zalloc(sizeof(*s));
			if (!s)
				goto error_unwind;
			s->file = c;
			line[len - 1] = '\0';
			s->name = strdup(line + 1);
			if (!s->name) {
				free(s);
				goto error_unwind;
			}
			hashtab_add(c->section_hashtab, s);
			continue;
		}
		if (!s) {
			logerr("%s:%u: Stray characters\n", path, lineno);
			goto error_unwind;
		}
		/* Config item in section */
		value = strchr(line, '=');
		if (!value) {
			logerr("%s:%u: Invalid config item\n", path, lineno);
			goto error_unwind;
		}
		value[0] = '\0';
		value++;
		name = line;
		i = zalloc(sizeof(*i));
		if (!i)
			goto error_unwind;
		i->section = s;
		i->name = strdup(name);
		if (!i->name) {
			free(i);
			goto error_unwind;
		}
		i->value = strdup(value);
		if (!i->value) {
			free(i->name);
			free(i);
			goto error_unwind;
		}
		hashtab_add(s->item_hashtab, i);
	}
out_ok:
	retval = c;
	if (loglevel_is_verbose())
		dump_config_tree(retval);

out_error:
	text_lines_free(&lines);
	file_close(fa);

	return retval;

error_unwind:
	config_file_free(c);
	goto out_error;
}

static void free_item(struct config_item *i)
{
	if (i) {
		free(i->name);
		free(i->value);
		free(i);
	}
}

static void free_section(struct config_section *s)
{
	unsigned int i;
	struct config_item *it, *next;

	if (s) {
		for (i = 0; i < CONF_HASHTAB_SIZE; i++) {
			for (it = s->item_hashtab[i]; it; it = next) {
				next = it->next;
				free_item(it);
			}
		}
		free(s->name);
		free(s);
	}
}

void config_file_free(struct config_file *f)
{
	unsigned int i;
	struct config_section *s, *next;

	if (f) {
		for (i = 0; i < CONF_HASHTAB_SIZE; i++) {
			for (s = f->section_hashtab[i]; s; s = next) {
				next = s->next;
				free_section(s);
			}
		}
		free(f);
	}
}
bues.ch cgit interface