mirror of
https://github.com/mpv-player/mpv
synced 2025-05-05 09:46:01 +00:00
input: use mpv_node parser for char** command parsers
Minor simplification, also drops some useless stuff.
This commit is contained in:
parent
63903c27bd
commit
c9f45ea93e
@ -261,11 +261,23 @@ error:
|
|||||||
return NULL;
|
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);
|
struct mp_cmd *cmd = talloc_ptrtype(NULL, cmd);
|
||||||
talloc_set_destructor(cmd, destroy_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) {
|
if (!ctx->array_input) {
|
||||||
ctx->str = bstr_lstrip(ctx->str);
|
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));
|
cmd->original = bstrdup(cmd, bstr_strip(orig));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*str = ctx->str;
|
||||||
return cmd;
|
return cmd;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
MP_ERR(ctx, "Command was defined at %s.\n", ctx->loc);
|
MP_ERR(ctx, "Command was defined at %s.\n", ctx->loc);
|
||||||
talloc_free(cmd);
|
talloc_free(cmd);
|
||||||
|
*str = ctx->str;
|
||||||
return NULL;
|
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)
|
mp_cmd_t *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc)
|
||||||
{
|
{
|
||||||
void *tmp = talloc_new(NULL);
|
void *tmp = talloc_new(NULL);
|
||||||
@ -402,36 +401,21 @@ done:
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, int def_flags,
|
struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, const char **argv)
|
||||||
const char **argv, const char *location)
|
|
||||||
{
|
{
|
||||||
bstr args[MP_CMD_MAX_ARGS];
|
mpv_node items[MP_CMD_MAX_ARGS];
|
||||||
int num = 0;
|
mpv_node_list list = {.values = items};
|
||||||
for (; argv[num]; num++) {
|
mpv_node node = {.format = MPV_FORMAT_NODE_ARRAY, .u = {.list = &list}};
|
||||||
if (num >= MP_CMD_MAX_ARGS) {
|
while (argv[list.num]) {
|
||||||
mp_err(log, "%s: too many arguments.\n", location);
|
if (list.num >= MP_CMD_MAX_ARGS) {
|
||||||
|
mp_err(log, "Too many arguments to command.\n");
|
||||||
return NULL;
|
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);
|
return mp_input_parse_cmd_node(log, &node);
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_cmd_free(mp_cmd_t *cmd)
|
void mp_cmd_free(mp_cmd_t *cmd)
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
#ifndef MP_PARSE_COMMAND_H
|
#ifndef MP_PARSE_COMMAND_H
|
||||||
#define MP_PARSE_COMMAND_H
|
#define MP_PARSE_COMMAND_H
|
||||||
|
|
||||||
|
#include "misc/bstr.h"
|
||||||
|
|
||||||
struct mp_log;
|
struct mp_log;
|
||||||
struct mp_cmd;
|
struct mp_cmd;
|
||||||
struct mpv_node;
|
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);
|
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.
|
// 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
|
// Also, MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES are not set by default.
|
||||||
// as used by mp_input_parse_cmd is MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES).
|
|
||||||
// Keep in mind that these functions (naturally) don't take multiple commands,
|
// Keep in mind that these functions (naturally) don't take multiple commands,
|
||||||
// i.e. a ";" argument does not start a new command.
|
// 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, const char **argv);
|
||||||
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_node(struct mp_log *log, struct mpv_node *node);
|
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
|
// After getting a command from mp_input_get_cmd you need to free it using this
|
||||||
|
@ -29,22 +29,24 @@ void mp_event_drop_files(struct input_ctx *ictx, int num_files, char **files)
|
|||||||
if (all_sub) {
|
if (all_sub) {
|
||||||
for (int i = 0; i < num_files; i++) {
|
for (int i = 0; i < num_files; i++) {
|
||||||
const char *cmd[] = {
|
const char *cmd[] = {
|
||||||
|
"osd-auto",
|
||||||
"sub_add",
|
"sub_add",
|
||||||
files[i],
|
files[i],
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
mp_input_run_cmd(ictx, MP_ON_OSD_AUTO, cmd, "<drop-subtitle>");
|
mp_input_run_cmd(ictx, cmd);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < num_files; i++) {
|
for (int i = 0; i < num_files; i++) {
|
||||||
const char *cmd[] = {
|
const char *cmd[] = {
|
||||||
|
"osd-auto",
|
||||||
"loadfile",
|
"loadfile",
|
||||||
files[i],
|
files[i],
|
||||||
/* Start playing the dropped files right away */
|
/* Start playing the dropped files right away */
|
||||||
(i == 0) ? "replace" : "append",
|
(i == 0) ? "replace" : "append",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
mp_input_run_cmd(ictx, MP_ON_OSD_AUTO, cmd, "<drop-files>");
|
mp_input_run_cmd(ictx, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"
|
"CLOSE_WIN was received. This pseudo key can be remapped too,\n"
|
||||||
"but --input-test will always quit when receiving it.\n");
|
"but --input-test will always quit when receiving it.\n");
|
||||||
const char *args[] = {"quit", NULL};
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,7 +336,7 @@ static mp_cmd_t *handle_test(struct input_ctx *ictx, int code)
|
|||||||
|
|
||||||
MP_INFO(ictx, "%s\n", msg);
|
MP_INFO(ictx, "%s\n", msg);
|
||||||
const char *args[] = {"show_text", msg, NULL};
|
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);
|
talloc_free(msg);
|
||||||
return res;
|
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);
|
return mp_input_parse_cmd_(ictx->log, str, location);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_input_run_cmd(struct input_ctx *ictx, int def_flags, const char **cmd,
|
void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd)
|
||||||
const char *location)
|
|
||||||
{
|
{
|
||||||
mp_cmd_t *cmdt = mp_input_parse_cmd_strv(ictx->log, def_flags, cmd, location);
|
mp_input_queue_cmd(ictx, mp_input_parse_cmd_strv(ictx->log, cmd));
|
||||||
mp_input_queue_cmd(ictx, cmdt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mp_input_src_internal {
|
struct mp_input_src_internal {
|
||||||
|
@ -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);
|
bool mp_input_use_alt_gr(struct input_ctx *ictx);
|
||||||
|
|
||||||
// Like mp_input_parse_cmd_strv, but also run the command.
|
// 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,
|
void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd);
|
||||||
const char *location);
|
|
||||||
|
|
||||||
void mp_input_pipe_add(struct input_ctx *ictx, const char *filename);
|
void mp_input_pipe_add(struct input_ctx *ictx, const char *filename);
|
||||||
void mp_input_joystick_add(struct input_ctx *ictx, char *dev);
|
void mp_input_joystick_add(struct input_ctx *ictx, char *dev);
|
||||||
|
@ -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)
|
int mpv_command(mpv_handle *ctx, const char **args)
|
||||||
{
|
{
|
||||||
return run_client_command(ctx, mp_input_parse_cmd_strv(ctx->log, 0, args,
|
return run_client_command(ctx, mp_input_parse_cmd_strv(ctx->log, args));
|
||||||
ctx->name));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int mpv_command_string(mpv_handle *ctx, const char *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)
|
if (!ctx->mpctx->initialized)
|
||||||
return MPV_ERROR_UNINITIALIZED;
|
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)
|
if (!cmd)
|
||||||
return MPV_ERROR_INVALID_PARAMETER;
|
return MPV_ERROR_INVALID_PARAMETER;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user