20 #define MAX_PROFILE_DEPTH 20
23 parse_profile(const m_option_t
*opt
, const char *name
, char *param
, void *dst
, int src
);
26 set_profile(const m_option_t
*opt
, void* dst
, void* src
);
29 show_profile(m_option_t
*opt
, char* name
, char *param
);
32 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
);
35 list_options(m_option_t
*opt
, char* name
, char *param
);
40 static int initialized
= 0;
41 static m_option_type_t profile_opt_type
;
42 static m_option_t ref_opts
[] = {
43 { "profile", NULL
, &profile_opt_type
, CONF_NOSAVE
, 0, 0, NULL
},
44 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
45 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
46 { NULL
, NULL
, NULL
, 0, 0, 0, NULL
}
50 config
= calloc(1,sizeof(m_config_t
));
51 config
->lvl
= 1; // 0 Is the defaults
54 profile_opt_type
= m_option_type_string_list
;
55 profile_opt_type
.parse
= parse_profile
;
56 profile_opt_type
.set
= set_profile
;
58 config
->self_opts
= malloc(sizeof(ref_opts
));
59 memcpy(config
->self_opts
,ref_opts
,sizeof(ref_opts
));
60 for(i
= 0 ; config
->self_opts
[i
].name
; i
++)
61 config
->self_opts
[i
].priv
= config
;
62 m_config_register_options(config
,config
->self_opts
);
68 m_config_free(m_config_t
* config
) {
69 m_config_option_t
*i
= config
->opts
, *ct
;
70 m_config_save_slot_t
*sl
,*st
;
75 assert(config
!= NULL
);
79 if (i
->flags
& M_CFG_OPT_ALIAS
)
84 m_option_free(i
->opt
,sl
->data
);
89 if(i
->name
!= i
->opt
->name
)
95 for(p
= config
->profiles
; p
; p
= pn
) {
98 if(p
->desc
) free(p
->desc
);
99 for(j
= 0 ; j
< p
->num_opts
; j
++) {
101 if(p
->opts
[2*j
+1]) free(p
->opts
[2*j
+1]);
106 free(config
->self_opts
);
111 m_config_push(m_config_t
* config
) {
112 m_config_option_t
*co
;
113 m_config_save_slot_t
*slot
;
116 assert(config
!= NULL
);
117 assert(config
->lvl
> 0);
122 for(co
= config
->opts
; co
; co
= co
->next
) {
123 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
125 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
127 if((co
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
& M_CFG_OPT_SET
))
129 if(co
->flags
& M_CFG_OPT_ALIAS
)
132 // Update the current status
133 m_option_save(co
->opt
,co
->slots
->data
,co
->opt
->p
);
135 // Allocate a new slot
136 slot
= calloc(1,sizeof(m_config_save_slot_t
) + co
->opt
->type
->size
);
137 slot
->lvl
= config
->lvl
;
138 slot
->prev
= co
->slots
;
140 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
141 // Reset our set flag
142 co
->flags
&= ~M_CFG_OPT_SET
;
145 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
149 m_config_pop(m_config_t
* config
) {
150 m_config_option_t
*co
;
151 m_config_save_slot_t
*slot
;
154 assert(config
!= NULL
);
155 assert(config
->lvl
> 1);
158 for(co
= config
->opts
; co
; co
= co
->next
) {
160 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
162 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
164 if(co
->flags
& M_CFG_OPT_ALIAS
)
166 if(co
->slots
->lvl
> config
->lvl
)
167 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,MSGTR_SaveSlotTooOld
,config
->lvl
,co
->slots
->lvl
);
169 while(co
->slots
->lvl
>= config
->lvl
) {
170 m_option_free(co
->opt
,co
->slots
->data
);
172 co
->slots
= slot
->prev
;
176 if(pop
) // We removed some ctx -> set the previous value
177 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
181 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
185 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
) {
186 m_config_option_t
*co
;
187 m_config_save_slot_t
* sl
;
190 assert(config
!= NULL
);
191 assert(config
->lvl
> 0);
195 // Allocate a new entry for this option
196 co
= calloc(1,sizeof(m_config_option_t
) + arg
->type
->size
);
199 // Fill in the full name
200 if(prefix
&& strlen(prefix
) > 0) {
201 int l
= strlen(prefix
) + 1 + strlen(arg
->name
) + 1;
202 co
->name
= malloc(l
);
203 sprintf(co
->name
,"%s:%s",prefix
,arg
->name
);
205 co
->name
= arg
->name
;
207 // Option with children -> add them
208 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
209 const m_option_t
*ol
= arg
->p
;
212 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
213 m_config_add_option(config
,&ol
[i
], co
->name
);
215 m_config_option_t
*i
;
216 // Check if there is already an option pointing to this address
218 for(i
= config
->opts
; i
; i
= i
->next
) {
219 if(i
->opt
->p
== arg
->p
) { // So we don't save the same vars more than 1 time
220 co
->slots
= i
->slots
;
221 co
->flags
|= M_CFG_OPT_ALIAS
;
226 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
227 // Allocate a slot for the defaults
228 sl
= calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
229 m_option_save(arg
,sl
->data
,(void**)arg
->p
);
230 // Hack to avoid too much trouble with dynamically allocated data :
231 // We always use a dynamic version
232 if((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
) && arg
->p
&& (*(void**)arg
->p
)) {
233 *(void**)arg
->p
= NULL
;
234 m_option_set(arg
,arg
->p
,sl
->data
);
238 co
->slots
= calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
239 co
->slots
->prev
= sl
;
240 co
->slots
->lvl
= config
->lvl
;
241 m_option_copy(co
->opt
,co
->slots
->data
,sl
->data
);
244 co
->next
= config
->opts
;
249 m_config_register_options(m_config_t
*config
, const m_option_t
*args
) {
253 assert(config
!= NULL
);
254 assert(config
->lvl
> 0);
255 assert(args
!= NULL
);
258 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
259 m_config_add_option(config
,&args
[i
],NULL
);
264 static m_config_option_t
*
265 m_config_get_co(m_config_t
*config
, char* arg
) {
266 m_config_option_t
*co
;
268 for(co
= config
->opts
; co
; co
= co
->next
) {
269 int l
= strlen(co
->name
) - 1;
270 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
271 (co
->name
[l
] == '*')) {
272 if(strncasecmp(co
->name
,arg
,l
) == 0)
274 } else if(strcasecmp(co
->name
,arg
) == 0)
281 m_config_parse_option(m_config_t
*config
, char* arg
, char* param
,int set
) {
282 m_config_option_t
*co
;
286 assert(config
!= NULL
);
287 assert(config
->lvl
> 0);
291 co
= m_config_get_co(config
,arg
);
293 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
294 return M_OPT_UNKNOWN
;
298 // This is the only mandatory function
299 assert(co
->opt
->type
->parse
);
302 // Check if this option isn't forbidden in the current mode
303 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
304 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCfgfileOption
,arg
);
305 return M_OPT_INVALID
;
307 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
308 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCmdlineOption
,arg
);
309 return M_OPT_INVALID
;
311 // During command line preparse set only pre-parse options
312 // Otherwise only set pre-parse option if they were not already set.
313 if(((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
314 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
315 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
316 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
319 // Option with children are a bit different to parse
320 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
323 // Parse the child options
324 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
327 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
328 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
330 // Build the full name
332 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
333 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
335 if(sr
== M_OPT_UNKNOWN
){
336 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidSuboption
,co
->name
,lst
[2*i
]);
339 if(sr
== M_OPT_MISSING_PARAM
){
340 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingSuboptionParameter
,lst
[2*i
],co
->name
);
351 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
358 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
359 co
->flags
|= M_CFG_OPT_SET
;
366 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
367 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
368 return m_config_parse_option(config
,arg
,param
,1);
372 m_config_check_option(m_config_t
*config
, char* arg
, char* param
) {
374 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
375 r
=m_config_parse_option(config
,arg
,param
,0);
376 if(r
==M_OPT_MISSING_PARAM
){
377 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingOptionParameter
,arg
);
378 return M_OPT_INVALID
;
385 m_config_get_option(m_config_t
*config
, char* arg
) {
386 m_config_option_t
*co
;
389 assert(config
!= NULL
);
390 assert(config
->lvl
> 0);
394 co
= m_config_get_co(config
,arg
);
402 m_config_get_option_ptr(m_config_t
*config
, char* arg
) {
403 const m_option_t
* conf
;
406 assert(config
!= NULL
);
410 conf
= m_config_get_option(config
,arg
);
411 if(!conf
) return NULL
;
416 m_config_print_option_list(m_config_t
*config
) {
417 char min
[50],max
[50];
418 m_config_option_t
* co
;
421 if(!config
->opts
) return;
423 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_OptionListHeader
);
424 for(co
= config
->opts
; co
; co
= co
->next
) {
425 const m_option_t
* opt
= co
->opt
;
426 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
427 if(opt
->flags
& M_OPT_MIN
)
428 sprintf(min
,"%-8.0f",opt
->min
);
431 if(opt
->flags
& M_OPT_MAX
)
432 sprintf(max
,"%-8.0f",opt
->max
);
435 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
440 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
441 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
442 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
445 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_TotalOptions
,count
);
449 m_config_get_profile(m_config_t
* config
, char* name
) {
451 for(p
= config
->profiles
; p
; p
= p
->next
)
452 if(!strcmp(p
->name
,name
)) return p
;
457 m_config_add_profile(m_config_t
* config
, char* name
) {
458 m_profile_t
* p
= m_config_get_profile(config
,name
);
460 p
= calloc(1,sizeof(m_profile_t
));
461 p
->name
= strdup(name
);
462 p
->next
= config
->profiles
;
463 config
->profiles
= p
;
468 m_profile_set_desc(m_profile_t
* p
, char* desc
) {
469 if(p
->desc
) free(p
->desc
);
470 p
->desc
= desc
? strdup(desc
) : NULL
;
474 m_config_set_profile_option(m_config_t
* config
, m_profile_t
* p
,
475 char* name
, char* val
) {
476 int i
= m_config_check_option(config
,name
,val
);
478 if(p
->opts
) p
->opts
= realloc(p
->opts
,2*(p
->num_opts
+2)*sizeof(char*));
479 else p
->opts
= malloc(2*(p
->num_opts
+2)*sizeof(char*));
480 p
->opts
[p
->num_opts
*2] = strdup(name
);
481 p
->opts
[p
->num_opts
*2+1] = val
? strdup(val
) : NULL
;
483 p
->opts
[p
->num_opts
*2] = p
->opts
[p
->num_opts
*2+1] = NULL
;
488 m_config_set_profile(m_config_t
* config
, m_profile_t
* p
) {
490 if(config
->profile_depth
> MAX_PROFILE_DEPTH
) {
491 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, MSGTR_ProfileInclusionTooDeep
);
494 config
->profile_depth
++;
495 for(i
= 0 ; i
< p
->num_opts
; i
++)
496 m_config_set_option(config
,p
->opts
[2*i
],p
->opts
[2*i
+1]);
497 config
->profile_depth
--;
501 parse_profile(const m_option_t
*opt
, const char *name
, char *param
, void *dst
, int src
)
503 m_config_t
* config
= opt
->priv
;
506 if(param
&& !strcmp(param
,"help")) {
508 if(!config
->profiles
) {
509 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_NoProfileDefined
);
512 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_AvailableProfiles
);
513 for(p
= config
->profiles
; p
; p
= p
->next
)
514 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n",p
->name
,
515 p
->desc
? p
->desc
: "");
516 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
520 r
= m_option_type_string_list
.parse(opt
,name
,param
,&list
,src
);
522 if(!list
|| !list
[0]) return M_OPT_INVALID
;
523 for(i
= 0 ; list
[i
] ; i
++)
524 if(!m_config_get_profile(config
,list
[i
])) {
525 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, MSGTR_UnknownProfile
,
530 m_option_copy(opt
,dst
,&list
);
532 m_option_free(opt
,&list
);
537 set_profile(const m_option_t
*opt
, void *dst
, void *src
) {
538 m_config_t
* config
= opt
->priv
;
542 if(!src
|| !*(char***)src
) return;
543 m_option_copy(opt
,&list
,src
);
544 for(i
= 0 ; list
[i
] ; i
++) {
545 p
= m_config_get_profile(config
,list
[i
]);
547 m_config_set_profile(config
,p
);
549 m_option_free(opt
,&list
);
553 show_profile(m_option_t
*opt
, char* name
, char *param
) {
554 m_config_t
* config
= opt
->priv
;
557 if(!param
) return M_OPT_MISSING_PARAM
;
558 if(!(p
= m_config_get_profile(config
,param
))) {
559 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, MSGTR_UnknownProfile
, param
);
562 if(!config
->profile_depth
)
563 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_Profile
, param
,
564 p
->desc
? p
->desc
: "");
565 config
->profile_depth
++;
566 for(i
= 0 ; i
< p
->num_opts
; i
++) {
567 char spc
[config
->profile_depth
+1];
568 for(j
= 0 ; j
< config
->profile_depth
; j
++)
570 spc
[config
->profile_depth
] = '\0';
572 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
573 p
->opts
[2*i
], p
->opts
[2*i
+1]);
576 if(config
->profile_depth
< MAX_PROFILE_DEPTH
&&
577 !strcmp(p
->opts
[2*i
],"profile")) {
578 char* e
,*list
= p
->opts
[2*i
+1];
579 while((e
= strchr(list
,','))) {
585 show_profile(opt
,name
,tmp
);
589 show_profile(opt
,name
,list
);
592 config
->profile_depth
--;
593 if(!config
->profile_depth
) mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
598 list_options(m_option_t
*opt
, char* name
, char *param
) {
599 m_config_t
* config
= opt
->priv
;
600 m_config_print_option_list(config
);