diff --git a/build.sh b/build.sh index 8c0038c..dc919c6 100755 --- a/build.sh +++ b/build.sh @@ -3,7 +3,7 @@ set -xe CC="gcc" -FLAGS="-Wall -Wpedantic -ggdb" +FLAGS="-Wall -ggdb" SRCS="$(find ./src -type f -name "*.c")" CURRENT_DIR="$(git rev-parse --show-toplevel)" BUILD_DIR="${CURRENT_DIR}/build" diff --git a/src/cmd.c b/src/cmd.c index 5c43720..a5f0f87 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,5 +1,6 @@ #include "cmd.h" #include "tools/log.h" +#include "tools/alloc_wrappers.h" #include "tools/buffer.h" #include @@ -8,15 +9,18 @@ static struct ArgList* supported_args; static const char* PREDEFINED_ARG_NAMES [] = { - "PROJECT" + "PROJECT", + "help" }; static const char* PREDEFINED_ARG_DESC [] = { - "Path to the project to be used for looking up TODO's" + "Path to the project to be used for looking up TODO's", + "Show this message" }; static const char* PREDEFINED_ARG_VALUES [] = { - "Curernt WD" + "Curernt WD", + NULL }; void usage(void) { @@ -32,26 +36,27 @@ void usage(void) { buffer_append(usage_lines, supported_args->args[i].value); buffer_append(usage_lines, "]"); } + buffer_append(usage_lines, "\n"); } buffer_append(usage_lines, "\n"); buffer_flush(usage_lines); } static struct CommandLineArg* renderArg(ArgName name) { - struct CommandLineArg* e = malloc(sizeof(struct CommandLineArg)); + struct CommandLineArg* e = cdo_malloc(sizeof(struct CommandLineArg)); e->name = NULL; e->description = NULL; e->value = NULL; if (PREDEFINED_ARG_NAMES[name]) { - e->name = malloc(strlen(PREDEFINED_ARG_NAMES[name]) + 1); + e->name = cdo_malloc(strlen(PREDEFINED_ARG_NAMES[name]) + 1); strcpy(e->name, PREDEFINED_ARG_NAMES[name]); } if (PREDEFINED_ARG_DESC[name]) { - e->description = malloc(strlen(PREDEFINED_ARG_DESC[name]) + 1); + e->description = cdo_malloc(strlen(PREDEFINED_ARG_DESC[name]) + 1); strcpy(e->description, PREDEFINED_ARG_DESC[name]); } if (PREDEFINED_ARG_VALUES[name]) { - e->value = malloc(strlen(PREDEFINED_ARG_VALUES[name]) + 1); + e->value = cdo_malloc(strlen(PREDEFINED_ARG_VALUES[name]) + 1); strcpy(e->value, PREDEFINED_ARG_VALUES[name]); } return e; @@ -60,7 +65,15 @@ static struct CommandLineArg* renderArg(ArgName name) { void init_cmd(void) { supported_args = create_arg_list(ARG_COUNT); for (int i = 0; i < ARG_COUNT; i++) { - append_arg(supported_args, renderArg(i)); + switch (i) + { + case ARG_PROJECT: + case ARG_HELP: + append_arg(supported_args, renderArg(i)); + break; + default: + LOG_ERROR("Undefined argument iterator %d", i); + } } } @@ -70,8 +83,8 @@ void exit_cmd(void) { struct ArgList* create_arg_list(size_t capacity) { struct ArgList* list = NULL; - list = (struct ArgList*) malloc(sizeof(struct ArgList)); - list->args = (struct CommandLineArg*) malloc(capacity * sizeof(struct CommandLineArg)); + list = (struct ArgList*) cdo_malloc(sizeof(struct ArgList)); + list->args = (struct CommandLineArg*) cdo_malloc(capacity * sizeof(struct CommandLineArg)); list->size = 0; list->capacity = capacity; return list; @@ -82,24 +95,24 @@ void append_arg(struct ArgList* list, struct CommandLineArg* arg) { list->capacity *= 2; list->args = (struct CommandLineArg*) - realloc( + cdo_realloc( list->args, list->capacity * sizeof(struct CommandLineArg) ); } memcpy(list->args + list->size, arg, sizeof(struct CommandLineArg)); - free(arg); + cdo_free(arg); list->size += 1; } void free_arg_list(struct ArgList* list) { for (size_t i = 0; i < ARG_COUNT; i++) { - free(list->args[i].name); - free(list->args[i].description); - free(list->args[i].value); + cdo_free(list->args[i].name); + cdo_free(list->args[i].description); + cdo_free(list->args[i].value); } - free(list->args); - free(list); + cdo_free(list->args); + cdo_free(list); } diff --git a/src/cmd.h b/src/cmd.h index adbfb60..242494b 100644 --- a/src/cmd.h +++ b/src/cmd.h @@ -10,6 +10,7 @@ typedef enum { ARG_PROJECT, + ARG_HELP, ARG_COUNT } ArgName; diff --git a/src/tools/alloc_wrappers.h b/src/tools/alloc_wrappers.h new file mode 100644 index 0000000..8c27fe3 --- /dev/null +++ b/src/tools/alloc_wrappers.h @@ -0,0 +1,53 @@ +#ifndef __ALLOC_WARPPERS_H__ +#define __ALLOC_WARPPERS_H__ + +#include "log.h" + +#include + +#define FAILED_ALLOC_MESSAGE \ + LOG_ERROR("Allocation failed. Returned pointer is NULL."); + +#define cdo_malloc(size) ((void*)({ \ + void *__ptr__ = malloc(size); \ + if (!__ptr__) { \ + FAILED_ALLOC_MESSAGE; \ + } \ + __ptr__; \ +})) + +#define cdo_free(ptr) do { \ + if (!ptr) { \ + LOG_WARNING("Freeing NULL pointer."); \ + } \ + free(ptr); \ +} while (0) + +#define cdo_calloc(nmemb, size) ((void*)({ \ + void *__ptr__ = calloc(nmemb, size); \ + if (!__ptr__) { \ + FAILED_ALLOC_MESSAGE; \ + } \ + __ptr__; \ +})) + +#define cdo_realloc(ptr, size) ((void*)({ \ + void *__ptr__ = realloc(ptr, size); \ + if (!__ptr__) { \ + FAILED_ALLOC_MESSAGE; \ + } \ + __ptr__; \ +})) + +#define cdo_reallocarray(ptr, nmemb, size) ((void*)({ \ + void *__ptr__ = reallocarray(ptr, nmemb, size); \ + if (!__ptr__) { \ + FAILED_ALLOC_MESSAGE; \ + } \ + __ptr__; \ +})) + + + + +#endif /*__ALLOC_WARPPERS_H__*/ \ No newline at end of file diff --git a/src/tools/buffer.c b/src/tools/buffer.c index 8099c86..e53458d 100644 --- a/src/tools/buffer.c +++ b/src/tools/buffer.c @@ -1,12 +1,13 @@ #include "buffer.h" +#include "alloc_wrappers.h" #include "log.h" #include #include Buffer* buffer_create(size_t capacity) { - Buffer* buffer = (Buffer*)malloc(sizeof(Buffer)); - buffer->data = (char*)malloc(capacity); + Buffer* buffer = (Buffer*)cdo_malloc(sizeof(Buffer)); + buffer->data = (char*)cdo_malloc(capacity); buffer->size = 0; buffer->capacity = capacity; return buffer; @@ -16,7 +17,7 @@ void buffer_append(Buffer* buffer, const char* str) { size_t len = strlen(str); if (buffer->size + len >= buffer->capacity) { buffer->capacity *= 2; - buffer->data = (char*)realloc(buffer->data, buffer->capacity); + buffer->data = (char*)cdo_realloc(buffer->data, buffer->capacity); } strcpy(buffer->data + buffer->size, str); buffer->size += len; @@ -31,9 +32,9 @@ void buffer_free(Buffer* buffer) { if (!buffer->data || !buffer) { LOG_WARNING("Trying to free NULL pointer"); } - free(buffer->data); + cdo_free(buffer->data); buffer->data = NULL; - free(buffer); + cdo_free(buffer); buffer = NULL; }