options: support parsing values into substructs
[mplayer.git] / m_config.c
blobe8996670f45a9a2d1eaadf3d4b442249f79e5a31
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 if (r < 0)
72 return r;
73 if (!list || !list[0])
74 return M_OPT_INVALID;
75 for (int i = 0; list[i]; i++) {
76 struct m_profile *p = m_config_get_profile(config, list[i]);
77 if (!p) {
78 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN, "Unknown profile '%s'.\n",
79 list[i]);
80 r = M_OPT_INVALID;
81 } else if (set)
82 m_config_set_profile(config, p);
84 m_option_free(opt, &list);
85 return r;
88 static int show_profile(struct m_option *opt, char *name, char *param)
90 struct m_config *config = opt->priv;
91 struct m_profile *p;
92 int i, j;
93 if (!param)
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++)
106 spc[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, ','))) {
116 int l = e - list;
117 char tmp[l+1];
118 if (!l)
119 continue;
120 memcpy(tmp, list, l);
121 tmp[l] = '\0';
122 show_profile(opt, name, tmp);
123 list = e + 1;
125 if (list[0] != '\0')
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);
139 return M_OPT_EXIT;
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(void *optstruct,
152 const struct m_option *opt, const void *src)
154 if (opt->type->copy) {
155 void *dst = m_option_get_ptr(opt, 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,
168 char *filename))
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 },
175 { NULL }
178 config = talloc_zero(NULL, struct m_config);
179 config->full = true;
180 config->lvl = 1; // 0 Is the defaults
181 struct m_option *self_opts = talloc_memdup(config, ref_opts,
182 sizeof(ref_opts));
183 for (int i = 1; self_opts[i].name; i++)
184 self_opts[i].priv = config;
185 m_config_register_options(config, self_opts);
186 if (includefunc) {
187 struct m_option *p = talloc_ptrtype(config, p);
188 *p = (struct m_option){
189 "include", NULL, CONF_TYPE_STRING, CONF_NOSAVE,
191 m_config_add_option(config, p, NULL, NULL);
192 config->includefunc = includefunc;
194 config->optstruct = optstruct;
196 return config;
199 struct m_config *m_config_simple(const struct m_option *options)
201 struct m_config *config = talloc_zero(NULL, struct m_config);
202 m_config_register_options(config, options);
203 return config;
206 void m_config_free(struct m_config *config)
208 assert(config->full); // use talloc_free() for simple
209 struct m_config_option *copt;
210 for (copt = config->opts; copt; copt = copt->next) {
211 if (copt->flags & M_CFG_OPT_ALIAS)
212 continue;
213 if (copt->opt->type->flags & M_OPT_TYPE_DYNAMIC) {
214 void *ptr = m_option_get_ptr(copt->opt, config->optstruct);
215 if (ptr)
216 m_option_free(copt->opt, ptr);
218 struct m_config_save_slot *sl;
219 for (sl = copt->slots; sl; sl = sl->prev)
220 m_option_free(copt->opt, sl->data);
222 talloc_free(config);
225 void m_config_initialize(struct m_config *config, void *optstruct)
227 struct m_config_option *copt;
228 for (copt = config->opts; copt; copt = copt->next) {
229 const struct m_option *opt = copt->opt;
230 if (!opt->defval)
231 continue;
232 m_option_set(optstruct, opt, opt->defval);
236 void m_config_push(struct m_config *config)
238 struct m_config_option *co;
239 struct m_config_save_slot *slot;
241 assert(config != NULL);
242 assert(config->lvl > 0);
244 config->lvl++;
246 for (co = config->opts; co; co = co->next) {
247 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
248 continue;
249 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
250 continue;
251 if (co->flags & M_CFG_OPT_ALIAS)
252 continue;
254 // Update the current status
255 m_option_save(config, co->opt, co->slots->data);
257 // Allocate a new slot
258 slot = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
259 co->opt->type->size);
260 slot->lvl = config->lvl;
261 slot->prev = co->slots;
262 co->slots = slot;
263 m_option_copy(co->opt, co->slots->data, co->slots->prev->data);
264 // Reset our set flag
265 co->flags &= ~M_CFG_OPT_SET;
268 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,
269 "Config pushed level is now %d\n", config->lvl);
272 void m_config_pop(struct m_config *config)
274 struct m_config_option *co;
275 struct m_config_save_slot *slot;
277 assert(config != NULL);
278 assert(config->lvl > 1);
280 for (co = config->opts; co; co = co->next) {
281 int pop = 0;
282 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
283 continue;
284 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
285 continue;
286 if (co->flags & M_CFG_OPT_ALIAS)
287 continue;
288 if (co->slots->lvl > config->lvl)
289 mp_msg(MSGT_CFGPARSER, MSGL_WARN,
290 "Save slot found from lvl %d is too old: %d !!!\n",
291 config->lvl, co->slots->lvl);
293 while (co->slots->lvl >= config->lvl) {
294 m_option_free(co->opt, co->slots->data);
295 slot = co->slots;
296 co->slots = slot->prev;
297 talloc_free(slot);
298 pop++;
300 if (pop) // We removed some ctx -> set the previous value
301 m_option_set(config->optstruct, co->opt, co->slots->data);
304 config->lvl--;
305 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Config poped level=%d\n", config->lvl);
308 static void add_options(struct m_config *config, const struct m_option *defs,
309 const char *prefix, char *disabled_feature)
311 char *dis = disabled_feature;
312 const char marker[] = "conditional functionality: ";
313 for (int i = 0; defs[i].name; i++) {
314 if (!strncmp(defs[i].name, marker, strlen(marker))) {
315 // If a subconfig entry itself is disabled, everything
316 // under it is already disabled for the same reason.
317 if (!disabled_feature) {
318 if (!strcmp(defs[i].name + strlen(marker), "1"))
319 dis = NULL;
320 else
321 dis = defs[i].p;
323 continue;
325 m_config_add_option(config, defs + i, prefix, dis);
329 static void m_config_add_option(struct m_config *config,
330 const struct m_option *arg, const char *prefix,
331 char *disabled_feature)
333 struct m_config_option *co;
334 struct m_config_save_slot *sl;
336 assert(config != NULL);
337 assert(config->lvl > 0 || !config->full);
338 assert(arg != NULL);
340 // Allocate a new entry for this option
341 co = talloc_zero_size(config,
342 sizeof(struct m_config_option) + arg->type->size);
343 co->opt = arg;
344 co->disabled_feature = disabled_feature;
346 // Fill in the full name
347 if (prefix && *prefix)
348 co->name = talloc_asprintf(co, "%s:%s", prefix, arg->name);
349 else
350 co->name = (char *)arg->name;
352 // Option with children -> add them
353 if (arg->type->flags & M_OPT_TYPE_HAS_CHILD) {
354 add_options(config, arg->p, co->name, disabled_feature);
355 } else {
356 struct m_config_option *i;
357 // Check if there is already an option pointing to this address
358 if (arg->p || arg->new && arg->offset >= 0) {
359 for (i = config->opts; i; i = i->next) {
360 if (arg->new ? (i->opt->new && i->opt->offset == arg->offset)
361 : (!i->opt->new && i->opt->p == arg->p)) {
362 // So we don't save the same vars more than 1 time
363 co->slots = i->slots;
364 co->flags |= M_CFG_OPT_ALIAS;
365 break;
369 if (config->full && !(co->flags & M_CFG_OPT_ALIAS)) {
370 // Allocate a slot for the defaults
371 sl = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
372 arg->type->size);
373 m_option_save(config, arg, sl->data);
374 // Hack to avoid too much trouble with dynamically allocated data:
375 // We replace original default and always use a dynamic version
376 if (!arg->new && (arg->type->flags & M_OPT_TYPE_DYNAMIC)) {
377 char **hackptr = m_option_get_ptr(arg, config->optstruct);
378 if (hackptr && *hackptr) {
379 *hackptr = NULL;
380 m_option_set(config->optstruct, arg, sl->data);
383 sl->lvl = 0;
384 sl->prev = NULL;
385 co->slots = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
386 arg->type->size);
387 co->slots->prev = sl;
388 co->slots->lvl = config->lvl;
389 m_option_copy(co->opt, co->slots->data, sl->data);
392 co->next = config->opts;
393 config->opts = co;
396 int m_config_register_options(struct m_config *config,
397 const struct m_option *args)
399 assert(config != NULL);
400 assert(config->lvl > 0 || !config->full);
401 assert(args != NULL);
403 add_options(config, args, NULL, NULL);
405 return 1;
408 static struct m_config_option *m_config_get_co(const struct m_config *config,
409 struct bstr name)
411 struct m_config_option *co;
413 for (co = config->opts; co; co = co->next) {
414 struct bstr coname = bstr(co->name);
415 if ((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD)
416 && bstr_endswith0(coname, "*")) {
417 coname.len--;
418 if (bstrcasecmp(bstr_splice(name, 0, coname.len), coname) == 0)
419 return co;
420 } else if (bstrcasecmp(coname, name) == 0)
421 return co;
423 return NULL;
426 static int parse_subopts(struct m_config *config, void *optstruct, char *name,
427 char *prefix, struct bstr param, bool set);
429 static int m_config_parse_option(struct m_config *config, void *optstruct,
430 struct bstr name, struct bstr param,
431 bool ambiguous_param, bool set)
433 assert(config != NULL);
434 assert(config->lvl > 0 || !config->full);
435 assert(name.len != 0);
437 struct m_config_option *co = m_config_get_co(config, name);
438 if (!co)
439 return M_OPT_UNKNOWN;
440 if (co->disabled_feature) {
441 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
442 "Option \"%.*s\" is not available in this version of mplayer2, "
443 "because it has been compiled with feature \"%s\" disabled.\n",
444 BSTR_P(name), co->disabled_feature);
445 return M_OPT_UNKNOWN;
448 // This is the only mandatory function
449 assert(co->opt->type->parse);
451 // Check if this option isn't forbidden in the current mode
452 if ((config->mode == M_CONFIG_FILE) && (co->opt->flags & M_OPT_NOCFG)) {
453 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
454 "The %.*s option can't be used in a config file.\n",
455 BSTR_P(name));
456 return M_OPT_INVALID;
458 if ((config->mode == M_COMMAND_LINE) && (co->opt->flags & M_OPT_NOCMD)) {
459 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
460 "The %.*s option can't be used on the command line.\n",
461 BSTR_P(name));
462 return M_OPT_INVALID;
464 // During command line preparse set only pre-parse options
465 // Otherwise only set pre-parse option if they were not already set.
466 if (((config->mode == M_COMMAND_LINE_PRE_PARSE) &&
467 !(co->opt->flags & M_OPT_PRE_PARSE)) ||
468 ((config->mode != M_COMMAND_LINE_PRE_PARSE) &&
469 (co->opt->flags & M_OPT_PRE_PARSE) && (co->flags & M_CFG_OPT_SET)))
470 set = 0;
472 if (config->includefunc && !bstrcmp0(name, "include")) {
473 return parse_include(config, param, set);
474 } else if (!bstrcmp0(name, "profile"))
475 return parse_profile(config, co->opt, name, param, set);
477 // Option with children are a bit different to parse
478 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) {
479 char prefix[110];
480 assert(strlen(co->name) < 100);
481 sprintf(prefix, "%s:", co->name);
482 return parse_subopts(config, optstruct, co->name, prefix, param, set);
485 void *dst = set ? m_option_get_ptr(co->opt, optstruct) : NULL;
486 int r = m_option_parse(co->opt, name, param, ambiguous_param, dst);
487 // Parsing failed ?
488 if (r < 0)
489 return r;
490 else if (set)
491 co->flags |= M_CFG_OPT_SET;
493 return r;
496 static int parse_subopts(struct m_config *config, void *optstruct, char *name,
497 char *prefix, struct bstr param, bool set)
499 char **lst = NULL;
500 // Split the argument into child options
501 int r = m_option_type_subconfig.parse(NULL, bstr(""), param, false, &lst);
502 if (r < 0)
503 return r;
504 // Parse the child options
505 for (int i = 0; lst && lst[2 * i]; i++) {
506 // Build the full name
507 char n[110];
508 if (snprintf(n, 110, "%s%s", prefix, lst[2 * i]) > 100)
509 abort();
510 int sr = m_config_parse_option(config, optstruct, bstr(n),
511 bstr(lst[2 * i + 1]), false, set);
512 if (sr < 0) {
513 if (sr == M_OPT_UNKNOWN) {
514 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
515 "Error: option '%s' has no suboption '%s'.\n",
516 name, lst[2 * i]);
517 r = M_OPT_INVALID;
518 } else if (sr == M_OPT_MISSING_PARAM) {
519 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
520 "Error: suboption '%s' of '%s' must have "
521 "a parameter!\n", lst[2 * i], name);
522 r = M_OPT_INVALID;
523 } else
524 r = sr;
525 break;
528 talloc_free(lst);
529 return r;
532 int m_config_set_option(struct m_config *config, struct bstr name,
533 struct bstr param, bool ambiguous_param)
535 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Setting %.*s=%.*s\n", BSTR_P(name),
536 BSTR_P(param));
537 return m_config_parse_option(config, config->optstruct, name, param,
538 ambiguous_param, true);
541 int m_config_check_option(struct m_config *config, struct bstr name,
542 struct bstr param, bool ambiguous_param)
544 int r;
545 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name),
546 BSTR_P(param));
547 r = m_config_parse_option(config, NULL, name, param, ambiguous_param, 0);
548 if (r == M_OPT_MISSING_PARAM) {
549 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
550 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name));
551 return M_OPT_INVALID;
553 return r;
556 int m_config_parse_suboptions(struct m_config *config, void *optstruct,
557 char *name, char *subopts)
559 if (!subopts || !*subopts)
560 return 0;
561 return parse_subopts(config, optstruct, name, "", bstr(subopts), true);
565 const struct m_option *m_config_get_option(const struct m_config *config,
566 struct bstr name)
568 struct m_config_option *co;
570 assert(config != NULL);
571 assert(config->lvl > 0);
573 co = m_config_get_co(config, name);
574 if (co)
575 return co->opt;
576 else
577 return NULL;
580 void m_config_print_option_list(const struct m_config *config)
582 char min[50], max[50];
583 struct m_config_option *co;
584 int count = 0;
586 if (!config->opts)
587 return;
589 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO,
590 "\n Name Type Min Max Global CL Cfg\n\n");
591 for (co = config->opts; co; co = co->next) {
592 const struct m_option *opt = co->opt;
593 if (opt->type->flags & M_OPT_TYPE_HAS_CHILD)
594 continue;
595 if (opt->flags & M_OPT_MIN)
596 sprintf(min, "%-8.0f", opt->min);
597 else
598 strcpy(min, "No");
599 if (opt->flags & M_OPT_MAX)
600 sprintf(max, "%-8.0f", opt->max);
601 else
602 strcpy(max, "No");
603 mp_msg(MSGT_CFGPARSER, MSGL_INFO,
604 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
605 co->name,
606 co->opt->type->name,
607 min,
608 max,
609 opt->flags & CONF_GLOBAL ? "Yes" : "No",
610 opt->flags & CONF_NOCMD ? "No" : "Yes",
611 opt->flags & CONF_NOCFG ? "No" : "Yes");
612 count++;
614 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\nTotal: %d options\n", count);
617 struct m_profile *m_config_get_profile(const struct m_config *config,
618 char *name)
620 struct m_profile *p;
621 for (p = config->profiles; p; p = p->next)
622 if (!strcmp(p->name, name))
623 return p;
624 return NULL;
627 struct m_profile *m_config_add_profile(struct m_config *config, char *name)
629 struct m_profile *p = m_config_get_profile(config, name);
630 if (p)
631 return p;
632 p = talloc_zero(config, struct m_profile);
633 p->name = talloc_strdup(p, name);
634 p->next = config->profiles;
635 config->profiles = p;
636 return p;
639 void m_profile_set_desc(struct m_profile *p, char *desc)
641 talloc_free(p->desc);
642 p->desc = talloc_strdup(p, desc);
645 int m_config_set_profile_option(struct m_config *config, struct m_profile *p,
646 char *name, char *val)
648 int i = m_config_check_option0(config, name, val, false);
649 if (i < 0)
650 return i;
651 p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2));
652 p->opts[p->num_opts * 2] = talloc_strdup(p, name);
653 p->opts[p->num_opts * 2 + 1] = talloc_strdup(p, val);
654 p->num_opts++;
655 p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL;
656 return 1;
659 void m_config_set_profile(struct m_config *config, struct m_profile *p)
661 int i;
662 if (config->profile_depth > MAX_PROFILE_DEPTH) {
663 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN,
664 "WARNING: Profile inclusion too deep.\n");
665 return;
667 int prev_mode = config->mode;
668 config->mode = M_CONFIG_FILE;
669 config->profile_depth++;
670 for (i = 0; i < p->num_opts; i++)
671 m_config_set_option0(config, p->opts[2 * i], p->opts[2 * i + 1], false);
672 config->profile_depth--;
673 config->mode = prev_mode;