summaryrefslogtreecommitdiffstats
path: root/cfgparser.c
blob: 0577322304b4a6affca5f6de734e2233cf8834bc (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
/*
 * command line and config file parser
 */

//#define DEBUG

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#define ERR_NOT_AN_OPTION	-1
#define ERR_MISSING_PARAM	-2
#define ERR_OUT_OF_RANGE	-3

#define COMMAND_LINE		0
#define CONFIG_FILE		1

#ifdef DEBUG
#include <assert.h>
#endif

#include "cfgparser.h"
#include "version.h"
#include "help_mp.h"

static struct config *config;
static int nr_options;		/* number of options in 'conf' */
static int parser_mode;		/* COMMAND_LINE or CONFIG_FILE */

static int init_conf(struct config *conf, int mode)
{
#ifdef DEBUG
	assert(conf != NULL);
#endif

	/* calculate the number of options in 'conf' */
	for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
		/* NOTHING */;

	config = conf;
#ifdef DEBUG
	if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
		printf("init_conf: wrong flag!\n");
		return -1;
	}
#endif
	parser_mode = mode;
	return 1;
}

static int read_option(char *opt, char *param)
{
	int i;
	int need_param = -1;
	int tmp_int;
	float tmp_float;

	for (i = 0; i < nr_options; i++) {
		if (!strcasecmp(opt, config[i].name))
			break;
	}
	if (i == nr_options)
		return ERR_NOT_AN_OPTION;

	switch (config[i].type) {
		case CONF_TYPE_FLAG:
			/* flags need a parameter in config file */
			if (parser_mode == CONFIG_FILE) {
				if (!strcasecmp(param, "yes") ||	/* any other language? */
				    !strcasecmp(param, "ja") ||
				    !strcasecmp(param, "igen") ||
				    !strcasecmp(param, "y") ||
				    !strcasecmp(param, "i") ||
				    !strcmp(param, "1"))
					*((int *) config[i].p) = config[i].max;
				if (!strcasecmp(param, "no") ||
				    !strcasecmp(param, "nein") ||
				    !strcasecmp(param, "nicht") ||
				    !strcasecmp(param, "nem") ||
				    !strcasecmp(param, "n") ||
				    !strcmp(param, "0"))
					*((int *) config[i].p) = config[i].min;
				need_param = 1;
			} else {	/* parser_mode == COMMAND_LINE */
				*((int *) config[i].p) = config[i].max;
				need_param = 0;
			}
			break;
		case CONF_TYPE_INT:
			if (param == NULL)
				return ERR_MISSING_PARAM;
			if (!isdigit(*param))
				return ERR_MISSING_PARAM;

			tmp_int = atoi(param);

			if (config[i].flags & CONF_CHK_MIN)
				if (tmp_int < config[i].min)
					return ERR_OUT_OF_RANGE;

			if (config[i].flags & CONF_CHK_MAX)
				if (tmp_int > config[i].max)
					return ERR_OUT_OF_RANGE;

			*((int *) config[i].p) = tmp_int;
			need_param = 1;
			break;
		case CONF_TYPE_FLOAT:
			if (param == NULL)
				return ERR_MISSING_PARAM;
			if (!isdigit(*param))
				return ERR_MISSING_PARAM;

			tmp_float = atof(param);

			if (config[i].flags & CONF_CHK_MIN)
				if (tmp_float < config[i].min)
					return ERR_OUT_OF_RANGE;

			if (config[i].flags & CONF_CHK_MAX)
				if (tmp_float > config[i].max)
					return ERR_OUT_OF_RANGE;

			*((float *) config[i].p) = tmp_float;
			need_param = 1;
			break;
		case CONF_TYPE_STRING:
			if (param == NULL)
				return ERR_MISSING_PARAM;

			if (config[i].flags & CONF_CHK_MIN)
				if (strlen(param) < config[i].min)
					return ERR_OUT_OF_RANGE;

			if (config[i].flags & CONF_CHK_MAX)
				if (strlen(param) > config[i].max)
					return ERR_OUT_OF_RANGE;

			*((char **) config[i].p) = strdup(param);
			need_param = 1;
			break;
		default:
			printf("picsaba\n");
			break;
	}
	return need_param;
}

