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.)
This commit is contained in:
wm4 2014-02-23 17:43:38 +01:00
parent b184c7d1e7
commit f5c781b0d5
2 changed files with 17 additions and 9 deletions

View File

@ -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) {

View File

@ -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;
}
}
}