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