Debian Codegraph @

src/xkbcomp/compat.c

raw
1 /*
2 * For HPND:
3 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 *
5 * For MIT:
6 * Copyright © 2012 Ran Benita <[email protected]>
7 *
8 * SPDX-License-Identifier: HPND AND MIT
9 */
10
11 #include "config.h"
12
13 #include <assert.h>
14 #include <limits.h>
15
16 #include "xkbcommon/xkbcommon.h"
17 #include "darray.h"
18 #include "keymap.h"
19 #include "xkbcomp-priv.h"
20 #include "ast.h"
21 #include "action.h"
22 #include "expr.h"
23 #include "include.h"
24 #include "text.h"
25 #include "vmod.h"
26 #include "util-mem.h"
27
28 enum si_field {
29 SI_FIELD_VIRTUAL_MOD = (1 << 0),
30 SI_FIELD_ACTION = (1 << 1),
31 SI_FIELD_AUTO_REPEAT = (1 << 2),
32 SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
33 };
34
35 typedef struct {
36 enum si_field defined;
37 enum merge_mode merge;
38
39 struct xkb_sym_interpret interp;
40 } SymInterpInfo;
41
42 enum led_field {
43 LED_FIELD_MODS = (1 << 0),
44 LED_FIELD_GROUPS = (1 << 1),
45 LED_FIELD_CTRLS = (1 << 2),
46 };
47
48 typedef struct {
49 enum led_field defined;
50 enum merge_mode merge;
51
52 struct xkb_led led;
53 } LedInfo;
54
55 typedef struct {
56 char *name;
57 int errorCount;
58 unsigned int include_depth;
59 SymInterpInfo default_interp;
60 darray(SymInterpInfo) interps;
61 LedInfo default_led;
62 LedInfo leds[XKB_MAX_LEDS];
63 xkb_led_index_t num_leds;
64 ActionsInfo default_actions;
65 struct xkb_mod_set mods;
66
67 enum xkb_keymap_format format;
68 struct xkb_context *ctx;
69 } CompatInfo;
70
71 static const char *
72 siText(SymInterpInfo *si, CompatInfo *info)
73 {
74 char *buf = xkb_context_get_buffer(info->ctx, 128);
75
76 if (si == &info->default_interp)
77 return "default";
78
79 snprintf(buf, 128, "%s+%s(%s)",
80 KeysymText(info->ctx, si->interp.sym),
81 SIMatchText(si->interp.match),
82 ModMaskText(info->ctx, MOD_BOTH, &info->mods, si->interp.mods));
83
84 return buf;
85 }
86
87 static inline bool
88 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
89 {
90 return ReportNotArray(info->ctx, "symbol interpretation", field,
91 siText(si, info));
92 }
93
94 static inline bool
95 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
96 const char *wanted)
97 {
98 return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
99 "symbol interpretation", field,
100 siText(si, info), wanted);
101 }
102
103 static const char*
104 LEDText(CompatInfo *info, LedInfo *ledi)
105 {
106 if (ledi == &info->default_led) {
107 assert(xkb_atom_text(info->ctx, ledi->led.name) == NULL);
108 return "default";
109 } else {
110 assert(xkb_atom_text(info->ctx, ledi->led.name) != NULL);
111 return xkb_atom_text(info->ctx, ledi->led.name);
112 }
113 }
114
115 static inline bool
116 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
117 const char *wanted)
118 {
119 return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
120 "indicator map",
121 field, LEDText(info, ledi), wanted);
122 }
123
124 static inline bool
125 ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)
126 {
127 return ReportNotArray(info->ctx, "indicator map", field,
128 LEDText(info, ledi));
129 }
130
131 static inline void
132 InitInterp(SymInterpInfo *info)
133 {
134 info->merge = MERGE_DEFAULT; /* Unused */
135 info->interp.virtual_mod = XKB_MOD_INVALID;
136 }
137
138 static inline void
139 InitLED(LedInfo *info)
140 {
141 info->merge = MERGE_DEFAULT; /* Unused */
142 }
143
144 static void
145 InitCompatInfo(CompatInfo *info, struct xkb_context *ctx,
146 unsigned int include_depth, enum xkb_keymap_format format,
147 const struct xkb_mod_set *mods)
148 {
149 memset(info, 0, sizeof(*info));
150 info->ctx = ctx;
151 info->format = format;
152 info->include_depth = include_depth;
153 InitActionsInfo(&info->default_actions);
154 InitVMods(&info->mods, mods, include_depth > 0);
155 InitInterp(&info->default_interp);
156 InitLED(&info->default_led);
157 }
158
159 static void
160 ClearCompatInfo(CompatInfo *info)
161 {
162 free(info->name);
163 darray_free(info->interps);
164 }
165
166 static SymInterpInfo *
167 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
168 {
169 SymInterpInfo *old;
170
171 darray_foreach(old, info->interps)
172 if (old->interp.sym == new->interp.sym &&
173 old->interp.mods == new->interp.mods &&
174 old->interp.match == new->interp.match)
175 return old;
176
177 return NULL;
178 }
179
180 static bool
181 UseNewInterpField(enum si_field field, enum si_field old, enum si_field new,
182 bool clobber, bool report, enum si_field *collide)
183 {
184 if (!(old & field))
185 return (new & field);
186
187 if (new & field) {
188 if (report)
189 *collide |= field;
190
191 return clobber;
192 }
193
194 return false;
195 }
196
197 static bool
198 MergeInterp(CompatInfo *info, SymInterpInfo *old, SymInterpInfo *new,
199 bool same_file)
200 {
201 const bool clobber = (new->merge != MERGE_AUGMENT);
202 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
203 const bool report = (same_file && verbosity > 0) || verbosity > 9;
204 enum si_field collide = 0;
205
206 if (new->merge == MERGE_REPLACE) {
207 if (report)
208 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
209 "Multiple definitions for \"%s\"; "
210 "Earlier interpretation ignored\n",
211 siText(new, info));
212 *old = *new;
213 return true;
214 }
215
216 if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old->defined, new->defined,
217 clobber, report, &collide)) {
218 old->interp.virtual_mod = new->interp.virtual_mod;
219 old->defined |= SI_FIELD_VIRTUAL_MOD;
220 }
221 if (UseNewInterpField(SI_FIELD_ACTION, old->defined, new->defined,
222 clobber, report, &collide)) {
223 if (old->interp.num_actions > 1) {
224 free(old->interp.a.actions);
225 }
226 old->interp.num_actions = new->interp.num_actions;
227 if (new->interp.num_actions > 1) {
228 old->interp.a.actions = new->interp.a.actions;
229 new->interp.a.action =
230 (union xkb_action) { .type = ACTION_TYPE_NONE };
231 new->interp.num_actions = 0;
232 } else {
233 old->interp.a.action = new->interp.a.action;
234 }
235 old->defined |= SI_FIELD_ACTION;
236 }
237 if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old->defined, new->defined,
238 clobber, report, &collide)) {
239 old->interp.repeat = new->interp.repeat;
240 old->defined |= SI_FIELD_AUTO_REPEAT;
241 }
242 if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old->defined, new->defined,
243 clobber, report, &collide)) {
244 old->interp.level_one_only = new->interp.level_one_only;
245 old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
246 }
247
248 if (collide) {
249 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
250 "Multiple interpretations of \"%s\"; "
251 "Using %s definition for duplicate fields\n",
252 siText(old, info),
253 (clobber ? "last" : "first"));
254 }
255
256 return true;
257 }
258
259 static bool
260 AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
261 {
262 SymInterpInfo *old = FindMatchingInterp(info, new);
263 if (old)
264 return MergeInterp(info, old, new, same_file);
265
266 darray_append(info->interps, *new);
267 return true;
268 }
269
270 /***====================================================================***/
271
272 static bool
273 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
274 xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
275 {
276 if (expr == NULL) {
277 *pred_rtrn = MATCH_ANY_OR_NONE;
278 *mods_rtrn = MOD_REAL_MASK_ALL;
279 return true;
280 }
281
282 *pred_rtrn = MATCH_EXACTLY;
283 if (expr->common.type == STMT_EXPR_ACTION_DECL) {
284 const char *pred_txt = xkb_atom_text(info->ctx, expr->action.name);
285 unsigned int pred = 0;
286 if (!LookupString(symInterpretMatchMaskNames, pred_txt, &pred) ||
287 !expr->action.args || expr->action.args->common.next) {
288 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
289 "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
290 return false;
291 }
292 *pred_rtrn = (enum xkb_match_operation) pred;
293 expr = expr->action.args;
294 }
295 else if (expr->common.type == STMT_EXPR_IDENT) {
296 const char *pred_txt = xkb_atom_text(info->ctx, expr->ident.ident);
297 if (pred_txt && istreq(pred_txt, "any")) {
298 *pred_rtrn = MATCH_ANY;
299 *mods_rtrn = MOD_REAL_MASK_ALL;
300 return true;
301 }
302 }
303
304 return ExprResolveModMask(info->ctx, expr, MOD_REAL, &info->mods,
305 mods_rtrn);
306 }
307
308 /***====================================================================***/
309
310 static bool
311 UseNewLEDField(enum led_field field, enum led_field old, enum led_field new,
312 bool clobber, bool report, enum led_field *collide)
313 {
314 if (!(old & field))
315 return (new & field);
316
317 if (new & field) {
318 if (report)
319 *collide |= field;
320
321 return clobber;
322 }
323
324 return false;
325 }
326
327 static bool
328 MergeLedMap(CompatInfo *info, LedInfo *old, LedInfo *new, bool same_file)
329 {
330 enum led_field collide;
331 const bool clobber = (new->merge != MERGE_AUGMENT);
332 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
333 const bool report = (same_file && verbosity > 0) || verbosity > 9;
334
335 if (old->led.mods.mods == new->led.mods.mods &&
336 old->led.groups == new->led.groups &&
337 old->led.ctrls == new->led.ctrls &&
338 old->led.which_mods == new->led.which_mods &&
339 old->led.which_groups == new->led.which_groups) {
340 old->defined |= new->defined;
341 return true;
342 }
343
344 if (new->merge == MERGE_REPLACE) {
345 if (report)
346 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
347 "Map for indicator %s redefined; "
348 "Earlier definition ignored\n",
349 LEDText(info, old));
350 *old = *new;
351 return true;
352 }
353
354 collide = 0;
355 if (UseNewLEDField(LED_FIELD_MODS, old->defined, new->defined,
356 clobber, report, &collide)) {
357 old->led.which_mods = new->led.which_mods;
358 old->led.mods = new->led.mods;
359 old->defined |= LED_FIELD_MODS;
360 }
361 if (UseNewLEDField(LED_FIELD_GROUPS, old->defined, new->defined,
362 clobber, report, &collide)) {
363 old->led.which_groups = new->led.which_groups;
364 old->led.groups = new->led.groups;
365 old->defined |= LED_FIELD_GROUPS;
366 }
367 if (UseNewLEDField(LED_FIELD_CTRLS, old->defined, new->defined,
368 clobber, report, &collide)) {
369 old->led.ctrls = new->led.ctrls;
370 old->defined |= LED_FIELD_CTRLS;
371 }
372
373 if (collide) {
374 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
375 "Map for indicator %s redefined; "
376 "Using %s definition for duplicate fields\n",
377 LEDText(info, old), (clobber ? "last" : "first"));
378 }
379
380 return true;
381 }
382
383 static bool
384 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
385 {
386 for (xkb_led_index_t i = 0; i < info->num_leds; i++) {
387 LedInfo *old = &info->leds[i];
388
389 if (old->led.name != new->led.name)
390 continue;
391
392 return MergeLedMap(info, old, new, same_file);
393 }
394
395 if (info->num_leds >= XKB_MAX_LEDS) {
396 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
397 "Too many LEDs defined (maximum %u)\n",
398 XKB_MAX_LEDS);
399 return false;
400 }
401 info->leds[info->num_leds++] = *new;
402 return true;
403 }
404
405 static void
406 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
407 enum merge_mode merge)
408 {
409 if (from->errorCount > 0) {
410 into->errorCount += from->errorCount;
411 return;
412 }
413
414 MergeModSets(into->ctx, &into->mods, &from->mods, merge);
415
416 if (into->name == NULL) {
417 into->name = steal(&from->name);
418 }
419
420 if (darray_empty(into->interps)) {
421 into->interps = from->interps;
422 darray_init(from->interps);
423 }
424 else {
425 SymInterpInfo *si;
426 darray_foreach(si, from->interps) {
427 si->merge = merge;
428 if (!AddInterp(into, si, false))
429 into->errorCount++;
430 }
431 }
432
433 if (into->num_leds == 0) {
434 memcpy(into->leds, from->leds, sizeof(*from->leds) * from->num_leds);
435 into->num_leds = from->num_leds;
436 from->num_leds = 0;
437 }
438 else {
439 for (xkb_led_index_t i = 0; i < from->num_leds; i++) {
440 LedInfo *ledi = &from->leds[i];
441 ledi->merge = merge;
442 if (!AddLedMap(into, ledi, false))
443 into->errorCount++;
444 }
445 }
446 }
447
448 static void
449 HandleCompatMapFile(CompatInfo *info, XkbFile *file);
450
451 static bool
452 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
453 {
454 CompatInfo included;
455
456 if (ExceedsIncludeMaxDepth(info->ctx, info->include_depth)) {
457 info->errorCount += 10;
458 return false;
459 }
460
461 InitCompatInfo(&included, info->ctx, info->include_depth + 1,
462 info->format, &info->mods);
463 included.name = steal(&include->stmt);
464
465 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
466 CompatInfo next_incl;
467 XkbFile *file;
468
469 char path[PATH_MAX];
470 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT,
471 path, sizeof(path));
472 if (!file) {
473 info->errorCount += 10;
474 ClearCompatInfo(&included);
475 return false;
476 }
477
478 InitCompatInfo(&next_incl, info->ctx, info->include_depth + 1,
479 info->format, &included.mods);
480 next_incl.default_interp = info->default_interp;
481 next_incl.default_led = info->default_led;
482
483 HandleCompatMapFile(&next_incl, file);
484
485 MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
486
487 ClearCompatInfo(&next_incl);
488 FreeXkbFile(file);
489 }
490
491 MergeIncludedCompatMaps(info, &included, include->merge);
492 ClearCompatInfo(&included);
493
494 return (info->errorCount == 0);
495 }
496
497 static bool
498 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
499 ExprDef *arrayNdx, ExprDef *value)
500 {
501 if (istreq(field, "action")) {
502 if (arrayNdx)
503 return ReportSINotArray(info, si, field);
504
505 if (value->common.type == STMT_EXPR_ACTION_LIST) {
506 unsigned int num_actions = 0;
507 for (ExprDef *act = value->actions.actions;
508 act; act = (ExprDef *) act->common.next)
509 num_actions++;
510
511 if (num_actions > MAX_ACTIONS_PER_LEVEL) {
512 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
513 "Interpret %s has too many actions; "
514 "expected max %u, got: %u\n",
515 siText(si, info), MAX_ACTIONS_PER_LEVEL, num_actions);
516 return false;
517 }
518
519 si->interp.num_actions = 0;
520 si->interp.a.action.type = ACTION_TYPE_NONE;
521
522 /* Parse actions and add only defined actions */
523 darray(union xkb_action) actions = darray_new();
524 for (ExprDef *act = value->actions.actions;
525 act; act = (ExprDef *) act->common.next) {
526 union xkb_action toAct = { 0 };
527 if (!HandleActionDef(info->ctx, info->format,
528 &info->default_actions, &info->mods,
529 act, &toAct)) {
530 darray_free(actions);
531 return false;
532 }
533 if (toAct.type == ACTION_TYPE_NONE) {
534 /* Drop action */
535 } else if (likely(num_actions == 1)) {
536 /* Only one action: do not allocate */
537 si->interp.num_actions = 1;
538 si->interp.a.action = toAct;
539 } else {
540 darray_append(actions, toAct);
541 }
542 }
543 switch (darray_size(actions)) {
544 case 0:
545 /* No action or exactly one action: already processed */
546 assert(si->interp.num_actions <= 1);
547 break;
548 case 1:
549 /* One action: some actions were dropped */
550 si->interp.num_actions = 1;
551 si->interp.a.action = darray_item(actions, 1);
552 darray_free(actions);
553 break;
554 default:
555 /* Multiple actions; no NoAction() left */
556 darray_shrink(actions);
557 si->interp.num_actions =
558 (xkb_action_count_t) darray_size(actions);
559 darray_steal(actions, &si->interp.a.actions, NULL);
560 }
561 }
562 else if (HandleActionDef(info->ctx, info->format,
563 &info->default_actions, &info->mods,
564 value, &si->interp.a.action))
565 si->interp.num_actions =
566 (si->interp.a.action.type != ACTION_TYPE_NONE);
567 else
568 return false;
569
570 si->defined |= SI_FIELD_ACTION;
571 }
572 else if (istreq(field, "virtualmodifier") ||
573 istreq(field, "virtualmod")) {
574 if (arrayNdx)
575 return ReportSINotArray(info, si, field);
576
577 xkb_mod_index_t ndx = 0;
578 if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->mods, &ndx))
579 return ReportSIBadType(info, si, field, "virtual modifier");
580
581 si->interp.virtual_mod = ndx;
582 si->defined |= SI_FIELD_VIRTUAL_MOD;
583 }
584 else if (istreq(field, "repeat")) {
585 bool set = false;
586
587 if (arrayNdx)
588 return ReportSINotArray(info, si, field);
589
590 if (!ExprResolveBoolean(info->ctx, value, &set))
591 return ReportSIBadType(info, si, field, "boolean");
592
593 si->interp.repeat = set;
594
595 si->defined |= SI_FIELD_AUTO_REPEAT;
596 }
597 else if (istreq(field, "locking")) {
598 log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
599 "The \"locking\" field in symbol interpretation is unsupported; "
600 "Ignored\n");
601 }
602 else if (istreq(field, "usemodmap") ||
603 istreq(field, "usemodmapmods")) {
604 uint32_t val = 0;
605
606 if (arrayNdx)
607 return ReportSINotArray(info, si, field);
608
609 if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
610 return ReportSIBadType(info, si, field, "level specification");
611
612 si->interp.level_one_only = val;
613 si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
614 }
615 else {
616 return ReportBadField(info->ctx, "symbol interpretation", field,
617 siText(si, info));
618 }
619
620 return true;
621 }
622
623 static bool
624 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
625 ExprDef *arrayNdx, ExprDef *value)
626 {
627 bool ok = true;
628
629 if (istreq(field, "modifiers") || istreq(field, "mods")) {
630 if (arrayNdx)
631 return ReportLedNotArray(info, ledi, field);
632
633 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
634 &info->mods, &ledi->led.mods.mods))
635 return ReportLedBadType(info, ledi, field, "modifier mask");
636
637 ledi->defined |= LED_FIELD_MODS;
638 }
639 else if (istreq(field, "groups")) {
640 uint32_t mask = 0;
641
642 if (arrayNdx)
643 return ReportLedNotArray(info, ledi, field);
644
645 const xkb_layout_index_t max_groups = format_max_groups(info->format);
646 if (!ExprResolveGroupMask(info->ctx, max_groups, value, &mask))
647 return ReportLedBadType(info, ledi, field, "group mask");
648
649 ledi->led.groups = mask;
650 ledi->defined |= LED_FIELD_GROUPS;
651 }
652 else if (istreq(field, "controls") || istreq(field, "ctrls")) {
653 uint32_t mask = 0;
654
655 if (arrayNdx)
656 return ReportLedNotArray(info, ledi, field);
657
658 if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
659 return ReportLedBadType(info, ledi, field, "controls mask");
660
661 ledi->led.ctrls = mask;
662 ledi->defined |= LED_FIELD_CTRLS;
663 }
664 else if (istreq(field, "allowexplicit")) {
665 log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
666 "The \"allowExplicit\" field in indicator statements is unsupported; "
667 "Ignored\n");
668 }
669 else if (istreq(field, "whichmodstate") ||
670 istreq(field, "whichmodifierstate")) {
671 uint32_t mask = 0;
672
673 if (arrayNdx)
674 return ReportLedNotArray(info, ledi, field);
675
676 if (!ExprResolveMask(info->ctx, value, &mask,
677 modComponentMaskNames))
678 return ReportLedBadType(info, ledi, field,
679 "mask of modifier state components");
680
681 ledi->led.which_mods = mask;
682 }
683 else if (istreq(field, "whichgroupstate")) {
684 uint32_t mask = 0;
685
686 if (arrayNdx)
687 return ReportLedNotArray(info, ledi, field);
688
689 if (!ExprResolveMask(info->ctx, value, &mask,
690 groupComponentMaskNames))
691 return ReportLedBadType(info, ledi, field,
692 "mask of group state components");
693
694 ledi->led.which_groups = mask;
695 }
696 else if (istreq(field, "driveskbd") ||
697 istreq(field, "driveskeyboard") ||
698 istreq(field, "leddriveskbd") ||
699 istreq(field, "leddriveskeyboard") ||
700 istreq(field, "indicatordriveskbd") ||
701 istreq(field, "indicatordriveskeyboard")) {
702 log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
703 "The \"%s\" field in indicator statements is unsupported; "
704 "Ignored\n", field);
705 }
706 else if (istreq(field, "index")) {
707 /* Users should see this, it might cause unexpected behavior. */
708 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
709 "The \"index\" field in indicator statements is unsupported; "
710 "Ignored\n");
711 }
712 else {
713 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
714 "Unknown field \"%s\" in map for %s indicator; "
715 "Definition ignored\n",
716 field, LEDText(info, ledi));
717 ok = false;
718 }
719
720 return ok;
721 }
722
723 static bool
724 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
725 {
726 const char *elem, *field;
727 ExprDef *ndx;
728 bool ret;
729
730 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
731 ret = false;
732 else if (elem && istreq(elem, "interpret")) {
733 SymInterpInfo temp = {0};
734 InitInterp(&temp);
735 /* Do not replace the whole interpret, only the current field */
736 temp.merge = (temp.merge == MERGE_REPLACE)
737 ? MERGE_OVERRIDE
738 : stmt->merge;
739 ret = SetInterpField(info, &temp, field, ndx, stmt->value);
740 MergeInterp(info, &info->default_interp, &temp, true);
741 }
742 else if (elem && istreq(elem, "indicator")) {
743 LedInfo temp = {0};
744 InitLED(&temp);
745 /* Do not replace the whole LED, only the current field */
746 temp.merge = (temp.merge == MERGE_REPLACE)
747 ? MERGE_OVERRIDE
748 : stmt->merge;
749 ret = SetLedMapField(info, &temp, field, ndx, stmt->value);
750 MergeLedMap(info, &info->default_led, &temp, true);
751 }
752 else if (elem) {
753 ret = SetDefaultActionField(info->ctx, info->format,
754 &info->default_actions, &info->mods,
755 elem, field, ndx,
756 stmt->value, stmt->merge);
757 } else {
758 log_err(info->ctx, XKB_ERROR_UNKNOWN_DEFAULT_FIELD,
759 "Default defined for unknown field \"%s\"; Ignored\n", field);
760 return false;
761 }
762 return ret;
763 }
764
765 static bool
766 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
767 {
768 bool ok = true;
769 const char *elem, *field;
770 ExprDef *arrayNdx;
771
772 for (; def; def = (VarDef *) def->common.next) {
773 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
774 if (!ok)
775 continue;
776 if (elem) {
777 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
778 "Cannot set a global default value for \"%s\" element from "
779 "within an interpret statement; "
780 "Move assignment to \"%s.%s\" to the global file scope\n",
781 elem, elem, field);
782 ok = false;
783 continue;
784 }
785 ok = SetInterpField(info, si, field, arrayNdx, def->value);
786 }
787
788 return ok;
789 }
790
791 static bool
792 HandleInterpDef(CompatInfo *info, InterpDef *def)
793 {
794 enum xkb_match_operation pred;
795 xkb_mod_mask_t mods;
796 SymInterpInfo si;
797
798 if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
799 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
800 "Couldn't determine matching modifiers; "
801 "Symbol interpretation ignored\n");
802 return false;
803 }
804
805 si = info->default_interp;
806 si.merge = def->merge;
807 si.interp.sym = def->sym;
808 si.interp.match = pred;
809 si.interp.mods = mods;
810
811 if (!HandleInterpBody(info, def->def, &si)) {
812 info->errorCount++;
813 return false;
814 }
815
816 if (!AddInterp(info, &si, true)) {
817 info->errorCount++;
818 return false;
819 }
820
821 return true;
822 }
823
824 static bool
825 HandleLedMapDef(CompatInfo *info, LedMapDef *def)
826 {
827 LedInfo ledi;
828 VarDef *var;
829 bool ok;
830
831 ledi = info->default_led;
832 ledi.merge = def->merge;
833 ledi.led.name = def->name;
834
835 ok = true;
836 for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
837 const char *elem, *field;
838 ExprDef *arrayNdx;
839 if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
840 ok = false;
841 continue;
842 }
843
844 if (elem) {
845 log_err(info->ctx, XKB_ERROR_GLOBAL_DEFAULTS_WRONG_SCOPE,
846 "Cannot set defaults for \"%s\" element in indicator map; "
847 "Assignment to %s.%s ignored\n", elem, elem, field);
848 ok = false;
849 }
850 else {
851 ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
852 }
853 }
854
855 if (ok)
856 return AddLedMap(info, &ledi, true);
857
858 return false;
859 }
860
861 static void
862 HandleCompatMapFile(CompatInfo *info, XkbFile *file)
863 {
864 bool ok;
865
866 free(info->name);
867 info->name = strdup_safe(file->name);
868
869 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
870 switch (stmt->type) {
871 case STMT_INCLUDE:
872 ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
873 break;
874 case STMT_INTERP:
875 ok = HandleInterpDef(info, (InterpDef *) stmt);
876 break;
877 case STMT_GROUP_COMPAT:
878 log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
879 "The \"group\" statement in compat is unsupported; "
880 "Ignored\n");
881 ok = true;
882 break;
883 case STMT_LED_MAP:
884 ok = HandleLedMapDef(info, (LedMapDef *) stmt);
885 break;
886 case STMT_VAR:
887 ok = HandleGlobalVar(info, (VarDef *) stmt);
888 break;
889 case STMT_VMOD:
890 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt);
891 break;
892 default:
893 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
894 "Compat files may not include other types; "
895 "Ignoring %s\n", stmt_type_to_string(stmt->type));
896 ok = false;
897 break;
898 }
899
900 if (!ok)
901 info->errorCount++;
902
903 if (info->errorCount > 10) {
904 log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
905 "Abandoning compatibility map \"%s\"\n",
906 safe_map_name(file));
907 break;
908 }
909 }
910 }
911
912 /* Temporary struct for CopyInterps. */
913 struct collect {
914 darray(struct xkb_sym_interpret) sym_interprets;
915 };
916
917 static void
918 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
919 struct collect *collect)
920 {
921 SymInterpInfo *si;
922
923 darray_foreach(si, info->interps)
924 if (si->interp.match == pred &&
925 (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
926 darray_append(collect->sym_interprets, si->interp);
927 }
928
929 static void
930 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
931 {
932 for (xkb_led_index_t idx = 0; idx < info->num_leds; idx++) {
933 LedInfo *ledi = &info->leds[idx];
934 xkb_led_index_t i;
935 struct xkb_led *led;
936
937 /*
938 * Find the LED with the given name, if it was already declared
939 * in keycodes.
940 */
941 xkb_leds_enumerate(i, led, keymap)
942 if (led->name == ledi->led.name)
943 break;
944
945 /* Not previously declared; create it with next free index. */
946 if (i >= keymap->num_leds) {
947 log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
948 "Indicator name \"%s\" was not declared in the keycodes "
949 "section; Adding new indicator\n",
950 LEDText(info, ledi));
951
952 xkb_leds_enumerate(i, led, keymap)
953 if (led->name == XKB_ATOM_NONE)
954 break;
955
956 if (i >= keymap->num_leds) {
957 /* Not place to put it; ignore. */
958 if (i >= XKB_MAX_LEDS) {
959 log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
960 "Too many indicators (maximum is %u); "
961 "Indicator name \"%s\" ignored\n",
962 XKB_MAX_LEDS, LEDText(info, ledi));
963 continue;
964 }
965
966 /* Add a new LED. */
967 led = &keymap->leds[keymap->num_leds++];
968 }
969 }
970
971 *led = ledi->led;
972 if (led->groups != 0 && led->which_groups == 0)
973 led->which_groups = XKB_STATE_LAYOUT_EFFECTIVE;
974 if (led->mods.mods != 0 && led->which_mods == 0)
975 led->which_mods = XKB_STATE_MODS_EFFECTIVE;
976 }
977 }
978
979 static bool
980 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
981 {
982 keymap->compat_section_name = strdup_safe(info->name);
983 XkbEscapeMapName(keymap->compat_section_name);
984
985 keymap->mods = info->mods;
986
987 if (!darray_empty(info->interps)) {
988 struct collect collect;
989 darray_init(collect.sym_interprets);
990
991 /* Most specific to least specific. */
992 CopyInterps(info, true, MATCH_EXACTLY, &collect);
993 CopyInterps(info, true, MATCH_ALL, &collect);
994 CopyInterps(info, true, MATCH_NONE, &collect);
995 CopyInterps(info, true, MATCH_ANY, &collect);
996 CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
997 CopyInterps(info, false, MATCH_EXACTLY, &collect);
998 CopyInterps(info, false, MATCH_ALL, &collect);
999 CopyInterps(info, false, MATCH_NONE, &collect);
1000 CopyInterps(info, false, MATCH_ANY, &collect);
1001 CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
1002
1003 darray_steal(collect.sym_interprets,
1004 &keymap->sym_interprets, &keymap->num_sym_interprets);
1005 }
1006
1007 CopyLedMapDefsToKeymap(keymap, info);
1008
1009 return true;
1010 }
1011
1012 bool
1013 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap)
1014 {
1015 CompatInfo info;
1016
1017 InitCompatInfo(&info, keymap->ctx, 0, keymap->format, &keymap->mods);
1018
1019 if (file != NULL)
1020 HandleCompatMapFile(&info, file);
1021
1022 if (info.errorCount != 0)
1023 goto err_info;
1024
1025 if (!CopyCompatToKeymap(keymap, &info))
1026 goto err_info;
1027
1028 ClearCompatInfo(&info);
1029 return true;
1030
1031 err_info:
1032 ClearCompatInfo(&info);
1033 return false;
1034 }