From 88d6dc2ef7ebee4b04b079455727034a0f594aae Mon Sep 17 00:00:00 2001 From: Jeinzi Date: Sat, 1 Sep 2018 00:34:50 +0200 Subject: Added support for multiple commands per bind. --- src/commands.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'src/commands.c') diff --git a/src/commands.c b/src/commands.c index 7dfbc2c..2bc33fe 100644 --- a/src/commands.c +++ b/src/commands.c @@ -46,29 +46,35 @@ void imv_command_alias(struct imv_commands *cmds, const char *command, const cha list_append(cmds->command_list, cmd); } -int imv_command_exec(struct imv_commands *cmds, const char *command, void *data) +int imv_command_exec(struct imv_commands *cmds, struct list *commands, void *data) { - struct list *args = list_from_string(command, ' '); int ret = 1; + for(size_t i = 0; i < commands->len; ++i) { + const char *command = commands->items[i]; + struct list *args = list_from_string(command, ' '); - if(args->len > 0) { - for(size_t i = 0; i < cmds->command_list->len; ++i) { - struct command *cmd = cmds->command_list->items[i]; - if(!strcmp(cmd->command, args->items[0])) { - if(cmd->handler) { - /* argstr = all args as a single string */ - const char *argstr = command + strlen(cmd->command) + 1; - cmd->handler(args, argstr, data); - ret = 0; - } else if(cmd->alias) { - ret = imv_command_exec(cmds, cmd->alias, data); + if(args->len > 0) { + for(size_t i = 0; i < cmds->command_list->len; ++i) { + struct command *cmd = cmds->command_list->items[i]; + if(!strcmp(cmd->command, args->items[0])) { + if(cmd->handler) { + /* argstr = all args as a single string */ + const char *argstr = command + strlen(cmd->command) + 1; + cmd->handler(args, argstr, data); + ret = 0; + } else if(cmd->alias) { + struct list *commands = list_create(); + list_append(commands, cmd->alias); + ret = imv_command_exec(cmds, commands, data); + list_free(commands); + } + break; } - break; } } + list_deep_free(args); } - list_deep_free(args); return ret; } -- cgit v1.2.3