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(const struct m_config
*config
,
152 const struct m_option
*opt
, const void *src
)
154 if (opt
->type
->copy
) {
155 void *dst
= m_option_get_ptr(opt
, config
->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
);
179 config
->lvl
= 1; // 0 Is the defaults
180 struct m_option
*self_opts
= talloc_memdup(config
, ref_opts
,
182 for (int i
= 1; self_opts
[i
].name
; i
++)
183 self_opts
[i
].priv
= config
;
184 m_config_register_options(config
, self_opts
);
186 struct m_option
*p
= talloc_ptrtype(config
, p
);
187 *p
= (struct m_option
){
188 "include", NULL
, CONF_TYPE_STRING
, CONF_NOSAVE
,
190 m_config_add_option(config
, p
, NULL
, NULL
);
191 config
->includefunc
= includefunc
;
193 config
->optstruct
= optstruct
;
198 void m_config_free(struct m_config
*config
)
200 struct m_config_option
*copt
;
201 for (copt
= config
->opts
; copt
; copt
= copt
->next
) {
202 if (copt
->flags
& M_CFG_OPT_ALIAS
)
204 if (copt
->opt
->type
->flags
& M_OPT_TYPE_DYNAMIC
) {
205 void *ptr
= m_option_get_ptr(copt
->opt
, config
->optstruct
);
207 m_option_free(copt
->opt
, ptr
);
209 struct m_config_save_slot
*sl
;
210 for (sl
= copt
->slots
; sl
; sl
= sl
->prev
)
211 m_option_free(copt
->opt
, sl
->data
);
216 void m_config_push(struct m_config
*config
)
218 struct m_config_option
*co
;
219 struct m_config_save_slot
*slot
;
221 assert(config
!= NULL
);
222 assert(config
->lvl
> 0);
226 for (co
= config
->opts
; co
; co
= co
->next
) {
227 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
229 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
231 if (co
->flags
& M_CFG_OPT_ALIAS
)
234 // Update the current status
235 m_option_save(config
, co
->opt
, co
->slots
->data
);
237 // Allocate a new slot
238 slot
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
239 co
->opt
->type
->size
);
240 slot
->lvl
= config
->lvl
;
241 slot
->prev
= co
->slots
;
243 m_option_copy(co
->opt
, co
->slots
->data
, co
->slots
->prev
->data
);
244 // Reset our set flag
245 co
->flags
&= ~M_CFG_OPT_SET
;
248 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,
249 "Config pushed level is now %d\n", config
->lvl
);
252 void m_config_pop(struct m_config
*config
)
254 struct m_config_option
*co
;
255 struct m_config_save_slot
*slot
;
257 assert(config
!= NULL
);
258 assert(config
->lvl
> 1);
260 for (co
= config
->opts
; co
; co
= co
->next
) {
262 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
264 if (co
->opt
->flags
& (M_OPT_GLOBAL
| M_OPT_NOSAVE
))
266 if (co
->flags
& M_CFG_OPT_ALIAS
)
268 if (co
->slots
->lvl
> config
->lvl
)
269 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
,
270 "Save slot found from lvl %d is too old: %d !!!\n",
271 config
->lvl
, co
->slots
->lvl
);
273 while (co
->slots
->lvl
>= config
->lvl
) {
274 m_option_free(co
->opt
, co
->slots
->data
);
276 co
->slots
= slot
->prev
;
280 if (pop
) // We removed some ctx -> set the previous value
281 m_option_set(config
, co
->opt
, co
->slots
->data
);
285 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Config poped level=%d\n", config
->lvl
);
288 static void add_options(struct m_config
*config
, const struct m_option
*defs
,
289 const char *prefix
, char *disabled_feature
)
291 char *dis
= disabled_feature
;
292 const char marker
[] = "conditional functionality: ";
293 for (int i
= 0; defs
[i
].name
; i
++) {
294 if (!strncmp(defs
[i
].name
, marker
, strlen(marker
))) {
295 // If a subconfig entry itself is disabled, everything
296 // under it is already disabled for the same reason.
297 if (!disabled_feature
) {
298 if (!strcmp(defs
[i
].name
+ strlen(marker
), "1"))
305 m_config_add_option(config
, defs
+ i
, prefix
, dis
);
309 static void m_config_add_option(struct m_config
*config
,
310 const struct m_option
*arg
, const char *prefix
,
311 char *disabled_feature
)
313 struct m_config_option
*co
;
314 struct m_config_save_slot
*sl
;
316 assert(config
!= NULL
);
317 assert(config
->lvl
> 0);
320 // Allocate a new entry for this option
321 co
= talloc_zero_size(config
,
322 sizeof(struct m_config_option
) + arg
->type
->size
);
324 co
->disabled_feature
= disabled_feature
;
326 // Fill in the full name
327 if (prefix
&& *prefix
)
328 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
330 co
->name
= (char *)arg
->name
;
332 // Option with children -> add them
333 if (arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
334 add_options(config
, arg
->p
, co
->name
, disabled_feature
);
336 struct m_config_option
*i
;
337 // Check if there is already an option pointing to this address
338 if (arg
->p
|| arg
->new && arg
->offset
>= 0) {
339 for (i
= config
->opts
; i
; i
= i
->next
) {
340 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
341 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
342 // So we don't save the same vars more than 1 time
343 co
->slots
= i
->slots
;
344 co
->flags
|= M_CFG_OPT_ALIAS
;
349 if (!(co
->flags
& M_CFG_OPT_ALIAS
)) {
350 // Allocate a slot for the defaults
351 sl
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
353 m_option_save(config
, arg
, sl
->data
);
354 // Hack to avoid too much trouble with dynamically allocated data:
355 // We replace original default and always use a dynamic version
356 if ((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
357 char **hackptr
= m_option_get_ptr(arg
, config
->optstruct
);
358 if (hackptr
&& *hackptr
) {
360 m_option_set(config
, arg
, sl
->data
);
365 co
->slots
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
367 co
->slots
->prev
= sl
;
368 co
->slots
->lvl
= config
->lvl
;
369 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
372 co
->next
= config
->opts
;
376 int m_config_register_options(struct m_config
*config
,
377 const struct m_option
*args
)
379 assert(config
!= NULL
);
380 assert(config
->lvl
> 0);
381 assert(args
!= NULL
);
383 add_options(config
, args
, NULL
, NULL
);
388 static struct m_config_option
*m_config_get_co(const struct m_config
*config
,
391 struct m_config_option
*co
;
393 for (co
= config
->opts
; co
; co
= co
->next
) {
394 struct bstr coname
= bstr(co
->name
);
395 if ((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
)
396 && bstr_endswith0(coname
, "*")) {
398 if (bstrcasecmp(bstr_splice(name
, 0, coname
.len
), coname
) == 0)
400 } else if (bstrcasecmp(coname
, name
) == 0)
406 static int m_config_parse_option(struct m_config
*config
,
407 struct bstr name
, struct bstr param
,
408 bool ambiguous_param
, bool set
)
410 assert(config
!= NULL
);
411 assert(config
->lvl
> 0);
412 assert(name
.len
!= 0);
414 struct m_config_option
*co
= m_config_get_co(config
, name
);
416 return M_OPT_UNKNOWN
;
417 if (co
->disabled_feature
) {
418 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
419 "Option \"%.*s\" is not available in this version of mplayer2, "
420 "because it has been compiled with feature \"%s\" disabled.\n",
421 BSTR_P(name
), co
->disabled_feature
);
422 return M_OPT_UNKNOWN
;
425 // This is the only mandatory function
426 assert(co
->opt
->type
->parse
);
428 // Check if this option isn't forbidden in the current mode
429 if ((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
430 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
431 "The %.*s option can't be used in a config file.\n",
433 return M_OPT_INVALID
;
435 if ((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
436 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
437 "The %.*s option can't be used on the command line.\n",
439 return M_OPT_INVALID
;
441 // During command line preparse set only pre-parse options
442 // Otherwise only set pre-parse option if they were not already set.
443 if (((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
444 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
445 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
446 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
449 if (config
->includefunc
&& !bstrcmp0(name
, "include")) {
450 return parse_include(config
, param
, set
);
451 } else if (!bstrcmp0(name
, "profile"))
452 return parse_profile(config
, co
->opt
, name
, param
, set
);
454 // Option with children are a bit different to parse
455 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
457 // Parse the child options
458 int r
= m_option_parse(co
->opt
, name
, param
, false, &lst
);
461 for (int i
= 0; lst
&& lst
[2 * i
]; i
++) {
462 int l
= strlen(co
->name
) + 1 + strlen(lst
[2 * i
]) + 1;
464 // Build the full name
466 sprintf(n
, "%s:%s", co
->name
, lst
[2 * i
]);
467 int sr
= m_config_parse_option(config
, bstr(n
),
468 bstr(lst
[2 * i
+ 1]), false,
471 if (sr
== M_OPT_UNKNOWN
) {
472 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
473 "Error: option '%s' has no suboption '%s'.\n",
474 co
->name
, lst
[2 * i
]);
476 } else if (sr
== M_OPT_MISSING_PARAM
) {
477 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
478 "Error: suboption '%s' of '%s' must have "
479 "a parameter!\n", lst
[2 * i
], co
->name
);
485 talloc_free(lst
[2 * i
]);
486 talloc_free(lst
[2 * i
+ 1]);
492 void *dst
= set
? m_option_get_ptr(co
->opt
, config
->optstruct
) : NULL
;
493 int r
= m_option_parse(co
->opt
, name
, param
, ambiguous_param
, dst
);
498 co
->flags
|= M_CFG_OPT_SET
;
503 int m_config_set_option(struct m_config
*config
, struct bstr name
,
504 struct bstr param
, bool ambiguous_param
)
506 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Setting %.*s=%.*s\n", BSTR_P(name
),
508 return m_config_parse_option(config
, name
, param
, ambiguous_param
, 1);
511 int m_config_check_option(struct m_config
*config
, struct bstr name
,
512 struct bstr param
, bool ambiguous_param
)
515 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Checking %.*s=%.*s\n", BSTR_P(name
),
517 r
= m_config_parse_option(config
, name
, param
, ambiguous_param
, 0);
518 if (r
== M_OPT_MISSING_PARAM
) {
519 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
520 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name
));
521 return M_OPT_INVALID
;
527 const struct m_option
*m_config_get_option(const struct m_config
*config
,
530 struct m_config_option
*co
;
532 assert(config
!= NULL
);
533 assert(config
->lvl
> 0);
535 co
= m_config_get_co(config
, name
);
542 void m_config_print_option_list(const struct m_config
*config
)
544 char min
[50], max
[50];
545 struct m_config_option
*co
;
551 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
552 "\n Name Type Min Max Global CL Cfg\n\n");
553 for (co
= config
->opts
; co
; co
= co
->next
) {
554 const struct m_option
*opt
= co
->opt
;
555 if (opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
557 if (opt
->flags
& M_OPT_MIN
)
558 sprintf(min
, "%-8.0f", opt
->min
);
561 if (opt
->flags
& M_OPT_MAX
)
562 sprintf(max
, "%-8.0f", opt
->max
);
565 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
,
566 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
571 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
572 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
573 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
576 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n", count
);
579 struct m_profile
*m_config_get_profile(const struct m_config
*config
,
583 for (p
= config
->profiles
; p
; p
= p
->next
)
584 if (!strcmp(p
->name
, name
))
589 struct m_profile
*m_config_add_profile(struct m_config
*config
, char *name
)
591 struct m_profile
*p
= m_config_get_profile(config
, name
);
594 p
= talloc_zero(config
, struct m_profile
);
595 p
->name
= talloc_strdup(p
, name
);
596 p
->next
= config
->profiles
;
597 config
->profiles
= p
;
601 void m_profile_set_desc(struct m_profile
*p
, char *desc
)
603 talloc_free(p
->desc
);
604 p
->desc
= talloc_strdup(p
, desc
);
607 int m_config_set_profile_option(struct m_config
*config
, struct m_profile
*p
,
608 char *name
, char *val
)
610 int i
= m_config_check_option0(config
, name
, val
, false);
613 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2 * (p
->num_opts
+ 2));
614 p
->opts
[p
->num_opts
* 2] = talloc_strdup(p
, name
);
615 p
->opts
[p
->num_opts
* 2 + 1] = talloc_strdup(p
, val
);
617 p
->opts
[p
->num_opts
* 2] = p
->opts
[p
->num_opts
* 2 + 1] = NULL
;
621 void m_config_set_profile(struct m_config
*config
, struct m_profile
*p
)
624 if (config
->profile_depth
> MAX_PROFILE_DEPTH
) {
625 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,
626 "WARNING: Profile inclusion too deep.\n");
629 int prev_mode
= config
->mode
;
630 config
->mode
= M_CONFIG_FILE
;
631 config
->profile_depth
++;
632 for (i
= 0; i
< p
->num_opts
; i
++)
633 m_config_set_option0(config
, p
->opts
[2 * i
], p
->opts
[2 * i
+ 1], false);
634 config
->profile_depth
--;
635 config
->mode
= prev_mode
;