diff --git a/changes/tools/+compile-compose.feature.md b/changes/tools/+compile-compose.feature.md new file mode 100644 index 000000000..5a581d938 --- /dev/null +++ b/changes/tools/+compile-compose.feature.md @@ -0,0 +1,3 @@ +`xkbcli compile-compose`: the Compose file may be passed as a positional argument +and `--file` is now deprecated. The file can also be piped to the standard input +by setting the path to `-`. diff --git a/meson.build b/meson.build index 03695af09..48bbc3079 100644 --- a/meson.build +++ b/meson.build @@ -520,6 +520,8 @@ if build_tools 'src/compose/dump.c', 'src/compose/dump.h', 'src/compose/escape.h', + 'tools/tools-common.c', + 'tools/tools-common.h', dependencies: tools_dep, install: true, install_dir: dir_libexec) diff --git a/tools/compile-compose.c b/tools/compile-compose.c index 7581b62d6..10a033d38 100644 --- a/tools/compile-compose.c +++ b/tools/compile-compose.c @@ -32,12 +32,13 @@ #include "xkbcommon/xkbcommon-compose.h" #include "src/compose/dump.h" #include "src/keysym.h" +#include "tools/tools-common.h" static void usage(FILE *fp, char *progname) { fprintf(fp, - "Usage: %s [--help] [--file FILE] [--locale LOCALE] [--test]\n", + "Usage: %s [--help] [--locale LOCALE] [--test] [FILE]\n", progname); fprintf(fp, "\n" @@ -47,7 +48,8 @@ usage(FILE *fp, char *progname) " --help\n" " Print this help and exit\n" " --file FILE\n" - " Specify a Compose file to load\n" + " Specify a Compose file to load.\n" + " DEPRECATED: use the positional argument instead.\n" " --locale LOCALE\n" " Specify the locale directly, instead of relying on the environment variables\n" " LC_ALL, LC_TYPE and LANG.\n" @@ -58,9 +60,6 @@ usage(FILE *fp, char *progname) int main(int argc, char *argv[]) { - int ret = EXIT_FAILURE; - struct xkb_context *ctx = NULL; - struct xkb_compose_table *compose_table = NULL; const char *locale = NULL; const char *path = NULL; enum xkb_compose_format format = XKB_COMPOSE_FORMAT_TEXT_V1; @@ -96,6 +95,7 @@ main(int argc, char *argv[]) switch (opt) { case OPT_FILE: path = optarg; + fprintf(stderr, "WARNING: the flag --file is deprecated\n"); break; case OPT_LOCALE: locale = optarg; @@ -118,24 +118,56 @@ main(int argc, char *argv[]) return EXIT_INVALID_USAGE; } - ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (optind < argc && !isempty(argv[optind])) { + /* Some positional arguments left: use a file */ + if (path) { + fprintf(stderr, + "ERROR: Path already provided via the flag: --file\n"); + usage(stderr, argv[0]); + exit(EXIT_INVALID_USAGE); + } + path = argv[optind++]; + if (optind < argc) { + fprintf(stderr, "ERROR: Too many positional arguments\n"); + usage(stderr, argv[0]); + exit(EXIT_INVALID_USAGE); + } + } else if (is_pipe_or_regular_file(STDIN_FILENO)) { + /* No positional argument: detect piping */ + path = "-"; + } + + struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!ctx) { - fprintf(stderr, "Couldn't create xkb context\n"); - goto out; + fprintf(stderr, "ERROR: Couldn't create xkb context\n"); + return EXIT_FAILURE; } + int ret = EXIT_FAILURE; + struct xkb_compose_table *compose_table = NULL; + if (path != NULL) { - FILE *file = fopen(path, "rb"); + FILE *file; + if (isempty(path) || strcmp(path, "-") == 0) { + /* Read from stdin */ + file = tools_read_stdin(); + } else { + /* Read from regular file */ + file = fopen(path, "rb"); + } + if (file == NULL) { perror(path); goto file_error; } + compose_table = xkb_compose_table_new_from_file(ctx, file, locale, format, XKB_COMPOSE_COMPILE_NO_FLAGS); fclose(file); if (!compose_table) { - fprintf(stderr, "Couldn't create compose from file: %s\n", path); + fprintf(stderr, + "ERROR: Couldn't create compose from file: %s\n", path); goto out; } } else { @@ -144,7 +176,8 @@ main(int argc, char *argv[]) XKB_COMPOSE_COMPILE_NO_FLAGS); if (!compose_table) { fprintf(stderr, - "Couldn't create compose from locale \"%s\"\n", locale); + "ERROR: Couldn't create compose from locale \"%s\"\n", + locale); goto out; } } diff --git a/tools/compile-keymap.c b/tools/compile-keymap.c index 007c741c6..5b4da4b81 100644 --- a/tools/compile-keymap.c +++ b/tools/compile-keymap.c @@ -246,7 +246,7 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names) *path = argv[optind++]; if (optind < argc) { too_much_arguments: - fprintf(stderr, "ERROR: Too much positional arguments\n"); + fprintf(stderr, "ERROR: Too many positional arguments\n"); usage(stderr, argv[0]); exit(EXIT_INVALID_USAGE); } diff --git a/tools/xkbcli-compile-compose.1 b/tools/xkbcli-compile-compose.1 index cc66cf9ae..7f067370a 100644 --- a/tools/xkbcli-compile-compose.1 +++ b/tools/xkbcli-compile-compose.1 @@ -9,17 +9,27 @@ .Sh SYNOPSIS .Nm .Op Ar options +.Op Ar COMPOSE_FILE . .Sh DESCRIPTION .Nm compile and print a Compose file based on the given options . -.Bl -tag -width Ds +.Bl -tag -width +.It Ar COMPOSE_FILE +Path to the compose file to load, or +.Dq \- +to read the standard input +. .It Fl \-help Print help and exit . .It Fl \-file Ar FILE -Specify a Compose file to load +Specify a Compose file to load, or +.Dq \- +to read the standard input. +.Pp +DEPRECATED: add the file path without the flag. . .It Fl \-locale Ar LOCALE Specify the locale directly, instead of relying on the environment variables