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
);
54 static void m_option_save(const m_config_t
*config
, const m_option_t
*opt
,
57 if (opt
->type
->save
) {
58 const void *src
= opt
->new ? (char*)config
->optstruct
+ opt
->offset
: opt
->p
;
59 opt
->type
->save(opt
, dst
, src
);
63 static void m_option_set(const m_config_t
*config
, const m_option_t
*opt
,
67 void *dst
= opt
->new ? (char*)config
->optstruct
+ opt
->offset
: opt
->p
;
68 opt
->type
->set(opt
, dst
, src
);
74 m_config_t
*m_config_new(void *optstruct
,
75 int includefunc(m_option_t
*conf
, char *filename
))
78 static int initialized
= 0;
79 static m_option_type_t profile_opt_type
;
80 static const m_option_t ref_opts
[] = {
81 { "profile", NULL
, &profile_opt_type
, CONF_NOSAVE
, 0, 0, NULL
},
82 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
83 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
84 { NULL
, NULL
, NULL
, 0, 0, 0, NULL
}
88 config
= talloc_zero(NULL
, m_config_t
);
89 config
->lvl
= 1; // 0 Is the defaults
92 profile_opt_type
= m_option_type_string_list
;
93 profile_opt_type
.parse
= parse_profile
;
94 profile_opt_type
.set
= set_profile
;
96 m_option_t
*self_opts
= talloc_memdup(config
, ref_opts
, sizeof(ref_opts
));
97 for (i
= 0; self_opts
[i
].name
; i
++)
98 self_opts
[i
].priv
= config
;
99 m_config_register_options(config
, self_opts
);
101 struct m_option
*p
= talloc_ptrtype(config
, p
);
102 *p
= (struct m_option
){"include", includefunc
, CONF_TYPE_FUNC_PARAM
,
103 CONF_NOSAVE
, 0, 0, config
};
104 m_config_add_option(config
, p
, NULL
);
106 config
->optstruct
= optstruct
;
111 void m_config_free(m_config_t
* config
)
113 m_config_option_t
*opt
;
114 for (opt
= config
->opts
; opt
; opt
= opt
->next
) {
115 if (opt
->flags
& M_CFG_OPT_ALIAS
)
117 m_config_save_slot_t
*sl
;
118 for (sl
= opt
->slots
; sl
; sl
= sl
->prev
)
119 m_option_free(opt
->opt
, sl
->data
);
125 m_config_push(m_config_t
* config
) {
126 m_config_option_t
*co
;
127 m_config_save_slot_t
*slot
;
130 assert(config
!= NULL
);
131 assert(config
->lvl
> 0);
136 for(co
= config
->opts
; co
; co
= co
->next
) {
137 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
139 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
141 if((co
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
& M_CFG_OPT_SET
))
143 if(co
->flags
& M_CFG_OPT_ALIAS
)
146 // Update the current status
147 m_option_save(config
, co
->opt
, co
->slots
->data
);
149 // Allocate a new slot
150 slot
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
151 co
->opt
->type
->size
);
152 slot
->lvl
= config
->lvl
;
153 slot
->prev
= co
->slots
;
155 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
156 // Reset our set flag
157 co
->flags
&= ~M_CFG_OPT_SET
;
160 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
164 m_config_pop(m_config_t
* config
) {
165 m_config_option_t
*co
;
166 m_config_save_slot_t
*slot
;
169 assert(config
!= NULL
);
170 assert(config
->lvl
> 1);
173 for(co
= config
->opts
; co
; co
= co
->next
) {
175 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
177 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
179 if(co
->flags
& M_CFG_OPT_ALIAS
)
181 if(co
->slots
->lvl
> config
->lvl
)
182 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,"Save slot found from lvl %d is too old: %d !!!\n",config
->lvl
,co
->slots
->lvl
);
184 while(co
->slots
->lvl
>= config
->lvl
) {
185 m_option_free(co
->opt
,co
->slots
->data
);
187 co
->slots
= slot
->prev
;
191 if(pop
) // We removed some ctx -> set the previous value
192 m_option_set(config
, co
->opt
, co
->slots
->data
);
196 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
200 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
) {
201 m_config_option_t
*co
;
202 m_config_save_slot_t
* sl
;
205 assert(config
!= NULL
);
206 assert(config
->lvl
> 0);
210 // Allocate a new entry for this option
211 co
= talloc_zero_size(config
, sizeof(m_config_option_t
) + arg
->type
->size
);
214 // Fill in the full name
215 if(prefix
&& strlen(prefix
) > 0) {
216 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
218 co
->name
= arg
->name
;
220 // Option with children -> add them
221 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
222 const m_option_t
*ol
= arg
->p
;
225 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
226 m_config_add_option(config
,&ol
[i
], co
->name
);
228 m_config_option_t
*i
;
229 // Check if there is already an option pointing to this address
230 if(arg
->p
|| arg
->new && arg
->offset
>= 0) {
231 for(i
= config
->opts
; i
; i
= i
->next
) {
232 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
233 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
234 // So we don't save the same vars more than 1 time
235 co
->slots
= i
->slots
;
236 co
->flags
|= M_CFG_OPT_ALIAS
;
241 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
242 // Allocate a slot for the defaults
243 sl
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
245 m_option_save(config
, arg
, sl
->data
);
246 // Hack to avoid too much trouble with dynamically allocated data :
247 // We always use a dynamic version
248 if ((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
249 char **hackptr
= arg
->new ? (char*)config
->optstruct
+ arg
->offset
251 if (hackptr
&& *hackptr
) {
253 m_option_set(config
, arg
, sl
->data
);
258 co
->slots
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
260 co
->slots
->prev
= sl
;
261 co
->slots
->lvl
= config
->lvl
;
262 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
265 co
->next
= config
->opts
;
270 m_config_register_options(m_config_t
*config
, const m_option_t
*args
) {
274 assert(config
!= NULL
);
275 assert(config
->lvl
> 0);
276 assert(args
!= NULL
);
279 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
280 m_config_add_option(config
,&args
[i
],NULL
);
285 static m_config_option_t
*
286 m_config_get_co(m_config_t
*config
, char* arg
) {
287 m_config_option_t
*co
;
289 for(co
= config
->opts
; co
; co
= co
->next
) {
290 int l
= strlen(co
->name
) - 1;
291 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
292 (co
->name
[l
] == '*')) {
293 if(strncasecmp(co
->name
,arg
,l
) == 0)
295 } else if(strcasecmp(co
->name
,arg
) == 0)
302 m_config_parse_option(m_config_t
*config
, char* arg
, char* param
,int set
) {
303 m_config_option_t
*co
;
307 assert(config
!= NULL
);
308 assert(config
->lvl
> 0);
312 co
= m_config_get_co(config
,arg
);
314 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
315 return M_OPT_UNKNOWN
;
319 // This is the only mandatory function
320 assert(co
->opt
->type
->parse
);
323 // Check if this option isn't forbidden in the current mode
324 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
325 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used in a config file.\n",arg
);
326 return M_OPT_INVALID
;
328 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
329 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used on the command line.\n",arg
);
330 return M_OPT_INVALID
;
332 // During command line preparse set only pre-parse options
333 // Otherwise only set pre-parse option if they were not already set.
334 if(((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
335 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
336 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
337 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
340 // Option with children are a bit different to parse
341 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
344 // Parse the child options
345 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
348 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
349 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
351 // Build the full name
353 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
354 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
356 if(sr
== M_OPT_UNKNOWN
){
357 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' has no suboption '%s'.\n",co
->name
,lst
[2*i
]);
360 if(sr
== M_OPT_MISSING_PARAM
){
361 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: suboption '%s' of '%s' must have a parameter!\n",lst
[2*i
],co
->name
);
372 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
379 m_option_set(config
, co
->opt
, co
->slots
->data
);
380 co
->flags
|= M_CFG_OPT_SET
;
387 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
388 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
389 return m_config_parse_option(config
,arg
,param
,1);
393 m_config_check_option(m_config_t
*config
, char* arg
, char* param
) {
395 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
396 r
=m_config_parse_option(config
,arg
,param
,0);
397 if(r
==M_OPT_MISSING_PARAM
){
398 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' must have a parameter!\n",arg
);
399 return M_OPT_INVALID
;
406 m_config_get_option(m_config_t
*config
, char* arg
) {
407 m_config_option_t
*co
;
410 assert(config
!= NULL
);
411 assert(config
->lvl
> 0);
415 co
= m_config_get_co(config
,arg
);
423 m_config_print_option_list(m_config_t
*config
) {
424 char min
[50],max
[50];
425 m_config_option_t
* co
;
428 if(!config
->opts
) return;
430 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\n Name Type Min Max Global CL Cfg\n\n");
431 for(co
= config
->opts
; co
; co
= co
->next
) {
432 const m_option_t
* opt
= co
->opt
;
433 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
434 if(opt
->flags
& M_OPT_MIN
)
435 sprintf(min
,"%-8.0f",opt
->min
);
438 if(opt
->flags
& M_OPT_MAX
)
439 sprintf(max
,"%-8.0f",opt
->max
);
442 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
447 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
448 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
449 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
452 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n",count
);
456 m_config_get_profile(m_config_t
* config
, char* name
) {
458 for(p
= config
->profiles
; p
; p
= p
->next
)
459 if(!strcmp(p
->name
,name
)) return p
;
464 m_config_add_profile(m_config_t
* config
, char* name
) {
465 m_profile_t
* p
= m_config_get_profile(config
,name
);
467 p
= talloc_zero(config
, m_profile_t
);
468 p
->name
= talloc_strdup(p
, name
);
469 p
->next
= config
->profiles
;
470 config
->profiles
= p
;
475 m_profile_set_desc(m_profile_t
* p
, char* desc
) {
476 talloc_free(p
->desc
);
477 p
->desc
= talloc_strdup(p
, desc
);
481 m_config_set_profile_option(m_config_t
* config
, m_profile_t
* p
,
482 char* name
, char* val
) {
483 int i
= m_config_check_option(config
,name
,val
);
485 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2*(p
->num_opts
+2));
486 p
->opts
[p
->num_opts
*2] = talloc_strdup(p
, name
);
487 p
->opts
[p
->num_opts
*2+1] = talloc_strdup(p
, val
);
489 p
->opts
[p
->num_opts
*2] = p
->opts
[p
->num_opts
*2+1] = NULL
;
494 m_config_set_profile(m_config_t
* config
, m_profile_t
* p
) {
496 if(config
->profile_depth
> MAX_PROFILE_DEPTH
) {
497 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "WARNING: Profile inclusion too deep.\n");
500 config
->profile_depth
++;
501 for(i
= 0 ; i
< p
->num_opts
; i
++)
502 m_config_set_option(config
,p
->opts
[2*i
],p
->opts
[2*i
+1]);
503 config
->profile_depth
--;
507 parse_profile(const m_option_t
*opt
, const char *name
, const char *param
, void *dst
, int src
)
509 m_config_t
* config
= opt
->priv
;
512 if(param
&& !strcmp(param
,"help")) {
514 if(!config
->profiles
) {
515 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "No profiles have been defined.\n");
518 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Available profiles:\n");
519 for(p
= config
->profiles
; p
; p
= p
->next
)
520 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n",p
->name
,
521 p
->desc
? p
->desc
: "");
522 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
526 r
= m_option_type_string_list
.parse(opt
,name
,param
,&list
,src
);
528 if(!list
|| !list
[0]) return M_OPT_INVALID
;
529 for(i
= 0 ; list
[i
] ; i
++)
530 if(!m_config_get_profile(config
,list
[i
])) {
531 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "Unknown profile '%s'.\n",
536 m_option_copy(opt
,dst
,&list
);
538 m_option_free(opt
,&list
);
543 set_profile(const m_option_t
*opt
, void *dst
, const void *src
) {
544 m_config_t
* config
= opt
->priv
;
548 if(!src
|| !*(char***)src
) return;
549 m_option_copy(opt
,&list
,src
);
550 for(i
= 0 ; list
[i
] ; i
++) {
551 p
= m_config_get_profile(config
,list
[i
]);
553 m_config_set_profile(config
,p
);
555 m_option_free(opt
,&list
);
559 show_profile(m_option_t
*opt
, char* name
, char *param
) {
560 m_config_t
* config
= opt
->priv
;
563 if(!param
) return M_OPT_MISSING_PARAM
;
564 if(!(p
= m_config_get_profile(config
,param
))) {
565 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
, "Unknown profile '%s'.\n", param
);
568 if(!config
->profile_depth
)
569 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Profile %s: %s\n", param
,
570 p
->desc
? p
->desc
: "");
571 config
->profile_depth
++;
572 for(i
= 0 ; i
< p
->num_opts
; i
++) {
573 char spc
[config
->profile_depth
+1];
574 for(j
= 0 ; j
< config
->profile_depth
; j
++)
576 spc
[config
->profile_depth
] = '\0';
578 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
579 p
->opts
[2*i
], p
->opts
[2*i
+1]);
582 if(config
->profile_depth
< MAX_PROFILE_DEPTH
&&
583 !strcmp(p
->opts
[2*i
],"profile")) {
584 char* e
,*list
= p
->opts
[2*i
+1];
585 while((e
= strchr(list
,','))) {
591 show_profile(opt
,name
,tmp
);
595 show_profile(opt
,name
,list
);
598 config
->profile_depth
--;
599 if(!config
->profile_depth
) mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
604 list_options(m_option_t
*opt
, char* name
, char *param
) {
605 m_config_t
* config
= opt
->priv
;
606 m_config_print_option_list(config
);