summaryrefslogtreecommitdiffstats
path: root/src/server/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/main.c')
-rw-r--r--src/server/main.c83
1 files changed, 77 insertions, 6 deletions
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 <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
+#include <arpa/inet.h>
#include <poll.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
-#include <arpa/inet.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
@@ -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=<num> set network polling rate to <num> milliseconds (default: 200)\n"
+ " --ui=<name> set the UI frontend (default: curses)\n"
+ " * possible choices: [none, file, curses, gtk]\n"
+ " --port=<num> set the listening port number to <num>\n"
+ "\n"
+ "In case more than one same option is provided, the last one take precedence\n"
+ "\n",
+ name);
+ exit(EXIT_FAILURE);
+ return 0;
+}