aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AUTHORS1
-rw-r--r--src/commands.c46
-rw-r--r--src/commands.h3
-rw-r--r--src/imv.c13
4 files changed, 32 insertions, 31 deletions
diff --git a/AUTHORS b/AUTHORS
index 99eb671..b88acf6 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -6,6 +6,7 @@ People who have contributed to imv:
* Dmitrij D. Czarkoff
* Jose Diez
* Kenneth Hanley
+ * Julian Heinzel
* Hannes Körber
* Aleksandra Kosiacka
* Michal Koutenský
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);
}
}
}