aboutsummaryrefslogtreecommitdiff
path: root/locale/programs/ld-ctype.c
blob: ffc759b272f689edce7ca12d8859503b9eee4369 (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
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
/* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <alloca.h>
#include <byteswap.h>
#include <endian.h>
#include <errno.h>
#include <limits.h>
#include <obstack.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <sys/uio.h>

#include "charmap.h"
#include "localeinfo.h"
#include "langinfo.h"
#include "linereader.h"
#include "locfile-token.h"
#include "locfile.h"
#include "localedef.h"

#include <assert.h>


/* These are the extra bits not in wctype.h since these are not preallocated
   classes.  */
#define _ISwspecial1	(1 << 29)
#define _ISwspecial2	(1 << 30)
#define _ISwspecial3	(1 << 31)


/* The bit used for representing a special class.  */
#define BITPOS(class) ((class) - tok_upper)
#define BIT(class) (_ISbit (BITPOS (class)))
#define BITw(class) (_ISwbit (BITPOS (class)))

#define ELEM(ctype, collection, idx, value)				      \
  *find_idx (ctype, &ctype->collection idx, &ctype->collection##_max idx,     \
	     &ctype->collection##_act idx, value)


/* To be compatible with former implementations we for now restrict
   the number of bits for character classes to 16.  When compatibility
   is not necessary anymore increase the number to 32.  */
#define char_class_t uint16_t
#define char_class32_t uint32_t


/* Type to describe a transliteration action.  We have a possibly
   multiple character from-string and a set of multiple character
   to-strings.  All are 32bit values since this is what is used in
   the gconv functions.  */
struct translit_to_t
{
  uint32_t *str;

  struct translit_to_t *next;
};

struct translit_t
{
  uint32_t *from;

  struct translit_to_t *to;

  struct translit_t *next;
};


/* The real definition of the struct for the LC_CTYPE locale.  */
struct locale_ctype_t
{
  uint32_t *charnames;
  size_t charnames_max;
  size_t charnames_act;

  struct repertoire_t *repertoire;

  /* We will allow up to 8 * sizeof (uint32_t) character classes.  */
#define MAX_NR_CHARCLASS (8 * sizeof (uint32_t))
  size_t nr_charclass;
  const char *classnames[MAX_NR_CHARCLASS];
  uint32_t last_class_char;
  uint32_t class256_collection[256];
  uint32_t *class_collection;
  size_t class_collection_max;
  size_t class_collection_act;
  uint32_t class_done;

  struct charseq **mbdigits;
  size_t mbdigits_act;
  size_t mbdigits_max;
  uint32_t *wcdigits;
  size_t wcdigits_act;
  size_t wcdigits_max;

  struct charseq *mboutdigits[10];
  uint32_t wcoutdigits[10];
  size_t outdigits_act;

  /* If the following number ever turns out to be too small simply
     increase it.  But I doubt it will.  --drepper@gnu */
#define MAX_NR_CHARMAP 16
  const char *mapnames[MAX_NR_CHARMAP];
  uint32_t *map_collection[MAX_NR_CHARMAP];
  uint32_t map256_collection[2][256];
  size_t map_collection_max[MAX_NR_CHARMAP];
  size_t map_collection_act[MAX_NR_CHARMAP];
  size_t map_collection_nr;
  size_t last_map_idx;
  int tomap_done[MAX_NR_CHARMAP];

  /* Transliteration information.  */
  const char *translit_copy_locale;
  const char *translit_copy_repertoire;
  struct translit_t *translit;

  /* The arrays for the binary representation.  */
  uint32_t plane_size;
  uint32_t plane_cnt;
  char_class_t *ctype_b;
  char_class32_t *ctype32_b;
  uint32_t *names;
  uint32_t **map;
  uint32_t *class_name_ptr;
  uint32_t *map_name_ptr;
  unsigned char *width;
  uint32_t mb_cur_max;
  const char *codeset_name;
  uint32_t translit_hash_size;
  uint32_t translit_hash_layers;
  uint32_t *translit_from_idx;
  uint32_t *translit_from_tbl;
  uint32_t *translit_to_idx;
  uint32_t *translit_to_tbl;
  size_t translit_idx_size;
  size_t translit_from_tbl_size;
  size_t translit_to_tbl_size;

  struct obstack mem_pool;
};


#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free


/* Prototypes for local functions.  */
static void ctype_startup (struct linereader *lr, struct localedef_t *locale,
			   struct charmap_t *charmap, int ignore_content);
static void ctype_class_new (struct linereader *lr,
			     struct locale_ctype_t *ctype, const char *name);
static void ctype_map_new (struct linereader *lr,
			   struct locale_ctype_t *ctype,
			   const char *name, struct charmap_t *charmap);
static uint32_t *find_idx (struct locale_ctype_t *ctype, uint32_t **table,
			   size_t *max, size_t *act, unsigned int idx);
static void set_class_defaults (struct locale_ctype_t *ctype,
				struct charmap_t *charmap,
				struct repertoire_t *repertoire);
static void allocate_arrays (struct locale_ctype_t *ctype,
			     struct charmap_t *charmap,
			     struct repertoire_t *repertoire);


static const char *longnames[] =
{
  "zero", "one", "two", "three", "four",
  "five", "six", "seven", "eight", "nine"
};
static const unsigned char digits[] = "0123456789";


static void
ctype_startup (struct linereader *lr, struct localedef_t *locale,
	       struct charmap_t *charmap, int ignore_content)
{
  unsigned int cnt;
  struct locale_ctype_t *ctype;

  if (!ignore_content)
    {
      /* Allocate the needed room.  */
      locale->categories[LC_CTYPE].ctype = ctype =
	(struct locale_ctype_t *) xcalloc (1, sizeof (struct locale_ctype_t));

      /* We have seen no names yet.  */
      ctype->charnames_max = charmap->mb_cur_max == 1 ? 256 : 512;
      ctype->charnames =
	(unsigned int *) xmalloc (ctype->charnames_max
				  * sizeof (unsigned int));
      for (cnt = 0; cnt < 256; ++cnt)
	ctype->charnames[cnt] = cnt;
      ctype->charnames_act = 256;

      /* Fill character class information.  */
      ctype->last_class_char = ILLEGAL_CHAR_VALUE;
      /* The order of the following instructions determines the bit
	 positions!  */
      ctype_class_new (lr, ctype, "upper");
      ctype_class_new (lr, ctype, "lower");
      ctype_class_new (lr, ctype, "alpha");
      ctype_class_new (lr, ctype, "digit");
      ctype_class_new (lr, ctype, "xdigit");
      ctype_class_new (lr, ctype, "space");
      ctype_class_new (lr, ctype, "print");
      ctype_class_new (lr, ctype, "graph");
      ctype_class_new (lr, ctype, "blank");
      ctype_class_new (lr, ctype, "cntrl");
      ctype_class_new (lr, ctype, "punct");
      ctype_class_new (lr, ctype, "alnum");
      /* The following are extensions from ISO 14652.  */
      ctype_class_new (lr, ctype, "left_to_right");
      ctype_class_new (lr, ctype, "right_to_left");
      ctype_class_new (lr, ctype, "num_terminator");
      ctype_class_new (lr, ctype, "num_separator");
      ctype_class_new (lr, ctype, "segment_separator");
      ctype_class_new (lr, ctype, "block_separator");
      ctype_class_new (lr, ctype, "direction_control");
      ctype_class_new (lr, ctype, "sym_swap_layout");
      ctype_class_new (lr, ctype, "char_shape_selector");
      ctype_class_new (lr, ctype, "num_shape_selector");
      ctype_class_new (lr, ctype, "non_spacing");
      ctype_class_new (lr, ctype, "non_spacing_level3");
      ctype_class_new (lr, ctype, "normal_connect");
      ctype_class_new (lr, ctype, "r_connect");
      ctype_class_new (lr, ctype, "no_connect");
      ctype_class_new (lr, ctype, "no_connect-space");
      ctype_class_new (lr, ctype, "vowel_connect");

      ctype->class_collection_max = charmap->mb_cur_max == 1 ? 256 : 512;
      ctype->class_collection
	= (uint32_t *) xcalloc (sizeof (unsigned long int),
				ctype->class_collection_max);
      ctype->class_collection_act = 256;

      /* Fill character map information.  */
      ctype->map_collection_nr = 0;
      ctype->last_map_idx = MAX_NR_CHARMAP;
      ctype_map_new (lr, ctype, "toupper", charmap);
      ctype_map_new (lr, ctype, "tolower", charmap);
      ctype_map_new (lr, ctype, "tosymmetric", charmap);

      /* Fill first 256 entries in `toXXX' arrays.  */
      for (cnt = 0; cnt < 256; ++cnt)
	{
	  ctype->map_collection[0][cnt] = cnt;
	  ctype->map_collection[1][cnt] = cnt;
	  ctype->map_collection[2][cnt] = cnt;
	  ctype->map256_collection[0][cnt] = cnt;
	  ctype->map256_collection[1][cnt] = cnt;
	}

      obstack_init (&ctype->mem_pool);
    }
}


void
ctype_finish (struct localedef_t *locale, struct charmap_t *charmap)
{
  /* See POSIX.2, table 2-6 for the meaning of the following table.  */
#define NCLASS 12
  static const struct
  {
    const char *name;
    const char allow[NCLASS];
  }
  valid_table[NCLASS] =
  {
    /* The order is important.  See token.h for more information.
       M = Always, D = Default, - = Permitted, X = Mutually exclusive  */
    { "upper",  "--MX-XDDXXX-" },
    { "lower",  "--MX-XDDXXX-" },
    { "alpha",  "---X-XDDXXX-" },
    { "digit",  "XXX--XDDXXX-" },
    { "xdigit", "-----XDDXXX-" },
    { "space",  "XXXXX------X" },
    { "print",  "---------X--" },
    { "graph",  "---------X--" },
    { "blank",  "XXXXXM-----X" },
    { "cntrl",  "XXXXX-XX--XX" },
    { "punct",  "XXXXX-DD-X-X" },
    { "alnum",  "-----XDDXXX-" }
  };
  size_t cnt;
  int cls1, cls2;
  uint32_t space_value;
  struct charseq *space_seq;
  struct locale_ctype_t *ctype = locale->categories[LC_CTYPE].ctype;
  int warned;

  /* Now resolve copying and also handle completely missing definitions.  */
  if (ctype == NULL)
    {
      /* First see whether we were supposed to copy.  If yes, find the
	 actual definition.  */
      if (locale->copy_name[LC_CTYPE] != NULL)
	{
	  /* Find the copying locale.  This has to happen transitively since
	     the locale we are copying from might also copying another one.  */
	  struct localedef_t *from = locale;

	  do
	    from = find_locale (LC_CTYPE, from->copy_name[LC_CTYPE],
				from->repertoire_name, charmap);
	  while (from->categories[LC_CTYPE].ctype == NULL
		 && from->copy_name[LC_CTYPE] != NULL);

	  ctype = locale->categories[LC_CTYPE].ctype
	    = from->categories[LC_CTYPE].ctype;
	}

      /* If there is still no definition issue an warning and create an
	 empty one.  */
      if (ctype == NULL)
	{
	  error (0, 0, _("No definition for %s category found"), "LC_CTYPE");
	  ctype_startup (NULL, locale, charmap, 0);
	  ctype = locale->categories[LC_CTYPE].ctype;
	}
    }

  /* Set default value for classes not specified.  */
  set_class_defaults (ctype, charmap, ctype->repertoire);

  /* Check according to table.  */
  for (cnt = 0; cnt < ctype->class_collection_act; ++cnt)
    {
      uint32_t tmp = ctype->class_collection[cnt];

      if (tmp != 0)
	{
	  for (cls1 = 0; cls1 < NCLASS; ++cls1)
	    if ((tmp & _ISwbit (cls1)) != 0)
	      for (cls2 = 0; cls2 < NCLASS; ++cls2)
		if (valid_table[cls1].allow[cls2] != '-')
		  {
		    int eq = (tmp & _ISwbit (cls2)) != 0;
		    switch (valid_table[cls1].allow[cls2])
		      {
		      case 'M':
			if (!eq)
			  {
			    uint32_t value = ctype->charnames[cnt];

			    if (!be_quiet)
			      error (0, 0, _("\
character L'\\u%0*x' in class `%s' must be in class `%s'"),
				     value > 0xffff ? 8 : 4, value,
				     valid_table[cls1].name,
				     valid_table[cls2].name);
			  }
			break;

		      case 'X':
			if (eq)
			  {
			    uint32_t value = ctype->charnames[cnt];

			    if (!be_quiet)
			      error (0, 0, _("\
character L'\\u%0*x' in class `%s' must not be in class `%s'"),
				     value > 0xffff ? 8 : 4, value,
				     valid_table[cls1].name,
				     valid_table[cls2].name);
			  }
			break;

		      case 'D':
			ctype->class_collection[cnt] |= _ISwbit (cls2);
			break;

		      default:
			error (5, 0, _("internal error in %s, line %u"),
			       __FUNCTION__, __LINE__);
		      }
		  }
	}
    }

  for (cnt = 0; cnt < 256; ++cnt)
    {
      uint32_t tmp = ctype->class256_collection[cnt];

      if (tmp != 0)
	{
	  for (cls1 = 0; cls1 < NCLASS; ++cls1)
	    if ((tmp & _ISbit (cls1)) != 0)
	      for (cls2 = 0; cls2 < NCLASS; ++cls2)
		if (valid_table[cls1].allow[cls2] != '-')
		  {
		    int eq = (tmp & _ISbit (cls2)) != 0;
		    switch (valid_table[cls1].allow[cls2])
		      {
		      case 'M':
			if (!eq)
			  {
			    char buf[17];

			    sprintf (buf, "\\%o", cnt);

			    if (!be_quiet)
			      error (0, 0, _("\
character '%s' in class `%s' must be in class `%s'"),
				     buf, valid_table[cls1].name,
				     valid_table[cls2].name);
			  }
			break;

		      case 'X':
			if (eq)
			  {
			    char buf[17];

			    sprintf (buf, "\\%o", cnt);

			    if (!be_quiet)
			      error (0, 0, _("\
character '%s' in class `%s' must not be in class `%s'"),
				     buf, valid_table[cls1].name,
				     valid_table[cls2].name);
			  }
			break;

		      case 'D':
			ctype->class256_collection[cnt] |= _ISbit (cls2);
			break;

		      default:
			error (5, 0, _("internal error in %s, line %u"),
			       __FUNCTION__, __LINE__);
		      }
		  }
	}
    }

  /* ... and now test <SP> as a special case.  */
  space_value = repertoire_find_value (ctype->repertoire, "SP", 2);
  if (space_value == ILLEGAL_CHAR_VALUE)
    {
      if (!be_quiet)
	error (0, 0, _("character <SP> not defined in character map"));
    }
  else if (((cnt = BITPOS (tok_space),
	     (ELEM (ctype, class_collection, , space_value)
	      & BITw (tok_space)) == 0)
	    || (cnt = BITPOS (tok_blank),
		(ELEM (ctype, class_collection, , space_value)
		 & BITw (tok_blank)) == 0)))
    {
      if (!be_quiet)
	error (0, 0, _("<SP> character not in class `%s'"),
	       valid_table[cnt].name);
    }
  else if (((cnt = BITPOS (tok_punct),
	     (ELEM (ctype, class_collection, , space_value)
	      & BITw (tok_punct)) != 0)
	    || (cnt = BITPOS (tok_graph),
		(ELEM (ctype, class_collection, , space_value)
		 & BITw (tok_graph))
		!= 0)))
    {
      if (!be_quiet)
	error (0, 0, _("<SP> character must not be in class `%s'"),
	       valid_table[cnt].name);
    }
  else
    ELEM (ctype, class_collection, , space_value) |= BITw (tok_print);

  space_seq = charmap_find_value (charmap, "SP", 2);
  if (space_seq == NULL || space_seq->nbytes != 1)
    {
      if (!be_quiet)
	error (0, 0, _("character <SP> not defined in character map"));
    }
  else if (((cnt = BITPOS (tok_space),
	     (ctype->class256_collection[space_seq->bytes[0]]
	      & BIT (tok_space)) == 0)
	    || (cnt = BITPOS (tok_blank),
		(ctype->class256_collection[space_seq->bytes[0]]
		 & BIT (tok_blank)) == 0)))
    {
      if (!be_quiet)
	error (0, 0, _("<SP> character not in class `%s'"),
	       valid_table[cnt].name);
    }
  else if (((cnt = BITPOS (tok_punct),
	     (ctype->class256_collection[space_seq->bytes[0]]
	      & BIT (tok_punct)) != 0)
	    || (cnt = BITPOS (tok_graph),
		(ctype->class256_collection[space_seq->bytes[0]]
		 & BIT (tok_graph)) != 0)))
    {
      if (!be_quiet)
	error (0, 0, _("<SP> character must not be in class `%s'"),
	       valid_table[cnt].name);
    }
  else
    ctype->class256_collection[space_seq->bytes[0]] |= BIT (tok_print);

  /* Now that the tests are done make sure the name array contains all
     characters which are handled in the WIDTH section of the
     character set definition file.  */
  if (charmap->width_rules != NULL)
    for (cnt = 0; cnt < charmap->nwidth_rules; ++cnt)
      {
#if 0
	size_t inner;
	for (inner = charmap->width_rules[cnt].from;
	     inner <= charmap->width_rules[cnt].to; ++inner)
	  (void) find_idx (ctype, NULL, NULL, NULL, inner);
#else
	/* XXX Handle width.  We must convert from the charseq to the
           repertoire value */
	abort ();
#endif
      }

  /* There must be a multiple of 10 digits.  */
  if (ctype->mbdigits_act % 10 != 0)
    {
      assert (ctype->mbdigits_act == ctype->wcdigits_act);
      ctype->wcdigits_act -= ctype->mbdigits_act % 10;
      ctype->mbdigits_act -= ctype->mbdigits_act % 10;
      error (0, 0, _("`digit' category has not entries in groups of ten"));
    }

  /* Check the input digits.  There must be a multiple of ten available.
     In each group it could be that one or the other character is missing.
     In this case the whole group must be removed.  */
  cnt = 0;
  while (cnt < ctype->mbdigits_act)
    {
      size_t inner;
      for (inner = 0; inner < 10; ++inner)
	if (ctype->mbdigits[cnt + inner] == NULL)
	  break;

      if (inner == 10)
	cnt += 10;
      else
	{
	  /* Remove the group.  */
	  memmove (&ctype->mbdigits[cnt], &ctype->mbdigits[cnt + 10],
		   ((ctype->wcdigits_act - cnt - 10)
		    * sizeof (ctype->mbdigits[0])));
	  ctype->mbdigits_act -= 10;
	}
    }

  /* If no input digits are given use the default.  */
  if (ctype->mbdigits_act == 0)
    {
      if (ctype->mbdigits_max == 0)
	{
	  ctype->mbdigits = obstack_alloc (&charmap->mem_pool,
					   10 * sizeof (struct charseq *));
	  ctype->mbdigits_max = 10;
	}

      for (cnt = 0; cnt < 10; ++cnt)
	{
	  ctype->mbdigits[cnt] = charmap_find_symbol (charmap,
						      digits + cnt, 1);
	  if (ctype->mbdigits[cnt] == NULL)
	    {
	      ctype->mbdigits[cnt] = charmap_find_symbol (charmap,
							  longnames[cnt],
							  strlen (longnames[cnt]));
	      if (ctype->mbdigits[cnt] == NULL)
		{
		  /* Hum, this ain't good.  */
		  error (0, 0, _("\
no input digits defined and none of the standard names in the charmap"));

		  ctype->mbdigits[cnt] = obstack_alloc (&charmap->mem_pool,
							sizeof (struct charseq) + 1);

		  /* This is better than nothing.  */
		  ctype->mbdigits[cnt]->bytes[0] = digits[cnt];
		  ctype->mbdigits[cnt]->nbytes = 1;
		}
	    }
	}

      ctype->mbdigits_act = 10;
    }

  /* Check the wide character input digits.  There must be a multiple
     of ten available.  In each group it could be that one or the other
     character is missing.  In this case the whole group must be
     removed.  */
  cnt = 0;
  while (cnt < ctype->wcdigits_act)
    {
      size_t inner;
      for (inner = 0; inner < 10; ++inner)
	if (ctype->wcdigits[cnt + inner] == ILLEGAL_CHAR_VALUE)
	  break;

      if (inner == 10)
	cnt += 10;
      else
	{
	  /* Remove the group.  */
	  memmove (&ctype->wcdigits[cnt], &ctype->wcdigits[cnt + 10],
		   ((ctype->wcdigits_act - cnt - 10)
		    * sizeof (ctype->wcdigits[0])));
	  ctype->wcdigits_act -= 10;
	}
    }

  /* If no input digits are given use the default.  */
  if (ctype->wcdigits_act == 0)
    {
      if (ctype->wcdigits_max == 0)
	{
	  ctype->wcdigits = obstack_alloc (&charmap->mem_pool,
					   10 * sizeof (uint32_t));
	  ctype->wcdigits_max = 10;
	}

      for (cnt = 0; cnt < 10; ++cnt)
	ctype->wcdigits[cnt] = L'0' + cnt;

      ctype->mbdigits_act = 10;
    }

  /* Check the outdigits.  */
  warned = 0;
  for (cnt = 0; cnt < 10; ++cnt)
    if (ctype->mboutdigits[cnt] == NULL)
      {
	static struct charseq replace[2];

	if (!warned)
	  {
	    error (0, 0, _("\
not all characters used in `outdigit' are available in the charmap"));
	    warned = 1;
	  }

	replace[0].nbytes = 1;
	replace[0].bytes[0] = '?';
	replace[0].bytes[1] = '\0';
	ctype->mboutdigits[cnt] = &replace[0];
      }

  warned = 0;
  for (cnt = 0; cnt < 10; ++cnt)
    if (ctype->wcoutdigits[cnt] == 0)
      {
	if (!warned)
	  {
	    error (0, 0, _("\
not all characters used in `outdigit' are available in the repertoire"));
	    warned = 1;
	  }

	ctype->wcoutdigits[cnt] = L'?';
      }
}


void
ctype_output (struct localedef_t *locale, struct charmap_t *charmap,
	      const char *output_path)
{
  struct locale_ctype_t *ctype = locale->categories[LC_CTYPE].ctype;
  const size_t nelems = (_NL_ITEM_INDEX (_NL_NUM_LC_CTYPE)
			 + 2 * (ctype->map_collection_nr - 2));
  struct iovec iov[2 + nelems + ctype->nr_charclass
		  + ctype->map_collection_nr];
  struct locale_file data;
  uint32_t idx[nelems + 1];
  size_t elem, cnt, offset, total;
  char *cp;

  /* Now prepare the output: Find the sizes of the table we can use.  */
  allocate_arrays (ctype, charmap, ctype->repertoire);

  data.magic = LIMAGIC (LC_CTYPE);
  data.n = nelems;
  iov[0].iov_base = (void *) &data;
  iov[0].iov_len = sizeof (data);

  iov[1].iov_base = (void *) idx;
  iov[1].iov_len = sizeof (idx);

  idx[0] = iov[0].iov_len + iov[1].iov_len;
  offset = 0;

  for (elem = 0; elem < nelems; ++elem)
    {
      if (elem < _NL_ITEM_INDEX (_NL_NUM_LC_CTYPE))
	switch (elem)
	  {
#define CTYPE_DATA(name, base, len)					      \
	  case _NL_ITEM_INDEX (name):					      \
	    iov[2 + elem + offset].iov_base = (base);			      \
	    iov[2 + elem + offset].iov_len = (len);			      \
	    if (elem + 1 < nelems)					      \
	      idx[elem + 1] = idx[elem] + iov[2 + elem + offset].iov_len;     \
	    break

	  CTYPE_DATA (_NL_CTYPE_CLASS,
		      ctype->ctype_b,
		      (256 + 128) * sizeof (char_class_t));

	  CTYPE_DATA (_NL_CTYPE_TOUPPER,
		      ctype->map[0],
		      (ctype->plane_size * ctype->plane_cnt + 128)
		      * sizeof (uint32_t));
	  CTYPE_DATA (_NL_CTYPE_TOLOWER,
		      ctype->map[1],
		      (ctype->plane_size * ctype->plane_cnt + 128)
		      * sizeof (uint32_t));

	  CTYPE_DATA (_NL_CTYPE_CLASS32,
		      ctype->ctype32_b,
		      (ctype->plane_size * ctype->plane_cnt
		       * sizeof (char_class32_t)));

	  CTYPE_DATA (_NL_CTYPE_NAMES,
		      ctype->names, (ctype->plane_size * ctype->plane_cnt
				     * sizeof (uint32_t)));

	  CTYPE_DATA (_NL_CTYPE_TRANSLIT_HASH_SIZE,
		      &ctype->translit_hash_size, sizeof (uint32_t));
	  CTYPE_DATA (_NL_CTYPE_TRANSLIT_HASH_LAYERS,
		      &ctype->translit_hash_layers, sizeof (uint32_t));

	  CTYPE_DATA (_NL_CTYPE_TRANSLIT_FROM_IDX,
		      ctype->translit_from_idx,
		      ctype->translit_idx_size);

	  CTYPE_DATA (_NL_CTYPE_TRANSLIT_FROM_TBL,
		      ctype->translit_from_tbl,
		      ctype->translit_from_tbl_size);

	  CTYPE_DATA (_NL_CTYPE_TRANSLIT_TO_IDX,
		      ctype->translit_to_idx,
		      ctype->translit_idx_size);

	  CTYPE_DATA (_NL_CTYPE_TRANSLIT_TO_TBL,
		      ctype->translit_to_tbl, ctype->translit_to_tbl_size);

	  CTYPE_DATA (_NL_CTYPE_HASH_SIZE,
		      &ctype->plane_size, sizeof (uint32_t));
	  CTYPE_DATA (_NL_CTYPE_HASH_LAYERS,
		      &ctype->plane_cnt, sizeof (uint32_t));

	  case _NL_ITEM_INDEX (_NL_CTYPE_CLASS_NAMES):
	    /* The class name array.  */
	    total = 0;
	    for (cnt = 0; cnt < ctype->nr_charclass; ++cnt, ++offset)
	      {
		iov[2 + elem + offset].iov_base
		  = (void *) ctype->classnames[cnt];
		iov[2 + elem + offset].iov_len
		  = strlen (ctype->classnames[cnt]) + 1;
		total += iov[2 + elem + offset].iov_len;
	      }
	    iov[2 + elem + offset].iov_base = (void *) "\0\0\0";
	    iov[2 + elem + offset].iov_len = 1 + (4 - ((total + 1) % 4));
	    total += 1 + (4 - ((total + 1) % 4));

	    idx[elem + 1] = idx[elem] + total;
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_MAP_NAMES):
	    /* The class name array.  */
	    total = 0;
	    for (cnt = 0; cnt < ctype->map_collection_nr; ++cnt, ++offset)
	      {
		iov[2 + elem + offset].iov_base
		  = (void *) ctype->mapnames[cnt];
		iov[2 + elem + offset].iov_len
		  = strlen (ctype->mapnames[cnt]) + 1;
		total += iov[2 + elem + offset].iov_len;
	      }
	    iov[2 + elem + offset].iov_base = (void *) "\0\0\0";
	    iov[2 + elem + offset].iov_len = 1 + (4 - ((total + 1) % 4));
	    total += 1 + (4 - ((total + 1) % 4));

	    idx[elem + 1] = idx[elem] + total;
	    break;

	  CTYPE_DATA (_NL_CTYPE_WIDTH,
		      ctype->width, ctype->plane_size * ctype->plane_cnt);

	  CTYPE_DATA (_NL_CTYPE_MB_CUR_MAX,
		      &ctype->mb_cur_max, sizeof (uint32_t));

	  case _NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME):
	    total = strlen (ctype->codeset_name) + 1;
	    if (total % 4 == 0)
	      iov[2 + elem + offset].iov_base = (char *) ctype->codeset_name;
	    else
	      {
		iov[2 + elem + offset].iov_base = alloca ((total + 3) & ~3);
		memset (mempcpy (iov[2 + elem + offset].iov_base,
				 ctype->codeset_name, total),
			'\0', 4 - (total & 3));
		total = (total + 3) & ~3;
	      }
	    iov[2 + elem + offset].iov_len = total;
	    idx[elem + 1] = idx[elem] + iov[2 + elem + offset].iov_len;
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN):
	    iov[2 + elem + offset].iov_base = alloca (sizeof (uint32_t));
	    iov[2 + elem + offset].iov_len = sizeof (uint32_t);
	    *(uint32_t *) iov[2 + elem + offset].iov_base =
	      ctype->mbdigits_act / 10;
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_WC_LEN):
	    iov[2 + elem + offset].iov_base = alloca (sizeof (uint32_t));
	    iov[2 + elem + offset].iov_len = sizeof (uint32_t);
	    *(uint32_t *) iov[2 + elem + offset].iov_base =
	      ctype->wcdigits_act / 10;
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_INDIGITS0_MB) ... _NL_ITEM_INDEX (_NL_CTYPE_INDIGITS9_MB):
	    /* Compute the length of all possible characters.  For INDIGITS
	       there might be more than one.  We simply concatenate all of
	       them with a NUL byte following.  The NUL byte wouldn't be
	       necessary but it makes it easier for the user.  */
	    total = 0;
	    for (cnt = elem - _NL_CTYPE_INDIGITS0_MB;
		 cnt < ctype->mbdigits_act; cnt += 10)
	      total += ctype->mbdigits[cnt]->nbytes + 1;
	    iov[2 + elem + offset].iov_base = (char *) alloca (total);
	    iov[2 + elem + offset].iov_len = total;

	    cp = iov[2 + elem + offset].iov_base;
	    for (cnt = elem - _NL_CTYPE_INDIGITS0_MB;
		 cnt < ctype->mbdigits_act; cnt += 10)
	      {
		cp = mempcpy (cp, ctype->mbdigits[cnt]->bytes,
			      ctype->mbdigits[cnt]->nbytes);
		*cp++ = '\0';
	      }
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT0_MB) ... _NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT9_MB):
	    /* Compute the length of all possible characters.  For INDIGITS
	       there might be more than one.  We simply concatenate all of
	       them with a NUL byte following.  The NUL byte wouldn't be
	       necessary but it makes it easier for the user.  */
	    cnt = elem - _NL_CTYPE_OUTDIGIT0_MB;
	    total = ctype->mboutdigits[cnt]->nbytes + 1;
	    iov[2 + elem + offset].iov_base = (char *) alloca (total);
	    iov[2 + elem + offset].iov_len = total;

	    *(char *) mempcpy (iov[2 + elem + offset].iov_base,
			       ctype->mbdigits[cnt]->bytes,
			       ctype->mbdigits[cnt]->nbytes) = '\0';
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_INDIGITS0_WC) ... _NL_ITEM_INDEX (_NL_CTYPE_INDIGITS9_WC):
	    total = ctype->wcdigits_act / 10;

	    iov[2 + elem + offset].iov_base =
	      (uint32_t *) alloca (total * sizeof (uint32_t));
	    iov[2 + elem + offset].iov_len = total * sizeof (uint32_t);

	    for (cnt = elem - _NL_CTYPE_INDIGITS0_WC;
		 cnt < ctype->wcdigits_act; cnt += 10)
	      ((uint32_t *) iov[2 + elem + offset].iov_base)[cnt / 10]
		= ctype->wcdigits[cnt];
	    break;

	  case _NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT0_WC) ... _NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT9_WC):
	    cnt = elem - _NL_CTYPE_OUTDIGIT0_WC;
	    iov[2 + elem + offset].iov_base = &ctype->wcoutdigits[cnt];
	    iov[2 + elem + offset].iov_len = sizeof (uint32_t);
	    break;

	  default:
	    assert (! "unknown CTYPE element");
	  }
      else
	{
	  /* Handle extra maps.  */
	  size_t nr = (elem - _NL_ITEM_INDEX (_NL_NUM_LC_CTYPE)) >> 1;

	  iov[2 + elem + offset].iov_base = ctype->map[nr];
	  iov[2 + elem + offset].iov_len = ((ctype->plane_size
					     * ctype->plane_cnt + 128)
					    * sizeof (uint32_t));

	  idx[elem + 1] = idx[elem] + iov[2 + elem + offset].iov_len;
	}
    }

  assert (2 + elem + offset == (nelems + ctype->nr_charclass
				+ ctype->map_collection_nr + 2));

  write_locale_data (output_path, "LC_CTYPE", 2 + elem + offset, iov);
}


