fix not to display error log about the absense of compose file
[platform/upstream/libxkbcommon.git] / src / compose / table.c
1 /*
2  * Copyright © 2013,2021 Ran Benita <ran234@gmail.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 "utils.h"
27 #include "table.h"
28 #include "parser.h"
29 #include "paths.h"
30 #include "xkbcommon/xkbcommon.h"
31
32 static struct xkb_compose_table *
33 xkb_compose_table_new(struct xkb_context *ctx,
34                       const char *locale,
35                       enum xkb_compose_format format,
36                       enum xkb_compose_compile_flags flags)
37 {
38     char *resolved_locale;
39     struct xkb_compose_table *table;
40     struct compose_node dummy;
41
42     resolved_locale = resolve_locale(ctx, locale);
43     if (!resolved_locale)
44         return NULL;
45
46     table = calloc(1, sizeof(*table));
47     if (!table) {
48         free(resolved_locale);
49         return NULL;
50     }
51
52     table->refcnt = 1;
53     table->ctx = xkb_context_ref(ctx);
54
55     table->locale = resolved_locale;
56     table->format = format;
57     table->flags = flags;
58
59     darray_init(table->nodes);
60     darray_init(table->utf8);
61
62     dummy.keysym = XKB_KEY_NoSymbol;
63     dummy.leaf.is_leaf = true;
64     dummy.leaf.utf8 = 0;
65     dummy.leaf.keysym = XKB_KEY_NoSymbol;
66     darray_append(table->nodes, dummy);
67
68     darray_append(table->utf8, '\0');
69
70     return table;
71 }
72
73 XKB_EXPORT struct xkb_compose_table *
74 xkb_compose_table_ref(struct xkb_compose_table *table)
75 {
76     table->refcnt++;
77     return table;
78 }
79
80 XKB_EXPORT void
81 xkb_compose_table_unref(struct xkb_compose_table *table)
82 {
83     if (!table || --table->refcnt > 0)
84         return;
85     free(table->locale);
86     darray_free(table->nodes);
87     darray_free(table->utf8);
88     xkb_context_unref(table->ctx);
89     free(table);
90 }
91
92 XKB_EXPORT struct xkb_compose_table *
93 xkb_compose_table_new_from_file(struct xkb_context *ctx,
94                                 FILE *file,
95                                 const char *locale,
96                                 enum xkb_compose_format format,
97                                 enum xkb_compose_compile_flags flags)
98 {
99     struct xkb_compose_table *table;
100     bool ok;
101
102     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
103         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
104         return NULL;
105     }
106
107     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
108         log_err_func(ctx, "unsupported compose format: %d\n", format);
109         return NULL;
110     }
111
112     table = xkb_compose_table_new(ctx, locale, format, flags);
113     if (!table)
114         return NULL;
115
116     ok = parse_file(table, file, "(unknown file)");
117     if (!ok) {
118         xkb_compose_table_unref(table);
119         return NULL;
120     }
121
122     return table;
123 }
124
125 XKB_EXPORT struct xkb_compose_table *
126 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
127                                   const char *buffer, size_t length,
128                                   const char *locale,
129                                   enum xkb_compose_format format,
130                                   enum xkb_compose_compile_flags flags)
131 {
132     struct xkb_compose_table *table;
133     bool ok;
134
135     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
136         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
137         return NULL;
138     }
139
140     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
141         log_err_func(ctx, "unsupported compose format: %d\n", format);
142         return NULL;
143     }
144
145     table = xkb_compose_table_new(ctx, locale, format, flags);
146     if (!table)
147         return NULL;
148
149     ok = parse_string(table, buffer, length, "(input string)");
150     if (!ok) {
151         xkb_compose_table_unref(table);
152         return NULL;
153     }
154
155     return table;
156 }
157
158 XKB_EXPORT struct xkb_compose_table *
159 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
160                                   const char *locale,
161                                   enum xkb_compose_compile_flags flags)
162 {
163     struct xkb_compose_table *table;
164     char *path;
165     FILE *file;
166     bool ok;
167
168     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
169         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
170         return NULL;
171     }
172
173     table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
174                                   flags);
175     if (!table)
176         return NULL;
177
178     path = get_xcomposefile_path(ctx);
179     if (path) {
180         file = fopen(path, "rb");
181         if (file)
182             goto found_path;
183     }
184     free(path);
185
186     path = get_xdg_xcompose_file_path(ctx);
187     if (path) {
188         file = fopen(path, "rb");
189         if (file)
190             goto found_path;
191     }
192     free(path);
193
194     path = get_home_xcompose_file_path(ctx);
195     if (path) {
196         file = fopen(path, "rb");
197         if (file)
198             goto found_path;
199     }
200     free(path);
201
202     path = get_locale_compose_file_path(ctx, table->locale);
203     if (path) {
204         file = fopen(path, "rb");
205         if (file)
206             goto found_path;
207     }
208     free(path);
209
210 // TIZEN_ONLY(20210525)
211 // : fix not to display error log about the absense of compose file
212     if (!strncmp("en_US.UTF-8", locale, 11)) {
213         log_err(ctx, "couldn't find a Compose file for locale \"%s\" (mapped to \"%s\")\n",
214                 locale, table->locale);
215     }
216 // END
217
218     xkb_compose_table_unref(table);
219     return NULL;
220
221 found_path:
222     ok = parse_file(table, file, path);
223     fclose(file);
224     if (!ok) {
225         free(path);
226         xkb_compose_table_unref(table);
227         return NULL;
228     }
229
230     log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID,
231             "created compose table from locale %s with path %s\n",
232             table->locale, path);
233
234     free(path);
235     return table;
236 }
237
238 XKB_EXPORT const xkb_keysym_t *
239 xkb_compose_table_entry_sequence(struct xkb_compose_table_entry *entry,
240                                  size_t *sequence_length)
241 {
242     *sequence_length = entry->sequence_length;
243     return entry->sequence;
244 }
245
246 XKB_EXPORT xkb_keysym_t
247 xkb_compose_table_entry_keysym(struct xkb_compose_table_entry *entry)
248 {
249     return entry->keysym;
250 }
251
252 XKB_EXPORT const char *
253 xkb_compose_table_entry_utf8(struct xkb_compose_table_entry *entry)
254 {
255     return entry->utf8;
256 }
257
258 enum node_direction {
259     NODE_LEFT = 0,
260     NODE_DOWN,
261     NODE_RIGHT,
262     NODE_UP
263 };
264
265 struct xkb_compose_table_iterator_cursor {
266     uint32_t node_offset:30; /* WARNING: ensure it fits MAX_COMPOSE_NODES */
267     uint8_t direction:2;     /* enum node_direction: current direction
268                               * traversing the tree */
269 };
270
271 struct xkb_compose_table_iterator {
272     struct xkb_compose_table *table;
273     /* Current entry */
274     struct xkb_compose_table_entry entry;
275     /* Stack of pending nodes to process */
276     darray(struct xkb_compose_table_iterator_cursor) cursors;
277 };
278
279 XKB_EXPORT struct xkb_compose_table_iterator *
280 xkb_compose_table_iterator_new(struct xkb_compose_table *table)
281 {
282     struct xkb_compose_table_iterator *iter;
283     struct xkb_compose_table_iterator_cursor cursor;
284     xkb_keysym_t *sequence;
285
286     iter = calloc(1, sizeof(*iter));
287     if (!iter) {
288         return NULL;
289     }
290     iter->table = xkb_compose_table_ref(table);
291     sequence = calloc(MAX_LHS_LEN, sizeof(xkb_keysym_t));
292     if (!sequence) {
293         free(iter);
294         return NULL;
295     }
296     iter->entry.sequence = sequence;
297     iter->entry.sequence_length = 0;
298
299     darray_init(iter->cursors);
300     cursor.direction = NODE_LEFT;
301     /* Offset 0 is a dummy null entry, skip it. */
302     cursor.node_offset = 1;
303     darray_append(iter->cursors, cursor);
304
305     return iter;
306 }
307
308 XKB_EXPORT void
309 xkb_compose_table_iterator_free(struct xkb_compose_table_iterator *iter)
310 {
311     xkb_compose_table_unref(iter->table);
312     darray_free(iter->cursors);
313     free(iter->entry.sequence);
314     free(iter);
315 }
316
317 XKB_EXPORT struct xkb_compose_table_entry *
318 xkb_compose_table_iterator_next(struct xkb_compose_table_iterator *iter)
319 {
320     /*
321      * This function takes the following recursive traversal function,
322      * and makes it non-recursive and resumable. The iter->cursors stack
323      * is analogous to the call stack, and cursor->direction to the
324      * instruction pointer of a stack frame.
325      *
326      *    traverse(xkb_keysym_t *sequence, size_t sequence_length, uint16_t p) {
327      *        if (!p) return
328      *        // cursor->direction == NODE_LEFT
329      *        node = &darray_item(table->nodes, p)
330      *        traverse(sequence, sequence_length, node->lokid)
331      *        // cursor->direction == NODE_DOWN
332      *        sequence[sequence_length++] = node->keysym
333      *        if (node->is_leaf)
334      *            emit(sequence, sequence_length, node->leaf.keysym, table->utf[node->leaf.utf8])
335      *        else
336      *            traverse(sequence, sequence_length, node->internal.eqkid)
337      *        sequence_length--
338      *        // cursor->direction == NODE_RIGHT
339      *        traverse(sequence, sequence_length, node->hikid)
340      *        // cursor->direction == NODE_UP
341      *    }
342      */
343
344     struct xkb_compose_table_iterator_cursor *cursor;
345     const struct compose_node *node;
346
347     while (!darray_empty(iter->cursors)) {
348         cursor = &darray_item(iter->cursors, darray_size(iter->cursors) - 1);
349         node = &darray_item(iter->table->nodes, cursor->node_offset);
350
351         switch (cursor->direction) {
352         case NODE_LEFT:
353             cursor->direction = NODE_DOWN;
354             if (node->lokid) {
355                 struct xkb_compose_table_iterator_cursor new_cursor = {node->lokid, NODE_LEFT};
356                 darray_append(iter->cursors, new_cursor);
357             }
358             break;
359
360         case NODE_DOWN:
361             cursor->direction = NODE_RIGHT;
362             assert (iter->entry.sequence_length <= MAX_LHS_LEN);
363             iter->entry.sequence[iter->entry.sequence_length] = node->keysym;
364             iter->entry.sequence_length++;
365             if (node->is_leaf) {
366                 iter->entry.keysym = node->leaf.keysym;
367                 iter->entry.utf8 = &darray_item(iter->table->utf8, node->leaf.utf8);
368                 return &iter->entry;
369             } else {
370                 struct xkb_compose_table_iterator_cursor new_cursor = {node->internal.eqkid, NODE_LEFT};
371                 darray_append(iter->cursors, new_cursor);
372             }
373             break;
374
375         case NODE_RIGHT:
376             cursor->direction = NODE_UP;
377             iter->entry.sequence_length--;
378             if (node->hikid) {
379                 struct xkb_compose_table_iterator_cursor new_cursor = {node->hikid, NODE_LEFT};
380                 darray_append(iter->cursors, new_cursor);
381             }
382             break;
383
384         case NODE_UP:
385             darray_remove_last(iter->cursors);
386             break;
387         }
388     }
389
390     return NULL;
391 }