fmt-conversion.h: fix pixfmt.h #include; fixes latest Libav
[mplayer.git] / m_config.c
blobfb9e1b6492a1050237fc71c6da857ea78d2ba133
1 /*
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.
19 /// \file
20 /// \ingroup Config
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <stdbool.h>
31 #include "talloc.h"
33 #include "m_config.h"
34 #include "m_option.h"
35 #include "mp_msg.h"
37 #define MAX_PROFILE_DEPTH 20
39 static int parse_include(struct m_config *config, struct bstr param, bool set)
41 if (param.len == 0)
42 return M_OPT_MISSING_PARAM;
43 if (!set)
44 return 1;
45 char *filename = bstrdup0(NULL, param);
46 config->includefunc(config, filename);
47 talloc_free(filename);
48 return 1;
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")) {
55 struct m_profile *p;
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;
69 char **list = NULL;
70 int r = m_option_type_string_list.parse(opt, name, param, false, &list,
71 NULL);
72 if (r < 0)
73 return r;
74 if (!list || !list[0])
75 return M_OPT_INVALID;
76 for (int i = 0; list[i]; i++) {
77 struct m_profile *p = m_config_get_profile(config, list[i]);
78 if (!p) {
79 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN, "Unknown profile '%s'.\n",
80 list[i]);
81 r = M_OPT_INVALID;
82 } else if (set)
83 m_config_set_profile(config, p);
85 m_option_free(opt, &list);
86 return r;
89 static int show_profile(struct m_option *opt, char *name, char *param)
91 struct m_config *config = opt->priv;
92 struct m_profile *p;
93 int i, j;
94 if (!param)
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++)
107 spc[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, ','))) {
117 int l = e - list;
118 char tmp[l+1];
119 if (!l)
120 continue;
121 memcpy(tmp, list, l);
122 tmp[l] = '\0';
123 show_profile(opt, name, tmp);
124 list = e + 1;
126 if (list[0] != '\0')
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);
140 return M_OPT_EXIT;
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,
169 char *filename))
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 },
176 { NULL }
179 config = talloc_zero(NULL, struct m_config);
180 config->full = true;
181 config->lvl = 1; // 0 Is the defaults
182 struct m_option *self_opts = talloc_memdup(config, ref_opts,
183 sizeof(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);
187 if (includefunc) {
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;
197 return config;
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);
204 return config;
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)
213 continue;
214 if (copt->opt->type->flags & M_OPT_TYPE_DYNAMIC) {
215 void *ptr = m_option_get_ptr(copt->opt, config->optstruct);
216 if (ptr)
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);
223 talloc_free(config);
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;
231 if (!opt->defval)
232 continue;
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);
245 config->lvl++;
247 for (co = config->opts; co; co = co->next) {
248 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
249 continue;
250 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
251 continue;
252 if (co->flags & M_CFG_OPT_ALIAS)
253 continue;
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;
263 co->slots = slot;
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) {
282 int pop = 0;
283 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
284 continue;
285 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
286 continue;
287 if (co->flags & M_CFG_OPT_ALIAS)
288 continue;
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);
296 slot = co->slots;
297 co->slots = slot->prev;
298 talloc_free(slot);
299 pop++;
301 if (pop) // We removed some ctx -> set the previous value
302 m_option_set(config->optstruct, co->opt, co->slots->data);
305 config->lvl--;
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"))
320 dis = NULL;
321 else
322 dis = defs[i].p;
324 continue;
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);
339 assert(arg != NULL);
341 // Allocate a new entry for this option
342 co = talloc_zero_size(config,
343 sizeof(struct m_config_option) + arg->type->size);
344 co->opt = arg;
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);
350 else
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);
356 } else {
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;
366 break;
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) +
373 arg->type->size);
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) {
380 *hackptr = NULL;
381 m_option_set(config->optstruct, arg, sl->data);
384 sl->lvl = 0;
385 sl->prev = NULL;
386 co->slots = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
387 arg->type->size);
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;
394 config->opts = co;
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);
406 return 1;
409 static struct m_config_option *m_config_get_co(const struct m_config *config,
410 struct bstr name)
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, "*")) {
418 coname.len--;
419 if (bstrcasecmp(bstr_splice(name, 0, coname.len), coname) == 0)
420 return co;
421 } else if (bstrcasecmp(coname, name) == 0)
422 return co;
424 return NULL;
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);
439 if (!co)
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",
456 BSTR_P(name));
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",
462 BSTR_P(name));
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)))
471 set = 0;
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) {
480 char prefix[110];
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,
488 optstruct);
489 // Parsing failed ?
490 if (r < 0)
491 return r;
492 else if (set)
493 co->flags |= M_CFG_OPT_SET;
495 return r;
498 static int parse_subopts(struct m_config *config, void *optstruct, char *name,
499 char *prefix, struct bstr param, bool set)
501 char **lst = NULL;
502 // Split the argument into child options
503 int r = m_option_type_subconfig.parse(NULL, bstr(""), param, false, &lst,
504 optstruct);
505 if (r < 0)
506 return r;
507 // Parse the child options
508 for (int i = 0; lst && lst[2 * i]; i++) {
509 // Build the full name
510 char n[110];
511 if (snprintf(n, 110, "%s%s", prefix, lst[2 * i]) > 100)
512 abort();
513 if (!m_config_get_option(config, bstr(n))) {
514 if (strncmp(lst[2 * i], "no-", 3))
515 goto nosubopt;
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) {
519 nosubopt:
520 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
521 "Error: option '%s' has no suboption '%s'.\n",
522 name, lst[2 * i]);
523 r = M_OPT_INVALID;
524 break;
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]);
530 r = M_OPT_INVALID;
531 break;
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);
537 if (sr < 0) {
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);
542 r = M_OPT_INVALID;
543 } else
544 r = sr;
545 break;
548 talloc_free(lst);
549 return r;
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),
556 BSTR_P(param));
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)
564 int r;
565 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name),
566 BSTR_P(param));
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;
573 return r;
576 int m_config_parse_suboptions(struct m_config *config, void *optstruct,
577 char *name, char *subopts)
579 if (!subopts || !*subopts)
580 return 0;
581 return parse_subopts(config, optstruct, name, "", bstr(subopts), true);
585 const struct m_option *m_config_get_option(const struct m_config *config,
586 struct bstr name)
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);
594 if (co)
595 return co->opt;
596 else
597 return NULL;
600 void m_config_print_option_list(const struct m_config *config)
602 char min[50], max[50];
603 struct m_config_option *co;
604 int count = 0;
606 if (!config->opts)
607 return;
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)
614 continue;
615 if (opt->flags & M_OPT_MIN)
616 sprintf(min, "%-8.0f", opt->min);
617 else
618 strcpy(min, "No");
619 if (opt->flags & M_OPT_MAX)
620 sprintf(max, "%-8.0f", opt->max);
621 else
622 strcpy(max, "No");
623 mp_msg(MSGT_CFGPARSER, MSGL_INFO,
624 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
625 co->name,
626 co->opt->type->name,
627 min,
628 max,
629 opt->flags & CONF_GLOBAL ? "Yes" : "No",
630 opt->flags & CONF_NOCMD ? "No" : "Yes",
631 opt->flags & CONF_NOCFG ? "No" : "Yes");
632 count++;
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,
638 char *name)
640 struct m_profile *p;
641 for (p = config->profiles; p; p = p->next)
642 if (!strcmp(p->name, name))
643 return p;
644 return NULL;
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);
650 if (p)
651 return p;
652 p = talloc_zero(config, struct m_profile);
653 p->name = talloc_strdup(p, name);
654 p->next = config->profiles;
655 config->profiles = p;
656 return 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);
669 if (i < 0)
670 return i;
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);
674 p->num_opts++;
675 p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL;
676 return 1;
679 void m_config_set_profile(struct m_config *config, struct m_profile *p)
681 int i;
682 if (config->profile_depth > MAX_PROFILE_DEPTH) {
683 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN,
684 "WARNING: Profile inclusion too deep.\n");
685 return;
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;