/* Local functions.  */
static void
ctype_class_new (struct linereader *lr, struct locale_ctype_t *ctype,
		 const char *name)
{
  size_t cnt;

  for (cnt = 0; cnt < ctype->nr_charclass; ++cnt)
    if (strcmp (ctype->classnames[cnt], name) == 0)
      break;

  if (cnt < ctype->nr_charclass)
    {
      lr_error (lr, _("character class `%s' already defined"), name);
      return;
    }

  if (ctype->nr_charclass == MAX_NR_CHARCLASS)
    /* Exit code 2 is prescribed in P1003.2b.  */
    error (2, 0, _("\
implementation limit: no more than %d character classes allowed"),
	   MAX_NR_CHARCLASS);

  ctype->classnames[ctype->nr_charclass++] = name;
}


static void
ctype_map_new (struct linereader *lr, struct locale_ctype_t *ctype,
	       const char *name, struct charmap_t *charmap)
{
  size_t max_chars = 0;
  size_t cnt;

  for (cnt = 0; cnt < ctype->map_collection_nr; ++cnt)
    {
      if (strcmp (ctype->mapnames[cnt], name) == 0)
	break;

      if (max_chars < ctype->map_collection_max[cnt])
	max_chars = ctype->map_collection_max[cnt];
    }

  if (cnt < ctype->map_collection_nr)
    {
      lr_error (lr, _("character map `%s' already defined"), name);
      return;
    }

  if (ctype->map_collection_nr == MAX_NR_CHARMAP)
    /* Exit code 2 is prescribed in P1003.2b.  */
    error (2, 0, _("\
implementation limit: no more than %d character maps allowed"),
	   MAX_NR_CHARMAP);

  ctype->mapnames[cnt] = name;

  if (max_chars == 0)
    ctype->map_collection_max[cnt] = charmap->mb_cur_max == 1 ? 256 : 512;
  else
    ctype->map_collection_max[cnt] = max_chars;

  ctype->map_collection[cnt] = (uint32_t *)
    xmalloc (sizeof (uint32_t) * ctype->map_collection_max[cnt]);
  memset (ctype->map_collection[cnt], '\0',
	  sizeof (uint32_t) * ctype->map_collection_max[cnt]);
  ctype->map_collection_act[cnt] = 256;

  ++ctype->map_collection_nr;
}


