Remove haveSelf include feature
[platform/upstream/libxkbcommon.git] / src / xkbcomp / keycodes.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 #include "xkbcomp-priv.h"
28 #include "expr.h"
29 #include "parseutils.h"
30
31 typedef struct _AliasInfo {
32     enum merge_mode merge;
33     unsigned file_id;
34     struct list entry;
35
36     char alias[XkbKeyNameLength + 1];
37     char real[XkbKeyNameLength + 1];
38 } AliasInfo;
39
40 typedef struct _IndicatorNameInfo {
41     enum merge_mode merge;
42     unsigned file_id;
43     struct list entry;
44
45     int ndx;
46     xkb_atom_t name;
47     bool virtual;
48 } IndicatorNameInfo;
49
50 typedef struct _KeyNamesInfo {
51     char *name;     /* e.g. evdev+aliases(qwerty) */
52     int errorCount;
53     unsigned file_id;
54     enum merge_mode merge;
55     xkb_keycode_t computedMin; /* lowest keycode stored */
56     xkb_keycode_t computedMax; /* highest keycode stored */
57     xkb_keycode_t explicitMin;
58     xkb_keycode_t explicitMax;
59     darray(unsigned long) names;
60     darray(unsigned int) files;
61     struct list leds;
62     struct list aliases;
63 } KeyNamesInfo;
64
65 static void
66 HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
67                    enum merge_mode merge,
68                    KeyNamesInfo *info);
69
70 static void
71 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
72 {
73     if (newMax < darray_size(info->names))
74         return;
75
76     darray_resize0(info->names, newMax + 1);
77     darray_resize0(info->files, newMax + 1);
78 }
79
80 static void
81 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
82               char *alias, char *real)
83 {
84     memset(info, 0, sizeof(*info));
85     info->merge = merge;
86     info->file_id = file_id;
87     strncpy(info->alias, alias, XkbKeyNameLength);
88     strncpy(info->real, real, XkbKeyNameLength);
89 }
90
91 static void
92 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
93 {
94     ii->merge = info->merge;
95     ii->file_id = info->file_id;
96     ii->ndx = 0;
97     ii->name = XKB_ATOM_NONE;
98     ii->virtual = false;
99 }
100
101 static IndicatorNameInfo *
102 NextIndicatorName(KeyNamesInfo * info)
103 {
104     IndicatorNameInfo *ii;
105
106     ii = malloc(sizeof(*ii));
107     if (!ii)
108         return NULL;
109
110     InitIndicatorNameInfo(ii, info);
111     list_append(&ii->entry, &info->leds);
112
113     return ii;
114 }
115
116 static IndicatorNameInfo *
117 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
118 {
119     IndicatorNameInfo *old;
120
121     list_foreach(old, &info->leds, entry)
122         if (old->ndx == ndx)
123             return old;
124
125     return NULL;
126 }
127
128 static IndicatorNameInfo *
129 FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
130 {
131     IndicatorNameInfo *old;
132
133     list_foreach(old, &info->leds, entry)
134         if (old->name == name)
135             return old;
136
137     return NULL;
138 }
139
140 static bool
141 AddIndicatorName(KeyNamesInfo *info, struct xkb_keymap *keymap,
142                  enum merge_mode merge,
143                  IndicatorNameInfo *new)
144 {
145     IndicatorNameInfo *old;
146     bool replace;
147
148     replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
149
150     old = FindIndicatorByName(info, new->name);
151     if (old) {
152         if ((old->file_id == new->file_id && warningLevel > 0) ||
153             warningLevel > 9) {
154             WARN("Multiple indicators named %s\n",
155                  xkb_atom_text(keymap->ctx, new->name));
156             if (old->ndx == new->ndx) {
157                 if (old->virtual != new->virtual) {
158                     if (replace)
159                         old->virtual = new->virtual;
160                     ACTION("Using %s instead of %s\n",
161                            (old->virtual ? "virtual" : "real"),
162                            (old->virtual ? "real" : "virtual"));
163                 }
164                 else {
165                     ACTION("Identical definitions ignored\n");
166                 }
167                 return true;
168             }
169             else {
170                 if (replace)
171                     ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
172                 else
173                     ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
174             }
175
176             if (replace) {
177                 list_del(&old->entry);
178                 free(old);
179             }
180         }
181     }
182
183     old = FindIndicatorByIndex(info, new->ndx);
184     if (old) {
185         if ((old->file_id == new->file_id && warningLevel > 0) ||
186             warningLevel > 9) {
187             WARN("Multiple names for indicator %d\n", new->ndx);
188             if ((old->name == new->name) && (old->virtual == new->virtual))
189                 ACTION("Identical definitions ignored\n");
190             else {
191                 const char *oldType, *newType;
192                 xkb_atom_t using, ignoring;
193                 if (old->virtual)
194                     oldType = "virtual indicator";
195                 else
196                     oldType = "real indicator";
197                 if (new->virtual)
198                     newType = "virtual indicator";
199                 else
200                     newType = "real indicator";
201                 if (replace) {
202                     using = new->name;
203                     ignoring = old->name;
204                 }
205                 else {
206                     using = old->name;
207                     ignoring = new->name;
208                 }
209                 ACTION("Using %s %s, ignoring %s %s\n",
210                        oldType, xkb_atom_text(keymap->ctx, using),
211                        newType, xkb_atom_text(keymap->ctx, ignoring));
212             }
213         }
214         if (replace) {
215             old->name = new->name;
216             old->virtual = new->virtual;
217         }
218         return true;
219     }
220     old = new;
221     new = NextIndicatorName(info);
222     if (!new) {
223         WSGO("Couldn't allocate name for indicator %d\n", old->ndx);
224         ACTION("Ignored\n");
225         return false;
226     }
227     new->name = old->name;
228     new->ndx = old->ndx;
229     new->virtual = old->virtual;
230     return true;
231 }
232
233 static void
234 ClearKeyNamesInfo(KeyNamesInfo * info)
235 {
236     AliasInfo *alias, *next_alias;
237     IndicatorNameInfo *ii, *next_ii;
238
239     free(info->name);
240     info->name = NULL;
241     info->computedMax = info->explicitMax = info->explicitMin = 0;
242     info->computedMin = XKB_KEYCODE_MAX;
243     darray_free(info->names);
244     darray_free(info->files);
245     list_foreach_safe(ii, next_ii, &info->leds, entry)
246         free(ii);
247     list_foreach_safe(alias, next_alias, &info->aliases, entry)
248         free(alias);
249     list_init(&info->aliases);
250 }
251
252 static void
253 InitKeyNamesInfo(KeyNamesInfo * info, unsigned file_id)
254 {
255     info->name = NULL;
256     list_init(&info->leds);
257     list_init(&info->aliases);
258     info->file_id = file_id;
259     darray_init(info->names);
260     darray_init(info->files);
261     ClearKeyNamesInfo(info);
262     info->errorCount = 0;
263 }
264
265 static int
266 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
267 {
268     uint64_t i;
269
270     for (i = info->computedMin; i <= info->computedMax; i++)
271         if (darray_item(info->names, i) == name)
272             return i;
273
274     return 0;
275 }
276
277 /**
278  * Store the name of the key as a long in the info struct under the given
279  * keycode. If the same keys is referred to twice, print a warning.
280  * Note that the key's name is stored as a long, the keycode is the index.
281  */
282 static bool
283 AddKeyName(KeyNamesInfo * info,
284            xkb_keycode_t kc, char *name, enum merge_mode merge,
285            unsigned file_id, bool reportCollisions)
286 {
287     xkb_keycode_t old;
288     unsigned long lval;
289
290     ResizeKeyNameArrays(info, kc);
291
292     if (kc < info->computedMin)
293         info->computedMin = kc;
294     if (kc > info->computedMax)
295         info->computedMax = kc;
296     lval = KeyNameToLong(name);
297
298     if (reportCollisions) {
299         reportCollisions = (warningLevel > 7 ||
300                             (warningLevel > 0 &&
301                              file_id == darray_item(info->files, kc)));
302     }
303
304     if (darray_item(info->names, kc) != 0) {
305         char buf[6];
306
307         LongToKeyName(darray_item(info->names, kc), buf);
308         buf[4] = '\0';
309         if (darray_item(info->names, kc) == lval && reportCollisions) {
310             WARN("Multiple identical key name definitions\n");
311             ACTION("Later occurences of \"<%s> = %d\" ignored\n",
312                    buf, kc);
313             return true;
314         }
315         if (merge == MERGE_AUGMENT) {
316             if (reportCollisions) {
317                 WARN("Multiple names for keycode %d\n", kc);
318                 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
319             }
320             return true;
321         }
322         else {
323             if (reportCollisions) {
324                 WARN("Multiple names for keycode %d\n", kc);
325                 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
326             }
327             darray_item(info->names, kc) = 0;
328             darray_item(info->files, kc) = 0;
329         }
330     }
331     old = FindKeyByLong(info, lval);
332     if ((old != 0) && (old != kc)) {
333         if (merge == MERGE_OVERRIDE) {
334             darray_item(info->names, old) = 0;
335             darray_item(info->files, old) = 0;
336             if (reportCollisions) {
337                 WARN("Key name <%s> assigned to multiple keys\n", name);
338                 ACTION("Using %d, ignoring %d\n", kc, old);
339             }
340         }
341         else {
342             if ((reportCollisions) && (warningLevel > 3)) {
343                 WARN("Key name <%s> assigned to multiple keys\n", name);
344                 ACTION("Using %d, ignoring %d\n", old, kc);
345             }
346             return true;
347         }
348     }
349     darray_item(info->names, kc) = lval;
350     darray_item(info->files, kc) = file_id;
351     return true;
352 }
353
354 /***====================================================================***/
355
356 static int
357 HandleAliasDef(KeyAliasDef *def, enum merge_mode merge, unsigned file_id,
358                KeyNamesInfo *info);
359
360 static bool
361 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
362 {
363     AliasInfo *alias, *next;
364     KeyAliasDef def;
365
366     if (list_empty(&from->aliases))
367         return true;
368
369     if (list_empty(&into->aliases)) {
370         list_replace(&from->aliases, &into->aliases);
371         list_init(&from->aliases);
372         return true;
373     }
374
375     memset(&def, 0, sizeof(def));
376
377     list_foreach_safe(alias, next, &from->aliases, entry) {
378         def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
379         memcpy(def.alias, alias->alias, XkbKeyNameLength);
380         memcpy(def.real, alias->real, XkbKeyNameLength);
381
382         if (!HandleAliasDef(&def, def.merge, alias->file_id, into))
383             return false;
384     }
385
386     return true;
387 }
388
389 static void
390 MergeIncludedKeycodes(KeyNamesInfo *into, struct xkb_keymap *keymap,
391                       KeyNamesInfo *from, enum merge_mode merge)
392 {
393     uint64_t i;
394     char buf[5];
395     IndicatorNameInfo *led;
396
397     if (from->errorCount > 0) {
398         into->errorCount += from->errorCount;
399         return;
400     }
401     if (into->name == NULL) {
402         into->name = from->name;
403         from->name = NULL;
404     }
405
406     ResizeKeyNameArrays(into, from->computedMax);
407
408     for (i = from->computedMin; i <= from->computedMax; i++) {
409         if (darray_item(from->names, i) == 0)
410             continue;
411         LongToKeyName(darray_item(from->names, i), buf);
412         buf[4] = '\0';
413         if (!AddKeyName(into, i, buf, merge, from->file_id, false))
414             into->errorCount++;
415     }
416
417     list_foreach(led, &from->leds, entry) {
418         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
419         if (!AddIndicatorName(into, keymap, led->merge, led))
420             into->errorCount++;
421     }
422
423     if (!MergeAliases(into, from, merge))
424         into->errorCount++;
425     if (from->explicitMin != 0) {
426         if ((into->explicitMin == 0)
427             || (into->explicitMin > from->explicitMin))
428             into->explicitMin = from->explicitMin;
429     }
430     if (from->explicitMax > 0) {
431         if ((into->explicitMax == 0)
432             || (into->explicitMax < from->explicitMax))
433             into->explicitMax = from->explicitMax;
434     }
435 }
436
437 /**
438  * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
439  *
440  * @param stmt The include statement from the keymap file.
441  * @param keymap Unused for all but the keymap->flags.
442  * @param info Struct to store the key info in.
443  */
444 static bool
445 HandleIncludeKeycodes(IncludeStmt *stmt, struct xkb_keymap *keymap,
446                       KeyNamesInfo *info)
447 {
448     enum merge_mode newMerge;
449     XkbFile *rtrn;
450     KeyNamesInfo included;
451
452     /* XXX: What's that? */
453     if (stmt->file && strcmp(stmt->file, "computed") == 0) {
454         keymap->flags |= AutoKeyNames;
455         info->explicitMin = 0;
456         info->explicitMax = XKB_KEYCODE_MAX;
457         return (info->errorCount == 0);
458     }
459
460     if (!ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_KEYCODES, &rtrn,
461                             &newMerge)) {
462         info->errorCount += 10;
463         return false;
464     }
465
466     InitKeyNamesInfo(&included, rtrn->id);
467     HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &included);
468     if (stmt->stmt) {
469         free(included.name);
470         included.name = stmt->stmt;
471         stmt->stmt = NULL;
472     }
473     FreeXKBFile(rtrn);
474
475     if (stmt->next && included.errorCount < 1) {
476         IncludeStmt *next;
477         unsigned op;
478         KeyNamesInfo next_incl;
479
480         for (next = stmt->next; next != NULL; next = next->next) {
481             if (!ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_KEYCODES,
482                                     &rtrn, &op)) {
483                 info->errorCount += 10;
484                 ClearKeyNamesInfo(&included);
485                 return false;
486             }
487
488             InitKeyNamesInfo(&next_incl, rtrn->id);
489             HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
490             MergeIncludedKeycodes(&included, keymap, &next_incl, op);
491             ClearKeyNamesInfo(&next_incl);
492             FreeXKBFile(rtrn);
493         }
494     }
495
496     MergeIncludedKeycodes(info, keymap, &included, newMerge);
497     ClearKeyNamesInfo(&included);
498
499     return (info->errorCount == 0);
500 }
501
502 /**
503  * Parse the given statement and store the output in the info struct.
504  * e.g. <ESC> = 9
505  */
506 static int
507 HandleKeycodeDef(KeycodeDef *stmt, enum merge_mode merge, KeyNamesInfo *info)
508 {
509     if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
510         (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
511         ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
512         ACTION("Must be in the range %d-%d inclusive\n",
513                info->explicitMin,
514                info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
515         return 0;
516     }
517     if (stmt->merge != MERGE_DEFAULT) {
518         if (stmt->merge == MERGE_REPLACE)
519             merge = MERGE_OVERRIDE;
520         else
521             merge = stmt->merge;
522     }
523     return AddKeyName(info, stmt->value, stmt->name, merge, info->file_id,
524                       true);
525 }
526
527 static void
528 HandleAliasCollision(AliasInfo *old, AliasInfo *new)
529 {
530     if (strncmp(new->real, old->real, XkbKeyNameLength) == 0) {
531         if ((new->file_id == old->file_id && warningLevel > 0) ||
532             warningLevel > 9) {
533             WARN("Alias of %s for %s declared more than once\n",
534                   XkbcKeyNameText(new->alias), XkbcKeyNameText(new->real));
535             ACTION("First definition ignored\n");
536         }
537     }
538     else {
539         char *use, *ignore;
540
541         if (new->merge == MERGE_AUGMENT) {
542             use = old->real;
543             ignore = new->real;
544         }
545         else {
546             use = new->real;
547             ignore = old->real;
548         }
549
550         if ((old->file_id == new->file_id && warningLevel > 0) ||
551             warningLevel > 9) {
552             WARN("Multiple definitions for alias %s\n",
553                  XkbcKeyNameText(old->alias));
554             ACTION("Using %s, ignoring %s\n",
555                    XkbcKeyNameText(use), XkbcKeyNameText(ignore));
556         }
557
558         if (use != old->real)
559             memcpy(old->real, use, XkbKeyNameLength);
560     }
561
562     old->file_id = new->file_id;
563     old->merge = new->merge;
564 }
565
566 static int
567 HandleAliasDef(KeyAliasDef *def, enum merge_mode merge, unsigned file_id,
568                KeyNamesInfo *info)
569 {
570     AliasInfo *alias;
571
572     list_foreach(alias, &info->aliases, entry) {
573         if (strncmp(alias->alias, def->alias, XkbKeyNameLength) == 0) {
574             AliasInfo new;
575             InitAliasInfo(&new, merge, file_id, def->alias, def->real);
576             HandleAliasCollision(alias, &new);
577             return true;
578         }
579     }
580
581     alias = calloc(1, sizeof(*alias));
582     if (!alias) {
583         WSGO("Allocation failure in HandleAliasDef\n");
584         return false;
585     }
586
587     alias->file_id = file_id;
588     alias->merge = merge;
589     memcpy(alias->alias, def->alias, XkbKeyNameLength);
590     memcpy(alias->real, def->real, XkbKeyNameLength);
591     list_append(&alias->entry, &info->aliases);
592
593     return true;
594 }
595
596 #define MIN_KEYCODE_DEF 0
597 #define MAX_KEYCODE_DEF 1
598
599 /**
600  * Handle the minimum/maximum statement of the xkb file.
601  * Sets explicitMin/Max of the info struct.
602  *
603  * @return 1 on success, 0 otherwise.
604  */
605 static int
606 HandleKeyNameVar(VarDef *stmt, struct xkb_keymap *keymap, KeyNamesInfo *info)
607 {
608     ExprResult tmp, field;
609     ExprDef *arrayNdx;
610     int which;
611
612     if (ExprResolveLhs(keymap, stmt->name, &tmp, &field, &arrayNdx) == 0)
613         return 0;               /* internal error, already reported */
614
615     if (tmp.str != NULL) {
616         ERROR("Unknown element %s encountered\n", tmp.str);
617         ACTION("Default for field %s ignored\n", field.str);
618         goto err_out;
619     }
620     if (strcasecmp(field.str, "minimum") == 0)
621         which = MIN_KEYCODE_DEF;
622     else if (strcasecmp(field.str, "maximum") == 0)
623         which = MAX_KEYCODE_DEF;
624     else {
625         ERROR("Unknown field encountered\n");
626         ACTION("Assigment to field %s ignored\n", field.str);
627         goto err_out;
628     }
629     if (arrayNdx != NULL) {
630         ERROR("The %s setting is not an array\n", field.str);
631         ACTION("Illegal array reference ignored\n");
632         goto err_out;
633     }
634
635     if (ExprResolveKeyCode(keymap->ctx, stmt->value, &tmp) == 0) {
636         ACTION("Assignment to field %s ignored\n", field.str);
637         goto err_out;
638     }
639     if (tmp.uval > XKB_KEYCODE_MAX) {
640         ERROR
641             ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
642             tmp.uval, 0, XKB_KEYCODE_MAX);
643         ACTION("Value of \"%s\" not changed\n", field.str);
644         goto err_out;
645     }
646     if (which == MIN_KEYCODE_DEF) {
647         if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval)) {
648             ERROR
649                 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
650                 tmp.uval, info->explicitMax);
651             ACTION("Minimum key code value not changed\n");
652             goto err_out;
653         }
654         if ((info->computedMax > 0) && (info->computedMin < tmp.uval)) {
655             ERROR
656                 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
657                 tmp.uval, info->computedMin);
658             ACTION("Minimum key code value not changed\n");
659             goto err_out;
660         }
661         info->explicitMin = tmp.uval;
662     }
663     if (which == MAX_KEYCODE_DEF) {
664         if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval)) {
665             ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
666                   tmp.uval, info->explicitMin);
667             ACTION("Maximum code value not changed\n");
668             goto err_out;
669         }
670         if ((info->computedMax > 0) && (info->computedMax > tmp.uval)) {
671             ERROR
672                 ("Maximum code (%d) must be >= highest defined key (%d)\n",
673                 tmp.uval, info->computedMax);
674             ACTION("Maximum code value not changed\n");
675             goto err_out;
676         }
677         info->explicitMax = tmp.uval;
678     }
679
680     free(field.str);
681     return 1;
682
683 err_out:
684     free(field.str);
685     return 0;
686 }
687
688 static int
689 HandleIndicatorNameDef(IndicatorNameDef *def, struct xkb_keymap *keymap,
690                        enum merge_mode merge, KeyNamesInfo *info)
691 {
692     IndicatorNameInfo ii;
693     ExprResult tmp;
694
695     if ((def->ndx < 1) || (def->ndx > XkbNumIndicators)) {
696         info->errorCount++;
697         ERROR("Name specified for illegal indicator index %d\n", def->ndx);
698         ACTION("Ignored\n");
699         return false;
700     }
701     InitIndicatorNameInfo(&ii, info);
702     ii.ndx = def->ndx;
703     if (!ExprResolveString(keymap->ctx, def->name, &tmp)) {
704         char buf[20];
705         snprintf(buf, sizeof(buf), "%d", def->ndx);
706         info->errorCount++;
707         return ReportBadType("indicator", "name", buf, "string");
708     }
709     ii.name = xkb_atom_intern(keymap->ctx, tmp.str);
710     free(tmp.str);
711     ii.virtual = def->virtual;
712     if (!AddIndicatorName(info, keymap, merge, &ii))
713         return false;
714     return true;
715 }
716
717 /**
718  * Handle the xkb_keycodes section of a xkb file.
719  * All information about parsed keys is stored in the info struct.
720  *
721  * Such a section may have include statements, in which case this function is
722  * semi-recursive (it calls HandleIncludeKeycodes, which may call
723  * HandleKeycodesFile again).
724  *
725  * @param file The input file (parsed xkb_keycodes section)
726  * @param xkb Necessary to pass down, may have flags changed.
727  * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
728  * @param info Struct to contain the fully parsed key information.
729  */
730 static void
731 HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
732                    enum merge_mode merge, KeyNamesInfo *info)
733 {
734     ParseCommon *stmt;
735
736     free(info->name);
737     info->name = uDupString(file->name);
738     stmt = file->defs;
739     while (stmt)
740     {
741         switch (stmt->stmtType) {
742         case StmtInclude:    /* e.g. include "evdev+aliases(qwerty)" */
743             if (!HandleIncludeKeycodes((IncludeStmt *) stmt, keymap, info))
744                 info->errorCount++;
745             break;
746         case StmtKeycodeDef: /* e.g. <ESC> = 9; */
747             if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
748                 info->errorCount++;
749             break;
750         case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
751             if (!HandleAliasDef((KeyAliasDef *) stmt, merge, info->file_id,
752                                 info))
753                 info->errorCount++;
754             break;
755         case StmtVarDef: /* e.g. minimum, maximum */
756             if (!HandleKeyNameVar((VarDef *) stmt, keymap, info))
757                 info->errorCount++;
758             break;
759         case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
760             if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt, keymap,
761                                         merge, info))
762                 info->errorCount++;
763             break;
764         case StmtInterpDef:
765         case StmtVModDef:
766             ERROR("Keycode files may define key and indicator names only\n");
767             ACTION("Ignoring definition of %s\n",
768                    ((stmt->stmtType ==
769                      StmtInterpDef) ? "a symbol interpretation" :
770                     "virtual modifiers"));
771             info->errorCount++;
772             break;
773         default:
774             WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
775                  stmt->stmtType);
776             break;
777         }
778         stmt = stmt->next;
779         if (info->errorCount > 10) {
780 #ifdef NOISY
781             ERROR("Too many errors\n");
782 #endif
783             ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
784             break;
785         }
786     }
787 }
788
789 static int
790 ApplyAliases(struct xkb_keymap *keymap, KeyNamesInfo *info)
791 {
792     int i;
793     struct xkb_key *key;
794     struct xkb_key_alias *old, *a;
795     AliasInfo *alias, *next;
796     int nNew = 0, nOld;
797
798     nOld = darray_size(keymap->key_aliases);
799     old = &darray_item(keymap->key_aliases, 0);
800
801     list_foreach(alias, &info->aliases, entry) {
802         unsigned long lname;
803
804         lname = KeyNameToLong(alias->real);
805         key = FindNamedKey(keymap, lname, false, CreateKeyNames(keymap), 0);
806         if (!key) {
807             if (warningLevel > 4) {
808                 WARN("Attempt to alias %s to non-existent key %s\n",
809                      XkbcKeyNameText(alias->alias),
810                      XkbcKeyNameText(alias->real));
811                 ACTION("Ignored\n");
812             }
813             alias->alias[0] = '\0';
814             continue;
815         }
816
817         lname = KeyNameToLong(alias->alias);
818         key = FindNamedKey(keymap, lname, false, false, 0);
819         if (key) {
820             if (warningLevel > 4) {
821                 WARN("Attempt to create alias with the name of a real key\n");
822                 ACTION("Alias \"%s = %s\" ignored\n",
823                        XkbcKeyNameText(alias->alias),
824                        XkbcKeyNameText(alias->real));
825             }
826             alias->alias[0] = '\0';
827             continue;
828         }
829
830         nNew++;
831
832         if (!old)
833             continue;
834
835         for (i = 0, a = old; i < nOld; i++, a++) {
836             AliasInfo old_alias;
837
838             if (strncmp(a->alias, alias->alias, XkbKeyNameLength) != 0)
839                 continue;
840
841             InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
842             HandleAliasCollision(&old_alias, alias);
843             memcpy(old_alias.real, a->real, XkbKeyNameLength);
844             alias->alias[0] = '\0';
845             nNew--;
846             break;
847         }
848     }
849
850     if (nNew == 0)
851         goto out;
852
853     darray_resize0(keymap->key_aliases, nOld + nNew);
854
855     a = &darray_item(keymap->key_aliases, nOld);
856     list_foreach(alias, &info->aliases, entry) {
857         if (alias->alias[0] != '\0') {
858             strncpy(a->alias, alias->alias, XkbKeyNameLength);
859             strncpy(a->real, alias->real, XkbKeyNameLength);
860             a++;
861         }
862     }
863
864 out:
865     list_foreach_safe(alias, next, &info->aliases, entry)
866         free(alias);
867     list_init(&info->aliases);
868     return true;
869 }
870
871 /**
872  * Compile the xkb_keycodes section, parse it's output, return the results.
873  *
874  * @param file The parsed XKB file (may have include statements requiring
875  * further parsing)
876  * @param result The effective keycodes, as gathered from the file.
877  * @param merge Merge strategy.
878  *
879  * @return true on success, false otherwise.
880  */
881 bool
882 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
883                 enum merge_mode merge)
884 {
885     xkb_keycode_t kc;
886     KeyNamesInfo info; /* contains all the info after parsing */
887     IndicatorNameInfo *ii;
888
889     InitKeyNamesInfo(&info, file->id);
890
891     HandleKeycodesFile(file, keymap, merge, &info);
892
893     /* all the keys are now stored in info */
894
895     if (info.errorCount != 0)
896         goto err_info;
897
898     if (info.explicitMin > 0) /* if "minimum" statement was present */
899         keymap->min_key_code = info.explicitMin;
900     else
901         keymap->min_key_code = info.computedMin;
902
903     if (info.explicitMax > 0) /* if "maximum" statement was present */
904         keymap->max_key_code = info.explicitMax;
905     else
906         keymap->max_key_code = info.computedMax;
907
908     darray_resize0(keymap->keys, keymap->max_key_code + 1);
909     for (kc = info.computedMin; kc <= info.computedMax; kc++)
910         LongToKeyName(darray_item(info.names, kc),
911                       XkbKey(keymap, kc)->name);
912
913     if (info.name)
914         keymap->keycodes_section_name = strdup(info.name);
915
916     list_foreach(ii, &info.leds, entry) {
917         free(keymap->indicator_names[ii->ndx - 1]);
918         keymap->indicator_names[ii->ndx - 1] =
919             xkb_atom_strdup(keymap->ctx, ii->name);
920     }
921
922     ApplyAliases(keymap, &info);
923
924     ClearKeyNamesInfo(&info);
925     return true;
926
927 err_info:
928     ClearKeyNamesInfo(&info);
929     return false;
930 }