xref: /aosp_15_r20/external/libxkbcommon/src/compose/table.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2013,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 "utils.h"
27*2b949d04SAndroid Build Coastguard Worker #include "table.h"
28*2b949d04SAndroid Build Coastguard Worker #include "parser.h"
29*2b949d04SAndroid Build Coastguard Worker #include "paths.h"
30*2b949d04SAndroid Build Coastguard Worker 
31*2b949d04SAndroid Build Coastguard Worker static struct xkb_compose_table *
xkb_compose_table_new(struct xkb_context * ctx,const char * locale,enum xkb_compose_format format,enum xkb_compose_compile_flags flags)32*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_new(struct xkb_context *ctx,
33*2b949d04SAndroid Build Coastguard Worker                       const char *locale,
34*2b949d04SAndroid Build Coastguard Worker                       enum xkb_compose_format format,
35*2b949d04SAndroid Build Coastguard Worker                       enum xkb_compose_compile_flags flags)
36*2b949d04SAndroid Build Coastguard Worker {
37*2b949d04SAndroid Build Coastguard Worker     char *resolved_locale;
38*2b949d04SAndroid Build Coastguard Worker     struct xkb_compose_table *table;
39*2b949d04SAndroid Build Coastguard Worker     struct compose_node dummy;
40*2b949d04SAndroid Build Coastguard Worker 
41*2b949d04SAndroid Build Coastguard Worker     resolved_locale = resolve_locale(locale);
42*2b949d04SAndroid Build Coastguard Worker     if (!resolved_locale)
43*2b949d04SAndroid Build Coastguard Worker         return NULL;
44*2b949d04SAndroid Build Coastguard Worker 
45*2b949d04SAndroid Build Coastguard Worker     table = calloc(1, sizeof(*table));
46*2b949d04SAndroid Build Coastguard Worker     if (!table) {
47*2b949d04SAndroid Build Coastguard Worker         free(resolved_locale);
48*2b949d04SAndroid Build Coastguard Worker         return NULL;
49*2b949d04SAndroid Build Coastguard Worker     }
50*2b949d04SAndroid Build Coastguard Worker 
51*2b949d04SAndroid Build Coastguard Worker     table->refcnt = 1;
52*2b949d04SAndroid Build Coastguard Worker     table->ctx = xkb_context_ref(ctx);
53*2b949d04SAndroid Build Coastguard Worker 
54*2b949d04SAndroid Build Coastguard Worker     table->locale = resolved_locale;
55*2b949d04SAndroid Build Coastguard Worker     table->format = format;
56*2b949d04SAndroid Build Coastguard Worker     table->flags = flags;
57*2b949d04SAndroid Build Coastguard Worker 
58*2b949d04SAndroid Build Coastguard Worker     darray_init(table->nodes);
59*2b949d04SAndroid Build Coastguard Worker     darray_init(table->utf8);
60*2b949d04SAndroid Build Coastguard Worker 
61*2b949d04SAndroid Build Coastguard Worker     dummy.keysym = XKB_KEY_NoSymbol;
62*2b949d04SAndroid Build Coastguard Worker     dummy.leaf.is_leaf = true;
63*2b949d04SAndroid Build Coastguard Worker     dummy.leaf.utf8 = 0;
64*2b949d04SAndroid Build Coastguard Worker     dummy.leaf.keysym = XKB_KEY_NoSymbol;
65*2b949d04SAndroid Build Coastguard Worker     darray_append(table->nodes, dummy);
66*2b949d04SAndroid Build Coastguard Worker 
67*2b949d04SAndroid Build Coastguard Worker     darray_append(table->utf8, '\0');
68*2b949d04SAndroid Build Coastguard Worker 
69*2b949d04SAndroid Build Coastguard Worker     return table;
70*2b949d04SAndroid Build Coastguard Worker }
71*2b949d04SAndroid Build Coastguard Worker 
72*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_table *
xkb_compose_table_ref(struct xkb_compose_table * table)73*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_ref(struct xkb_compose_table *table)
74*2b949d04SAndroid Build Coastguard Worker {
75*2b949d04SAndroid Build Coastguard Worker     table->refcnt++;
76*2b949d04SAndroid Build Coastguard Worker     return table;
77*2b949d04SAndroid Build Coastguard Worker }
78*2b949d04SAndroid Build Coastguard Worker 
79*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_compose_table_unref(struct xkb_compose_table * table)80*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_unref(struct xkb_compose_table *table)
81*2b949d04SAndroid Build Coastguard Worker {
82*2b949d04SAndroid Build Coastguard Worker     if (!table || --table->refcnt > 0)
83*2b949d04SAndroid Build Coastguard Worker         return;
84*2b949d04SAndroid Build Coastguard Worker     free(table->locale);
85*2b949d04SAndroid Build Coastguard Worker     darray_free(table->nodes);
86*2b949d04SAndroid Build Coastguard Worker     darray_free(table->utf8);
87*2b949d04SAndroid Build Coastguard Worker     xkb_context_unref(table->ctx);
88*2b949d04SAndroid Build Coastguard Worker     free(table);
89*2b949d04SAndroid Build Coastguard Worker }
90*2b949d04SAndroid Build Coastguard Worker 
91*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_table *
xkb_compose_table_new_from_file(struct xkb_context * ctx,FILE * file,const char * locale,enum xkb_compose_format format,enum xkb_compose_compile_flags flags)92*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_new_from_file(struct xkb_context *ctx,
93*2b949d04SAndroid Build Coastguard Worker                                 FILE *file,
94*2b949d04SAndroid Build Coastguard Worker                                 const char *locale,
95*2b949d04SAndroid Build Coastguard Worker                                 enum xkb_compose_format format,
96*2b949d04SAndroid Build Coastguard Worker                                 enum xkb_compose_compile_flags flags)
97*2b949d04SAndroid Build Coastguard Worker {
98*2b949d04SAndroid Build Coastguard Worker     struct xkb_compose_table *table;
99*2b949d04SAndroid Build Coastguard Worker     bool ok;
100*2b949d04SAndroid Build Coastguard Worker 
101*2b949d04SAndroid Build Coastguard Worker     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
102*2b949d04SAndroid Build Coastguard Worker         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
103*2b949d04SAndroid Build Coastguard Worker         return NULL;
104*2b949d04SAndroid Build Coastguard Worker     }
105*2b949d04SAndroid Build Coastguard Worker 
106*2b949d04SAndroid Build Coastguard Worker     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
107*2b949d04SAndroid Build Coastguard Worker         log_err_func(ctx, "unsupported compose format: %d\n", format);
108*2b949d04SAndroid Build Coastguard Worker         return NULL;
109*2b949d04SAndroid Build Coastguard Worker     }
110*2b949d04SAndroid Build Coastguard Worker 
111*2b949d04SAndroid Build Coastguard Worker     table = xkb_compose_table_new(ctx, locale, format, flags);
112*2b949d04SAndroid Build Coastguard Worker     if (!table)
113*2b949d04SAndroid Build Coastguard Worker         return NULL;
114*2b949d04SAndroid Build Coastguard Worker 
115*2b949d04SAndroid Build Coastguard Worker     ok = parse_file(table, file, "(unknown file)");
116*2b949d04SAndroid Build Coastguard Worker     if (!ok) {
117*2b949d04SAndroid Build Coastguard Worker         xkb_compose_table_unref(table);
118*2b949d04SAndroid Build Coastguard Worker         return NULL;
119*2b949d04SAndroid Build Coastguard Worker     }
120*2b949d04SAndroid Build Coastguard Worker 
121*2b949d04SAndroid Build Coastguard Worker     return table;
122*2b949d04SAndroid Build Coastguard Worker }
123*2b949d04SAndroid Build Coastguard Worker 
124*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_table *
xkb_compose_table_new_from_buffer(struct xkb_context * ctx,const char * buffer,size_t length,const char * locale,enum xkb_compose_format format,enum xkb_compose_compile_flags flags)125*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
126*2b949d04SAndroid Build Coastguard Worker                                   const char *buffer, size_t length,
127*2b949d04SAndroid Build Coastguard Worker                                   const char *locale,
128*2b949d04SAndroid Build Coastguard Worker                                   enum xkb_compose_format format,
129*2b949d04SAndroid Build Coastguard Worker                                   enum xkb_compose_compile_flags flags)
130*2b949d04SAndroid Build Coastguard Worker {
131*2b949d04SAndroid Build Coastguard Worker     struct xkb_compose_table *table;
132*2b949d04SAndroid Build Coastguard Worker     bool ok;
133*2b949d04SAndroid Build Coastguard Worker 
134*2b949d04SAndroid Build Coastguard Worker     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
135*2b949d04SAndroid Build Coastguard Worker         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
136*2b949d04SAndroid Build Coastguard Worker         return NULL;
137*2b949d04SAndroid Build Coastguard Worker     }
138*2b949d04SAndroid Build Coastguard Worker 
139*2b949d04SAndroid Build Coastguard Worker     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
140*2b949d04SAndroid Build Coastguard Worker         log_err_func(ctx, "unsupported compose format: %d\n", format);
141*2b949d04SAndroid Build Coastguard Worker         return NULL;
142*2b949d04SAndroid Build Coastguard Worker     }
143*2b949d04SAndroid Build Coastguard Worker 
144*2b949d04SAndroid Build Coastguard Worker     table = xkb_compose_table_new(ctx, locale, format, flags);
145*2b949d04SAndroid Build Coastguard Worker     if (!table)
146*2b949d04SAndroid Build Coastguard Worker         return NULL;
147*2b949d04SAndroid Build Coastguard Worker 
148*2b949d04SAndroid Build Coastguard Worker     ok = parse_string(table, buffer, length, "(input string)");
149*2b949d04SAndroid Build Coastguard Worker     if (!ok) {
150*2b949d04SAndroid Build Coastguard Worker         xkb_compose_table_unref(table);
151*2b949d04SAndroid Build Coastguard Worker         return NULL;
152*2b949d04SAndroid Build Coastguard Worker     }
153*2b949d04SAndroid Build Coastguard Worker 
154*2b949d04SAndroid Build Coastguard Worker     return table;
155*2b949d04SAndroid Build Coastguard Worker }
156*2b949d04SAndroid Build Coastguard Worker 
157*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_table *
xkb_compose_table_new_from_locale(struct xkb_context * ctx,const char * locale,enum xkb_compose_compile_flags flags)158*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_new_from_locale(struct xkb_context *ctx,
159*2b949d04SAndroid Build Coastguard Worker                                   const char *locale,
160*2b949d04SAndroid Build Coastguard Worker                                   enum xkb_compose_compile_flags flags)
161*2b949d04SAndroid Build Coastguard Worker {
162*2b949d04SAndroid Build Coastguard Worker     struct xkb_compose_table *table;
163*2b949d04SAndroid Build Coastguard Worker     char *path;
164*2b949d04SAndroid Build Coastguard Worker     FILE *file;
165*2b949d04SAndroid Build Coastguard Worker     bool ok;
166*2b949d04SAndroid Build Coastguard Worker 
167*2b949d04SAndroid Build Coastguard Worker     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
168*2b949d04SAndroid Build Coastguard Worker         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
169*2b949d04SAndroid Build Coastguard Worker         return NULL;
170*2b949d04SAndroid Build Coastguard Worker     }
171*2b949d04SAndroid Build Coastguard Worker 
172*2b949d04SAndroid Build Coastguard Worker     table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
173*2b949d04SAndroid Build Coastguard Worker                                   flags);
174*2b949d04SAndroid Build Coastguard Worker     if (!table)
175*2b949d04SAndroid Build Coastguard Worker         return NULL;
176*2b949d04SAndroid Build Coastguard Worker 
177*2b949d04SAndroid Build Coastguard Worker     path = get_xcomposefile_path();
178*2b949d04SAndroid Build Coastguard Worker     if (path) {
179*2b949d04SAndroid Build Coastguard Worker         file = fopen(path, "rb");
180*2b949d04SAndroid Build Coastguard Worker         if (file)
181*2b949d04SAndroid Build Coastguard Worker             goto found_path;
182*2b949d04SAndroid Build Coastguard Worker     }
183*2b949d04SAndroid Build Coastguard Worker     free(path);
184*2b949d04SAndroid Build Coastguard Worker 
185*2b949d04SAndroid Build Coastguard Worker     path = get_xdg_xcompose_file_path();
186*2b949d04SAndroid Build Coastguard Worker     if (path) {
187*2b949d04SAndroid Build Coastguard Worker         file = fopen(path, "rb");
188*2b949d04SAndroid Build Coastguard Worker         if (file)
189*2b949d04SAndroid Build Coastguard Worker             goto found_path;
190*2b949d04SAndroid Build Coastguard Worker     }
191*2b949d04SAndroid Build Coastguard Worker     free(path);
192*2b949d04SAndroid Build Coastguard Worker 
193*2b949d04SAndroid Build Coastguard Worker     path = get_home_xcompose_file_path();
194*2b949d04SAndroid Build Coastguard Worker     if (path) {
195*2b949d04SAndroid Build Coastguard Worker         file = fopen(path, "rb");
196*2b949d04SAndroid Build Coastguard Worker         if (file)
197*2b949d04SAndroid Build Coastguard Worker             goto found_path;
198*2b949d04SAndroid Build Coastguard Worker     }
199*2b949d04SAndroid Build Coastguard Worker     free(path);
200*2b949d04SAndroid Build Coastguard Worker 
201*2b949d04SAndroid Build Coastguard Worker     path = get_locale_compose_file_path(table->locale);
202*2b949d04SAndroid Build Coastguard Worker     if (path) {
203*2b949d04SAndroid Build Coastguard Worker         file = fopen(path, "rb");
204*2b949d04SAndroid Build Coastguard Worker         if (file)
205*2b949d04SAndroid Build Coastguard Worker             goto found_path;
206*2b949d04SAndroid Build Coastguard Worker     }
207*2b949d04SAndroid Build Coastguard Worker     free(path);
208*2b949d04SAndroid Build Coastguard Worker 
209*2b949d04SAndroid Build Coastguard Worker     log_err(ctx, "couldn't find a Compose file for locale \"%s\" (mapped to \"%s\")\n",
210*2b949d04SAndroid Build Coastguard Worker             locale, table->locale);
211*2b949d04SAndroid Build Coastguard Worker     xkb_compose_table_unref(table);
212*2b949d04SAndroid Build Coastguard Worker     return NULL;
213*2b949d04SAndroid Build Coastguard Worker 
214*2b949d04SAndroid Build Coastguard Worker found_path:
215*2b949d04SAndroid Build Coastguard Worker     ok = parse_file(table, file, path);
216*2b949d04SAndroid Build Coastguard Worker     fclose(file);
217*2b949d04SAndroid Build Coastguard Worker     if (!ok) {
218*2b949d04SAndroid Build Coastguard Worker         free(path);
219*2b949d04SAndroid Build Coastguard Worker         xkb_compose_table_unref(table);
220*2b949d04SAndroid Build Coastguard Worker         return NULL;
221*2b949d04SAndroid Build Coastguard Worker     }
222*2b949d04SAndroid Build Coastguard Worker 
223*2b949d04SAndroid Build Coastguard Worker     log_dbg(ctx, "created compose table from locale %s with path %s\n",
224*2b949d04SAndroid Build Coastguard Worker             table->locale, path);
225*2b949d04SAndroid Build Coastguard Worker 
226*2b949d04SAndroid Build Coastguard Worker     free(path);
227*2b949d04SAndroid Build Coastguard Worker     return table;
228*2b949d04SAndroid Build Coastguard Worker }
229