/* We have to be prepared that TABLE, MAX, and ACT can be NULL.  This
   is possible if we only want to extend the name array.  */
static uint32_t *
find_idx (struct locale_ctype_t *ctype, uint32_t **table, size_t *max,
	  size_t *act, uint32_t idx)
{
  size_t cnt;

  if (idx < 256)
    return table == NULL ? NULL : &(*table)[idx];

  for (cnt = 256; cnt < ctype->charnames_act; ++cnt)
    if (ctype->charnames[cnt] == idx)
      break;

  /* We have to distinguish two cases: the name is found or not.  */
  if (cnt == ctype->charnames_act)
    {
      /* Extend the name array.  */
      if (ctype->charnames_act == ctype->charnames_max)
	{
	  ctype->charnames_max *= 2;
	  ctype->charnames = (unsigned int *)
	    xrealloc (ctype->charnames,
		      sizeof (unsigned int) * ctype->charnames_max);
	}
      ctype->charnames[ctype->charnames_act++] = idx;
    }

  if (table == NULL)
    /* We have done everything we are asked to do.  */
    return NULL;

  if (cnt >= *act)
    {
      if (cnt >= *max)
	{
	  size_t old_max = *max;
	  do
	    *max *= 2;
	  while (*max <= cnt);

	  *table =
	    (uint32_t *) xrealloc (*table, *max * sizeof (unsigned long int));
	  memset (&(*table)[old_max], '\0',
		  (*max - old_max) * sizeof (uint32_t));
	}

      *act = cnt;
    }

  return &(*table)[cnt];
}


