summaryrefslogtreecommitdiff
path: root/accel-dp/conf_file.c
blob: c22cfd5d11590868a9d0a6ee79607aac6b0d34dc (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "conf_file.h"

struct conf_ctx {
	const char *fname;
	FILE *file;
	int line;
	struct conf_opt **list;
	int options:1;
};

static struct conf_sect *sect_head;
static struct conf_sect *sect_tail;

static char* skip_space(char *str);
static char* skip_word(char *str);

static struct conf_sect *find_sect(const char *name);
static struct conf_sect *create_sect(const char *name);
static int sect_add_item(struct conf_ctx *ctx, const char *name, char *val, char *raw);
static struct conf_opt *find_item(struct conf_opt *list, const char *name);
static int load_file(struct conf_ctx *ctx);

static char *buf;
static struct conf_sect *cur_sect;

static int open_ctx(struct conf_ctx *ctx, const char *fname, struct conf_opt **list)
{
	ctx->file = fopen(fname, "r");
	if (!ctx->file)
		return -1;

	ctx->fname = fname;
	ctx->line = 0;
	ctx->list = ctx->list;
	ctx->options = 0;

	return 0;
}

static void close_ctx(struct conf_ctx *ctx)
{
	fclose(ctx->file);
}

static int load_file(struct conf_ctx *ctx)
{
	char *str, *str2, *raw;
	int len;

	while(1) {
		if (!fgets(buf, 1024, ctx->file))
			break;
		ctx->line++;

		len = strlen(buf);
		if (buf[len - 1] == '\n')
			buf[--len] = 0;

		while (len && (buf[len - 1] == ' ' || buf[len - 1] == '\t'))
			buf[--len] = 0;

		str = skip_space(buf);
		if (*str == '#' || *str == 0)
			continue;

		if (strncmp(str, "$include", 8) == 0)	{
			struct conf_ctx ctx1;
			int r;
			str = skip_word(str);
			str = skip_space(str);

			if (open_ctx(&ctx1, str, ctx->list))
				break;

			r = load_file(&ctx1);

			close_ctx(&ctx1);

			if (r)
				break;

			ctx->list = ctx1.list;

			continue;
		}

		if (*str == '[') {
			for (str2 = ++str; *str2 && *str2 != ']'; str2++);
			if (*str2 != ']') {
				fprintf(stderr, "conf_file:%s:%i: sintax error\n", ctx->fname, ctx->line);
				return -1;
			}

			if (ctx->options) {
				fprintf(stderr, "conf_file:%s:%i: cann't open section inside option\n", ctx->fname, ctx->line);
				return -1;
			}

			*str2 = 0;
			cur_sect = find_sect(str);
			if (!cur_sect)
				cur_sect = create_sect(str);
			ctx->list = (struct conf_opt **)&cur_sect->opt;
			continue;
		}

		if (!cur_sect) {
			fprintf(stderr, "conf_file:%s:%i: no section opened\n", ctx->fname, ctx->line);
			return -1;
		}

		if (*str == '}') {
			if (ctx->options)
				return 0;

			fprintf(stderr, "conf_file:%s:%i: sintax error\n", ctx->fname, ctx->line);
			return -1;
		}

		raw = strdup(str);
		str2 = skip_word(str);
		if (*str2 == ' ') {
			*str2 = 0;
			++str2;
		}

		str2 = skip_space(str2);
		if (*str2 == '=' || *str2 == ',') {
			*str2 = 0;
			str2 = skip_space(str2 + 1);
			if (*str2 && *(str2 + 1) && *str2 == '$' && *(str2 + 1) == '{') {
				char *s;
				struct conf_opt *opt;
				for (s = str2 + 2; *s && *s != '}'; s++);
				if (*s == '}') {
					*s = 0;
					str2 += 2;
				}
				opt = find_item(cur_sect->opt, str2);
				if (!opt) {
					fprintf(stderr, "conf_file:%s:%i: parent option not found\n", ctx->fname, ctx->line);
					return -1;
				}
				str2 = (char *)opt->val;
			}
		} else
			str2 = NULL;

		if (sect_add_item(ctx, str, str2, raw))
			return -1;
	}

	return 0;
}

/*static void print_items(struct list_head *items, int dep)
{
	struct conf_option_t *opt;
	int i;

	list_for_each_entry(opt, items, entry) {
		for (i = 0; i < dep; i++)
			printf("\t");
		printf("%s=%s\n", opt->name, opt->val);
		print_items(&opt->items, dep + 1);
	}
}

static void print_conf()
{
	struct sect_t *s;

	list_for_each_entry(s, &sections, entry) {
		printf("[%s]\n", s->sect->name);
		print_items(&s->sect->items, 0);
	}
}*/

static void free_items(struct conf_opt *opt)
{
	struct conf_opt *next;

	for (next = opt->next; opt; opt = next) {
		if (opt->child)
			free_items(opt->child);
		free(opt->name);
		free(opt->raw);
		if (opt->val)
			free(opt->val);
		free((void *)opt);
	}
}

static void conf_clear(struct conf_sect *s)
{
	struct conf_sect *next;

	for (next = s->next; s; s = next) {
		if (s->opt)
			free_items(s->opt);
		free(s->name);
		free((void *)s);
	}
}

int conf_load(const char *fname)
{
	struct conf_sect *head = sect_head;
	struct conf_ctx ctx;
	int r;

	if (open_ctx(&ctx, fname, NULL))
		return -1;

	cur_sect = NULL;
	sect_head = NULL;

	buf = malloc(1024);

	r = load_file(&ctx);

	free(buf);

	close_ctx(&ctx);

	if (r)
		sect_head = head;
	else if (head)
		conf_clear(head);

	return r;
}

static char* skip_space(char *str)
{
	for (; *str && (*str == ' ' || *str == '\t'); str++);
	return str;
}

static char* skip_word(char *str)
{
	for (; *str && (*str != ' ' && *str != '\t' && *str != '='); str++);
	return str;
}

static struct conf_sect *find_sect(const char *name)
{
	struct conf_sect *s;

	for (s = sect_head; s; s = s->next) {
		if (strcmp(s->name, name) == 0)
			return s;
	}

	return NULL;
}

static struct conf_sect *create_sect(const char *name)
{
	struct conf_sect *s = malloc(sizeof(struct conf_sect));

	s->name = strdup(name);
	s->next = NULL;
	s->opt = NULL;

	if (!sect_head)
		sect_head = s;
	else
		sect_tail->next = s;

	sect_tail = s;

	return s;
}

static int sect_add_item(struct conf_ctx *ctx, const char *name, char *val, char *raw)
{
	struct conf_opt *opt = malloc(sizeof(struct conf_opt));
	int r = 0;
	int chld = 0;

	if (val) {
		int len = strlen(val);
		if (val[len - 1] == '{') {
			chld = 1;
			val[--len] = 0;

			while (--len >= 0 && (val[len] == ' ' || val[len] == '\t'));

			if (len >= 0)
				val[len + 1] = 0;
			else
				val = NULL;
		}
	}

	opt->name = strdup(name);
	opt->val = val ? strdup(val) : NULL;
	opt->raw = raw;
	opt->next = NULL;
	opt->child = NULL;

	*ctx->list = opt;
	ctx->list = &opt->next;

	if (chld) {
		struct conf_opt **list = ctx->list;
		ctx->list = &opt->child;
		ctx->options = 1;
		r = load_file(ctx);
		ctx->options = 0;
		ctx->list = list;
	}

	return r;
}

static struct conf_opt *find_item(struct conf_opt *list, const char *name)
{
	struct conf_opt *opt;

	for (opt = list; opt; opt = opt->next) {
		if (strcmp(opt->name, name) == 0)
			return opt;
	}

	return NULL;
}

struct conf_sect* conf_get_sect(const char *name)
{
	return find_sect(name);
}

const char *conf_get_opt(const char *sect, const char *name)
{
	struct conf_opt *opt;
	struct conf_sect *s = conf_get_sect(sect);

	if (!s)
		return NULL;

	opt = find_item(s->opt, name);
	if (!opt)
		return NULL;

	return opt->val;
}

const char *conf_get_subopt(const struct conf_opt *opt, const char *name)
{
	if (!opt->child)
		return NULL;

	opt = find_item(opt->child, name);

	return opt ? opt->val : NULL;
}