Forked from
Distributed Autonomous Networked Control Lab / MicroCART
1869 commits behind, 84 commits ahead of the upstream repository.
cli.c 1.68 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 <getopt.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);
}
int s;
if((s = connectToBackEnd()) == -1) {
err(-1, "connectToBackEnd");
}
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;
}
}
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;
}