1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker * Copyright © 2021 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker *
4*2b949d04SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker *
11*2b949d04SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker * Software.
14*2b949d04SAndroid Build Coastguard Worker *
15*2b949d04SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker */
23*2b949d04SAndroid Build Coastguard Worker
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker
26*2b949d04SAndroid Build Coastguard Worker #include <getopt.h>
27*2b949d04SAndroid Build Coastguard Worker #include <locale.h>
28*2b949d04SAndroid Build Coastguard Worker #include <stdio.h>
29*2b949d04SAndroid Build Coastguard Worker #include <stdlib.h>
30*2b949d04SAndroid Build Coastguard Worker
31*2b949d04SAndroid Build Coastguard Worker #include "xkbcommon/xkbcommon.h"
32*2b949d04SAndroid Build Coastguard Worker #include "xkbcommon/xkbcommon-compose.h"
33*2b949d04SAndroid Build Coastguard Worker
34*2b949d04SAndroid Build Coastguard Worker static void
usage(FILE * fp,char * progname)35*2b949d04SAndroid Build Coastguard Worker usage(FILE *fp, char *progname)
36*2b949d04SAndroid Build Coastguard Worker {
37*2b949d04SAndroid Build Coastguard Worker fprintf(fp,
38*2b949d04SAndroid Build Coastguard Worker "Usage: %s [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
39*2b949d04SAndroid Build Coastguard Worker progname);
40*2b949d04SAndroid Build Coastguard Worker fprintf(fp,
41*2b949d04SAndroid Build Coastguard Worker " --locale - specify the locale directly\n"
42*2b949d04SAndroid Build Coastguard Worker " --locale-from-env - get the locale from the LC_ALL/LC_CTYPE/LANG environment variables (falling back to C)\n"
43*2b949d04SAndroid Build Coastguard Worker " --locale-from-setlocale - get the locale using setlocale(3)\n"
44*2b949d04SAndroid Build Coastguard Worker );
45*2b949d04SAndroid Build Coastguard Worker }
46*2b949d04SAndroid Build Coastguard Worker
47*2b949d04SAndroid Build Coastguard Worker int
main(int argc,char * argv[])48*2b949d04SAndroid Build Coastguard Worker main(int argc, char *argv[])
49*2b949d04SAndroid Build Coastguard Worker {
50*2b949d04SAndroid Build Coastguard Worker int ret = EXIT_FAILURE;
51*2b949d04SAndroid Build Coastguard Worker struct xkb_context *ctx = NULL;
52*2b949d04SAndroid Build Coastguard Worker struct xkb_compose_table *compose_table = NULL;
53*2b949d04SAndroid Build Coastguard Worker const char *locale = NULL;
54*2b949d04SAndroid Build Coastguard Worker enum options {
55*2b949d04SAndroid Build Coastguard Worker OPT_LOCALE,
56*2b949d04SAndroid Build Coastguard Worker OPT_LOCALE_FROM_ENV,
57*2b949d04SAndroid Build Coastguard Worker OPT_LOCALE_FROM_SETLOCALE,
58*2b949d04SAndroid Build Coastguard Worker };
59*2b949d04SAndroid Build Coastguard Worker static struct option opts[] = {
60*2b949d04SAndroid Build Coastguard Worker {"locale", required_argument, 0, OPT_LOCALE},
61*2b949d04SAndroid Build Coastguard Worker {"locale-from-env", no_argument, 0, OPT_LOCALE_FROM_ENV},
62*2b949d04SAndroid Build Coastguard Worker {"locale-from-setlocale", no_argument, 0, OPT_LOCALE_FROM_SETLOCALE},
63*2b949d04SAndroid Build Coastguard Worker {0, 0, 0, 0},
64*2b949d04SAndroid Build Coastguard Worker };
65*2b949d04SAndroid Build Coastguard Worker
66*2b949d04SAndroid Build Coastguard Worker setlocale(LC_ALL, "");
67*2b949d04SAndroid Build Coastguard Worker
68*2b949d04SAndroid Build Coastguard Worker while (1) {
69*2b949d04SAndroid Build Coastguard Worker int opt;
70*2b949d04SAndroid Build Coastguard Worker int option_index = 0;
71*2b949d04SAndroid Build Coastguard Worker
72*2b949d04SAndroid Build Coastguard Worker opt = getopt_long(argc, argv, "h", opts, &option_index);
73*2b949d04SAndroid Build Coastguard Worker if (opt == -1)
74*2b949d04SAndroid Build Coastguard Worker break;
75*2b949d04SAndroid Build Coastguard Worker
76*2b949d04SAndroid Build Coastguard Worker switch (opt) {
77*2b949d04SAndroid Build Coastguard Worker case OPT_LOCALE:
78*2b949d04SAndroid Build Coastguard Worker locale = optarg;
79*2b949d04SAndroid Build Coastguard Worker break;
80*2b949d04SAndroid Build Coastguard Worker case OPT_LOCALE_FROM_ENV:
81*2b949d04SAndroid Build Coastguard Worker locale = getenv("LC_ALL");
82*2b949d04SAndroid Build Coastguard Worker if (!locale)
83*2b949d04SAndroid Build Coastguard Worker locale = getenv("LC_CTYPE");
84*2b949d04SAndroid Build Coastguard Worker if (!locale)
85*2b949d04SAndroid Build Coastguard Worker locale = getenv("LANG");
86*2b949d04SAndroid Build Coastguard Worker if (!locale)
87*2b949d04SAndroid Build Coastguard Worker locale = "C";
88*2b949d04SAndroid Build Coastguard Worker break;
89*2b949d04SAndroid Build Coastguard Worker case OPT_LOCALE_FROM_SETLOCALE:
90*2b949d04SAndroid Build Coastguard Worker locale = setlocale(LC_CTYPE, NULL);
91*2b949d04SAndroid Build Coastguard Worker break;
92*2b949d04SAndroid Build Coastguard Worker case 'h':
93*2b949d04SAndroid Build Coastguard Worker usage(stdout, argv[0]);
94*2b949d04SAndroid Build Coastguard Worker return EXIT_SUCCESS;
95*2b949d04SAndroid Build Coastguard Worker case '?':
96*2b949d04SAndroid Build Coastguard Worker usage(stderr, argv[0]);
97*2b949d04SAndroid Build Coastguard Worker return EXIT_INVALID_USAGE;
98*2b949d04SAndroid Build Coastguard Worker }
99*2b949d04SAndroid Build Coastguard Worker }
100*2b949d04SAndroid Build Coastguard Worker if (locale == NULL) {
101*2b949d04SAndroid Build Coastguard Worker usage(stderr, argv[0]);
102*2b949d04SAndroid Build Coastguard Worker return EXIT_INVALID_USAGE;
103*2b949d04SAndroid Build Coastguard Worker }
104*2b949d04SAndroid Build Coastguard Worker
105*2b949d04SAndroid Build Coastguard Worker ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
106*2b949d04SAndroid Build Coastguard Worker if (!ctx) {
107*2b949d04SAndroid Build Coastguard Worker fprintf(stderr, "Couldn't create xkb context\n");
108*2b949d04SAndroid Build Coastguard Worker goto out;
109*2b949d04SAndroid Build Coastguard Worker }
110*2b949d04SAndroid Build Coastguard Worker
111*2b949d04SAndroid Build Coastguard Worker compose_table =
112*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_new_from_locale(ctx, locale,
113*2b949d04SAndroid Build Coastguard Worker XKB_COMPOSE_COMPILE_NO_FLAGS);
114*2b949d04SAndroid Build Coastguard Worker if (!compose_table) {
115*2b949d04SAndroid Build Coastguard Worker fprintf(stderr, "Couldn't create compose from locale\n");
116*2b949d04SAndroid Build Coastguard Worker goto out;
117*2b949d04SAndroid Build Coastguard Worker }
118*2b949d04SAndroid Build Coastguard Worker
119*2b949d04SAndroid Build Coastguard Worker printf("Compiled successfully from locale %s\n", locale);
120*2b949d04SAndroid Build Coastguard Worker
121*2b949d04SAndroid Build Coastguard Worker out:
122*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_unref(compose_table);
123*2b949d04SAndroid Build Coastguard Worker xkb_context_unref(ctx);
124*2b949d04SAndroid Build Coastguard Worker
125*2b949d04SAndroid Build Coastguard Worker return ret;
126*2b949d04SAndroid Build Coastguard Worker }
127