aboutsummaryrefslogtreecommitdiff
path: root/src/cfg.c
blob: 2f23f92d5ee1468dc41e80ebe03d10f15ebb7cca (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* This file is part of Smap.
   Copyright (C) 2006-2007, 2010, 2014 Sergey Poznyakoff

   Smap 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 3, or (at your option)
   any later version.

   Smap 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.

   You should have received a copy of the GNU General Public License
   along with Smap.  If not, see <http://www.gnu.org/licenses/>. */

#include "smapd.h"

FILE *infile;
const char *cfg_file_name;
unsigned cfg_line;
unsigned cfg_cur_line;
int cfg_errors;

#define INITBUFSIZE 512
#define MINBUFSIZE  8

int
read_line(char **pbuf, size_t *psize)
{
	char *buf = *pbuf;
	size_t size = *psize;
	size_t off = 0;

	if (feof(infile))
		return 0;
	cfg_line = cfg_cur_line;
	if (size == 0) {
		size = INITBUFSIZE;
		buf = emalloc(size);
	}

	while (1) {
		size_t len = size - off;

		if (len < MINBUFSIZE) {
			size *= 2;
			buf = realloc(buf, size);
		}

		if (fgets(buf + off, size - off, infile) == NULL)
			break;

		len = strlen(buf);

		if (len == 0)
			continue;
		if (buf[len-1] != '\n') {
			off = len;
			continue;
		}
		buf[--len] = 0;
		cfg_cur_line++;
		if (len == 0)
			continue;
		if (buf[len-1] == '\\') {
			off = len - 1;
			continue;
		}
		off = len;
		break;
	}
	*pbuf = buf;
	*psize = size;
	return off;
}

static struct cfg_kw *
find_cfg_kw(struct cfg_kw *kwtab, const char *word)
{
	for (; kwtab->kw; kwtab++)
		if (strcmp(kwtab->kw, word) == 0)
			return kwtab;
	return NULL;
}

int
cfg_chkargc(int wordc, int min, int max)
{
	if (wordc < min) {
		smap_error(min == 2 ?
			     "%s:%u: required argument missing" :
			     "%s:%u: required arguments missing",
			   cfg_file_name, cfg_line);
		cfg_errors = 1;
		return 1;
	} else if (max > 0 && wordc > max) {
		smap_error("%s:%u: too many arguments",
			   cfg_file_name, cfg_line);
		cfg_errors = 1;
		return 1;
	}
	return 0;
}

int
cfg_parse_bool(const char *str, int *res)
{
	if (strcmp (str, "yes") == 0
	    || strcmp (str, "on") == 0
	    || strcmp (str, "t") == 0
	    || strcmp (str, "true") == 0
	    || strcmp (str, "1") == 0)
		*res = 1;
	else if (strcmp (str, "no") == 0
		 || strcmp (str, "off") == 0
		 || strcmp (str, "nil") == 0
		 || strcmp (str, "false") == 0
		 || strcmp (str, "0") == 0)
		*res = 0;
	else {
		smap_error("%s:%u: unrecognized boolean value (%s)",
			   cfg_file_name, cfg_line, str);
		cfg_errors = 1;
		return 1;
	}
	return 0;
}

static void
parse_int(const char *str, int base, int sign, int *res)
{
	char *p;
	unsigned long n = strtoul(str, &p, base);

	if (*p) {
		smap_error("%s:%u: invalid numeric value (near %s)",
			   cfg_file_name, cfg_line, p);
		cfg_errors = 1;
		return;
	}

	if (sign) {
		int i;

		if ((i = n) == n) {
			*res = i;
			return;
		}
	} else {
		unsigned u;

		if ((u = n) == n) {
			*res = u;
			return;
		}
	}
	smap_error("%s:%u: numeric value out of range",
		   cfg_file_name, cfg_line);
	cfg_errors = 1;
}

static void
save_words(struct wordsplit *ws, struct cfg_kw *kwp)
{
	int i, n;
	char **aval;

	aval = ecalloc(ws->ws_wordc, sizeof(kwp->aval[0]));
	*kwp->ival = n = ws->ws_wordc - 1;
	for (i = 0; i < n; i++)
		aval[i] = estrdup(ws->ws_wordv[i + 1]);
	aval[i] = 0;
	*kwp->aval = aval;
}

int
wrdse_to_ex(int code)
{
  	switch (code) {
  	case WRDSE_EOF:
  		return 0;
  	case WRDSE_QUOTE:
  		return EX_CONFIG;
  	case WRDSE_NOSPACE:
  		return EX_UNAVAILABLE;
	}
	return EX_UNAVAILABLE;
}

static int
asgn_p(char *buf)
{
	unsigned char *p = (unsigned char*)buf;
	while (*p && (*p == ' ' || *p == '\t'))
		p++;
	if (!*p)
		return 0;
	if (*p < 127 && (isalpha(*p) || *p == '_')) {
		while (*p && *p < 127 && (isalnum(*p) || *p == '_'))
			p++;
		return *p == '=';
	}
	return 0;
}

static size_t
find_env_var(const char **env, char *name)
{
	size_t i;
		
	for (i = 0; env[i]; i++) {
		size_t j;
		const char *var = env[i];
		
		for (j = 0; name[j]; j++) {
			if (name[j] != var[j])
				break;
			if (name[j] == '=')
				return i;
		}
	}
	return i;
}

static void
asgn(struct wordsplit *ws, size_t *psize)
{
	size_t size = *psize;

	if (size == 0) {
		size = 16;
		ws->ws_env = ecalloc(size, sizeof(ws->ws_env[0]));
		ws->ws_env[0] = ws->ws_wordv[0];
	} else {
		size_t n = find_env_var(ws->ws_env, ws->ws_wordv[0]);
		if (ws->ws_env[n])
			free((char*)ws->ws_env[n]);
		else if (n == size - 1) {
			size *= 2;
			ws->ws_env = erealloc(ws->ws_env,
					      size * sizeof(ws->ws_env[0]));
		}
		ws->ws_env[n] = ws->ws_wordv[0];
		ws->ws_env[++n] = NULL;
	}
	ws->ws_wordv[0] = NULL;
	*psize = size;
}


static void
smapd_ws_debug(const char *fmt, ...)
{
	va_list ap;
	
	smap_stream_printf(smap_debug_str, "WS: ");
	va_start(ap, fmt);
	smap_stream_vprintf(smap_debug_str, fmt, ap);
	va_end(ap);
	smap_stream_write(smap_debug_str, "\n", 1, NULL);
}

void
parse_config_loop(struct cfg_kw *kwtab, void *data)
{
	char *buf = NULL;
	size_t size = 0;
	struct wordsplit ws;
	int wsflags = WRDSF_DEFFLAGS |
		      WRDSF_ERROR |
		      WRDSF_COMMENT |
		      WRDSF_ENOMEMABRT;
	size_t envsize = 0;
	int eof = 0;

	ws.ws_error = smap_error;
	ws.ws_comment = "#";

	if (smap_debug_np(DBG_CONF, 1))
		wsflags |= WRDSF_WARNUNDEF;
	if (smap_debug_np(DBG_CONF, 100)) {
		ws.ws_debug = smapd_ws_debug;
		wsflags |= WRDSF_DEBUG | WRDSF_SHOWDBG;
	}
	
	while (!eof && read_line(&buf, &size)) {
		struct cfg_kw *kwp;

		if (wordsplit(buf, &ws, wsflags)) {
			smap_error("%s:%u: %s",
				   cfg_file_name, cfg_line, 
				   wordsplit_strerror(&ws));
			exit(wrdse_to_ex(ws.ws_errno));
		}
		wsflags |= WRDSF_REUSE;

		if (ws.ws_wordc == 0)
			continue;

		if (smap_debug_np(DBG_CONF, 2)) {
			size_t i;
			
			smap_stream_printf(smap_debug_str,
					   "%s:%u: [%lu args]:",
					   cfg_file_name, cfg_line,
					   (unsigned long)ws.ws_wordc);
			for (i = 0; i < ws.ws_wordc; i++)
				smap_stream_printf(smap_debug_str, " '%s'",
						   ws.ws_wordv[i]);
			smap_stream_printf(smap_debug_str, "\n");
		}
		
		if (asgn_p(ws.ws_wordv[0])) {
			if (ws.ws_wordc > 1) {
				smap_error("%s:%u: too many arguments "
					   "(unquoted assignment?)",
					   cfg_file_name, cfg_line);
				cfg_errors = 1;
			} else {
				asgn(&ws, &envsize);
				wsflags &= ~WRDSF_NOVAR;
				wsflags |= WRDSF_ENV | WRDSF_KEEPUNDEF;
			}
			continue;
		}
		
		kwp = find_cfg_kw(kwtab, ws.ws_wordv[0]);
		if (!kwp) {
			smap_error("%s:%u: unrecognized line",
				   cfg_file_name, cfg_line);
			cfg_errors = 1;
			continue;
		}
		switch (kwp->type) {
		case KWT_STRING:
			if (cfg_chkargc(ws.ws_wordc, 2, 2))
				continue;
			*kwp->sval = estrdup(ws.ws_wordv[1]);
			break;

		case KWT_BOOL:
			if (cfg_chkargc(ws.ws_wordc, 2, 2))
				continue;
			cfg_parse_bool(ws.ws_wordv[1], kwp->ival);
			break;

		case KWT_INT:
		case KWT_UINT:
			if (cfg_chkargc(ws.ws_wordc, 2, 2))
				continue;
			parse_int(ws.ws_wordv[1], 10,
				  kwp->type == KWT_INT,
				  kwp->ival);
			break;

		case KWT_INT_O:
		case KWT_UINT_O:
			if (cfg_chkargc(ws.ws_wordc, 2, 2))
				continue;
			parse_int(ws.ws_wordv[1], 8,
				  kwp->type == KWT_INT_O,
				  kwp->ival);
			break;

		case KWT_INT_X:
		case KWT_UINT_X:
			if (cfg_chkargc(ws.ws_wordc, 2, 2))
				continue;
			parse_int(ws.ws_wordv[1], 16,
				  kwp->type == KWT_INT_X,
				  kwp->ival);
			break;

		case KWT_ARR:
			if (cfg_chkargc(ws.ws_wordc, 2, 0))
				continue;
			save_words(&ws, kwp);
			break;

		case KWT_FUN:
			break;

		case KWT_EOF:
			eof = 1;
			break;

		default:
			smap_error("INTERNAL ERROR at %s:%d, please report",
				   __FILE__, __LINE__);
			abort();
		}
		if (kwp->fun) {
			int rc = kwp->fun(kwp, ws.ws_wordc, ws.ws_wordv, data);
			if (eof) {
				if (rc < 0) {
					cfg_errors = 1;
					break;
				}
				if (rc == 0)
					eof = 0;
			} else if (rc)
				cfg_errors = 1;
		}
	}
	if (wsflags & WRDSF_REUSE)
		wordsplit_free(&ws);
	if (envsize)
		free(ws.ws_env);
	free(buf);
}

int
parse_config(const char *filename, struct cfg_kw *kwtab, void *data)
{
	infile = fopen(filename, "r");
	if (!infile) {
		smap_error("cannot open file %s: %s", filename,
			   strerror(errno));
		return -1;
	}
	cfg_file_name = filename;
	cfg_cur_line = 1;
	parse_config_loop(kwtab, data);
	fclose(infile);
	return cfg_errors;
}

Return to:

Send suggestions and report system problems to the System administrator.