2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #define MAX_PROFILE_DEPTH 20
40 parse_profile(const m_option_t
*opt
, const char *name
, const char *param
, void *dst
, int src
);
43 set_profile(const m_option_t
*opt
, void* dst
, const void* src
);
46 show_profile(m_option_t
*opt
, char* name
, char *param
);
49 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
);
52 list_options(m_option_t
*opt
, char* name
, char *param
);
57 static int initialized
= 0;
58 static m_option_type_t profile_opt_type
;
59 static m_option_t ref_opts
[] = {
60 { "profile", NULL
, &profile_opt_type
, CONF_NOSAVE
, 0, 0, NULL
},
61 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
62 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
63 { NULL
, NULL
, NULL
, 0, 0, 0, NULL
}
67 config
= calloc(1,sizeof(m_config_t
));
68 config
->lvl
= 1; // 0 Is the defaults
71 profile_opt_type
= m_option_type_string_list
;
72 profile_opt_type
.parse
= parse_profile
;
73 profile_opt_type
.set
= set_profile
;
75 config
->self_opts
= malloc(sizeof(ref_opts
));
76 memcpy(config
->self_opts
,ref_opts
,sizeof(ref_opts
));
77 for(i
= 0 ; config
->self_opts
[i
].name
; i
++)
78 config
->self_opts
[i
].priv
= config
;
79 m_config_register_options(config
,config
->self_opts
);
85 m_config_free(m_config_t
* config
) {
86 m_config_option_t
*i
= config
->opts
, *ct
;
87 m_config_save_slot_t
*sl
,*st
;
92 assert(config
!= NULL
);
96 if (i
->flags
& M_CFG_OPT_ALIAS
)
101 m_option_free(i
->opt
,sl
->data
);
106 if(i
->name
!= i
->opt
->name
)
112 for(p
= config
->profiles
; p
; p
= pn
) {
115 if(p
->desc
) free(p
->desc
);
116 for(j
= 0 ; j
< p
->num_opts
; j
++) {
118 if(p
->opts
[2*j
+1]) free(p
->opts
[2*j
+1]);
123 free(config
->self_opts
);
128 m_config_push(m_config_t
* config
) {
129 m_config_option_t
*co
;
130 m_config_save_slot_t
*slot
;
133 assert(config
!= NULL
);
134 assert(config
->lvl
> 0);
139 for(co
= config
->opts
; co
; co
= co
->next
) {
140 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
142 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
144 if((co
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
& M_CFG_OPT_SET
))
146 if(co
->flags
& M_CFG_OPT_ALIAS
)
149 // Update the current status
150 m_option_save(co
->opt
,co
->slots
->data
,co
->opt
->p
);
152 // Allocate a new slot
153 slot
= calloc(1,sizeof(m_config_save_slot_t
) + co
->opt
->type
->size
);
154 slot
->lvl
= config
->lvl
;
155 slot
->prev
= co
->slots
;
157 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
158 // Reset our set flag
159 co
->flags
&= ~M_CFG_OPT_SET
;
162 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
166 m_config_pop(m_config_t
* config
) {
167 m_config_option_t
*co
;
168 m_config_save_slot_t
*slot
;
171 assert(config
!= NULL
);
172 assert(config
->lvl
> 1);
175 for(co
= config
->opts
; co
; co
= co
->next
) {
177 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
179 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
181 if(co
->flags
& M_CFG_OPT_ALIAS
)
183 if(co
->slots
->lvl
> config
->lvl
)
184 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,MSGTR_SaveSlotTooOld
,config
->lvl
,co
->slots
->lvl
);
186 while(co
->slots
->lvl
>= config
->lvl
) {
187 m_option_free(co
->opt
,co
->slots
->data
);
189 co
->slots
= slot
->prev
;
193 if(pop
) // We removed some ctx -> set the previous value
194 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
198 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
202 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
) {
203 m_config_option_t
*co
;
204 m_config_save_slot_t
* sl
;
207 assert(config
!= NULL
);
208 assert(config
->lvl
> 0);
212 // Allocate a new entry for this option
213 co
= calloc(1,sizeof(m_config_option_t
) + arg
->type
->size
);
216 // Fill in the full name
217 if(prefix
&& strlen(prefix
) > 0) {
218 int l
= strlen(prefix
) + 1 + strlen(arg
->name
) + 1;
219 co
->name
= malloc(l
);
220 sprintf(co
->name
,"%s:%s",prefix
,arg
->name
);
222 co
->name
= arg
->name
;
224 // Option with children -> add them
225 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
226 const m_option_t
*ol
= arg
->p
;
229 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
230 m_config_add_option(config
,&ol
[i
], co
->name
);
232 m_config_option_t
*i
;
233 // Check if there is already an option pointing to this address
235 for(i
= config
->opts
; i
; i
= i
->next
) {
236 if(i
->opt
->p
== arg
->p
) { // So we don't save the same vars more than 1 time
237 co
->slots
= i
->slots
;
238 co
->flags
|= M_CFG_OPT_ALIAS
;
243 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
244 // Allocate a slot for the defaults
245 sl
= calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
246 m_option_save(arg
,sl
->data
,(void**)arg
->p
);
247 // Hack to avoid too much trouble with dynamically allocated data :
248 // We always use a dynamic version
249 if((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
) && arg
->p
&& (*(void**)arg
->p
)) {
250 *(void**)arg
->p
= NULL
;
251 m_option_set(arg
,arg
->p
,sl
->data
);
255 co
->slots
= calloc(1,sizeof(m_config_save_slot_t
) + arg
->type
->size
);
256 co
->slots
->prev
= sl
;
257 co
->slots
->lvl
= config
->lvl
;
258 m_option_copy(co
->opt
,co
->slots
->data
,sl
->data
);
261 co
->next
= config
->opts
;
266 m_config_register_options(m_config_t
*config
, const m_option_t
*args
) {
270 assert(config
!= NULL
);
271 assert(config
->lvl
> 0);
272 assert(args
!= NULL
);
275 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
276 m_config_add_option(config
,&args
[i
],NULL
);
281 static m_config_option_t
*
282 m_config_get_co(m_config_t
*config
, char* arg
) {
283 m_config_option_t
*co
;
285 for(co
= config
->opts
; co
; co
= co
->next
) {
286 int l
= strlen(co
->name
) - 1;
287 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
288 (co
->name
[l
] == '*')) {
289 if(strncasecmp(co
->name
,arg
,l
) == 0)
291 } else if(strcasecmp(co
->name
,arg
) == 0)
298 m_config_parse_option(m_config_t
*config
, char* arg
, char* param
,int set
) {
299 m_config_option_t
*co
;
303 assert(config
!= NULL
);
304 assert(config
->lvl
> 0);
308 co
= m_config_get_co(config
,arg
);
310 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
311 return M_OPT_UNKNOWN
;
315 // This is the only mandatory function
316 assert(co
->opt
->type
->parse
);
319 // Check if this option isn't forbidden in the current mode
320 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
321 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCfgfileOption
,arg
);
322 return M_OPT_INVALID
;
324 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
325 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidCmdlineOption
,arg
);
326 return M_OPT_INVALID
;
328 // During command line preparse set only pre-parse options
329 // Otherwise only set pre-parse option if they were not already set.
330 if(((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
331 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
332 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
333 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
336 // Option with children are a bit different to parse
337 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
340 // Parse the child options
341 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
344 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
345 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
347 // Build the full name
349 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
350 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
352 if(sr
== M_OPT_UNKNOWN
){
353 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_InvalidSuboption
,co
->name
,lst
[2*i
]);
356 if(sr
== M_OPT_MISSING_PARAM
){
357 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingSuboptionParameter
,lst
[2*i
],co
->name
);
368 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
375 m_option_set(co
->opt
,co
->opt
->p
,co
->slots
->data
);
376 co
->flags
|= M_CFG_OPT_SET
;
383 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
384 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
385 return m_config_parse_option(config
,arg
,param
,1);
389 m_config_check_option(m_config_t
*config
, char* arg
, char* param
) {
391 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
392 r
=m_config_parse_option(config
,arg
,param
,0);
393 if(r
==M_OPT_MISSING_PARAM
){
394 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,MSGTR_MissingOptionParameter
,arg
);
395 return M_OPT_INVALID
;
402 m_config_get_option(m_config_t
*config
, char* arg
) {
403 m_config_option_t
*co
;
406 assert(config
!= NULL
);
407 assert(config
->lvl
> 0);
411 co
= m_config_get_co(config
,arg
);
419 m_config_get_option_ptr(m_config_t
*config
, char* arg
) {
420 const m_option_t
* conf
;
423 assert(config
!= NULL
);
427 conf
= m_config_get_option(config
,arg
);
428 if(!conf
) return NULL
;
433 m_config_print_option_list(m_config_t
*config
) {
434 char min
[50],max
[50];
435 m_config_option_t
* co
;
438 if(!config
->opts
) return;
440 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_OptionListHeader
);
441 for(co
= config
->opts
; co
; co
= co
->next
) {
442 const m_option_t
* opt
= co
->opt
;
443 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
444 if(opt
->flags
& M_OPT_MIN
)
445 sprintf(min
,"%-8.0f",opt
->min
);
448 if(opt
->flags
& M_OPT_MAX
)
449 sprintf(max
,"%-8.0f",opt
->max
);
452 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
457 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
458 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
459 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
462 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_TotalOptions
,count
);
466 m_config_get_profile(m_config_t
* config
, char* name
) {
468 for(p
= config
->profiles
; p
; p
= p
->next
)
469 if(!strcmp(p
->name
,name
)) return p
;
474 m_config_add_profile(m_config_t
* config
, char* name
) {
475 m_profile_t
* p
= m_config_get_profile(config
,name
);
477 p
= calloc(1,sizeof(m_profile_t
));
478 p
->name
= strdup(name
);
479 p
->next
= config
->profiles
;
480 config
->profiles
= p
;
485 m_profile_set_desc(m_profile_t
* p
, char* desc
) {
486 if(p
->desc
) free(p
->desc
);
487 p
->desc
= desc
? strdup(desc
) : NULL
;
491 m_config_set_profile_option(m_config_t
* config
, m_profile_t
* p
,
492 char* name
, char* val
) {
493 int i
= m_config_check_option(config
,name
,val
);
495 if(p
->opts
) p
->opts
= realloc(p
->opts
,2*(p
->num_opts
+2)*sizeof(char*));
496 else p
->opts
= malloc(2*(p
->num_opts
+2)*sizeof(char*));
497 p
->opts
[p
->num_opts
*2] = strdup(name
);
498 p
->opts
[p
->num_opts
*2+1] = val
? strdup(val
) : NULL
;
500 p
->opts
[p
->num_opts
*2] = p
->opts
[p
->num_opts
*2+1] = NULL
;
505 m_config_set_profile(m_config_t
* config
, m_profile_t
* p
) {
507 if(config
->profile_depth
> MAX_PROFILE_DEPTH
) {
508 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, MSGTR_ProfileInclusionTooDeep
);
511 config
->profile_depth
++;
512 for(i
= 0 ; i
< p
->num_opts
; i
++)
513 m_config_set_option(config
,p
->opts
[2*i
],p
->opts
[2*i
+1]);
514 config
->profile_depth
--;
518 parse_profile(const m_option_t
*opt
, const char *name
, const char *param
, void *dst
, int src
)
520 m_config_t
* config
= opt
->priv
;
523 if(param
&& !strcmp(param
,"help")) {
525 if(!config
->profiles
) {
526 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_NoProfileDefined
);
529 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_AvailableProfiles
);
530 for(p
= config
->profiles
; p
; p
= p
->next
)
531 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n",p
->name
,
532 p
->desc
? p
->desc
: "");
533 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
537 r
= m_option_type_string_list
.parse(opt
,name
,param
,&list
,src
);
539 if(!list
|| !list
[0]) return M_OPT_INVALID
;
540 for(i
= 0 ; list
[i
] ; i
++)
541 if(!m_config_get_profile(config
,list
[i
])) {
542 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, MSGTR_UnknownProfile
,
547 m_option_copy(opt
,dst
,&list
);
549 m_option_free(opt
,&list
);
554 set_profile(const m_option_t
*opt
, void *dst
, const void *src
) {
555 m_config_t
* config
= opt
->priv
;
559 if(!src
|| !*(char***)src
) return;
560 m_option_copy(opt
,&list
,src
);
561 for(i
= 0 ; list
[i
] ; i
++) {
562 p
= m_config_get_profile(config
,list
[i
]);
564 m_config_set_profile(config
,p
);
566 m_option_free(opt
,&list
);
570 show_profile(m_option_t
*opt
, char* name
, char *param
) {
571 m_config_t
* config
= opt
->priv
;
574 if(!param
) return M_OPT_MISSING_PARAM
;
575 if(!(p
= m_config_get_profile(config
,param
))) {
576 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, MSGTR_UnknownProfile
, param
);
579 if(!config
->profile_depth
)
580 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_Profile
, param
,
581 p
->desc
? p
->desc
: "");
582 config
->profile_depth
++;
583 for(i
= 0 ; i
< p
->num_opts
; i
++) {
584 char spc
[config
->profile_depth
+1];
585 for(j
= 0 ; j
< config
->profile_depth
; j
++)
587 spc
[config
->profile_depth
] = '\0';
589 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
590 p
->opts
[2*i
], p
->opts
[2*i
+1]);
593 if(config
->profile_depth
< MAX_PROFILE_DEPTH
&&
594 !strcmp(p
->opts
[2*i
],"profile")) {
595 char* e
,*list
= p
->opts
[2*i
+1];
596 while((e
= strchr(list
,','))) {
602 show_profile(opt
,name
,tmp
);
606 show_profile(opt
,name
,list
);
609 config
->profile_depth
--;
610 if(!config
->profile_depth
) mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
615 list_options(m_option_t
*opt
, char* name
, char *param
) {
616 m_config_t
* config
= opt
->priv
;
617 m_config_print_option_list(config
);