From 824eaa1678043afd8e31e4de529aaf61165295d7 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 18 Nov 2012 00:07:00 +0000 Subject: Added parser module --- src/command.c | 44 +++++-------------------- src/contact_list.c | 2 -- src/parser.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.h | 30 +++++++++++++++++ 4 files changed, 135 insertions(+), 37 deletions(-) create mode 100644 src/parser.c create mode 100644 src/parser.h (limited to 'src') diff --git a/src/command.c b/src/command.c index a02c77e9..160c8091 100644 --- a/src/command.c +++ b/src/command.c @@ -36,6 +36,7 @@ #include "history.h" #include "jabber.h" #include "log.h" +#include "parser.h" #include "preferences.h" #include "prof_autocomplete.h" #include "tinyurl.h" @@ -835,7 +836,7 @@ _cmd_connect(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 1, 1, &num_args); + gchar **args = parse_args(inp, 1, 1, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -882,7 +883,7 @@ _cmd_sub(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 1, 2, &num_args); + gchar **args = parse_args(inp, 1, 2, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1220,7 +1221,7 @@ _cmd_info(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 1, 1, &num_args); + gchar **args = parse_args(inp, 1, 1, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1252,7 +1253,7 @@ _cmd_join(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 1, 2, &num_args); + gchar **args = parse_args(inp, 1, 2, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1296,7 +1297,7 @@ _cmd_tiny(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 1, 1, &num_args); + gchar **args = parse_args(inp, 1, 1, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1401,7 +1402,7 @@ _cmd_set_notify(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 2, 2, &num_args); + gchar **args = parse_args(inp, 2, 2, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1463,7 +1464,7 @@ _cmd_set_log(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 2, 2, &num_args); + gchar **args = parse_args(inp, 2, 2, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1496,7 +1497,7 @@ _cmd_set_priority(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; int num_args = 0; - gchar **args = _cmd_parse_args(inp, 1, 1, &num_args); + gchar **args = parse_args(inp, 1, 1, &num_args); if (args == NULL) { cons_show("Usage: %s", help.usage); @@ -1805,30 +1806,3 @@ _strtoi(char *str, int *saveptr, int min, int max) return 0; } - -gchar ** -_cmd_parse_args(const char * const inp, int min, int max, int *num) -{ - char *copy = strdup(inp); - gchar **args = NULL; - g_strstrip(copy); - gchar **split = g_strsplit(copy, " ", 0); - *num = g_strv_length(split) - 1; - - if ((*num < min) || (*num > max)) { - g_strfreev(split); - free(copy); - return NULL; - } else { - args = malloc((*num + 1) * sizeof(*args)); - int i; - for (i = 0; i < *num; i++) { - args[i] = strdup(split[i+1]); - } - args[i] = NULL; - g_strfreev(split); - free(copy); - - return args; - } -} diff --git a/src/contact_list.c b/src/contact_list.c index d05d7412..714750dd 100644 --- a/src/contact_list.c +++ b/src/contact_list.c @@ -25,7 +25,6 @@ #include #include "contact.h" -#include "log.h" #include "prof_autocomplete.h" static PAutocomplete ac; @@ -80,7 +79,6 @@ contact_list_update_contact(const char * const jid, const char * const presence, PContact contact = g_hash_table_lookup(contacts, jid); if (contact == NULL) { - log_warning("Contact not in list: %s", jid); return FALSE; } diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 00000000..5034c215 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,96 @@ +/* + * parser.c + * + * Copyright (C) 2012 James Booth + * + * 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 . + * + */ + +#include +#include + +#include + +gchar ** +parse_args(const char * const inp, int min, int max, int *num) +{ + if (inp == NULL) { + *num = 0; + 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; + char *token_start = ©[0]; + int token_size = 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; + token_start = ©[i]; + token_size++; + } + } else { + if (copy[i] == ' ' || copy[i] == '\0') { + tokens = g_slist_append(tokens, g_strndup(token_start, + token_size)); + token_size = 0; + in_token = FALSE; + } else { + token_size++; + } + } + } + + *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); + *num = 0; + return NULL; + + // 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; + } +} diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 00000000..a0442f33 --- /dev/null +++ b/src/parser.h @@ -0,0 +1,30 @@ +/* + * parser.h + * + * Copyright (C) 2012 James Booth + * + * 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 . + * + */ + +#ifndef PARSER_H +#define PARSER_H + +#include + +gchar** parse_args(const char * const inp, int min, int max, int *num); + +#endif -- cgit 1.4.1-2-gfad0