Mercurial > mplayer.hg
annotate cfgparser.c @ 344:9f66a83f31e6
yeah, cvs/DOCS is back! some minor corrections
author | gabucino |
---|---|
date | Wed, 11 Apr 2001 09:25:59 +0000 |
parents | e24fe1b5b918 |
children | 787f2792beeb |
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 | |
287 /* read option. accept char if isalnum(char) */ | |
288 for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) { | |
289 opt[opt_pos++] = line[line_pos++]; | |
290 if (opt_pos >= MAX_OPT_LEN) { | |
291 PRINT_LINENUM; | |
292 printf("too long option\n"); | |
293 ret = -1; | |
294 continue; | |
295 } | |
296 } | |
297 if (opt_pos == 0) { | |
298 PRINT_LINENUM; | |
299 printf("parse error\n"); | |
300 ret = -1; | |
301 continue; | |
302 } | |
303 opt[opt_pos] = '\0'; | |
304 #ifdef DEBUG | |
305 PRINT_LINENUM; | |
306 printf("option: %s\n", opt); | |
307 #endif | |
308 | |
309 /* skip whitespaces */ | |
310 while (isspace(line[line_pos])) | |
311 ++line_pos; | |
312 | |
313 /* check '=' */ | |
314 if (line[line_pos++] != '=') { | |
315 PRINT_LINENUM; | |
316 printf("option without parameter\n"); | |
317 ret = -1; | |
318 continue; | |
319 } | |
320 | |
321 /* whitespaces... */ | |
322 while (isspace(line[line_pos])) | |
323 ++line_pos; | |
324 | |
325 /* read the parameter */ | |
167 | 326 if (line[line_pos] == '"' || line[line_pos] == '\'') { |
327 c = line[line_pos]; | |
328 ++line_pos; | |
329 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
330 param[param_pos++] = line[line_pos++]; | |
331 if (param_pos >= MAX_PARAM_LEN) { | |
332 PRINT_LINENUM; | |
333 printf("too long parameter\n"); | |
334 ret = -1; | |
335 continue; | |
336 } | |
337 } | |
338 line_pos++; /* skip the closing " or ' */ | |
339 } else { | |
340 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
341 && line[line_pos] != '#'; /* NOTHING */) { | |
342 param[param_pos++] = line[line_pos++]; | |
343 if (param_pos >= MAX_PARAM_LEN) { | |
344 PRINT_LINENUM; | |
345 printf("too long parameter\n"); | |
346 ret = -1; | |
347 continue; | |
348 } | |
147 | 349 } |
350 } | |
351 param[param_pos] = '\0'; | |
352 | |
353 /* did we read a parameter? */ | |
354 if (param_pos == 0) { | |
355 PRINT_LINENUM; | |
356 printf("option without parameter\n"); | |
357 ret = -1; | |
358 continue; | |
359 } | |
360 #ifdef DEBUG | |
361 PRINT_LINENUM; | |
362 printf("parameter: %s\n", param); | |
363 #endif | |
364 /* now, check if we have some more chars on the line */ | |
365 /* whitespace... */ | |
366 while (isspace(line[line_pos])) | |
367 ++line_pos; | |
368 | |
369 /* EOL / comment */ | |
370 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
371 PRINT_LINENUM; | |
372 printf("extra characters on line: %s\n", line+line_pos); | |
373 ret = -1; | |
374 } | |
375 | |
376 tmp = read_option(opt, param); | |
377 switch (tmp) { | |
378 case ERR_NOT_AN_OPTION: | |
379 case ERR_MISSING_PARAM: | |
380 case ERR_OUT_OF_RANGE: | |
150 | 381 case ERR_FUNC_ERR: |
382 PRINT_LINENUM; | |
160 | 383 printf("%s\n", opt); |
150 | 384 ret = -1; |
385 continue; | |
386 /* break */ | |
147 | 387 } |
388 } | |
389 | |
390 free(line); | |
391 fclose(fp); | |
166 | 392 out: |
393 --recursion_depth; | |
147 | 394 return ret; |
395 } | |
396 | |
397 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char **filename) | |
398 { | |
399 int i; | |
400 int found_filename = 0; | |
401 int tmp; | |
402 char *opt; | |
403 | |
404 #ifdef DEBUG | |
405 assert(argv != NULL); | |
406 assert(envp != NULL); | |
407 assert(argc >= 1); | |
408 #endif | |
409 | |
410 if (init_conf(conf, COMMAND_LINE) == -1) | |
411 return -1; | |
412 | |
413 for (i = 1; i < argc; i++) { | |
414 opt = argv[i]; | |
415 if (*opt != '-') | |
416 goto not_an_option; | |
417 | |
418 /* remove trailing '-' */ | |
419 opt++; | |
420 | |
421 tmp = read_option(opt, argv[i + 1]); | |
422 | |
423 switch (tmp) { | |
424 case ERR_NOT_AN_OPTION: | |
425 not_an_option: | |
426 /* opt is not an option -> treat it as a filename */ | |
427 if (found_filename) { | |
428 /* we already have a filename */ | |
160 | 429 goto err_out; |
147 | 430 } else { |
431 found_filename = 1; | |
432 *filename = argv[i]; | |
433 continue; /* next option */ | |
434 } | |
435 break; | |
436 case ERR_MISSING_PARAM: | |
437 case ERR_OUT_OF_RANGE: | |
150 | 438 case ERR_FUNC_ERR: |
160 | 439 goto err_out; |
150 | 440 /* break; */ |
147 | 441 } |
442 | |
443 i += tmp; /* we already processed the params (if there was any) */ | |
160 | 444 } |
147 | 445 return found_filename; |
160 | 446 err_out: |
447 printf("parse_command_line: %s\n", argv[i]); | |
448 return -1; | |
147 | 449 } |