diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/tools-common.c | 61 | ||||
-rw-r--r-- | tools/tools-common.h | 3 | ||||
-rw-r--r-- | tools/xkbcli.1.ronn | 31 | ||||
-rw-r--r-- | tools/xkbcli.c | 92 |
4 files changed, 187 insertions, 0 deletions
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); +} |