Debian Codegraph @

src/compose/table.c

raw
1 /*
2 * Copyright © 2013,2021 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9
10 #include "xkbcommon/xkbcommon.h"
11 #include "messages-codes.h"
12 #include "utils.h"
13 #include "constants.h"
14 #include "table.h"
15 #include "parser.h"
16 #include "paths.h"
17
18 static struct xkb_compose_table *
19 xkb_compose_table_new(struct xkb_context *ctx,
20 const char *locale,
21 enum xkb_compose_format format,
22 enum xkb_compose_compile_flags flags)
23 {
24 char *resolved_locale;
25 struct xkb_compose_table *table;
26 struct compose_node dummy = {0};
27
28 resolved_locale = resolve_locale(ctx, locale);
29 if (!resolved_locale)
30 return NULL;
31
32 table = calloc(1, sizeof(*table));
33 if (!table) {
34 free(resolved_locale);
35 return NULL;
36 }
37
38 table->refcnt = 1;
39 table->ctx = xkb_context_ref(ctx);
40
41 table->locale = resolved_locale;
42 table->format = format;
43 table->flags = flags;
44
45 darray_init(table->nodes);
46 darray_init(table->utf8);
47
48 dummy.keysym = XKB_KEY_NoSymbol;
49 dummy.leaf.is_leaf = true;
50 dummy.leaf.utf8 = 0;
51 dummy.leaf.keysym = XKB_KEY_NoSymbol;
52 darray_append(table->nodes, dummy);
53
54 darray_append(table->utf8, '\0');
55
56 return table;
57 }
58
59 struct xkb_compose_table *
60 xkb_compose_table_ref(struct xkb_compose_table *table)
61 {
62 assert(table->refcnt > 0);
63 table->refcnt++;
64 return table;
65 }
66
67 void
68 xkb_compose_table_unref(struct xkb_compose_table *table)
69 {
70 assert(!table || table->refcnt > 0);
71 if (!table || --table->refcnt > 0)
72 return;
73 free(table->locale);
74 darray_free(table->nodes);
75 darray_free(table->utf8);
76 xkb_context_unref(table->ctx);
77 free(table);
78 }
79
80 struct xkb_compose_table *
81 xkb_compose_table_new_from_file(struct xkb_context *ctx,
82 FILE *file,
83 const char *locale,
84 enum xkb_compose_format format,
85 enum xkb_compose_compile_flags flags)
86 {
87 struct xkb_compose_table *table;
88 bool ok;
89
90 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
91 log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
92 "unrecognized flags: %#x\n", flags);
93 return NULL;
94 }
95
96 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
97 log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
98 "unsupported compose format: %d\n", format);
99 return NULL;
100 }
101
102 table = xkb_compose_table_new(ctx, locale, format, flags);
103 if (!table)
104 return NULL;
105
106 ok = parse_file(table, file, "(unknown file)");
107 if (!ok) {
108 xkb_compose_table_unref(table);
109 return NULL;
110 }
111
112 return table;
113 }
114
115 struct xkb_compose_table *
116 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
117 const char *buffer, size_t length,
118 const char *locale,
119 enum xkb_compose_format format,
120 enum xkb_compose_compile_flags flags)
121 {
122 struct xkb_compose_table *table;
123 bool ok;
124
125 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
126 log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
127 "unrecognized flags: %#x\n", flags);
128 return NULL;
129 }
130
131 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
132 log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
133 "unsupported compose format: %d\n", format);
134 return NULL;
135 }
136
137 table = xkb_compose_table_new(ctx, locale, format, flags);
138 if (!table)
139 return NULL;
140
141 ok = parse_string(table, buffer, length, "(input string)");
142 if (!ok) {
143 xkb_compose_table_unref(table);
144 return NULL;
145 }
146
147 return table;
148 }
149
150 struct xkb_compose_table *
151 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
152 const char *locale,
153 enum xkb_compose_compile_flags flags)
154 {
155 struct xkb_compose_table *table;
156 char *path;
157 FILE *file;
158 bool ok;
159
160 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
161 log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
162 "unrecognized flags: %#x\n", flags);
163 return NULL;
164 }
165
166 table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
167 flags);
168 if (!table)
169 return NULL;
170
171 path = get_xcomposefile_path(ctx);
172 file = open_file(path);
173 if (file)
174 goto found_path;
175 free(path);
176
177 path = get_xdg_xcompose_file_path(ctx);
178 file = open_file(path);
179 if (file)
180 goto found_path;
181 free(path);
182
183 path = get_home_xcompose_file_path(ctx);
184 file = open_file(path);
185 if (file)
186 goto found_path;
187 free(path);
188
189 path = get_locale_compose_file_path(ctx, table->locale);
190 file = open_file(path);
191 if (file)
192 goto found_path;
193 free(path);
194
195 log_err(ctx, XKB_ERROR_INVALID_COMPOSE_LOCALE,
196 "couldn't find a Compose file for locale \"%s\" (mapped to \"%s\")\n",
197 locale, table->locale);
198 xkb_compose_table_unref(table);
199 return NULL;
200
201 found_path:
202 ok = parse_file(table, file, path);
203 fclose(file);
204 if (!ok) {
205 free(path);
206 xkb_compose_table_unref(table);
207 return NULL;
208 }
209
210 log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID,
211 "created compose table from locale %s with path %s\n",
212 table->locale, path);
213
214 free(path);
215 return table;
216 }
217
218 const xkb_keysym_t *
219 xkb_compose_table_entry_sequence(struct xkb_compose_table_entry *entry,
220 size_t *sequence_length)
221 {
222 *sequence_length = entry->sequence_length;
223 return entry->sequence;
224 }
225
226 xkb_keysym_t
227 xkb_compose_table_entry_keysym(struct xkb_compose_table_entry *entry)
228 {
229 return entry->keysym;
230 }
231
232 const char *
233 xkb_compose_table_entry_utf8(struct xkb_compose_table_entry *entry)
234 {
235 return entry->utf8;
236 }
237
238 #if MAX_COMPOSE_NODES_LOG2 > 31
239 #error "Cannot implement bit field xkb_compose_table_iterator_pending_node.offset"
240 #endif
241
242 struct xkb_compose_table_iterator_pending_node {
243 uint32_t offset:31;
244 bool processed:1;
245 };
246
247 struct xkb_compose_table_iterator {
248 struct xkb_compose_table *table;
249 /* Current entry */
250 struct xkb_compose_table_entry entry;
251 /* Stack of pending nodes to process */
252 darray(struct xkb_compose_table_iterator_pending_node) pending_nodes;
253 };
254
255 struct xkb_compose_table_iterator *
256 xkb_compose_table_iterator_new(struct xkb_compose_table *table)
257 {
258 struct xkb_compose_table_iterator *iter;
259 xkb_keysym_t *sequence;
260
261 iter = calloc(1, sizeof(*iter));
262 if (!iter) {
263 return NULL;
264 }
265 iter->table = xkb_compose_table_ref(table);
266 sequence = calloc(COMPOSE_MAX_LHS_LEN, sizeof(xkb_keysym_t));
267 if (!sequence) {
268 free(iter);
269 return NULL;
270 }
271 iter->entry.sequence = sequence;
272 iter->entry.sequence_length = 0;
273
274 darray_init(iter->pending_nodes);
275 /* Short-circuit if table contains only the dummy entry */
276 if (darray_size(table->nodes) == 1) {
277 return iter;
278 }
279 /* Add root node */
280 struct xkb_compose_table_iterator_pending_node pending = {
281 .offset = 1,
282 .processed = false
283 };
284 darray_append(iter->pending_nodes, pending);
285 const struct compose_node *node = &darray_item(iter->table->nodes,
286 pending.offset);
287
288 /* Find the first left-most node and store intermediate nodes as pending */
289 while (node->lokid) {
290 pending.offset = node->lokid;
291 darray_append(iter->pending_nodes, pending);
292 node = &darray_item(iter->table->nodes, pending.offset);
293 };
294
295 return iter;
296 }
297
298 void
299 xkb_compose_table_iterator_free(struct xkb_compose_table_iterator *iter)
300 {
301 xkb_compose_table_unref(iter->table);
302 darray_free(iter->pending_nodes);
303 free(iter->entry.sequence);
304 free(iter);
305 }
306
307 struct xkb_compose_table_entry *
308 xkb_compose_table_iterator_next(struct xkb_compose_table_iterator *iter)
309 {
310 /* Traversal algorithm (simplified):
311 * 1. Resume last pending node from the stack as the current pending node.
312 * 2. If the node is not yet processed, go to 5.
313 * 3. Remove node from the stack and remove last keysym from the entry.
314 * 4. If there is a right arrow, set it as the current pending node
315 * (unprocessed) and go to 6; else go to 1.
316 * 5. Follow down arrow: set the pending node as processed, then:
317 * a) if it is a leaf, complete the entry and return it.
318 * b) else append the child node to the stack and set it as the current
319 * pending node.
320 * 6. Find the next left-most arrow and store intermediate pending nodes.
321 * 7. Go to 5.
322 */
323
324 struct xkb_compose_table_iterator_pending_node *pending;
325 const struct compose_node *node;
326
327 /* Iterator is empty if there is no pending nodes */
328 if (unlikely(darray_empty(iter->pending_nodes))) {
329 return NULL;
330 }
331
332 /* Resume to the last element in the stack */
333 pending = &darray_item(iter->pending_nodes,
334 darray_size(iter->pending_nodes) - 1);
335 node = &darray_item(iter->table->nodes, pending->offset);
336
337 struct xkb_compose_table_iterator_pending_node new = {
338 .processed = false,
339 .offset = 0
340 };
341
342 /* Remove processed leaves until an unprocessed right arrow or parent
343 * is found, then update entry accordingly */
344 while (pending->processed) {
345 /* Remove last keysym */
346 iter->entry.sequence_length--;
347 if (node->hikid) {
348 /* Follow right arrow: replace current pending node */
349 pending->processed = false;
350 pending->offset = node->hikid;
351 /* Process child’s left arrow */
352 goto node_left;
353 } else {
354 /* Remove processed node */
355 darray_remove_last(iter->pending_nodes);
356 if (unlikely(darray_empty(iter->pending_nodes))) {
357 /* No more nodes to process */
358 return NULL;
359 }
360 /* Get parent node */
361 pending = &darray_item(iter->pending_nodes,
362 darray_size(iter->pending_nodes) - 1);
363 node = &darray_item(iter->table->nodes, pending->offset);
364 }
365 }
366
367 while (1) {
368 /* Follow down arrow */
369 pending->processed = true;
370 iter->entry.sequence[iter->entry.sequence_length] = node->keysym;
371 iter->entry.sequence_length++;
372 if (node->is_leaf) {
373 /* Leaf: return entry */
374 iter->entry.keysym = node->leaf.keysym;
375 iter->entry.utf8 = &darray_item(iter->table->utf8, node->leaf.utf8);
376 return &iter->entry;
377 }
378 /* Not a leaf: process child node */
379 new.offset = node->internal.eqkid;
380 darray_append(iter->pending_nodes, new);
381 pending = &darray_item(iter->pending_nodes,
382 darray_size(iter->pending_nodes) - 1);
383 node_left:
384 node = &darray_item(iter->table->nodes, pending->offset);
385 /* Find the next left-most arrow and store intermediate pending nodes */
386 while (node->lokid) {
387 /* Follow left arrow */
388 new.offset = node->lokid;
389 darray_append(iter->pending_nodes, new);
390 pending = &darray_item(iter->pending_nodes,
391 darray_size(iter->pending_nodes) - 1);
392 node = &darray_item(iter->table->nodes, new.offset);
393 }
394 }
395 }