From 7b7cd1b7cc8d5dd86c9241f948a0df7ab3f76cdf Mon Sep 17 00:00:00 2001 From: Alex Denes Date: Sat, 6 Mar 2021 22:23:55 +0000 Subject: [PATCH] Add argc for message allocator and add tag list manager --- CMakeLists.txt | 3 ++ include/memory.h | 5 +++- src/memory/allocators.c | 16 +++++----- src/memory/listmgrs.c | 31 ++++++++++++++++++++ tests/CMakeLists.txt | 9 ++++++ tests/allocs.c | 65 +++++++++++++++++++++++++++++++++++++++++ tests/manassm.c | 62 +++++++++++++++++++++++++++++++++++++++ tests/optcrlf.c | 44 ++++++++++++++++++++++++++++ 8 files changed, 225 insertions(+), 10 deletions(-) create mode 100644 tests/allocs.c create mode 100644 tests/manassm.c create mode 100644 tests/optcrlf.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c9291a..9369600 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,9 @@ if (BUILD_TESTS) enable_testing() add_test(NAME "FullLoop" COMMAND "fullloop") add_test(NAME "EarlyTrail" COMMAND "earlytrail") + add_test(NAME "OptCRLF" COMMAND "optcrlf") + add_test(NAME "ManualAssemblers" COMMAND "manassm") + add_test(NAME "Allocators" COMMAND "allocs") endif() add_library(uirc ${UIRC_SOURCE}) diff --git a/include/memory.h b/include/memory.h index f164417..f563004 100644 --- a/include/memory.h +++ b/include/memory.h @@ -28,16 +28,19 @@ IRC_Tag* uirc_malloc_tag(const char* key, const char* value); IRC_Capability* uirc_malloc_capability(const char* name); #endif /* UIRC_FEATURE_IRCV3 */ IRC_User* uirc_malloc_user(const char* nick, const char* user, const char* real, const char* host); -IRC_Message* uirc_malloc_message(const char* command, ...); +IRC_Message* uirc_malloc_message(const char* command, int argc, ...); IRC_Buffer* uirc_malloc_buffer(const char* name, const char* topic, const char* key); IRC_Network* uirc_malloc_network(const char* addr, const char* svc, const char* pass); bool uirc_list_add_buffer(IRC_Network* net, IRC_Buffer* buf); bool uirc_list_add_user(IRC_Buffer* buf, IRC_User* usr); bool uirc_list_add_message(IRC_Buffer* buf, IRC_Message* msg); +bool uirc_list_add_tag(IRC_Message* msg, IRC_Tag* tag); + bool uirc_list_remove_buffer(IRC_Network* net, IRC_Buffer* buf); bool uirc_list_remove_user(IRC_Buffer* buf, IRC_User* usr); bool uirc_list_remove_message(IRC_Buffer* buf, IRC_Message* msg); +bool uirc_list_remove_tag(IRC_Message* msg, IRC_Tag* tag); #ifdef UIRC_FEATURE_IRCV3 void uirc_free_tag(IRC_Tag* t); diff --git a/src/memory/allocators.c b/src/memory/allocators.c index e9882bf..1e34cf2 100644 --- a/src/memory/allocators.c +++ b/src/memory/allocators.c @@ -99,7 +99,7 @@ uirc_malloc_user(const char* nick, const char* user, const char* real, const cha } IRC_Message* -uirc_malloc_message(const char* command, ...) +uirc_malloc_message(const char* command, int argc, ...) { IRC_Message* ret; assert(command != NULL); @@ -113,14 +113,12 @@ uirc_malloc_message(const char* command, ...) return NULL; va_list ap; - va_start(ap, command); - for (unsigned short i = 0; i < IRC_MAXARGS; i++) { - const char* carg = NULL; - if ((carg = va_arg(ap, const char*)) != NULL) { - if ((ret->args[i] = malloc_string(carg, strlen(carg))) == NULL) { - uirc_free_message(ret); - ret = NULL; - } + va_start(ap, argc); + for (unsigned short i = 0; i < IRC_MAXARGS && i < argc; i++) { + const char* carg = va_arg(ap, const char*); + if ((ret->args[i] = malloc_string(carg, strlen(carg))) == NULL) { + uirc_free_message(ret); + ret = NULL; } } va_end(ap); diff --git a/src/memory/listmgrs.c b/src/memory/listmgrs.c index 72d6e12..bcf9646 100644 --- a/src/memory/listmgrs.c +++ b/src/memory/listmgrs.c @@ -68,6 +68,20 @@ uirc_list_add_user(IRC_Buffer* buf, IRC_User* usr) return 1; } +bool +uirc_list_add_tag(IRC_Message* msg, IRC_Tag* tag) +{ + assert(msg != NULL); + if (tag == NULL) return 0; + llist_t* tmp = NULL; + if ((tmp = allocate_linked_list_elem(0)) == NULL) return 0; + if (msg->tag_list != NULL) connect_linked_list_elem(msg->tag_list, tmp); + else + msg->tag_list = tmp; + tmp->content = tag; + return 1; +} + bool uirc_list_remove_buffer(IRC_Network* net, IRC_Buffer* buf) { @@ -118,3 +132,20 @@ uirc_list_remove_message(IRC_Buffer* buf, IRC_Message* msg) } return 1; } + +bool +uirc_list_remove_tag(IRC_Message* msg, IRC_Tag* tag) +{ + assert(msg != NULL); + assert(tag != NULL); + for (llist_t* cur = msg->tag_list; cur != NULL;) { + if (cur->content == tag) { + if (cur == msg->tag_list) msg->tag_list = (cur->prev != NULL) ? cur->prev : cur->next; + uirc_free_tag(tag); + remove_linked_list_elem(cur); + break; + } + if ((cur = cur->next) == NULL) return 0; + } + return 1; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d0e2c87..fb859d8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,3 +7,12 @@ target_link_libraries(fullloop uirc) add_executable(earlytrail earlytrail.c common.c) target_link_libraries(earlytrail uirc) + +add_executable(optcrlf optcrlf.c common.c) +target_link_libraries(optcrlf uirc) + +add_executable(manassm manassm.c common.c) +target_link_libraries(manassm uirc) + +add_executable(allocs allocs.c common.c) +target_link_libraries(allocs uirc) diff --git a/tests/allocs.c b/tests/allocs.c new file mode 100644 index 0000000..5edb132 --- /dev/null +++ b/tests/allocs.c @@ -0,0 +1,65 @@ +/* + * This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC) + * Copyright (c) 2019, 2020 Alex-David Denes + * + * uIRC 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 + * any later version. + * + * uIRC 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 uIRC. If not, see . + */ + +#include "common.h" // expect() print_irc_message() +#include "uirc.h" // uirc_* IRC_* + +#include // true +#include // fprintf() +#include // EXIT_* +#include // strcmp() + +#define NICKN "nick" +#define USERN "someuser" +#define HOSTN "pop.com" +#define TAGKEY "time" +#define TAGVAL "2020-09-02T20:10:30.000Z" +#define CMD "NOTICE" +#define ARG1 "*" +#define ARG2 "piss off" + +int +main(void) +{ + IRC_User* u = uirc_malloc_user(NICKN, USERN, NULL, HOSTN); + if (strcmp(NICKN, u->nick) != 0 || strcmp(USERN, u->user) != 0 || strcmp(HOSTN, u->host) != 0) { + fprintf(stderr, "User allocator returned unexpected results\n"); + return EXIT_FAILURE; + } + uirc_free_user(u); + free(u); + + IRC_Tag* t = uirc_malloc_tag(TAGKEY, TAGVAL); + if (strcmp(TAGKEY, t->key) != 0 || strcmp(TAGVAL, t->value) != 0) { + fprintf(stderr, "Tag allocator returned unexpected results\n"); + return EXIT_FAILURE; + } + uirc_free_tag(t); + free(t); + + IRC_Message* m = uirc_malloc_message(CMD, 2, ARG1, ARG2); + m->trailing = true; + if (strcmp(m->command, CMD) != 0 || strcmp(m->args[0], ARG1) != 0 || strcmp(m->args[1], ARG2) != 0) { + fprintf(stderr, "Message allocator returned unexpected results\n"); + return EXIT_FAILURE; + } + uirc_free_message(m); + free(m); + + return EXIT_SUCCESS; +} diff --git a/tests/manassm.c b/tests/manassm.c new file mode 100644 index 0000000..0c64882 --- /dev/null +++ b/tests/manassm.c @@ -0,0 +1,62 @@ +/* + * This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC) + * Copyright (c) 2019, 2020 Alex-David Denes + * + * uIRC 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 + * any later version. + * + * uIRC 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 uIRC. If not, see . + */ + +#include "common.h" // expect() print_irc_message() +#include "uirc.h" // uirc_* IRC_* + +#include // fprintf() +#include // EXIT_* +#include // strcmp() + +#define TAGKEY "wow" +#define TAGKEY2 "wowcum" +#define TAGVAL "wow2" +#define TAGVAL2 "wow3rd" + +int +main(void) +{ + IRC_Message* m = uirc_malloc_message("NONE", 0); + + uirc_list_add_tag(m, uirc_malloc_tag(TAGKEY, TAGVAL)); + uirc_list_add_tag(m, uirc_malloc_tag(TAGKEY2, TAGVAL2)); + + IRC_Tag* t = m->tag_list->next->content; + if (strcmp(TAGKEY2, t->key) != 0 || strcmp(TAGVAL2, t->value) != 0) { + fprintf(stderr, "List allocator didn't allocate things right\n"); + return EXIT_FAILURE; + } + + t = m->tag_list->content; + if (strcmp(TAGKEY, t->key) != 0 || strcmp(TAGVAL, t->value) != 0) { + fprintf(stderr, "List allocator didn't allocate things right\n"); + return EXIT_FAILURE; + } + + uirc_list_remove_tag(m, m->tag_list->content); + t = m->tag_list->content; + if (strcmp(TAGKEY2, t->key) != 0 || strcmp(TAGVAL2, t->value) != 0) { + fprintf(stderr, "List remover fucked up\n"); + return EXIT_FAILURE; + } + + uirc_free_message(m); + free(m); + + return EXIT_SUCCESS; +} diff --git a/tests/optcrlf.c b/tests/optcrlf.c new file mode 100644 index 0000000..a1edfac --- /dev/null +++ b/tests/optcrlf.c @@ -0,0 +1,44 @@ +/* + * This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC) + * Copyright (c) 2019, 2020 Alex-David Denes + * + * uIRC 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 + * any later version. + * + * uIRC 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 uIRC. If not, see . + */ + +#include "common.h" // expect() print_irc_message() +#include "uirc.h" // uirc_* IRC_* + +#include // assert() +#include // printf() +#include // EXIT_SUCCESS +#include // strlen() + +int +main(void) +{ + const char* str = + "NOTICE #public :yo does someone want to watch Redo of Healer with me?"; // This should parse just fine as CRLF is a delimiter and nothing + // important for the message itself + + IRC_Message* m = NULL; + if ((m = uirc_tokenizer_message(str)) == NULL) { + fprintf(stderr, "Failed to convert string to IRC_Message struct.\n"); + return EXIT_FAILURE; + } + print_irc_message(m); + uirc_free_message(m); + free(m); + + return EXIT_SUCCESS; +}