int parse_config_file(struct config *conf, char *conffile)
{
#define PRINT_LINENUM	printf("%s(%d): ", conffile, line_num)
#define MAX_LINE_LEN	1000
#define MAX_OPT_LEN	100
#define MAX_PARAM_LEN	100
	FILE *fp;
	char *line;
	char opt[MAX_OPT_LEN];
	char param[MAX_PARAM_LEN];
	int tmp;
	int line_num = 0;
	int line_pos;	/* line pos */
	int opt_pos;	/* opt pos */
	int param_pos;	/* param pos */
	int ret = 1;

#ifdef DEBUG
	assert(conffile != NULL);
#endif
	printf("Reading config file: %s\n", conffile);

	if (init_conf(conf, CONFIG_FILE) == -1)
		return -1;

	if ((line = (char *) malloc(MAX_LINE_LEN)) == NULL) {
		perror("parse_config_file: can't get memory for 'line'");
		return -1;
	}

	if ((fp = fopen(conffile, "r")) == NULL) {
		perror("parse_config_file: can't open filename");
		free(line);
		return 0;
	}

	while (fgets(line, MAX_LINE_LEN, fp)) {
		line_num++;
		line_pos = 0;

		/* skip whitespaces */
		while (isspace(line[line_pos]))
			++line_pos;

		/* EOL / comment */
		if (line[line_pos] == '\0' || line[line_pos] == '#')
			continue;

		/* read option. accept char if isalnum(char) */
		for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
			opt[opt_pos++] = line[line_pos++];
			if (opt_pos >= MAX_OPT_LEN) {
				PRINT_LINENUM;
				printf("too long option\n");
				ret = -1;
				continue;
			}
		}
		if (opt_pos == 0) {
			PRINT_LINENUM;
			printf("parse error\n");
			ret = -1;
			continue;
		}
		opt[opt_pos] = '\0';
#ifdef DEBUG
		PRINT_LINENUM;
		printf("option: %s\n", opt);
#endif

		/* skip whitespaces */
		while (isspace(line[line_pos]))
			++line_pos;

		/* check '=' */
		if (line[line_pos++] != '=') {
			PRINT_LINENUM;
			printf("option without parameter\n");
			ret = -1;
			continue;
		}

		/* whitespaces... */
		while (isspace(line[line_pos]))
			++line_pos;

		/* read the parameter */
		for (param_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
			param[param_pos++] = line[line_pos++];
			if (param_pos >= MAX_PARAM_LEN) {
				PRINT_LINENUM;
				printf("too long parameter\n");
				ret = -1;
				continue;
			}
		}
		param[param_pos] = '\0';

		/* did we read a parameter? */
		if (param_pos == 0) {
			PRINT_LINENUM;
			printf("option without parameter\n");
			ret = -1;
			continue;
		}
#ifdef DEBUG
		PRINT_LINENUM;
		printf("parameter: %s\n", param);
#endif
		/* now, check if we have some more chars on the line */
		/* whitespace... */
		while (isspace(line[line_pos]))
			++line_pos;

		/* EOL / comment */
		if (line[line_pos] != '\0' && line[line_pos] != '#') {
			PRINT_LINENUM;
			printf("extra characters on line: %s\n", line+line_pos);
			ret = -1;
		}

		tmp = read_option(opt, param);
		switch (tmp) {
		case ERR_NOT_AN_OPTION:
			PRINT_LINENUM;
			printf("invalid option: %s\n", opt);
			ret = -1;
			continue;
			/* break; */
		case ERR_MISSING_PARAM:
			PRINT_LINENUM;
			printf("missing parameter: %s\n", opt);
			ret = -1;
			continue;
			/* break; */
		case ERR_OUT_OF_RANGE:
			PRINT_LINENUM;
			printf("parameter of %s out of range\n", opt);
			ret = -1;
			continue;
			/* break; */
		}	
	}

	free(line);
	fclose(fp);
	return ret;
}

int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char **filename)
{
	int i;
	int found_filename = 0;
	int tmp;
	char *opt;

#ifdef DEBUG
	assert(argv != NULL);
	assert(envp != NULL);
	assert(argc >= 1);
#endif

	if (init_conf(conf, COMMAND_LINE) == -1)
		return -1;

	for (i = 1; i < argc; i++) {
		opt = argv[i];
		if (*opt != '-')
			goto not_an_option;

		/* remove trailing '-' */
		opt++;

		/* check for --help, -h, and --version */
		if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
			printf("%s%s", banner_text, help_text);
			continue;
		}
		if (!strcasecmp(opt, "-version")) {
			printf("%s", banner_text);
			continue;
		}

		tmp = read_option(opt, argv[i + 1]);

		switch (tmp) {
		case ERR_NOT_AN_OPTION:
not_an_option:
			/* opt is not an option -> treat it as a filename */
			if (found_filename) {
				/* we already have a filename */
				printf("parse_command_line: invalid option: %s\n", argv[i]);
				return -1;
			} else {
				found_filename = 1;
				*filename = argv[i];
				printf("parse_command_line: found filename: %s\n", *filename);
				continue;	/* next option */
			}
			break;
		case ERR_MISSING_PARAM:
			printf("parse_command_line: missing parameter: %s\n", argv[i]);
			return -1;
			/* break; */
		case ERR_OUT_OF_RANGE:
			printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]);
			return -1;
			/* break; */
		}

		i += tmp;	/* we already processed the params (if there was any) */
	}
	return found_filename;
}