Mercurial > mplayer.hg
annotate parser-cfg.c @ 9859:16a7d1f4da38
Rewritten, to use new libmpeg2 0.3.1 API
based on work by Fabian Franz <FabianFranz@gmx.de>
author | arpi |
---|---|
date | Sun, 06 Apr 2003 16:43:46 +0000 |
parents | b0141de527df |
children | a660de2556c2 |
rev | line source |
---|---|
8164 | 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) | |
9813 | 27 #define MAX_LINE_LEN 10000 |
28 #define MAX_OPT_LEN 1000 | |
29 #define MAX_PARAM_LEN 1000 | |
8164 | 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; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
97 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long option at line %d\n",line_num); |
8164 | 98 errors++; |
99 ret = -1; | |
100 goto nextline; | |
101 } | |
102 } | |
103 if (opt_pos == 0) { | |
104 PRINT_LINENUM; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
105 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"parse error at line %d\n",line_num); |
8164 | 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; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
124 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s need a parameter at line %d\n",line_num); |
8164 | 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; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
142 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s have a too long parameter at line %d\n",opt,line_num); |
8164 | 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; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
167 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s need a parameter at line %d\n",opt,line_num); |
8164 | 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; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
186 mp_msg(MSGT_CFGPARSER,MSGL_WARN,"extra characters on line %d: %s\n",line_num, line+line_pos); |
8164 | 187 ret = -1; |
188 } | |
189 | |
190 tmp = m_config_set_option(config, opt, param); | |
191 if (tmp < 0) { | |
192 PRINT_LINENUM; | |
9578
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
193 if(tmp == M_OPT_UNKNOW) { |
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
194 mp_msg(MSGT_CFGPARSER,MSGL_WARN,"Warning unknown option %s at line %d\n", opt,line_num); |
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
195 continue; |
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
196 } |
0c5454233dcf
Better error messages (with line number now) and make unknow option
albeu
parents:
8164
diff
changeset
|
197 mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Error parsing option %s=%s at line %d\n",opt,param,line_num); |
8164 | 198 ret = -1; |
199 errors++; | |
200 continue; | |
201 /* break */ | |
202 } | |
203 nextline: | |
204 ; | |
205 } | |
206 | |
207 free(line); | |
208 fclose(fp); | |
209 out: | |
210 config->mode = prev_mode; | |
211 --recursion_depth; | |
212 return ret; | |
213 } | |
214 | |
215 #endif |