From b0d4cccc214d9b0438e26794059a320dec178b0c Mon Sep 17 00:00:00 2001
From: burneykb <burneykb@iastate.edu>
Date: Sun, 26 Mar 2017 08:18:32 -0500
Subject: [PATCH] string options work for all commands but setsource

---
 groundStation/src/cli/cli.c        | 106 +++++++++++++++++++++++++++--
 groundStation/src/cli/cli.h        |  36 ++++++++++
 groundStation/src/cli/cli_nodes.c  |   8 +--
 groundStation/src/cli/cli_output.c |   9 +--
 groundStation/src/cli/cli_param.c  |  15 ++--
 groundStation/src/cli/cli_param.h  |  80 ----------------------
 groundStation/src/cli/cli_source.c |   5 +-
 7 files changed, 157 insertions(+), 102 deletions(-)

diff --git a/groundStation/src/cli/cli.c b/groundStation/src/cli/cli.c
index 5aa308290..ebde95590 100644
--- a/groundStation/src/cli/cli.c
+++ b/groundStation/src/cli/cli.c
@@ -52,9 +52,8 @@ int main(int argc, char **argv)
 	// recognize which cli command the user has entered
 	if (cmdID == -1) {
 		command = argv[1];
-		for(i = 0; i < MAX_COMMANDS; ++i) {
-			if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0)
-			{
+		for (i = 0; i < MAX_COMMANDS; ++i) {
+			if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0) {
 				cmdID = i;
 			}	
 		}
@@ -100,7 +99,7 @@ int main(int argc, char **argv)
 	// Call the appropriate function
 	if (useSymlink) {
 		(*cli_functions[cmdID]) (conn, argc, &argv[0]);
-	}else {
+	} else {
 		(*cli_functions[cmdID]) (conn, argc-1, &argv[1]);
 	}
 
@@ -111,7 +110,6 @@ int main(int argc, char **argv)
     return 0;
 }
 
-
 /* This function is called by other cli functions to check for a help condition */
 int help_check(int argc, char ** argv) {
 	int c;
@@ -138,4 +136,102 @@ int help_check(int argc, char ** argv) {
 		}
 	}
 	return needHelp;
+}
+
+int isNumber(char *number) {
+    int i = 0;
+    //checking for negative numbers
+    if (number[0] == '-')
+        i = 1;
+    for (; number[i] != 0; i++)
+    {
+        //if (number[i] > '9' || number[i] < '0')
+        if (!isdigit(number[i]))
+            return 0;
+    }
+    return 1;
+}
+
+int convert_to_id(struct backend_conn * conn, char **argv, struct convert_data * convert_data, int conversion_requirement) {
+	/* variables used to search for id values */
+	size_t num_nodes = 0, i;
+	struct frontend_node_data* search_data;
+	int search_1 = 0, search_2 = 0;
+	const struct graph_node_type * node_definition;
+
+		
+
+	if (!isNumber(argv[1])) {
+		search_1 = 1;
+	} else {
+		convert_data->val_1 = atoi(argv[1]);	
+	}
+
+	if (!isNumber(argv[2])) {
+		search_2 = 1;
+	} else {
+		convert_data->val_2 = atoi(argv[2]);
+	}
+
+	if (!search_1 && !search_2) {
+		return 0;
+	}
+
+	search_data = malloc(sizeof((*search_data)) * num_nodes);
+
+	if (frontend_getnodes(conn, &search_data, &num_nodes)) {
+		return 1;
+	}
+
+	if (search_1) {
+		for (i = 0; i < num_nodes; ++i) {
+			if (strncasecmp(search_data[i].name, argv[1], strlen(search_data[i].name)) == 0) {
+				convert_data->val_1 = search_data[i].block;
+				node_definition = blockDefs[search_data[i].type];
+				break;
+			}
+		}
+	}
+
+	if (i == num_nodes)
+		return 1;
+
+	if (search_2) {
+		const int  *num_s2_options;
+		const char* const* s2_options_arr;
+
+		if (!search_1) {
+			node_definition = blockDefs[search_data[convert_data->val_1].type];
+		}
+		
+		switch (conversion_requirement) {
+			case PARAM:
+				num_s2_options = &node_definition->n_params;
+				s2_options_arr = node_definition->param_names;
+				break;
+			case INPUT:
+				num_s2_options = &node_definition->n_inputs;
+				s2_options_arr = node_definition->input_names;
+				break;
+			case OUTPUT:
+				num_s2_options = &node_definition->n_outputs;
+				s2_options_arr = node_definition->output_names;
+				break;
+		}
+
+		for (i = 0; i < (size_t)*num_s2_options; ++i) {
+			if (strncasecmp(s2_options_arr[i], 
+					argv[2], 
+					strlen(s2_options_arr[i])) == 0) {
+				convert_data->val_2 = i;
+				search_2 = 0;
+				break;
+			}
+		}
+	}
+	for(i = 0; i < num_nodes; ++i) {
+		free(search_data[i].name);
+	}
+	free(search_data);
+	return search_2;
 }
