19 config
= (m_config_t
*)calloc(1,sizeof(m_config_t
));
20 config
->lvl
= 1; // 0 Is the defaults
25 m_config_free(m_config_t
* config
) {
26 m_config_option_t
*i
= config
->opts
, *ct
;
27 m_config_save_slot_t
*sl
,*st
;
30 assert(config
!= NULL
);
36 m_option_free(i
->opt
,sl
->data
);
41 if(i
->name
!= i
->opt
->name
)
51 m_config_push(m_config_t
* config
) {
52 m_config_option_t
*co
;
53 m_config_save_slot_t
*slot
;
56 assert(config
!= NULL
);
57 assert(config
->lvl
> 0);
62 for(co
= config
->opts
; co
; co
= co
->next
) {
63 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
65 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
67 if((co
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
&& M_CFG_OPT_SET
))
69 if(co
->flags
& M_CFG_OPT_ALIAS
)
72 // Update the current status
73 m_option_save(co
->opt
,co
->slots
->data
,co
->opt
->p
);
75 // Allocate a new slot
76 slot
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + co
->opt
->type
->size
);
77 slot
->lvl
= config
->lvl
;
78 slot
->prev
= co
->slots
;
80 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
82 co
->flags
&= ~M_CFG_OPT_SET
;
85 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
89 m_config_pop(m_config_t
* config
) {
90 m_config_option_t
*co
;
91 m_config_save_slot_t
*slot
;
94 assert(config
!= NULL
);
95 assert(config
->lvl
> 1);
98 for(co
= config
->opts
; co
; co
= co
->next
) {
100 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
102 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
104 if(co
->flags
& M_CFG_OPT_ALIAS
)
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
);
109 while(co
->slots
->lvl
>= config
->lvl
) {
110 m_option_free(co
->opt
,co
->slots
->data
);
112 co
->slots
= slot
->prev
;
116 if(pop
) // We removed some ctx -> set the previous value
117 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
121 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
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
;
130 assert(config
!= NULL
);
131 assert(config
->lvl
> 0);
135 // Allocate a new entry for this option
136 co
= (m_config_option_t
*)calloc(1,sizeof(m_config_option_t
) + arg
->type
->size
);
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
);
145 co
->name
= arg
->name
;
147 // Option with childs -> add them
148 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
149 m_option_t
*ol
= arg
->p
;
151 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
152 m_config_add_option(config
,&ol
[i
], co
->name
);
154 m_config_option_t
*i
;
155 // Check if there is alredy an option pointing to this address
157 for(i
= config
->opts
; i
; i
= i
->next
) {
158 if(i
->opt
->p
== arg
->p
) { // So we don't save the same vars more than 1 time
159 co
->slots
= i
->slots
;
160 co
->flags
|= M_CFG_OPT_ALIAS
;
165 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
166 // Allocate a slot for the defaults
167 sl
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
168 m_option_save(arg
,sl
->data
,(void**)arg
->p
);
169 // Hack to avoid too much trouble with dynamicly allocated data :
170 // We always use a dynamic version
171 if((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
) && arg
->p
&& (*(void**)arg
->p
)) {
172 *(void**)arg
->p
= NULL
;
173 m_option_set(arg
,arg
->p
,sl
->data
);
176 co
->slots
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
177 co
->slots
->prev
= sl
;
178 co
->slots
->lvl
= config
->lvl
;
179 m_option_copy(co
->opt
,co
->slots
->data
,sl
->data
);
182 co
->next
= config
->opts
;
187 m_config_register_options(m_config_t
*config
, m_option_t
*args
) {
191 assert(config
!= NULL
);
192 assert(config
->lvl
> 0);
193 assert(args
!= NULL
);
196 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
197 m_config_add_option(config
,&args
[i
],NULL
);
202 static m_config_option_t
*
203 m_config_get_co(m_config_t
*config
, char* arg
) {
204 m_config_option_t
*co
;
206 for(co
= config
->opts
; co
; co
= co
->next
) {
207 int l
= strlen(co
->name
) - 1;
208 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
209 (co
->name
[l
] == '*')) {
210 if(strncasecmp(co
->name
,arg
,l
) == 0)
212 } else if(strcasecmp(co
->name
,arg
) == 0)
219 m_config_parse_option(m_config_t
*config
, char* arg
, char* param
,int set
) {
220 m_config_option_t
*co
;
224 assert(config
!= NULL
);
225 assert(config
->lvl
> 0);
229 co
= m_config_get_co(config
,arg
);
231 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
232 return M_OPT_UNKNOWN
;
236 // This is the only mandatory function
237 assert(co
->opt
->type
->parse
);
240 // Check if this option isn't forbiden in the current mode
241 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
242 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used in a config file\n",arg
);
243 return M_OPT_INVALID
;
245 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
246 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used on the command line\n",arg
);
247 return M_OPT_INVALID
;
250 // Option with childs are a bit different to parse
251 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
254 // Parse the child options
255 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
258 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
259 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
261 // Build the full name
263 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
264 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
266 if(sr
== M_OPT_UNKNOWN
){
267 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' has no suboption '%s'\n",co
->name
,lst
[2*i
]);
270 if(sr
== M_OPT_MISSING_PARAM
){
271 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: suboption '%s' of '%s' must have a parameter!\n",lst
[2*i
],co
->name
);
282 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
289 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
290 co
->flags
|= M_CFG_OPT_SET
;
297 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
298 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
299 return m_config_parse_option(config
,arg
,param
,1);
303 m_config_check_option(m_config_t
*config
, char* arg
, char* param
) {
305 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
306 r
=m_config_parse_option(config
,arg
,param
,0);
307 if(r
==M_OPT_MISSING_PARAM
){
308 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' must have a parameter!\n",arg
);
309 return M_OPT_INVALID
;
316 m_config_get_option(m_config_t
*config
, char* arg
) {
317 m_config_option_t
*co
;
320 assert(config
!= NULL
);
321 assert(config
->lvl
> 0);
325 co
= m_config_get_co(config
,arg
);
333 m_config_get_option_ptr(m_config_t
*config
, char* arg
) {
337 assert(config
!= NULL
);
341 conf
= m_config_get_option(config
,arg
);
342 if(!conf
) return NULL
;
347 m_config_print_option_list(m_config_t
*config
) {
348 char min
[50],max
[50];
349 m_config_option_t
* co
;
352 if(!config
->opts
) return;
354 printf("\n Name Type Min Max Global CL Cfg\n\n");
355 for(co
= config
->opts
; co
; co
= co
->next
) {
356 m_option_t
* opt
= co
->opt
;
357 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
358 if(opt
->flags
& M_OPT_MIN
)
359 sprintf(min
,"%-8.0f",opt
->min
);
362 if(opt
->flags
& M_OPT_MAX
)
363 sprintf(max
,"%-8.0f",opt
->max
);
366 printf(" %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
371 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
372 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
373 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
376 printf("\nTotal: %d options\n",count
);