From 9f3d8cc0c09a4167638c9af603deffcf364a206a Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 13 Dec 2020 13:05:49 +0100 Subject: [PATCH] Added argument parsing --- run.sh | 2 +- src/necronda-server.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/run.sh b/run.sh index 84f5e37..3bcca15 100755 --- a/run.sh +++ b/run.sh @@ -3,4 +3,4 @@ echo "-- Building and starting Necronda Server..." make compile && \ echo "-- Successfully finished compiling!" && \ echo "-- Starting Server..." && \ - ./bin/necronda-server + ./bin/necronda-server $@ diff --git a/src/necronda-server.c b/src/necronda-server.c index 5f2de76..a087107 100644 --- a/src/necronda-server.c +++ b/src/necronda-server.c @@ -171,6 +171,32 @@ int main(int argc, const char *argv[]) { printf("Necronda Web Server\n"); + for (int i = 1; i < argc; i++) { + const char *arg = argv[i]; + unsigned long len = strlen(arg); + if ((len == 2 && strncmp(arg, "-h", 2) == 0) || (len == 6 && strncmp(arg, "--help", 6) == 0)) { + printf("Usage: necronda-server [-h] -w -c -p \n" + "\n" + "Options:\n" + " -c, --cert path to the full chain certificate file\n" + " -h, --help print this dialogue\n" + " -p, --privkey path to the private key file\n" + " -w, --webroot path to the web root directory\n"); + return 0; + } else if ((len == 2 && strncmp(arg, "-w", 2) == 0) || (len == 9 && strncmp(arg, "--webroot", 9) == 0)) { + if (i == argc - 1) { + fprintf(stderr, ERR_STR "Unable to parse argument %s, usage: {-w|--webroot} " CLR_STR "\n", arg); + return 1; + } + arg = argv[++i]; + len = strlen(arg); + + } else { + fprintf(stderr, ERR_STR "Unable to parse argument '%s'" CLR_STR "\n", arg); + return 1; + } + } + SOCKETS[0] = socket(AF_INET6, SOCK_STREAM, 0); if (SOCKETS[0] == -1) goto socket_err; SOCKETS[1] = socket(AF_INET6, SOCK_STREAM, 0);