Skip to content

Commit

Permalink
Add option --file
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Jul 25, 2023
1 parent d417692 commit 653a331
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions tools/compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ static void
usage(FILE *fp, char *progname)
{
fprintf(fp,
"Usage: %s [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
"Usage: %s [--file FILE] [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
progname);
fprintf(fp,
" --file - specify a file to load\n"
" --locale - specify the locale directly\n"
" --locale-from-env - get the locale from the LC_ALL/LC_CTYPE/LANG environment variables (falling back to C)\n"
" --locale-from-setlocale - get the locale using setlocale(3)\n"
Expand Down Expand Up @@ -78,12 +79,17 @@ main(int argc, char *argv[])
struct xkb_context *ctx = NULL;
struct xkb_compose_table *compose_table = NULL;
const char *locale = NULL;
const char *path = NULL;
/* TODO: add option */
enum xkb_compose_format format = XKB_COMPOSE_FORMAT_TEXT_V1;
enum options {
OPT_FILE,
OPT_LOCALE,
OPT_LOCALE_FROM_ENV,
OPT_LOCALE_FROM_SETLOCALE,
};
static struct option opts[] = {
{"file", required_argument, 0, OPT_FILE},
{"locale", required_argument, 0, OPT_LOCALE},
{"locale-from-env", no_argument, 0, OPT_LOCALE_FROM_ENV},
{"locale-from-setlocale", no_argument, 0, OPT_LOCALE_FROM_SETLOCALE},
Expand All @@ -101,6 +107,9 @@ main(int argc, char *argv[])
break;

switch (opt) {
case OPT_FILE:
path = optarg;
break;
case OPT_LOCALE:
locale = optarg;
break;
Expand Down Expand Up @@ -135,14 +144,31 @@ main(int argc, char *argv[])
goto out;
}

compose_table =
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");
goto out;
if (path != NULL) {
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);
goto out;
}
} else {
compose_table =
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");
goto out;
}
}


struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(compose_table);
while (xkb_compose_table_iterator_next(iter)) {
compose_table_print_cb(xkb_compose_table_iterator_current(iter), NULL);
Expand All @@ -151,6 +177,7 @@ main(int argc, char *argv[])

out:
xkb_compose_table_unref(compose_table);
file_error:
xkb_context_unref(ctx);

return ret;
Expand Down

0 comments on commit 653a331

Please sign in to comment.