147
|
1 /*
|
|
2 * command line and config file parser
|
|
3 */
|
|
4
|
|
5 //#define DEBUG
|
|
6
|
|
7 #include <stdlib.h>
|
|
8 #include <stdio.h>
|
|
9 #include <ctype.h>
|
|
10 #include <unistd.h>
|
|
11 #include <fcntl.h>
|
|
12 #include <string.h>
|
|
13
|
|
14 #define ERR_NOT_AN_OPTION -1
|
|
15 #define ERR_MISSING_PARAM -2
|
|
16 #define ERR_OUT_OF_RANGE -3
|
150
|
17 #define ERR_FUNC_ERR -4
|
147
|
18
|
|
19 #define COMMAND_LINE 0
|
|
20 #define CONFIG_FILE 1
|
|
21
|
|
22 #ifdef DEBUG
|
|
23 #include <assert.h>
|
|
24 #endif
|
|
25
|
|
26 #include "cfgparser.h"
|
|
27
|
|
28 static struct config *config;
|
|
29 static int nr_options; /* number of options in 'conf' */
|
|
30 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
|
|
31
|
|
32 static int init_conf(struct config *conf, int mode)
|
|
33 {
|
|
34 #ifdef DEBUG
|
|
35 assert(conf != NULL);
|
|
36 #endif
|
|
37
|
|
38 /* calculate the number of options in 'conf' */
|
|
39 for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
|
|
40 /* NOTHING */;
|
|
41
|
|
42 config = conf;
|
|
43 #ifdef DEBUG
|
|
44 if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
|
150
|
45 printf("init_conf: wrong mode!\n");
|
147
|
46 return -1;
|
|
47 }
|
|
48 #endif
|
|
49 parser_mode = mode;
|
|
50 return 1;
|
|
51 }
|
|
52
|
|
53 static int read_option(char *opt, char *param)
|
|
54 {
|
|
55 int i;
|
|
56 int need_param = -1;
|
|
57 int tmp_int;
|
|
58 float tmp_float;
|
|
59
|
|
60 for (i = 0; i < nr_options; i++) {
|
|
61 if (!strcasecmp(opt, config[i].name))
|
|
62 break;
|
|
63 }
|
|
64 if (i == nr_options)
|
|
65 return ERR_NOT_AN_OPTION;
|
|
66
|
150
|
67 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE)
|
|
68 return ERR_NOT_AN_OPTION;
|
|
69 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE)
|
|
70 return ERR_NOT_AN_OPTION;
|
|
71
|
147
|
72 switch (config[i].type) {
|
|
73 case CONF_TYPE_FLAG:
|
|
74 /* flags need a parameter in config file */
|
|
75 if (parser_mode == CONFIG_FILE) {
|
|
76 if (!strcasecmp(param, "yes") || /* any other language? */
|
|
77 !strcasecmp(param, "ja") ||
|
|
78 !strcasecmp(param, "igen") ||
|
|
79 !strcasecmp(param, "y") ||
|
|
80 !strcasecmp(param, "i") ||
|
|
81 !strcmp(param, "1"))
|
|
82 *((int *) config[i].p) = config[i].max;
|
|
83 if (!strcasecmp(param, "no") ||
|
|
84 !strcasecmp(param, "nein") ||
|
|
85 !strcasecmp(param, "nicht") ||
|
|
86 !strcasecmp(param, "nem") ||
|
|
87 !strcasecmp(param, "n") ||
|
|
88 !strcmp(param, "0"))
|
|
89 *((int *) config[i].p) = config[i].min;
|
|
90 need_param = 1;
|
|
91 } else { /* parser_mode == COMMAND_LINE */
|
|
92 *((int *) config[i].p) = config[i].max;
|
|
93 need_param = 0;
|
|
94 }
|
|
95 break;
|
|
96 case CONF_TYPE_INT:
|
|
97 if (param == NULL)
|
|
98 return ERR_MISSING_PARAM;
|
|
99 if (!isdigit(*param))
|
|
100 return ERR_MISSING_PARAM;
|
|
101
|
|
102 tmp_int = atoi(param);
|
|
103
|
|
104 if (config[i].flags & CONF_CHK_MIN)
|
|
105 if (tmp_int < config[i].min)
|
|
106 return ERR_OUT_OF_RANGE;
|
|
107
|
|
108 if (config[i].flags & CONF_CHK_MAX)
|
|
109 if (tmp_int > config[i].max)
|
|
110 return ERR_OUT_OF_RANGE;
|
|
111
|
|
112 *((int *) config[i].p) = tmp_int;
|
|
113 need_param = 1;
|
|
114 break;
|
|
115 case CONF_TYPE_FLOAT:
|
|
116 if (param == NULL)
|
|
117 return ERR_MISSING_PARAM;
|
|
118 if (!isdigit(*param))
|
|
119 return ERR_MISSING_PARAM;
|
|
120
|
|
121 tmp_float = atof(param);
|
|
122
|
|
123 if (config[i].flags & CONF_CHK_MIN)
|
|
124 if (tmp_float < config[i].min)
|
|
125 return ERR_OUT_OF_RANGE;
|
|
126
|
|
127 if (config[i].flags & CONF_CHK_MAX)
|
|
128 if (tmp_float > config[i].max)
|
|
129 return ERR_OUT_OF_RANGE;
|
|
130
|
|
131 *((float *) config[i].p) = tmp_float;
|
|
132 need_param = 1;
|
|
133 break;
|
|
134 case CONF_TYPE_STRING:
|
|
135 if (param == NULL)
|
|
136 return ERR_MISSING_PARAM;
|
|
137
|
|
138 if (config[i].flags & CONF_CHK_MIN)
|
|
139 if (strlen(param) < config[i].min)
|
|
140 return ERR_OUT_OF_RANGE;
|
|
141
|
|
142 if (config[i].flags & CONF_CHK_MAX)
|
|
143 if (strlen(param) > config[i].max)
|
|
144 return ERR_OUT_OF_RANGE;
|
|
145
|
|
146 *((char **) config[i].p) = strdup(param);
|
|
147 need_param = 1;
|
|
148 break;
|
150
|
149 case CONF_TYPE_FUNC:
|
|
150 if (config[i].flags & CONF_FUNC_PARAM) {
|
|
151 if (param == NULL)
|
|
152 return ERR_MISSING_PARAM;
|
|
153 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0)
|
|
154 return ERR_FUNC_ERR;
|
|
155 need_param = 1;
|
|
156 } else {
|
|
157 if ((((cfg_func_t) config[i].p)(config + i)) < 0)
|
|
158 return ERR_FUNC_ERR;
|
|
159 need_param = 0;
|
|
160 }
|
|
161 break;
|
147
|
162 default:
|
|
163 printf("picsaba\n");
|
|
164 break;
|
|
165 }
|
|
166 return need_param;
|
|
167 }
|
|
168
|
|
169 int parse_config_file(struct config *conf, char *conffile)
|
|
170 {
|
|
171 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
|
|
172 #define MAX_LINE_LEN 1000
|
|
173 #define MAX_OPT_LEN 100
|
|
174 #define MAX_PARAM_LEN 100
|
|
175 FILE *fp;
|
|
176 char *line;
|
|
177 char opt[MAX_OPT_LEN];
|
|
178 char param[MAX_PARAM_LEN];
|
|
179 int tmp;
|
|
180 int line_num = 0;
|
|
181 int line_pos; /* line pos */
|
|
182 int opt_pos; /* opt pos */
|
|
183 int param_pos; /* param pos */
|
|
184 int ret = 1;
|
|
185
|
|
186 #ifdef DEBUG
|
|
187 assert(conffile != NULL);
|
|
188 #endif
|
|
189 printf("Reading config file: %s\n", conffile);
|
|
190
|
|
191 if (init_conf(conf, CONFIG_FILE) == -1)
|
|
192 return -1;
|
|
193
|
|
194 if ((line = (char *) malloc(MAX_LINE_LEN)) == NULL) {
|
|
195 perror("parse_config_file: can't get memory for 'line'");
|
|
196 return -1;
|
|
197 }
|
|
198
|
|
199 if ((fp = fopen(conffile, "r")) == NULL) {
|
|
200 perror("parse_config_file: can't open filename");
|
|
201 free(line);
|
|
202 return 0;
|
|
203 }
|
|
204
|
|
205 while (fgets(line, MAX_LINE_LEN, fp)) {
|
|
206 line_num++;
|
|
207 line_pos = 0;
|
|
208
|
|
209 /* skip whitespaces */
|
|
210 while (isspace(line[line_pos]))
|
|
211 ++line_pos;
|
|
212
|
|
213 /* EOL / comment */
|
|
214 if (line[line_pos] == '\0' || line[line_pos] == '#')
|
|
215 continue;
|
|
216
|
|
217 /* read option. accept char if isalnum(char) */
|
|
218 for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
|
|
219 opt[opt_pos++] = line[line_pos++];
|
|
220 if (opt_pos >= MAX_OPT_LEN) {
|
|
221 PRINT_LINENUM;
|
|
222 printf("too long option\n");
|
|
223 ret = -1;
|
|
224 continue;
|
|
225 }
|
|
226 }
|
|
227 if (opt_pos == 0) {
|
|
228 PRINT_LINENUM;
|
|
229 printf("parse error\n");
|
|
230 ret = -1;
|
|
231 continue;
|
|
232 }
|
|
233 opt[opt_pos] = '\0';
|
|
234 #ifdef DEBUG
|
|
235 PRINT_LINENUM;
|
|
236 printf("option: %s\n", opt);
|
|
237 #endif
|
|
238
|
|
239 /* skip whitespaces */
|
|
240 while (isspace(line[line_pos]))
|
|
241 ++line_pos;
|
|
242
|
|
243 /* check '=' */
|
|
244 if (line[line_pos++] != '=') {
|
|
245 PRINT_LINENUM;
|
|
246 printf("option without parameter\n");
|
|
247 ret = -1;
|
|
248 continue;
|
|
249 }
|
|
250
|
|
251 /* whitespaces... */
|
|
252 while (isspace(line[line_pos]))
|
|
253 ++line_pos;
|
|
254
|
|
255 /* read the parameter */
|
|
256 for (param_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
|
|
257 param[param_pos++] = line[line_pos++];
|
|
258 if (param_pos >= MAX_PARAM_LEN) {
|
|
259 PRINT_LINENUM;
|
|
260 printf("too long parameter\n");
|
|
261 ret = -1;
|
|
262 continue;
|
|
263 }
|
|
264 }
|
|
265 param[param_pos] = '\0';
|
|
266
|
|
267 /* did we read a parameter? */
|
|
268 if (param_pos == 0) {
|
|
269 PRINT_LINENUM;
|
|
270 printf("option without parameter\n");
|
|
271 ret = -1;
|
|
272 continue;
|
|
273 }
|
|
274 #ifdef DEBUG
|
|
275 PRINT_LINENUM;
|
|
276 printf("parameter: %s\n", param);
|
|
277 #endif
|
|
278 /* now, check if we have some more chars on the line */
|
|
279 /* whitespace... */
|
|
280 while (isspace(line[line_pos]))
|
|
281 ++line_pos;
|
|
282
|
|
283 /* EOL / comment */
|
|
284 if (line[line_pos] != '\0' && line[line_pos] != '#') {
|
|
285 PRINT_LINENUM;
|
|
286 printf("extra characters on line: %s\n", line+line_pos);
|
|
287 ret = -1;
|
|
288 }
|
|
289
|
|
290 tmp = read_option(opt, param);
|
|
291 switch (tmp) {
|
|
292 case ERR_NOT_AN_OPTION:
|
|
293 PRINT_LINENUM;
|
|
294 printf("invalid option: %s\n", opt);
|
|
295 ret = -1;
|
|
296 continue;
|
|
297 /* break; */
|
|
298 case ERR_MISSING_PARAM:
|
|
299 PRINT_LINENUM;
|
|
300 printf("missing parameter: %s\n", opt);
|
|
301 ret = -1;
|
|
302 continue;
|
|
303 /* break; */
|
|
304 case ERR_OUT_OF_RANGE:
|
|
305 PRINT_LINENUM;
|
|
306 printf("parameter of %s out of range\n", opt);
|
|
307 ret = -1;
|
|
308 continue;
|
|
309 /* break; */
|
150
|
310 case ERR_FUNC_ERR:
|
|
311 PRINT_LINENUM;
|
|
312 printf("parser function returned error: %s\n", opt);
|
|
313 ret = -1;
|
|
314 continue;
|
|
315 /* break */
|
147
|
316 }
|
|
317 }
|
|
318
|
|
319 free(line);
|
|
320 fclose(fp);
|
|
321 return ret;
|
|
322 }
|
|
323
|
|
324 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char **filename)
|
|
325 {
|
|
326 int i;
|
|
327 int found_filename = 0;
|
|
328 int tmp;
|
|
329 char *opt;
|
|
330
|
|
331 #ifdef DEBUG
|
|
332 assert(argv != NULL);
|
|
333 assert(envp != NULL);
|
|
334 assert(argc >= 1);
|
|
335 #endif
|
|
336
|
|
337 if (init_conf(conf, COMMAND_LINE) == -1)
|
|
338 return -1;
|
|
339
|
|
340 for (i = 1; i < argc; i++) {
|
|
341 opt = argv[i];
|
|
342 if (*opt != '-')
|
|
343 goto not_an_option;
|
|
344
|
|
345 /* remove trailing '-' */
|
|
346 opt++;
|
150
|
347 #if 0
|
147
|
348 /* check for --help, -h, and --version */
|
|
349 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
|
|
350 printf("%s%s", banner_text, help_text);
|
|
351 continue;
|
|
352 }
|
|
353 if (!strcasecmp(opt, "-version")) {
|
|
354 printf("%s", banner_text);
|
|
355 continue;
|
|
356 }
|
150
|
357 #endif
|
147
|
358
|
|
359 tmp = read_option(opt, argv[i + 1]);
|
|
360
|
|
361 switch (tmp) {
|
|
362 case ERR_NOT_AN_OPTION:
|
|
363 not_an_option:
|
|
364 /* opt is not an option -> treat it as a filename */
|
|
365 if (found_filename) {
|
|
366 /* we already have a filename */
|
|
367 printf("parse_command_line: invalid option: %s\n", argv[i]);
|
|
368 return -1;
|
|
369 } else {
|
|
370 found_filename = 1;
|
|
371 *filename = argv[i];
|
|
372 printf("parse_command_line: found filename: %s\n", *filename);
|
|
373 continue; /* next option */
|
|
374 }
|
|
375 break;
|
|
376 case ERR_MISSING_PARAM:
|
|
377 printf("parse_command_line: missing parameter: %s\n", argv[i]);
|
|
378 return -1;
|
|
379 /* break; */
|
|
380 case ERR_OUT_OF_RANGE:
|
|
381 printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]);
|
|
382 return -1;
|
|
383 /* break; */
|
150
|
384 case ERR_FUNC_ERR:
|
|
385 printf("parse_command_line: parser function returned error: %s\n", argv[i]);
|
|
386 return -1;
|
|
387 /* break; */
|
147
|
388 }
|
|
389
|
|
390 i += tmp; /* we already processed the params (if there was any) */
|
|
391 }
|
|
392 return found_filename;
|
|
393 }
|