talloc.[ch]: remove "type safety" hack that violates C types
[mplayer.git] / m_config.c
blob3cf8c721accf2452dd11429a4ccc0dd12752f007
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);
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);
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 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);
313 assert(arg != NULL);
315 // Allocate a new entry for this option
316 co = talloc_zero_size(config,
317 sizeof(struct m_config_option) + arg->type->size);
318 co->opt = arg;
320 // Fill in the full name
321 if (prefix && *prefix)
322 co->name = talloc_asprintf(co, "%s:%s", prefix, arg->name);
323 else
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;
329 int i;
330 co->slots = NULL;
331 for (i = 0; ol[i].name != NULL; i++)
332 m_config_add_option(config, &ol[i], co->name);
333 } else {
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;
343 break;
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) +
350 arg->type->size);
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) {
357 *hackptr = NULL;
358 m_option_set(config, arg, sl->data);
361 sl->lvl = 0;
362 sl->prev = NULL;
363 co->slots = talloc_zero_size(co, sizeof(struct m_config_save_slot) +
364 arg->type->size);
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;
371 config->opts = co;
374 int m_config_register_options(struct m_config *config,
375 const struct m_option *args)
377 int i;
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);
386 return 1;
389 static struct m_config_option *m_config_get_co(const struct m_config *config,
390 struct bstr name)
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, "*")) {
398 coname.len--;
399 if (bstrcasecmp(bstr_splice(name, 0, coname.len), coname) == 0)
400 return co;
401 } else if (bstrcasecmp(coname, name) == 0)
402 return co;
404 return NULL;
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;
412 int r = 0;
414 assert(config != NULL);
415 assert(config->lvl > 0);
416 assert(name.len != 0);
418 co = m_config_get_co(config, name);
419 if (!co)
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",
429 BSTR_P(name));
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",
435 BSTR_P(name));
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)))
444 set = 0;
446 // Option with children are a bit different to parse
447 if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) {
448 char **lst = NULL;
449 int i, sr;
450 // Parse the child options
451 r = m_option_parse(co->opt, name, param, false, &lst);
452 // Set them now
453 if (r >= 0)
454 for (i = 0; lst && lst[2 * i]; i++) {
455 int l = strlen(co->name) + 1 + strlen(lst[2 * i]) + 1;
456 if (r >= 0) {
457 // Build the full name
458 char n[l];
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,
462 set);
463 if (sr < 0) {
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]);
468 r = M_OPT_INVALID;
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);
473 r = M_OPT_INVALID;
474 } else
475 r = sr;
478 talloc_free(lst[2 * i]);
479 talloc_free(lst[2 * i + 1]);
481 talloc_free(lst);
482 } else
483 r = m_option_parse(co->opt, name, param, ambiguous_param,
484 set ? co->slots->data : NULL);
486 // Parsing failed ?
487 if (r < 0)
488 return r;
489 // Set the option
490 if (set) {
491 m_option_set(config, co->opt, co->slots->data);
492 co->flags |= M_CFG_OPT_SET;
495 return r;
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),
502 BSTR_P(param));
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)
509 int r;
510 mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name),
511 BSTR_P(param));
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;
518 return r;
522 const struct m_option *m_config_get_option(const struct m_config *config,
523 struct bstr name)
525 struct m_config_option *co;
527 assert(config != NULL);
528 assert(config->lvl > 0);
530 co = m_config_get_co(config, name);
531 if (co)
532 return co->opt;
533 else
534 return NULL;
537 void m_config_print_option_list(const struct m_config *config)
539 char min[50], max[50];
540 struct m_config_option *co;
541 int count = 0;
543 if (!config->opts)
544 return;
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)
551 continue;
552 if (opt->flags & M_OPT_MIN)
553 sprintf(min, "%-8.0f", opt->min);
554 else
555 strcpy(min, "No");
556 if (opt->flags & M_OPT_MAX)
557 sprintf(max, "%-8.0f", opt->max);
558 else
559 strcpy(max, "No");
560 mp_msg(MSGT_CFGPARSER, MSGL_INFO,
561 " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
562 co->name,
563 co->opt->type->name,
564 min,
565 max,
566 opt->flags & CONF_GLOBAL ? "Yes" : "No",
567 opt->flags & CONF_NOCMD ? "No" : "Yes",
568 opt->flags & CONF_NOCFG ? "No" : "Yes");
569 count++;
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,
575 char *name)
577 struct m_profile *p;
578 for (p = config->profiles; p; p = p->next)
579 if (!strcmp(p->name, name))
580 return p;
581 return NULL;
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);
587 if (p)
588 return p;
589 p = talloc_zero(config, struct m_profile);
590 p->name = talloc_strdup(p, name);
591 p->next = config->profiles;
592 config->profiles = p;
593 return 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);
606 if (i < 0)
607 return i;
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);
611 p->num_opts++;
612 p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL;
613 return 1;
616 void m_config_set_profile(struct m_config *config, struct m_profile *p)
618 int i;
619 if (config->profile_depth > MAX_PROFILE_DEPTH) {
620 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN,
621 "WARNING: Profile inclusion too deep.\n");
622 return;
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;