\ No newline at end of file
diff --git a/groundStation/src/cli/cli.h b/groundStation/src/cli/cli.h
index 7a7d4fdf8..8a1420102 100644
--- a/groundStation/src/cli/cli.h
+++ b/groundStation/src/cli/cli.h
@@ -40,7 +40,43 @@ static char* commandNames[MAX_COMMANDS] = {
 	"getnodes",
 	"addnode"
 };
+
+/**
+ * generic two short struct for use with 'convert_to_id()'
+ */
+struct convert_data {
+	int16_t val_1;
+	int16_t val_2;
+};
+
+enum conversion_requirements {
+	INPUT,
+	PARAM,
+	OUTPUT
+};
+/**
+ * isNumber takes a char * such as argv[n] and checks to see if it is a number.
+ * @param  number  null terminating string (ex. argv[1])
+ * @return         1 if 'number' is a number
+ *                 0 if 'number' is not a number 
+ */
+int isNumber(char *number);
+
+/**
+ * convert_to_id will take in the convert_data struct and either use the values from argv or 
+ * 	find the correct values by using the optional strings passed in from argv
+ * @param  conn         backend connection struct
+ * @param  argv         argv from the cmd line
+ * @param  convert_data saves the necissary converted data in this struct for later use.
+ * @return             	returns an error integer
+ */
+int convert_to_id(struct backend_conn * conn, char **argv, struct convert_data * convert_data, int conversion_requirement);
+
 /* This function is called by other cli functions to check for a help condition */
+/**
+ * determine if the user asked for help
+ * @return      1 if help is needed, else 0.
+ */
 int help_check(int argc, char ** argv);
 
 #endif /* _CLI_H */
\ No newline at end of file
diff --git a/groundStation/src/cli/cli_nodes.c b/groundStation/src/cli/cli_nodes.c
index 341a8f059..281ee6fb6 100644
--- a/groundStation/src/cli/cli_nodes.c
+++ b/groundStation/src/cli/cli_nodes.c
@@ -37,11 +37,11 @@ int cli_getnodes(struct backend_conn * conn, int argc, char ** argv) {
 		return 1;
 	}
 
-	printf("------------------------------------------------\n");
+	printf("--------------------------------------------------------------------------------------------------------------\n");
 	printf("The following %lu Nodes have been found:\n", num_nodes);
