16 #define MAX_PROFILE_DEPTH 20
19 parse_profile(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
);
22 set_profile(m_option_t
*opt
, void* dst
, void* src
);
25 show_profile(m_option_t
*opt
, char* name
, char *param
);
28 m_config_add_option(m_config_t
*config
, m_option_t
*arg
, char* prefix
);
31 list_options(m_option_t
*opt
, char* name
, char *param
);
36 static int inited
= 0;
37 static m_option_type_t profile_opt_type
;
38 static m_option_t ref_opts
[] = {
39 { "profile", NULL
, &profile_opt_type
, CONF_NOSAVE
, 0, 0, NULL
},
40 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
41 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
42 { NULL
, NULL
, NULL
, 0, 0, 0, NULL
}
46 config
= (m_config_t
*)calloc(1,sizeof(m_config_t
));
47 config
->lvl
= 1; // 0 Is the defaults
50 profile_opt_type
= m_option_type_string_list
;
51 profile_opt_type
.parse
= parse_profile
;
52 profile_opt_type
.set
= set_profile
;
54 config
->self_opts
= malloc(sizeof(ref_opts
));
55 memcpy(config
->self_opts
,ref_opts
,sizeof(ref_opts
));
56 for(i
= 0 ; config
->self_opts
[i
].name
; i
++)
57 config
->self_opts
[i
].priv
= config
;
58 m_config_register_options(config
,config
->self_opts
);
64 m_config_free(m_config_t
* config
) {
65 m_config_option_t
*i
= config
->opts
, *ct
;
66 m_config_save_slot_t
*sl
,*st
;
71 assert(config
!= NULL
);
75 if (i
->flags
& M_CFG_OPT_ALIAS
)
80 m_option_free(i
->opt
,sl
->data
);
85 if(i
->name
!= i
->opt
->name
)
91 for(p
= config
->profiles
; p
; p
= pn
) {
94 if(p
->desc
) free(p
->desc
);
95 for(j
= 0 ; j
< p
->num_opts
; j
++) {
97 if(p
->opts
[2*j
+1]) free(p
->opts
[2*j
+1]);
102 free(config
->self_opts
);
107 m_config_push(m_config_t
* config
) {
108 m_config_option_t
*co
;
109 m_config_save_slot_t
*slot
;
112 assert(config
!= NULL
);
113 assert(config
->lvl
> 0);
118 for(co
= config
->opts
; co
; co
= co
->next
) {
119 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
121 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
123 if((co
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
&& M_CFG_OPT_SET
))
125 if(co
->flags
& M_CFG_OPT_ALIAS
)
128 // Update the current status
129 m_option_save(co
->opt
,co
->slots
->data
,co
->opt
->p
);
131 // Allocate a new slot
132 slot
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + co
->opt
->type
->size
);
133 slot
->lvl
= config
->lvl
;
134 slot
->prev
= co
->slots
;
136 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
137 // Reset our set flag
138 co
->flags
&= ~M_CFG_OPT_SET
;
141 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
145 m_config_pop(m_config_t
* config
) {
146 m_config_option_t
*co
;
147 m_config_save_slot_t
*slot
;
150 assert(config
!= NULL
);
151 assert(config
->lvl
> 1);
154 for(co
= config
->opts
; co
; co
= co
->next
) {
156 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
158 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
160 if(co
->flags
& M_CFG_OPT_ALIAS
)
162 if(co
->slots
->lvl
> config
->lvl
)
163 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,MSGTR_SaveSlotTooOld
,config
->lvl
,co
->slots
->lvl
);
165 while(co
->slots
->lvl
>= config
->lvl
) {
166 m_option_free(co
->opt
,co
->slots
->data
);
168 co
->slots
= slot
->prev
;
172 if(pop
) // We removed some ctx -> set the previous value
173 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
177 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
181 m_config_add_option(m_config_t
*config
, m_option_t
*arg
, char* prefix
) {
182 m_config_option_t
*co
;
183 m_config_save_slot_t
* sl
;
186 assert(config
!= NULL
);
187 assert(config
->lvl
> 0);
191 // Allocate a new entry for this option
192 co
= (m_config_option_t
*)calloc(1,sizeof(m_config_option_t
) + arg
->type
->size
);
195 // Fill in the full name
196 if(prefix
&& strlen(prefix
) > 0) {
197 int l
= strlen(prefix
) + 1 + strlen(arg
->name
) + 1;
198 co
->name
= (char*) malloc(l
);
199 sprintf(co
->name
,"%s:%s",prefix
,arg
->name
);
201 co
->name
= arg
->name
;
203 // Option with childs -> add them
204 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
205 m_option_t
*ol
= arg
->p
;
208 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
209 m_config_add_option(config
,&ol
[i
], co
->name
);
211 m_config_option_t
*i
;
212 // Check if there is alredy an option pointing to this address
214 for(i
= config
->opts
; i
; i
= i
->next
) {
215 if(i
->opt
->p
== arg
->p
) { // So we don't save the same vars more than 1 time
216 co
->slots
= i
->slots
;
217 co
->flags
|= M_CFG_OPT_ALIAS
;
222 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
223 // Allocate a slot for the defaults
224 sl
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
225 m_option_save(arg
,sl
->data
,(void**)arg
->p
);
226 // Hack to avoid too much trouble with dynamicly allocated data :
227 // We always use a dynamic version
228 if((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
) && arg
->p
&& (*(void**)arg
->p
)) {
229 *(void**)arg
->p
= NULL
;
230 m_option_set(arg
,arg
->p
,sl
->data
);
234 co
->slots
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
235 co
->slots
->prev
= sl
;
236 co
->slots
->lvl
= config
->lvl
;
237 m_option_copy(co
->opt
,co
->slots
->data
,sl
->data
);
240 co
->next
= config
->opts
;
245 m_config_register_options(m_config_t
*config
, m_option_t
*args
) {
249 assert(config
!= NULL
);
250 assert(config
->lvl
> 0);
251 assert(args
!= NULL
);
254 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
255 m_config_add_option(config
,&args
[i
],NULL
);
260 static m_config_option_t
*
261 m_config_get_co(m_config_t
*config
, char* arg
) {
262 m_config_option_t
*co
;
264 for(co
= config
->opts
; co
; co
= co
->next
) {
265 int l
= strlen(co
->name
) - 1;
266 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
267 (co
->name
[l
] == '*')) {
268 if(strncasecmp(co
->name
,arg
,l
) == 0)
270 } else if(strcasecmp(co
->name
,arg
) == 0)
277 m_config_parse_option(m_config_t
*config
, char* arg
, char* param
,int set
) {
278 m_config_option_t
*co
;
282 assert(config
!= NULL
);
283 assert(config
->lvl
> 0);
287 co
= m_config_get_co(config
,arg
);
289 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
290 return M_OPT_UNKNOWN
;
294 // This is the only mandatory function
295 assert(co
->opt
->type
->parse
);
298 // Check if this option isn't forbiden in the current mode
299 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
300 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCfgfileOption
,arg
);
301 return M_OPT_INVALID
;
303 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
304 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCmdlineOption
,arg
);
305 return M_OPT_INVALID
;
308 // Option with childs are a bit different to parse
309 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
312 // Parse the child options
313 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
316 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
317 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
319 // Build the full name
321 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
322 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
324 if(sr
== M_OPT_UNKNOWN
){
325 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidSuboption
,co
->name
,lst
[2*i
]);
328 if(sr
== M_OPT_MISSING_PARAM
){
329 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingSuboptionParameter
,lst
[2*i
],co
->name
);
340 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
347 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
348 co
->flags
|= M_CFG_OPT_SET
;
355 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
356 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
357 return m_config_parse_option(config
,arg
,param
,1);
361 m_config_check_option(m_config_t
*config
, char* arg
, char* param
) {
363 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
364 r
=m_config_parse_option(config
,arg
,param
,0);
365 if(r
==M_OPT_MISSING_PARAM
){
366 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingOptionParameter
,arg
);
367 return M_OPT_INVALID
;
374 m_config_get_option(m_config_t
*config
, char* arg
) {
375 m_config_option_t
*co
;
378 assert(config
!= NULL
);
379 assert(config
->lvl
> 0);
383 co
= m_config_get_co(config
,arg
);
391 m_config_get_option_ptr(m_config_t
*config
, char* arg
) {
395 assert(config
!= NULL
);
399 conf
= m_config_get_option(config
,arg
);
400 if(!conf
) return NULL
;
405 m_config_print_option_list(m_config_t
*config
) {
406 char min
[50],max
[50];
407 m_config_option_t
* co
;
410 if(!config
->opts
) return;
412 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_OptionListHeader
);
413 for(co
= config
->opts
; co
; co
= co
->next
) {
414 m_option_t
* opt
= co
->opt
;
415 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
416 if(opt
->flags
& M_OPT_MIN
)
417 sprintf(min
,"%-8.0f",opt
->min
);
420 if(opt
->flags
& M_OPT_MAX
)
421 sprintf(max
,"%-8.0f",opt
->max
);
424 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
429 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
430 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
431 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
434 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_TotalOptions
,count
);
438 m_config_get_profile(m_config_t
* config
, char* name
) {
440 for(p
= config
->profiles
; p
; p
= p
->next
)
441 if(!strcmp(p
->name
,name
)) return p
;
446 m_config_add_profile(m_config_t
* config
, char* name
) {
447 m_profile_t
* p
= m_config_get_profile(config
,name
);
449 p
= calloc(1,sizeof(m_profile_t
));
450 p
->name
= strdup(name
);
451 p
->next
= config
->profiles
;
452 config
->profiles
= p
;
457 m_profile_set_desc(m_profile_t
* p
, char* desc
) {
458 if(p
->desc
) free(p
->desc
);
459 p
->desc
= desc
? strdup(desc
) : NULL
;
463 m_config_set_profile_option(m_config_t
* config
, m_profile_t
* p
,
464 char* name
, char* val
) {
465 int i
= m_config_check_option(config
,name
,val
);
467 if(p
->opts
) p
->opts
= realloc(p
->opts
,2*(p
->num_opts
+2)*sizeof(char*));
468 else p
->opts
= malloc(2*(p
->num_opts
+2)*sizeof(char*));
469 p
->opts
[p
->num_opts
*2] = strdup(name
);
470 p
->opts
[p
->num_opts
*2+1] = val
? strdup(val
) : NULL
;
472 p
->opts
[p
->num_opts
*2] = p
->opts
[p
->num_opts
*2+1] = NULL
;
476 m_config_set_profile(m_config_t
* config
, m_profile_t
* p
) {
478 if(config
->profile_depth
> MAX_PROFILE_DEPTH
) {
479 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, MSGTR_TooDeepProfileInclusion
);
482 config
->profile_depth
++;
483 for(i
= 0 ; i
< p
->num_opts
; i
++)
484 m_config_set_option(config
,p
->opts
[2*i
],p
->opts
[2*i
+1]);
485 config
->profile_depth
--;
489 parse_profile(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
490 m_config_t
* config
= opt
->priv
;
493 if(param
&& !strcmp(param
,"help")) {
495 if(!config
->profiles
) {
496 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_NoProfileDefined
);
499 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_AvailableProfiles
);
500 for(p
= config
->profiles
; p
; p
= p
->next
)
501 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n",p
->name
,
502 p
->desc
? p
->desc
: "");
503 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
507 r
= m_option_type_string_list
.parse(opt
,name
,param
,&list
,src
);
509 if(!list
|| !list
[0]) return M_OPT_INVALID
;
510 for(i
= 0 ; list
[i
] ; i
++)
511 if(!m_config_get_profile(config
,list
[i
])) {
512 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, MSGTR_UnknownProfile
,
517 m_option_copy(opt
,dst
,&list
);
519 m_option_free(opt
,&list
);
524 set_profile(m_option_t
*opt
, void* dst
, void* src
) {
525 m_config_t
* config
= opt
->priv
;
529 if(!src
|| !*(char***)src
) return;
530 m_option_copy(opt
,&list
,src
);
531 for(i
= 0 ; list
[i
] ; i
++) {
532 p
= m_config_get_profile(config
,list
[i
]);
534 m_config_set_profile(config
,p
);
536 m_option_free(opt
,&list
);
540 show_profile(m_option_t
*opt
, char* name
, char *param
) {
541 m_config_t
* config
= opt
->priv
;
544 if(!param
) return M_OPT_MISSING_PARAM
;
545 if(!(p
= m_config_get_profile(config
,param
))) {
546 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, MSGTR_UnknownProfile
, param
);
549 if(!config
->profile_depth
)
550 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_Profile
, param
,
551 p
->desc
? p
->desc
: "");
552 config
->profile_depth
++;
553 for(i
= 0 ; i
< p
->num_opts
; i
++) {
554 char spc
[config
->profile_depth
+1];
555 for(j
= 0 ; j
< config
->profile_depth
; j
++)
557 spc
[config
->profile_depth
] = '\0';
559 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
560 p
->opts
[2*i
], p
->opts
[2*i
+1]);
563 if(config
->profile_depth
< MAX_PROFILE_DEPTH
&&
564 !strcmp(p
->opts
[2*i
],"profile")) {
565 char* e
,*list
= p
->opts
[2*i
+1];
566 while((e
= strchr(list
,','))) {
572 show_profile(opt
,name
,tmp
);
576 show_profile(opt
,name
,list
);
579 config
->profile_depth
--;
580 if(!config
->profile_depth
) mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
585 list_options(m_option_t
*opt
, char* name
, char *param
) {
586 m_config_t
* config
= opt
->priv
;
587 m_config_print_option_list(config
);