static int
get_character (struct token *now, struct charmap_t *charmap,
	       struct repertoire_t *repertoire,
	       struct charseq **seqp, uint32_t *wchp)
{
  if (now->tok == tok_bsymbol)
    {
      /* This will hopefully be the normal case.  */
      *wchp = repertoire_find_value (repertoire, now->val.str.startmb,
				     now->val.str.lenmb);
      *seqp = charmap_find_value (charmap, now->val.str.startmb,
				  now->val.str.lenmb);
    }
  else if (now->tok == tok_ucs4)
    {
      *seqp = repertoire_find_seq (repertoire, now->val.ucs4);

      if (*seqp == NULL)
	{
	  /* Compute the value in the charmap from the UCS value.  */
	  const char *symbol = repertoire_find_symbol (repertoire,
						       now->val.ucs4);

	  if (symbol == NULL)
	    *seqp = NULL;
	  else
	    *seqp = charmap_find_value (charmap, symbol, strlen (symbol));

	  if (*seqp == NULL)
	    {
	      /* Insert a negative entry.  */
	      static const struct charseq negative
		= { .ucs4 = ILLEGAL_CHAR_VALUE };
	      uint32_t *newp = obstack_alloc (&repertoire->mem_pool, 4);
	      *newp = now->val.ucs4;

	      insert_entry (&repertoire->seq_table, newp, 4,
			    (void *) &negative);
	    }
	  else
	    (*seqp)->ucs4 = now->val.ucs4;
	}
      else if ((*seqp)->ucs4 != now->val.ucs4)
	*seqp = NULL;

      *wchp = now->val.ucs4;
    }
  else if (now->tok == tok_charcode)
    {
      /* We must map from the byte code to UCS4.  */
      *seqp = charmap_find_symbol (charmap, now->val.str.startmb,
				   now->val.str.lenmb);

      if (*seqp == NULL)
	*wchp = ILLEGAL_CHAR_VALUE;
      else
	{
	  if ((*seqp)->ucs4 == UNINITIALIZED_CHAR_VALUE)
	    (*seqp)->ucs4 = repertoire_find_value (repertoire, (*seqp)->name,
						   strlen ((*seqp)->name));
	  *wchp = (*seqp)->ucs4;
	}
    }
  else
    return 1;

  return 0;
}


/* Ellipsis like in `<foo123>..<foo12a>' or `<j1234>....<j1245>'.  */
static void
charclass_symbolic_ellipsis (struct linereader *ldfile,
			     struct locale_ctype_t *ctype,
			     struct charmap_t *charmap,
			     struct repertoire_t *repertoire,
			     struct token *now,
			     const char *last_str,
			     unsigned long int class256_bit,
			     unsigned long int class_bit, int base,
			     int ignore_content, int handle_digits)
{
  const char *nowstr = now->val.str.startmb;
  char tmp[now->val.str.lenmb + 1];
  const char *cp;
  char *endp;
  unsigned long int from;
  unsigned long int to;

  /* We have to compute the ellipsis values using the symbolic names.  */
  assert (last_str != NULL);

  if (strlen (last_str) != now->val.str.lenmb)
    {
    invalid_range:
      lr_error (ldfile,
		_("`%s' and `%.*s' are no valid names for symbolic range"),
		last_str, now->val.str.lenmb, nowstr);
      return;
    }

  if (memcmp (last_str, nowstr, now->val.str.lenmb) == 0)
    /* Nothing to do, the names are the same.  */
    return;

  for (cp = last_str; *cp == *(nowstr + (cp - last_str)); ++cp)
    ;

  errno = 0;
  from = strtoul (cp, &endp, base);
  if ((from == UINT_MAX && errno == ERANGE) || *endp != '\0')
    goto invalid_range;

  to = strtoul (nowstr + (cp - last_str), &endp, base);
  if ((to == UINT_MAX && errno == ERANGE)
      || (endp - nowstr) != now->val.str.lenmb || from >= to)
    goto invalid_range;

  /* OK, we have a range FROM - TO.  Now we can create the symbolic names.  */
  if (!ignore_content)
    {
      now->val.str.startmb = tmp;
      while (++from <= to)
	{
	  struct charseq *seq;
	  uint32_t wch;

	  sprintf (tmp, (base == 10 ? "%.*s%0*d" : "%.*s%0*X"), cp - last_str,
		   last_str, now->val.str.lenmb - (cp - last_str), from);

	  get_character (now, charmap, repertoire, &seq, &wch);

	  if (seq != NULL && seq->nbytes == 1)
	    /* Yep, we can store information about this byte sequence.  */
	    ctype->class256_collection[seq->bytes[0]] |= class256_bit;

	  if (wch != ILLEGAL_CHAR_VALUE && class_bit != 0)
	    /* We have the UCS4 position.  */
	    *find_idx (ctype, &ctype->class_collection,
		       &ctype->class_collection_max,
		       &ctype->class_collection_act, wch) |= class_bit;

	  if (handle_digits == 1)
	    {
	      /* We must store the digit values.  */
	      if (ctype->mbdigits_act == ctype->mbdigits_max)
		{
		  ctype->mbdigits_max *= 2;
		  ctype->mbdigits = xrealloc (ctype->mbdigits,
					      (ctype->mbdigits_max
					       * sizeof (char *)));
		  ctype->wcdigits_max *= 2;
		  ctype->wcdigits = xrealloc (ctype->wcdigits,
					      (ctype->wcdigits_max
					       * sizeof (uint32_t)));
		}

	      ctype->mbdigits[ctype->mbdigits_act++] = seq;
	      ctype->wcdigits[ctype->wcdigits_act++] = wch;
	    }
	  else if (handle_digits == 2)
	    {
	      /* We must store the digit values.  */
	      if (ctype->outdigits_act >= 10)
		{
		  lr_error (ldfile, _("\
%s: field `%s' does not contain exactly ten entries"),
			    "LC_CTYPE", "outdigit");
		  return;
		}

	      ctype->mboutdigits[ctype->outdigits_act] = seq;
	      ctype->wcoutdigits[ctype->outdigits_act] = wch;
	      ++ctype->outdigits_act;
	    }
	}
    }
}


/* Ellipsis like in `<U1234>..<U2345>'.  */
static void
charclass_ucs4_ellipsis (struct linereader *ldfile,
			 struct locale_ctype_t *ctype,
			 struct charmap_t *charmap,
			 struct repertoire_t *repertoire,
			 struct token *now, uint32_t last_wch,
			 unsigned long int class256_bit,
			 unsigned long int class_bit, int ignore_content,
			 int handle_digits)
{
  if (last_wch > now->val.ucs4)
    {
      lr_error (ldfile, _("\
to-value <U%0*X> of range is smaller than from-value <U%0*X>"),
		(now->val.ucs4 | last_wch) < 65536 ? 4 : 8, now->val.ucs4,
		(now->val.ucs4 | last_wch) < 65536 ? 4 : 8, last_wch);
      return;
    }

  if (!ignore_content)
    while (++last_wch <= now->val.ucs4)
      {
	/* We have to find out whether there is a byte sequence corresponding
	   to this UCS4 value.  */
	struct charseq *seq = repertoire_find_seq (repertoire, last_wch);

	/* If this is the first time we look for this sequence create a new
	   entry.  */
	if (seq == NULL)
	  {
	    /* Find the symbolic name for this UCS4 value.  */
	    const char *symbol = repertoire_find_symbol (repertoire, last_wch);
	    uint32_t *newp = obstack_alloc (&repertoire->mem_pool, 4);
	    *newp = last_wch;

	    if (symbol != NULL)
	      /* We have a name, now search the multibyte value.  */
	      seq = charmap_find_value (charmap, symbol, strlen (symbol));

	    if (seq == NULL)
	      {
		/* We have to create a fake entry.  */
		static const struct charseq negative
		  = { .ucs4 = ILLEGAL_CHAR_VALUE };
		seq = (struct charseq *) &negative;
	      }
	    else
	      seq->ucs4 = last_wch;

	    insert_entry (&repertoire->seq_table, newp, 4, seq);
	  }

	/* We have a name, now search the multibyte value.  */
	if (seq->ucs4 == last_wch && seq->nbytes == 1)
	  /* Yep, we can store information about this byte sequence.  */
	  ctype->class256_collection[(size_t) seq->bytes[0]]
	    |= class256_bit;

	/* And of course we have the UCS4 position.  */
	if (class_bit != 0 && class_bit != 0)
	  *find_idx (ctype, &ctype->class_collection,
		     &ctype->class_collection_max,
		     &ctype->class_collection_act, last_wch) |= class_bit;

	if (handle_digits == 1)
	  {
	    /* We must store the digit values.  */
	    if (ctype->mbdigits_act == ctype->mbdigits_max)
	      {
		ctype->mbdigits_max *= 2;
		ctype->mbdigits = xrealloc (ctype->mbdigits,
					    (ctype->mbdigits_max
					     * sizeof (char *)));
		ctype->wcdigits_max *= 2;
		ctype->wcdigits = xrealloc (ctype->wcdigits,
					    (ctype->wcdigits_max
					     * sizeof (uint32_t)));
	      }

	    ctype->mbdigits[ctype->mbdigits_act++] = (seq->ucs4 == last_wch
						      ? seq : NULL);
	    ctype->wcdigits[ctype->wcdigits_act++] = last_wch;
	  }
	else if (handle_digits == 2)
	  {
	    /* We must store the digit values.  */
	    if (ctype->outdigits_act >= 10)
	      {
		lr_error (ldfile, _("\
%s: field `%s' does not contain exactly ten entries"),
			  "LC_CTYPE", "outdigit");
		return;
	      }

	    ctype->mboutdigits[ctype->outdigits_act] = (seq->ucs4 == last_wch
							? seq : NULL);
	    ctype->wcoutdigits[ctype->outdigits_act] = last_wch;
	    ++ctype->outdigits_act;
	  }
      }
}


/* Ellipsis as in `/xea/x12.../xea/x34'.  */
static void
charclass_charcode_ellipsis (struct linereader *ldfile,
			     struct locale_ctype_t *ctype,
			     struct charmap_t *charmap,
			     struct repertoire_t *repertoire,
			     struct token *now, char *last_charcode,
			     uint32_t last_charcode_len,
			     unsigned long int class256_bit,
			     unsigned long int class_bit, int ignore_content,
			     int handle_digits)
{
  /* First check whether the to-value is larger.  */
  if (now->val.charcode.nbytes != last_charcode_len)
    {
      lr_error (ldfile, _("\
start end end character sequence of range must have the same length"));
      return;
    }

  if (memcmp (last_charcode, now->val.charcode.bytes, last_charcode_len) > 0)
    {
      lr_error (ldfile, _("\
to-value character sequence is smaller than from-value sequence"));
      return;
    }

  if (!ignore_content)
    {
      do
	{
	  /* Increment the byte sequence value.  */
	  struct charseq *seq;
	  uint32_t wch;
	  int i;

	  for (i = last_charcode_len - 1; i >= 0; --i)
	    if (++last_charcode[i] != 0)
	      break;

	  if (last_charcode_len == 1)
	    /* Of course we have the charcode value.  */
	    ctype->class256_collection[(size_t) last_charcode[0]]
	      |= class256_bit;

	  /* Find the symbolic name.  */
	  seq = charmap_find_symbol (charmap, last_charcode,
				     last_charcode_len);
	  if (seq != NULL)
	    {
	      if (seq->ucs4 == UNINITIALIZED_CHAR_VALUE)
		seq->ucs4 = repertoire_find_value (repertoire, seq->name,
						   strlen (seq->name));
	      wch = seq->ucs4;

	      if (wch != ILLEGAL_CHAR_VALUE && class_bit != 0)
		*find_idx (ctype, &ctype->class_collection,
			   &ctype->class_collection_max,
			   &ctype->class_collection_act, wch) |= class_bit;
	    }
	  else
	    wch = ILLEGAL_CHAR_VALUE;

	  if (handle_digits == 1)
	    {
	      /* We must store the digit values.  */
	      if (ctype->mbdigits_act == ctype->mbdigits_max)
		{
		  ctype->mbdigits_max *= 2;
		  ctype->mbdigits = xrealloc (ctype->mbdigits,
					      (ctype->mbdigits_max
					       * sizeof (char *)));
		  ctype->wcdigits_max *= 2;
		  ctype->wcdigits = xrealloc (ctype->wcdigits,
					      (ctype->wcdigits_max
					       * sizeof (uint32_t)));
		}

	      seq = xmalloc (sizeof (struct charseq) + last_charcode_len);
	      memcpy ((char *) (seq + 1), last_charcode, last_charcode_len);
	      seq->nbytes = last_charcode_len;

	      ctype->mbdigits[ctype->mbdigits_act++] = seq;
	      ctype->wcdigits[ctype->wcdigits_act++] = wch;
	    }
	  else if (handle_digits == 2)
	    {
	      struct charseq *seq;
	      /* We must store the digit values.  */
	      if (ctype->outdigits_act >= 10)
		{
		  lr_error (ldfile, _("\
%s: field `%s' does not contain exactly ten entries"),
			    "LC_CTYPE", "outdigit");
		  return;
		}

	      seq = xmalloc (sizeof (struct charseq) + last_charcode_len);
	      memcpy ((char *) (seq + 1), last_charcode, last_charcode_len);
	      seq->nbytes = last_charcode_len;

	      ctype->mboutdigits[ctype->outdigits_act] = seq;
	      ctype->wcoutdigits[ctype->outdigits_act] = wch;
	      ++ctype->outdigits_act;
	    }
	}
      while (memcmp (last_charcode, now->val.charcode.bytes,
		     last_charcode_len) != 0);
    }
}


