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