Skip to content

Commit

Permalink
tools: Enable Compose file positional argument and piping
Browse files Browse the repository at this point in the history
Also deprecate the `--file` flag as redundant.
  • Loading branch information
wismill committed Jan 30, 2025
1 parent 502e9e5 commit db3f2c3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
3 changes: 3 additions & 0 deletions changes/tools/+compile-compose.feature.md
Original file line number Diff line number Diff line change
@@ -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 `-`.
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 44 additions & 11 deletions tools/compile-compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tools/compile-keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 12 additions & 2 deletions tools/xkbcli-compile-compose.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db3f2c3

Please sign in to comment.