-	printf("------------------------------------------------\n");
-	printf("\tBLOCK\t\tTYPE\t\tNAME\n");
-	printf("------------------------------------------------\n");
+	printf("--------------------------------------------------------------------------------------------------------------\n");
+	printf("\tBLOCK\t\tTYPE\t\tNAME\t\tINPUTS\t\tPARAMS\t\tOUTPUTS\n");
+	printf("--------------------------------------------------------------------------------------------------------------\n");
 	for(i = 0; i < num_nodes; ++i) {
 		printf("\t%3d\t\t%3d\t\t%s\t", node_data[i].block, node_data[i].type, node_data[i].name);
 		
diff --git a/groundStation/src/cli/cli_output.c b/groundStation/src/cli/cli_output.c
index 273b749c3..9f4a5fb5f 100644
--- a/groundStation/src/cli/cli_output.c
+++ b/groundStation/src/cli/cli_output.c
@@ -12,8 +12,8 @@ int cli_getoutput(struct backend_conn * conn, int argc, char ** argv) {
 
 	if ((needHelp = help_check(argc, argv))) {
 		printf("getoutput gets the output value of a specified block_id and output_id\n");
-		printf("Usage Syntax : \n\t./Cli getoutput block_id output_id\n");
-		printf("Symlink Usage Syntax : \n\t./getoutput block_id output_id\n\n");
+		printf("Usage Syntax : \n\t./Cli getoutput <block_id|'block_name'> <output_id|'output_name'>\n");
+		printf("Symlink Usage Syntax : \n\t./getoutput <block_id|'block_name'> <output_id|'output_name'>\n\n");
 		return 0;
 	}
 
@@ -22,8 +22,9 @@ int cli_getoutput(struct backend_conn * conn, int argc, char ** argv) {
 		return 1;
 	}
 
-	output_data.block = atoi(argv[1]);
-	output_data.output = atoi(argv[2]);
+	if (convert_to_id(conn, argv, (struct convert_data *)&output_data, OUTPUT)) {
+		return 1;
+	}
 
 	if (frontend_getoutput(conn, &output_data)) {
 		return 1;
diff --git a/groundStation/src/cli/cli_param.c b/groundStation/src/cli/cli_param.c
index 4cc50b3f6..4b3b802c4 100644
--- a/groundStation/src/cli/cli_param.c
+++ b/groundStation/src/cli/cli_param.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <inttypes.h>
 
@@ -12,8 +13,8 @@ int cli_getparam(struct backend_conn * conn, int argc, char ** argv) {
 
 	if ((needHelp = help_check(argc, argv))) {
 		printf("getparam gets the param_val for a specified block_id and param_id\n");
-		printf("Usage Syntax : \n\t./Cli getparam block_id param_id\n");
-		printf("Symlink Usage Syntax : \n\t./getparam block_id param_id\n\n");
+		printf("Usage Syntax : \n\t./Cli getparam <block_id|'block_name'> <param_id|'param_name'>\n");
+		printf("Symlink Usage Syntax : \n\t./getparam <block_id|'block_name'> <param_id|'param_name'>\n\n");
 		return 0;
 	}
 
@@ -22,7 +23,7 @@ int cli_getparam(struct backend_conn * conn, int argc, char ** argv) {
 		return 1;
 	}
 
-	if (convert_to_id(conn, argv, &param_data)) {
+	if (convert_to_id(conn, argv, (struct convert_data *)&param_data, PARAM)) {
 		return 1;
 	}
 	
@@ -44,8 +45,8 @@ int cli_setparam(struct backend_conn * conn, int argc, char ** argv) {
 
 	if ((needHelp = help_check(argc, argv))) {
 		printf("setparam sets the param_val for a specified block_id and param_id\n");
-		printf("Usage Syntax : \n\t./Cli setparam block_id param_id value\n");
-		printf("Symlink Usage Syntax : \n\t./setparam block_id param_id value\n\n");
+		printf("Usage Syntax : \n\t./Cli setparam <block_id|'block_name'> <param_id|'param_name'> value\n");
+		printf("Symlink Usage Syntax : \n\t./setparam <block_id|'block_name'> <param_id|'param_name'> value\n\n");
 		return 0;
 	}
 
@@ -55,11 +56,11 @@ int cli_setparam(struct backend_conn * conn, int argc, char ** argv) {
 	}
 
 
-	if (convert_to_id(conn, argv, &param_data)) {
+	if (convert_to_id(conn, argv, (struct convert_data *)&param_data, PARAM)) {
 		return 1;
 	}
 	
-	param_data.value = atoi(argv[3]);
+	param_data.value = atof(argv[3]);
 	
 	if (frontend_setparam(conn, &param_data)) {
 		return 1;
diff --git a/groundStation/src/cli/cli_param.h b/groundStation/src/cli/cli_param.h
index f80b78292..b48f033b8 100644
--- a/groundStation/src/cli/cli_param.h
+++ b/groundStation/src/cli/cli_param.h
@@ -8,86 +8,6 @@
 #include "frontend_nodes.h"
 #include "graph_blocks.h"
 
-
-static int isNumber(char *number)
-{
-    int i = 0;
-    //checking for negative numbers
-    if (number[0] == '-')
-        i = 1;
-    for (; number[i] != 0; i++)
-    {
-        //if (number[i] > '9' || number[i] < '0')
-        if (!isdigit(number[i]))
-            return 0;
-    }
-    return 1;
-}
-
-static int convert_to_id(struct backend_conn * conn, char **argv, struct frontend_param_data * param_data) {
-	/* variables used to search for id values */
-	size_t num_nodes = 0, i;
-	struct frontend_node_data* search_data;
-	int search_for_block = 0, search_for_param = 0;
-	const struct graph_node_type * node_definition;
-		
-
-	if (!isNumber(argv[1])) {
-		search_for_block = 1;
-	} else {
-		param_data->block = atoi(argv[1]);	
-	}
-
-	if (!isNumber(argv[2])) {
-		search_for_param = 1;
-	} else {
-		param_data->param = atoi(argv[2]);
-	}
-
-	if (!search_for_block && !search_for_param) {
-		return 0;
-	}
-
-	search_data = malloc(sizeof((*search_data)) * num_nodes);
-
-	if (frontend_getnodes(conn, &search_data, &num_nodes)) {
-		return 1;
-	}
-
-	if (search_for_block) {
-		for (i = 0; i < num_nodes; ++i) {
-			if (strncmp(search_data[i].name, argv[1], strlen(search_data[i].name)) == 0) {
-				param_data->block = search_data[i].block;
-				node_definition = blockDefs[search_data[i].type];
-				break;
-			}
-		}
-	}
-
-	if (i == num_nodes)
-		return 1;
-
-	if (search_for_param) {
-		if (!search_for_block) {
-			node_definition = blockDefs[search_data[param_data->block].type];
-		}
-		for (i = 0; i < (size_t)node_definition->n_params; ++i) {
-			if (strncmp(node_definition->param_names[i], 
-					argv[2], 
-					strlen(node_definition->param_names[i])) == 0) {
-				param_data->param = i;
-				search_for_param = 0;
-				break;
-			}
-		}
-	}
-	for(i = 0; i < num_nodes; ++i) {
-		free(search_data[i].name);
-	}
-	free(search_data);
-	return search_for_param;
-}
-
 int cli_getparam(struct backend_conn * conn, int argc, char ** argv);
 int cli_setparam(struct backend_conn * conn, int argc, char ** argv);
 
diff --git a/groundStation/src/cli/cli_source.c b/groundStation/src/cli/cli_source.c
index c7168a6fb..214174224 100644
--- a/groundStation/src/cli/cli_source.c
+++ b/groundStation/src/cli/cli_source.c
@@ -22,8 +22,9 @@ int cli_getsource(struct backend_conn * conn, int argc, char ** argv) {
 		return 1;
 	}
 	
-	source_data.dst_block = atoi(argv[1]);
-	source_data.dst_input = atoi(argv[2]);
+	if (convert_to_id(conn, argv, (struct convert_data *)&source_data, INPUT)) {
+		return 1;
+	}
 
 	if (frontend_getsource(conn, &source_data)) {
 		return 1;
-- 
GitLab