Simplify HandleInclude functions
[platform/upstream/libxkbcommon.git] / src / xkbcomp / symbols.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 #include <limits.h>
28
29 #include "xkbcomp-priv.h"
30 #include "parseutils.h"
31 #include "action.h"
32 #include "vmod.h"
33
34 /***====================================================================***/
35
36 /* Needed to work with the typechecker. */
37 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
38 typedef darray(union xkb_action) darray_xkb_action;
39
40 #define RepeatYes       1
41 #define RepeatNo        0
42 #define RepeatUndefined ~((unsigned) 0)
43
44 #define _Key_Syms       (1 << 0)
45 #define _Key_Acts       (1 << 1)
46 #define _Key_Repeat     (1 << 2)
47 #define _Key_Behavior   (1 << 3)
48 #define _Key_Type_Dflt  (1 << 4)
49 #define _Key_Types      (1 << 5)
50 #define _Key_GroupInfo  (1 << 6)
51 #define _Key_VModMap    (1 << 7)
52
53 static inline const char *
54 longText(unsigned long val)
55 {
56     char buf[4];
57
58     LongToKeyName(val, buf);
59     return XkbcKeyNameText(buf);
60 }
61
62 typedef struct _KeyInfo {
63     unsigned short defined;
64     unsigned file_id;
65     enum merge_mode merge;
66
67     unsigned long name; /* the 4 chars of the key name, as long */
68     unsigned char typesDefined;
69     unsigned char symsDefined;
70     unsigned char actsDefined;
71     unsigned int numLevels[XkbNumKbdGroups];
72
73     /* syms[group] -> Single array for all the keysyms in the group. */
74     darray_xkb_keysym_t syms[XkbNumKbdGroups];
75     /*
76      * symsMapIndex[group][level] -> The index from which the syms for
77      * the level begin in the syms[group] array. Remember each keycode
78      * can have multiple keysyms in each level (that is, each key press
79      * can result in multiple keysyms).
80      */
81     darray(int) symsMapIndex[XkbNumKbdGroups];
82     /*
83      * symsMapNumEntries[group][level] -> How many syms are in
84      * syms[group][symsMapIndex[group][level]].
85      */
86     darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
87
88     darray_xkb_action acts[XkbNumKbdGroups];
89
90     xkb_atom_t types[XkbNumKbdGroups];
91     unsigned repeat;
92     struct xkb_behavior behavior;
93     unsigned short vmodmap;
94     xkb_atom_t dfltType;
95
96     uint8_t out_of_range_group_action;
97     xkb_group_index_t out_of_range_group_number;
98 } KeyInfo;
99
100 /**
101  * Init the given key info to sane values.
102  */
103 static void
104 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
105 {
106     xkb_group_index_t i;
107     static const char dflt[4] = "*";
108
109     keyi->defined = 0;
110     keyi->file_id = file_id;
111     keyi->merge = MERGE_OVERRIDE;
112     keyi->name = KeyNameToLong(dflt);
113     keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
114
115     for (i = 0; i < XkbNumKbdGroups; i++) {
116         keyi->numLevels[i] = 0;
117         keyi->types[i] = XKB_ATOM_NONE;
118         darray_init(keyi->syms[i]);
119         darray_init(keyi->symsMapIndex[i]);
120         darray_init(keyi->symsMapNumEntries[i]);
121         darray_init(keyi->acts[i]);
122     }
123
124     keyi->dfltType = XKB_ATOM_NONE;
125     keyi->behavior.type = XkbKB_Default;
126     keyi->behavior.data = 0;
127     keyi->vmodmap = 0;
128     keyi->repeat = RepeatUndefined;
129     keyi->out_of_range_group_action = 0;
130     keyi->out_of_range_group_number = 0;
131 }
132
133 static void
134 FreeKeyInfo(KeyInfo *keyi)
135 {
136     xkb_group_index_t i;
137
138     for (i = 0; i < XkbNumKbdGroups; i++) {
139         darray_free(keyi->syms[i]);
140         darray_free(keyi->symsMapIndex[i]);
141         darray_free(keyi->symsMapNumEntries[i]);
142         darray_free(keyi->acts[i]);
143     }
144 }
145
146 /**
147  * Copy old into new, optionally reset old to 0.
148  * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
149  * newly allocated and new points to the new memory areas.
150  */
151 static bool
152 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
153 {
154     xkb_group_index_t i;
155
156     *new = *old;
157
158     if (clearOld) {
159         for (i = 0; i < XkbNumKbdGroups; i++) {
160             old->numLevels[i] = 0;
161             darray_init(old->symsMapIndex[i]);
162             darray_init(old->symsMapNumEntries[i]);
163             darray_init(old->syms[i]);
164             darray_init(old->acts[i]);
165         }
166     }
167     else {
168         for (i = 0; i < XkbNumKbdGroups; i++) {
169             darray_copy(new->syms[i], old->syms[i]);
170             darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
171             darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
172             darray_copy(new->acts[i], old->acts[i]);
173         }
174     }
175
176     return true;
177 }
178
179 /***====================================================================***/
180
181 typedef struct _ModMapEntry {
182     struct list entry;
183     enum merge_mode merge;
184     bool haveSymbol;
185     int modifier;
186     union {
187         unsigned long keyName;
188         xkb_keysym_t keySym;
189     } u;
190 } ModMapEntry;
191
192 typedef struct _SymbolsInfo {
193     char *name;         /* e.g. pc+us+inet(evdev) */
194     int errorCount;
195     unsigned file_id;
196     enum merge_mode merge;
197     xkb_group_index_t explicit_group;
198     darray(KeyInfo) keys;
199     KeyInfo dflt;
200     VModInfo vmods;
201     ActionInfo *action;
202     xkb_atom_t groupNames[XkbNumKbdGroups];
203
204     struct list modMaps;
205 } SymbolsInfo;
206
207 static void
208 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
209                 unsigned file_id)
210 {
211     xkb_group_index_t i;
212
213     info->name = NULL;
214     info->explicit_group = 0;
215     info->errorCount = 0;
216     info->file_id = file_id;
217     info->merge = MERGE_OVERRIDE;
218     darray_init(info->keys);
219     darray_growalloc(info->keys, 110);
220     list_init(&info->modMaps);
221     for (i = 0; i < XkbNumKbdGroups; i++)
222         info->groupNames[i] = XKB_ATOM_NONE;
223     InitKeyInfo(&info->dflt, file_id);
224     InitVModInfo(&info->vmods, keymap);
225     info->action = NULL;
226 }
227
228 static void
229 FreeSymbolsInfo(SymbolsInfo * info)
230 {
231     KeyInfo *keyi;
232     ModMapEntry *mm, *next;
233
234     free(info->name);
235     darray_foreach(keyi, info->keys) {
236         FreeKeyInfo(keyi);
237     }
238     darray_free(info->keys);
239     list_foreach_safe(mm, next, &info->modMaps, entry)
240         free(mm);
241     memset(info, 0, sizeof(SymbolsInfo));
242 }
243
244 static bool
245 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group, unsigned int numLevels,
246                unsigned sizeSyms, bool forceActions)
247 {
248     int i;
249
250     if (darray_size(keyi->syms[group]) < sizeSyms)
251         darray_resize0(keyi->syms[group], sizeSyms);
252
253     if (darray_empty(keyi->symsMapIndex[group]) ||
254         keyi->numLevels[group] < numLevels) {
255         darray_resize(keyi->symsMapIndex[group], numLevels);
256         for (i = keyi->numLevels[group]; i < numLevels; i++)
257             darray_item(keyi->symsMapIndex[group], i) = -1;
258     }
259
260     if (darray_empty(keyi->symsMapNumEntries[group]) ||
261         keyi->numLevels[group] < numLevels)
262         darray_resize0(keyi->symsMapNumEntries[group], numLevels);
263
264     if ((forceActions && (keyi->numLevels[group] < numLevels ||
265                           darray_empty(keyi->acts[group]))) ||
266         (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
267         darray_resize0(keyi->acts[group], numLevels);
268
269     if (keyi->numLevels[group] < numLevels)
270         keyi->numLevels[group] = numLevels;
271
272     return true;
273 }
274
275 enum key_group_selector {
276     NONE = 0,
277     FROM = (1 << 0),
278     TO = (1 << 1),
279 };
280
281 static bool
282 MergeKeyGroups(SymbolsInfo * info,
283                KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
284 {
285     darray_xkb_keysym_t resultSyms;
286     enum key_group_selector using = NONE;
287     darray_xkb_action resultActs;
288     unsigned int resultWidth;
289     unsigned int resultSize = 0;
290     int cur_idx = 0;
291     int i;
292     bool report, clobber;
293
294     clobber = (from->merge != MERGE_AUGMENT);
295
296     report = (warningLevel > 9) ||
297              (into->file_id == from->file_id && warningLevel > 0);
298
299     darray_init(resultSyms);
300
301     if (into->numLevels[group] >= from->numLevels[group]) {
302         resultActs = into->acts[group];
303         resultWidth = into->numLevels[group];
304     }
305     else {
306         resultActs = from->acts[group];
307         resultWidth = from->numLevels[group];
308         darray_resize(into->symsMapIndex[group],
309                       from->numLevels[group]);
310         darray_resize0(into->symsMapNumEntries[group],
311                        from->numLevels[group]);
312
313         for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
314             darray_item(into->symsMapIndex[group], i) = -1;
315     }
316
317     if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
318                                      !darray_empty(from->acts[group]))) {
319         darray_resize0(resultActs, resultWidth);
320         for (i = 0; i < resultWidth; i++) {
321             union xkb_action *fromAct = NULL, *toAct = NULL;
322
323             if (!darray_empty(from->acts[group]))
324                 fromAct = &darray_item(from->acts[group], i);
325
326             if (!darray_empty(into->acts[group]))
327                 toAct = &darray_item(into->acts[group], i);
328
329             if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
330                 && (toAct != NULL)) {
331                 darray_item(resultActs, i) = *toAct;
332             }
333             else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
334                      && (fromAct != NULL)) {
335                 darray_item(resultActs, i) = *fromAct;
336             }
337             else {
338                 union xkb_action *use, *ignore;
339                 if (clobber) {
340                     use = fromAct;
341                     ignore = toAct;
342                 }
343                 else {
344                     use = toAct;
345                     ignore = fromAct;
346                 }
347                 if (report) {
348                     WARN
349                         ("Multiple actions for level %d/group %u on key %s\n",
350                         i + 1, group + 1, longText(into->name));
351                     ACTION("Using %s, ignoring %s\n",
352                            XkbcActionTypeText(use->type),
353                            XkbcActionTypeText(ignore->type));
354                 }
355                 if (use)
356                     darray_item(resultActs, i) = *use;
357             }
358         }
359     }
360
361     for (i = 0; i < resultWidth; i++) {
362         unsigned int fromSize = 0;
363         unsigned toSize = 0;
364
365         if (!darray_empty(from->symsMapNumEntries[group]) &&
366             i < from->numLevels[group])
367             fromSize = darray_item(from->symsMapNumEntries[group], i);
368
369         if (!darray_empty(into->symsMapNumEntries[group]) &&
370             i < into->numLevels[group])
371             toSize = darray_item(into->symsMapNumEntries[group], i);
372
373         if (fromSize == 0) {
374             resultSize += toSize;
375             using |= TO;
376         }
377         else if (toSize == 0 || clobber) {
378             resultSize += fromSize;
379             using |= FROM;
380         }
381         else {
382             resultSize += toSize;
383             using |= TO;
384         }
385     }
386
387     if (resultSize == 0)
388         goto out;
389
390     if (using == FROM) {
391         resultSyms = from->syms[group];
392         darray_free(into->symsMapNumEntries[group]);
393         darray_free(into->symsMapIndex[group]);
394         into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
395         into->symsMapIndex[group] = from->symsMapIndex[group];
396         darray_init(from->symsMapNumEntries[group]);
397         darray_init(from->symsMapIndex[group]);
398         goto out;
399     }
400     else if (using == TO) {
401         resultSyms = into->syms[group];
402         goto out;
403     }
404
405     darray_resize0(resultSyms, resultSize);
406
407     for (i = 0; i < resultWidth; i++) {
408         enum key_group_selector use = NONE;
409         unsigned int fromSize = 0;
410         unsigned int toSize = 0;
411
412         if (i < from->numLevels[group])
413             fromSize = darray_item(from->symsMapNumEntries[group], i);
414
415         if (i < into->numLevels[group])
416             toSize = darray_item(into->symsMapNumEntries[group], i);
417
418         if (fromSize == 0 && toSize == 0) {
419             darray_item(into->symsMapIndex[group], i) = -1;
420             darray_item(into->symsMapNumEntries[group], i) = 0;
421             continue;
422         }
423
424         if (fromSize == 0)
425             use = TO;
426         else if (toSize == 0 || clobber)
427             use = FROM;
428         else
429             use = TO;
430
431         if (toSize && fromSize && report) {
432             INFO("Multiple symbols for group %u, level %d on key %s\n",
433                  group + 1, i + 1, longText(into->name));
434             ACTION("Using %s, ignoring %s\n",
435                    (use == FROM ? "from" : "to"),
436                    (use == FROM ? "to" : "from"));
437         }
438
439         if (use == FROM) {
440             memcpy(darray_mem(resultSyms, cur_idx),
441                    darray_mem(from->syms[group],
442                               darray_item(from->symsMapIndex[group], i)),
443                    darray_item(from->symsMapNumEntries[group],
444                                i) * sizeof(xkb_keysym_t));
445             darray_item(into->symsMapIndex[group], i) = cur_idx;
446             darray_item(into->symsMapNumEntries[group], i) =
447                 darray_item(from->symsMapNumEntries[group], i);
448         }
449         else {
450             memcpy(darray_mem(resultSyms, cur_idx),
451                    darray_mem(into->syms[group],
452                               darray_item(into->symsMapIndex[group], i)),
453                    darray_item(into->symsMapNumEntries[group],
454                                i) * sizeof(xkb_keysym_t));
455             darray_item(into->symsMapIndex[group], i) = cur_idx;
456         }
457         cur_idx += darray_item(into->symsMapNumEntries[group], i);
458     }
459
460 out:
461     if (!darray_same(resultActs, into->acts[group]))
462         darray_free(into->acts[group]);
463     if (!darray_same(resultActs, from->acts[group]))
464         darray_free(from->acts[group]);
465     into->numLevels[group] = resultWidth;
466     if (!darray_same(resultSyms, into->syms[group]))
467         darray_free(into->syms[group]);
468     into->syms[group] = resultSyms;
469     if (!darray_same(resultSyms, from->syms[group]))
470         darray_free(from->syms[group]);
471     darray_init(from->syms[group]);
472     darray_free(from->symsMapIndex[group]);
473     darray_free(from->symsMapNumEntries[group]);
474     into->acts[group] = resultActs;
475     darray_init(from->acts[group]);
476     if (!darray_empty(into->syms[group]))
477         into->symsDefined |= (1 << group);
478     from->symsDefined &= ~(1 << group);
479     into->actsDefined |= (1 << group);
480     from->actsDefined &= ~(1 << group);
481
482     return true;
483 }
484
485 static bool
486 MergeKeys(SymbolsInfo *info, struct xkb_keymap *keymap,
487           KeyInfo *into, KeyInfo *from)
488 {
489     xkb_group_index_t i;
490     unsigned collide = 0;
491     bool report;
492
493     if (from->merge == MERGE_REPLACE) {
494         for (i = 0; i < XkbNumKbdGroups; i++) {
495             if (into->numLevels[i] != 0) {
496                 darray_free(into->syms[i]);
497                 darray_free(into->acts[i]);
498             }
499         }
500         *into = *from;
501         memset(from, 0, sizeof(KeyInfo));
502         return true;
503     }
504     report = ((warningLevel > 9) ||
505               ((into->file_id == from->file_id)
506                && (warningLevel > 0)));
507     for (i = 0; i < XkbNumKbdGroups; i++) {
508         if (from->numLevels[i] > 0) {
509             if (into->numLevels[i] == 0) {
510                 into->numLevels[i] = from->numLevels[i];
511                 into->syms[i] = from->syms[i];
512                 into->symsMapIndex[i] = from->symsMapIndex[i];
513                 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
514                 into->acts[i] = from->acts[i];
515                 into->symsDefined |= (1 << i);
516                 darray_init(from->syms[i]);
517                 darray_init(from->symsMapIndex[i]);
518                 darray_init(from->symsMapNumEntries[i]);
519                 darray_init(from->acts[i]);
520                 from->numLevels[i] = 0;
521                 from->symsDefined &= ~(1 << i);
522                 if (!darray_empty(into->syms[i]))
523                     into->defined |= _Key_Syms;
524                 if (!darray_empty(into->acts[i]))
525                     into->defined |= _Key_Acts;
526             }
527             else {
528                 if (report) {
529                     if (!darray_empty(into->syms[i]))
530                         collide |= _Key_Syms;
531                     if (!darray_empty(into->acts[i]))
532                         collide |= _Key_Acts;
533                 }
534                 MergeKeyGroups(info, into, from, (unsigned) i);
535             }
536         }
537         if (from->types[i] != XKB_ATOM_NONE) {
538             if ((into->types[i] != XKB_ATOM_NONE) && report &&
539                 (into->types[i] != from->types[i])) {
540                 xkb_atom_t use, ignore;
541                 collide |= _Key_Types;
542                 if (from->merge != MERGE_AUGMENT) {
543                     use = from->types[i];
544                     ignore = into->types[i];
545                 }
546                 else {
547                     use = into->types[i];
548                     ignore = from->types[i];
549                 }
550                 WARN
551                     ("Multiple definitions for group %d type of key %s\n",
552                     i, longText(into->name));
553                 ACTION("Using %s, ignoring %s\n",
554                        xkb_atom_text(keymap->ctx, use),
555                        xkb_atom_text(keymap->ctx, ignore));
556             }
557             if (from->merge != MERGE_AUGMENT ||
558                 into->types[i] == XKB_ATOM_NONE) {
559                 into->types[i] = from->types[i];
560             }
561         }
562     }
563     if (use_new_field(_Key_Behavior, into->defined, into->file_id,
564                       from->defined, from->file_id, from->merge, &collide)) {
565         into->behavior = from->behavior;
566         into->defined |= _Key_Behavior;
567     }
568     if (use_new_field(_Key_VModMap, into->defined, into->file_id,
569                       from->defined, from->file_id, from->merge, &collide)) {
570         into->vmodmap = from->vmodmap;
571         into->defined |= _Key_VModMap;
572     }
573     if (use_new_field(_Key_Repeat, into->defined, into->file_id,
574                       from->defined, from->file_id, from->merge, &collide)) {
575         into->repeat = from->repeat;
576         into->defined |= _Key_Repeat;
577     }
578     if (use_new_field(_Key_Type_Dflt, into->defined, into->file_id,
579                       from->defined, from->file_id, from->merge, &collide)) {
580         into->dfltType = from->dfltType;
581         into->defined |= _Key_Type_Dflt;
582     }
583     if (use_new_field(_Key_GroupInfo, into->defined, into->file_id,
584                       from->defined, from->file_id, from->merge, &collide)) {
585         into->out_of_range_group_action = from->out_of_range_group_action;
586         into->out_of_range_group_number = from->out_of_range_group_number;
587         into->defined |= _Key_GroupInfo;
588     }
589     if (collide) {
590         WARN("Symbol map for key %s redefined\n",
591              longText(into->name));
592         ACTION("Using %s definition for conflicting fields\n",
593                (from->merge == MERGE_AUGMENT ? "first" : "last"));
594     }
595     return true;
596 }
597
598 static bool
599 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, struct xkb_keymap *keymap)
600 {
601     unsigned long real_name;
602     KeyInfo *iter, *new;
603
604     darray_foreach(iter, info->keys)
605         if (iter->name == keyi->name)
606             return MergeKeys(info, keymap, iter, keyi);
607
608     if (FindKeyNameForAlias(keymap, keyi->name, &real_name))
609         darray_foreach(iter, info->keys)
610             if (iter->name == real_name)
611                 return MergeKeys(info, keymap, iter, keyi);
612
613     darray_resize0(info->keys, darray_size(info->keys) + 1);
614     new = &darray_item(info->keys, darray_size(info->keys) - 1);
615     return CopyKeyInfo(keyi, new, true);
616 }
617
618 static bool
619 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
620 {
621     ModMapEntry *mm;
622     bool clobber;
623
624     clobber = (new->merge != MERGE_AUGMENT);
625     list_foreach(mm, &info->modMaps, entry) {
626         if (new->haveSymbol && mm->haveSymbol
627             && (new->u.keySym == mm->u.keySym)) {
628             unsigned use, ignore;
629             if (mm->modifier != new->modifier) {
630                 if (clobber) {
631                     use = new->modifier;
632                     ignore = mm->modifier;
633                 }
634                 else {
635                     use = mm->modifier;
636                     ignore = new->modifier;
637                 }
638                 ERROR
639                     ("%s added to symbol map for multiple modifiers\n",
640                     XkbcKeysymText(new->u.keySym));
641                 ACTION("Using %s, ignoring %s.\n",
642                        XkbcModIndexText(use),
643                        XkbcModIndexText(ignore));
644                 mm->modifier = use;
645             }
646             return true;
647         }
648         if ((!new->haveSymbol) && (!mm->haveSymbol) &&
649             (new->u.keyName == mm->u.keyName)) {
650             unsigned use, ignore;
651             if (mm->modifier != new->modifier) {
652                 if (clobber) {
653                     use = new->modifier;
654                     ignore = mm->modifier;
655                 }
656                 else {
657                     use = mm->modifier;
658                     ignore = new->modifier;
659                 }
660                 ERROR("Key %s added to map for multiple modifiers\n",
661                       longText(new->u.keyName));
662                 ACTION("Using %s, ignoring %s.\n",
663                        XkbcModIndexText(use),
664                        XkbcModIndexText(ignore));
665                 mm->modifier = use;
666             }
667             return true;
668         }
669     }
670
671     mm = malloc(sizeof(*mm));
672     if (!mm) {
673         WSGO("Could not allocate modifier map entry\n");
674         ACTION("Modifier map for %s will be incomplete\n",
675                XkbcModIndexText(new->modifier));
676         return false;
677     }
678
679     *mm = *new;
680     list_add(&mm->entry, &info->modMaps);
681     return true;
682 }
683
684 /***====================================================================***/
685
686 static void
687 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
688                      enum merge_mode merge, struct xkb_keymap *keymap)
689 {
690     unsigned int i;
691     KeyInfo *keyi;
692     ModMapEntry *mm, *next;
693
694     if (from->errorCount > 0) {
695         into->errorCount += from->errorCount;
696         return;
697     }
698     if (into->name == NULL) {
699         into->name = from->name;
700         from->name = NULL;
701     }
702     for (i = 0; i < XkbNumKbdGroups; i++) {
703         if (from->groupNames[i] != XKB_ATOM_NONE) {
704             if ((merge != MERGE_AUGMENT) ||
705                 (into->groupNames[i] == XKB_ATOM_NONE))
706                 into->groupNames[i] = from->groupNames[i];
707         }
708     }
709
710     darray_foreach(keyi, from->keys) {
711         if (merge != MERGE_DEFAULT)
712             keyi->merge = merge;
713
714         if (!AddKeySymbols(into, keyi, keymap))
715             into->errorCount++;
716     }
717
718     list_foreach_safe(mm, next, &from->modMaps, entry) {
719         if (merge != MERGE_DEFAULT)
720             mm->merge = merge;
721         if (!AddModMapEntry(into, mm))
722             into->errorCount++;
723         free(mm);
724     }
725     list_init(&from->modMaps);
726 }
727
728 static void
729 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
730                   enum merge_mode merge,
731                   SymbolsInfo *info);
732
733 static bool
734 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
735                      SymbolsInfo *info)
736 {
737     enum merge_mode merge = MERGE_DEFAULT;
738     XkbFile *rtrn;
739     SymbolsInfo included, next_incl;
740
741     InitSymbolsInfo(&included, keymap, info->file_id);
742     if (stmt->stmt) {
743         free(included.name);
744         included.name = stmt->stmt;
745         stmt->stmt = NULL;
746     }
747
748     for (; stmt; stmt = stmt->next) {
749         if (!ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
750                                 &rtrn, &merge)) {
751             info->errorCount += 10;
752             FreeSymbolsInfo(&included);
753             return false;
754         }
755
756         InitSymbolsInfo(&next_incl, keymap, rtrn->id);
757         next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
758         if (stmt->modifier)
759             next_incl.explicit_group = atoi(stmt->modifier) - 1;
760         else
761             next_incl.explicit_group = info->explicit_group;
762
763         HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
764
765         MergeIncludedSymbols(&included, &next_incl, merge, keymap);
766
767         FreeSymbolsInfo(&next_incl);
768         FreeXKBFile(rtrn);
769     }
770
771     MergeIncludedSymbols(info, &included, merge, keymap);
772     FreeSymbolsInfo(&included);
773
774     return (info->errorCount == 0);
775 }
776
777 #define SYMBOLS 1
778 #define ACTIONS 2
779
780 static bool
781 GetGroupIndex(KeyInfo *keyi, struct xkb_keymap *keymap,
782               ExprDef *arrayNdx, unsigned what, xkb_group_index_t *ndx_rtrn)
783 {
784     const char *name;
785     ExprResult tmp;
786
787     if (what == SYMBOLS)
788         name = "symbols";
789     else
790         name = "actions";
791
792     if (arrayNdx == NULL) {
793         xkb_group_index_t i;
794         unsigned defined;
795         if (what == SYMBOLS)
796             defined = keyi->symsDefined;
797         else
798             defined = keyi->actsDefined;
799
800         for (i = 0; i < XkbNumKbdGroups; i++) {
801             if ((defined & (1 << i)) == 0) {
802                 *ndx_rtrn = i;
803                 return true;
804             }
805         }
806         ERROR("Too many groups of %s for key %s (max %u)\n", name,
807               longText(keyi->name), XkbNumKbdGroups + 1);
808         ACTION("Ignoring %s defined for extra groups\n", name);
809         return false;
810     }
811     if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
812         ERROR("Illegal group index for %s of key %s\n", name,
813               longText(keyi->name));
814         ACTION("Definition with non-integer array index ignored\n");
815         return false;
816     }
817     *ndx_rtrn = tmp.uval - 1;
818     return true;
819 }
820
821 static bool
822 AddSymbolsToKey(KeyInfo *keyi, struct xkb_keymap *keymap,
823                 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
824 {
825     xkb_group_index_t ndx;
826     size_t nSyms, nLevels;
827     size_t i, j;
828
829     if (!GetGroupIndex(keyi, keymap, arrayNdx, SYMBOLS, &ndx))
830         return false;
831     if (value == NULL) {
832         keyi->symsDefined |= (1 << ndx);
833         return true;
834     }
835     if (value->op != ExprKeysymList) {
836         ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
837         ACTION("Ignoring symbols for group %u of %s\n", ndx + 1,
838                longText(keyi->name));
839         return false;
840     }
841     if (!darray_empty(keyi->syms[ndx])) {
842         ERROR("Symbols for key %s, group %u already defined\n",
843               longText(keyi->name), ndx + 1);
844         ACTION("Ignoring duplicate definition\n");
845         return false;
846     }
847     nSyms = darray_size(value->value.list.syms);
848     nLevels = darray_size(value->value.list.symsMapIndex);
849     if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
850         (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
851         WSGO("Could not resize group %u of key %s to contain %zu levels\n",
852              ndx + 1, longText(keyi->name), nSyms);
853         ACTION("Symbols lost\n");
854         return false;
855     }
856     keyi->symsDefined |= (1 << ndx);
857     for (i = 0; i < nLevels; i++) {
858         darray_item(keyi->symsMapIndex[ndx], i) =
859             darray_item(value->value.list.symsMapIndex, i);
860         darray_item(keyi->symsMapNumEntries[ndx], i) =
861             darray_item(value->value.list.symsNumEntries, i);
862
863         for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
864             /* FIXME: What's abort() doing here? */
865             if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
866                 abort();
867             if (!LookupKeysym(darray_item(value->value.list.syms,
868                                           darray_item(value->value.list.symsMapIndex,
869                                                       i) + j),
870                               &darray_item(keyi->syms[ndx],
871                                            darray_item(keyi->symsMapIndex[ndx],
872                                                        i) + j))) {
873                 WARN(
874                     "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
875                     darray_item(value->value.list.syms, i),
876                     longText(keyi->name),
877                     ndx + 1,
878                     xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
879                 while (--j >= 0)
880                     darray_item(keyi->syms[ndx],
881                                 darray_item(keyi->symsMapIndex[ndx],
882                                             i) + j) = XKB_KEY_NoSymbol;
883                 darray_item(keyi->symsMapIndex[ndx], i) = -1;
884                 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
885                 break;
886             }
887             if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
888                 darray_item(keyi->syms[ndx],
889                             darray_item(keyi->symsMapIndex[ndx],
890                                         i) + j) == XKB_KEY_NoSymbol) {
891                 darray_item(keyi->symsMapIndex[ndx], i) = -1;
892                 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
893             }
894         }
895     }
896     for (j = keyi->numLevels[ndx] - 1;
897          j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
898         keyi->numLevels[ndx]--;
899     return true;
900 }
901
902 static bool
903 AddActionsToKey(KeyInfo *keyi, struct xkb_keymap *keymap, ExprDef *arrayNdx,
904                 ExprDef *value, SymbolsInfo *info)
905 {
906     size_t i;
907     xkb_group_index_t ndx;
908     size_t nActs;
909     ExprDef *act;
910     struct xkb_any_action *toAct;
911
912     if (!GetGroupIndex(keyi, keymap, arrayNdx, ACTIONS, &ndx))
913         return false;
914
915     if (value == NULL) {
916         keyi->actsDefined |= (1 << ndx);
917         return true;
918     }
919     if (value->op != ExprActionList) {
920         WSGO("Bad expression type (%d) for action list value\n", value->op);
921         ACTION("Ignoring actions for group %u of %s\n", ndx,
922                longText(keyi->name));
923         return false;
924     }
925     if (!darray_empty(keyi->acts[ndx])) {
926         WSGO("Actions for key %s, group %u already defined\n",
927               longText(keyi->name), ndx);
928         return false;
929     }
930     for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
931         act = (ExprDef *) act->common.next;
932     }
933     if (nActs < 1) {
934         WSGO("Action list but not actions in AddActionsToKey\n");
935         return false;
936     }
937     if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
938         !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
939         WSGO("Could not resize group %u of key %s\n", ndx,
940               longText(keyi->name));
941         ACTION("Actions lost\n");
942         return false;
943     }
944     keyi->actsDefined |= (1 << ndx);
945
946     toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
947     act = value->value.child;
948     for (i = 0; i < nActs; i++, toAct++) {
949         if (!HandleActionDef(act, keymap, toAct, info->action)) {
950             ERROR("Illegal action definition for %s\n",
951                   longText(keyi->name));
952             ACTION("Action for group %u/level %zu ignored\n", ndx + 1, i + 1);
953         }
954         act = (ExprDef *) act->common.next;
955     }
956     return true;
957 }
958
959 static const LookupEntry lockingEntries[] = {
960     { "true", XkbKB_Lock },
961     { "yes", XkbKB_Lock },
962     { "on", XkbKB_Lock },
963     { "false", XkbKB_Default },
964     { "no", XkbKB_Default },
965     { "off", XkbKB_Default },
966     { "permanent", XkbKB_Lock | XkbKB_Permanent },
967     { NULL, 0 }
968 };
969
970 static const LookupEntry repeatEntries[] = {
971     { "true", RepeatYes },
972     { "yes", RepeatYes },
973     { "on", RepeatYes },
974     { "false", RepeatNo },
975     { "no", RepeatNo },
976     { "off", RepeatNo },
977     { "default", RepeatUndefined },
978     { NULL, 0 }
979 };
980
981 static bool
982 SetSymbolsField(KeyInfo *keyi, struct xkb_keymap *keymap, char *field,
983                 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
984 {
985     bool ok = true;
986     ExprResult tmp;
987
988     if (strcasecmp(field, "type") == 0) {
989         ExprResult ndx;
990         if ((!ExprResolveString(keymap->ctx, value, &tmp))
991             && (warningLevel > 0)) {
992             WARN("The type field of a key symbol map must be a string\n");
993             ACTION("Ignoring illegal type definition\n");
994         }
995         if (arrayNdx == NULL) {
996             keyi->dfltType = xkb_atom_intern(keymap->ctx, tmp.str);
997             keyi->defined |= _Key_Type_Dflt;
998         }
999         else if (!ExprResolveGroup(keymap->ctx, arrayNdx, &ndx)) {
1000             ERROR("Illegal group index for type of key %s\n",
1001                   longText(keyi->name));
1002             ACTION("Definition with non-integer array index ignored\n");
1003             free(tmp.str);
1004             return false;
1005         }
1006         else {
1007             keyi->types[ndx.uval - 1] = xkb_atom_intern(keymap->ctx, tmp.str);
1008             keyi->typesDefined |= (1 << (ndx.uval - 1));
1009         }
1010         free(tmp.str);
1011     }
1012     else if (strcasecmp(field, "symbols") == 0)
1013         return AddSymbolsToKey(keyi, keymap, arrayNdx, value, info);
1014     else if (strcasecmp(field, "actions") == 0)
1015         return AddActionsToKey(keyi, keymap, arrayNdx, value, info);
1016     else if ((strcasecmp(field, "vmods") == 0) ||
1017              (strcasecmp(field, "virtualmods") == 0) ||
1018              (strcasecmp(field, "virtualmodifiers") == 0)) {
1019         ok = ExprResolveVModMask(value, &tmp, keymap);
1020         if (ok) {
1021             keyi->vmodmap = (tmp.uval >> 8);
1022             keyi->defined |= _Key_VModMap;
1023         }
1024         else {
1025             ERROR("Expected a virtual modifier mask, found %s\n",
1026                   exprOpText(value->op));
1027             ACTION("Ignoring virtual modifiers definition for key %s\n",
1028                    longText(keyi->name));
1029         }
1030     }
1031     else if ((strcasecmp(field, "locking") == 0) ||
1032              (strcasecmp(field, "lock") == 0) ||
1033              (strcasecmp(field, "locks") == 0)) {
1034         ok = ExprResolveEnum(keymap->ctx, value, &tmp, lockingEntries);
1035         if (ok)
1036             keyi->behavior.type = tmp.uval;
1037         keyi->defined |= _Key_Behavior;
1038     }
1039     else if ((strcasecmp(field, "radiogroup") == 0) ||
1040              (strcasecmp(field, "permanentradiogroup") == 0) ||
1041              (strcasecmp(field, "allownone") == 0)) {
1042         ERROR("Radio groups not supported\n");
1043         ACTION("Ignoring radio group specification for key %s\n",
1044                longText(keyi->name));
1045         return false;
1046     }
1047     else if (uStrCasePrefix("overlay", field) ||
1048              uStrCasePrefix("permanentoverlay", field)) {
1049         ERROR("Overlays not supported\n");
1050         ACTION("Ignoring overlay specification for key %s\n",
1051                longText(keyi->name));
1052     }
1053     else if ((strcasecmp(field, "repeating") == 0) ||
1054              (strcasecmp(field, "repeats") == 0) ||
1055              (strcasecmp(field, "repeat") == 0)) {
1056         ok = ExprResolveEnum(keymap->ctx, value, &tmp, repeatEntries);
1057         if (!ok) {
1058             ERROR("Illegal repeat setting for %s\n",
1059                   longText(keyi->name));
1060             ACTION("Non-boolean repeat setting ignored\n");
1061             return false;
1062         }
1063         keyi->repeat = tmp.uval;
1064         keyi->defined |= _Key_Repeat;
1065     }
1066     else if ((strcasecmp(field, "groupswrap") == 0) ||
1067              (strcasecmp(field, "wrapgroups") == 0)) {
1068         ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1069         if (!ok) {
1070             ERROR("Illegal groupsWrap setting for %s\n",
1071                   longText(keyi->name));
1072             ACTION("Non-boolean value ignored\n");
1073             return false;
1074         }
1075         if (tmp.uval)
1076             keyi->out_of_range_group_action = XkbWrapIntoRange;
1077         else
1078             keyi->out_of_range_group_action = XkbClampIntoRange;
1079         keyi->defined |= _Key_GroupInfo;
1080     }
1081     else if ((strcasecmp(field, "groupsclamp") == 0) ||
1082              (strcasecmp(field, "clampgroups") == 0)) {
1083         ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1084         if (!ok) {
1085             ERROR("Illegal groupsClamp setting for %s\n",
1086                   longText(keyi->name));
1087             ACTION("Non-boolean value ignored\n");
1088             return false;
1089         }
1090         if (tmp.uval)
1091             keyi->out_of_range_group_action = XkbClampIntoRange;
1092         else
1093             keyi->out_of_range_group_action = XkbWrapIntoRange;
1094         keyi->defined |= _Key_GroupInfo;
1095     }
1096     else if ((strcasecmp(field, "groupsredirect") == 0) ||
1097              (strcasecmp(field, "redirectgroups") == 0)) {
1098         if (!ExprResolveGroup(keymap->ctx, value, &tmp)) {
1099             ERROR("Illegal group index for redirect of key %s\n",
1100                   longText(keyi->name));
1101             ACTION("Definition with non-integer group ignored\n");
1102             return false;
1103         }
1104         keyi->out_of_range_group_action = XkbRedirectIntoRange;
1105         keyi->out_of_range_group_number = tmp.uval - 1;
1106         keyi->defined |= _Key_GroupInfo;
1107     }
1108     else {
1109         ERROR("Unknown field %s in a symbol interpretation\n", field);
1110         ACTION("Definition ignored\n");
1111         ok = false;
1112     }
1113     return ok;
1114 }
1115
1116 static int
1117 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1118              ExprDef *value)
1119 {
1120     ExprResult tmp, name;
1121
1122     if ((arrayNdx == NULL) && (warningLevel > 0)) {
1123         WARN("You must specify an index when specifying a group name\n");
1124         ACTION("Group name definition without array subscript ignored\n");
1125         return false;
1126     }
1127     if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
1128         ERROR("Illegal index in group name definition\n");
1129         ACTION("Definition with non-integer array index ignored\n");
1130         return false;
1131     }
1132     if (!ExprResolveString(keymap->ctx, value, &name)) {
1133         ERROR("Group name must be a string\n");
1134         ACTION("Illegal name for group %d ignored\n", tmp.uval);
1135         return false;
1136     }
1137     info->groupNames[tmp.uval - 1 + info->explicit_group] =
1138         xkb_atom_intern(keymap->ctx, name.str);
1139     free(name.str);
1140
1141     return true;
1142 }
1143
1144 static int
1145 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1146 {
1147     ExprResult elem, field;
1148     ExprDef *arrayNdx;
1149     bool ret;
1150
1151     if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1152         return 0;               /* internal error, already reported */
1153     if (elem.str && (strcasecmp(elem.str, "key") == 0)) {
1154         ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1155                               stmt->value, info);
1156     }
1157     else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1158                                     (strcasecmp(field.str, "groupname") ==
1159                                      0))) {
1160         ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1161     }
1162     else if ((elem.str == NULL)
1163              && ((strcasecmp(field.str, "groupswrap") == 0) ||
1164                  (strcasecmp(field.str, "wrapgroups") == 0))) {
1165         ERROR("Global \"groupswrap\" not supported\n");
1166         ACTION("Ignored\n");
1167         ret = true;
1168     }
1169     else if ((elem.str == NULL)
1170              && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1171                  (strcasecmp(field.str, "clampgroups") == 0))) {
1172         ERROR("Global \"groupsclamp\" not supported\n");
1173         ACTION("Ignored\n");
1174         ret = true;
1175     }
1176     else if ((elem.str == NULL)
1177              && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1178                  (strcasecmp(field.str, "redirectgroups") == 0))) {
1179         ERROR("Global \"groupsredirect\" not supported\n");
1180         ACTION("Ignored\n");
1181         ret = true;
1182     }
1183     else if ((elem.str == NULL) &&
1184              (strcasecmp(field.str, "allownone") == 0)) {
1185         ERROR("Radio groups not supported\n");
1186         ACTION("Ignoring \"allownone\" specification\n");
1187         ret = true;
1188     }
1189     else {
1190         ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1191                              stmt->value, &info->action);
1192     }
1193
1194     free(elem.str);
1195     free(field.str);
1196     return ret;
1197 }
1198
1199 static bool
1200 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *keyi,
1201                   SymbolsInfo *info)
1202 {
1203     bool ok = true;
1204     ExprResult tmp, field;
1205     ExprDef *arrayNdx;
1206
1207     for (; def != NULL; def = (VarDef *) def->common.next) {
1208         if ((def->name) && (def->name->type == ExprFieldRef)) {
1209             ok = HandleSymbolsVar(def, keymap, info);
1210             continue;
1211         }
1212         else {
1213             if (def->name == NULL) {
1214                 if ((def->value == NULL)
1215                     || (def->value->op == ExprKeysymList))
1216                     field.str = strdup("symbols");
1217                 else
1218                     field.str = strdup("actions");
1219                 arrayNdx = NULL;
1220             }
1221             else {
1222                 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1223                                     &arrayNdx);
1224             }
1225             if (ok)
1226                 ok = SetSymbolsField(keyi, keymap, field.str, arrayNdx,
1227                                      def->value, info);
1228             free(field.str);
1229         }
1230     }
1231     return ok;
1232 }
1233
1234 static bool
1235 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1236 {
1237     xkb_group_index_t group = info->explicit_group;
1238
1239     if (group == 0)
1240         return true;
1241
1242     if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1243         xkb_group_index_t i;
1244         WARN("For the map %s an explicit group specified\n", info->name);
1245         WARN("but key %s has more than one group defined\n",
1246              longText(keyi->name));
1247         ACTION("All groups except first one will be ignored\n");
1248         for (i = 1; i < XkbNumKbdGroups; i++) {
1249             keyi->numLevels[i] = 0;
1250             darray_free(keyi->syms[i]);
1251             darray_free(keyi->acts[i]);
1252             keyi->types[i] = 0;
1253         }
1254     }
1255     keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1256
1257     keyi->numLevels[group] = keyi->numLevels[0];
1258     keyi->numLevels[0] = 0;
1259     keyi->syms[group] = keyi->syms[0];
1260     darray_init(keyi->syms[0]);
1261     keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1262     darray_init(keyi->symsMapIndex[0]);
1263     keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1264     darray_init(keyi->symsMapNumEntries[0]);
1265     keyi->acts[group] = keyi->acts[0];
1266     darray_init(keyi->acts[0]);
1267     keyi->types[group] = keyi->types[0];
1268     keyi->types[0] = 0;
1269     return true;
1270 }
1271
1272 static int
1273 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1274                  SymbolsInfo *info)
1275 {
1276     KeyInfo keyi;
1277
1278     InitKeyInfo(&keyi, info->file_id);
1279     CopyKeyInfo(&info->dflt, &keyi, false);
1280     keyi.merge = stmt->merge;
1281     keyi.name = KeyNameToLong(stmt->keyName);
1282     if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &keyi, info)) {
1283         info->errorCount++;
1284         return false;
1285     }
1286
1287     if (!SetExplicitGroup(info, &keyi)) {
1288         info->errorCount++;
1289         return false;
1290     }
1291
1292     if (!AddKeySymbols(info, &keyi, keymap)) {
1293         info->errorCount++;
1294         return false;
1295     }
1296     return true;
1297 }
1298
1299 static bool
1300 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1301 {
1302     ExprDef *key;
1303     ModMapEntry tmp;
1304     ExprResult rtrn;
1305     bool ok;
1306
1307     if (!LookupModIndex(keymap->ctx, NULL, def->modifier, TypeInt, &rtrn)) {
1308         ERROR("Illegal modifier map definition\n");
1309         ACTION("Ignoring map for non-modifier \"%s\"\n",
1310                xkb_atom_text(keymap->ctx, def->modifier));
1311         return false;
1312     }
1313     ok = true;
1314     tmp.modifier = rtrn.uval;
1315     for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1316         if ((key->op == ExprValue) && (key->type == TypeKeyName)) {
1317             tmp.haveSymbol = false;
1318             tmp.u.keyName = KeyNameToLong(key->value.keyName);
1319         }
1320         else if (ExprResolveKeySym(keymap->ctx, key, &rtrn)) {
1321             tmp.haveSymbol = true;
1322             tmp.u.keySym = rtrn.uval;
1323         }
1324         else {
1325             ERROR("Modmap entries may contain only key names or keysyms\n");
1326             ACTION("Illegal definition for %s modifier ignored\n",
1327                    XkbcModIndexText(tmp.modifier));
1328             continue;
1329         }
1330
1331         ok = AddModMapEntry(info, &tmp) && ok;
1332     }
1333     return ok;
1334 }
1335
1336 static void
1337 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1338                   enum merge_mode merge, SymbolsInfo *info)
1339 {
1340     ParseCommon *stmt;
1341
1342     free(info->name);
1343     info->name = uDupString(file->name);
1344     stmt = file->defs;
1345     while (stmt)
1346     {
1347         switch (stmt->stmtType) {
1348         case StmtInclude:
1349             if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info))
1350                 info->errorCount++;
1351             break;
1352         case StmtSymbolsDef:
1353             if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1354                 info->errorCount++;
1355             break;
1356         case StmtVarDef:
1357             if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1358                 info->errorCount++;
1359             break;
1360         case StmtVModDef:
1361             if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1362                 info->errorCount++;
1363             break;
1364         case StmtInterpDef:
1365             ERROR("Interpretation files may not include other types\n");
1366             ACTION("Ignoring definition of symbol interpretation\n");
1367             info->errorCount++;
1368             break;
1369         case StmtKeycodeDef:
1370             ERROR("Interpretation files may not include other types\n");
1371             ACTION("Ignoring definition of key name\n");
1372             info->errorCount++;
1373             break;
1374         case StmtModMapDef:
1375             if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1376                 info->errorCount++;
1377             break;
1378         default:
1379             WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1380                  stmt->stmtType);
1381             break;
1382         }
1383         stmt = stmt->next;
1384         if (info->errorCount > 10) {
1385 #ifdef NOISY
1386             ERROR("Too many errors\n");
1387 #endif
1388             ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1389             break;
1390         }
1391     }
1392 }
1393
1394 /**
1395  * Given a keysym @sym, return a key which generates it, or NULL.
1396  * This is used for example in a modifier map definition, such as:
1397  *      modifier_map Lock           { Caps_Lock };
1398  * where we want to add the Lock modifier to the modmap of the key
1399  * which matches the keysym Caps_Lock.
1400  * Since there can be many keys which generates the keysym, the key
1401  * is chosen first by lowest group in which the keysym appears, than
1402  * by lowest level and than by lowest key code.
1403  */
1404 static struct xkb_key *
1405 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1406 {
1407     struct xkb_key *key, *ret = NULL;
1408     xkb_group_index_t group, min_group = UINT_MAX;
1409     unsigned int level, min_level = UINT_MAX;
1410
1411     xkb_foreach_key(key, keymap) {
1412         for (group = 0; group < key->num_groups; group++) {
1413             for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1414                  level++) {
1415                 if (XkbKeyNumSyms(key, group, level) != 1 ||
1416                     (XkbKeySymEntry(key, group, level))[0] != sym)
1417                     continue;
1418
1419                 /*
1420                  * If the keysym was found in a group or level > 0, we must
1421                  * keep looking since we might find a key in which the keysym
1422                  * is in a lower group or level.
1423                  */
1424                 if (group < min_group ||
1425                     (group == min_group && level < min_level)) {
1426                     ret = key;
1427                     if (group == 0 && level == 0) {
1428                         return ret;
1429                     }
1430                     else {
1431                         min_group = group;
1432                         min_level = level;
1433                     }
1434                 }
1435             }
1436         }
1437     }
1438
1439     return ret;
1440 }
1441
1442 /**
1443  * Find the given name in the keymap->map->types and return its index.
1444  *
1445  * @param atom The atom to search for.
1446  * @param type_rtrn Set to the index of the name if found.
1447  *
1448  * @return true if found, false otherwise.
1449  */
1450 static bool
1451 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1452 {
1453     unsigned n = 0;
1454     const char *name = xkb_atom_text(keymap->ctx, atom);
1455     struct xkb_key_type *type;
1456
1457     if (keymap) {
1458         darray_foreach(type, keymap->types) {
1459             if (strcmp(type->name, name) == 0) {
1460                 *type_rtrn = n;
1461                 return true;
1462             }
1463             n++;
1464         }
1465     }
1466     return false;
1467 }
1468
1469 /**
1470  * Assign a type to the given sym and return the Atom for the type assigned.
1471  *
1472  * Simple recipe:
1473  * - ONE_LEVEL for width 0/1
1474  * - ALPHABETIC for 2 shift levels, with lower/upercase
1475  * - KEYPAD for keypad keys.
1476  * - TWO_LEVEL for other 2 shift level keys.
1477  * and the same for four level keys.
1478  *
1479  * @param width Number of sysms in syms.
1480  * @param syms The keysyms for the given key (must be size width).
1481  * @param typeNameRtrn Set to the Atom of the type name.
1482  *
1483  * @returns true if a type could be found, false otherwise.
1484  *
1485  * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1486  *        all that fun stuff rather than just assuming there's always one
1487  *        symbol per level.
1488  */
1489 static bool
1490 FindAutomaticType(struct xkb_keymap *keymap, int width,
1491                   const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1492                   bool *autoType)
1493 {
1494     *autoType = false;
1495     if ((width == 1) || (width == 0)) {
1496         *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1497         *autoType = true;
1498     }
1499     else if (width == 2) {
1500         if (syms && xkb_keysym_is_lower(syms[0]) &&
1501             xkb_keysym_is_upper(syms[1])) {
1502             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1503         }
1504         else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1505                           xkb_keysym_is_keypad(syms[1]))) {
1506             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1507             *autoType = true;
1508         }
1509         else {
1510             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1511             *autoType = true;
1512         }
1513     }
1514     else if (width <= 4) {
1515         if (syms && xkb_keysym_is_lower(syms[0]) &&
1516             xkb_keysym_is_upper(syms[1]))
1517             if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1518                 *typeNameRtrn =
1519                     xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1520             else
1521                 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1522                                                 "FOUR_LEVEL_SEMIALPHABETIC");
1523
1524         else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1525                           xkb_keysym_is_keypad(syms[1])))
1526             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1527         else
1528             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1529         /* XXX: why not set autoType here? */
1530     }
1531     return ((width >= 0) && (width <= 4));
1532 }
1533
1534 /**
1535  * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1536  * groups, and reduce to one group if all groups are identical anyway.
1537  */
1538 static void
1539 PrepareKeyDef(KeyInfo *keyi)
1540 {
1541     xkb_group_index_t i, lastGroup;
1542     int j, width, defined;
1543     bool identical;
1544
1545     defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1546     /* get highest group number */
1547     for (i = XkbNumKbdGroups - 1; i >= 0; i--) {
1548         if (defined & (1 << i))
1549             break;
1550     }
1551     lastGroup = i;
1552
1553     if (lastGroup == 0)
1554         return;
1555
1556     /* If there are empty groups between non-empty ones fill them with data */
1557     /* from the first group. */
1558     /* We can make a wrong assumption here. But leaving gaps is worse. */
1559     for (i = lastGroup; i > 0; i--) {
1560         if (defined & (1 << i))
1561             continue;
1562         width = keyi->numLevels[0];
1563         if (keyi->typesDefined & 1) {
1564             for (j = 0; j < width; j++) {
1565                 keyi->types[i] = keyi->types[0];
1566             }
1567             keyi->typesDefined |= 1 << i;
1568         }
1569         if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1570             darray_copy(keyi->acts[i], keyi->acts[0]);
1571             keyi->actsDefined |= 1 << i;
1572         }
1573         if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1574             darray_copy(keyi->syms[i], keyi->syms[0]);
1575             darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1576             darray_copy(keyi->symsMapNumEntries[i],
1577                         keyi->symsMapNumEntries[0]);
1578             keyi->symsDefined |= 1 << i;
1579         }
1580         if (defined & 1) {
1581             keyi->numLevels[i] = keyi->numLevels[0];
1582         }
1583     }
1584     /* If all groups are completely identical remove them all */
1585     /* exept the first one. */
1586     identical = true;
1587     for (i = lastGroup; i > 0; i--) {
1588         if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1589             (keyi->types[i] != keyi->types[0])) {
1590             identical = false;
1591             break;
1592         }
1593         if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1594             (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1595              darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1596              memcmp(darray_mem(keyi->syms[i], 0),
1597                     darray_mem(keyi->syms[0], 0),
1598                    sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1599             identical = false;
1600             break;
1601         }
1602         if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1603             (darray_empty(keyi->symsMapIndex[i]) ||
1604              darray_empty(keyi->symsMapIndex[0]) ||
1605              memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1606                     darray_mem(keyi->symsMapIndex[0], 0),
1607                     keyi->numLevels[0] * sizeof(int)))) {
1608             identical = false;
1609             continue;
1610         }
1611         if (!darray_same(keyi->symsMapNumEntries[i],
1612                          keyi->symsMapNumEntries[0]) &&
1613             (darray_empty(keyi->symsMapNumEntries[i]) ||
1614              darray_empty(keyi->symsMapNumEntries[0]) ||
1615              memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1616                     darray_mem(keyi->symsMapNumEntries[0], 0),
1617                     keyi->numLevels[0] * sizeof(size_t)))) {
1618             identical = false;
1619             continue;
1620         }
1621         if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1622             (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1623              memcmp(darray_mem(keyi->acts[i], 0),
1624                     darray_mem(keyi->acts[0], 0),
1625                     keyi->numLevels[0] * sizeof(union xkb_action)))) {
1626             identical = false;
1627             break;
1628         }
1629     }
1630     if (identical) {
1631         for (i = lastGroup; i > 0; i--) {
1632             keyi->numLevels[i] = 0;
1633             darray_free(keyi->syms[i]);
1634             darray_free(keyi->symsMapIndex[i]);
1635             darray_free(keyi->symsMapNumEntries[i]);
1636             darray_free(keyi->acts[i]);
1637             keyi->types[i] = 0;
1638         }
1639         keyi->symsDefined &= 1;
1640         keyi->actsDefined &= 1;
1641         keyi->typesDefined &= 1;
1642     }
1643 }
1644
1645 /**
1646  * Copy the KeyInfo into the keyboard description.
1647  *
1648  * This function recurses.
1649  */
1650 static bool
1651 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *keyi,
1652                xkb_keycode_t start_from)
1653 {
1654     xkb_keycode_t kc;
1655     struct xkb_key *key;
1656     size_t sizeSyms = 0;
1657     xkb_group_index_t i, nGroups;
1658     unsigned width, tmp;
1659     struct xkb_key_type * type;
1660     bool haveActions, autoType, useAlias;
1661     unsigned types[XkbNumKbdGroups];
1662     union xkb_action *outActs;
1663     unsigned int symIndex = 0;
1664
1665     useAlias = (start_from == 0);
1666
1667     key = FindNamedKey(keymap, keyi->name, useAlias,
1668                        CreateKeyNames(keymap), start_from);
1669     if (!key) {
1670         if (start_from == 0 && warningLevel >= 5) {
1671             WARN("Key %s not found in keycodes\n", longText(keyi->name));
1672             ACTION("Symbols ignored\n");
1673         }
1674         return false;
1675     }
1676     kc = XkbKeyGetKeycode(keymap, key);
1677
1678     haveActions = false;
1679     width = 0;
1680     for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1681         if (((i + 1) > nGroups)
1682             && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1683                 || (keyi->typesDefined) & (1 << i)))
1684             nGroups = i + 1;
1685         if (!darray_empty(keyi->acts[i]))
1686             haveActions = true;
1687         autoType = false;
1688         /* Assign the type to the key, if it is missing. */
1689         if (keyi->types[i] == XKB_ATOM_NONE) {
1690             if (keyi->dfltType != XKB_ATOM_NONE)
1691                 keyi->types[i] = keyi->dfltType;
1692             else if (FindAutomaticType(keymap, keyi->numLevels[i],
1693                                        darray_mem(keyi->syms[i], 0),
1694                                        &keyi->types[i], &autoType)) { }
1695             else {
1696                 if (warningLevel >= 5) {
1697                     WARN("No automatic type for %d symbols\n",
1698                           keyi->numLevels[i]);
1699                     ACTION("Using %s for the %s key (keycode %d)\n",
1700                             xkb_atom_text(keymap->ctx, keyi->types[i]),
1701                             longText(keyi->name), kc);
1702                 }
1703             }
1704         }
1705         if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1706             if (!autoType || keyi->numLevels[i] > 2)
1707                 key->explicit |= (1 << i);
1708         }
1709         else {
1710             if (warningLevel >= 3) {
1711                 WARN("Type \"%s\" is not defined\n",
1712                      xkb_atom_text(keymap->ctx, keyi->types[i]));
1713                 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
1714                        longText(keyi->name), kc);
1715             }
1716             types[i] = XkbTwoLevelIndex;
1717         }
1718         /* if the type specifies fewer levels than the key has, shrink the key */
1719         type = &darray_item(keymap->types, types[i]);
1720         if (type->num_levels < keyi->numLevels[i]) {
1721             if (warningLevel > 0) {
1722                 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
1723                      type->name, type->num_levels,
1724                      xkb_atom_text(keymap->ctx, keyi->name), keyi->numLevels[i]);
1725                 ACTION("Ignoring extra symbols\n");
1726             }
1727             keyi->numLevels[i] = type->num_levels;
1728         }
1729         if (keyi->numLevels[i] > width)
1730             width = keyi->numLevels[i];
1731         if (type->num_levels > width)
1732             width = type->num_levels;
1733         sizeSyms += darray_size(keyi->syms[i]);
1734     }
1735
1736     darray_resize0(key->syms, sizeSyms);
1737
1738     if (haveActions) {
1739         outActs = XkbcResizeKeyActions(keymap, key, width * nGroups);
1740         if (outActs == NULL) {
1741             WSGO("Could not enlarge actions for %s (key %d)\n",
1742                  longText(keyi->name), kc);
1743             return false;
1744         }
1745         key->explicit |= XkbExplicitInterpretMask;
1746     }
1747     else
1748         outActs = NULL;
1749
1750     key->num_groups = nGroups;
1751     if (keyi->defined & _Key_GroupInfo) {
1752         key->out_of_range_group_number = keyi->out_of_range_group_number;
1753         key->out_of_range_group_action = keyi->out_of_range_group_action;
1754     }
1755     key->width = width;
1756     key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1757     key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1758
1759     for (i = 0; i < nGroups; i++) {
1760         /* assign kt_index[i] to the index of the type in map->types.
1761          * kt_index[i] may have been set by a previous run (if we have two
1762          * layouts specified). Let's not overwrite it with the ONE_LEVEL
1763          * default group if we dont even have keys for this group anyway.
1764          *
1765          * FIXME: There should be a better fix for this.
1766          */
1767         if (keyi->numLevels[i])
1768             key->kt_index[i] = types[i];
1769         if (!darray_empty(keyi->syms[i])) {
1770             /* fill key to "width" symbols*/
1771             for (tmp = 0; tmp < width; tmp++) {
1772                 if (tmp < keyi->numLevels[i] &&
1773                     darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1774                     memcpy(darray_mem(key->syms, symIndex),
1775                            darray_mem(keyi->syms[i],
1776                                       darray_item(keyi->symsMapIndex[i], tmp)),
1777                            darray_item(keyi->symsMapNumEntries[i],
1778                                        tmp) * sizeof(xkb_keysym_t));
1779                     key->sym_index[(i * width) + tmp] = symIndex;
1780                     key->num_syms[(i * width) + tmp] =
1781                         darray_item(keyi->symsMapNumEntries[i], tmp);
1782                     symIndex += key->num_syms[(i * width) + tmp];
1783                 }
1784                 else {
1785                     key->sym_index[(i * width) + tmp] = -1;
1786                     key->num_syms[(i * width) + tmp] = 0;
1787                 }
1788                 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1789                     if (tmp < keyi->numLevels[i])
1790                         outActs[tmp] = darray_item(keyi->acts[i], tmp);
1791                     else
1792                         outActs[tmp].type = XkbSA_NoAction;
1793                 }
1794             }
1795         }
1796     }
1797     switch (keyi->behavior.type & XkbKB_OpMask) {
1798     case XkbKB_Default:
1799         break;
1800
1801     default:
1802         key->behavior = keyi->behavior;
1803         key->explicit |= XkbExplicitBehaviorMask;
1804         break;
1805     }
1806     if (keyi->defined & _Key_VModMap) {
1807         key->vmodmap = keyi->vmodmap;
1808         key->explicit |= XkbExplicitVModMapMask;
1809     }
1810     if (keyi->repeat != RepeatUndefined) {
1811         key->repeats = keyi->repeat == RepeatYes;
1812         key->explicit |= XkbExplicitAutoRepeatMask;
1813     }
1814
1815     /* do the same thing for the next key */
1816     CopySymbolsDef(keymap, keyi, kc + 1);
1817     return true;
1818 }
1819
1820 static bool
1821 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
1822 {
1823     struct xkb_key *key;
1824
1825     if (!entry->haveSymbol) {
1826         key = FindNamedKey(keymap, entry->u.keyName, true,
1827                            CreateKeyNames(keymap), 0);
1828         if (!key) {
1829             if (warningLevel >= 5) {
1830                 WARN("Key %s not found in keycodes\n",
1831                      longText(entry->u.keyName));
1832                 ACTION("Modifier map entry for %s not updated\n",
1833                        XkbcModIndexText(entry->modifier));
1834             }
1835             return false;
1836         }
1837     }
1838     else {
1839         key = FindKeyForSymbol(keymap, entry->u.keySym);
1840         if (!key) {
1841             if (warningLevel > 5) {
1842                 WARN("Key \"%s\" not found in symbol map\n",
1843                      XkbcKeysymText(entry->u.keySym));
1844                 ACTION("Modifier map entry for %s not updated\n",
1845                        XkbcModIndexText(entry->modifier));
1846             }
1847             return false;
1848         }
1849     }
1850
1851     key->modmap |= (1 << entry->modifier);
1852     return true;
1853 }
1854
1855 /**
1856  * Handle the xkb_symbols section of an xkb file.
1857  *
1858  * @param file The parsed xkb_symbols section of the xkb file.
1859  * @param keymap Handle to the keyboard description to store the symbols in.
1860  * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1861  */
1862 bool
1863 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1864                enum merge_mode merge)
1865 {
1866     xkb_group_index_t i;
1867     struct xkb_key *key;
1868     SymbolsInfo info;
1869     KeyInfo *keyi;
1870     ModMapEntry *mm;
1871
1872     InitSymbolsInfo(&info, keymap, file->id);
1873     info.dflt.merge = merge;
1874
1875     HandleSymbolsFile(file, keymap, merge, &info);
1876
1877     if (darray_empty(info.keys))
1878         goto err_info;
1879
1880     if (info.errorCount != 0)
1881         goto err_info;
1882
1883     darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1884
1885     if (info.name)
1886         keymap->symbols_section_name = strdup(info.name);
1887
1888     for (i = 0; i < XkbNumKbdGroups; i++) {
1889         if (info.groupNames[i] != XKB_ATOM_NONE) {
1890             free(keymap->group_names[i]);
1891             keymap->group_names[i] = xkb_atom_strdup(keymap->ctx,
1892                                                      info.groupNames[i]);
1893         }
1894     }
1895
1896     /* sanitize keys */
1897     darray_foreach(keyi, info.keys)
1898         PrepareKeyDef(keyi);
1899
1900     /* copy! */
1901     darray_foreach(keyi, info.keys)
1902         if (!CopySymbolsDef(keymap, keyi, 0))
1903             info.errorCount++;
1904
1905     if (warningLevel > 3) {
1906         xkb_foreach_key(key, keymap) {
1907             if (key->name[0] == '\0')
1908                 continue;
1909
1910             if (key->num_groups < 1)
1911                 WARN("No symbols defined for <%.4s> (keycode %d)\n",
1912                      key->name, XkbKeyGetKeycode(keymap, key));
1913         }
1914     }
1915
1916     list_foreach(mm, &info.modMaps, entry)
1917         if (!CopyModMapDef(keymap, mm))
1918             info.errorCount++;
1919
1920     FreeSymbolsInfo(&info);
1921     return true;
1922
1923 err_info:
1924     FreeSymbolsInfo(&info);
1925     return false;
1926 }