summaryrefslogtreecommitdiff
path: root/tools/xkbcli.c
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2020-07-08 12:51:54 +1000
committerRan Benita <ran@unusedvar.com>2020-07-25 11:05:14 +0300
commited57fb8b869ba1edd226b55953ace0719fe8e1c1 (patch)
tree00cf3c08e52080de2953db61ac999c5f46aa08da /tools/xkbcli.c
parent1b796a7209b4962db69a9de2096464d117d9b0ef (diff)
downloadxorg-lib-libxkbcommon-ed57fb8b869ba1edd226b55953ace0719fe8e1c1.tar.gz
tools: add a xkbcli tool as entry point for the various tools we have
This is the base tool, no subtools are currently connected so you only get help and version for now. The goal here is to have a git-like infrastructure where /usr/bin/xkbcli is the main tool, anything else will hide in libexec. The infrastructure for this is copied from libinput. Tools themselves will will be installed in $prefix/libexec/xkbcommon and the xkbcli tool forks off whatever argv[1] is after modifying the PATH to include the libexec dir. libinput has additional code for checking whether we're running this from the builddir but it's a bit iffy and it's usefulness is limited - if you're in the builddir anyway you can just run ./builddir/xkbcli-<toolname> directly. So for this code here, running ./builddir/xkbcli <toolname> will execute the one in the prefix/libexecdir. Since we want that tool available everywhere even where some of the subtools aren't present, we need to ifdef the getopt handling. man page generation is handled via ronn which is a ruby program but allows markdown for the sources. It's hidden behind a meson option to disable where downloading ronn isn't an option. The setup is generic enough that we can add other man-pages by just appending to the array. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'tools/xkbcli.c')
-rw-r--r--tools/xkbcli.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/tools/xkbcli.c b/tools/xkbcli.c
new file mode 100644
index 0000000..fbab9dd
--- /dev/null
+++ b/tools/xkbcli.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright © 2020 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "tools-common.h"
+
+static void
+usage(void)
+{
+ printf("Usage: xkbcli [--help|-h] [--version|-V] <command> [<args>]\n"
+ "\n"
+ "Global options:\n"
+ " -h, --help ...... show this help and exit\n"
+ " -V, --version ... show version information and exit\n"
+ "\n");
+}
+
+int
+main(int argc, char **argv)
+{
+ enum options {
+ OPT_HELP = 1,
+ OPT_VERSION,
+ };
+ int option_index = 0;
+
+ while (1) {
+ int c;
+#if HAVE_GETOPT_LONG
+ static struct option opts[] = {
+ { "help", no_argument, 0, OPT_HELP },
+ { "version", no_argument, 0, OPT_VERSION },
+ { 0, 0, 0, 0}
+ };
+
+ c = getopt_long(argc, argv, "+hV", opts, &option_index);
+#else
+ c = getopt(argc, argv, "+hV");
+#endif
+ if (c == -1)
+ break;
+
+ switch(c) {
+ case 'h':
+ case OPT_HELP:
+ usage();
+ return EXIT_SUCCESS;
+ case 'V':
+ case OPT_VERSION:
+ printf("%s\n", LIBXKBCOMMON_VERSION);
+ return EXIT_SUCCESS;
+ default:
+ usage();
+ return EXIT_INVALID_USAGE;
+ }
+ }
+
+ if (optind >= argc) {
+ usage();
+ return EXIT_INVALID_USAGE;
+ }
+
+ argv += optind;
+ argc -= optind;
+
+ return tools_exec_command("xkbcli", argc, argv);
+}