Mercurial > mplayer.hg
annotate cfgparser.c @ 3971:b30ffe58ff18
fix typos in latest checkin. although ( and { look alike, they're not.d'ah.
author | eyck |
---|---|
date | Fri, 04 Jan 2002 10:27:04 +0000 |
parents | b66a62f332d6 |
children | 22fadd4022b5 |
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) |
3684
00bd914b6fb1
subconfig fix (if sscanf()==1, then null out second (non-present) parameter) and some errormessage fixes
alex
parents:
3613
diff
changeset
|
82 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid option: %s\n", opt); |
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) { |
3684
00bd914b6fb1
subconfig fix (if sscanf()==1, then null out second (non-present) parameter) and some errormessage fixes
alex
parents:
3613
diff
changeset
|
90 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "this option can only be used on command line: %s\n", opt); |
160 | 91 ret = ERR_NOT_AN_OPTION; |
92 goto out; | |
93 } | |
2619 | 94 if (conf[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) { |
3684
00bd914b6fb1
subconfig fix (if sscanf()==1, then null out second (non-present) parameter) and some errormessage fixes
alex
parents:
3613
diff
changeset
|
95 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "this option can only be used in config file: %s\n", opt); |
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 { |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
121 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid parameter for flag: %s\n", param); |
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) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
137 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be an integer: %s\n", param); |
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) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
144 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %d: %s\n", (int) conf[i].min, param); |
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) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
151 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %d: %s\n", (int) conf[i].max, param); |
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" |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
170 " or a ratio (numerator[:/]denominator): %s\n", param); |
160 | 171 ret = ERR_MISSING_PARAM; |
172 goto out; | |
173 } | |
147 | 174 |
2619 | 175 if (conf[i].flags & CONF_MIN) |
176 if (tmp_float < conf[i].min) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
177 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %f: %s\n", conf[i].min, param); |
160 | 178 ret = ERR_OUT_OF_RANGE; |
179 goto out; | |
180 } | |
147 | 181 |
2619 | 182 if (conf[i].flags & CONF_MAX) |
183 if (tmp_float > conf[i].max) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
184 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %f: %s\n", conf[i].max, param); |
160 | 185 ret = ERR_OUT_OF_RANGE; |
186 goto out; | |
187 } | |
147 | 188 |
2619 | 189 *((float *) conf[i].p) = tmp_float; |
160 | 190 ret = 1; |
147 | 191 break; |
192 case CONF_TYPE_STRING: | |
193 if (param == NULL) | |
160 | 194 goto err_missing_param; |
147 | 195 |
2619 | 196 if (conf[i].flags & CONF_MIN) |
197 if (strlen(param) < conf[i].min) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
198 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %d chars: %s\n", |
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
199 (int) conf[i].min, param); |
160 | 200 ret = ERR_OUT_OF_RANGE; |
201 goto out; | |
202 } | |
147 | 203 |
2619 | 204 if (conf[i].flags & CONF_MAX) |
205 if (strlen(param) > conf[i].max) { | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
206 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %d chars: %s\n", |
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
207 (int) conf[i].max, param); |
160 | 208 ret = ERR_OUT_OF_RANGE; |
209 goto out; | |
210 } | |
2619 | 211 *((char **) conf[i].p) = strdup(param); |
160 | 212 ret = 1; |
147 | 213 break; |
151 | 214 case CONF_TYPE_FUNC_PARAM: |
215 if (param == NULL) | |
160 | 216 goto err_missing_param; |
2619 | 217 if ((((cfg_func_param_t) conf[i].p)(config + i, param)) < 0) { |
160 | 218 ret = ERR_FUNC_ERR; |
219 goto out; | |
220 } | |
221 ret = 1; | |
151 | 222 break; |
1536
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
223 case CONF_TYPE_FUNC_FULL: |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
224 if (param!=NULL && param[0]=='-'){ |
2619 | 225 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
|
226 if (ret>=0) ret=0; |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
227 /* 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
|
228 }else{ |
2619 | 229 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
|
230 /* 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
|
231 /* if we return 1: accepted param */ |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
232 } |
e89233dab4da
New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents:
1304
diff
changeset
|
233 break; |
150 | 234 case CONF_TYPE_FUNC: |
2619 | 235 if ((((cfg_func_t) conf[i].p)(config + i)) < 0) { |
160 | 236 ret = ERR_FUNC_ERR; |
237 goto out; | |
238 } | |
239 ret = 0; | |
150 | 240 break; |
2619 | 241 case CONF_TYPE_SUBCONFIG: |
242 { | |
243 char *subparam; | |
244 char *subopt; | |
245 int subconf_optnr; | |
246 struct config *subconf; | |
247 char *token; | |
3761 | 248 int sscanf_ret; |
2619 | 249 |
250 if (param == NULL) | |
251 goto err_missing_param; | |
252 | |
3613 | 253 subparam = malloc(strlen(param)+1); |
254 subopt = malloc(strlen(param)+1); | |
2619 | 255 |
256 subconf = conf[i].p; | |
257 for (subconf_optnr = 0; subconf[subconf_optnr].name != NULL; subconf_optnr++) | |
258 /* NOTHING */; | |
259 | |
260 token = strtok(param, (char *)&(":")); | |
261 while(token) | |
262 { | |
3761 | 263 /* clear out */ |
264 subopt[0] = subparam[0] = 0; | |
2619 | 265 |
3761 | 266 // sscanf_ret = sscanf(token, "%[^=]=%[^\n\0]", subopt, subparam); |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
267 sscanf_ret = sscanf(token, "%[^=]=%s", subopt, subparam); |
3761 | 268 |
269 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "token: '%s', i=%d, subopt='%s', subparam='%s' (ret: %d)\n", token, i, subopt, subparam, sscanf_ret); | |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
270 switch(sscanf_ret) |
2621 | 271 { |
272 case 1: | |
3761 | 273 subparam[0] = 0; |
2621 | 274 case 2: |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
275 if ((ret = read_option((struct config *)subconf, subconf_optnr, subopt, subparam)) < 0) |
2621 | 276 { |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
277 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Subconfig parsing returned error: %d in token: %s\n", |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
278 ret, token); |
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
279 goto out; |
2621 | 280 } |
281 break; | |
282 default: | |
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_ERR, "Invalid subconfig argument! ('%s')\n", token); |
3559
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
284 ret = ERR_NOT_AN_OPTION; |
f61dcc63be5f
exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents:
2650
diff
changeset
|
285 goto out; |
2621 | 286 } |
2619 | 287 token = strtok(NULL, (char *)&(":")); |
288 } | |
289 | |
290 free(subparam); | |
291 free(subopt); | |
292 ret = 1; | |
293 break; | |
294 } | |
152 | 295 case CONF_TYPE_PRINT: |
2619 | 296 printf("%s", (char *) conf[i].p); |
152 | 297 exit(1); |
147 | 298 default: |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
299 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown config type specified in conf-mplayer.h!\n"); |
147 | 300 break; |
301 } | |
160 | 302 out: |
303 return ret; | |
304 err_missing_param: | |
3684
00bd914b6fb1
subconfig fix (if sscanf()==1, then null out second (non-present) parameter) and some errormessage fixes
alex
parents:
3613
diff
changeset
|
305 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "missing parameter for option: %s\n", opt); |
160 | 306 ret = ERR_MISSING_PARAM; |
307 goto out; | |
147 | 308 } |
309 | |
310 int parse_config_file(struct config *conf, char *conffile) | |
311 { | |
312 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num) | |
313 #define MAX_LINE_LEN 1000 | |
314 #define MAX_OPT_LEN 100 | |
315 #define MAX_PARAM_LEN 100 | |
316 FILE *fp; | |
317 char *line; | |
179 | 318 char opt[MAX_OPT_LEN + 1]; |
319 char param[MAX_PARAM_LEN + 1]; | |
167 | 320 char c; /* for the "" and '' check */ |
147 | 321 int tmp; |
322 int line_num = 0; | |
323 int line_pos; /* line pos */ | |
324 int opt_pos; /* opt pos */ | |
325 int param_pos; /* param pos */ | |
326 int ret = 1; | |
1090 | 327 int errors = 0; |
147 | 328 |
329 #ifdef DEBUG | |
330 assert(conffile != NULL); | |
331 #endif | |
1089 | 332 if (++recursion_depth > 1) |
333 printf("Reading config file: %s", conffile); | |
334 | |
335 if (recursion_depth > MAX_RECURSION_DEPTH) { | |
336 printf(": too deep 'include'. check your configfiles\n"); | |
337 ret = -1; | |
338 goto out; | |
339 } | |
166 | 340 |
341 if (init_conf(conf, CONFIG_FILE) == -1) { | |
342 ret = -1; | |
343 goto out; | |
344 } | |
147 | 345 |
179 | 346 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
|
347 printf("\ncan't get memory for 'line': %s", strerror(errno)); |
166 | 348 ret = -1; |
349 goto out; | |
147 | 350 } |
351 | |
352 if ((fp = fopen(conffile, "r")) == NULL) { | |
1089 | 353 if (recursion_depth > 1) |
354 printf(": %s\n", strerror(errno)); | |
147 | 355 free(line); |
166 | 356 ret = 0; |
357 goto out; | |
147 | 358 } |
1089 | 359 if (recursion_depth > 1) |
360 printf("\n"); | |
147 | 361 |
362 while (fgets(line, MAX_LINE_LEN, fp)) { | |
1090 | 363 if (errors >= 16) { |
364 printf("too many errors\n"); | |
365 goto out; | |
366 } | |
367 | |
147 | 368 line_num++; |
369 line_pos = 0; | |
370 | |
371 /* skip whitespaces */ | |
372 while (isspace(line[line_pos])) | |
373 ++line_pos; | |
374 | |
375 /* EOL / comment */ | |
376 if (line[line_pos] == '\0' || line[line_pos] == '#') | |
377 continue; | |
378 | |
480 | 379 /* read option. */ |
380 for (opt_pos = 0; isprint(line[line_pos]) && | |
381 line[line_pos] != ' ' && | |
382 line[line_pos] != '#' && | |
383 line[line_pos] != '='; /* NOTHING */) { | |
147 | 384 opt[opt_pos++] = line[line_pos++]; |
385 if (opt_pos >= MAX_OPT_LEN) { | |
386 PRINT_LINENUM; | |
387 printf("too long option\n"); | |
1090 | 388 errors++; |
147 | 389 ret = -1; |
1090 | 390 goto nextline; |
147 | 391 } |
392 } | |
393 if (opt_pos == 0) { | |
394 PRINT_LINENUM; | |
395 printf("parse error\n"); | |
396 ret = -1; | |
1090 | 397 errors++; |
147 | 398 continue; |
399 } | |
400 opt[opt_pos] = '\0'; | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
401 |
147 | 402 #ifdef DEBUG |
403 PRINT_LINENUM; | |
404 printf("option: %s\n", opt); | |
405 #endif | |
406 | |
407 /* skip whitespaces */ | |
408 while (isspace(line[line_pos])) | |
409 ++line_pos; | |
410 | |
411 /* check '=' */ | |
412 if (line[line_pos++] != '=') { | |
413 PRINT_LINENUM; | |
414 printf("option without parameter\n"); | |
415 ret = -1; | |
1090 | 416 errors++; |
147 | 417 continue; |
418 } | |
419 | |
420 /* whitespaces... */ | |
421 while (isspace(line[line_pos])) | |
422 ++line_pos; | |
423 | |
424 /* read the parameter */ | |
167 | 425 if (line[line_pos] == '"' || line[line_pos] == '\'') { |
426 c = line[line_pos]; | |
427 ++line_pos; | |
428 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
429 param[param_pos++] = line[line_pos++]; | |
430 if (param_pos >= MAX_PARAM_LEN) { | |
431 PRINT_LINENUM; | |
432 printf("too long parameter\n"); | |
433 ret = -1; | |
1090 | 434 errors++; |
435 goto nextline; | |
167 | 436 } |
437 } | |
438 line_pos++; /* skip the closing " or ' */ | |
439 } else { | |
440 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
441 && line[line_pos] != '#'; /* NOTHING */) { | |
442 param[param_pos++] = line[line_pos++]; | |
443 if (param_pos >= MAX_PARAM_LEN) { | |
444 PRINT_LINENUM; | |
445 printf("too long parameter\n"); | |
446 ret = -1; | |
1090 | 447 errors++; |
448 goto nextline; | |
167 | 449 } |
147 | 450 } |
451 } | |
452 param[param_pos] = '\0'; | |
453 | |
454 /* did we read a parameter? */ | |
455 if (param_pos == 0) { | |
456 PRINT_LINENUM; | |
457 printf("option without parameter\n"); | |
458 ret = -1; | |
1090 | 459 errors++; |
147 | 460 continue; |
461 } | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
462 |
147 | 463 #ifdef DEBUG |
464 PRINT_LINENUM; | |
465 printf("parameter: %s\n", param); | |
466 #endif | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
467 |
147 | 468 /* now, check if we have some more chars on the line */ |
469 /* whitespace... */ | |
470 while (isspace(line[line_pos])) | |
471 ++line_pos; | |
472 | |
473 /* EOL / comment */ | |
474 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
475 PRINT_LINENUM; | |
476 printf("extra characters on line: %s\n", line+line_pos); | |
477 ret = -1; | |
478 } | |
479 | |
2619 | 480 tmp = read_option(config, nr_options, opt, param); |
147 | 481 switch (tmp) { |
482 case ERR_NOT_AN_OPTION: | |
483 case ERR_MISSING_PARAM: | |
484 case ERR_OUT_OF_RANGE: | |
150 | 485 case ERR_FUNC_ERR: |
486 PRINT_LINENUM; | |
160 | 487 printf("%s\n", opt); |
150 | 488 ret = -1; |
1090 | 489 errors++; |
150 | 490 continue; |
491 /* break */ | |
147 | 492 } |
1093 | 493 nextline: |
1304 | 494 ; |
147 | 495 } |
496 | |
497 free(line); | |
498 fclose(fp); | |
166 | 499 out: |
500 --recursion_depth; | |
147 | 501 return ret; |
502 } | |
503 | |
1629 | 504 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames) |
147 | 505 { |
506 int i; | |
1629 | 507 char **f = NULL; |
508 int f_nr = 0; | |
147 | 509 int tmp; |
510 char *opt; | |
2615 | 511 int no_more_opts = 0; |
147 | 512 |
513 #ifdef DEBUG | |
514 assert(argv != NULL); | |
515 assert(envp != NULL); | |
516 assert(argc >= 1); | |
517 #endif | |
518 | |
519 if (init_conf(conf, COMMAND_LINE) == -1) | |
520 return -1; | |
521 | |
1089 | 522 /* in order to work recursion detection properly in parse_config_file */ |
523 ++recursion_depth; | |
524 | |
147 | 525 for (i = 1; i < argc; i++) { |
2615 | 526 next: |
147 | 527 opt = argv[i]; |
2650
bf7248edcc20
fixed commandline bug: handling '-' as option when '--' unused
alex
parents:
2624
diff
changeset
|
528 /* check for -- (no more options id.) except --help! */ |
2623 | 529 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h')) |
2615 | 530 { |
531 no_more_opts = 1; | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
532 if (i+1 >= argc) |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
533 { |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
534 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
|
535 goto err_out; |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
536 } |
2615 | 537 i++; |
538 goto next; | |
539 } | |
540 | |
2650
bf7248edcc20
fixed commandline bug: handling '-' as option when '--' unused
alex
parents:
2624
diff
changeset
|
541 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */ |
2615 | 542 { |
543 /* remove trailing '-' */ | |
544 opt++; | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
545 |
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
546 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt); |
147 | 547 |
2619 | 548 tmp = read_option(config, nr_options, opt, argv[i + 1]); |
1629 | 549 |
2615 | 550 switch (tmp) { |
551 case ERR_NOT_AN_OPTION: | |
552 case ERR_MISSING_PARAM: | |
553 case ERR_OUT_OF_RANGE: | |
554 case ERR_FUNC_ERR: | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
555 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Error %d while parsing option: '%s'!\n", |
2615 | 556 tmp, opt); |
160 | 557 goto err_out; |
2615 | 558 default: |
1075 | 559 i += tmp; |
2615 | 560 break; |
561 } | |
562 } | |
563 else /* filename */ | |
564 { | |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
565 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
|
566 |
2615 | 567 /* opt is not an option -> treat it as a filename */ |
568 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2)))) | |
569 goto err_out_mem; | |
570 | |
571 f[f_nr++] = argv[i]; | |
147 | 572 } |
1089 | 573 } |
2615 | 574 |
1629 | 575 if (f) |
576 f[f_nr] = NULL; | |
577 if (filenames) | |
578 *filenames = f; | |
1089 | 579 --recursion_depth; |
1629 | 580 return f_nr; //filenames_nr; |
581 err_out_mem: | |
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, "can't allocate memory for filenames (%s)\n", strerror(errno)); |
160 | 583 err_out: |
1089 | 584 --recursion_depth; |
2624
64844fccf623
partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents:
2623
diff
changeset
|
585 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "command line: %s\n", argv[i]); |
160 | 586 return -1; |
147 | 587 } |