Mercurial > mplayer.hg
annotate cfgparser.c @ 3041:6b31e121f36a
Changed the some functions prototypes for the network streaming.
author | bertrand |
---|---|
date | Tue, 20 Nov 2001 22:17:16 +0000 |
parents | bf7248edcc20 |
children | f61dcc63be5f |
rev | line source |
---|---|
147 | 1 /* |
2 * command line and config file parser | |
336 | 3 * by Szabolcs Berecz <szabi@inf.elte.hu> |
4 * (C) 2001 | |
2619 | 5 * |
6 * subconfig support by alex | |
147 | 7 */ |
8 | |
9 //#define DEBUG | |
10 | |
11 #include <stdlib.h> | |
12 #include <stdio.h> | |
13 #include <ctype.h> | |
14 #include <unistd.h> | |
15 #include <fcntl.h> | |
16 #include <string.h> | |
336 | 17 #include <errno.h> |
147 | 18 |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
19 #include "mp_msg.h" |
147 | 20 |
21 #define COMMAND_LINE 0 | |
22 #define CONFIG_FILE 1 | |
23 | |
177 | 24 #define MAX_RECURSION_DEPTH 8 |
166 | 25 |
147 | 26 #ifdef DEBUG |
27 #include <assert.h> | |
28 #endif | |
29 | |
30 #include "cfgparser.h" | |
31 | |
32 static struct config *config; | |
33 static int nr_options; /* number of options in 'conf' */ | |
34 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */ | |
166 | 35 static int recursion_depth = 0; |
147 | 36 |
37 static int init_conf(struct config *conf, int mode) | |
38 { | |
39 #ifdef DEBUG | |
40 assert(conf != NULL); | |
41 #endif | |
42 | |
43 /* calculate the number of options in 'conf' */ | |
44 for (nr_options = 0; conf[nr_options].name != NULL; nr_options++) | |
45 /* NOTHING */; | |
46 | |
47 config = conf; | |
48 #ifdef DEBUG | |
49 if (mode != COMMAND_LINE && mode != CONFIG_FILE) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
50 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "init_conf: wrong mode!\n"); |
147 | 51 return -1; |
52 } | |
53 #endif | |
54 parser_mode = mode; | |
55 return 1; | |
56 } | |
57 | |
2619 | 58 static int read_option(struct config *conf, int conf_optnr, char *opt, char *param) |
147 | 59 { |
60 int i; | |
195 | 61 long tmp_int; |
62 double tmp_float; | |
160 | 63 int ret = -1; |
195 | 64 char *endptr; |
147 | 65 |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
66 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "read_option: conf=%p optnr=%d opt='%s' param='%s'\n", |
2621 | 67 conf, conf_optnr, opt, param); |
2619 | 68 for (i = 0; i < conf_optnr; i++) { |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
69 int namelength; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
70 /* allow 'aa*' in config.name */ |
2619 | 71 namelength=strlen(conf[i].name); |
72 if ( (conf[i].name[namelength-1]=='*') && | |
73 !memcmp(opt, conf[i].name, namelength-1)) | |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
74 break; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
75 |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
76 |
2619 | 77 if (!strcasecmp(opt, conf[i].name)) |
147 | 78 break; |
79 } | |
2619 | 80 if (i == conf_optnr) { |
2380 | 81 if (parser_mode == CONFIG_FILE) |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
82 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid option:\n"); |
160 | 83 ret = ERR_NOT_AN_OPTION; |
84 goto out; | |
85 } | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
86 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "read_option: name='%s' p=%p type=%d\n", |
2621 | 87 conf[i].name, conf[i].p, conf[i].type); |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
88 |
2619 | 89 if (conf[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) { |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
90 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "this option can only be used on command line:\n"); |
160 | 91 ret = ERR_NOT_AN_OPTION; |
92 goto out; | |
93 } | |
2619 | 94 if (conf[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) { |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
95 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "this option can only be used in config file:\n"); |
160 | 96 ret = ERR_NOT_AN_OPTION; |
97 goto out; | |
98 } | |
150 | 99 |
2619 | 100 switch (conf[i].type) { |
147 | 101 case CONF_TYPE_FLAG: |
102 /* flags need a parameter in config file */ | |
103 if (parser_mode == CONFIG_FILE) { | |
104 if (!strcasecmp(param, "yes") || /* any other language? */ | |
105 !strcasecmp(param, "ja") || | |
161
9008302e2dc0
Added support for spanish (Yes, I'm a C programer also ;o)
telenieko
parents:
160
diff
changeset
|
106 !strcasecmp(param, "si") || |
147 | 107 !strcasecmp(param, "igen") || |
108 !strcasecmp(param, "y") || | |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
109 !strcasecmp(param, "j") || |
147 | 110 !strcasecmp(param, "i") || |
111 !strcmp(param, "1")) | |
2619 | 112 *((int *) conf[i].p) = conf[i].max; |
151 | 113 else if (!strcasecmp(param, "no") || |
147 | 114 !strcasecmp(param, "nein") || |
115 !strcasecmp(param, "nicht") || | |
116 !strcasecmp(param, "nem") || | |
117 !strcasecmp(param, "n") || | |
118 !strcmp(param, "0")) | |
2619 | 119 *((int *) conf[i].p) = conf[i].min; |
160 | 120 else { |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
121 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid parameter for flag:\n"); |
160 | 122 ret = ERR_OUT_OF_RANGE; |
123 goto out; | |
124 } | |
125 ret = 1; | |
147 | 126 } else { /* parser_mode == COMMAND_LINE */ |
2619 | 127 *((int *) conf[i].p) = conf[i].max; |
160 | 128 ret = 0; |
147 | 129 } |
130 break; | |
131 case CONF_TYPE_INT: | |
132 if (param == NULL) | |
160 | 133 goto err_missing_param; |
195 | 134 |
135 tmp_int = strtol(param, &endptr, 0); | |
136 if (*endptr) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
137 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be an integer:\n"); |
160 | 138 ret = ERR_OUT_OF_RANGE; |
139 goto out; | |
140 } | |
147 | 141 |
2619 | 142 if (conf[i].flags & CONF_MIN) |
143 if (tmp_int < conf[i].min) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
144 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %d:\n", (int) conf[i].min); |
160 | 145 ret = ERR_OUT_OF_RANGE; |
146 goto out; | |
147 } | |
147 | 148 |
2619 | 149 if (conf[i].flags & CONF_MAX) |
150 if (tmp_int > conf[i].max) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
151 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %d:\n", (int) conf[i].max); |
160 | 152 ret = ERR_OUT_OF_RANGE; |
153 goto out; | |
154 } | |
147 | 155 |
2619 | 156 *((int *) conf[i].p) = tmp_int; |
160 | 157 ret = 1; |
147 | 158 break; |
159 case CONF_TYPE_FLOAT: | |
160 if (param == NULL) | |
160 | 161 goto err_missing_param; |
195 | 162 |
163 tmp_float = strtod(param, &endptr); | |
2031
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
164 |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
165 if ((*endptr == ':') || (*endptr == '/')) |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
166 tmp_float /= strtod(endptr+1, &endptr); |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
167 |
195 | 168 if (*endptr) { |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
169 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be a floating point number" |
2031
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
170 " or a ratio (numerator[:/]denominator):\n"); |
624df8ea0e0e
New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents:
1629
diff
changeset
|
171 |
160 | 172 ret = ERR_MISSING_PARAM; |
173 goto out; | |
174 } | |
147 | 175 |
2619 | 176 if (conf[i].flags & CONF_MIN) |
177 if (tmp_float < conf[i].min) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
178 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %f:\n", conf[i].min); |
160 | 179 ret = ERR_OUT_OF_RANGE; |
180 goto out; | |
181 } | |
147 | 182 |
2619 | 183 if (conf[i].flags & CONF_MAX) |
184 if (tmp_float > conf[i].max) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
185 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %f:\n", conf[i].max); |
160 | 186 ret = ERR_OUT_OF_RANGE; |
187 goto out; | |
188 } | |
147 | 189 |
2619 | 190 *((float *) conf[i].p) = tmp_float; |
160 | 191 ret = 1; |
147 | 192 break; |
193 case CONF_TYPE_STRING: | |
194 if (param == NULL) | |
160 | 195 goto err_missing_param; |
147 | 196 |
2619 | 197 if (conf[i].flags & CONF_MIN) |
198 if (strlen(param) < conf[i].min) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
199 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %d chars:\n", |
2619 | 200 (int) conf[i].min); |
160 | 201 ret = ERR_OUT_OF_RANGE; |
202 goto out; | |
203 } | |
147 | 204 |
2619 | 205 if (conf[i].flags & CONF_MAX) |
206 if (strlen(param) > conf[i].max) { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
207 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %d chars:\n", |
2619 | 208 (int) conf[i].max); |
160 | 209 ret = ERR_OUT_OF_RANGE; |
210 goto out; | |
211 } | |
147 | 212 |
2619 | 213 *((char **) conf[i].p) = strdup(param); |
160 | 214 ret = 1; |
147 | 215 break; |
151 | 216 case CONF_TYPE_FUNC_PARAM: |
217 if (param == NULL) | |
160 | 218 goto err_missing_param; |
2619 | 219 if ((((cfg_func_param_t) conf[i].p)(config + i, param)) < 0) { |
160 | 220 ret = ERR_FUNC_ERR; |
221 goto out; | |
222 } | |
223 ret = 1; | |
151 | 224 break; |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
225 case CONF_TYPE_FUNC_FULL: |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
226 if (param!=NULL && param[0]=='-'){ |
2619 | 227 ret=((cfg_func_arg_param_t) conf[i].p)(config + i, opt, NULL); |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
228 if (ret>=0) ret=0; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
229 /* if we return >=0: param is processed again (if there is any) */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
230 }else{ |
2619 | 231 ret=((cfg_func_arg_param_t) conf[i].p)(config + i, opt, param); |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
232 /* if we return 0: need no param, precess it again */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
233 /* if we return 1: accepted param */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
234 } |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
235 break; |
150 | 236 case CONF_TYPE_FUNC: |
2619 | 237 if ((((cfg_func_t) conf[i].p)(config + i)) < 0) { |
160 | 238 ret = ERR_FUNC_ERR; |
239 goto out; | |
240 } | |
241 ret = 0; | |
150 | 242 break; |
2619 | 243 case CONF_TYPE_SUBCONFIG: |
244 { | |
245 char *subparam; | |
246 char *subopt; | |
247 int subconf_optnr; | |
248 struct config *subconf; | |
249 char *token; | |
250 | |
251 if (param == NULL) | |
252 goto err_missing_param; | |
253 | |
254 subparam = malloc(strlen(param)); | |
255 subopt = malloc(strlen(param)); | |
256 | |
257 subconf = conf[i].p; | |
258 for (subconf_optnr = 0; subconf[subconf_optnr].name != NULL; subconf_optnr++) | |
259 /* NOTHING */; | |
260 | |
261 token = strtok(param, (char *)&(":")); | |
262 while(token) | |
263 { | |
2621 | 264 int ret; |
265 int err; | |
2619 | 266 |
2621 | 267 ret = sscanf(token, "%[^=]=%s", subopt, subparam); |
268 switch(ret) | |
269 { | |
270 case 1: | |
271 case 2: | |
272 if ((err = read_option((struct config *)subconf, subconf_optnr, subopt, subparam)) < 0) | |
273 { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
274 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Subconfig parsing returned error: %d in token: %s\n", |
2621 | 275 err, token); |
276 return(err); | |
277 } | |
278 break; | |
279 default: | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
280 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid subconfig argument! ('%s')\n", token); |
2621 | 281 return(ERR_NOT_AN_OPTION); |
282 } | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
283 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "token: '%s', i=%d, subopt='%s, subparam='%s'\n", token, i, subopt, subparam); |
2619 | 284 token = strtok(NULL, (char *)&(":")); |
285 } | |
286 | |
287 free(subparam); | |
288 free(subopt); | |
289 ret = 1; | |
290 break; | |
291 } | |
152 | 292 case CONF_TYPE_PRINT: |
2619 | 293 printf("%s", (char *) conf[i].p); |
152 | 294 exit(1); |
147 | 295 default: |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
296 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown config type specified in conf-mplayer.h!\n"); |
147 | 297 break; |
298 } | |
160 | 299 out: |
300 return ret; | |
301 err_missing_param: | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
302 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "missing parameter:\n"); |
160 | 303 ret = ERR_MISSING_PARAM; |
304 goto out; | |
147 | 305 } |
306 | |
307 int parse_config_file(struct config *conf, char *conffile) | |
308 { | |
309 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num) | |
310 #define MAX_LINE_LEN 1000 | |
311 #define MAX_OPT_LEN 100 | |
312 #define MAX_PARAM_LEN 100 | |
313 FILE *fp; | |
314 char *line; | |
179 | 315 char opt[MAX_OPT_LEN + 1]; |
316 char param[MAX_PARAM_LEN + 1]; | |
167 | 317 char c; /* for the "" and '' check */ |
147 | 318 int tmp; |
319 int line_num = 0; | |
320 int line_pos; /* line pos */ | |
321 int opt_pos; /* opt pos */ | |
322 int param_pos; /* param pos */ | |
323 int ret = 1; | |
1090 | 324 int errors = 0; |
147 | 325 |
326 #ifdef DEBUG | |
327 assert(conffile != NULL); | |
328 #endif | |
1089 | 329 if (++recursion_depth > 1) |
330 printf("Reading config file: %s", conffile); | |
331 | |
332 if (recursion_depth > MAX_RECURSION_DEPTH) { | |
333 printf(": too deep 'include'. check your configfiles\n"); | |
334 ret = -1; | |
335 goto out; | |
336 } | |
166 | 337 |
338 if (init_conf(conf, CONFIG_FILE) == -1) { | |
339 ret = -1; | |
340 goto out; | |
341 } | |
147 | 342 |
179 | 343 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
344 printf("\ncan't get memory for 'line': %s", strerror(errno)); |
166 | 345 ret = -1; |
346 goto out; | |
147 | 347 } |
348 | |
349 if ((fp = fopen(conffile, "r")) == NULL) { | |
1089 | 350 if (recursion_depth > 1) |
351 printf(": %s\n", strerror(errno)); | |
147 | 352 free(line); |
166 | 353 ret = 0; |
354 goto out; | |
147 | 355 } |
1089 | 356 if (recursion_depth > 1) |
357 printf("\n"); | |
147 | 358 |
359 while (fgets(line, MAX_LINE_LEN, fp)) { | |
1090 | 360 if (errors >= 16) { |
361 printf("too many errors\n"); | |
362 goto out; | |
363 } | |
364 | |
147 | 365 line_num++; |
366 line_pos = 0; | |
367 | |
368 /* skip whitespaces */ | |
369 while (isspace(line[line_pos])) | |
370 ++line_pos; | |
371 | |
372 /* EOL / comment */ | |
373 if (line[line_pos] == '\0' || line[line_pos] == '#') | |
374 continue; | |
375 | |
480 | 376 /* read option. */ |
377 for (opt_pos = 0; isprint(line[line_pos]) && | |
378 line[line_pos] != ' ' && | |
379 line[line_pos] != '#' && | |
380 line[line_pos] != '='; /* NOTHING */) { | |
147 | 381 opt[opt_pos++] = line[line_pos++]; |
382 if (opt_pos >= MAX_OPT_LEN) { | |
383 PRINT_LINENUM; | |
384 printf("too long option\n"); | |
1090 | 385 errors++; |
147 | 386 ret = -1; |
1090 | 387 goto nextline; |
147 | 388 } |
389 } | |
390 if (opt_pos == 0) { | |
391 PRINT_LINENUM; | |
392 printf("parse error\n"); | |
393 ret = -1; | |
1090 | 394 errors++; |
147 | 395 continue; |
396 } | |
397 opt[opt_pos] = '\0'; | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
398 |
147 | 399 #ifdef DEBUG |
400 PRINT_LINENUM; | |
401 printf("option: %s\n", opt); | |
402 #endif | |
403 | |
404 /* skip whitespaces */ | |
405 while (isspace(line[line_pos])) | |
406 ++line_pos; | |
407 | |
408 /* check '=' */ | |
409 if (line[line_pos++] != '=') { | |
410 PRINT_LINENUM; | |
411 printf("option without parameter\n"); | |
412 ret = -1; | |
1090 | 413 errors++; |
147 | 414 continue; |
415 } | |
416 | |
417 /* whitespaces... */ | |
418 while (isspace(line[line_pos])) | |
419 ++line_pos; | |
420 | |
421 /* read the parameter */ | |
167 | 422 if (line[line_pos] == '"' || line[line_pos] == '\'') { |
423 c = line[line_pos]; | |
424 ++line_pos; | |
425 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
426 param[param_pos++] = line[line_pos++]; | |
427 if (param_pos >= MAX_PARAM_LEN) { | |
428 PRINT_LINENUM; | |
429 printf("too long parameter\n"); | |
430 ret = -1; | |
1090 | 431 errors++; |
432 goto nextline; | |
167 | 433 } |
434 } | |
435 line_pos++; /* skip the closing " or ' */ | |
436 } else { | |
437 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
438 && line[line_pos] != '#'; /* NOTHING */) { | |
439 param[param_pos++] = line[line_pos++]; | |
440 if (param_pos >= MAX_PARAM_LEN) { | |
441 PRINT_LINENUM; | |
442 printf("too long parameter\n"); | |
443 ret = -1; | |
1090 | 444 errors++; |
445 goto nextline; | |
167 | 446 } |
147 | 447 } |
448 } | |
449 param[param_pos] = '\0'; | |
450 | |
451 /* did we read a parameter? */ | |
452 if (param_pos == 0) { | |
453 PRINT_LINENUM; | |
454 printf("option without parameter\n"); | |
455 ret = -1; | |
1090 | 456 errors++; |
147 | 457 continue; |
458 } | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
459 |
147 | 460 #ifdef DEBUG |
461 PRINT_LINENUM; | |
462 printf("parameter: %s\n", param); | |
463 #endif | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
464 |
147 | 465 /* now, check if we have some more chars on the line */ |
466 /* whitespace... */ | |
467 while (isspace(line[line_pos])) | |
468 ++line_pos; | |
469 | |
470 /* EOL / comment */ | |
471 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
472 PRINT_LINENUM; | |
473 printf("extra characters on line: %s\n", line+line_pos); | |
474 ret = -1; | |
475 } | |
476 | |
2619 | 477 tmp = read_option(config, nr_options, opt, param); |
147 | 478 switch (tmp) { |
479 case ERR_NOT_AN_OPTION: | |
480 case ERR_MISSING_PARAM: | |
481 case ERR_OUT_OF_RANGE: | |
150 | 482 case ERR_FUNC_ERR: |
483 PRINT_LINENUM; | |
160 | 484 printf("%s\n", opt); |
150 | 485 ret = -1; |
1090 | 486 errors++; |
150 | 487 continue; |
488 /* break */ | |
147 | 489 } |
1093 | 490 nextline: |
1304 | 491 ; |
147 | 492 } |
493 | |
494 free(line); | |
495 fclose(fp); | |
166 | 496 out: |
497 --recursion_depth; | |
147 | 498 return ret; |
499 } | |
500 | |
1629 | 501 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames) |
147 | 502 { |
503 int i; | |
1629 | 504 char **f = NULL; |
505 int f_nr = 0; | |
147 | 506 int tmp; |
507 char *opt; | |
2615 | 508 int no_more_opts = 0; |
147 | 509 |
510 #ifdef DEBUG | |
511 assert(argv != NULL); | |
512 assert(envp != NULL); | |
513 assert(argc >= 1); | |
514 #endif | |
515 | |
516 if (init_conf(conf, COMMAND_LINE) == -1) | |
517 return -1; | |
518 | |
1089 | 519 /* in order to work recursion detection properly in parse_config_file */ |
520 ++recursion_depth; | |
521 | |
147 | 522 for (i = 1; i < argc; i++) { |
2615 | 523 next: |
147 | 524 opt = argv[i]; |
2650
bf7248edcc20
fixed commandline bug: handling '-' as option when '--' unused
alex
parents:
2624
diff
changeset
|
525 /* check for -- (no more options id.) except --help! */ |
2623 | 526 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h')) |
2615 | 527 { |
528 no_more_opts = 1; | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
529 if (i+1 >= argc) |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
530 { |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
531 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n"); |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
532 goto err_out; |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
533 } |
2615 | 534 i++; |
535 goto next; | |
536 } | |
537 | |
2650
bf7248edcc20
fixed commandline bug: handling '-' as option when '--' unused
alex
parents:
2624
diff
changeset
|
538 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */ |
2615 | 539 { |
540 /* remove trailing '-' */ | |
541 opt++; | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
542 |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
543 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt); |
147 | 544 |
2619 | 545 tmp = read_option(config, nr_options, opt, argv[i + 1]); |
1629 | 546 |
2615 | 547 switch (tmp) { |
548 case ERR_NOT_AN_OPTION: | |
549 case ERR_MISSING_PARAM: | |
550 case ERR_OUT_OF_RANGE: | |
551 case ERR_FUNC_ERR: | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
552 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Error %d while parsing option: '%s'!\n", |
2615 | 553 tmp, opt); |
160 | 554 goto err_out; |
2615 | 555 default: |
1075 | 556 i += tmp; |
2615 | 557 break; |
558 } | |
559 } | |
560 else /* filename */ | |
561 { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
562 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = filename: %s\n", opt); |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
563 |
2615 | 564 /* opt is not an option -> treat it as a filename */ |
565 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2)))) | |
566 goto err_out_mem; | |
567 | |
568 f[f_nr++] = argv[i]; | |
147 | 569 } |
1089 | 570 } |
2615 | 571 |
1629 | 572 if (f) |
573 f[f_nr] = NULL; | |
574 if (filenames) | |
575 *filenames = f; | |
1089 | 576 --recursion_depth; |
1629 | 577 return f_nr; //filenames_nr; |
578 err_out_mem: | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
579 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "can't allocate memory for filenames (%s)\n", strerror(errno)); |
160 | 580 err_out: |
1089 | 581 --recursion_depth; |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
582 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "command line: %s\n", argv[i]); |
160 | 583 return -1; |
147 | 584 } |