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_include(struct m_config
*config
, struct bstr param
, bool set
)
42 return M_OPT_MISSING_PARAM
;
45 char *filename
= bstrdup0(NULL
, param
);
46 config
->includefunc(config
, filename
);
47 talloc_free(filename
);
51 static int parse_profile(struct m_config
*config
, const struct m_option
*opt
,
52 struct bstr name
, struct bstr param
, bool set
)
54 if (!bstrcmp0(param
, "help")) {
56 if (!config
->profiles
) {
57 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
58 "No profiles have been defined.\n");
59 return M_OPT_EXIT
- 1;
61 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Available profiles:\n");
62 for (p
= config
->profiles
; p
; p
= p
->next
)
63 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n", p
->name
,
64 p
->desc
? p
->desc
: "");
65 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
66 return M_OPT_EXIT
- 1;
70 int r
= m_option_type_string_list
.parse(opt
, name
, param
, false, &list
);
73 if (!list
|| !list
[0])
75 for (int i
= 0; list
[i
]; i
++) {
76 struct m_profile
*p
= m_config_get_profile(config
, list
[i
]);
78 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "Unknown profile '%s'.\n",
82 m_config_set_profile(config
, p
);
84 m_option_free(opt
, &list
);
88 static int show_profile(struct m_option
*opt
, char *name
, char *param
)
90 struct m_config
*config
= opt
->priv
;
94 return M_OPT_MISSING_PARAM
;
95 if (!(p
= m_config_get_profile(config
, param
))) {
96 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
, "Unknown profile '%s'.\n", param
);
97 return M_OPT_EXIT
- 1;
99 if (!config
->profile_depth
)
100 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Profile %s: %s\n", param
,
101 p
->desc
? p
->desc
: "");
102 config
->profile_depth
++;
103 for (i
= 0; i
< p
->num_opts
; i
++) {
104 char spc
[config
->profile_depth
+ 1];
105 for (j
= 0; j
< config
->profile_depth
; j
++)
107 spc
[config
->profile_depth
] = '\0';
109 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
110 p
->opts
[2 * i
], p
->opts
[2 * i
+ 1]);
112 if (config
->profile_depth
< MAX_PROFILE_DEPTH
113 && !strcmp(p
->opts
[2*i
], "profile")) {
114 char *e
, *list
= p
->opts
[2 * i
+ 1];
115 while ((e
= strchr(list
, ','))) {
120 memcpy(tmp
, list
, l
);
122 show_profile(opt
, name
, tmp
);
126 show_profile(opt
, name
, list
);
129 config
->profile_depth
--;
130 if (!config
->profile_depth
)
131 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
132 return M_OPT_EXIT
- 1;
135 static int list_options(struct m_option
*opt
, char *name
, char *param
)
137 struct m_config
*config
= opt
->priv
;
138 m_config_print_option_list(config
);
142 static void m_option_save(const struct m_config
*config
,
143 const struct m_option
*opt
, void *dst
)
145 if (opt
->type
->copy
) {
146 const void *src
= m_option_get_ptr(opt
, config
->optstruct
);
147 opt
->type
->copy(opt
, dst
, src
);
151 static void m_option_set(void *optstruct
,
152 const struct m_option
*opt
, const void *src
)
154 if (opt
->type
->copy
) {
155 void *dst
= m_option_get_ptr(opt
, optstruct
);
156 opt
->type
->copy(opt
, dst
, src
);
162 static void m_config_add_option(struct m_config
*config
,
163 const struct m_option
*arg
,
164 const char *prefix
, char *disabled_feature
);
166 struct m_config
*m_config_new(void *optstruct
,
167 int includefunc(struct m_config
*conf
,
170 struct m_config
*config
;
171 static const struct m_option ref_opts
[] = {
172 { "profile", NULL
, CONF_TYPE_STRING_LIST
, CONF_NOSAVE
, 0, 0, NULL
},
173 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
174 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
178 config
= talloc_zero(NULL
, struct m_config
);
180 config
->lvl
= 1; // 0 Is the defaults
181 struct m_option
*self_opts
= talloc_memdup(config
, ref_opts
,
183 for (int i
= 1; self_opts
[i
].name
; i
++)
184 self_opts
[i
].priv
= config
;
185 m_config_register_options(config
, self_opts
);
187 struct m_option
*p
= talloc_ptrtype(config
, p
);
188 *p
= (struct m_option
){
189 "include", NULL
, CONF_TYPE_STRING
, CONF_NOSAVE
,
191 m_config_add_option(config
, p
, NULL
, NULL
);
192 config
->includefunc
= includefunc
;
194 config
->optstruct
= optstruct
;
199 struct m_config
*m_config_simple(const struct m_option
*options
)
201 struct m_config
*config
= talloc_zero(NULL
, struct m_config
);
202 m_config_register_options(config
, options
);
206 void m_config_free(struct m_config
*config
)
208 assert(config
->full
); // use talloc_free() for simple
209 struct m_config_option
*copt
;
210 for (copt
= config
->opts
; copt
; copt
= copt
->next
) {
211 if (copt
->flags
& M_CFG_OPT_ALIAS
)
213 if (copt
->opt
->type
->flags
& M_OPT_TYPE_DYNAMIC
) {
214 void *ptr
= m_option_get_ptr(copt
->opt
, config
->optstruct
);
216 m_option_free(copt
->opt
, ptr
);
218 struct m_config_save_slot
*sl
;
219 for (sl
= copt
->slots
; sl
; sl
= sl
->prev
)
220 m_option_free(copt
->opt
, sl
->data
);
225 void m_config_initialize(struct m_config
*config
, void *optstruct
)
227 struct m_config_option
*copt
;
228 for (copt
= config
->opts
; copt
; copt
= copt
->next
) {
229 const struct m_option
*opt
= copt
->opt
;
232 m_option_set(optstruct
, opt
, opt
->defval
);
236 void m_config_push(struct m_config
*config
)
238 struct m_config_option
*co
;
239 struct m_config_save_slot
*slot
;
241 assert(config
!= NULL
);
242 assert(config
->lvl
> 0);
246 for (co
= config
->opts
; co
; co
= co
->next
) {
247 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
249 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
251 if (co
->flags
& M_CFG_OPT_ALIAS
)
254 // Update the current status
255 m_option_save(config
, co
->opt
, co
->slots
->data
);
257 // Allocate a new slot
258 slot
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
259 co
->opt
->type
->size
);
260 slot
->lvl
= config
->lvl
;
261 slot
->prev
= co
->slots
;
263 m_option_copy(co
->opt
, co
->slots
->data
, co
->slots
->prev
->data
);
264 // Reset our set flag
265 co
->flags
&= ~M_CFG_OPT_SET
;
268 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,
269 "Config pushed level is now %d\n", config
->lvl
);
272 void m_config_pop(struct m_config
*config
)
274 struct m_config_option
*co
;
275 struct m_config_save_slot
*slot
;
277 assert(config
!= NULL
);
278 assert(config
->lvl
> 1);
280 for (co
= config
->opts
; co
; co
= co
->next
) {
282 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
284 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
286 if (co
->flags
& M_CFG_OPT_ALIAS
)
288 if (co
->slots
->lvl
> config
->lvl
)
289 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,
290 "Save slot found from lvl %d is too old: %d !!!\n",
291 config
->lvl
, co
->slots
->lvl
);
293 while (co
->slots
->lvl
>= config
->lvl
) {
294 m_option_free(co
->opt
, co
->slots
->data
);
296 co
->slots
= slot
->prev
;
300 if (pop
) // We removed some ctx -> set the previous value
301 m_option_set(config
->optstruct
, co
->opt
, co
->slots
->data
);
305 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Config poped level=%d\n", config
->lvl
);
308 static void add_options(struct m_config
*config
, const struct m_option
*defs
,
309 const char *prefix
, char *disabled_feature
)
311 char *dis
= disabled_feature
;
312 const char marker
[] = "conditional functionality: ";
313 for (int i
= 0; defs
[i
].name
; i
++) {
314 if (!strncmp(defs
[i
].name
, marker
, strlen(marker
))) {
315 // If a subconfig entry itself is disabled, everything
316 // under it is already disabled for the same reason.
317 if (!disabled_feature
) {
318 if (!strcmp(defs
[i
].name
+ strlen(marker
), "1"))
325 m_config_add_option(config
, defs
+ i
, prefix
, dis
);
329 static void m_config_add_option(struct m_config
*config
,
330 const struct m_option
*arg
, const char *prefix
,
331 char *disabled_feature
)
333 struct m_config_option
*co
;
334 struct m_config_save_slot
*sl
;
336 assert(config
!= NULL
);
337 assert(config
->lvl
> 0 || !config
->full
);
340 // Allocate a new entry for this option
341 co
= talloc_zero_size(config
,
342 sizeof(struct m_config_option
) + arg
->type
->size
);
344 co
->disabled_feature
= disabled_feature
;
346 // Fill in the full name
347 if (prefix
&& *prefix
)
348 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
350 co
->name
= (char *)arg
->name
;
352 // Option with children -> add them
353 if (arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
354 add_options(config
, arg
->p
, co
->name
, disabled_feature
);
356 struct m_config_option
*i
;
357 // Check if there is already an option pointing to this address
358 if (arg
->p
|| arg
->new && arg
->offset
>= 0) {
359 for (i
= config
->opts
; i
; i
= i
->next
) {
360 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
361 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
362 // So we don't save the same vars more than 1 time
363 co
->slots
= i
->slots
;
364 co
->flags
|= M_CFG_OPT_ALIAS
;
369 if (config
->full
&& !(co
->flags
& M_CFG_OPT_ALIAS
)) {
370 // Allocate a slot for the defaults
371 sl
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
373 m_option_save(config
, arg
, sl
->data
);
374 // Hack to avoid too much trouble with dynamically allocated data:
375 // We replace original default and always use a dynamic version
376 if (!arg
->new && (arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
377 char **hackptr
= m_option_get_ptr(arg
, config
->optstruct
);
378 if (hackptr
&& *hackptr
) {
380 m_option_set(config
->optstruct
, arg
, sl
->data
);
385 co
->slots
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
387 co
->slots
->prev
= sl
;
388 co
->slots
->lvl
= config
->lvl
;
389 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
392 co
->next
= config
->opts
;
396 int m_config_register_options(struct m_config
*config
,
397 const struct m_option
*args
)
399 assert(config
!= NULL
);
400 assert(config
->lvl
> 0 || !config
->full
);
401 assert(args
!= NULL
);
403 add_options(config
, args
, NULL
, NULL
);
408 static struct m_config_option
*m_config_get_co(const struct m_config
*config
,
411 struct m_config_option
*co
;
413 for (co
= config
->opts
; co
; co
= co
->next
) {
414 struct bstr coname
= bstr(co
->name
);
415 if ((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
)
416 && bstr_endswith0(coname
, "*")) {
418 if (bstrcasecmp(bstr_splice(name
, 0, coname
.len
), coname
) == 0)
420 } else if (bstrcasecmp(coname
, name
) == 0)
426 static int parse_subopts(struct m_config
*config
, void *optstruct
, char *name
,
427 char *prefix
, struct bstr param
, bool set
);
429 static int m_config_parse_option(struct m_config
*config
, void *optstruct
,
430 struct bstr name
, struct bstr param
,
431 bool ambiguous_param
, bool set
)
433 assert(config
!= NULL
);
434 assert(config
->lvl
> 0 || !config
->full
);
435 assert(name
.len
!= 0);
437 struct m_config_option
*co
= m_config_get_co(config
, name
);
439 return M_OPT_UNKNOWN
;
440 if (co
->disabled_feature
) {
441 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
442 "Option \"%.*s\" is not available in this version of mplayer2, "
443 "because it has been compiled with feature \"%s\" disabled.\n",
444 BSTR_P(name
), co
->disabled_feature
);
445 return M_OPT_UNKNOWN
;
448 // This is the only mandatory function
449 assert(co
->opt
->type
->parse
);
451 // Check if this option isn't forbidden in the current mode
452 if ((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
453 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
454 "The %.*s option can't be used in a config file.\n",
456 return M_OPT_INVALID
;
458 if ((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
459 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
460 "The %.*s option can't be used on the command line.\n",
462 return M_OPT_INVALID
;
464 // During command line preparse set only pre-parse options
465 // Otherwise only set pre-parse option if they were not already set.
466 if (((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
467 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
468 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
469 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
472 if (config
->includefunc
&& !bstrcmp0(name
, "include")) {
473 return parse_include(config
, param
, set
);
474 } else if (!bstrcmp0(name
, "profile"))
475 return parse_profile(config
, co
->opt
, name
, param
, set
);
477 // Option with children are a bit different to parse
478 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
480 assert(strlen(co
->name
) < 100);
481 sprintf(prefix
, "%s:", co
->name
);
482 return parse_subopts(config
, optstruct
, co
->name
, prefix
, param
, set
);
485 void *dst
= set
? m_option_get_ptr(co
->opt
, optstruct
) : NULL
;
486 int r
= m_option_parse(co
->opt
, name
, param
, ambiguous_param
, dst
);
491 co
->flags
|= M_CFG_OPT_SET
;
496 static int parse_subopts(struct m_config
*config
, void *optstruct
, char *name
,
497 char *prefix
, struct bstr param
, bool set
)
500 // Split the argument into child options
501 int r
= m_option_type_subconfig
.parse(NULL
, bstr(""), param
, false, &lst
);
504 // Parse the child options
505 for (int i
= 0; lst
&& lst
[2 * i
]; i
++) {
506 // Build the full name
508 if (snprintf(n
, 110, "%s%s", prefix
, lst
[2 * i
]) > 100)
510 int sr
= m_config_parse_option(config
, optstruct
, bstr(n
),
511 bstr(lst
[2 * i
+ 1]), false, set
);
513 if (sr
== M_OPT_UNKNOWN
) {
514 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
515 "Error: option '%s' has no suboption '%s'.\n",
518 } else if (sr
== M_OPT_MISSING_PARAM
) {
519 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
520 "Error: suboption '%s' of '%s' must have "
521 "a parameter!\n", lst
[2 * i
], name
);
532 int m_config_set_option(struct m_config
*config
, struct bstr name
,
533 struct bstr param
, bool ambiguous_param
)
535 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Setting %.*s=%.*s\n", BSTR_P(name
),
537 return m_config_parse_option(config
, config
->optstruct
, name
, param
,
538 ambiguous_param
, true);
541 int m_config_check_option(struct m_config
*config
, struct bstr name
,
542 struct bstr param
, bool ambiguous_param
)
545 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Checking %.*s=%.*s\n", BSTR_P(name
),
547 r
= m_config_parse_option(config
, NULL
, name
, param
, ambiguous_param
, 0);
548 if (r
== M_OPT_MISSING_PARAM
) {
549 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
550 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name
));
551 return M_OPT_INVALID
;
556 int m_config_parse_suboptions(struct m_config
*config
, void *optstruct
,
557 char *name
, char *subopts
)
559 if (!subopts
|| !*subopts
)
561 return parse_subopts(config
, optstruct
, name
, "", bstr(subopts
), true);
565 const struct m_option
*m_config_get_option(const struct m_config
*config
,
568 struct m_config_option
*co
;
570 assert(config
!= NULL
);
571 assert(config
->lvl
> 0);
573 co
= m_config_get_co(config
, name
);
580 void m_config_print_option_list(const struct m_config
*config
)
582 char min
[50], max
[50];
583 struct m_config_option
*co
;
589 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
590 "\n Name Type Min Max Global CL Cfg\n\n");
591 for (co
= config
->opts
; co
; co
= co
->next
) {
592 const struct m_option
*opt
= co
->opt
;
593 if (opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
595 if (opt
->flags
& M_OPT_MIN
)
596 sprintf(min
, "%-8.0f", opt
->min
);
599 if (opt
->flags
& M_OPT_MAX
)
600 sprintf(max
, "%-8.0f", opt
->max
);
603 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
,
604 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
609 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
610 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
611 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
614 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n", count
);
617 struct m_profile
*m_config_get_profile(const struct m_config
*config
,
621 for (p
= config
->profiles
; p
; p
= p
->next
)
622 if (!strcmp(p
->name
, name
))
627 struct m_profile
*m_config_add_profile(struct m_config
*config
, char *name
)
629 struct m_profile
*p
= m_config_get_profile(config
, name
);
632 p
= talloc_zero(config
, struct m_profile
);
633 p
->name
= talloc_strdup(p
, name
);
634 p
->next
= config
->profiles
;
635 config
->profiles
= p
;
639 void m_profile_set_desc(struct m_profile
*p
, char *desc
)
641 talloc_free(p
->desc
);
642 p
->desc
= talloc_strdup(p
, desc
);
645 int m_config_set_profile_option(struct m_config
*config
, struct m_profile
*p
,
646 char *name
, char *val
)
648 int i
= m_config_check_option0(config
, name
, val
, false);
651 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2 * (p
->num_opts
+ 2));
652 p
->opts
[p
->num_opts
* 2] = talloc_strdup(p
, name
);
653 p
->opts
[p
->num_opts
* 2 + 1] = talloc_strdup(p
, val
);
655 p
->opts
[p
->num_opts
* 2] = p
->opts
[p
->num_opts
* 2 + 1] = NULL
;
659 void m_config_set_profile(struct m_config
*config
, struct m_profile
*p
)
662 if (config
->profile_depth
> MAX_PROFILE_DEPTH
) {
663 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,
664 "WARNING: Profile inclusion too deep.\n");
667 int prev_mode
= config
->mode
;
668 config
->mode
= M_CONFIG_FILE
;
669 config
->profile_depth
++;
670 for (i
= 0; i
< p
->num_opts
; i
++)
671 m_config_set_option0(config
, p
->opts
[2 * i
], p
->opts
[2 * i
+ 1], false);
672 config
->profile_depth
--;
673 config
->mode
= prev_mode
;