cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / m_config.c
blob94d7e26b2e414163bed57fe7c11145e97d213e27
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_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;
43 char **list = NULL;
44 int i, r;
45 if (!bstrcmp0(param, "help")) {
46 struct m_profile *p;
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);
61 if (r < 0)
62 return r;
63 if (!list || !list[0])
64 return M_OPT_INVALID;
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",
68 list[i]);
69 r = M_OPT_INVALID;
71 if (dst)
72 m_option_copy(opt, dst, &list);
73 else
74 m_option_free(opt, &list);
75 return r;
78 static void set_profile(const struct m_option *opt, void *dst, const void *src)
80 struct m_config *config = opt->priv;
81 struct m_profile *p;
82 char **list = NULL;
83 int i;
84 if (!src || !*(char ***)src)
85 return;
86 m_option_copy(opt, &list, src);
87 for (i = 0; list[i]; i++) {
88 p = m_config_get_profile(config, list[i]);
89 if (!p)
90 continue;
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;
99 struct m_profile *p;
100 int i, j;
101 if (!param)
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++)
114 spc[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, ','))) {
124 int l = e - list;
125 char tmp[l+1];
126 if (!l)
127 continue;
128 memcpy(tmp, list, l);
129 tmp[l] = '\0';
130 show_profile(opt, name, tmp);
131 list = e + 1;
133 if (list[0] != '\0')
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);
147 return M_OPT_EXIT;
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,
172 const char *prefix, char *disabled_feature);
174 struct m_config *m_config_new(void *optstruct,
175 int includefunc(struct m_option *conf,
176 char *filename))
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 },
185 { NULL }
187 int i;
189 config = talloc_zero(NULL, struct m_config);
190 config->lvl = 1; // 0 Is the defaults
191 if (!initialized) {
192 initialized = 1;
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,
198 sizeof(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);
202 if (includefunc) {
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, NULL);
210 config->optstruct = optstruct;
212 return config;
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)
220 continue;
221 if (copt->opt->type->flags & M_OPT_TYPE_DYNAMIC) {
222 void *ptr = m_option_get_ptr(copt->opt, config->optstruct);
223 if (ptr)
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);
230 talloc_free(config);
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);
241 config->lvl++;
243 for (co = config->opts; co; co = co->next) {
244 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
245 continue;
246 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
247 continue;
248 if (co->flags & M_CFG_OPT_ALIAS)
249 continue;
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;
259 co->slots = slot;
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) {
278 int pop = 0;
279 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
280 continue;
281 if (co->opt->flags & (M_OPT_GLOBAL | M_OPT_NOSAVE))
282 continue;
283 if (co->flags & M_CFG_OPT_ALIAS)
284 continue;
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);
292 slot = co->slots;
293 co->slots = slot->prev;
294 talloc_free(slot);
295 pop++;
297 if (pop) // We removed some ctx -> set the previous value
298 m_option_set(config, co->opt, co->slots->data);
301 config->lvl--;
302 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Config poped level=%d\n", config->lvl);
305 static void add_options(struct m_config *config, const struct m_option *defs,
306 const char *prefix, char *disabled_feature)
308 char *dis = disabled_feature;
309 const char marker[] = "conditional functionality: ";
310 for (int i = 0; defs[i].name; i++) {
311 if (!strncmp(defs[i].name, marker, strlen(marker))) {
312 // If a subconfig entry itself is disabled, everything
313 // under it is already disabled for the same reason.
314 if (!disabled_feature) {
315 if (!strcmp(defs[i].name + strlen(marker), "1"))
316 dis = NULL;
317 else
318 dis = defs[i].p;
320 continue;
322 m_config_add_option(config, defs + i, prefix, dis);
326 static void m_config_add_option(struct m_config *config,
327 const struct m_option *arg, const char *prefix,
328 char *disabled_feature)
330 struct m_config_option *co;
331 struct m_config_save_slot *sl;
333 assert(config != NULL);
334 assert(config->lvl > 0);
335 assert(arg != NULL);
337 // Allocate a new entry for this option
338 co = talloc_zero_size(config,
339 sizeof(struct m_config_option) + arg->type->size);
340 co->opt = arg;
341 co->disabled_feature = disabled_feature;
343 // Fill in the full name
344 if (prefix && *prefix)
345 co->name = talloc_asprintf(co, "%s:%s", prefix, arg->name);
346 else
347 co->name = (char *)arg->name;
349 // Option with children -> add them
350 if (arg->type->flags & M_OPT_TYPE_HAS_CHILD) {
351 add_options(config, arg->p, co->name, disabled_feature);
352 } else {
353 struct m_config_option *i;
354 // Check if there is already an option pointing to this address
355 if (arg->p || arg->new && arg->offset >= 0) {
356 for (i = config->opts; i; i = i->next) {
357 if (arg->new ? (i->opt->new && i->opt->offset == arg->offset)
358 : (!i->opt->new && i->opt->p == arg->p)) {
359 // So we don't save the same vars more than 1 time
360 co->slots = i->slots;
361 co->flags |= M_CFG_OPT_ALIAS;
362 break;
366 if (!(co->flags & M_CFG_OPT_ALIAS)) {
367 // Allocate a slot for the defaults
368 sl = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
369 arg->type->size);
370 m_option_save(config, arg, sl->data);
371 // Hack to avoid too much trouble with dynamically allocated data:
372 // We replace original default and always use a dynamic version
373 if ((arg->type->flags & M_OPT_TYPE_DYNAMIC)) {
374 char **hackptr = m_option_get_ptr(arg, config->optstruct);
375 if (hackptr && *hackptr) {
376 *hackptr = NULL;
377 m_option_set(config, arg, sl->data);
380 sl->lvl = 0;
381 sl->prev = NULL;
382 co->slots = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
383 arg->type->size);
384 co->slots->prev = sl;
385 co->slots->lvl = config->lvl;
386 m_option_copy(co->opt, co->slots->data, sl->data);
389 co->next = config->opts;
390 config->opts = co;
393 int m_config_register_options(struct m_config *config,
394 const struct m_option *args)
396 assert(config != NULL);
397 assert(config->lvl > 0);
398 assert(args != NULL);
400 add_options(config, args, NULL, NULL);
402 return 1;
405 static struct m_config_option *m_config_get_co(const struct m_config *config,
406 struct bstr name)
408 struct m_config_option *co;
410 for (co = config->opts; co; co = co->next) {
411 struct bstr coname = bstr(co->name);
412 if ((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD)
413 && bstr_endswith0(coname, "*")) {
414 coname.len--;
415 if (bstrcasecmp(bstr_splice(name, 0, coname.len), coname) == 0)
416 return co;
417 } else if (bstrcasecmp(coname, name) == 0)
418 return co;
420 return NULL;
423 static int m_config_parse_option(const struct m_config *config,
424 struct bstr name, struct bstr param,
425 bool ambiguous_param, bool set)
427 struct m_config_option *co;
428 int r = 0;
430 assert(config != NULL);
431 assert(config->lvl > 0);
432 assert(name.len != 0);
434 co = m_config_get_co(config, name);
435 if (!co)
436 return M_OPT_UNKNOWN;
437 if (co->disabled_feature) {
438 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
439 "Option \"%.*s\" is not available in this version of mplayer2, "
440 "because it has been compiled with feature \"%s\" disabled.\n",
441 BSTR_P(name), co->disabled_feature);
442 return M_OPT_UNKNOWN;
445 // This is the only mandatory function
446 assert(co->opt->type->parse);
448 // Check if this option isn't forbidden in the current mode
449 if ((config->mode == M_CONFIG_FILE) && (co->opt->flags & M_OPT_NOCFG)) {
450 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
451 "The %.*s option can't be used in a config file.\n",
452 BSTR_P(name));
453 return M_OPT_INVALID;
455 if ((config->mode == M_COMMAND_LINE) && (co->opt->flags & M_OPT_NOCMD)) {
456 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
457 "The %.*s option can't be used on the command line.\n",
458 BSTR_P(name));
459 return M_OPT_INVALID;
461 // During command line preparse set only pre-parse options
462 // Otherwise only set pre-parse option if they were not already set.
463 if (((config->mode == M_COMMAND_LINE_PRE_PARSE) &&
464 !(co->opt->flags & M_OPT_PRE_PARSE)) ||
465 ((config->mode != M_COMMAND_LINE_PRE_PARSE) &&
466 (co->opt->flags & M_OPT_PRE_PARSE) && (co->flags & M_CFG_OPT_SET)))
467 set = 0;
469 // Option with children are a bit different to parse
470 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) {
471 char **lst = NULL;
472 int i, sr;
473 // Parse the child options
474 r = m_option_parse(co->opt, name, param, false, &lst);
475 // Set them now
476 if (r >= 0)
477 for (i = 0; lst && lst[2 * i]; i++) {
478 int l = strlen(co->name) + 1 + strlen(lst[2 * i]) + 1;
479 if (r >= 0) {
480 // Build the full name
481 char n[l];
482 sprintf(n, "%s:%s", co->name, lst[2 * i]);
483 sr = m_config_parse_option(config, bstr(n),
484 bstr(lst[2 * i + 1]), false,
485 set);
486 if (sr < 0) {
487 if (sr == M_OPT_UNKNOWN) {
488 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
489 "Error: option '%s' has no suboption '%s'.\n",
490 co->name, lst[2 * i]);
491 r = M_OPT_INVALID;
492 } else if (sr == M_OPT_MISSING_PARAM) {
493 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
494 "Error: suboption '%s' of '%s' must have "
495 "a parameter!\n", lst[2 * i], co->name);
496 r = M_OPT_INVALID;
497 } else
498 r = sr;
501 talloc_free(lst[2 * i]);
502 talloc_free(lst[2 * i + 1]);
504 talloc_free(lst);
505 } else
506 r = m_option_parse(co->opt, name, param, ambiguous_param,
507 set ? co->slots->data : NULL);
509 // Parsing failed ?
510 if (r < 0)
511 return r;
512 // Set the option
513 if (set) {
514 m_option_set(config, co->opt, co->slots->data);
515 co->flags |= M_CFG_OPT_SET;
518 return r;
521 int m_config_set_option(struct m_config *config, struct bstr name,
522 struct bstr param, bool ambiguous_param)
524 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Setting %.*s=%.*s\n", BSTR_P(name),
525 BSTR_P(param));
526 return m_config_parse_option(config, name, param, ambiguous_param, 1);
529 int m_config_check_option(const struct m_config *config, struct bstr name,
530 struct bstr param, bool ambiguous_param)
532 int r;
533 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name),
534 BSTR_P(param));
535 r = m_config_parse_option(config, name, param, ambiguous_param, 0);
536 if (r == M_OPT_MISSING_PARAM) {
537 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
538 "Error: option '%.*s' must have a parameter!\n", BSTR_P(name));
539 return M_OPT_INVALID;
541 return r;
545 const struct m_option *m_config_get_option(const struct m_config *config,
546 struct bstr name)
548 struct m_config_option *co;
550 assert(config != NULL);
551 assert(config->lvl > 0);
553 co = m_config_get_co(config, name);
554 if (co)
555 return co->opt;
556 else
557 return NULL;
560 void m_config_print_option_list(const struct m_config *config)
562 char min[50], max[50];
563 struct m_config_option *co;
564 int count = 0;
566 if (!config->opts)
567 return;
569 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO,
570 "\n Name Type Min Max Global CL Cfg\n\n");
571 for (co = config->opts; co; co = co->next) {
572 const struct m_option *opt = co->opt;
573 if (opt->type->flags & M_OPT_TYPE_HAS_CHILD)
574 continue;
575 if (opt->flags & M_OPT_MIN)
576 sprintf(min, "%-8.0f", opt->min);
577 else
578 strcpy(min, "No");
579 if (opt->flags & M_OPT_MAX)
580 sprintf(max, "%-8.0f", opt->max);
581 else
582 strcpy(max, "No");
583 mp_msg(MSGT_CFGPARSER, MSGL_INFO,
584 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
585 co->name,
586 co->opt->type->name,
587 min,
588 max,
589 opt->flags & CONF_GLOBAL ? "Yes" : "No",
590 opt->flags & CONF_NOCMD ? "No" : "Yes",
591 opt->flags & CONF_NOCFG ? "No" : "Yes");
592 count++;
594 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\nTotal: %d options\n", count);
597 struct m_profile *m_config_get_profile(const struct m_config *config,
598 char *name)
600 struct m_profile *p;
601 for (p = config->profiles; p; p = p->next)
602 if (!strcmp(p->name, name))
603 return p;
604 return NULL;
607 struct m_profile *m_config_add_profile(struct m_config *config, char *name)
609 struct m_profile *p = m_config_get_profile(config, name);
610 if (p)
611 return p;
612 p = talloc_zero(config, struct m_profile);
613 p->name = talloc_strdup(p, name);
614 p->next = config->profiles;
615 config->profiles = p;
616 return p;
619 void m_profile_set_desc(struct m_profile *p, char *desc)
621 talloc_free(p->desc);
622 p->desc = talloc_strdup(p, desc);
625 int m_config_set_profile_option(struct m_config *config, struct m_profile *p,
626 char *name, char *val)
628 int i = m_config_check_option0(config, name, val, false);
629 if (i < 0)
630 return i;
631 p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2));
632 p->opts[p->num_opts * 2] = talloc_strdup(p, name);
633 p->opts[p->num_opts * 2 + 1] = talloc_strdup(p, val);
634 p->num_opts++;
635 p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL;
636 return 1;
639 void m_config_set_profile(struct m_config *config, struct m_profile *p)
641 int i;
642 if (config->profile_depth > MAX_PROFILE_DEPTH) {
643 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN,
644 "WARNING: Profile inclusion too deep.\n");
645 return;
647 int prev_mode = config->mode;
648 config->mode = M_CONFIG_FILE;
649 config->profile_depth++;
650 for (i = 0; i < p->num_opts; i++)
651 m_config_set_option0(config, p->opts[2 * i], p->opts[2 * i + 1], false);
652 config->profile_depth--;
653 config->mode = prev_mode;