From 51837d93572fe229275d239c7a8d4113ef81ea38 Mon Sep 17 00:00:00 2001 From: Jeinzi Date: Thu, 27 Sep 2018 00:53:17 +0200 Subject: Implemented imv_command_exec_list(). --- src/commands.c | 46 +++++++++++++++++++++++++--------------------- src/commands.h | 3 ++- src/imv.c | 13 ++++--------- 3 files changed, 31 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/commands.c b/src/commands.c index a989a7c..0fb8b46 100644 --- a/src/commands.c +++ b/src/commands.c @@ -46,35 +46,39 @@ 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, struct list *commands, void *data) +int imv_command_exec(struct imv_commands *cmds, const char *command, 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 j = 0; j < cmds->command_list->len; ++j) { - struct command *cmd = cmds->command_list->items[j]; - 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 *command_list = list_create(); - list_append(command_list, cmd->alias); - ret = imv_command_exec(cmds, command_list, data); - list_free(command_list); - } - break; + 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); } + break; } } - list_deep_free(args); } + list_deep_free(args); + return ret; +} + +int imv_command_exec_list(struct imv_commands *cmds, struct list *commands, void *data) +{ + int ret = 0; + for(size_t i = 0; i < commands->len; ++i) { + const char *command = commands->items[i]; + ret += imv_command_exec(cmds, command, data); + } return ret; } diff --git a/src/commands.h b/src/commands.h index 256018e..cf81f68 100644 --- a/src/commands.h +++ b/src/commands.h @@ -11,7 +11,8 @@ struct imv_commands *imv_commands_create(void); void imv_commands_free(struct imv_commands *cmds); void imv_command_register(struct imv_commands *cmds, const char *command, void (*handler)(struct list*, const char*, void*)); void imv_command_alias(struct imv_commands *cmds, const char *command, const char *alias); -int imv_command_exec(struct imv_commands *cmds, struct list *commands, void *data); +int imv_command_exec(struct imv_commands *cmds, const char *command, void *data); +int imv_command_exec_list(struct imv_commands *cmds, struct list *commands, void *data); #endif diff --git a/src/imv.c b/src/imv.c index 674283f..fd239bb 100644 --- a/src/imv.c +++ b/src/imv.c @@ -765,14 +765,9 @@ static void handle_event(struct imv *imv, SDL_Event *event) } switch(event->type) { - case SDL_QUIT: { - /* new scope needed in order to declare variables in a switch statement */ - struct list *commands = list_create(); - list_append(commands, "quit"); - imv_command_exec(imv->commands, commands, imv); - list_free(commands); + case SDL_QUIT: + imv_command_exec(imv->commands, "quit", imv); break; - } case SDL_TEXTINPUT: strncat(imv->input_buffer, event->text.text, command_buffer_len - 1); imv->need_redraw = true; @@ -791,7 +786,7 @@ static void handle_event(struct imv *imv, SDL_Event *event) } else if(event->key.keysym.sym == SDLK_RETURN) { struct list *commands = list_create(); list_append(commands, imv->input_buffer); - imv_command_exec(imv->commands, commands, imv); + imv_command_exec_list(imv->commands, commands, imv); SDL_StopTextInput(); list_free(commands); free(imv->input_buffer); @@ -820,7 +815,7 @@ static void handle_event(struct imv *imv, SDL_Event *event) default: { /* braces to allow const char *cmd definition */ struct list *cmds = imv_bind_handle_event(imv->binds, event); if(cmds) { - imv_command_exec(imv->commands, cmds, imv); + imv_command_exec_list(imv->commands, cmds, imv); } } } -- cgit v1.2.3