8164
|
1
|
|
2 #include "config.h"
|
|
3
|
|
4 #ifdef NEW_CONFIG
|
|
5
|
|
6 #include <stdlib.h>
|
|
7 #include <stdio.h>
|
|
8 #include <errno.h>
|
|
9 #include <string.h>
|
|
10 #ifdef MP_DEBUG
|
|
11 #include <assert.h>
|
|
12 #endif
|
|
13
|
|
14
|
|
15 #include "m_config.h"
|
|
16 #include "m_option.h"
|
|
17 #include "mp_msg.h"
|
|
18
|
|
19 m_config_t*
|
|
20 m_config_new(void) {
|
|
21 m_config_t* config;
|
|
22
|
|
23 config = (m_config_t*)calloc(1,sizeof(m_config_t));
|
|
24 config->lvl = 1; // 0 Is the defaults
|
|
25 return config;
|
|
26 }
|
|
27
|
|
28 void
|
|
29 m_config_free(m_config_t* config) {
|
|
30 m_config_option_t *i = config->opts, *ct;
|
|
31 m_config_save_slot_t *sl,*st;
|
|
32
|
|
33 #ifdef MP_DEBUG
|
|
34 assert(config != NULL);
|
|
35 #endif
|
|
36
|
|
37 while(i) {
|
|
38 sl = i->slots;
|
|
39 while(sl) {
|
|
40 m_option_free(i->opt,sl->data);
|
|
41 st = sl->prev;
|
|
42 free(sl);
|
|
43 sl = st;
|
|
44 }
|
|
45 if(i->name != i->opt->name)
|
|
46 free(i->name);
|
|
47 ct = i->next;
|
|
48 free(i);
|
|
49 ct = i;
|
|
50 }
|
|
51 free(config);
|
|
52 }
|
|
53
|
|
54 void
|
|
55 m_config_push(m_config_t* config) {
|
|
56 m_config_option_t *co;
|
|
57 m_config_save_slot_t *slot;
|
|
58
|
|
59 #ifdef MP_DEBUG
|
|
60 assert(config != NULL);
|
|
61 assert(config->lvl > 0);
|
|
62 #endif
|
|
63
|
|
64 config->lvl++;
|
|
65
|
|
66 for(co = config->opts ; co ; co = co->next ) {
|
|
67 if(co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
|
|
68 continue;
|
|
69 if(co->opt->flags & (M_OPT_GLOBAL|M_OPT_NOSAVE))
|
|
70 continue;
|
|
71 if((co->opt->flags & M_OPT_OLD) && !co->flags)
|
|
72 continue;
|
|
73
|
|
74 // Update the current status
|
|
75 m_option_save(co->opt,co->slots->data,co->opt->p);
|
|
76
|
|
77 // Allocate a new slot
|
|
78 slot = (m_config_save_slot_t*)calloc(1,sizeof(m_config_save_slot_t) + co->opt->type->size);
|
|
79 slot->lvl = config->lvl;
|
|
80 slot->prev = co->slots;
|
|
81 co->slots = slot;
|
|
82 m_option_copy(co->opt,co->slots->data,co->slots->prev->data);
|
|
83 // Reset our flags
|
|
84 co->flags=0;
|
|
85 }
|
|
86
|
|
87 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Config pushed level is now %d\n",config->lvl);
|
|
88 }
|
|
89
|
|
90 void
|
|
91 m_config_pop(m_config_t* config) {
|
|
92 m_config_option_t *co;
|
|
93 m_config_save_slot_t *slot;
|
|
94
|
|
95 #ifdef MP_DEBUG
|
|
96 assert(config != NULL);
|
|
97 assert(config->lvl > 1);
|
|
98 #endif
|
|
99
|
|
100 for(co = config->opts ; co ; co = co->next ) {
|
|
101 int pop = 0;
|
|
102 if(co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
|
|
103 continue;
|
|
104 if(co->opt->flags & (M_OPT_GLOBAL|M_OPT_NOSAVE))
|
|
105 continue;
|
|
106 if(co->slots->lvl > config->lvl)
|
|
107 mp_msg(MSGT_CFGPARSER, MSGL_WARN,"Too old save slot found from lvl %d : %d !!!\n",config->lvl,co->slots->lvl);
|
|
108
|
|
109 while(co->slots->lvl >= config->lvl) {
|
|
110 m_option_free(co->opt,co->slots->data);
|
|
111 slot = co->slots;
|
|
112 co->slots = slot->prev;
|
|
113 free(slot);
|
|
114 pop++;
|
|
115 }
|
|
116 if(pop) // We removed some ctx -> set the previous value
|
|
117 m_option_set(co->opt,co->opt->p,co->slots->data);
|
|
118 }
|
|
119
|
|
120 config->lvl--;
|
|
121 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Config poped level=%d\n",config->lvl);
|
|
122 }
|
|
123
|
|
124 static void
|
|
125 m_config_add_option(m_config_t *config, m_option_t *arg, char* prefix) {
|
|
126 m_config_option_t *co;
|
|
127 m_config_save_slot_t* sl;
|
|
128
|
|
129 #ifdef MP_DEBUG
|
|
130 assert(config != NULL);
|
|
131 assert(config->lvl > 0);
|
|
132 assert(arg != NULL);
|
|
133 #endif
|
|
134
|
|
135 // Allocate a new entry for this option
|
|
136 co = (m_config_option_t*)calloc(1,sizeof(m_config_option_t) + arg->type->size);
|
|
137 co->opt = arg;
|
|
138
|
|
139 // Fill in the full name
|
|
140 if(prefix && strlen(prefix) > 0) {
|
|
141 int l = strlen(prefix) + 1 + strlen(arg->name) + 1;
|
|
142 co->name = (char*) malloc(l);
|
|
143 sprintf(co->name,"%s:%s",prefix,arg->name);
|
|
144 } else
|
|
145 co->name = arg->name;
|
|
146
|
|
147 // Option with childs -> add them
|
|
148 if(arg->type->flags & M_OPT_TYPE_HAS_CHILD) {
|
|
149 m_option_t *ol = arg->p;
|
|
150 int i;
|
|
151 for(i = 0 ; ol[i].name != NULL ; i++)
|
|
152 m_config_add_option(config,&ol[i], co->name);
|
|
153 } else {
|
|
154 // Allocate a slot for the defaults
|
|
155 sl = (m_config_save_slot_t*)calloc(1,sizeof(m_config_save_slot_t) + arg->type->size);
|
|
156 m_option_save(arg,sl->data,(void**)arg->p);
|
|
157 // Hack to avoid too much trouble with dynamicly allocated data :
|
|
158 // We always use a dynamic version
|
|
159 if((arg->type->flags & M_OPT_TYPE_DYNAMIC) && arg->p && (*(void**)arg->p)) {
|
|
160 *(void**)arg->p = NULL;
|
|
161 m_option_set(arg,arg->p,sl->data);
|
|
162 }
|
|
163 sl->lvl = 0;
|
|
164 co->slots = (m_config_save_slot_t*)calloc(1,sizeof(m_config_save_slot_t) + arg->type->size);
|
|
165 co->slots->prev = sl;
|
|
166 co->slots->lvl = config->lvl;
|
|
167 m_option_copy(co->opt,co->slots->data,sl->data);
|
|
168 }
|
|
169 co->next = config->opts;
|
|
170 config->opts = co;
|
|
171 }
|
|
172
|
|
173 int
|
|
174 m_config_register_options(m_config_t *config, m_option_t *args) {
|
|
175 int i;
|
|
176
|
|
177 #ifdef MP_DEBUG
|
|
178 assert(config != NULL);
|
|
179 assert(config->lvl > 0);
|
|
180 assert(args != NULL);
|
|
181 #endif
|
|
182
|
|
183 for(i = 0 ; args[i].name != NULL ; i++)
|
|
184 m_config_add_option(config,&args[i],NULL);
|
|
185
|
|
186 return 1;
|
|
187 }
|
|
188
|
|
189 static m_config_option_t*
|
|
190 m_config_get_co(m_config_t *config, char* arg) {
|
|
191 m_config_option_t *co;
|
|
192
|
|
193 for(co = config->opts ; co ; co = co->next ) {
|
|
194 int l = strlen(co->name) - 1;
|
|
195 if((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD) &&
|
|
196 (co->name[l] == '*')) {
|
|
197 if(strncasecmp(co->name,arg,l) == 0)
|
|
198 return co;
|
|
199 } else if(strcasecmp(co->name,arg) == 0)
|
|
200 return co;
|
|
201 }
|
|
202 return NULL;
|
|
203 }
|
|
204
|
|
205 static int
|
|
206 m_config_parse_option(m_config_t *config, char* arg, char* param,int set) {
|
|
207 m_config_option_t *co;
|
|
208 int r = 0;
|
|
209
|
|
210 #ifdef MP_DEBUG
|
|
211 assert(config != NULL);
|
|
212 assert(config->lvl > 0);
|
|
213 assert(arg != NULL);
|
|
214 #endif
|
|
215
|
|
216 co = m_config_get_co(config,arg);
|
8892
|
217 if(!co){
|
|
218 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
|
8164
|
219 return M_OPT_UNKNOW;
|
8892
|
220 }
|
8164
|
221
|
|
222 #ifdef MP_DEBUG
|
|
223 // This is the only mandatory function
|
|
224 assert(co->opt->type->parse);
|
|
225 #endif
|
|
226
|
|
227 // Check if this option isn't forbiden in the current mode
|
|
228 if((config->mode == M_CONFIG_FILE) && (co->opt->flags & M_OPT_NOCFG)) {
|
8512
|
229 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"The %s option can't be used in a config file\n",arg);
|
8164
|
230 return M_OPT_INVALID;
|
|
231 }
|
|
232 if((config->mode == M_COMMAND_LINE) && (co->opt->flags & M_OPT_NOCMD)) {
|
8512
|
233 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"The %s option can't be used on the command line\n",arg);
|
8164
|
234 return M_OPT_INVALID;
|
|
235 }
|
|
236
|
|
237 // Option with childs are a bit different to parse
|
|
238 if(co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) {
|
|
239 char** lst = NULL;
|
|
240 int i,sr;
|
|
241 // Parse the child options
|
|
242 r = m_option_parse(co->opt,arg,param,&lst,config->mode);
|
|
243 // Set them now
|
|
244 for(i = 0 ; lst && lst[2*i] ; i++) {
|
|
245 int l = strlen(co->name) + 1 + strlen(lst[2*i]) + 1;
|
|
246 if(r >= 0) {
|
|
247 // Build the full name
|
|
248 char n[l];
|
|
249 sprintf(n,"%s:%s",co->name,lst[2*i]);
|
|
250 sr = m_config_parse_option(config,n,lst[2*i+1],set);
|
|
251 if(sr < 0) r = sr;
|
|
252 }
|
|
253 free(lst[2*i]);
|
|
254 free(lst[2*i+1]);
|
|
255 }
|
|
256 if(lst) free(lst);
|
|
257 } else
|
|
258 r = m_option_parse(co->opt,arg,param,set ? co->slots->data : NULL,config->mode);
|
|
259
|
|
260 // Parsing failed ?
|
|
261 if(r < 0)
|
|
262 return r;
|
|
263 // Set the option
|
|
264 if(set) {
|
|
265 m_option_set(co->opt,co->opt->p,co->slots->data);
|
|
266 co->flags = 1;
|
|
267 }
|
|
268
|
|
269 return r;
|
|
270 }
|
|
271
|
|
272 int
|
|
273 m_config_set_option(m_config_t *config, char* arg, char* param) {
|
|
274 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Setting %s=%s\n",arg,param);
|
|
275 return m_config_parse_option(config,arg,param,1);
|
|
276 }
|
|
277
|
|
278 int
|
|
279 m_config_check_option(m_config_t *config, char* arg, char* param) {
|
|
280 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Checking %s=%s\n",arg,param);
|
|
281 return m_config_parse_option(config,arg,param,0);
|
|
282 }
|
|
283
|
|
284
|
|
285 m_option_t*
|
|
286 m_config_get_option(m_config_t *config, char* arg) {
|
|
287 m_config_option_t *co;
|
|
288
|
|
289 #ifdef MP_DEBUG
|
|
290 assert(config != NULL);
|
|
291 assert(config->lvl > 0);
|
|
292 assert(arg != NULL);
|
|
293 #endif
|
|
294
|
|
295 co = m_config_get_co(config,arg);
|
|
296 if(co)
|
|
297 return co->opt;
|
|
298 else
|
|
299 return NULL;
|
|
300 }
|
|
301
|
|
302 void*
|
|
303 m_config_get_option_ptr(m_config_t *config, char* arg) {
|
|
304 m_option_t* conf;
|
|
305
|
|
306 #ifdef MP_DEBUG
|
|
307 assert(config != NULL);
|
|
308 assert(arg != NULL);
|
|
309 #endif
|
|
310
|
|
311 conf = m_config_get_option(config,arg);
|
|
312 if(!conf) return NULL;
|
|
313 return conf->p;
|
|
314 }
|
|
315
|
|
316 void
|
|
317 m_config_print_option_list(m_config_t *config) {
|
|
318 char min[50],max[50];
|
|
319 m_config_option_t* co;
|
|
320 int count = 0;
|
|
321
|
|
322 if(!config->opts) return;
|
|
323
|
|
324 printf("\n Name Type Min Max Global CL Cfg\n\n");
|
|
325 for(co = config->opts ; co ; co = co->next) {
|
|
326 m_option_t* opt = co->opt;
|
|
327 if(opt->type->flags & M_OPT_TYPE_HAS_CHILD) continue;
|
|
328 if(opt->flags & M_OPT_MIN)
|
|
329 sprintf(min,"%-8.0f",opt->min);
|
|
330 else
|
|
331 strcpy(min,"No");
|
|
332 if(opt->flags & M_OPT_MAX)
|
|
333 sprintf(max,"%-8.0f",opt->max);
|
|
334 else
|
|
335 strcpy(max,"No");
|
|
336 printf(" %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
|
|
337 co->name,
|
|
338 co->opt->type->name,
|
|
339 min,
|
|
340 max,
|
|
341 opt->flags & CONF_GLOBAL ? "Yes" : "No",
|
|
342 opt->flags & CONF_NOCMD ? "No" : "Yes",
|
|
343 opt->flags & CONF_NOCFG ? "No" : "Yes");
|
|
344 count++;
|
|
345 }
|
|
346 printf("\nTotal: %d options\n",count);
|
|
347 }
|
|
348
|
|
349 #endif // NEW_CONFIG
|