From 28284a348d11599f32f8af08024e3730afdc3269 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Mon, 7 May 2018 00:31:07 +0300 Subject: server: initial work on command line argument handling. --- src/server/main.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++---- src/server/ui.h | 3 -- 2 files changed, 77 insertions(+), 9 deletions(-) (limited to 'src/server') diff --git a/src/server/main.c b/src/server/main.c index 8406d11..939581b 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -22,11 +22,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include @@ -34,14 +34,26 @@ #include "net.h" #include "ui.h" -static int init(int argc, char ***argv, const unsigned short int port, enum uitype ui); +struct settings { + struct timespec period; + enum uitype ui; + unsigned short int port; +}; + +static int init(int argc, char ***argv, const struct settings * const settings); +static struct settings parseargs(int argc, char **argv); +static enum uitype identify_ui(const char * const s); +static struct timespec calc_period(const char * const s); +static size_t printhelp(const char * const name); int main(int argc, char **argv) { + struct settings settings; int ret; int nd; - nd = init(argc, &argv, 2191, UI_DEFAULT); + settings = parseargs(argc, argv); + nd = init(argc, &argv, &settings); if (nd == ERROR) { ret = nd; @@ -54,18 +66,18 @@ fail: return ret; } -static int init(int argc, char ***argv, const unsigned short int port, enum uitype ui) +static int init(int argc, char ***argv, const struct settings * const settings) { int ret = A_OK; int nd; - nd = net_init(port); + nd = net_init(settings->port); if (nd == ERROR) { ret = nd; goto fail; } - ret = ui_init(argc, argv, ui, nd, DEFAULT_PERIOD); + ret = ui_init(argc, argv, settings->ui, nd, settings->period); if (ret == ERROR) { net_close(nd); goto fail; @@ -74,3 +86,62 @@ static int init(int argc, char ***argv, const unsigned short int port, enum uity fail: return ret; } + +static struct settings parseargs(int argc, char **argv) +{ + /* collection of defaults */ + struct settings ret = {{0, 200 * 1000 * 1000}, /* 200 ms */ + ui_curses, + 2191}; + ssize_t i; + + if (argc == 1) { + return ret; + } + + for (i = 1; i < argc; ++i) { + + if (!(strncmp(argv[i], "--period=", 8))) { + ret.period = strlen(argv[i]) > 9 ? calc_period(argv[i] + 9) : (struct timespec) {0, printhelp(*argv)}; + } else if (!(strncmp(argv[i], "--ui=", 8))) { + ret.ui = strlen(argv[i]) > 5 ? identify_ui(argv[i] + 5) : printhelp(*argv); + } else if (!(strncmp(argv[i], "--port=", 7))) { + ret.port = strlen(argv[i]) > 7 ? strtoul(argv[i] + 7, NULL, 10) : printhelp(*argv); + } else { + printhelp(*argv); + } + } + + return ret; +} + +static enum uitype identify_ui(const char * const s) +{ + /* TODO: stub! */ + return ui_curses; +} + +static struct timespec calc_period(const char * const s) +{ + /* TODO: stub! */ + return (struct timespec) {0, 200 * 1000 * 1000}; +} + +static size_t printhelp(const char * const name) +{ + printf( "This is a coffeetemp companion application\n" + "\n" + "usage:\t%s [options]\n" + "\n" + "Options:\n" + " --period= set network polling rate to milliseconds (default: 200)\n" + " --ui= set the UI frontend (default: curses)\n" + " * possible choices: [none, file, curses, gtk]\n" + " --port= set the listening port number to \n" + "\n" + "In case more than one same option is provided, the last one take precedence\n" + "\n", + name); + exit(EXIT_FAILURE); + return 0; +} diff --git a/src/server/ui.h b/src/server/ui.h index 8070be3..d764180 100644 --- a/src/server/ui.h +++ b/src/server/ui.h @@ -30,9 +30,6 @@ enum uitype { ui_gtk }; -static const enum uitype UI_DEFAULT = ui_curses; -static const struct timespec DEFAULT_PERIOD = {0, 200 * 1000 * 1000}; /* 200 ms */ - int ui_init(int argc, char ***argv, enum uitype ui, int nd, struct timespec period); int ui_startloop(void); -- cgit v1.2.3