annotate cfgparser.c @ 3891:e87f69a44813

a52: CRC check for AC3 frames
author arpi
date Sun, 30 Dec 2001 04:44:20 +0000
parents b66a62f332d6
children 22fadd4022b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
1 /*
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
2 * command line and config file parser
336
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
3 * by Szabolcs Berecz <szabi@inf.elte.hu>
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
4 * (C) 2001
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
5 *
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
6 * subconfig support by alex
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
7 */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
8
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
9 //#define DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
10
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
11 #include <stdlib.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
12 #include <stdio.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
13 #include <ctype.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
14 #include <unistd.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
15 #include <fcntl.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
16 #include <string.h>
336
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
17 #include <errno.h>
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
20
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
21 #define COMMAND_LINE 0
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
22 #define CONFIG_FILE 1
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
23
177
szabii
parents: 167
diff changeset
24 #define MAX_RECURSION_DEPTH 8
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
25
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
26 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
27 #include <assert.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
28 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
29
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
30 #include "cfgparser.h"
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
31
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
32 static struct config *config;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
33 static int nr_options; /* number of options in 'conf' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
34 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
35 static int recursion_depth = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
36
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
37 static int init_conf(struct config *conf, int mode)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
38 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
39 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
40 assert(conf != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
41 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
42
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
43 /* calculate the number of options in 'conf' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
44 for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
45 /* NOTHING */;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
46
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
47 config = conf;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
48 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
51 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
52 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
53 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
54 parser_mode = mode;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
55 return 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
56 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
57
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
58 static int read_option(struct config *conf, int conf_optnr, char *opt, char *param)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
59 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
60 int i;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
61 long tmp_int;
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
62 double tmp_float;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
63 int ret = -1;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
64 char *endptr;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
67 conf, conf_optnr, opt, param);
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
71 namelength=strlen(conf[i].name);
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
72 if ( (conf[i].name[namelength-1]=='*') &&
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
77 if (!strcasecmp(opt, conf[i].name))
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
78 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
79 }
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
80 if (i == conf_optnr) {
2380
c1f71b8190d1 fix error reporting bug
szabi
parents: 2031
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
83 ret = ERR_NOT_AN_OPTION;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
84 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
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
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
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
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
91 ret = ERR_NOT_AN_OPTION;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
92 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
93 }
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
96 ret = ERR_NOT_AN_OPTION;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
97 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
98 }
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
99
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
100 switch (conf[i].type) {
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
101 case CONF_TYPE_FLAG:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
102 /* flags need a parameter in config file */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
103 if (parser_mode == CONFIG_FILE) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
104 if (!strcasecmp(param, "yes") || /* any other language? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
107 !strcasecmp(param, "igen") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
110 !strcasecmp(param, "i") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
111 !strcmp(param, "1"))
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
112 *((int *) conf[i].p) = conf[i].max;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
113 else if (!strcasecmp(param, "no") ||
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
114 !strcasecmp(param, "nein") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
115 !strcasecmp(param, "nicht") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
116 !strcasecmp(param, "nem") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
117 !strcasecmp(param, "n") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
118 !strcmp(param, "0"))
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
119 *((int *) conf[i].p) = conf[i].min;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
122 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
123 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
124 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
125 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
126 } else { /* parser_mode == COMMAND_LINE */
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
127 *((int *) conf[i].p) = conf[i].max;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
128 ret = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
129 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
130 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
131 case CONF_TYPE_INT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
132 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
133 goto err_missing_param;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
134
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
135 tmp_int = strtol(param, &endptr, 0);
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
138 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
139 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
140 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
141
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
142 if (conf[i].flags & CONF_MIN)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
145 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
146 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
147 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
148
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
149 if (conf[i].flags & CONF_MAX)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
152 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
153 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
154 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
155
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
156 *((int *) conf[i].p) = tmp_int;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
157 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
158 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
159 case CONF_TYPE_FLOAT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
160 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
161 goto err_missing_param;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
162
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
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
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
171 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
172 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
173 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
174
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
175 if (conf[i].flags & CONF_MIN)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
178 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
179 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
180 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
181
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
182 if (conf[i].flags & CONF_MAX)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
185 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
186 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
187 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
188
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
189 *((float *) conf[i].p) = tmp_float;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
190 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
191 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
192 case CONF_TYPE_STRING:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
193 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
194 goto err_missing_param;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
195
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
196 if (conf[i].flags & CONF_MIN)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
200 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
201 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
202 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
203
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
204 if (conf[i].flags & CONF_MAX)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
208 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
209 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
210 }
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
211 *((char **) conf[i].p) = strdup(param);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
212 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
213 break;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
214 case CONF_TYPE_FUNC_PARAM:
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
215 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
216 goto err_missing_param;
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
217 if ((((cfg_func_param_t) conf[i].p)(config + i, param)) < 0) {
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
218 ret = ERR_FUNC_ERR;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
219 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
220 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
221 ret = 1;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
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
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
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
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
234 case CONF_TYPE_FUNC:
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
235 if ((((cfg_func_t) conf[i].p)(config + i)) < 0) {
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
236 ret = ERR_FUNC_ERR;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
237 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
238 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
239 ret = 0;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
240 break;
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
241 case CONF_TYPE_SUBCONFIG:
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
242 {
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
243 char *subparam;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
244 char *subopt;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
245 int subconf_optnr;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
246 struct config *subconf;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
247 char *token;
3761
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
248 int sscanf_ret;
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
249
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
250 if (param == NULL)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
251 goto err_missing_param;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
252
3613
8a82dca566bc missing 1 byte in array size
pl
parents: 3559
diff changeset
253 subparam = malloc(strlen(param)+1);
8a82dca566bc missing 1 byte in array size
pl
parents: 3559
diff changeset
254 subopt = malloc(strlen(param)+1);
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
255
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
256 subconf = conf[i].p;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
257 for (subconf_optnr = 0; subconf[subconf_optnr].name != NULL; subconf_optnr++)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
258 /* NOTHING */;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
259
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
260 token = strtok(param, (char *)&(":"));
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
261 while(token)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
262 {
3761
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
263 /* clear out */
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
264 subopt[0] = subparam[0] = 0;
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
265
3761
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
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
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
268
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
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
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
271 {
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
272 case 1:
3761
b66a62f332d6 fixed subconfig parser
alex
parents: 3684
diff changeset
273 subparam[0] = 0;
2621
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
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
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
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
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
280 }
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
281 break;
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
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
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
286 }
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
287 token = strtok(NULL, (char *)&(":"));
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
288 }
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
289
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
290 free(subparam);
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
291 free(subopt);
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
292 ret = 1;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
293 break;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
294 }
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
295 case CONF_TYPE_PRINT:
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
296 printf("%s", (char *) conf[i].p);
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
297 exit(1);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
300 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
301 }
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
302 out:
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
303 return ret;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
306 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
307 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
310 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
311 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
312 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
313 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
314 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
315 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
316 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
317 char *line;
179
szabii
parents: 177
diff changeset
318 char opt[MAX_OPT_LEN + 1];
szabii
parents: 177
diff changeset
319 char param[MAX_PARAM_LEN + 1];
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
320 char c; /* for the "" and '' check */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
321 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
322 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
323 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
324 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
325 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
326 int ret = 1;
1090
szabii
parents: 1089
diff changeset
327 int errors = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
328
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
329 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
330 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
331 #endif
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
332 if (++recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
333 printf("Reading config file: %s", conffile);
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
334
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
335 if (recursion_depth > MAX_RECURSION_DEPTH) {
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
336 printf(": too deep 'include'. check your configfiles\n");
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
337 ret = -1;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
338 goto out;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
339 }
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
340
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
341 if (init_conf(conf, CONFIG_FILE) == -1) {
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
342 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
343 goto out;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
344 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
345
179
szabii
parents: 177
diff changeset
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
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
348 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
349 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
350 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
352 if ((fp = fopen(conffile, "r")) == NULL) {
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
353 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
354 printf(": %s\n", strerror(errno));
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
355 free(line);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
356 ret = 0;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
357 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
358 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
359 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
360 printf("\n");
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
361
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
362 while (fgets(line, MAX_LINE_LEN, fp)) {
1090
szabii
parents: 1089
diff changeset
363 if (errors >= 16) {
szabii
parents: 1089
diff changeset
364 printf("too many errors\n");
szabii
parents: 1089
diff changeset
365 goto out;
szabii
parents: 1089
diff changeset
366 }
szabii
parents: 1089
diff changeset
367
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
368 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
369 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
370
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
371 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
372 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
373 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
374
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
375 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
376 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
377 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
378
480
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
379 /* read option. */
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
380 for (opt_pos = 0; isprint(line[line_pos]) &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
381 line[line_pos] != ' ' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
382 line[line_pos] != '#' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
383 line[line_pos] != '='; /* NOTHING */) {
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
384 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
385 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
386 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
387 printf("too long option\n");
1090
szabii
parents: 1089
diff changeset
388 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
389 ret = -1;
1090
szabii
parents: 1089
diff changeset
390 goto nextline;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
391 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
392 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
393 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
394 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
395 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
396 ret = -1;
1090
szabii
parents: 1089
diff changeset
397 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
398 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
399 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
402 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
403 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
404 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
405 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
406
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
407 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
408 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
409 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
410
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
411 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
412 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
413 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
414 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
415 ret = -1;
1090
szabii
parents: 1089
diff changeset
416 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
417 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
418 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
419
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
420 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
421 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
422 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
423
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
424 /* read the parameter */
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
425 if (line[line_pos] == '"' || line[line_pos] == '\'') {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
426 c = line[line_pos];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
427 ++line_pos;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
428 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
429 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
430 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
431 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
432 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
433 ret = -1;
1090
szabii
parents: 1089
diff changeset
434 errors++;
szabii
parents: 1089
diff changeset
435 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
436 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
437 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
438 line_pos++; /* skip the closing " or ' */
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
439 } else {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
440 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos])
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
441 && line[line_pos] != '#'; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
442 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
443 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
444 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
445 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
446 ret = -1;
1090
szabii
parents: 1089
diff changeset
447 errors++;
szabii
parents: 1089
diff changeset
448 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
449 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
450 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
451 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
452 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
453
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
454 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
455 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
456 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
457 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
458 ret = -1;
1090
szabii
parents: 1089
diff changeset
459 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
460 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
461 }
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
462
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
463 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
464 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
465 printf("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
466 #endif
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
467
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
468 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
469 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
470 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
471 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
472
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
473 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
474 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
475 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
476 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
477 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
478 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
479
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
480 tmp = read_option(config, nr_options, opt, param);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
481 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
482 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
483 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
484 case ERR_OUT_OF_RANGE:
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
485 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
486 PRINT_LINENUM;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
487 printf("%s\n", opt);
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
488 ret = -1;
1090
szabii
parents: 1089
diff changeset
489 errors++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
490 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
491 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
492 }
1093
szabii
parents: 1090
diff changeset
493 nextline:
1304
ecb834719dc9 fix gcc-3.0 warning
jkeil
parents: 1093
diff changeset
494 ;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
495 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
496
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
497 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
498 fclose(fp);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
499 out:
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
500 --recursion_depth;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
501 return ret;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
502 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
503
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
504 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
505 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
506 int i;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
507 char **f = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
508 int f_nr = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
509 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
510 char *opt;
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
511 int no_more_opts = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
512
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
513 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
514 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
515 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
516 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
517 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
518
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
519 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
520 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
521
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
522 /* in order to work recursion detection properly in parse_config_file */
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
523 ++recursion_depth;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
524
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
525 for (i = 1; i < argc; i++) {
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
526 next:
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
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
0d8f8d313f9c fixed fault with --help
alex
parents: 2621
diff changeset
529 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h'))
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
530 {
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
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
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
537 i++;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
538 goto next;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
539 }
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
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
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
542 {
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
543 /* remove trailing '-' */
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
547
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
548 tmp = read_option(config, nr_options, opt, argv[i + 1]);
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
549
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
550 switch (tmp) {
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
551 case ERR_NOT_AN_OPTION:
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
552 case ERR_MISSING_PARAM:
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
553 case ERR_OUT_OF_RANGE:
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
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
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
556 tmp, opt);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
557 goto err_out;
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
558 default:
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
559 i += tmp;
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
560 break;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
561 }
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
562 }
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
563 else /* filename */
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
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
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
567 /* opt is not an option -> treat it as a filename */
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
568 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2))))
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
569 goto err_out_mem;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
570
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
571 f[f_nr++] = argv[i];
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
572 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
573 }
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
574
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
575 if (f)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
576 f[f_nr] = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
577 if (filenames)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
578 *filenames = f;
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
579 --recursion_depth;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
580 return f_nr; //filenames_nr;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
583 err_out:
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
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
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
586 return -1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
587 }