2013-12-26 16:10:35 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2013-12-26 16:10:35 +00:00
|
|
|
*
|
input: change license to LGPL
cehoyos adds the step_property command in 7a71da01d, and it could be
argued that copyright of this still applies to the later add/cycle
commands (a668ae0ff90c4). While I'm not sure if this is really the case,
stay conservative for now and mark these commands as GPL-only. Mark the
command.c code too, although that is not being relicensed yet.
I'm leaving the MP_CMD_* enum items, as they are obviously different.
In commit 116ca0c7682, "veal" (essentially an anonymous author) adds an
"osd_show_property_text" command (well, the commit message says "based
on" that person's code, so it's not clear how much is from him or from
albeu, who agreed to LGPL). This was later merged again with the
"osd_show_text" command, and then all original code was removed in
commit 58cc0f637f, so I claim that no copyright applies anymore. (Though
technically the input.conf addition still might be copyrighted, so I'm
just dropping it to get rid of the thought.)
"kiriuja" added 2f376d1b39 (sub_load etc.) and be54f4813 (switch_audio).
The latter is gone. I would argue that the former is fully rewritten
with commits b7052b431c9 and 0f155921b0. But like in the step_property
case, I will be overly conservative for now, and mark them as GPL-only,
as this is potentially shaky and should be thought through first. (Not
bothering with the command define/enum in the header, as it will be
unused in LGPL mode anyway.)
keycodes.c/h can be GPL, except for commit 2b1f95dcc2f8, which is a
patch by someone who wasn't asked yet. Before doing something radical, I
will wait for a reply.
2017-06-19 11:02:35 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-12-26 16:10:35 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-12-26 16:10:35 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
input: change license to LGPL
cehoyos adds the step_property command in 7a71da01d, and it could be
argued that copyright of this still applies to the later add/cycle
commands (a668ae0ff90c4). While I'm not sure if this is really the case,
stay conservative for now and mark these commands as GPL-only. Mark the
command.c code too, although that is not being relicensed yet.
I'm leaving the MP_CMD_* enum items, as they are obviously different.
In commit 116ca0c7682, "veal" (essentially an anonymous author) adds an
"osd_show_property_text" command (well, the commit message says "based
on" that person's code, so it's not clear how much is from him or from
albeu, who agreed to LGPL). This was later merged again with the
"osd_show_text" command, and then all original code was removed in
commit 58cc0f637f, so I claim that no copyright applies anymore. (Though
technically the input.conf addition still might be copyrighted, so I'm
just dropping it to get rid of the thought.)
"kiriuja" added 2f376d1b39 (sub_load etc.) and be54f4813 (switch_audio).
The latter is gone. I would argue that the former is fully rewritten
with commits b7052b431c9 and 0f155921b0. But like in the step_property
case, I will be overly conservative for now, and mark them as GPL-only,
as this is potentially shaky and should be thought through first. (Not
bothering with the command define/enum in the header, as it will be
unused in LGPL mode anyway.)
keycodes.c/h can be GPL, except for commit 2b1f95dcc2f8, which is a
patch by someone who wasn't asked yet. Before doing something radical, I
will wait for a reply.
2017-06-19 11:02:35 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-12-26 16:10:35 +00:00
|
|
|
*
|
input: change license to LGPL
cehoyos adds the step_property command in 7a71da01d, and it could be
argued that copyright of this still applies to the later add/cycle
commands (a668ae0ff90c4). While I'm not sure if this is really the case,
stay conservative for now and mark these commands as GPL-only. Mark the
command.c code too, although that is not being relicensed yet.
I'm leaving the MP_CMD_* enum items, as they are obviously different.
In commit 116ca0c7682, "veal" (essentially an anonymous author) adds an
"osd_show_property_text" command (well, the commit message says "based
on" that person's code, so it's not clear how much is from him or from
albeu, who agreed to LGPL). This was later merged again with the
"osd_show_text" command, and then all original code was removed in
commit 58cc0f637f, so I claim that no copyright applies anymore. (Though
technically the input.conf addition still might be copyrighted, so I'm
just dropping it to get rid of the thought.)
"kiriuja" added 2f376d1b39 (sub_load etc.) and be54f4813 (switch_audio).
The latter is gone. I would argue that the former is fully rewritten
with commits b7052b431c9 and 0f155921b0. But like in the step_property
case, I will be overly conservative for now, and mark them as GPL-only,
as this is potentially shaky and should be thought through first. (Not
bothering with the command define/enum in the header, as it will be
unused in LGPL mode anyway.)
keycodes.c/h can be GPL, except for commit 2b1f95dcc2f8, which is a
patch by someone who wasn't asked yet. Before doing something radical, I
will wait for a reply.
2017-06-19 11:02:35 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-12-26 16:10:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2014-08-29 10:09:04 +00:00
|
|
|
#include "misc/bstr.h"
|
2018-05-12 12:50:07 +00:00
|
|
|
#include "misc/node.h"
|
2013-12-26 16:10:35 +00:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "options/m_option.h"
|
|
|
|
|
2018-05-01 01:14:30 +00:00
|
|
|
#include "cmd.h"
|
2013-12-26 16:10:35 +00:00
|
|
|
#include "input.h"
|
2020-01-12 00:04:33 +00:00
|
|
|
#include "misc/json.h"
|
2013-12-26 16:10:35 +00:00
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
#include "libmpv/client.h"
|
|
|
|
|
2014-02-23 16:43:38 +00:00
|
|
|
static void destroy_cmd(void *ptr)
|
|
|
|
{
|
|
|
|
struct mp_cmd *cmd = ptr;
|
2018-05-12 12:50:07 +00:00
|
|
|
for (int n = 0; n < cmd->nargs; n++) {
|
|
|
|
if (cmd->args[n].type)
|
|
|
|
m_option_free(cmd->args[n].type, &cmd->args[n].v);
|
|
|
|
}
|
2014-02-23 16:43:38 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 16:10:35 +00:00
|
|
|
struct flag {
|
|
|
|
const char *name;
|
|
|
|
unsigned int remove, add;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct flag cmd_flags[] = {
|
|
|
|
{"no-osd", MP_ON_OSD_FLAGS, MP_ON_OSD_NO},
|
|
|
|
{"osd-bar", MP_ON_OSD_FLAGS, MP_ON_OSD_BAR},
|
|
|
|
{"osd-msg", MP_ON_OSD_FLAGS, MP_ON_OSD_MSG},
|
|
|
|
{"osd-msg-bar", MP_ON_OSD_FLAGS, MP_ON_OSD_MSG | MP_ON_OSD_BAR},
|
|
|
|
{"osd-auto", MP_ON_OSD_FLAGS, MP_ON_OSD_AUTO},
|
2014-11-20 22:41:01 +00:00
|
|
|
{"expand-properties", 0, MP_EXPAND_PROPERTIES},
|
2013-12-26 16:10:35 +00:00
|
|
|
{"raw", MP_EXPAND_PROPERTIES, 0},
|
2014-11-20 22:41:01 +00:00
|
|
|
{"repeatable", 0, MP_ALLOW_REPEAT},
|
2018-05-06 16:27:18 +00:00
|
|
|
{"async", MP_SYNC_CMD, MP_ASYNC_CMD},
|
|
|
|
{"sync", MP_ASYNC_CMD, MP_SYNC_CMD},
|
2013-12-26 16:10:35 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
static bool apply_flag(struct mp_cmd *cmd, bstr str)
|
|
|
|
{
|
|
|
|
for (int n = 0; cmd_flags[n].name; n++) {
|
|
|
|
if (bstr_equals0(str, cmd_flags[n].name)) {
|
|
|
|
cmd->flags = (cmd->flags & ~cmd_flags[n].remove) | cmd_flags[n].add;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool find_cmd(struct mp_log *log, struct mp_cmd *cmd, bstr name)
|
|
|
|
{
|
|
|
|
if (name.len == 0) {
|
|
|
|
mp_err(log, "Command name missing.\n");
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-25 19:59:44 +00:00
|
|
|
|
|
|
|
char nname[80];
|
|
|
|
snprintf(nname, sizeof(nname), "%.*s", BSTR_P(name));
|
|
|
|
for (int n = 0; nname[n]; n++) {
|
|
|
|
if (nname[n] == '_')
|
|
|
|
nname[n] = '-';
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
for (int n = 0; mp_cmds[n].name; n++) {
|
2015-05-25 19:59:44 +00:00
|
|
|
if (strcmp(nname, mp_cmds[n].name) == 0) {
|
2014-10-10 20:37:11 +00:00
|
|
|
cmd->def = &mp_cmds[n];
|
|
|
|
cmd->name = (char *)cmd->def->name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_err(log, "Command '%.*s' not found.\n", BSTR_P(name));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_vararg(const struct mp_cmd_def *m, int i)
|
|
|
|
{
|
2018-01-10 00:20:58 +00:00
|
|
|
return m->vararg && (i + 1 >= MP_CMD_DEF_MAX_ARGS || !m->args[i + 1].type);
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct m_option *get_arg_type(const struct mp_cmd_def *cmd, int i)
|
|
|
|
{
|
2018-01-10 00:20:58 +00:00
|
|
|
const struct m_option *opt = NULL;
|
|
|
|
if (is_vararg(cmd, i)) {
|
2014-10-10 20:37:11 +00:00
|
|
|
// The last arg in a vararg command sets all vararg types.
|
2018-01-10 00:20:58 +00:00
|
|
|
for (int n = MPMIN(i, MP_CMD_DEF_MAX_ARGS - 1); n >= 0; n--) {
|
2014-10-10 20:37:11 +00:00
|
|
|
if (cmd->args[n].type) {
|
|
|
|
opt = &cmd->args[n];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 00:20:58 +00:00
|
|
|
} else if (i < MP_CMD_DEF_MAX_ARGS) {
|
|
|
|
opt = &cmd->args[i];
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
2018-01-10 00:20:58 +00:00
|
|
|
return opt && opt->type ? opt : NULL;
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 12:50:07 +00:00
|
|
|
// Return the name of the argument, possibly as stack allocated string (which is
|
|
|
|
// why this is a macro, and out of laziness). Otherwise as get_arg_type().
|
|
|
|
#define get_arg_name(cmd, i) \
|
|
|
|
((i) < MP_CMD_DEF_MAX_ARGS && (cmd)->args[(i)].name && \
|
|
|
|
(cmd)->args[(i)].name[0] \
|
|
|
|
? (cmd)->args[(i)].name : mp_tprintf(10, "%d", (i) + 1))
|
|
|
|
|
|
|
|
// Verify that there are no missing args, fill in missing optional args.
|
2014-10-10 20:37:11 +00:00
|
|
|
static bool finish_cmd(struct mp_log *log, struct mp_cmd *cmd)
|
|
|
|
{
|
2018-05-12 12:50:07 +00:00
|
|
|
for (int i = 0; i < MP_CMD_DEF_MAX_ARGS; i++) {
|
|
|
|
// (type==NULL is used for yet unset arguments)
|
|
|
|
if (i < cmd->nargs && cmd->args[i].type)
|
|
|
|
continue;
|
2018-01-10 00:20:58 +00:00
|
|
|
const struct m_option *opt = get_arg_type(cmd->def, i);
|
2018-05-12 12:50:07 +00:00
|
|
|
if (i >= cmd->nargs && (!opt || is_vararg(cmd->def, i)))
|
2018-01-10 00:20:58 +00:00
|
|
|
break;
|
|
|
|
if (!opt->defval && !(opt->flags & MP_CMD_OPT_ARG)) {
|
2018-05-12 12:50:07 +00:00
|
|
|
mp_err(log, "Command %s: required argument %s not set.\n",
|
|
|
|
cmd->name, get_arg_name(cmd->def, i));
|
2018-01-10 00:20:58 +00:00
|
|
|
return false;
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
2018-01-10 00:20:58 +00:00
|
|
|
struct mp_cmd_arg arg = {.type = opt};
|
|
|
|
if (opt->defval)
|
|
|
|
m_option_copy(opt, &arg.v, opt->defval);
|
2018-05-12 12:50:07 +00:00
|
|
|
assert(i <= cmd->nargs);
|
|
|
|
if (i == cmd->nargs) {
|
|
|
|
MP_TARRAY_APPEND(cmd, cmd->args, cmd->nargs, arg);
|
|
|
|
} else {
|
|
|
|
cmd->args[i] = arg;
|
|
|
|
}
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
2018-05-06 16:27:18 +00:00
|
|
|
|
|
|
|
if (!(cmd->flags & (MP_ASYNC_CMD | MP_SYNC_CMD)))
|
|
|
|
cmd->flags |= cmd->def->default_async ? MP_ASYNC_CMD : MP_SYNC_CMD;
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-12 12:50:07 +00:00
|
|
|
static bool set_node_arg(struct mp_log *log, struct mp_cmd *cmd, int i,
|
|
|
|
mpv_node *val)
|
2014-10-10 20:37:11 +00:00
|
|
|
{
|
2018-05-12 12:50:07 +00:00
|
|
|
const char *name = get_arg_name(cmd->def, i);
|
2014-10-10 20:37:11 +00:00
|
|
|
|
2018-05-12 12:50:07 +00:00
|
|
|
const struct m_option *opt = get_arg_type(cmd->def, i);
|
|
|
|
if (!opt) {
|
|
|
|
mp_err(log, "Command %s: has only %d arguments.\n", cmd->name, i);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < cmd->nargs && cmd->args[i].type) {
|
|
|
|
mp_err(log, "Command %s: argument %s was already set.\n", cmd->name, name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mp_cmd_arg arg = {.type = opt};
|
|
|
|
void *dst = &arg.v;
|
|
|
|
if (val->format == MPV_FORMAT_STRING) {
|
|
|
|
int r = m_option_parse(log, opt, bstr0(cmd->name),
|
|
|
|
bstr0(val->u.string), dst);
|
|
|
|
if (r < 0) {
|
|
|
|
mp_err(log, "Command %s: argument %s can't be parsed: %s.\n",
|
|
|
|
cmd->name, name, m_option_strerror(r));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int r = m_option_set_node(opt, dst, val);
|
|
|
|
if (r < 0) {
|
|
|
|
mp_err(log, "Command %s: argument %s has incompatible type.\n",
|
|
|
|
cmd->name, name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (leave unset arguments blank, to be set later or checked by finish_cmd())
|
|
|
|
while (i >= cmd->nargs) {
|
|
|
|
struct mp_cmd_arg t = {0};
|
|
|
|
MP_TARRAY_APPEND(cmd, cmd->args, cmd->nargs, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->args[i] = arg;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool cmd_node_array(struct mp_log *log, struct mp_cmd *cmd, mpv_node *node)
|
|
|
|
{
|
|
|
|
assert(node->format == MPV_FORMAT_NODE_ARRAY);
|
2014-10-10 20:37:11 +00:00
|
|
|
mpv_node_list *args = node->u.list;
|
|
|
|
int cur = 0;
|
|
|
|
|
|
|
|
while (cur < args->num) {
|
|
|
|
if (args->values[cur].format != MPV_FORMAT_STRING)
|
|
|
|
break;
|
|
|
|
if (!apply_flag(cmd, bstr0(args->values[cur].u.string)))
|
|
|
|
break;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bstr cmd_name = {0};
|
|
|
|
if (cur < args->num && args->values[cur].format == MPV_FORMAT_STRING)
|
|
|
|
cmd_name = bstr0(args->values[cur++].u.string);
|
|
|
|
if (!find_cmd(log, cmd, cmd_name))
|
2018-05-12 12:50:07 +00:00
|
|
|
return false;
|
2014-10-10 20:37:11 +00:00
|
|
|
|
|
|
|
int first = cur;
|
|
|
|
for (int i = 0; i < args->num - first; i++) {
|
2018-05-12 12:50:07 +00:00
|
|
|
if (!set_node_arg(log, cmd, cmd->nargs, &args->values[cur++]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool cmd_node_map(struct mp_log *log, struct mp_cmd *cmd, mpv_node *node)
|
|
|
|
{
|
|
|
|
assert(node->format == MPV_FORMAT_NODE_MAP);
|
|
|
|
mpv_node_list *args = node->u.list;
|
|
|
|
|
|
|
|
mpv_node *name = node_map_get(node, "name");
|
|
|
|
if (!name || name->format != MPV_FORMAT_STRING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!find_cmd(log, cmd, bstr0(name->u.string)))
|
|
|
|
return false;
|
|
|
|
|
2018-05-17 18:10:39 +00:00
|
|
|
if (cmd->def->vararg) {
|
|
|
|
mp_err(log, "Command %s: this command uses a variable number of "
|
|
|
|
"arguments, which does not work with named arguments.\n",
|
|
|
|
cmd->name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-12 12:50:07 +00:00
|
|
|
for (int n = 0; n < args->num; n++) {
|
|
|
|
const char *key = args->keys[n];
|
|
|
|
mpv_node *val = &args->values[n];
|
|
|
|
|
|
|
|
if (strcmp(key, "name") == 0) {
|
|
|
|
// already handled above
|
|
|
|
} else if (strcmp(key, "_flags") == 0) {
|
|
|
|
if (val->format != MPV_FORMAT_NODE_ARRAY)
|
|
|
|
return false;
|
|
|
|
mpv_node_list *flags = val->u.list;
|
|
|
|
for (int i = 0; i < flags->num; i++) {
|
|
|
|
if (flags->values[i].format != MPV_FORMAT_STRING)
|
|
|
|
return false;
|
|
|
|
if (!apply_flag(cmd, bstr0(flags->values[i].u.string)))
|
|
|
|
return false;
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-12 12:50:07 +00:00
|
|
|
int arg = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < MP_CMD_DEF_MAX_ARGS; i++) {
|
|
|
|
const char *arg_name = cmd->def->args[i].name;
|
|
|
|
if (arg_name && arg_name[0] && strcmp(key, arg_name) == 0) {
|
|
|
|
arg = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg < 0) {
|
|
|
|
mp_err(log, "Command %s: no argument %s.\n", cmd->name, key);
|
|
|
|
return false;
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
2018-05-12 12:50:07 +00:00
|
|
|
|
|
|
|
if (!set_node_arg(log, cmd, arg, val))
|
|
|
|
return false;
|
2014-10-10 20:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 12:50:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mp_cmd *mp_input_parse_cmd_node(struct mp_log *log, mpv_node *node)
|
|
|
|
{
|
|
|
|
struct mp_cmd *cmd = talloc_ptrtype(NULL, cmd);
|
|
|
|
talloc_set_destructor(cmd, destroy_cmd);
|
|
|
|
*cmd = (struct mp_cmd) { .scale = 1, .scale_units = 1 };
|
|
|
|
|
|
|
|
bool res = false;
|
|
|
|
if (node->format == MPV_FORMAT_NODE_ARRAY) {
|
|
|
|
res = cmd_node_array(log, cmd, node);
|
|
|
|
} else if (node->format == MPV_FORMAT_NODE_MAP) {
|
|
|
|
res = cmd_node_map(log, cmd, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = res && finish_cmd(log, cmd);
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
TA_FREEP(&cmd);
|
2014-10-10 20:37:11 +00:00
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2014-10-10 21:01:00 +00:00
|
|
|
static bool read_token(bstr str, bstr *out_rest, bstr *out_token)
|
|
|
|
{
|
|
|
|
bstr t = bstr_lstrip(str);
|
2014-10-31 22:53:39 +00:00
|
|
|
int next = bstrcspn(t, WHITESPACE "#;");
|
2014-10-10 21:01:00 +00:00
|
|
|
if (!next)
|
|
|
|
return false;
|
|
|
|
*out_token = bstr_splice(t, 0, next);
|
|
|
|
*out_rest = bstr_cut(t, next);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct parse_ctx {
|
|
|
|
struct mp_log *log;
|
|
|
|
void *tmp;
|
|
|
|
bstr start, str;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int pctx_read_token(struct parse_ctx *ctx, bstr *out)
|
|
|
|
{
|
|
|
|
*out = (bstr){0};
|
2014-10-10 21:05:33 +00:00
|
|
|
ctx->str = bstr_lstrip(ctx->str);
|
|
|
|
bstr start = ctx->str;
|
|
|
|
if (bstr_eatstart0(&ctx->str, "\"")) {
|
|
|
|
if (!mp_append_escaped_string_noalloc(ctx->tmp, out, &ctx->str)) {
|
|
|
|
MP_ERR(ctx, "Broken string escapes: ...>%.*s<.\n", BSTR_P(start));
|
|
|
|
return -1;
|
2014-10-10 21:01:00 +00:00
|
|
|
}
|
2014-10-10 21:05:33 +00:00
|
|
|
if (!bstr_eatstart0(&ctx->str, "\"")) {
|
2021-07-28 09:29:08 +00:00
|
|
|
MP_ERR(ctx, "Unterminated double quote: ...>%.*s<.\n", BSTR_P(start));
|
2014-10-10 21:05:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
2014-10-10 21:01:00 +00:00
|
|
|
}
|
2021-07-28 09:29:08 +00:00
|
|
|
if (bstr_eatstart0(&ctx->str, "'")) {
|
|
|
|
int next = bstrchr(ctx->str, '\'');
|
|
|
|
if (next < 0) {
|
|
|
|
MP_ERR(ctx, "Unterminated single quote: ...>%.*s<.\n", BSTR_P(start));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*out = bstr_splice(ctx->str, 0, next);
|
|
|
|
ctx->str = bstr_cut(ctx->str, next+1);
|
|
|
|
return 1;
|
|
|
|
}
|
2021-07-28 08:09:30 +00:00
|
|
|
if (ctx->start.len > 1 && bstr_eatstart0(&ctx->str, "`")) {
|
|
|
|
char endquote[2] = {ctx->str.start[0], '`'};
|
2020-12-28 13:58:38 +00:00
|
|
|
ctx->str = bstr_cut(ctx->str, 1);
|
|
|
|
int next = bstr_find(ctx->str, (bstr){endquote, 2});
|
|
|
|
if (next < 0) {
|
|
|
|
MP_ERR(ctx, "Unterminated custom quote: ...>%.*s<.\n", BSTR_P(start));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*out = bstr_splice(ctx->str, 0, next);
|
|
|
|
ctx->str = bstr_cut(ctx->str, next+2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-10 21:05:33 +00:00
|
|
|
return read_token(ctx->str, &ctx->str, out) ? 1 : 0;
|
2014-10-10 21:01:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:28 +00:00
|
|
|
static struct mp_cmd *parse_cmd_str(struct mp_log *log, void *tmp,
|
|
|
|
bstr *str, const char *loc)
|
2013-12-26 16:10:35 +00:00
|
|
|
{
|
2014-10-10 20:58:28 +00:00
|
|
|
struct parse_ctx *ctx = &(struct parse_ctx){
|
|
|
|
.log = log,
|
|
|
|
.tmp = tmp,
|
|
|
|
.str = *str,
|
|
|
|
.start = *str,
|
|
|
|
};
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
struct mp_cmd *cmd = talloc_ptrtype(NULL, cmd);
|
|
|
|
talloc_set_destructor(cmd, destroy_cmd);
|
2014-10-10 20:58:28 +00:00
|
|
|
*cmd = (struct mp_cmd) {
|
|
|
|
.flags = MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES,
|
|
|
|
.scale = 1,
|
2017-04-25 08:03:06 +00:00
|
|
|
.scale_units = 1,
|
2014-10-10 20:58:28 +00:00
|
|
|
};
|
2013-12-26 16:10:35 +00:00
|
|
|
|
2014-10-10 21:05:33 +00:00
|
|
|
ctx->str = bstr_lstrip(ctx->str);
|
2013-12-26 16:10:35 +00:00
|
|
|
|
|
|
|
bstr cur_token;
|
|
|
|
if (pctx_read_token(ctx, &cur_token) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while (1) {
|
2014-10-10 20:37:11 +00:00
|
|
|
if (!apply_flag(cmd, cur_token))
|
|
|
|
break;
|
|
|
|
if (pctx_read_token(ctx, &cur_token) < 0)
|
|
|
|
goto error;
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
if (!find_cmd(ctx->log, cmd, cur_token))
|
2013-12-26 16:10:35 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (int i = 0; i < MP_CMD_MAX_ARGS; i++) {
|
2014-10-10 20:37:11 +00:00
|
|
|
const struct m_option *opt = get_arg_type(cmd->def, i);
|
|
|
|
if (!opt)
|
2013-12-26 16:10:35 +00:00
|
|
|
break;
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
int r = pctx_read_token(ctx, &cur_token);
|
2013-12-26 16:10:35 +00:00
|
|
|
if (r < 0) {
|
|
|
|
MP_ERR(ctx, "Command %s: error in argument %d.\n", cmd->name, i + 1);
|
|
|
|
goto error;
|
|
|
|
}
|
2014-10-10 20:37:11 +00:00
|
|
|
if (r < 1)
|
|
|
|
break;
|
2013-12-26 16:10:35 +00:00
|
|
|
|
2018-01-10 00:20:58 +00:00
|
|
|
struct mp_cmd_arg arg = {.type = opt};
|
|
|
|
r = m_option_parse(ctx->log, opt, bstr0(cmd->name), cur_token, &arg.v);
|
2013-12-26 16:10:35 +00:00
|
|
|
if (r < 0) {
|
|
|
|
MP_ERR(ctx, "Command %s: argument %d can't be parsed: %s.\n",
|
|
|
|
cmd->name, i + 1, m_option_strerror(r));
|
|
|
|
goto error;
|
|
|
|
}
|
2018-01-10 00:20:58 +00:00
|
|
|
|
|
|
|
MP_TARRAY_APPEND(cmd, cmd->args, cmd->nargs, arg);
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:37:11 +00:00
|
|
|
if (!finish_cmd(ctx->log, cmd))
|
|
|
|
goto error;
|
|
|
|
|
2014-10-10 21:05:33 +00:00
|
|
|
bstr dummy;
|
|
|
|
if (read_token(ctx->str, &dummy, &dummy) && ctx->str.len) {
|
2013-12-26 16:10:35 +00:00
|
|
|
MP_ERR(ctx, "Command %s has trailing unused arguments: '%.*s'.\n",
|
2014-10-10 21:05:33 +00:00
|
|
|
cmd->name, BSTR_P(ctx->str));
|
2013-12-26 16:10:35 +00:00
|
|
|
// Better make it fatal to make it clear something is wrong.
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-10-10 21:05:33 +00:00
|
|
|
bstr orig = {ctx->start.start, ctx->str.start - ctx->start.start};
|
2019-11-22 17:21:33 +00:00
|
|
|
cmd->original = bstrto0(cmd, bstr_strip(orig));
|
2013-12-26 16:10:35 +00:00
|
|
|
|
2014-10-10 20:58:28 +00:00
|
|
|
*str = ctx->str;
|
2013-12-26 16:10:35 +00:00
|
|
|
return cmd;
|
|
|
|
|
|
|
|
error:
|
2014-10-10 21:05:33 +00:00
|
|
|
MP_ERR(ctx, "Command was defined at %s.\n", loc);
|
2013-12-26 16:10:35 +00:00
|
|
|
talloc_free(cmd);
|
2014-10-10 20:58:28 +00:00
|
|
|
*str = ctx->str;
|
2013-12-26 16:10:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-01 01:36:39 +00:00
|
|
|
mp_cmd_t *mp_input_parse_cmd_str(struct mp_log *log, bstr str, const char *loc)
|
2013-12-26 16:10:35 +00:00
|
|
|
{
|
2014-09-08 13:13:11 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
2013-12-26 16:10:35 +00:00
|
|
|
bstr original = str;
|
2014-09-08 13:13:11 +00:00
|
|
|
struct mp_cmd *cmd = parse_cmd_str(log, tmp, &str, loc);
|
2013-12-26 16:10:35 +00:00
|
|
|
if (!cmd)
|
2014-09-08 13:13:11 +00:00
|
|
|
goto done;
|
2013-12-26 16:10:35 +00:00
|
|
|
|
|
|
|
// Handle "multi" commands
|
|
|
|
struct mp_cmd **p_prev = NULL;
|
|
|
|
while (1) {
|
|
|
|
str = bstr_lstrip(str);
|
|
|
|
// read_token just to check whether it's trailing whitespace only
|
|
|
|
bstr u1, u2;
|
|
|
|
if (!bstr_eatstart0(&str, ";") || !read_token(str, &u1, &u2))
|
|
|
|
break;
|
|
|
|
// Multi-command. Since other input.c code uses queue_next for its
|
|
|
|
// own purposes, a pseudo-command is used to wrap the command list.
|
|
|
|
if (!p_prev) {
|
|
|
|
struct mp_cmd *list = talloc_ptrtype(NULL, list);
|
2014-02-23 16:43:38 +00:00
|
|
|
talloc_set_destructor(list, destroy_cmd);
|
2013-12-26 16:10:35 +00:00
|
|
|
*list = (struct mp_cmd) {
|
2018-04-30 18:33:05 +00:00
|
|
|
.name = (char *)mp_cmd_list.name,
|
|
|
|
.def = &mp_cmd_list,
|
2013-12-26 16:10:35 +00:00
|
|
|
};
|
|
|
|
talloc_steal(list, cmd);
|
2018-01-10 00:20:58 +00:00
|
|
|
struct mp_cmd_arg arg = {0};
|
|
|
|
arg.v.p = cmd;
|
2018-01-18 09:08:48 +00:00
|
|
|
list->args = talloc_dup(list, &arg);
|
2013-12-26 16:10:35 +00:00
|
|
|
p_prev = &cmd->queue_next;
|
|
|
|
cmd = list;
|
|
|
|
}
|
2014-09-08 13:13:11 +00:00
|
|
|
struct mp_cmd *sub = parse_cmd_str(log, tmp, &str, loc);
|
2013-12-26 16:10:35 +00:00
|
|
|
if (!sub) {
|
|
|
|
talloc_free(cmd);
|
2014-09-08 13:13:11 +00:00
|
|
|
cmd = NULL;
|
|
|
|
goto done;
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
|
|
|
talloc_steal(cmd, sub);
|
|
|
|
*p_prev = sub;
|
|
|
|
p_prev = &sub->queue_next;
|
|
|
|
}
|
|
|
|
|
2019-11-23 00:09:09 +00:00
|
|
|
cmd->original = bstrto0(cmd, bstr_strip(
|
|
|
|
bstr_splice(original, 0, str.start - original.start)));
|
|
|
|
|
|
|
|
str = bstr_strip(str);
|
|
|
|
if (bstr_eatstart0(&str, "#") && !bstr_startswith0(str, "#")) {
|
|
|
|
str = bstr_strip(str);
|
|
|
|
if (str.len)
|
|
|
|
cmd->desc = bstrto0(cmd, str);
|
|
|
|
}
|
|
|
|
|
2014-09-08 13:13:11 +00:00
|
|
|
done:
|
|
|
|
talloc_free(tmp);
|
2013-12-26 16:10:35 +00:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:28 +00:00
|
|
|
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, const char **argv)
|
2013-12-26 16:10:35 +00:00
|
|
|
{
|
2018-01-10 00:20:58 +00:00
|
|
|
int count = 0;
|
|
|
|
while (argv[count])
|
|
|
|
count++;
|
|
|
|
mpv_node *items = talloc_zero_array(NULL, mpv_node, count);
|
|
|
|
mpv_node_list list = {.values = items, .num = count};
|
2014-10-10 20:58:28 +00:00
|
|
|
mpv_node node = {.format = MPV_FORMAT_NODE_ARRAY, .u = {.list = &list}};
|
2018-01-10 00:20:58 +00:00
|
|
|
for (int n = 0; n < count; n++) {
|
|
|
|
items[n] = (mpv_node){.format = MPV_FORMAT_STRING,
|
|
|
|
.u = {.string = (char *)argv[n]}};
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
2018-01-10 00:20:58 +00:00
|
|
|
struct mp_cmd *res = mp_input_parse_cmd_node(log, &node);
|
|
|
|
talloc_free(items);
|
|
|
|
return res;
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mp_cmd_free(mp_cmd_t *cmd)
|
|
|
|
{
|
|
|
|
talloc_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
|
|
|
|
{
|
|
|
|
if (!cmd)
|
|
|
|
return NULL;
|
|
|
|
|
2018-01-18 09:18:22 +00:00
|
|
|
mp_cmd_t *ret = talloc_dup(NULL, cmd);
|
2014-02-23 16:43:38 +00:00
|
|
|
talloc_set_destructor(ret, destroy_cmd);
|
2013-12-26 16:10:35 +00:00
|
|
|
ret->name = talloc_strdup(ret, cmd->name);
|
2018-01-10 00:20:58 +00:00
|
|
|
ret->args = talloc_zero_array(ret, struct mp_cmd_arg, ret->nargs);
|
2018-01-18 09:18:22 +00:00
|
|
|
for (int i = 0; i < ret->nargs; i++) {
|
2018-01-10 00:20:58 +00:00
|
|
|
ret->args[i].type = cmd->args[i].type;
|
2014-02-23 16:43:38 +00:00
|
|
|
m_option_copy(ret->args[i].type, &ret->args[i].v, &cmd->args[i].v);
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
2019-11-22 17:21:33 +00:00
|
|
|
ret->original = talloc_strdup(ret, cmd->original);
|
2019-11-23 00:09:09 +00:00
|
|
|
ret->desc = talloc_strdup(ret, cmd->desc);
|
2019-11-22 17:21:33 +00:00
|
|
|
ret->sender = NULL;
|
2015-12-23 18:10:38 +00:00
|
|
|
ret->key_name = talloc_strdup(ret, ret->key_name);
|
2019-11-21 22:01:56 +00:00
|
|
|
ret->key_text = talloc_strdup(ret, ret->key_text);
|
2013-12-26 16:10:35 +00:00
|
|
|
|
2018-04-30 18:33:05 +00:00
|
|
|
if (cmd->def == &mp_cmd_list) {
|
2013-12-26 16:10:35 +00:00
|
|
|
struct mp_cmd *prev = NULL;
|
|
|
|
for (struct mp_cmd *sub = cmd->args[0].v.p; sub; sub = sub->queue_next) {
|
|
|
|
sub = mp_cmd_clone(sub);
|
|
|
|
talloc_steal(ret, sub);
|
|
|
|
if (prev) {
|
|
|
|
prev->queue_next = sub;
|
|
|
|
} else {
|
2018-01-10 00:20:58 +00:00
|
|
|
struct mp_cmd_arg arg = {0};
|
|
|
|
arg.v.p = sub;
|
2018-01-18 09:08:48 +00:00
|
|
|
ret->args = talloc_dup(ret, &arg);
|
2013-12-26 16:10:35 +00:00
|
|
|
}
|
|
|
|
prev = sub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:25:18 +00:00
|
|
|
static int get_arg_count(const struct mp_cmd_def *cmd)
|
|
|
|
{
|
|
|
|
for (int i = MP_CMD_DEF_MAX_ARGS - 1; i >= 0; i--) {
|
|
|
|
if (cmd->args[i].type)
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-23 12:40:38 +00:00
|
|
|
void mp_cmd_dump(struct mp_log *log, int msgl, char *header, struct mp_cmd *cmd)
|
|
|
|
{
|
|
|
|
if (!mp_msg_test(log, msgl))
|
|
|
|
return;
|
|
|
|
if (header)
|
2014-10-24 11:42:02 +00:00
|
|
|
mp_msg(log, msgl, "%s ", header);
|
2014-10-23 12:40:38 +00:00
|
|
|
if (!cmd) {
|
|
|
|
mp_msg(log, msgl, "(NULL)\n");
|
|
|
|
return;
|
|
|
|
}
|
2014-10-24 11:42:02 +00:00
|
|
|
mp_msg(log, msgl, "%s, flags=%d, args=[", cmd->name, cmd->flags);
|
2020-02-19 15:25:18 +00:00
|
|
|
int argc = get_arg_count(cmd->def);
|
2014-10-23 12:40:38 +00:00
|
|
|
for (int n = 0; n < cmd->nargs; n++) {
|
2020-02-19 15:25:18 +00:00
|
|
|
const char *argname = cmd->def->args[MPMIN(n, argc - 1)].name;
|
2014-10-23 12:40:38 +00:00
|
|
|
char *s = m_option_print(cmd->args[n].type, &cmd->args[n].v);
|
|
|
|
if (n)
|
|
|
|
mp_msg(log, msgl, ", ");
|
2020-01-12 00:04:33 +00:00
|
|
|
struct mpv_node node = {
|
|
|
|
.format = MPV_FORMAT_STRING,
|
|
|
|
.u.string = s ? s : "(NULL)",
|
|
|
|
};
|
|
|
|
char *esc = NULL;
|
|
|
|
json_write(&esc, &node);
|
2020-02-19 15:25:18 +00:00
|
|
|
mp_msg(log, msgl, "%s=%s", argname, esc ? esc : "<error>");
|
2020-01-12 00:04:33 +00:00
|
|
|
talloc_free(esc);
|
2014-10-23 12:40:38 +00:00
|
|
|
talloc_free(s);
|
|
|
|
}
|
|
|
|
mp_msg(log, msgl, "]\n");
|
|
|
|
}
|
|
|
|
|
2018-05-01 01:19:50 +00:00
|
|
|
bool mp_input_is_repeatable_cmd(struct mp_cmd *cmd)
|
|
|
|
{
|
2021-08-16 21:11:17 +00:00
|
|
|
if (cmd->def == &mp_cmd_list && cmd->args[0].v.p)
|
|
|
|
cmd = cmd->args[0].v.p; // list - only 1st cmd is considered
|
|
|
|
|
|
|
|
return (cmd->def->allow_auto_repeat) || (cmd->flags & MP_ALLOW_REPEAT);
|
2018-05-01 01:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_input_is_scalable_cmd(struct mp_cmd *cmd)
|
|
|
|
{
|
|
|
|
return cmd->def->scalable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_print_cmd_list(struct mp_log *out)
|
|
|
|
{
|
|
|
|
for (int i = 0; mp_cmds[i].name; i++) {
|
|
|
|
const struct mp_cmd_def *def = &mp_cmds[i];
|
|
|
|
mp_info(out, "%-20.20s", def->name);
|
|
|
|
for (int j = 0; j < MP_CMD_DEF_MAX_ARGS && def->args[j].type; j++) {
|
2018-05-17 18:34:31 +00:00
|
|
|
const struct m_option *arg = &def->args[j];
|
|
|
|
bool is_opt = arg->defval || (arg->flags & MP_CMD_OPT_ARG);
|
|
|
|
mp_info(out, " %s%s=%s%s", is_opt ? "[" : "", arg->name,
|
|
|
|
arg->type->name, is_opt ? "]" : "");
|
2018-05-01 01:19:50 +00:00
|
|
|
}
|
2018-05-17 18:34:31 +00:00
|
|
|
if (def->vararg)
|
|
|
|
mp_info(out, "..."); // essentially append to last argument
|
2018-05-01 01:19:50 +00:00
|
|
|
mp_info(out, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 16:10:35 +00:00
|
|
|
static int parse_cycle_dir(struct mp_log *log, const struct m_option *opt,
|
|
|
|
struct bstr name, struct bstr param, void *dst)
|
|
|
|
{
|
|
|
|
double val;
|
|
|
|
if (bstrcmp0(param, "up") == 0) {
|
|
|
|
val = +1;
|
|
|
|
} else if (bstrcmp0(param, "down") == 0) {
|
|
|
|
val = -1;
|
|
|
|
} else {
|
|
|
|
return m_option_type_double.parse(log, opt, name, param, dst);
|
|
|
|
}
|
|
|
|
*(double *)dst = val;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-19 19:53:26 +00:00
|
|
|
static char *print_cycle_dir(const m_option_t *opt, const void *val)
|
|
|
|
{
|
|
|
|
return talloc_asprintf(NULL, "%f", *(double *)val);
|
|
|
|
}
|
|
|
|
|
2015-09-10 12:15:12 +00:00
|
|
|
static void copy_opt(const m_option_t *opt, void *dst, const void *src)
|
|
|
|
{
|
|
|
|
if (dst && src)
|
|
|
|
memcpy(dst, src, opt->type->size);
|
|
|
|
}
|
|
|
|
|
2013-12-26 16:10:35 +00:00
|
|
|
const struct m_option_type m_option_type_cycle_dir = {
|
|
|
|
.name = "up|down",
|
|
|
|
.parse = parse_cycle_dir,
|
2020-01-19 19:53:26 +00:00
|
|
|
.print = print_cycle_dir,
|
2015-09-10 12:15:12 +00:00
|
|
|
.copy = copy_opt,
|
2013-12-26 16:10:35 +00:00
|
|
|
.size = sizeof(double),
|
|
|
|
};
|