| summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | src/io/data_input.c | 98 | ||||
| -rw-r--r-- | src/io/data_input.h | 21 | ||||
| -rw-r--r-- | src/io/data_input_types.h | 16 | ||||
| -rw-r--r-- | src/io/data_output.c | 65 | ||||
| -rw-r--r-- | src/io/data_output.h | 11 | ||||
| -rw-r--r-- | src/io/error.h | 148 | ||||
| -rw-r--r-- | src/io/network.c | 568 | ||||
| -rw-r--r-- | src/io/network.h | 28 | ||||
| -rw-r--r-- | src/io/network_types.h | 34 | ||||
| -rw-r--r-- | src/io/parameters.c | 385 | ||||
| -rw-r--r-- | src/io/parameters.h | 13 | ||||
| -rw-r--r-- | src/io/parameters_types.h | 21 |
13 files changed, 0 insertions, 1417 deletions
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt deleted file mode 100644 index c36413a..0000000 --- a/src/io/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set( - SRC_FILES ${SRC_FILES} - ${CMAKE_CURRENT_SOURCE_DIR}/parameters.c - ${CMAKE_CURRENT_SOURCE_DIR}/network.c - ${CMAKE_CURRENT_SOURCE_DIR}/data_input.c - ${CMAKE_CURRENT_SOURCE_DIR}/data_output.c -) -set(SRC_FILES ${SRC_FILES} PARENT_SCOPE) - diff --git a/src/io/data_input.c b/src/io/data_input.c deleted file mode 100644 index e31d33b..0000000 --- a/src/io/data_input.c +++ /dev/null @@ -1,98 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include <stdlib.h> -#include <string.h> -#include <stdint.h> /* defines SIZE_MAX */ - -#include "error.h" - -#include "data_input.h" - -int ZoO_data_input_open -( - struct ZoO_data_input di [const static 1], - const char filename [const restrict static 1] -) -{ - /* prevents di [restrict] */ - ZoO_strings_initialize(&(di->string)); - - di->file = fopen(filename, "r"); - - if (di->file == (FILE *) NULL) - { - ZoO_ERROR - ( - "Could not open file '%s' in readonly mode.", - filename - ); - - return -1; - } - - return 0; -} - -int ZoO_data_input_read_line -( - struct ZoO_data_input di [const static 1], - ZoO_index const punctuations_count, - const ZoO_char punctuations [const restrict static punctuations_count] -) -{ - size_t line_size, i, w_start; - ZoO_char * line; - - /* prevents di [restrict] */ - ZoO_strings_finalize(&(di->string)); - - line = (ZoO_char *) NULL; - line_size = 0; - - /* XXX: assumed compatible with ZoO_char */ - - if (getline(&line, &line_size, di->file) < 1) - { - free((void *) line); - - return -1; - } - - line_size = strlen(line); - line[line_size - 1] = '\0'; - - --line_size; /* removed '\n' */ - - if - ( - ZoO_strings_parse - ( - &(di->string), - line_size, - line, - punctuations_count, - punctuations - ) < 0 - ) - { - free((void *) line); - - return -1; - } - - free((void *) line); - - return 0; -} - -void ZoO_data_input_close (struct ZoO_data_input di [const static 1]) -{ - if (di->file != (FILE *) NULL) - { - fclose(di->file); - - di->file = (FILE *) NULL; - } - - /* prevents di [restrict] */ - ZoO_strings_finalize(&(di->string)); -} diff --git a/src/io/data_input.h b/src/io/data_input.h deleted file mode 100644 index a2f004b..0000000 --- a/src/io/data_input.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _ZoO_IO_DATA_INPUT_H_ -#define _ZoO_IO_DATA_INPUT_H_ - -#include "data_input_types.h" - -int ZoO_data_input_open -( - struct ZoO_data_input di [const static 1], - const char filename [const restrict static 1] -); - -int ZoO_data_input_read_line -( - struct ZoO_data_input di [const static 1], - ZoO_index const punctuations_count, - const ZoO_char punctuations [const restrict static punctuations_count] -); - -void ZoO_data_input_close (struct ZoO_data_input di [const static 1]); - -#endif diff --git a/src/io/data_input_types.h b/src/io/data_input_types.h deleted file mode 100644 index bd2709b..0000000 --- a/src/io/data_input_types.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef _ZoO_IO_DATA_INPUT_TYPES_H_ -#define _ZoO_IO_DATA_INPUT_TYPES_H_ - -#include <stdio.h> - -#include "../pervasive.h" - -#include "../tool/strings.h" - -struct ZoO_data_input -{ - FILE * restrict file; - struct ZoO_strings string; -}; - -#endif diff --git a/src/io/data_output.c b/src/io/data_output.c deleted file mode 100644 index 796d3d0..0000000 --- a/src/io/data_output.c +++ /dev/null @@ -1,65 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <stdint.h> /* defines SIZE_MAX */ -#include <stdio.h> - -#include "error.h" - -#include "data_output.h" - -int ZoO_data_output_write_line -( - const char filename [const restrict static 1], - char line [const restrict static 1], - size_t const line_size -) -{ - const int old_errno = errno; - FILE * file; - - file = fopen(filename, "a"); - - if (file == (FILE *) NULL) - { - ZoO_ERROR - ( - "Could not open file '%s' in appending mode.", - filename - ); - - return -1; - } - - line[line_size - 1] = '\n'; - - if - ( - fwrite - ( - (const void *) line, - sizeof(char), - line_size, - file - ) < line_size - ) - { - line[line_size - 1] = '\0'; - - ZoO_ERROR - ( - "Could not store line '%s' in %s.", - line, - filename - ); - - fclose(file); - - return -1; - } - - fclose(file); - - return 0; -} diff --git a/src/io/data_output.h b/src/io/data_output.h deleted file mode 100644 index ef963a0..0000000 --- a/src/io/data_output.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _ZoO_IO_DATA_OUTPUT_H_ -#define _ZoO_IO_DATA_OUTPUT_H_ - -int ZoO_data_output_write_line -( - const char filename [const restrict static 1], - char line [const restrict static 1], - size_t const line_size -); - -#endif diff --git a/src/io/error.h b/src/io/error.h deleted file mode 100644 index be7359f..0000000 --- a/src/io/error.h +++ /dev/null @@ -1,148 +0,0 @@ -#ifndef _ZoO_IO_ERROR_H_ -#define _ZoO_IO_ERROR_H_ - -#include <stdio.h> - -#include "../pervasive.h" - -#define ZoO_DEBUG_ALL 1 - -#ifndef ZoO_DEBUG_ALL - #define ZoO_DEBUG_ALL 0 -#endif - -#ifndef ZoO_DEBUG_PROGRAM_FLOW - #define ZoO_DEBUG_PROGRAM_FLOW (0 || ZoO_DEBUG_ALL) -#endif - -#ifndef ZoO_DEBUG_CONFIG - #define ZoO_DEBUG_CONFIG (0 || ZoO_DEBUG_ALL) -#endif - -#ifndef ZoO_DEBUG_LEARNING - #define ZoO_DEBUG_LEARNING (0 || ZoO_DEBUG_ALL) -#endif - -#define ZoO_DEBUG_NETWORK 1 - -#ifndef ZoO_DEBUG_NETWORK - #define ZoO_DEBUG_NETWORK (0 || ZoO_DEBUG_ALL) -#endif - -#define ZoO_ENABLE_WARNINGS_OUTPUT 1 -#define ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT 1 -#define ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT 1 -#define ZoO_ENABLE_FATAL_ERROR_OUTPUT 1 - -#ifdef ZoO_ENABLE_ERROR_LOCATION - #define ZoO_LOCATION "[" __FILE__ "][" ZoO_TO_STRING(__LINE__) "]" -#else - #define ZoO_LOCATION "" -#endif - -#define ZoO_PRINT_STDERR(symbol, str, ...)\ - fprintf(stderr, "[" symbol "]" ZoO_LOCATION " " str "\n", __VA_ARGS__); - -/* - * Given that we use preprocessor contants as flags, we can expect the compilers - * to remove the test condition for disabled flags. No need to be shy about - * allowing many debug options. - */ - -#define ZoO_DEBUG(flag, str, ...)\ - ZoO_ISOLATE\ - (\ - if (flag)\ - {\ - ZoO_PRINT_STDERR("D", str, __VA_ARGS__);\ - }\ - ) - - -#define ZoO_WARNING(str, ...)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_WARNINGS_OUTPUT)\ - {\ - ZoO_PRINT_STDERR("W", str, __VA_ARGS__);\ - }\ - ) - -#define ZoO_ERROR(str, ...)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT)\ - {\ - ZoO_PRINT_STDERR("E", str, __VA_ARGS__);\ - }\ - ) - -#define ZoO_PROG_ERROR(str, ...)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\ - {\ - ZoO_PRINT_STDERR("P", str, __VA_ARGS__);\ - }\ - ) - -#define ZoO_FATAL(str, ...)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_FATAL_ERROR_OUTPUT)\ - {\ - ZoO_PRINT_STDERR("F", str, __VA_ARGS__);\ - }\ - ) - -/* For outputs without dynamic content (static). ******************************/ - -#define ZoO_PRINT_S_STDERR(symbol, str)\ - fprintf(stderr, "[" symbol "]" ZoO_LOCATION " " str "\n"); - -#define ZoO_S_DEBUG(flag, str)\ - ZoO_ISOLATE\ - (\ - if (flag)\ - {\ - ZoO_PRINT_S_STDERR("D", str);\ - }\ - ) - -#define ZoO_S_WARNING(str)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_WARNINGS_OUTPUT)\ - {\ - ZoO_PRINT_S_STDERR("W", str);\ - }\ - ) - -#define ZoO_S_ERROR(str)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT)\ - {\ - ZoO_PRINT_S_STDERR("E", str);\ - }\ - ) - -#define ZoO_S_PROG_ERROR(str)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\ - {\ - ZoO_PRINT_S_STDERR("P", str);\ - }\ - ) - -#define ZoO_S_FATAL(str)\ - ZoO_ISOLATE\ - (\ - if (ZoO_ENABLE_FATAL_ERROR_OUTPUT)\ - {\ - ZoO_PRINT_S_STDERR("F", str);\ - }\ - ) - -#endif diff --git a/src/io/network.c b/src/io/network.c deleted file mode 100644 index edafd4f..0000000 --- a/src/io/network.c +++ /dev/null @@ -1,568 +0,0 @@ -#include <stdio.h> -#include <unistd.h> -#include <string.h> -#include <errno.h> - -/* "POSIX.1 does not require the inclusion of <sys/types.h>" */ -/* - man page for setsockopt */ -/* #include <sys/types.h> */ -#include <sys/socket.h> -#include <sys/time.h> - -#include "error.h" - -#include "network.h" - -static int re_create_socket (struct ZoO_network net [const restrict static 1]) -{ - struct timeval timeout; - const int old_errno = errno; - - errno = 0; - timeout.tv_sec = ZoO_NETWORK_TIMEOUT; - timeout.tv_usec = 0; - - if (net->connection != -1) - { - close(net->connection); - } - - net->connection = - socket - ( - net->addrinfo->ai_family, - net->addrinfo->ai_socktype, - net->addrinfo->ai_protocol - ); - - if (net->connection == -1) - { - ZoO_ERROR("Could not create socket: %s.", strerror(errno)); - - goto RETURN_FAILED; - } - - if - ( - ( - setsockopt - ( - net->connection, - SOL_SOCKET, - SO_RCVTIMEO, - (const void *) &timeout, - (socklen_t) sizeof(struct timeval) - ) < 0 - ) - || - ( - setsockopt - ( - net->connection, - SOL_SOCKET, - SO_SNDTIMEO, - (const void *) &timeout, - (socklen_t) sizeof(struct timeval) - ) < 0 - ) - ) - { - ZoO_ERROR("Could not set timeout on network socket: %s", strerror(errno)); - - goto RETURN_FAILED; - } - - ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "(Re)connecting to network..."); - - if - ( - connect - ( - net->connection, - net->addrinfo->ai_addr, - net->addrinfo->ai_addrlen - ) != 0 - ) - { - ZoO_ERROR("Could not establish connection: %s", strerror(errno)); - - goto RETURN_FAILED; - } - - errno = old_errno; - - return 0; - -RETURN_FAILED: - errno = old_errno; - - return -1; -} - -static int reconnect (struct ZoO_network net [const restrict static 1]) -{ - const int old_errno = errno; - - memset(net->in, 0, (sizeof(ZoO_char) * 513)); - memset(net->out, 0, (sizeof(ZoO_char) * 513)); - memset(net->buffer, 0, (sizeof(ZoO_char) * 513)); - - net->buffer_index = 0; - net->buffer_remaining = 0; - - if (re_create_socket(net) < 0) - { - return -1; - } - - snprintf(net->out, 512, "USER %s 8 * :%s\r\n", net->user, net->name); - - if (write(net->connection, net->out, strlen(net->out)) < 1) - { - goto RETURN_WRITE_FAILED; - } - - snprintf(net->out, 512, "NICK %s\r\n", net->nick); - - if (write(net->connection, net->out, strlen(net->out)) < 1) - { - goto RETURN_WRITE_FAILED; - } - - net->buffer_remaining = 0; - net->buffer_index = 0; - - ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "(Re)connected."); - - errno = old_errno; - - return 0; - -RETURN_WRITE_FAILED: - ZoO_ERROR - ( - "Unable to write to the network: %s", - strerror(errno) - ); - - errno = old_errno; - - return -1; -} - -int ZoO_network_connect -( - struct ZoO_network net [const static 1], - const char host [const restrict static 1], - const char port [const restrict static 1], - const char channel [const restrict static 1], - const char user [const restrict static 1], - const char name [const restrict static 1], - const char nick [const restrict static 1] -) -{ - int error; - struct addrinfo hints; - const int old_errno = errno; - - net->connection = -1; - net->channel = channel; - net->user = user; - net->name = name; - net->nick = nick; - net->buffer_index = 0; - net->buffer_remaining = 0; - - memset(&hints, 0, sizeof(struct addrinfo)); - memset(net->in, 0, (sizeof(ZoO_char) * 513)); - memset(net->out, 0, (sizeof(ZoO_char) * 513)); - memset(net->buffer, 0, (sizeof(ZoO_char) * 513)); - - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - - errno = 0; - - error = getaddrinfo(host, port, &hints, &(net->addrinfo)); - - if (error != 0) - { - if (error == EAI_SYSTEM) - { - ZoO_ERROR - ( - "Could not retrieve server information: %s.", - strerror(errno) - ); - } - else - { - ZoO_FATAL - ( - "Could not retrieve server information: %s.", - gai_strerror(error) - ); - } - - errno = old_errno; - - return -1; - } - - errno = old_errno; - - reconnect(net); - - return 0; -} - -static void buffer_msg -( - struct ZoO_network net [const static 1] -) -{ - ssize_t in_count, i; - - if (net->buffer_remaining > 0) - { - in_count = net->buffer_remaining; - net->buffer_remaining = 0; - - goto PARSE_READ; - } - -READ_MORE: - in_count = read(net->connection, net->buffer, 512); - - if (in_count <= 0) - { - ZoO_ERROR("Could not read from network: %s", strerror(errno)); - - while (reconnect(net) < 0) - { - ZoO_S_DEBUG - ( - ZoO_DEBUG_NETWORK, - "Attempting new connection in 5s." - ); - sleep(5); - } - - goto READ_MORE; - } - -PARSE_READ: - for (i = 0; i < in_count; ++i) - { - net->in[net->buffer_index] = net->buffer[i]; - - if - ( - (net->buffer_index > 0) - && (net->in[net->buffer_index - 1] == '\r') - && (net->in[net->buffer_index] == '\n') - ) - { - net->buffer_remaining = (in_count - (i + 1)); - net->in_length = (net->buffer_index - 1); - net->buffer_index = 0; - - if (net->buffer_remaining > 0) - { - memmove - ( - (void *) net->buffer, - (const void *) (net->buffer + (i + 1)), - net->buffer_remaining - ); - } - - return; - } - - net->buffer_index += 1; - - if (net->buffer_index > 512) - { - ZoO_S_WARNING("Incoming message is too long. Discarded."); - - net->buffer_index = 0; - net->buffer_remaining = 0; - - break; - } - } - - goto READ_MORE; -} - -void handle_ping (struct ZoO_network net [const restrict static 1]) -{ - const int old_errno = errno; - - #if ZoO_RANDOMLY_IGNORE_PING == 1 - if ((rand() % 10) < 3) - { - ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "Ping request ignored."); - - return; - } - - #endif - - #if ZoO_DEBUG_NETWORK_PING == 1 - net->in[net->in_length] = '\0'; - - ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s", net->in); - - net->in[net->in_length] = '\r'; - #endif - - net->in[1] = 'O'; - - errno = 0; - - if (write(net->connection, net->in, (net->in_length + 2)) < 1) - { - ZoO_ERROR("Could not reply to PING request: %s", strerror(errno)); - - errno = old_errno; - - while (reconnect(net) < 0) - { - ZoO_S_DEBUG - ( - ZoO_DEBUG_NETWORK, - "Attempting new connection in 5s." - ); - sleep(5); - } - - return; - } - - errno = old_errno; - -#if ZoO_DEBUG_NETWORK_PING == 1 - net->in[net->in_length] = '\0'; - - ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->in); -#endif - -} - -int ZoO_network_receive -( - struct ZoO_network net [const restrict static 1], - size_t msg_offset [const restrict static 1], - size_t msg_size [const restrict static 1], - enum ZoO_msg_type type [const restrict static 1] -) -{ - const int old_errno = errno; - ssize_t cmd, i; - -READ_NEW_MSG: - buffer_msg(net); - - net->in[net->in_length + 2] = '\0'; - - /* XXX: doesn't that prevent net [restrict]? */ - if (ZoO_IS_PREFIX("PING", net->in)) - { - - handle_ping(net); - - goto READ_NEW_MSG; - } - - if (net->in_length == 0) - { - goto READ_NEW_MSG; - } - - net->in[net->in_length] = '\0'; - - ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s", net->in); - - if (net->in[0] == ':') - { - cmd = 0; - - for (i = 1; i < 512; i++) - { - if (net->in[i] == ' ') - { - cmd = (i + 1); - - break; - } - } - - if (ZoO_IS_PREFIX("001", (net->in + cmd))) - { - snprintf - ( - net->out, - 512, - "JOIN :%s\r\n", - net->channel - ); - - errno = 0; - - if (write(net->connection, net->out, strlen(net->out)) < 1) - { - ZoO_ERROR - ( - "Could not send JOIN request: %s", - strerror(errno) - ); - - errno = old_errno; - - if (reconnect(net) < 0) - { - return -1; - } - } - - errno = old_errno; - - ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->out); - - goto READ_NEW_MSG; - } - - if (ZoO_IS_PREFIX("JOIN", (net->in + cmd))) - { - for (i = 1; (i < 512) && (net->in[i] != '!'); ++i) - { - } - - if ((i == 512) || (i == 1)) - { - ZoO_ERROR("Could not find JOIN username: %s", net->in); - - goto READ_NEW_MSG; - } - - *msg_offset = 1; - *msg_size = (i - 1); - net->in[i] = '\0'; - - *type = ZoO_JOIN; - - return 0; - } - - if (ZoO_IS_PREFIX("PRIVMSG", (net->in + cmd))) - { - - for (; i < 512; i++) - { - if (net->in[i] == ':') - { - cmd = (i + 1); - - break; - } - } - - *msg_offset = cmd; - *msg_size = (net->in_length - cmd); - - /*net->in[*msg_size - 1] = '\0'; */ - - *type = ZoO_PRIVMSG; - - return 0; - } - } - - if (ZoO_IS_PREFIX("ERROR", (net->in + cmd))) - { - while (reconnect(net) < 0) - { - ZoO_S_DEBUG - ( - ZoO_DEBUG_NETWORK, - "Attempting new connection in 5s." - ); - sleep(5); - } - } - - goto READ_NEW_MSG; -} - -int ZoO_network_send (struct ZoO_network net [const restrict static 1]) -{ - int const old_errno = errno; - - if (ZoO_IS_PREFIX("\001action", net->out)) - { - - net->out[1] = 'A'; - net->out[2] = 'C'; - net->out[3] = 'T'; - net->out[4] = 'I'; - net->out[5] = 'O'; - net->out[6] = 'N'; - - snprintf - ( - net->in, - 512, - "PRIVMSG %s :%s\001\r\n", - net->channel, - net->out - ); - } - else - { - snprintf - ( - net->in, - 512, - "PRIVMSG %s :%s\r\n", - net->channel, - net->out - ); - } - - errno = 0; - - if (write(net->connection, net->in, strlen(net->in)) < 1) - { - ZoO_ERROR - ( - "Could not send PRIVMSG: %s.", - strerror(errno) - ); - - errno = old_errno; - - if (reconnect(net) < 0) - { - return -2; - } - else - { - return -1; - } - } - - errno = old_errno; - - ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->in); - - return 0; -} - -void ZoO_network_disconnect (struct ZoO_network net [const restrict static 1]) -{ - freeaddrinfo(net->addrinfo); - close(net->connection); -} - diff --git a/src/io/network.h b/src/io/network.h deleted file mode 100644 index 647b19c..0000000 --- a/src/io/network.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _ZoO_IO_NETWORK_H_ -#define _ZoO_IO_NETWORK_H_ -#include "network_types.h" - -int ZoO_network_connect -( - struct ZoO_network net [const static 1], - const char host [const restrict static 1], - const char port [const restrict static 1], - const char channel [const restrict static 1], - const char user [const restrict static 1], - const char name [const restrict static 1], - const char nick [const restrict static 1] -); - -int ZoO_network_receive -( - struct ZoO_network net [const static 1], - size_t msg_offset [const restrict static 1], - size_t msg_size [const restrict static 1], - enum ZoO_msg_type type [const restrict static 1] -); - -int ZoO_network_send (struct ZoO_network net [const restrict static 1]); - -void ZoO_network_disconnect (struct ZoO_network net [const restrict static 1]); - -#endif diff --git a/src/io/network_types.h b/src/io/network_types.h deleted file mode 100644 index 9a328a7..0000000 --- a/src/io/network_types.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef _ZoO_IO_NETWORK_TYPES_H_ -#define _ZoO_IO_NETWORK_TYPES_H_ - -#define POSIX_C_SOURCE - -#include <sys/types.h> -#include <sys/socket.h> -#include <netdb.h> - -#include "../pervasive.h" - -enum ZoO_msg_type -{ - ZoO_PRIVMSG, - ZoO_JOIN -}; - -struct ZoO_network -{ - size_t buffer_index; - size_t buffer_remaining; - size_t in_length; - struct addrinfo * addrinfo; - ZoO_char buffer [513]; - ZoO_char in [513]; - ZoO_char out [513]; - int connection; - const char * restrict channel; - const char * restrict user; - const char * restrict name; - const char * restrict nick; -}; - -#endif diff --git a/src/io/parameters.c b/src/io/parameters.c deleted file mode 100644 index 77c33aa..0000000 --- a/src/io/parameters.c +++ /dev/null @@ -1,385 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <errno.h> - -#include "../pervasive.h" - -#include "error.h" - -#include "parameters.h" - -static void load_default_parameters -( - struct ZoO_parameters param [const restrict static 1] -) -{ - param->data_filename = ZoO_DEFAULT_DATA_FILENAME; - param->new_data_filename = (char *) NULL; - - param->irc_server_addr = ZoO_DEFAULT_IRC_SERVER_ADDR; - param->irc_server_port = ZoO_DEFAULT_IRC_SERVER_PORT; - param->irc_server_channel = ZoO_DEFAULT_IRC_SERVER_CHANNEL; - param->irc_username = ZoO_DEFAULT_IRC_USERNAME; - param->irc_realname = ZoO_DEFAULT_IRC_REALNAME; - - param->reply_rate = ZoO_DEFAULT_REPLY_RATE; - - param->aliases_count = 0; - param->aliases = NULL; -} - -static void print_help (const char exec [const restrict static 1]) -{ - printf - ( - "Usage: %s [option_1 option_2 ...] NICKNAME [ALIAS_1 ALIAS_2 ...] \n" - "NICKNAME is used as the IRC nickname value.\n" - "If NICKNAME or any ALIAS is found in an event, the program will reply.\n" - "\nAvailable options:\n" - " [--data-filename | -df] FILENAME\n" - " Learn content from FILENAME before connecting.\n" - " Default: %s.\n" - " [--new-data-filename | -ndf] FILENAME\n" - " Store new data learned in FILENAME.\n" - " Default: value of the --data-filename param.\n" - " [--irc-server-addr | -isa] IRC_SERVER_ADDR\n" - " Connect to this server address.\n" - " Default: %s.\n" - " [--irc-server-port | -isp] IRC_SERVER_PORT\n" - " Connect to this server port.\n" - " Default: %s.\n" - " [--irc-server-channel | -isc] IRC_SERVER_CHANNEL\n" - " Connect to this server's channel.\n" - " Default: %s.\n" - " [--irc-username | -iu] USERNAME\n" - " Connect using this as 'username' (shown in WHOIS).\n" - " Default: %s.\n" - " [--irc-realname | -ir] REALNAME\n" - " Connect using this as 'realname' (shown in WHOIS).\n" - " Default: %s.\n" - " [--reply-rate | -rr] REPLY_RATE\n" - " Chance to reply to an event (integer, range [0, 100]).\n" - " Default: %d.\n", - exec, - ZoO_DEFAULT_DATA_FILENAME, - ZoO_DEFAULT_IRC_SERVER_ADDR, - ZoO_DEFAULT_IRC_SERVER_PORT, - ZoO_DEFAULT_IRC_SERVER_CHANNEL, - ZoO_DEFAULT_IRC_USERNAME, - ZoO_DEFAULT_IRC_REALNAME, - ZoO_DEFAULT_REPLY_RATE - ); -} - -static int parse_string_arg -( - const char * restrict dest [const restrict static 1], - int const i, - const char * restrict argv [const restrict static 1], - int const argc -) -{ - if (i == argc) - { - ZoO_FATAL - ( - "Missing value for parameter '%s'.", - /* Safe: i > 1 */ - argv[i - 1] - ); - - return -1; - } - - *dest = argv[i]; - - return 0; -} - -static int parse_integer_arg -( - int dest [const restrict static 1], - int const i, - const char * argv [const restrict static 1], - int const argc, - int const min_val, - int const max_val -) -{ - long int result; - char * endptr; - const int old_errno = errno; - - if (i == argc) - { - ZoO_FATAL - ( - "Missing value for parameter '%s'.", - /* Safe: i > 1 */ - argv[i - 1] - ); - - return -1; - } - - errno = 0; - - result = strtol(argv[i], &endptr, 10); - - if - ( - (errno != 0) - || ((*endptr) == '\n') - || (result < min_val) - || (result > max_val) - ) - { - ZoO_FATAL - ( - "Invalid or missing value for parameter '%s', accepted range is " - "[%d, %d] (integer).", - /* Safe: i > 1 */ - argv[i - 1], - min_val, - max_val - ); - - errno = old_errno; - - return -1; - } - - *dest = (int) result; - - errno = old_errno; - - return 0; -} - -int ZoO_parameters_initialize -( - struct ZoO_parameters param [const restrict static 1], - int const argc, - const char * argv [const restrict static argc] -) -{ - int i; - - load_default_parameters(param); - - for (i = 1; i < argc; ++i) - { - if - ( - (strcmp(argv[i], "--data-filename") == 0) - || (strcmp(argv[i], "-df") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->data_filename), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--new-data-filename") == 0) - || (strcmp(argv[i], "-ndf") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->new_data_filename), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--irc-server-addr") == 0) - || (strcmp(argv[i], "-isa") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->irc_server_addr), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--irc-server-port") == 0) - || (strcmp(argv[i], "-isp") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->irc_server_port), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--irc-server-channel") == 0) - || (strcmp(argv[i], "-isc") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->irc_server_channel), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--irc-username") == 0) - || (strcmp(argv[i], "-iu") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->irc_username), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--irc-realname") == 0) - || (strcmp(argv[i], "-in") == 0) - ) - { - i += 1; - - if - ( - parse_string_arg - ( - &(param->irc_realname), - i, - argv, - argc - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--reply-rate") == 0) - || (strcmp(argv[i], "-rr") == 0) - ) - { - i += 1; - - if - ( - parse_integer_arg - ( - &(param->reply_rate), - i, - argv, - argc, - 0, - 100 - ) < 0 - ) - { - return -1; - } - } - else if - ( - (strcmp(argv[i], "--help") == 0) - || (strcmp(argv[i], "-h") == 0) - ) - { - print_help(argv[0]); - - return 0; - } - else - { - break; - } - } - - if (i == argc) - { - ZoO_S_FATAL("Missing argument: NICKNAME"); - - print_help(argv[0]); - - return -1; - } - - param->aliases_count = (argc - i); - param->aliases = (argv + i); - - if (param->new_data_filename == (char *) NULL) - { - param->new_data_filename = param->data_filename; - } - - return 1; -} diff --git a/src/io/parameters.h b/src/io/parameters.h deleted file mode 100644 index 1011e2b..0000000 --- a/src/io/parameters.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef _ZoO_IO_PARAMETERS_H_ -#define _ZoO_IO_PARAMETERS_H_ - -#include "parameters_types.h" - -int ZoO_parameters_initialize -( - struct ZoO_parameters param [const static 1], - int const argc, - const char * argv [const static argc] -); - -#endif diff --git a/src/io/parameters_types.h b/src/io/parameters_types.h deleted file mode 100644 index 92a9e30..0000000 --- a/src/io/parameters_types.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _ZoO_IO_PARAMETERS_TYPES_H_ -#define _ZoO_IO_PARAMETERS_TYPES_H_ - -struct ZoO_parameters -{ - const char * restrict data_filename; - const char * restrict new_data_filename; - - const char * restrict irc_server_addr; - const char * restrict irc_server_port; - const char * restrict irc_server_channel; - const char * restrict irc_username; - const char * restrict irc_realname; - - int reply_rate; - - int aliases_count; - const char * restrict * restrict aliases; -}; - -#endif |


