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
,
74 if (!list
|| !list
[0])
76 for (int i
= 0; list
[i
]; i
++) {
77 struct m_profile
*p
= m_config_get_profile(config
, list
[i
]);
79 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "Unknown profile '%s'.\n",
83 m_config_set_profile(config
, p
);
85 m_option_free(opt
, &list
);
89 static int show_profile(struct m_option
*opt
, char *name
, char *param
)
91 struct m_config
*config
= opt
->priv
;
95 return M_OPT_MISSING_PARAM
;
96 if (!(p
= m_config_get_profile(config
, param
))) {
97 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
, "Unknown profile '%s'.\n", param
);
98 return M_OPT_EXIT
- 1;
100 if (!config
->profile_depth
)
101 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Profile %s: %s\n", param
,
102 p
->desc
? p
->desc
: "");
103 config
->profile_depth
++;
104 for (i
= 0; i
< p
->num_opts
; i
++) {
105 char spc
[config
->profile_depth
+ 1];
106 for (j
= 0; j
< config
->profile_depth
; j
++)
108 spc
[config
->profile_depth
] = '\0';
110 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
111 p
->opts
[2 * i
], p
->opts
[2 * i
+ 1]);
113 if (config
->profile_depth
< MAX_PROFILE_DEPTH
114 && !strcmp(p
->opts
[2*i
], "profile")) {
115 char *e
, *list
= p
->opts
[2 * i
+ 1];
116 while ((e
= strchr(list
, ','))) {
121 memcpy(tmp
, list
, l
);
123 show_profile(opt
, name
, tmp
);
127 show_profile(opt
, name
, list
);
130 config
->profile_depth
--;
131 if (!config
->profile_depth
)
132 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
133 return M_OPT_EXIT
- 1;
136 static int list_options(struct m_option
*opt
, char *name
, char *param
)
138 struct m_config
*config
= opt
->priv
;
139 m_config_print_option_list(config
);
143 static void m_option_save(const struct m_config
*config
,
144 const struct m_option
*opt
, void *dst
)
146 if (opt
->type
->copy
) {
147 const void *src
= m_option_get_ptr(opt
, config
->optstruct
);
148 opt
->type
->copy(opt
, dst
, src
, NULL
);
152 static void m_option_set(void *optstruct
,
153 const struct m_option
*opt
, const void *src
)
155 if (opt
->type
->copy
) {
156 void *dst
= m_option_get_ptr(opt
, optstruct
);
157 opt
->type
->copy(opt
, dst
, src
, optstruct
);
163 static void m_config_add_option(struct m_config
*config
,
164 const struct m_option
*arg
,
165 const char *prefix
, char *disabled_feature
);
167 struct m_config
*m_config_new(void *optstruct
,
168 int includefunc(struct m_config
*conf
,
171 struct m_config
*config
;
172 static const struct m_option ref_opts
[] = {
173 { "profile", NULL
, CONF_TYPE_STRING_LIST
, CONF_NOSAVE
, 0, 0, NULL
},
174 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
175 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
179 config
= talloc_zero(NULL
, struct m_config
);
181 config
->lvl
= 1; // 0 Is the defaults
182 struct m_option
*self_opts
= talloc_memdup(config
, ref_opts
,
184 for (int i
= 1; self_opts
[i
].name
; i
++)
185 self_opts
[i
].priv
= config
;
186 m_config_register_options(config
, self_opts
);
188 struct m_option
*p
= talloc_ptrtype(config
, p
);
189 *p
= (struct m_option
){
190 "include", NULL
, CONF_TYPE_STRING
, CONF_NOSAVE
,
192 m_config_add_option(config
, p
, NULL
, NULL
);
193 config
->includefunc
= includefunc
;
195 config
->optstruct
= optstruct
;
200 struct m_config
*m_config_simple(const struct m_option
*options
)
202 struct m_config
*config
= talloc_zero(NULL
, struct m_config
);
203 m_config_register_options(config
, options
);
207 void m_config_free(struct m_config
*config
)
209 assert(config
->full
); // use talloc_free() for simple
210 struct m_config_option
*copt
;
211 for (copt
= config
->opts
; copt
; copt
= copt
->next
) {
212 if (copt
->flags
& M_CFG_OPT_ALIAS
)
214 if (copt
->opt
->type
->flags
& M_OPT_TYPE_DYNAMIC
) {
215 void *ptr
= m_option_get_ptr(copt
->opt
, config
->optstruct
);
217 m_option_free(copt
->opt
, ptr
);
219 struct m_config_save_slot
*sl
;
220 for (sl
= copt
->slots
; sl
; sl
= sl
->prev
)
221 m_option_free(copt
->opt
, sl
->data
);
226 void m_config_initialize(struct m_config
*config
, void *optstruct
)
228 struct m_config_option
*copt
;
229 for (copt
= config
->opts
; copt
; copt
= copt
->next
) {
230 const struct m_option
*opt
= copt
->opt
;
233 m_option_set(optstruct
, opt
, opt
->defval
);
237 void m_config_push(struct m_config
*config
)
239 struct m_config_option
*co
;
240 struct m_config_save_slot
*slot
;
242 assert(config
!= NULL
);
243 assert(config
->lvl
> 0);
247 for (co
= config
->opts
; co
; co
= co
->next
) {
248 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
250 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
252 if (co
->flags
& M_CFG_OPT_ALIAS
)
255 // Update the current status
256 m_option_save(config
, co
->opt
, co
->slots
->data
);
258 // Allocate a new slot
259 slot
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
260 co
->opt
->type
->size
);
261 slot
->lvl
= config
->lvl
;
262 slot
->prev
= co
->slots
;
264 m_option_copy(co
->opt
, co
->slots
->data
, co
->slots
->prev
->data
);
265 // Reset our set flag
266 co
->flags
&= ~M_CFG_OPT_SET
;
269 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,
270 "Config pushed level is now %d\n", config
->lvl
);
273 void m_config_pop(struct m_config
*config
)
275 struct m_config_option
*co
;
276 struct m_config_save_slot
*slot
;
278 assert(config
!= NULL
);
279 assert(config
->lvl
> 1);
281 for (co
= config
->opts
; co
; co
= co
->next
) {
283 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
285 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
287 if (co
->flags
& M_CFG_OPT_ALIAS
)
289 if (co
->slots
->lvl
> config
->lvl
)
290 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,
291 "Save slot found from lvl %d is too old: %d !!!\n",
292 config
->lvl
, co
->slots
->lvl
);
294 while (co
->slots
->lvl
>= config
->lvl
) {
295 m_option_free(co
->opt
, co
->slots
->data
);
297 co
->slots
= slot
->prev
;
301 if (pop
) // We removed some ctx -> set the previous value
302 m_option_set(config
->optstruct
, co
->opt
, co
->slots
->data
);
306 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Config poped level=%d\n", config
->lvl
);
309 static void add_options(struct m_config
*config
, const struct m_option
*defs
,
310 const char *prefix
, char *disabled_feature
)
312 char *dis
= disabled_feature
;
313 const char marker
[] = "conditional functionality: ";
314 for (int i
= 0; defs
[i
].name
; i
++) {
315 if (!strncmp(defs
[i
].name
, marker
, strlen(marker
))) {
316 // If a subconfig entry itself is disabled, everything
317 // under it is already disabled for the same reason.
318 if (!disabled_feature
) {
319 if (!strcmp(defs
[i
].name
+ strlen(marker
), "1"))
326 m_config_add_option(config
, defs
+ i
, prefix
, dis
);
330 static void m_config_add_option(struct m_config
*config
,
331 const struct m_option
*arg
, const char *prefix
,
332 char *disabled_feature
)
334 struct m_config_option
*co
;
335 struct m_config_save_slot
*sl
;
337 assert(config
!= NULL
);
338 assert(config
->lvl
> 0 || !config
->full
);
341 // Allocate a new entry for this option
342 co
= talloc_zero_size(config
,
343 sizeof(struct m_config_option
) + arg
->type
->size
);
345 co
->disabled_feature
= disabled_feature
;
347 // Fill in the full name
348 if (prefix
&& *prefix
)
349 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
351 co
->name
= (char *)arg
->name
;
353 // Option with children -> add them
354 if (arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
355 add_options(config
, arg
->p
, co
->name
, disabled_feature
);
357 struct m_config_option
*i
;
358 // Check if there is already an option pointing to this address
359 if (arg
->p
|| arg
->new && arg
->offset
>= 0) {
360 for (i
= config
->opts
; i
; i
= i
->next
) {
361 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
362 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
363 // So we don't save the same vars more than 1 time
364 co
->slots
= i
->slots
;
365 co
->flags
|= M_CFG_OPT_ALIAS
;
370 if (config
->full
&& !(co
->flags
& M_CFG_OPT_ALIAS
)) {
371 // Allocate a slot for the defaults
372 sl
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
374 m_option_save(config
, arg
, sl
->data
);
375 // Hack to avoid too much trouble with dynamically allocated data:
376 // We replace original default and always use a dynamic version
377 if (!arg
->new && (arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
378 char **hackptr
= m_option_get_ptr(arg
, config
->optstruct
);
379 if (hackptr
&& *hackptr
) {
381 m_option_set(config
->optstruct
, arg
, sl
->data
);
386 co
->slots
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
388 co
->slots
->prev
= sl
;
389 co
->slots
->lvl
= config
->lvl
;
390 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
393 co
->next
= config
->opts
;
397 int m_config_register_options(struct m_config
*config
,
398 const struct m_option
*args
)
400 assert(config
!= NULL
);
401 assert(config
->lvl
> 0 || !config
->full
);
402 assert(args
!= NULL
);
404 add_options(config
, args
, NULL
, NULL
);
409 static struct m_config_option
*m_config_get_co(const struct m_config
*config
,
412 struct m_config_option
*co
;
414 for (co
= config
->opts
; co
; co
= co
->next
) {
415 struct bstr coname
= bstr(co
->name
);
416 if ((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
)
417 && bstr_endswith0(coname
, "*")) {
419 if (bstrcasecmp(bstr_splice(name
, 0, coname
.len
), coname
) == 0)
421 } else if (bstrcasecmp(coname
, name
) == 0)
427 static int parse_subopts(struct m_config
*config
, void *optstruct
, char *name
,
428 char *prefix
, struct bstr param
, bool set
);
430 static int m_config_parse_option(struct m_config
*config
, void *optstruct
,
431 struct bstr name
, struct bstr param
,
432 bool ambiguous_param
, bool set
)
434 assert(config
!= NULL
);
435 assert(config
->lvl
> 0 || !config
->full
);
436 assert(name
.len
!= 0);
438 struct m_config_option
*co
= m_config_get_co(config
, name
);
440 return M_OPT_UNKNOWN
;
441 if (co
->disabled_feature
) {
442 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
443 "Option \"%.*s\" is not available in this version of mplayer2, "
444 "because it has been compiled with feature \"%s\" disabled.\n",
445 BSTR_P(name
), co
->disabled_feature
);
446 return M_OPT_UNKNOWN
;
449 // This is the only mandatory function
450 assert(co
->opt
->type
->parse
);
452 // Check if this option isn't forbidden in the current mode
453 if ((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
454 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
455 "The %.*s option can't be used in a config file.\n",
457 return M_OPT_INVALID
;
459 if ((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
460 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
461 "The %.*s option can't be used on the command line.\n",
463 return M_OPT_INVALID
;
465 // During command line preparse set only pre-parse options
466 // Otherwise only set pre-parse option if they were not already set.
467 if (((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
468 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
469 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
470 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
473 if (config
->includefunc
&& !bstrcmp0(name
, "include")) {
474 return parse_include(config
, param
, set
);
475 } else if (!bstrcmp0(name
, "profile"))
476 return parse_profile(config
, co
->opt
, name
, param
, set
);
478 // Option with children are a bit different to parse
479 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
481 assert(strlen(co
->name
) < 100);
482 sprintf(prefix
, "%s:", co
->name
);
483 return parse_subopts(config
, optstruct
, co
->name
, prefix
, param
, set
);
486 void *dst
= set
? m_option_get_ptr(co
->opt
, optstruct
) : NULL
;
487 int r
= co
->opt
->type
->parse(co
->opt
, name
, param
, ambiguous_param
, dst
,
493 co
->flags
|= M_CFG_OPT_SET
;
498 static int parse_subopts(struct m_config
*config
, void *optstruct
, char *name
,
499 char *prefix
, struct bstr param
, bool set
)
502 // Split the argument into child options
503 int r
= m_option_type_subconfig
.parse(NULL
, bstr(""), param
, false, &lst
,
507 // Parse the child options
508 for (int i
= 0; lst
&& lst
[2 * i
]; i
++) {
509 // Build the full name
511 if (snprintf(n
, 110, "%s%s", prefix
, lst
[2 * i
]) > 100)
513 if (!m_config_get_option(config
, bstr(n
))) {
514 if (strncmp(lst
[2 * i
], "no-", 3))
516 snprintf(n
, 110, "%s%s", prefix
, lst
[2 * i
] + 3);
517 const struct m_option
*o
= m_config_get_option(config
, bstr(n
));
518 if (!o
|| o
->type
!= &m_option_type_flag
) {
520 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
521 "Error: option '%s' has no suboption '%s'.\n",
526 if (lst
[2 * i
+ 1]) {
527 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
528 "A --no-* option can't take parameters: "
529 "%s=%s\n", lst
[2 * i
], lst
[2 * i
+ 1]);
533 lst
[2 * i
+ 1] = "no";
535 int sr
= m_config_parse_option(config
, optstruct
, bstr(n
),
536 bstr(lst
[2 * i
+ 1]), false, set
);
538 if (sr
== M_OPT_MISSING_PARAM
) {
539 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
540 "Error: suboption '%s' of '%s' must have "
541 "a parameter!\n", lst
[2 * i
], name
);
552 int m_config_set_option(struct m_config
*config
, struct bstr name
,
553 struct bstr param
, bool ambiguous_param
)
555 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Setting %.*s=%.*s\n", BSTR_P(name
),
557 return m_config_parse_option(config
, config
->optstruct
, name
, param
,
558 ambiguous_param
, true);
561 int m_config_check_option(struct m_config
*config
, struct bstr name
,
562 struct bstr param
, bool ambiguous_param
)
565 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Checking %.*s=%.*s\n", BSTR_P(name
),
567 r
= m_config_parse_option(config
, NULL
, name
, param
, ambiguous_param
, 0);
568 if (r
== M_OPT_MISSING_PARAM
) {
569 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
570 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name
));
571 return M_OPT_INVALID
;
576 int m_config_parse_suboptions(struct m_config
*config
, void *optstruct
,
577 char *name
, char *subopts
)
579 if (!subopts
|| !*subopts
)
581 return parse_subopts(config
, optstruct
, name
, "", bstr(subopts
), true);
585 const struct m_option
*m_config_get_option(const struct m_config
*config
,
588 struct m_config_option
*co
;
590 assert(config
!= NULL
);
591 assert(config
->lvl
> 0 || !config
->full
);
593 co
= m_config_get_co(config
, name
);
600 void m_config_print_option_list(const struct m_config
*config
)
602 char min
[50], max
[50];
603 struct m_config_option
*co
;
609 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
610 "\n Name Type Min Max Global CL Cfg\n\n");
611 for (co
= config
->opts
; co
; co
= co
->next
) {
612 const struct m_option
*opt
= co
->opt
;
613 if (opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
615 if (opt
->flags
& M_OPT_MIN
)
616 sprintf(min
, "%-8.0f", opt
->min
);
619 if (opt
->flags
& M_OPT_MAX
)
620 sprintf(max
, "%-8.0f", opt
->max
);
623 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
,
624 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
629 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
630 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
631 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
634 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n", count
);
637 struct m_profile
*m_config_get_profile(const struct m_config
*config
,
641 for (p
= config
->profiles
; p
; p
= p
->next
)
642 if (!strcmp(p
->name
, name
))
647 struct m_profile
*m_config_add_profile(struct m_config
*config
, char *name
)
649 struct m_profile
*p
= m_config_get_profile(config
, name
);
652 p
= talloc_zero(config
, struct m_profile
);
653 p
->name
= talloc_strdup(p
, name
);
654 p
->next
= config
->profiles
;
655 config
->profiles
= p
;
659 void m_profile_set_desc(struct m_profile
*p
, char *desc
)
661 talloc_free(p
->desc
);
662 p
->desc
= talloc_strdup(p
, desc
);
665 int m_config_set_profile_option(struct m_config
*config
, struct m_profile
*p
,
666 char *name
, char *val
)
668 int i
= m_config_check_option0(config
, name
, val
, false);
671 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2 * (p
->num_opts
+ 2));
672 p
->opts
[p
->num_opts
* 2] = talloc_strdup(p
, name
);
673 p
->opts
[p
->num_opts
* 2 + 1] = talloc_strdup(p
, val
);
675 p
->opts
[p
->num_opts
* 2] = p
->opts
[p
->num_opts
* 2 + 1] = NULL
;
679 void m_config_set_profile(struct m_config
*config
, struct m_profile
*p
)
682 if (config
->profile_depth
> MAX_PROFILE_DEPTH
) {
683 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,
684 "WARNING: Profile inclusion too deep.\n");
687 int prev_mode
= config
->mode
;
688 config
->mode
= M_CONFIG_FILE
;
689 config
->profile_depth
++;
690 for (i
= 0; i
< p
->num_opts
; i
++)
691 m_config_set_option0(config
, p
->opts
[2 * i
], p
->opts
[2 * i
+ 1], false);
692 config
->profile_depth
--;
693 config
->mode
= prev_mode
;