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 struct m_option
*opt
, struct bstr name
,
40 struct bstr param
, bool ambiguous_param
, void *dst
)
42 struct m_config
*config
= opt
->priv
;
45 if (!bstrcmp0(param
, "help")) {
47 if (!config
->profiles
) {
48 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
49 "No profiles have been defined.\n");
50 return M_OPT_EXIT
- 1;
52 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Available profiles:\n");
53 for (p
= config
->profiles
; p
; p
= p
->next
)
54 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\t%s\t%s\n", p
->name
,
55 p
->desc
? p
->desc
: "");
56 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
57 return M_OPT_EXIT
- 1;
60 r
= m_option_type_string_list
.parse(opt
, name
, param
, false, &list
);
63 if (!list
|| !list
[0])
65 for (i
= 0; list
[i
]; i
++)
66 if (!m_config_get_profile(config
, list
[i
])) {
67 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
, "Unknown profile '%s'.\n",
72 m_option_copy(opt
, dst
, &list
);
74 m_option_free(opt
, &list
);
78 static void set_profile(const struct m_option
*opt
, void *dst
, const void *src
)
80 struct m_config
*config
= opt
->priv
;
84 if (!src
|| !*(char ***)src
)
86 m_option_copy(opt
, &list
, src
);
87 for (i
= 0; list
[i
]; i
++) {
88 p
= m_config_get_profile(config
, list
[i
]);
91 m_config_set_profile(config
, p
);
93 m_option_free(opt
, &list
);
96 static int show_profile(struct m_option
*opt
, char *name
, char *param
)
98 struct m_config
*config
= opt
->priv
;
102 return M_OPT_MISSING_PARAM
;
103 if (!(p
= m_config_get_profile(config
, param
))) {
104 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
, "Unknown profile '%s'.\n", param
);
105 return M_OPT_EXIT
- 1;
107 if (!config
->profile_depth
)
108 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "Profile %s: %s\n", param
,
109 p
->desc
? p
->desc
: "");
110 config
->profile_depth
++;
111 for (i
= 0; i
< p
->num_opts
; i
++) {
112 char spc
[config
->profile_depth
+ 1];
113 for (j
= 0; j
< config
->profile_depth
; j
++)
115 spc
[config
->profile_depth
] = '\0';
117 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s%s=%s\n", spc
,
118 p
->opts
[2 * i
], p
->opts
[2 * i
+ 1]);
120 if (config
->profile_depth
< MAX_PROFILE_DEPTH
121 && !strcmp(p
->opts
[2*i
], "profile")) {
122 char *e
, *list
= p
->opts
[2 * i
+ 1];
123 while ((e
= strchr(list
, ','))) {
128 memcpy(tmp
, list
, l
);
130 show_profile(opt
, name
, tmp
);
134 show_profile(opt
, name
, list
);
137 config
->profile_depth
--;
138 if (!config
->profile_depth
)
139 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
140 return M_OPT_EXIT
- 1;
143 static int list_options(struct m_option
*opt
, char *name
, char *param
)
145 struct m_config
*config
= opt
->priv
;
146 m_config_print_option_list(config
);
150 static void m_option_save(const struct m_config
*config
,
151 const struct m_option
*opt
, void *dst
)
153 if (opt
->type
->save
) {
154 const void *src
= m_option_get_ptr(opt
, config
->optstruct
);
155 opt
->type
->save(opt
, dst
, src
);
159 static void m_option_set(const struct m_config
*config
,
160 const struct m_option
*opt
, const void *src
)
162 if (opt
->type
->set
) {
163 void *dst
= m_option_get_ptr(opt
, config
->optstruct
);
164 opt
->type
->set(opt
, dst
, src
);
170 static void m_config_add_option(struct m_config
*config
,
171 const struct m_option
*arg
,
172 const char *prefix
, char *disabled_feature
);
174 struct m_config
*m_config_new(void *optstruct
,
175 int includefunc(struct m_option
*conf
,
178 struct m_config
*config
;
179 static int initialized
= 0;
180 static struct m_option_type profile_opt_type
;
181 static const struct m_option ref_opts
[] = {
182 { "profile", NULL
, &profile_opt_type
, CONF_NOSAVE
, 0, 0, NULL
},
183 { "show-profile", show_profile
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
184 { "list-options", list_options
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
189 config
= talloc_zero(NULL
, struct m_config
);
190 config
->lvl
= 1; // 0 Is the defaults
193 profile_opt_type
= m_option_type_string_list
;
194 profile_opt_type
.parse
= parse_profile
;
195 profile_opt_type
.set
= set_profile
;
197 struct m_option
*self_opts
= talloc_memdup(config
, ref_opts
,
199 for (i
= 0; self_opts
[i
].name
; i
++)
200 self_opts
[i
].priv
= config
;
201 m_config_register_options(config
, self_opts
);
203 struct m_option
*p
= talloc_ptrtype(config
, p
);
204 *p
= (struct m_option
){
205 "include", includefunc
, CONF_TYPE_FUNC_PARAM
,
206 CONF_NOSAVE
, 0, 0, config
208 m_config_add_option(config
, p
, NULL
, NULL
);
210 config
->optstruct
= optstruct
;
215 void m_config_free(struct m_config
*config
)
217 struct m_config_option
*copt
;
218 for (copt
= config
->opts
; copt
; copt
= copt
->next
) {
219 if (copt
->flags
& M_CFG_OPT_ALIAS
)
221 if (copt
->opt
->type
->flags
& M_OPT_TYPE_DYNAMIC
) {
222 void *ptr
= m_option_get_ptr(copt
->opt
, config
->optstruct
);
224 m_option_free(copt
->opt
, ptr
);
226 struct m_config_save_slot
*sl
;
227 for (sl
= copt
->slots
; sl
; sl
= sl
->prev
)
228 m_option_free(copt
->opt
, sl
->data
);
233 void m_config_push(struct m_config
*config
)
235 struct m_config_option
*co
;
236 struct m_config_save_slot
*slot
;
238 assert(config
!= NULL
);
239 assert(config
->lvl
> 0);
243 for (co
= config
->opts
; co
; co
= co
->next
) {
244 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
246 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
248 if (co
->flags
& M_CFG_OPT_ALIAS
)
251 // Update the current status
252 m_option_save(config
, co
->opt
, co
->slots
->data
);
254 // Allocate a new slot
255 slot
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
256 co
->opt
->type
->size
);
257 slot
->lvl
= config
->lvl
;
258 slot
->prev
= co
->slots
;
260 m_option_copy(co
->opt
, co
->slots
->data
, co
->slots
->prev
->data
);
261 // Reset our set flag
262 co
->flags
&= ~M_CFG_OPT_SET
;
265 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,
266 "Config pushed level is now %d\n", config
->lvl
);
269 void m_config_pop(struct m_config
*config
)
271 struct m_config_option
*co
;
272 struct m_config_save_slot
*slot
;
274 assert(config
!= NULL
);
275 assert(config
->lvl
> 1);
277 for (co
= config
->opts
; co
; co
= co
->next
) {
279 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
281 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
283 if (co
->flags
& M_CFG_OPT_ALIAS
)
285 if (co
->slots
->lvl
> config
->lvl
)
286 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,
287 "Save slot found from lvl %d is too old: %d !!!\n",
288 config
->lvl
, co
->slots
->lvl
);
290 while (co
->slots
->lvl
>= config
->lvl
) {
291 m_option_free(co
->opt
, co
->slots
->data
);
293 co
->slots
= slot
->prev
;
297 if (pop
) // We removed some ctx -> set the previous value
298 m_option_set(config
, co
->opt
, co
->slots
->data
);
302 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Config poped level=%d\n", config
->lvl
);
305 static void add_options(struct m_config
*config
, const struct m_option
*defs
,
306 const char *prefix
, char *disabled_feature
)
308 char *dis
= disabled_feature
;
309 const char marker
[] = "conditional functionality: ";
310 for (int i
= 0; defs
[i
].name
; i
++) {
311 if (!strncmp(defs
[i
].name
, marker
, strlen(marker
))) {
312 // If a subconfig entry itself is disabled, everything
313 // under it is already disabled for the same reason.
314 if (!disabled_feature
) {
315 if (!strcmp(defs
[i
].name
+ strlen(marker
), "1"))
322 m_config_add_option(config
, defs
+ i
, prefix
, dis
);
326 static void m_config_add_option(struct m_config
*config
,
327 const struct m_option
*arg
, const char *prefix
,
328 char *disabled_feature
)
330 struct m_config_option
*co
;
331 struct m_config_save_slot
*sl
;
333 assert(config
!= NULL
);
334 assert(config
->lvl
> 0);
337 // Allocate a new entry for this option
338 co
= talloc_zero_size(config
,
339 sizeof(struct m_config_option
) + arg
->type
->size
);
341 co
->disabled_feature
= disabled_feature
;
343 // Fill in the full name
344 if (prefix
&& *prefix
)
345 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
347 co
->name
= (char *)arg
->name
;
349 // Option with children -> add them
350 if (arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
351 add_options(config
, arg
->p
, co
->name
, disabled_feature
);
353 struct m_config_option
*i
;
354 // Check if there is already an option pointing to this address
355 if (arg
->p
|| arg
->new && arg
->offset
>= 0) {
356 for (i
= config
->opts
; i
; i
= i
->next
) {
357 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
358 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
359 // So we don't save the same vars more than 1 time
360 co
->slots
= i
->slots
;
361 co
->flags
|= M_CFG_OPT_ALIAS
;
366 if (!(co
->flags
& M_CFG_OPT_ALIAS
)) {
367 // Allocate a slot for the defaults
368 sl
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
370 m_option_save(config
, arg
, sl
->data
);
371 // Hack to avoid too much trouble with dynamically allocated data:
372 // We replace original default and always use a dynamic version
373 if ((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
374 char **hackptr
= m_option_get_ptr(arg
, config
->optstruct
);
375 if (hackptr
&& *hackptr
) {
377 m_option_set(config
, arg
, sl
->data
);
382 co
->slots
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
384 co
->slots
->prev
= sl
;
385 co
->slots
->lvl
= config
->lvl
;
386 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
389 co
->next
= config
->opts
;
393 int m_config_register_options(struct m_config
*config
,
394 const struct m_option
*args
)
396 assert(config
!= NULL
);
397 assert(config
->lvl
> 0);
398 assert(args
!= NULL
);
400 add_options(config
, args
, NULL
, NULL
);
405 static struct m_config_option
*m_config_get_co(const struct m_config
*config
,
408 struct m_config_option
*co
;
410 for (co
= config
->opts
; co
; co
= co
->next
) {
411 struct bstr coname
= bstr(co
->name
);
412 if ((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
)
413 && bstr_endswith0(coname
, "*")) {
415 if (bstrcasecmp(bstr_splice(name
, 0, coname
.len
), coname
) == 0)
417 } else if (bstrcasecmp(coname
, name
) == 0)
423 static int m_config_parse_option(const struct m_config
*config
,
424 struct bstr name
, struct bstr param
,
425 bool ambiguous_param
, bool set
)
427 struct m_config_option
*co
;
430 assert(config
!= NULL
);
431 assert(config
->lvl
> 0);
432 assert(name
.len
!= 0);
434 co
= m_config_get_co(config
, name
);
436 return M_OPT_UNKNOWN
;
437 if (co
->disabled_feature
) {
438 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
439 "Option \"%.*s\" is not available in this version of mplayer2, "
440 "because it has been compiled with feature \"%s\" disabled.\n",
441 BSTR_P(name
), co
->disabled_feature
);
442 return M_OPT_UNKNOWN
;
445 // This is the only mandatory function
446 assert(co
->opt
->type
->parse
);
448 // Check if this option isn't forbidden in the current mode
449 if ((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
450 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
451 "The %.*s option can't be used in a config file.\n",
453 return M_OPT_INVALID
;
455 if ((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
456 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
457 "The %.*s option can't be used on the command line.\n",
459 return M_OPT_INVALID
;
461 // During command line preparse set only pre-parse options
462 // Otherwise only set pre-parse option if they were not already set.
463 if (((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
464 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
465 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
466 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
469 // Option with children are a bit different to parse
470 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
473 // Parse the child options
474 r
= m_option_parse(co
->opt
, name
, param
, false, &lst
);
477 for (i
= 0; lst
&& lst
[2 * i
]; i
++) {
478 int l
= strlen(co
->name
) + 1 + strlen(lst
[2 * i
]) + 1;
480 // Build the full name
482 sprintf(n
, "%s:%s", co
->name
, lst
[2 * i
]);
483 sr
= m_config_parse_option(config
, bstr(n
),
484 bstr(lst
[2 * i
+ 1]), false,
487 if (sr
== M_OPT_UNKNOWN
) {
488 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
489 "Error: option '%s' has no suboption '%s'.\n",
490 co
->name
, lst
[2 * i
]);
492 } else if (sr
== M_OPT_MISSING_PARAM
) {
493 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
494 "Error: suboption '%s' of '%s' must have "
495 "a parameter!\n", lst
[2 * i
], co
->name
);
501 talloc_free(lst
[2 * i
]);
502 talloc_free(lst
[2 * i
+ 1]);
506 r
= m_option_parse(co
->opt
, name
, param
, ambiguous_param
,
507 set
? co
->slots
->data
: NULL
);
514 m_option_set(config
, co
->opt
, co
->slots
->data
);
515 co
->flags
|= M_CFG_OPT_SET
;
521 int m_config_set_option(struct m_config
*config
, struct bstr name
,
522 struct bstr param
, bool ambiguous_param
)
524 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Setting %.*s=%.*s\n", BSTR_P(name
),
526 return m_config_parse_option(config
, name
, param
, ambiguous_param
, 1);
529 int m_config_check_option(const struct m_config
*config
, struct bstr name
,
530 struct bstr param
, bool ambiguous_param
)
533 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Checking %.*s=%.*s\n", BSTR_P(name
),
535 r
= m_config_parse_option(config
, name
, param
, ambiguous_param
, 0);
536 if (r
== M_OPT_MISSING_PARAM
) {
537 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
538 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name
));
539 return M_OPT_INVALID
;
545 const struct m_option
*m_config_get_option(const struct m_config
*config
,
548 struct m_config_option
*co
;
550 assert(config
!= NULL
);
551 assert(config
->lvl
> 0);
553 co
= m_config_get_co(config
, name
);
560 void m_config_print_option_list(const struct m_config
*config
)
562 char min
[50], max
[50];
563 struct m_config_option
*co
;
569 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
570 "\n Name Type Min Max Global CL Cfg\n\n");
571 for (co
= config
->opts
; co
; co
= co
->next
) {
572 const struct m_option
*opt
= co
->opt
;
573 if (opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
575 if (opt
->flags
& M_OPT_MIN
)
576 sprintf(min
, "%-8.0f", opt
->min
);
579 if (opt
->flags
& M_OPT_MAX
)
580 sprintf(max
, "%-8.0f", opt
->max
);
583 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
,
584 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
589 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
590 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
591 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
594 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n", count
);
597 struct m_profile
*m_config_get_profile(const struct m_config
*config
,
601 for (p
= config
->profiles
; p
; p
= p
->next
)
602 if (!strcmp(p
->name
, name
))
607 struct m_profile
*m_config_add_profile(struct m_config
*config
, char *name
)
609 struct m_profile
*p
= m_config_get_profile(config
, name
);
612 p
= talloc_zero(config
, struct m_profile
);
613 p
->name
= talloc_strdup(p
, name
);
614 p
->next
= config
->profiles
;
615 config
->profiles
= p
;
619 void m_profile_set_desc(struct m_profile
*p
, char *desc
)
621 talloc_free(p
->desc
);
622 p
->desc
= talloc_strdup(p
, desc
);
625 int m_config_set_profile_option(struct m_config
*config
, struct m_profile
*p
,
626 char *name
, char *val
)
628 int i
= m_config_check_option0(config
, name
, val
, false);
631 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2 * (p
->num_opts
+ 2));
632 p
->opts
[p
->num_opts
* 2] = talloc_strdup(p
, name
);
633 p
->opts
[p
->num_opts
* 2 + 1] = talloc_strdup(p
, val
);
635 p
->opts
[p
->num_opts
* 2] = p
->opts
[p
->num_opts
* 2 + 1] = NULL
;
639 void m_config_set_profile(struct m_config
*config
, struct m_profile
*p
)
642 if (config
->profile_depth
> MAX_PROFILE_DEPTH
) {
643 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,
644 "WARNING: Profile inclusion too deep.\n");
647 int prev_mode
= config
->mode
;
648 config
->mode
= M_CONFIG_FILE
;
649 config
->profile_depth
++;
650 for (i
= 0; i
< p
->num_opts
; i
++)
651 m_config_set_option0(config
, p
->opts
[2 * i
], p
->opts
[2 * i
+ 1], false);
652 config
->profile_depth
--;
653 config
->mode
= prev_mode
;