Skip to content
Snippets Groups Projects
Commit c4d5cbdf authored by burneykb's avatar burneykb
Browse files

Fixed null argv segfault and cli.c can handle --help option

parent e6ab8f52
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <err.h> #include <err.h>
#include <libgen.h> #include <libgen.h>
#include <getopt.h>
#include "cli.h" #include "cli.h"
...@@ -9,9 +10,20 @@ int main(int argc, char **argv) ...@@ -9,9 +10,20 @@ int main(int argc, char **argv)
{ {
int cmdID = -1; int cmdID = -1;
char * command; char * command;
int c;
int i , useSymlink = 0; int i , useSymlink = 0;
struct backend_conn *conn; struct backend_conn *conn;
static int needHelp = 0;
static struct option long_options[] = {
/* These options don’t set a flag. We distinguish them by their indices. */
{"help", no_argument, &needHelp, 1},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "", long_options, &option_index);
// Determine if the cli was called using a symlink
command = basename(argv[0]); command = basename(argv[0]);
for(i = 0; i < MAX_COMMANDS; ++i) { for(i = 0; i < MAX_COMMANDS; ++i) {
if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0) if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0)
...@@ -21,6 +33,28 @@ int main(int argc, char **argv) ...@@ -21,6 +33,28 @@ int main(int argc, char **argv)
} }
} }
// Verify the user has entered enough information to continue
if(argc < 2 && !useSymlink) {
printf("Incorrect usage :\n");
printf("\n\tUsage : ./Cli command [options] \n");
printf("\tFor a list of available commands run ./Cli --help\n\n");
printf("\tFor a list of available options for a command run ./Cli command --help\n");
return -1;
}
// If the user runs './Cli help' , provide the user with a list of commands available.
if(needHelp) {
printf("Usage : ./Cli command [options]\n");
printf("For a list of available options for a command run ./Cli command --help\n\n");
printf("Available commands include the following\n");
for(int i = 0; i < MAX_COMMANDS; ++i) {
printf("\t '%s'\n",commandNames[i]);
}
return 0;
}
// recognize which cli command the user has entered
if(cmdID == -1) { if(cmdID == -1) {
command = argv[1]; command = argv[1];
for(i = 0; i < MAX_COMMANDS; ++i) { for(i = 0; i < MAX_COMMANDS; ++i) {
...@@ -32,22 +66,41 @@ int main(int argc, char **argv) ...@@ -32,22 +66,41 @@ int main(int argc, char **argv)
} }
if(cmdID == -1){ if(cmdID == -1){
printf("Could not match input with a command. Please try again...\n"); printf("Could not match '%s' with a command. Please try again...\n", command);
printf("For help running the program, run ./Cli --help\n");
return -1; return -1;
} }
printf("Parsed Command : %s\n", commandNames[cmdID]); printf("Parsed Command : %s\n", commandNames[cmdID]);
conn = ucart_backendConnect();
if(conn == NULL) { /**
return -1; * I the user has asked for help, and we have already found
* the command that they are trying to use. Then we need
* to be able to provide the help info wihtout the
* requirement of the backend
*
* It is important to note that this will only work if the
* command the user has asked for handles this help case
*
*/
// Only require the backend if we will need to use it.
if(!needHelp) {
// Create the connection to the backend
conn = ucart_backendConnect();
if(conn == NULL) {
return -1;
}
} }
// Call the appropriate function
if(useSymlink) { if(useSymlink) {
(*cli_functions[cmdID]) (conn, argc, &argv[0]); (*cli_functions[cmdID]) (conn, argc, &argv[0]);
}else { }else {
(*cli_functions[cmdID]) (conn, argc-1, &argv[1]); (*cli_functions[cmdID]) (conn, argc-1, &argv[1]);
} }
// Disconnect from the backend
ucart_backendDisconnect(conn); ucart_backendDisconnect(conn);
return 0; return 0;
} }
\ No newline at end of file
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
#include "cli_monitor.h" #include "cli_monitor.h"
#include "cli_setpid.h" #include "cli_setpid.h"
#include "cli_getpid.h" #include "cli_getpid.h"
// #include "cli_getimu.h" #include "cli_getimu.h"
enum CommandNameIds{ enum CommandNameIds{
CMD_MONITOR, CMD_MONITOR,
CMD_GETPID, CMD_GETPID,
...@@ -17,16 +18,14 @@ typedef int (*cli_function_ptr)(struct backend_conn *, int, char **); ...@@ -17,16 +18,14 @@ typedef int (*cli_function_ptr)(struct backend_conn *, int, char **);
static cli_function_ptr cli_functions[] = { static cli_function_ptr cli_functions[] = {
&cli_monitor, &cli_monitor,
&cli_getpid, &cli_getpid,
&cli_setpid &cli_setpid,
//&cli_getimu &cli_getimu
}; };
static char* commandNames[MAX_COMMANDS] = { static char* commandNames[MAX_COMMANDS] = {
"monitor", "monitor",
"getpid", "getpid",
"setpid", "setpid",
"getImu" "getimu"
}; };
#endif #endif
#include <stdio.h>
#include "cli_getimu.h"
int cli_getimu(struct backend_conn * conn, int argc, char ** argv) {
printf("This functionality has not been added yet\n");
return 0;
}
#ifndef CLI_GETIMU_H #ifndef CLI_GETIMU_H
#define CLI_GETIMU_H #define CLI_GETIMU_H
#include "frontend_getimu.h" #include "frontend_getimu.h"
int cli_getimu( int cli_getimu(struct backend_conn * conn, int argc, char ** argv);
struct backend_conn * conn,
struct frontend_pid_data * pid_data);
#endif #endif
\ No newline at end of file
...@@ -17,7 +17,6 @@ int cli_monitor(struct backend_conn * conn, int argc, char **argv) { ...@@ -17,7 +17,6 @@ int cli_monitor(struct backend_conn * conn, int argc, char **argv) {
int count, rate = 10; int count, rate = 10;
int forever = 0; int forever = 0;
while ((c = getopt(argc, argv, "fc:r:")) != -1) { while ((c = getopt(argc, argv, "fc:r:")) != -1) {
switch(c) { switch(c) {
case 'c' : case 'c' :
......
#ifndef FRONTEND_GETIMU_H
#define FRONTEND_GETIMU_H
#include "frontend_common.h"
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment