diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2019-08-07 20:44:34 +0100 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2019-08-07 20:44:34 +0100 |
commit | c0f1d73df3fef83cb6a60825c08620de0ded39d5 (patch) | |
tree | e469bf39d935ac82f5645b7449612ac5c0814886 /src/commands.c | |
parent | e889f417c7edd3d47a54b9464e1b6b72c3f7ae1c (diff) | |
download | imv-c0f1d73df3fef83cb6a60825c08620de0ded39d5.tar.gz |
Make aliases smarter
Diffstat (limited to 'src/commands.c')
-rw-r--r-- | src/commands.c | 37 |
1 files changed, 36 insertions, 1 deletions
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 <assert.h> +#include <stdio.h> +#include <string.h> + 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; } |