Package version up
[platform/upstream/libxkbcommon.git] / tools / compose.c
1 /*
2  * Copyright © 2021 Ran Benita <ran@unusedvar.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "config.h"
25
26 #include <getopt.h>
27 #include <locale.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "xkbcommon/xkbcommon.h"
32 #include "xkbcommon/xkbcommon-keysyms.h"
33 #include "xkbcommon/xkbcommon-compose.h"
34
35 static void
36 usage(FILE *fp, char *progname)
37 {
38     fprintf(fp,
39             "Usage: %s [--file FILE] [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
40             progname);
41     fprintf(fp,
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"
46     );
47 }
48
49 static void
50 print_compose_table_entry(struct xkb_compose_table_entry *entry)
51 {
52     size_t nsyms;
53     const xkb_keysym_t *syms = xkb_compose_table_entry_sequence(entry, &nsyms);
54     char buf[128];
55     for (size_t i = 0; i < nsyms; i++) {
56         xkb_keysym_get_name(syms[i], buf, sizeof(buf));
57         printf("<%s>", buf);
58         if (i + 1 < nsyms) {
59             printf(" ");
60         }
61     }
62     printf(":");
63     const char *utf8 = xkb_compose_table_entry_utf8(entry);
64     if (*utf8 != '\0') {
65         printf(" \"%s\"", utf8);
66     }
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));
70         printf(" %s", buf);
71     }
72     printf("\n");
73 }
74
75 int
76 main(int argc, char *argv[])
77 {
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;
84     enum options {
85         OPT_FILE,
86         OPT_LOCALE,
87         OPT_LOCALE_FROM_ENV,
88         OPT_LOCALE_FROM_SETLOCALE,
89     };
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},
95         {0, 0, 0, 0},
96     };
97
98     setlocale(LC_ALL, "");
99
100     while (1) {
101         int opt;
102         int option_index = 0;
103
104         opt = getopt_long(argc, argv, "h", opts, &option_index);
105         if (opt == -1)
106             break;
107
108         switch (opt) {
109         case OPT_FILE:
110             path = optarg;
111             break;
112         case OPT_LOCALE:
113             locale = optarg;
114             break;
115         case OPT_LOCALE_FROM_ENV:
116             locale = getenv("LC_ALL");
117             if (!locale)
118                 locale = getenv("LC_CTYPE");
119             if (!locale)
120                 locale = getenv("LANG");
121             if (!locale)
122                 locale = "C";
123             break;
124         case OPT_LOCALE_FROM_SETLOCALE:
125             locale = setlocale(LC_CTYPE, NULL);
126             break;
127         case 'h':
128             usage(stdout, argv[0]);
129             return EXIT_SUCCESS;
130         case '?':
131             usage(stderr, argv[0]);
132             return EXIT_INVALID_USAGE;
133         }
134     }
135     if (locale == NULL) {
136         usage(stderr, argv[0]);
137         return EXIT_INVALID_USAGE;
138     }
139
140     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
141     if (!ctx) {
142         fprintf(stderr, "Couldn't create xkb context\n");
143         goto out;
144     }
145
146     if (path != NULL) {
147         FILE *file = fopen(path, "rb");
148         if (file == NULL) {
149             perror(path);
150             goto file_error;
151         }
152         compose_table =
153             xkb_compose_table_new_from_file(ctx, file, locale, format,
154                                             XKB_COMPOSE_COMPILE_NO_FLAGS);
155         fclose(file);
156         if (!compose_table) {
157             fprintf(stderr, "Couldn't create compose from file: %s\n", path);
158             goto out;
159         }
160     } else {
161         compose_table =
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");
166             goto out;
167         }
168     }
169
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);
174     }
175     xkb_compose_table_iterator_free(iter);
176
177 out:
178     xkb_compose_table_unref(compose_table);
179 file_error:
180     xkb_context_unref(ctx);
181
182     return ret;
183 }