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