diff options
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/autocomplete.c | 81 | ||||
-rw-r--r-- | src/tools/parser.c | 352 | ||||
-rw-r--r-- | src/tools/parser.h | 33 |
3 files changed, 389 insertions, 77 deletions
diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index 849aaf1d..4b25edd6 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -24,7 +24,8 @@ #include <stdlib.h> #include <string.h> -#include "autocomplete.h" +#include "tools/autocomplete.h" +#include "tools/parser.h" struct autocomplete_t { GSList *items; @@ -260,80 +261,6 @@ autocomplete_param_with_ac(char *input, int *size, char *command, return auto_msg; } -int -_count_tokens(char *string) -{ - int num_tokens = 0; - - // if no quotes, use glib - if (g_strrstr(string, "\"") == NULL) { - gchar **tokens = g_strsplit(string, " ", 0); - num_tokens = g_strv_length(tokens); - g_strfreev(tokens); - - // else count tokens including quoted - } else { - int length = strlen(string); - int i = 0; - gboolean in_quotes = FALSE; - - // include first token - num_tokens++; - - for (i = 0; i < length; i++) { - if (string[i] == ' ') { - if (!in_quotes) { - num_tokens++; - } - } else if (string[i] == '"') { - if (in_quotes) { - in_quotes = FALSE; - } else { - in_quotes = TRUE; - } - } - } - } - - return num_tokens; -} - -char * -_get_start(char *string, int tokens) -{ - char *result_str = NULL; - int num_tokens = 0; - int length = strlen(string); - int i = 0; - gboolean in_quotes = FALSE; - GString *result = g_string_new(""); - - // include first token - num_tokens++; - - for (i = 0; i < length; i++) { - if (num_tokens < tokens) { - g_string_append_c(result, string[i]); - } - if (string[i] == ' ') { - if (!in_quotes) { - num_tokens++; - } - } else if (string[i] == '"') { - if (in_quotes) { - in_quotes = FALSE; - } else { - in_quotes = TRUE; - } - } - } - - result_str = result->str; - g_string_free(result, FALSE); - - return result_str; -} - char * autocomplete_param_no_with_func(char *input, int *size, char *command, int arg_number, autocomplete_func func) @@ -353,11 +280,11 @@ autocomplete_param_no_with_func(char *input, int *size, char *command, g_strstrip(inp_cpy); // count tokens properly - int num_tokens = _count_tokens(inp_cpy); + int num_tokens = count_tokens(inp_cpy); // if correct number of tokens, then candidate for autocompletion of last param if (num_tokens == arg_number) { - gchar *start_str = _get_start(inp_cpy, arg_number); + gchar *start_str = get_start(inp_cpy, arg_number); gchar *comp_str = g_strdup(&inp_cpy[strlen(start_str)]); // autocomplete param diff --git a/src/tools/parser.c b/src/tools/parser.c new file mode 100644 index 00000000..c8335879 --- /dev/null +++ b/src/tools/parser.c @@ -0,0 +1,352 @@ +/* + * parser.c + * + * Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com> + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <stdlib.h> +#include <string.h> + +#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) +{ + if (inp == NULL) { + return NULL; + } + + // copy and strip input of leading/trailing whitepsace + char *copy = strdup(inp); + g_strstrip(copy); + + int inp_size = g_utf8_strlen(copy, -1); + gboolean in_token = FALSE; + gboolean in_quotes = FALSE; + char *token_start = ©[0]; + int token_size = 0; + GSList *tokens = NULL; + + // add tokens to GSList + int i; + for (i = 0; i < inp_size; i++) { + gchar *curr_ch = g_utf8_offset_to_pointer(copy, i); + gunichar curr_uni = g_utf8_get_char(curr_ch); + if (!in_token) { + if (curr_uni == ' ') { + continue; + } else { + in_token = TRUE; + if (curr_uni == '"') { + in_quotes = TRUE; + i++; + } + token_start = curr_ch; + token_size += g_unichar_to_utf8(curr_uni, NULL); + } + } else { + if (in_quotes) { + if (curr_uni == '"') { + tokens = g_slist_append(tokens, g_strndup(token_start, + token_size)); + token_size = 0; + in_token = FALSE; + in_quotes = FALSE; + } else { + token_size += g_unichar_to_utf8(curr_uni, NULL); + } + } else { + if (curr_uni == ' ') { + tokens = g_slist_append(tokens, g_strndup(token_start, + token_size)); + token_size = 0; + in_token = FALSE; + } else { + token_size += g_unichar_to_utf8(curr_uni, NULL); + token_size++; + } + } + } + } + + tokens = g_slist_append(tokens, g_strndup(token_start, token_size)); + + int num = g_slist_length(tokens) - 1; + + // if num args not valid return NULL + if ((num < min) || (num > max)) { + g_slist_free_full(tokens, free); + g_free(copy); + return NULL; + + // if min allowed is 0 and 0 found, return empty char* array + } else if (min == 0 && num == 0) { + g_slist_free_full(tokens, free); + gchar **args = malloc((num + 1) * sizeof(*args)); + args[0] = NULL; + g_free(copy); + return args; + + // otherwise return args array + } else { + gchar **args = malloc((num + 1) * sizeof(*args)); + GSList *token = tokens; + token = g_slist_next(token); + int arg_count = 0; + + while (token != NULL) { + args[arg_count++] = strdup(token->data); + token = g_slist_next(token); + } + + args[arg_count] = NULL; + g_slist_free_full(tokens, free); + g_free(copy); + + return args; + } +} + +/* + * 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) +{ + if (inp == NULL) { + return NULL; + } + + // copy and strip input of leading/trailing whitepsace + char *copy = strdup(inp); + g_strstrip(copy); + + int inp_size = strlen(copy); + gboolean in_token = FALSE; + gboolean in_freetext = FALSE; + gboolean in_quotes = FALSE; + char *token_start = ©[0]; + int token_size = 0; + int num_tokens = 0; + GSList *tokens = NULL; + + // add tokens to GSList + int i; + for (i = 0; i <= inp_size; i++) { + if (!in_token) { + if (copy[i] == ' ') { + continue; + } else { + in_token = TRUE; + num_tokens++; + if (num_tokens == max + 1) { + in_freetext = TRUE; + } else if (copy[i] == '"') { + in_quotes = TRUE; + i++; + } + if (copy[i] == '"') { + token_start = ©[i+1]; + } else { + token_start = ©[i]; + token_size++; + } + } + } else { + if (in_quotes) { + if ((copy[i] == '\0') || (copy[i] == '"')) { + tokens = g_slist_append(tokens, g_strndup(token_start, + token_size)); + token_size = 0; + in_token = FALSE; + in_quotes = FALSE; + } else { + if (copy[i] != '"') { + token_size++; + } + } + } else { + if ((!in_freetext && copy[i] == ' ') || copy[i] == '\0') { + tokens = g_slist_append(tokens, g_strndup(token_start, + token_size)); + token_size = 0; + in_token = FALSE; + } else { + if (copy[i] != '"') { + token_size++; + } + } + } + } + } + + int num = g_slist_length(tokens) - 1; + + // if num args not valid return NULL + if ((num < min) || (num > max)) { + g_slist_free_full(tokens, free); + free(copy); + 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; + return args; + + // otherwise return args array + } else { + gchar **args = malloc((num + 1) * sizeof(*args)); + GSList *token = tokens; + token = g_slist_next(token); + int arg_count = 0; + + while (token != NULL) { + args[arg_count++] = strdup(token->data); + token = g_slist_next(token); + } + + args[arg_count] = NULL; + g_slist_free_full(tokens, free); + free(copy); + + return args; + } +} + +int +count_tokens(char *string) +{ + int num_tokens = 0; + + // if no quotes, use glib + if (g_strrstr(string, "\"") == NULL) { + gchar **tokens = g_strsplit(string, " ", 0); + num_tokens = g_strv_length(tokens); + g_strfreev(tokens); + + // else count tokens including quoted + } else { + int length = strlen(string); + int i = 0; + gboolean in_quotes = FALSE; + + // include first token + num_tokens++; + + for (i = 0; i < length; i++) { + if (string[i] == ' ') { + if (!in_quotes) { + num_tokens++; + } + } else if (string[i] == '"') { + if (in_quotes) { + in_quotes = FALSE; + } else { + in_quotes = TRUE; + } + } + } + } + + return num_tokens; +} + +char * +get_start(char *string, int tokens) +{ + char *result_str = NULL; + int num_tokens = 0; + int length = strlen(string); + int i = 0; + gboolean in_quotes = FALSE; + GString *result = g_string_new(""); + + // include first token + num_tokens++; + + for (i = 0; i < length; i++) { + if (num_tokens < tokens) { + g_string_append_c(result, string[i]); + } + if (string[i] == ' ') { + if (!in_quotes) { + num_tokens++; + } + } else if (string[i] == '"') { + if (in_quotes) { + in_quotes = FALSE; + } else { + in_quotes = TRUE; + } + } + } + + result_str = result->str; + g_string_free(result, FALSE); + + return result_str; +} + + diff --git a/src/tools/parser.h b/src/tools/parser.h new file mode 100644 index 00000000..6f00cc90 --- /dev/null +++ b/src/tools/parser.h @@ -0,0 +1,33 @@ +/* + * parser.h + * + * Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com> + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifndef PARSER_H +#define PARSER_H + +#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); +int count_tokens(char *string); +char* get_start(char *string, int tokens); + +#endif |