17 #define MAX_RECURSION_DEPTH 8
19 static int recursion_depth
= 0;
21 int m_config_parse_config_file(m_config_t
* config
, char *conffile
)
23 #define PRINT_LINENUM mp_msg(MSGT_CFGPARSER,MSGL_V,"%s(%d): ", conffile, line_num)
24 #define MAX_LINE_LEN 10000
25 #define MAX_OPT_LEN 1000
26 #define MAX_PARAM_LEN 1000
29 char opt
[MAX_OPT_LEN
+ 1];
30 char param
[MAX_PARAM_LEN
+ 1];
31 char c
; /* for the "" and '' check */
34 int line_pos
; /* line pos */
35 int opt_pos
; /* opt pos */
36 int param_pos
; /* param pos */
39 int prev_mode
= config
->mode
;
42 assert(config
!= NULL
);
43 // assert(conf_list != NULL);
45 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"Reading config file %s", conffile
);
47 if (recursion_depth
> MAX_RECURSION_DEPTH
) {
48 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,": too deep 'include'. check your configfiles\n");
53 config
->mode
= M_CONFIG_FILE
;
55 if ((line
= (char *) malloc(MAX_LINE_LEN
+ 1)) == NULL
) {
56 mp_msg(MSGT_CFGPARSER
,MSGL_FATAL
,"\ncan't get memory for 'line': %s", strerror(errno
));
61 if ((fp
= fopen(conffile
, "r")) == NULL
) {
62 mp_msg(MSGT_CFGPARSER
,MSGL_V
,": %s\n", strerror(errno
));
67 mp_msg(MSGT_CFGPARSER
,MSGL_INFO
,"\n");
69 while (fgets(line
, MAX_LINE_LEN
, fp
)) {
71 mp_msg(MSGT_CFGPARSER
,MSGL_FATAL
,"too many errors\n");
78 /* skip whitespaces */
79 while (isspace(line
[line_pos
]))
83 if (line
[line_pos
] == '\0' || line
[line_pos
] == '#')
87 for (opt_pos
= 0; isprint(line
[line_pos
]) &&
88 line
[line_pos
] != ' ' &&
89 line
[line_pos
] != '#' &&
90 line
[line_pos
] != '='; /* NOTHING */) {
91 opt
[opt_pos
++] = line
[line_pos
++];
92 if (opt_pos
>= MAX_OPT_LEN
) {
94 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"too long option at line %d\n",line_num
);
102 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"parse error at line %d\n",line_num
);
111 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"option: %s\n", opt
);
114 /* skip whitespaces */
115 while (isspace(line
[line_pos
]))
119 if (line
[line_pos
++] != '=') {
121 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Option %s needs a parameter at line %d\n",opt
,line_num
);
128 while (isspace(line
[line_pos
]))
131 /* read the parameter */
132 if (line
[line_pos
] == '"' || line
[line_pos
] == '\'') {
135 for (param_pos
= 0; line
[line_pos
] != c
; /* NOTHING */) {
136 param
[param_pos
++] = line
[line_pos
++];
137 if (param_pos
>= MAX_PARAM_LEN
) {
139 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Option %s has a too long parameter at line %d\n",opt
,line_num
);
145 line_pos
++; /* skip the closing " or ' */
147 for (param_pos
= 0; isprint(line
[line_pos
]) && !isspace(line
[line_pos
])
148 && line
[line_pos
] != '#'; /* NOTHING */) {
149 param
[param_pos
++] = line
[line_pos
++];
150 if (param_pos
>= MAX_PARAM_LEN
) {
152 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"too long parameter\n");
159 param
[param_pos
] = '\0';
161 /* did we read a parameter? */
162 if (param_pos
== 0) {
164 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Option %s needs a parameter at line %d\n",opt
,line_num
);
172 mp_msg(MSGT_CFGPARSER
,MSGL_V
,"parameter: %s\n", param
);
175 /* now, check if we have some more chars on the line */
177 while (isspace(line
[line_pos
]))
181 if (line
[line_pos
] != '\0' && line
[line_pos
] != '#') {
183 mp_msg(MSGT_CFGPARSER
,MSGL_WARN
,"extra characters on line %d: %s\n",line_num
, line
+line_pos
);
187 tmp
= m_config_set_option(config
, opt
, param
);
190 if(tmp
== M_OPT_UNKNOWN
) {
191 mp_msg(MSGT_CFGPARSER
,MSGL_WARN
,"Warning unknown option %s at line %d\n", opt
,line_num
);
194 mp_msg(MSGT_CFGPARSER
,MSGL_ERR
,"Error parsing option %s=%s at line %d\n",opt
,param
,line_num
);
207 config
->mode
= prev_mode
;