summaryrefslogtreecommitdiff
path: root/app/xkbcomp/symbols.c
blob: 69cbe32841602f9df8defa630204346a36aac0b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
/************************************************************
 Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.

 Permission to use, copy, modify, and distribute this
 software and its documentation for any purpose and without
 fee is hereby granted, provided that the above copyright
 notice appear in all copies and that both that copyright
 notice and this permission notice appear in supporting
 documentation, and that the name of Silicon Graphics not be
 used in advertising or publicity pertaining to distribution
 of the software without specific prior written permission.
 Silicon Graphics makes no representation about the suitability
 of this software for any purpose. It is provided "as is"
 without any express or implied warranty.

 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 THE USE OR PERFORMANCE OF THIS SOFTWARE.

 ********************************************************/

#include "xkbcomp.h"
#include "tokens.h"
#include "expr.h"
#include "parseutils.h"

#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <stdlib.h>

#include "expr.h"
#include "vmod.h"
#include "action.h"
#include "keycodes.h"
#include "misc.h"
#include "alias.h"

/***====================================================================***/

#define	RepeatYes	1
#define	RepeatNo	0
#define	RepeatUndefined	~((unsigned)0)

#define	_Key_Syms	(1<<0)
#define	_Key_Acts	(1<<1)
#define	_Key_Repeat	(1<<2)
#define	_Key_Behavior	(1<<3)
#define	_Key_Type_Dflt	(1<<4)
#define	_Key_Types	(1<<5)
#define	_Key_GroupInfo	(1<<6)
#define	_Key_VModMap	(1<<7)

typedef struct _KeyInfo
{
    CommonInfo defs;
    unsigned long name; /* the 4 chars of the key name, as long */
    unsigned char groupInfo;
    unsigned char typesDefined;
    unsigned char symsDefined;
    unsigned char actsDefined;
    short numLevels[XkbNumKbdGroups];
    KeySym *syms[XkbNumKbdGroups];
    XkbAction *acts[XkbNumKbdGroups];
    Atom types[XkbNumKbdGroups];
    unsigned repeat;
    XkbBehavior behavior;
    unsigned short vmodmap;
    unsigned long nameForOverlayKey;
    unsigned long allowNone;
    Atom dfltType;
} KeyInfo;

/**
 * Init the given key info to sane values.
 */
static void
InitKeyInfo(KeyInfo * info)
{
    static char dflt[4] = "*";

    info->defs.defined = 0;
    info->defs.fileID = 0;
    info->defs.merge = MergeOverride;
    info->defs.next = NULL;
    info->name = KeyNameToLong(dflt);
    info->groupInfo = 0;
    info->typesDefined = info->symsDefined = info->actsDefined = 0;
    for (int i = 0; i < XkbNumKbdGroups; i++)
    {
        info->numLevels[i] = 0;
        info->types[i] = None;
        info->syms[i] = NULL;
        info->acts[i] = NULL;
    }
    info->dfltType = None;
    info->behavior.type = XkbKB_Default;
    info->behavior.data = 0;
    info->vmodmap = 0;
    info->nameForOverlayKey = 0;
    info->repeat = RepeatUndefined;
    info->allowNone = 0;
    return;
}

/**
 * Free memory associated with this key info and reset to sane values.
 */
static void
FreeKeyInfo(KeyInfo * info)
{
    info->defs.defined = 0;
    info->defs.fileID = 0;
    info->defs.merge = MergeOverride;
    info->defs.next = NULL;
    info->groupInfo = 0;
    info->typesDefined = info->symsDefined = info->actsDefined = 0;
    for (int i = 0; i < XkbNumKbdGroups; i++)
    {
        info->numLevels[i] = 0;
        info->types[i] = None;
        free(info->syms[i]);
        info->syms[i] = NULL;
        free(info->acts[i]);
        info->acts[i] = NULL;
    }
    info->dfltType = None;
    info->behavior.type = XkbKB_Default;
    info->behavior.data = 0;
    info->vmodmap = 0;
    info->nameForOverlayKey = 0;
    info->repeat = RepeatUndefined;
    info->allowNone = 0;
    return;
}

/**
 * Copy old into new, optionally reset old to 0.
 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
 * newly allocated and new points to the new memory areas.
 */
static Bool
CopyKeyInfo(KeyInfo * old, KeyInfo * new, Bool clearOld)
{
    *new = *old;
    new->defs.next = NULL;
    if (clearOld)
    {
        for (int i = 0; i < XkbNumKbdGroups; i++)
        {
            old->numLevels[i] = 0;
            old->syms[i] = NULL;
            old->acts[i] = NULL;
        }
    }
    else
    {
        for (int i = 0; i < XkbNumKbdGroups; i++)
        {
            int width = new->numLevels[i];
            if (old->syms[i] != NULL)
            {
                new->syms[i] = calloc(width, sizeof(KeySym));
                if (!new->syms[i])
                {
                    new->syms[i] = NULL;
                    new->numLevels[i] = 0;
                    return False;
                }
                memcpy(new->syms[i], old->syms[i], width * sizeof(KeySym));
            }
            if (old->acts[i] != NULL)
            {
                new->acts[i] = calloc(width, sizeof(XkbAction));
                if (!new->acts[i])
                {
                    new->acts[i] = NULL;
                    return False;
                }
                memcpy(new->acts[i], old->acts[i], width * sizeof(XkbAction));
            }
        }
    }
    return True;
}

/***====================================================================***/

typedef struct _ModMapEntry
{
    CommonInfo defs;
    Bool haveSymbol;
    int modifier;
    union
    {
        unsigned long keyName;
        KeySym keySym;
    } u;
} ModMapEntry;

#define	SYMBOLS_INIT_SIZE	110
#define	SYMBOLS_CHUNK		20
typedef struct _SymbolsInfo
{
    char *name;         /* e.g. pc+us+inet(evdev) */
    int errorCount;
    unsigned fileID;
    unsigned merge;
    unsigned explicit_group;
    unsigned groupInfo;
    unsigned szKeys;
    unsigned nKeys;
    KeyInfo *keys;
    KeyInfo dflt;
    VModInfo vmods;
    ActionInfo *action;
    Atom groupNames[XkbNumKbdGroups];

    ModMapEntry *modMap;
    AliasInfo *aliases;
} SymbolsInfo;

static void
InitSymbolsInfo(SymbolsInfo * info, XkbDescPtr xkb)
{
    tok_ONE_LEVEL = XkbInternAtom(NULL, "ONE_LEVEL", False);
    tok_TWO_LEVEL = XkbInternAtom(NULL, "TWO_LEVEL", False);
    tok_KEYPAD = XkbInternAtom(NULL, "KEYPAD", False);
    info->name = NULL;
    info->explicit_group = 0;
    info->errorCount = 0;
    info->fileID = 0;
    info->merge = MergeOverride;
    info->groupInfo = 0;
    info->szKeys = SYMBOLS_INIT_SIZE;
    info->nKeys = 0;
    info->keys = calloc(SYMBOLS_INIT_SIZE, sizeof(KeyInfo));
    info->modMap = NULL;
    for (int i = 0; i < XkbNumKbdGroups; i++)
        info->groupNames[i] = None;
    InitKeyInfo(&info->dflt);
    InitVModInfo(&info->vmods, xkb);
    info->action = NULL;
    info->aliases = NULL;
    return;
}

static void
FreeSymbolsInfo(SymbolsInfo * info)
{
    free(info->name);
    info->name = NULL;
    if (info->keys)
    {
        for (int i = 0; i < info->nKeys; i++)
        {
            FreeKeyInfo(&info->keys[i]);
        }
        free(info->keys);
        info->keys = NULL;
    }
    if (info->modMap)
    {
        ClearCommonInfo(&info->modMap->defs);
        info->modMap = NULL;
    }
    if (info->aliases)
    {
        ClearAliases(&info->aliases);
        info->aliases = NULL;
    }
    bzero(info, sizeof(SymbolsInfo));
    return;
}

static Bool
ResizeKeyGroup(KeyInfo * key,
               unsigned group, unsigned atLeastSize, Bool forceActions)
{
    Bool tooSmall;
    unsigned newWidth;

    tooSmall = (key->numLevels[group] < atLeastSize);
    if (tooSmall)
        newWidth = atLeastSize;
    else
        newWidth = key->numLevels[group];

    if ((key->syms[group] == NULL) || tooSmall)
    {
        key->syms[group] = recallocarray(key->syms[group],
                                         key->numLevels[group], newWidth,
                                         sizeof(KeySym));
        if (!key->syms[group])
            return False;
    }
    if (((forceActions) && (tooSmall || (key->acts[group] == NULL))) ||
        (tooSmall && (key->acts[group] != NULL)))
    {
        key->acts[group] = recallocarray(key->acts[group],
                                         key->numLevels[group], newWidth,
                                         sizeof(XkbAction));
        if (!key->acts[group])
            return False;
    }
    key->numLevels[group] = newWidth;
    return True;
}

