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
,
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
);
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 m_config_add_option(struct m_config
*config
,
306 const struct m_option
*arg
, const char *prefix
)
308 struct m_config_option
*co
;
309 struct m_config_save_slot
*sl
;
311 assert(config
!= NULL
);
312 assert(config
->lvl
> 0);
315 // Allocate a new entry for this option
316 co
= talloc_zero_size(config
,
317 sizeof(struct m_config_option
) + arg
->type
->size
);
320 // Fill in the full name
321 if (prefix
&& *prefix
)
322 co
->name
= talloc_asprintf(co
, "%s:%s", prefix
, arg
->name
);
324 co
->name
= (char *)arg
->name
;
326 // Option with children -> add them
327 if (arg
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
328 const struct m_option
*ol
= arg
->p
;
331 for (i
= 0; ol
[i
].name
!= NULL
; i
++)
332 m_config_add_option(config
, &ol
[i
], co
->name
);
334 struct m_config_option
*i
;
335 // Check if there is already an option pointing to this address
336 if (arg
->p
|| arg
->new && arg
->offset
>= 0) {
337 for (i
= config
->opts
; i
; i
= i
->next
) {
338 if (arg
->new ? (i
->opt
->new && i
->opt
->offset
== arg
->offset
)
339 : (!i
->opt
->new && i
->opt
->p
== arg
->p
)) {
340 // So we don't save the same vars more than 1 time
341 co
->slots
= i
->slots
;
342 co
->flags
|= M_CFG_OPT_ALIAS
;
347 if (!(co
->flags
& M_CFG_OPT_ALIAS
)) {
348 // Allocate a slot for the defaults
349 sl
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
351 m_option_save(config
, arg
, sl
->data
);
352 // Hack to avoid too much trouble with dynamically allocated data:
353 // We replace original default and always use a dynamic version
354 if ((arg
->type
->flags
& M_OPT_TYPE_DYNAMIC
)) {
355 char **hackptr
= m_option_get_ptr(arg
, config
->optstruct
);
356 if (hackptr
&& *hackptr
) {
358 m_option_set(config
, arg
, sl
->data
);
363 co
->slots
= talloc_zero_size(co
, sizeof(struct m_config_save_slot
) +
365 co
->slots
->prev
= sl
;
366 co
->slots
->lvl
= config
->lvl
;
367 m_option_copy(co
->opt
, co
->slots
->data
, sl
->data
);
370 co
->next
= config
->opts
;
374 int m_config_register_options(struct m_config
*config
,
375 const struct m_option
*args
)
379 assert(config
!= NULL
);
380 assert(config
->lvl
> 0);
381 assert(args
!= NULL
);
383 for (i
= 0; args
[i
].name
!= NULL
; i
++)
384 m_config_add_option(config
, &args
[i
], NULL
);
389 static struct m_config_option
*m_config_get_co(const struct m_config
*config
,
392 struct m_config_option
*co
;
394 for (co
= config
->opts
; co
; co
= co
->next
) {
395 struct bstr coname
= bstr(co
->name
);
396 if ((co
->opt
->type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
)
397 && bstr_endswith0(coname
, "*")) {
399 if (bstrcasecmp(bstr_splice(name
, 0, coname
.len
), coname
) == 0)
401 } else if (bstrcasecmp(coname
, name
) == 0)
407 static int m_config_parse_option(const struct m_config
*config
,
408 struct bstr name
, struct bstr param
,
409 bool ambiguous_param
, bool set
)
411 struct m_config_option
*co
;
414 assert(config
!= NULL
);
415 assert(config
->lvl
> 0);
416 assert(name
.len
!= 0);
418 co
= m_config_get_co(config
, name
);
420 return M_OPT_UNKNOWN
;
422 // This is the only mandatory function
423 assert(co
->opt
->type
->parse
);
425 // Check if this option isn't forbidden in the current mode
426 if ((config
->mode
== M_CONFIG_FILE
) && (co
->opt
->flags
& M_OPT_NOCFG
)) {
427 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
428 "The %.*s option can't be used in a config file.\n",
430 return M_OPT_INVALID
;
432 if ((config
->mode
== M_COMMAND_LINE
) && (co
->opt
->flags
& M_OPT_NOCMD
)) {
433 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
434 "The %.*s option can't be used on the command line.\n",
436 return M_OPT_INVALID
;
438 // During command line preparse set only pre-parse options
439 // Otherwise only set pre-parse option if they were not already set.
440 if (((config
->mode
== M_COMMAND_LINE_PRE_PARSE
) &&
441 !(co
->opt
->flags
& M_OPT_PRE_PARSE
)) ||
442 ((config
->mode
!= M_COMMAND_LINE_PRE_PARSE
) &&
443 (co
->opt
->flags
& M_OPT_PRE_PARSE
) && (co
->flags
& M_CFG_OPT_SET
)))
446 // Option with children are a bit different to parse
447 if (co
->opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) {
450 // Parse the child options
451 r
= m_option_parse(co
->opt
, name
, param
, false, &lst
);
454 for (i
= 0; lst
&& lst
[2 * i
]; i
++) {
455 int l
= strlen(co
->name
) + 1 + strlen(lst
[2 * i
]) + 1;
457 // Build the full name
459 sprintf(n
, "%s:%s", co
->name
, lst
[2 * i
]);
460 sr
= m_config_parse_option(config
, bstr(n
),
461 bstr(lst
[2 * i
+ 1]), false,
464 if (sr
== M_OPT_UNKNOWN
) {
465 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
466 "Error: option '%s' has no suboption '%s'.\n",
467 co
->name
, lst
[2 * i
]);
469 } else if (sr
== M_OPT_MISSING_PARAM
) {
470 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
471 "Error: suboption '%s' of '%s' must have "
472 "a parameter!\n", lst
[2 * i
], co
->name
);
478 talloc_free(lst
[2 * i
]);
479 talloc_free(lst
[2 * i
+ 1]);
483 r
= m_option_parse(co
->opt
, name
, param
, ambiguous_param
,
484 set
? co
->slots
->data
: NULL
);
491 m_option_set(config
, co
->opt
, co
->slots
->data
);
492 co
->flags
|= M_CFG_OPT_SET
;
498 int m_config_set_option(struct m_config
*config
, struct bstr name
,
499 struct bstr param
, bool ambiguous_param
)
501 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Setting %.*s=%.*s\n", BSTR_P(name
),
503 return m_config_parse_option(config
, name
, param
, ambiguous_param
, 1);
506 int m_config_check_option(const struct m_config
*config
, struct bstr name
,
507 struct bstr param
, bool ambiguous_param
)
510 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
, "Checking %.*s=%.*s\n", BSTR_P(name
),
512 r
= m_config_parse_option(config
, name
, param
, ambiguous_param
, 0);
513 if (r
== M_OPT_MISSING_PARAM
) {
514 mp_tmsg(MSGT_CFGPARSER
, MSGL_ERR
,
515 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name
));
516 return M_OPT_INVALID
;
522 const struct m_option
*m_config_get_option(const struct m_config
*config
,
525 struct m_config_option
*co
;
527 assert(config
!= NULL
);
528 assert(config
->lvl
> 0);
530 co
= m_config_get_co(config
, name
);
537 void m_config_print_option_list(const struct m_config
*config
)
539 char min
[50], max
[50];
540 struct m_config_option
*co
;
546 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
,
547 "\n Name Type Min Max Global CL Cfg\n\n");
548 for (co
= config
->opts
; co
; co
= co
->next
) {
549 const struct m_option
*opt
= co
->opt
;
550 if (opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
)
552 if (opt
->flags
& M_OPT_MIN
)
553 sprintf(min
, "%-8.0f", opt
->min
);
556 if (opt
->flags
& M_OPT_MAX
)
557 sprintf(max
, "%-8.0f", opt
->max
);
560 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
,
561 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
566 opt
->flags
& CONF_GLOBAL
? "Yes" : "No",
567 opt
->flags
& CONF_NOCMD
? "No" : "Yes",
568 opt
->flags
& CONF_NOCFG
? "No" : "Yes");
571 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d options\n", count
);
574 struct m_profile
*m_config_get_profile(const struct m_config
*config
,
578 for (p
= config
->profiles
; p
; p
= p
->next
)
579 if (!strcmp(p
->name
, name
))
584 struct m_profile
*m_config_add_profile(struct m_config
*config
, char *name
)
586 struct m_profile
*p
= m_config_get_profile(config
, name
);
589 p
= talloc_zero(config
, struct m_profile
);
590 p
->name
= talloc_strdup(p
, name
);
591 p
->next
= config
->profiles
;
592 config
->profiles
= p
;
596 void m_profile_set_desc(struct m_profile
*p
, char *desc
)
598 talloc_free(p
->desc
);
599 p
->desc
= talloc_strdup(p
, desc
);
602 int m_config_set_profile_option(struct m_config
*config
, struct m_profile
*p
,
603 char *name
, char *val
)
605 int i
= m_config_check_option0(config
, name
, val
, false);
608 p
->opts
= talloc_realloc(p
, p
->opts
, char *, 2 * (p
->num_opts
+ 2));
609 p
->opts
[p
->num_opts
* 2] = talloc_strdup(p
, name
);
610 p
->opts
[p
->num_opts
* 2 + 1] = talloc_strdup(p
, val
);
612 p
->opts
[p
->num_opts
* 2] = p
->opts
[p
->num_opts
* 2 + 1] = NULL
;
616 void m_config_set_profile(struct m_config
*config
, struct m_profile
*p
)
619 if (config
->profile_depth
> MAX_PROFILE_DEPTH
) {
620 mp_tmsg(MSGT_CFGPARSER
, MSGL_WARN
,
621 "WARNING: Profile inclusion too deep.\n");
624 int prev_mode
= config
->mode
;
625 config
->mode
= M_CONFIG_FILE
;
626 config
->profile_depth
++;
627 for (i
= 0; i
< p
->num_opts
; i
++)
628 m_config_set_option0(config
, p
->opts
[2 * i
], p
->opts
[2 * i
+ 1], false);
629 config
->profile_depth
--;
630 config
->mode
= prev_mode
;