Started work on argument parsing.
This commit is contained in:
parent
f738e05048
commit
8473c40c36
|
@ -0,0 +1,58 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
void help()
|
||||
{
|
||||
char * help = "Usage: partager [OPTIONS] ip\n"
|
||||
"Straight forward communication over the internet\n"
|
||||
"\nOptions:\n"
|
||||
"";
|
||||
printf("%s\n", help);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
partresult parseArgs(int argc, char ** argv, State * state)
|
||||
{
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
if (*argv[i] == '-')
|
||||
{
|
||||
char * value = argv[i + 1];
|
||||
|
||||
if (!strcmp("-p", argv[i]) ||
|
||||
!strcmp("--port", argv[i])
|
||||
)
|
||||
{
|
||||
state->conf.port = atoi(value);
|
||||
goto skip;
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
skip:
|
||||
++i;
|
||||
} else
|
||||
{
|
||||
if (!strcmp(argv[i], "server"))
|
||||
{
|
||||
state->conf.mode = PARTSERVER;
|
||||
}
|
||||
else if (!strcmp(argv[i], "client"))
|
||||
{
|
||||
state->conf.mode = PARTCLIENT;
|
||||
}
|
||||
else if (i == argc - 1) //TODO: replace with proper ip address validation and add domain support
|
||||
{
|
||||
strncpy(state->conf.ip, argv[i], IPADDRLEN);
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef ARGS_H_
|
||||
#define ARGS_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
void help();
|
||||
partresult parseArgs(int argc, char ** argv, State * state);
|
||||
|
||||
#endif // ARGS_H_
|
|
@ -0,0 +1,14 @@
|
|||
#include "common.h"
|
||||
|
||||
State createDefaultState()
|
||||
{
|
||||
return (State) {
|
||||
.term = {0},
|
||||
.conf = {
|
||||
"",
|
||||
0,
|
||||
PARTMODEINVALID,
|
||||
},
|
||||
.messages = daCreate(char *, 64),
|
||||
};
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "term.h"
|
||||
|
||||
#include "da.h"
|
||||
|
||||
#define IPADDRLEN 15
|
||||
|
||||
typedef int fd_t;
|
||||
|
||||
typedef enum OperationMode {
|
||||
PARTMODEINVALID = 0,
|
||||
PARTSERVER = 1,
|
||||
PARTCLIENT = 2,
|
||||
PARTMODECOUNT = 3,
|
||||
} OperationMode;
|
||||
|
||||
typedef struct Config {
|
||||
char ip[IPADDRLEN];
|
||||
uint16_t port;
|
||||
OperationMode mode;
|
||||
} Config;
|
||||
|
||||
typedef struct State {
|
||||
Term term;
|
||||
Config conf;
|
||||
dynarr(char *) messages;
|
||||
} State;
|
||||
|
||||
State createDefaultState();
|
||||
|
||||
typedef int partresult; // TODO: Implement proper error reporting system
|
||||
|
||||
#endif // COMMON_H_
|
50
main.c
50
main.c
|
@ -18,7 +18,9 @@
|
|||
#include <bits/pthreadtypes.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "args.h"
|
||||
#include "term.h"
|
||||
#include "common.h"
|
||||
|
||||
#define DA_IMPLEMENTATION
|
||||
#include "da.h"
|
||||
|
@ -32,12 +34,12 @@ enum {
|
|||
THREAD_COUNT = 2,
|
||||
};
|
||||
|
||||
typedef int fd_t;
|
||||
|
||||
void * sender(void * arg)
|
||||
{
|
||||
fd_t * connFd = arg;
|
||||
|
||||
(void) connFd;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -122,14 +124,9 @@ void serve()
|
|||
close(sockFd);
|
||||
}
|
||||
|
||||
typedef struct State {
|
||||
Term term;
|
||||
dynarr(char *) messages;
|
||||
} State;
|
||||
|
||||
void draw(State * state)
|
||||
{
|
||||
int x, y;
|
||||
size_t x, y;
|
||||
getmaxyx(stdscr, y, x);
|
||||
if (state->term.termX != x ||
|
||||
state->term.termY != y)
|
||||
|
@ -172,7 +169,7 @@ void loop(State * state)
|
|||
daBzero(buffer);
|
||||
state->term.inputX = 1;
|
||||
|
||||
for (size_t i = 1; i < getmaxx(state->term.input) - 1; ++i)
|
||||
for (int i = 1; i < getmaxx(state->term.input) - 1; ++i)
|
||||
mvwprintw(state->term.input, state->term.inputY, i, " ");
|
||||
|
||||
break;
|
||||
|
@ -189,6 +186,7 @@ void loop(State * state)
|
|||
|
||||
case KEY_UP:
|
||||
case KEY_DOWN:
|
||||
case KEY_RESIZE:
|
||||
break;
|
||||
|
||||
case KEY_BACKSPACE:
|
||||
|
@ -207,9 +205,6 @@ void loop(State * state)
|
|||
recreateWindows(&state->term);
|
||||
break;
|
||||
|
||||
case 410: /* F11 */
|
||||
break;
|
||||
|
||||
default:
|
||||
if (state->term.inputX < getmaxx(state->term.input) - 2)
|
||||
{
|
||||
|
@ -226,21 +221,32 @@ void loop(State * state)
|
|||
daFree(buffer);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
State state = {
|
||||
.term = { },
|
||||
.messages = daCreate(char *, 64),
|
||||
};
|
||||
int ret = 0;
|
||||
State state = createDefaultState();
|
||||
|
||||
loop(&state);
|
||||
|
||||
for (size_t i = 0; i < daSize(state.messages); ++i)
|
||||
partresult result = parseArgs(argc, argv, &state);
|
||||
if (result < 0)
|
||||
{
|
||||
free(state.messages[i]);
|
||||
fprintf(stderr, "ERROR: Parsing of command line arguments failed!\n");
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf("IP: %s\n", state.conf.ip);
|
||||
printf("PORT: %d\n", state.conf.port);
|
||||
printf("MODE: %d\n", state.conf.mode);
|
||||
|
||||
// loop(&state);
|
||||
|
||||
// for (size_t i = 0; i < daSize(state.messages); ++i)
|
||||
// {
|
||||
// free(state.messages[i]);
|
||||
// }
|
||||
|
||||
exit:
|
||||
daFree(state.messages);
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue