about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-09 21:31:43 +0100
committerJames Booth <boothj5@gmail.com>2014-04-09 21:31:43 +0100
commitc3e3759256bc9b8f53f1334c3a352877d28846e1 (patch)
treec6946ed61f0f7678ae7c5df758c7f201394b7b6f /src
parent6275644de4a4177889950f852c8307002f629d47 (diff)
downloadprofani-tty-c3e3759256bc9b8f53f1334c3a352877d28846e1.tar.gz
Command argument parsers set result argument
Diffstat (limited to 'src')
-rw-r--r--src/command/command.c77
-rw-r--r--src/command/commands.h2
-rw-r--r--src/tools/parser.c14
-rw-r--r--src/tools/parser.h4
4 files changed, 51 insertions, 46 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 5e833ace..bea347e7 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1247,15 +1247,16 @@ gboolean
 cmd_execute(const char * const command, const char * const inp)
 {
     Command *cmd = g_hash_table_lookup(commands, command);
+    gboolean result = FALSE;
 
     if (cmd != NULL) {
-        gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args);
-        if ((args == NULL) && (cmd->setting_func != NULL)) {
+        gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result);
+        if ((result == FALSE) && (cmd->setting_func != NULL)) {
             cons_show("");
             (*cmd->setting_func)();
             cons_show("Usage: %s", cmd->help.usage);
             return TRUE;
-        } else if (args == NULL) {
+        } else if (result == FALSE) {
             cons_show("");
             cons_show("Usage: %s", cmd->help.usage);
             if (ui_current_win_type() == WIN_CHAT) {
@@ -1808,12 +1809,13 @@ _alias_autocomplete(char *input, int *size)
 static char *
 _connect_autocomplete(char *input, int *size)
 {
-    char *result = NULL;
+    char *found = NULL;
+    gboolean result = FALSE;
 
     input[*size] = '\0';
-    gchar **args = parse_args(input, 2, 4);
+    gchar **args = parse_args(input, 2, 4, &result);
 
-    if ((strncmp(input, "/connect", 8) == 0) && (args != NULL)) {
+    if ((strncmp(input, "/connect", 8) == 0) && (result == TRUE)) {
         GString *beginning = g_string_new("/connect ");
         g_string_append(beginning, args[0]);
         if (args[1] != NULL && args[2] != NULL) {
@@ -1822,16 +1824,16 @@ _connect_autocomplete(char *input, int *size)
             g_string_append(beginning, " ");
             g_string_append(beginning, args[2]);
         }
-        result = autocomplete_param_with_ac(input, size, beginning->str, connect_property_ac);
+        found = autocomplete_param_with_ac(input, size, beginning->str, connect_property_ac);
         g_string_free(beginning, TRUE);
-        if (result != NULL) {
-            return result;
+        if (found != NULL) {
+            return found;
         }
     }
 
-    result = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled);
-    if (result != NULL) {
-        return result;
+    found = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled);
+    if (found != NULL) {
+        return found;
     }
 
     return NULL;
@@ -1840,12 +1842,13 @@ _connect_autocomplete(char *input, int *size)
 static char *
 _join_autocomplete(char *input, int *size)
 {
-    char *result = NULL;
+    char *found = NULL;
+    gboolean result = FALSE;
 
     input[*size] = '\0';
-    gchar **args = parse_args(input, 2, 4);
+    gchar **args = parse_args(input, 2, 4, &result);
 
-    if ((strncmp(input, "/join", 5) == 0) && (args != NULL)) {
+    if ((strncmp(input, "/join", 5) == 0) && (result == TRUE)) {
         GString *beginning = g_string_new("/join ");
         g_string_append(beginning, args[0]);
         if (args[1] != NULL && args[2] != NULL) {
@@ -1854,10 +1857,10 @@ _join_autocomplete(char *input, int *size)
             g_string_append(beginning, " ");
             g_string_append(beginning, args[2]);
         }
-        result = autocomplete_param_with_ac(input, size, beginning->str, join_property_ac);
+        found = autocomplete_param_with_ac(input, size, beginning->str, join_property_ac);
         g_string_free(beginning, TRUE);
-        if (result != NULL) {
-            return result;
+        if (found != NULL) {
+            return found;
         }
     }
 
@@ -1867,28 +1870,29 @@ _join_autocomplete(char *input, int *size)
 static char *
 _account_autocomplete(char *input, int *size)
 {
-    char *result = NULL;
+    char *found = NULL;
+    gboolean result = FALSE;
 
     input[*size] = '\0';
-    gchar **args = parse_args(input, 3, 3);
+    gchar **args = parse_args(input, 3, 3, &result);
 
-    if ((strncmp(input, "/account set", 12) == 0) && (args != NULL)) {
+    if ((strncmp(input, "/account set", 12) == 0) && (result == TRUE)) {
         GString *beginning = g_string_new("/account set ");
         g_string_append(beginning, args[1]);
-        result = autocomplete_param_with_ac(input, size, beginning->str, account_set_ac);
+        found = autocomplete_param_with_ac(input, size, beginning->str, account_set_ac);
         g_string_free(beginning, TRUE);
-        if (result != NULL) {
-            return result;
+        if (found != NULL) {
+            return found;
         }
     }
 
-    if ((strncmp(input, "/account clear", 14) == 0) && (args != NULL)) {
+    if ((strncmp(input, "/account clear", 14) == 0) && (result == TRUE)) {
         GString *beginning = g_string_new("/account clear ");
         g_string_append(beginning, args[1]);
-        result = autocomplete_param_with_ac(input, size, beginning->str, account_clear_ac);
+        found = autocomplete_param_with_ac(input, size, beginning->str, account_clear_ac);
         g_string_free(beginning, TRUE);
-        if (result != NULL) {
-            return result;
+        if (found != NULL) {
+            return found;
         }
     }
 
@@ -1899,18 +1903,13 @@ _account_autocomplete(char *input, int *size)
         "/account disable", "/account rename", "/account clear" };
 
     for (i = 0; i < ARRAY_SIZE(account_choice); i++) {
-        result = autocomplete_param_with_func(input, size, account_choice[i],
+        found = autocomplete_param_with_func(input, size, account_choice[i],
             accounts_find_all);
-        if (result != NULL) {
-            return result;
+        if (found != NULL) {
+            return found;
         }
     }
 
-    result = autocomplete_param_with_ac(input, size, "/account", account_ac);
-    if (result != NULL) {
-        return result;
-    }
-
-    return NULL;
-}
-
+    found = autocomplete_param_with_ac(input, size, "/account", account_ac);
+    return found;
+}
\ No newline at end of file
diff --git a/src/command/commands.h b/src/command/commands.h
index e95986c1..fa1afbff 100644
--- a/src/command/commands.h
+++ b/src/command/commands.h
@@ -43,7 +43,7 @@ typedef struct cmd_help_t {
 typedef struct cmd_t {
     gchar *cmd;
     gboolean (*func)(gchar **args, struct cmd_help_t help);
-    gchar** (*parser)(const char * const inp, int min, int max);
+    gchar** (*parser)(const char * const inp, int min, int max, gboolean *result);
     int min_args;
     int max_args;
     void (**setting_func)(void);
diff --git a/src/tools/parser.c b/src/tools/parser.c
index 4e746557..932fd9aa 100644
--- a/src/tools/parser.c
+++ b/src/tools/parser.c
@@ -48,9 +48,10 @@
  *
  */
 gchar **
-parse_args(const char * const inp, int min, int max)
+parse_args(const char * const inp, int min, int max, gboolean *result)
 {
     if (inp == NULL) {
+        *result = FALSE;
         return NULL;
     }
 
@@ -122,6 +123,7 @@ parse_args(const char * const inp, int min, int max)
     if ((num < min) || (num > max)) {
         g_slist_free_full(tokens, free);
         g_free(copy);
+        *result = FALSE;
         return NULL;
 
     // if min allowed is 0 and 0 found, return empty char* array
@@ -130,6 +132,7 @@ parse_args(const char * const inp, int min, int max)
         gchar **args = malloc((num + 1) * sizeof(*args));
         args[0] = NULL;
         g_free(copy);
+        *result = TRUE;
         return args;
 
     // otherwise return args array
@@ -147,7 +150,7 @@ parse_args(const char * const inp, int min, int max)
         args[arg_count] = NULL;
         g_slist_free_full(tokens, free);
         g_free(copy);
-
+        *result = TRUE;
         return args;
     }
 }
@@ -179,9 +182,10 @@ parse_args(const char * const inp, int min, int max)
  *
  */
 gchar **
-parse_args_with_freetext(const char * const inp, int min, int max)
+parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result)
 {
     if (inp == NULL) {
+        *result = FALSE;
         return NULL;
     }
 
@@ -267,12 +271,14 @@ parse_args_with_freetext(const char * const inp, int min, int max)
     // if num args not valid return NULL
     if ((num < min) || (num > max)) {
         g_slist_free_full(tokens, free);
+        *result = FALSE;
         return NULL;
 
     // if min allowed is 0 and 0 found, return empty char* array
     } else if (min == 0 && num == 0) {
         gchar **args = malloc((num + 1) * sizeof(*args));
         args[0] = NULL;
+        *result = TRUE;
         return args;
 
     // otherwise return args array
@@ -289,7 +295,7 @@ parse_args_with_freetext(const char * const inp, int min, int max)
 
         args[arg_count] = NULL;
         g_slist_free_full(tokens, free);
-
+        *result = TRUE;
         return args;
     }
 }
diff --git a/src/tools/parser.h b/src/tools/parser.h
index c82199d4..f03fca81 100644
--- a/src/tools/parser.h
+++ b/src/tools/parser.h
@@ -25,8 +25,8 @@
 
 #include <glib.h>
 
-gchar** parse_args(const char * const inp, int min, int max);
-gchar** parse_args_with_freetext(const char * const inp, int min, int max);
+gchar** parse_args(const char * const inp, int min, int max, gboolean *result);
+gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result);
 int count_tokens(char *string);
 char* get_start(char *string, int tokens);