rough framework
[lemu] / main.c
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..9b0d98d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,93 @@
+/*
+ * toy server
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+#include <arpa/inet.h>
+
+#include <event2/event.h>
+#include <event2/thread.h>
+
+#include <openssl/ssl.h>
+#include <openssl/crypto.h>
+#include <openssl/err.h>
+
+#include "version.h"
+#include "notify.h"
+#include "server.h"
+
+#define DEFAULT_CONFIG_FILE "conf/lemu.conf"
+
+#define USAGE_FLAGS_LONG (1<<0)
+static void
+usage_(char *prog, unsigned int usage_flags)
+{
+       char *x;
+
+       x = strrchr(prog, '/');
+       if (x && *(x + 1)) {
+               prog = x + 1;
+       }
+
+       printf("Usage: %s [-c config_file]\n", prog);
+       if (! (usage_flags & USAGE_FLAGS_LONG)) {
+               return;
+       }
+       printf(
+              "\t-h \t : this help\n"
+              "\t-c config_file\t : loads the specified config file [default: '" DEFAULT_CONFIG_FILE "']\n"
+       );
+}
+
+int
+main(int argc, char **argv)
+{
+       struct server *server;
+       char *conf_file = DEFAULT_CONFIG_FILE;
+       int opt;
+       int r;
+
+       /* enable threading in libevent before anything tries to use it */
+       if ( (r = evthread_use_pthreads()) ) {
+               NOTIFY_ERROR("%s:%d", "evthread_use_pthreads", r);
+       }
+
+       SSL_load_error_strings();
+       SSL_library_init();
+       OpenSSL_add_all_algorithms();
+
+       while ( (opt = getopt(argc, argv, "c:h")) != -1 ) {
+               switch (opt) {
+               case 'c':
+                       conf_file = optarg;
+                       break;
+               case 'h':
+                       usage_(argv[0], USAGE_FLAGS_LONG);
+                       exit(EXIT_SUCCESS);
+                       break;
+               default:
+                       usage_(argv[0], 0);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       server = server_new();
+       if (!server) {
+               NOTIFY_FATAL("server_new");
+               exit(EXIT_FAILURE);
+       }
+
+       if (server_init(server, conf_file)) {
+               NOTIFY_FATAL("error parsing config file '%s'", conf_file);
+               exit(EXIT_FAILURE);
+       }
+
+       server_free(server);
+
+       exit(EXIT_SUCCESS);
+}