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