static Bool
MergeKeyGroups(SymbolsInfo * info,
               KeyInfo * into, KeyInfo * from, unsigned group)
{
    KeySym *resultSyms;
    XkbAction *resultActs;
    int resultWidth;
    Bool report, clobber;

    clobber = (from->defs.merge != MergeAugment);
    report = (warningLevel > 9) ||
        ((into->defs.fileID == from->defs.fileID) && (warningLevel > 0));
    if ((from->numLevels[group] > into->numLevels[group])
        || (clobber && (from->types[group] != None)))
    {
        resultSyms = from->syms[group];
        resultActs = from->acts[group];
        resultWidth = from->numLevels[group];
    }
    else
    {
        resultSyms = into->syms[group];
        resultActs = into->acts[group];
        resultWidth = into->numLevels[group];
    }
    if (resultSyms == NULL)
    {
        resultSyms = calloc(resultWidth, sizeof(KeySym));
        if (!resultSyms)
        {
            WSGO("Could not allocate symbols for group merge\n");
            ACTION("Group %d of key %s not merged\n", group,
                    longText(into->name, XkbMessage));
            return False;
        }
    }
    if ((resultActs == NULL) && (into->acts[group] || from->acts[group]))
    {
        resultActs = calloc(resultWidth, sizeof(XkbAction));
        if (!resultActs)
        {
            WSGO("Could not allocate actions for group merge\n");
            ACTION("Group %d of key %s not merged\n", group,
                    longText(into->name, XkbMessage));
            return False;
        }
    }
    for (int i = 0; i < resultWidth; i++)
    {
        KeySym fromSym, toSym;
        if (from->syms[group] && (i < from->numLevels[group]))
            fromSym = from->syms[group][i];
        else
            fromSym = NoSymbol;
        if (into->syms[group] && (i < into->numLevels[group]))
            toSym = into->syms[group][i];
        else
            toSym = NoSymbol;
        if ((fromSym == NoSymbol) || (fromSym == toSym))
            resultSyms[i] = toSym;
        else if (toSym == NoSymbol)
            resultSyms[i] = fromSym;
        else
        {
            KeySym use, ignore;
            if (clobber)
            {
                use = fromSym;
                ignore = toSym;
            }
            else
            {
                use = toSym;
                ignore = fromSym;
            }
            if (report)
            {
                WARN
                    ("Multiple symbols for level %d/group %d on key %s\n",
                     i + 1, group + 1, longText(into->name, XkbMessage));
                ACTION("Using %s, ignoring %s\n",
                        XkbKeysymText(use, XkbMessage),
                        XkbKeysymText(ignore, XkbMessage));
            }
            resultSyms[i] = use;
        }
        if (resultActs != NULL)
        {
            XkbAction *fromAct, *toAct;
            fromAct = (from->acts[group] ? &from->acts[group][i] : NULL);
            toAct = (into->acts[group] ? &into->acts[group][i] : NULL);
            if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
                && (toAct != NULL))
            {
                resultActs[i] = *toAct;
            }
            else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
                     && (fromAct != NULL))
            {
                resultActs[i] = *fromAct;
            }
            else
            {
                XkbAction *use, *ignore;
                if (clobber)
                {
                    use = fromAct;
                    ignore = toAct;
                }
                else
                {
                    use = toAct;
                    ignore = fromAct;
                }
                if (report)
                {
                    WARN
                        ("Multiple actions for level %d/group %d on key %s\n",
                         i + 1, group + 1, longText(into->name, XkbMessage));
                    ACTION("Using %s, ignoring %s\n",
                            XkbActionTypeText(use->type, XkbMessage),
                            XkbActionTypeText(ignore->type, XkbMessage));
                }
                resultActs[i] = *use;
            }
        }
    }
    if ((into->syms[group] != NULL) && (resultSyms != into->syms[group]))
        free(into->syms[group]);
    if ((from->syms[group] != NULL) && (resultSyms != from->syms[group]))
        free(from->syms[group]);
    if ((into->acts[group] != NULL) && (resultActs != into->acts[group]))
        free(into->acts[group]);
    if ((from->acts[group] != NULL) && (resultActs != from->acts[group]))
        free(from->acts[group]);
    into->numLevels[group] = resultWidth;
    into->syms[group] = resultSyms;
    from->syms[group] = NULL;
    into->acts[group] = resultActs;
    from->acts[group] = NULL;
    into->symsDefined |= (1U << group);
    from->symsDefined &= ~(1U << group);
    into->actsDefined |= (1U << group);
    from->actsDefined &= ~(1U << group);
    return True;
}

static Bool
MergeKeys(SymbolsInfo * info, KeyInfo * into, KeyInfo * from)
{
    unsigned collide = 0;
    Bool report;

    if (from->defs.merge == MergeReplace)
    {
        for (int i = 0; i < XkbNumKbdGroups; i++)
        {
            if (into->numLevels[i] != 0)
            {
                free(into->syms[i]);
                free(into->acts[i]);
            }
        }
        *into = *from;
        bzero(from, sizeof(KeyInfo));
        return True;
    }
    report = ((warningLevel > 9) ||
              ((into->defs.fileID == from->defs.fileID)
               && (warningLevel > 0)));
    for (int i = 0; i < XkbNumKbdGroups; i++)
    {
        if (from->numLevels[i] > 0)
        {
            if (into->numLevels[i] == 0)
            {
                into->numLevels[i] = from->numLevels[i];
                into->syms[i] = from->syms[i];
                into->acts[i] = from->acts[i];
                into->symsDefined |= (1U << i);
                from->syms[i] = NULL;
                from->acts[i] = NULL;
                from->numLevels[i] = 0;
                from->symsDefined &= ~(1U << i);
                if (into->syms[i])
                    into->defs.defined |= _Key_Syms;
                if (into->acts[i])
                    into->defs.defined |= _Key_Acts;
            }
            else
            {
                if (report)
                {
                    if (into->syms[i])
                        collide |= _Key_Syms;
                    if (into->acts[i])
                        collide |= _Key_Acts;
                }
                MergeKeyGroups(info, into, from, (unsigned) i);
            }
        }
        if (from->types[i] != None)
        {
            if ((into->types[i] != None) && (report) &&
                (into->types[i] != from->types[i]))
            {
                Atom use, ignore;
                collide |= _Key_Types;
                if (from->defs.merge != MergeAugment)
                {
                    use = from->types[i];
                    ignore = into->types[i];
                }
                else
                {
                    use = into->types[i];
                    ignore = from->types[i];
                }
                WARN
                    ("Multiple definitions for group %d type of key %s\n",
                     i, longText(into->name, XkbMessage));
                ACTION("Using %s, ignoring %s\n",
                        XkbAtomText(NULL, use, XkbMessage),
                        XkbAtomText(NULL, ignore, XkbMessage));
            }
            if ((from->defs.merge != MergeAugment)
                || (into->types[i] == None))
            {
                into->types[i] = from->types[i];
            }
        }
    }
    if (UseNewField(_Key_Behavior, &into->defs, &from->defs, &collide))
    {
        into->behavior = from->behavior;
        into->nameForOverlayKey = from->nameForOverlayKey;
        into->defs.defined |= _Key_Behavior;
    }
    if (UseNewField(_Key_VModMap, &into->defs, &from->defs, &collide))
    {
        into->vmodmap = from->vmodmap;
        into->defs.defined |= _Key_VModMap;
    }
    if (UseNewField(_Key_Repeat, &into->defs, &from->defs, &collide))
    {
        into->repeat = from->repeat;
        into->defs.defined |= _Key_Repeat;
    }
    if (UseNewField(_Key_Type_Dflt, &into->defs, &from->defs, &collide))
    {
        into->dfltType = from->dfltType;
        into->defs.defined |= _Key_Type_Dflt;
    }
    if (UseNewField(_Key_GroupInfo, &into->defs, &from->defs, &collide))
    {
        into->groupInfo = from->groupInfo;
        into->defs.defined |= _Key_GroupInfo;
    }
    if (collide && (warningLevel > 0))
    {
        WARN("Symbol map for key %s redefined\n",
              longText(into->name, XkbMessage));
        ACTION("Using %s definition for conflicting fields\n",
                (from->defs.merge == MergeAugment ? "first" : "last"));
    }
    return True;
}

