Fix some cppcheck warnings
[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 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  *         Ran Benita <ran234@gmail.com>
52  */
53
54 #include "xkbcomp-priv.h"
55 #include "text.h"
56 #include "expr.h"
57 #include "action.h"
58 #include "vmod.h"
59 #include "include.h"
60 #include "keysym.h"
61
62 enum key_repeat {
63     KEY_REPEAT_UNDEFINED = 0,
64     KEY_REPEAT_YES = 1,
65     KEY_REPEAT_NO = 2,
66 };
67
68 enum group_field {
69     GROUP_FIELD_SYMS = (1 << 0),
70     GROUP_FIELD_ACTS = (1 << 1),
71     GROUP_FIELD_TYPE = (1 << 2),
72 };
73
74 enum key_field {
75     KEY_FIELD_REPEAT    = (1 << 0),
76     KEY_FIELD_DEFAULT_TYPE = (1 << 1),
77     KEY_FIELD_GROUPINFO = (1 << 2),
78     KEY_FIELD_VMODMAP   = (1 << 3),
79 };
80
81 typedef struct {
82     enum group_field defined;
83     darray(struct xkb_level) levels;
84     xkb_atom_t type;
85 } GroupInfo;
86
87 typedef struct {
88     enum key_field defined;
89     enum merge_mode merge;
90
91     xkb_atom_t name;
92
93     darray(GroupInfo) groups;
94
95     enum key_repeat repeat;
96     xkb_mod_mask_t vmodmap;
97     xkb_atom_t default_type;
98
99     enum xkb_range_exceed_type out_of_range_group_action;
100     xkb_layout_index_t out_of_range_group_number;
101 } KeyInfo;
102
103 static void
104 ClearLevelInfo(struct xkb_level *leveli)
105 {
106     if (leveli->num_syms > 1)
107         free(leveli->u.syms);
108 }
109
110 static void
111 InitGroupInfo(GroupInfo *groupi)
112 {
113     memset(groupi, 0, sizeof(*groupi));
114 }
115
116 static void
117 ClearGroupInfo(GroupInfo *groupi)
118 {
119     struct xkb_level *leveli;
120     darray_foreach(leveli, groupi->levels)
121         ClearLevelInfo(leveli);
122     darray_free(groupi->levels);
123 }
124
125 static void
126 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
127 {
128     xkb_level_index_t j;
129     to->defined = from->defined;
130     to->type = from->type;
131     darray_init(to->levels);
132     darray_copy(to->levels, from->levels);
133     for (j = 0; j < darray_size(to->levels); j++)
134         if (darray_item(from->levels, j).num_syms > 1)
135             darray_item(to->levels, j).u.syms =
136                 memdup(darray_item(from->levels, j).u.syms,
137                        darray_item(from->levels, j).num_syms,
138                        sizeof(xkb_keysym_t));
139 }
140
141 static void
142 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
143 {
144     memset(keyi, 0, sizeof(*keyi));
145     keyi->merge = MERGE_OVERRIDE;
146     keyi->name = xkb_atom_intern_literal(ctx, "*");
147     keyi->out_of_range_group_action = RANGE_WRAP;
148 }
149
150 static void
151 ClearKeyInfo(KeyInfo *keyi)
152 {
153     GroupInfo *groupi;
154     darray_foreach(groupi, keyi->groups)
155         ClearGroupInfo(groupi);
156     darray_free(keyi->groups);
157 }
158
159 /***====================================================================***/
160
161 typedef struct {
162     enum merge_mode merge;
163     bool haveSymbol;
164     xkb_mod_index_t modifier;
165     union {
166         xkb_atom_t keyName;
167         xkb_keysym_t keySym;
168     } u;
169 } ModMapEntry;
170
171 typedef struct {
172     char *name;         /* e.g. pc+us+inet(evdev) */
173     int errorCount;
174     enum merge_mode merge;
175     xkb_layout_index_t explicit_group;
176     darray(KeyInfo) keys;
177     KeyInfo default_key;
178     ActionsInfo *actions;
179     darray(xkb_atom_t) group_names;
180     darray(ModMapEntry) modmaps;
181
182     struct xkb_keymap *keymap;
183 } SymbolsInfo;
184
185 static void
186 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
187                 ActionsInfo *actions)
188 {
189     memset(info, 0, sizeof(*info));
190     info->keymap = keymap;
191     info->merge = MERGE_OVERRIDE;
192     InitKeyInfo(keymap->ctx, &info->default_key);
193     info->actions = actions;
194     info->explicit_group = XKB_LAYOUT_INVALID;
195 }
196
197 static void
198 ClearSymbolsInfo(SymbolsInfo *info)
199 {
200     KeyInfo *keyi;
201     free(info->name);
202     darray_foreach(keyi, info->keys)
203         ClearKeyInfo(keyi);
204     darray_free(info->keys);
205     darray_free(info->group_names);
206     darray_free(info->modmaps);
207     ClearKeyInfo(&info->default_key);
208 }
209
210 static const char *
211 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
212 {
213     return KeyNameText(info->keymap->ctx, keyi->name);
214 }
215
216 static bool
217 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
218             bool report, xkb_layout_index_t group, xkb_atom_t key_name)
219 {
220     xkb_level_index_t i, levels_in_both;
221     struct xkb_context *ctx = info->keymap->ctx;
222
223     /* First find the type of the merged group. */
224     if (into->type != from->type) {
225         if (from->type == XKB_ATOM_NONE) {
226         }
227         else if (into->type == XKB_ATOM_NONE) {
228             into->type = from->type;
229         }
230         else {
231             xkb_atom_t use = (clobber ? from->type : into->type);
232             xkb_atom_t ignore = (clobber ? into->type : from->type);
233
234             if (report)
235                 log_warn(info->keymap->ctx,
236                          "Multiple definitions for group %d type of key %s; "
237                          "Using %s, ignoring %s\n",
238                          group + 1, KeyNameText(ctx, key_name),
239                          xkb_atom_text(ctx, use), xkb_atom_text(ctx, ignore));
240
241             into->type = use;
242         }
243     }
244     into->defined |= (from->defined & GROUP_FIELD_TYPE);
245
246     /* Now look at the levels. */
247
248     if (darray_empty(from->levels)) {
249         InitGroupInfo(from);
250         return true;
251     }
252
253     if (darray_empty(into->levels)) {
254         from->type = into->type;
255         *into = *from;
256         InitGroupInfo(from);
257         return true;
258     }
259
260     /* Merge the actions and syms. */
261     levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
262     for (i = 0; i < levels_in_both; i++) {
263         struct xkb_level *intoLevel = &darray_item(into->levels, i);
264         struct xkb_level *fromLevel = &darray_item(from->levels, i);
265
266         if (fromLevel->action.type == ACTION_TYPE_NONE) {
267         }
268         else if (intoLevel->action.type == ACTION_TYPE_NONE) {
269             intoLevel->action = fromLevel->action;
270         }
271         else {
272             union xkb_action *use, *ignore;
273             use = (clobber ? &fromLevel->action : &intoLevel->action);
274             ignore = (clobber ? &intoLevel->action : &fromLevel->action);
275
276             if (report)
277                 log_warn(ctx,
278                          "Multiple actions for level %d/group %u on key %s; "
279                          "Using %s, ignoring %s\n",
280                          i + 1, group + 1, KeyNameText(ctx, key_name),
281                          ActionTypeText(use->type),
282                          ActionTypeText(ignore->type));
283
284             intoLevel->action = *use;
285         }
286
287         if (fromLevel->num_syms == 0) {
288         }
289         else if (intoLevel->num_syms == 0) {
290             intoLevel->num_syms = fromLevel->num_syms;
291             if (fromLevel->num_syms > 1)
292                 intoLevel->u.syms = fromLevel->u.syms;
293             else
294                 intoLevel->u.sym = fromLevel->u.sym;
295             fromLevel->num_syms = 0;
296         }
297         else {
298             if (report)
299                 log_warn(ctx,
300                          "Multiple symbols for level %d/group %u on key %s; "
301                          "Using %s, ignoring %s\n",
302                          i + 1, group + 1, KeyNameText(ctx, key_name),
303                          (clobber ? "from" : "to"),
304                          (clobber ? "to" : "from"));
305
306             if (clobber) {
307                 ClearLevelInfo(intoLevel);
308                 intoLevel->num_syms = fromLevel->num_syms;
309                 if (fromLevel->num_syms > 1)
310                     intoLevel->u.syms = fromLevel->u.syms;
311                 else
312                     intoLevel->u.sym = fromLevel->u.sym;
313                 fromLevel->num_syms = 0;
314             }
315         }
316     }
317     /* If @from has extra levels, get them as well. */
318     for (i = levels_in_both; i < darray_size(from->levels); i++) {
319         darray_append(into->levels, darray_item(from->levels, i));
320         darray_item(from->levels, i).num_syms = 0;
321     }
322     into->defined |= (from->defined & GROUP_FIELD_ACTS);
323     into->defined |= (from->defined & GROUP_FIELD_SYMS);
324
325     return true;
326 }
327
328 static bool
329 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
330                bool clobber, bool report, enum key_field *collide)
331 {
332     if (!(old & field))
333         return (new & field);
334
335     if (new & field) {
336         if (report)
337             *collide |= field;
338
339         if (clobber)
340             return true;
341     }
342
343     return false;
344 }
345
346 static bool
347 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
348 {
349     xkb_layout_index_t i;
350     xkb_layout_index_t groups_in_both;
351     enum key_field collide = 0;
352     const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
353     const bool clobber = (from->merge != MERGE_AUGMENT);
354     const bool report = (same_file && verbosity > 0) || verbosity > 9;
355
356     if (from->merge == MERGE_REPLACE) {
357         ClearKeyInfo(into);
358         *into = *from;
359         InitKeyInfo(info->keymap->ctx, from);
360         return true;
361     }
362
363     groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
364     for (i = 0; i < groups_in_both; i++)
365         MergeGroups(info,
366                     &darray_item(into->groups, i),
367                     &darray_item(from->groups, i),
368                     clobber, report, i, into->name);
369     /* If @from has extra groups, just move them to @into. */
370     for (i = groups_in_both; i < darray_size(from->groups); i++) {
371         darray_append(into->groups, darray_item(from->groups, i));
372         InitGroupInfo(&darray_item(from->groups, i));
373     }
374
375     if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
376                        clobber, report, &collide)) {
377         into->vmodmap = from->vmodmap;
378         into->defined |= KEY_FIELD_VMODMAP;
379     }
380     if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
381                        clobber, report, &collide)) {
382         into->repeat = from->repeat;
383         into->defined |= KEY_FIELD_REPEAT;
384     }
385     if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
386                        clobber, report, &collide)) {
387         into->default_type = from->default_type;
388         into->defined |= KEY_FIELD_DEFAULT_TYPE;
389     }
390     if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
391                        clobber, report, &collide)) {
392         into->out_of_range_group_action = from->out_of_range_group_action;
393         into->out_of_range_group_number = from->out_of_range_group_number;
394         into->defined |= KEY_FIELD_GROUPINFO;
395     }
396
397     if (collide)
398         log_warn(info->keymap->ctx,
399                  "Symbol map for key %s redefined; "
400                  "Using %s definition for conflicting fields\n",
401                  KeyNameText(info->keymap->ctx, into->name),
402                  (clobber ? "first" : "last"));
403
404     ClearKeyInfo(from);
405     InitKeyInfo(info->keymap->ctx, from);
406     return true;
407 }
408
409 static bool
410 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
411 {
412     xkb_atom_t real_name;
413     KeyInfo *iter;
414
415     /*
416      * Don't keep aliases in the keys array; this guarantees that
417      * searching for keys to merge with by straight comparison (see the
418      * following loop) is enough, and we won't get multiple KeyInfo's
419      * for the same key because of aliases.
420      */
421     real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
422     if (real_name != XKB_ATOM_NONE)
423         keyi->name = real_name;
424
425     darray_foreach(iter, info->keys)
426         if (iter->name == keyi->name)
427             return MergeKeys(info, iter, keyi, same_file);
428
429     darray_append(info->keys, *keyi);
430     InitKeyInfo(info->keymap->ctx, keyi);
431     return true;
432 }
433
434 static bool
435 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
436 {
437     ModMapEntry *old;
438     bool clobber = (new->merge != MERGE_AUGMENT);
439
440     darray_foreach(old, info->modmaps) {
441         xkb_mod_index_t use, ignore;
442
443         if ((new->haveSymbol != old->haveSymbol) ||
444             (new->haveSymbol && new->u.keySym != old->u.keySym) ||
445             (!new->haveSymbol && new->u.keyName != old->u.keyName))
446             continue;
447
448         if (new->modifier == old->modifier)
449             return true;
450
451         use = (clobber ? new->modifier : old->modifier);
452         ignore = (clobber ? old->modifier : new->modifier);
453
454         if (new->haveSymbol)
455             log_err(info->keymap->ctx,
456                     "Symbol \"%s\" added to modifier map for multiple modifiers; "
457                     "Using %s, ignoring %s\n",
458                     KeysymText(info->keymap->ctx, new->u.keySym),
459                     ModIndexText(info->keymap, use),
460                     ModIndexText(info->keymap, ignore));
461         else
462             log_err(info->keymap->ctx,
463                     "Key \"%s\" added to modifier map for multiple modifiers; "
464                     "Using %s, ignoring %s\n",
465                     KeyNameText(info->keymap->ctx, new->u.keyName),
466                     ModIndexText(info->keymap, use),
467                     ModIndexText(info->keymap, ignore));
468
469         old->modifier = use;
470         return true;
471     }
472
473     darray_append(info->modmaps, *new);
474     return true;
475 }
476
477 /***====================================================================***/
478
479 static void
480 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
481                      enum merge_mode merge)
482 {
483     unsigned int i;
484     KeyInfo *keyi;
485     ModMapEntry *mm;
486     xkb_atom_t *group_name;
487     xkb_layout_index_t group_names_in_both;
488
489     if (from->errorCount > 0) {
490         into->errorCount += from->errorCount;
491         return;
492     }
493
494     if (into->name == NULL) {
495         into->name = from->name;
496         from->name = NULL;
497     }
498
499     group_names_in_both = MIN(darray_size(into->group_names),
500                               darray_size(from->group_names));
501     for (i = 0; i < group_names_in_both; i++) {
502         if (!darray_item(from->group_names, i))
503             continue;
504
505         if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
506             continue;
507
508         darray_item(into->group_names, i) = darray_item(from->group_names, i);
509     }
510     /* If @from has more, get them as well. */
511     darray_foreach_from(group_name, from->group_names, group_names_in_both)
512         darray_append(into->group_names, *group_name);
513
514     darray_foreach(keyi, from->keys) {
515         keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
516         if (!AddKeySymbols(into, keyi, false))
517             into->errorCount++;
518     }
519
520     darray_foreach(mm, from->modmaps) {
521         mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
522         if (!AddModMapEntry(into, mm))
523             into->errorCount++;
524     }
525 }
526
527 static void
528 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
529
530 static bool
531 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
532 {
533     SymbolsInfo included;
534
535     InitSymbolsInfo(&included, info->keymap, info->actions);
536     included.name = include->stmt;
537     include->stmt = NULL;
538
539     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
540         SymbolsInfo next_incl;
541         XkbFile *file;
542
543         file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS);
544         if (!file) {
545             info->errorCount += 10;
546             ClearSymbolsInfo(&included);
547             return false;
548         }
549
550         InitSymbolsInfo(&next_incl, info->keymap, info->actions);
551         if (stmt->modifier) {
552             next_incl.explicit_group = atoi(stmt->modifier) - 1;
553             if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
554                 log_err(info->keymap->ctx,
555                         "Cannot set explicit group to %d - must be between 1..%d; "
556                         "Ignoring group number\n",
557                         next_incl.explicit_group + 1, XKB_MAX_GROUPS);
558                 next_incl.explicit_group = info->explicit_group;
559             }
560         }
561         else {
562             next_incl.explicit_group = info->explicit_group;
563         }
564
565         HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
566
567         MergeIncludedSymbols(&included, &next_incl, stmt->merge);
568
569         ClearSymbolsInfo(&next_incl);
570         FreeXkbFile(file);
571     }
572
573     MergeIncludedSymbols(info, &included, include->merge);
574     ClearSymbolsInfo(&included);
575
576     return (info->errorCount == 0);
577 }
578
579 #define SYMBOLS 1
580 #define ACTIONS 2
581
582 static bool
583 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
584               unsigned what, xkb_layout_index_t *ndx_rtrn)
585 {
586     const char *name = (what == SYMBOLS ? "symbols" : "actions");
587
588     if (arrayNdx == NULL) {
589         xkb_layout_index_t i;
590         GroupInfo *groupi;
591         enum group_field field = (what == SYMBOLS ?
592                                   GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
593
594         darray_enumerate(i, groupi, keyi->groups) {
595             if (!(groupi->defined & field)) {
596                 *ndx_rtrn = i;
597                 return true;
598             }
599         }
600
601         if (i >= XKB_MAX_GROUPS) {
602             log_err(info->keymap->ctx,
603                     "Too many groups of %s for key %s (max %u); "
604                     "Ignoring %s defined for extra groups\n",
605                     name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
606             return false;
607         }
608
609         darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
610         *ndx_rtrn = darray_size(keyi->groups) - 1;
611         return true;
612     }
613
614     if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
615         log_err(info->keymap->ctx,
616                 "Illegal group index for %s of key %s\n"
617                 "Definition with non-integer array index ignored\n",
618                 name, KeyInfoText(info, keyi));
619         return false;
620     }
621
622     (*ndx_rtrn)--;
623     if (*ndx_rtrn >= darray_size(keyi->groups))
624         darray_resize0(keyi->groups, *ndx_rtrn + 1);
625
626     return true;
627 }
628
629 static bool
630 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
631                 ExprDef *value)
632 {
633     xkb_layout_index_t ndx;
634     GroupInfo *groupi;
635     xkb_level_index_t nLevels;
636     xkb_level_index_t i;
637     int j;
638
639     if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
640         return false;
641
642     groupi = &darray_item(keyi->groups, ndx);
643
644     if (value == NULL) {
645         groupi->defined |= GROUP_FIELD_SYMS;
646         return true;
647     }
648
649     if (value->expr.op != EXPR_KEYSYM_LIST) {
650         log_err(info->keymap->ctx,
651                 "Expected a list of symbols, found %s; "
652                 "Ignoring symbols for group %u of %s\n",
653                 expr_op_type_to_string(value->expr.op), ndx + 1,
654                 KeyInfoText(info, keyi));
655         return false;
656     }
657
658     if (groupi->defined & GROUP_FIELD_SYMS) {
659         log_err(info->keymap->ctx,
660                 "Symbols for key %s, group %u already defined; "
661                 "Ignoring duplicate definition\n",
662                 KeyInfoText(info, keyi), ndx + 1);
663         return false;
664     }
665
666     nLevels = darray_size(value->keysym_list.symsMapIndex);
667     if (darray_size(groupi->levels) < nLevels)
668         darray_resize0(groupi->levels, nLevels);
669
670     groupi->defined |= GROUP_FIELD_SYMS;
671
672     for (i = 0; i < nLevels; i++) {
673         unsigned int sym_index;
674         struct xkb_level *leveli = &darray_item(groupi->levels, i);
675
676         sym_index = darray_item(value->keysym_list.symsMapIndex, i);
677         leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
678         if (leveli->num_syms > 1)
679             leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
680
681         for (j = 0; j < leveli->num_syms; j++) {
682             xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
683                                               sym_index + j);
684
685             if (leveli->num_syms == 1) {
686                 if (keysym == XKB_KEY_NoSymbol)
687                     leveli->num_syms = 0;
688                 else
689                     leveli->u.sym = keysym;
690             }
691             else if (leveli->num_syms > 1) {
692                 leveli->u.syms[j] = keysym;
693             }
694         }
695     }
696
697     return true;
698 }
699
700 static bool
701 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
702                 ExprDef *value)
703 {
704     unsigned int i;
705     xkb_layout_index_t ndx;
706     GroupInfo *groupi;
707     unsigned int nActs;
708     ExprDef *act;
709
710     if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
711         return false;
712
713     groupi = &darray_item(keyi->groups, ndx);
714
715     if (value == NULL) {
716         groupi->defined |= GROUP_FIELD_ACTS;
717         return true;
718     }
719
720     if (value->expr.op != EXPR_ACTION_LIST) {
721         log_wsgo(info->keymap->ctx,
722                  "Bad expression type (%d) for action list value; "
723                  "Ignoring actions for group %u of %s\n",
724                  value->expr.op, ndx, KeyInfoText(info, keyi));
725         return false;
726     }
727
728     if (groupi->defined & GROUP_FIELD_ACTS) {
729         log_wsgo(info->keymap->ctx,
730                  "Actions for key %s, group %u already defined\n",
731                  KeyInfoText(info, keyi), ndx);
732         return false;
733     }
734
735     nActs = 0;
736     for (act = value->unary.child; act; act = (ExprDef *) act->common.next)
737         nActs++;
738
739     if (darray_size(groupi->levels) < nActs)
740         darray_resize0(groupi->levels, nActs);
741
742     groupi->defined |= GROUP_FIELD_ACTS;
743
744     act = value->unary.child;
745     for (i = 0; i < nActs; i++) {
746         union xkb_action *toAct = &darray_item(groupi->levels, i).action;
747
748         if (!HandleActionDef(act, info->keymap, toAct, info->actions))
749             log_err(info->keymap->ctx,
750                     "Illegal action definition for %s; "
751                     "Action for group %u/level %u ignored\n",
752                     KeyInfoText(info, keyi), ndx + 1, i + 1);
753
754         act = (ExprDef *) act->common.next;
755     }
756
757     return true;
758 }
759
760 static const LookupEntry repeatEntries[] = {
761     { "true", KEY_REPEAT_YES },
762     { "yes", KEY_REPEAT_YES },
763     { "on", KEY_REPEAT_YES },
764     { "false", KEY_REPEAT_NO },
765     { "no", KEY_REPEAT_NO },
766     { "off", KEY_REPEAT_NO },
767     { "default", KEY_REPEAT_UNDEFINED },
768     { NULL, 0 }
769 };
770
771 static bool
772 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
773                 ExprDef *arrayNdx, ExprDef *value)
774 {
775     bool ok = true;
776     struct xkb_context *ctx = info->keymap->ctx;
777
778     if (istreq(field, "type")) {
779         xkb_layout_index_t ndx;
780         xkb_atom_t val;
781
782         if (!ExprResolveString(ctx, value, &val))
783             log_vrb(ctx, 1,
784                     "The type field of a key symbol map must be a string; "
785                     "Ignoring illegal type definition\n");
786
787         if (arrayNdx == NULL) {
788             keyi->default_type = val;
789             keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
790         }
791         else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
792             log_err(ctx,
793                     "Illegal group index for type of key %s; "
794                     "Definition with non-integer array index ignored\n",
795                     KeyInfoText(info, keyi));
796             return false;
797         }
798         else {
799             ndx--;
800             if (ndx >= darray_size(keyi->groups))
801                 darray_resize0(keyi->groups, ndx + 1);
802             darray_item(keyi->groups, ndx).type = val;
803             darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
804         }
805     }
806     else if (istreq(field, "symbols"))
807         return AddSymbolsToKey(info, keyi, arrayNdx, value);
808     else if (istreq(field, "actions"))
809         return AddActionsToKey(info, keyi, arrayNdx, value);
810     else if (istreq(field, "vmods") ||
811              istreq(field, "virtualmods") ||
812              istreq(field, "virtualmodifiers")) {
813         xkb_mod_mask_t mask;
814
815         ok = ExprResolveModMask(info->keymap, value, MOD_VIRT, &mask);
816         if (ok) {
817             keyi->vmodmap = mask;
818             keyi->defined |= KEY_FIELD_VMODMAP;
819         }
820         else {
821             log_err(info->keymap->ctx,
822                     "Expected a virtual modifier mask, found %s; "
823                     "Ignoring virtual modifiers definition for key %s\n",
824                     expr_op_type_to_string(value->expr.op),
825                     KeyInfoText(info, keyi));
826         }
827     }
828     else if (istreq(field, "locking") ||
829              istreq(field, "lock") ||
830              istreq(field, "locks")) {
831         log_err(info->keymap->ctx,
832                 "Key behaviors not supported; "
833                 "Ignoring locking specification for key %s\n",
834                 KeyInfoText(info, keyi));
835     }
836     else if (istreq(field, "radiogroup") ||
837              istreq(field, "permanentradiogroup") ||
838              istreq(field, "allownone")) {
839         log_err(info->keymap->ctx,
840                 "Radio groups not supported; "
841                 "Ignoring radio group specification for key %s\n",
842                 KeyInfoText(info, keyi));
843     }
844     else if (istreq_prefix("overlay", field) ||
845              istreq_prefix("permanentoverlay", field)) {
846         log_err(info->keymap->ctx,
847                 "Overlays not supported; "
848                 "Ignoring overlay specification for key %s\n",
849                 KeyInfoText(info, keyi));
850     }
851     else if (istreq(field, "repeating") ||
852              istreq(field, "repeats") ||
853              istreq(field, "repeat")) {
854         unsigned int val;
855
856         ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
857         if (!ok) {
858             log_err(info->keymap->ctx,
859                     "Illegal repeat setting for %s; "
860                     "Non-boolean repeat setting ignored\n",
861                     KeyInfoText(info, keyi));
862             return false;
863         }
864         keyi->repeat = val;
865         keyi->defined |= KEY_FIELD_REPEAT;
866     }
867     else if (istreq(field, "groupswrap") ||
868              istreq(field, "wrapgroups")) {
869         bool set;
870
871         if (!ExprResolveBoolean(ctx, value, &set)) {
872             log_err(info->keymap->ctx,
873                     "Illegal groupsWrap setting for %s; "
874                     "Non-boolean value ignored\n",
875                     KeyInfoText(info, keyi));
876             return false;
877         }
878
879         if (set)
880             keyi->out_of_range_group_action = RANGE_WRAP;
881         else
882             keyi->out_of_range_group_action = RANGE_SATURATE;
883
884         keyi->defined |= KEY_FIELD_GROUPINFO;
885     }
886     else if (istreq(field, "groupsclamp") ||
887              istreq(field, "clampgroups")) {
888         bool set;
889
890         if (!ExprResolveBoolean(ctx, value, &set)) {
891             log_err(info->keymap->ctx,
892                     "Illegal groupsClamp setting for %s; "
893                     "Non-boolean value ignored\n",
894                     KeyInfoText(info, keyi));
895             return false;
896         }
897
898         if (set)
899             keyi->out_of_range_group_action = RANGE_SATURATE;
900         else
901             keyi->out_of_range_group_action = RANGE_WRAP;
902
903         keyi->defined |= KEY_FIELD_GROUPINFO;
904     }
905     else if (istreq(field, "groupsredirect") ||
906              istreq(field, "redirectgroups")) {
907         xkb_layout_index_t grp;
908
909         if (!ExprResolveGroup(ctx, value, &grp)) {
910             log_err(info->keymap->ctx,
911                     "Illegal group index for redirect of key %s; "
912                     "Definition with non-integer group ignored\n",
913                     KeyInfoText(info, keyi));
914             return false;
915         }
916
917         keyi->out_of_range_group_action = RANGE_REDIRECT;
918         keyi->out_of_range_group_number = grp - 1;
919         keyi->defined |= KEY_FIELD_GROUPINFO;
920     }
921     else {
922         log_err(info->keymap->ctx,
923                 "Unknown field %s in a symbol interpretation; "
924                 "Definition ignored\n",
925                 field);
926         ok = false;
927     }
928
929     return ok;
930 }
931
932 static int
933 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
934 {
935     xkb_layout_index_t group, group_to_use;
936     xkb_atom_t name;
937
938     if (!arrayNdx) {
939         log_vrb(info->keymap->ctx, 1,
940                 "You must specify an index when specifying a group name; "
941                 "Group name definition without array subscript ignored\n");
942         return false;
943     }
944
945     if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &group)) {
946         log_err(info->keymap->ctx,
947                 "Illegal index in group name definition; "
948                 "Definition with non-integer array index ignored\n");
949         return false;
950     }
951
952     if (!ExprResolveString(info->keymap->ctx, value, &name)) {
953         log_err(info->keymap->ctx,
954                 "Group name must be a string; "
955                 "Illegal name for group %d ignored\n", group);
956         return false;
957     }
958
959     if (info->explicit_group == XKB_LAYOUT_INVALID) {
960         group_to_use = group - 1;
961     }
962     else if (group - 1 == 0) {
963         group_to_use = info->explicit_group;
964     }
965     else {
966         log_warn(info->keymap->ctx,
967                  "An explicit group was specified for the '%s' map, "
968                  "but it provides a name for a group other than Group1 (%d); "
969                  "Ignoring group name '%s'\n",
970                  info->name, group,
971                  xkb_atom_text(info->keymap->ctx, name));
972         return false;
973     }
974
975     if (group_to_use >= darray_size(info->group_names))
976         darray_resize0(info->group_names, group_to_use + 1);
977     darray_item(info->group_names, group_to_use) = name;
978
979     return true;
980 }
981
982 static int
983 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
984 {
985     const char *elem, *field;
986     ExprDef *arrayNdx;
987     bool ret;
988
989     if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
990                        &arrayNdx) == 0)
991         return 0;               /* internal error, already reported */
992     if (elem && istreq(elem, "key")) {
993         ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
994                               stmt->value);
995     }
996     else if (!elem && (istreq(field, "name") ||
997                        istreq(field, "groupname"))) {
998         ret = SetGroupName(info, arrayNdx, stmt->value);
999     }
1000     else if (!elem && (istreq(field, "groupswrap") ||
1001                        istreq(field, "wrapgroups"))) {
1002         log_err(info->keymap->ctx,
1003                 "Global \"groupswrap\" not supported; Ignored\n");
1004         ret = true;
1005     }
1006     else if (!elem && (istreq(field, "groupsclamp") ||
1007                        istreq(field, "clampgroups"))) {
1008         log_err(info->keymap->ctx,
1009                 "Global \"groupsclamp\" not supported; Ignored\n");
1010         ret = true;
1011     }
1012     else if (!elem && (istreq(field, "groupsredirect") ||
1013                        istreq(field, "redirectgroups"))) {
1014         log_err(info->keymap->ctx,
1015                 "Global \"groupsredirect\" not supported; Ignored\n");
1016         ret = true;
1017     }
1018     else if (!elem && istreq(field, "allownone")) {
1019         log_err(info->keymap->ctx,
1020                 "Radio groups not supported; "
1021                 "Ignoring \"allownone\" specification\n");
1022         ret = true;
1023     }
1024     else {
1025         ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1026                              info->actions);
1027     }
1028
1029     return ret;
1030 }
1031
1032 static bool
1033 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1034 {
1035     bool ok = true;
1036     const char *elem, *field;
1037     ExprDef *arrayNdx;
1038
1039     for (; def; def = (VarDef *) def->common.next) {
1040         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1041             log_err(info->keymap->ctx,
1042                     "Cannot set a global default value from within a key statement; "
1043                     "Move statements to the global file scope\n");
1044             continue;
1045         }
1046
1047         if (!def->name) {
1048             if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1049                 field = "symbols";
1050             else
1051                 field = "actions";
1052             arrayNdx = NULL;
1053         }
1054         else {
1055             ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1056                                 &arrayNdx);
1057         }
1058
1059         if (ok)
1060             ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1061     }
1062
1063     return ok;
1064 }
1065
1066 static bool
1067 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1068 {
1069     xkb_layout_index_t i;
1070     GroupInfo *groupi;
1071     bool warn = false;
1072
1073     if (info->explicit_group == XKB_LAYOUT_INVALID)
1074         return true;
1075
1076     darray_enumerate_from(i, groupi, keyi->groups, 1) {
1077         if (groupi->defined) {
1078             warn = true;
1079             ClearGroupInfo(groupi);
1080             InitGroupInfo(groupi);
1081         }
1082     }
1083
1084     if (warn)
1085         log_warn(info->keymap->ctx,
1086                  "For the map %s an explicit group specified, "
1087                  "but key %s has more than one group defined; "
1088                  "All groups except first one will be ignored\n",
1089                  info->name, KeyInfoText(info, keyi));
1090
1091     darray_resize0(keyi->groups, info->explicit_group + 1);
1092     if (info->explicit_group > 0) {
1093         darray_item(keyi->groups, info->explicit_group) =
1094             darray_item(keyi->groups, 0);
1095         InitGroupInfo(&darray_item(keyi->groups, 0));
1096     }
1097
1098     return true;
1099 }
1100
1101 static int
1102 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1103 {
1104     KeyInfo keyi;
1105     xkb_layout_index_t i;
1106
1107     keyi = info->default_key;
1108     darray_init(keyi.groups);
1109     darray_copy(keyi.groups, info->default_key.groups);
1110     for (i = 0; i < darray_size(keyi.groups); i++)
1111         CopyGroupInfo(&darray_item(keyi.groups, i),
1112                       &darray_item(info->default_key.groups, i));
1113     keyi.merge = stmt->merge;
1114     keyi.name = stmt->keyName;
1115
1116     if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1117         info->errorCount++;
1118         return false;
1119     }
1120
1121     if (!SetExplicitGroup(info, &keyi)) {
1122         info->errorCount++;
1123         return false;
1124     }
1125
1126     if (!AddKeySymbols(info, &keyi, true)) {
1127         info->errorCount++;
1128         return false;
1129     }
1130
1131     return true;
1132 }
1133
1134 static bool
1135 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1136 {
1137     ExprDef *key;
1138     ModMapEntry tmp;
1139     xkb_mod_index_t ndx;
1140     bool ok;
1141     struct xkb_context *ctx = info->keymap->ctx;
1142
1143     ndx = ModNameToIndex(info->keymap, def->modifier, MOD_REAL);
1144     if (ndx == XKB_MOD_INVALID) {
1145         log_err(info->keymap->ctx,
1146                 "Illegal modifier map definition; "
1147                 "Ignoring map for non-modifier \"%s\"\n",
1148                 xkb_atom_text(ctx, def->modifier));
1149         return false;
1150     }
1151
1152     ok = true;
1153     tmp.modifier = ndx;
1154     tmp.merge = def->merge;
1155
1156     for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1157         xkb_keysym_t sym;
1158
1159         if (key->expr.op == EXPR_VALUE &&
1160             key->expr.value_type == EXPR_TYPE_KEYNAME) {
1161             tmp.haveSymbol = false;
1162             tmp.u.keyName = key->key_name.key_name;
1163         }
1164         else if (ExprResolveKeySym(ctx, key, &sym)) {
1165             tmp.haveSymbol = true;
1166             tmp.u.keySym = sym;
1167         }
1168         else {
1169             log_err(info->keymap->ctx,
1170                     "Modmap entries may contain only key names or keysyms; "
1171                     "Illegal definition for %s modifier ignored\n",
1172                     ModIndexText(info->keymap, tmp.modifier));
1173             continue;
1174         }
1175
1176         ok = AddModMapEntry(info, &tmp) && ok;
1177     }
1178     return ok;
1179 }
1180
1181 static void
1182 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1183 {
1184     bool ok;
1185
1186     free(info->name);
1187     info->name = strdup_safe(file->name);
1188
1189     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1190         switch (stmt->type) {
1191         case STMT_INCLUDE:
1192             ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1193             break;
1194         case STMT_SYMBOLS:
1195             ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1196             break;
1197         case STMT_VAR:
1198             ok = HandleGlobalVar(info, (VarDef *) stmt);
1199             break;
1200         case STMT_VMOD:
1201             ok = HandleVModDef(info->keymap, (VModDef *) stmt);
1202             break;
1203         case STMT_MODMAP:
1204             ok = HandleModMapDef(info, (ModMapDef *) stmt);
1205             break;
1206         default:
1207             log_err(info->keymap->ctx,
1208                     "Symbols files may not include other types; "
1209                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
1210             ok = false;
1211             break;
1212         }
1213
1214         if (!ok)
1215             info->errorCount++;
1216
1217         if (info->errorCount > 10) {
1218             log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1219                     file->topName);
1220             break;
1221         }
1222     }
1223 }
1224
1225 /**
1226  * Given a keysym @sym, return a key which generates it, or NULL.
1227  * This is used for example in a modifier map definition, such as:
1228  *      modifier_map Lock           { Caps_Lock };
1229  * where we want to add the Lock modifier to the modmap of the key
1230  * which matches the keysym Caps_Lock.
1231  * Since there can be many keys which generates the keysym, the key
1232  * is chosen first by lowest group in which the keysym appears, than
1233  * by lowest level and than by lowest key code.
1234  */
1235 static struct xkb_key *
1236 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1237 {
1238     struct xkb_key *key, *ret = NULL;
1239     xkb_layout_index_t group, min_group = UINT32_MAX;
1240     xkb_level_index_t level, min_level = UINT16_MAX;
1241
1242     xkb_foreach_key(key, keymap) {
1243         for (group = 0; group < key->num_groups; group++) {
1244             for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1245                 if (key->groups[group].levels[level].num_syms != 1 ||
1246                     key->groups[group].levels[level].u.sym != sym)
1247                     continue;
1248
1249                 /*
1250                  * If the keysym was found in a group or level > 0, we must
1251                  * keep looking since we might find a key in which the keysym
1252                  * is in a lower group or level.
1253                  */
1254                 if (group < min_group ||
1255                     (group == min_group && level < min_level)) {
1256                     ret = key;
1257                     if (group == 0 && level == 0) {
1258                         return ret;
1259                     }
1260                     else {
1261                         min_group = group;
1262                         min_level = level;
1263                     }
1264                 }
1265             }
1266         }
1267     }
1268
1269     return ret;
1270 }
1271
1272 /*
1273  * Find an appropriate type for a group and return its name.
1274  *
1275  * Simple recipe:
1276  * - ONE_LEVEL for width 0/1
1277  * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1278  * - KEYPAD for keypad keys.
1279  * - TWO_LEVEL for other 2 shift level keys.
1280  * and the same for four level keys.
1281  *
1282  * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1283  */
1284 static xkb_atom_t
1285 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1286 {
1287     xkb_keysym_t sym0, sym1, sym2, sym3;
1288     xkb_level_index_t width = darray_size(groupi->levels);
1289
1290 #define GET_SYM(level) \
1291     (darray_item(groupi->levels, level).num_syms == 0 ? \
1292         XKB_KEY_NoSymbol : \
1293      darray_item(groupi->levels, level).num_syms == 1 ? \
1294         darray_item(groupi->levels, level).u.sym : \
1295      /* num_syms > 1 */ \
1296         darray_item(groupi->levels, level).u.syms[0])
1297
1298     if (width == 1 || width <= 0)
1299         return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1300
1301     sym0 = GET_SYM(0);
1302     sym1 = GET_SYM(1);
1303
1304     if (width == 2) {
1305         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1306             return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1307
1308         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1309             return xkb_atom_intern_literal(ctx, "KEYPAD");
1310
1311         return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1312     }
1313
1314     if (width <= 4) {
1315         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1316             sym2 = GET_SYM(2);
1317             sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1318
1319             if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1320                 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1321
1322             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1323         }
1324
1325         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1326             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1327
1328         return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1329     }
1330
1331     return XKB_ATOM_NONE;
1332
1333 #undef GET_SYM
1334 }
1335
1336 static const struct xkb_key_type *
1337 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1338                  xkb_layout_index_t group, bool *explicit_type)
1339 {
1340     unsigned int i;
1341     GroupInfo *groupi = &darray_item(keyi->groups, group);
1342     xkb_atom_t type_name = groupi->type;
1343
1344     *explicit_type = true;
1345
1346     if (type_name == XKB_ATOM_NONE) {
1347         if (keyi->default_type != XKB_ATOM_NONE) {
1348             type_name  = keyi->default_type;
1349         }
1350         else {
1351             type_name = FindAutomaticType(keymap->ctx, groupi);
1352             if (type_name != XKB_ATOM_NONE)
1353                 *explicit_type = false;
1354         }
1355     }
1356
1357     if (type_name == XKB_ATOM_NONE) {
1358         log_warn(keymap->ctx,
1359                  "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1360                  "Using the default type\n",
1361                  KeyNameText(keymap->ctx, keyi->name), group + 1,
1362                  (unsigned long) darray_size(groupi->levels));
1363         goto use_default;
1364     }
1365
1366     for (i = 0; i < keymap->num_types; i++)
1367         if (keymap->types[i].name == type_name)
1368             break;
1369
1370     if (i >= keymap->num_types) {
1371         log_warn(keymap->ctx,
1372                  "The type \"%s\" for key '%s' group %d was not previously defined; "
1373                  "Using the default type\n",
1374                  xkb_atom_text(keymap->ctx, type_name),
1375                  KeyNameText(keymap->ctx, keyi->name), group + 1);
1376         goto use_default;
1377     }
1378
1379     return &keymap->types[i];
1380
1381 use_default:
1382     /*
1383      * Index 0 is guaranteed to contain something, usually
1384      * ONE_LEVEL or at least some default one-level type.
1385      */
1386     return &keymap->types[0];
1387 }
1388
1389 static bool
1390 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1391 {
1392     struct xkb_keymap *keymap = info->keymap;
1393     struct xkb_key *key;
1394     GroupInfo *groupi;
1395     const GroupInfo *group0;
1396     xkb_layout_index_t i;
1397
1398     /*
1399      * The name is guaranteed to be real and not an alias (see
1400      * AddKeySymbols), so 'false' is safe here.
1401      */
1402     key = XkbKeyByName(keymap, keyi->name, false);
1403     if (!key) {
1404         log_vrb(info->keymap->ctx, 5,
1405                 "Key %s not found in keycodes; Symbols ignored\n",
1406                 KeyInfoText(info, keyi));
1407         return false;
1408     }
1409
1410     /* Find the range of groups we need. */
1411     key->num_groups = 0;
1412     darray_enumerate(i, groupi, keyi->groups)
1413         if (groupi->defined)
1414             key->num_groups = i + 1;
1415
1416     if (key->num_groups <= 0)
1417         return false; /* WSGO */
1418
1419     darray_resize(keyi->groups, key->num_groups);
1420
1421     /*
1422      * If there are empty groups between non-empty ones, fill them with data
1423      * from the first group.
1424      * We can make a wrong assumption here. But leaving gaps is worse.
1425      */
1426     group0 = &darray_item(keyi->groups, 0);
1427     darray_foreach_from(groupi, keyi->groups, 1) {
1428         if (groupi->defined)
1429             continue;
1430
1431         CopyGroupInfo(groupi, group0);
1432     }
1433
1434     key->groups = calloc(key->num_groups, sizeof(*key->groups));
1435
1436     /* Find and assign the groups' types in the keymap. */
1437     darray_enumerate(i, groupi, keyi->groups) {
1438         const struct xkb_key_type *type;
1439         bool explicit_type;
1440
1441         type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1442
1443         /* Always have as many levels as the type specifies. */
1444         if (type->num_levels < darray_size(groupi->levels)) {
1445             struct xkb_level *leveli;
1446
1447             log_vrb(info->keymap->ctx, 1,
1448                     "Type \"%s\" has %d levels, but %s has %d levels; "
1449                     "Ignoring extra symbols\n",
1450                     xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1451                     KeyInfoText(info, keyi),
1452                     (int) darray_size(groupi->levels));
1453
1454             darray_foreach_from(leveli, groupi->levels, type->num_levels)
1455                 ClearLevelInfo(leveli);
1456         }
1457         darray_resize0(groupi->levels, type->num_levels);
1458
1459         key->groups[i].explicit_type = explicit_type;
1460         key->groups[i].type = type;
1461     }
1462
1463     /* Copy levels. */
1464     darray_enumerate(i, groupi, keyi->groups) {
1465         key->groups[i].levels = darray_mem(groupi->levels, 0);
1466         darray_init(groupi->levels);
1467     }
1468
1469     key->out_of_range_group_number = keyi->out_of_range_group_number;
1470     key->out_of_range_group_action = keyi->out_of_range_group_action;
1471
1472     if (keyi->defined & KEY_FIELD_VMODMAP) {
1473         key->vmodmap = keyi->vmodmap;
1474         key->explicit |= EXPLICIT_VMODMAP;
1475     }
1476
1477     if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1478         key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1479         key->explicit |= EXPLICIT_REPEAT;
1480     }
1481
1482     darray_foreach(groupi, keyi->groups) {
1483         if (groupi->defined & GROUP_FIELD_ACTS) {
1484             key->explicit |= EXPLICIT_INTERP;
1485             break;
1486         }
1487     }
1488
1489     return true;
1490 }
1491
1492 static bool
1493 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1494 {
1495     struct xkb_key *key;
1496     struct xkb_keymap *keymap = info->keymap;
1497
1498     if (!entry->haveSymbol) {
1499         key = XkbKeyByName(keymap, entry->u.keyName, true);
1500         if (!key) {
1501             log_vrb(info->keymap->ctx, 5,
1502                     "Key %s not found in keycodes; "
1503                     "Modifier map entry for %s not updated\n",
1504                     KeyNameText(keymap->ctx, entry->u.keyName),
1505                     ModIndexText(info->keymap, entry->modifier));
1506             return false;
1507         }
1508     }
1509     else {
1510         key = FindKeyForSymbol(keymap, entry->u.keySym);
1511         if (!key) {
1512             log_vrb(info->keymap->ctx, 5,
1513                     "Key \"%s\" not found in symbol map; "
1514                     "Modifier map entry for %s not updated\n",
1515                     KeysymText(info->keymap->ctx, entry->u.keySym),
1516                     ModIndexText(info->keymap, entry->modifier));
1517             return false;
1518         }
1519     }
1520
1521     key->modmap |= (1 << entry->modifier);
1522     return true;
1523 }
1524
1525 static bool
1526 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1527 {
1528     KeyInfo *keyi;
1529     ModMapEntry *mm;
1530
1531     keymap->symbols_section_name = strdup_safe(info->name);
1532     XkbEscapeMapName(keymap->symbols_section_name);
1533
1534     keymap->num_group_names = darray_size(info->group_names);
1535     keymap->group_names = darray_mem(info->group_names, 0);
1536     darray_init(info->group_names);
1537
1538     darray_foreach(keyi, info->keys)
1539         if (!CopySymbolsDef(info, keyi))
1540             info->errorCount++;
1541
1542     if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1543         struct xkb_key *key;
1544
1545         xkb_foreach_key(key, keymap) {
1546             if (key->name == XKB_ATOM_NONE)
1547                 continue;
1548
1549             if (key->num_groups < 1)
1550                 log_info(keymap->ctx,
1551                          "No symbols defined for %s\n",
1552                          KeyNameText(keymap->ctx, key->name));
1553         }
1554     }
1555
1556     darray_foreach(mm, info->modmaps)
1557         if (!CopyModMapDef(info, mm))
1558             info->errorCount++;
1559
1560     /* XXX: If we don't ignore errorCount, things break. */
1561     return true;
1562 }
1563
1564 bool
1565 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1566                enum merge_mode merge)
1567 {
1568     SymbolsInfo info;
1569     ActionsInfo *actions;
1570
1571     actions = NewActionsInfo();
1572     if (!actions)
1573         return false;
1574
1575     InitSymbolsInfo(&info, keymap, actions);
1576     info.default_key.merge = merge;
1577
1578     HandleSymbolsFile(&info, file, merge);
1579
1580     if (info.errorCount != 0)
1581         goto err_info;
1582
1583     if (!CopySymbolsToKeymap(keymap, &info))
1584         goto err_info;
1585
1586     ClearSymbolsInfo(&info);
1587     FreeActionsInfo(actions);
1588     return true;
1589
1590 err_info:
1591     FreeActionsInfo(actions);
1592     ClearSymbolsInfo(&info);
1593     return false;
1594 }