Mercurial > mplayer.hg
annotate cfgparser.c @ 1896:e6acbf9e9b53
Translated by me, but not checked
author | jaf |
---|---|
date | Sun, 16 Sep 2001 08:53:30 +0000 |
parents | 13aeaa05ac5e |
children | 624df8ea0e0e |
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); | |
155 if (*endptr) { | |
160 | 156 printf("parameter must be a floating point number:\n"); |
157 ret = ERR_MISSING_PARAM; | |
158 goto out; | |
159 } | |
147 | 160 |
153 | 161 if (config[i].flags & CONF_MIN) |
160 | 162 if (tmp_float < config[i].min) { |
163 printf("parameter must be >= %f:\n", config[i].min); | |
164 ret = ERR_OUT_OF_RANGE; | |
165 goto out; | |
166 } | |
147 | 167 |
153 | 168 if (config[i].flags & CONF_MAX) |
160 | 169 if (tmp_float > config[i].max) { |
170 printf("parameter must be <= %f:\n", config[i].max); | |
171 ret = ERR_OUT_OF_RANGE; | |
172 goto out; | |
173 } | |
147 | 174 |
175 *((float *) config[i].p) = tmp_float; | |
160 | 176 ret = 1; |
147 | 177 break; |
178 case CONF_TYPE_STRING: | |
179 if (param == NULL) | |
160 | 180 goto err_missing_param; |
147 | 181 |
153 | 182 if (config[i].flags & CONF_MIN) |
160 | 183 if (strlen(param) < config[i].min) { |
184 printf("parameter must be >= %d chars:\n", | |
185 (int) config[i].min); | |
186 ret = ERR_OUT_OF_RANGE; | |
187 goto out; | |
188 } | |
147 | 189 |
153 | 190 if (config[i].flags & CONF_MAX) |
160 | 191 if (strlen(param) > config[i].max) { |
192 printf("parameter must be <= %d chars:\n", | |
193 (int) config[i].max); | |
194 ret = ERR_OUT_OF_RANGE; | |
195 goto out; | |
196 } | |
147 | 197 |
198 *((char **) config[i].p) = strdup(param); | |
160 | 199 ret = 1; |
147 | 200 break; |
151 | 201 case CONF_TYPE_FUNC_PARAM: |
202 if (param == NULL) | |
160 | 203 goto err_missing_param; |
204 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) { | |
205 ret = ERR_FUNC_ERR; | |
206 goto out; | |
207 } | |
208 ret = 1; | |
151 | 209 break; |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
210 case CONF_TYPE_FUNC_FULL: |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
211 if (param!=NULL && param[0]=='-'){ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
212 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
|
213 if (ret>=0) ret=0; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
214 /* 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
|
215 }else{ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
216 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
|
217 /* 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
|
218 /* if we return 1: accepted param */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
219 } |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
220 break; |
150 | 221 case CONF_TYPE_FUNC: |
160 | 222 if ((((cfg_func_t) config[i].p)(config + i)) < 0) { |
223 ret = ERR_FUNC_ERR; | |
224 goto out; | |
225 } | |
226 ret = 0; | |
150 | 227 break; |
152 | 228 case CONF_TYPE_PRINT: |
229 printf("%s", (char *) config[i].p); | |
230 exit(1); | |
147 | 231 default: |
232 printf("picsaba\n"); | |
233 break; | |
234 } | |
160 | 235 out: |
236 return ret; | |
237 err_missing_param: | |
238 printf("missing parameter:\n"); | |
239 ret = ERR_MISSING_PARAM; | |
240 goto out; | |
147 | 241 } |
242 | |
243 int parse_config_file(struct config *conf, char *conffile) | |
244 { | |
245 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num) | |
246 #define MAX_LINE_LEN 1000 | |
247 #define MAX_OPT_LEN 100 | |
248 #define MAX_PARAM_LEN 100 | |
249 FILE *fp; | |
250 char *line; | |
179 | 251 char opt[MAX_OPT_LEN + 1]; |
252 char param[MAX_PARAM_LEN + 1]; | |
167 | 253 char c; /* for the "" and '' check */ |
147 | 254 int tmp; |
255 int line_num = 0; | |
256 int line_pos; /* line pos */ | |
257 int opt_pos; /* opt pos */ | |
258 int param_pos; /* param pos */ | |
259 int ret = 1; | |
1090 | 260 int errors = 0; |
147 | 261 |
262 #ifdef DEBUG | |
263 assert(conffile != NULL); | |
264 #endif | |
1089 | 265 if (++recursion_depth > 1) |
266 printf("Reading config file: %s", conffile); | |
267 | |
268 if (recursion_depth > MAX_RECURSION_DEPTH) { | |
269 printf(": too deep 'include'. check your configfiles\n"); | |
270 ret = -1; | |
271 goto out; | |
272 } | |
166 | 273 |
274 if (init_conf(conf, CONFIG_FILE) == -1) { | |
275 ret = -1; | |
276 goto out; | |
277 } | |
147 | 278 |
179 | 279 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { |
336 | 280 perror("\ncan't get memory for 'line'"); |
166 | 281 ret = -1; |
282 goto out; | |
147 | 283 } |
284 | |
285 if ((fp = fopen(conffile, "r")) == NULL) { | |
1089 | 286 if (recursion_depth > 1) |
287 printf(": %s\n", strerror(errno)); | |
147 | 288 free(line); |
166 | 289 ret = 0; |
290 goto out; | |
147 | 291 } |
1089 | 292 if (recursion_depth > 1) |
293 printf("\n"); | |
147 | 294 |
295 while (fgets(line, MAX_LINE_LEN, fp)) { | |
1090 | 296 if (errors >= 16) { |
297 printf("too many errors\n"); | |
298 goto out; | |
299 } | |
300 | |
147 | 301 line_num++; |
302 line_pos = 0; | |
303 | |
304 /* skip whitespaces */ | |
305 while (isspace(line[line_pos])) | |
306 ++line_pos; | |
307 | |
308 /* EOL / comment */ | |
309 if (line[line_pos] == '\0' || line[line_pos] == '#') | |
310 continue; | |
311 | |
480 | 312 /* read option. */ |
313 for (opt_pos = 0; isprint(line[line_pos]) && | |
314 line[line_pos] != ' ' && | |
315 line[line_pos] != '#' && | |
316 line[line_pos] != '='; /* NOTHING */) { | |
147 | 317 opt[opt_pos++] = line[line_pos++]; |
318 if (opt_pos >= MAX_OPT_LEN) { | |
319 PRINT_LINENUM; | |
320 printf("too long option\n"); | |
1090 | 321 errors++; |
147 | 322 ret = -1; |
1090 | 323 goto nextline; |
147 | 324 } |
325 } | |
326 if (opt_pos == 0) { | |
327 PRINT_LINENUM; | |
328 printf("parse error\n"); | |
329 ret = -1; | |
1090 | 330 errors++; |
147 | 331 continue; |
332 } | |
333 opt[opt_pos] = '\0'; | |
334 #ifdef DEBUG | |
335 PRINT_LINENUM; | |
336 printf("option: %s\n", opt); | |
337 #endif | |
338 | |
339 /* skip whitespaces */ | |
340 while (isspace(line[line_pos])) | |
341 ++line_pos; | |
342 | |
343 /* check '=' */ | |
344 if (line[line_pos++] != '=') { | |
345 PRINT_LINENUM; | |
346 printf("option without parameter\n"); | |
347 ret = -1; | |
1090 | 348 errors++; |
147 | 349 continue; |
350 } | |
351 | |
352 /* whitespaces... */ | |
353 while (isspace(line[line_pos])) | |
354 ++line_pos; | |
355 | |
356 /* read the parameter */ | |
167 | 357 if (line[line_pos] == '"' || line[line_pos] == '\'') { |
358 c = line[line_pos]; | |
359 ++line_pos; | |
360 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
361 param[param_pos++] = line[line_pos++]; | |
362 if (param_pos >= MAX_PARAM_LEN) { | |
363 PRINT_LINENUM; | |
364 printf("too long parameter\n"); | |
365 ret = -1; | |
1090 | 366 errors++; |
367 goto nextline; | |
167 | 368 } |
369 } | |
370 line_pos++; /* skip the closing " or ' */ | |
371 } else { | |
372 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
373 && line[line_pos] != '#'; /* NOTHING */) { | |
374 param[param_pos++] = line[line_pos++]; | |
375 if (param_pos >= MAX_PARAM_LEN) { | |
376 PRINT_LINENUM; | |
377 printf("too long parameter\n"); | |
378 ret = -1; | |
1090 | 379 errors++; |
380 goto nextline; | |
167 | 381 } |
147 | 382 } |
383 } | |
384 param[param_pos] = '\0'; | |
385 | |
386 /* did we read a parameter? */ | |
387 if (param_pos == 0) { | |
388 PRINT_LINENUM; | |
389 printf("option without parameter\n"); | |
390 ret = -1; | |
1090 | 391 errors++; |
147 | 392 continue; |
393 } | |
394 #ifdef DEBUG | |
395 PRINT_LINENUM; | |
396 printf("parameter: %s\n", param); | |
397 #endif | |
398 /* now, check if we have some more chars on the line */ | |
399 /* whitespace... */ | |
400 while (isspace(line[line_pos])) | |
401 ++line_pos; | |
402 | |
403 /* EOL / comment */ | |
404 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
405 PRINT_LINENUM; | |
406 printf("extra characters on line: %s\n", line+line_pos); | |
407 ret = -1; | |
408 } | |
409 | |
410 tmp = read_option(opt, param); | |
411 switch (tmp) { | |
412 case ERR_NOT_AN_OPTION: | |
413 case ERR_MISSING_PARAM: | |
414 case ERR_OUT_OF_RANGE: | |
150 | 415 case ERR_FUNC_ERR: |
416 PRINT_LINENUM; | |
160 | 417 printf("%s\n", opt); |
150 | 418 ret = -1; |
1090 | 419 errors++; |
150 | 420 continue; |
421 /* break */ | |
147 | 422 } |
1093 | 423 nextline: |
1304 | 424 ; |
147 | 425 } |
426 | |
427 free(line); | |
428 fclose(fp); | |
166 | 429 out: |
430 --recursion_depth; | |
147 | 431 return ret; |
432 } | |
433 | |
1629 | 434 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames) |
147 | 435 { |
436 int i; | |
1629 | 437 char **f = NULL; |
438 int f_nr = 0; | |
147 | 439 int tmp; |
440 char *opt; | |
441 | |
442 #ifdef DEBUG | |
443 assert(argv != NULL); | |
444 assert(envp != NULL); | |
445 assert(argc >= 1); | |
446 #endif | |
447 | |
448 if (init_conf(conf, COMMAND_LINE) == -1) | |
449 return -1; | |
450 | |
1089 | 451 /* in order to work recursion detection properly in parse_config_file */ |
452 ++recursion_depth; | |
453 | |
147 | 454 for (i = 1; i < argc; i++) { |
455 opt = argv[i]; | |
1629 | 456 if (*opt != '-') |
1075 | 457 goto filename; |
147 | 458 |
459 /* remove trailing '-' */ | |
460 opt++; | |
461 | |
462 tmp = read_option(opt, argv[i + 1]); | |
463 | |
464 switch (tmp) { | |
465 case ERR_NOT_AN_OPTION: | |
1629 | 466 filename: |
147 | 467 /* opt is not an option -> treat it as a filename */ |
1629 | 468 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2)))) |
469 goto err_out_mem; | |
470 | |
471 f[f_nr++] = argv[i]; | |
147 | 472 break; |
473 case ERR_MISSING_PARAM: | |
474 case ERR_OUT_OF_RANGE: | |
150 | 475 case ERR_FUNC_ERR: |
160 | 476 goto err_out; |
150 | 477 /* break; */ |
1075 | 478 default: |
479 i += tmp; | |
147 | 480 } |
1089 | 481 } |
1629 | 482 if (f) |
483 f[f_nr] = NULL; | |
484 if (filenames) | |
485 *filenames = f; | |
1089 | 486 --recursion_depth; |
1629 | 487 return f_nr; //filenames_nr; |
488 err_out_mem: | |
489 printf("can't allocate memory for filenames\n"); | |
160 | 490 err_out: |
1089 | 491 --recursion_depth; |
1075 | 492 printf("command line: %s\n", argv[i]); |
160 | 493 return -1; |
147 | 494 } |