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
39 static int parse_profile(const m_option_t
*opt
, const char *name
,
40 const char *param
, void *dst
, int src
)
42 m_config_t
*config
= opt
->priv
;
45 if (param
&& !strcmp(param
, "help")) {
47 if (!config
->profiles
) {
48 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "No profiles have been defined.\n");
51 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Available profiles:\n");
52 for (p
= config
->profiles
; p
; p
= p
->next
)
53 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n", p
->name
,
54 p
->desc
? p
->desc
: "");
55 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
59 r
= m_option_type_string_list
.parse(opt
, name
, param
, &list
, src
);
62 if (!list
|| !list
[0])
64 for (i
= 0; list
[i
]; i
++)
65 if (!m_config_get_profile(config
,list
[i
])) {
66 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "Unknown profile '%s'.\n",
71 m_option_copy(opt
, dst
, &list
);
73 m_option_free(opt
, &list
);
77 static void set_profile(const m_option_t
*opt
, void *dst
, const void *src
)
79 m_config_t
*config
= opt
->priv
;
83 if (!src
|| !*(char***)src
)
85 m_option_copy(opt
, &list
, src
);
86 for (i
= 0; list
[i
]; i
++) {
87 p
= m_config_get_profile(config
, list
[i
]);
90 m_config_set_profile(config
, p
);
92 m_option_free(opt
, &list
);
95 static int show_profile(m_option_t
*opt
, char* name
, char *param
)
97 m_config_t
*config
= opt
->priv
;
101 return M_OPT_MISSING_PARAM
;
102 if (!(p
= m_config_get_profile(config
, param
))) {
103 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
, "Unknown profile '%s'.\n", param
);
104 return M_OPT_EXIT
- 1;
106 if (!config
->profile_depth
)
107 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Profile %s: %s\n", param
,
108 p
->desc
? p
->desc
: "");
109 config
->profile_depth
++;
110 for (i
= 0; i
< p
->num_opts
; i
++) {
111 char spc
[config
->profile_depth
+ 1];
112 for (j
= 0; j
< config
->profile_depth
; j
++)
114 spc
[config
->profile_depth
] = '\0';
116 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
117 p
->opts
[2 * i
], p
->opts
[2 * i
+ 1]);
119 if (config
->profile_depth
< MAX_PROFILE_DEPTH
120 && !strcmp(p
->opts
[2*i
], "profile")) {
121 char *e
, *list
= p
->opts
[2 * i
+ 1];
122 while ((e
= strchr(list
, ','))) {
127 memcpy(tmp
, list
, l
);
129 show_profile(opt
, name
, tmp
);
133 show_profile(opt
, name
, list
);
136 config
->profile_depth
--;
137 if (!config
->profile_depth
)
138 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
139 return M_OPT_EXIT
- 1;
142 static int list_options(m_option_t
*opt
, char *name
, char *param
)
144 m_config_t
*config
= opt
->priv
;
145 m_config_print_option_list(config
);
149 static void m_option_save(const m_config_t
*config
, const m_option_t
*opt
,
152 if (opt
->type
->save
) {
153 const void *src
= m_option_get_ptr(opt
, config
->optstruct
);
154 opt
->type
->save(opt
, dst
, src
);
158 static void m_option_set(const m_config_t
*config
, const m_option_t
*opt
,
161 if (opt
->type
->set
) {
162 void *dst
= m_option_get_ptr(opt
, config
->optstruct
);
163 opt
->type
->set(opt
, dst
, src
);
170 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
);
172 m_config_t
*m_config_new(void *optstruct
,
173 int includefunc(m_option_t
*conf
, char *filename
))
176 static int initialized
= 0;
177 static m_option_type_t profile_opt_type
;
178 static const m_option_t ref_opts
[] = {
179 { "profile", NULL
, &profile_opt_type
, CONF_NOSAVE
, 0, 0, NULL
},
180 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
181 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
, 0, 0, NULL
},
182 { NULL
, NULL
, NULL
, 0, 0, 0, NULL
}
186 config
= talloc_zero(NULL
, m_config_t
);
187 config
->lvl
= 1; // 0 Is the defaults
190 profile_opt_type
= m_option_type_string_list
;
191 profile_opt_type
.parse
= parse_profile
;
192 profile_opt_type
.set
= set_profile
;
194 m_option_t
*self_opts
= talloc_memdup(config
, ref_opts
, sizeof(ref_opts
));
195 for (i
= 0; self_opts
[i
].name
; i
++)
196 self_opts
[i
].priv
= config
;
197 m_config_register_options(config
, self_opts
);
199 struct m_option
*p
= talloc_ptrtype(config
, p
);
200 *p
= (struct m_option
){"include", includefunc
, CONF_TYPE_FUNC_PARAM
,
201 CONF_NOSAVE
, 0, 0, config
};
202 m_config_add_option(config
, p
, NULL
);
204 config
->optstruct
= optstruct
;
209 void m_config_free(m_config_t
* config
)
211 m_config_option_t
*opt
;
212 for (opt
= config
->opts
; opt
; opt
= opt
->next
) {
213 if (opt
->flags
& M_CFG_OPT_ALIAS
)
215 m_config_save_slot_t
*sl
;
216 for (sl
= opt
->slots
; sl
; sl
= sl
->prev
)
217 m_option_free(opt
->opt
, sl
->data
);
223 m_config_push(m_config_t
* config
) {
224 m_config_option_t
*co
;
225 m_config_save_slot_t
*slot
;
228 assert(config
!= NULL
);
229 assert(config
->lvl
> 0);
234 for(co
= config
->opts
; co
; co
= co
->next
) {
235 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
237 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
239 if(co
->flags
& M_CFG_OPT_ALIAS
)
242 // Update the current status
243 m_option_save(config
, co
->opt
, co
->slots
->data
);
245 // Allocate a new slot
246 slot
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
247 co
->opt
->type
->size
);
248 slot
->lvl
= config
->lvl
;
249 slot
->prev
= co
->slots
;
251 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
252 // Reset our set flag
253 co
->flags
&= ~M_CFG_OPT_SET
;
256 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
260 m_config_pop(m_config_t
* config
) {
261 m_config_option_t
*co
;
262 m_config_save_slot_t
*slot
;
265 assert(config
!= NULL
);
266 assert(config
->lvl
> 1);
269 for(co
= config
->opts
; co
; co
= co
->next
) {
271 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
273 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
275 if(co
->flags
& M_CFG_OPT_ALIAS
)
277 if(co
->slots
->lvl
> config
->lvl
)
278 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,"Save slot found from lvl %d is too old: %d !!!\n",config
->lvl
,co
->slots
->lvl
);
280 while(co
->slots
->lvl
>= config
->lvl
) {
281 m_option_free(co
->opt
,co
->slots
->data
);
283 co
->slots
= slot
->prev
;
287 if(pop
) // We removed some ctx -> set the previous value
288 m_option_set(config
, co
->opt
, co
->slots
->data
);
292 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
296 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
) {
297 m_config_option_t
*co
;
298 m_config_save_slot_t
* sl
;
301 assert(config
!= NULL
);
302 assert(config
->lvl
> 0);
306 // Allocate a new entry for this option
307 co
= talloc_zero_size(config
, sizeof(m_config_option_t
) + arg
->type
->size
);
310 // Fill in the full name
311 if(prefix
&& strlen(prefix
) > 0) {
312 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
314 co
->name
= arg
->name
;
316 // Option with children -> add them
317 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
318 const m_option_t
*ol
= arg
->p
;
321 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
322 m_config_add_option(config
,&ol
[i
], co
->name
);
324 m_config_option_t
*i
;
325 // Check if there is already an option pointing to this address
326 if(arg
->p
|| arg
->new && arg
->offset
>= 0) {
327 for(i
= config
->opts
; i
; i
= i
->next
) {
328 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
329 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
330 // So we don't save the same vars more than 1 time
331 co
->slots
= i
->slots
;
332 co
->flags
|= M_CFG_OPT_ALIAS
;
337 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
338 // Allocate a slot for the defaults
339 sl
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
341 m_option_save(config
, arg
, sl
->data
);
342 // Hack to avoid too much trouble with dynamically allocated data :
343 // We always use a dynamic version
344 if ((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
345 char **hackptr
= arg
->new ? (char*)config
->optstruct
+ arg
->offset
347 if (hackptr
&& *hackptr
) {
349 m_option_set(config
, arg
, sl
->data
);
354 co
->slots
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
356 co
->slots
->prev
= sl
;
357 co
->slots
->lvl
= config
->lvl
;
358 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
361 co
->next
= config
->opts
;
366 m_config_register_options(m_config_t
*config
, const m_option_t
*args
) {
370 assert(config
!= NULL
);
371 assert(config
->lvl
> 0);
372 assert(args
!= NULL
);
375 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
376 m_config_add_option(config
,&args
[i
],NULL
);
381 static m_config_option_t
*
382 m_config_get_co(const m_config_t
*config
, char *arg
) {
383 m_config_option_t
*co
;
385 for(co
= config
->opts
; co
; co
= co
->next
) {
386 int l
= strlen(co
->name
) - 1;
387 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
388 (co
->name
[l
] == '*')) {
389 if(strncasecmp(co
->name
,arg
,l
) == 0)
391 } else if(strcasecmp(co
->name
,arg
) == 0)
398 m_config_parse_option(const m_config_t
*config
, char *arg
, char *param
, int set
) {
399 m_config_option_t
*co
;
403 assert(config
!= NULL
);
404 assert(config
->lvl
> 0);
408 co
= m_config_get_co(config
,arg
);
410 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
411 return M_OPT_UNKNOWN
;
415 // This is the only mandatory function
416 assert(co
->opt
->type
->parse
);
419 // Check if this option isn't forbidden in the current mode
420 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
421 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used in a config file.\n",arg
);
422 return M_OPT_INVALID
;
424 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
425 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used on the command line.\n",arg
);
426 return M_OPT_INVALID
;
428 // During command line preparse set only pre-parse options
429 // Otherwise only set pre-parse option if they were not already set.
430 if(((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
431 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
432 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
433 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
436 // Option with children are a bit different to parse
437 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
440 // Parse the child options
441 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
444 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
445 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
447 // Build the full name
449 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
450 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
452 if(sr
== M_OPT_UNKNOWN
){
453 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' has no suboption '%s'.\n",co
->name
,lst
[2*i
]);
456 if(sr
== M_OPT_MISSING_PARAM
){
457 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: suboption '%s' of '%s' must have a parameter!\n",lst
[2*i
],co
->name
);
468 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
475 m_option_set(config
, co
->opt
, co
->slots
->data
);
476 co
->flags
|= M_CFG_OPT_SET
;
483 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
484 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
485 return m_config_parse_option(config
,arg
,param
,1);
489 m_config_check_option(const m_config_t
*config
, char *arg
, char *param
) {
491 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
492 r
=m_config_parse_option(config
,arg
,param
,0);
493 if(r
==M_OPT_MISSING_PARAM
){
494 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' must have a parameter!\n",arg
);
495 return M_OPT_INVALID
;
502 m_config_get_option(const m_config_t
*config
, char *arg
) {
503 m_config_option_t
*co
;
506 assert(config
!= NULL
);
507 assert(config
->lvl
> 0);
511 co
= m_config_get_co(config
,arg
);
519 m_config_print_option_list(const m_config_t
*config
) {
520 char min
[50],max
[50];
521 m_config_option_t
* co
;
524 if(!config
->opts
) return;
526 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\n Name Type Min Max Global CL Cfg\n\n");
527 for(co
= config
->opts
; co
; co
= co
->next
) {
528 const m_option_t
* opt
= co
->opt
;
529 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
530 if(opt
->flags
& M_OPT_MIN
)
531 sprintf(min
,"%-8.0f",opt
->min
);
534 if(opt
->flags
& M_OPT_MAX
)
535 sprintf(max
,"%-8.0f",opt
->max
);
538 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
543 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
544 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
545 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
548 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n",count
);
552 m_config_get_profile(const m_config_t
*config
, char *name
) {
554 for(p
= config
->profiles
; p
; p
= p
->next
)
555 if(!strcmp(p
->name
,name
)) return p
;
560 m_config_add_profile(m_config_t
* config
, char* name
) {
561 m_profile_t
* p
= m_config_get_profile(config
,name
);
563 p
= talloc_zero(config
, m_profile_t
);
564 p
->name
= talloc_strdup(p
, name
);
565 p
->next
= config
->profiles
;
566 config
->profiles
= p
;
571 m_profile_set_desc(m_profile_t
* p
, char* desc
) {
572 talloc_free(p
->desc
);
573 p
->desc
= talloc_strdup(p
, desc
);
577 m_config_set_profile_option(m_config_t
* config
, m_profile_t
* p
,
578 char* name
, char* val
) {
579 int i
= m_config_check_option(config
,name
,val
);
581 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2*(p
->num_opts
+2));
582 p
->opts
[p
->num_opts
*2] = talloc_strdup(p
, name
);
583 p
->opts
[p
->num_opts
*2+1] = talloc_strdup(p
, val
);
585 p
->opts
[p
->num_opts
*2] = p
->opts
[p
->num_opts
*2+1] = NULL
;
590 m_config_set_profile(m_config_t
* config
, m_profile_t
* p
) {
592 if(config
->profile_depth
> MAX_PROFILE_DEPTH
) {
593 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "WARNING: Profile inclusion too deep.\n");
596 int prev_mode
= config
->mode
;
597 config
->mode
= M_CONFIG_FILE
;
598 config
->profile_depth
++;
599 for(i
= 0 ; i
< p
->num_opts
; i
++)
600 m_config_set_option(config
,p
->opts
[2*i
],p
->opts
[2*i
+1]);
601 config
->profile_depth
--;
602 config
->mode
= prev_mode
;