From fe3798e48a4ff843d7dc3be77356217d6c251537 Mon Sep 17 00:00:00 2001 From: Charlotte Meyer 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 61465f9..5563af2 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 #include +#include + +#include #include #include #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', )