Simplify HandleInclude functions
[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 merge = MERGE_DEFAULT;
449     XkbFile *rtrn;
450     KeyNamesInfo included, next_incl;
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     InitKeyNamesInfo(&included, info->file_id);
461     if (stmt->stmt) {
462         free(included.name);
463         included.name = stmt->stmt;
464         stmt->stmt = NULL;
465     }
466
467     for (; stmt; stmt = stmt->next) {
468         if (!ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_KEYCODES,
469                                 &rtrn, &merge)) {
470             info->errorCount += 10;
471             ClearKeyNamesInfo(&included);
472             return false;
473         }
474
475         InitKeyNamesInfo(&next_incl, rtrn->id);
476
477         HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
478
479         MergeIncludedKeycodes(&included, keymap, &next_incl, merge);
480
481         ClearKeyNamesInfo(&next_incl);
482         FreeXKBFile(rtrn);
483     }
484
485     MergeIncludedKeycodes(info, keymap, &included, merge);
486     ClearKeyNamesInfo(&included);
487
488     return (info->errorCount == 0);
489 }
490
491 /**
492  * Parse the given statement and store the output in the info struct.
493  * e.g. <ESC> = 9
494  */
495 static int
496 HandleKeycodeDef(KeycodeDef *stmt, enum merge_mode merge, KeyNamesInfo *info)
497 {
498     if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
499         (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
500         ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
501         ACTION("Must be in the range %d-%d inclusive\n",
502                info->explicitMin,
503                info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
504         return 0;
505     }
506     if (stmt->merge != MERGE_DEFAULT) {
507         if (stmt->merge == MERGE_REPLACE)
508             merge = MERGE_OVERRIDE;
509         else
510             merge = stmt->merge;
511     }
512     return AddKeyName(info, stmt->value, stmt->name, merge, info->file_id,
513                       true);
514 }
515
516 static void
517 HandleAliasCollision(AliasInfo *old, AliasInfo *new)
518 {
519     if (strncmp(new->real, old->real, XkbKeyNameLength) == 0) {
520         if ((new->file_id == old->file_id && warningLevel > 0) ||
521             warningLevel > 9) {
522             WARN("Alias of %s for %s declared more than once\n",
523                   XkbcKeyNameText(new->alias), XkbcKeyNameText(new->real));
524             ACTION("First definition ignored\n");
525         }
526     }
527     else {
528         char *use, *ignore;
529
530         if (new->merge == MERGE_AUGMENT) {
531             use = old->real;
532             ignore = new->real;
533         }
534         else {
535             use = new->real;
536             ignore = old->real;
537         }
538
539         if ((old->file_id == new->file_id && warningLevel > 0) ||
540             warningLevel > 9) {
541             WARN("Multiple definitions for alias %s\n",
542                  XkbcKeyNameText(old->alias));
543             ACTION("Using %s, ignoring %s\n",
544                    XkbcKeyNameText(use), XkbcKeyNameText(ignore));
545         }
546
547         if (use != old->real)
548             memcpy(old->real, use, XkbKeyNameLength);
549     }
550
551     old->file_id = new->file_id;
552     old->merge = new->merge;
553 }
554
555 static int
556 HandleAliasDef(KeyAliasDef *def, enum merge_mode merge, unsigned file_id,
557                KeyNamesInfo *info)
558 {
559     AliasInfo *alias;
560
561     list_foreach(alias, &info->aliases, entry) {
562         if (strncmp(alias->alias, def->alias, XkbKeyNameLength) == 0) {
563             AliasInfo new;
564             InitAliasInfo(&new, merge, file_id, def->alias, def->real);
565             HandleAliasCollision(alias, &new);
566             return true;
567         }
568     }
569
570     alias = calloc(1, sizeof(*alias));
571     if (!alias) {
572         WSGO("Allocation failure in HandleAliasDef\n");
573         return false;
574     }
575
576     alias->file_id = file_id;
577     alias->merge = merge;
578     memcpy(alias->alias, def->alias, XkbKeyNameLength);
579     memcpy(alias->real, def->real, XkbKeyNameLength);
580     list_append(&alias->entry, &info->aliases);
581
582     return true;
583 }
584
585 #define MIN_KEYCODE_DEF 0
586 #define MAX_KEYCODE_DEF 1
587
588 /**
589  * Handle the minimum/maximum statement of the xkb file.
590  * Sets explicitMin/Max of the info struct.
591  *
592  * @return 1 on success, 0 otherwise.
593  */
594 static int
595 HandleKeyNameVar(VarDef *stmt, struct xkb_keymap *keymap, KeyNamesInfo *info)
596 {
597     ExprResult tmp, field;
598     ExprDef *arrayNdx;
599     int which;
600
601     if (ExprResolveLhs(keymap, stmt->name, &tmp, &field, &arrayNdx) == 0)
602         return 0;               /* internal error, already reported */
603
604     if (tmp.str != NULL) {
605         ERROR("Unknown element %s encountered\n", tmp.str);
606         ACTION("Default for field %s ignored\n", field.str);
607         goto err_out;
608     }
609     if (strcasecmp(field.str, "minimum") == 0)
610         which = MIN_KEYCODE_DEF;
611     else if (strcasecmp(field.str, "maximum") == 0)
612         which = MAX_KEYCODE_DEF;
613     else {
614         ERROR("Unknown field encountered\n");
615         ACTION("Assigment to field %s ignored\n", field.str);
616         goto err_out;
617     }
618     if (arrayNdx != NULL) {
619         ERROR("The %s setting is not an array\n", field.str);
620         ACTION("Illegal array reference ignored\n");
621         goto err_out;
622     }
623
624     if (ExprResolveKeyCode(keymap->ctx, stmt->value, &tmp) == 0) {
625         ACTION("Assignment to field %s ignored\n", field.str);
626         goto err_out;
627     }
628     if (tmp.uval > XKB_KEYCODE_MAX) {
629         ERROR
630             ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
631             tmp.uval, 0, XKB_KEYCODE_MAX);
632         ACTION("Value of \"%s\" not changed\n", field.str);
633         goto err_out;
634     }
635     if (which == MIN_KEYCODE_DEF) {
636         if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval)) {
637             ERROR
638                 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
639                 tmp.uval, info->explicitMax);
640             ACTION("Minimum key code value not changed\n");
641             goto err_out;
642         }
643         if ((info->computedMax > 0) && (info->computedMin < tmp.uval)) {
644             ERROR
645                 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
646                 tmp.uval, info->computedMin);
647             ACTION("Minimum key code value not changed\n");
648             goto err_out;
649         }
650         info->explicitMin = tmp.uval;
651     }
652     if (which == MAX_KEYCODE_DEF) {
653         if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval)) {
654             ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
655                   tmp.uval, info->explicitMin);
656             ACTION("Maximum code value not changed\n");
657             goto err_out;
658         }
659         if ((info->computedMax > 0) && (info->computedMax > tmp.uval)) {
660             ERROR
661                 ("Maximum code (%d) must be >= highest defined key (%d)\n",
662                 tmp.uval, info->computedMax);
663             ACTION("Maximum code value not changed\n");
664             goto err_out;
665         }
666         info->explicitMax = tmp.uval;
667     }
668
669     free(field.str);
670     return 1;
671
672 err_out:
673     free(field.str);
674     return 0;
675 }
676
677 static int
678 HandleIndicatorNameDef(IndicatorNameDef *def, struct xkb_keymap *keymap,
679                        enum merge_mode merge, KeyNamesInfo *info)
680 {
681     IndicatorNameInfo ii;
682     ExprResult tmp;
683
684     if ((def->ndx < 1) || (def->ndx > XkbNumIndicators)) {
685         info->errorCount++;
686         ERROR("Name specified for illegal indicator index %d\n", def->ndx);
687         ACTION("Ignored\n");
688         return false;
689     }
690     InitIndicatorNameInfo(&ii, info);
691     ii.ndx = def->ndx;
692     if (!ExprResolveString(keymap->ctx, def->name, &tmp)) {
693         char buf[20];
694         snprintf(buf, sizeof(buf), "%d", def->ndx);
695         info->errorCount++;
696         return ReportBadType("indicator", "name", buf, "string");
697     }
698     ii.name = xkb_atom_intern(keymap->ctx, tmp.str);
699     free(tmp.str);
700     ii.virtual = def->virtual;
701     if (!AddIndicatorName(info, keymap, merge, &ii))
702         return false;
703     return true;
704 }
705
706 /**
707  * Handle the xkb_keycodes section of a xkb file.
708  * All information about parsed keys is stored in the info struct.
709  *
710  * Such a section may have include statements, in which case this function is
711  * semi-recursive (it calls HandleIncludeKeycodes, which may call
712  * HandleKeycodesFile again).
713  *
714  * @param file The input file (parsed xkb_keycodes section)
715  * @param xkb Necessary to pass down, may have flags changed.
716  * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
717  * @param info Struct to contain the fully parsed key information.
718  */
719 static void
720 HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
721                    enum merge_mode merge, KeyNamesInfo *info)
722 {
723     ParseCommon *stmt;
724
725     free(info->name);
726     info->name = uDupString(file->name);
727     stmt = file->defs;
728     while (stmt)
729     {
730         switch (stmt->stmtType) {
731         case StmtInclude:    /* e.g. include "evdev+aliases(qwerty)" */
732             if (!HandleIncludeKeycodes((IncludeStmt *) stmt, keymap, info))
733                 info->errorCount++;
734             break;
735         case StmtKeycodeDef: /* e.g. <ESC> = 9; */
736             if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
737                 info->errorCount++;
738             break;
739         case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
740             if (!HandleAliasDef((KeyAliasDef *) stmt, merge, info->file_id,
741                                 info))
742                 info->errorCount++;
743             break;
744         case StmtVarDef: /* e.g. minimum, maximum */
745             if (!HandleKeyNameVar((VarDef *) stmt, keymap, info))
746                 info->errorCount++;
747             break;
748         case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
749             if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt, keymap,
750                                         merge, info))
751                 info->errorCount++;
752             break;
753         case StmtInterpDef:
754         case StmtVModDef:
755             ERROR("Keycode files may define key and indicator names only\n");
756             ACTION("Ignoring definition of %s\n",
757                    ((stmt->stmtType ==
758                      StmtInterpDef) ? "a symbol interpretation" :
759                     "virtual modifiers"));
760             info->errorCount++;
761             break;
762         default:
763             WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
764                  stmt->stmtType);
765             break;
766         }
767         stmt = stmt->next;
768         if (info->errorCount > 10) {
769 #ifdef NOISY
770             ERROR("Too many errors\n");
771 #endif
772             ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
773             break;
774         }
775     }
776 }
777
778 static int
779 ApplyAliases(struct xkb_keymap *keymap, KeyNamesInfo *info)
780 {
781     int i;
782     struct xkb_key *key;
783     struct xkb_key_alias *old, *a;
784     AliasInfo *alias, *next;
785     int nNew = 0, nOld;
786
787     nOld = darray_size(keymap->key_aliases);
788     old = &darray_item(keymap->key_aliases, 0);
789
790     list_foreach(alias, &info->aliases, entry) {
791         unsigned long lname;
792
793         lname = KeyNameToLong(alias->real);
794         key = FindNamedKey(keymap, lname, false, CreateKeyNames(keymap), 0);
795         if (!key) {
796             if (warningLevel > 4) {
797                 WARN("Attempt to alias %s to non-existent key %s\n",
798                      XkbcKeyNameText(alias->alias),
799                      XkbcKeyNameText(alias->real));
800                 ACTION("Ignored\n");
801             }
802             alias->alias[0] = '\0';
803             continue;
804         }
805
806         lname = KeyNameToLong(alias->alias);
807         key = FindNamedKey(keymap, lname, false, false, 0);
808         if (key) {
809             if (warningLevel > 4) {
810                 WARN("Attempt to create alias with the name of a real key\n");
811                 ACTION("Alias \"%s = %s\" ignored\n",
812                        XkbcKeyNameText(alias->alias),
813                        XkbcKeyNameText(alias->real));
814             }
815             alias->alias[0] = '\0';
816             continue;
817         }
818
819         nNew++;
820
821         if (!old)
822             continue;
823
824         for (i = 0, a = old; i < nOld; i++, a++) {
825             AliasInfo old_alias;
826
827             if (strncmp(a->alias, alias->alias, XkbKeyNameLength) != 0)
828                 continue;
829
830             InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
831             HandleAliasCollision(&old_alias, alias);
832             memcpy(old_alias.real, a->real, XkbKeyNameLength);
833             alias->alias[0] = '\0';
834             nNew--;
835             break;
836         }
837     }
838
839     if (nNew == 0)
840         goto out;
841
842     darray_resize0(keymap->key_aliases, nOld + nNew);
843
844     a = &darray_item(keymap->key_aliases, nOld);
845     list_foreach(alias, &info->aliases, entry) {
846         if (alias->alias[0] != '\0') {
847             strncpy(a->alias, alias->alias, XkbKeyNameLength);
848             strncpy(a->real, alias->real, XkbKeyNameLength);
849             a++;
850         }
851     }
852
853 out:
854     list_foreach_safe(alias, next, &info->aliases, entry)
855         free(alias);
856     list_init(&info->aliases);
857     return true;
858 }
859
860 /**
861  * Compile the xkb_keycodes section, parse it's output, return the results.
862  *
863  * @param file The parsed XKB file (may have include statements requiring
864  * further parsing)
865  * @param result The effective keycodes, as gathered from the file.
866  * @param merge Merge strategy.
867  *
868  * @return true on success, false otherwise.
869  */
870 bool
871 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
872                 enum merge_mode merge)
873 {
874     xkb_keycode_t kc;
875     KeyNamesInfo info; /* contains all the info after parsing */
876     IndicatorNameInfo *ii;
877
878     InitKeyNamesInfo(&info, file->id);
879
880     HandleKeycodesFile(file, keymap, merge, &info);
881
882     /* all the keys are now stored in info */
883
884     if (info.errorCount != 0)
885         goto err_info;
886
887     if (info.explicitMin > 0) /* if "minimum" statement was present */
888         keymap->min_key_code = info.explicitMin;
889     else
890         keymap->min_key_code = info.computedMin;
891
892     if (info.explicitMax > 0) /* if "maximum" statement was present */
893         keymap->max_key_code = info.explicitMax;
894     else
895         keymap->max_key_code = info.computedMax;
896
897     darray_resize0(keymap->keys, keymap->max_key_code + 1);
898     for (kc = info.computedMin; kc <= info.computedMax; kc++)
899         LongToKeyName(darray_item(info.names, kc),
900                       XkbKey(keymap, kc)->name);
901
902     if (info.name)
903         keymap->keycodes_section_name = strdup(info.name);
904
905     list_foreach(ii, &info.leds, entry) {
906         free(keymap->indicator_names[ii->ndx - 1]);
907         keymap->indicator_names[ii->ndx - 1] =
908             xkb_atom_strdup(keymap->ctx, ii->name);
909     }
910
911     ApplyAliases(keymap, &info);
912
913     ClearKeyNamesInfo(&info);
914     return true;
915
916 err_info:
917     ClearKeyNamesInfo(&info);
918     return false;
919 }