Mercurial > mplayer.hg
annotate cfgparser.c @ 2359:ee6ef0b10655
More iopl'ed ports and test for brken BIOS
author | nick |
---|---|
date | Mon, 22 Oct 2001 09:28:44 +0000 |
parents | 624df8ea0e0e |
children | c1f71b8190d1 |
rev | line source |
---|---|
147 | 1 /* |
2 * command line and config file parser | |
336 | 3 * by Szabolcs Berecz <szabi@inf.elte.hu> |
4 * (C) 2001 | |
147 | 5 */ |
6 | |
7 //#define DEBUG | |
8 | |
9 #include <stdlib.h> | |
10 #include <stdio.h> | |
11 #include <ctype.h> | |
12 #include <unistd.h> | |
13 #include <fcntl.h> | |
14 #include <string.h> | |
336 | 15 #include <errno.h> |
147 | 16 |
17 | |
18 #define COMMAND_LINE 0 | |
19 #define CONFIG_FILE 1 | |
20 | |
177 | 21 #define MAX_RECURSION_DEPTH 8 |
166 | 22 |
147 | 23 #ifdef DEBUG |
24 #include <assert.h> | |
25 #endif | |
26 | |
27 #include "cfgparser.h" | |
28 | |
29 static struct config *config; | |
30 static int nr_options; /* number of options in 'conf' */ | |
31 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */ | |
166 | 32 static int recursion_depth = 0; |
147 | 33 |
34 static int init_conf(struct config *conf, int mode) | |
35 { | |
36 #ifdef DEBUG | |
37 assert(conf != NULL); | |
38 #endif | |
39 | |
40 /* calculate the number of options in 'conf' */ | |
41 for (nr_options = 0; conf[nr_options].name != NULL; nr_options++) | |
42 /* NOTHING */; | |
43 | |
44 config = conf; | |
45 #ifdef DEBUG | |
46 if (mode != COMMAND_LINE && mode != CONFIG_FILE) { | |
150 | 47 printf("init_conf: wrong mode!\n"); |
147 | 48 return -1; |
49 } | |
50 #endif | |
51 parser_mode = mode; | |
52 return 1; | |
53 } | |
54 | |
55 static int read_option(char *opt, char *param) | |
56 { | |
57 int i; | |
195 | 58 long tmp_int; |
59 double tmp_float; | |
160 | 60 int ret = -1; |
195 | 61 char *endptr; |
147 | 62 |
63 for (i = 0; i < nr_options; i++) { | |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
64 int namelength; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
65 /* allow 'aa*' in config.name */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
66 namelength=strlen(config[i].name); |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
67 if ( (config[i].name[namelength-1]=='*') && |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
68 !memcmp(opt, config[i].name, namelength-1)) |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
69 break; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
70 |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
71 |
147 | 72 if (!strcasecmp(opt, config[i].name)) |
73 break; | |
74 } | |
160 | 75 if (i == nr_options) { |
76 printf("invalid option:\n"); | |
77 ret = ERR_NOT_AN_OPTION; | |
78 goto out; | |
79 } | |
80 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) { | |
81 printf("this option can only be used on command line:\n"); | |
82 ret = ERR_NOT_AN_OPTION; | |
83 goto out; | |
84 } | |
85 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) { | |
86 printf("this option can only be used in config file:\n"); | |
87 ret = ERR_NOT_AN_OPTION; | |
88 goto out; | |
89 } | |
150 | 90 |
147 | 91 switch (config[i].type) { |
92 case CONF_TYPE_FLAG: | |
93 /* flags need a parameter in config file */ | |
94 if (parser_mode == CONFIG_FILE) { | |
95 if (!strcasecmp(param, "yes") || /* any other language? */ | |
96 !strcasecmp(param, "ja") || | |
161
9008302e2dc0
Added support for spanish (Yes, I'm a C programer also ;o)
telenieko
parents:
160
diff
changeset
|
97 !strcasecmp(param, "si") || |
147 | 98 !strcasecmp(param, "igen") || |
99 !strcasecmp(param, "y") || | |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
100 !strcasecmp(param, "j") || |
147 | 101 !strcasecmp(param, "i") || |
102 !strcmp(param, "1")) | |
103 *((int *) config[i].p) = config[i].max; | |
151 | 104 else if (!strcasecmp(param, "no") || |
147 | 105 !strcasecmp(param, "nein") || |
106 !strcasecmp(param, "nicht") || | |
107 !strcasecmp(param, "nem") || | |
108 !strcasecmp(param, "n") || | |
109 !strcmp(param, "0")) | |
110 *((int *) config[i].p) = config[i].min; | |
160 | 111 else { |
112 printf("invalid parameter for flag:\n"); | |
113 ret = ERR_OUT_OF_RANGE; | |
114 goto out; | |
115 } | |
116 ret = 1; | |
147 | 117 } else { /* parser_mode == COMMAND_LINE */ |
118 *((int *) config[i].p) = config[i].max; | |
160 | 119 ret = 0; |
147 | 120 } |
121 break; | |
122 case CONF_TYPE_INT: | |
123 if (param == NULL) | |
160 | 124 goto err_missing_param; |
195 | 125 |
126 tmp_int = strtol(param, &endptr, 0); | |
127 if (*endptr) { | |
160 | 128 printf("parameter must be an integer:\n"); |
129 ret = ERR_OUT_OF_RANGE; | |
130 goto out; | |
131 } | |
147 | 132 |
153 | 133 if (config[i].flags & CONF_MIN) |
160 | 134 if (tmp_int < config[i].min) { |
135 printf("parameter must be >= %d:\n", (int) config[i].min); | |
136 ret = ERR_OUT_OF_RANGE; | |
137 goto out; | |
138 } | |
147 | 139 |
153 | 140 if (config[i].flags & CONF_MAX) |
160 | 141 if (tmp_int > config[i].max) { |
142 printf("parameter must be <= %d:\n", (int) config[i].max); | |
143 ret = ERR_OUT_OF_RANGE; | |
144 goto out; | |
145 } | |
147 | 146 |
147 *((int *) config[i].p) = tmp_int; | |
160 | 148 ret = 1; |
147 | 149 break; |
150 case CONF_TYPE_FLOAT: | |
151 if (param == NULL) | |
160 | 152 goto err_missing_param; |
195 | 153 |
154 tmp_float = strtod(param, &endptr); | |
2031
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
155 |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
156 if ((*endptr == ':') || (*endptr == '/')) |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
157 tmp_float /= strtod(endptr+1, &endptr); |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
158 |
195 | 159 if (*endptr) { |
2031
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
160 printf("parameter must be a floating point number" |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
161 " or a ratio (numerator[:/]denominator):\n"); |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
162 |
160 | 163 ret = ERR_MISSING_PARAM; |
164 goto out; | |
165 } | |
147 | 166 |
153 | 167 if (config[i].flags & CONF_MIN) |
160 | 168 if (tmp_float < config[i].min) { |
169 printf("parameter must be >= %f:\n", config[i].min); | |
170 ret = ERR_OUT_OF_RANGE; | |
171 goto out; | |
172 } | |
147 | 173 |
153 | 174 if (config[i].flags & CONF_MAX) |
160 | 175 if (tmp_float > config[i].max) { |
176 printf("parameter must be <= %f:\n", config[i].max); | |
177 ret = ERR_OUT_OF_RANGE; | |
178 goto out; | |
179 } | |
147 | 180 |
181 *((float *) config[i].p) = tmp_float; | |
160 | 182 ret = 1; |
147 | 183 break; |
184 case CONF_TYPE_STRING: | |
185 if (param == NULL) | |
160 | 186 goto err_missing_param; |
147 | 187 |
153 | 188 if (config[i].flags & CONF_MIN) |
160 | 189 if (strlen(param) < config[i].min) { |
190 printf("parameter must be >= %d chars:\n", | |
191 (int) config[i].min); | |
192 ret = ERR_OUT_OF_RANGE; | |
193 goto out; | |
194 } | |
147 | 195 |
153 | 196 if (config[i].flags & CONF_MAX) |
160 | 197 if (strlen(param) > config[i].max) { |
198 printf("parameter must be <= %d chars:\n", | |
199 (int) config[i].max); | |
200 ret = ERR_OUT_OF_RANGE; | |
201 goto out; | |
202 } | |
147 | 203 |
204 *((char **) config[i].p) = strdup(param); | |
160 | 205 ret = 1; |
147 | 206 break; |
151 | 207 case CONF_TYPE_FUNC_PARAM: |
208 if (param == NULL) | |
160 | 209 goto err_missing_param; |
210 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) { | |
211 ret = ERR_FUNC_ERR; | |
212 goto out; | |
213 } | |
214 ret = 1; | |
151 | 215 break; |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
216 case CONF_TYPE_FUNC_FULL: |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
217 if (param!=NULL && param[0]=='-'){ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
218 ret=((cfg_func_arg_param_t) config[i].p)(config + i, opt, NULL); |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
219 if (ret>=0) ret=0; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
220 /* if we return >=0: param is processed again (if there is any) */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
221 }else{ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
222 ret=((cfg_func_arg_param_t) config[i].p)(config + i, opt, param); |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
223 /* if we return 0: need no param, precess it again */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
224 /* if we return 1: accepted param */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
225 } |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
226 break; |
150 | 227 case CONF_TYPE_FUNC: |
160 | 228 if ((((cfg_func_t) config[i].p)(config + i)) < 0) { |
229 ret = ERR_FUNC_ERR; | |
230 goto out; | |
231 } | |
232 ret = 0; | |
150 | 233 break; |
152 | 234 case CONF_TYPE_PRINT: |
235 printf("%s", (char *) config[i].p); | |
236 exit(1); | |
147 | 237 default: |
238 printf("picsaba\n"); | |
239 break; | |
240 } | |
160 | 241 out: |
242 return ret; | |
243 err_missing_param: | |
244 printf("missing parameter:\n"); | |
245 ret = ERR_MISSING_PARAM; | |
246 goto out; | |
147 | 247 } |
248 | |
249 int parse_config_file(struct config *conf, char *conffile) | |
250 { | |
251 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num) | |
252 #define MAX_LINE_LEN 1000 | |
253 #define MAX_OPT_LEN 100 | |
254 #define MAX_PARAM_LEN 100 | |
255 FILE *fp; | |
256 char *line; | |
179 | 257 char opt[MAX_OPT_LEN + 1]; |
258 char param[MAX_PARAM_LEN + 1]; | |
167 | 259 char c; /* for the "" and '' check */ |
147 | 260 int tmp; |
261 int line_num = 0; | |
262 int line_pos; /* line pos */ | |
263 int opt_pos; /* opt pos */ | |
264 int param_pos; /* param pos */ | |
265 int ret = 1; | |
1090 | 266 int errors = 0; |
147 | 267 |
268 #ifdef DEBUG | |
269 assert(conffile != NULL); | |
270 #endif | |
1089 | 271 if (++recursion_depth > 1) |
272 printf("Reading config file: %s", conffile); | |
273 | |
274 if (recursion_depth > MAX_RECURSION_DEPTH) { | |
275 printf(": too deep 'include'. check your configfiles\n"); | |
276 ret = -1; | |
277 goto out; | |
278 } | |
166 | 279 |
280 if (init_conf(conf, CONFIG_FILE) == -1) { | |
281 ret = -1; | |
282 goto out; | |
283 } | |
147 | 284 |
179 | 285 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { |
336 | 286 perror("\ncan't get memory for 'line'"); |
166 | 287 ret = -1; |
288 goto out; | |
147 | 289 } |
290 | |
291 if ((fp = fopen(conffile, "r")) == NULL) { | |
1089 | 292 if (recursion_depth > 1) |
293 printf(": %s\n", strerror(errno)); | |
147 | 294 free(line); |
166 | 295 ret = 0; |
296 goto out; | |
147 | 297 } |
1089 | 298 if (recursion_depth > 1) |
299 printf("\n"); | |
147 | 300 |
301 while (fgets(line, MAX_LINE_LEN, fp)) { | |
1090 | 302 if (errors >= 16) { |
303 printf("too many errors\n"); | |
304 goto out; | |
305 } | |
306 | |
147 | 307 line_num++; |
308 line_pos = 0; | |
309 | |
310 /* skip whitespaces */ | |
311 while (isspace(line[line_pos])) | |
312 ++line_pos; | |
313 | |
314 /* EOL / comment */ | |
315 if (line[line_pos] == '\0' || line[line_pos] == '#') | |
316 continue; | |
317 | |
480 | 318 /* read option. */ |
319 for (opt_pos = 0; isprint(line[line_pos]) && | |
320 line[line_pos] != ' ' && | |
321 line[line_pos] != '#' && | |
322 line[line_pos] != '='; /* NOTHING */) { | |
147 | 323 opt[opt_pos++] = line[line_pos++]; |
324 if (opt_pos >= MAX_OPT_LEN) { | |
325 PRINT_LINENUM; | |
326 printf("too long option\n"); | |
1090 | 327 errors++; |
147 | 328 ret = -1; |
1090 | 329 goto nextline; |
147 | 330 } |
331 } | |
332 if (opt_pos == 0) { | |
333 PRINT_LINENUM; | |
334 printf("parse error\n"); | |
335 ret = -1; | |
1090 | 336 errors++; |
147 | 337 continue; |
338 } | |
339 opt[opt_pos] = '\0'; | |
340 #ifdef DEBUG | |
341 PRINT_LINENUM; | |
342 printf("option: %s\n", opt); | |
343 #endif | |
344 | |
345 /* skip whitespaces */ | |
346 while (isspace(line[line_pos])) | |
347 ++line_pos; | |
348 | |
349 /* check '=' */ | |
350 if (line[line_pos++] != '=') { | |
351 PRINT_LINENUM; | |
352 printf("option without parameter\n"); | |
353 ret = -1; | |
1090 | 354 errors++; |
147 | 355 continue; |
356 } | |
357 | |
358 /* whitespaces... */ | |
359 while (isspace(line[line_pos])) | |
360 ++line_pos; | |
361 | |
362 /* read the parameter */ | |
167 | 363 if (line[line_pos] == '"' || line[line_pos] == '\'') { |
364 c = line[line_pos]; | |
365 ++line_pos; | |
366 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
367 param[param_pos++] = line[line_pos++]; | |
368 if (param_pos >= MAX_PARAM_LEN) { | |
369 PRINT_LINENUM; | |
370 printf("too long parameter\n"); | |
371 ret = -1; | |
1090 | 372 errors++; |
373 goto nextline; | |
167 | 374 } |
375 } | |
376 line_pos++; /* skip the closing " or ' */ | |
377 } else { | |
378 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
379 && line[line_pos] != '#'; /* NOTHING */) { | |
380 param[param_pos++] = line[line_pos++]; | |
381 if (param_pos >= MAX_PARAM_LEN) { | |
382 PRINT_LINENUM; | |
383 printf("too long parameter\n"); | |
384 ret = -1; | |
1090 | 385 errors++; |
386 goto nextline; | |
167 | 387 } |
147 | 388 } |
389 } | |
390 param[param_pos] = '\0'; | |
391 | |
392 /* did we read a parameter? */ | |
393 if (param_pos == 0) { | |
394 PRINT_LINENUM; | |
395 printf("option without parameter\n"); | |
396 ret = -1; | |
1090 | 397 errors++; |
147 | 398 continue; |
399 } | |
400 #ifdef DEBUG | |
401 PRINT_LINENUM; | |
402 printf("parameter: %s\n", param); | |
403 #endif | |
404 /* now, check if we have some more chars on the line */ | |
405 /* whitespace... */ | |
406 while (isspace(line[line_pos])) | |
407 ++line_pos; | |
408 | |
409 /* EOL / comment */ | |
410 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
411 PRINT_LINENUM; | |
412 printf("extra characters on line: %s\n", line+line_pos); | |
413 ret = -1; | |
414 } | |
415 | |
416 tmp = read_option(opt, param); | |
417 switch (tmp) { | |
418 case ERR_NOT_AN_OPTION: | |
419 case ERR_MISSING_PARAM: | |
420 case ERR_OUT_OF_RANGE: | |
150 | 421 case ERR_FUNC_ERR: |
422 PRINT_LINENUM; | |
160 | 423 printf("%s\n", opt); |
150 | 424 ret = -1; |
1090 | 425 errors++; |
150 | 426 continue; |
427 /* break */ | |
147 | 428 } |
1093 | 429 nextline: |
1304 | 430 ; |
147 | 431 } |
432 | |
433 free(line); | |
434 fclose(fp); | |
166 | 435 out: |
436 --recursion_depth; | |
147 | 437 return ret; |
438 } | |
439 | |
1629 | 440 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames) |
147 | 441 { |
442 int i; | |
1629 | 443 char **f = NULL; |
444 int f_nr = 0; | |
147 | 445 int tmp; |
446 char *opt; | |
447 | |
448 #ifdef DEBUG | |
449 assert(argv != NULL); | |
450 assert(envp != NULL); | |
451 assert(argc >= 1); | |
452 #endif | |
453 | |
454 if (init_conf(conf, COMMAND_LINE) == -1) | |
455 return -1; | |
456 | |
1089 | 457 /* in order to work recursion detection properly in parse_config_file */ |
458 ++recursion_depth; | |
459 | |
147 | 460 for (i = 1; i < argc; i++) { |
461 opt = argv[i]; | |
1629 | 462 if (*opt != '-') |
1075 | 463 goto filename; |
147 | 464 |
465 /* remove trailing '-' */ | |
466 opt++; | |
467 | |
468 tmp = read_option(opt, argv[i + 1]); | |
469 | |
470 switch (tmp) { | |
471 case ERR_NOT_AN_OPTION: | |
1629 | 472 filename: |
147 | 473 /* opt is not an option -> treat it as a filename */ |
1629 | 474 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2)))) |
475 goto err_out_mem; | |
476 | |
477 f[f_nr++] = argv[i]; | |
147 | 478 break; |
479 case ERR_MISSING_PARAM: | |
480 case ERR_OUT_OF_RANGE: | |
150 | 481 case ERR_FUNC_ERR: |
160 | 482 goto err_out; |
150 | 483 /* break; */ |
1075 | 484 default: |
485 i += tmp; | |
147 | 486 } |
1089 | 487 } |
1629 | 488 if (f) |
489 f[f_nr] = NULL; | |
490 if (filenames) | |
491 *filenames = f; | |
1089 | 492 --recursion_depth; |
1629 | 493 return f_nr; //filenames_nr; |
494 err_out_mem: | |
495 printf("can't allocate memory for filenames\n"); | |
160 | 496 err_out: |
1089 | 497 --recursion_depth; |
1075 | 498 printf("command line: %s\n", argv[i]); |
160 | 499 return -1; |
147 | 500 } |