input: use mpv_node parser for char** command parsers

Minor simplification, also drops some useless stuff.
This commit is contained in:
wm4 2014-10-10 22:58:28 +02:00
parent 63903c27bd
commit c9f45ea93e
6 changed files with 43 additions and 64 deletions

View File

@ -261,11 +261,23 @@ error:
return NULL;
}
static struct mp_cmd *parse_cmd(struct parse_ctx *ctx, int def_flags)
static struct mp_cmd *parse_cmd_str(struct mp_log *log, void *tmp,
bstr *str, const char *loc)
{
struct parse_ctx *ctx = &(struct parse_ctx){
.log = log,
.loc = loc,
.tmp = tmp,
.str = *str,
.start = *str,
};
struct mp_cmd *cmd = talloc_ptrtype(NULL, cmd);
talloc_set_destructor(cmd, destroy_cmd);
*cmd = (struct mp_cmd) { .flags = def_flags, .scale = 1, };
*cmd = (struct mp_cmd) {
.flags = MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES,
.scale = 1,
};
if (!ctx->array_input) {
ctx->str = bstr_lstrip(ctx->str);
@ -332,29 +344,16 @@ static struct mp_cmd *parse_cmd(struct parse_ctx *ctx, int def_flags)
cmd->original = bstrdup(cmd, bstr_strip(orig));
}
*str = ctx->str;
return cmd;
error:
MP_ERR(ctx, "Command was defined at %s.\n", ctx->loc);
talloc_free(cmd);
*str = ctx->str;
return NULL;
}
static struct mp_cmd *parse_cmd_str(struct mp_log *log, void *tmp,
bstr *str, const char *loc)
{
struct parse_ctx ctx = {
.log = log,
.loc = loc,
.tmp = tmp,
.str = *str,
.start = *str,
};
struct mp_cmd *res = parse_cmd(&ctx, MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES);
*str = ctx.str;
return res;
}
mp_cmd_t *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc)
{
void *tmp = talloc_new(NULL);
@ -402,36 +401,21 @@ done:
return cmd;
}
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, int def_flags,
const char **argv, const char *location)
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, const char **argv)
{
bstr args[MP_CMD_MAX_ARGS];
int num = 0;
for (; argv[num]; num++) {
if (num >= MP_CMD_MAX_ARGS) {
mp_err(log, "%s: too many arguments.\n", location);
mpv_node items[MP_CMD_MAX_ARGS];
mpv_node_list list = {.values = items};
mpv_node node = {.format = MPV_FORMAT_NODE_ARRAY, .u = {.list = &list}};
while (argv[list.num]) {
if (list.num >= MP_CMD_MAX_ARGS) {
mp_err(log, "Too many arguments to command.\n");
return NULL;
}
args[num] = bstr0(argv[num]);
char *s = (char *)argv[list.num];
items[list.num++] = (mpv_node){.format = MPV_FORMAT_STRING,
.u = {.string = s}};
}
return mp_input_parse_cmd_bstrv(log, def_flags, num, args, location);
}
struct mp_cmd *mp_input_parse_cmd_bstrv(struct mp_log *log, int def_flags,
int argc, bstr *argv,
const char *location)
{
struct parse_ctx ctx = {
.log = log,
.loc = location,
.tmp = talloc_new(NULL),
.array_input = true,
.strs = argv,
.num_strs = argc,
};
struct mp_cmd *res = parse_cmd(&ctx, def_flags);
talloc_free(ctx.tmp);
return res;
return mp_input_parse_cmd_node(log, &node);
}
void mp_cmd_free(mp_cmd_t *cmd)

View File

@ -18,6 +18,8 @@
#ifndef MP_PARSE_COMMAND_H
#define MP_PARSE_COMMAND_H
#include "misc/bstr.h"
struct mp_log;
struct mp_cmd;
struct mpv_node;
@ -27,16 +29,11 @@ struct mpv_node;
struct mp_cmd *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc);
// Similar to mp_input_parse_cmd(), but takes a list of strings instead.
// Also, def_flags contains initial command flags (see mp_cmd_flags; the default
// as used by mp_input_parse_cmd is MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES).
// Also, MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES are not set by default.
// Keep in mind that these functions (naturally) don't take multiple commands,
// i.e. a ";" argument does not start a new command.
// The _strv version is limitted to MP_CMD_MAX_ARGS argv array items.
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, int def_flags,
const char **argv, const char *location);
struct mp_cmd *mp_input_parse_cmd_bstrv(struct mp_log *log, int def_flags,
int argc, bstr *argv,
const char *location);
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, const char **argv);
struct mp_cmd *mp_input_parse_cmd_node(struct mp_log *log, struct mpv_node *node);
// After getting a command from mp_input_get_cmd you need to free it using this