/* Read one transliteration entry.  */
static uint32_t *
read_widestring (struct linereader *ldfile, struct token *now,
		 struct charmap_t *charmap, struct repertoire_t *repertoire)
{
  uint32_t *wstr;

  if (now->tok == tok_default_missing)
    /* The special name "" will denote this case.  */
    wstr = (uint32_t *) L"";
  else if (now->tok == tok_bsymbol)
    {
      /* Get the value from the repertoire.  */
      wstr = xmalloc (2 * sizeof (uint32_t));
      wstr[0] = repertoire_find_value (repertoire, now->val.str.startmb,
				       now->val.str.lenmb);
      if (wstr[0] == ILLEGAL_CHAR_VALUE)
	/* We cannot proceed, we don't know the UCS4 value.  */
	return NULL;

      wstr[1] = 0;
    }
  else if (now->tok == tok_ucs4)
    {
      wstr = xmalloc (2 * sizeof (uint32_t));
      wstr[0] = now->val.ucs4;
      wstr[1] = 0;
    }
  else if (now->tok == tok_charcode)
    {
      /* Argh, we have to convert to the symbol name first and then to the
	 UCS4 value.  */
      struct charseq *seq = charmap_find_symbol (charmap,
						 now->val.str.startmb,
						 now->val.str.lenmb);
      if (seq == NULL)
	/* Cannot find the UCS4 value.  */
	return NULL;

      if (seq->ucs4 == UNINITIALIZED_CHAR_VALUE)
	seq->ucs4 = repertoire_find_value (repertoire, seq->name,
					   strlen (seq->name));
      if (seq->ucs4 == ILLEGAL_CHAR_VALUE)
	/* We cannot proceed, we don't know the UCS4 value.  */
	return NULL;

      wstr = xmalloc (2 * sizeof (uint32_t));
      wstr[0] = seq->ucs4;
      wstr[1] = 0;
    }
  else if (now->tok == tok_string)
    {
      wstr = now->val.str.startwc;
      if (wstr[0] == 0)
	return NULL;
    }
  else
    {
      if (now->tok != tok_eol && now->tok != tok_eof)
	lr_ignore_rest (ldfile, 0);
      SYNTAX_ERROR (_("%s: syntax error"), "LC_CTYPE");
      return (uint32_t *) -1l;
    }

  return wstr;
}


static void
read_translit_entry (struct linereader *ldfile, struct locale_ctype_t *ctype,
		     struct token *now, struct charmap_t *charmap,
		     struct repertoire_t *repertoire)
{
  uint32_t *from_wstr = read_widestring (ldfile, now, charmap, repertoire);
  struct translit_t *result;
  struct translit_to_t **top;
  struct obstack *ob = &ctype->mem_pool;
  int first;
  int ignore;

  if (from_wstr == NULL)
    /* There is no valid from string.  */
    return;

  result = (struct translit_t *) obstack_alloc (ob,
						sizeof (struct translit_t));
  result->from = from_wstr;
  result->next = NULL;
  result->to = NULL;
  top = &result->to;
  first = 1;
  ignore = 0;

  while (1)
    {
      uint32_t *to_wstr;

      /* Next we have one or more transliterations.  They are
	 separated by semicolons.  */
      now = lr_token (ldfile, charmap, repertoire);

      if (!first && (now->tok == tok_semicolon || now->tok == tok_eol))
	{
	  /* One string read.  */
	  const uint32_t zero = 0;

	  if (!ignore)
	    {
	      obstack_grow (ob, &zero, 4);
	      to_wstr = obstack_finish (ob);

	      *top = obstack_alloc (ob, sizeof (struct translit_to_t));
	      (*top)->str = to_wstr;
	      (*top)->next = NULL;
	    }

	  if (now->tok == tok_eol)
	    {
	      result->next = ctype->translit;
	      ctype->translit = result;
	      return;
	    }

	  if (!ignore)
	    top = &(*top)->next;
	  ignore = 0;
	}
      else
	{
	  to_wstr = read_widestring (ldfile, now, charmap, repertoire);
	  if (to_wstr == (uint32_t *) -1l)
	    {
	      /* An error occurred.  */
	      obstack_free (ob, result);
	      return;
	    }

	  if (to_wstr == NULL)
	    ignore = 1;
	  else
	    /* This value is usable.  */
	    obstack_grow (ob, to_wstr, wcslen ((wchar_t *) to_wstr) * 4);

	  first = 0;
	}
    }
}


