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