From c0f1d73df3fef83cb6a60825c08620de0ded39d5 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Wed, 7 Aug 2019 20:44:34 +0100 Subject: Make aliases smarter --- src/commands.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/commands.c') diff --git a/src/commands.c b/src/commands.c index ca40244..3808929 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1,12 +1,41 @@ #include "commands.h" #include "list.h" +#include +#include +#include + struct command { char* command; void (*handler)(struct list *args, const char *argstr, void *data); char* alias; }; +static char *join_str_list(struct list *list, const char *sep, size_t start) +{ + size_t len = 0; + size_t cap = 512; + char *buf = malloc(cap); + buf[0] = 0; + + size_t sep_len = strlen(sep); + for (size_t i = start; i < list->len; ++i) { + size_t item_len = strlen(list->items[i]); + if (len + item_len + sep_len >= cap) { + cap *= 2; + buf = realloc(buf, cap); + assert(buf); + } + + strncat(buf, list->items[i], cap); + len += item_len; + + strncat(buf, sep, cap); + len += sep_len; + } + return buf; +} + struct imv_commands *imv_commands_create(void) { struct imv_commands *cmds = malloc(sizeof *cmds); @@ -61,7 +90,13 @@ int imv_command_exec(struct imv_commands *cmds, const char *command, void *data) cmd->handler(args, argstr, data); ret = 0; } else if(cmd->alias) { - ret = imv_command_exec(cmds, cmd->alias, data); + char *new_args = join_str_list(args, " ", 1); + size_t cmd_len = strlen(cmd->alias) + 1 + strlen(new_args) + 1; + char *new_cmd = malloc(cmd_len); + snprintf(new_cmd, cmd_len, "%s %s", cmd->alias, new_args); + ret = imv_command_exec(cmds, new_cmd, data); + free(new_args); + free(new_cmd); } break; } -- cgit v1.2.3