1
mirror of https://github.com/mpv-player/mpv synced 2025-04-23 12:59:51 +02:00

options: get rid of ambiguous option parsing

Options parsing used to be ambiguous, as in the splitting into option
and values pairs was ambiguous. Example:

    -option -something

It wasn't clear whether -option actually takes an argument or not. The
string "-something" could either be a separate option, or an argument
to "-option". The code had to call the option specific parser function
to resolve this.

This made everything complicated and didn't even have a real use. There
was only one case where this was actually used: string lists
(m_option_type_string_list) and options based on it. That is because
this option type actually turns a single option into a proxy for several
real arguments, e.g. "vf*" can handle "-vf-add" and "-vf-clr". Options
suffixed with "-clr" are the only options of this group which take no
arguments.

This is ambiguous only with the "old syntax" (as shown above). The "new"
option syntax always puts option name and value into same argument.
(E.g. "--option=--something" or "--option" "--something".)

Simplify the code by making it statically known whether an option takes
a parameter or not with the flag M_OPT_TYPE_OLD_SYNTAX_NO_PARAM. If it's
set, the option parser assumes the option takes no argument.

The only real ambiguity left, string list options that end on "-clr",
are special cased in the parser.

Remove some duplication of the logic in the command line parser by
moving all argument splitting logic into split_opt(). (It's arguable
whether that can be considered code duplication, but now the code is a
bit simpler anyway. This might be subjective.)

Remove the "ambiguous" parameter from all option parsing related code.

Make m_config unaware of the pre-parsing concept.

Make most CONF_NOCFG options also CONF_GLOBAL (except those explicitly
usable as per-file options.)
This commit is contained in:
wm4 2012-08-05 23:34:28 +02:00
parent 039a6194a4
commit 94782e464d
14 changed files with 324 additions and 313 deletions