static Bool
AddKeySymbols(SymbolsInfo * info, KeyInfo * key, XkbDescPtr xkb)
{
    unsigned long real_name;

    for (int i = 0; i < info->nKeys; i++)
    {
        if (info->keys[i].name == key->name)
            return MergeKeys(info, &info->keys[i], key);
    }
    if (FindKeyNameForAlias(xkb, key->name, &real_name))
    {
        for (int i = 0; i < info->nKeys; i++)
        {
            if (info->keys[i].name == real_name)
                return MergeKeys(info, &info->keys[i], key);
        }
    }
    if (info->nKeys >= info->szKeys)
    {
        info->szKeys += SYMBOLS_CHUNK;
        info->keys = recallocarray(info->keys, info->nKeys, info->szKeys,
                                   sizeof(KeyInfo));
        if (!info->keys)
        {
            WSGO("Could not allocate key symbols descriptions\n");
            ACTION("Some key symbols definitions may be lost\n");
            return False;
        }
    }
    return CopyKeyInfo(key, &info->keys[info->nKeys++], True);
}

static Bool
AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
{
    ModMapEntry *mm;
    Bool clobber;

    clobber = (new->defs.merge != MergeAugment);
    for (mm = info->modMap; mm != NULL; mm = (ModMapEntry *) mm->defs.next)
    {
        if (new->haveSymbol && mm->haveSymbol
            && (new->u.keySym == mm->u.keySym))
        {
            if (mm->modifier != new->modifier)
            {
                unsigned use, ignore;

                if (clobber)
                {
                    use = new->modifier;
                    ignore = mm->modifier;
                }
                else
                {
                    use = mm->modifier;
                    ignore = new->modifier;
                }
                ERROR
                    ("%s added to symbol map for multiple modifiers\n",
                     XkbKeysymText(new->u.keySym, XkbMessage));
                ACTION("Using %s, ignoring %s.\n",
                        XkbModIndexText(use, XkbMessage),
                        XkbModIndexText(ignore, XkbMessage));
                mm->modifier = use;
            }
            return True;
        }
        if ((!new->haveSymbol) && (!mm->haveSymbol) &&
            (new->u.keyName == mm->u.keyName))
        {
            if (mm->modifier != new->modifier)
            {
                unsigned use, ignore;

                if (clobber)
                {
                    use = new->modifier;
                    ignore = mm->modifier;
                }
                else
                {
                    use = mm->modifier;
                    ignore = new->modifier;
                }
                ERROR("Key %s added to map for multiple modifiers\n",
                       longText(new->u.keyName, XkbMessage));
                ACTION("Using %s, ignoring %s.\n",
                        XkbModIndexText(use, XkbMessage),
                        XkbModIndexText(ignore, XkbMessage));
                mm->modifier = use;
            }
            return True;
        }
    }
    mm = malloc(sizeof(ModMapEntry));
    if (mm == NULL)
    {
        WSGO("Could not allocate modifier map entry\n");
        ACTION("Modifier map for %s will be incomplete\n",
                XkbModIndexText(new->modifier, XkbMessage));
        return False;
    }
    *mm = *new;
    mm->defs.next = &info->modMap->defs;
    info->modMap = mm;
    return True;
}

/***====================================================================***/

static void
MergeIncludedSymbols(SymbolsInfo * into, SymbolsInfo * from,
                     unsigned merge, XkbDescPtr xkb)
{
    int i;
    KeyInfo *key;

    if (from->errorCount > 0)
    {
        into->errorCount += from->errorCount;
        return;
    }
    if (into->name == NULL)
    {
        into->name = from->name;
        from->name = NULL;
    }
    for (i = 0; i < XkbNumKbdGroups; i++)
    {
        if (from->groupNames[i] != None)
        {
            if ((merge != MergeAugment) || (into->groupNames[i] == None))
                into->groupNames[i] = from->groupNames[i];
        }
    }
    for (i = 0, key = from->keys; i < from->nKeys; i++, key++)
    {
        if (merge != MergeDefault)
            key->defs.merge = merge;
        if (!AddKeySymbols(into, key, xkb))
            into->errorCount++;
    }
    if (from->modMap != NULL)
    {
        ModMapEntry *mm, *next;
        for (mm = from->modMap; mm != NULL; mm = next)
        {
            if (merge != MergeDefault)
                mm->defs.merge = merge;
            if (!AddModMapEntry(into, mm))
                into->errorCount++;
            next = (ModMapEntry *) mm->defs.next;
            free(mm);
        }
        from->modMap = NULL;
    }
    if (!MergeAliases(&into->aliases, &from->aliases, merge))
        into->errorCount++;
    return;
}

typedef void (*FileHandler) (XkbFile * /* rtrn */ ,
                             XkbDescPtr /* xkb */ ,
                             unsigned /* merge */ ,
                             SymbolsInfo *      /* included */
    );

static Bool
HandleIncludeSymbols(IncludeStmt * stmt,
                     XkbDescPtr xkb, SymbolsInfo * info, FileHandler hndlr)
{
    unsigned newMerge;
    XkbFile *rtrn;
    SymbolsInfo included;
    Bool haveSelf;

    haveSelf = False;
    if ((stmt->file == NULL) && (stmt->map == NULL))
    {
        haveSelf = True;
        included = *info;
        bzero(info, sizeof(SymbolsInfo));
    }
    else if (ProcessIncludeFile(stmt, XkmSymbolsIndex, &rtrn, &newMerge))
    {
        InitSymbolsInfo(&included, xkb);
        included.fileID = included.dflt.defs.fileID = rtrn->id;
        included.merge = included.dflt.defs.merge = MergeOverride;
        if (stmt->modifier)
        {
            included.explicit_group = atoi(stmt->modifier) - 1;
        }
        else
        {
            included.explicit_group = info->explicit_group;
        }
        (*hndlr) (rtrn, xkb, MergeOverride, &included);
        if (stmt->stmt != NULL)
        {
            free(included.name);
            included.name = stmt->stmt;
            stmt->stmt = NULL;
        }
    }
    else
    {
        info->errorCount += 10;
        return False;
    }
    if ((stmt->next != NULL) && (included.errorCount < 1))
    {
        IncludeStmt *next;
        unsigned op;
        SymbolsInfo next_incl;

        for (next = stmt->next; next != NULL; next = next->next)
        {
            if ((next->file == NULL) && (next->map == NULL))
            {
                haveSelf = True;
                MergeIncludedSymbols(&included, info, next->merge, xkb);
                FreeSymbolsInfo(info);
            }
            else if (ProcessIncludeFile(next, XkmSymbolsIndex, &rtrn, &op))
            {
                InitSymbolsInfo(&next_incl, xkb);
                next_incl.fileID = next_incl.dflt.defs.fileID = rtrn->id;
                next_incl.merge = next_incl.dflt.defs.merge = MergeOverride;
                if (next->modifier)
                {
                    next_incl.explicit_group = atoi(next->modifier) - 1;
                }
                else
                {
                    next_incl.explicit_group = info->explicit_group;
                }
                (*hndlr) (rtrn, xkb, MergeOverride, &next_incl);
                MergeIncludedSymbols(&included, &next_incl, op, xkb);
                FreeSymbolsInfo(&next_incl);
            }
            else
            {
                info->errorCount += 10;
                return False;
            }
        }
    }
    if (haveSelf)
        *info = included;
    else
    {
        MergeIncludedSymbols(info, &included, newMerge, xkb);
        FreeSymbolsInfo(&included);
    }
    return (info->errorCount == 0);
}

static LookupEntry groupNames[] = {
    {"group1", 1},
    {"group2", 2},
    {"group3", 3},
    {"group4", 4},
    {"group5", 5},
    {"group6", 6},
    {"group7", 7},
    {"group8", 8},
    {NULL, 0}
};


#define	SYMBOLS 1
#define	ACTIONS	2

static Bool
GetGroupIndex(KeyInfo *key, const ExprDef *arrayNdx,
              unsigned what, unsigned *ndx_rtrn)
{
    const char *name;
    ExprResult tmp;

    if (what == SYMBOLS)
        name = "symbols";
    else
        name = "actions";

    if (arrayNdx == NULL)
    {
        unsigned defined;
        if (what == SYMBOLS)
            defined = key->symsDefined;
        else
            defined = key->actsDefined;

        for (int i = 0; i < XkbNumKbdGroups; i++)
        {
            if ((defined & (1U << i)) == 0)
            {
                *ndx_rtrn = i;
                return True;
            }
        }
        ERROR("Too many groups of %s for key %s (max %d)\n", name,
               longText(key->name, XkbMessage), XkbNumKbdGroups + 1);
        ACTION("Ignoring %s defined for extra groups\n", name);
        return False;
    }
    if (!ExprResolveInteger
        (arrayNdx, &tmp, SimpleLookup, (XPointer) groupNames))
    {
        ERROR("Illegal group index for %s of key %s\n", name,
               longText(key->name, XkbMessage));
        ACTION("Definition with non-integer array index ignored\n");
        return False;
    }
    if ((tmp.uval < 1) || (tmp.uval > XkbNumKbdGroups))
    {
        ERROR("Group index for %s of key %s is out of range (1..%d)\n",
               name, longText(key->name, XkbMessage), XkbNumKbdGroups + 1);
        ACTION("Ignoring %s for group %d\n", name, tmp.uval);
        return False;
    }
    *ndx_rtrn = tmp.uval - 1;
    return True;
}

