From d44aee855f7b4a6838ff9149c2fb84b2e2b720ca Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Mon, 18 Jan 2021 12:38:46 +1000 Subject: [PATCH] * f2b_cmd_{create,destroy}() --- src/commands.c | 26 ++++++++++++++++++++++++++ src/commands.h | 14 ++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/commands.c b/src/commands.c index 765b70f..f42add1 100644 --- a/src/commands.c +++ b/src/commands.c @@ -116,6 +116,32 @@ f2b_cmd_help() { return help; } +f2b_cmd_t * +f2b_cmd_create(const char *line) { + f2b_cmd_t *cmd = NULL; + + assert(line != NULL); + + if ((cmd = calloc(1, sizeof(f2b_cmd_t))) == NULL) + return NULL; + + if (f2b_buf_alloc(&cmd->data, strlen(line))) { + if (f2b_cmd_parse(cmd, line)) + return cmd; + free(cmd); + cmd = NULL; + } + + + return cmd; +} + +void +f2b_cmd_destroy(f2b_cmd_t *cmd) { + f2b_buf_free(&cmd->data); + free(cmd); +} + bool f2b_cmd_parse(f2b_cmd_t *cmd, const char *src) { char *p = NULL; diff --git a/src/commands.h b/src/commands.h index 194ea39..2327ca1 100644 --- a/src/commands.h +++ b/src/commands.h @@ -51,6 +51,20 @@ typedef struct f2b_cmd_t { const char * f2b_cmd_help(); +/** + * @brief Creates new struct from user input + * @param line User input string + * @returns pointer to newly allocated struct or NULL on malloc()/parse error + */ +f2b_cmd_t * +f2b_cmd_create(const char *line); + +/** + * @brief Frees memory + */ +void +f2b_cmd_destroy(f2b_cmd_t *cmd); + /** * @brief Try to parse user input * @param cmd pointer to preallocated f2b_cmd_t struct