• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
siText(SymInterpInfo * si,CompatInfo * info)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
ReportSINotArray(CompatInfo * info,SymInterpInfo * si,const char * field)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
ReportSIBadType(CompatInfo * info,SymInterpInfo * si,const char * field,const char * wanted)124 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
125                 const char *wanted)
126 {
127     return ReportBadType(info->ctx, "symbol interpretation", field,
128                          siText(si, info), wanted);
129 }
130 
131 static inline bool
ReportLedBadType(CompatInfo * info,LedInfo * ledi,const char * field,const char * wanted)132 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
133                  const char *wanted)
134 {
135     return ReportBadType(info->ctx, "indicator map", field,
136                          xkb_atom_text(info->ctx, ledi->led.name),
137                          wanted);
138 }
139 
140 static inline bool
ReportLedNotArray(CompatInfo * info,LedInfo * ledi,const char * field)141 ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)
142 {
143     return ReportNotArray(info->ctx, "indicator map", field,
144                           xkb_atom_text(info->ctx, ledi->led.name));
145 }
146 
147 static void
InitCompatInfo(CompatInfo * info,struct xkb_context * ctx,ActionsInfo * actions,const struct xkb_mod_set * mods)148 InitCompatInfo(CompatInfo *info, struct xkb_context *ctx,
149                ActionsInfo *actions, const struct xkb_mod_set *mods)
150 {
151     memset(info, 0, sizeof(*info));
152     info->ctx = ctx;
153     info->actions = actions;
154     info->mods = *mods;
155     info->default_interp.merge = MERGE_OVERRIDE;
156     info->default_interp.interp.virtual_mod = XKB_MOD_INVALID;
157     info->default_led.merge = MERGE_OVERRIDE;
158 }
159 
160 static void
ClearCompatInfo(CompatInfo * info)161 ClearCompatInfo(CompatInfo *info)
162 {
163     free(info->name);
164     darray_free(info->interps);
165 }
166 
167 static SymInterpInfo *
FindMatchingInterp(CompatInfo * info,SymInterpInfo * new)168 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
169 {
170     SymInterpInfo *old;
171 
172     darray_foreach(old, info->interps)
173         if (old->interp.sym == new->interp.sym &&
174             old->interp.mods == new->interp.mods &&
175             old->interp.match == new->interp.match)
176             return old;
177 
178     return NULL;
179 }
180 
181 static bool
UseNewInterpField(enum si_field field,SymInterpInfo * old,SymInterpInfo * new,bool report,enum si_field * collide)182 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
183                   bool report, enum si_field *collide)
184 {
185     if (!(old->defined & field))
186         return true;
187 
188     if (new->defined & field) {
189         if (report)
190             *collide |= field;
191 
192         if (new->merge != MERGE_AUGMENT)
193             return true;
194     }
195 
196     return false;
197 }
198 
199 static bool
AddInterp(CompatInfo * info,SymInterpInfo * new,bool same_file)200 AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
201 {
202     SymInterpInfo *old = FindMatchingInterp(info, new);
203     if (old) {
204         const int verbosity = xkb_context_get_log_verbosity(info->ctx);
205         const bool report = (same_file && verbosity > 0) || verbosity > 9;
206         enum si_field collide = 0;
207 
208         if (new->merge == MERGE_REPLACE) {
209             if (report)
210                 log_warn(info->ctx,
211                          "Multiple definitions for \"%s\"; "
212                          "Earlier interpretation ignored\n",
213                          siText(new, info));
214             *old = *new;
215             return true;
216         }
217 
218         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
219                               &collide)) {
220             old->interp.virtual_mod = new->interp.virtual_mod;
221             old->defined |= SI_FIELD_VIRTUAL_MOD;
222         }
223         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
224                               &collide)) {
225             old->interp.action = new->interp.action;
226             old->defined |= SI_FIELD_ACTION;
227         }
228         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
229                               &collide)) {
230             old->interp.repeat = new->interp.repeat;
231             old->defined |= SI_FIELD_AUTO_REPEAT;
232         }
233         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
234                               &collide)) {
235             old->interp.level_one_only = new->interp.level_one_only;
236             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
237         }
238 
239         if (collide) {
240             log_warn(info->ctx,
241                      "Multiple interpretations of \"%s\"; "
242                      "Using %s definition for duplicate fields\n",
243                      siText(new, info),
244                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
245         }
246 
247         return true;
248     }
249 
250     darray_append(info->interps, *new);
251     return true;
252 }
253 
254 /***====================================================================***/
255 
256 static bool
ResolveStateAndPredicate(ExprDef * expr,enum xkb_match_operation * pred_rtrn,xkb_mod_mask_t * mods_rtrn,CompatInfo * info)257 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
258                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
259 {
260     if (expr == NULL) {
261         *pred_rtrn = MATCH_ANY_OR_NONE;
262         *mods_rtrn = MOD_REAL_MASK_ALL;
263         return true;
264     }
265 
266     *pred_rtrn = MATCH_EXACTLY;
267     if (expr->expr.op == EXPR_ACTION_DECL) {
268         const char *pred_txt = xkb_atom_text(info->ctx, expr->action.name);
269         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn) ||
270             !expr->action.args || expr->action.args->common.next) {
271             log_err(info->ctx,
272                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
273             return false;
274         }
275         expr = expr->action.args;
276     }
277     else if (expr->expr.op == EXPR_IDENT) {
278         const char *pred_txt = xkb_atom_text(info->ctx, expr->ident.ident);
279         if (pred_txt && istreq(pred_txt, "any")) {
280             *pred_rtrn = MATCH_ANY;
281             *mods_rtrn = MOD_REAL_MASK_ALL;
282             return true;
283         }
284     }
285 
286     return ExprResolveModMask(info->ctx, expr, MOD_REAL, &info->mods,
287                               mods_rtrn);
288 }
289 
290 /***====================================================================***/
291 
292 static bool
UseNewLEDField(enum led_field field,LedInfo * old,LedInfo * new,bool report,enum led_field * collide)293 UseNewLEDField(enum led_field field, LedInfo *old, LedInfo *new,
294                bool report, enum led_field *collide)
295 {
296     if (!(old->defined & field))
297         return true;
298 
299     if (new->defined & field) {
300         if (report)
301             *collide |= field;
302 
303         if (new->merge != MERGE_AUGMENT)
304             return true;
305     }
306 
307     return false;
308 }
309 
310 static bool
AddLedMap(CompatInfo * info,LedInfo * new,bool same_file)311 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
312 {
313     enum led_field collide;
314     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
315     const bool report = (same_file && verbosity > 0) || verbosity > 9;
316 
317     for (xkb_led_index_t i = 0; i < info->num_leds; i++) {
318         LedInfo *old = &info->leds[i];
319 
320         if (old->led.name != new->led.name)
321             continue;
322 
323         if (old->led.mods.mods == new->led.mods.mods &&
324             old->led.groups == new->led.groups &&
325             old->led.ctrls == new->led.ctrls &&
326             old->led.which_mods == new->led.which_mods &&
327             old->led.which_groups == new->led.which_groups) {
328             old->defined |= new->defined;
329             return true;
330         }
331 
332         if (new->merge == MERGE_REPLACE) {
333             if (report)
334                 log_warn(info->ctx,
335                          "Map for indicator %s redefined; "
336                          "Earlier definition ignored\n",
337                          xkb_atom_text(info->ctx, old->led.name));
338             *old = *new;
339             return true;
340         }
341 
342         collide = 0;
343         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
344             old->led.which_mods = new->led.which_mods;
345             old->led.mods = new->led.mods;
346             old->defined |= LED_FIELD_MODS;
347         }
348         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
349             old->led.which_groups = new->led.which_groups;
350             old->led.groups = new->led.groups;
351             old->defined |= LED_FIELD_GROUPS;
352         }
353         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
354             old->led.ctrls = new->led.ctrls;
355             old->defined |= LED_FIELD_CTRLS;
356         }
357 
358         if (collide) {
359             log_warn(info->ctx,
360                      "Map for indicator %s redefined; "
361                      "Using %s definition for duplicate fields\n",
362                      xkb_atom_text(info->ctx, old->led.name),
363                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
364         }
365 
366         return true;
367     }
368 
369     if (info->num_leds >= XKB_MAX_LEDS) {
370         log_err(info->ctx,
371                 "Too many LEDs defined (maximum %d)\n",
372                 XKB_MAX_LEDS);
373         return false;
374     }
375     info->leds[info->num_leds++] = *new;
376     return true;
377 }
378 
379 static void
MergeIncludedCompatMaps(CompatInfo * into,CompatInfo * from,enum merge_mode merge)380 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
381                         enum merge_mode merge)
382 {
383     if (from->errorCount > 0) {
384         into->errorCount += from->errorCount;
385         return;
386     }
387 
388     into->mods = from->mods;
389 
390     if (into->name == NULL) {
391         into->name = from->name;
392         from->name = NULL;
393     }
394 
395     if (darray_empty(into->interps)) {
396         into->interps = from->interps;
397         darray_init(from->interps);
398     }
399     else {
400         SymInterpInfo *si;
401         darray_foreach(si, from->interps) {
402             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
403             if (!AddInterp(into, si, false))
404                 into->errorCount++;
405         }
406     }
407 
408     if (into->num_leds == 0) {
409         memcpy(into->leds, from->leds, sizeof(*from->leds) * from->num_leds);
410         into->num_leds = from->num_leds;
411         from->num_leds = 0;
412     }
413     else {
414         for (xkb_led_index_t i = 0; i < from->num_leds; i++) {
415             LedInfo *ledi = &from->leds[i];
416             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
417             if (!AddLedMap(into, ledi, false))
418                 into->errorCount++;
419         }
420     }
421 }
422 
423 static void
424 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
425 
426 static bool
HandleIncludeCompatMap(CompatInfo * info,IncludeStmt * include)427 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
428 {
429     CompatInfo included;
430 
431     InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
432     included.name = include->stmt;
433     include->stmt = NULL;
434 
435     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
436         CompatInfo next_incl;
437         XkbFile *file;
438 
439         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
440         if (!file) {
441             info->errorCount += 10;
442             ClearCompatInfo(&included);
443             return false;
444         }
445 
446         InitCompatInfo(&next_incl, info->ctx, info->actions, &included.mods);
447         next_incl.default_interp = info->default_interp;
448         next_incl.default_interp.merge = stmt->merge;
449         next_incl.default_led = info->default_led;
450         next_incl.default_led.merge = stmt->merge;
451 
452         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
453 
454         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
455 
456         ClearCompatInfo(&next_incl);
457         FreeXkbFile(file);
458     }
459 
460     MergeIncludedCompatMaps(info, &included, include->merge);
461     ClearCompatInfo(&included);
462 
463     return (info->errorCount == 0);
464 }
465 
466 static bool
SetInterpField(CompatInfo * info,SymInterpInfo * si,const char * field,ExprDef * arrayNdx,ExprDef * value)467 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
468                ExprDef *arrayNdx, ExprDef *value)
469 {
470     xkb_mod_index_t ndx;
471 
472     if (istreq(field, "action")) {
473         if (arrayNdx)
474             return ReportSINotArray(info, si, field);
475 
476         if (!HandleActionDef(info->ctx, info->actions, &info->mods,
477                              value, &si->interp.action))
478             return false;
479 
480         si->defined |= SI_FIELD_ACTION;
481     }
482     else if (istreq(field, "virtualmodifier") ||
483              istreq(field, "virtualmod")) {
484         if (arrayNdx)
485             return ReportSINotArray(info, si, field);
486 
487         if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->mods, &ndx))
488             return ReportSIBadType(info, si, field, "virtual modifier");
489 
490         si->interp.virtual_mod = ndx;
491         si->defined |= SI_FIELD_VIRTUAL_MOD;
492     }
493     else if (istreq(field, "repeat")) {
494         bool set;
495 
496         if (arrayNdx)
497             return ReportSINotArray(info, si, field);
498 
499         if (!ExprResolveBoolean(info->ctx, value, &set))
500             return ReportSIBadType(info, si, field, "boolean");
501 
502         si->interp.repeat = set;
503 
504         si->defined |= SI_FIELD_AUTO_REPEAT;
505     }
506     else if (istreq(field, "locking")) {
507         log_dbg(info->ctx,
508                 "The \"locking\" field in symbol interpretation is unsupported; "
509                 "Ignored\n");
510     }
511     else if (istreq(field, "usemodmap") ||
512              istreq(field, "usemodmapmods")) {
513         unsigned int val;
514 
515         if (arrayNdx)
516             return ReportSINotArray(info, si, field);
517 
518         if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
519             return ReportSIBadType(info, si, field, "level specification");
520 
521         si->interp.level_one_only = val;
522         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
523     }
524     else {
525         return ReportBadField(info->ctx, "symbol interpretation", field,
526                               siText(si, info));
527     }
528 
529     return true;
530 }
531 
532 static bool
SetLedMapField(CompatInfo * info,LedInfo * ledi,const char * field,ExprDef * arrayNdx,ExprDef * value)533 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
534                ExprDef *arrayNdx, ExprDef *value)
535 {
536     bool ok = true;
537 
538     if (istreq(field, "modifiers") || istreq(field, "mods")) {
539         if (arrayNdx)
540             return ReportLedNotArray(info, ledi, field);
541 
542         if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
543                                 &info->mods, &ledi->led.mods.mods))
544             return ReportLedBadType(info, ledi, field, "modifier mask");
545 
546         ledi->defined |= LED_FIELD_MODS;
547     }
548     else if (istreq(field, "groups")) {
549         unsigned int mask;
550 
551         if (arrayNdx)
552             return ReportLedNotArray(info, ledi, field);
553 
554         if (!ExprResolveMask(info->ctx, value, &mask, groupMaskNames))
555             return ReportLedBadType(info, ledi, field, "group mask");
556 
557         ledi->led.groups = mask;
558         ledi->defined |= LED_FIELD_GROUPS;
559     }
560     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
561         unsigned int mask;
562 
563         if (arrayNdx)
564             return ReportLedNotArray(info, ledi, field);
565 
566         if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
567             return ReportLedBadType(info, ledi, field, "controls mask");
568 
569         ledi->led.ctrls = mask;
570         ledi->defined |= LED_FIELD_CTRLS;
571     }
572     else if (istreq(field, "allowexplicit")) {
573         log_dbg(info->ctx,
574                 "The \"allowExplicit\" field in indicator statements is unsupported; "
575                 "Ignored\n");
576     }
577     else if (istreq(field, "whichmodstate") ||
578              istreq(field, "whichmodifierstate")) {
579         unsigned int mask;
580 
581         if (arrayNdx)
582             return ReportLedNotArray(info, ledi, field);
583 
584         if (!ExprResolveMask(info->ctx, value, &mask,
585                              modComponentMaskNames))
586             return ReportLedBadType(info, ledi, field,
587                                     "mask of modifier state components");
588 
589         ledi->led.which_mods = mask;
590     }
591     else if (istreq(field, "whichgroupstate")) {
592         unsigned mask;
593 
594         if (arrayNdx)
595             return ReportLedNotArray(info, ledi, field);
596 
597         if (!ExprResolveMask(info->ctx, value, &mask,
598                              groupComponentMaskNames))
599             return ReportLedBadType(info, ledi, field,
600                                     "mask of group state components");
601 
602         ledi->led.which_groups = mask;
603     }
604     else if (istreq(field, "driveskbd") ||
605              istreq(field, "driveskeyboard") ||
606              istreq(field, "leddriveskbd") ||
607              istreq(field, "leddriveskeyboard") ||
608              istreq(field, "indicatordriveskbd") ||
609              istreq(field, "indicatordriveskeyboard")) {
610         log_dbg(info->ctx,
611                 "The \"%s\" field in indicator statements is unsupported; "
612                 "Ignored\n", field);
613     }
614     else if (istreq(field, "index")) {
615         /* Users should see this, it might cause unexpected behavior. */
616         log_err(info->ctx,
617                 "The \"index\" field in indicator statements is unsupported; "
618                 "Ignored\n");
619     }
620     else {
621         log_err(info->ctx,
622                 "Unknown field %s in map for %s indicator; "
623                 "Definition ignored\n",
624                 field, xkb_atom_text(info->ctx, ledi->led.name));
625         ok = false;
626     }
627 
628     return ok;
629 }
630 
631 static bool
HandleGlobalVar(CompatInfo * info,VarDef * stmt)632 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
633 {
634     const char *elem, *field;
635     ExprDef *ndx;
636     bool ret;
637 
638     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
639         ret = false;
640     else if (elem && istreq(elem, "interpret"))
641         ret = SetInterpField(info, &info->default_interp, field, ndx,
642                              stmt->value);
643     else if (elem && istreq(elem, "indicator"))
644         ret = SetLedMapField(info, &info->default_led, field, ndx,
645                              stmt->value);
646     else
647         ret = SetActionField(info->ctx, info->actions, &info->mods,
648                              elem, field, ndx, stmt->value);
649     return ret;
650 }
651 
652 static bool
HandleInterpBody(CompatInfo * info,VarDef * def,SymInterpInfo * si)653 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
654 {
655     bool ok = true;
656     const char *elem, *field;
657     ExprDef *arrayNdx;
658 
659     for (; def; def = (VarDef *) def->common.next) {
660         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
661             log_err(info->ctx,
662                     "Cannot set a global default value from within an interpret statement; "
663                     "Move statements to the global file scope\n");
664             ok = false;
665             continue;
666         }
667 
668         ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
669         if (!ok)
670             continue;
671 
672         ok = SetInterpField(info, si, field, arrayNdx, def->value);
673     }
674 
675     return ok;
676 }
677 
678 static bool
HandleInterpDef(CompatInfo * info,InterpDef * def,enum merge_mode merge)679 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
680 {
681     enum xkb_match_operation pred;
682     xkb_mod_mask_t mods;
683     SymInterpInfo si;
684 
685     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
686         log_err(info->ctx,
687                 "Couldn't determine matching modifiers; "
688                 "Symbol interpretation ignored\n");
689         return false;
690     }
691 
692     si = info->default_interp;
693     si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
694     si.interp.sym = def->sym;
695     si.interp.match = pred;
696     si.interp.mods = mods;
697 
698     if (!HandleInterpBody(info, def->def, &si)) {
699         info->errorCount++;
700         return false;
701     }
702 
703     if (!AddInterp(info, &si, true)) {
704         info->errorCount++;
705         return false;
706     }
707 
708     return true;
709 }
710 
711 static bool
HandleLedMapDef(CompatInfo * info,LedMapDef * def,enum merge_mode merge)712 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
713 {
714     LedInfo ledi;
715     VarDef *var;
716     bool ok;
717 
718     if (def->merge != MERGE_DEFAULT)
719         merge = def->merge;
720 
721     ledi = info->default_led;
722     ledi.merge = merge;
723     ledi.led.name = def->name;
724 
725     ok = true;
726     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
727         const char *elem, *field;
728         ExprDef *arrayNdx;
729         if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
730             ok = false;
731             continue;
732         }
733 
734         if (elem) {
735             log_err(info->ctx,
736                     "Cannot set defaults for \"%s\" element in indicator map; "
737                     "Assignment to %s.%s ignored\n", elem, elem, field);
738             ok = false;
739         }
740         else {
741             ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
742         }
743     }
744 
745     if (ok)
746         return AddLedMap(info, &ledi, true);
747 
748     return false;
749 }
750 
751 static void
HandleCompatMapFile(CompatInfo * info,XkbFile * file,enum merge_mode merge)752 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
753 {
754     bool ok;
755 
756     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
757 
758     free(info->name);
759     info->name = strdup_safe(file->name);
760 
761     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
762         switch (stmt->type) {
763         case STMT_INCLUDE:
764             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
765             break;
766         case STMT_INTERP:
767             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
768             break;
769         case STMT_GROUP_COMPAT:
770             log_dbg(info->ctx,
771                     "The \"group\" statement in compat is unsupported; "
772                     "Ignored\n");
773             ok = true;
774             break;
775         case STMT_LED_MAP:
776             ok = HandleLedMapDef(info, (LedMapDef *) stmt, merge);
777             break;
778         case STMT_VAR:
779             ok = HandleGlobalVar(info, (VarDef *) stmt);
780             break;
781         case STMT_VMOD:
782             ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
783             break;
784         default:
785             log_err(info->ctx,
786                     "Compat files may not include other types; "
787                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
788             ok = false;
789             break;
790         }
791 
792         if (!ok)
793             info->errorCount++;
794 
795         if (info->errorCount > 10) {
796             log_err(info->ctx,
797                     "Abandoning compatibility map \"%s\"\n", file->name);
798             break;
799         }
800     }
801 }
802 
803 /* Temporary struct for CopyInterps. */
804 struct collect {
805     darray(struct xkb_sym_interpret) sym_interprets;
806 };
807 
808 static void
CopyInterps(CompatInfo * info,bool needSymbol,enum xkb_match_operation pred,struct collect * collect)809 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
810             struct collect *collect)
811 {
812     SymInterpInfo *si;
813 
814     darray_foreach(si, info->interps)
815         if (si->interp.match == pred &&
816             (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
817             darray_append(collect->sym_interprets, si->interp);
818 }
819 
820 static void
CopyLedMapDefsToKeymap(struct xkb_keymap * keymap,CompatInfo * info)821 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
822 {
823     for (xkb_led_index_t idx = 0; idx < info->num_leds; idx++) {
824         LedInfo *ledi = &info->leds[idx];
825         xkb_led_index_t i;
826         struct xkb_led *led;
827 
828         /*
829          * Find the LED with the given name, if it was already declared
830          * in keycodes.
831          */
832         xkb_leds_enumerate(i, led, keymap)
833             if (led->name == ledi->led.name)
834                 break;
835 
836         /* Not previously declared; create it with next free index. */
837         if (i >= keymap->num_leds) {
838             log_dbg(keymap->ctx,
839                     "Indicator name \"%s\" was not declared in the keycodes section; "
840                     "Adding new indicator\n",
841                     xkb_atom_text(keymap->ctx, ledi->led.name));
842 
843             xkb_leds_enumerate(i, led, keymap)
844                 if (led->name == XKB_ATOM_NONE)
845                     break;
846 
847             if (i >= keymap->num_leds) {
848                 /* Not place to put it; ignore. */
849                 if (i >= XKB_MAX_LEDS) {
850                     log_err(keymap->ctx,
851                             "Too many indicators (maximum is %d); "
852                             "Indicator name \"%s\" ignored\n",
853                             XKB_MAX_LEDS,
854                             xkb_atom_text(keymap->ctx, ledi->led.name));
855                     continue;
856                 }
857 
858                 /* Add a new LED. */
859                 led = &keymap->leds[keymap->num_leds++];
860             }
861         }
862 
863         *led = ledi->led;
864         if (led->groups != 0 && led->which_groups == 0)
865             led->which_groups = XKB_STATE_LAYOUT_EFFECTIVE;
866         if (led->mods.mods != 0 && led->which_mods == 0)
867             led->which_mods = XKB_STATE_MODS_EFFECTIVE;
868     }
869 }
870 
871 static bool
CopyCompatToKeymap(struct xkb_keymap * keymap,CompatInfo * info)872 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
873 {
874     keymap->compat_section_name = strdup_safe(info->name);
875     XkbEscapeMapName(keymap->compat_section_name);
876 
877     keymap->mods = info->mods;
878 
879     if (!darray_empty(info->interps)) {
880         struct collect collect;
881         darray_init(collect.sym_interprets);
882 
883         /* Most specific to least specific. */
884         CopyInterps(info, true, MATCH_EXACTLY, &collect);
885         CopyInterps(info, true, MATCH_ALL, &collect);
886         CopyInterps(info, true, MATCH_NONE, &collect);
887         CopyInterps(info, true, MATCH_ANY, &collect);
888         CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
889         CopyInterps(info, false, MATCH_EXACTLY, &collect);
890         CopyInterps(info, false, MATCH_ALL, &collect);
891         CopyInterps(info, false, MATCH_NONE, &collect);
892         CopyInterps(info, false, MATCH_ANY, &collect);
893         CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
894 
895         darray_steal(collect.sym_interprets,
896                      &keymap->sym_interprets, &keymap->num_sym_interprets);
897     }
898 
899     CopyLedMapDefsToKeymap(keymap, info);
900 
901     return true;
902 }
903 
904 bool
CompileCompatMap(XkbFile * file,struct xkb_keymap * keymap,enum merge_mode merge)905 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
906                  enum merge_mode merge)
907 {
908     CompatInfo info;
909     ActionsInfo *actions;
910 
911     actions = NewActionsInfo();
912     if (!actions)
913         return false;
914 
915     InitCompatInfo(&info, keymap->ctx, actions, &keymap->mods);
916     info.default_interp.merge = merge;
917     info.default_led.merge = merge;
918 
919     HandleCompatMapFile(&info, file, merge);
920     if (info.errorCount != 0)
921         goto err_info;
922 
923     if (!CopyCompatToKeymap(keymap, &info))
924         goto err_info;
925 
926     ClearCompatInfo(&info);
927     FreeActionsInfo(actions);
928     return true;
929 
930 err_info:
931     ClearCompatInfo(&info);
932     FreeActionsInfo(actions);
933     return false;
934 }
935