Something went wrong on our end
cli.c 1.72 KiB
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include "cli.h"
int connectToBackEnd();
int main(int argc, char **argv)
{
int cmdID = -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;
}
}
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];
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
printf("Trying to connect...\n");
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, sizeof(remote)) == -1) {
perror("connect");
exit(1);
} else {
printf("Connected.\n");
return s;
}
}