options: simplify option parsing/setting machinery
[mplayer.git] / m_config.c
blob3da7707f147a499f23a5f88fba737e2456a33345
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(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,
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->lvl = 1; // 0 Is the defaults
180 struct m_option *self_opts = talloc_memdup(config, ref_opts,
181 sizeof(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);
185 if (includefunc) {
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;
195 return config;
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)
203 continue;
204 if (copt->opt->type->flags & M_OPT_TYPE_DYNAMIC) {
205 void *ptr = m_option_get_ptr(copt->opt, config->optstruct);
206 if (ptr)
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);
213 talloc_free(config);
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);
224 config->lvl++;
226 for (co = config->opts; co; co = co->next) {
227 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
228 continue;
229 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
230 continue;
231 if (co->flags & M_CFG_OPT_ALIAS)
232 continue;
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;
242 co->slots = slot;
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) {
261 int pop = 0;
262 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
263 continue;
264 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
265 continue;
266 if (co->flags & M_CFG_OPT_ALIAS)
267 continue;
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);
275 slot = co->slots;
276 co->slots = slot->prev;
277 talloc_free(slot);
278 pop++;
280 if (pop) // We removed some ctx -> set the previous value
281 m_option_set(config, co->opt, co->slots->data);
284 config->lvl--;
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"))
299 dis = NULL;
300 else
301 dis = defs[i].p;
303 continue;
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);
318 assert(arg != NULL);
320 // Allocate a new entry for this option
321 co = talloc_zero_size(config,
322 sizeof(struct m_config_option) + arg->type->size);
323 co->opt = arg;
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);
329 else
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);
335 } else {
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;
345 break;
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) +
352 arg->type->size);
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) {
359 *hackptr = NULL;
360 m_option_set(config, arg, sl->data);
363 sl->lvl = 0;
364 sl->prev = NULL;
365 co->slots = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
366 arg->type->size);
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;
373 config->opts = co;
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);
385 return 1;
388 static struct m_config_option *m_config_get_co(const struct m_config *config,
389 struct bstr name)
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, "*")) {
397 coname.len--;
398 if (bstrcasecmp(bstr_splice(name, 0, coname.len), coname) == 0)
399 return co;
400 } else if (bstrcasecmp(coname, name) == 0)
401 return co;
403 return NULL;
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);
415 if (!co)
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",
432 BSTR_P(name));
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",
438 BSTR_P(name));
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)))
447 set = 0;
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) {
456 char **lst = NULL;
457 // Parse the child options
458 int r = m_option_parse(co->opt, name, param, false, &lst);
459 // Set them now
460 if (r >= 0)
461 for (int i = 0; lst && lst[2 * i]; i++) {
462 int l = strlen(co->name) + 1 + strlen(lst[2 * i]) + 1;
463 if (r >= 0) {
464 // Build the full name
465 char n[l];
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,
469 set);
470 if (sr < 0) {
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]);
475 r = M_OPT_INVALID;
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);
480 r = M_OPT_INVALID;
481 } else
482 r = sr;
485 talloc_free(lst[2 * i]);
486 talloc_free(lst[2 * i + 1]);
488 talloc_free(lst);
489 return r;
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);
494 // Parsing failed ?
495 if (r < 0)
496 return r;
497 else if (set)
498 co->flags |= M_CFG_OPT_SET;
500 return r;
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),
507 BSTR_P(param));
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)
514 int r;
515 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name),
516 BSTR_P(param));
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;
523 return r;
527 const struct m_option *m_config_get_option(const struct m_config *config,
528 struct bstr name)
530 struct m_config_option *co;
532 assert(config != NULL);
533 assert(config->lvl > 0);
535 co = m_config_get_co(config, name);
536 if (co)
537 return co->opt;
538 else
539 return NULL;
542 void m_config_print_option_list(const struct m_config *config)
544 char min[50], max[50];
545 struct m_config_option *co;
546 int count = 0;
548 if (!config->opts)
549 return;
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)
556 continue;
557 if (opt->flags & M_OPT_MIN)
558 sprintf(min, "%-8.0f", opt->min);
559 else
560 strcpy(min, "No");
561 if (opt->flags & M_OPT_MAX)
562 sprintf(max, "%-8.0f", opt->max);
563 else
564 strcpy(max, "No");
565 mp_msg(MSGT_CFGPARSER, MSGL_INFO,
566 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
567 co->name,
568 co->opt->type->name,
569 min,
570 max,
571 opt->flags & CONF_GLOBAL ? "Yes" : "No",
572 opt->flags & CONF_NOCMD ? "No" : "Yes",
573 opt->flags & CONF_NOCFG ? "No" : "Yes");
574 count++;
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,
580 char *name)
582 struct m_profile *p;
583 for (p = config->profiles; p; p = p->next)
584 if (!strcmp(p->name, name))
585 return p;
586 return NULL;
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);
592 if (p)
593 return p;
594 p = talloc_zero(config, struct m_profile);
595 p->name = talloc_strdup(p, name);
596 p->next = config->profiles;
597 config->profiles = p;
598 return 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);
611 if (i < 0)
612 return i;
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);
616 p->num_opts++;
617 p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL;
618 return 1;
621 void m_config_set_profile(struct m_config *config, struct m_profile *p)
623 int i;
624 if (config->profile_depth > MAX_PROFILE_DEPTH) {
625 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN,
626 "WARNING: Profile inclusion too deep.\n");
627 return;
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;