1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Command argument parsers set result argument

This commit is contained in:
James Booth 2014-04-09 21:31:43 +01:00
parent 6275644de4
commit c3e3759256
5 changed files with 237 additions and 176 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);

View File

@ -10,325 +10,381 @@ void
parse_null_returns_null(void **state)
{
char *inp = NULL;
gchar **result = parse_args(inp, 1, 2);
gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_empty_returns_null(void **state)
{
char *inp = "";
gchar **result = parse_args(inp, 1, 2);
gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_space_returns_null(void **state)
{
char *inp = " ";
gchar **result = parse_args(inp, 1, 2);
gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_cmd_no_args_returns_null(void **state)
{
char *inp = "/cmd";
gchar **result = parse_args(inp, 1, 2);
gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_cmd_with_space_returns_null(void **state)
{
char *inp = "/cmd ";
gchar **result = parse_args(inp, 1, 2);
gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_cmd_with_too_few_returns_null(void **state)
{
char *inp = "/cmd arg1";
gchar **result = parse_args(inp, 2, 3);
gboolean result = TRUE;
gchar **args = parse_args(inp, 2, 3, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_cmd_with_too_many_returns_null(void **state)
{
char *inp = "/cmd arg1 arg2 arg3 arg4";
gchar **result = parse_args(inp, 1, 3);
gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 3, &result);
assert_null(result);
g_strfreev(result);
assert_false(result);
assert_null(args);
g_strfreev(args);
}
void
parse_cmd_one_arg(void **state)
{
char *inp = "/cmd arg1";
gchar **result = parse_args(inp, 1, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_int_equal(1, g_strv_length(result));
assert_string_equal("arg1", result[0]);
g_strfreev(result);
assert_true(result);
assert_int_equal(1, g_strv_length(args));
assert_string_equal("arg1", args[0]);
g_strfreev(args);
}
void
parse_cmd_two_args(void **state)
{
char *inp = "/cmd arg1 arg2";
gchar **result = parse_args(inp, 1, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("arg2", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("arg2", args[1]);
g_strfreev(args);
}
void
parse_cmd_three_args(void **state)
{
char *inp = "/cmd arg1 arg2 arg3";
gchar **result = parse_args(inp, 3, 3);
gboolean result = FALSE;
gchar **args = parse_args(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("arg2", result[1]);
assert_string_equal("arg3", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("arg2", args[1]);
assert_string_equal("arg3", args[2]);
g_strfreev(args);
}
void
parse_cmd_three_args_with_spaces(void **state)
{
char *inp = " /cmd arg1 arg2 arg3 ";
gchar **result = parse_args(inp, 3, 3);
gboolean result = FALSE;
gchar **args = parse_args(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("arg2", result[1]);
assert_string_equal("arg3", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("arg2", args[1]);
assert_string_equal("arg3", args[2]);
g_strfreev(args);
}
void
parse_cmd_with_freetext(void **state)
{
char *inp = "/cmd this is some free text";
gchar **result = parse_args_with_freetext(inp, 1, 1);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 1, &result);
assert_int_equal(1, g_strv_length(result));
assert_string_equal("this is some free text", result[0]);
g_strfreev(result);
assert_true(result);
assert_int_equal(1, g_strv_length(args));
assert_string_equal("this is some free text", args[0]);
g_strfreev(args);
}
void
parse_cmd_one_arg_with_freetext(void **state)
{
char *inp = "/cmd arg1 this is some free text";
gchar **result = parse_args_with_freetext(inp, 1, 2);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("this is some free text", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("this is some free text", args[1]);
g_strfreev(args);
}
void
parse_cmd_two_args_with_freetext(void **state)
{
char *inp = "/cmd arg1 arg2 this is some free text";
gchar **result = parse_args_with_freetext(inp, 1, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("arg2", result[1]);
assert_string_equal("this is some free text", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("arg2", args[1]);
assert_string_equal("this is some free text", args[2]);
g_strfreev(args);
}
void
parse_cmd_min_zero(void **state)
{
char *inp = "/cmd";
gchar **result = parse_args(inp, 0, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 0, 2, &result);
assert_int_equal(0, g_strv_length(result));
assert_null(result[0]);
g_strfreev(result);
assert_true(result);
assert_int_equal(0, g_strv_length(args));
assert_null(args[0]);
g_strfreev(args);
}
void
parse_cmd_min_zero_with_freetext(void **state)
{
char *inp = "/cmd";
gchar **result = parse_args_with_freetext(inp, 0, 2);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 2, &result);
assert_int_equal(0, g_strv_length(result));
assert_null(result[0]);
g_strfreev(result);
assert_true(result);
assert_int_equal(0, g_strv_length(args));
assert_null(args[0]);
g_strfreev(args);
}
void
parse_cmd_with_quoted(void **state)
{
char *inp = "/cmd \"arg1\" arg2";
gchar **result = parse_args(inp, 2, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("arg2", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("arg2", args[1]);
g_strfreev(args);
}
void
parse_cmd_with_quoted_and_space(void **state)
{
char *inp = "/cmd \"the arg1\" arg2";
gchar **result = parse_args(inp, 2, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("the arg1", result[0]);
assert_string_equal("arg2", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("the arg1", args[0]);
assert_string_equal("arg2", args[1]);
g_strfreev(args);
}
void
parse_cmd_with_quoted_and_many_spaces(void **state)
{
char *inp = "/cmd \"the arg1 is here\" arg2";
gchar **result = parse_args(inp, 2, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("the arg1 is here", result[0]);
assert_string_equal("arg2", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("the arg1 is here", args[0]);
assert_string_equal("arg2", args[1]);
g_strfreev(args);
}
void
parse_cmd_with_many_quoted_and_many_spaces(void **state)
{
char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\"";
gchar **result = parse_args(inp, 2, 2);
gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("the arg1 is here", result[0]);
assert_string_equal("and arg2 is right here", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("the arg1 is here", args[0]);
assert_string_equal("and arg2 is right here", args[1]);
g_strfreev(args);
}
void
parse_cmd_freetext_with_quoted(void **state)
{
char *inp = "/cmd \"arg1\" arg2 hello there whats up";
gchar **result = parse_args_with_freetext(inp, 3, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("arg2", result[1]);
assert_string_equal("hello there whats up", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("arg2", args[1]);
assert_string_equal("hello there whats up", args[2]);
g_strfreev(args);
}
void
parse_cmd_freetext_with_quoted_and_space(void **state)
{
char *inp = "/cmd \"the arg1\" arg2 another bit of freetext";
gchar **result = parse_args_with_freetext(inp, 3, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("the arg1", result[0]);
assert_string_equal("arg2", result[1]);
assert_string_equal("another bit of freetext", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("the arg1", args[0]);
assert_string_equal("arg2", args[1]);
assert_string_equal("another bit of freetext", args[2]);
g_strfreev(args);
}
void
parse_cmd_freetext_with_quoted_and_many_spaces(void **state)
{
char *inp = "/cmd \"the arg1 is here\" arg2 some more freetext";
gchar **result = parse_args_with_freetext(inp, 3, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("the arg1 is here", result[0]);
assert_string_equal("arg2", result[1]);
assert_string_equal("some more freetext", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("the arg1 is here", args[0]);
assert_string_equal("arg2", args[1]);
assert_string_equal("some more freetext", args[2]);
g_strfreev(args);
}
void
parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state)
{
char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text";
gchar **result = parse_args_with_freetext(inp, 3, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("the arg1 is here", result[0]);
assert_string_equal("and arg2 is right here", result[1]);
assert_string_equal("and heres the free text", result[2]);
g_strfreev(result);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("the arg1 is here", args[0]);
assert_string_equal("and arg2 is right here", args[1]);
assert_string_equal("and heres the free text", args[2]);
g_strfreev(args);
}
void
parse_cmd_with_quoted_freetext(void **state)
{
char *inp = "/cmd arg1 here is \"some\" quoted freetext";
gchar **result = parse_args_with_freetext(inp, 1, 2);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
assert_int_equal(2, g_strv_length(result));
assert_string_equal("arg1", result[0]);
assert_string_equal("here is \"some\" quoted freetext", result[1]);
g_strfreev(result);
assert_true(result);
assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg1", args[0]);
assert_string_equal("here is \"some\" quoted freetext", args[1]);
g_strfreev(args);
}
void
parse_cmd_with_third_arg_quoted_0_min_3_max(void **state)
{
char *inp = "/group add friends \"The User\"";
gchar **result = parse_args_with_freetext(inp, 0, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("add", result[0]);
assert_string_equal("friends", result[1]);
assert_string_equal("The User", result[2]);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("add", args[0]);
assert_string_equal("friends", args[1]);
assert_string_equal("The User", args[2]);
}
void
parse_cmd_with_second_arg_quoted_0_min_3_max(void **state)
{
char *inp = "/group add \"The Group\" friend";
gchar **result = parse_args_with_freetext(inp, 0, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("add", result[0]);
assert_string_equal("The Group", result[1]);
assert_string_equal("friend", result[2]);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("add", args[0]);
assert_string_equal("The Group", args[1]);
assert_string_equal("friend", args[2]);
}
void
parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state)
{
char *inp = "/group add \"The Group\" \"The User\"";
gchar **result = parse_args_with_freetext(inp, 0, 3);
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 3, &result);
assert_int_equal(3, g_strv_length(result));
assert_string_equal("add", result[0]);
assert_string_equal("The Group", result[1]);
assert_string_equal("The User", result[2]);
assert_true(result);
assert_int_equal(3, g_strv_length(args));
assert_string_equal("add", args[0]);
assert_string_equal("The Group", args[1]);
assert_string_equal("The User", args[2]);
}
void
@ -446,4 +502,4 @@ get_first_two_of_three_first_and_second_quoted(void **state)
char *result = get_start(inp, 3);
assert_string_equal("\"one\" \"two\" ", result);
}
}