Skip to content

Commit

Permalink
tools: Miscellaneous enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Jan 28, 2025
1 parent 6a064be commit aae7fc9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
62 changes: 33 additions & 29 deletions tools/compile-keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ static size_t num_includes = 0;
static bool test = false;

static void
usage(char **argv)
usage(FILE *file, const char *progname)
{
printf("Usage: %s [OPTIONS]\n"
fprintf(file,
"Usage: %s [OPTIONS]\n"
"\n"
"Compile the given RMLVO to a keymap and print it\n"
"\n"
Expand Down Expand Up @@ -107,7 +108,7 @@ usage(char **argv)
" --options <options>\n"
" The XKB options (default: '%s')\n"
"\n",
argv[0], DEFAULT_XKB_RULES,
progname, DEFAULT_XKB_RULES,
DEFAULT_XKB_MODEL, DEFAULT_XKB_LAYOUT,
DEFAULT_XKB_VARIANT ? DEFAULT_XKB_VARIANT : "<none>",
DEFAULT_XKB_OPTIONS ? DEFAULT_XKB_OPTIONS : "<none>");
Expand Down Expand Up @@ -153,15 +154,14 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)

bool has_rmlvo_options = false;
while (1) {
int c;
int option_index = 0;
c = getopt_long(argc, argv, "h", opts, &option_index);
int c = getopt_long(argc, argv, "h", opts, &option_index);
if (c == -1)
break;

switch (c) {
case 'h':
usage(argv);
usage(stdout, argv[0]);
exit(0);
case OPT_VERBOSE:
verbose = true;
Expand Down Expand Up @@ -196,17 +196,13 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
}
break;
case OPT_INCLUDE:
if (num_includes >= ARRAY_SIZE(includes)) {
fprintf(stderr, "error: too many includes\n");
exit(EXIT_INVALID_USAGE);
}
if (num_includes >= ARRAY_SIZE(includes))
goto too_many_includes;
includes[num_includes++] = optarg;
break;
case OPT_INCLUDE_DEFAULTS:
if (num_includes >= ARRAY_SIZE(includes)) {
fprintf(stderr, "error: too many includes\n");
exit(EXIT_INVALID_USAGE);
}
if (num_includes >= ARRAY_SIZE(includes))
goto too_many_includes;
includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
break;
case OPT_RULES:
Expand Down Expand Up @@ -240,10 +236,9 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
has_rmlvo_options = true;
break;
default:
usage(argv);
usage(stderr, argv[0]);
exit(EXIT_INVALID_USAGE);
}

}

if (optind < argc && !isempty(argv[optind])) {
Expand All @@ -257,7 +252,7 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
if (optind < argc) {
too_much_arguments:
fprintf(stderr, "ERROR: Too much positional arguments\n");
usage(argv);
usage(stderr, argv[0]);
exit(EXIT_INVALID_USAGE);
}
} else if (!isatty(STDIN_FILENO) && !has_rmlvo_options &&
Expand All @@ -270,16 +265,21 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
*path = NULL;

return true;

output_format_error:
fprintf(stderr, "ERROR: Cannot mix output formats\n");
usage(argv);
usage(stderr, argv[0]);
exit(EXIT_INVALID_USAGE);

input_format_error:
fprintf(stderr, "ERROR: Cannot use RMLVO options with keymap input\n");
usage(argv);
usage(stderr, argv[0]);
exit(EXIT_INVALID_USAGE);

too_many_includes:
fprintf(stderr, "ERROR: too many includes (max: %zu)\n",
ARRAY_SIZE(includes));
exit(EXIT_INVALID_USAGE);
}