/* The parser for the LC_CTYPE section of the locale definition.  */
void
ctype_read (struct linereader *ldfile, struct localedef_t *result,
	    struct charmap_t *charmap, const char *repertoire_name,
	    int ignore_content)
{
  struct repertoire_t *repertoire = NULL;
  struct locale_ctype_t *ctype;
  struct token *now;
  enum token_t nowtok;
  size_t cnt;
  struct charseq *last_seq;
  uint32_t last_wch = 0;
  enum token_t last_token;
  enum token_t ellipsis_token;
  char last_charcode[16];
  size_t last_charcode_len = 0;
  const char *last_str = NULL;
  int mapidx;

  /* Get the repertoire we have to use.  */
  if (repertoire_name != NULL)
    repertoire = repertoire_read (repertoire_name);

  /* The rest of the line containing `LC_CTYPE' must be free.  */
  lr_ignore_rest (ldfile, 1);


  do
    {
      now = lr_token (ldfile, charmap, NULL);
      nowtok = now->tok;
    }
  while (nowtok == tok_eol);

  /* If we see `copy' now we are almost done.  */
  if (nowtok == tok_copy)
    {
      handle_copy (ldfile, charmap, repertoire, result, tok_lc_ctype, LC_CTYPE,
		   "LC_CTYPE", ignore_content);
      return;
    }

  /* Prepare the data structures.  */
  ctype_startup (ldfile, result, charmap, ignore_content);
  ctype = result->categories[LC_CTYPE].ctype;

  /* Remember the repertoire we use.  */
  if (!ignore_content)
    ctype->repertoire = repertoire;

  while (1)
    {
      unsigned long int class_bit = 0;
      unsigned long int class256_bit = 0;
      int handle_digits = 0;

      /* Of course we don't proceed beyond the end of file.  */
      if (nowtok == tok_eof)
	break;

      /* Ingore empty lines.  */
      if (nowtok == tok_eol)
	{
	  now = lr_token (ldfile, charmap, NULL);
	  nowtok = now->tok;
	  continue;
	}

      switch (nowtok)
	{
	case tok_class:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  /* We simply forget the `class' keyword and use the following
	     operand to determine the bit.  */
	  now = lr_token (ldfile, charmap, NULL);
	  if (now->tok == tok_ident || now->tok == tok_string)
	    {
	      /* Must be one of the predefined class names.  */
	      for (cnt = 0; cnt < ctype->nr_charclass; ++cnt)
		if (strcmp (ctype->classnames[cnt], now->val.str.startmb) == 0)
		  break;
	      if (cnt >= ctype->nr_charclass)
		{
		  if (now->val.str.lenmb == 8
		      && memcmp ("special1", now->val.str.startmb, 8) == 0)
		    class_bit = _ISwspecial1;
		  else if (now->val.str.lenmb == 8
		      && memcmp ("special2", now->val.str.startmb, 8) == 0)
		    class_bit = _ISwspecial2;
		  else if (now->val.str.lenmb == 8
		      && memcmp ("special3", now->val.str.startmb, 8) == 0)
		    class_bit = _ISwspecial3;
		  else
		    {
		      lr_error (ldfile, _("\
unknown character class `%s' in category `LC_CTYPE'"),
				now->val.str.startmb);
		      free (now->val.str.startmb);

		      lr_ignore_rest (ldfile, 0);
		      continue;
		    }
		}
	      else
		class_bit = _ISwbit (cnt);

	      free (now->val.str.startmb);
	    }
	  else if (now->tok == tok_digit)
	    goto handle_tok_digit;
	  else if (now->tok < tok_upper || now->tok > tok_blank)
	    goto err_label;
	  else
	    {
	      class_bit = BITw (now->tok);
	      class256_bit = BIT (now->tok);
	    }

	  /* The next character must be a semicolon.  */
	  now = lr_token (ldfile, charmap, NULL);
	  if (now->tok != tok_semicolon)
	    goto err_label;
	  goto read_charclass;

	case tok_upper:
	case tok_lower:
	case tok_alpha:
	case tok_alnum:
	case tok_space:
	case tok_cntrl:
	case tok_punct:
	case tok_graph:
	case tok_print:
	case tok_xdigit:
	case tok_blank:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  class_bit = BITw (now->tok);
	  class256_bit = BIT (now->tok);
	  handle_digits = 0;
	read_charclass:
	  ctype->class_done |= class_bit;
	  last_token = tok_none;
	  ellipsis_token = tok_none;
	  now = lr_token (ldfile, charmap, NULL);
	  while (now->tok != tok_eol && now->tok != tok_eof)
	    {
	      uint32_t wch;
	      struct charseq *seq;

	      if (ellipsis_token == tok_none)
		{
		  if (get_character (now, charmap, repertoire, &seq, &wch))
		    goto err_label;

		  if (!ignore_content && seq != NULL && seq->nbytes == 1)
		    /* Yep, we can store information about this byte
		       sequence.  */
		    ctype->class256_collection[seq->bytes[0]] |= class256_bit;

		  if (!ignore_content && wch != ILLEGAL_CHAR_VALUE
		      && class_bit != 0)
		    /* We have the UCS4 position.  */
		    *find_idx (ctype, &ctype->class_collection,
			       &ctype->class_collection_max,
			       &ctype->class_collection_act, wch) |= class_bit;

		  last_token = now->tok;
		  /* Terminate the string.  */
		  now->val.str.startmb[now->val.str.lenmb] = '\0';
		  last_str = now->val.str.startmb;
		  last_seq = seq;
		  last_wch = wch;
		  memcpy (last_charcode, now->val.charcode.bytes, 16);
		  last_charcode_len = now->val.charcode.nbytes;

		  if (!ignore_content && handle_digits == 1)
		    {
		      /* We must store the digit values.  */
		      if (ctype->mbdigits_act == ctype->mbdigits_max)
			{
			  ctype->mbdigits_max += 10;
			  ctype->mbdigits = xrealloc (ctype->mbdigits,
						      (ctype->mbdigits_max
						       * sizeof (char *)));
			  ctype->wcdigits_max += 10;
			  ctype->wcdigits = xrealloc (ctype->wcdigits,
						      (ctype->wcdigits_max
						       * sizeof (uint32_t)));
			}

		      ctype->mbdigits[ctype->mbdigits_act++] = seq;
		      ctype->wcdigits[ctype->wcdigits_act++] = wch;
		    }
		  else if (!ignore_content && handle_digits == 2)
		    {
		      /* We must store the digit values.  */
		      if (ctype->outdigits_act >= 10)
			{
			  lr_error (ldfile, _("\
%s: field `%s' does not contain exactly ten entries"),
			    "LC_CTYPE", "outdigit");
			  goto err_label;
			}

		      ctype->mboutdigits[ctype->outdigits_act] = seq;
		      ctype->wcoutdigits[ctype->outdigits_act] = wch;
		      ++ctype->outdigits_act;
		    }
		}
	      else
		{
		  /* Now it gets complicated.  We have to resolve the
		     ellipsis problem.  First we must distinguish between
		     the different kind of ellipsis and this must match the
		     tokens we have seen.  */
		  assert (last_token != tok_none);

		  if (last_token != now->tok)
		    {
		      lr_error (ldfile, _("\
ellipsis range must be marked by two operands of same type"));
		      lr_ignore_rest (ldfile, 0);
		      break;
		    }

		  if (last_token == tok_bsymbol)
		    {
		      if (ellipsis_token == tok_ellipsis3)
			lr_error (ldfile, _("with symbolic name range values \
the absolute ellipsis `...' must not be used"));

		      charclass_symbolic_ellipsis (ldfile, ctype, charmap,
						   repertoire, now, last_str,
						   class256_bit, class_bit,
						   (ellipsis_token
						    == tok_ellipsis4
						    ? 10 : 16),
						   ignore_content,
						   handle_digits);
		    }
		  else if (last_token == tok_ucs4)
		    {
		      if (ellipsis_token != tok_ellipsis2)
			lr_error (ldfile, _("\
with UCS range values one must use the hexadecimal symbolic ellipsis `..'"));

		      charclass_ucs4_ellipsis (ldfile, ctype, charmap,
					       repertoire, now, last_wch,
					       class256_bit, class_bit,
					       ignore_content, handle_digits);
		    }
		  else
		    {
		      assert (last_token == tok_charcode);

		      if (ellipsis_token != tok_ellipsis3)
			lr_error (ldfile, _("\
with character code range values one must use the absolute ellipsis `...'"));

		      charclass_charcode_ellipsis (ldfile, ctype, charmap,
						   repertoire, now,
						   last_charcode,
						   last_charcode_len,
						   class256_bit, class_bit,
						   ignore_content,
						   handle_digits);
		    }

		  /* Now we have used the last value.  */
		  last_token = tok_none;
		}

	      /* Next we expect a semicolon or the end of the line.  */
	      now = lr_token (ldfile, charmap, NULL);
	      if (now->tok == tok_eol || now->tok == tok_eof)
		break;

	      if (last_token != tok_none
		  && now->tok >= tok_ellipsis2 && now->tok <= tok_ellipsis4)
		{
		  ellipsis_token = now->tok;
		  now = lr_token (ldfile, charmap, NULL);
		  continue;
		}

	      if (now->tok != tok_semicolon)
		goto err_label;

	      /* And get the next character.  */
	      now = lr_token (ldfile, charmap, NULL);

	      ellipsis_token = tok_none;
	    }
	  break;

	case tok_digit:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	handle_tok_digit:
	  class_bit = _ISwdigit;
	  class256_bit = _ISdigit;
	  handle_digits = 1;
	  goto read_charclass;

	case tok_outdigit:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  if (ctype->outdigits_act != 0)
	    lr_error (ldfile, _("\
%s: field `%s' declared more than once"),
		      "LC_CTYPE", "outdigit");
	  class_bit = 0;
	  class256_bit = 0;
	  handle_digits = 2;
	  goto read_charclass;

	case tok_toupper:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  mapidx = 0;
	  goto read_mapping;

	case tok_tolower:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  mapidx = 1;
	  goto read_mapping;

	case tok_map:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  /* We simply forget the `map' keyword and use the following
	     operand to determine the mapping.  */
	  now = lr_token (ldfile, charmap, NULL);
	  if (now->tok == tok_ident || now->tok == tok_string)
	    {
	      size_t cnt;

	      for (cnt = 2; cnt < ctype->map_collection_nr; ++cnt)
		if (strcmp (now->val.str.startmb, ctype->mapnames[cnt]) == 0)
		  break;

	      if (cnt < ctype->map_collection_nr)
		mapidx = cnt;
	      else
		{
		  lr_error (ldfile, _("unknown map `%s'"),
			    now->val.str.startmb);
		  lr_ignore_rest (ldfile, 0);
		  break;
		}
	    }
	  else if (now->tok < tok_toupper || now->tok > tok_tolower)
	    goto err_label;
	  else
	    mapidx = now->tok - tok_toupper;

	  now = lr_token (ldfile, charmap, NULL);
	  /* This better should be a semicolon.  */
	  if (now->tok != tok_semicolon)
	    goto err_label;

	read_mapping:
	  /* Test whether this mapping was already defined.  */
	  if (ctype->tomap_done[mapidx])
	    {
	      lr_error (ldfile, _("duplicated definition for mapping `%s'"),
			ctype->mapnames[mapidx]);
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }
	  ctype->tomap_done[mapidx] = 1;

	  now = lr_token (ldfile, charmap, NULL);
	  while (now->tok != tok_eol && now->tok != tok_eof)
	    {
	      struct charseq *from_seq;
	      uint32_t from_wch;
	      struct charseq *to_seq;
	      uint32_t to_wch;

	      /* Every pair starts with an opening brace.  */
	      if (now->tok != tok_open_brace)
		goto err_label;

	      /* Next comes the from-value.  */
	      now = lr_token (ldfile, charmap, NULL);
	      if (get_character (now, charmap, repertoire, &from_seq,
				 &from_wch) != 0)
		goto err_label;

	      /* The next is a comma.  */
	      now = lr_token (ldfile, charmap, NULL);
	      if (now->tok != tok_comma)
		goto err_label;

	      /* And the other value.  */
	      now = lr_token (ldfile, charmap, NULL);
	      if (get_character (now, charmap, repertoire, &to_seq,
				 &to_wch) != 0)
		goto err_label;

	      /* And the last thing is the closing brace.  */
	      now = lr_token (ldfile, charmap, NULL);
	      if (now->tok != tok_close_brace)
		goto err_label;

	      if (!ignore_content)
		{
		  if (mapidx < 2 && from_seq != NULL && to_seq != NULL
		      && from_seq->nbytes == 1 && to_seq->nbytes == 1)
		    /* We can use this value.  */
		    ctype->map256_collection[mapidx][from_seq->bytes[0]]
		      = to_seq->bytes[0];

		  if (from_wch != ILLEGAL_CHAR_VALUE
		      && to_wch != ILLEGAL_CHAR_VALUE)
		    /* Both correct values.  */
		    *find_idx (ctype, &ctype->map_collection[mapidx],
			       &ctype->map_collection_max[mapidx],
			       &ctype->map_collection_act[mapidx],
			       from_wch) = to_wch;
		}

	      /* Now comes a semicolon or the end of the line/file.  */
	      now = lr_token (ldfile, charmap, NULL);
	      if (now->tok == tok_semicolon)
		now = lr_token (ldfile, charmap, NULL);
	    }
	  break;

	case tok_translit_start:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  /* The rest of the line better should be empty.  */
	  lr_ignore_rest (ldfile, 1);

	  /* We count here the number of allocated entries in the `translit'
	     array.  */
	  cnt = 0;

	  /* We proceed until we see the `translit_end' token.  */
	  while (now = lr_token (ldfile, charmap, repertoire),
		 now->tok != tok_translit_end && now->tok != tok_eof)
	    {
	      if (now->tok == tok_eol)
		/* Ignore empty lines.  */
		continue;

	      if (now->tok == tok_translit_end)
		{
		  lr_ignore_rest (ldfile, 0);
		  break;
		}

	      if (now->tok == tok_include)
		{
		  /* We have to include locale.  */
		  const char *locale_name;
		  const char *repertoire_name;

		  now = lr_token (ldfile, charmap, NULL);
		  /* This should be a string or an identifier.  In any
		     case something to name a locale.  */
		  if (now->tok != tok_string && now->tok != tok_ident)
		    {
		    translit_syntax:
		      lr_error (ldfile, _("%s: syntax error"), "LC_CTYPE");
		      lr_ignore_rest (ldfile, 0);
		      continue;
		    }
		  locale_name = now->val.str.startmb;

		  /* Next should be a semicolon.  */
		  now = lr_token (ldfile, charmap, NULL);
		  if (now->tok != tok_semicolon)
		    goto translit_syntax;

		  /* Now the repertoire name.  */
		  now = lr_token (ldfile, charmap, NULL);
		  if ((now->tok != tok_string && now->tok != tok_ident)
		      || now->val.str.startmb == NULL)
		    goto translit_syntax;
		  repertoire_name = now->val.str.startmb;

		  /* We must not have more than one `include'.  */
		  if (ctype->translit_copy_locale != NULL)
		    {
		      lr_error (ldfile, _("\
%s: only one `include' instruction allowed"), "LC_CTYPE");
		      lr_ignore_rest (ldfile, 0);
		      continue;
		    }

		  ctype->translit_copy_locale = locale_name;
		  ctype->translit_copy_repertoire = repertoire_name;

		  /* The rest of the line must be empty.  */
		  lr_ignore_rest (ldfile, 1);
		  continue;
		}

	      read_translit_entry (ldfile, ctype, now, charmap, repertoire);
	    }
	  break;

	case tok_ident:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    {
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  /* This could mean one of several things.  First test whether
	     it's a character class name.  */
	  for (cnt = 0; cnt < ctype->nr_charclass; ++cnt)
	    if (strcmp (now->val.str.startmb, ctype->classnames[cnt]) == 0)
	      break;
	  if (cnt < ctype->nr_charclass)
	    {
	      class_bit = _ISwbit (cnt);
	      class256_bit = cnt <= 11 ? _ISbit (cnt) : 0;
	      free (now->val.str.startmb);
	      goto read_charclass;
	    }
	  if (strcmp (now->val.str.startmb, "special1") == 0)
	    {
	      class_bit = _ISwspecial1;
	      free (now->val.str.startmb);
	      goto read_charclass;
	    }
	  if (strcmp (now->val.str.startmb, "special2") == 0)
	    {
	      class_bit = _ISwspecial2;
	      free (now->val.str.startmb);
	      goto read_charclass;
	    }
	  if (strcmp (now->val.str.startmb, "special3") == 0)
	    {
	      class_bit = _ISwspecial3;
	      free (now->val.str.startmb);
	      goto read_charclass;
	    }
	  if (strcmp (now->val.str.startmb, "tosymmetric") == 0)
	    {
	      mapidx = 2;
	      goto read_mapping;
	    }
	  break;

	case tok_end:
	  /* Next we assume `LC_CTYPE'.  */
	  now = lr_token (ldfile, charmap, NULL);
	  if (now->tok == tok_eof)
	    break;
	  if (now->tok == tok_eol)
	    lr_error (ldfile, _("%s: incomplete `END' line"),
		      "LC_CTYPE");
	  else if (now->tok != tok_lc_ctype)
	    lr_error (ldfile, _("\
%1$s: definition does not end with `END %1$s'"), "LC_CTYPE");
	  lr_ignore_rest (ldfile, now->tok == tok_lc_ctype);
	  return;

	default:
	err_label:
	  if (now->tok != tok_eof)
	    SYNTAX_ERROR (_("%s: syntax error"), "LC_CTYPE");
	}

      /* Prepare for the next round.  */
      now = lr_token (ldfile, charmap, NULL);
      nowtok = now->tok;
    }

  /* When we come here we reached the end of the file.  */
  lr_error (ldfile, _("%s: premature end of file"), "LC_CTYPE");
}


static void
set_class_defaults (struct locale_ctype_t *ctype, struct charmap_t *charmap,
		    struct repertoire_t *repertoire)
{
  size_t cnt;

  /* These function defines the default values for the classes and conversions
     according to POSIX.2 2.5.2.1.
     It may seem that the order of these if-blocks is arbitrary but it is NOT.
     Don't move them unless you know what you do!  */

  void set_default (int bitpos, int from, int to)
    {
      char tmp[2];
      int ch;
      int bit = _ISbit (bitpos);
      int bitw = _ISwbit (bitpos);
      /* Define string.  */
      strcpy (tmp, "?");

      for (ch = from; ch <= to; ++ch)
	{
	  uint32_t value;
	  struct charseq *seq;
	  tmp[0] = ch;

	  value = repertoire_find_value (repertoire, tmp, 1);
	  if (value == ILLEGAL_CHAR_VALUE)
	    {
	      if (!be_quiet)
		error (0, 0, _("\
%s: character `%s' not defined in repertoire while needed as default value"),
		       "LC_CTYPE", tmp);
	    }
	  else
	    ELEM (ctype, class_collection, , value) |= bitw;

	  seq = charmap_find_value (charmap, tmp, 1);
	  if (seq == NULL)
	    {
	      if (!be_quiet)
		error (0, 0, _("\
%s: character `%s' not defined in charmap while needed as default value"),
		       "LC_CTYPE", tmp);
	    }
	  else if (seq->nbytes != 1)
	    error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
		   "LC_CTYPE", tmp);
	  else
	    ctype->class256_collection[seq->bytes[0]] |= bit;
	}
    }

  /* Set default values if keyword was not present.  */
  if ((ctype->class_done & BITw (tok_upper)) == 0)
    /* "If this keyword [lower] is not specified, the lowercase letters
        `A' through `Z', ..., shall automatically belong to this class,
	with implementation defined character values."  [P1003.2, 2.5.2.1]  */
    set_default (BITPOS (tok_upper), 'A', 'Z');

  if ((ctype->class_done & BITw (tok_lower)) == 0)
    /* "If this keyword [lower] is not specified, the lowercase letters
        `a' through `z', ..., shall automatically belong to this class,
	with implementation defined character values."  [P1003.2, 2.5.2.1]  */
    set_default (BITPOS (tok_lower), 'a', 'z');

  if ((ctype->class_done & BITw (tok_alpha)) == 0)
    {
      /* Table 2-6 in P1003.2 says that characters in class `upper' or
	 class `lower' *must* be in class `alpha'.  */
      unsigned long int mask = BIT (tok_upper) | BIT (tok_lower);

      for (cnt = 0; cnt < ctype->class_collection_act; ++cnt)
	if ((ctype->class_collection[cnt] & mask) != 0)
	  ctype->class_collection[cnt] |= BIT (tok_alpha);
    }

  if ((ctype->class_done & BITw (tok_digit)) == 0)
    /* "If this keyword [digit] is not specified, the digits `0' through
        `9', ..., shall automatically belong to this class, with
	implementation-defined character values."  [P1003.2, 2.5.2.1]  */
    set_default (BITPOS (tok_digit), '0', '9');

  /* "Only characters specified for the `alpha' and `digit' keyword
     shall be specified.  Characters specified for the keyword `alpha'
     and `digit' are automatically included in this class.  */
  {
    unsigned long int mask = BIT (tok_alpha) | BIT (tok_digit);

    for (cnt = 0; cnt < ctype->class_collection_act; ++cnt)
      if ((ctype->class_collection[cnt] & mask) != 0)
	ctype->class_collection[cnt] |= BIT (tok_alnum);
  }

  if ((ctype->class_done & BITw (tok_space)) == 0)
    /* "If this keyword [space] is not specified, the characters <space>,
        <form-feed>, <newline>, <carriage-return>, <tab>, and
	<vertical-tab>, ..., shall automatically belong to this class,
	with implementation-defined character values."  [P1003.2, 2.5.2.1]  */
    {
      uint32_t value;
      struct charseq *seq;

      value = repertoire_find_value (repertoire, "space", 5);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<space>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_space);

      seq = charmap_find_value (charmap, "space", 5);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<space>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<space>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_space);


      value = repertoire_find_value (repertoire, "form-feed", 9);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<form-feed>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_space);

      seq = charmap_find_value (charmap, "form-feed", 9);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<form-feed>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<form-feed>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_space);


      value = repertoire_find_value (repertoire, "newline", 7);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<newline>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_space);

      seq = charmap_find_value (charmap, "newline", 7);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
