m_option: remove redundant indirections

Remove the various redundant m_config_set_option* calls, rename the
remaining one to m_config_set_option_cli(), and merge the
m_config_parse_option() function.
This commit is contained in:
wm4 2017-07-02 13:00:22 +02:00
parent 66478cff14
commit 3d31807198
5 changed files with 29 additions and 52 deletions

View File

@ -243,7 +243,7 @@ static int m_config_set_obj_params(struct m_config *config, struct mp_log *log,
for (int n = 0; args && args[n * 2 + 0]; n++) {
bstr opt = bstr0(args[n * 2 + 0]);
bstr val = bstr0(args[n * 2 + 1]);
if (m_config_set_option(config, opt, val) < 0)
if (m_config_set_option_cli(config, opt, val, 0) < 0)
return -1;
}
@ -755,7 +755,7 @@ int m_config_set_option_raw_direct(struct m_config *config,
return 0;
}
// Similar to m_config_set_option_ext(), but set as data in its native format.
// Similar to m_config_set_option_cli(), but set as data in its native format.
// This takes care of some details like sending change notifications.
// The type data points to is as in: co->opt
int m_config_set_option_raw(struct m_config *config, struct m_config_option *co,
@ -833,21 +833,30 @@ static struct m_config_option *m_config_mogrify_cli_opt(struct m_config *config,
return NULL;
}
static int m_config_parse_option(struct m_config *config, struct bstr name,
// Set the named option to the given string. This is for command line and config
// file use only.
// flags: combination of M_SETOPT_* flags (0 for normal operation)
// Returns >= 0 on success, otherwise see OptionParserReturn.
int m_config_set_option_cli(struct m_config *config, struct bstr name,
struct bstr param, int flags)
{
int r;
assert(config != NULL);
bool negate;
struct m_config_option *co =
m_config_mogrify_cli_opt(config, &name, &negate, &(int){0});
if (!co)
return M_OPT_UNKNOWN;
if (!co) {
r = M_OPT_UNKNOWN;
goto done;
}
if (negate) {
if (param.len)
return M_OPT_DISALLOW_PARAM;
if (param.len) {
r = M_OPT_DISALLOW_PARAM;
goto done;
}
param = bstr0("no");
}
@ -855,12 +864,11 @@ static int m_config_parse_option(struct m_config *config, struct bstr name,
// This is the only mandatory function
assert(co->opt->type->parse);
int r = handle_set_opt_flags(config, co, flags);
r = handle_set_opt_flags(config, co, flags);
if (r <= 0)
return r;
bool set = r == 2;
goto done;
if (set) {
if (r == 2) {
MP_VERBOSE(config, "Setting option '%.*s' = '%.*s' (flags = %d)\n",
BSTR_P(name), BSTR_P(param), flags);
}
@ -879,13 +887,7 @@ static int m_config_parse_option(struct m_config *config, struct bstr name,
m_option_free(co->opt, &val);
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);
done:
if (r < 0 && r != M_OPT_EXIT) {
MP_ERR(config, "Error parsing option %.*s (%s)\n",
BSTR_P(name), m_option_strerror(r));
@ -894,12 +896,6 @@ int m_config_set_option_ext(struct m_config *config, struct bstr name,
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);
}
int m_config_set_option_node(struct m_config *config, bstr name,
struct mpv_node *data, int flags)
{
@ -1076,7 +1072,7 @@ void m_profile_set_desc(struct m_profile *p, bstr 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,
int i = m_config_set_option_cli(config, name, val,
M_SETOPT_CHECK_ONLY |
M_SETOPT_FROM_CONFIG_FILE);
if (i < 0)
@ -1103,7 +1099,7 @@ int m_config_set_profile(struct m_config *config, char *name, int flags)
}
config->profile_depth++;
for (int i = 0; i < p->num_opts; i++) {
m_config_set_option_ext(config,
m_config_set_option_cli(config,
bstr0(p->opts[2 * i]),
bstr0(p->opts[2 * i + 1]),
flags | M_SETOPT_FROM_CONFIG_FILE);

View File

@ -163,27 +163,9 @@ enum {
// Flags for safe option setting during runtime.
#define M_SETOPT_RUNTIME M_SETOPT_NO_FIXED
// 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,
int m_config_set_option_cli(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_set_option_raw(struct m_config *config, struct m_config_option *co,
void *data, int flags);
@ -193,7 +175,6 @@ int m_config_set_option_raw_direct(struct m_config *config,
struct m_config_option *co,
void *data, int flags);
// Similar to m_config_set_option_ext(), but set as data using mpv_node.
struct mpv_node;
int m_config_set_option_node(struct m_config *config, bstr name,
struct mpv_node *data, int flags);

View File

@ -2621,7 +2621,7 @@ static int get_obj_param(struct mp_log *log, bstr opt_name, bstr obj_name,
// If it's just "name", and the associated option exists and is a flag,
// don't accept it as positional argument.
if (val.start || m_config_option_requires_param(config, name) == 0 || nopos) {
r = m_config_set_option_ext(config, name, val, flags);
r = m_config_set_option_cli(config, name, val, flags);
if (r < 0) {
if (r == M_OPT_UNKNOWN) {
mp_err(log, "Option %.*s: %.*s doesn't have a %.*s parameter.\n",
@ -2652,7 +2652,7 @@ static int get_obj_param(struct mp_log *log, bstr opt_name, bstr obj_name,
BSTR_P(opt_name), BSTR_P(obj_name), *nold, *nold);
return M_OPT_OUT_OF_RANGE;
}
r = m_config_set_option_ext(config, bstr0(opt), val, flags);
r = m_config_set_option_cli(config, bstr0(opt), val, flags);
if (r < 0) {
if (r != M_OPT_EXIT)
mp_err(log, "Option %.*s: "

View File

@ -146,7 +146,7 @@ int m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
int flags = M_SETOPT_FROM_CMDLINE;
if (mode == LOCAL)
flags |= M_SETOPT_BACKUP | M_SETOPT_CHECK_ONLY;
int r = m_config_set_option_ext(config, p.arg, p.param, flags);
int r = m_config_set_option_cli(config, p.arg, p.param, flags);
if (r == M_OPT_EXIT) {
ret = r;
goto err_out;
@ -291,7 +291,7 @@ void m_config_preparse_command_line(m_config_t *config, struct mpv_global *globa
// Ignore non-pre-parse options. They will be set later.
// Option parsing errors will be handled later as well.
int flags = M_SETOPT_FROM_CMDLINE | M_SETOPT_PRE_PARSE_ONLY;
m_config_set_option_ext(config, p.arg, p.param, flags);
m_config_set_option_cli(config, p.arg, p.param, flags);
if (bstrcmp0(p.arg, "v") == 0)
opts->verbose++;
}

View File

@ -800,7 +800,7 @@ static void load_per_file_options(m_config_t *conf,
int params_count)
{
for (int n = 0; n < params_count; n++) {
m_config_set_option_ext(conf, params[n].name, params[n].value,
m_config_set_option_cli(conf, params[n].name, params[n].value,
M_SETOPT_RUNTIME | M_SETOPT_BACKUP);
}
}