static int
Expand Down Expand Up @@ -359,15 +359,15 @@ print_keymap_from_file(struct xkb_context *ctx, const char *path)
file = tools_read_stdin();
}
if (!file) {
fprintf(stderr, "Failed to open keymap file \"%s\": %s\n",
fprintf(stderr, "ERROR: Failed to open keymap file \"%s\": %s\n",
path ? path : "stdin", strerror(errno));
goto out;
}
keymap = xkb_keymap_new_from_file(ctx, file,
XKB_KEYMAP_FORMAT_TEXT_V1,
XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!keymap) {
fprintf(stderr, "Couldn't create xkb keymap\n");
fprintf(stderr, "ERROR: Couldn't create xkb keymap\n");
goto out;
} else if (test) {
ret = EXIT_SUCCESS;
Expand All @@ -376,7 +376,7 @@ print_keymap_from_file(struct xkb_context *ctx, const char *path)

keymap_string = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
if (!keymap_string) {
fprintf(stderr, "Couldn't get the keymap string\n");
fprintf(stderr, "ERROR: Couldn't get the keymap string\n");
goto out;
}

Expand Down Expand Up @@ -409,7 +409,7 @@ main(int argc, char **argv)
int rc = 1;

if (argc < 1) {
usage(argv);
usage(stderr, argv[0]);
return EXIT_INVALID_USAGE;
}

Expand All @@ -419,7 +419,7 @@ main(int argc, char **argv)
/* Now fill in the layout */
if (!names.layout || !*names.layout) {
if (names.variant && *names.variant) {
fprintf(stderr, "Error: a variant requires a layout\n");
fprintf(stderr, "ERROR: a variant requires a layout\n");
return EXIT_INVALID_USAGE;
}
names.layout = DEFAULT_XKB_LAYOUT;
Expand All @@ -445,14 +445,18 @@ main(int argc, char **argv)
xkb_context_include_path_append(ctx, include);
}

if (output_format == FORMAT_RMLVO) {
switch (output_format) {
case FORMAT_RMLVO:
rc = print_rmlvo(ctx, &names);
} else if (output_format == FORMAT_KEYMAP_FROM_RMLVO) {
rc = print_keymap_from_names(ctx, &names);
} else if (output_format == FORMAT_KCCGST) {
break;
case FORMAT_KCCGST:
rc = print_kccgst(ctx, &names);
} else if (output_format == FORMAT_KEYMAP_FROM_XKB) {
break;
case FORMAT_KEYMAP_FROM_XKB:
rc = print_keymap_from_file(ctx, keymap_path);
break;
default:
rc = print_keymap_from_names(ctx, &names);
}

xkb_context_unref(ctx);
Expand Down
34 changes: 17 additions & 17 deletions tools/interactive-evdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,8 @@ main(int argc, char *argv[])

bool has_rmlvo_options = false;
while (1) {
int opt;
int option_index = 0;

opt = getopt_long(argc, argv, "h", opts, &option_index);
int opt = getopt_long(argc, argv, "h", opts, &option_index);
if (opt == -1)
break;

Expand All @@ -474,17 +472,13 @@ main(int argc, char *argv[])
verbose = true;
break;
case OPT_INCLUDE:
if (num_includes >= ARRAY_SIZE(includes)) {
fprintf(stderr, "error: too many includes\n");
exit(EXIT_INVALID_USAGE);
}
if (num_includes >= ARRAY_SIZE(includes))
goto too_many_includes;
includes[num_includes++] = optarg;
break;
case OPT_INCLUDE_DEFAULTS:
if (num_includes >= ARRAY_SIZE(includes)) {
fprintf(stderr, "error: too many includes\n");
exit(EXIT_INVALID_USAGE);
}
if (num_includes >= ARRAY_SIZE(includes))
goto too_many_includes;
includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
break;
case OPT_RULES:
Expand Down Expand Up @@ -540,7 +534,7 @@ main(int argc, char *argv[])
} else if (strcmp(optarg, "xkb") == 0) {
consumed_mode = XKB_CONSUMED_MODE_XKB;
} else {
fprintf(stderr, "error: invalid --consumed-mode \"%s\"\n", optarg);
fprintf(stderr, "ERROR: invalid --consumed-mode \"%s\"\n", optarg);
usage(stderr, argv[0]);
return EXIT_INVALID_USAGE;
}
Expand Down Expand Up @@ -574,7 +568,7 @@ main(int argc, char *argv[])

ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
if (!ctx) {
fprintf(stderr, "Couldn't create xkb context\n");
fprintf(stderr, "ERROR: Couldn't create xkb context\n");
goto out;
}

Expand All @@ -597,7 +591,7 @@ main(int argc, char *argv[])
if (keymap_path) {
FILE *file = fopen(keymap_path, "rb");
if (!file) {
fprintf(stderr, "Couldn't open '%s': %s\n",
fprintf(stderr, "ERROR: Couldn't open '%s': %s\n",
keymap_path, strerror(errno));
goto out;
}
Expand All @@ -622,14 +616,15 @@ main(int argc, char *argv[])

if (!keymap) {
fprintf(stderr,
"Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
"ERROR: Failed to compile RMLVO: "
"'%s', '%s', '%s', '%s', '%s'\n",
rules, model, layout, variant, options);
goto out;
}
}

if (!keymap) {
fprintf(stderr, "Couldn't create xkb keymap\n");
fprintf(stderr, "ERROR: Couldn't create xkb keymap\n");
goto out;
}

Expand All @@ -639,7 +634,7 @@ main(int argc, char *argv[])
xkb_compose_table_new_from_locale(ctx, locale,
XKB_COMPOSE_COMPILE_NO_FLAGS);
if (!compose_table) {
fprintf(stderr, "Couldn't create compose from locale\n");
fprintf(stderr, "ERROR: Couldn't create compose from locale\n");
goto out;
}
}
Expand Down Expand Up @@ -676,6 +671,11 @@ main(int argc, char *argv[])

return ret;

too_many_includes:
fprintf(stderr, "ERROR: too many includes (max: %zu)\n",
ARRAY_SIZE(includes));
exit(EXIT_INVALID_USAGE);

input_format_error:
fprintf(stderr, "ERROR: Cannot use RMLVO options with keymap input\n");
usage(stderr, argv[0]);
Expand Down
2 changes: 1 addition & 1 deletion tools/xkbcli-compile-keymap.1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Add the given path to the include path list.
This option is order\-dependent, include paths given first are searched first.
If an include path is given, the default include path list is not used.
Use
.Fl -\-include\-defaults
.Fl \-include\-defaults
to add the default include paths.
.
.It Fl \-include\-defaults
Expand Down
2 changes: 1 addition & 1 deletion tools/xkbcli-interactive-evdev.1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Add the given path to the include path list.
This option is order\-dependent, include paths given first are searched first.
If an include path is given, the default include path list is not used.
Use
.Fl -\-include\-defaults
.Fl \-include\-defaults
to add the default include paths.
.
.It Fl \-include\-defaults
Expand Down

0 comments on commit aae7fc9

Please sign in to comment.