From bfe41c0cb95dfd23e55914f98315861ac12972a0 Mon Sep 17 00:00:00 2001
From: buffet <niclas@countingsort.com>
Date: Tue, 19 Mar 2019 20:33:39 +0100
Subject: [PATCH] Basic argument parsing

---
 kiwmi/main.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++---
 meson.build  |   2 +-
 2 files changed, 98 insertions(+), 6 deletions(-)

diff --git a/kiwmi/main.c b/kiwmi/main.c
index 1ccd33f..48acc42 100644
--- a/kiwmi/main.c
+++ b/kiwmi/main.c
@@ -5,17 +5,111 @@
  * You can obtain one at https://mozilla.org/MPL/2.0/.
  */
 
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
+#include <limits.h>
 
 #include <wayland-server.h>
 #include <wlr/util/log.h>
 
 #include "kiwmi/server.h"
 
-int
-main(void)
+struct kiwmi_config {
+    int verbosity;
+    const char *frontend_path;
+};
+
+struct option {
+    const char *name;
+    int val;
+};
+
+static void
+parse_arguments(int argc, char **argv, struct kiwmi_config *config)
 {
-    wlr_log_init(WLR_DEBUG, NULL);
+    static struct option options[] = {
+        {"help", 'h'},
+        {"version", 'v'},
+        {"verbose", 'V'},
+
+        {0, 0},
+    };
+
+    const char *usage =
+        "Usage: kiwmi [options] FRONTEND\n"
+        "\n"
+        "  -h, --help     Show help message and quit\n"
+        "  -v, --version  Show version number and quit\n"
+        "  -V, --verbose  Increase verbosity Level\n";
+
+    config->verbosity     = 0;
+    config->frontend_path = NULL;
+
+    for (int i = 1; i < argc; ++i) {
+        const char *opt = argv[i];
+        char val        = '?';
+
+        if (opt[0] == '-') {
+            if (opt[1] == '-') {
+                for (const struct option *current = options; current->name;
+                     ++current) {
+                    if (strcmp(&opt[2], current->name)) {
+                        val = current->val;
+                        break;
+                    }
+                }
+            } else {
+                val = opt[1];
+            }
+
+            switch (val) {
+            case 'h':
+                printf("%s", usage);
+                exit(EXIT_SUCCESS);
+                break;
+            case 'v':
+                printf("kiwmi version " KIWMI_VERSION "\n");
+                exit(EXIT_SUCCESS);
+                break;
+            case 'V':
+                ++config->verbosity;
+                break;
+            default:
+                fprintf(stderr, "%s", usage);
+                exit(EXIT_FAILURE);
+            }
+        } else {
+            config->frontend_path = opt;
+            break;
+        }
+    }
+
+    if (!config->frontend_path) {
+        fprintf(stderr, "%s", usage);
+        exit(EXIT_FAILURE);
+    }
+}
+
+int
+main(int argc, char **argv)
+{
+    struct kiwmi_config config;
+    parse_arguments(argc, argv, &config);
+
+    switch (config.verbosity) {
+    case 0:
+        wlr_log_init(WLR_ERROR, NULL);
+        break;
+    case 1:
+        wlr_log_init(WLR_INFO, NULL);
+        break;
+    default:
+        wlr_log_init(WLR_DEBUG, NULL);
+    }
+
+    fprintf(stderr, "Using kiwmi v" KIWMI_VERSION "\n");
 
     struct kiwmi_server server;
 
@@ -24,8 +118,6 @@ main(void)
         exit(EXIT_FAILURE);
     }
 
-    wlr_log(WLR_INFO, "Starting kiwmi v" KIWMI_VERSION);
-
     if (!server_run(&server)) {
         wlr_log(WLR_ERROR, "Failed to run server");
         exit(EXIT_FAILURE);
diff --git a/meson.build b/meson.build
index e0a0a27..d5014c9 100644
--- a/meson.build
+++ b/meson.build
@@ -11,7 +11,7 @@ project(
 add_project_arguments(
   [
     '-DWLR_USE_UNSTABLE',
-    '-D_POSIX_C_SOURCE=200112L',
+    '-D_POSIX_C_SOURCE=200809L',
   ],
   language: 'c',
 )