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;
|
151
|
83 else if (!strcasecmp(param, "no") ||
|
147
|
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;
|
151
|
90 else
|
|
91 return ERR_OUT_OF_RANGE;
|
147
|
92 need_param = 1;
|
|
93 } else { /* parser_mode == COMMAND_LINE */
|
|
94 *((int *) config[i].p) = config[i].max;
|
|
95 need_param = 0;
|
|
96 }
|
|
97 break;
|
|
98 case CONF_TYPE_INT:
|
|
99 if (param == NULL)
|
|
100 return ERR_MISSING_PARAM;
|
|
101 if (!isdigit(*param))
|
|
102 return ERR_MISSING_PARAM;
|
|
103
|
|
104 tmp_int = atoi(param);
|
|
105
|
153
|
106 if (config[i].flags & CONF_MIN)
|
147
|
107 if (tmp_int < config[i].min)
|
|
108 return ERR_OUT_OF_RANGE;
|
|
109
|
153
|
110 if (config[i].flags & CONF_MAX)
|
147
|
111 if (tmp_int > config[i].max)
|
|
112 return ERR_OUT_OF_RANGE;
|
|
113
|
|
114 *((int *) config[i].p) = tmp_int;
|
|
115 need_param = 1;
|
|
116 break;
|
|
117 case CONF_TYPE_FLOAT:
|
|
118 if (param == NULL)
|
|
119 return ERR_MISSING_PARAM;
|
|
120 if (!isdigit(*param))
|
|
121 return ERR_MISSING_PARAM;
|
|
122
|
|
123 tmp_float = atof(param);
|
|
124
|
153
|
125 if (config[i].flags & CONF_MIN)
|
147
|
126 if (tmp_float < config[i].min)
|
|
127 return ERR_OUT_OF_RANGE;
|
|
128
|
153
|
129 if (config[i].flags & CONF_MAX)
|
147
|
130 if (tmp_float > config[i].max)
|
|
131 return ERR_OUT_OF_RANGE;
|
|
132
|
|
133 *((float *) config[i].p) = tmp_float;
|
|
134 need_param = 1;
|
|
135 break;
|
|
136 case CONF_TYPE_STRING:
|
|
137 if (param == NULL)
|
|
138 return ERR_MISSING_PARAM;
|
|
139
|
153
|
140 if (config[i].flags & CONF_MIN)
|
147
|
141 if (strlen(param) < config[i].min)
|
|
142 return ERR_OUT_OF_RANGE;
|
|
143
|
153
|
144 if (config[i].flags & CONF_MAX)
|
147
|
145 if (strlen(param) > config[i].max)
|
|
146 return ERR_OUT_OF_RANGE;
|
|
147
|
|
148 *((char **) config[i].p) = strdup(param);
|
|
149 need_param = 1;
|
|
150 break;
|
151
|
151 case CONF_TYPE_FUNC_PARAM:
|
|
152 if (param == NULL)
|
|
153 return ERR_MISSING_PARAM;
|
|
154 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0)
|
|
155 return ERR_FUNC_ERR;
|
|
156 need_param = 1;
|
|
157 break;
|
150
|
158 case CONF_TYPE_FUNC:
|
151
|
159 if ((((cfg_func_t) config[i].p)(config + i)) < 0)
|
|
160 return ERR_FUNC_ERR;
|
|
161 need_param = 0;
|
150
|
162 break;
|
152
|
163 case CONF_TYPE_PRINT:
|
|
164 printf("%s", (char *) config[i].p);
|
|
165 exit(1);
|
147
|
166 default:
|
|
167 printf("picsaba\n");
|
|
168 break;
|
|
169 }
|
|
170 return need_param;
|
|
171 }
|
|
172
|
|
173 int parse_config_file(struct config *conf, char *conffile)
|
|
174 {
|
|
175 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
|
|
176 #define MAX_LINE_LEN 1000
|
|
177 #define MAX_OPT_LEN 100
|
|
178 #define MAX_PARAM_LEN 100
|
|
179 FILE *fp;
|
|
180 char *line;
|
|
181 char opt[MAX_OPT_LEN];
|
|
182 char param[MAX_PARAM_LEN];
|
|
183 int tmp;
|
|
184 int line_num = 0;
|
|
185 int line_pos; /* line pos */
|
|
186 int opt_pos; /* opt pos */
|
|
187 int param_pos; /* param pos */
|
|
188 int ret = 1;
|
|
189
|
|
190 #ifdef DEBUG
|
|
191 assert(conffile != NULL);
|
|
192 #endif
|
|
193 printf("Reading config file: %s\n", conffile);
|
|
194
|
|
195 if (init_conf(conf, CONFIG_FILE) == -1)
|
|
196 return -1;
|
|
197
|
|
198 if ((line = (char *) malloc(MAX_LINE_LEN)) == NULL) {
|
|
199 perror("parse_config_file: can't get memory for 'line'");
|
|
200 return -1;
|
|
201 }
|
|
202
|
|
203 if ((fp = fopen(conffile, "r")) == NULL) {
|
|
204 perror("parse_config_file: can't open filename");
|
|
205 free(line);
|
|
206 return 0;
|
|
207 }
|
|
208
|
|
209 while (fgets(line, MAX_LINE_LEN, fp)) {
|
|
210 line_num++;
|
|
211 line_pos = 0;
|
|
212
|
|
213 /* skip whitespaces */
|
|
214 while (isspace(line[line_pos]))
|
|
215 ++line_pos;
|
|
216
|
|
217 /* EOL / comment */
|
|
218 if (line[line_pos] == '\0' || line[line_pos] == '#')
|
|
219 continue;
|
|
220
|
|
221 /* read option. accept char if isalnum(char) */
|
|
222 for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
|
|
223 opt[opt_pos++] = line[line_pos++];
|
|
224 if (opt_pos >= MAX_OPT_LEN) {
|
|
225 PRINT_LINENUM;
|
|
226 printf("too long option\n");
|
|
227 ret = -1;
|
|
228 continue;
|
|
229 }
|
|
230 }
|
|
231 if (opt_pos == 0) {
|
|
232 PRINT_LINENUM;
|
|
233 printf("parse error\n");
|
|
234 ret = -1;
|
|
235 continue;
|
|
236 }
|
|
237 opt[opt_pos] = '\0';
|
|
238 #ifdef DEBUG
|
|
239 PRINT_LINENUM;
|
|
240 printf("option: %s\n", opt);
|
|
241 #endif
|
|
242
|
|
243 /* skip whitespaces */
|
|
244 while (isspace(line[line_pos]))
|
|
245 ++line_pos;
|
|
246
|
|
247 /* check '=' */
|
|
248 if (line[line_pos++] != '=') {
|
|
249 PRINT_LINENUM;
|
|
250 printf("option without parameter\n");
|
|
251 ret = -1;
|
|
252 continue;
|
|
253 }
|
|
254
|
|
255 /* whitespaces... */
|
|
256 while (isspace(line[line_pos]))
|
|
257 ++line_pos;
|
|
258
|
|
259 /* read the parameter */
|
158
|
260 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]);
|
|
261 /* NOTHING */) {
|
147
|
262 param[param_pos++] = line[line_pos++];
|
|
263 if (param_pos >= MAX_PARAM_LEN) {
|
|
264 PRINT_LINENUM;
|
|
265 printf("too long parameter\n");
|
|
266 ret = -1;
|
|
267 continue;
|
|
268 }
|
|
269 }
|
|
270 param[param_pos] = '\0';
|
|
271
|
|
272 /* did we read a parameter? */
|
|
273 if (param_pos == 0) {
|
|
274 PRINT_LINENUM;
|
|
275 printf("option without parameter\n");
|
|
276 ret = -1;
|
|
277 continue;
|
|
278 }
|
|
279 #ifdef DEBUG
|
|
280 PRINT_LINENUM;
|
|
281 printf("parameter: %s\n", param);
|
|
282 #endif
|
|
283 /* now, check if we have some more chars on the line */
|
|
284 /* whitespace... */
|
|
285 while (isspace(line[line_pos]))
|
|
286 ++line_pos;
|
|
287
|
|
288 /* EOL / comment */
|
|
289 if (line[line_pos] != '\0' && line[line_pos] != '#') {
|
|
290 PRINT_LINENUM;
|
|
291 printf("extra characters on line: %s\n", line+line_pos);
|
|
292 ret = -1;
|
|
293 }
|
|
294
|
|
295 tmp = read_option(opt, param);
|
|
296 switch (tmp) {
|
|
297 case ERR_NOT_AN_OPTION:
|
|
298 PRINT_LINENUM;
|
|
299 printf("invalid option: %s\n", opt);
|
|
300 ret = -1;
|
|
301 continue;
|
|
302 /* break; */
|
|
303 case ERR_MISSING_PARAM:
|
|
304 PRINT_LINENUM;
|
|
305 printf("missing parameter: %s\n", opt);
|
|
306 ret = -1;
|
|
307 continue;
|
|
308 /* break; */
|
|
309 case ERR_OUT_OF_RANGE:
|
|
310 PRINT_LINENUM;
|
|
311 printf("parameter of %s out of range\n", opt);
|
|
312 ret = -1;
|
|
313 continue;
|
|
314 /* break; */
|
150
|
315 case ERR_FUNC_ERR:
|
|
316 PRINT_LINENUM;
|
|
317 printf("parser function returned error: %s\n", opt);
|
|
318 ret = -1;
|
|
319 continue;
|
|
320 /* break */
|
147
|
321 }
|
|
322 }
|
|
323
|
|
324 free(line);
|
|
325 fclose(fp);
|
|
326 return ret;
|
|
327 }
|
|
328
|
|
329 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char **filename)
|
|
330 {
|
|
331 int i;
|
|
332 int found_filename = 0;
|
|
333 int tmp;
|
|
334 char *opt;
|
|
335
|
|
336 #ifdef DEBUG
|
|
337 assert(argv != NULL);
|
|
338 assert(envp != NULL);
|
|
339 assert(argc >= 1);
|
|
340 #endif
|
|
341
|
|
342 if (init_conf(conf, COMMAND_LINE) == -1)
|
|
343 return -1;
|
|
344
|
|
345 for (i = 1; i < argc; i++) {
|
|
346 opt = argv[i];
|
|
347 if (*opt != '-')
|
|
348 goto not_an_option;
|
|
349
|
|
350 /* remove trailing '-' */
|
|
351 opt++;
|
150
|
352 #if 0
|
147
|
353 /* check for --help, -h, and --version */
|
|
354 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
|
|
355 printf("%s%s", banner_text, help_text);
|
|
356 continue;
|
|
357 }
|
|
358 if (!strcasecmp(opt, "-version")) {
|
|
359 printf("%s", banner_text);
|
|
360 continue;
|
|
361 }
|
150
|
362 #endif
|
147
|
363
|
|
364 tmp = read_option(opt, argv[i + 1]);
|
|
365
|
|
366 switch (tmp) {
|
|
367 case ERR_NOT_AN_OPTION:
|
|
368 not_an_option:
|
|
369 /* opt is not an option -> treat it as a filename */
|
|
370 if (found_filename) {
|
|
371 /* we already have a filename */
|
|
372 printf("parse_command_line: invalid option: %s\n", argv[i]);
|
|
373 return -1;
|
|
374 } else {
|
|
375 found_filename = 1;
|
|
376 *filename = argv[i];
|
|
377 printf("parse_command_line: found filename: %s\n", *filename);
|
|
378 continue; /* next option */
|
|
379 }
|
|
380 break;
|
|
381 case ERR_MISSING_PARAM:
|
|
382 printf("parse_command_line: missing parameter: %s\n", argv[i]);
|
|
383 return -1;
|
|
384 /* break; */
|
|
385 case ERR_OUT_OF_RANGE:
|
|
386 printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]);
|
|
387 return -1;
|
|
388 /* break; */
|
150
|
389 case ERR_FUNC_ERR:
|
|
390 printf("parse_command_line: parser function returned error: %s\n", argv[i]);
|
|
391 return -1;
|
|
392 /* break; */
|
147
|
393 }
|
|
394
|
|
395 i += tmp; /* we already processed the params (if there was any) */
|
|
396 }
|
|
397 return found_filename;
|
|
398 }
|