Messages: merge macros with and without message code
[platform/upstream/libxkbcommon.git] / src / xkbcomp / compat.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 /*
28  * Copyright © 2012 Ran Benita <ran234@gmail.com>
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a
31  * copy of this software and associated documentation files (the "Software"),
32  * to deal in the Software without restriction, including without limitation
33  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34  * and/or sell copies of the Software, and to permit persons to whom the
35  * Software is furnished to do so, subject to the following conditions:
36  *
37  * The above copyright notice and this permission notice (including the next
38  * paragraph) shall be included in all copies or substantial portions of the
39  * Software.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
44  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47  * DEALINGS IN THE SOFTWARE.
48  */
49
50 #include "config.h"
51
52 #include "xkbcomp-priv.h"
53 #include "text.h"
54 #include "expr.h"
55 #include "action.h"
56 #include "vmod.h"
57 #include "include.h"
58
59 enum si_field {
60     SI_FIELD_VIRTUAL_MOD = (1 << 0),
61     SI_FIELD_ACTION = (1 << 1),
62     SI_FIELD_AUTO_REPEAT = (1 << 2),
63     SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
64 };
65
66 typedef struct {
67     enum si_field defined;
68     enum merge_mode merge;
69
70     struct xkb_sym_interpret interp;
71 } SymInterpInfo;
72
73 enum led_field {
74     LED_FIELD_MODS = (1 << 0),
75     LED_FIELD_GROUPS = (1 << 1),
76     LED_FIELD_CTRLS = (1 << 2),
77 };
78
79 typedef struct {
80     enum led_field defined;
81     enum merge_mode merge;
82
83     struct xkb_led led;
84 } LedInfo;
85
86 typedef struct {
87     char *name;
88     int errorCount;
89     SymInterpInfo default_interp;
90     darray(SymInterpInfo) interps;
91     LedInfo default_led;
92     LedInfo leds[XKB_MAX_LEDS];
93     unsigned int num_leds;
94     ActionsInfo *actions;
95     struct xkb_mod_set mods;
96
97     struct xkb_context *ctx;
98 } CompatInfo;
99
100 static const char *
101 siText(SymInterpInfo *si, CompatInfo *info)
102 {
103     char *buf = xkb_context_get_buffer(info->ctx, 128);
104
105     if (si == &info->default_interp)
106         return "default";
107
108     snprintf(buf, 128, "%s+%s(%s)",
109              KeysymText(info->ctx, si->interp.sym),
110              SIMatchText(si->interp.match),
111              ModMaskText(info->ctx, &info->mods, si->interp.mods));
112
113     return buf;
114 }
115
116 static inline bool
117 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
118 {
119     return ReportNotArray(info->ctx, "symbol interpretation", field,
120                           siText(si, info));
121 }
122
123 static inline bool
124 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
125                 const char *wanted)
126 {
127     return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
128                          "symbol interpretation", field,
129                          siText(si, info), wanted);
130 }
131
132 static inline bool
133 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
134                  const char *wanted)
135 {
136     return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
137                          "indicator map", field,
138                          xkb_atom_text(info->ctx, ledi->led.name),
139                          wanted);
140 }
141
142 static inline bool
143 ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)
144 {
145     return ReportNotArray(info->ctx, "indicator map", field,
146                           xkb_atom_text(info->ctx, ledi->led.name));
147 }
148
149 static void
150 InitCompatInfo(CompatInfo *info, struct xkb_context *ctx,
151                ActionsInfo *actions, const struct xkb_mod_set *mods)
152 {
153     memset(info, 0, sizeof(*info));
154     info->ctx = ctx;
155     info->actions = actions;
156     info->mods = *mods;
157     info->default_interp.merge = MERGE_OVERRIDE;
158     info->default_interp.interp.virtual_mod = XKB_MOD_INVALID;
159     info->default_led.merge = MERGE_OVERRIDE;
160 }
161
162 static void
163 ClearCompatInfo(CompatInfo *info)
164 {
165     free(info->name);
166     darray_free(info->interps);
167 }
168
169 static SymInterpInfo *
170 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
171 {
172     SymInterpInfo *old;
173
174     darray_foreach(old, info->interps)
175         if (old->interp.sym == new->interp.sym &&
176             old->interp.mods == new->interp.mods &&
177             old->interp.match == new->interp.match)
178             return old;
179
180     return NULL;
181 }
182
183 static bool
184 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
185                   bool report, enum si_field *collide)
186 {
187     if (!(old->defined & field))
188         return true;
189
190     if (new->defined & field) {
191         if (report)
192             *collide |= field;
193
194         if (new->merge != MERGE_AUGMENT)
195             return true;
196     }
197
198     return false;
199 }
200
201 static bool
202 AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
203 {
204     SymInterpInfo *old = FindMatchingInterp(info, new);
205     if (old) {
206         const int verbosity = xkb_context_get_log_verbosity(info->ctx);
207         const bool report = (same_file && verbosity > 0) || verbosity > 9;
208         enum si_field collide = 0;
209
210         if (new->merge == MERGE_REPLACE) {
211             if (report)
212                 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
213                          "Multiple definitions for \"%s\"; "
214                          "Earlier interpretation ignored\n",
215                          siText(new, info));
216             *old = *new;
217             return true;
218         }
219
220         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
221                               &collide)) {
222             old->interp.virtual_mod = new->interp.virtual_mod;
223             old->defined |= SI_FIELD_VIRTUAL_MOD;
224         }
225         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
226                               &collide)) {
227             old->interp.action = new->interp.action;
228             old->defined |= SI_FIELD_ACTION;
229         }
230         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
231                               &collide)) {
232             old->interp.repeat = new->interp.repeat;
233             old->defined |= SI_FIELD_AUTO_REPEAT;
234         }
235         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
236                               &collide)) {
237             old->interp.level_one_only = new->interp.level_one_only;
238             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
239         }
240
241         if (collide) {
242             log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
243                      "Multiple interpretations of \"%s\"; "
244                      "Using %s definition for duplicate fields\n",
245                      siText(new, info),
246                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
247         }
248
249         return true;
250     }
251
252     darray_append(info->interps, *new);
253     return true;
254 }
255
256 /***====================================================================***/
257
258 static bool
259 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
260                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
261 {
262     if (expr == NULL) {
263         *pred_rtrn = MATCH_ANY_OR_NONE;
264         *mods_rtrn = MOD_REAL_MASK_ALL;
265         return true;
266     }
267
268     *pred_rtrn = MATCH_EXACTLY;
269     if (expr->expr.op == EXPR_ACTION_DECL) {
270         const char *pred_txt = xkb_atom_text(info->ctx, expr->action.name);
271         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn) ||
272             !expr->action.args || expr->action.args->common.next) {
273             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
274                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
275             return false;
276         }
277         expr = expr->action.args;
278     }
279     else if (expr->expr.op == EXPR_IDENT) {
280         const char *pred_txt = xkb_atom_text(info->ctx, expr->ident.ident);
281         if (pred_txt && istreq(pred_txt, "any")) {
282             *pred_rtrn = MATCH_ANY;
283             *mods_rtrn = MOD_REAL_MASK_ALL;
284             return true;
285         }
286     }
287
288     return ExprResolveModMask(info->ctx, expr, MOD_REAL, &info->mods,
289                               mods_rtrn);
290 }
291
292 /***====================================================================***/
293
294 static bool
295 UseNewLEDField(enum led_field field, LedInfo *old, LedInfo *new,
296                bool report, enum led_field *collide)
297 {
298     if (!(old->defined & field))
299         return true;
300
301     if (new->defined & field) {
302         if (report)
303             *collide |= field;
304
305         if (new->merge != MERGE_AUGMENT)
306             return true;
307     }
308
309     return false;
310 }
311
312 static bool
313 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
314 {
315     enum led_field collide;
316     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
317     const bool report = (same_file && verbosity > 0) || verbosity > 9;
318
319     for (xkb_led_index_t i = 0; i < info->num_leds; i++) {
320         LedInfo *old = &info->leds[i];
321
322         if (old->led.name != new->led.name)
323             continue;
324
325         if (old->led.mods.mods == new->led.mods.mods &&
326             old->led.groups == new->led.groups &&
327             old->led.ctrls == new->led.ctrls &&
328             old->led.which_mods == new->led.which_mods &&
329             old->led.which_groups == new->led.which_groups) {
330             old->defined |= new->defined;
331             return true;
332         }
333
334         if (new->merge == MERGE_REPLACE) {
335             if (report)
336                 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
337                          "Map for indicator %s redefined; "
338                          "Earlier definition ignored\n",
339                          xkb_atom_text(info->ctx, old->led.name));
340             *old = *new;
341             return true;
342         }
343
344         collide = 0;
345         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
346             old->led.which_mods = new->led.which_mods;
347             old->led.mods = new->led.mods;
348             old->defined |= LED_FIELD_MODS;
349         }
350         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
351             old->led.which_groups = new->led.which_groups;
352             old->led.groups = new->led.groups;
353             old->defined |= LED_FIELD_GROUPS;
354         }
355         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
356             old->led.ctrls = new->led.ctrls;
357             old->defined |= LED_FIELD_CTRLS;
358         }
359
360         if (collide) {
361             log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
362                      "Map for indicator %s redefined; "
363                      "Using %s definition for duplicate fields\n",
364                      xkb_atom_text(info->ctx, old->led.name),
365                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
366         }
367
368         return true;
369     }
370
371     if (info->num_leds >= XKB_MAX_LEDS) {
372         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
373                 "Too many LEDs defined (maximum %d)\n",
374                 XKB_MAX_LEDS);
375         return false;
376     }
377     info->leds[info->num_leds++] = *new;
378     return true;
379 }
380
381 static void
382 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
383                         enum merge_mode merge)
384 {
385     if (from->errorCount > 0) {
386         into->errorCount += from->errorCount;
387         return;
388     }
389
390     into->mods = from->mods;
391
392     if (into->name == NULL) {
393         into->name = from->name;
394         from->name = NULL;
395     }
396
397     if (darray_empty(into->interps)) {
398         into->interps = from->interps;
399         darray_init(from->interps);
400     }
401     else {
402         SymInterpInfo *si;
403         darray_foreach(si, from->interps) {
404             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
405             if (!AddInterp(into, si, false))
406                 into->errorCount++;
407         }
408     }
409
410     if (into->num_leds == 0) {
411         memcpy(into->leds, from->leds, sizeof(*from->leds) * from->num_leds);
412         into->num_leds = from->num_leds;
413         from->num_leds = 0;
414     }
415     else {
416         for (xkb_led_index_t i = 0; i < from->num_leds; i++) {
417             LedInfo *ledi = &from->leds[i];
418             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
419             if (!AddLedMap(into, ledi, false))
420                 into->errorCount++;
421         }
422     }
423 }
424
425 static void
426 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
427
428 static bool
429 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
430 {
431     CompatInfo included;
432
433     InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
434     included.name = include->stmt;
435     include->stmt = NULL;
436
437     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
438         CompatInfo next_incl;
439         XkbFile *file;
440
441         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
442         if (!file) {
443             info->errorCount += 10;
444             ClearCompatInfo(&included);
445             return false;
446         }
447
448         InitCompatInfo(&next_incl, info->ctx, info->actions, &included.mods);
449         next_incl.default_interp = info->default_interp;
450         next_incl.default_interp.merge = stmt->merge;
451         next_incl.default_led = info->default_led;
452         next_incl.default_led.merge = stmt->merge;
453
454         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
455
456         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
457
458         ClearCompatInfo(&next_incl);
459         FreeXkbFile(file);
460     }
461
462     MergeIncludedCompatMaps(info, &included, include->merge);
463     ClearCompatInfo(&included);
464
465     return (info->errorCount == 0);
466 }
467
468 static bool
469 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
470                ExprDef *arrayNdx, ExprDef *value)
471 {
472     xkb_mod_index_t ndx;
473
474     if (istreq(field, "action")) {
475         if (arrayNdx)
476             return ReportSINotArray(info, si, field);
477
478         if (!HandleActionDef(info->ctx, info->actions, &info->mods,
479                              value, &si->interp.action))
480             return false;
481
482         si->defined |= SI_FIELD_ACTION;
483     }
484     else if (istreq(field, "virtualmodifier") ||
485              istreq(field, "virtualmod")) {
486         if (arrayNdx)
487             return ReportSINotArray(info, si, field);
488
489         if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->mods, &ndx))
490             return ReportSIBadType(info, si, field, "virtual modifier");
491
492         si->interp.virtual_mod = ndx;
493         si->defined |= SI_FIELD_VIRTUAL_MOD;
494     }
495     else if (istreq(field, "repeat")) {
496         bool set;
497
498         if (arrayNdx)
499             return ReportSINotArray(info, si, field);
500
501         if (!ExprResolveBoolean(info->ctx, value, &set))
502             return ReportSIBadType(info, si, field, "boolean");
503
504         si->interp.repeat = set;
505
506         si->defined |= SI_FIELD_AUTO_REPEAT;
507     }
508     else if (istreq(field, "locking")) {
509         log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
510                 "The \"locking\" field in symbol interpretation is unsupported; "
511                 "Ignored\n");
512     }
513     else if (istreq(field, "usemodmap") ||
514              istreq(field, "usemodmapmods")) {
515         unsigned int val;
516
517         if (arrayNdx)
518             return ReportSINotArray(info, si, field);
519
520         if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
521             return ReportSIBadType(info, si, field, "level specification");
522
523         si->interp.level_one_only = val;
524         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
525     }
526     else {
527         return ReportBadField(info->ctx, "symbol interpretation", field,
528                               siText(si, info));
529     }
530
531     return true;
532 }
533
534 static bool
535 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
536                ExprDef *arrayNdx, ExprDef *value)
537 {
538     bool ok = true;
539
540     if (istreq(field, "modifiers") || istreq(field, "mods")) {
541         if (arrayNdx)
542             return ReportLedNotArray(info, ledi, field);
543
544         if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
545                                 &info->mods, &ledi->led.mods.mods))
546             return ReportLedBadType(info, ledi, field, "modifier mask");
547
548         ledi->defined |= LED_FIELD_MODS;
549     }
550     else if (istreq(field, "groups")) {
551         unsigned int mask;
552
553         if (arrayNdx)
554             return ReportLedNotArray(info, ledi, field);
555
556         if (!ExprResolveMask(info->ctx, value, &mask, groupMaskNames))
557             return ReportLedBadType(info, ledi, field, "group mask");
558
559         ledi->led.groups = mask;
560         ledi->defined |= LED_FIELD_GROUPS;
561     }
562     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
563         unsigned int mask;
564
565         if (arrayNdx)
566             return ReportLedNotArray(info, ledi, field);
567
568         if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
569             return ReportLedBadType(info, ledi, field, "controls mask");
570
571         ledi->led.ctrls = mask;
572         ledi->defined |= LED_FIELD_CTRLS;
573     }
574     else if (istreq(field, "allowexplicit")) {
575         log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
576                 "The \"allowExplicit\" field in indicator statements is unsupported; "
577                 "Ignored\n");
578     }
579     else if (istreq(field, "whichmodstate") ||
580              istreq(field, "whichmodifierstate")) {
581         unsigned int mask;
582
583         if (arrayNdx)
584             return ReportLedNotArray(info, ledi, field);
585
586         if (!ExprResolveMask(info->ctx, value, &mask,
587                              modComponentMaskNames))
588             return ReportLedBadType(info, ledi, field,
589                                     "mask of modifier state components");
590
591         ledi->led.which_mods = mask;
592     }
593     else if (istreq(field, "whichgroupstate")) {
594         unsigned mask;
595
596         if (arrayNdx)
597             return ReportLedNotArray(info, ledi, field);
598
599         if (!ExprResolveMask(info->ctx, value, &mask,
600                              groupComponentMaskNames))
601             return ReportLedBadType(info, ledi, field,
602                                     "mask of group state components");
603
604         ledi->led.which_groups = mask;
605     }
606     else if (istreq(field, "driveskbd") ||
607              istreq(field, "driveskeyboard") ||
608              istreq(field, "leddriveskbd") ||
609              istreq(field, "leddriveskeyboard") ||
610              istreq(field, "indicatordriveskbd") ||
611              istreq(field, "indicatordriveskeyboard")) {
612         log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
613                 "The \"%s\" field in indicator statements is unsupported; "
614                 "Ignored\n", field);
615     }
616     else if (istreq(field, "index")) {
617         /* Users should see this, it might cause unexpected behavior. */
618         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
619                 "The \"index\" field in indicator statements is unsupported; "
620                 "Ignored\n");
621     }
622     else {
623         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
624                 "Unknown field %s in map for %s indicator; "
625                 "Definition ignored\n",
626                 field, xkb_atom_text(info->ctx, ledi->led.name));
627         ok = false;
628     }
629
630     return ok;
631 }
632
633 static bool
634 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
635 {
636     const char *elem, *field;
637     ExprDef *ndx;
638     bool ret;
639
640     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
641         ret = false;
642     else if (elem && istreq(elem, "interpret"))
643         ret = SetInterpField(info, &info->default_interp, field, ndx,
644                              stmt->value);
645     else if (elem && istreq(elem, "indicator"))
646         ret = SetLedMapField(info, &info->default_led, field, ndx,
647                              stmt->value);
648     else
649         ret = SetActionField(info->ctx, info->actions, &info->mods,
650                              elem, field, ndx, stmt->value);
651     return ret;
652 }
653
654 static bool
655 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
656 {
657     bool ok = true;
658     const char *elem, *field;
659     ExprDef *arrayNdx;
660
661     for (; def; def = (VarDef *) def->common.next) {
662         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
663             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
664                     "Cannot set a global default value from within an interpret statement; "
665                     "Move statements to the global file scope\n");
666             ok = false;
667             continue;
668         }
669
670         ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
671         if (!ok)
672             continue;
673
674         ok = SetInterpField(info, si, field, arrayNdx, def->value);
675     }
676
677     return ok;
678 }
679
680 static bool
681 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
682 {
683     enum xkb_match_operation pred;
684     xkb_mod_mask_t mods;
685     SymInterpInfo si;
686
687     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
688         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
689                 "Couldn't determine matching modifiers; "
690                 "Symbol interpretation ignored\n");
691         return false;
692     }
693
694     si = info->default_interp;
695     si.merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
696     si.interp.sym = def->sym;
697     si.interp.match = pred;
698     si.interp.mods = mods;
699
700     if (!HandleInterpBody(info, def->def, &si)) {
701         info->errorCount++;
702         return false;
703     }
704
705     if (!AddInterp(info, &si, true)) {
706         info->errorCount++;
707         return false;
708     }
709
710     return true;
711 }
712
713 static bool
714 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
715 {
716     LedInfo ledi;
717     VarDef *var;
718     bool ok;
719
720     if (def->merge != MERGE_DEFAULT)
721         merge = def->merge;
722
723     ledi = info->default_led;
724     ledi.merge = merge;
725     ledi.led.name = def->name;
726
727     ok = true;
728     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
729         const char *elem, *field;
730         ExprDef *arrayNdx;
731         if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
732             ok = false;
733             continue;
734         }
735
736         if (elem) {
737             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
738                     "Cannot set defaults for \"%s\" element in indicator map; "
739                     "Assignment to %s.%s ignored\n", elem, elem, field);
740             ok = false;
741         }
742         else {
743             ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
744         }
745     }
746
747     if (ok)
748         return AddLedMap(info, &ledi, true);
749
750     return false;
751 }
752
753 static void
754 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
755 {
756     bool ok;
757
758     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
759
760     free(info->name);
761     info->name = strdup_safe(file->name);
762
763     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
764         switch (stmt->type) {
765         case STMT_INCLUDE:
766             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
767             break;
768         case STMT_INTERP:
769             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
770             break;
771         case STMT_GROUP_COMPAT:
772             log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
773                     "The \"group\" statement in compat is unsupported; "
774                     "Ignored\n");
775             ok = true;
776             break;
777         case STMT_LED_MAP:
778             ok = HandleLedMapDef(info, (LedMapDef *) stmt, merge);
779             break;
780         case STMT_VAR:
781             ok = HandleGlobalVar(info, (VarDef *) stmt);
782             break;
783         case STMT_VMOD:
784             ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
785             break;
786         default:
787             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
788                     "Compat files may not include other types; "
789                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
790             ok = false;
791             break;
792         }
793
794         if (!ok)
795             info->errorCount++;
796
797         if (info->errorCount > 10) {
798             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
799                     "Abandoning compatibility map \"%s\"\n", file->name);
800             break;
801         }
802     }
803 }
804
805 /* Temporary struct for CopyInterps. */
806 struct collect {
807     darray(struct xkb_sym_interpret) sym_interprets;
808 };
809
810 static void
811 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
812             struct collect *collect)
813 {
814     SymInterpInfo *si;
815
816     darray_foreach(si, info->interps)
817         if (si->interp.match == pred &&
818             (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
819             darray_append(collect->sym_interprets, si->interp);
820 }
821
822 static void
823 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
824 {
825     for (xkb_led_index_t idx = 0; idx < info->num_leds; idx++) {
826         LedInfo *ledi = &info->leds[idx];
827         xkb_led_index_t i;
828         struct xkb_led *led;
829
830         /*
831          * Find the LED with the given name, if it was already declared
832          * in keycodes.
833          */
834         xkb_leds_enumerate(i, led, keymap)
835             if (led->name == ledi->led.name)
836                 break;
837
838         /* Not previously declared; create it with next free index. */
839         if (i >= keymap->num_leds) {
840             log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
841                     "Indicator name \"%s\" was not declared in the keycodes section; "
842                     "Adding new indicator\n",
843                     xkb_atom_text(keymap->ctx, ledi->led.name));
844
845             xkb_leds_enumerate(i, led, keymap)
846                 if (led->name == XKB_ATOM_NONE)
847                     break;
848
849             if (i >= keymap->num_leds) {
850                 /* Not place to put it; ignore. */
851                 if (i >= XKB_MAX_LEDS) {
852                     log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
853                             "Too many indicators (maximum is %d); "
854                             "Indicator name \"%s\" ignored\n",
855                             XKB_MAX_LEDS,
856                             xkb_atom_text(keymap->ctx, ledi->led.name));
857                     continue;
858                 }
859
860                 /* Add a new LED. */
861                 led = &keymap->leds[keymap->num_leds++];
862             }
863         }
864
865         *led = ledi->led;
866         if (led->groups != 0 && led->which_groups == 0)
867             led->which_groups = XKB_STATE_LAYOUT_EFFECTIVE;
868         if (led->mods.mods != 0 && led->which_mods == 0)
869             led->which_mods = XKB_STATE_MODS_EFFECTIVE;
870     }
871 }
872
873 static bool
874 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
875 {
876     keymap->compat_section_name = strdup_safe(info->name);
877     XkbEscapeMapName(keymap->compat_section_name);
878
879     keymap->mods = info->mods;
880
881     if (!darray_empty(info->interps)) {
882         struct collect collect;
883         darray_init(collect.sym_interprets);
884
885         /* Most specific to least specific. */
886         CopyInterps(info, true, MATCH_EXACTLY, &collect);
887         CopyInterps(info, true, MATCH_ALL, &collect);
888         CopyInterps(info, true, MATCH_NONE, &collect);
889         CopyInterps(info, true, MATCH_ANY, &collect);
890         CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
891         CopyInterps(info, false, MATCH_EXACTLY, &collect);
892         CopyInterps(info, false, MATCH_ALL, &collect);
893         CopyInterps(info, false, MATCH_NONE, &collect);
894         CopyInterps(info, false, MATCH_ANY, &collect);
895         CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
896
897         darray_steal(collect.sym_interprets,
898                      &keymap->sym_interprets, &keymap->num_sym_interprets);
899     }
900
901     CopyLedMapDefsToKeymap(keymap, info);
902
903     return true;
904 }
905
906 bool
907 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
908                  enum merge_mode merge)
909 {
910     CompatInfo info;
911     ActionsInfo *actions;
912
913     actions = NewActionsInfo();
914     if (!actions)
915         return false;
916
917     InitCompatInfo(&info, keymap->ctx, actions, &keymap->mods);
918     info.default_interp.merge = merge;
919     info.default_led.merge = merge;
920
921     HandleCompatMapFile(&info, file, merge);
922     if (info.errorCount != 0)
923         goto err_info;
924
925     if (!CopyCompatToKeymap(keymap, &info))
926         goto err_info;
927
928     ClearCompatInfo(&info);
929     FreeActionsInfo(actions);
930     return true;
931
932 err_info:
933     ClearCompatInfo(&info);
934     FreeActionsInfo(actions);
935     return false;
936 }