Mercurial > mplayer.hg
annotate cfgparser.c @ 1077:c433e62cb3de
fix again
author | szabii |
---|---|
date | Sat, 09 Jun 2001 17:41:31 +0000 |
parents | a981413af7cd |
children | b2a29e1224e4 |
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 | |
254 if (init_conf(conf, CONFIG_FILE) == -1) { | |
255 ret = -1; | |
256 goto out; | |
257 } | |
147 | 258 |
179 | 259 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { |
336 | 260 perror("\ncan't get memory for 'line'"); |
166 | 261 ret = -1; |
262 goto out; | |
147 | 263 } |
264 | |
265 if ((fp = fopen(conffile, "r")) == NULL) { | |
266 free(line); | |
166 | 267 ret = 0; |
268 goto out; | |
147 | 269 } |
270 | |
271 while (fgets(line, MAX_LINE_LEN, fp)) { | |
272 line_num++; | |
273 line_pos = 0; | |
274 | |
275 /* skip whitespaces */ | |
276 while (isspace(line[line_pos])) | |
277 ++line_pos; | |
278 | |
279 /* EOL / comment */ | |
280 if (line[line_pos] == '\0' || line[line_pos] == '#') | |
281 continue; | |
282 | |
480 | 283 /* read option. */ |
284 for (opt_pos = 0; isprint(line[line_pos]) && | |
285 line[line_pos] != ' ' && | |
286 line[line_pos] != '#' && | |
287 line[line_pos] != '='; /* NOTHING */) { | |
147 | 288 opt[opt_pos++] = line[line_pos++]; |
289 if (opt_pos >= MAX_OPT_LEN) { | |
290 PRINT_LINENUM; | |
291 printf("too long option\n"); | |
292 ret = -1; | |
293 continue; | |
294 } | |
295 } | |
296 if (opt_pos == 0) { | |
297 PRINT_LINENUM; | |
298 printf("parse error\n"); | |
299 ret = -1; | |
300 continue; | |
301 } | |
302 opt[opt_pos] = '\0'; | |
303 #ifdef DEBUG | |
304 PRINT_LINENUM; | |
305 printf("option: %s\n", opt); | |
306 #endif | |
307 | |
308 /* skip whitespaces */ | |
309 while (isspace(line[line_pos])) | |
310 ++line_pos; | |
311 | |
312 /* check '=' */ | |
313 if (line[line_pos++] != '=') { | |
314 PRINT_LINENUM; | |
315 printf("option without parameter\n"); | |
316 ret = -1; | |
317 continue; | |
318 } | |
319 | |
320 /* whitespaces... */ | |
321 while (isspace(line[line_pos])) | |
322 ++line_pos; | |
323 | |
324 /* read the parameter */ | |
167 | 325 if (line[line_pos] == '"' || line[line_pos] == '\'') { |
326 c = line[line_pos]; | |
327 ++line_pos; | |
328 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
329 param[param_pos++] = line[line_pos++]; | |
330 if (param_pos >= MAX_PARAM_LEN) { | |
331 PRINT_LINENUM; | |
332 printf("too long parameter\n"); | |
333 ret = -1; | |
334 continue; | |
335 } | |
336 } | |
337 line_pos++; /* skip the closing " or ' */ | |
338 } else { | |
339 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
340 && line[line_pos] != '#'; /* NOTHING */) { | |
341 param[param_pos++] = line[line_pos++]; | |
342 if (param_pos >= MAX_PARAM_LEN) { | |
343 PRINT_LINENUM; | |
344 printf("too long parameter\n"); | |
345 ret = -1; | |
346 continue; | |
347 } | |
147 | 348 } |
349 } | |
350 param[param_pos] = '\0'; | |
351 | |
352 /* did we read a parameter? */ | |
353 if (param_pos == 0) { | |
354 PRINT_LINENUM; | |
355 printf("option without parameter\n"); | |
356 ret = -1; | |
357 continue; | |
358 } | |
359 #ifdef DEBUG | |
360 PRINT_LINENUM; | |
361 printf("parameter: %s\n", param); | |
362 #endif | |
363 /* now, check if we have some more chars on the line */ | |
364 /* whitespace... */ | |
365 while (isspace(line[line_pos])) | |
366 ++line_pos; | |
367 | |
368 /* EOL / comment */ | |
369 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
370 PRINT_LINENUM; | |
371 printf("extra characters on line: %s\n", line+line_pos); | |
372 ret = -1; | |
373 } | |
374 | |
375 tmp = read_option(opt, param); | |
376 switch (tmp) { | |
377 case ERR_NOT_AN_OPTION: | |
378 case ERR_MISSING_PARAM: | |
379 case ERR_OUT_OF_RANGE: | |
150 | 380 case ERR_FUNC_ERR: |
381 PRINT_LINENUM; | |
160 | 382 printf("%s\n", opt); |
150 | 383 ret = -1; |
384 continue; | |
385 /* break */ | |
147 | 386 } |
387 } | |
388 | |
389 free(line); | |
390 fclose(fp); | |
166 | 391 out: |
392 --recursion_depth; | |
147 | 393 return ret; |
394 } | |
395 | |
396 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char **filename) | |
397 { | |
398 int i; | |
399 int found_filename = 0; | |
400 int tmp; | |
401 char *opt; | |
402 | |
403 #ifdef DEBUG | |
404 assert(argv != NULL); | |
405 assert(envp != NULL); | |
406 assert(argc >= 1); | |
407 #endif | |
408 | |
409 if (init_conf(conf, COMMAND_LINE) == -1) | |
410 return -1; | |
411 | |
412 for (i = 1; i < argc; i++) { | |
413 opt = argv[i]; | |
1075 | 414 if (*opt != '-') { |
415 if (found_filename) { | |
416 printf("invalid option:\n"); | |
417 goto err_out; | |
418 } | |
419 goto filename; | |
420 } | |
147 | 421 |
422 /* remove trailing '-' */ | |
423 opt++; | |
424 | |
425 tmp = read_option(opt, argv[i + 1]); | |
426 | |
427 switch (tmp) { | |
428 case ERR_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 } |
1075 | 434 filename: |
435 found_filename = 1; | |
436 *filename = argv[i]; | |
147 | 437 break; |
438 case ERR_MISSING_PARAM: | |
439 case ERR_OUT_OF_RANGE: | |
150 | 440 case ERR_FUNC_ERR: |
160 | 441 goto err_out; |
150 | 442 /* break; */ |
1075 | 443 default: |
444 i += tmp; | |
147 | 445 } |
160 | 446 } |
147 | 447 return found_filename; |
160 | 448 err_out: |
1075 | 449 printf("command line: %s\n", argv[i]); |
160 | 450 return -1; |
147 | 451 } |