2 * Copyright © 2021 Ran Benita <ran@unusedvar.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
31 #include "xkbcommon/xkbcommon.h"
32 #include "xkbcommon/xkbcommon-keysyms.h"
33 #include "xkbcommon/xkbcommon-compose.h"
36 usage(FILE *fp, char *progname)
39 "Usage: %s [--file FILE] [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
42 " --file - specify a file to load\n"
43 " --locale - specify the locale directly\n"
44 " --locale-from-env - get the locale from the LC_ALL/LC_CTYPE/LANG environment variables (falling back to C)\n"
45 " --locale-from-setlocale - get the locale using setlocale(3)\n"
50 print_compose_table_entry(struct xkb_compose_table_entry *entry)
53 const xkb_keysym_t *syms = xkb_compose_table_entry_sequence(entry, &nsyms);
55 for (size_t i = 0; i < nsyms; i++) {
56 xkb_keysym_get_name(syms[i], buf, sizeof(buf));
63 const char *utf8 = xkb_compose_table_entry_utf8(entry);
65 printf(" \"%s\"", utf8);
67 const xkb_keysym_t keysym = xkb_compose_table_entry_keysym(entry);
68 if (keysym != XKB_KEY_NoSymbol) {
69 xkb_keysym_get_name(keysym, buf, sizeof(buf));
76 main(int argc, char *argv[])
78 int ret = EXIT_FAILURE;
79 struct xkb_context *ctx = NULL;
80 struct xkb_compose_table *compose_table = NULL;
81 const char *locale = NULL;
82 const char *path = NULL;
83 enum xkb_compose_format format = XKB_COMPOSE_FORMAT_TEXT_V1;
88 OPT_LOCALE_FROM_SETLOCALE,
90 static struct option opts[] = {
91 {"file", required_argument, 0, OPT_FILE},
92 {"locale", required_argument, 0, OPT_LOCALE},
93 {"locale-from-env", no_argument, 0, OPT_LOCALE_FROM_ENV},
94 {"locale-from-setlocale", no_argument, 0, OPT_LOCALE_FROM_SETLOCALE},
98 setlocale(LC_ALL, "");
102 int option_index = 0;
104 opt = getopt_long(argc, argv, "h", opts, &option_index);
115 case OPT_LOCALE_FROM_ENV:
116 locale = getenv("LC_ALL");
118 locale = getenv("LC_CTYPE");
120 locale = getenv("LANG");
124 case OPT_LOCALE_FROM_SETLOCALE:
125 locale = setlocale(LC_CTYPE, NULL);
128 usage(stdout, argv[0]);
131 usage(stderr, argv[0]);
132 return EXIT_INVALID_USAGE;
135 if (locale == NULL) {
136 usage(stderr, argv[0]);
137 return EXIT_INVALID_USAGE;
140 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
142 fprintf(stderr, "Couldn't create xkb context\n");
147 FILE *file = fopen(path, "rb");
153 xkb_compose_table_new_from_file(ctx, file, locale, format,
154 XKB_COMPOSE_COMPILE_NO_FLAGS);
156 if (!compose_table) {
157 fprintf(stderr, "Couldn't create compose from file: %s\n", path);
162 xkb_compose_table_new_from_locale(ctx, locale,
163 XKB_COMPOSE_COMPILE_NO_FLAGS);
164 if (!compose_table) {
165 fprintf(stderr, "Couldn't create compose from locale\n");
170 struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(compose_table);
171 struct xkb_compose_table_entry *entry;
172 while ((entry = xkb_compose_table_iterator_next(iter))) {
173 print_compose_table_entry(entry);
175 xkb_compose_table_iterator_free(iter);
178 xkb_compose_table_unref(compose_table);
180 xkb_context_unref(ctx);