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-compose.h"
35 usage(FILE *fp, char *progname)
38 "Usage: %s [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
41 " --locale - specify the locale directly\n"
42 " --locale-from-env - get the locale from the LC_ALL/LC_CTYPE/LANG environment variables (falling back to C)\n"
43 " --locale-from-setlocale - get the locale using setlocale(3)\n"
48 main(int argc, char *argv[])
50 int ret = EXIT_FAILURE;
51 struct xkb_context *ctx = NULL;
52 struct xkb_compose_table *compose_table = NULL;
53 const char *locale = NULL;
57 OPT_LOCALE_FROM_SETLOCALE,
59 static struct option opts[] = {
60 {"locale", required_argument, 0, OPT_LOCALE},
61 {"locale-from-env", no_argument, 0, OPT_LOCALE_FROM_ENV},
62 {"locale-from-setlocale", no_argument, 0, OPT_LOCALE_FROM_SETLOCALE},
66 setlocale(LC_ALL, "");
72 opt = getopt_long(argc, argv, "h", opts, &option_index);
80 case OPT_LOCALE_FROM_ENV:
81 locale = getenv("LC_ALL");
83 locale = getenv("LC_CTYPE");
85 locale = getenv("LANG");
89 case OPT_LOCALE_FROM_SETLOCALE:
90 locale = setlocale(LC_CTYPE, NULL);
93 usage(stdout, argv[0]);
96 usage(stderr, argv[0]);
97 return EXIT_INVALID_USAGE;
100 if (locale == NULL) {
101 usage(stderr, argv[0]);
102 return EXIT_INVALID_USAGE;
105 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
107 fprintf(stderr, "Couldn't create xkb context\n");
112 xkb_compose_table_new_from_locale(ctx, locale,
113 XKB_COMPOSE_COMPILE_NO_FLAGS);
114 if (!compose_table) {
115 fprintf(stderr, "Couldn't create compose from locale\n");
119 printf("Compiled successfully from locale %s\n", locale);
122 xkb_compose_table_unref(compose_table);
123 xkb_context_unref(ctx);