static Bool
AddSymbolsToKey(KeyInfo *key, XkbDescPtr xkb, const char *field,
                const ExprDef *arrayNdx, const ExprDef *value,
                SymbolsInfo *info)
{
    unsigned ndx, nSyms;

    if (!GetGroupIndex(key, arrayNdx, SYMBOLS, &ndx))
        return False;
    if (value == NULL)
    {
        key->symsDefined |= (1U << ndx);
        return True;
    }
    if (value->op != ExprKeysymList)
    {
        ERROR("Expected a list of symbols, found %s\n",
               exprOpText(value->op));
        ACTION("Ignoring symbols for group %d of %s\n", ndx,
                longText(key->name, XkbMessage));
        return False;
    }
    if (key->syms[ndx] != NULL)
    {
        WSGO("Symbols for key %s, group %d already defined\n",
              longText(key->name, XkbMessage), ndx);
        return False;
    }
    nSyms = value->value.list.nSyms;
    if (((key->numLevels[ndx] < nSyms) || (key->syms[ndx] == NULL)) &&
        (!ResizeKeyGroup(key, ndx, nSyms, False)))
    {
        WSGO("Could not resize group %d of key %s\n", ndx,
              longText(key->name, XkbMessage));
        ACTION("Symbols lost\n");
        return False;
    }
    key->symsDefined |= (1U << ndx);
    for (int i = 0; i < nSyms; i++) {
        if (!LookupKeysym(value->value.list.syms[i], &key->syms[ndx][i])) {
            if (warningLevel > 0)
            {
                WARN("Could not resolve keysym %s\n",
                      value->value.list.syms[i]);
            }
            key->syms[ndx][i] = NoSymbol;
        }
    }
    for (int i = key->numLevels[ndx] - 1;
         (i >= 0) && (key->syms[ndx][i] == NoSymbol); i--)
    {
        key->numLevels[ndx]--;
    }
    return True;
}

static Bool
AddActionsToKey(KeyInfo *key, XkbDescPtr xkb, const char *field,
                const ExprDef *arrayNdx, const ExprDef *value,
                SymbolsInfo *info)
{
    unsigned ndx, nActs;
    ExprDef *act;
    XkbAnyAction *toAct;

    if (!GetGroupIndex(key, arrayNdx, ACTIONS, &ndx))
        return False;

    if (value == NULL)
    {
        key->actsDefined |= (1U << ndx);
        return True;
    }
    if (value->op != ExprActionList)
    {
        WSGO("Bad expression type (%d) for action list value\n", value->op);
        ACTION("Ignoring actions for group %d of %s\n", ndx,
                longText(key->name, XkbMessage));
        return False;
    }
    if (key->acts[ndx] != NULL)
    {
        WSGO("Actions for key %s, group %d already defined\n",
              longText(key->name, XkbMessage), ndx);
        return False;
    }
    for (nActs = 0, act = value->value.child; act != NULL; nActs++)
    {
        act = (ExprDef *) act->common.next;
    }
    if (nActs < 1)
    {
        WSGO("Action list but not actions in AddActionsToKey\n");
        return False;
    }
    if (((key->numLevels[ndx] < nActs) || (key->acts[ndx] == NULL)) &&
        (!ResizeKeyGroup(key, ndx, nActs, True)))
    {
        WSGO("Could not resize group %d of key %s\n", ndx,
              longText(key->name, XkbMessage));
        ACTION("Actions lost\n");
        return False;
    }
    key->actsDefined |= (1U << ndx);

    toAct = (XkbAnyAction *) key->acts[ndx];
    act = value->value.child;
    for (int i = 0; i < nActs; i++, toAct++)
    {
        if (!HandleActionDef(act, xkb, toAct, MergeOverride, info->action))
        {
            ERROR("Illegal action definition for %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Action for group %d/level %d ignored\n", ndx + 1, i + 1);
        }
        act = (ExprDef *) act->common.next;
    }
    return True;
}

static int
SetAllowNone(KeyInfo *key, const ExprDef *arrayNdx, const ExprDef *value)
{
    ExprResult tmp;
    unsigned radio_groups = 0;

    if (arrayNdx == NULL)
    {
        radio_groups = XkbAllRadioGroupsMask;
    }
    else
    {
        if (!ExprResolveInteger(arrayNdx, &tmp, RadioLookup, NULL))
        {
            ERROR("Illegal index in group name definition\n");
            ACTION("Definition with non-integer array index ignored\n");
            return False;
        }
        if ((tmp.uval < 1) || (tmp.uval > XkbMaxRadioGroups))
        {
            ERROR("Illegal radio group specified (must be 1..%d)\n",
                   XkbMaxRadioGroups + 1);
            ACTION("Value of \"allow none\" for group %d ignored\n",
                    tmp.uval);
            return False;
        }
        radio_groups |= (1U << (tmp.uval - 1));
    }
    if (!ExprResolveBoolean(value, &tmp, NULL, NULL))
    {
        ERROR("Illegal \"allow none\" value for %s\n",
               longText(key->name, XkbMessage));
        ACTION("Non-boolean value ignored\n");
        return False;
    }
    if (tmp.uval)
        key->allowNone |= radio_groups;
    else
        key->allowNone &= ~radio_groups;
    return True;
}


static LookupEntry lockingEntries[] = {
    {"true", XkbKB_Lock},
    {"yes", XkbKB_Lock},
    {"on", XkbKB_Lock},
    {"false", XkbKB_Default},
    {"no", XkbKB_Default},
    {"off", XkbKB_Default},
    {"permanent", XkbKB_Lock | XkbKB_Permanent},
    {NULL, 0}
};

static LookupEntry repeatEntries[] = {
    {"true", RepeatYes},
    {"yes", RepeatYes},
    {"on", RepeatYes},
    {"false", RepeatNo},
    {"no", RepeatNo},
    {"off", RepeatNo},
    {"default", RepeatUndefined},
    {NULL, 0}
};

static LookupEntry rgEntries[] = {
    {"none", 0},
    {NULL, 0}
};

