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