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