annotate cfgparser.c @ 3684:00bd914b6fb1

subconfig fix (if sscanf()==1, then null out second (non-present) parameter) and some errormessage fixes
author alex
date Sun, 23 Dec 2001 21:18:06 +0000
parents 8a82dca566bc
children b66a62f332d6
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;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
248
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
249 if (param == NULL)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
250 goto err_missing_param;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
251
3613
8a82dca566bc missing 1 byte in array size
pl
parents: 3559
diff changeset
252 subparam = malloc(strlen(param)+1);
8a82dca566bc missing 1 byte in array size
pl
parents: 3559
diff changeset
253 subopt = malloc(strlen(param)+1);
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
254
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
255 subconf = conf[i].p;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
256 for (subconf_optnr = 0; subconf[subconf_optnr].name != NULL; subconf_optnr++)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
257 /* NOTHING */;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
258
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
259 token = strtok(param, (char *)&(":"));
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
260 while(token)
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
261 {
3559
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
262 int sscanf_ret;
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
263
3559
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
264 sscanf_ret = sscanf(token, "%[^=]=%s", subopt, subparam);
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
265 switch(sscanf_ret)
2621
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
266 {
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
267 case 1:
3684
00bd914b6fb1 subconfig fix (if sscanf()==1, then null out second (non-present) parameter) and some errormessage fixes
alex
parents: 3613
diff changeset
268 subparam = NULL;
2621
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
269 case 2:
3559
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
270 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
271 {
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
272 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
273 ret, token);
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
274 goto out;
2621
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
275 }
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
276 break;
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
277 default:
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
278 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
279 ret = ERR_NOT_AN_OPTION;
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
280 goto out;
2621
169d97f8aeaf fixed subconfig, exiting on error, supporting flags
alex
parents: 2619
diff changeset
281 }
3559
f61dcc63be5f exchanged return with goto out in subconfig parsing and fixed error messages
alex
parents: 2650
diff changeset
282 mp_dbg(MSGT_CFGPARSER, MSGL_DBG3, "token: '%s', i=%d, subopt='%s, subparam='%s'\n", token, i, subopt, subparam);
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
283 token = strtok(NULL, (char *)&(":"));
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
284 }
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
285
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
286 free(subparam);
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
287 free(subopt);
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
288 ret = 1;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
289 break;
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
290 }
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
291 case CONF_TYPE_PRINT:
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
292 printf("%s", (char *) conf[i].p);
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
293 exit(1);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
294 default:
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
295 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
296 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
297 }
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
298 out:
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
299 return ret;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
300 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
301 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "missing parameter for option: %s\n", opt);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
302 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
303 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
304 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
305
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
306 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
307 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
310 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
311 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
312 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
313 char *line;
179
szabii
parents: 177
diff changeset
314 char opt[MAX_OPT_LEN + 1];
szabii
parents: 177
diff changeset
315 char param[MAX_PARAM_LEN + 1];
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
316 char c; /* for the "" and '' check */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
317 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
318 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
319 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
320 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
321 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
322 int ret = 1;
1090
szabii
parents: 1089
diff changeset
323 int errors = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
324
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
325 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
326 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
327 #endif
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
328 if (++recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
329 printf("Reading config file: %s", conffile);
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
330
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
331 if (recursion_depth > MAX_RECURSION_DEPTH) {
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
332 printf(": too deep 'include'. check your configfiles\n");
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
333 ret = -1;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
334 goto out;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
335 }
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
336
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
337 if (init_conf(conf, CONFIG_FILE) == -1) {
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
338 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
339 goto out;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
340 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
341
179
szabii
parents: 177
diff changeset
342 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
343 printf("\ncan't get memory for 'line': %s", strerror(errno));
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
344 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
345 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
346 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
347
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
348 if ((fp = fopen(conffile, "r")) == NULL) {
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
349 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
350 printf(": %s\n", strerror(errno));
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351 free(line);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
352 ret = 0;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
353 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
354 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
355 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
356 printf("\n");
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
357
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
358 while (fgets(line, MAX_LINE_LEN, fp)) {
1090
szabii
parents: 1089
diff changeset
359 if (errors >= 16) {
szabii
parents: 1089
diff changeset
360 printf("too many errors\n");
szabii
parents: 1089
diff changeset
361 goto out;
szabii
parents: 1089
diff changeset
362 }
szabii
parents: 1089
diff changeset
363
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
364 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
365 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
366
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
367 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
368 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
369 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
370
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
371 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
372 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
373 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
374
480
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
375 /* read option. */
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
376 for (opt_pos = 0; isprint(line[line_pos]) &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
377 line[line_pos] != ' ' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
378 line[line_pos] != '#' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
379 line[line_pos] != '='; /* NOTHING */) {
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
380 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
381 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
382 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
383 printf("too long option\n");
1090
szabii
parents: 1089
diff changeset
384 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
385 ret = -1;
1090
szabii
parents: 1089
diff changeset
386 goto nextline;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
387 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
388 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
389 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
390 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
391 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
392 ret = -1;
1090
szabii
parents: 1089
diff changeset
393 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
394 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
395 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
396 opt[opt_pos] = '\0';
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
397
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
398 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
399 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
400 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
401 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
402
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
403 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
404 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
405 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
406
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
407 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
408 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
409 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
410 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
411 ret = -1;
1090
szabii
parents: 1089
diff changeset
412 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
413 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
414 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
415
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
416 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
417 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
418 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
419
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
420 /* read the parameter */
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
421 if (line[line_pos] == '"' || line[line_pos] == '\'') {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
422 c = line[line_pos];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
423 ++line_pos;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
424 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
425 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
426 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
427 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
428 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
429 ret = -1;
1090
szabii
parents: 1089
diff changeset
430 errors++;
szabii
parents: 1089
diff changeset
431 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
432 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
433 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
434 line_pos++; /* skip the closing " or ' */
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
435 } else {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
436 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
437 && line[line_pos] != '#'; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
438 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
439 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
440 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
441 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
442 ret = -1;
1090
szabii
parents: 1089
diff changeset
443 errors++;
szabii
parents: 1089
diff changeset
444 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
445 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
446 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
447 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
448 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
449
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
450 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
451 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
452 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
453 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
454 ret = -1;
1090
szabii
parents: 1089
diff changeset
455 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
456 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
457 }
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
458
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
459 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
460 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
461 printf("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
462 #endif
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
463
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
464 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
465 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
466 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
467 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
468
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
469 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
470 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
471 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
472 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
473 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
474 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
475
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
476 tmp = read_option(config, nr_options, opt, param);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
477 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
478 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
479 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
480 case ERR_OUT_OF_RANGE:
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
481 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
482 PRINT_LINENUM;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
483 printf("%s\n", opt);
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
484 ret = -1;
1090
szabii
parents: 1089
diff changeset
485 errors++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
486 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
487 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
488 }
1093
szabii
parents: 1090
diff changeset
489 nextline:
1304
ecb834719dc9 fix gcc-3.0 warning
jkeil
parents: 1093
diff changeset
490 ;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
491 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
492
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
493 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
494 fclose(fp);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
495 out:
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
496 --recursion_depth;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
497 return ret;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
498 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
499
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
500 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
501 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
502 int i;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
503 char **f = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
504 int f_nr = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
505 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
506 char *opt;
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
507 int no_more_opts = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
508
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
509 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
510 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
511 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
512 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
513 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
514
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
515 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
516 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
517
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
518 /* in order to work recursion detection properly in parse_config_file */
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
519 ++recursion_depth;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
520
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
521 for (i = 1; i < argc; i++) {
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
522 next:
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
523 opt = argv[i];
2650
bf7248edcc20 fixed commandline bug: handling '-' as option when '--' unused
alex
parents: 2624
diff changeset
524 /* check for -- (no more options id.) except --help! */
2623
0d8f8d313f9c fixed fault with --help
alex
parents: 2621
diff changeset
525 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h'))
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
526 {
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
527 no_more_opts = 1;
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
528 if (i+1 >= argc)
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
529 {
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
530 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
531 goto err_out;
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
532 }
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
533 i++;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
534 goto next;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
535 }
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
536
2650
bf7248edcc20 fixed commandline bug: handling '-' as option when '--' unused
alex
parents: 2624
diff changeset
537 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
538 {
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
539 /* remove trailing '-' */
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
540 opt++;
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
541
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
542 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
543
2619
b66f42e9ec2d subconfig support
alex
parents: 2615
diff changeset
544 tmp = read_option(config, nr_options, opt, argv[i + 1]);
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
545
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
546 switch (tmp) {
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
547 case ERR_NOT_AN_OPTION:
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
548 case ERR_MISSING_PARAM:
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
549 case ERR_OUT_OF_RANGE:
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
550 case ERR_FUNC_ERR:
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
551 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
552 tmp, opt);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
553 goto err_out;
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
554 default:
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
555 i += tmp;
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
556 break;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
557 }
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
558 }
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
559 else /* filename */
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
560 {
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
561 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
562
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
563 /* opt is not an option -> treat it as a filename */
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
564 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2))))
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
565 goto err_out_mem;
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
566
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
567 f[f_nr++] = argv[i];
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
568 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
569 }
2615
fc7985beff39 fixed arpi's cfgparser bug
alex
parents: 2380
diff changeset
570
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
571 if (f)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
572 f[f_nr] = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
573 if (filenames)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
574 *filenames = f;
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
575 --recursion_depth;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
576 return f_nr; //filenames_nr;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
577 err_out_mem:
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
578 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
579 err_out:
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
580 --recursion_depth;
2624
64844fccf623 partly upgraded to mp_msg and fixed minor bug in parse_command_line
alex
parents: 2623
diff changeset
581 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "command line: %s\n", argv[i]);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
582 return -1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
583 }