character `%s' not defined while needed as default value"),
		   "<newline>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<newline>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_space);


      value = repertoire_find_value (repertoire, "carriage-return", 15);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<carriage-return>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_space);

      seq = charmap_find_value (charmap, "carriage-return", 15);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<carriage-return>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<carriage-return>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_space);


      value = repertoire_find_value (repertoire, "tab", 3);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<tab>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_space);

      seq = charmap_find_value (charmap, "tab", 3);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<tab>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<tab>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_space);


      value = repertoire_find_value (repertoire, "vertical-tab", 12);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<vertical-tab>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_space);

      seq = charmap_find_value (charmap, "vertical-tab", 12);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<vertical-tab>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<vertical-tab>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_space);
    }

  if ((ctype->class_done & BITw (tok_xdigit)) == 0)
    /* "If this keyword is not specified, the digits `0' to `9', the
        uppercase letters `A' through `F', and the lowercase letters `a'
	through `f', ..., shell automatically belong to this class, with
	implementation defined character values."  [P1003.2, 2.5.2.1]  */
    {
      set_default (BITPOS (tok_xdigit), '0', '9');
      set_default (BITPOS (tok_xdigit), 'A', 'F');
      set_default (BITPOS (tok_xdigit), 'a', 'f');
    }

  if ((ctype->class_done & BITw (tok_blank)) == 0)
    /* "If this keyword [blank] is unspecified, the characters <space> and
       <tab> shall belong to this character class."  [P1003.2, 2.5.2.1]  */
   {
      uint32_t value;
      struct charseq *seq;

      value = repertoire_find_value (repertoire, "space", 5);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<space>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_blank);

      seq = charmap_find_value (charmap, "space", 5);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<space>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<space>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_blank);


      value = repertoire_find_value (repertoire, "tab", 3);
      if (value == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<tab>");
	}
      else
	ELEM (ctype, class_collection, , value) |= BIT (tok_blank);

      seq = charmap_find_value (charmap, "tab", 3);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<tab>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<tab>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_blank);
    }

  if ((ctype->class_done & BITw (tok_graph)) == 0)
    /* "If this keyword [graph] is not specified, characters specified for
        the keywords `upper', `lower', `alpha', `digit', `xdigit' and `punct',
	shall belong to this character class."  [P1003.2, 2.5.2.1]  */
    {
      unsigned long int mask = BIT (tok_upper) | BIT (tok_lower) |
	BIT (tok_alpha) | BIT (tok_digit) | BIT (tok_xdigit) | BIT (tok_punct);
      size_t cnt;

      for (cnt = 0; cnt < ctype->class_collection_act; ++cnt)
	if ((ctype->class_collection[cnt] & mask) != 0)
	  ctype->class_collection[cnt] |= BIT (tok_graph);

      for (cnt = 0; cnt < 256; ++cnt)
	if ((ctype->class256_collection[cnt] & mask) != 0)
	  ctype->class256_collection[cnt] |= BIT (tok_graph);
    }

  if ((ctype->class_done & BITw (tok_print)) == 0)
    /* "If this keyword [print] is not provided, characters specified for
        the keywords `upper', `lower', `alpha', `digit', `xdigit', `punct',
	and the <space> character shall belong to this character class."
	[P1003.2, 2.5.2.1]  */
    {
      unsigned long int mask = BIT (tok_upper) | BIT (tok_lower) |
	BIT (tok_alpha) | BIT (tok_digit) | BIT (tok_xdigit) | BIT (tok_punct);
      size_t cnt;
      uint32_t space;
      struct charseq *seq;

      for (cnt = 0; cnt < ctype->class_collection_act; ++cnt)
	if ((ctype->class_collection[cnt] & mask) != 0)
	  ctype->class_collection[cnt] |= BIT (tok_print);

      for (cnt = 0; cnt < 256; ++cnt)
	if ((ctype->class256_collection[cnt] & mask) != 0)
	  ctype->class256_collection[cnt] |= BIT (tok_print);


      space = repertoire_find_value (repertoire, "space", 5);
      if (space == ILLEGAL_CHAR_VALUE)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<space>");
	}
      else
	ELEM (ctype, class_collection, , space) |= BIT (tok_print);

      seq = charmap_find_value (charmap, "space", 5);
      if (seq == NULL)
	{
	  if (!be_quiet)
	    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		   "LC_CTYPE", "<space>");
	}
      else if (seq->nbytes != 1)
	error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
	       "LC_CTYPE", "<space>");
      else
	ctype->class256_collection[seq->bytes[0]] |= BIT (tok_print);
    }

  if (ctype->tomap_done[0] == 0)
    /* "If this keyword [toupper] is not specified, the lowercase letters
        `a' through `z', and their corresponding uppercase letters `A' to
	`Z', ..., shall automatically be included, with implementation-
	defined character values."  [P1003.2, 2.5.2.1]  */
    {
      char tmp[4];
      int ch;

      strcpy (tmp, "<?>");

      for (ch = 'a'; ch <= 'z'; ++ch)
	{
	  uint32_t value_from, value_to;
	  struct charseq *seq_from, *seq_to;

	  tmp[1] = (char) ch;

	  value_from = repertoire_find_value (repertoire, &tmp[1], 1);
	  if (value_from == ILLEGAL_CHAR_VALUE)
	    {
	      if (!be_quiet)
		error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		       "LC_CTYPE", tmp);
	    }
	  else
	    {
	      /* This conversion is implementation defined.  */
	      tmp[1] = (char) (ch + ('A' - 'a'));
	      value_to = repertoire_find_value (repertoire, &tmp[1], 1);
	      if (value_to == ILLEGAL_CHAR_VALUE)
		{
		  if (!be_quiet)
		    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
			   "LC_CTYPE", tmp);
		}
	      else
		/* The index [0] is determined by the order of the
		   `ctype_map_newP' calls in `ctype_startup'.  */
		ELEM (ctype, map_collection, [0], value_from) = value_to;
	    }

	  seq_from = charmap_find_value (charmap, &tmp[1], 1);
	  if (seq_from == NULL)
	    {
	      if (!be_quiet)
		error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
		       "LC_CTYPE", tmp);
	    }
	  else if (seq_from->nbytes != 1)
	    {
	      if (!be_quiet)
		error (0, 0, _("\
%s: character `%s' needed as default value not representable with one byte"),
		       "LC_CTYPE", tmp);
	    }
	  else
	    {
	      /* This conversion is implementation defined.  */
	      tmp[1] = (char) (ch + ('A' - 'a'));
	      seq_to = charmap_find_value (charmap, &tmp[1], 1);
	      if (seq_to == NULL)
		{
		  if (!be_quiet)
		    error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
			   "LC_CTYPE", tmp);
		}
	      else if (seq_to->nbytes != 1)
		{
		  if (!be_quiet)
		    error (0, 0, _("\
%s: character `%s' needed as default value not representable with one byte"),
			   "LC_CTYPE", tmp);
		}
	      else
		/* The index [0] is determined by the order of the
		   `ctype_map_newP' calls in `ctype_startup'.  */
		ctype->map256_collection[0][seq_from->bytes[0]]
		  = seq_to->bytes[0];
	    }
	}
    }

  if (ctype->tomap_done[1] == 0)
    /* "If this keyword [tolower] is not specified, the mapping shall be
       the reverse mapping of the one specified to `toupper'."  [P1003.2]  */
    {
      for (cnt = 0; cnt < ctype->map_collection_act[0]; ++cnt)
	if (ctype->map_collection[0][cnt] != 0)
	  ELEM (ctype, map_collection, [1],
		ctype->map_collection[0][cnt])
	    = ctype->charnames[cnt];

      for (cnt = 0; cnt < 256; ++cnt)
	if (ctype->map256_collection[0][cnt] != 0)
	  ctype->map_collection[1][ctype->map_collection[0][cnt]]
	    = ctype->charnames[cnt];
    }

  if (ctype->outdigits_act == 0)
    {
      for (cnt = 0; cnt < 10; ++cnt)
	{
	  ctype->mboutdigits[cnt] = charmap_find_symbol (charmap,
							 digits + cnt, 1);

	  if (ctype->mboutdigits[cnt] == NULL)
	    {
	      ctype->mboutdigits[cnt] = charmap_find_symbol (charmap,
							     longnames[cnt],
							     strlen (longnames[cnt]));

	      if (ctype->mboutdigits[cnt] == NULL)
		{
		  /* Provide a replacement.  */
		  error (0, 0, _("\
no output digits defined and none of the standard names in the charmap"));

		  ctype->mboutdigits[cnt] = obstack_alloc (&charmap->mem_pool,
							   sizeof (struct charseq) + 1);

		  /* This is better than nothing.  */
		  ctype->mboutdigits[cnt]->bytes[0] = digits[cnt];
		  ctype->mboutdigits[cnt]->nbytes = 1;
		}
	    }

	  ctype->wcoutdigits[cnt] = repertoire_find_value (repertoire,
							   digits + cnt, 1);

	  if (ctype->wcoutdigits[cnt] == ILLEGAL_CHAR_VALUE)
	    {
	      ctype->wcoutdigits[cnt] = repertoire_find_value (repertoire,
							       longnames[cnt],
							       strlen (longnames[cnt]));

	      if (ctype->wcoutdigits[cnt] == ILLEGAL_CHAR_VALUE)
		{
		  /* Provide a replacement.  */
		  error (0, 0, _("\
no output digits defined and none of the standard names in the repertoire"));

		  /* This is better than nothing.  */
		  ctype->wcoutdigits[cnt] = (uint32_t) digits[cnt];
		}
	    }
	}

      ctype->outdigits_act = 10;
    }
}


