From 7095bedd256b07e80f6f852622025a899f7af1da Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Fri, 25 Sep 2020 11:41:33 +0200 Subject: [PATCH] Accept user-specified config path Adds a -c,--config command line option that allows the user to specify an arbitrary config path. --- kanshi.1.scd | 10 +++++++++- main.c | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/kanshi.1.scd b/kanshi.1.scd index 8577a4b..f50c7f6 100644 --- a/kanshi.1.scd +++ b/kanshi.1.scd @@ -6,7 +6,15 @@ kanshi - dynamic output configuration # DESCRIPTION -*kanshi* +*kanshi* [options...] + +# OPTIONS + +*-h, --help* + Show help message and quit. + +*-c, --config* + Specifies a config file. # SYNOPSIS diff --git a/main.c b/main.c index 67ea446..b7f974c 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include #include +#include #include #include #include @@ -453,7 +454,11 @@ static const struct wl_registry_listener registry_listener = { .global_remove = registry_handle_global_remove, }; -static struct kanshi_config *read_config(void) { +static struct kanshi_config *read_config(const char *config) { + if (config != NULL) { + return parse_config(config); + } + const char config_filename[] = "kanshi/config"; char config_path[PATH_MAX]; const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); @@ -472,12 +477,42 @@ static struct kanshi_config *read_config(void) { return parse_config(config_path); } +static const char usage[] = "Usage: %s [options...]\n" +" -h, --help Show help message and quit\n" +" -c, --config Path to config file.\n"; + +static const struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"config", required_argument, 0, 'c'}, + {0}, +}; + int main(int argc, char *argv[]) { - struct kanshi_config *config = read_config(); + char *config_arg = NULL; + + int opt; + while ((opt = getopt_long(argc, argv, "hc:", long_options, NULL)) != -1) { + switch (opt) { + case 'c': + free(config_arg); + config_arg = strdup(optarg); + break; + case 'h': + fprintf(stderr, usage, argv[0]); + return EXIT_SUCCESS; + default: + fprintf(stderr, usage, argv[0]); + return EXIT_FAILURE; + } + } + + struct kanshi_config *config = read_config(config_arg); if (config == NULL) { return EXIT_FAILURE; } + free(config_arg); + struct wl_display *display = wl_display_connect(NULL); if (display == NULL) { fprintf(stderr, "failed to connect to display\n");