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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index f23e77a..72bf0e9 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -23,7 +23,7 @@ jobs:
sudo apt update -y
sudo env DEBIAN_FRONTEND=noninteractive apt install -y \
doxygen libxcb-xkb-dev valgrind ninja-build \
- libwayland-dev wayland-protocols bison graphviz
+ libwayland-dev wayland-protocols bison graphviz ruby-ronn
- name: Setup
run: |
meson setup build
@@ -40,12 +40,14 @@ jobs:
runs-on: macos-10.15
steps:
- uses: actions/checkout@v2
+ - uses: actions/setup-ruby@v1
- uses: actions/setup-python@v1
with:
python-version: '3.7'
- name: Install dependencies
run: |
python -m pip install --upgrade pip meson
+ gem install ronn
brew install doxygen bison ninja
brew link bison --force
env:
@@ -65,6 +67,7 @@ jobs:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
+ - uses: actions/setup-ruby@v1
- uses: actions/setup-python@v1
with:
python-version: '3.7'
@@ -78,6 +81,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip meson
+ gem install ronn
choco install ninja winflexbison3 -y --no-progress --stop-on-first-failure
- name: Setup
shell: cmd
diff --git a/meson.build b/meson.build
index 4ac397b..7b584c5 100644
--- a/meson.build
+++ b/meson.build
@@ -12,6 +12,8 @@ project(
pkgconfig = import('pkgconfig')
cc = meson.get_compiler('c')
+dir_libexec = join_paths(get_option('prefix'), get_option('libexecdir'), 'xkbcommon')
+dir_man = join_paths(get_option('prefix'), get_option('mandir'))
# Compiler flags.
foreach cflag: [
@@ -61,6 +63,9 @@ endif
# config.h.
configh_data = configuration_data()
+configh_data.set('EXIT_INVALID_USAGE', '2')
+configh_data.set_quoted('LIBXKBCOMMON_VERSION', meson.project_version())
+configh_data.set_quoted('LIBXKBCOMMON_TOOL_PATH', dir_libexec)
# Like AC_USE_SYSTEM_EXTENSIONS, what #define to use to get extensions
# beyond the base POSIX function set.
if host_machine.system() == 'sunos'
@@ -121,6 +126,9 @@ endif
have_getopt = cc.has_header_symbol('getopt.h', 'getopt')
have_getopt_long = cc.has_header_symbol('getopt.h', 'getopt_long',
prefix: '#define _GNU_SOURCE')
+if have_getopt_long
+ configh_data.set10('HAVE_GETOPT_LONG', true)
+endif
# Silence some security & deprecation warnings on MSVC
# for some unix/C functions we use.
@@ -518,8 +526,9 @@ endif
executable('fuzz-keymap', 'fuzz/keymap/target.c', dependencies: test_dep)
executable('fuzz-compose', 'fuzz/compose/target.c', dependencies: test_dep)
+man_pages = []
-# Demo programs.
+# Tools
build_tools = have_getopt
if build_tools
libxkbcommon_tools_internal = static_library(
@@ -534,6 +543,10 @@ if build_tools
link_with: libxkbcommon_tools_internal,
)
+ executable('xkbcli', 'tools/xkbcli.c',
+ dependencies: tools_dep, install: true)
+ man_pages += 'tools/xkbcli.1.ronn'
+
if have_getopt_long
executable('xkbcommon-rmlvo-to-keymap', 'tools/rmlvo-to-keymap.c', dependencies: tools_dep)
executable('xkbcommon-how-to-type', 'tools/how-to-type.c', dependencies: tools_dep)
@@ -592,6 +605,23 @@ if build_tools
endif
endif
+if get_option('enable-manpages')
+ prog_ronn = find_program('ronn', required: true)
+ foreach manpage : man_pages
+ # man page filenames adhere to directory/topic.section.ronn
+ topic = manpage.split('/')[-1].split('.')[-3]
+ section = manpage.split('.')[-2]
+ output = '@0@.@1@'.format(topic, section)
+ custom_target(output,
+ input: manpage,
+ output: output,
+ command: [prog_ronn, '--manual=libxkbcommon manual', '--pipe', '--roff', files(manpage)],
+ capture: true,
+ install: true,
+ install_dir: join_paths(dir_man, section))
+ endforeach
+endif
+
# xkeyboard-config "verifier"
xkct_config = configuration_data()
xkct_config.set('MESON_BUILD_ROOT', meson.build_root())
diff --git a/meson_options.txt b/meson_options.txt
index 5eaa081..95c5a10 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -62,3 +62,9 @@ option(
value: true,
description: 'Enable building libxkbregistry',
)
+option(
+ 'enable-manpages',
+ type: 'boolean',
+ value: true,
+ description: 'Enable building man pages',
+)
diff --git a/tools/tools-common.c b/tools/tools-common.c
index 6e397c2..0b42892 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -34,16 +34,22 @@
#include <limits.h>
#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _MSC_VER
#include <io.h>
#include <windows.h>
+#ifndef PATH_MAX
+#define PATH_MAX MAX_PATH
+#endif
#else
#include <unistd.h>
#include <termios.h>
#endif
+#include "utils.h"
#include "tools-common.h"
void
@@ -197,4 +203,59 @@ tools_enable_stdin_echo(void)
(void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
}
}
+
#endif
+
+static inline bool
+tools_setup_path(void)
+{
+ const char *path = getenv("PATH");
+ const char *extra_path = LIBXKBCOMMON_TOOL_PATH;
+ char new_path[PATH_MAX];
+
+ if (snprintf_safe(new_path, sizeof(new_path), "%s:%s",
+ extra_path, path ? path : "")) {
+ setenv("PATH", new_path, 1);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+int
+tools_exec_command(const char *prefix, int real_argc, char **real_argv)
+{
+ char *argv[64] = {NULL};
+ char executable[128];
+ const char *command;
+
+ assert((size_t)real_argc < ARRAY_SIZE(argv));
+
+ command = real_argv[0];
+
+ if (!snprintf_safe(executable, sizeof(executable),
+ "%s-%s", prefix, command)) {
+ fprintf(stderr, "Failed to assemble command\n");
+ return EXIT_FAILURE;
+ }
+
+ if (!tools_setup_path()) {
+ fprintf(stderr, "Failed to set PATH\n");
+ return EXIT_FAILURE;
+ }
+
+ argv[0] = executable;
+ for (int i = 1; i < real_argc; i++)
+ argv[i] = real_argv[i];
+
+ execvp(executable, argv);
+ if (errno == ENOENT) {
+ fprintf(stderr, "Command '%s' is not available\n", command);
+ return EXIT_INVALID_USAGE;
+ } else {
+ fprintf(stderr, "Failed to execute '%s' (%s)\n",
+ command, strerror(errno));
+ }
+
+ return EXIT_FAILURE;
+}
diff --git a/tools/tools-common.h b/tools/tools-common.h
index f0faa34..bc6fa38 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -49,6 +49,9 @@ tools_disable_stdin_echo(void);
void
tools_enable_stdin_echo(void);
+int
+tools_exec_command(const char *prefix, int argc, char **argv);
+
#ifdef _MSC_VER
#define setenv(varname, value, overwrite) _putenv_s((varname), (value))
#define unsetenv(varname) _putenv_s(varname, "")
diff --git a/tools/xkbcli.1.ronn b/tools/xkbcli.1.ronn
new file mode 100644
index 0000000..8e2c338
--- /dev/null
+++ b/tools/xkbcli.1.ronn
@@ -0,0 +1,31 @@
+# xkbcli(1) - tool to interact with XKB keymaps
+
+## SYNOPSIS
+
+**xkbcli** [--help|--version] <command> [<args>]
+
+## DESCRIPTION
+
+**xkbcli** is a commandline tool to query, compile and test XKB keymaps,
+layouts and other elements.
+
+## OPTIONS
+
+ * `--help`:
+ Print help and exit
+
+ * `--version`:
+ Print the version and exit
+
+## EXIT STATUS
+
+ * 0:
+ exited successfully
+ * 1:
+ an error occured
+ * 2:
+ program was called with invalid arguments
+
+## SEE ALSO
+
+The libxkbcommon online documentation at <https://xkbcommon.org>
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);
+}