static void
allocate_arrays (struct locale_ctype_t *ctype, struct charmap_t *charmap,
		 struct repertoire_t *repertoire)
{
  size_t idx;

  /* First we have to decide how we organize the arrays.  It is easy
     for a one-byte character set.  But multi-byte character set
     cannot be stored flat because the chars might be sparsely used.
     So we determine an optimal hashing function for the used
     characters.

     We use a very trivial hashing function to store the sparse
     table.  CH % TABSIZE is used as an index.  To solve multiple hits
     we have N planes.  This guarantees a fixed search time for a
     character [N / 2].  In the following code we determine the minimum
     value for TABSIZE * N, where TABSIZE >= 256.  */
  size_t min_total = UINT_MAX;
  size_t act_size = 256;

  if (!be_quiet)
    fputs (_("\
Computing table size for character classes might take a while..."),
	   stderr);

  while (act_size < min_total)
    {
      size_t cnt[act_size];
      size_t act_planes = 1;

      memset (cnt, '\0', sizeof cnt);

      for (idx = 0; idx < 256; ++idx)
	cnt[idx] = 1;

      for (idx = 0; idx < ctype->charnames_act; ++idx)
	if (ctype->charnames[idx] >= 256)
	  {
	    size_t nr = ctype->charnames[idx] % act_size;

	    if (++cnt[nr] > act_planes)
	      {
		act_planes = cnt[nr];
		if (act_size * act_planes >= min_total)
		  break;
	      }
	  }

      if (act_size * act_planes < min_total)
	{
	  min_total = act_size * act_planes;
	  ctype->plane_size = act_size;
	  ctype->plane_cnt = act_planes;
	}

      ++act_size;
    }

  if (!be_quiet)
    fputs (_(" done\n"), stderr);


  ctype->names = (uint32_t *) xcalloc (ctype->plane_size
				       * ctype->plane_cnt,
				       sizeof (uint32_t));

  for (idx = 1; idx < 256; ++idx)
    ctype->names[idx] = idx;

  /* Trick: change the 0th entry's name to 1 to mark the cell occupied.  */
  ctype->names[0] = 1;

  for (idx = 256; idx < ctype->charnames_act; ++idx)
    {
      size_t nr = (ctype->charnames[idx] % ctype->plane_size);
      size_t depth = 0;

      while (ctype->names[nr + depth * ctype->plane_size])
	++depth;
      assert (depth < ctype->plane_cnt);

      ctype->names[nr + depth * ctype->plane_size] = ctype->charnames[idx];

      /* Now for faster access remember the index in the NAMES_B array.  */
      ctype->charnames[idx] = nr + depth * ctype->plane_size;
    }
  ctype->names[0] = 0;


  /* You wonder about this amount of memory?  This is only because some
     users do not manage to address the array with unsigned values or
     data types with range >= 256.  '\200' would result in the array
     index -128.  To help these poor people we duplicate the entries for
     128 up to 255 below the entry for \0.  */
  ctype->ctype_b = (char_class_t *) xcalloc (256 + 128,
					     sizeof (char_class_t));
  ctype->ctype32_b = (char_class32_t *) xcalloc (ctype->plane_size
						 * ctype->plane_cnt,
						 sizeof (char_class32_t));

  /* This is the array accessed using the multibyte string elements.  */
  for (idx = 0; idx < 256; ++idx)
    ctype->ctype_b[128 + idx] = ctype->class256_collection[idx];

  /* Mirror first 127 entries.  We must take care that entry -1 is not
     mirrored because EOF == -1.  */
  for (idx = 0; idx < 127; ++idx)
    ctype->ctype_b[idx] = ctype->ctype_b[256 + idx];

  /* The 32 bit array contains all characters.  */
  for (idx = 0; idx < ctype->class_collection_act; ++idx)
    ctype->ctype32_b[ctype->charnames[idx]] = ctype->class_collection[idx];

  /* Room for table of mappings.  */
  ctype->map = (uint32_t **) xmalloc (ctype->map_collection_nr
				      * sizeof (uint32_t *));

  /* Fill in all mappings.  */
  for (idx = 0; idx < ctype->map_collection_nr; ++idx)
    {
      unsigned int idx2;

      /* Allocate table.  */
      ctype->map[idx] = (uint32_t *) xmalloc ((ctype->plane_size
					       * ctype->plane_cnt + 128)
					      * sizeof (uint32_t));

      /* Copy default value (identity mapping).  */
      memcpy (&ctype->map[idx][128], ctype->names,
	      ctype->plane_size * ctype->plane_cnt * sizeof (uint32_t));

      /* Copy values from collection.  */
      for (idx2 = 0; idx2 < 256; ++idx2)
	ctype->map[idx][128 + idx2] = ctype->map256_collection[idx][idx2];

      /* Mirror first 127 entries.  We must take care not to map entry
	 -1 because EOF == -1.  */
      for (idx2 = 0; idx2 < 127; ++idx2)
	ctype->map[idx][idx2] = ctype->map[idx][256 + idx2];

      /* EOF must map to EOF.  */
      ctype->map[idx][127] = EOF;
    }

  /* Extra array for class and map names.  */
  ctype->class_name_ptr = (uint32_t *) xmalloc (ctype->nr_charclass
						* sizeof (uint32_t));
  ctype->map_name_ptr = (uint32_t *) xmalloc (ctype->map_collection_nr
					      * sizeof (uint32_t));

  /* Array for width information.  Because the expected width are very
     small we use only one single byte.  This save space and we need
     not provide the information twice with both endianesses.  */
  ctype->width = (unsigned char *) xmalloc (ctype->plane_size
					    * ctype->plane_cnt);
  /* Initialize with default width value.  */
  memset (ctype->width, charmap->width_default,
	  ctype->plane_size * ctype->plane_cnt);
  if (charmap->width_rules != NULL)
    {
#if 0
      size_t cnt;

      for (cnt = 0; cnt < charmap->nwidth_rules; ++cnt)
	if (charmap->width_rules[cnt].width != charmap->width_default)
	  for (idx = charmap->width_rules[cnt].from;
	       idx <= charmap->width_rules[cnt].to; ++idx)
	    {
	      size_t nr = idx % ctype->plane_size;
	      size_t depth = 0;

	      while (ctype->names[nr + depth * ctype->plane_size] != nr)
		++depth;
	      assert (depth < ctype->plane_cnt);

	      ctype->width[nr + depth * ctype->plane_size]
		= charmap->width_rules[cnt].width;
	    }
#else
      abort ();
#endif
    }

  /* Set MB_CUR_MAX.  */
  ctype->mb_cur_max = charmap->mb_cur_max;

  /* We need the name of the currently used 8-bit character set to
     make correct conversion between this 8-bit representation and the
     ISO 10646 character set used internally for wide characters.  */
  ctype->codeset_name = charmap->code_set_name;

  /* Now determine the table for the transliteration information.

     XXX It is not yet clear to me whether it is worth implementing a
     complicated algorithm which uses a hash table to locate the entries.
     For now I'll use a simple array which can be searching using binary
     search.  */
  if (ctype->translit_copy_locale != NULL)
    {
      /* Fold in the transliteration information from the locale mentioned
	 in the `include' statement.  */
      struct locale_ctype_t *here = ctype;

      do
	{
	  struct localedef_t *other = find_locale (LC_CTYPE,
						   here->translit_copy_locale,
						   repertoire->name, charmap);

	  if (other == NULL)
	    {
	      error (0, 0, _("\
%s: transliteration data from locale `%s' not available"),
		     "LC_CTYPE", here->translit_copy_locale);
	      break;
	    }

	  here = other->categories[LC_CTYPE].ctype;

	  /* Enqueue the information if necessary.  */
	  if (here->translit != NULL)
	    {
	      struct translit_t *endp = here->translit;
	      while (endp->next != NULL)
		endp = endp->next;

	      endp->next = ctype->translit;
	      ctype->translit = here->translit;
	    }
	}
      while (here->translit_copy_locale != NULL);
    }

  if (ctype->translit != NULL)
    {
      /* First count how many entries we have.  This is the upper limit
	 since some entries from the included files might be overwritten.  */
      size_t number = 0;
      size_t cnt;
      struct translit_t *runp = ctype->translit;
      struct translit_t **sorted;
      size_t from_len, to_len;

      while (runp != NULL)
	{
	  ++number;
	  runp = runp->next;
	}

      /* Next we allocate an array large enough and fill in the values.  */
      sorted = alloca (number * sizeof (struct translit_t **));
      runp = ctype->translit;
      number = 0;
      do
	{
	  /* Search for the place where to insert this string.
	     XXX Better use a real sorting algorithm later.  */
	  size_t idx = 0;
	  int replace = 0;

	  while (idx < number)
	    {
	      int res = wcscmp ((const wchar_t *) sorted[idx]->from,
				(const wchar_t *) runp->from);
	      if (res == 0)
		{
		  replace = 1;
		  break;
		}
	      if (res > 0)
		break;
	      ++idx;
	    }

	  if (replace)
	    sorted[idx] = runp;
	  else
	    {
	      memmove (&sorted[idx + 1], &sorted[idx],
		       (number - idx) * sizeof (struct translit_t *));
	      sorted[idx] = runp;
	      ++number;
	    }

	  runp = runp->next;
	}
      while (runp != NULL);

      /* The next step is putting all the possible transliteration
	 strings in one memory block so that we can write it out.
	 We need several different blocks:
	 - index to the tfromstring array
	 - from-string array
	 - index to the to-string array
	 - to-string array.
	 And this all must be available for both endianes variants.
      */
      from_len = to_len = 0;
      for (cnt = 0; cnt < number; ++cnt)
	{
	  struct translit_to_t *srunp;
	  from_len += wcslen ((const wchar_t *) sorted[cnt]->from) + 1;
	  srunp = sorted[cnt]->to;
	  while (srunp != NULL)
	    {
	      to_len += wcslen ((const wchar_t *) srunp->str) + 1;
	      srunp = srunp->next;
	    }
	  /* Plus one for the extra NUL character marking the end of
	     the list for the current entry.  */
	  ++to_len;
	}

      /* We can allocate the arrays for the results.  */
      ctype->translit_from_idx = xmalloc (number * sizeof (uint32_t));
      ctype->translit_from_tbl = xmalloc (from_len * sizeof (uint32_t));
      ctype->translit_to_idx = xmalloc (number * sizeof (uint32_t));
      ctype->translit_to_tbl = xmalloc (to_len * sizeof (uint32_t));

      from_len = 0;
      to_len = 0;
      for (cnt = 0; cnt < number; ++cnt)
	{
	  size_t len;
	  struct translit_to_t *srunp;

	  ctype->translit_from_idx[cnt] = from_len;
	  ctype->translit_to_idx[cnt] = to_len;

	  len = wcslen ((const wchar_t *) sorted[cnt]->from) + 1;
	  wmemcpy ((wchar_t *) &ctype->translit_from_tbl[from_len],
		   (const wchar_t *) sorted[cnt]->from, len);
	  from_len += len;

	  ctype->translit_to_idx[cnt] = to_len;
	  srunp = sorted[cnt]->to;
	  while (srunp != NULL)
	    {
	      len = wcslen ((const wchar_t *) srunp->str) + 1;
	      wmemcpy ((wchar_t *) &ctype->translit_to_tbl[to_len],
		       (const wchar_t *) srunp->str, len);
	      to_len += len;
	      srunp = srunp->next;
	    }
	  ctype->translit_to_tbl[to_len++] = L'\0';
	}

      /* Store the information about the length.  */
      ctype->translit_idx_size = number * sizeof (uint32_t);
      ctype->translit_from_tbl_size = from_len * sizeof (uint32_t);
      ctype->translit_to_tbl_size = to_len * sizeof (uint32_t);
    }
  else
    {
      /* Provide some dummy pointers since we have nothing to write out.  */
      static uint32_t no_str = { 0 };

      ctype->translit_from_idx = &no_str;
      ctype->translit_from_tbl = &no_str;
      ctype->translit_to_tbl = &no_str;
      ctype->translit_idx_size = 0;
      ctype->translit_from_tbl_size = 0;
      ctype->translit_to_tbl_size = 0;
    }
}