static Bool
SetSymbolsField(KeyInfo *key, XkbDescPtr xkb, const char *field,
                const ExprDef *arrayNdx, const ExprDef *value,
                SymbolsInfo *info)
{
    Bool ok = True;
    ExprResult tmp;

    if (uStrCaseCmp(field, "type") == 0)
    {
        ExprResult ndx;
        if ((!ExprResolveString(value, &tmp, NULL, NULL))
            && (warningLevel > 0))
        {
            WARN("The type field of a key symbol map must be a string\n");
            ACTION("Ignoring illegal type definition\n");
        }
        if (arrayNdx == NULL)
        {
            key->dfltType = XkbInternAtom(NULL, tmp.str, False);
            key->defs.defined |= _Key_Type_Dflt;
        }
        else if (!ExprResolveInteger(arrayNdx, &ndx, SimpleLookup,
                                     (XPointer) groupNames))
        {
            ERROR("Illegal group index for type of key %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Definition with non-integer array index ignored\n");
            return False;
        }
        else if ((ndx.uval < 1) || (ndx.uval > XkbNumKbdGroups))
        {
            ERROR
                ("Group index for type of key %s is out of range (1..%d)\n",
                 longText(key->name, XkbMessage), XkbNumKbdGroups + 1);
            ACTION("Ignoring type for group %d\n", ndx.uval);
            return False;
        }
        else
        {
            key->types[ndx.uval - 1] = XkbInternAtom(NULL, tmp.str, False);
            key->typesDefined |= (1U << (ndx.uval - 1));
        }
    }
    else if (uStrCaseCmp(field, "symbols") == 0)
        return AddSymbolsToKey(key, xkb, field, arrayNdx, value, info);
    else if (uStrCaseCmp(field, "actions") == 0)
        return AddActionsToKey(key, xkb, field, arrayNdx, value, info);
    else if ((uStrCaseCmp(field, "vmods") == 0) ||
             (uStrCaseCmp(field, "virtualmods") == 0) ||
             (uStrCaseCmp(field, "virtualmodifiers") == 0))
    {
        ok = ExprResolveModMask(value, &tmp, LookupVModMask, (XPointer) xkb);
        if (ok)
        {
            key->vmodmap = (tmp.uval >> 8);
            key->defs.defined |= _Key_VModMap;
        }
        else
        {
            ERROR("Expected a virtual modifier mask, found %s\n",
                   exprOpText(value->op));
            ACTION("Ignoring virtual modifiers definition for key %s\n",
                    longText(key->name, XkbMessage));
        }
    }
    else if ((uStrCaseCmp(field, "locking") == 0)
             || (uStrCaseCmp(field, "lock") == 0)
             || (uStrCaseCmp(field, "locks") == 0))
    {
        ok = ExprResolveEnum(value, &tmp, lockingEntries);
        if (ok)
            key->behavior.type = tmp.uval;
        key->defs.defined |= _Key_Behavior;
    }
    else if ((uStrCaseCmp(field, "radiogroup") == 0) ||
             (uStrCaseCmp(field, "permanentradiogroup") == 0))
    {
        Bool permanent = False;
        if (uStrCaseCmp(field, "permanentradiogroup") == 0)
            permanent = True;
        ok = ExprResolveInteger(value, &tmp, SimpleLookup,
                                (XPointer) rgEntries);
        if (!ok)
        {
            ERROR("Illegal radio group specification for %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Non-integer radio group ignored\n");
            return False;
        }
        if (tmp.uval == 0)
        {
            key->behavior.type = XkbKB_Default;
            key->behavior.data = 0;
            return ok;
        }
        if ((tmp.uval < 1) || (tmp.uval > XkbMaxRadioGroups))
        {
            ERROR
                ("Radio group specification for %s out of range (1..32)\n",
                 longText(key->name, XkbMessage));
            ACTION("Illegal radio group %d ignored\n", tmp.uval);
            return False;
        }
        key->behavior.type =
            XkbKB_RadioGroup | (permanent ? XkbKB_Permanent : 0);
        key->behavior.data = tmp.uval - 1;
        if (key->allowNone & (1U << (tmp.uval - 1)))
            key->behavior.data |= XkbKB_RGAllowNone;
        key->defs.defined |= _Key_Behavior;
    }
    else if (uStrCaseEqual(field, "allownone"))
    {
        ok = SetAllowNone(key, arrayNdx, value);
    }
    else if (uStrCasePrefix("overlay", field) ||
             uStrCasePrefix("permanentoverlay", field))
    {
        Bool permanent = False;
        const char *which;
        int overlayNdx;
        if (uStrCasePrefix("permanent", field))
        {
            permanent = True;
            which = &field[sizeof("permanentoverlay") - 1];
        }
        else
        {
            which = &field[sizeof("overlay") - 1];
        }
        if (sscanf(which, "%d", &overlayNdx) == 1)
        {
            if (((overlayNdx < 1) || (overlayNdx > 2)) && (warningLevel > 0))
            {
                ERROR("Illegal overlay %d specified for %s\n",
                       overlayNdx, longText(key->name, XkbMessage));
                ACTION("Ignored\n");
                return False;
            }
        }
        else if (*which == '\0')
            overlayNdx = 1;
        else if (warningLevel > 0)
        {
            ERROR("Illegal overlay \"%s\" specified for %s\n",
                   which, longText(key->name, XkbMessage));
            ACTION("Ignored\n");
            return False;
        }
        ok = ExprResolveKeyName(value, &tmp, NULL, NULL);
        if (!ok)
        {
            ERROR("Illegal overlay key specification for %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Overlay key must be specified by name\n");
            return False;
        }
        if (overlayNdx == 1)
            key->behavior.type = XkbKB_Overlay1;
        else
            key->behavior.type = XkbKB_Overlay2;
        if (permanent)
            key->behavior.type |= XkbKB_Permanent;

        key->behavior.data = 0;
        key->nameForOverlayKey = KeyNameToLong(tmp.keyName.name);
        key->defs.defined |= _Key_Behavior;
    }
    else if ((uStrCaseCmp(field, "repeating") == 0) ||
             (uStrCaseCmp(field, "repeats") == 0) ||
             (uStrCaseCmp(field, "repeat") == 0))
    {
        ok = ExprResolveEnum(value, &tmp, repeatEntries);
        if (!ok)
        {
            ERROR("Illegal repeat setting for %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Non-boolean repeat setting ignored\n");
            return False;
        }
        key->repeat = tmp.uval;
        key->defs.defined |= _Key_Repeat;
    }
    else if ((uStrCaseCmp(field, "groupswrap") == 0) ||
             (uStrCaseCmp(field, "wrapgroups") == 0))
    {
        ok = ExprResolveBoolean(value, &tmp, NULL, NULL);
        if (!ok)
        {
            ERROR("Illegal groupsWrap setting for %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Non-boolean value ignored\n");
            return False;
        }
        if (tmp.uval)
            key->groupInfo = XkbWrapIntoRange;
        else
            key->groupInfo = XkbClampIntoRange;
        key->defs.defined |= _Key_GroupInfo;
    }
    else if ((uStrCaseCmp(field, "groupsclamp") == 0) ||
             (uStrCaseCmp(field, "clampgroups") == 0))
    {
        ok = ExprResolveBoolean(value, &tmp, NULL, NULL);
        if (!ok)
        {
            ERROR("Illegal groupsClamp setting for %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Non-boolean value ignored\n");
            return False;
        }
        if (tmp.uval)
            key->groupInfo = XkbClampIntoRange;
        else
            key->groupInfo = XkbWrapIntoRange;
        key->defs.defined |= _Key_GroupInfo;
    }
    else if ((uStrCaseCmp(field, "groupsredirect") == 0) ||
             (uStrCaseCmp(field, "redirectgroups") == 0))
    {
        if (!ExprResolveInteger
            (value, &tmp, SimpleLookup, (XPointer) groupNames))
        {
            ERROR("Illegal group index for redirect of key %s\n",
                   longText(key->name, XkbMessage));
            ACTION("Definition with non-integer group ignored\n");
            return False;
        }
        if ((tmp.uval < 1) || (tmp.uval > XkbNumKbdGroups))
        {
            ERROR("Out-of-range (1..%d) group for redirect of key %s\n",
                   XkbNumKbdGroups, longText(key->name, XkbMessage));
            ERROR("Ignoring illegal group %d\n", tmp.uval);
            return False;
        }
        key->groupInfo =
            XkbSetGroupInfo(0, XkbRedirectIntoRange, tmp.uval - 1);
        key->defs.defined |= _Key_GroupInfo;
    }
    else
    {
        ERROR("Unknown field %s in a symbol interpretation\n", field);
        ACTION("Definition ignored\n");
        ok = False;
    }
    return ok;
}

static int
SetGroupName(SymbolsInfo *info, const ExprDef *arrayNdx,const  ExprDef *value)
{
    ExprResult tmp, name;

    if ((arrayNdx == NULL) && (warningLevel > 0))
    {
        WARN("You must specify an index when specifying a group name\n");
        ACTION("Group name definition without array subscript ignored\n");
        return False;
    }
    if (!ExprResolveInteger
        (arrayNdx, &tmp, SimpleLookup, (XPointer) groupNames))
    {
        ERROR("Illegal index in group name definition\n");
        ACTION("Definition with non-integer array index ignored\n");
        return False;
    }
    if ((tmp.uval < 1) || (tmp.uval > XkbNumKbdGroups))
    {
        ERROR
            ("Attempt to specify name for illegal group (must be 1..%d)\n",
             XkbNumKbdGroups + 1);
        ACTION("Name for group %d ignored\n", tmp.uval);
        return False;
    }
    if (!ExprResolveString(value, &name, NULL, NULL))
    {
        ERROR("Group name must be a string\n");
        ACTION("Illegal name for group %d ignored\n", tmp.uval);
        return False;
    }
    info->groupNames[tmp.uval - 1 + info->explicit_group] =
        XkbInternAtom(NULL, name.str, False);

    return True;
}

