2 /// \defgroup ConfigParsers Config parsers
4 /// The \ref ConfigParsers make use of the \ref Config to setup the config variables,
5 /// the command line parsers also build the playlist.
22 #include "parser-cfg.h"
27 /// Maximal include depth.
28 #define MAX_RECURSION_DEPTH 8
30 /// Current include depth.
31 static int recursion_depth
= 0;
33 /// Setup the \ref Config from a config file.
34 /** \param config The config object.
35 * \param conffile Path to the config file.
36 * \return 1 on sucess, -1 on error.
38 int m_config_parse_config_file(m_config_t
* config
, const char *conffile
)
40 #define PRINT_LINENUM mp_msg(MSGT_CFGPARSER,MSGL_V,"%s(%d): ", conffile, line_num)
41 #define MAX_LINE_LEN 10000
42 #define MAX_OPT_LEN 1000
43 #define MAX_PARAM_LEN 1500
46 char opt
[MAX_OPT_LEN
+ 1];
47 char param
[MAX_PARAM_LEN
+ 1];
48 char c
; /* for the "" and '' check */
51 int line_pos
; /* line pos */
52 int opt_pos
; /* opt pos */
53 int param_pos
; /* param pos */
56 int prev_mode
= config
->mode
;
57 m_profile_t
* profile
= NULL
;
60 assert(config
!= NULL
);
61 // assert(conf_list != NULL);
63 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"Reading config file %s", conffile
);
65 if (recursion_depth
> MAX_RECURSION_DEPTH
) {
66 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,": too deep 'include'. check your configfiles\n");
71 config
->mode
= M_CONFIG_FILE
;
73 if ((line
= malloc(MAX_LINE_LEN
+ 1)) == NULL
) {
74 mp_msg(MSGT_CFGPARSER
,MSGL_FATAL
,"\ncan't get memory for 'line': %s", strerror(errno
));
79 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"\n");
81 if ((fp
= fopen(conffile
, "r")) == NULL
) {
82 mp_msg(MSGT_CFGPARSER
,MSGL_V
,": %s\n", strerror(errno
));
88 while (fgets(line
, MAX_LINE_LEN
, fp
)) {
90 mp_msg(MSGT_CFGPARSER
,MSGL_FATAL
,"too many errors\n");
97 /* skip whitespaces */
98 while (isspace(line
[line_pos
]))
102 if (line
[line_pos
] == '\0' || line
[line_pos
] == '#')
106 for (opt_pos
= 0; isprint(line
[line_pos
]) &&
107 line
[line_pos
] != ' ' &&
108 line
[line_pos
] != '#' &&
109 line
[line_pos
] != '='; /* NOTHING */) {
110 opt
[opt_pos
++] = line
[line_pos
++];
111 if (opt_pos
>= MAX_OPT_LEN
) {
113 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"too long option at line %d\n",line_num
);
121 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"parse error at line %d\n",line_num
);
128 /* Profile declaration */
129 if(opt_pos
> 2 && opt
[0] == '[' && opt
[opt_pos
-1] == ']') {
130 opt
[opt_pos
-1] = '\0';
131 if(strcmp(opt
+1,"default"))
132 profile
= m_config_add_profile(config
,opt
+1);
140 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"option: %s\n", opt
);
143 /* skip whitespaces */
144 while (isspace(line
[line_pos
]))
148 if (line
[line_pos
++] != '=') {
150 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Option %s needs a parameter at line %d\n",opt
,line_num
);
157 while (isspace(line
[line_pos
]))
160 /* read the parameter */
161 if (line
[line_pos
] == '"' || line
[line_pos
] == '\'') {
164 for (param_pos
= 0; line
[line_pos
] != c
; /* NOTHING */) {
165 param
[param_pos
++] = line
[line_pos
++];
166 if (param_pos
>= MAX_PARAM_LEN
) {
168 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Option %s has a too long parameter at line %d\n",opt
,line_num
);
174 line_pos
++; /* skip the closing " or ' */
176 for (param_pos
= 0; isprint(line
[line_pos
]) && !isspace(line
[line_pos
])
177 && line
[line_pos
] != '#'; /* NOTHING */) {
178 param
[param_pos
++] = line
[line_pos
++];
179 if (param_pos
>= MAX_PARAM_LEN
) {
181 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"too long parameter\n");
188 param
[param_pos
] = '\0';
190 /* did we read a parameter? */
191 if (param_pos
== 0) {
193 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Option %s needs a parameter at line %d\n",opt
,line_num
);
201 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"parameter: %s\n", param
);
204 /* now, check if we have some more chars on the line */
206 while (isspace(line
[line_pos
]))
210 if (line
[line_pos
] != '\0' && line
[line_pos
] != '#') {
212 mp_msg(MSGT_CFGPARSER
,MSGL_WARN
,"extra characters on line %d: %s\n",line_num
, line
+line_pos
);
217 if(!strcmp(opt
,"profile-desc"))
218 m_profile_set_desc(profile
,param
), tmp
= 1;
220 tmp
= m_config_set_profile_option(config
,profile
,
223 tmp
= m_config_set_option(config
, opt
, param
);
226 if(tmp
== M_OPT_UNKNOWN
) {
227 mp_msg(MSGT_CFGPARSER
,MSGL_WARN
,"Warning unknown option %s at line %d\n", opt
,line_num
);
230 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Error parsing option %s=%s at line %d\n",opt
,param
,line_num
);
243 config
->mode
= prev_mode
;
248 extern int mp_msg_levels
[];
250 /// Parse the command line option that must be handled at startup.
251 int m_config_preparse_command_line(m_config_t
*config
, int argc
, char **argv
)
253 int msg_lvl
, i
, r
, ret
= 0;
256 // Hack to shutup the parser error messages.
257 msg_lvl
= mp_msg_levels
[MSGT_CFGPARSER
];
258 mp_msg_levels
[MSGT_CFGPARSER
] = -11;
260 config
->mode
= M_COMMAND_LINE_PRE_PARSE
;
262 for(i
= 1 ; i
< argc
; i
++) {
263 const m_option_t
* opt
;
266 if(arg
[0] != '-' || arg
[1] == 0) continue;
268 // No more options after --
269 if(arg
[0] == '-' && arg
[1] == 0) break;
271 opt
= m_config_get_option(config
,arg
);
272 // Ignore invalid option
274 // Set, non-pre-parse options will be ignored
275 r
= m_config_set_option(config
,arg
,
276 i
+1 < argc
? argv
[i
+1] : NULL
);
281 mp_msg_levels
[MSGT_CFGPARSER
] = msg_lvl
;