about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-08-15 00:42:38 +0100
committerJames Booth <boothj5@gmail.com>2012-08-15 00:42:38 +0100
commit4e18d65977d24288e0e22dc15ed98b84051a031e (patch)
tree8ace1ef46d61d808f9cc7febf7c160c4b7d20eb7 /src
parent254866e8b92eae682cf7d28514505f2ddc2296ba (diff)
downloadprofani-tty-4e18d65977d24288e0e22dc15ed98b84051a031e.tar.gz
Refactored command lookup
Diffstat (limited to 'src')
-rw-r--r--src/command.c55
1 files changed, 26 insertions, 29 deletions
diff --git a/src/command.c b/src/command.c
index 358fa85a..f8bd2a32 100644
--- a/src/command.c
+++ b/src/command.c
@@ -48,6 +48,7 @@ struct cmd_t {
 
 
 // helpers
+static struct cmd_t * _cmd_get_command(const char * const command);
 static gboolean _handle_command(const char * const command, 
     const char * const inp);
 static void _update_presence(const jabber_presence_t presence, 
@@ -414,32 +415,45 @@ cmd_get_help_list_status(void)
     return result;
 }
 
-static gboolean
-_handle_command(const char * const command, const char * const inp)
+static struct cmd_t *
+_cmd_get_command(const char * const command)
 {
     unsigned int i;
     for (i = 0; i < ARRAY_SIZE(main_commands); i++) {
         struct cmd_t *pcmd = main_commands+i;
         if (strcmp(pcmd->cmd, command) == 0) {
-            return (pcmd->func(inp, pcmd->help));
+            return pcmd;
         }
     }
 
     for (i = 0; i < ARRAY_SIZE(setting_commands); i++) {
         struct cmd_t *pcmd = setting_commands+i;
         if (strcmp(pcmd->cmd, command) == 0) {
-            return (pcmd->func(inp, pcmd->help));
+            return pcmd;
         }
     }
 
     for (i = 0; i < ARRAY_SIZE(status_commands); i++) {
         struct cmd_t *pcmd = status_commands+i;
         if (strcmp(pcmd->cmd, command) == 0) {
-            return (pcmd->func(inp, pcmd->help));
+            return pcmd;
         }
     }
 
-    return _cmd_default(inp);
+    return NULL;
+}
+
+
+static gboolean
+_handle_command(const char * const command, const char * const inp)
+{
+    struct cmd_t *cmd = _cmd_get_command(command);
+    
+    if (cmd != NULL) {
+        return (cmd->func(inp, cmd->help));
+    } else {
+        return _cmd_default(inp);
+    }
 }
 
 static gboolean
@@ -492,38 +506,21 @@ _cmd_help(const char * const inp, struct cmd_help_t help)
     if (strcmp(inp, "/help") == 0) {
         cons_help();
     } else {
-        char *cmd;
-        cmd = strndup(inp+6, strlen(inp)-6);
+        char *cmd = strndup(inp+6, strlen(inp)-6);
         char cmd_with_slash[1 + strlen(cmd) + 1];
         sprintf(cmd_with_slash, "/%s", cmd);
 
         const gchar **help_text = NULL;
+        struct cmd_t *command = _cmd_get_command(cmd_with_slash);
 
-        unsigned int i;
-        for (i = 0; i < ARRAY_SIZE(main_commands); i++) {
-            struct cmd_t *pcmd = main_commands+i;
-            if (strcmp(pcmd->cmd, cmd_with_slash) == 0) {
-                help_text = pcmd->help.long_help;
-            }
-        }
-
-        for (i = 0; i < ARRAY_SIZE(setting_commands); i++) {
-            struct cmd_t *pcmd = setting_commands+i;
-            if (strcmp(pcmd->cmd, cmd_with_slash) == 0) {
-                help_text = pcmd->help.long_help;
-            }
-        }
-
-        for (i = 0; i < ARRAY_SIZE(status_commands); i++) {
-            struct cmd_t *pcmd = status_commands+i;
-            if (strcmp(pcmd->cmd, cmd_with_slash) == 0) {
-                help_text = pcmd->help.long_help;
-            }
+        if (command != NULL) {
+            help_text = command->help.long_help;
         }
-
+        
         cons_show("");
 
         if (help_text != NULL) {
+            int i;
             for (i = 0; help_text[i] != NULL; i++) {
                 cons_show(help_text[i]);
             }