@ -343,7 +343,7 @@ const m_option_t msgl_config[]={
" ass - libass messages\n" " ass - libass messages\n"
" statusline - playback/encoding status line\n" " statusline - playback/encoding status line\n"
" fixme - messages not yet fixed to map to module\n" " fixme - messages not yet fixed to map to module\n"
"\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, "\n", CONF_TYPE_PRINT, CONF_GLOBAL | CONF_NOCFG, 0, 0, NULL},
{NULL, NULL, 0, 0, 0, 0, NULL} {NULL, NULL, 0, 0, 0, 0, NULL}
}; };
@ -356,7 +356,7 @@ const m_option_t common_opts[] = {
OPT_MAKE_FLAGS("quiet", quiet, CONF_GLOBAL), OPT_MAKE_FLAGS("quiet", quiet, CONF_GLOBAL),
{"really-quiet", &verbose, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_PRE_PARSE, 0, -10, NULL}, {"really-quiet", &verbose, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_PRE_PARSE, 0, -10, NULL},
// -v is handled in command line preparser // -v is handled in command line preparser
{"v", NULL, CONF_TYPE_FLAG, CONF_NOCFG, 0, 0, NULL}, {"v", NULL, CONF_TYPE_FLAG, CONF_GLOBAL | CONF_NOCFG, 0, 0, NULL},
{"msglevel", (void *) msgl_config, CONF_TYPE_SUBCONFIG, CONF_GLOBAL, 0, 0, NULL}, {"msglevel", (void *) msgl_config, CONF_TYPE_SUBCONFIG, CONF_GLOBAL, 0, 0, NULL},
{"msgcolor", &mp_msg_color, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL}, {"msgcolor", &mp_msg_color, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL},
{"msgmodule", &mp_msg_module, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL}, {"msgmodule", &mp_msg_module, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL},
@ -537,7 +537,7 @@ const m_option_t common_opts[] = {
// postprocessing: // postprocessing:
{"pp", &divx_quality, CONF_TYPE_INT, 0, 0, 0, NULL}, {"pp", &divx_quality, CONF_TYPE_INT, 0, 0, 0, NULL},
#ifdef CONFIG_LIBPOSTPROC #ifdef CONFIG_LIBPOSTPROC
{"pphelp", &pp_help, CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, {"pphelp", &pp_help, CONF_TYPE_PRINT, CONF_GLOBAL | CONF_NOCFG, 0, 0, NULL},
#endif #endif
// scaling: // scaling:
@ -549,7 +549,6 @@ const m_option_t common_opts[] = {
OPT_FLOATRANGE("xy", screen_size_xy, 0, 0.001, 4096), OPT_FLOATRANGE("xy", screen_size_xy, 0, 0.001, 4096),
OPT_FLAG_CONSTANTS("flip", flip, 0, -1, 1), OPT_FLAG_CONSTANTS("flip", flip, 0, -1, 1),
OPT_FLAG_CONSTANTS("no-flip", flip, 0, -1, 0),
// draw by slices or whole frame (useful with libmpeg2/libavcodec) // draw by slices or whole frame (useful with libmpeg2/libavcodec)
OPT_MAKE_FLAGS("slices", vd_use_slices, 0), OPT_MAKE_FLAGS("slices", vd_use_slices, 0),
@ -632,7 +631,6 @@ const m_option_t mplayer_opts[]={
OPT_MAKE_FLAGS("ontop", vo_ontop, 0), OPT_MAKE_FLAGS("ontop", vo_ontop, 0),
{"rootwin", &vo_rootwin, CONF_TYPE_FLAG, 0, 0, 1, NULL}, {"rootwin", &vo_rootwin, CONF_TYPE_FLAG, 0, 0, 1, NULL},
{"border", &vo_border, CONF_TYPE_FLAG, 0, 0, 1, NULL}, {"border", &vo_border, CONF_TYPE_FLAG, 0, 0, 1, NULL},
{"no-border", &vo_border, CONF_TYPE_FLAG, 0, 1, 0, NULL},
OPT_STRING("mixer", mixer_device, 0), OPT_STRING("mixer", mixer_device, 0),
OPT_STRING("mixer-channel", mixer_channel, 0), OPT_STRING("mixer-channel", mixer_channel, 0),
@ -752,8 +750,11 @@ const m_option_t mplayer_opts[]={
OPT_FLAG_CONSTANTS("no-loop", loop_times, 0, 0, -1), OPT_FLAG_CONSTANTS("no-loop", loop_times, 0, 0, -1),
OPT_INTRANGE("loop", loop_times, 0, -1, 10000), OPT_INTRANGE("loop", loop_times, 0, -1, 10000),
{"playlist", NULL, CONF_TYPE_STRING, CONF_NOCFG, 0, 0, NULL},
{"playlist", NULL, CONF_TYPE_STRING, CONF_NOCFG | M_OPT_MIN, 1, 0, NULL},
{"shuffle", NULL, CONF_TYPE_FLAG, CONF_NOCFG, 0, 0, NULL}, {"shuffle", NULL, CONF_TYPE_FLAG, CONF_NOCFG, 0, 0, NULL},
{"{", NULL, CONF_TYPE_FLAG, CONF_NOCFG, 0, 0, NULL},
{"}", NULL, CONF_TYPE_FLAG, CONF_NOCFG, 0, 0, NULL},
OPT_MAKE_FLAGS("ordered-chapters", ordered_chapters, 0), OPT_MAKE_FLAGS("ordered-chapters", ordered_chapters, 0),
OPT_INTRANGE("chapter-merge-threshold", chapter_merge_threshold, 0, 0, 10000), OPT_INTRANGE("chapter-merge-threshold", chapter_merge_threshold, 0, 0, 10000),

@ -484,8 +484,8 @@ static const m_option_t input_conf[] = {
OPT_STRING("conf", input.config_file, CONF_GLOBAL, OPTDEF_STR("input.conf")), OPT_STRING("conf", input.config_file, CONF_GLOBAL, OPTDEF_STR("input.conf")),
OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL), OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL), OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
{ "keylist", print_key_list, CONF_TYPE_PRINT_FUNC, CONF_NOCFG }, { "keylist", print_key_list, CONF_TYPE_PRINT_FUNC, CONF_GLOBAL | CONF_NOCFG },
{ "cmdlist", print_cmd_list, CONF_TYPE_PRINT_FUNC, CONF_NOCFG }, { "cmdlist", print_cmd_list, CONF_TYPE_PRINT_FUNC, CONF_GLOBAL | CONF_NOCFG },
OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL), OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL), OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
OPT_STRING("file", input.in_file, CONF_GLOBAL), OPT_STRING("file", input.in_file, CONF_GLOBAL),

@ -67,7 +67,7 @@ static int parse_profile(struct m_config *config, const struct m_option *opt,
} }
char **list = NULL; char **list = NULL;
int r = m_option_type_string_list.parse(opt, name, param, false, &list); int r = m_option_type_string_list.parse(opt, name, param, &list);
if (r < 0) if (r < 0)
return r; return r;
if (!list || !list[0]) if (!list || !list[0])
@ -197,7 +197,8 @@ struct m_config *m_config_new(void *optstruct,
{ {
static const struct m_option ref_opts[] = { static const struct m_option ref_opts[] = {
{ "profile", NULL, CONF_TYPE_STRING_LIST, 0, 0, 0, NULL }, { "profile", NULL, CONF_TYPE_STRING_LIST, 0, 0, 0, NULL },
{ "show-profile", show_profile, CONF_TYPE_PRINT_FUNC, CONF_NOCFG }, { "show-profile", show_profile, &m_option_type_print_func_param,
CONF_NOCFG },
{ "list-options", list_options, CONF_TYPE_PRINT_FUNC, CONF_NOCFG }, { "list-options", list_options, CONF_TYPE_PRINT_FUNC, CONF_NOCFG },
{ NULL } { NULL }
}; };
@ -380,8 +381,7 @@ static int parse_subopts(struct m_config *config, void *optstruct, char *name,
char *prefix, struct bstr param, bool set); char *prefix, struct bstr param, bool set);
static int m_config_parse_option(struct m_config *config, void *optstruct, static int m_config_parse_option(struct m_config *config, void *optstruct,
struct bstr name, struct bstr param, struct bstr name, struct bstr param, bool set)
bool ambiguous_param, bool set)
{ {
assert(config != NULL); assert(config != NULL);
assert(name.len != 0); assert(name.len != 0);
@ -419,13 +419,6 @@ static int m_config_parse_option(struct m_config *config, void *optstruct,
BSTR_P(name)); BSTR_P(name));
return M_OPT_INVALID; return M_OPT_INVALID;
} }
// During command line preparse set only pre-parse options
// Otherwise only set pre-parse option if they were not already set.
if (((config->mode == M_COMMAND_LINE_PRE_PARSE) &&
!(co->opt->flags & M_OPT_PRE_PARSE)) ||
((config->mode != M_COMMAND_LINE_PRE_PARSE) &&
(co->opt->flags & M_OPT_PRE_PARSE) && (co->flags & M_CFG_OPT_SET)))
set = 0;
if (config->includefunc && !bstrcmp0(name, "include")) { if (config->includefunc && !bstrcmp0(name, "include")) {
return parse_include(config, param, set); return parse_include(config, param, set);
@ -444,14 +437,7 @@ static int m_config_parse_option(struct m_config *config, void *optstruct,
ensure_backup(config, co); ensure_backup(config, co);
void *dst = set ? m_option_get_ptr(co->opt, optstruct) : NULL; void *dst = set ? m_option_get_ptr(co->opt, optstruct) : NULL;
int r = m_option_parse(co->opt, name, param, ambiguous_param, dst); return m_option_parse(co->opt, name, param, dst);
// Parsing failed ?
if (r < 0)
return r;
else if (set)
co->flags |= M_CFG_OPT_SET;
return r;
} }
static int parse_subopts(struct m_config *config, void *optstruct, char *name, static int parse_subopts(struct m_config *config, void *optstruct, char *name,
@ -459,7 +445,7 @@ static int parse_subopts(struct m_config *config, void *optstruct, char *name,
{ {
char **lst = NULL; char **lst = NULL;
// Split the argument into child options // Split the argument into child options
int r = m_option_type_subconfig.parse(NULL, bstr0(""), param, false, &lst); int r = m_option_type_subconfig.parse(NULL, bstr0(""), param, &lst);
if (r < 0) if (r < 0)
return r; return r;
// Parse the child options // Parse the child options
@ -491,7 +477,7 @@ static int parse_subopts(struct m_config *config, void *optstruct, char *name,
lst[2 * i + 1] = "no"; lst[2 * i + 1] = "no";
} }
int sr = m_config_parse_option(config, optstruct, bstr0(n), int sr = m_config_parse_option(config, optstruct, bstr0(n),
bstr0(lst[2 * i + 1]), false, set); bstr0(lst[2 * i + 1]), set);
if (sr < 0) { if (sr < 0) {
if (sr == M_OPT_MISSING_PARAM) { if (sr == M_OPT_MISSING_PARAM) {
mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
@ -508,21 +494,20 @@ static int parse_subopts(struct m_config *config, void *optstruct, char *name,
} }
int m_config_set_option(struct m_config *config, struct bstr name, int m_config_set_option(struct m_config *config, struct bstr name,
struct bstr param, bool ambiguous_param) struct bstr param)
{ {
mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Setting %.*s=%.*s\n", BSTR_P(name), mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Setting %.*s=%.*s\n", BSTR_P(name),
BSTR_P(param)); BSTR_P(param));
return m_config_parse_option(config, config->optstruct, name, param, return m_config_parse_option(config, config->optstruct, name, param, true);
ambiguous_param, true);
} }
int m_config_check_option(struct m_config *config, struct bstr name, int m_config_check_option(struct m_config *config, struct bstr name,
struct bstr param, bool ambiguous_param) struct bstr param)
{ {
int r; int r;
mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name), mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name),
BSTR_P(param)); BSTR_P(param));
r = m_config_parse_option(config, NULL, name, param, ambiguous_param, 0); r = m_config_parse_option(config, NULL, name, param, 0);
if (r == M_OPT_MISSING_PARAM) { if (r == M_OPT_MISSING_PARAM) {
mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
"Error: option '%.*s' must have a parameter!\n", BSTR_P(name)); "Error: option '%.*s' must have a parameter!\n", BSTR_P(name));
@ -623,7 +608,7 @@ void m_profile_set_desc(struct m_profile *p, char *desc)
int m_config_set_profile_option(struct m_config *config, struct m_profile *p, int m_config_set_profile_option(struct m_config *config, struct m_profile *p,
char *name, char *val) char *name, char *val)
{ {
int i = m_config_check_option0(config, name, val, false); int i = m_config_check_option0(config, name, val);
if (i < 0) if (i < 0)
return i; return i;
p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2)); p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2));
@ -646,7 +631,7 @@ void m_config_set_profile(struct m_config *config, struct m_profile *p)
config->mode = M_CONFIG_FILE; config->mode = M_CONFIG_FILE;
config->profile_depth++; config->profile_depth++;
for (i = 0; i < p->num_opts; i++) for (i = 0; i < p->num_opts; i++)
m_config_set_option0(config, p->opts[2 * i], p->opts[2 * i + 1], false); m_config_set_option0(config, p->opts[2 * i], p->opts[2 * i + 1]);
config->profile_depth--; config->profile_depth--;
config->mode = prev_mode; config->mode = prev_mode;
} }

@ -60,12 +60,10 @@ struct m_profile {
}; };
enum option_source { enum option_source {
// Set when parsing from a config file.
M_CONFIG_FILE,
// Set when parsing command line arguments. // Set when parsing command line arguments.
M_COMMAND_LINE, M_COMMAND_LINE,
// Set when pre-parsing the command line // Set when parsing from a config file.
M_COMMAND_LINE_PRE_PARSE, M_CONFIG_FILE,
}; };
// Config object // Config object
@ -90,10 +88,6 @@ typedef struct m_config {
int (*includefunc)(struct m_config *conf, char *filename); int (*includefunc)(struct m_config *conf, char *filename);
} m_config_t; } m_config_t;
// Set if an option has been set at the current level.
#define M_CFG_OPT_SET (1 << 0)
// Set if another option already uses the same variable. // Set if another option already uses the same variable.
#define M_CFG_OPT_ALIAS (1 << 1) #define M_CFG_OPT_ALIAS (1 << 1)
@ -122,31 +116,27 @@ int m_config_register_options(struct m_config *config,
* \param config The config object. * \param config The config object.
* \param name The option's name. * \param name The option's name.
* \param param The value of the option, can be NULL. * \param param The value of the option, can be NULL.
* \param ambiguous_param: old style cmdline option, "param" may be a
parameter to this option or something entirely unrelated
* \return See \ref OptionParserReturn. * \return See \ref OptionParserReturn.
*/ */
int m_config_set_option(struct m_config *config, struct bstr name, int m_config_set_option(struct m_config *config, struct bstr name,
struct bstr param, bool ambiguous_param); struct bstr param);
static inline int m_config_set_option0(struct m_config *config, static inline int m_config_set_option0(struct m_config *config,
const char *name, const char *param, const char *name, const char *param)
bool ambiguous)
{ {
return m_config_set_option(config, bstr0(name), bstr0(param), ambiguous); return m_config_set_option(config, bstr0(name), bstr0(param));
} }
/* Check if an option setting is valid. /* Check if an option setting is valid.
* Same as above m_config_set_option() but doesn't actually set anything. * Same as above m_config_set_option() but doesn't actually set anything.
*/ */
int m_config_check_option(struct m_config *config, struct bstr name, int m_config_check_option(struct m_config *config, struct bstr name,
struct bstr param, bool ambiguous_param); struct bstr param);
static inline int m_config_check_option0(struct m_config *config, static inline int m_config_check_option0(struct m_config *config,
const char *name, const char *param, const char *name, const char *param)
bool ambiguous)
{ {
return m_config_check_option(config, bstr0(name), bstr0(param), ambiguous); return m_config_check_option(config, bstr0(name), bstr0(param));
} }
int m_config_parse_suboptions(struct m_config *config, char *name, int m_config_parse_suboptions(struct m_config *config, char *name,

@ -88,9 +88,9 @@ static void copy_opt(const m_option_t *opt, void *dst, const void *src)
#define VAL(x) (*(int *)(x)) #define VAL(x) (*(int *)(x))
static int parse_flag(const m_option_t *opt, struct bstr name, static int parse_flag(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
if (param.len && !ambiguous_param) { if (param.len) {
char * const enable[] = { "yes", "on", "ja", "si", "igen", "y", "j", char * const enable[] = { "yes", "on", "ja", "si", "igen", "y", "j",
"i", "tak", "ja", "true", "1" }; "i", "tak", "ja", "true", "1" };
for (int i = 0; i < sizeof(enable) / sizeof(enable[0]); i++) { for (int i = 0; i < sizeof(enable) / sizeof(enable[0]); i++) {
@ -132,6 +132,7 @@ const m_option_type_t m_option_type_flag = {
// need yes or no in config files // need yes or no in config files
.name = "Flag", .name = "Flag",
.size = sizeof(int), .size = sizeof(int),
.flags = M_OPT_TYPE_OLD_SYNTAX_NO_PARAM,
.parse = parse_flag, .parse = parse_flag,
.print = print_flag, .print = print_flag,
.copy = copy_opt, .copy = copy_opt,
@ -140,7 +141,7 @@ const m_option_type_t m_option_type_flag = {
// Integer // Integer
static int parse_longlong(const m_option_t *opt, struct bstr name, static int parse_longlong(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
if (param.len == 0) if (param.len == 0)
return M_OPT_MISSING_PARAM; return M_OPT_MISSING_PARAM;
@ -177,20 +178,20 @@ static int parse_longlong(const m_option_t *opt, struct bstr name,
} }
static int parse_int(const m_option_t *opt, struct bstr name, static int parse_int(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
long long tmp; long long tmp;
int r = parse_longlong(opt, name, param, false, &tmp); int r = parse_longlong(opt, name, param, &tmp);
if (r >= 0 && dst) if (r >= 0 && dst)
*(int *)dst = tmp; *(int *)dst = tmp;
return r; return r;
} }
static int parse_int64(const m_option_t *opt, struct bstr name, static int parse_int64(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
long long tmp; long long tmp;
int r = parse_longlong(opt, name, param, false, &tmp); int r = parse_longlong(opt, name, param, &tmp);
if (r >= 0 && dst) if (r >= 0 && dst)
*(int64_t *)dst = tmp; *(int64_t *)dst = tmp;
return r; return r;
@ -221,7 +222,7 @@ const m_option_type_t m_option_type_int64 = {
}; };
static int parse_intpair(const struct m_option *opt, struct bstr name, static int parse_intpair(const struct m_option *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
if (param.len == 0) if (param.len == 0)
return M_OPT_MISSING_PARAM; return M_OPT_MISSING_PARAM;
@ -264,13 +265,13 @@ const struct m_option_type m_option_type_intpair = {
}; };
static int parse_choice(const struct m_option *opt, struct bstr name, static int parse_choice(const struct m_option *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
bool allow_empty = opt->flags & M_OPT_IMPLICIT_DEFAULT; bool allow_empty = opt->flags & M_OPT_IMPLICIT_DEFAULT;
int ret; int ret;
struct m_opt_choice_alternatives *alt = opt->priv; struct m_opt_choice_alternatives *alt = opt->priv;
if (param.len == 0 || (ambiguous_param && allow_empty)) { if (param.len == 0) {
if (!allow_empty) if (!allow_empty)
return M_OPT_MISSING_PARAM; return M_OPT_MISSING_PARAM;
ret = 0; ret = 0;
@ -320,7 +321,7 @@ const struct m_option_type m_option_type_choice = {
#define VAL(x) (*(double *)(x)) #define VAL(x) (*(double *)(x))
static int parse_double(const m_option_t *opt, struct bstr name, static int parse_double(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
if (param.len == 0) if (param.len == 0)
return M_OPT_MISSING_PARAM; return M_OPT_MISSING_PARAM;
@ -394,10 +395,10 @@ const m_option_type_t m_option_type_double = {
#define VAL(x) (*(float *)(x)) #define VAL(x) (*(float *)(x))
static int parse_float(const m_option_t *opt, struct bstr name, static int parse_float(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
double tmp; double tmp;
int r = parse_double(opt, name, param, false, &tmp); int r = parse_double(opt, name, param, &tmp);
if (r == 1 && dst) if (r == 1 && dst)
VAL(dst) = tmp; VAL(dst) = tmp;
return r; return r;
@ -423,10 +424,10 @@ const m_option_type_t m_option_type_float = {
#define VAL(x) (*(off_t *)(x)) #define VAL(x) (*(off_t *)(x))
static int parse_position(const m_option_t *opt, struct bstr name, static int parse_position(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
long long tmp; long long tmp;
int r = parse_longlong(opt, name, param, false, &tmp); int r = parse_longlong(opt, name, param, &tmp);
if (r >= 0 && dst) if (r >= 0 && dst)
*(off_t *)dst = tmp; *(off_t *)dst = tmp;
return r; return r;
@ -453,7 +454,7 @@ const m_option_type_t m_option_type_position = {
#define VAL(x) (*(char **)(x)) #define VAL(x) (*(char **)(x))
static int parse_str(const m_option_t *opt, struct bstr name, static int parse_str(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
if (param.start == NULL) if (param.start == NULL)
return M_OPT_MISSING_PARAM; return M_OPT_MISSING_PARAM;
@ -642,7 +643,7 @@ static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify)
} }
static int parse_str_list(const m_option_t *opt, struct bstr name, static int parse_str_list(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
char **res; char **res;
int op = OP_NONE; int op = OP_NONE;
@ -788,17 +789,18 @@ const m_option_type_t m_option_type_string_list = {
/////////////////// Print /////////////////// Print
static int parse_print(const m_option_t *opt, struct bstr name, static int parse_print(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
if (opt->type == CONF_TYPE_PRINT_FUNC) { if (opt->type == CONF_TYPE_PRINT) {
mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s", mp_gtext(opt->p));
} else {
char *name0 = bstrdup0(NULL, name); char *name0 = bstrdup0(NULL, name);
char *param0 = bstrdup0(NULL, param); char *param0 = bstrdup0(NULL, param);
int r = ((m_opt_func_full_t) opt->p)(opt, name0, param0); int r = ((m_opt_func_full_t) opt->p)(opt, name0, param0);
talloc_free(name0); talloc_free(name0);
talloc_free(param0); talloc_free(param0);
return r; return r;
} else }
mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s", mp_gtext(opt->p));
if (opt->priv == NULL) if (opt->priv == NULL)
return M_OPT_EXIT; return M_OPT_EXIT;
@ -807,12 +809,19 @@ static int parse_print(const m_option_t *opt, struct bstr name,
const m_option_type_t m_option_type_print = { const m_option_type_t m_option_type_print = {
.name = "Print", .name = "Print",
.flags = M_OPT_TYPE_OLD_SYNTAX_NO_PARAM,
.parse = parse_print,
};
const m_option_type_t m_option_type_print_func_param = {
.name = "Print",
.flags = M_OPT_TYPE_ALLOW_WILDCARD,
.parse = parse_print, .parse = parse_print,
}; };
const m_option_type_t m_option_type_print_func = { const m_option_type_t m_option_type_print_func = {
.name = "Print", .name = "Print",
.flags = M_OPT_TYPE_ALLOW_WILDCARD, .flags = M_OPT_TYPE_ALLOW_WILDCARD | M_OPT_TYPE_OLD_SYNTAX_NO_PARAM,
.parse = parse_print, .parse = parse_print,
}; };
@ -822,7 +831,7 @@ const m_option_type_t m_option_type_print_func = {
#define VAL(x) (*(char ***)(x)) #define VAL(x) (*(char ***)(x))
static int parse_subconf(const m_option_t *opt, struct bstr name, static int parse_subconf(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
int nr = 0; int nr = 0;
char **lst = NULL; char **lst = NULL;
@ -982,7 +991,7 @@ static struct {
}; };
static int parse_imgfmt(const m_option_t *opt, struct bstr name, static int parse_imgfmt(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
uint32_t fmt = 0; uint32_t fmt = 0;
int i; int i;
@ -1072,7 +1081,7 @@ static struct {
}; };
static int parse_afmt(const m_option_t *opt, struct bstr name, static int parse_afmt(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
uint32_t fmt = 0; uint32_t fmt = 0;
int i; int i;
@ -1140,7 +1149,7 @@ static int parse_timestring(struct bstr str, double *time, char endchar)
static int parse_time(const m_option_t *opt, struct bstr name, static int parse_time(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
double time; double time;
@ -1170,7 +1179,7 @@ const m_option_type_t m_option_type_time = {
// Time or size (-endpos) // Time or size (-endpos)
static int parse_time_size(const m_option_t *opt, struct bstr name, static int parse_time_size(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
m_time_size_t ts; m_time_size_t ts;
char unit[4]; char unit[4];
@ -1267,7 +1276,7 @@ static int get_obj_param(struct bstr opt_name, struct bstr obj_name,
BSTR_P(opt_name), BSTR_P(obj_name), BSTR_P(str)); BSTR_P(opt_name), BSTR_P(obj_name), BSTR_P(str));
return M_OPT_UNKNOWN; return M_OPT_UNKNOWN;
} }
r = m_option_parse(opt, str, p, false, NULL); r = m_option_parse(opt, str, p, NULL);
if (r < 0) { if (r < 0) {
if (r > M_OPT_EXIT) if (r > M_OPT_EXIT)
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %.*s: " mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %.*s: "
@ -1287,7 +1296,7 @@ static int get_obj_param(struct bstr opt_name, struct bstr obj_name,
return M_OPT_OUT_OF_RANGE; return M_OPT_OUT_OF_RANGE;
} }
opt = &desc->fields[(*nold)]; opt = &desc->fields[(*nold)];
r = m_option_parse(opt, bstr0(opt->name), str, false, NULL); r = m_option_parse(opt, bstr0(opt->name), str, NULL);
if (r < 0) { if (r < 0) {
if (r > M_OPT_EXIT) if (r > M_OPT_EXIT)
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %.*s: " mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %.*s: "
@ -1402,7 +1411,7 @@ static int get_obj_params(struct bstr opt_name, struct bstr name,
} }
static int parse_obj_params(const m_option_t *opt, struct bstr name, static int parse_obj_params(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, void *dst) struct bstr param, void *dst)
{ {
char **opts; char **opts;
int r; int r;
@ -1510,7 +1519,7 @@ static int parse_obj_settings(struct bstr opt, struct bstr str,
} }
static int obj_settings_list_del(struct bstr opt_name, struct bstr param, static int obj_settings_list_del(struct bstr opt_name, struct bstr param,
bool ambiguous_param, void *dst) void *dst)
{ {
char **str_list = NULL; char **str_list = NULL;
int r, i, idx_max = 0; int r, i, idx_max = 0;
@ -1534,7 +1543,7 @@ static int obj_settings_list_del(struct bstr opt_name, struct bstr param,
/* NOP */; /* NOP */;
} }
r = m_option_parse(&list_opt, opt_name, param, false, &str_list); r = m_option_parse(&list_opt, opt_name, param, &str_list);
if (r < 0 || !str_list) if (r < 0 || !str_list)
return r; return r;
@ -1599,8 +1608,7 @@ static void free_obj_settings_list(void *dst)
} }
static int parse_obj_settings_list(const m_option_t *opt, struct bstr name, static int parse_obj_settings_list(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, struct bstr param, void *dst)
void *dst)
{ {
int len = strlen(opt->name); int len = strlen(opt->name);
m_obj_settings_t *res = NULL, *queue = NULL, *head = NULL; m_obj_settings_t *res = NULL, *queue = NULL, *head = NULL;
@ -1662,7 +1670,7 @@ static int parse_obj_settings_list(const m_option_t *opt, struct bstr name,
queue = VAL(dst); queue = VAL(dst);
break; break;
case OP_DEL: case OP_DEL:
return obj_settings_list_del(name, param, false, dst); return obj_settings_list_del(name, param, dst);
case OP_NONE: case OP_NONE:
if (dst && VAL(dst)) if (dst && VAL(dst))
free_obj_settings_list(dst); free_obj_settings_list(dst);
@ -1773,8 +1781,7 @@ const m_option_type_t m_option_type_obj_settings_list = {
static int parse_obj_presets(const m_option_t *opt, struct bstr name, static int parse_obj_presets(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, struct bstr param, void *dst)
void *dst)
{ {
m_obj_presets_t *obj_p = (m_obj_presets_t *)opt->priv; m_obj_presets_t *obj_p = (m_obj_presets_t *)opt->priv;
const m_struct_t *in_desc; const m_struct_t *in_desc;
@ -1849,7 +1856,7 @@ const m_option_type_t m_option_type_obj_presets = {
}; };
static int parse_custom_url(const m_option_t *opt, struct bstr name, static int parse_custom_url(const m_option_t *opt, struct bstr name,
struct bstr url, bool ambiguous_param, void *dst) struct bstr url, void *dst)
{ {
int r; int r;
m_struct_t *desc = opt->priv; m_struct_t *desc = opt->priv;

@ -50,6 +50,7 @@ extern const m_option_type_t m_option_type_choice;
extern const m_option_type_t m_option_type_print; extern const m_option_type_t m_option_type_print;
extern const m_option_type_t m_option_type_print_func; extern const m_option_type_t m_option_type_print_func;
extern const m_option_type_t m_option_type_print_func_param;
extern const m_option_type_t m_option_type_subconfig; extern const m_option_type_t m_option_type_subconfig;
extern const m_option_type_t m_option_type_imgfmt; extern const m_option_type_t m_option_type_imgfmt;
extern const m_option_type_t m_option_type_afmt; extern const m_option_type_t m_option_type_afmt;
@ -177,7 +178,7 @@ struct m_option_type {
const char *name; const char *name;
// Size needed for the data. // Size needed for the data.
unsigned int size; unsigned int size;
// See \ref OptionTypeFlags. // One of M_OPT_TYPE*.
unsigned int flags; unsigned int flags;
// Parse the data from a string. // Parse the data from a string.
@ -186,7 +187,6 @@ struct m_option_type {
* \param opt The option that is parsed. * \param opt The option that is parsed.
* \param name The full option name. * \param name The full option name.
* \param param The parameter to parse. * \param param The parameter to parse.
* \param ambiguous_param: "param" old cmdline style, "param" may or
* may not be an argument meant for this option * may not be an argument meant for this option
* \param dst Pointer to the memory where the data should be written. * \param dst Pointer to the memory where the data should be written.
* If NULL the parameter validity should still be checked. * If NULL the parameter validity should still be checked.
@ -194,7 +194,7 @@ struct m_option_type {
* of arguments consumed. For details see \ref OptionParserReturn. * of arguments consumed. For details see \ref OptionParserReturn.
*/ */
int (*parse)(const m_option_t *opt, struct bstr name, struct bstr param, int (*parse)(const m_option_t *opt, struct bstr name, struct bstr param,
bool ambiguous_param, void *dst); void *dst);
// Print back a value in string form. // Print back a value in string form.
/** \param opt The option to print. /** \param opt The option to print.
@ -325,6 +325,11 @@ struct m_option {
*/ */
#define M_OPT_TYPE_DYNAMIC (1 << 2) #define M_OPT_TYPE_DYNAMIC (1 << 2)
// The parameter is optional and by default no parameter is preferred. If the
// "old syntax" is used, the command line parser will assume that the argument
// takes no parameter.
#define M_OPT_TYPE_OLD_SYNTAX_NO_PARAM (1 << 3)
///////////////////////////// Parser flags ///////////////////////////////// ///////////////////////////// Parser flags /////////////////////////////////
// On success parsers return the number of arguments consumed: 0 or 1. // On success parsers return the number of arguments consumed: 0 or 1.
@ -377,10 +382,9 @@ static inline void *m_option_get_ptr(const struct m_option *opt,
// Helper to parse options, see \ref m_option_type::parse. // Helper to parse options, see \ref m_option_type::parse.
static inline int m_option_parse(const m_option_t *opt, struct bstr name, static inline int m_option_parse(const m_option_t *opt, struct bstr name,
struct bstr param, bool ambiguous_param, struct bstr param, void *dst)
void *dst)
{ {
return opt->type->parse(opt, name, param, ambiguous_param, dst); return opt->type->parse(opt, name, param, dst);
} }
// Helper to print options, see \ref m_option_type::print. // Helper to print options, see \ref m_option_type::print.

@ -108,8 +108,7 @@ int m_property_do(const m_option_t *prop_list, const char *name,
if (!arg) if (!arg)
return M_PROPERTY_ERROR; return M_PROPERTY_ERROR;
val = calloc(1, opt->type->size); val = calloc(1, opt->type->size);
if ((r = m_option_parse(opt, bstr0(opt->name), bstr0(arg), false, if ((r = m_option_parse(opt, bstr0(opt->name), bstr0(arg), val)) <= 0) {
val)) <= 0) {
free(val); free(val);
return r; return r;
} }

@ -71,7 +71,7 @@ int m_struct_set(const m_struct_t *st, void *obj, const char *field,
return 0; return 0;
} }
if(f->type->parse(f, bstr0(field), param, false, M_ST_MB_P(obj,f->p)) < 0) { if(f->type->parse(f, bstr0(field), param, M_ST_MB_P(obj,f->p)) < 0) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %.*s\n", mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %.*s\n",
st->name, field, BSTR_P(param)); st->name, field, BSTR_P(param));
return 0; return 0;

@ -26,6 +26,8 @@
#define ROUND(x) ((int)((x) < 0 ? (x) - 0.5 : (x) + 0.5)) #define ROUND(x) ((int)((x) < 0 ? (x) - 0.5 : (x) + 0.5))
#define MP_EXPAND_ARGS(...) __VA_ARGS__
#define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0])) #define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0]))
#define MP_GROW_ARRAY(p, nextidx) do { \ #define MP_GROW_ARRAY(p, nextidx) do { \
if ((nextidx) == MP_TALLOC_ELEMS(p)) \ if ((nextidx) == MP_TALLOC_ELEMS(p)) \
@ -43,15 +45,13 @@
(nextidx_ + 1) * sizeof((p)[0]) * 2);\ (nextidx_ + 1) * sizeof((p)[0]) * 2);\
} while (0) } while (0)
#define MP_TARRAY_APPEND(ctx, p, idxvar, val) \ #define MP_TARRAY_APPEND(ctx, p, idxvar, ...) \
do { \ do { \
MP_TARRAY_GROW(ctx, p, idxvar); \ MP_TARRAY_GROW(ctx, p, idxvar); \
p[idxvar] = (val); \ p[idxvar] = (MP_EXPAND_ARGS(__VA_ARGS__)); \
idxvar++; \ idxvar++; \
} while (0) } while (0)
#define MP_EXPAND_ARGS(...) __VA_ARGS__
#define talloc_struct(ctx, type, ...) \ #define talloc_struct(ctx, type, ...) \
talloc_memdup(ctx, &(type) MP_EXPAND_ARGS(__VA_ARGS__), sizeof(type)) talloc_memdup(ctx, &(type) MP_EXPAND_ARGS(__VA_ARGS__), sizeof(type))

@ -853,7 +853,7 @@ static void load_per_file_options(m_config_t *conf,
int params_count) int params_count)
{ {
for (int n = 0; n < params_count; n++) for (int n = 0; n < params_count; n++)
m_config_set_option(conf, params[n].name, params[n].value, false); m_config_set_option(conf, params[n].name, params[n].value);
} }
/* When libmpdemux performs a blocking operation (network connection or /* When libmpdemux performs a blocking operation (network connection or
@ -4211,8 +4211,13 @@ int main(int argc, char *argv[])
{ {
osdep_preinit(&argc, &argv); osdep_preinit(&argc, &argv);
if (argc > 1 && (!strcmp(argv[1], "-leak-report") if (argc >= 1) {
|| !strcmp(argv[1], "--leak-report"))) argc--;
argv++;
}
if (argc > 0 && (!strcmp(argv[0], "-leak-report")
|| !strcmp(argv[0], "--leak-report")))
talloc_enable_leak_report(); talloc_enable_leak_report();
struct MPContext *mpctx = talloc(NULL, MPContext); struct MPContext *mpctx = talloc(NULL, MPContext);
@ -4224,6 +4229,7 @@ int main(int argc, char *argv[])
.file_format = DEMUXER_TYPE_UNKNOWN, .file_format = DEMUXER_TYPE_UNKNOWN,
.last_dvb_step = 1, .last_dvb_step = 1,
.terminal_osd_text = talloc_strdup(mpctx, ""), .terminal_osd_text = talloc_strdup(mpctx, ""),
.playlist = talloc_struct(mpctx, struct playlist, {0}),
}; };
mp_msg_init(); mp_msg_init();
@ -4239,7 +4245,7 @@ int main(int argc, char *argv[])
mp_input_register_options(mpctx->mconfig); mp_input_register_options(mpctx->mconfig);
// Preparse the command line // Preparse the command line
m_config_preparse_command_line(mpctx->mconfig, argc, argv, &verbose); m_config_preparse_command_line(mpctx->mconfig, argc, argv);
print_version(false); print_version(false);
print_libav_versions(); print_libav_versions();
@ -4247,12 +4253,9 @@ int main(int argc, char *argv[])
if (!parse_cfgfiles(mpctx, mpctx->mconfig)) if (!parse_cfgfiles(mpctx, mpctx->mconfig))
exit_player(mpctx, EXIT_NONE, 1); exit_player(mpctx, EXIT_NONE, 1);
mpctx->playlist = talloc_struct(mpctx, struct playlist, {0}); if (!m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist,
if (m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist,
argc, argv)) argc, argv))
{ {
mpctx->playlist->current = mpctx->playlist->first;
} else {
exit_player(mpctx, EXIT_ERROR, 1); exit_player(mpctx, EXIT_ERROR, 1);
} }
@ -4269,7 +4272,6 @@ int main(int argc, char *argv[])
mp_msg(MSGT_CPLAYER, MSGL_V, "\n"); mp_msg(MSGT_CPLAYER, MSGL_V, "\n");
if (!mpctx->playlist->first && !opts->player_idle_mode) { if (!mpctx->playlist->first && !opts->player_idle_mode) {
// no file/vcd/dvd -> show HELP:
print_version(true); print_version(true);
mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s", mp_gtext(help_text)); mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s", mp_gtext(help_text));
exit_player(mpctx, EXIT_NONE, 0); exit_player(mpctx, EXIT_NONE, 0);
@ -4287,6 +4289,7 @@ int main(int argc, char *argv[])
init_input(mpctx); init_input(mpctx);
mpctx->playlist->current = mpctx->playlist->first;
play_files(mpctx); play_files(mpctx);
exit_player(mpctx, EXIT_EOF, mpctx->quit_player_rc); exit_player(mpctx, EXIT_EOF, mpctx->quit_player_rc);

@ -225,7 +225,7 @@ int m_config_parse_config_file(m_config_t *config, const char *conffile)
tmp = m_config_set_profile_option(config, profile, tmp = m_config_set_profile_option(config, profile,
opt, param); opt, param);
} else } else
tmp = m_config_set_option0(config, opt, param, false); tmp = m_config_set_option0(config, opt, param);
if (tmp < 0) { if (tmp < 0) {
PRINT_LINENUM; PRINT_LINENUM;
if (tmp == M_OPT_UNKNOWN) { if (tmp == M_OPT_UNKNOWN) {

@ -39,58 +39,120 @@
#define dvd_range(a) (a > 0 && a < 256) #define dvd_range(a) (a > 0 && a < 256)
static bool split_opt(struct bstr *opt, struct bstr *param, bool *old_syntax) struct parse_state {
struct m_config *config;
int argc;
char **argv;
bool no_more_opts;
bool error;
const struct m_option *mp_opt; // NULL <=> it's a file arg
struct bstr arg;
struct bstr param;
};
// Returns 0 if a valid option/file is available, <0 on error, 1 on end of args.
static int split_opt_silent(struct parse_state *p)
{ {
if (!bstr_startswith0(*opt, "-") || opt->len == 1) assert(!p->error);
return false;
if (bstr_startswith0(*opt, "--")) { if (p->argc < 1)
*old_syntax = false; return 1;
*opt = bstr_cut(*opt, 2);
*param = bstr0(NULL); p->mp_opt = NULL;
int idx = bstrchr(*opt, '='); p->arg = bstr0(p->argv[0]);
if (idx > 0) { p->param = bstr0(NULL);
*param = bstr_cut(*opt, idx + 1);
*opt = bstr_splice(*opt, 0, idx); p->argc--;
} p->argv++;
if (p->no_more_opts || !bstr_startswith0(p->arg, "-") || p->arg.len == 1)
return 0;
bool old_syntax = !bstr_startswith0(p->arg, "--");
if (old_syntax) {
p->arg = bstr_cut(p->arg, 1);
} else { } else {
*old_syntax = true; p->arg = bstr_cut(p->arg, 2);
*opt = bstr_cut(*opt, 1); int idx = bstrchr(p->arg, '=');
if (idx > 0) {
p->param = bstr_cut(p->arg, idx + 1);
p->arg = bstr_splice(p->arg, 0, idx);
} }
return true; }
p->mp_opt = m_config_get_option(p->config, p->arg);
if (!p->mp_opt) {
// Automagic "no-" arguments: "--no-bla" turns into "--bla=no".
if (!bstr_startswith0(p->arg, "no-"))
return -1;
struct bstr s = bstr_cut(p->arg, 3);
p->mp_opt = m_config_get_option(p->config, s);
if (!p->mp_opt || p->mp_opt->type != &m_option_type_flag)
return -1;
// Avoid allowing "--no-no-bla".
if (bstr_startswith(bstr0(p->mp_opt->name), bstr0("no-")))
return -1;
// Flag options never have parameters.
old_syntax = false;
if (p->param.len)
return -2;
p->arg = s;
p->param = bstr0("no");
}
if (bstr_endswith0(p->arg, "-clr"))
old_syntax = false;
if (old_syntax && !(p->mp_opt->type->flags & M_OPT_TYPE_OLD_SYNTAX_NO_PARAM))
{
if (p->argc < 1)
return -3;
p->param = bstr0(p->argv[0]);
p->argc--;
p->argv++;
}
return 0;
} }
static int map_to_option(struct m_config *config, bool old_syntax, // Returns true if more args, false if all parsed or an error occurred.
const struct m_option **mp_opt, static bool split_opt(struct parse_state *p)
struct bstr *optname, struct bstr *param)
{ {
if (!mp_opt) int r = split_opt_silent(p);
mp_opt = &(const struct m_option *){0}; if (r >= 0)
*mp_opt = m_config_get_option(config, *optname); return r == 0;
if (*mp_opt) p->error = true;
return 0; if (r == -2)
if (!bstr_startswith0(*optname, "no-")) mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
return -1; "A no-* option can't take parameters: --%.*s=%.*s\n",
struct bstr s = bstr_cut(*optname, 3); BSTR_P(p->arg), BSTR_P(p->param));
*mp_opt = m_config_get_option(config, s); else if (r == -3)
if (!*mp_opt || (*mp_opt)->type != &m_option_type_flag) mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
return -1; "Option %.*s needs a parameter.\n", BSTR_P(p->arg));
if (param->len) else
return -2; mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
if (old_syntax) "Unknown option on the command line: %.*s\n",
return -3; BSTR_P(p->arg));
*optname = s; return false;
*param = bstr0("no"); }
return 0;
static bool parse_flag(bstr name, bstr f)
{
struct m_option opt = {NULL, NULL, CONF_TYPE_FLAG, 0, 0, 1, NULL};
int val = 0;
m_option_parse(&opt, name, f, &val);
return !!val;
} }
bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files, bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
int argc, char **argv) int argc, char **argv)
{ {
int mode = 0; int mode = 0;
bool no_more_opts = false;
bool opt_exit = false; // exit immediately after parsing (help options) bool opt_exit = false; // exit immediately after parsing (help options)
struct playlist_entry *local_start = NULL; struct playlist_entry *local_start = NULL;
struct bstr orig_opt;
bool shuffle = false; bool shuffle = false;
int local_params_count = 0; int local_params_count = 0;
@ -98,8 +160,6 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
assert(config != NULL); assert(config != NULL);
assert(!config->file_local_mode); assert(!config->file_local_mode);
assert(argv != NULL);
assert(argc >= 1);
config->mode = M_COMMAND_LINE; config->mode = M_COMMAND_LINE;
mode = GLOBAL; mode = GLOBAL;
@ -108,18 +168,34 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
return true; return true;
#endif #endif
for (int i = 1; i < argc; i++) { struct parse_state p = {config, argc, argv};
//next: while (split_opt(&p)) {
struct bstr opt = bstr0(argv[i]); if (p.mp_opt) {
orig_opt = opt; int r;
/* check for -- (no more options id.) except --help! */ if (mode == GLOBAL && !(p.mp_opt->flags & M_OPT_PRE_PARSE)) {
if (!bstrcmp0(opt, "--")) { r = m_config_set_option(config, p.arg, p.param);
no_more_opts = true; } else {
continue; r = m_config_check_option(config, p.arg, p.param);
} }
if (!bstrcmp0(opt, "--{")) { if (r <= M_OPT_EXIT) {
opt_exit = true;
r = M_OPT_EXIT - r;
} else if (r < 0) {
char *msg = m_option_strerror(r);
if (!msg)
goto print_err;
mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL,
"Error parsing commandline option %.*s: %s\n",
BSTR_P(p.arg), msg);
goto err_out;
}
// Handle some special arguments outside option parser.
if (!bstrcmp0(p.arg, "{")) {
if (mode != GLOBAL) { if (mode != GLOBAL) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--{' can not be nested\n"); mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"'--{' can not be nested.\n");
goto err_out; goto err_out;
} }
mode = LOCAL; mode = LOCAL;
@ -130,20 +206,21 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
continue; continue;
} }
if (!bstrcmp0(opt, "--}")) { if (!bstrcmp0(p.arg, "}")) {
if (mode != LOCAL) { if (mode != LOCAL) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too many closing '--}'\n"); mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"Too many closing '--}'.\n");
goto err_out; goto err_out;
} }
if (local_params_count) { if (local_params_count) {
// The files added between '{' and '}' are the entries from the // The files added between '{' and '}' are the entries from
// entry _after_ local_start, until the end of the list. If // the entry _after_ local_start, until the end of the list.
// local_start is NULL, the list was empty on '{', and we want // If local_start is NULL, the list was empty on '{', and we
// all files in the list. // want all files in the list.
struct playlist_entry *cur struct playlist_entry *cur
= local_start ? local_start->next : files->first; = local_start ? local_start->next : files->first;
if (!cur) if (!cur)
mp_msg(MSGT_CFGPARSER, MSGL_WARN, "ignored options\n"); mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Ignored options!\n");
while (cur) { while (cur) {
playlist_entry_add_params(cur, local_params, playlist_entry_add_params(cur, local_params,
local_params_count); local_params_count);
@ -154,86 +231,47 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
mode = GLOBAL; mode = GLOBAL;
m_config_leave_file_local(config); m_config_leave_file_local(config);
local_start = NULL; local_start = NULL;
shuffle = false;
continue; continue;
} }
struct bstr param = bstr0(i+1 < argc ? argv[i+1] : NULL); if (bstrcmp0(p.arg, "shuffle") == 0) {
bool old_syntax; shuffle = parse_flag(p.arg, p.param);
if (!no_more_opts && split_opt(&opt, &param, &old_syntax)) { continue;
const struct m_option *mp_opt;
int ok = map_to_option(config, old_syntax, &mp_opt, &opt, &param);
if (ok < 0) {
if (ok == -3)
mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
"Option --%.*s can't be used with single-dash "
"syntax\n", BSTR_P(opt));
else if (ok == -2)
mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
"A --no-* option can't take parameters: "
"--%.*s=%.*s\n", BSTR_P(opt), BSTR_P(param));
else
mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
"Unknown option on the command line: --%.*s\n",
BSTR_P(opt));
goto print_err;
} }
// Handle some special arguments outside option parser.
// --loop when it applies to a group of files (per-file is option) if (bstrcmp0(p.arg, "playlist") == 0) {
if (bstrcasecmp0(opt, "shuffle") == 0) {
shuffle = true;
} else if (bstrcasecmp0(opt, "no-shuffle") == 0) {
shuffle = false;
} else if (bstrcasecmp0(opt, "playlist") == 0) {
if (param.len <= 0)
goto print_err;
// append the playlist to the local args // append the playlist to the local args
char *param0 = bstrdup0(NULL, param); char *param0 = bstrdup0(NULL, p.param);
struct playlist *pl = playlist_parse_file(param0); struct playlist *pl = playlist_parse_file(param0);
talloc_free(param0); talloc_free(param0);
if (!pl) if (!pl)
goto print_err; goto print_err;
playlist_transfer_entries(files, pl); playlist_transfer_entries(files, pl);
talloc_free(pl); talloc_free(pl);
i += old_syntax; continue;
}
if (bstrcmp0(p.arg, "v") == 0) {
verbose++;
continue;
}
if (mode == LOCAL) {
MP_TARRAY_APPEND(NULL, local_params, local_params_count,
(struct playlist_param) {p.arg, p.param});
}
} else { } else {
// "normal" options // filename
int r; bstr file = p.arg;
if (mode == GLOBAL) { char *file0 = bstrdup0(NULL, p.arg);
r = m_config_set_option(config, opt, param, old_syntax); int is_dvdnav = bstr_startswith0(file, "dvdnav://");
} else {
r = m_config_check_option(config, opt, param, old_syntax);
if (r >= 0) {
if (r == 0)
param = bstr0(NULL); // for old_syntax case
struct playlist_param p = {opt, param};
MP_TARRAY_APPEND(NULL, local_params,
local_params_count, p);
}
}
if (r <= M_OPT_EXIT) {
opt_exit = true;
r = M_OPT_EXIT - r;
} else if (r < 0) {
char *msg = m_option_strerror(r);
if (!msg)
goto print_err;
mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL,
"Error parsing commandline option \"%.*s\": %s\n",
BSTR_P(orig_opt), msg);
goto err_out;
}
if (old_syntax)
i += r;
}
} else { /* filename */
int is_dvdnav = strstr(argv[i], "dvdnav://") != NULL;
mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Adding file %s\n", argv[i]);
// expand DVD filename entries like dvd://1-3 into component titles // expand DVD filename entries like dvd://1-3 into component titles
if (strstr(argv[i], "dvd://") != NULL || is_dvdnav) { if (bstr_startswith0(file, "dvd://") || is_dvdnav) {
int offset = is_dvdnav ? 9 : 6; int offset = is_dvdnav ? 9 : 6;
char *splitpos = strstr(argv[i] + offset, "-"); char *splitpos = strstr(file0 + offset, "-");
if (splitpos != NULL) { if (splitpos != NULL) {
int start_title = strtol(argv[i] + offset, NULL, 10); int start_title = strtol(file0 + offset, NULL, 10);
int end_title; int end_title;
//entries like dvd://-2 imply start at title 1 //entries like dvd://-2 imply start at title 1
if (start_title < 0) { if (start_title < 0) {
@ -252,20 +290,21 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
} }
} else } else
mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,
"Invalid play entry %s\n", argv[i]); "Invalid play entry %s\n", file0);
} else // dvd:// or dvd://x entry } else // dvd:// or dvd://x entry
playlist_add_file(files, argv[i]); playlist_add_file(files, file0);
} else } else
playlist_add_file(files, argv[i]); playlist_add_file(files, file0);
talloc_free(file0);
// Lock stdin if it will be used as input // Lock stdin if it will be used as input
if (strcasecmp(argv[i], "-") == 0) if (bstrcmp0(file, "-") == 0)
m_config_set_option0(config, "consolecontrols", "no", false); m_config_set_option0(config, "consolecontrols", "no");
} }
} }
if (opt_exit) if (p.error)
goto err_out; goto err_out;
if (mode != GLOBAL) { if (mode != GLOBAL) {
@ -274,6 +313,9 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
goto err_out; goto err_out;
} }
if (opt_exit)
goto err_out;
if (shuffle) if (shuffle)
playlist_shuffle(files); playlist_shuffle(files);
@ -283,8 +325,7 @@ bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
print_err: print_err:
mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL, mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL,
"Error parsing option on the command line: %.*s\n", "Error parsing option on the command line: %.*s\n", BSTR_P(p.arg));
BSTR_P(orig_opt));
err_out: err_out:
talloc_free(local_params); talloc_free(local_params);
if (config->file_local_mode) if (config->file_local_mode)
@ -299,43 +340,21 @@ extern int mp_msg_levels[];
* command line parsing), and --really-quiet suppresses messages printed * command line parsing), and --really-quiet suppresses messages printed
* during normal options parsing. * during normal options parsing.
*/ */
int m_config_preparse_command_line(m_config_t *config, int argc, char **argv, void m_config_preparse_command_line(m_config_t *config, int argc, char **argv)
int *verbose)
{ {
int ret = 0;
// Hack to shut up parser error messages // Hack to shut up parser error messages
int msg_lvl_backup = mp_msg_levels[MSGT_CFGPARSER]; int msg_lvl_backup = mp_msg_levels[MSGT_CFGPARSER];
mp_msg_levels[MSGT_CFGPARSER] = -11; mp_msg_levels[MSGT_CFGPARSER] = -11;
config->mode = M_COMMAND_LINE_PRE_PARSE; struct parse_state p = {config, argc, argv};
while (split_opt_silent(&p) == 0) {
for (int i = 1 ; i < argc ; i++) { if (p.mp_opt) {
struct bstr opt = bstr0(argv[i]); // Ignore non-pre-parse options. They will be set later.
// No more options after -- // Option parsing errors will be handled later as well.
if (!bstrcmp0(opt, "--")) if (p.mp_opt->flags & M_OPT_PRE_PARSE)
break; m_config_set_option(config, p.arg, p.param);
struct bstr param = bstr0(i+1 < argc ? argv[i+1] : NULL);
bool old_syntax;
if (!split_opt(&opt, &param, &old_syntax))
continue; // Ignore non-option arguments
// Ignore invalid options
if (map_to_option(config, old_syntax, NULL, &opt, &param) < 0)
continue;
// "-v" is handled here
if (!bstrcmp0(opt, "v")) {
(*verbose)++;
continue;
} }
// Set, non-pre-parse options will be ignored
int r = m_config_set_option(config, opt, param, old_syntax);
if (r < 0)
ret = r;
else if (old_syntax)
i += r;
} }
mp_msg_levels[MSGT_CFGPARSER] = msg_lvl_backup; mp_msg_levels[MSGT_CFGPARSER] = msg_lvl_backup;
return ret;
} }

@ -20,11 +20,14 @@
#define MPLAYER_PARSER_MPCMD_H #define MPLAYER_PARSER_MPCMD_H
#include <stdbool.h> #include <stdbool.h>
#include "m_config.h"
bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files, struct playlist;
struct m_config;
bool m_config_parse_mp_command_line(struct m_config *config,
struct playlist *files,
int argc, char **argv);
void m_config_preparse_command_line(struct m_config *config,
int argc, char **argv); int argc, char **argv);
int m_config_preparse_command_line(m_config_t *config, int argc, char **argv,
int *verbose);
#endif /* MPLAYER_PARSER_MPCMD_H */ #endif /* MPLAYER_PARSER_MPCMD_H */

@ -160,7 +160,7 @@ static stream_t *open_stream_plugin(const stream_info_t *sinfo,
if(sinfo->opts_url) { if(sinfo->opts_url) {
m_option_t url_opt = m_option_t url_opt =
{ "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, (void *)sinfo->opts }; { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, (void *)sinfo->opts };
if (m_option_parse(&url_opt, bstr0("stream url"), bstr0(filename), false, arg) < 0) { if (m_option_parse(&url_opt, bstr0("stream url"), bstr0(filename), arg) < 0) {
mp_tmsg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename); mp_tmsg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
m_struct_free(desc,arg); m_struct_free(desc,arg);
return NULL; return NULL;