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