building the init command struct

xethyrion-master
jmjatlanta 2016-10-26 21:13:32 -05:00
parent 1d324cf391
commit a38c3a8253
6 changed files with 65 additions and 16 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -1,21 +1,35 @@
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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);

View File

@ -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*);

View File

@ -5,31 +5,45 @@
#define __TEST_INIT_H__
#include "../../../cmd/ipfs/init.h"
#include "../../../commands/argument.h"
#include <stdio.h>
#include <string.h>
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