From eb15151705d47d23da844449126cc6b4879f110e Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 17 Dec 2013 02:02:25 +0100 Subject: Move options/config related files from mpvcore/ to options/ Since m_option.h and options.h are extremely often included, a lot of files have to be changed. Moving path.c/h to options/ is a bit questionable, but since this is mainly about access to config files (which are also handled in options/), it's probably ok. --- options/m_config.c | 755 +++++++++++++ options/m_config.h | 216 ++++ options/m_option.c | 2477 +++++++++++++++++++++++++++++++++++++++++++ options/m_option.h | 648 +++++++++++ options/m_property.c | 382 +++++++ options/m_property.h | 142 +++ options/options.c | 886 ++++++++++++++++ options/options.h | 297 ++++++ options/parse_commandline.c | 294 +++++ options/parse_commandline.h | 33 + options/parse_configfile.c | 277 +++++ options/parse_configfile.h | 27 + options/path.c | 238 +++++ options/path.h | 75 ++ 14 files changed, 6747 insertions(+) create mode 100644 options/m_config.c create mode 100644 options/m_config.h create mode 100644 options/m_option.c create mode 100644 options/m_option.h create mode 100644 options/m_property.c create mode 100644 options/m_property.h create mode 100644 options/options.c create mode 100644 options/options.h create mode 100644 options/parse_commandline.c create mode 100644 options/parse_commandline.h create mode 100644 options/parse_configfile.c create mode 100644 options/parse_configfile.h create mode 100644 options/path.c create mode 100644 options/path.h (limited to 'options') diff --git a/options/m_config.c b/options/m_config.c new file mode 100644 index 0000000000..5040482c84 --- /dev/null +++ b/options/m_config.c @@ -0,0 +1,755 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/// \file +/// \ingroup Config + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "talloc.h" + +#include "m_config.h" +#include "options/m_option.h" +#include "mpvcore/mp_msg.h" + +static const union m_option_value default_value; + +// Profiles allow to predefine some sets of options that can then +// be applied later on with the internal -profile option. +#define MAX_PROFILE_DEPTH 20 + +struct m_profile { + struct m_profile *next; + char *name; + char *desc; + int num_opts; + // Option/value pair array. + char **opts; +}; + +// In the file local case, this contains the old global value. +struct m_opt_backup { + struct m_opt_backup *next; + struct m_config_option *co; + void *backup; +}; + +static int parse_include(struct m_config *config, struct bstr param, bool set, + int flags) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + if (!set) + return 1; + char *filename = bstrdup0(NULL, param); + config->includefunc(config, filename, flags); + talloc_free(filename); + return 1; +} + +static int parse_profile(struct m_config *config, const struct m_option *opt, + struct bstr name, struct bstr param, bool set, int flags) +{ + if (!bstrcmp0(param, "help")) { + struct m_profile *p; + if (!config->profiles) { + mp_msg(MSGT_CFGPARSER, MSGL_INFO, + "No profiles have been defined.\n"); + return M_OPT_EXIT - 1; + } + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Available profiles:\n"); + for (p = config->profiles; p; p = p->next) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\t%s\t%s\n", p->name, + p->desc ? p->desc : ""); + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n"); + return M_OPT_EXIT - 1; + } + + char **list = NULL; + int r = m_option_type_string_list.parse(opt, name, param, &list); + if (r < 0) + return r; + if (!list || !list[0]) + return M_OPT_INVALID; + for (int i = 0; list[i]; i++) { + struct m_profile *p = m_config_get_profile0(config, list[i]); + if (!p) { + mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Unknown profile '%s'.\n", + list[i]); + r = M_OPT_INVALID; + } else if (set) + m_config_set_profile(config, p, flags); + } + m_option_free(opt, &list); + return r; +} + +static int show_profile(struct m_config *config, bstr param) +{ + struct m_profile *p; + int i, j; + if (!param.len) + return M_OPT_MISSING_PARAM; + if (!(p = m_config_get_profile(config, param))) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown profile '%.*s'.\n", + BSTR_P(param)); + return M_OPT_EXIT - 1; + } + if (!config->profile_depth) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Profile %s: %s\n", p->name, + p->desc ? p->desc : ""); + config->profile_depth++; + for (i = 0; i < p->num_opts; i++) { + char spc[config->profile_depth + 1]; + for (j = 0; j < config->profile_depth; j++) + spc[j] = ' '; + spc[config->profile_depth] = '\0'; + + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s%s=%s\n", spc, + p->opts[2 * i], p->opts[2 * i + 1]); + + if (config->profile_depth < MAX_PROFILE_DEPTH + && !strcmp(p->opts[2*i], "profile")) { + char *e, *list = p->opts[2 * i + 1]; + while ((e = strchr(list, ','))) { + int l = e - list; + char tmp[l+1]; + if (!l) + continue; + memcpy(tmp, list, l); + tmp[l] = '\0'; + show_profile(config, bstr0(tmp)); + list = e + 1; + } + if (list[0] != '\0') + show_profile(config, bstr0(list)); + } + } + config->profile_depth--; + if (!config->profile_depth) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n"); + return M_OPT_EXIT - 1; +} + +static int list_options(struct m_config *config) +{ + m_config_print_option_list(config); + return M_OPT_EXIT; +} + +// The memcpys are supposed to work around the strict aliasing violation, +// that would result if we just dereferenced a void** (where the void** is +// actually casted from struct some_type* ). +static void *substruct_read_ptr(const void *ptr) +{ + void *res; + memcpy(&res, ptr, sizeof(void*)); + return res; +} +static void substruct_write_ptr(void *ptr, void *val) +{ + memcpy(ptr, &val, sizeof(void*)); +} + +static void add_options(struct m_config *config, + const char *parent_name, + void *optstruct, + const void *optstruct_def, + const struct m_option *defs); + +static void config_destroy(void *p) +{ + struct m_config *config = p; + m_config_restore_backups(config); + for (int n = 0; n < config->num_opts; n++) + m_option_free(config->opts[n].opt, config->opts[n].data); +} + +struct m_config *m_config_new(void *talloc_ctx, size_t size, + const void *defaults, + const struct m_option *options) +{ + struct m_config *config = talloc(talloc_ctx, struct m_config); + talloc_set_destructor(config, config_destroy); + *config = (struct m_config) {0}; + // size==0 means a dummy object is created + if (size) { + config->optstruct = talloc_zero_size(config, size); + if (defaults) + memcpy(config->optstruct, defaults, size); + } + if (options) + add_options(config, "", config->optstruct, defaults, options); + return config; +} + +struct m_config *m_config_from_obj_desc(void *talloc_ctx, + struct m_obj_desc *desc) +{ + return m_config_new(talloc_ctx, desc->priv_size, desc->priv_defaults, + desc->options); +} + +// Like m_config_from_obj_desc(), but don't allocate option struct. +struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx, + struct m_obj_desc *desc) +{ + return m_config_new(talloc_ctx, 0, desc->priv_defaults, desc->options); +} + +int m_config_set_obj_params(struct m_config *conf, char **args) +{ + for (int n = 0; args && args[n * 2 + 0]; n++) { + int r = m_config_set_option(conf, bstr0(args[n * 2 + 0]), + bstr0(args[n * 2 + 1])); + if (r < 0) + return r; + } + return 0; +} + +int m_config_apply_defaults(struct m_config *config, const char *name, + struct m_obj_settings *defaults) +{ + int r = 0; + for (int n = 0; defaults && defaults[n].name; n++) { + struct m_obj_settings *entry = &defaults[n]; + if (name && strcmp(entry->name, name) == 0) { + r = m_config_set_obj_params(config, entry->attribs); + break; + } + } + return r; +} + +static void ensure_backup(struct m_config *config, struct m_config_option *co) +{ + if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) + return; + if (co->opt->flags & M_OPT_GLOBAL) + return; + if (!co->data) + return; + for (struct m_opt_backup *cur = config->backup_opts; cur; cur = cur->next) { + if (cur->co->data == co->data) // comparing data ptr catches aliases + return; + } + struct m_opt_backup *bc = talloc_ptrtype(NULL, bc); + *bc = (struct m_opt_backup) { + .co = co, + .backup = talloc_zero_size(bc, co->opt->type->size), + }; + m_option_copy(co->opt, bc->backup, co->data); + bc->next = config->backup_opts; + config->backup_opts = bc; +} + +void m_config_restore_backups(struct m_config *config) +{ + while (config->backup_opts) { + struct m_opt_backup *bc = config->backup_opts; + config->backup_opts = bc->next; + + m_option_copy(bc->co->opt, bc->co->data, bc->backup); + m_option_free(bc->co->opt, bc->backup); + talloc_free(bc); + } +} + +void m_config_backup_opt(struct m_config *config, const char *opt) +{ + struct m_config_option *co = m_config_get_co(config, bstr0(opt)); + if (co) { + ensure_backup(config, co); + } else { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s not found.\n", opt); + } +} + +void m_config_backup_all_opts(struct m_config *config) +{ + for (int n = 0; n < config->num_opts; n++) + ensure_backup(config, &config->opts[n]); +} + +// Given an option --opt, add --no-opt (if applicable). +static void add_negation_option(struct m_config *config, + struct m_config_option *orig, + const char *parent_name) +{ + const struct m_option *opt = orig->opt; + int value; + if (opt->type == CONF_TYPE_FLAG) { + value = opt->min; + } else if (opt->type == CONF_TYPE_CHOICE) { + // Find out whether there's a "no" choice. + // m_option_parse() should be used for this, but it prints + // unsilenceable error messages. + struct m_opt_choice_alternatives *alt = opt->priv; + for ( ; alt->name; alt++) { + if (strcmp(alt->name, "no") == 0) + break; + } + if (!alt->name) + return; + value = alt->value; + } else { + return; + } + struct m_option *no_opt = talloc_ptrtype(config, no_opt); + *no_opt = (struct m_option) { + .name = opt->name, + .type = CONF_TYPE_STORE, + .flags = opt->flags & (M_OPT_NOCFG | M_OPT_GLOBAL | M_OPT_PRE_PARSE), + .is_new_option = opt->is_new_option, + .p = opt->p, + .offset = opt->offset, + .max = value, + }; + // Add --no-sub-opt + struct m_config_option co = *orig; + co.name = talloc_asprintf(config, "no-%s", orig->name); + co.opt = no_opt; + co.is_generated = true; + MP_TARRAY_APPEND(config, config->opts, config->num_opts, co); + // Add --sub-no-opt (unfortunately needed for: "--sub=...:no-opt") + if (parent_name[0]) { + co.name = talloc_asprintf(config, "%s-no-%s", parent_name, opt->name); + MP_TARRAY_APPEND(config, config->opts, config->num_opts, co); + } +} + +static void m_config_add_option(struct m_config *config, + const char *parent_name, + void *optstruct, + const void *optstruct_def, + const struct m_option *arg); + +static void add_options(struct m_config *config, + const char *parent_name, + void *optstruct, + const void *optstruct_def, + const struct m_option *defs) +{ + for (int i = 0; defs && defs[i].name; i++) + m_config_add_option(config, parent_name, optstruct, optstruct_def, &defs[i]); +} + +static void m_config_add_option(struct m_config *config, + const char *parent_name, + void *optstruct, + const void *optstruct_def, + const struct m_option *arg) +{ + assert(config != NULL); + assert(arg != NULL); + + struct m_config_option co = { + .opt = arg, + .name = arg->name, + }; + + if (arg->is_new_option) { + if (optstruct) + co.data = (char *)optstruct + arg->offset; + if (optstruct_def) + co.default_data = (char *)optstruct_def + arg->offset; + } else { + co.data = arg->p; + co.default_data = arg->p; + } + + if (arg->defval) + co.default_data = arg->defval; + + if (!co.default_data) + co.default_data = &default_value; + + // Fill in the full name + if (!co.name[0]) { + co.name = parent_name; + } else if (parent_name[0]) { + co.name = talloc_asprintf(config, "%s-%s", parent_name, co.name); + } + + // Option with children -> add them + if (arg->type->flags & M_OPT_TYPE_HAS_CHILD) { + if (arg->type->flags & M_OPT_TYPE_USE_SUBSTRUCT) { + const struct m_sub_options *subopts = arg->priv; + + void *new_optstruct = NULL; + if (co.data) { + new_optstruct = m_config_alloc_struct(config, subopts); + substruct_write_ptr(co.data, new_optstruct); + } + + const void *new_optstruct_def = substruct_read_ptr(co.default_data); + if (!new_optstruct_def) + new_optstruct_def = subopts->defaults; + + add_options(config, co.name, new_optstruct, + new_optstruct_def, subopts->opts); + } else { + const struct m_option *sub = arg->p; + add_options(config, co.name, optstruct, optstruct_def, sub); + } + } else { + // Initialize options + if (co.data && co.default_data) { + if (arg->type->flags & M_OPT_TYPE_DYNAMIC) { + // Would leak memory by overwriting *co.data repeatedly. + for (int i = 0; i < config->num_opts; i++) { + if (co.data == config->opts[i].data) + assert(0); + } + } + // In case this is dynamic data, it has to be allocated and copied. + union m_option_value temp = {0}; + memcpy(&temp, co.default_data, arg->type->size); + memset(co.data, 0, arg->type->size); + m_option_copy(arg, co.data, &temp); + } + } + + if (arg->name[0]) // no own name -> hidden + MP_TARRAY_APPEND(config, config->opts, config->num_opts, co); + + add_negation_option(config, &co, parent_name); +} + +struct m_config_option *m_config_get_co(const struct m_config *config, + struct bstr name) +{ + + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co = &config->opts[n]; + struct bstr coname = bstr0(co->name); + if ((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD) + && bstr_endswith0(coname, "*")) { + coname.len--; + if (bstrcmp(bstr_splice(name, 0, coname.len), coname) == 0) + return co; + } else if (bstrcmp(coname, name) == 0) + return co; + } + return NULL; +} + +const char *m_config_get_positional_option(const struct m_config *config, int p) +{ + int pos = 0; + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co = &config->opts[n]; + if (!co->is_generated) { + if (pos == p) + return co->name; + pos++; + } + } + return NULL; +} + +static int parse_subopts(struct m_config *config, char *name, char *prefix, + struct bstr param, int flags); + +static int m_config_parse_option(struct m_config *config, struct bstr name, + struct bstr param, int flags) +{ + assert(config != NULL); + assert(name.len != 0); + bool set = !(flags & M_SETOPT_CHECK_ONLY); + + struct m_config_option *co = m_config_get_co(config, name); + if (!co) + return M_OPT_UNKNOWN; + + // This is the only mandatory function + assert(co->opt->type->parse); + + if ((flags & M_SETOPT_PRE_PARSE_ONLY) && !(co->opt->flags & M_OPT_PRE_PARSE)) + return 0; + + if ((flags & M_SETOPT_PRESERVE_CMDLINE) && co->is_set_from_cmdline) + set = false; + + // Check if this option isn't forbidden in the current mode + if ((flags & M_SETOPT_FROM_CONFIG_FILE) && (co->opt->flags & M_OPT_NOCFG)) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option can't be used in a config file.\n", + BSTR_P(name)); + return M_OPT_INVALID; + } + if (flags & M_SETOPT_BACKUP) { + if (co->opt->flags & M_OPT_GLOBAL) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option is global and can't be set per-file.\n", + BSTR_P(name)); + return M_OPT_INVALID; + } + if (set) + ensure_backup(config, co); + } + + if (config->includefunc && bstr_equals0(name, "include")) + return parse_include(config, param, set, flags); + if (config->use_profiles && bstr_equals0(name, "profile")) + return parse_profile(config, co->opt, name, param, set, flags); + if (config->use_profiles && bstr_equals0(name, "show-profile")) + return show_profile(config, param); + if (bstr_equals0(name, "list-options")) + return list_options(config); + + // Option with children are a bit different to parse + if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) { + char prefix[110]; + assert(strlen(co->name) < 100); + sprintf(prefix, "%s-", co->name); + return parse_subopts(config, (char *)co->name, prefix, param, flags); + } + + int r = m_option_parse(co->opt, name, param, set ? co->data : NULL); + + if (r >= 0 && set && (flags & M_SETOPT_FROM_CMDLINE)) { + co->is_set_from_cmdline = true; + // Mark aliases too + if (co->data) { + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co2 = &config->opts[n]; + if (co2->data == co->data) + co2->is_set_from_cmdline = true; + } + } + } + + return r; +} + +static int parse_subopts(struct m_config *config, char *name, char *prefix, + struct bstr param, int flags) +{ + char **lst = NULL; + // Split the argument into child options + int r = m_option_type_subconfig.parse(NULL, bstr0(""), param, &lst); + if (r < 0) + return r; + // Parse the child options + for (int i = 0; lst && lst[2 * i]; i++) { + // Build the full name + char n[110]; + if (snprintf(n, 110, "%s%s", prefix, lst[2 * i]) > 100) + abort(); + r = m_config_parse_option(config,bstr0(n), bstr0(lst[2 * i + 1]), flags); + if (r < 0) { + if (r > M_OPT_EXIT) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Error parsing suboption %s/%s (%s)\n", + name, lst[2 * i], m_option_strerror(r)); + r = M_OPT_INVALID; + } + break; + } + } + talloc_free(lst); + return r; +} + +int m_config_parse_suboptions(struct m_config *config, char *name, + char *subopts) +{ + if (!subopts || !*subopts) + return 0; + int r = parse_subopts(config, name, "", bstr0(subopts), 0); + if (r < 0 && r > M_OPT_EXIT) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Error parsing suboption %s (%s)\n", + name, m_option_strerror(r)); + r = M_OPT_INVALID; + } + return r; +} + +int m_config_set_option_ext(struct m_config *config, struct bstr name, + struct bstr param, int flags) +{ + int r = m_config_parse_option(config, name, param, flags); + if (r < 0 && r > M_OPT_EXIT) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Error parsing option %.*s (%s)\n", + BSTR_P(name), m_option_strerror(r)); + r = M_OPT_INVALID; + } + return r; +} + +int m_config_set_option(struct m_config *config, struct bstr name, + struct bstr param) +{ + return m_config_set_option_ext(config, name, param, 0); +} + +const struct m_option *m_config_get_option(const struct m_config *config, + struct bstr name) +{ + assert(config != NULL); + + struct m_config_option *co = m_config_get_co(config, name); + return co ? co->opt : NULL; +} + +int m_config_option_requires_param(struct m_config *config, bstr name) +{ + const struct m_option *opt = m_config_get_option(config, name); + if (opt) { + if (bstr_endswith0(name, "-clr")) + return 0; + return m_option_required_params(opt); + } + return M_OPT_UNKNOWN; +} + +void m_config_print_option_list(const struct m_config *config) +{ + char min[50], max[50]; + int count = 0; + const char *prefix = config->is_toplevel ? "--" : ""; + + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Options:\n\n"); + for (int i = 0; i < config->num_opts; i++) { + struct m_config_option *co = &config->opts[i]; + const struct m_option *opt = co->opt; + if (opt->type->flags & M_OPT_TYPE_HAS_CHILD) + continue; + if (co->is_generated) + continue; + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s%-30.30s", prefix, co->name); + if (opt->type == &m_option_type_choice) { + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " Choices:"); + struct m_opt_choice_alternatives *alt = opt->priv; + for (int n = 0; alt[n].name; n++) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s", alt[n].name); + if (opt->flags & (M_OPT_MIN | M_OPT_MAX)) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " (or an integer)"); + } else { + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s", co->opt->type->name); + } + if (opt->flags & (M_OPT_MIN | M_OPT_MAX)) { + snprintf(min, sizeof(min), "any"); + snprintf(max, sizeof(max), "any"); + if (opt->flags & M_OPT_MIN) + snprintf(min, sizeof(min), "%.14g", opt->min); + if (opt->flags & M_OPT_MAX) + snprintf(max, sizeof(max), "%.14g", opt->max); + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " (%s to %s)", min, max); + } + char *def = NULL; + if (co->default_data) + def = m_option_print(co->opt, co->default_data); + if (def) { + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " (default: %s)", def); + talloc_free(def); + } + if (opt->flags & CONF_GLOBAL) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " [global]"); + if (opt->flags & CONF_NOCFG) + mp_msg(MSGT_CFGPARSER, MSGL_INFO, " [nocfg]"); + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n"); + count++; + } + mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\nTotal: %d options\n", count); +} + +struct m_profile *m_config_get_profile(const struct m_config *config, bstr name) +{ + for (struct m_profile *p = config->profiles; p; p = p->next) { + if (bstr_equals0(name, p->name)) + return p; + } + return NULL; +} + +struct m_profile *m_config_get_profile0(const struct m_config *config, + char *name) +{ + return m_config_get_profile(config, bstr0(name)); +} + +struct m_profile *m_config_add_profile(struct m_config *config, char *name) +{ + struct m_profile *p = m_config_get_profile0(config, name); + if (p) + return p; + p = talloc_zero(config, struct m_profile); + p->name = talloc_strdup(p, name); + p->next = config->profiles; + config->profiles = p; + return p; +} + +void m_profile_set_desc(struct m_profile *p, bstr desc) +{ + talloc_free(p->desc); + p->desc = bstrdup0(p, desc); +} + +int m_config_set_profile_option(struct m_config *config, struct m_profile *p, + bstr name, bstr val) +{ + int i = m_config_set_option_ext(config, name, val, + M_SETOPT_CHECK_ONLY | + M_SETOPT_FROM_CONFIG_FILE); + if (i < 0) + return i; + p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2)); + p->opts[p->num_opts * 2] = bstrdup0(p, name); + p->opts[p->num_opts * 2 + 1] = bstrdup0(p, val); + p->num_opts++; + p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL; + return 1; +} + +void m_config_set_profile(struct m_config *config, struct m_profile *p, + int flags) +{ + if (config->profile_depth > MAX_PROFILE_DEPTH) { + mp_msg(MSGT_CFGPARSER, MSGL_WARN, + "WARNING: Profile inclusion too deep.\n"); + return; + } + config->profile_depth++; + for (int i = 0; i < p->num_opts; i++) { + m_config_set_option_ext(config, + bstr0(p->opts[2 * i]), + bstr0(p->opts[2 * i + 1]), + flags | M_SETOPT_FROM_CONFIG_FILE); + } + config->profile_depth--; +} + +void *m_config_alloc_struct(void *talloc_ctx, + const struct m_sub_options *subopts) +{ + void *substruct = talloc_zero_size(talloc_ctx, subopts->size); + if (subopts->defaults) + memcpy(substruct, subopts->defaults, subopts->size); + return substruct; +} diff --git a/options/m_config.h b/options/m_config.h new file mode 100644 index 0000000000..8a8865d68e --- /dev/null +++ b/options/m_config.h @@ -0,0 +1,216 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_M_CONFIG_H +#define MPLAYER_M_CONFIG_H + +#include +#include + +#include "mpvcore/bstr.h" + +// m_config provides an API to manipulate the config variables in MPlayer. +// It makes use of the Options API to provide a context stack that +// allows saving and later restoring the state of all variables. + +typedef struct m_profile m_profile_t; +struct m_option; +struct m_option_type; +struct m_sub_options; +struct m_obj_desc; +struct m_obj_settings; + +// Config option +struct m_config_option { + bool is_generated : 1; // Automatically added ("no-" options) + bool is_set_from_cmdline : 1; // Set by user from command line + const char *name; // Full name (ie option-subopt) + const struct m_option *opt; // Option description + void *data; // Raw value of the option + const void *default_data; // Raw default value +}; + +// Config object +/** \ingroup Config */ +typedef struct m_config { + // Registered options. + struct m_config_option *opts; // all options, even suboptions + int num_opts; + + // List of defined profiles. + struct m_profile *profiles; + // Depth when recursively including profiles. + int profile_depth; + + struct m_opt_backup *backup_opts; + + bool use_profiles; + bool is_toplevel; + int (*includefunc)(struct m_config *conf, char *filename, int flags); + + void *optstruct; // struct mpopts or other +} m_config_t; + +// Create a new config object. +// talloc_ctx: talloc parent context for the m_config allocation +// size: size of the optstruct (where option values are stored) +// size==0 creates a config object with no option struct allocated +// defaults: if not NULL, points to a struct of same type as optstruct, which +// contains default values for all options +// options: list of options. Each option defines a member of the optstruct +// and a corresponding option switch or sub-option field. +// suboptinit: if not NULL, initialize the suboption string (used for presets) +// Note that the m_config object will keep pointers to defaults and options. +struct m_config *m_config_new(void *talloc_ctx, size_t size, + const void *defaults, + const struct m_option *options); + +struct m_config *m_config_from_obj_desc(void *talloc_ctx, + struct m_obj_desc *desc); + +struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx, + struct m_obj_desc *desc); + +int m_config_set_obj_params(struct m_config *conf, char **args); + +// Search for the object with the given name in the defaults list, and apply +// its parameters. +int m_config_apply_defaults(struct m_config *config, const char *name, + struct m_obj_settings *defaults); + +// Make sure the option is backed up. If it's already backed up, do nothing. +// All backed up options can be restored with m_config_restore_backups(). +void m_config_backup_opt(struct m_config *config, const char *opt); + +// Call m_config_backup_opt() on all options. +void m_config_backup_all_opts(struct m_config *config); + +// Restore all options backed up with m_config_backup_opt(), and delete the +// backups afterwards. +void m_config_restore_backups(struct m_config *config); + +enum { + M_SETOPT_PRE_PARSE_ONLY = 1, // Silently ignore non-M_OPT_PRE_PARSE opt. + M_SETOPT_CHECK_ONLY = 2, // Don't set, just check name/value + M_SETOPT_FROM_CONFIG_FILE = 4, // Reject M_OPT_NOCFG opt. (print error) + M_SETOPT_FROM_CMDLINE = 8, // Mark as set by command line + M_SETOPT_BACKUP = 16, // Call m_config_backup_opt() before + M_SETOPT_PRESERVE_CMDLINE = 32, // Don't set if already marked as FROM_CMDLINE +}; + +// Set the named option to the given string. +// flags: combination of M_SETOPT_* flags (0 for normal operation) +// Returns >= 0 on success, otherwise see OptionParserReturn. +int m_config_set_option_ext(struct m_config *config, struct bstr name, + struct bstr param, int flags); + +/* Set an option. (Like: m_config_set_option_ext(config, name, param, 0)) + * \param config The config object. + * \param name The option's name. + * \param param The value of the option, can be NULL. + * \return See \ref OptionParserReturn. + */ +int m_config_set_option(struct m_config *config, struct bstr name, + struct bstr param); + +static inline int m_config_set_option0(struct m_config *config, + const char *name, const char *param) +{ + return m_config_set_option(config, bstr0(name), bstr0(param)); +} + +int m_config_parse_suboptions(struct m_config *config, char *name, + char *subopts); + + +/* Get the option matching the given name. + * \param config The config object. + * \param name The option's name. + */ +const struct m_option *m_config_get_option(const struct m_config *config, + struct bstr name); + +struct m_config_option *m_config_get_co(const struct m_config *config, + struct bstr name); + +// Return the n-th option by position. n==0 is the first option. If there are +// less than (n + 1) options, return NULL. +const char *m_config_get_positional_option(const struct m_config *config, int n); + +// Return a hint to the option parser whether a parameter is/may be required. +// The option may still accept empty/non-empty parameters independent from +// this, and this function is useful only for handling ambiguous options like +// flags (e.g. "--a" is ok, "--a=yes" is also ok). +// Returns: error code (<0), or number of expected params (0, 1) +int m_config_option_requires_param(struct m_config *config, bstr name); + +/* Print a list of all registered options. + * \param config The config object. + */ +void m_config_print_option_list(const struct m_config *config); + + +/* Find the profile with the given name. + * \param config The config object. + * \param arg The profile's name. + * \return The profile object or NULL. + */ +struct m_profile *m_config_get_profile0(const struct m_config *config, + char *name); +struct m_profile *m_config_get_profile(const struct m_config *config, bstr name); + +/* Get the profile with the given name, creating it if necessary. + * \param config The config object. + * \param arg The profile's name. + * \return The profile object. + */ +struct m_profile *m_config_add_profile(struct m_config *config, char *name); + +/* Set the description of a profile. + * Used by the config file parser when defining a profile. + * + * \param p The profile object. + * \param arg The profile's name. + */ +void m_profile_set_desc(struct m_profile *p, bstr desc); + +/* Add an option to a profile. + * Used by the config file parser when defining a profile. + * + * \param config The config object. + * \param p The profile object. + * \param name The option's name. + * \param val The option's value. + */ +int m_config_set_profile_option(struct m_config *config, struct m_profile *p, + bstr name, bstr val); + +/* Enables profile usage + * Used by the config file parser when loading a profile. + * + * \param config The config object. + * \param p The profile object. + * \param flags M_SETOPT_* bits + */ +void m_config_set_profile(struct m_config *config, struct m_profile *p, + int flags); + +void *m_config_alloc_struct(void *talloc_ctx, + const struct m_sub_options *subopts); + +#endif /* MPLAYER_M_CONFIG_H */ diff --git a/options/m_option.c b/options/m_option.c new file mode 100644 index 0000000000..b4bd50a23f --- /dev/null +++ b/options/m_option.c @@ -0,0 +1,2477 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/// \file +/// \ingroup Options + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "talloc.h" +#include "mpvcore/mp_common.h" +#include "mpvcore/mp_msg.h" +#include "m_option.h" +#include "m_config.h" + +char *m_option_strerror(int code) +{ + switch (code) { + case M_OPT_UNKNOWN: + return "option not found"; + case M_OPT_MISSING_PARAM: + return "option requires parameter"; + case M_OPT_INVALID: + return "option parameter could not be parsed"; + case M_OPT_OUT_OF_RANGE: + return "parameter is outside values allowed for option"; + case M_OPT_DISALLOW_PARAM: + return "option doesn't take a parameter"; + case M_OPT_PARSER_ERR: + default: + return "parser error"; + } +} + +int m_option_required_params(const m_option_t *opt) +{ + if (((opt->flags & M_OPT_OPTIONAL_PARAM) || + (opt->type->flags & M_OPT_TYPE_OPTIONAL_PARAM))) + return 0; + return 1; +} + +static const struct m_option *m_option_list_findb(const struct m_option *list, + struct bstr name) +{ + for (int i = 0; list[i].name; i++) { + struct bstr lname = bstr0(list[i].name); + if ((list[i].type->flags & M_OPT_TYPE_ALLOW_WILDCARD) + && bstr_endswith0(lname, "*")) { + lname.len--; + if (bstrcmp(bstr_splice(name, 0, lname.len), lname) == 0) + return &list[i]; + } else if (bstrcmp(lname, name) == 0) + return &list[i]; + } + return NULL; +} + +const m_option_t *m_option_list_find(const m_option_t *list, const char *name) +{ + return m_option_list_findb(list, bstr0(name)); +} + +// Default function that just does a memcpy + +static void copy_opt(const m_option_t *opt, void *dst, const void *src) +{ + if (dst && src) + memcpy(dst, src, opt->type->size); +} + +// Flag + +#define VAL(x) (*(int *)(x)) + +static int clamp_flag(const m_option_t *opt, void *val) +{ + if (VAL(val) == opt->min || VAL(val) == opt->max) + return 0; + VAL(val) = opt->min; + return M_OPT_OUT_OF_RANGE; +} + +static int parse_flag(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len) { + if (!bstrcmp0(param, "yes")) { + if (dst) + VAL(dst) = opt->max; + return 1; + } + if (!bstrcmp0(param, "no")) { + if (dst) + VAL(dst) = opt->min; + return 1; + } + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Invalid parameter for %.*s flag: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } else { + if (dst) + VAL(dst) = opt->max; + return 0; + } +} + +static char *print_flag(const m_option_t *opt, const void *val) +{ + if (VAL(val) == opt->min) + return talloc_strdup(NULL, "no"); + else + return talloc_strdup(NULL, "yes"); +} + +static void add_flag(const m_option_t *opt, void *val, double add, bool wrap) +{ + if (fabs(add) < 0.5) + return; + bool state = VAL(val) != opt->min; + state = wrap ? !state : add > 0; + VAL(val) = state ? opt->max : opt->min; +} + +const m_option_type_t m_option_type_flag = { + // need yes or no in config files + .name = "Flag", + .size = sizeof(int), + .flags = M_OPT_TYPE_OPTIONAL_PARAM, + .parse = parse_flag, + .print = print_flag, + .copy = copy_opt, + .add = add_flag, + .clamp = clamp_flag, +}; + +// Single-value, write-only flag + +static int parse_store(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len == 0 || bstrcmp0(param, "yes") == 0) { + if (dst) + VAL(dst) = opt->max; + return 0; + } else { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Invalid parameter for %.*s flag: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_DISALLOW_PARAM; + } +} + +const m_option_type_t m_option_type_store = { + // can only be activated + .name = "Flag", + .size = sizeof(int), + .flags = M_OPT_TYPE_OPTIONAL_PARAM, + .parse = parse_store, + .copy = copy_opt, +}; + +// Same for float types + +#undef VAL +#define VAL(x) (*(float *)(x)) + +static int parse_store_float(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len == 0 || bstrcmp0(param, "yes") == 0) { + if (dst) + VAL(dst) = opt->max; + return 0; + } else { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Invalid parameter for %.*s flag: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_DISALLOW_PARAM; + } +} + +const m_option_type_t m_option_type_float_store = { + // can only be activated + .name = "Flag", + .size = sizeof(float), + .flags = M_OPT_TYPE_OPTIONAL_PARAM, + .parse = parse_store_float, + .copy = copy_opt, +}; + +// Integer + +#undef VAL + +static int clamp_longlong(const m_option_t *opt, void *val) +{ + long long v = *(long long *)val; + int r = 0; + if ((opt->flags & M_OPT_MAX) && (v > opt->max)) { + v = opt->max; + r = M_OPT_OUT_OF_RANGE; + } + if ((opt->flags & M_OPT_MIN) && (v < opt->min)) { + v = opt->min; + r = M_OPT_OUT_OF_RANGE; + } + *(long long *)val = v; + return r; +} + +static int parse_longlong(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + struct bstr rest; + long long tmp_int = bstrtoll(param, &rest, 10); + if (rest.len) + tmp_int = bstrtoll(param, &rest, 0); + if (rest.len) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be an integer: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + if ((opt->flags & M_OPT_MIN) && (tmp_int < opt->min)) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be >= %d: %.*s\n", + BSTR_P(name), (int) opt->min, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if ((opt->flags & M_OPT_MAX) && (tmp_int > opt->max)) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be <= %d: %.*s\n", + BSTR_P(name), (int) opt->max, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (dst) + *(long long *)dst = tmp_int; + + return 1; +} + +static int clamp_int(const m_option_t *opt, void *val) +{ + long long tmp = *(int *)val; + int r = clamp_longlong(opt, &tmp); + *(int *)val = tmp; + return r; +} + +static int clamp_int64(const m_option_t *opt, void *val) +{ + long long tmp = *(int64_t *)val; + int r = clamp_longlong(opt, &tmp); + *(int64_t *)val = tmp; + return r; +} + +static int parse_int(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + long long tmp; + int r = parse_longlong(opt, name, param, &tmp); + if (r >= 0 && dst) + *(int *)dst = tmp; + return r; +} + +static int parse_int64(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + long long tmp; + int r = parse_longlong(opt, name, param, &tmp); + if (r >= 0 && dst) + *(int64_t *)dst = tmp; + return r; +} + +static char *print_int(const m_option_t *opt, const void *val) +{ + if (opt->type->size == sizeof(int64_t)) + return talloc_asprintf(NULL, "%"PRId64, *(const int64_t *)val); + return talloc_asprintf(NULL, "%d", *(const int *)val); +} + +static void add_int64(const m_option_t *opt, void *val, double add, bool wrap) +{ + int64_t v = *(int64_t *)val; + + v = v + add; + + bool is64 = opt->type->size == sizeof(int64_t); + int64_t nmin = is64 ? INT64_MIN : INT_MIN; + int64_t nmax = is64 ? INT64_MAX : INT_MAX; + + int64_t min = (opt->flags & M_OPT_MIN) ? opt->min : nmin; + int64_t max = (opt->flags & M_OPT_MAX) ? opt->max : nmax; + + if (v < min) + v = wrap ? max : min; + if (v > max) + v = wrap ? min : max; + + *(int64_t *)val = v; +} + +static void add_int(const m_option_t *opt, void *val, double add, bool wrap) +{ + int64_t tmp = *(int *)val; + add_int64(opt, &tmp, add, wrap); + *(int *)val = tmp; +} + +static void multiply_int64(const m_option_t *opt, void *val, double f) +{ + double v = *(int64_t *)val * f; + int64_t iv = v; + if (v < INT64_MIN) + iv = INT64_MIN; + if (v > INT64_MAX) + iv = INT64_MAX; + *(int64_t *)val = iv; + clamp_int64(opt, val); +} + +static void multiply_int(const m_option_t *opt, void *val, double f) +{ + int64_t tmp = *(int *)val; + multiply_int64(opt, &tmp, f); + *(int *)val = MPCLAMP(tmp, INT_MIN, INT_MAX); +} + +const m_option_type_t m_option_type_int = { + .name = "Integer", + .size = sizeof(int), + .parse = parse_int, + .print = print_int, + .copy = copy_opt, + .add = add_int, + .multiply = multiply_int, + .clamp = clamp_int, +}; + +const m_option_type_t m_option_type_int64 = { + .name = "Integer64", + .size = sizeof(int64_t), + .parse = parse_int64, + .print = print_int, + .copy = copy_opt, + .add = add_int64, + .multiply = multiply_int64, + .clamp = clamp_int64, +}; + +static int parse_intpair(const struct m_option *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + struct bstr s = param; + int end = -1; + int start = bstrtoll(s, &s, 10); + if (s.len == param.len) + goto bad; + if (s.len > 0) { + if (!bstr_startswith0(s, "-")) + goto bad; + s = bstr_cut(s, 1); + } + if (s.len > 0) + end = bstrtoll(s, &s, 10); + if (s.len > 0) + goto bad; + + if (dst) { + int *p = dst; + p[0] = start; + p[1] = end; + } + + return 1; + +bad: + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid integer range " + "specification for option %.*s: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; +} + +const struct m_option_type m_option_type_intpair = { + .name = "Int[-Int]", + .size = sizeof(int[2]), + .parse = parse_intpair, + .copy = copy_opt, +}; + +static int clamp_choice(const m_option_t *opt, void *val) +{ + int v = *(int *)val; + if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + if (v >= opt->min && v <= opt->max) + return 0; + } + ; + for (struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) { + if (alt->value == v) + return 0; + } + return M_OPT_INVALID; +} + +static int parse_choice(const struct m_option *opt, struct bstr name, + struct bstr param, void *dst) +{ + struct m_opt_choice_alternatives *alt = opt->priv; + for ( ; alt->name; alt++) + if (!bstrcmp0(param, alt->name)) + break; + if (!alt->name) { + if (param.len == 0) + return M_OPT_MISSING_PARAM; + if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + long long val; + if (parse_longlong(opt, name, param, &val) == 1) { + if (dst) + *(int *)dst = val; + return 1; + } + } + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Invalid value for option %.*s: %.*s\n", + BSTR_P(name), BSTR_P(param)); + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Valid values are:"); + for (alt = opt->priv; alt->name; alt++) + mp_msg(MSGT_CFGPARSER, MSGL_ERR, " %s", alt->name); + if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) + mp_msg(MSGT_CFGPARSER, MSGL_ERR, " %g-%g", opt->min, opt->max); + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "\n"); + return M_OPT_INVALID; + } + if (dst) + *(int *)dst = alt->value; + + return 1; +} + +static char *print_choice(const m_option_t *opt, const void *val) +{ + int v = *(int *)val; + struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) + if (alt->value == v) + return talloc_strdup(NULL, alt->name); + if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + if (v >= opt->min && v <= opt->max) + return talloc_asprintf(NULL, "%d", v); + } + abort(); +} + +static void choice_get_min_max(const struct m_option *opt, int *min, int *max) +{ + assert(opt->type == &m_option_type_choice); + *min = INT_MAX; + *max = INT_MIN; + for (struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) { + *min = FFMIN(*min, alt->value); + *max = FFMAX(*max, alt->value); + } + if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + *min = FFMIN(*min, opt->min); + *max = FFMAX(*max, opt->max); + } +} + +static void check_choice(int dir, int val, bool *found, int *best, int choice) +{ + if ((dir == -1 && (!(*found) || choice > (*best)) && choice < val) || + (dir == +1 && (!(*found) || choice < (*best)) && choice > val)) + { + *found = true; + *best = choice; + } +} + +static void add_choice(const m_option_t *opt, void *val, double add, bool wrap) +{ + assert(opt->type == &m_option_type_choice); + int dir = add > 0 ? +1 : -1; + bool found = false; + int ival = *(int *)val; + int best = 0; // init. value unused + + if (fabs(add) < 0.5) + return; + + if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + int newval = ival + add; + if (ival >= opt->min && ival <= opt->max && + newval >= opt->min && newval <= opt->max) + { + found = true; + best = newval; + } else { + check_choice(dir, ival, &found, &best, opt->min); + check_choice(dir, ival, &found, &best, opt->max); + } + } + + for (struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) + check_choice(dir, ival, &found, &best, alt->value); + + if (!found) { + int min, max; + choice_get_min_max(opt, &min, &max); + best = (dir == -1) ^ wrap ? min : max; + } + + *(int *)val = best; +} + +const struct m_option_type m_option_type_choice = { + .name = "String", // same as arbitrary strings in option list for now + .size = sizeof(int), + .parse = parse_choice, + .print = print_choice, + .copy = copy_opt, + .add = add_choice, + .clamp = clamp_choice, +}; + +// Float + +#undef VAL +#define VAL(x) (*(double *)(x)) + +static int clamp_double(const m_option_t *opt, void *val) +{ + double v = VAL(val); + int r = 0; + if ((opt->flags & M_OPT_MAX) && (v > opt->max)) { + v = opt->max; + r = M_OPT_OUT_OF_RANGE; + } + if ((opt->flags & M_OPT_MIN) && (v < opt->min)) { + v = opt->min; + r = M_OPT_OUT_OF_RANGE; + } + if (!isfinite(v)) { + v = opt->min; + r = M_OPT_OUT_OF_RANGE; + } + VAL(val) = v; + return r; +} + +static int parse_double(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + struct bstr rest; + double tmp_float = bstrtod(param, &rest); + + if (bstr_eatstart0(&rest, ":") || bstr_eatstart0(&rest, "/")) + tmp_float /= bstrtod(rest, &rest); + + if (rest.len) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be a floating point number or a " + "ratio (numerator[:/]denominator): %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + if (opt->flags & M_OPT_MIN) + if (tmp_float < opt->min) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be >= %f: %.*s\n", + BSTR_P(name), opt->min, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (opt->flags & M_OPT_MAX) + if (tmp_float > opt->max) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be <= %f: %.*s\n", + BSTR_P(name), opt->max, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (!isfinite(tmp_float)) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "The %.*s option must be a finite number: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (dst) + VAL(dst) = tmp_float; + return 1; +} + +static char *print_double(const m_option_t *opt, const void *val) +{ + return talloc_asprintf(NULL, "%f", VAL(val)); +} + +static char *print_double_f3(const m_option_t *opt, const void *val) +{ + return talloc_asprintf(NULL, "%.3f", VAL(val)); +} + +static void add_double(const m_option_t *opt, void *val, double add, bool wrap) +{ + double v = VAL(val); + + v = v + add; + + double min = (opt->flags & M_OPT_MIN) ? opt->min : -INFINITY; + double max = (opt->flags & M_OPT_MAX) ? opt->max : +INFINITY; + + if (v < min) + v = wrap ? max : min; + if (v > max) + v = wrap ? min : max; + + VAL(val) = v; +} + +static void multiply_double(const m_option_t *opt, void *val, double f) +{ + *(double *)val *= f; + clamp_double(opt, val); +} + +const m_option_type_t m_option_type_double = { + // double precision float or ratio (numerator[:/]denominator) + .name = "Double", + .size = sizeof(double), + .parse = parse_double, + .print = print_double, + .pretty_print = print_double_f3, + .copy = copy_opt, + .clamp = clamp_double, + .add = add_double, + .multiply = multiply_double, +}; + +#undef VAL +#define VAL(x) (*(float *)(x)) + +static int clamp_float(const m_option_t *opt, void *val) +{ + double tmp = VAL(val); + int r = clamp_double(opt, &tmp); + VAL(val) = tmp; + return r; +} + +static int parse_float(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + double tmp; + int r = parse_double(opt, name, param, &tmp); + if (r == 1 && dst) + VAL(dst) = tmp; + return r; +} + +static char *print_float(const m_option_t *opt, const void *val) +{ + return talloc_asprintf(NULL, "%f", VAL(val)); +} + +static char *print_float_f3(const m_option_t *opt, const void *val) +{ + return talloc_asprintf(NULL, "%.3f", VAL(val)); +} + +static void add_float(const m_option_t *opt, void *val, double add, bool wrap) +{ + double tmp = VAL(val); + add_double(opt, &tmp, add, wrap); + VAL(val) = tmp; +} + +static void multiply_float(const m_option_t *opt, void *val, double f) +{ + double tmp = VAL(val); + multiply_double(opt, &tmp, f); + VAL(val) = tmp; +} + +const m_option_type_t m_option_type_float = { + // floating point number or ratio (numerator[:/]denominator) + .name = "Float", + .size = sizeof(float), + .parse = parse_float, + .print = print_float, + .pretty_print = print_float_f3, + .copy = copy_opt, + .add = add_float, + .multiply = multiply_float, + .clamp = clamp_float, +}; + +///////////// String + +#undef VAL +#define VAL(x) (*(char **)(x)) + +static char *unescape_string(void *talloc_ctx, bstr str) +{ + char *res = talloc_strdup(talloc_ctx, ""); + while (str.len) { + bstr rest; + bool esc = bstr_split_tok(str, "\\", &str, &rest); + res = talloc_strndup_append_buffer(res, str.start, str.len); + if (esc) { + if (!mp_parse_escape(&rest, &res)) { + talloc_free(res); + return NULL; + } + } + str = rest; + } + return res; +} + +static char *escape_string(char *str0) +{ + char *res = talloc_strdup(NULL, ""); + bstr str = bstr0(str0); + while (str.len) { + bstr rest; + bool esc = bstr_split_tok(str, "\\", &str, &rest); + res = talloc_strndup_append_buffer(res, str.start, str.len); + if (esc) + res = talloc_strdup_append_buffer(res, "\\\\"); + str = rest; + } + return res; +} + +static int clamp_str(const m_option_t *opt, void *val) +{ + char *v = VAL(val); + int len = v ? strlen(v) : 0; + if ((opt->flags & M_OPT_MIN) && (len < opt->min)) + return M_OPT_OUT_OF_RANGE; + if ((opt->flags & M_OPT_MAX) && (len > opt->max)) + return M_OPT_OUT_OF_RANGE; + return 0; +} + +static int parse_str(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + int r = 1; + void *tmp = talloc_new(NULL); + + if (param.start == NULL) { + r = M_OPT_MISSING_PARAM; + goto exit; + } + + m_opt_string_validate_fn validate = opt->priv; + if (validate) { + r = validate(opt, name, param); + if (r < 0) + goto exit; + } + + if (opt->flags & M_OPT_PARSE_ESCAPES) { + char *res = unescape_string(tmp, param); + if (!res) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Parameter has broken escapes: %.*s\n", BSTR_P(param)); + r = M_OPT_INVALID; + goto exit; + } + param = bstr0(res); + } + + if ((opt->flags & M_OPT_MIN) && (param.len < opt->min)) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Parameter must be >= %d chars: %.*s\n", + (int) opt->min, BSTR_P(param)); + r = M_OPT_OUT_OF_RANGE; + goto exit; + } + + if ((opt->flags & M_OPT_MAX) && (param.len > opt->max)) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Parameter must be <= %d chars: %.*s\n", + (int) opt->max, BSTR_P(param)); + r = M_OPT_OUT_OF_RANGE; + goto exit; + } + + if (dst) { + talloc_free(VAL(dst)); + VAL(dst) = bstrdup0(NULL, param); + } + +exit: + talloc_free(tmp); + return r; +} + +static char *print_str(const m_option_t *opt, const void *val) +{ + bool need_escape = opt->flags & M_OPT_PARSE_ESCAPES; + char *s = val ? VAL(val) : NULL; + return s ? (need_escape ? escape_string(s) : talloc_strdup(NULL, s)) : NULL; +} + +static void copy_str(const m_option_t *opt, void *dst, const void *src) +{ + if (dst && src) { + talloc_free(VAL(dst)); + VAL(dst) = talloc_strdup(NULL, VAL(src)); + } +} + +static void free_str(void *src) +{ + if (src && VAL(src)) { + talloc_free(VAL(src)); + VAL(src) = NULL; + } +} + +const m_option_type_t m_option_type_string = { + .name = "String", + .size = sizeof(char *), + .flags = M_OPT_TYPE_DYNAMIC, + .parse = parse_str, + .print = print_str, + .copy = copy_str, + .free = free_str, + .clamp = clamp_str, +}; + +//////////// String list + +#undef VAL +#define VAL(x) (*(char ***)(x)) + +#define OP_NONE 0 +#define OP_ADD 1 +#define OP_PRE 2 +#define OP_DEL 3 +#define OP_CLR 4 +#define OP_TOGGLE 5 + +static void free_str_list(void *dst) +{ + char **d; + int i; + + if (!dst || !VAL(dst)) + return; + d = VAL(dst); + + for (i = 0; d[i] != NULL; i++) + talloc_free(d[i]); + talloc_free(d); + VAL(dst) = NULL; +} + +static int str_list_add(char **add, int n, void *dst, int pre) +{ + if (!dst) + return M_OPT_PARSER_ERR; + char **lst = VAL(dst); + + int ln; + for (ln = 0; lst && lst[ln]; ln++) + /**/; + + lst = talloc_realloc(NULL, lst, char *, n + ln + 1); + + if (pre) { + memmove(&lst[n], lst, ln * sizeof(char *)); + memcpy(lst, add, n * sizeof(char *)); + } else + memcpy(&lst[ln], add, n * sizeof(char *)); + // (re-)add NULL-termination + lst[ln + n] = NULL; + + talloc_free(add); + + VAL(dst) = lst; + + return 1; +} + +static int str_list_del(char **del, int n, void *dst) +{ + char **lst, *ep; + int i, ln, s; + long idx; + + if (!dst) + return M_OPT_PARSER_ERR; + lst = VAL(dst); + + for (ln = 0; lst && lst[ln]; ln++) + /**/; + s = ln; + + for (i = 0; del[i] != NULL; i++) { + idx = strtol(del[i], &ep, 0); + if (*ep) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid index: %s\n", del[i]); + talloc_free(del[i]); + continue; + } + talloc_free(del[i]); + if (idx < 0 || idx >= ln) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Index %ld is out of range.\n", idx); + continue; + } else if (!lst[idx]) + continue; + talloc_free(lst[idx]); + lst[idx] = NULL; + s--; + } + talloc_free(del); + + if (s == 0) { + talloc_free(lst); + VAL(dst) = NULL; + return 1; + } + + // Don't bother shrinking the list allocation + for (i = 0, n = 0; i < ln; i++) { + if (!lst[i]) + continue; + lst[n] = lst[i]; + n++; + } + lst[s] = NULL; + + return 1; +} + +static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify) +{ + struct bstr str = *ptr; + struct bstr orig = str; + for (;;) { + int idx = bstrchr(str, sep); + if (idx > 0 && str.start[idx - 1] == '\\') { + if (modify) { + memmove(str.start + idx - 1, str.start + idx, str.len - idx); + str.len--; + str = bstr_cut(str, idx); + } else + str = bstr_cut(str, idx + 1); + } else { + str = bstr_cut(str, idx < 0 ? str.len : idx); + break; + } + } + *ptr = str; + return bstr_splice(orig, 0, str.start - orig.start); +} + +static int parse_str_list(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + char **res; + int op = OP_NONE; + int len = strlen(opt->name); + if (opt->name[len - 1] == '*' && (name.len > len - 1)) { + struct bstr suffix = bstr_cut(name, len - 1); + if (bstrcmp0(suffix, "-add") == 0) + op = OP_ADD; + else if (bstrcmp0(suffix, "-pre") == 0) + op = OP_PRE; + else if (bstrcmp0(suffix, "-del") == 0) + op = OP_DEL; + else if (bstrcmp0(suffix, "-clr") == 0) + op = OP_CLR; + else + return M_OPT_UNKNOWN; + } + + // Clear the list ?? + if (op == OP_CLR) { + if (dst) + free_str_list(dst); + return 0; + } + + // All other ops need a param + if (param.len == 0 && op != OP_NONE) + return M_OPT_MISSING_PARAM; + + // custom type for "profile" calls this but uses ->priv for something else + char separator = opt->type == &m_option_type_string_list && opt->priv ? + *(char *)opt->priv : OPTION_LIST_SEPARATOR; + int n = 0; + struct bstr str = param; + while (str.len) { + get_nextsep(&str, separator, 0); + str = bstr_cut(str, 1); + n++; + } + if (n == 0 && op != OP_NONE) + return M_OPT_INVALID; + if (((opt->flags & M_OPT_MIN) && (n < opt->min)) || + ((opt->flags & M_OPT_MAX) && (n > opt->max))) + return M_OPT_OUT_OF_RANGE; + + if (!dst) + return 1; + + res = talloc_array(NULL, char *, n + 2); + str