View File

@ -29,22 +29,24 @@ void mp_event_drop_files(struct input_ctx *ictx, int num_files, char **files)
if (all_sub) {
for (int i = 0; i < num_files; i++) {
const char *cmd[] = {
"osd-auto",
"sub_add",
files[i],
NULL
};
mp_input_run_cmd(ictx, MP_ON_OSD_AUTO, cmd, "<drop-subtitle>");
mp_input_run_cmd(ictx, cmd);
}
} else {
for (int i = 0; i < num_files; i++) {
const char *cmd[] = {
"osd-auto",
"loadfile",
files[i],
/* Start playing the dropped files right away */
(i == 0) ? "replace" : "append",
NULL
};
mp_input_run_cmd(ictx, MP_ON_OSD_AUTO, cmd, "<drop-files>");
mp_input_run_cmd(ictx, cmd);
}
}
}

View File

@ -308,7 +308,7 @@ static mp_cmd_t *handle_test(struct input_ctx *ictx, int code)
"CLOSE_WIN was received. This pseudo key can be remapped too,\n"
"but --input-test will always quit when receiving it.\n");
const char *args[] = {"quit", NULL};
mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, 0, args, "");
mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, args);
return res;
}
@ -336,7 +336,7 @@ static mp_cmd_t *handle_test(struct input_ctx *ictx, int code)
MP_INFO(ictx, "%s\n", msg);
const char *args[] = {"show_text", msg, NULL};
mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, MP_ON_OSD_MSG, args, "");
mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, args);
talloc_free(msg);
return res;
}
@ -1355,11 +1355,9 @@ struct mp_cmd *mp_input_parse_cmd(struct input_ctx *ictx, bstr str,
return mp_input_parse_cmd_(ictx->log, str, location);
}
void mp_input_run_cmd(struct input_ctx *ictx, int def_flags, const char **cmd,
const char *location)
void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd)
{
mp_cmd_t *cmdt = mp_input_parse_cmd_strv(ictx->log, def_flags, cmd, location);
mp_input_queue_cmd(ictx, cmdt);
mp_input_queue_cmd(ictx, mp_input_parse_cmd_strv(ictx->log, cmd));
}
struct mp_input_src_internal {

View File

@ -247,8 +247,7 @@ void mp_input_set_cancel(struct input_ctx *ictx, struct mp_cancel *cancel);
bool mp_input_use_alt_gr(struct input_ctx *ictx);
// Like mp_input_parse_cmd_strv, but also run the command.
void mp_input_run_cmd(struct input_ctx *ictx, int def_flags, const char **cmd,
const char *location);
void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd);
void mp_input_pipe_add(struct input_ctx *ictx, const char *filename);
void mp_input_joystick_add(struct input_ctx *ictx, char *dev);

View File

@ -877,8 +877,7 @@ static int run_client_command(mpv_handle *ctx, struct mp_cmd *cmd)
int mpv_command(mpv_handle *ctx, const char **args)
{
return run_client_command(ctx, mp_input_parse_cmd_strv(ctx->log, 0, args,
ctx->name));
return run_client_command(ctx, mp_input_parse_cmd_strv(ctx->log, args));
}
int mpv_command_string(mpv_handle *ctx, const char *args)
@ -892,7 +891,7 @@ int mpv_command_async(mpv_handle *ctx, uint64_t ud, const char **args)
if (!ctx->mpctx->initialized)
return MPV_ERROR_UNINITIALIZED;
struct mp_cmd *cmd = mp_input_parse_cmd_strv(ctx->log, 0, args, "<client>");
struct mp_cmd *cmd = mp_input_parse_cmd_strv(ctx->log, args);
if (!cmd)
return MPV_ERROR_INVALID_PARAMETER;