static int
HandleSymbolsVar(VarDef * stmt, XkbDescPtr xkb, SymbolsInfo * info)
{
    ExprResult elem, field, tmp;
    ExprDef *arrayNdx;

    if (ExprResolveLhs(stmt->name, &elem, &field, &arrayNdx) == 0)
        return 0;               /* internal error, already reported */
    if (elem.str && (uStrCaseCmp(elem.str, "key") == 0))
    {
        return SetSymbolsField(&info->dflt, xkb, field.str, arrayNdx,
                               stmt->value, info);
    }
    else if ((elem.str == NULL) && ((uStrCaseCmp(field.str, "name") == 0) ||
                                    (uStrCaseCmp(field.str, "groupname") ==
                                     0)))
    {
        return SetGroupName(info, arrayNdx, stmt->value);
    }
    else if ((elem.str == NULL)
             && ((uStrCaseCmp(field.str, "groupswrap") == 0)
                 || (uStrCaseCmp(field.str, "wrapgroups") == 0)))
    {
        if (!ExprResolveBoolean(stmt->value, &tmp, NULL, NULL))
        {
            ERROR("Illegal setting for global groupsWrap\n");
            ACTION("Non-boolean value ignored\n");
            return False;
        }
        if (tmp.uval)
            info->groupInfo = XkbWrapIntoRange;
        else
            info->groupInfo = XkbClampIntoRange;
        return True;
    }
    else if ((elem.str == NULL)
             && ((uStrCaseCmp(field.str, "groupsclamp") == 0)
                 || (uStrCaseCmp(field.str, "clampgroups") == 0)))
    {
        if (!ExprResolveBoolean(stmt->value, &tmp, NULL, NULL))
        {
            ERROR("Illegal setting for global groupsClamp\n");
            ACTION("Non-boolean value ignored\n");
            return False;
        }
        if (tmp.uval)
            info->groupInfo = XkbClampIntoRange;
        else
            info->groupInfo = XkbWrapIntoRange;
        return True;
    }
    else if ((elem.str == NULL)
             && ((uStrCaseCmp(field.str, "groupsredirect") == 0)
                 || (uStrCaseCmp(field.str, "redirectgroups") == 0)))
    {
        if (!ExprResolveInteger(stmt->value, &tmp,
                                SimpleLookup, (XPointer) groupNames))
        {
            ERROR("Illegal group index for global groupsRedirect\n");
            ACTION("Definition with non-integer group ignored\n");
            return False;
        }
        if ((tmp.uval < 1) || (tmp.uval > XkbNumKbdGroups))
        {
            ERROR
                ("Out-of-range (1..%d) group for global groupsRedirect\n",
                 XkbNumKbdGroups);
            ACTION("Ignoring illegal group %d\n", tmp.uval);
            return False;
        }
        info->groupInfo = XkbSetGroupInfo(0, XkbRedirectIntoRange, tmp.uval);
        return True;
    }
    else if ((elem.str == NULL) && (uStrCaseCmp(field.str, "allownone") == 0))
    {
        return SetAllowNone(&info->dflt, arrayNdx, stmt->value);
    }
    return SetActionField(xkb, elem.str, field.str, arrayNdx, stmt->value,
                          &info->action);
}

static Bool
HandleSymbolsBody(VarDef * def,
                  XkbDescPtr xkb, KeyInfo * key, SymbolsInfo * info)
{
    Bool ok = True;

    for (; def != NULL; def = (VarDef *) def->common.next)
    {
        if ((def->name) && (def->name->type == ExprFieldRef))
        {
            ok = HandleSymbolsVar(def, xkb, info);
            continue;
        }
        else
        {
            ExprResult tmp, field;
            ExprDef *arrayNdx;

            if (def->name == NULL)
            {
                if ((def->value == NULL)
                    || (def->value->op == ExprKeysymList))
                    field.str = "symbols";
                else
                    field.str = "actions";
                arrayNdx = NULL;
            }
            else
            {
                ok = ExprResolveLhs(def->name, &tmp, &field, &arrayNdx);
            }
            if (ok)
                ok = SetSymbolsField(key, xkb, field.str, arrayNdx,
                                     def->value, info);
        }
    }
    return ok;
}

static Bool
SetExplicitGroup(SymbolsInfo * info, KeyInfo * key)
{
    unsigned group = info->explicit_group;

    if (group == 0)
        return True;

    if ((key->typesDefined | key->symsDefined | key->actsDefined) & ~1)
    {
        if (warningLevel > 0)
        {
            WARN("For map %s an explicit group is specified\n", info->name);
            WARN("but key %s has more than one group defined\n",
                  longText(key->name, XkbMessage));
            ACTION("All groups except first one will be ignored\n");
        }
        for (int i = 1; i < XkbNumKbdGroups; i++)
        {
            key->numLevels[i] = 0;
            free(key->syms[i]);
            key->syms[i] = (KeySym *) NULL;
            free(key->acts[i]);
            key->acts[i] = (XkbAction *) NULL;
            key->types[i] = (Atom) 0;
        }
    }
    key->typesDefined = key->symsDefined = key->actsDefined = 1U << group;

    key->numLevels[group] = key->numLevels[0];
    key->numLevels[0] = 0;
    key->syms[group] = key->syms[0];
    key->syms[0] = (KeySym *) NULL;
    key->acts[group] = key->acts[0];
    key->acts[0] = (XkbAction *) NULL;
    key->types[group] = key->types[0];
    key->types[0] = (Atom) 0;
    return True;
}

static int
HandleSymbolsDef(SymbolsDef * stmt,
                 XkbDescPtr xkb, unsigned merge, SymbolsInfo * info)
{
    KeyInfo key;

    InitKeyInfo(&key);
    CopyKeyInfo(&info->dflt, &key, False);
    key.defs.merge = stmt->merge;
    key.name = KeyNameToLong(stmt->keyName);
    if (!HandleSymbolsBody((VarDef *) stmt->symbols, xkb, &key, info))
    {
        info->errorCount++;
        return False;
    }

    if (!SetExplicitGroup(info, &key))
    {
        info->errorCount++;
        return False;
    }

    if (!AddKeySymbols(info, &key, xkb))
    {
        info->errorCount++;
        return False;
    }
    return True;
}

static Bool
HandleModMapDef(ModMapDef * def,
                XkbDescPtr xkb, unsigned merge, SymbolsInfo * info)
{
    ModMapEntry tmp;
    ExprResult rtrn;
    Bool ok;

    if (!LookupModIndex(NULL, None, def->modifier, TypeInt, &rtrn))
    {
        ERROR("Illegal modifier map definition\n");
        ACTION("Ignoring map for non-modifier \"%s\"\n",
                XkbAtomText(NULL, def->modifier, XkbMessage));
        return False;
    }
    ok = True;
    tmp.modifier = rtrn.uval;
    for (ExprDef *key = def->keys; key != NULL;
         key = (ExprDef *) key->common.next)
    {
        if ((key->op == ExprValue) && (key->type == TypeKeyName))
        {
            tmp.haveSymbol = False;
            tmp.u.keyName = KeyNameToLong(key->value.keyName);
        }
        else if (ExprResolveKeySym(key, &rtrn, NULL, NULL))
        {
            tmp.haveSymbol = True;
            tmp.u.keySym = rtrn.uval;
        }
        else
        {
            ERROR("Modmap entries may contain only key names or keysyms\n");
            ACTION("Illegal definition for %s modifier ignored\n",
                    XkbModIndexText(tmp.modifier, XkbMessage));
            continue;
        }

        ok = AddModMapEntry(info, &tmp) && ok;
    }
    return ok;
}

static void
HandleSymbolsFile(XkbFile * file,
                  XkbDescPtr xkb, unsigned merge, SymbolsInfo * info)
{
    ParseCommon *stmt;

    info->name = uStringDup(file->name);
    stmt = file->defs;
    while (stmt)
    {
        switch (stmt->stmtType)
        {
        case StmtInclude:
            if (!HandleIncludeSymbols((IncludeStmt *) stmt, xkb, info,
                                      HandleSymbolsFile))
                info->errorCount++;
            break;
        case StmtSymbolsDef:
            if (!HandleSymbolsDef((SymbolsDef *) stmt, xkb, merge, info))
                info->errorCount++;
            break;
        case StmtVarDef:
            if (!HandleSymbolsVar((VarDef *) stmt, xkb, info))
                info->errorCount++;
            break;
        case StmtVModDef:
            if (!HandleVModDef((VModDef *) stmt, merge, &info->vmods))
                info->errorCount++;
            break;
        case StmtInterpDef:
            ERROR("Interpretation files may not include other types\n");
            ACTION("Ignoring definition of symbol interpretation\n");
            info->errorCount++;
            break;
        case StmtKeycodeDef:
            ERROR("Interpretation files may not include other types\n");
            ACTION("Ignoring definition of key name\n");
            info->errorCount++;
            break;
        case StmtModMapDef:
            if (!HandleModMapDef((ModMapDef *) stmt, xkb, merge, info))
                info->errorCount++;
            break;
        default:
            WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
                  stmt->stmtType);
            break;
        }
        stmt = stmt->next;
        if (info->errorCount > 10)
        {
#ifdef NOISY
            ERROR("Too many errors\n");
#endif
            ACTION("Abandoning symbols file \"%s\"\n", file->topName);
            break;
        }
    }
    return;
}

static Bool
FindKeyForSymbol(XkbDescPtr xkb, KeySym sym, unsigned int *kc_rtrn)
{
    int j;
    Bool gotOne;

    j = 0;
    do
    {
        gotOne = False;
        for (int i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++)
        {
            if (j < (int) XkbKeyNumSyms(xkb, i))
            {
                gotOne = True;
                if (XkbKeySym(xkb, i, j) == sym)
                {
                    *kc_rtrn = i;
                    return True;
                }
            }
        }
        j++;
    }
    while (gotOne);
    return False;
}

/**
 * Find the given name in the xkb->map->types and return its index.
 *
 * @param name The atom to search for.
 * @param type_rtrn Set to the index of the name if found.
 *
 * @return True if found, False otherwise.
 */
static Bool
FindNamedType(const XkbDescPtr xkb, Atom name, unsigned *type_rtrn)
{
    if (xkb && xkb->map && xkb->map->types)
    {
        for (unsigned n = 0; n < xkb->map->num_types; n++)
        {
            if (xkb->map->types[n].name == (Atom) name)
            {
                *type_rtrn = n;
                return True;
            }
        }
    }
    return False;
}

