Mercurial > mplayer.hg
comparison cfgparser.c @ 160:5f0c50a9e347
cfgparse fix...
author | szabii |
---|---|
date | Mon, 19 Mar 2001 14:35:38 +0000 |
parents | 05e9c8083922 |
children | 9008302e2dc0 |
comparison
equal
deleted
inserted
replaced
159:8e9b6c00756f | 160:5f0c50a9e347 |
---|---|
51 } | 51 } |
52 | 52 |
53 static int read_option(char *opt, char *param) | 53 static int read_option(char *opt, char *param) |
54 { | 54 { |
55 int i; | 55 int i; |
56 int need_param = -1; | |
57 int tmp_int; | 56 int tmp_int; |
58 float tmp_float; | 57 float tmp_float; |
58 int ret = -1; | |
59 | 59 |
60 for (i = 0; i < nr_options; i++) { | 60 for (i = 0; i < nr_options; i++) { |
61 if (!strcasecmp(opt, config[i].name)) | 61 if (!strcasecmp(opt, config[i].name)) |
62 break; | 62 break; |
63 } | 63 } |
64 if (i == nr_options) | 64 if (i == nr_options) { |
65 return ERR_NOT_AN_OPTION; | 65 printf("invalid option:\n"); |
66 | 66 ret = ERR_NOT_AN_OPTION; |
67 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) | 67 goto out; |
68 return ERR_NOT_AN_OPTION; | 68 } |
69 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) | 69 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) { |
70 return ERR_NOT_AN_OPTION; | 70 printf("this option can only be used on command line:\n"); |
71 ret = ERR_NOT_AN_OPTION; | |
72 goto out; | |
73 } | |
74 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) { | |
75 printf("this option can only be used in config file:\n"); | |
76 ret = ERR_NOT_AN_OPTION; | |
77 goto out; | |
78 } | |
71 | 79 |
72 switch (config[i].type) { | 80 switch (config[i].type) { |
73 case CONF_TYPE_FLAG: | 81 case CONF_TYPE_FLAG: |
74 /* flags need a parameter in config file */ | 82 /* flags need a parameter in config file */ |
75 if (parser_mode == CONFIG_FILE) { | 83 if (parser_mode == CONFIG_FILE) { |
85 !strcasecmp(param, "nicht") || | 93 !strcasecmp(param, "nicht") || |
86 !strcasecmp(param, "nem") || | 94 !strcasecmp(param, "nem") || |
87 !strcasecmp(param, "n") || | 95 !strcasecmp(param, "n") || |
88 !strcmp(param, "0")) | 96 !strcmp(param, "0")) |
89 *((int *) config[i].p) = config[i].min; | 97 *((int *) config[i].p) = config[i].min; |
90 else | 98 else { |
91 return ERR_OUT_OF_RANGE; | 99 printf("invalid parameter for flag:\n"); |
92 need_param = 1; | 100 ret = ERR_OUT_OF_RANGE; |
101 goto out; | |
102 } | |
103 ret = 1; | |
93 } else { /* parser_mode == COMMAND_LINE */ | 104 } else { /* parser_mode == COMMAND_LINE */ |
94 *((int *) config[i].p) = config[i].max; | 105 *((int *) config[i].p) = config[i].max; |
95 need_param = 0; | 106 ret = 0; |
96 } | 107 } |
97 break; | 108 break; |
98 case CONF_TYPE_INT: | 109 case CONF_TYPE_INT: |
99 if (param == NULL) | 110 if (param == NULL) |
100 return ERR_MISSING_PARAM; | 111 goto err_missing_param; |
101 if (!isdigit(*param)) | 112 if (!isdigit(*param)) { |
102 return ERR_MISSING_PARAM; | 113 printf("parameter must be an integer:\n"); |
114 ret = ERR_OUT_OF_RANGE; | |
115 goto out; | |
116 } | |
103 | 117 |
104 tmp_int = atoi(param); | 118 tmp_int = atoi(param); |
105 | 119 |
106 if (config[i].flags & CONF_MIN) | 120 if (config[i].flags & CONF_MIN) |
107 if (tmp_int < config[i].min) | 121 if (tmp_int < config[i].min) { |
108 return ERR_OUT_OF_RANGE; | 122 printf("parameter must be >= %d:\n", (int) config[i].min); |
123 ret = ERR_OUT_OF_RANGE; | |
124 goto out; | |
125 } | |
109 | 126 |
110 if (config[i].flags & CONF_MAX) | 127 if (config[i].flags & CONF_MAX) |
111 if (tmp_int > config[i].max) | 128 if (tmp_int > config[i].max) { |
112 return ERR_OUT_OF_RANGE; | 129 printf("parameter must be <= %d:\n", (int) config[i].max); |
130 ret = ERR_OUT_OF_RANGE; | |
131 goto out; | |
132 } | |
113 | 133 |
114 *((int *) config[i].p) = tmp_int; | 134 *((int *) config[i].p) = tmp_int; |
115 need_param = 1; | 135 ret = 1; |
116 break; | 136 break; |
117 case CONF_TYPE_FLOAT: | 137 case CONF_TYPE_FLOAT: |
118 if (param == NULL) | 138 if (param == NULL) |
119 return ERR_MISSING_PARAM; | 139 goto err_missing_param; |
120 if (!isdigit(*param)) | 140 if (!isdigit(*param)) { |
121 return ERR_MISSING_PARAM; | 141 printf("parameter must be a floating point number:\n"); |
142 ret = ERR_MISSING_PARAM; | |
143 goto out; | |
144 } | |
122 | 145 |
123 tmp_float = atof(param); | 146 tmp_float = atof(param); |
124 | 147 |
125 if (config[i].flags & CONF_MIN) | 148 if (config[i].flags & CONF_MIN) |
126 if (tmp_float < config[i].min) | 149 if (tmp_float < config[i].min) { |
127 return ERR_OUT_OF_RANGE; | 150 printf("parameter must be >= %f:\n", config[i].min); |
151 ret = ERR_OUT_OF_RANGE; | |
152 goto out; | |
153 } | |
128 | 154 |
129 if (config[i].flags & CONF_MAX) | 155 if (config[i].flags & CONF_MAX) |
130 if (tmp_float > config[i].max) | 156 if (tmp_float > config[i].max) { |
131 return ERR_OUT_OF_RANGE; | 157 printf("parameter must be <= %f:\n", config[i].max); |
158 ret = ERR_OUT_OF_RANGE; | |
159 goto out; | |
160 } | |
132 | 161 |
133 *((float *) config[i].p) = tmp_float; | 162 *((float *) config[i].p) = tmp_float; |
134 need_param = 1; | 163 ret = 1; |
135 break; | 164 break; |
136 case CONF_TYPE_STRING: | 165 case CONF_TYPE_STRING: |
137 if (param == NULL) | 166 if (param == NULL) |
138 return ERR_MISSING_PARAM; | 167 goto err_missing_param; |
139 | 168 |
140 if (config[i].flags & CONF_MIN) | 169 if (config[i].flags & CONF_MIN) |
141 if (strlen(param) < config[i].min) | 170 if (strlen(param) < config[i].min) { |
142 return ERR_OUT_OF_RANGE; | 171 printf("parameter must be >= %d chars:\n", |
172 (int) config[i].min); | |
173 ret = ERR_OUT_OF_RANGE; | |
174 goto out; | |
175 } | |
143 | 176 |
144 if (config[i].flags & CONF_MAX) | 177 if (config[i].flags & CONF_MAX) |
145 if (strlen(param) > config[i].max) | 178 if (strlen(param) > config[i].max) { |
146 return ERR_OUT_OF_RANGE; | 179 printf("parameter must be <= %d chars:\n", |
180 (int) config[i].max); | |
181 ret = ERR_OUT_OF_RANGE; | |
182 goto out; | |
183 } | |
147 | 184 |
148 *((char **) config[i].p) = strdup(param); | 185 *((char **) config[i].p) = strdup(param); |
149 need_param = 1; | 186 ret = 1; |
150 break; | 187 break; |
151 case CONF_TYPE_FUNC_PARAM: | 188 case CONF_TYPE_FUNC_PARAM: |
152 if (param == NULL) | 189 if (param == NULL) |
153 return ERR_MISSING_PARAM; | 190 goto err_missing_param; |
154 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) | 191 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) { |
155 return ERR_FUNC_ERR; | 192 printf("parser function returned error:\n"); |
156 need_param = 1; | 193 ret = ERR_FUNC_ERR; |
194 goto out; | |
195 } | |
196 ret = 1; | |
157 break; | 197 break; |
158 case CONF_TYPE_FUNC: | 198 case CONF_TYPE_FUNC: |
159 if ((((cfg_func_t) config[i].p)(config + i)) < 0) | 199 if ((((cfg_func_t) config[i].p)(config + i)) < 0) { |
160 return ERR_FUNC_ERR; | 200 printf("parser function returned error:\n"); |
161 need_param = 0; | 201 ret = ERR_FUNC_ERR; |
202 goto out; | |
203 } | |
204 ret = 0; | |
162 break; | 205 break; |
163 case CONF_TYPE_PRINT: | 206 case CONF_TYPE_PRINT: |
164 printf("%s", (char *) config[i].p); | 207 printf("%s", (char *) config[i].p); |
165 exit(1); | 208 exit(1); |
166 default: | 209 default: |
167 printf("picsaba\n"); | 210 printf("picsaba\n"); |
168 break; | 211 break; |
169 } | 212 } |
170 return need_param; | 213 out: |
214 return ret; | |
215 err_missing_param: | |
216 printf("missing parameter:\n"); | |
217 ret = ERR_MISSING_PARAM; | |
218 goto out; | |
171 } | 219 } |
172 | 220 |
173 int parse_config_file(struct config *conf, char *conffile) | 221 int parse_config_file(struct config *conf, char *conffile) |
174 { | 222 { |
175 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num) | 223 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num) |
255 /* whitespaces... */ | 303 /* whitespaces... */ |
256 while (isspace(line[line_pos])) | 304 while (isspace(line[line_pos])) |
257 ++line_pos; | 305 ++line_pos; |
258 | 306 |
259 /* read the parameter */ | 307 /* read the parameter */ |
260 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]); | 308 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) |
261 /* NOTHING */) { | 309 && line[line_pos] != '#'; /* NOTHING */) { |
262 param[param_pos++] = line[line_pos++]; | 310 param[param_pos++] = line[line_pos++]; |
263 if (param_pos >= MAX_PARAM_LEN) { | 311 if (param_pos >= MAX_PARAM_LEN) { |
264 PRINT_LINENUM; | 312 PRINT_LINENUM; |
265 printf("too long parameter\n"); | 313 printf("too long parameter\n"); |
266 ret = -1; | 314 ret = -1; |
293 } | 341 } |
294 | 342 |
295 tmp = read_option(opt, param); | 343 tmp = read_option(opt, param); |
296 switch (tmp) { | 344 switch (tmp) { |
297 case ERR_NOT_AN_OPTION: | 345 case ERR_NOT_AN_OPTION: |
298 PRINT_LINENUM; | |
299 printf("invalid option: %s\n", opt); | |
300 ret = -1; | |
301 continue; | |
302 /* break; */ | |
303 case ERR_MISSING_PARAM: | 346 case ERR_MISSING_PARAM: |
304 PRINT_LINENUM; | |
305 printf("missing parameter: %s\n", opt); | |
306 ret = -1; | |
307 continue; | |
308 /* break; */ | |
309 case ERR_OUT_OF_RANGE: | 347 case ERR_OUT_OF_RANGE: |
310 PRINT_LINENUM; | |
311 printf("parameter of %s out of range\n", opt); | |
312 ret = -1; | |
313 continue; | |
314 /* break; */ | |
315 case ERR_FUNC_ERR: | 348 case ERR_FUNC_ERR: |
316 PRINT_LINENUM; | 349 PRINT_LINENUM; |
317 printf("parser function returned error: %s\n", opt); | 350 printf("%s\n", opt); |
318 ret = -1; | 351 ret = -1; |
319 continue; | 352 continue; |
320 /* break */ | 353 /* break */ |
321 } | 354 } |
322 } | 355 } |
347 if (*opt != '-') | 380 if (*opt != '-') |
348 goto not_an_option; | 381 goto not_an_option; |
349 | 382 |
350 /* remove trailing '-' */ | 383 /* remove trailing '-' */ |
351 opt++; | 384 opt++; |
352 #if 0 | |
353 /* check for --help, -h, and --version */ | |
354 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) { | |
355 printf("%s%s", banner_text, help_text); | |
356 continue; | |
357 } | |
358 if (!strcasecmp(opt, "-version")) { | |
359 printf("%s", banner_text); | |
360 continue; | |
361 } | |
362 #endif | |
363 | 385 |
364 tmp = read_option(opt, argv[i + 1]); | 386 tmp = read_option(opt, argv[i + 1]); |
365 | 387 |
366 switch (tmp) { | 388 switch (tmp) { |
367 case ERR_NOT_AN_OPTION: | 389 case ERR_NOT_AN_OPTION: |
368 not_an_option: | 390 not_an_option: |
369 /* opt is not an option -> treat it as a filename */ | 391 /* opt is not an option -> treat it as a filename */ |
370 if (found_filename) { | 392 if (found_filename) { |
371 /* we already have a filename */ | 393 /* we already have a filename */ |
372 printf("parse_command_line: invalid option: %s\n", argv[i]); | 394 goto err_out; |
373 return -1; | |
374 } else { | 395 } else { |
375 found_filename = 1; | 396 found_filename = 1; |
376 *filename = argv[i]; | 397 *filename = argv[i]; |
377 printf("parse_command_line: found filename: %s\n", *filename); | 398 printf("parse_command_line: found filename: %s\n", *filename); |
378 continue; /* next option */ | 399 continue; /* next option */ |
379 } | 400 } |
380 break; | 401 break; |
381 case ERR_MISSING_PARAM: | 402 case ERR_MISSING_PARAM: |
382 printf("parse_command_line: missing parameter: %s\n", argv[i]); | 403 case ERR_OUT_OF_RANGE: |
383 return -1; | 404 case ERR_FUNC_ERR: |
405 goto err_out; | |
384 /* break; */ | 406 /* break; */ |
385 case ERR_OUT_OF_RANGE: | |
386 printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]); | |
387 return -1; | |
388 /* break; */ | |
389 case ERR_FUNC_ERR: | |
390 printf("parse_command_line: parser function returned error: %s\n", argv[i]); | |
391 return -1; | |
392 /* break; */ | |
393 } | 407 } |
394 | 408 |
395 i += tmp; /* we already processed the params (if there was any) */ | 409 i += tmp; /* we already processed the params (if there was any) */ |
396 } | 410 } |
397 return found_filename; | 411 return found_filename; |
412 err_out: | |
413 printf("parse_command_line: %s\n", argv[i]); | |
414 return -1; | |
398 } | 415 } |