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
= opt
->new ? (char*)config
->optstruct
+ opt
->offset
: opt
->p
;
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
= opt
->new ? (char*)config
->optstruct
+ opt
->offset
: opt
->p
;
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
->opt
->flags
& M_OPT_OLD
) && !(co
->flags
& M_CFG_OPT_SET
))
241 if(co
->flags
& M_CFG_OPT_ALIAS
)
244 // Update the current status
245 m_option_save(config
, co
->opt
, co
->slots
->data
);
247 // Allocate a new slot
248 slot
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
249 co
->opt
->type
->size
);
250 slot
->lvl
= config
->lvl
;
251 slot
->prev
= co
->slots
;
253 m_option_copy(co
->opt
,co
->slots
->data
,co
->slots
->prev
->data
);
254 // Reset our set flag
255 co
->flags
&= ~M_CFG_OPT_SET
;
258 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config pushed level is now %d\n",config
->lvl
);
262 m_config_pop(m_config_t
* config
) {
263 m_config_option_t
*co
;
264 m_config_save_slot_t
*slot
;
267 assert(config
!= NULL
);
268 assert(config
->lvl
> 1);
271 for(co
= config
->opts
; co
; co
= co
->next
) {
273 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
275 if(co
->opt
->flags
& (M_OPT_GLOBAL
|M_OPT_NOSAVE
))
277 if(co
->flags
& M_CFG_OPT_ALIAS
)
279 if(co
->slots
->lvl
> config
->lvl
)
280 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,"Save slot found from lvl %d is too old: %d !!!\n",config
->lvl
,co
->slots
->lvl
);
282 while(co
->slots
->lvl
>= config
->lvl
) {
283 m_option_free(co
->opt
,co
->slots
->data
);
285 co
->slots
= slot
->prev
;
289 if(pop
) // We removed some ctx -> set the previous value
290 m_option_set(config
, co
->opt
, co
->slots
->data
);
294 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Config poped level=%d\n",config
->lvl
);
298 m_config_add_option(m_config_t
*config
, const m_option_t
*arg
, const char* prefix
) {
299 m_config_option_t
*co
;
300 m_config_save_slot_t
* sl
;
303 assert(config
!= NULL
);
304 assert(config
->lvl
> 0);
308 // Allocate a new entry for this option
309 co
= talloc_zero_size(config
, sizeof(m_config_option_t
) + arg
->type
->size
);
312 // Fill in the full name
313 if(prefix
&& strlen(prefix
) > 0) {
314 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
316 co
->name
= arg
->name
;
318 // Option with children -> add them
319 if(arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
320 const m_option_t
*ol
= arg
->p
;
323 for(i
= 0 ; ol
[i
].name
!= NULL
; i
++)
324 m_config_add_option(config
,&ol
[i
], co
->name
);
326 m_config_option_t
*i
;
327 // Check if there is already an option pointing to this address
328 if(arg
->p
|| arg
->new && arg
->offset
>= 0) {
329 for(i
= config
->opts
; i
; i
= i
->next
) {
330 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
331 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
332 // So we don't save the same vars more than 1 time
333 co
->slots
= i
->slots
;
334 co
->flags
|= M_CFG_OPT_ALIAS
;
339 if(!(co
->flags
& M_CFG_OPT_ALIAS
)) {
340 // Allocate a slot for the defaults
341 sl
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
343 m_option_save(config
, arg
, sl
->data
);
344 // Hack to avoid too much trouble with dynamically allocated data :
345 // We always use a dynamic version
346 if ((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
347 char **hackptr
= arg
->new ? (char*)config
->optstruct
+ arg
->offset
349 if (hackptr
&& *hackptr
) {
351 m_option_set(config
, arg
, sl
->data
);
356 co
->slots
= talloc_zero_size(co
, sizeof(m_config_save_slot_t
) +
358 co
->slots
->prev
= sl
;
359 co
->slots
->lvl
= config
->lvl
;
360 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
363 co
->next
= config
->opts
;
368 m_config_register_options(m_config_t
*config
, const m_option_t
*args
) {
372 assert(config
!= NULL
);
373 assert(config
->lvl
> 0);
374 assert(args
!= NULL
);
377 for(i
= 0 ; args
[i
].name
!= NULL
; i
++)
378 m_config_add_option(config
,&args
[i
],NULL
);
383 static m_config_option_t
*
384 m_config_get_co(const m_config_t
*config
, char *arg
) {
385 m_config_option_t
*co
;
387 for(co
= config
->opts
; co
; co
= co
->next
) {
388 int l
= strlen(co
->name
) - 1;
389 if((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
390 (co
->name
[l
] == '*')) {
391 if(strncasecmp(co
->name
,arg
,l
) == 0)
393 } else if(strcasecmp(co
->name
,arg
) == 0)
400 m_config_parse_option(const m_config_t
*config
, char *arg
, char *param
, int set
) {
401 m_config_option_t
*co
;
405 assert(config
!= NULL
);
406 assert(config
->lvl
> 0);
410 co
= m_config_get_co(config
,arg
);
412 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
413 return M_OPT_UNKNOWN
;
417 // This is the only mandatory function
418 assert(co
->opt
->type
->parse
);
421 // Check if this option isn't forbidden in the current mode
422 if((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
423 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used in a config file.\n",arg
);
424 return M_OPT_INVALID
;
426 if((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
427 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"The %s option can't be used on the command line.\n",arg
);
428 return M_OPT_INVALID
;
430 // During command line preparse set only pre-parse options
431 // Otherwise only set pre-parse option if they were not already set.
432 if(((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
433 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
434 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
435 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
438 // Option with children are a bit different to parse
439 if(co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
442 // Parse the child options
443 r
= m_option_parse(co
->opt
,arg
,param
,&lst
,M_COMMAND_LINE
);
446 for(i
= 0 ; lst
&& lst
[2*i
] ; i
++) {
447 int l
= strlen(co
->name
) + 1 + strlen(lst
[2*i
]) + 1;
449 // Build the full name
451 sprintf(n
,"%s:%s",co
->name
,lst
[2*i
]);
452 sr
= m_config_parse_option(config
,n
,lst
[2*i
+1],set
);
454 if(sr
== M_OPT_UNKNOWN
){
455 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' has no suboption '%s'.\n",co
->name
,lst
[2*i
]);
458 if(sr
== M_OPT_MISSING_PARAM
){
459 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: suboption '%s' of '%s' must have a parameter!\n",lst
[2*i
],co
->name
);
470 r
= m_option_parse(co
->opt
,arg
,param
,set
? co
->slots
->data
: NULL
,config
->mode
);
477 m_option_set(config
, co
->opt
, co
->slots
->data
);
478 co
->flags
|= M_CFG_OPT_SET
;
485 m_config_set_option(m_config_t
*config
, char* arg
, char* param
) {
486 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Setting %s=%s\n",arg
,param
);
487 return m_config_parse_option(config
,arg
,param
,1);
491 m_config_check_option(const m_config_t
*config
, char *arg
, char *param
) {
493 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Checking %s=%s\n",arg
,param
);
494 r
=m_config_parse_option(config
,arg
,param
,0);
495 if(r
==M_OPT_MISSING_PARAM
){
496 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,"Error: option '%s' must have a parameter!\n",arg
);
497 return M_OPT_INVALID
;
504 m_config_get_option(const m_config_t
*config
, char *arg
) {
505 m_config_option_t
*co
;
508 assert(config
!= NULL
);
509 assert(config
->lvl
> 0);
513 co
= m_config_get_co(config
,arg
);
521 m_config_print_option_list(const m_config_t
*config
) {
522 char min
[50],max
[50];
523 m_config_option_t
* co
;
526 if(!config
->opts
) return;
528 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\n Name Type Min Max Global CL Cfg\n\n");
529 for(co
= config
->opts
; co
; co
= co
->next
) {
530 const m_option_t
* opt
= co
->opt
;
531 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
532 if(opt
->flags
& M_OPT_MIN
)
533 sprintf(min
,"%-8.0f",opt
->min
);
536 if(opt
->flags
& M_OPT_MAX
)
537 sprintf(max
,"%-8.0f",opt
->max
);
540 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
545 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
546 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
547 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
550 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n",count
);
554 m_config_get_profile(const m_config_t
*config
, char *name
) {
556 for(p
= config
->profiles
; p
; p
= p
->next
)
557 if(!strcmp(p
->name
,name
)) return p
;
562 m_config_add_profile(m_config_t
* config
, char* name
) {
563 m_profile_t
* p
= m_config_get_profile(config
,name
);
565 p
= talloc_zero(config
, m_profile_t
);
566 p
->name
= talloc_strdup(p
, name
);
567 p
->next
= config
->profiles
;
568 config
->profiles
= p
;
573 m_profile_set_desc(m_profile_t
* p
, char* desc
) {
574 talloc_free(p
->desc
);
575 p
->desc
= talloc_strdup(p
, desc
);
579 m_config_set_profile_option(m_config_t
* config
, m_profile_t
* p
,
580 char* name
, char* val
) {
581 int i
= m_config_check_option(config
,name
,val
);
583 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2*(p
->num_opts
+2));
584 p
->opts
[p
->num_opts
*2] = talloc_strdup(p
, name
);
585 p
->opts
[p
->num_opts
*2+1] = talloc_strdup(p
, val
);
587 p
->opts
[p
->num_opts
*2] = p
->opts
[p
->num_opts
*2+1] = NULL
;
592 m_config_set_profile(m_config_t
* config
, m_profile_t
* p
) {
594 if(config
->profile_depth
> MAX_PROFILE_DEPTH
) {
595 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "WARNING: Profile inclusion too deep.\n");
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
--;