static Bool
KSIsLower(KeySym ks)
{
    KeySym lower, upper;
    XConvertCase(ks, &lower, &upper);

    if (lower == upper)
        return False;
    return (ks == lower ? True : False);
}

static Bool
KSIsUpper(KeySym ks)
{
    KeySym lower, upper;
    XConvertCase(ks, &lower, &upper);

    if (lower == upper)
        return False;
    return (ks == upper ? True : False);
}

/**
 * Assign a type to the given sym and return the Atom for the type assigned.
 *
 * Simple recipe:
 * - ONE_LEVEL for width 0/1
 * - ALPHABETIC for 2 shift levels, with lower/uppercase
 * - KEYPAD for keypad keys.
 * - TWO_LEVEL for other 2 shift level keys.
 * and the same for four level keys.
 *
 * @param width Number of syms in syms.
 * @param syms The keysyms for the given key (must be size width).
 * @param typeNameRtrn Set to the Atom of the type name.
 *
 * @returns True if a type could be found, False otherwise.
 */
static Bool
FindAutomaticType(int width, const KeySym *syms,
                  Atom *typeNameRtrn, Bool *autoType)
{
    *autoType = False;
    if ((width == 1) || (width == 0))
    {
        *typeNameRtrn = XkbInternAtom(NULL, "ONE_LEVEL", False);
        *autoType = True;
    }
    else if (width == 2)
    {
        if (syms && KSIsLower(syms[0]) && KSIsUpper(syms[1]))
        {
            *typeNameRtrn = XkbInternAtom(NULL, "ALPHABETIC", False);
        }
        else if (syms && (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1])))
        {
            *typeNameRtrn = XkbInternAtom(NULL, "KEYPAD", False);
            *autoType = True;
        }
        else
        {
            *typeNameRtrn = XkbInternAtom(NULL, "TWO_LEVEL", False);
            *autoType = True;
        }
    }
    else if (width <= 4)
    {
        if (syms && KSIsLower(syms[0]) && KSIsUpper(syms[1]))
            if (KSIsLower(syms[2]) && KSIsUpper(syms[3]))
                *typeNameRtrn =
                    XkbInternAtom(NULL, "FOUR_LEVEL_ALPHABETIC", False);
            else
                *typeNameRtrn = XkbInternAtom(NULL,
                                              "FOUR_LEVEL_SEMIALPHABETIC",
                                              False);

        else if (syms && (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1])))
            *typeNameRtrn = XkbInternAtom(NULL, "FOUR_LEVEL_KEYPAD", False);
        else
            *typeNameRtrn = XkbInternAtom(NULL, "FOUR_LEVEL", False);
        /* XXX: why not set autoType here? */
    }
    return ((width >= 0) && (width <= 4));
}

/**
 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
 * groups, and reduce to one group if all groups are identical anyway.
 */
static void
PrepareKeyDef(KeyInfo * key)
{
    int i, defined, lastGroup;
    Bool identical;

    defined = key->symsDefined | key->actsDefined | key->typesDefined;
    /* get highest group number */
    for (i = XkbNumKbdGroups - 1; i >= 0; i--)
    {
        if (defined & (1U << i))
            break;
    }
    lastGroup = i;

    if (lastGroup == 0)
        return;

    /* If there are empty groups between non-empty ones fill them with data */
    /* from the first group. */
    /* We can make a wrong assumption here. But leaving gaps is worse. */
    for (i = lastGroup; i > 0; i--)
    {
        int width;

        if (defined & (1U << i))
            continue;
        width = key->numLevels[0];
        if (key->typesDefined & 1)
        {
            for (int j = 0; j < width; j++)
            {
                key->types[i] = key->types[0];
            }
            key->typesDefined |= 1U << i;
        }
        if ((key->actsDefined & 1) && key->acts[0])
        {
            key->acts[i] = calloc(width, sizeof(XkbAction));
            if (key->acts[i] == NULL)
                continue;
            memcpy(key->acts[i], key->acts[0], width * sizeof(XkbAction));
            key->actsDefined |= 1U << i;
        }
        if ((key->symsDefined & 1) && key->syms[0])
        {
            key->syms[i] = calloc(width, sizeof(KeySym));
            if (key->syms[i] == NULL)
                continue;
            memcpy(key->syms[i], key->syms[0], width * sizeof(KeySym));
            key->symsDefined |= 1U << i;
        }
        if (defined & 1)
        {
            key->numLevels[i] = key->numLevels[0];
        }
    }
    /* If all groups are completely identical remove them all */
    /* except the first one. */
    identical = True;
    for (i = lastGroup; i > 0; i--)
    {
        if ((key->numLevels[i] != key->numLevels[0]) ||
            (key->types[i] != key->types[0]))
        {
            identical = False;
            break;
        }
        if ((key->syms[i] != key->syms[0]) &&
            (key->syms[i] == NULL || key->syms[0] == NULL ||
             memcmp((void *) key->syms[i], (void *) key->syms[0],
                    sizeof(KeySym) * key->numLevels[0])))
        {
            identical = False;
            break;
        }
        if ((key->acts[i] != key->acts[0]) &&
            (key->acts[i] == NULL || key->acts[0] == NULL ||
             memcmp((void *) key->acts[i], (void *) key->acts[0],
                    sizeof(XkbAction) * key->numLevels[0])))
        {
            identical = False;
            break;
        }
    }
    if (identical)
    {
        for (i = lastGroup; i > 0; i--)
        {
            key->numLevels[i] = 0;
            free(key->syms[i]);
            key->syms[i] = (KeySym *) NULL;
            free(key->acts[i]);
            key->acts[i] = (XkbAction *) NULL;
            key->types[i] = (Atom) 0;
        }
        key->symsDefined &= 1;
        key->actsDefined &= 1;
        key->typesDefined &= 1;
    }
    return;
}

/**
 * Copy the KeyInfo into result.
 *
 * This function recurses.
 */
