From a38c3a8253dabf35d437ffd5ae48214cf776a96e Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 26 Oct 2016 21:13:32 -0500 Subject: [PATCH] building the init command struct --- cmd/ipfs/init.c | 11 +++++++++-- cmd/ipfs/init.h | 12 ++++++++++++ commands/argument.c | 28 +++++++++++++++++++++------- commands/argument.h | 1 + commands/command.h | 1 + test/cmd/ipfs/test_init.h | 28 +++++++++++++++++++++------- 6 files changed, 65 insertions(+), 16 deletions(-) diff --git a/cmd/ipfs/init.c b/cmd/ipfs/init.c index 99dc8a9..a9c414c 100644 --- a/cmd/ipfs/init.c +++ b/cmd/ipfs/init.c @@ -7,9 +7,16 @@ int get_init_command(struct command* cmd) { // arguments // initialize an array of arguments with size of 1 struct argument argument; - init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration"); + int retVal = init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration"); argument.enable_stdin = 1; - struct argument* array[1] = { &argument }; + struct argument* array[] = { &argument }; cmd->arguments = array; + cmd->argument_count = 1; + return retVal; +} + +int uninit_command(struct command* cmd) { + for(int i = 0; i < cmd->argument_count; i++) + uninit_argument(cmd->arguments[i]); return 0; } diff --git a/cmd/ipfs/init.h b/cmd/ipfs/init.h index 335f211..7f8ef21 100644 --- a/cmd/ipfs/init.h +++ b/cmd/ipfs/init.h @@ -6,6 +6,18 @@ #include "../../commands/command.h" +/*** + * Returns a command structure customized for the init command + * @param command the struct to fill + * @returns 0 on failure, otherwise 1 + */ int get_init_command(struct command* command); +/*** + * Uninitializes all the dynamic memory caused by get_init_command + * @param command the struct + * @returns 0 on failure, otherwise 1 + */ +int uninit_command(struct command* command); + #endif diff --git a/commands/argument.c b/commands/argument.c index 38506be..2aeaec7 100644 --- a/commands/argument.c +++ b/commands/argument.c @@ -1,21 +1,35 @@ +#include +#include + #include "argument.h" +int uninit_argument(struct argument* argument) { + free(argument->name); + return 1; +} + int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) { + argument->name = name == NULL ? NULL : strdup(name); + if (argument->name == NULL) + return 0; + strncpy(argument->name, name, strlen(name)); argument->name = name; argument->required = required; argument->variadic = variadic; argument->description = description; - return 0; + return 1; } int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description) { - init_argument(argument, name, required, variadic, description); - argument->type = string; - return 0; + int retVal = init_argument(argument, name, required, variadic, description); + if (retVal) + argument->type = string; + return retVal; } int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description) { - init_argument(argument, name, required, variadic, description); - argument->type = file; - return 0; + int retVal = init_argument(argument, name, required, variadic, description); + if (retVal) + argument->type = file; + return retVal; } diff --git a/commands/argument.h b/commands/argument.h index 13b4611..cd19956 100644 --- a/commands/argument.h +++ b/commands/argument.h @@ -15,6 +15,7 @@ struct argument { }; int init_argument(struct argument* argument, char* name, int required, int variadic, char* description); +int uninit_argument(struct argument* argument); int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description); int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description); diff --git a/commands/command.h b/commands/command.h index 1e354de..70e3220 100644 --- a/commands/command.h +++ b/commands/command.h @@ -23,6 +23,7 @@ struct help_text { struct command { //struct option* options; struct argument** arguments; + int argument_count; //int (*pre_run)(struct request*); //int (*run)(struct request*); //int (*post_run)(struct request*); diff --git a/test/cmd/ipfs/test_init.h b/test/cmd/ipfs/test_init.h index e7cc339..055faa0 100644 --- a/test/cmd/ipfs/test_init.h +++ b/test/cmd/ipfs/test_init.h @@ -5,31 +5,45 @@ #define __TEST_INIT_H__ #include "../../../cmd/ipfs/init.h" +#include "../../../commands/argument.h" #include #include int test_get_init_command() { struct command cmd = { 0 }; - + int retVal = 1; // make sure its empty if (cmd.help_text.tagline != NULL) { fprintf(stderr, "short description should be null\n"); return 0; } // grab the stuff - get_init_command(&cmd); + retVal = get_init_command(&cmd); + if (!retVal) { + fprintf(stderr, "Function call to get_init_command not successful. Return was %d\n", retVal); + return retVal; + } // make sure its right if (cmd.help_text.tagline == NULL) { fprintf(stderr, "short description is null\n"); - return 0; - } - if (strcmp(cmd.help_text.tagline, "Initializes IPFS config file.") != 0) { + retVal = 0; + } else if (strcmp(cmd.help_text.tagline, "Initializes IPFS config file.") != 0) { fprintf(stderr, "short description is not null\n"); - return 0; + retVal = 0; + } else if (cmd.argument_count != 1) { + fprintf(stderr, "argument count should be 1"); + retVal = 0; + } else { + struct argument arg1 = *(cmd.arguments[0]); + if (strncmp(arg1.name, "default-config", 14) != 0) { + fprintf(stderr, "arg1 wrong name. Expected %s but got %s\n", "default_config", arg1.name); + retVal = 0; + } } - return 1; + uninit_command(&cmd); + return retVal; } #endif