From c9f45ea93e470159b1bd4c4ddbd5c2c05149d76e Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 10 Oct 2014 22:58:28 +0200 Subject: input: use mpv_node parser for char** command parsers Minor simplification, also drops some useless stuff. --- input/cmd_parse.c | 70 +++++++++++++++++++++---------------------------------- input/cmd_parse.h | 13 ++++------- input/event.c | 6 +++-- input/input.c | 10 ++++---- input/input.h | 3 +-- player/client.c | 5 ++-- 6 files changed, 43 insertions(+), 64 deletions(-) diff --git a/input/cmd_parse.c b/input/cmd_parse.c index b9fd494914..b723737606 100644 --- a/input/cmd_parse.c +++ b/input/cmd_parse.c @@ -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) diff --git a/input/cmd_parse.h b/input/cmd_parse.h index 3c08f0b4ba..b5b0c3e0f4 100644 --- a/input/cmd_parse.h +++ b/input/cmd_parse.h @@ -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 diff --git a/input/event.c b/input/event.c index 661a68ecf3..add77f0b0c 100644 --- a/input/event.c +++ b/input/event.c @@ -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, ""); + 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, ""); + mp_input_run_cmd(ictx, cmd); } } } diff --git a/input/input.c b/input/input.c index 439a0cf4fa..613d1ec3b0 100644 --- a/input/input.c +++ b/input/input.c @@ -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 { diff --git a/input/input.h b/input/input.h index 02257d9c85..40c9369271 100644 --- a/input/input.h +++ b/input/input.h @@ -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); diff --git a/player/client.c b/player/client.c index c0b73977d3..d1bdec001e 100644 --- a/player/client.c +++ b/player/client.c @@ -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, ""); + struct mp_cmd *cmd = mp_input_parse_cmd_strv(ctx->log, args); if (!cmd) return MPV_ERROR_INVALID_PARAMETER; -- cgit v1.2.3