Debian Codegraph @

src/compose/parser.c

raw
1 /*
2 * For HPND-sell-variant:
3 * Copyright 1992 by Oki Technosystems Laboratory, Inc.
4 * Copyright 1992 by Fuji Xerox Co., Ltd.
5 *
6 * For MIT:
7 * Copyright © 2013 Ran Benita <[email protected]>
8 *
9 * SPDX-License-Identifier: HPND-sell-variant AND MIT
10 *
11 * Author: Yasuhiro Kawai Oki Technosystems Laboratory
12 * Author: Kazunori Nishihara Fuji Xerox
13 */
14
15 #include "config.h"
16
17 #include <errno.h>
18 #include <string.h>
19
20 #include "xkbcommon/xkbcommon-keysyms.h"
21 #include "darray.h"
22 #include "messages-codes.h"
23 #include "utils.h"
24 #include "constants.h"
25 #include "table.h"
26 #include "scanner-utils.h"
27 #include "paths.h"
28 #include "utf8.h"
29 #include "parser.h"
30 #include "keysym.h"
31
32 /*
33 * Grammar adapted from libX11/modules/im/ximcp/imLcPrs.c.
34 * See also the XCompose(5) manpage.
35 *
36 * FILE ::= { [PRODUCTION] [COMMENT] "\n" | INCLUDE }
37 * INCLUDE ::= "include" '"' INCLUDE_STRING '"'
38 * PRODUCTION ::= LHS ":" RHS [ COMMENT ]
39 * COMMENT ::= "#" {<any character except null or newline>}
40 * LHS ::= EVENT { EVENT }
41 * EVENT ::= [MODIFIER_LIST] "<" keysym ">"
42 * MODIFIER_LIST ::= (["!"] {MODIFIER} ) | "None"
43 * MODIFIER ::= ["~"] MODIFIER_NAME
44 * MODIFIER_NAME ::= ("Ctrl"|"Lock"|"Caps"|"Shift"|"Alt"|"Meta")
45 * RHS ::= ( STRING | keysym | STRING keysym )
46 * STRING ::= '"' { CHAR } '"'
47 * CHAR ::= GRAPHIC_CHAR | ESCAPED_CHAR
48 * GRAPHIC_CHAR ::= locale (codeset) dependent code
49 * ESCAPED_CHAR ::= ('\\' | '\"' | OCTAL | HEX )
50 * OCTAL ::= '\' OCTAL_CHAR [OCTAL_CHAR [OCTAL_CHAR]]
51 * OCTAL_CHAR ::= (0|1|2|3|4|5|6|7)
52 * HEX ::= '\' (x|X) HEX_CHAR [HEX_CHAR]]
53 * HEX_CHAR ::= (0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|a|b|c|d|e|f)
54 *
55 * INCLUDE_STRING is a filesystem path, with the following %-expansions:
56 * %% - '%'.
57 * %H - The user's home directory (the $HOME environment variable).
58 * %L - The name of the locale specific Compose file (e.g.,
59 * "/usr/share/X11/locale/<localename>/Compose").
60 * %S - The name of the system directory for Compose files (e.g.,
61 * "/usr/share/X11/locale").
62 */
63
64 enum rules_token {
65 TOK_END_OF_FILE = 0,
66 TOK_END_OF_LINE,
67 TOK_INCLUDE,
68 TOK_INCLUDE_STRING,
69 TOK_LHS_KEYSYM,
70 TOK_COLON,
71 TOK_BANG,
72 TOK_TILDE,
73 TOK_STRING,
74 TOK_IDENT,
75 TOK_ERROR
76 };
77
78 /* Values returned with some tokens, like yylval. */
79 union lvalue {
80 struct {
81 /* Still \0-terminated. */
82 const char *str;
83 size_t len;
84 } string;
85 };
86
87 static enum rules_token
88 lex(struct scanner *s, union lvalue *val)
89 {
90 skip_more_whitespace_and_comments:
91 /* Skip spaces. */
92 while (is_space(scanner_peek(s)))
93 if (scanner_next(s) == '\n')
94 return TOK_END_OF_LINE;
95
96 /* Skip comments. */
97 if (scanner_chr(s, '#')) {
98 scanner_skip_to_eol(s);
99 goto skip_more_whitespace_and_comments;
100 }
101
102 /* See if we're done. */
103 if (scanner_eof(s)) return TOK_END_OF_FILE;
104
105 /* New token. */
106 s->token_pos = s->pos;
107 s->buf_pos = 0;
108
109 /* LHS Keysym. */
110 if (scanner_chr(s, '<')) {
111 while (scanner_peek(s) != '>' && !scanner_eol(s) && !scanner_eof(s))
112 scanner_buf_append(s, scanner_next(s));
113 if (!scanner_chr(s, '>')) {
114 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
115 "unterminated keysym literal");
116 return TOK_ERROR;
117 }
118 if (!scanner_buf_append(s, '\0')) {
119 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
120 "keysym literal is too long");
121 return TOK_ERROR;
122 }
123 val->string.str = s->buf;
124 val->string.len = s->buf_pos;
125 return TOK_LHS_KEYSYM;
126 }
127
128 /* Colon. */
129 if (scanner_chr(s, ':'))
130 return TOK_COLON;
131 if (scanner_chr(s, '!'))
132 return TOK_BANG;
133 if (scanner_chr(s, '~'))
134 return TOK_TILDE;
135
136 /* String literal. */
137 if (scanner_chr(s, '\"')) {
138 while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '\"') {
139 if (scanner_chr(s, '\\')) {
140 uint8_t o;
141 size_t start_pos = s->pos;
142 if (scanner_chr(s, '\\')) {
143 scanner_buf_append(s, '\\');
144 }
145 else if (scanner_chr(s, '"')) {
146 scanner_buf_append(s, '"');
147 }
148 else if (scanner_chr(s, 'x') || scanner_chr(s, 'X')) {
149 if (scanner_hex(s, &o) && is_valid_char((char) o)) {
150 scanner_buf_append(s, (char) o);
151 } else {
152 scanner_warn(s, XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
153 "illegal hexadecimal escape sequence "
154 "\"%.*s\" in string literal",
155 (int) (s->pos - start_pos + 1),
156 &s->s[start_pos - 1]);
157 }
158 }
159 else if (scanner_oct(s, &o) && is_valid_char((char) o)) {
160 scanner_buf_append(s, (char) o);
161 }
162 else if (s->pos > start_pos) {
163 scanner_warn(s, XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
164 "illegal octal escape sequence \"%.*s\" "
165 "in string literal",
166 (int) (s->pos - start_pos + 1),
167 &s->s[start_pos - 1]);
168 /* Ignore. */
169 }
170 else {
171 scanner_warn(s, XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE,
172 "unknown escape sequence \"\\%c\" "
173 "in string literal",
174 scanner_peek(s));
175 /* Ignore. */
176 }
177 } else {
178 scanner_buf_append(s, scanner_next(s));
179 }
180 }
181 if (!scanner_chr(s, '\"')) {
182 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
183 "unterminated string literal");
184 return TOK_ERROR;
185 }
186 if (!scanner_buf_append(s, '\0')) {
187 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
188 "string literal is too long");
189 return TOK_ERROR;
190 }
191 if (!is_valid_utf8(s->buf, s->buf_pos - 1)) {
192 scanner_err(s, XKB_ERROR_INVALID_FILE_ENCODING,
193 "string literal is not a valid UTF-8 string");
194 return TOK_ERROR;
195 }
196 val->string.str = s->buf;
197 val->string.len = s->buf_pos;
198 return TOK_STRING;
199 }
200
201 /* Identifier or include. */
202 if (is_alpha(scanner_peek(s)) || scanner_peek(s) == '_') {
203 s->buf_pos = 0;
204 while (is_alnum(scanner_peek(s)) || scanner_peek(s) == '_')
205 scanner_buf_append(s, scanner_next(s));
206 if (!scanner_buf_append(s, '\0')) {
207 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
208 "identifier is too long");
209 return TOK_ERROR;
210 }
211
212 if (streq(s->buf, "include"))
213 return TOK_INCLUDE;
214
215 val->string.str = s->buf;
216 val->string.len = s->buf_pos;
217 return TOK_IDENT;
218 }
219
220 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
221 "unrecognized token");
222 /* Discard rest of line. */
223 scanner_skip_to_eol(s);
224 return TOK_ERROR;
225 }
226
227 static enum rules_token
228 lex_include_string(struct scanner *s, struct xkb_compose_table *table,
229 union lvalue *val_out)
230 {
231 while (is_space(scanner_peek(s)))
232 if (scanner_next(s) == '\n')
233 return TOK_END_OF_LINE;
234
235 s->token_pos = s->pos;
236 s->buf_pos = 0;
237
238 if (!scanner_chr(s, '\"')) {
239 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
240 "include statement must be followed by a path");
241 return TOK_ERROR;
242 }
243
244 while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '\"') {
245 if (scanner_chr(s, '%')) {
246 if (scanner_chr(s, '%')) {
247 scanner_buf_append(s, '%');
248 }
249 else if (scanner_chr(s, 'H')) {
250 const char *home = xkb_context_getenv(table->ctx, "HOME");
251 if (!home) {
252 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
253 "%%H was used in an include statement, "
254 "but the HOME environment variable is not set");
255 return TOK_ERROR;
256 }
257 if (!scanner_buf_appends(s, home)) {
258 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
259 "include path after expanding %%H is too long");
260 return TOK_ERROR;
261 }
262 }
263 else if (scanner_chr(s, 'L')) {
264 char *path = get_locale_compose_file_path(table->ctx, table->locale);
265 if (!path) {
266 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_LOCALE,
267 "failed to expand %%L to the locale Compose file");
268 return TOK_ERROR;
269 }
270 if (!scanner_buf_appends(s, path)) {
271 free(path);
272 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
273 "include path after expanding %%L is too long");
274 return TOK_ERROR;
275 }
276 free(path);
277 }
278 else if (scanner_chr(s, 'S')) {
279 const char *xlocaledir = get_xlocaledir_path(table->ctx);
280 if (!scanner_buf_appends(s, xlocaledir)) {
281 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
282 "include path after expanding %%S is too long");
283 return TOK_ERROR;
284 }
285 }
286 else {
287 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
288 "unknown %% format (%c) in include statement", scanner_peek(s));
289 return TOK_ERROR;
290 }
291 } else {
292 scanner_buf_append(s, scanner_next(s));
293 }
294 }
295 if (!scanner_chr(s, '\"')) {
296 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
297 "unterminated include statement");
298 return TOK_ERROR;
299 }
300 if (!scanner_buf_append(s, '\0')) {
301 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
302 "include path is too long");
303 return TOK_ERROR;
304 }
305 val_out->string.str = s->buf;
306 val_out->string.len = s->buf_pos;
307 return TOK_INCLUDE_STRING;
308 }
309
310 struct production {
311 xkb_keysym_t lhs[COMPOSE_MAX_LHS_LEN];
312 unsigned int len;
313 xkb_keysym_t keysym;
314 char string[XKB_COMPOSE_MAX_STRING_SIZE];
315 /* At least one of these is true. */
316 bool has_keysym;
317 bool has_string;
318
319 /* The matching is as follows: (active_mods & modmask) == mods. */
320 xkb_mod_mask_t modmask;
321 xkb_mod_mask_t mods;
322 };
323
324 static void
325 add_production(struct xkb_compose_table *table, struct scanner *s,
326 const struct production *production)
327 {
328 unsigned int lhs_pos = 0;
329 uint32_t curr = darray_size(table->nodes) == 1 ? 0 : 1;
330 uint32_t *pptr = NULL;
331 struct compose_node *node = NULL;
332
333 /* Warn before potentially going over the limit, discard silently after. */
334 if (darray_size(table->nodes) + production->len + COMPOSE_MAX_LHS_LEN >
335 MAX_COMPOSE_NODES)
336 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
337 "too many sequences for one Compose file; "
338 "will ignore further lines");
339 if (darray_size(table->nodes) + production->len >= MAX_COMPOSE_NODES)
340 return;
341
342 /*
343 * Insert the sequence to the ternary search tree, creating new nodes as
344 * needed.
345 *
346 * TODO: We insert in the order given, this means some inputs can create
347 * long O(n) chains, which results in total O(n^2) parsing time. We should
348 * ensure the tree is reasonably balanced somehow.
349 */
350 while (true) {
351 const xkb_keysym_t keysym = production->lhs[lhs_pos];
352 const bool last = lhs_pos + 1 == production->len;
353
354 if (curr == 0) {
355 /*
356 * Create a new node and update the parent pointer to it.
357 * Update the pointer first because the append invalidates it.
358 */
359 struct compose_node new = {
360 .keysym = keysym,
361 .lokid = 0,
362 .hikid = 0,
363 .internal = {
364 .eqkid = 0,
365 .is_leaf = false,
366 },
367 };
368 curr = darray_size(table->nodes);
369 if (pptr != NULL) {
370 *pptr = curr;
371 pptr = NULL;
372 }
373 darray_append(table->nodes, new);
374 }
375
376 node = &darray_item(table->nodes, curr);
377
378 if (keysym < node->keysym) {
379 pptr = &node->lokid;
380 curr = node->lokid;
381 } else if (keysym > node->keysym) {
382 pptr = &node->hikid;
383 curr = node->hikid;
384 } else if (!last) {
385 if (node->is_leaf) {
386 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
387 "a sequence already exists which is a prefix of "
388 "this sequence; overriding");
389 node->internal.eqkid = 0;
390 node->internal.is_leaf = false;
391 }
392 lhs_pos++;
393 pptr = &node->internal.eqkid;
394 curr = node->internal.eqkid;
395 } else {
396 if (node->is_leaf) {
397 bool same_string =
398 (node->leaf.utf8 == 0 && !production->has_string) ||
399 (
400 node->leaf.utf8 != 0 && production->has_string &&
401 streq(&darray_item(table->utf8, node->leaf.utf8),
402 production->string)
403 );
404 bool same_keysym =
405 (node->leaf.keysym == XKB_KEY_NoSymbol && !production->has_keysym) ||
406 (
407 node->leaf.keysym != XKB_KEY_NoSymbol && production->has_keysym &&
408 node->leaf.keysym == production->keysym
409 );
410 if (same_string && same_keysym) {
411 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
412 "this compose sequence is a duplicate of another; "
413 "skipping line");
414 return;
415 } else {
416 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
417 "this compose sequence already exists; overriding");
418 }
419 } else if (node->internal.eqkid != 0) {
420 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
421 "this compose sequence is a prefix of another; "
422 "overriding");
423 node->internal.eqkid = 0;
424 }
425
426 /* NOTE: If there was a previous entry, its string may *not* be
427 * reused in the UTF8 table and the corresponding memory is then
428 * wasted! */
429 if (production->has_string) {
430 const size_t len = strlen(production->string);
431 if (node->is_leaf && node->leaf.utf8 &&
432 len <= strlen(&darray_item(table->utf8, node->leaf.utf8))) {
433 /* There is a previous entry with an overwritable string */
434 memcpy(&darray_item(table->utf8, node->leaf.utf8),
435 production->string, len + 1);
436 } else {
437 /* Cannot reuse string: allocate new one */
438 node->leaf.utf8 = darray_size(table->utf8);
439 darray_append_items(table->utf8, production->string,
440 (darray_size_t)strlen(production->string) + 1);
441 }
442 } else {
443 /* Ensure we reset possible previous entry */
444 node->leaf.utf8 = 0;
445 }
446
447 if (production->has_keysym) {
448 node->leaf.keysym = production->keysym;
449 } else {
450 /* Ensure we reset possible previous entry */
451 node->leaf.keysym = XKB_KEY_NoSymbol;
452 }
453
454 node->is_leaf = true;
455 return;
456 }
457 }
458 }
459
460 /* Should match resolve_modifier(). */
461 #define ALL_MODS_MASK ((1u << 0) | (1u << 1) | (1u << 2) | (1u << 3))
462
463 static xkb_mod_index_t
464 resolve_modifier(const char *name)
465 {
466 static const struct {
467 const char *name;
468 xkb_mod_index_t mod;
469 } mods[] = {
470 { "Shift", 0 },
471 { "Ctrl", 2 },
472 { "Alt", 3 },
473 { "Meta", 3 },
474 { "Lock", 1 },
475 { "Caps", 1 },
476 };
477
478 for (unsigned int i = 0; i < ARRAY_SIZE(mods); i++)
479 if (streq(name, mods[i].name))
480 return mods[i].mod;
481
482 return XKB_MOD_INVALID;
483 }
484
485 /* Parse a string literal ("...") and return the corresponding unescaped string,
486 * or NULL if it fails.
487 * This is aimed only for testing (un)escaping characters. */
488 char *
489 parse_string_literal(struct xkb_context *ctx, const char *string)
490 {
491 struct scanner s;
492 union lvalue val;
493 scanner_init(&s, ctx, string, strlen(string), "(unnamed)", NULL);
494 switch (lex(&s, &val)) {
495 case TOK_STRING:
496 return strdup(val.string.str);
497 default:
498 fprintf(stderr, "ERROR: %s\n", s.s);
499 return NULL;
500 }
501 }
502
503 static bool
504 parse(struct xkb_compose_table *table, struct scanner *s,
505 unsigned int include_depth);
506
507 static bool
508 do_include(struct xkb_compose_table *table, struct scanner *s,
509 const char *path, unsigned int include_depth)
510 {
511 FILE *file;
512 bool ok;
513 char *string;
514 size_t size;
515 struct scanner new_s;
516
517 if (include_depth >= COMPOSE_MAX_INCLUDE_DEPTH) {
518 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
519 "maximum include depth (%u) exceeded; maybe there is an include loop?",
520 COMPOSE_MAX_INCLUDE_DEPTH);
521 return false;
522 }
523
524 file = fopen(path, "rb");
525 if (!file) {
526 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
527 "failed to open included Compose file \"%s\": %s",
528 path, strerror(errno));
529 return false;
530 }
531
532 ok = map_file(file, &string, &size);
533 if (!ok) {
534 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
535 "failed to read included Compose file \"%s\": %s",
536 path, strerror(errno));
537 goto err_file;
538 }
539
540 scanner_init(&new_s, table->ctx, string, size, path, s->priv);
541
542 ok = parse(table, &new_s, include_depth + 1);
543 if (!ok)
544 goto err_unmap;
545
546 err_unmap:
547 unmap_file(string, size);
548 err_file:
549 fclose(file);
550 return ok;
551 }
552
553 static bool
554 parse(struct xkb_compose_table *table, struct scanner *s,
555 unsigned int include_depth)
556 {
557 enum rules_token tok;
558 union lvalue val;
559 xkb_keysym_t keysym;
560 struct production production;
561 enum { MAX_ERRORS = 10 };
562 int num_errors = 0;
563
564 /* Basic detection of wrong character encoding.
565 The first character relevant to the grammar must be ASCII:
566 whitespace, include, modifier list, keysym, comment */
567 if (!scanner_check_supported_char_encoding(s)) {
568 scanner_err(s, XKB_ERROR_INVALID_FILE_ENCODING,
569 "This could be a file encoding issue. "
570 "Supported file encodings are ASCII and UTF-8.");
571 goto fail;
572 }
573
574 initial:
575 production.len = 0;
576 production.has_keysym = false;
577 production.has_string = false;
578 production.mods = 0;
579 production.modmask = 0;
580
581 /* fallthrough */
582
583 initial_eol:
584 switch (tok = lex(s, &val)) {
585 case TOK_END_OF_LINE:
586 goto initial_eol;
587 case TOK_END_OF_FILE:
588 goto finished;
589 case TOK_INCLUDE:
590 goto include;
591 default:
592 goto lhs_tok;
593 }
594
595 include:
596 switch (tok = lex_include_string(s, table, &val)) {
597 case TOK_INCLUDE_STRING:
598 goto include_eol;
599 default:
600 goto unexpected;
601 }
602
603 include_eol:
604 switch (tok = lex(s, &val)) {
605 case TOK_END_OF_LINE:
606 if (!do_include(table, s, val.string.str, include_depth))
607 goto fail;
608 goto initial;
609 default:
610 goto unexpected;
611 }
612
613 lhs:
614 tok = lex(s, &val);
615 lhs_tok:
616 switch (tok) {
617 case TOK_COLON:
618 if (production.len <= 0) {
619 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
620 "expected at least one keysym on left-hand side; "
621 "skipping line");
622 goto skip;
623 }
624 goto rhs;
625 case TOK_IDENT:
626 if (streq(val.string.str, "None")) {
627 production.mods = 0;
628 production.modmask = ALL_MODS_MASK;
629 goto lhs_keysym;
630 }
631 goto lhs_mod_list_tok;
632 case TOK_TILDE:
633 goto lhs_mod_list_tok;
634 case TOK_BANG:
635 production.modmask = ALL_MODS_MASK;
636 goto lhs_mod_list;
637 default:
638 goto lhs_keysym_tok;
639 }
640
641 lhs_keysym:
642 tok = lex(s, &val);
643 lhs_keysym_tok:
644 switch (tok) {
645 case TOK_LHS_KEYSYM:
646 keysym = xkb_keysym_from_name(val.string.str, XKB_KEYSYM_NO_FLAGS);
647 if (keysym == XKB_KEY_NoSymbol) {
648 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
649 "unrecognized keysym \"%s\" on left-hand side",
650 val.string.str);
651 goto error;
652 }
653 check_deprecated_keysyms(scanner_warn, s, s->ctx,
654 keysym, val.string.str, val.string.str, "%s", "");
655 if (production.len + 1 > COMPOSE_MAX_LHS_LEN) {
656 scanner_warn(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
657 "too many keysyms (%u) on left-hand side; skipping line",
658 COMPOSE_MAX_LHS_LEN + 1);
659 goto skip;
660 }
661 production.lhs[production.len++] = keysym;
662 production.mods = 0;
663 production.modmask = 0;
664 goto lhs;
665 default:
666 goto unexpected;
667 }
668
669 lhs_mod_list:
670 tok = lex(s, &val);
671 lhs_mod_list_tok: {
672 bool tilde = false;
673 xkb_mod_index_t mod;
674
675 if (tok != TOK_TILDE && tok != TOK_IDENT)
676 goto lhs_keysym_tok;
677
678 if (tok == TOK_TILDE) {
679 tilde = true;
680 tok = lex(s, &val);
681 }
682
683 if (tok != TOK_IDENT)
684 goto unexpected;
685
686 mod = resolve_modifier(val.string.str);
687 if (mod == XKB_MOD_INVALID) {
688 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
689 "unrecognized modifier \"%s\"",
690 val.string.str);
691 goto error;
692 }
693
694 production.modmask |= UINT32_C(1) << mod;
695 if (tilde)
696 production.mods &= ~(UINT32_C(1) << mod);
697 else
698 production.mods |= UINT32_C(1) << mod;
699
700 goto lhs_mod_list;
701 }
702
703 rhs:
704 switch (tok = lex(s, &val)) {
705 case TOK_STRING:
706 if (production.has_string) {
707 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
708 "right-hand side can have at most one string; "
709 "skipping line");
710 goto skip;
711 }
712 if (val.string.len <= 0) {
713 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
714 "right-hand side string must not be empty; "
715 "skipping line");
716 goto skip;
717 }
718 if (val.string.len > sizeof(production.string)) {
719 scanner_warn(s, XKB_LOG_MESSAGE_NO_ID,
720 "right-hand side string is too long: "
721 "expected max: %zu, got: %zu; skipping line",
722 sizeof(production.string) - 1,
723 val.string.len - 1);
724 goto skip;
725 }
726 memcpy(production.string, val.string.str, val.string.len);
727 production.has_string = true;
728 goto rhs;
729 case TOK_IDENT:
730 keysym = xkb_keysym_from_name(val.string.str, XKB_KEYSYM_NO_FLAGS);
731 if (keysym == XKB_KEY_NoSymbol) {
732 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
733 "unrecognized keysym \"%s\" on right-hand side",
734 val.string.str);
735 goto error;
736 }
737 check_deprecated_keysyms(scanner_warn, s, s->ctx,
738 keysym, val.string.str, val.string.str, "%s", "");
739 if (production.has_keysym) {
740 scanner_warn(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
741 "right-hand side can have at most one keysym; "
742 "skipping line");
743 goto skip;
744 }
745 production.keysym = keysym;
746 production.has_keysym = true;
747 /* fallthrough */
748 case TOK_END_OF_LINE:
749 if (!production.has_string && !production.has_keysym) {
750 scanner_warn(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
751 "right-hand side must have at least one of string "
752 "or keysym; skipping line");
753 goto skip;
754 }
755 add_production(table, s, &production);
756 goto initial;
757 default:
758 goto unexpected;
759 }
760
761 unexpected:
762 if (tok != TOK_ERROR)
763 scanner_err(s, XKB_ERROR_INVALID_COMPOSE_SYNTAX,
764 "unexpected token");
765 error:
766 num_errors++;
767 if (num_errors <= MAX_ERRORS)
768 goto skip;
769
770 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
771 "too many errors");
772 goto fail;
773
774 fail:
775 scanner_err(s, XKB_LOG_MESSAGE_NO_ID,
776 "failed to parse file");
777 return false;
778
779 skip:
780 while (tok != TOK_END_OF_LINE && tok != TOK_END_OF_FILE)
781 tok = lex(s, &val);
782 goto initial;
783
784 finished:
785 return true;
786 }
787
788 bool
789 parse_string(struct xkb_compose_table *table, const char *string, size_t len,
790 const char *file_name)
791 {
792 struct scanner s;
793 scanner_init(&s, table->ctx, string, len, file_name, NULL);
794 if (!parse(table, &s, 0))
795 return false;
796 /* Maybe the allocator can use the excess space. */
797 darray_shrink(table->nodes);
798 darray_shrink(table->utf8);
799 return true;
800 }
801
802 bool
803 parse_file(struct xkb_compose_table *table, FILE *file, const char *file_name)
804 {
805 bool ok;
806 char *string;
807 size_t size;
808
809 ok = map_file(file, &string, &size);
810 if (!ok) {
811 log_err(table->ctx, XKB_LOG_MESSAGE_NO_ID,
812 "Couldn't read Compose file %s: %s\n",
813 file_name, strerror(errno));
814 return false;
815 }
816
817 ok = parse_string(table, string, size, file_name);
818 unmap_file(string, size);
819 return ok;
820 }