static Bool
CopySymbolsDef(XkbFileInfo * result, KeyInfo * key, int start_from)
{
    int i;
    unsigned kc, width, nGroups;
    XkbKeyTypePtr type;
    Bool haveActions, autoType, useAlias;
    KeySym *outSyms;
    XkbAction *outActs;
    XkbDescPtr xkb;
    unsigned types[XkbNumKbdGroups];

    xkb = result->xkb;
    useAlias = (start_from == 0);

    /* get the keycode for the key. */
    if (!FindNamedKey(xkb, key->name, &kc, useAlias, CreateKeyNames(xkb),
                      start_from))
    {
        if ((start_from == 0) && (warningLevel >= 5))
        {
            WARN("Key %s not found in %s keycodes\n",
                  longText(key->name, XkbMessage),
                  XkbAtomText(NULL, xkb->names->keycodes, XkbMessage));
            ACTION("Symbols ignored\n");
        }
        return False;
    }

    haveActions = False;
    for (i = width = nGroups = 0; i < XkbNumKbdGroups; i++)
    {
        if (((i + 1) > nGroups)
            && (((key->symsDefined | key->actsDefined) & (1U << i))
                || (key->typesDefined) & (1U << i)))
            nGroups = i + 1;
        if (key->acts[i])
            haveActions = True;
        autoType = False;
        /* Assign the type to the key, if it is missing. */
        if (key->types[i] == None)
        {
            if (key->dfltType != None)
                key->types[i] = key->dfltType;
            else if (FindAutomaticType(key->numLevels[i], key->syms[i],
                                       &key->types[i], &autoType))
            {
            }
            else
            {
                if (warningLevel >= 5)
                {
                    WARN("No automatic type for %d symbols\n",
                          (unsigned int) key->numLevels[i]);
                    ACTION("Using %s for the %s key (keycode %d)\n",
                            XkbAtomText(NULL, key->types[i],
                                        XkbMessage),
                            longText(key->name, XkbMessage), kc);
                }
            }
        }
        if (FindNamedType(xkb, key->types[i], &types[i]))
        {
            if (!autoType || key->numLevels[i] > 2)
                xkb->server->explicit[kc] |= (1U << i);
        }
        else
        {
            if (warningLevel >= 3)
            {
                WARN("Type \"%s\" is not defined\n",
                      XkbAtomText(NULL, key->types[i], XkbMessage));
                ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
                        longText(key->name, XkbMessage), kc);
            }
            types[i] = XkbTwoLevelIndex;
        }
        /* if the type specifies less syms than the key has, shrink the key */
        type = &xkb->map->types[types[i]];
        if (type->num_levels < key->numLevels[i])
        {
            if (warningLevel > 5)
            {
                WARN
                    ("Type \"%s\" has %d levels, but %s has %d symbols\n",
                     XkbAtomText(NULL, type->name, XkbMessage),
                     (unsigned int) type->num_levels,
                     longText(key->name, XkbMessage),
                     (unsigned int) key->numLevels[i]);
                ACTION("Ignoring extra symbols\n");
            }
            key->numLevels[i] = type->num_levels;
        }
        if (key->numLevels[i] > width)
            width = key->numLevels[i];
        if (type->num_levels > width)
            width = type->num_levels;
    }

    /* width is now the largest width found */

    i = width * nGroups;
    outSyms = XkbResizeKeySyms(xkb, kc, i);
    if (outSyms == NULL)
    {
        WSGO("Could not enlarge symbols for %s (keycode %d)\n",
              longText(key->name, XkbMessage), kc);
        return False;
    }
    if (haveActions)
    {
        outActs = XkbResizeKeyActions(xkb, kc, i);
        if (outActs == NULL)
        {
            WSGO("Could not enlarge actions for %s (key %d)\n",
                  longText(key->name, XkbMessage), kc);
            return False;
        }
        xkb->server->explicit[kc] |= XkbExplicitInterpretMask;
    }
    else
        outActs = NULL;
    if (key->defs.defined & _Key_GroupInfo)
        i = key->groupInfo;
    else
        i = xkb->map->key_sym_map[kc].group_info;

    xkb->map->key_sym_map[kc].group_info = XkbSetNumGroups(i, nGroups);
    xkb->map->key_sym_map[kc].width = width;
    for (i = 0; i < nGroups; i++)
    {
        /* assign kt_index[i] to the index of the type in map->types.
         * kt_index[i] may have been set by a previous run (if we have two
         * layouts specified). Let's not overwrite it with the ONE_LEVEL
         * default group if we don't even have keys for this group anyway.
         *
         * FIXME: There should be a better fix for this.
         */
        if (key->numLevels[i])
            xkb->map->key_sym_map[kc].kt_index[i] = types[i];
        if (key->syms[i] != NULL)
        {
            /* fill key to "width" symbols*/
            for (unsigned tmp = 0; tmp < width; tmp++)
            {
                if (tmp < key->numLevels[i])
                    outSyms[tmp] = key->syms[i][tmp];
                else
                    outSyms[tmp] = NoSymbol;
                if ((outActs != NULL) && (key->acts[i] != NULL))
                {
                    if (tmp < key->numLevels[i])
                        outActs[tmp] = key->acts[i][tmp];
                    else
                        outActs[tmp].type = XkbSA_NoAction;
                }
            }
        }
        outSyms += width;
        if (outActs)
            outActs += width;
    }
    switch (key->behavior.type & XkbKB_OpMask)
    {
    case XkbKB_Default:
        break;
    case XkbKB_Overlay1:
    case XkbKB_Overlay2:
    {
        unsigned okc;
        /* find key by name! */
        if (!FindNamedKey(xkb, key->nameForOverlayKey, &okc, True,
                          CreateKeyNames(xkb), 0))
        {
            if (warningLevel >= 1)
            {
                WARN("Key %s not found in %s keycodes\n",
                      longText(key->nameForOverlayKey, XkbMessage),
                      XkbAtomText(NULL, xkb->names->keycodes, XkbMessage));
                ACTION("Not treating %s as an overlay key \n",
                        longText(key->name, XkbMessage));
            }
            break;
        }
        key->behavior.data = okc;
    }
    default:
        xkb->server->behaviors[kc] = key->behavior;
        xkb->server->explicit[kc] |= XkbExplicitBehaviorMask;
        break;
    }
    if (key->defs.defined & _Key_VModMap)
    {
        xkb->server->vmodmap[kc] = key->vmodmap;
        xkb->server->explicit[kc] |= XkbExplicitVModMapMask;
    }
    if (key->repeat != RepeatUndefined)
    {
        if (key->repeat == RepeatYes)
            xkb->ctrls->per_key_repeat[kc / 8] |= (1U << (kc % 8));
        else
            xkb->ctrls->per_key_repeat[kc / 8] &= ~(1U << (kc % 8));
        xkb->server->explicit[kc] |= XkbExplicitAutoRepeatMask;
    }

    /* do the same thing for the next key */
    CopySymbolsDef(result, key, kc + 1);
    return True;
}

static Bool
CopyModMapDef(XkbFileInfo * result, ModMapEntry * entry)
{
    unsigned kc;
    XkbDescPtr xkb;

    xkb = result->xkb;
    if ((!entry->haveSymbol)
        &&
        (!FindNamedKey
         (xkb, entry->u.keyName, &kc, True, CreateKeyNames(xkb), 0)))
    {
        if (warningLevel >= 5)
        {
            WARN("Key %s not found in %s keycodes\n",
                  longText(entry->u.keyName, XkbMessage),
                  XkbAtomText(NULL, xkb->names->keycodes, XkbMessage));
            ACTION("Modifier map entry for %s not updated\n",
                    XkbModIndexText(entry->modifier, XkbMessage));
        }
        return False;
    }
    else if (entry->haveSymbol
             && (!FindKeyForSymbol(xkb, entry->u.keySym, &kc)))
    {
        if (warningLevel > 5)
        {
            WARN("Key \"%s\" not found in %s symbol map\n",
                  XkbKeysymText(entry->u.keySym, XkbMessage),
                  XkbAtomText(NULL, xkb->names->symbols, XkbMessage));
            ACTION("Modifier map entry for %s not updated\n",
                    XkbModIndexText(entry->modifier, XkbMessage));
        }
        return False;
    }
    xkb->map->modmap[kc] |= (1U << entry->modifier);
    return True;
}

/**
 * Handle the xkb_symbols section of an xkb file.
 *
 * @param file The parsed xkb_symbols section of the xkb file.
 * @param result Handle to the data to store the result in.
 * @param merge Merge strategy (e.g. MergeOverride).
 */
Bool
CompileSymbols(XkbFile * file, XkbFileInfo * result, unsigned merge)
{
    SymbolsInfo info;
    XkbDescPtr xkb;

    xkb = result->xkb;
    InitSymbolsInfo(&info, xkb);
    info.dflt.defs.fileID = file->id;
    info.dflt.defs.merge = merge;
    HandleSymbolsFile(file, xkb, merge, &info);

    if (info.nKeys == 0)
        return True;
    if (info.errorCount == 0)
    {
        int i;
        KeyInfo *key;

        /* alloc memory in the xkb struct */
        if (XkbAllocNames(xkb, XkbSymbolsNameMask | XkbGroupNamesMask, 0, 0)
            != Success)
        {
            WSGO("Can not allocate names in CompileSymbols\n");
            ACTION("Symbols not added\n");
            return False;
        }
        if (XkbAllocClientMap(xkb, XkbKeySymsMask | XkbModifierMapMask, 0)
            != Success)
        {
            WSGO("Could not allocate client map in CompileSymbols\n");
            ACTION("Symbols not added\n");
            return False;
        }
        if (XkbAllocServerMap(xkb, XkbAllServerInfoMask, 32) != Success)
        {
            WSGO("Could not allocate server map in CompileSymbols\n");
            ACTION("Symbols not added\n");
            return False;
        }
        if (XkbAllocControls(xkb, XkbPerKeyRepeatMask) != Success)
        {
            WSGO("Could not allocate controls in CompileSymbols\n");
            ACTION("Symbols not added\n");
            return False;
        }

        /* now copy info into xkb. */
        xkb->names->symbols = XkbInternAtom(xkb->dpy, info.name, False);
        if (info.aliases)
            ApplyAliases(xkb, False, &info.aliases);
        for (i = 0; i < XkbNumKbdGroups; i++)
        {
            if (info.groupNames[i] != None)
                xkb->names->groups[i] = info.groupNames[i];
        }
        /* sanitize keys */
        for (key = info.keys, i = 0; i < info.nKeys; i++, key++)
        {
            PrepareKeyDef(key);
        }
        /* copy! */
        for (key = info.keys, i = 0; i < info.nKeys; i++, key++)
        {
            if (!CopySymbolsDef(result, key, 0))
                info.errorCount++;
        }
        if (warningLevel > 3)
        {
            for (i = xkb->min_key_code; i <= xkb->max_key_code; i++)
            {
                if (xkb->names->keys[i].name[0] == '\0')
                    continue;
                if (XkbKeyNumGroups(xkb, i) < 1)
                {
                    char buf[5];
                    memcpy(buf, xkb->names->keys[i].name, 4);
                    buf[4] = '\0';
                    INFO("No symbols defined for <%s> (keycode %d)\n",
                         buf, i);
                }
            }
        }
        if (info.modMap)
        {
            ModMapEntry *mm, *next;
            for (mm = info.modMap; mm != NULL; mm = next)
            {
                if (!CopyModMapDef(result, mm))
                    info.errorCount++;
                next = (ModMapEntry *) mm->defs.next;
            }
        }
        return True;
    }
    return False;
}