annotate cfgparser.c @ 3021:8c765d4a31b8

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