src: add help param in supported_params #1

- Add allocation wrapper
- Use wrapper in all sources
This commit is contained in:
tylen 2024-09-24 20:09:24 +00:00 committed by Vasily Davydov
parent 15d29d0a9e
commit dd15119a08
5 changed files with 91 additions and 23 deletions

View File

@ -3,7 +3,7 @@
set -xe set -xe
CC="gcc" CC="gcc"
FLAGS="-Wall -Wpedantic -ggdb" FLAGS="-Wall -ggdb"
SRCS="$(find ./src -type f -name "*.c")" SRCS="$(find ./src -type f -name "*.c")"
CURRENT_DIR="$(git rev-parse --show-toplevel)" CURRENT_DIR="$(git rev-parse --show-toplevel)"
BUILD_DIR="${CURRENT_DIR}/build" BUILD_DIR="${CURRENT_DIR}/build"

View File

@ -1,5 +1,6 @@
#include "cmd.h" #include "cmd.h"
#include "tools/log.h" #include "tools/log.h"
#include "tools/alloc_wrappers.h"
#include "tools/buffer.h" #include "tools/buffer.h"
#include <stdlib.h> #include <stdlib.h>
@ -8,15 +9,18 @@
static struct ArgList* supported_args; static struct ArgList* supported_args;
static const char* PREDEFINED_ARG_NAMES [] = { static const char* PREDEFINED_ARG_NAMES [] = {
"PROJECT" "PROJECT",
"help"
}; };
static const char* PREDEFINED_ARG_DESC [] = { 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 [] = { static const char* PREDEFINED_ARG_VALUES [] = {
"Curernt WD" "Curernt WD",
NULL
}; };
void usage(void) { void usage(void) {
@ -32,26 +36,27 @@ void usage(void) {
buffer_append(usage_lines, supported_args->args[i].value); buffer_append(usage_lines, supported_args->args[i].value);
buffer_append(usage_lines, "]"); buffer_append(usage_lines, "]");
} }
buffer_append(usage_lines, "\n");
} }
buffer_append(usage_lines, "\n"); buffer_append(usage_lines, "\n");
buffer_flush(usage_lines); buffer_flush(usage_lines);
} }
static struct CommandLineArg* renderArg(ArgName name) { 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->name = NULL;
e->description = NULL; e->description = NULL;
e->value = NULL; e->value = NULL;
if (PREDEFINED_ARG_NAMES[name]) { 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]); strcpy(e->name, PREDEFINED_ARG_NAMES[name]);
} }
if (PREDEFINED_ARG_DESC[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]); strcpy(e->description, PREDEFINED_ARG_DESC[name]);
} }
if (PREDEFINED_ARG_VALUES[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]); strcpy(e->value, PREDEFINED_ARG_VALUES[name]);
} }
return e; return e;
@ -60,7 +65,15 @@ static struct CommandLineArg* renderArg(ArgName name) {
void init_cmd(void) { void init_cmd(void) {
supported_args = create_arg_list(ARG_COUNT); supported_args = create_arg_list(ARG_COUNT);
for (int i = 0; i < ARG_COUNT; i++) { for (int i = 0; i < ARG_COUNT; i++) {
switch (i)
{
case ARG_PROJECT:
case ARG_HELP:
append_arg(supported_args, renderArg(i)); 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* create_arg_list(size_t capacity) {
struct ArgList* list = NULL; struct ArgList* list = NULL;
list = (struct ArgList*) malloc(sizeof(struct ArgList)); list = (struct ArgList*) cdo_malloc(sizeof(struct ArgList));
list->args = (struct CommandLineArg*) malloc(capacity * sizeof(struct CommandLineArg)); list->args = (struct CommandLineArg*) cdo_malloc(capacity * sizeof(struct CommandLineArg));
list->size = 0; list->size = 0;
list->capacity = capacity; list->capacity = capacity;
return list; return list;
@ -82,24 +95,24 @@ void append_arg(struct ArgList* list, struct CommandLineArg* arg) {
list->capacity *= 2; list->capacity *= 2;
list->args = list->args =
(struct CommandLineArg*) (struct CommandLineArg*)
realloc( cdo_realloc(
list->args, list->args,
list->capacity * sizeof(struct CommandLineArg) list->capacity * sizeof(struct CommandLineArg)
); );
} }
memcpy(list->args + list->size, arg, sizeof(struct CommandLineArg)); memcpy(list->args + list->size, arg, sizeof(struct CommandLineArg));
free(arg); cdo_free(arg);
list->size += 1; list->size += 1;
} }
void free_arg_list(struct ArgList* list) { void free_arg_list(struct ArgList* list) {
for (size_t i = 0; i < ARG_COUNT; i++) { for (size_t i = 0; i < ARG_COUNT; i++) {
free(list->args[i].name); cdo_free(list->args[i].name);
free(list->args[i].description); cdo_free(list->args[i].description);
free(list->args[i].value); cdo_free(list->args[i].value);
} }
free(list->args); cdo_free(list->args);
free(list); cdo_free(list);
} }

View File

@ -10,6 +10,7 @@
typedef enum { typedef enum {
ARG_PROJECT, ARG_PROJECT,
ARG_HELP,
ARG_COUNT ARG_COUNT
} ArgName; } ArgName;

View File

@ -0,0 +1,53 @@
#ifndef __ALLOC_WARPPERS_H__
#define __ALLOC_WARPPERS_H__
#include "log.h"
#include <stdlib.h>
#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__*/

View File

@ -1,12 +1,13 @@
#include "buffer.h" #include "buffer.h"
#include "alloc_wrappers.h"
#include "log.h" #include "log.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
Buffer* buffer_create(size_t capacity) { Buffer* buffer_create(size_t capacity) {
Buffer* buffer = (Buffer*)malloc(sizeof(Buffer)); Buffer* buffer = (Buffer*)cdo_malloc(sizeof(Buffer));
buffer->data = (char*)malloc(capacity); buffer->data = (char*)cdo_malloc(capacity);
buffer->size = 0; buffer->size = 0;
buffer->capacity = capacity; buffer->capacity = capacity;
return buffer; return buffer;
@ -16,7 +17,7 @@ void buffer_append(Buffer* buffer, const char* str) {
size_t len = strlen(str); size_t len = strlen(str);
if (buffer->size + len >= buffer->capacity) { if (buffer->size + len >= buffer->capacity) {
buffer->capacity *= 2; 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); strcpy(buffer->data + buffer->size, str);
buffer->size += len; buffer->size += len;
@ -31,9 +32,9 @@ void buffer_free(Buffer* buffer) {
if (!buffer->data || !buffer) { if (!buffer->data || !buffer) {
LOG_WARNING("Trying to free NULL pointer"); LOG_WARNING("Trying to free NULL pointer");
} }
free(buffer->data); cdo_free(buffer->data);
buffer->data = NULL; buffer->data = NULL;
free(buffer); cdo_free(buffer);
buffer = NULL; buffer = NULL;
} }