20 config
= (m_config_t
*)calloc(1,sizeof(m_config_t
));
21 config
->lvl
= 1; // 0 Is the defaults
26 m_config_free(m_config_t
* config
) {
27 m_config_option_t
*i
= config
->opts
, *ct
;
28 m_config_save_slot_t
*sl
,*st
;
31 assert(config
!= NULL
);
35 if (i
->flags
& M_CFG_OPT_ALIAS
)
40 m_option_free(i
->opt
,sl
->data
);
45 if(i
->name
!= i
->opt
->name
)
55 m_config_push(m_config_t
* config
) {
56 m_config_option_t
*co
;
57 m_config_save_slot_t
*slot
;
60 assert(config
!= NULL
);
61 assert(config
->lvl
> 0);
66 for(co
= config
->opts
; co
; co
= co
->next
) {
67 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
69 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
71 if((co
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
&& M_CFG_OPT_SET
))
73 if(co
->flags
& M_CFG_OPT_ALIAS
)
76 // Update the current status
77 m_option_save(co
->opt
,co
->slots
->data
,co
->opt
->p
);
79 // Allocate a new slot
80 slot
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + co
->opt
->type
->size
);
81 slot
->lvl
= config
->lvl
;
82 slot
->prev
= co
->slots
;
84 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
86 co
->flags
&= ~M_CFG_OPT_SET
;
89 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
93 m_config_pop(m_config_t
* config
) {
94 m_config_option_t
*co
;
95 m_config_save_slot_t
*slot
;
98 assert(config
!= NULL
);
99 assert(config
->lvl
> 1);
102 for(co
= config
->opts
; co
; co
= co
->next
) {
104 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
106 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
108 if(co
->flags
& M_CFG_OPT_ALIAS
)
110 if(co
->slots
->lvl
> config
->lvl
)
111 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,MSGTR_SaveSlotTooOld
,config
->lvl
,co
->slots
->lvl
);
113 while(co
->slots
->lvl
>= config
->lvl
) {
114 m_option_free(co
->opt
,co
->slots
->data
);
116 co
->slots
= slot
->prev
;
120 if(pop
) // We removed some ctx -> set the previous value
121 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
125 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
129 m_config_add_option(m_config_t
*config
, m_option_t
*arg
, char* prefix
) {
130 m_config_option_t
*co
;
131 m_config_save_slot_t
* sl
;
134 assert(config
!= NULL
);
135 assert(config
->lvl
> 0);
139 // Allocate a new entry for this option
140 co
= (m_config_option_t
*)calloc(1,sizeof(m_config_option_t
) + arg
->type
->size
);
143 // Fill in the full name
144 if(prefix
&& strlen(prefix
) > 0) {
145 int l
= strlen(prefix
) + 1 + strlen(arg
->name
) + 1;
146 co
->name
= (char*) malloc(l
);
147 sprintf(co
->name
,"%s:%s",prefix
,arg
->name
);
149 co
->name
= arg
->name
;
151 // Option with childs -> add them
152 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
153 m_option_t
*ol
= arg
->p
;
156 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
157 m_config_add_option(config
,&ol
[i
], co
->name
);
159 m_config_option_t
*i
;
160 // Check if there is alredy an option pointing to this address
162 for(i
= config
->opts
; i
; i
= i
->next
) {
163 if(i
->opt
->p
== arg
->p
) { // So we don't save the same vars more than 1 time
164 co
->slots
= i
->slots
;
165 co
->flags
|= M_CFG_OPT_ALIAS
;
170 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
171 // Allocate a slot for the defaults
172 sl
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
173 m_option_save(arg
,sl
->data
,(void**)arg
->p
);
174 // Hack to avoid too much trouble with dynamicly allocated data :
175 // We always use a dynamic version
176 if((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
) && arg
->p
&& (*(void**)arg
->p
)) {
177 *(void**)arg
->p
= NULL
;
178 m_option_set(arg
,arg
->p
,sl
->data
);
182 co
->slots
= (m_config_save_slot_t
*)calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
183 co
->slots
->prev
= sl
;
184 co
->slots
->lvl
= config
->lvl
;
185 m_option_copy(co
->opt
,co
->slots
->data
,sl
->data
);
188 co
->next
= config
->opts
;
193 m_config_register_options(m_config_t
*config
, m_option_t
*args
) {
197 assert(config
!= NULL
);
198 assert(config
->lvl
> 0);
199 assert(args
!= NULL
);
202 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
203 m_config_add_option(config
,&args
[i
],NULL
);
208 static m_config_option_t
*
209 m_config_get_co(m_config_t
*config
, char* arg
) {
210 m_config_option_t
*co
;
212 for(co
= config
->opts
; co
; co
= co
->next
) {
213 int l
= strlen(co
->name
) - 1;
214 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
215 (co
->name
[l
] == '*')) {
216 if(strncasecmp(co
->name
,arg
,l
) == 0)
218 } else if(strcasecmp(co
->name
,arg
) == 0)
225 m_config_parse_option(m_config_t
*config
, char* arg
, char* param
,int set
) {
226 m_config_option_t
*co
;
230 assert(config
!= NULL
);
231 assert(config
->lvl
> 0);
235 co
= m_config_get_co(config
,arg
);
237 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
238 return M_OPT_UNKNOWN
;
242 // This is the only mandatory function
243 assert(co
->opt
->type
->parse
);
246 // Check if this option isn't forbiden in the current mode
247 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
248 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCfgfileOption
,arg
);
249 return M_OPT_INVALID
;
251 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
252 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCmdlineOption
,arg
);
253 return M_OPT_INVALID
;
256 // Option with childs are a bit different to parse
257 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
260 // Parse the child options
261 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
264 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
265 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
267 // Build the full name
269 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
270 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
272 if(sr
== M_OPT_UNKNOWN
){
273 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidSuboption
,co
->name
,lst
[2*i
]);
276 if(sr
== M_OPT_MISSING_PARAM
){
277 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingSuboptionParameter
,lst
[2*i
],co
->name
);
288 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
295 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
296 co
->flags
|= M_CFG_OPT_SET
;
303 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
304 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
305 return m_config_parse_option(config
,arg
,param
,1);
309 m_config_check_option(m_config_t
*config
, char* arg
, char* param
) {
311 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
312 r
=m_config_parse_option(config
,arg
,param
,0);
313 if(r
==M_OPT_MISSING_PARAM
){
314 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingOptionParameter
,arg
);
315 return M_OPT_INVALID
;
322 m_config_get_option(m_config_t
*config
, char* arg
) {
323 m_config_option_t
*co
;
326 assert(config
!= NULL
);
327 assert(config
->lvl
> 0);
331 co
= m_config_get_co(config
,arg
);
339 m_config_get_option_ptr(m_config_t
*config
, char* arg
) {
343 assert(config
!= NULL
);
347 conf
= m_config_get_option(config
,arg
);
348 if(!conf
) return NULL
;
353 m_config_print_option_list(m_config_t
*config
) {
354 char min
[50],max
[50];
355 m_config_option_t
* co
;
358 if(!config
->opts
) return;
360 mp_msg(MSGT_FIXME
, MSGL_FIXME
, MSGTR_OptionListHeader
);
361 for(co
= config
->opts
; co
; co
= co
->next
) {
362 m_option_t
* opt
= co
->opt
;
363 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
364 if(opt
->flags
& M_OPT_MIN
)
365 sprintf(min
,"%-8.0f",opt
->min
);
368 if(opt
->flags
& M_OPT_MAX
)
369 sprintf(max
,"%-8.0f",opt
->max
);
372 mp_msg(MSGT_FIXME
, MSGL_FIXME
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
377 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
378 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
379 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
382 mp_msg(MSGT_FIXME
, MSGL_FIXME
, MSGTR_TotalOptions
,count
);