1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-07-21 18:24:14 -04:00

Commands now use parser function to handle parameters

This commit is contained in:
James Booth 2012-11-18 02:40:49 +00:00
parent 0cec188eb5
commit 78dd79f6a0
4 changed files with 591 additions and 690 deletions

File diff suppressed because it is too large Load Diff

View File

@ -25,11 +25,32 @@
#include <glib.h>
/*
* Take a full line of input and return an array of strings representing
* the arguments of a command.
* If the number of arguments found is less than min, or more than max
* NULL is returned.
*
* inp - The line of input
* min - The minimum allowed number of arguments
* max - The maxmimum allowed number of arguments
*
* Returns - An NULL terminated array of strings representing the aguments
* of the command, or NULL if the validation fails.
*
* E.g. the following input line:
*
* /cmd arg1 arg2
*
* Will return a pointer to the following array:
*
* { "arg1", "arg2", NULL }
*
*/
gchar **
parse_args(const char * const inp, int min, int max, int *num)
parse_args(const char * const inp, int min, int max)
{
if (inp == NULL) {
*num = 0;
return NULL;
}
@ -66,24 +87,23 @@ parse_args(const char * const inp, int min, int max, int *num)
}
}
*num = g_slist_length(tokens) - 1;
int num = g_slist_length(tokens) - 1;
// if num args not valid return NULL
if ((*num < min) || (*num > max)) {
if ((num < min) || (num > max)) {
g_slist_free_full(tokens, free);
free(copy);
*num = 0;
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));
} else if (min == 0 && num == 0) {
gchar **args = malloc((num + 1) * sizeof(*args));
args[0] = NULL;
return args;
// otherwise return args array
} else {
gchar **args = malloc((*num + 1) * sizeof(*args));
gchar **args = malloc((num + 1) * sizeof(*args));
GSList *token = tokens;
token = g_slist_next(token);
int arg_count = 0;
@ -101,11 +121,36 @@ parse_args(const char * const inp, int min, int max, int *num)
}
}
/*
* Take a full line of input and return an array of strings representing
* the arguments of a command. This function handles when the last parameter
* to the command is free text e.g.
*
* /msg user@host here is a message
*
* If the number of arguments found is less than min, or more than max
* NULL is returned.
*
* inp - The line of input
* min - The minimum allowed number of arguments
* max - The maxmimum allowed number of arguments
*
* Returns - An NULL terminated array of strings representing the aguments
* of the command, or NULL if the validation fails.
*
* E.g. the following input line:
*
* /cmd arg1 arg2 some free text
*
* Will return a pointer to the following array:
*
* { "arg1", "arg2", "some free text", NULL }
*
*/
gchar **
parse_args_with_freetext(const char * const inp, int min, int max, int *num)
parse_args_with_freetext(const char * const inp, int min, int max)
{
if (inp == NULL) {
*num = 0;
return NULL;
}
@ -148,24 +193,23 @@ parse_args_with_freetext(const char * const inp, int min, int max, int *num)
}
}
*num = g_slist_length(tokens) - 1;
int num = g_slist_length(tokens) - 1;
// if num args not valid return NULL
if ((*num < min) || (*num > max)) {
if ((num < min) || (num > max)) {
g_slist_free_full(tokens, free);
free(copy);
*num = 0;
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));
} else if (min == 0 && num == 0) {
gchar **args = malloc((num + 1) * sizeof(*args));
args[0] = NULL;
return args;
// otherwise return args array
} else {
gchar **args = malloc((*num + 1) * sizeof(*args));
gchar **args = malloc((num + 1) * sizeof(*args));
GSList *token = tokens;
token = g_slist_next(token);
int arg_count = 0;

View File

@ -25,7 +25,7 @@
#include <glib.h>
gchar** parse_args(const char * const inp, int min, int max, int *num);
gchar** parse_args_with_freetext(const char * const inp, int min, int max, int *num);
gchar** parse_args(const char * const inp, int min, int max);
gchar** parse_args_with_freetext(const char * const inp, int min, int max);
#endif

View File

@ -7,8 +7,7 @@ void
parse_null_returns_null(void)
{
char *inp = NULL;
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_is_null(result);
g_strfreev(result);
@ -18,8 +17,7 @@ void
parse_empty_returns_null(void)
{
char *inp = "";
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_is_null(result);
g_strfreev(result);
@ -29,8 +27,7 @@ void
parse_space_returns_null(void)
{
char *inp = " ";
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_is_null(result);
g_strfreev(result);
@ -40,8 +37,7 @@ void
parse_cmd_no_args_returns_null(void)
{
char *inp = "/cmd";
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_is_null(result);
g_strfreev(result);
@ -51,8 +47,7 @@ void
parse_cmd_with_space_returns_null(void)
{
char *inp = "/cmd ";
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_is_null(result);
g_strfreev(result);
@ -62,8 +57,7 @@ void
parse_cmd_with_too_few_returns_null(void)
{
char *inp = "/cmd arg1";
int num = 0;
gchar **result = parse_args(inp, 2, 3, &num);
gchar **result = parse_args(inp, 2, 3);
assert_is_null(result);
g_strfreev(result);
@ -73,8 +67,7 @@ void
parse_cmd_with_too_many_returns_null(void)
{
char *inp = "/cmd arg1 arg2 arg3 arg4";
int num = 0;
gchar **result = parse_args(inp, 1, 3, &num);
gchar **result = parse_args(inp, 1, 3);
assert_is_null(result);
g_strfreev(result);
@ -84,10 +77,9 @@ void
parse_cmd_one_arg(void)
{
char *inp = "/cmd arg1";
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_int_equals(1, num);
assert_int_equals(1, g_strv_length(result));
assert_string_equals("arg1", result[0]);
g_strfreev(result);
}
@ -96,10 +88,9 @@ void
parse_cmd_two_args(void)
{
char *inp = "/cmd arg1 arg2";
int num = 0;
gchar **result = parse_args(inp, 1, 2, &num);
gchar **result = parse_args(inp, 1, 2);
assert_int_equals(2, num);
assert_int_equals(2, g_strv_length(result));
assert_string_equals("arg1", result[0]);
assert_string_equals("arg2", result[1]);
g_strfreev(result);
@ -109,10 +100,9 @@ void
parse_cmd_three_args(void)
{
char *inp = "/cmd arg1 arg2 arg3";
int num = 0;
gchar **result = parse_args(inp, 3, 3, &num);
gchar **result = parse_args(inp, 3, 3);
assert_int_equals(3, num);
assert_int_equals(3, g_strv_length(result));
assert_string_equals("arg1", result[0]);
assert_string_equals("arg2", result[1]);
assert_string_equals("arg3", result[2]);
@ -123,10 +113,9 @@ void
parse_cmd_three_args_with_spaces(void)
{
char *inp = " /cmd arg1 arg2 arg3 ";
int num = 0;
gchar **result = parse_args(inp, 3, 3, &num);
gchar **result = parse_args(inp, 3, 3);
assert_int_equals(3, num);
assert_int_equals(3, g_strv_length(result));
assert_string_equals("arg1", result[0]);
assert_string_equals("arg2", result[1]);
assert_string_equals("arg3", result[2]);
@ -137,10 +126,9 @@ void
parse_cmd_with_freetext(void)
{
char *inp = "/cmd this is some free text";
int num = 0;
gchar **result = parse_args_with_freetext(inp, 1, 1, &num);
gchar **result = parse_args_with_freetext(inp, 1, 1);
assert_int_equals(1, num);
assert_int_equals(1, g_strv_length(result));
assert_string_equals("this is some free text", result[0]);
g_strfreev(result);
}
@ -149,10 +137,9 @@ void
parse_cmd_one_arg_with_freetext(void)
{
char *inp = "/cmd arg1 this is some free text";
int num = 0;
gchar **result = parse_args_with_freetext(inp, 1, 2, &num);
gchar **result = parse_args_with_freetext(inp, 1, 2);
assert_int_equals(2, num);
assert_int_equals(2, g_strv_length(result));
assert_string_equals("arg1", result[0]);
assert_string_equals("this is some free text", result[1]);
g_strfreev(result);
@ -162,10 +149,9 @@ void
parse_cmd_two_args_with_freetext(void)
{
char *inp = "/cmd arg1 arg2 this is some free text";
int num = 0;
gchar **result = parse_args_with_freetext(inp, 1, 3, &num);
gchar **result = parse_args_with_freetext(inp, 1, 3);
assert_int_equals(3, num);
assert_int_equals(3, g_strv_length(result));
assert_string_equals("arg1", result[0]);
assert_string_equals("arg2", result[1]);
assert_string_equals("this is some free text", result[2]);
@ -176,10 +162,9 @@ void
parse_cmd_min_zero(void)
{
char *inp = "/cmd";
int num = 0;
gchar **result = parse_args(inp, 0, 2, &num);
gchar **result = parse_args(inp, 0, 2);
assert_int_equals(0, num);
assert_int_equals(0, g_strv_length(result));
assert_is_null(result[0]);
g_strfreev(result);
}
@ -188,10 +173,9 @@ void
parse_cmd_min_zero_with_freetext(void)
{
char *inp = "/cmd";
int num = 0;
gchar **result = parse_args_with_freetext(inp, 0, 2, &num);
gchar **result = parse_args_with_freetext(inp, 0, 2);
assert_int_equals(0, num);
assert_int_equals(0, g_strv_length(result));
assert_is_null(result[0]);
g_strfreev(result);
}