Mercurial > mplayer.hg
comparison parser-cfg.c @ 8164:487cfc28525d
New config system + cleanup of header inter dependency
author | albeu |
---|---|
date | Tue, 12 Nov 2002 01:56:42 +0000 |
parents | |
children | 0c5454233dcf |
comparison
equal
deleted
inserted
replaced
8163:51e5033ee687 | 8164:487cfc28525d |
---|---|
1 | |
2 #include "config.h" | |
3 | |
4 #ifdef NEW_CONFIG | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <errno.h> | |
10 #include <ctype.h> | |
11 | |
12 #ifdef MP_DEBUG | |
13 #include <assert.h> | |
14 #endif | |
15 | |
16 #include "mp_msg.h" | |
17 #include "m_option.h" | |
18 #include "m_config.h" | |
19 | |
20 #define MAX_RECURSION_DEPTH 8 | |
21 | |
22 static int recursion_depth = 0; | |
23 | |
24 int m_config_parse_config_file(m_config_t* config, char *conffile) | |
25 { | |
26 #define PRINT_LINENUM mp_msg(MSGT_CFGPARSER,MSGL_V,"%s(%d): ", conffile, line_num) | |
27 #define MAX_LINE_LEN 1000 | |
28 #define MAX_OPT_LEN 100 | |
29 #define MAX_PARAM_LEN 100 | |
30 FILE *fp; | |
31 char *line; | |
32 char opt[MAX_OPT_LEN + 1]; | |
33 char param[MAX_PARAM_LEN + 1]; | |
34 char c; /* for the "" and '' check */ | |
35 int tmp; | |
36 int line_num = 0; | |
37 int line_pos; /* line pos */ | |
38 int opt_pos; /* opt pos */ | |
39 int param_pos; /* param pos */ | |
40 int ret = 1; | |
41 int errors = 0; | |
42 int prev_mode = config->mode; | |
43 | |
44 #ifdef MP_DEBUG | |
45 assert(config != NULL); | |
46 // assert(conf_list != NULL); | |
47 #endif | |
48 mp_msg(MSGT_CFGPARSER,MSGL_INFO,"Reading config file %s", conffile); | |
49 | |
50 if (recursion_depth > MAX_RECURSION_DEPTH) { | |
51 mp_msg(MSGT_CFGPARSER,MSGL_ERR,": too deep 'include'. check your configfiles\n"); | |
52 ret = -1; | |
53 goto out; | |
54 } else | |
55 | |
56 config->mode = M_CONFIG_FILE; | |
57 | |
58 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { | |
59 mp_msg(MSGT_CFGPARSER,MSGL_FATAL,"\ncan't get memory for 'line': %s", strerror(errno)); | |
60 ret = -1; | |
61 goto out; | |
62 } | |
63 | |
64 if ((fp = fopen(conffile, "r")) == NULL) { | |
65 mp_msg(MSGT_CFGPARSER,MSGL_ERR,": %s\n", strerror(errno)); | |
66 free(line); | |
67 ret = 0; | |
68 goto out; | |
69 } | |
70 mp_msg(MSGT_CFGPARSER,MSGL_INFO,"\n"); | |
71 | |
72 while (fgets(line, MAX_LINE_LEN, fp)) { | |
73 if (errors >= 16) { | |
74 mp_msg(MSGT_CFGPARSER,MSGL_FATAL,"too many errors\n"); | |
75 goto out; | |
76 } | |
77 | |
78 line_num++; | |
79 line_pos = 0; | |
80 | |
81 /* skip whitespaces */ | |
82 while (isspace(line[line_pos])) | |
83 ++line_pos; | |
84 | |
85 /* EOL / comment */ | |
86 if (line[line_pos] == '\0' || line[line_pos] == '#') | |
87 continue; | |
88 | |
89 /* read option. */ | |
90 for (opt_pos = 0; isprint(line[line_pos]) && | |
91 line[line_pos] != ' ' && | |
92 line[line_pos] != '#' && | |
93 line[line_pos] != '='; /* NOTHING */) { | |
94 opt[opt_pos++] = line[line_pos++]; | |
95 if (opt_pos >= MAX_OPT_LEN) { | |
96 PRINT_LINENUM; | |
97 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long option\n"); | |
98 errors++; | |
99 ret = -1; | |
100 goto nextline; | |
101 } | |
102 } | |
103 if (opt_pos == 0) { | |
104 PRINT_LINENUM; | |
105 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"parse error\n"); | |
106 ret = -1; | |
107 errors++; | |
108 continue; | |
109 } | |
110 opt[opt_pos] = '\0'; | |
111 | |
112 #ifdef MP_DEBUG | |
113 PRINT_LINENUM; | |
114 mp_msg(MSGT_CFGPARSER,MSGL_V,"option: %s\n", opt); | |
115 #endif | |
116 | |
117 /* skip whitespaces */ | |
118 while (isspace(line[line_pos])) | |
119 ++line_pos; | |
120 | |
121 /* check '=' */ | |
122 if (line[line_pos++] != '=') { | |
123 PRINT_LINENUM; | |
124 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"option without parameter\n"); | |
125 ret = -1; | |
126 errors++; | |
127 continue; | |
128 } | |
129 | |
130 /* whitespaces... */ | |
131 while (isspace(line[line_pos])) | |
132 ++line_pos; | |
133 | |
134 /* read the parameter */ | |
135 if (line[line_pos] == '"' || line[line_pos] == '\'') { | |
136 c = line[line_pos]; | |
137 ++line_pos; | |
138 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { | |
139 param[param_pos++] = line[line_pos++]; | |
140 if (param_pos >= MAX_PARAM_LEN) { | |
141 PRINT_LINENUM; | |
142 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long parameter\n"); | |
143 ret = -1; | |
144 errors++; | |
145 goto nextline; | |
146 } | |
147 } | |
148 line_pos++; /* skip the closing " or ' */ | |
149 } else { | |
150 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) | |
151 && line[line_pos] != '#'; /* NOTHING */) { | |
152 param[param_pos++] = line[line_pos++]; | |
153 if (param_pos >= MAX_PARAM_LEN) { | |
154 PRINT_LINENUM; | |
155 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long parameter\n"); | |
156 ret = -1; | |
157 errors++; | |
158 goto nextline; | |
159 } | |
160 } | |
161 } | |
162 param[param_pos] = '\0'; | |
163 | |
164 /* did we read a parameter? */ | |
165 if (param_pos == 0) { | |
166 PRINT_LINENUM; | |
167 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"option without parameter\n"); | |
168 ret = -1; | |
169 errors++; | |
170 continue; | |
171 } | |
172 | |
173 #ifdef MP_DEBUG | |
174 PRINT_LINENUM; | |
175 mp_msg(MSGT_CFGPARSER,MSGL_V,"parameter: %s\n", param); | |
176 #endif | |
177 | |
178 /* now, check if we have some more chars on the line */ | |
179 /* whitespace... */ | |
180 while (isspace(line[line_pos])) | |
181 ++line_pos; | |
182 | |
183 /* EOL / comment */ | |
184 if (line[line_pos] != '\0' && line[line_pos] != '#') { | |
185 PRINT_LINENUM; | |
186 mp_msg(MSGT_CFGPARSER,MSGL_WARN,"extra characters on line: %s\n", line+line_pos); | |
187 ret = -1; | |
188 } | |
189 | |
190 tmp = m_config_set_option(config, opt, param); | |
191 if (tmp < 0) { | |
192 PRINT_LINENUM; | |
193 mp_msg(MSGT_CFGPARSER,MSGL_INFO,"%s\n", opt); | |
194 ret = -1; | |
195 errors++; | |
196 continue; | |
197 /* break */ | |
198 } | |
199 nextline: | |
200 ; | |
201 } | |
202 | |
203 free(line); | |
204 fclose(fp); | |
205 out: | |
206 config->mode = prev_mode; | |
207 --recursion_depth; | |
208 return ret; | |
209 } | |
210 | |
211 #endif |