initial commit
This commit is contained in:
55
src/help_message.c
Normal file
55
src/help_message.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "../kerep/src/kprint/kprintf.h"
|
||||
|
||||
// option help definition
|
||||
#define HELP_OPT(ALIASES, PARAMS, DESCRIPTION...) \
|
||||
FWHI " " ALIASES " " PARAMS FGRY "\n" DESCRIPTION
|
||||
|
||||
// option parameter help definition
|
||||
#define HELP_PRM(PNAME) \
|
||||
FWHI "[" FCYN PNAME FWHI "]"
|
||||
|
||||
// option nullable parameter help definition
|
||||
#define HELP_PRMN(PNAME) \
|
||||
FWHI "[" FCYN PNAME FWHI "?]"
|
||||
|
||||
// option parameter1/parameter2 help definition
|
||||
#define HELP_PRM2(PNAME1, PNAME2) \
|
||||
FWHI "[" FCYN PNAME1 FWHI "/" FCYN PNAME2 FWHI "]"
|
||||
|
||||
const char* help_message = (
|
||||
FWHI"Usage: port-tunnel "
|
||||
HELP_PRM("program_mode")
|
||||
" " HELP_PRM("other_options...") "\n"
|
||||
|
||||
FWHI" Options syntax: --"FCYN" " HELP_PRM("options") "\n"
|
||||
" " HELP_PRM("val") FGRY" - some value\n"
|
||||
" " HELP_PRMN("val") FGRY" - value or nothing\n"
|
||||
" " HELP_PRM2("variant1", "variant2") FGRY" - variant1 or variant2\n"
|
||||
|
||||
FWHI" Program mode options:\n"
|
||||
|
||||
HELP_OPT("-c, --connect",
|
||||
HELP_PRM2("tcp","udp") "://" HELP_PRMN("ip") ":" HELP_PRM("port"),
|
||||
" Connect to a listening instance of port-tunnel.\n")
|
||||
|
||||
HELP_OPT("-l, --listen",
|
||||
HELP_PRM2("tcp","udp") "://" HELP_PRMN("ip") ":" HELP_PRM("port"),
|
||||
" Listen for incomming packets at the address (if provided) and the port\n")
|
||||
|
||||
FWHI" Other options:\n"
|
||||
|
||||
HELP_OPT("-h, --help, /?", "",
|
||||
" Show this message.\n")
|
||||
|
||||
HELP_OPT("-p, --out-port",
|
||||
HELP_PRM("port"),
|
||||
" Port incoming packets will be sent to.\n")
|
||||
|
||||
HELP_OPT("-d, --decrypt",
|
||||
HELP_PRM("key"),
|
||||
" Decrypt incoming packets with a key.\n")
|
||||
|
||||
HELP_OPT("-e, --encrypt",
|
||||
HELP_PRM("key"),
|
||||
" Encrypt outgoing packets with a key.\n")
|
||||
);
|
||||
70
src/main.c
Normal file
70
src/main.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "../kerep/src/base/base.h"
|
||||
|
||||
extern const char* help_message;
|
||||
|
||||
int errs(char* err_msg){
|
||||
throw(err_msg);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#define argIs(STR) cptr_equals(arg, STR)
|
||||
|
||||
#define argNext() argv[++argi < argc ? argi : errs(cptr_concat("option '",arg,"' must have a parameter"))]
|
||||
|
||||
typedef enum {
|
||||
ProgramMode_None,
|
||||
ProgramMode_Listen,
|
||||
ProgramMode_Connect
|
||||
} ProgramMode;
|
||||
|
||||
int main(int argc, const char* const* argv){
|
||||
kt_beginInit(false);
|
||||
kt_initKerepTypes();
|
||||
// kt_register(...);
|
||||
kt_endInit();
|
||||
|
||||
ProgramMode mode = ProgramMode_None;
|
||||
const char* endpoint_str = "";
|
||||
const char* out_port_str = "";
|
||||
|
||||
if(argc < 2)
|
||||
throw("No arguments provided. Run the program with argument -h to see help message");
|
||||
|
||||
for(int argi = 1; argi < argc; argi++){
|
||||
const char* arg = argv[argi];
|
||||
if(argIs("-h") || argIs("--help") || argIs("/?")){
|
||||
kprintf("%s"FGRY"\n", help_message);
|
||||
return 0;
|
||||
}
|
||||
else if(argIs("-c") || argIs("--connect")){
|
||||
if(mode != ProgramMode_None)
|
||||
throw(cptr_concat("invalid option '",arg,"': program mode has been selected already"));
|
||||
|
||||
mode = ProgramMode_Connect;
|
||||
endpoint_str = argNext();
|
||||
}
|
||||
else if(argIs("-l") || argIs("--listen")){
|
||||
if(mode != ProgramMode_None)
|
||||
throw(cptr_concat("invalid option '",arg,"': program mode has been selected already"));
|
||||
|
||||
mode = ProgramMode_Listen;
|
||||
endpoint_str = argNext();
|
||||
}
|
||||
else if(argIs("-p") || argIs("--out-port")){
|
||||
if(out_port_str[0] != 0)
|
||||
throw(cptr_concat("invalid option '",arg,"': out port has been selected already"));
|
||||
if(mode != ProgramMode_Connect)
|
||||
throw(cptr_concat("invalid option '",arg,"': out port setting is avaliable only for 'connect' program mode"));
|
||||
|
||||
out_port_str = argNext();
|
||||
}
|
||||
else {
|
||||
throw(cptr_concat("invalid argument: ", arg));
|
||||
}
|
||||
}
|
||||
|
||||
kprintf("endpoint_str: %s\n", endpoint_str);
|
||||
kprintf("out_port_str: %s\n", out_port_str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user