mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-21 18:06:21 +00:00
Added parser module
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include <glib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
96
src/parser.c
Normal file
96
src/parser.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* parser.c
|
||||
*
|
||||
* Copyright (C) 2012 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>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
30
src/parser.h
Normal file
30
src/parser.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* parser.h
|
||||
*
|
||||
* Copyright (C) 2012 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, int *num);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user