diff options
author | wm4 <wm4@nowhere> | 2014-02-23 17:43:38 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-02-23 17:43:38 +0100 |
commit | f5c781b0d5755cb503a0531a159cd3664b32c620 (patch) | |
tree | f5716f73d3afef35536355ef159f135985a1cda2 | |
parent | b184c7d1e7e665730b5f1397661da02a6db1afc5 (diff) |
command: remove special casing for strings in input commands
Until now, strings were the only allowed dynamically allocated argument
type in input commands. Extend it so that it works for any type. (The
string expansion in command.c is of course still string specific.)
-rw-r--r-- | input/cmd_parse.c | 18 | ||||
-rw-r--r-- | player/command.c | 8 |
2 files changed, 17 insertions, 9 deletions
diff --git a/input/cmd_parse.c b/input/cmd_parse.c index 2369ff5a8e..e2296ce215 100644 --- a/input/cmd_parse.c +++ b/input/cmd_parse.c @@ -27,6 +27,13 @@ #include "cmd_list.h" #include "input.h" +static void destroy_cmd(void *ptr) +{ + struct mp_cmd *cmd = ptr; + for (int n = 0; n < cmd->nargs; n++) + m_option_free(cmd->args[n].type, &cmd->args[n].v); +} + static bool read_token(bstr str, bstr *out_rest, bstr *out_token) { bstr t = bstr_lstrip(str); @@ -165,6 +172,7 @@ static struct mp_cmd *parse_cmd(struct parse_ctx *ctx, int def_flags) } cmd = talloc_ptrtype(NULL, cmd); + talloc_set_destructor(cmd, destroy_cmd); *cmd = (struct mp_cmd) { .name = (char *)cmd_def->name, .id = cmd_def->id, @@ -194,7 +202,7 @@ static struct mp_cmd *parse_cmd(struct parse_ctx *ctx, int def_flags) if (opt->defval) { struct mp_cmd_arg *cmdarg = &cmd->args[cmd->nargs]; cmdarg->type = opt; - memcpy(&cmdarg->v, opt->defval, opt->type->size); + m_option_copy(opt, &cmdarg->v, opt->defval); cmd->nargs++; continue; } @@ -212,8 +220,6 @@ static struct mp_cmd *parse_cmd(struct parse_ctx *ctx, int def_flags) cmd->name, i + 1, m_option_strerror(r)); goto error; } - if (opt->type == &m_option_type_string) - talloc_steal(cmd, cmdarg->v.s); } bstr left = pctx_get_trailing(ctx); @@ -271,6 +277,7 @@ mp_cmd_t *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc) // own purposes, a pseudo-command is used to wrap the command list. if (!p_prev) { struct mp_cmd *list = talloc_ptrtype(NULL, list); + talloc_set_destructor(list, destroy_cmd); *list = (struct mp_cmd) { .id = MP_CMD_COMMAND_LIST, .name = "list", @@ -340,10 +347,11 @@ mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd) return NULL; ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t)); + talloc_set_destructor(ret, destroy_cmd); ret->name = talloc_strdup(ret, cmd->name); for (i = 0; i < ret->nargs; i++) { - if (cmd->args[i].type->type == &m_option_type_string) - ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s); + memset(&ret->args[i].v, 0, ret->args[i].type->type->size); + m_option_copy(ret->args[i].type, &ret->args[i].v, &cmd->args[i].v); } if (cmd->id == MP_CMD_COMMAND_LIST) { diff --git a/player/command.c b/player/command.c index d27c3f4418..2a40132a70 100644 --- a/player/command.c +++ b/player/command.c @@ -2710,11 +2710,11 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd) if (cmd->flags & MP_EXPAND_PROPERTIES) { for (int n = 0; n < cmd->nargs; n++) { if (cmd->args[n].type->type == CONF_TYPE_STRING) { - cmd->args[n].v.s = - mp_property_expand_string(mpctx, cmd->args[n].v.s); - if (!cmd->args[n].v.s) + char *s = mp_property_expand_string(mpctx, cmd->args[n].v.s); + if (!s) return; - talloc_steal(cmd, cmd->args[n].v.s); + talloc_free(cmd->args[n].v.s); + cmd->args[n].v.s = s; } } } |