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

simplifying and finishing command parsing

parent df6006c1
No related branches found
No related tags found
No related merge requests found
......@@ -44,3 +44,4 @@ BackEnd
obj
cli
monitorQuad
testClient
......@@ -6,36 +6,67 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <err.h>
#include "cli.h"
int parseInput(int argc, char **argv);
int connectToBackEnd();
int main(int argc, char **argv)
{
int cmdID = -1;
if((cmdID = parseInput(argc, argv)) == -1) {
exit(1);
char * command;
int i , s, useSymlink = 0;
command = argv[0];
for(i = 0; i < MAX_COMMANDS; ++i) {
if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0)
{
cmdID = i;
useSymlink = 1;
}
}
int s;
if(cmdID == -1) {
command = argv[1];
for(i = 0; i < MAX_COMMANDS; ++i) {
if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0)
{
cmdID = i;
}
}
}
if(cmdID == -1){
printf("Could not match input with a command. Please try again...\n");
return -1;
}
printf("Parsed Command : %s\n", commandNames[cmdID]);
if((s = connectToBackEnd()) == -1) {
err(-1, "connectToBackEnd");
}
printf("connection to backend Successful\n");
if(useSymlink) {
//TODO Call correct command function pointer with (argv[1] ... argc[argc])
}else {
//TODO Call correct command function pointer with (argv[2] ... argv[argc])
}
close(s);
return 0;
}
int connectToBackEnd() {
int s, t, len;
struct sockaddr_un remote;
char str[100];
......@@ -57,35 +88,4 @@ int connectToBackEnd() {
printf("Connected.\n");
return s;
}
}
int parseInput(int argc, char **argv) {
static int cmdID = -1;
int c;
static struct option long_options[] =
{
/* These options don’t set a flag. We distinguish them by their indices. */
{"monitor", no_argument, &cmdID, CMD_MONITOR},
{0, 0, 0, 0}
};
while (1)
{
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
}
if(cmdID == -1)
{
printf("Invalid command\n");
return cmdID;
}
return 0;
}
\ No newline at end of file
......@@ -5,11 +5,17 @@
enum CommandNameIds{
CMD_MONITOR,
CMD_SETPID,
CMD_GETPID,
CMD_GETImu,
MAX_COMMANDS
};
char* commandNames[MAX_COMMANDS] = {
static char* commandNames[MAX_COMMANDS] = {
"monitor",
"setPid",
"getPid",
"getImu"
};
#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