summary refs log tree commit diff
path: root/locale/programs/ld-collate.c
blob: 63c379ae844b9af9a7f29ce568d09bfc4dd9bf32 (plain) (blame)
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
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
/* Copyright (C) 1995-1999, 2000, 2001 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 Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; 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 <errno.h>
#include <error.h>
#include <stdlib.h>
#include <wchar.h>
#include <sys/param.h>

#include "charmap.h"
#include "localeinfo.h"
#include "linereader.h"
#include "locfile.h"
#include "localedef.h"
#include "elem-hash.h"

/* Uncomment the following line in the production version.  */
/* #define NDEBUG 1 */
#include <assert.h>

#define obstack_chunk_alloc malloc
#define obstack_chunk_free free

static inline void
obstack_int32_grow (struct obstack *obstack, int32_t data)
{
  if (sizeof (int32_t) == sizeof (int))
    obstack_int_grow (obstack, data);
  else
    obstack_grow (obstack, &data, sizeof (int32_t));
}

static inline void
obstack_int32_grow_fast (struct obstack *obstack, int32_t data)
{
  if (sizeof (int32_t) == sizeof (int))
    obstack_int_grow_fast (obstack, data);
  else
    obstack_grow (obstack, &data, sizeof (int32_t));
}

/* Forward declaration.  */
struct element_t;

/* Data type for list of strings.  */
struct section_list
{
  /* Successor in the known_sections list.  */
  struct section_list *def_next;
  /* Successor in the sections list.  */
  struct section_list *next;
  /* Name of the section.  */
  const char *name;
  /* First element of this section.  */
  struct element_t *first;
  /* Last element of this section.  */
  struct element_t *last;
  /* These are the rules for this section.  */
  enum coll_sort_rule *rules;
  /* Index of the rule set in the appropriate section of the output file.  */
  int ruleidx;
};

struct element_t;

struct element_list_t
{
  /* Number of elements.  */
  int cnt;

  struct element_t **w;
};

/* Data type for collating element.  */
struct element_t
{
  const char *name;

  const char *mbs;
  size_t nmbs;
  const uint32_t *wcs;
  size_t nwcs;
  int *mborder;
  int wcorder;

  /* The following is a bit mask which bits are set if this element is
     used in the appropriate level.  Interesting for the singlebyte
     weight computation.

     XXX The type here restricts the number of levels to 32.  It could
     be changed if necessary but I doubt this is necessary.  */
  unsigned int used_in_level;

  struct element_list_t *weights;

  /* Nonzero if this is a real character definition.  */
  int is_character;

  /* Order of the character in the sequence.  This information will
     be used in range expressions.  */
  int mbseqorder;
  int wcseqorder;

  /* Where does the definition come from.  */
  const char *file;
  size_t line;

  /* Which section does this belong to.  */
  struct section_list *section;

  /* Predecessor and successor in the order list.  */
  struct element_t *last;
  struct element_t *next;

  /* Next element in multibyte output list.  */
  struct element_t *mbnext;
  struct element_t *mblast;

  /* Next element in wide character output list.  */
  struct element_t *wcnext;
  struct element_t *wclast;
};

/* Special element value.  */
#define ELEMENT_ELLIPSIS2	((struct element_t *) 1)
#define ELEMENT_ELLIPSIS3	((struct element_t *) 2)
#define ELEMENT_ELLIPSIS4	((struct element_t *) 3)

/* Data type for collating symbol.  */
struct symbol_t
{
  const char *name;

  /* Point to place in the order list.  */
  struct element_t *order;

  /* Where does the definition come from.  */
  const char *file;
  size_t line;
};

/* Sparse table of struct element_t *.  */
#define TABLE wchead_table
#define ELEMENT struct element_t *
#define DEFAULT NULL
#define ITERATE
#define NO_FINALIZE
#include "3level.h"

/* Sparse table of int32_t.  */
#define TABLE collidx_table
#define ELEMENT int32_t
#define DEFAULT 0
#include "3level.h"

/* Sparse table of uint32_t.  */
#define TABLE collseq_table
#define ELEMENT uint32_t
#define DEFAULT ~((uint32_t) 0)
#include "3level.h"


/* The real definition of the struct for the LC_COLLATE locale.  */
struct locale_collate_t
{
  int col_weight_max;
  int cur_weight_max;

  /* List of known scripts.  */
  struct section_list *known_sections;
  /* List of used sections.  */
  struct section_list *sections;
  /* Current section using definition.  */
  struct section_list *current_section;
  /* There always can be an unnamed section.  */
  struct section_list unnamed_section;
  /* To make handling of errors easier we have another section.  */
  struct section_list error_section;
  /* Sometimes we are defining the values for collating symbols before
     the first actual section.  */
  struct section_list symbol_section;

  /* Start of the order list.  */
  struct element_t *start;

  /* The undefined element.  */
  struct element_t undefined;

  /* This is the cursor for `reorder_after' insertions.  */
  struct element_t *cursor;

  /* This value is used when handling ellipsis.  */
  struct element_t ellipsis_weight;

  /* Known collating elements.  */
  hash_table elem_table;

  /* Known collating symbols.  */
  hash_table sym_table;

  /* Known collation sequences.  */
  hash_table seq_table;

  struct obstack mempool;

  /* The LC_COLLATE category is a bit special as it is sometimes possible
     that the definitions from more than one input file contains information.
     Therefore we keep all relevant input in a list.  */
  struct locale_collate_t *next;

  /* Arrays with heads of the list for each of the leading bytes in
     the multibyte sequences.  */
  struct element_t *mbheads[256];

  /* Arrays with heads of the list for each of the leading bytes in
     the multibyte sequences.  */
  struct wchead_table wcheads;

  /* The arrays with the collation sequence order.  */
  unsigned char mbseqorder[256];
  struct collseq_table wcseqorder;
};


/* We have a few global variables which are used for reading all
   LC_COLLATE category descriptions in all files.  */
static uint32_t nrules;


/* We need UTF-8 encoding of numbers.  */
static inline int
utf8_encode (char *buf, int val)
{
  int retval;

  if (val < 0x80)
    {
      *buf++ = (char) val;
      retval = 1;
    }
  else
    {
      int step;

      for (step = 2; step < 6; ++step)
	if ((val & (~(uint32_t)0 << (5 * step + 1))) == 0)
	  break;
      retval = step;

      *buf = (unsigned char) (~0xff >> step);
      --step;
      do
	{
	  buf[step] = 0x80 | (val & 0x3f);
	  val >>= 6;
	}
      while (--step > 0);
      *buf |= val;
    }

  return retval;
}


static struct section_list *
make_seclist_elem (struct locale_collate_t *collate, const char *string,
		   struct section_list *next)
{
  struct section_list *newp;

  newp = (struct section_list *) obstack_alloc (&collate->mempool,
						sizeof (*newp));
  newp->next = next;
  newp->name = string;
  newp->first = NULL;
  newp->last = NULL;

  return newp;
}


static struct element_t *
new_element (struct locale_collate_t *collate, const char *mbs, size_t mbslen,
	     const uint32_t *wcs, const char *name, size_t namelen,
	     int is_character)
{
  struct element_t *newp;

  newp = (struct element_t *) obstack_alloc (&collate->mempool,
					     sizeof (*newp));
  newp->name = name == NULL ? NULL : obstack_copy0 (&collate->mempool,
						    name, namelen);
  if (mbs != NULL)
    {
      newp->mbs = obstack_copy0 (&collate->mempool, mbs, mbslen);
      newp->nmbs = mbslen;
    }
  else
    {
      newp->mbs = NULL;
      newp->nmbs = 0;
    }
  if (wcs != NULL)
    {
      size_t nwcs = wcslen ((wchar_t *) wcs);
      uint32_t zero = 0;
      obstack_grow (&collate->mempool, wcs, nwcs * sizeof (uint32_t));
      obstack_grow (&collate->mempool, &zero, sizeof (uint32_t));
      newp->wcs = (uint32_t *) obstack_finish (&collate->mempool);
      newp->nwcs = nwcs;
    }
  else
    {
      newp->wcs = NULL;
      newp->nwcs = 0;
    }
  newp->mborder = NULL;
  newp->wcorder = 0;
  newp->used_in_level = 0;
  newp->is_character = is_character;

  /* Will be assigned later.  XXX  */
  newp->mbseqorder = 0;
  newp->wcseqorder = 0;

  /* Will be allocated later.  */
  newp->weights = NULL;

  newp->file = NULL;
  newp->line = 0;

  newp->section = collate->current_section;

  newp->last = NULL;
  newp->next = NULL;

  newp->mbnext = NULL;
  newp->mblast = NULL;

  newp->wcnext = NULL;
  newp->wclast = NULL;

  return newp;
}


static struct symbol_t *
new_symbol (struct locale_collate_t *collate, const char *name, size_t len)
{
  struct symbol_t *newp;

  newp = (struct symbol_t *) obstack_alloc (&collate->mempool, sizeof (*newp));

  newp->name = obstack_copy0 (&collate->mempool, name, len);
  newp->order = NULL;

  newp->file = NULL;
  newp->line = 0;

  return newp;
}


/* Test whether this name is already defined somewhere.  */
static int
check_duplicate (struct linereader *ldfile, struct locale_collate_t *collate,
		 struct charmap_t *charmap, struct repertoire_t *repertoire,
		 const char *symbol, size_t symbol_len)
{
  void *ignore = NULL;

  if (find_entry (&charmap->char_table, symbol, symbol_len, &ignore) == 0)
    {
      lr_error (ldfile, _("`%.*s' already defined in charmap"),
		(int) symbol_len, symbol);
      return 1;
    }

  if (repertoire != NULL
      && (find_entry (&repertoire->char_table, symbol, symbol_len, &ignore)
	  == 0))
    {
      lr_error (ldfile, _("`%.*s' already defined in repertoire"),
		(int) symbol_len, symbol);
      return 1;
    }

  if (find_entry (&collate->sym_table, symbol, symbol_len, &ignore) == 0)
    {
      lr_error (ldfile, _("`%.*s' already defined as collating symbol"),
		(int) symbol_len, symbol);
      return 1;
    }

  if (find_entry (&collate->elem_table, symbol, symbol_len, &ignore) == 0)
    {
      lr_error (ldfile, _("`%.*s' already defined as collating element"),
		(int) symbol_len, symbol);
      return 1;
    }

  return 0;
}


/* Read the direction specification.  */
static void
read_directions (struct linereader *ldfile, struct token *arg,
		 struct charmap_t *charmap, struct repertoire_t *repertoire,
		 struct locale_collate_t *collate)
{
  int cnt = 0;
  int max = nrules ?: 10;
  enum coll_sort_rule *rules = calloc (max, sizeof (*rules));
  int warned = 0;

  while (1)
    {
      int valid = 0;

      if (arg->tok == tok_forward)
	{
	  if (rules[cnt] & sort_backward)
	    {
	      if (! warned)
		{
		  lr_error (ldfile, _("\
%s: `forward' and `backward' are mutually excluding each other"),
			    "LC_COLLATE");
		  warned = 1;
		}
	    }
	  else if (rules[cnt] & sort_forward)
	    {
	      if (! warned)
		{
		  lr_error (ldfile, _("\
%s: `%s' mentioned more than once in definition of weight %d"),
			    "LC_COLLATE", "forward", cnt + 1);
		}
	    }
	  else
	    rules[cnt] |= sort_forward;

	  valid = 1;
	}
      else if (arg->tok == tok_backward)
	{
	  if (rules[cnt] & sort_forward)
	    {
	      if (! warned)
		{
		  lr_error (ldfile, _("\
%s: `forward' and `backward' are mutually excluding each other"),
			    "LC_COLLATE");
		  warned = 1;
		}
	    }
	  else if (rules[cnt] & sort_backward)
	    {
	      if (! warned)
		{
		  lr_error (ldfile, _("\
%s: `%s' mentioned more than once in definition of weight %d"),
			    "LC_COLLATE", "backward", cnt + 1);
		}
	    }
	  else
	    rules[cnt] |= sort_backward;

	  valid = 1;
	}
      else if (arg->tok == tok_position)
	{
	  if (rules[cnt] & sort_position)
	    {
	      if (! warned)
		{
		  lr_error (ldfile, _("\
%s: `%s' mentioned more than once in definition of weight %d"),
			    "LC_COLLATE", "position", cnt + 1);
		}
	    }
	  else
	    rules[cnt] |= sort_position;

	  valid = 1;
	}

      if (valid)
	arg = lr_token (ldfile, charmap, repertoire, verbose);

      if (arg->tok == tok_eof || arg->tok == tok_eol || arg->tok == tok_comma
	  || arg->tok == tok_semicolon)
	{
	  if (! valid && ! warned)
	    {
	      lr_error (ldfile, _("%s: syntax error"), "LC_COLLATE");
	      warned = 1;
	    }

	  /* See whether we have to increment the counter.  */
	  if (arg->tok != tok_comma && rules[cnt] != 0)
	    {
	      /* Add the default `forward' if we have seen only `position'.  */
	      if (rules[cnt] == sort_position)
		rules[cnt] = sort_position | sort_forward;

	      ++cnt;
	    }

	  if (arg->tok == tok_eof || arg->tok == tok_eol)
	    /* End of line or file, so we exit the loop.  */
	    break;

	  if (nrules == 0)
	    {
	      /* See whether we have enough room in the array.  */
	      if (cnt == max)
		{
		  max += 10;
		  rules = (enum coll_sort_rule *) xrealloc (rules,
							    max
							    * sizeof (*rules));
		  memset (&rules[cnt], '\0', (max - cnt) * sizeof (*rules));
		}
	    }
	  else
	    {
	      if (cnt == nrules)
		{
		  /* There must not be any more rule.  */
		  if (! warned)
		    {
		      lr_error (ldfile, _("\
%s: too many rules; first entry only had %d"),
				"LC_COLLATE", nrules);
		      warned = 1;
		    }

		  lr_ignore_rest (ldfile, 0);
		  break;
		}
	    }
	}
      else
	{
	  if (! warned)
	    {
	      lr_error (ldfile, _("%s: syntax error"), "LC_COLLATE");
	      warned = 1;
	    }
	}

      arg = lr_token (ldfile, charmap, repertoire, verbose);
    }

  if (nrules == 0)
    {
      /* Now we know how many rules we have.  */
      nrules = cnt;
      rules = (enum coll_sort_rule *) xrealloc (rules,
						nrules * sizeof (*rules));
    }
  else
    {
      if (cnt < nrules)
	{
	  /* Not enough rules in this specification.  */
	  if (! warned)
	    lr_error (ldfile, _("%s: not enough sorting rules"), "LC_COLLATE");

	  do
	    rules[cnt] = sort_forward;
	  while (++cnt < nrules);
	}
    }

  collate->current_section->rules = rules;
}


static struct element_t *
find_element (struct linereader *ldfile, struct locale_collate_t *collate,
	      const char *str, size_t len)
{
  struct element_t *result = NULL;

  /* Search for the entries among the collation sequences already define.  */
  if (find_entry (&collate->seq_table, str, len, (void **) &result) != 0)
    {
      /* Nope, not define yet.  So we see whether it is a
         collation symbol.  */
      void *ptr;

      if (find_entry (&collate->sym_table, str, len, &ptr) == 0)
	{
	  /* It's a collation symbol.  */
	  struct symbol_t *sym = (struct symbol_t *) ptr;
	  result = sym->order;

	  if (result == NULL)
	    result = sym->order = new_element (collate, NULL, 0, NULL,
					       NULL, 0, 0);
	}
      else if (find_entry (&collate->elem_table, str, len,
			   (void **) &result) != 0)
	{
	  /* It's also no collation element.  So it is a character
	     element defined later.  */
	  result = new_element (collate, NULL, 0, NULL, str, len, 1);
	  /* Insert it into the sequence table.  */
	  insert_entry (&collate->seq_table, str, len, result);
	}
    }

  return result;
}


static void
unlink_element (struct locale_collate_t *collate)
{
  if (collate->cursor == collate->start)
    {
      assert (collate->cursor->next == NULL);
      assert (collate->cursor->last == NULL);
      collate->cursor = NULL;
    }
  else
    {
      if (collate->cursor->next != NULL)
	collate->cursor->next->last = collate->cursor->last;
      if (collate->cursor->last != NULL)
	collate->cursor->last->next = collate->cursor->next;
      collate->cursor = collate->cursor->last;
    }
}


static void
insert_weights (struct linereader *ldfile, struct element_t *elem,
		struct charmap_t *charmap, struct repertoire_t *repertoire,
		struct locale_collate_t *collate, enum token_t ellipsis)
{
  int weight_cnt;
  struct token *arg;

  /* Initialize all the fields.  */
  elem->file = ldfile->fname;
  elem->line = ldfile->lineno;

  elem->last = collate->cursor;
  elem->next = collate->cursor ? collate->cursor->next : NULL;
  if (collate->cursor != NULL && collate->cursor->next != NULL)
    collate->cursor->next->last = elem;
  if (collate->cursor != NULL)
    collate->cursor->next = elem;
  if (collate->start == NULL)
    {
      assert (collate->cursor == NULL);
      collate->start = elem;
    }

  elem->section = collate->current_section;

  if (collate->current_section->first == NULL)
    collate->current_section->first = elem;
  if (collate->current_section->last == collate->cursor)
    collate->current_section->last = elem;

  collate->cursor = elem;

  elem->weights = (struct element_list_t *)
    obstack_alloc (&collate->mempool, nrules * sizeof (struct element_list_t));
  memset (elem->weights, '\0', nrules * sizeof (struct element_list_t));

  weight_cnt = 0;

  arg = lr_token (ldfile, charmap, repertoire, verbose);
  do
    {
      if (arg->tok == tok_eof || arg->tok == tok_eol)
	break;

      if (arg->tok == tok_ignore)
	{
	  /* The weight for this level has to be ignored.  We use the
	     null pointer to indicate this.  */
	  elem->weights[weight_cnt].w = (struct element_t **)
	    obstack_alloc (&collate->mempool, sizeof (struct element_t *));
	  elem->weights[weight_cnt].w[0] = NULL;
	  elem->weights[weight_cnt].cnt = 1;
	}
      else if (arg->tok == tok_bsymbol || arg->tok == tok_ucs4)
	{
	  char ucs4str[10];
	  struct element_t *val;
	  char *symstr;
	  size_t symlen;

	  if (arg->tok == tok_bsymbol)
	    {
	      symstr = arg->val.str.startmb;
	      symlen = arg->val.str.lenmb;
	    }
	  else
	    {
	      snprintf (ucs4str, sizeof (ucs4str), "U%08X", arg->val.ucs4);
	      symstr = ucs4str;
	      symlen = 9;
	    }

	  val = find_element (ldfile, collate, symstr, symlen);
	  if (val == NULL)
	    break;

	  elem->weights[weight_cnt].w = (struct element_t **)
	    obstack_alloc (&collate->mempool, sizeof (struct element_t *));
	  elem->weights[weight_cnt].w[0] = val;
	  elem->weights[weight_cnt].cnt = 1;
	}
      else if (arg->tok == tok_string)
	{
	  /* Split the string up in the individual characters and put
	     the element definitions in the list.  */
	  const char *cp = arg->val.str.startmb;
	  int cnt = 0;
	  struct element_t *charelem;
	  struct element_t **weights = NULL;
	  int max = 0;

	  if (*cp == '\0')
	    {
	      lr_error (ldfile, _("%s: empty weight string not allowed"),
			"LC_COLLATE");
	      lr_ignore_rest (ldfile, 0);
	      break;
	    }

	  do
	    {
	      if (*cp == '<')
		{
		  /* Ahh, it's a bsymbol or an UCS4 value.  If it's
                     the latter we have to unify the name.  */
		  const char *startp = ++cp;
		  size_t len;

		  while (*cp != '>')
		    {
		      if (*cp == ldfile->escape_char)
			++cp;
		      if (*cp == '\0')
			/* It's a syntax error.  */
			goto syntax;

		      ++cp;
		    }

		  if (cp - startp == 5 && startp[0] == 'U'
		      && isxdigit (startp[1]) && isxdigit (startp[2])
		      && isxdigit (startp[3]) && isxdigit (startp[4]))
		    {
		      unsigned int ucs4 = strtoul (startp + 1, NULL, 16);
		      char *newstr;

		      newstr = (char *) xmalloc (10);
		      snprintf (newstr, 10, "U%08X", ucs4);
		      startp = newstr;

		      len = 9;
		    }
		  else
		    len = cp - startp;

		  charelem = find_element (ldfile, collate, startp, len);
		  ++cp;
		}
	      else
		{
		  /* People really shouldn't use characters directly in
		     the string.  Especially since it's not really clear
		     what this means.  We interpret all characters in the
		     string as if that would be bsymbols.  Otherwise we
		     would have to match back to bsymbols somehow and this
		     is normally not what people normally expect.  */
		  charelem = find_element (ldfile, collate, cp++, 1);
		}

	      if (charelem == NULL)
		{
		  /* We ignore the rest of the line.  */
		  lr_ignore_rest (ldfile, 0);
		  break;
		}

	      /* Add the pointer.  */
	      if (cnt >= max)
		{
		  struct element_t **newp;
		  max += 10;
		  newp = (struct element_t **)
		    alloca (max * sizeof (struct element_t *));
		  memcpy (newp, weights, cnt * sizeof (struct element_t *));
		  weights = newp;
		}
	      weights[cnt++] = charelem;
	    }
	  while (*cp != '\0');

	  /* Now store the information.  */
	  elem->weights[weight_cnt].w = (struct element_t **)
	    obstack_alloc (&collate->mempool,
			   cnt * sizeof (struct element_t *));
	  memcpy (elem->weights[weight_cnt].w, weights,
		  cnt * sizeof (struct element_t *));
	  elem->weights[weight_cnt].cnt = cnt;

	  /* We don't need the string anymore.  */
	  free (arg->val.str.startmb);
	}
      else if (ellipsis != tok_none
	       && (arg->tok == tok_ellipsis2
		   || arg->tok == tok_ellipsis3
		   || arg->tok == tok_ellipsis4))
	{
	  /* It must be the same ellipsis as used in the initial column.  */
	  if (arg->tok != ellipsis)
	    lr_error (ldfile, _("\
%s: weights must use the same ellipsis symbol as the name"),
		      "LC_COLLATE");

	  /* The weight for this level will depend on the element
	     iterating over the range.  Put a placeholder.  */
	  elem->weights[weight_cnt].w = (struct element_t **)
	    obstack_alloc (&collate->mempool, sizeof (struct element_t *));
	  elem->weights[weight_cnt].w[0] = ELEMENT_ELLIPSIS2;
	  elem->weights[weight_cnt].cnt = 1;
	}
      else
	{
	syntax:
	  /* It's a syntax error.  */
	  lr_error (ldfile, _("%s: syntax error"), "LC_COLLATE");
	  lr_ignore_rest (ldfile, 0);
	  break;
	}

      arg = lr_token (ldfile, charmap, repertoire, verbose);
      /* This better should be the end of the line or a semicolon.  */
      if (arg->tok == tok_semicolon)
	/* OK, ignore this and read the next token.  */
	arg = lr_token (ldfile, charmap, repertoire, verbose);
      else if (arg->tok != tok_eof && arg->tok != tok_eol)
	{
	  /* It's a syntax error.  */
	  lr_error (ldfile, _("%s: syntax error"), "LC_COLLATE");
	  lr_ignore_rest (ldfile, 0);
	  break;
	}
    }
  while (++weight_cnt < nrules);

  if (weight_cnt < nrules)
    {
      /* This means the rest of the line uses the current element as
	 the weight.  */
      do
	{
	  elem->weights[weight_cnt].w = (struct element_t **)
	    obstack_alloc (&collate->mempool, sizeof (struct element_t *));
	  if (ellipsis == tok_none)
	    elem->weights[weight_cnt].w[0] = elem;
	  else
	    elem->weights[weight_cnt].w[0] = ELEMENT_ELLIPSIS2;
	  elem->weights[weight_cnt].cnt = 1;
	}
      while (++weight_cnt < nrules);
    }
  else
    {
      if (arg->tok == tok_ignore || arg->tok == tok_bsymbol)
	{
	  /* Too many rule values.  */
	  lr_error (ldfile, _("%s: too many values"), "LC_COLLATE");
	  lr_ignore_rest (ldfile, 0);
	}
      else
	lr_ignore_rest (ldfile, arg->tok != tok_eol && arg->tok != tok_eof);
    }
}


static int
insert_value (struct linereader *ldfile, const char *symstr, size_t symlen,
	      struct charmap_t *charmap, struct repertoire_t *repertoire,
	      struct locale_collate_t *collate)
{
  /* First find out what kind of symbol this is.  */
  struct charseq *seq;
  uint32_t wc;
  struct element_t *elem = NULL;

  /* Try to find the character in the charmap.  */
  seq = charmap_find_value (charmap, symstr, symlen);

  /* Determine the wide character.  */
  if (seq == NULL || seq->ucs4 == UNINITIALIZED_CHAR_VALUE)
    {
      wc = repertoire_find_value (repertoire, symstr, symlen);
      if (seq != NULL)
	seq->ucs4 = wc;
    }
  else
    wc = seq->ucs4;

  if (wc == ILLEGAL_CHAR_VALUE && seq == NULL)
    {
      /* It's no character, so look through the collation elements and
	 symbol list.  */
      void *result;

      if (find_entry (&collate->sym_table, symstr, symlen, &result) == 0)
	{
	  /* It's a collation symbol.  */
	  struct symbol_t *sym = (struct symbol_t *) result;
	  elem = sym->order;

	  if (elem == NULL)
	    elem = sym->order = new_element (collate, NULL, 0, NULL,
					     sym->name, strlen (sym->name), 0);
	}
      else if (find_entry (&collate->elem_table, symstr, symlen,
			   (void **) &elem) != 0)
	{
	  /* It's also no collation element.  Therefore ignore it.  */
	  lr_ignore_rest (ldfile, 0);
	  return 1;
	}
    }
  else
    {
      /* Otherwise the symbols stands for a character.  */
      if (find_entry (&collate->seq_table, symstr, symlen,
		      (void **) &elem) != 0)
	{
	  uint32_t wcs[2] = { wc, 0 };

	  /* We have to allocate an entry.  */
	  elem = new_element (collate, seq != NULL ? seq->bytes : NULL,
			      seq != NULL ? seq->nbytes : 0,
			      wc == ILLEGAL_CHAR_VALUE ? NULL : wcs,
			      symstr, symlen, 1);

	  /* And add it to the table.  */
	  if (insert_entry (&collate->seq_table, symstr, symlen, elem) != 0)
	    /* This cannot happen.  */
	    assert (! "Internal error");
	}
      else
	{
	  /* Maybe the character was used before the definition.  In this case
	     we have to insert the byte sequences now.  */
	  if (elem->mbs == NULL && seq != NULL)
	    {
	      elem->mbs = obstack_copy0 (&collate->mempool,
					 seq->bytes, seq->nbytes);
	      elem->nmbs = seq->nbytes;
	    }

	  if (elem->wcs == NULL && wc != ILLEGAL_CHAR_VALUE)
	    {
	      uint32_t wcs[2] = { wc, 0 };

	      elem->wcs = obstack_copy (&collate->mempool, wcs, sizeof (wcs));
	      elem->nwcs = 1;
	    }
	}
    }

  /* Test whether this element is not already in the list.  */
  if (elem->next != NULL || elem == collate->cursor)
    {
      lr_error (ldfile, _("order for `%.*s' already defined at %s:%Zu"),
		(int) symlen, symstr, elem->file, elem->line);
      lr_ignore_rest (ldfile, 0);
      return 1;
    }

  insert_weights (ldfile, elem, charmap, repertoire, collate, tok_none);

  return 0;
}


static void
handle_ellipsis (struct linereader *ldfile, const char *symstr, size_t symlen,
		 enum token_t ellipsis, struct charmap_t *charmap,
		 struct repertoire_t *repertoire,
		 struct locale_collate_t *collate)
{
  struct element_t *startp;
  struct element_t *endp;

  /* Unlink the entry added for the ellipsis.  */
  unlink_element (collate);
  startp = collate->cursor;

  /* Process and add the end-entry.  */
  if (symstr != NULL
      && insert_value (ldfile, symstr, symlen, charmap, repertoire, collate))
    /* Something went wrong with inserting the to-value.  This means
       we cannot process the ellipsis.  */
    return;

  /* Reset the cursor.  */
  collate->cursor = startp;

  /* Now we have to handle many different situations:
     - we have to distinguish between the three different ellipsis forms
     - the is the ellipsis at the beginning, in the middle, or at the end.
  */
  endp = collate->cursor->next;
  assert (symstr == NULL || endp != NULL);

  /* XXX The following is probably very wrong since also collating symbols
     can appear in ranges.  But do we want/can refine the test for that?  */
#if 0
  /* Both, the start and the end symbol, must stand for characters.  */
  if ((startp != NULL && (startp->name == NULL || ! startp->is_character))
      || (endp != NULL && (endp->name == NULL|| ! endp->is_character)))
    {
      lr_error (ldfile, _("\
%s: the start and the end symbol of a range must stand for characters"),
		"LC_COLLATE");
      return;
    }
#endif

  if (ellipsis == tok_ellipsis3)
    {
      /* One requirement we make here: the length of the byte
	 sequences for the first and end character must be the same.
	 This is mainly to prevent unwanted effects and this is often
	 not what is wanted.  */
      size_t len = (startp->mbs != NULL ? startp->nmbs
		    : (endp->mbs != NULL ? endp->nmbs : 0));
      char mbcnt[len + 1];
      char mbend[len + 1];

      /* Well, this should be caught somewhere else already.  Just to
	 make sure.  */
      assert (startp == NULL || startp->wcs == NULL || startp->wcs[1] == 0);
      assert (endp == NULL || endp->wcs == NULL || endp->wcs[1] == 0);

      if (startp != NULL && endp != NULL
	  && startp->mbs != NULL && endp->mbs != NULL
	  && startp->nmbs != endp->nmbs)
	{
	  lr_error (ldfile, _("\
%s: byte sequences of first and last character must have the same length"),
		    "LC_COLLATE");
	  return;
	}

      /* Determine whether we have to generate multibyte sequences.  */
      if ((startp == NULL || startp->mbs != NULL)
	  && (endp == NULL || endp->mbs != NULL))
	{
	  int cnt;
	  int ret;

	  /* Prepare the beginning byte sequence.  This is either from the
	     beginning byte sequence or it is all nulls if it was an
	     initial ellipsis.  */
	  if (startp == NULL || startp->mbs == NULL)
	    memset (mbcnt, '\0', len);
	  else
	    {
	      memcpy (mbcnt, startp->mbs, len);

	      /* And increment it so that the value is the first one we will
		 try to insert.  */
	      for (cnt = len - 1; cnt >= 0; --cnt)
		if (++mbcnt[cnt] != '\0')
		  break;
	    }
	  mbcnt[len] = '\0';

	  /* And the end sequence.  */
	  if (endp == NULL || endp->mbs == NULL)
	    memset (mbend, '\0', len);
	  else
	    memcpy (mbend, endp->mbs, len);
	  mbend[len] = '\0';

	  /* Test whether we have a correct range.  */
	  ret = memcmp (mbcnt, mbend, len);
	  if (ret >= 0)
	    {
	      if (ret > 0)
		lr_error (ldfile, _("%s: byte sequence of first character of \
sequence is not lower than that of the last character"), "LC_COLLATE");
	      return;
	    }

	  /* Generate the byte sequences data.  */
	  while (1)
	    {
	      struct charseq *seq;

	      /* Quite a bit of work ahead.  We have to find the character
		 definition for the byte sequence and then determine the
		 wide character belonging to it.  */
	      seq = charmap_find_symbol (charmap, mbcnt, len);
	      if (seq != NULL)
		{
		  struct element_t *elem;
		  size_t namelen;

		  /* I don't this this can ever happen.  */
		  assert (seq->name != NULL);
		  namelen = strlen (seq->name);

		  if (seq->ucs4 == UNINITIALIZED_CHAR_VALUE)
		    seq->ucs4 = repertoire_find_value (repertoire, seq->name,
						       namelen);

		  /* Now we are ready to insert the new value in the
		     sequence.  Find out whether the element is
		     already known.  */
		  if (find_entry (&collate->seq_table, seq->name, namelen,
				  (void **) &elem) != 0)
		    {
		      uint32_t wcs[2] = { seq->ucs4, 0 };

		      /* We have to allocate an entry.  */
		      elem = new_element (collate, mbcnt, len,
					  seq->ucs4 == ILLEGAL_CHAR_VALUE
					  ? NULL : wcs, seq->name,
					  namelen, 1);

		      /* And add it to the table.  */
		      if (insert_entry (&collate->seq_table, seq->name,
					namelen, elem) != 0)
			/* This cannot happen.  */
			assert (! "Internal error");
		    }

		  /* Test whether this element is not already in the list.  */
		  if (elem->next != NULL || (collate->cursor != NULL
					     && elem->next == collate->cursor))
		    {
		      lr_error (ldfile, _("\
order for `%.*s' already defined at %s:%Zu"),
				(int) namelen, seq->name,
				elem->file, elem->line);
		      goto increment;
		    }

		  /* Enqueue the new element.  */
		  elem->last = collate->cursor;
		  if (collate->cursor == NULL)
		    elem->next = NULL;
		  else
		    {
		      elem->next = collate->cursor->next;
		      elem->last->next = elem;
		      if (elem->next != NULL)
			elem->next->last = elem;
		    }
		  if (collate->start == NULL)
		    {
		      assert (collate->cursor == NULL);
		      collate->start = elem;
		    }
		  collate->cursor = elem;

		 /* Add the weight value.  We take them from the
		    `ellipsis_weights' member of `collate'.  */
		  elem->weights = (struct element_list_t *)
		    obstack_alloc (&collate->mempool,
				   nrules * sizeof (struct element_list_t));
		  for (cnt = 0; cnt < nrules; ++cnt)
		    if (collate->ellipsis_weight.weights[cnt].cnt == 1
			&& (collate->ellipsis_weight.weights[cnt].w[0]
			    == ELEMENT_ELLIPSIS2))
		      {
			elem->weights[cnt].w = (struct element_t **)
			  obstack_alloc (&collate->mempool,
					 sizeof (struct element_t *));
			elem->weights[cnt].w[0] = elem;
			elem->weights[cnt].cnt = 1;
		      }
		    else
		      {
			/* Simply use the weight from `ellipsis_weight'.  */
			elem->weights[cnt].w =
			  collate->ellipsis_weight.weights[cnt].w;
			elem->weights[cnt].cnt =
			  collate->ellipsis_weight.weights[cnt].cnt;
		      }
		}

	      /* Increment for the next round.  */
	    increment:
	      for (cnt = len - 1; cnt >= 0; --cnt)
		if (++mbcnt[cnt] != '\0')
		  break;

	      /* Find out whether this was all.  */
	      if (cnt < 0 || memcmp (mbcnt, mbend, len) >= 0)
		/* Yep, that's all.  */
		break;
	    }
	}
    }
  else
    {
      /* For symbolic range we naturally must have a beginning and an
	 end specified by the user.  */
      if (startp == NULL)
	lr_error (ldfile, _("\
%s: symbolic range ellipsis must not directly follow `order_start'"),
		  "LC_COLLATE");
      else if (endp == NULL)
	lr_error (ldfile, _("\
%s: symbolic range ellipsis must not be directly followed by `order_end'"),
		  "LC_COLLATE");
      else
	{
	  /* Determine the range.  To do so we have to determine the
             common prefix of the both names and then the numeric
             values of both ends.  */
	  size_t lenfrom = strlen (startp->name);
	  size_t lento = strlen (endp->name);
	  char buf[lento + 1];
	  int preflen = 0;
	  long int from;
	  long int to;
	  char *cp;
	  int base = ellipsis == tok_ellipsis2 ? 16 : 10;

	  if (lenfrom != lento)
	    {
	    invalid_range:
	      lr_error (ldfile, _("\
`%s' and `%.*s' are no valid names for symbolic range"),
			startp->name, (int) lento, endp->name);
	      return;
	    }

	  while (startp->name[preflen] == endp->name[preflen])
	    if (startp->name[preflen] == '\0')
	      /* Nothing to be done.  The start and end point are identical
		 and while inserting the end point we have already given
		 the user an error message.  */
	      return;
	    else
	      ++preflen;

	  errno = 0;
	  from = strtol (startp->name + preflen, &cp, base);
	  if ((from == UINT_MAX && errno == ERANGE) || *cp != '\0')
	    goto invalid_range;

	  errno = 0;
	  to = strtol (endp->name + preflen, &cp, base);
	  if ((to == UINT_MAX && errno == ERANGE) || *cp != '\0')
	    goto invalid_range;

	  /* Copy the prefix.  */
	  memcpy (buf, startp->name, preflen);

	  /* Loop over all values.  */
	  for (++from; from < to; ++from)
	    {
	      struct element_t *elem = NULL;
	      struct charseq *seq;
	      uint32_t wc;
	      int cnt;

	      /* Generate the the name.  */
	      sprintf (buf + preflen, base == 10 ? "%ld" : "%lx", from);

	      /* Look whether this name is already defined.  */
	      if (find_entry (&collate->seq_table, buf, symlen,
			      (void **) &elem) == 0)
		{
		  if (elem->next != NULL || (collate->cursor != NULL
					     && elem->next == collate->cursor))
		    {
		      lr_error (ldfile, _("\
%s: order for `%.*s' already defined at %s:%Zu"),
				"LC_COLLATE", (int) lenfrom, buf,
				elem->file, elem->line);
		      continue;
		    }

		  if (elem->name == NULL)
		    {
		      lr_error (ldfile, _("%s: `%s' must be a character"),
				"LC_COLLATE", buf);
		      continue;
		    }
		}

	      if (elem == NULL || (elem->mbs == NULL && elem->wcs == NULL))
		{
		  /* Search for a character of this name.  */
		  seq = charmap_find_value (charmap, buf, lenfrom);
		  if (seq == NULL || seq->ucs4 == UNINITIALIZED_CHAR_VALUE)
		    {
		      wc = repertoire_find_value (repertoire, buf, lenfrom);

		      if (seq != NULL)
			seq->ucs4 = wc;
		    }
		  else
		    wc = seq->ucs4;

		  if (wc == ILLEGAL_CHAR_VALUE && seq == NULL)
		    /* We don't know anything about a character with this
		       name.  XXX Should we warn?  */
		    continue;

		  if (elem == NULL)
		    {
		      uint32_t wcs[2] = { wc, 0 };

		      /* We have to allocate an entry.  */
		      elem = new_element (collate,
					  seq != NULL ? seq->bytes : NULL,
					  seq != NULL ? seq->nbytes : 0,
					  wc == ILLEGAL_CHAR_VALUE
					  ? NULL : wcs, buf, lenfrom, 1);
		    }
		  else
		    {
		      /* Update the element.  */
		      if (seq != NULL)
			{
			  elem->mbs = obstack_copy0 (&collate->mempool,
						     seq->bytes, seq->nbytes);
			  elem->nmbs = seq->nbytes;
			}

		      if (wc != ILLEGAL_CHAR_VALUE)
			{
			  uint32_t zero = 0;

			  obstack_grow (&collate->mempool,
					&wc, sizeof (uint32_t));
			  obstack_grow (&collate->mempool,
					&zero, sizeof (uint32_t));
			  elem->wcs = obstack_finish (&collate->mempool);
			  elem->nwcs = 1;
			}
		    }

		  elem->file = ldfile->fname;
		  elem->line = ldfile->lineno;
		  elem->section = collate->current_section;
		}

	      /* Enqueue the new element.  */
	      elem->last = collate->cursor;
	      elem->next = collate->cursor->next;
	      elem->last->next = elem;
	      if (elem->next != NULL)
		elem->next->last = elem;
	      collate->cursor = elem;

	      /* Now add the weights.  They come from the `ellipsis_weights'
		 member of `collate'.  */
	      elem->weights = (struct element_list_t *)
		obstack_alloc (&collate->mempool,
			       nrules * sizeof (struct element_list_t));
	      for (cnt = 0; cnt < nrules; ++cnt)
		if (collate->ellipsis_weight.weights[cnt].cnt == 1
		    && (collate->ellipsis_weight.weights[cnt].w[0]
			== ELEMENT_ELLIPSIS2))
		  {
		    elem->weights[cnt].w = (struct element_t **)
		      obstack_alloc (&collate->mempool,
				     sizeof (struct element_t *));
		    elem->weights[cnt].w[0] = elem;
		    elem->weights[cnt].cnt = 1;
		  }
		else
		  {
		    /* Simly use the weight from `ellipsis_weight'.  */
		    elem->weights[cnt].w =
		      collate->ellipsis_weight.weights[cnt].w;
		    elem->weights[cnt].cnt =
		      collate->ellipsis_weight.weights[cnt].cnt;
		  }
	    }
	}
    }
}


static void
collate_startup (struct linereader *ldfile, struct localedef_t *locale,
		 struct localedef_t *copy_locale, int ignore_content)
{
  if (!ignore_content && locale->categories[LC_COLLATE].collate == NULL)
    {
      struct locale_collate_t *collate;

      if (copy_locale == NULL)
	{
	  collate = locale->categories[LC_COLLATE].collate =
	    (struct locale_collate_t *)
	    xcalloc (1, sizeof (struct locale_collate_t));

	  /* Init the various data structures.  */
	  init_hash (&collate->elem_table, 100);
	  init_hash (&collate->sym_table, 100);
	  init_hash (&collate->seq_table, 500);
	  obstack_init (&collate->mempool);

	  collate->col_weight_max = -1;
	}
      else
	/* Reuse the copy_locale's data structures.  */
	collate = locale->categories[LC_COLLATE].collate =
	  copy_locale->categories[LC_COLLATE].collate;
    }

  ldfile->translate_strings = 0;
  ldfile->return_widestr = 0;
}


void
collate_finish (struct localedef_t *locale, struct charmap_t *charmap)
{
  /* Now is the time when we can assign the individual collation
     values for all the symbols.  We have possibly different values
     for the wide- and the multibyte-character symbols.  This is done
     since it might make a difference in the encoding if there is in
     some cases no multibyte-character but there are wide-characters.
     (The other way around it is not important since theencoded
     collation value in the wide-character case is 32 bits wide and
     therefore requires no encoding).

     The lowest collation value assigned is 2.  Zero is reserved for
     the NUL byte terminating the strings in the `strxfrm'/`wcsxfrm'
     functions and 1 is used to separate the individual passes for the
     different rules.

     We also have to construct is list with all the bytes/words which
     can come first in a sequence, followed by all the elements which
     also start with this byte/word.  The order is reverse which has
     among others the important effect that longer strings are located
     first in the list.  This is required for the output data since
     the algorithm used in `strcoll' etc depends on this.

     The multibyte case is easy.  We simply sort into an array with
     256 elements.  */
  struct locale_collate_t *collate = locale->categories[LC_COLLATE].collate;
  int mbact[nrules];
  int wcact;
  int mbseqact;
  int wcseqact;
  struct element_t *runp;
  int i;
  int need_undefined = 0;
  struct section_list *sect;
  int ruleidx;
  int nr_wide_elems = 0;

  if (collate == NULL)
    {
      /* No data, no check.  */
      if (! be_quiet)
	error (0, 0, _("No definition for %s category found"), "LC_COLLATE");
      return;
    }

  /* If this assertion is hit change the type in `element_t'.  */
  assert (nrules <= sizeof (runp->used_in_level) * 8);

  /* Make sure that the `position' rule is used either in all sections
     or in none.  */
  for (i = 0; i < nrules; ++i)
    for (sect = collate->sections; sect != NULL; sect = sect->next)
      if (sect->rules != NULL
	  && ((sect->rules[i] & sort_position)
	      != (collate->sections->rules[i] & sort_position)))
	{
	  error (0, 0, _("\
%s: `position' must be used for a specific level in all sections or none"),
		 "LC_COLLATE");
	  break;
	}

  /* Find out which elements are used at which level.  At the same
     time we find out whether we have any undefined symbols.  */
  runp = collate->start;
  while (runp != NULL)
    {
      if (runp->mbs != NULL)
	{
	  for (i = 0; i < nrules; ++i)
	    {
	      int j;

	      for (j = 0; j < runp->weights[i].cnt; ++j)
		/* A NULL pointer as the weight means IGNORE.  */
		if (runp->weights[i].w[j] != NULL)
		  {
		    if (runp->weights[i].w[j]->weights == NULL)
		      {
			error_at_line (0, 0, runp->file, runp->line,
				       _("symbol `%s' not defined"),
				       runp->weights[i].w[j]->name);

			need_undefined = 1;
			runp->weights[i].w[j] = &collate->undefined;
		      }
		    else
		      /* Set the bit for the level.  */
		      runp->weights[i].w[j]->used_in_level |= 1 << i;
		  }
	    }
	}

      /* Up to the next entry.  */
      runp = runp->next;
    }

  /* Walk through the list of defined sequences and assign weights.  Also
     create the data structure which will allow generating the single byte
     character based tables.

     Since at each time only the weights for each of the rules are
     only compared to other weights for this rule it is possible to
     assign more compact weight values than simply counting all
     weights in sequence.  We can assign weights from 3, one for each
     rule individually and only for those elements, which are actually
     used for this rule.

     Why is this important?  It is not for the wide char table.  But
     it is for the singlebyte output since here larger numbers have to
     be encoded to make it possible to emit the value as a byte
     string.  */
  for (i = 0; i < nrules; ++i)
    mbact[i] = 2;
  wcact = 2;
  mbseqact = 0;
  wcseqact = 0;
  runp = collate->start;
  while (runp != NULL)
    {
      /* Determine the order.  */
      if (runp->used_in_level != 0)
	{
	  runp->mborder = (int *) obstack_alloc (&collate->mempool,
						 nrules * sizeof (int));

	  for (i = 0; i < nrules; ++i)
	    if ((runp->used_in_level & (1 << i)) != 0)
	      runp->mborder[i] = mbact[i]++;
	    else
	      runp->mborder[i] = 0;
	}

      if (runp->mbs != NULL)
	{
	  struct element_t **eptr;
	  struct element_t *lastp = NULL;

	  /* Find the point where to insert in the list.  */
	  eptr = &collate->mbheads[((unsigned char *) runp->mbs)[0]];
	  while (*eptr != NULL)
	    {
	      if ((*eptr)->nmbs < runp->nmbs)
		break;

	      if ((*eptr)->nmbs == runp->nmbs)
		{
		  int c = memcmp ((*eptr)->mbs, runp->mbs, runp->nmbs);

		  if (c == 0)
		    {
		      /* This should not happen.  It means that we have
			 to symbols with the same byte sequence.  It is
			 of course an error.  */
		      error_at_line (0, 0, (*eptr)->file, (*eptr)->line,
				     _("symbol `%s' has the same encoding as"),
				     (*eptr)->name);
		      error_at_line (0, 0, runp->file, runp->line,
				     _("symbol `%s'"), runp->name);
		      goto dont_insert;
		    }
		  else if (c < 0)
		    /* Insert it here.  */
		    break;
		}

	      /* To the next entry.  */
	      lastp = *eptr;
	      eptr = &(*eptr)->mbnext;
	    }

	  /* Set the pointers.  */
	  runp->mbnext = *eptr;
	  runp->mblast = lastp;
	  if (*eptr != NULL)
	    (*eptr)->mblast = runp;
	  *eptr = runp;
	dont_insert:
	  ;
	}

      if (runp->used_in_level)
	{
	  runp->wcorder = wcact++;

	  /* We take the opportunity to count the elements which have
	     wide characters.  */
	  ++nr_wide_elems;
	}

      if (runp->is_character)
	{
	  if (runp->nmbs == 1)
	    collate->mbseqorder[((unsigned char *) runp->mbs)[0]] = mbseqact++;

	  runp->wcseqorder = wcseqact++;
	}

      /* Up to the next entry.  */
      runp = runp->next;
    }

  /* Find out whether any of the `mbheads' entries is unset.  In this
     case we use the UNDEFINED entry.  */
  for (i = 1; i < 256; ++i)
    if (collate->mbheads[i] == NULL)
      {
	need_undefined = 1;
	collate->mbheads[i] = &collate->undefined;
      }

  /* Now to the wide character case.  */
  collate->wcheads.p = 6;
  collate->wcheads.q = 10;
  wchead_table_init (&collate->wcheads);

  collate->wcseqorder.p = 6;
  collate->wcseqorder.q = 10;
  collseq_table_init (&collate->wcseqorder);

  /* Start adding.  */
  runp = collate->start;
  while (runp != NULL)
    {
      if (runp->wcs != NULL)
	{
	  struct element_t *e;
	  struct element_t **eptr;
	  struct element_t *lastp;

	  /* Insert the collation sequence value.  */
	  collseq_table_add (&collate->wcseqorder, runp->wcs[0],
			     runp->wcseqorder);

	  /* Find the point where to insert in the list.  */
	  e = wchead_table_get (&collate->wcheads, runp->wcs[0]);
	  eptr = &e;
	  lastp = NULL;
	  while (*eptr != NULL)
	    {
	      if ((*eptr)->nwcs < runp->nwcs)
		break;

	      if ((*eptr)->nwcs == runp->nwcs)
		{
		  int c = wmemcmp ((wchar_t *) (*eptr)->wcs,
				   (wchar_t *) runp->wcs, runp->nwcs);

		  if (c == 0)
		    {
		      /* This should not happen.  It means that we have
			 two symbols with the same byte sequence.  It is
			 of course an error.  */
		      error_at_line (0, 0, (*eptr)->file, (*eptr)->line,
				     _("symbol `%s' has the same encoding as"),
				     (*eptr)->name);
		      error_at_line (0, 0, runp->file, runp->line,
				     _("symbol `%s'"), runp->name);
		      goto dont_insertwc;
		    }
		  else if (c < 0)
		    /* Insert it here.  */
		    break;
		}

	      /* To the next entry.  */
	      lastp = *eptr;
	      eptr = &(*eptr)->wcnext;
	    }

	  /* Set the pointers.  */
	  runp->wcnext = *eptr;
	  runp->wclast = lastp;
	  if (*eptr != NULL)
	    (*eptr)->wclast = runp;
	  *eptr = runp;
	  if (eptr == &e)
	    wchead_table_add (&collate->wcheads, runp->wcs[0], e);
	dont_insertwc:
	  ;
	}

      /* Up to the next entry.  */
      runp = runp->next;
    }

  collseq_table_finalize (&collate->wcseqorder);

  /* Now determine whether the UNDEFINED entry is needed and if yes,
     whether it was defined.  */
  collate->undefined.used_in_level = need_undefined ? ~0ul : 0;
  if (collate->undefined.file == NULL)
    {
      if (need_undefined)
	{
	  /* This seems not to be enforced by recent standards.  Don't
	     emit an error, simply append UNDEFINED at the end.  */
	  if (0)
	    error (0, 0, _("no definition of `UNDEFINED'"));

	  /* Add UNDEFINED at the end.  */
	  collate->undefined.mborder =
	    (int *) obstack_alloc (&collate->mempool, nrules * sizeof (int));

	  for (i = 0; i < nrules; ++i)
	    collate->undefined.mborder[i] = mbact[i]++;
	}

      /* In any case we will need the definition for the wide character
	 case.  But we will not complain that it is missing since the
	 specification strangely enough does not seem to account for
	 this.  */
      collate->undefined.wcorder = wcact++;
    }

  /* Finally, try to unify the rules for the sections.  Whenever the rules
     for a section are the same as those for another section give the
     ruleset the same index.  Since there are never many section we can
     use an O(n^2) algorithm here.  */
  sect = collate->sections;
  while (sect != NULL && sect->rules == NULL)
    sect = sect->next;
  assert (sect != NULL);
  ruleidx = 0;
  do
    {
      struct section_list *osect = collate->sections;

      while (osect != sect)
	if (osect->rules != NULL
	    && memcmp (osect->rules, sect->rules, nrules) == 0)
	  break;
	else
	  osect = osect->next;

      if (osect == sect)
	sect->ruleidx = ruleidx++;
      else
	sect->ruleidx = osect->ruleidx;

      /* Next section.  */
      do
	sect = sect->next;
      while (sect != NULL && sect->rules == NULL);
    }
  while (sect != NULL);
  /* We are currently not prepared for more than 128 rulesets.  But this
     should never really be a problem.  */
  assert (ruleidx <= 128);
}


static int32_t
output_weight (struct obstack *pool, struct locale_collate_t *collate,
	       struct element_t *elem)
{
  size_t cnt;
  int32_t retval;

  /* Optimize the use of UNDEFINED.  */
  if (elem == &collate->undefined)
    /* The weights are already inserted.  */
    return 0;

  /* This byte can start exactly one collation element and this is
     a single byte.  We can directly give the index to the weights.  */
  retval = obstack_object_size (pool);

  /* Construct the weight.  */
  for (cnt = 0; cnt < nrules; ++cnt)
    {
      char buf[elem->weights[cnt].cnt * 7];
      int len = 0;
      int i;

      for (i = 0; i < elem->weights[cnt].cnt; ++i)
	/* Encode the weight value.  We do nothing for IGNORE entries.  */
	if (elem->weights[cnt].w[i] != NULL)
	  len += utf8_encode (&buf[len],
			      elem->weights[cnt].w[i]->mborder[cnt]);

      /* And add the buffer content.  */
      obstack_1grow (pool, len);
      obstack_grow (pool, buf, len);
    }

  return retval | ((elem->section->ruleidx & 0x7f) << 24);
}


static int32_t
output_weightwc (struct obstack *pool, struct locale_collate_t *collate,
		 struct element_t *elem)
{
  size_t cnt;
  int32_t retval;

  /* Optimize the use of UNDEFINED.  */
  if (elem == &collate->undefined)
    /* The weights are already inserted.  */
    return 0;

  /* This byte can start exactly one collation element and this is
     a single byte.  We can directly give the index to the weights.  */
  retval = obstack_object_size (pool) / sizeof (int32_t);

  /* Construct the weight.  */
  for (cnt = 0; cnt < nrules; ++cnt)
    {
      int32_t buf[elem->weights[cnt].cnt];
      int i;
      int32_t j;

      for (i = 0, j = 0; i < elem->weights[cnt].cnt; ++i)
	if (elem->weights[cnt].w[i] != NULL)
	  buf[j++] = elem->weights[cnt].w[i]->wcorder;

      /* And add the buffer content.  */
      obstack_int32_grow (pool, j);

      obstack_grow (pool, buf, j * sizeof (int32_t));
    }

  return retval | ((elem->section->ruleidx & 0x7f) << 24);
}


void
collate_output (struct localedef_t *locale, struct charmap_t *charmap,
		const char *output_path)
{
  struct locale_collate_t *collate = locale->categories[LC_COLLATE].collate;
  const size_t nelems = _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE);
  struct iovec iov[2 + nelems];
  struct locale_file data;
  uint32_t idx[nelems];
  size_t cnt;
  size_t ch;
  int32_t tablemb[256];
  struct obstack weightpool;
  struct obstack extrapool;
  struct obstack indirectpool;
  struct section_list *sect;
  struct collidx_table tablewc;
  uint32_t elem_size;
  uint32_t *elem_table;
  int i;
  struct element_t *runp;

  data.magic = LIMAGIC (LC_COLLATE);
  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;
  cnt = 0;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_NRULES));
  iov[2 + cnt].iov_base = &nrules;
  iov[2 + cnt].iov_len = sizeof (uint32_t);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  ++cnt;

  /* If we have no LC_COLLATE data emit only the number of rules as zero.  */
  if (collate == NULL)
    {
      int32_t dummy = 0;

      while (cnt < _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE))
	{
	  /* The words have to be handled specially.  */
	  if (cnt == _NL_ITEM_INDEX (_NL_COLLATE_SYMB_HASH_SIZEMB))
	    {
	      iov[2 + cnt].iov_base = &dummy;
	      iov[2 + cnt].iov_len = sizeof (int32_t);
	    }
	  else
	    {
	      iov[2 + cnt].iov_base = NULL;
	      iov[2 + cnt].iov_len = 0;
	    }

	  if (cnt + 1 < _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE))
	    idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
	  ++cnt;
	}

      assert (cnt == _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE));

      write_locale_data (output_path, "LC_COLLATE", 2 + cnt, iov);

      return;
    }

  obstack_init (&weightpool);
  obstack_init (&extrapool);
  obstack_init (&indirectpool);

  /* Since we are using the sign of an integer to mark indirection the
     offsets in the arrays we are indirectly referring to must not be
     zero since -0 == 0.  Therefore we add a bit of dummy content.  */
  obstack_int32_grow (&extrapool, 0);
  obstack_int32_grow (&indirectpool, 0);

  /* Prepare the ruleset table.  */
  for (sect = collate->sections, i = 0; sect != NULL; sect = sect->next)
    if (sect->rules != NULL && sect->ruleidx == i)
      {
	int j;

	obstack_make_room (&weightpool, nrules);

	for (j = 0; j < nrules; ++j)
	  obstack_1grow_fast (&weightpool, sect->rules[j]);
	++i;
      }
  /* And align the output.  */
  i = (nrules * i) % __alignof__ (int32_t);
  if (i > 0)
    do
      obstack_1grow (&weightpool, '\0');
    while (++i < __alignof__ (int32_t));

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_RULESETS));
  iov[2 + cnt].iov_len = obstack_object_size (&weightpool);
  iov[2 + cnt].iov_base = obstack_finish (&weightpool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  ++cnt;

  /* Generate the 8-bit table.  Walk through the lists of sequences
     starting with the same byte and add them one after the other to
     the table.  In case we have more than one sequence starting with
     the same byte we have to use extra indirection.

     First add a record for the NUL byte.  This entry will never be used
     so it does not matter.  */
  tablemb[0] = 0;

  /* Now insert the `UNDEFINED' value if it is used.  Since this value
     will probably be used more than once it is good to store the
     weights only once.  */
  if (collate->undefined.used_in_level != 0)
    output_weight (&weightpool, collate, &collate->undefined);

  for (ch = 1; ch < 256; ++ch)
    if (collate->mbheads[ch]->mbnext == NULL
	&& collate->mbheads[ch]->nmbs <= 1)
      {
	tablemb[ch] = output_weight (&weightpool, collate,
				     collate->mbheads[ch]);
      }
    else
      {
	/* The entries in the list are sorted by length and then
           alphabetically.  This is the order in which we will add the
           elements to the collation table.  This allows simply walking
	   the table in sequence and stopping at the first matching
           entry.  Since the longer sequences are coming first in the
           list they have the possibility to match first, just as it
           has to be.  In the worst case we are walking to the end of
           the list where we put, if no singlebyte sequence is defined
           in the locale definition, the weights for UNDEFINED.

	   To reduce the length of the search list we compress them a bit.
	   This happens by collecting sequences of consecutive byte
	   sequences in one entry (having and begin and end byte sequence)
	   and add only one index into the weight table.  We can find the
	   consecutive entries since they are also consecutive in the list.  */
	struct element_t *runp = collate->mbheads[ch];
	struct element_t *lastp;

	assert ((obstack_object_size (&extrapool)
		 & (__alignof__ (int32_t) - 1)) == 0);

	tablemb[ch] = -obstack_object_size (&extrapool);

	do
	  {
	    /* Store the current index in the weight table.  We know that
	       the current position in the `extrapool' is aligned on a
	       32-bit address.  */
	    int32_t weightidx;
	    int added;

	    /* Find out wether this is a single entry or we have more than
	       one consecutive entry.  */
	    if (runp->mbnext != NULL
		&& runp->nmbs == runp->mbnext->nmbs
		&& memcmp (runp->mbs, runp->mbnext->mbs, runp->nmbs - 1) == 0
		&& (runp->mbs[runp->nmbs - 1]
		    == runp->mbnext->mbs[runp->nmbs - 1] + 1))
	      {
		int i;
		struct element_t *series_startp = runp;
		struct element_t *curp;

		/* Compute how much space we will need.  */
		added = ((sizeof (int32_t) + 1 + 2 * (runp->nmbs - 1)
			  + __alignof__ (int32_t) - 1)
			 & ~(__alignof__ (int32_t) - 1));
		assert ((obstack_object_size (&extrapool)
			 & (__alignof__ (int32_t) - 1)) == 0);
		obstack_make_room (&extrapool, added);

		/* More than one consecutive entry.  We mark this by having
		   a negative index into the indirect table.  */
		obstack_int32_grow_fast (&extrapool,
					 -(obstack_object_size (&indirectpool)
					   / sizeof (int32_t)));

		/* Now search first the end of the series.  */
		do
		  runp = runp->mbnext;
		while (runp->mbnext != NULL
		       && runp->nmbs == runp->mbnext->nmbs
		       && memcmp (runp->mbs, runp->mbnext->mbs,
				  runp->nmbs - 1) == 0
		       && (runp->mbs[runp->nmbs - 1]
			   == runp->mbnext->mbs[runp->nmbs - 1] + 1));

		/* Now walk backward from here to the beginning.  */
		curp = runp;

		assert (runp->nmbs <= 256);
		obstack_1grow_fast (&extrapool, curp->nmbs - 1);
		for (i = 1; i < curp->nmbs; ++i)
		  obstack_1grow_fast (&extrapool, curp->mbs[i]);

		/* Now find the end of the consecutive sequence and
                   add all the indeces in the indirect pool.  */
		do
		  {
		    weightidx = output_weight (&weightpool, collate, curp);
		    obstack_int32_grow (&indirectpool, weightidx);

		    curp = curp->mblast;
		  }
		while (curp != series_startp);

		/* Add the final weight.  */
		weightidx = output_weight (&weightpool, collate, curp);
		obstack_int32_grow (&indirectpool, weightidx);

		/* And add the end byte sequence.  Without length this
                   time.  */
		for (i = 1; i < curp->nmbs; ++i)
		  obstack_1grow_fast (&extrapool, curp->mbs[i]);
	      }
	    else
	      {
		/* A single entry.  Simply add the index and the length and
		   string (except for the first character which is already
		   tested for).  */
		int i;

		/* Output the weight info.  */
		weightidx = output_weight (&weightpool, collate, runp);

		added = ((sizeof (int32_t) + 1 + runp->nmbs - 1
			  + __alignof__ (int32_t) - 1)
			 & ~(__alignof__ (int32_t) - 1));
		assert ((obstack_object_size (&extrapool)
			 & (__alignof__ (int32_t) - 1)) == 0);
		obstack_make_room (&extrapool, added);

		obstack_int32_grow_fast (&extrapool, weightidx);
		assert (runp->nmbs <= 256);
		obstack_1grow_fast (&extrapool, runp->nmbs - 1);

		for (i = 1; i < runp->nmbs; ++i)
		  obstack_1grow_fast (&extrapool, runp->mbs[i]);
	      }

	    /* Add alignment bytes if necessary.  */
	    while ((obstack_object_size (&extrapool)
		    & (__alignof__ (int32_t) - 1)) != 0)
	      obstack_1grow_fast (&extrapool, '\0');

	    /* Next entry.  */
	    lastp = runp;
	    runp = runp->mbnext;
	  }
	while (runp != NULL);

	assert ((obstack_object_size (&extrapool)
		 & (__alignof__ (int32_t) - 1)) == 0);

	/* If the final entry in the list is not a single character we
           add an UNDEFINED entry here.  */
	if (lastp->nmbs != 1)
	  {
	    int added = ((sizeof (int32_t) + 1 + 1 + __alignof__ (int32_t) - 1)
			 & ~(__alignof__ (int32_t) - 1));
	    obstack_make_room (&extrapool, added);

	    obstack_int32_grow_fast (&extrapool, 0);
	    /* XXX What rule? We just pick the first.  */
	    obstack_1grow_fast (&extrapool, 0);
	    /* Length is zero.  */
	    obstack_1grow_fast (&extrapool, 0);

	    /* Add alignment bytes if necessary.  */
	    while ((obstack_object_size (&extrapool)
		    & (__alignof__ (int32_t) - 1)) != 0)
	      obstack_1grow_fast (&extrapool, '\0');
	  }
      }

  /* Add padding to the tables if necessary.  */
  while ((obstack_object_size (&weightpool) & (__alignof__ (int32_t) - 1))
	 != 0)
    obstack_1grow (&weightpool, 0);

  /* Now add the four tables.  */
  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_TABLEMB));
  iov[2 + cnt].iov_base = tablemb;
  iov[2 + cnt].iov_len = sizeof (tablemb);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert ((iov[2 + cnt].iov_len & (__alignof__ (int32_t) - 1)) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_WEIGHTMB));
  iov[2 + cnt].iov_len = obstack_object_size (&weightpool);
  iov[2 + cnt].iov_base = obstack_finish (&weightpool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_EXTRAMB));
  iov[2 + cnt].iov_len = obstack_object_size (&extrapool);
  iov[2 + cnt].iov_base = obstack_finish (&extrapool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_INDIRECTMB));
  iov[2 + cnt].iov_len = obstack_object_size (&indirectpool);
  iov[2 + cnt].iov_base = obstack_finish (&indirectpool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert ((iov[2 + cnt].iov_len & (__alignof__ (int32_t) - 1)) == 0);
  ++cnt;


  /* Now the same for the wide character table.  We need to store some
     more information here.  */
  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_GAP1));
  iov[2 + cnt].iov_base = NULL;
  iov[2 + cnt].iov_len = 0;
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_GAP2));
  iov[2 + cnt].iov_base = NULL;
  iov[2 + cnt].iov_len = 0;
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_GAP3));
  iov[2 + cnt].iov_base = NULL;
  iov[2 + cnt].iov_len = 0;
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  /* Since we are using the sign of an integer to mark indirection the
     offsets in the arrays we are indirectly referring to must not be
     zero since -0 == 0.  Therefore we add a bit of dummy content.  */
  obstack_int32_grow (&extrapool, 0);
  obstack_int32_grow (&indirectpool, 0);

  /* Now insert the `UNDEFINED' value if it is used.  Since this value
     will probably be used more than once it is good to store the
     weights only once.  */
  if (output_weightwc (&weightpool, collate, &collate->undefined) != 0)
    abort ();

  /* Generate the table.  Walk through the lists of sequences starting
     with the same wide character and add them one after the other to
     the table.  In case we have more than one sequence starting with
     the same byte we have to use extra indirection.  */
  {
    auto void add_to_tablewc (uint32_t ch, struct element_t *runp);

    void add_to_tablewc (uint32_t ch, struct element_t *runp)
      {
	if (runp->wcnext == NULL && runp->nwcs == 1)
	  {
	    int32_t weigthidx = output_weightwc (&weightpool, collate, runp);
	    collidx_table_add (&tablewc, ch, weigthidx);
	  }
	else
	  {
	    /* As for the singlebyte table, we recognize sequences and
	       compress them.  */
	    struct element_t *lastp;

	    collidx_table_add (&tablewc, ch,
			       -(obstack_object_size (&extrapool) / sizeof (uint32_t)));

	    do
	      {
		/* Store the current index in the weight table.  We know that
		   the current position in the `extrapool' is aligned on a
		   32-bit address.  */
		int32_t weightidx;
		int added;

		/* Find out wether this is a single entry or we have more than
		   one consecutive entry.  */
		if (runp->wcnext != NULL
		    && runp->nwcs == runp->wcnext->nwcs
		    && wmemcmp ((wchar_t *) runp->wcs,
				(wchar_t *)runp->wcnext->wcs,
				runp->nwcs - 1) == 0
		    && (runp->wcs[runp->nwcs - 1]
			== runp->wcnext->wcs[runp->nwcs - 1] + 1))
		  {
		    int i;
		    struct element_t *series_startp = runp;
		    struct element_t *curp;

		    /* Now add first the initial byte sequence.  */
		    added = (1 + 1 + 2 * (runp->nwcs - 1)) * sizeof (int32_t);
		    if (sizeof (int32_t) == sizeof (int))
		      obstack_make_room (&extrapool, added);

		    /* More than one consecutive entry.  We mark this by having
		       a negative index into the indirect table.  */
		    obstack_int32_grow_fast (&extrapool,
					     -(obstack_object_size (&indirectpool)
					       / sizeof (int32_t)));
		    obstack_int32_grow_fast (&extrapool, runp->nwcs - 1);

		    do
		      runp = runp->wcnext;
		    while (runp->wcnext != NULL
			   && runp->nwcs == runp->wcnext->nwcs
			   && wmemcmp ((wchar_t *) runp->wcs,
				       (wchar_t *)runp->wcnext->wcs,
				       runp->nwcs - 1) == 0
			   && (runp->wcs[runp->nwcs - 1]
			       == runp->wcnext->wcs[runp->nwcs - 1] + 1));

		    /* Now walk backward from here to the beginning.  */
		    curp = runp;

		    for (i = 1; i < runp->nwcs; ++i)
		      obstack_int32_grow_fast (&extrapool, curp->wcs[i]);

		    /* Now find the end of the consecutive sequence and
		       add all the indeces in the indirect pool.  */
		    do
		      {
			weightidx = output_weightwc (&weightpool, collate,
						     curp);
			obstack_int32_grow (&indirectpool, weightidx);

			curp = curp->wclast;
		      }
		    while (curp != series_startp);

		    /* Add the final weight.  */
		    weightidx = output_weightwc (&weightpool, collate, curp);
		    obstack_int32_grow (&indirectpool, weightidx);

		    /* And add the end byte sequence.  Without length this
		       time.  */
		    for (i = 1; i < curp->nwcs; ++i)
		      obstack_int32_grow (&extrapool, curp->wcs[i]);
		  }
		else
		  {
		    /* A single entry.  Simply add the index and the length and
		       string (except for the first character which is already
		       tested for).  */
		    int i;

		    /* Output the weight info.  */
		    weightidx = output_weightwc (&weightpool, collate, runp);

		    added = (1 + 1 + runp->nwcs - 1) * sizeof (int32_t);
		    if (sizeof (int) == sizeof (int32_t))
		      obstack_make_room (&extrapool, added);

		    obstack_int32_grow_fast (&extrapool, weightidx);
		    obstack_int32_grow_fast (&extrapool, runp->nwcs - 1);
		    for (i = 1; i < runp->nwcs; ++i)
		      obstack_int32_grow_fast (&extrapool, runp->wcs[i]);
		  }

		/* Next entry.  */
		lastp = runp;
		runp = runp->wcnext;
	      }
	    while (runp != NULL);
	  }
      }

    tablewc.p = 6;
    tablewc.q = 10;
    collidx_table_init (&tablewc);

    wchead_table_iterate (&collate->wcheads, add_to_tablewc);

    collidx_table_finalize (&tablewc);
  }

  /* Now add the four tables.  */
  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_TABLEWC));
  iov[2 + cnt].iov_base = tablewc.result;
  iov[2 + cnt].iov_len = tablewc.result_size;
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (iov[2 + cnt].iov_len % sizeof (int32_t) == 0);
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_WEIGHTWC));
  iov[2 + cnt].iov_len = obstack_object_size (&weightpool);
  iov[2 + cnt].iov_base = obstack_finish (&weightpool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (iov[2 + cnt].iov_len % sizeof (int32_t) == 0);
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_EXTRAWC));
  iov[2 + cnt].iov_len = obstack_object_size (&extrapool);
  iov[2 + cnt].iov_base = obstack_finish (&extrapool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (iov[2 + cnt].iov_len % sizeof (int32_t) == 0);
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_INDIRECTWC));
  iov[2 + cnt].iov_len = obstack_object_size (&indirectpool);
  iov[2 + cnt].iov_base = obstack_finish (&indirectpool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (iov[2 + cnt].iov_len % sizeof (int32_t) == 0);
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;


  /* Finally write the table with collation element names out.  It is
     a hash table with a simple function which gets the name of the
     character as the input.  One character might have many names.  The
     value associated with the name is an index into the weight table
     where we are then interested in the first-level weight value.

     To determine how large the table should be we are counting the
     elements have to put in.  Since we are using internal chaining
     using a secondary hash function we have to make the table a bit
     larger to avoid extremely long search times.  We can achieve
     good results with a 40% larger table than there are entries.  */
  elem_size = 0;
  runp = collate->start;
  while (runp != NULL)
    {
      if (runp->mbs != NULL && runp->weights != NULL)
	/* Yep, the element really counts.  */
	++elem_size;

      runp = runp->next;
    }
  /* Add 40% and find the next prime number.  */
  elem_size = MIN (next_prime (elem_size * 1.4), 257);

  /* Allocate the table.  Each entry consists of two words: the hash
     value and an index in a secondary table which provides the index
     into the weight table and the string itself (so that a match can
     be determined).  */
  elem_table = (uint32_t *) obstack_alloc (&extrapool,
					   elem_size * 2 * sizeof (uint32_t));
  memset (elem_table, '\0', elem_size * 2 * sizeof (uint32_t));

  /* Now add the elements.  */
  runp = collate->start;
  while (runp != NULL)
    {
      if (runp->mbs != NULL && runp->weights != NULL)
	{
	  /* Compute the hash value of the name.  */
	  uint32_t namelen = strlen (runp->name);
	  uint32_t hash = elem_hash (runp->name, namelen);
	  size_t idx = hash % elem_size;

	  if (elem_table[idx * 2] != 0)
	    {
	      /* The spot is already take.  Try iterating using the value
		 from the secondary hashing function.  */
	      size_t iter = hash % (elem_size - 2);

	      do
		{
		  idx += iter;
		  if (idx >= elem_size)
		    idx -= elem_size;
		}
	      while (elem_table[idx * 2] != 0);

	      /* This is the spot where we will insert the value.  */
	      elem_table[idx * 2] = hash;
	      elem_table[idx * 2 + 1] = obstack_object_size (&extrapool);

	      /* The the string itself including length.  */
	      obstack_1grow (&extrapool, namelen);
	      obstack_grow (&extrapool, runp->name, namelen);

	      /* And the multibyte representation.  */
	      obstack_1grow (&extrapool, runp->nmbs);
	      obstack_grow (&extrapool, runp->mbs, runp->nmbs);

	      /* And align again to 32 bits.  */
	      if ((1 + namelen + 1 + runp->nmbs) % sizeof (int32_t) != 0)
		obstack_grow (&extrapool, "\0\0",
			      (sizeof (int32_t)
			       - ((1 + namelen + 1 + runp->nmbs)
				  % sizeof (int32_t))));

	      /* Now some 32-bit values: multibyte collation sequence,
		 wide char string (including length), and wide char
		 collation sequence.  */
	      obstack_int32_grow (&extrapool, runp->mbseqorder);

	      obstack_int32_grow (&extrapool, runp->nwcs);
	      obstack_grow (&extrapool, runp->wcs,
			    runp->nwcs * sizeof (uint32_t));

	      obstack_int32_grow (&extrapool, runp->wcseqorder);
	    }
	}

      runp = runp->next;
    }

  /* Prepare to write out this data.  */
  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_SYMB_HASH_SIZEMB));
  iov[2 + cnt].iov_base = &elem_size;
  iov[2 + cnt].iov_len = sizeof (int32_t);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_SYMB_TABLEMB));
  iov[2 + cnt].iov_base = elem_table;
  iov[2 + cnt].iov_len = elem_size * 2 * sizeof (int32_t);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_SYMB_EXTRAMB));
  iov[2 + cnt].iov_len = obstack_object_size (&extrapool);
  iov[2 + cnt].iov_base = obstack_finish (&extrapool);
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_COLLSEQMB));
  iov[2 + cnt].iov_base = collate->mbseqorder;
  iov[2 + cnt].iov_len = 256;
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_COLLSEQWC));
  iov[2 + cnt].iov_base = collate->wcseqorder.result;
  iov[2 + cnt].iov_len = collate->wcseqorder.result_size;
  idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
  assert (idx[cnt] % __alignof__ (int32_t) == 0);
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_COLLATE_CODESET));
  iov[2 + cnt].iov_base = (void *) charmap->code_set_name;
  iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
  ++cnt;

  assert (cnt == _NL_ITEM_INDEX (_NL_NUM_LC_COLLATE));

  write_locale_data (output_path, "LC_COLLATE", 2 + cnt, iov);

  obstack_free (&weightpool, NULL);
  obstack_free (&extrapool, NULL);
  obstack_free (&indirectpool, NULL);
}


void
collate_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_collate_t *collate;
  struct token *now;
  struct token *arg = NULL;
  enum token_t nowtok;
  enum token_t was_ellipsis = tok_none;
  struct localedef_t *copy_locale = NULL;
  /* Parsing state:
     0 - start
     1 - between `order-start' and `order-end'
     2 - after `order-end'
     3 - after `reorder-after', waiting for `reorder-end'
     4 - after `reorder-end'
     5 - after `reorder-sections-after', waiting for `reorder-sections-end'
     6 - after `reorder-sections-end'
  */
  int state = 0;

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

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

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

  if (nowtok == tok_copy)
    {
      state = 2;
      now = lr_token (ldfile, charmap, NULL, verbose);
      if (now->tok != tok_string)
	{
	  SYNTAX_ERROR (_("%s: syntax error"), "LC_COLLATE");

	skip_category:
	  do
	    now = lr_token (ldfile, charmap, NULL, verbose);
	  while (now->tok != tok_eof && now->tok != tok_end);

	  if (now->tok != tok_eof
	      || (now = lr_token (ldfile, charmap, NULL, verbose),
		  now->tok == tok_eof))
	    lr_error (ldfile, _("%s: premature end of file"), "LC_COLLATE");
	  else if (now->tok != tok_lc_collate)
	    {
	      lr_error (ldfile, _("\
%1$s: definition does not end with `END %1$s'"), "LC_COLLATE");
	      lr_ignore_rest (ldfile, 0);
	    }
	  else
	    lr_ignore_rest (ldfile, 1);

	  return;
	}

      if (! ignore_content)
	{
	  /* Get the locale definition.  */
	  copy_locale = load_locale (LC_COLLATE, now->val.str.startmb,
				     repertoire_name, charmap, NULL);
	  if ((copy_locale->avail & COLLATE_LOCALE) == 0)
	    {
	      /* Not yet loaded.  So do it now.  */
	      if (locfile_read (copy_locale, charmap) != 0)
		goto skip_category;
	    }
	}

      lr_ignore_rest (ldfile, 1);

      now = lr_token (ldfile, charmap, NULL, verbose);
      nowtok = now->tok;
    }

  /* Prepare the data structures.  */
  collate_startup (ldfile, result, copy_locale, ignore_content);
  collate = result->categories[LC_COLLATE].collate;

  while (1)
    {
      char ucs4buf[10];
      char *symstr;
      size_t symlen;

      /* 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, verbose);
	  nowtok = now->tok;
	  continue;
	}

      switch (nowtok)
	{
	case tok_copy:
	  /* Allow copying other locales.  */
	  now = lr_token (ldfile, charmap, NULL, verbose);
	  if (now->tok != tok_string)
	    goto err_label;

	  if (! ignore_content)
	    load_locale (LC_COLLATE, now->val.str.startmb, repertoire_name,
			 charmap, result);

	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_coll_weight_max:
	  /* 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 (state != 0)
	    goto err_label;

	  arg = lr_token (ldfile, charmap, NULL, verbose);
	  if (arg->tok != tok_number)
	    goto err_label;
	  if (collate->col_weight_max != -1)
	    lr_error (ldfile, _("%s: duplicate definition of `%s'"),
		      "LC_COLLATE", "col_weight_max");
	  else
	    collate->col_weight_max = arg->val.num;
	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_section_symbol:
	  /* 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 (state != 0)
	    goto err_label;

	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok != tok_bsymbol)
	    goto err_label;
	  else if (!ignore_content)
	    {
	      /* Check whether this section is already known.  */
	      struct section_list *known = collate->sections;
	      while (known != NULL)
		{
		  if (strcmp (known->name, arg->val.str.startmb) == 0)
		    break;
		  known = known->next;
		}

	      if (known != NULL)
		{
		  lr_error (ldfile,
			    _("%s: duplicate declaration of section `%s'"),
			    "LC_COLLATE", arg->val.str.startmb);
		  free (arg->val.str.startmb);
		}
	      else
		collate->sections = make_seclist_elem (collate,
						       arg->val.str.startmb,
						       collate->sections);

	      lr_ignore_rest (ldfile, known == NULL);
	    }
	  else
	    {
	      free (arg->val.str.startmb);
	      lr_ignore_rest (ldfile, 0);
	    }
	  break;

	case tok_collating_element:
	  /* 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 (state != 0)
	    goto err_label;

	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok != tok_bsymbol)
	    goto err_label;
	  else
	    {
	      const char *symbol = arg->val.str.startmb;
	      size_t symbol_len = arg->val.str.lenmb;

	      /* Next the `from' keyword.  */
	      arg = lr_token (ldfile, charmap, repertoire, verbose);
	      if (arg->tok != tok_from)
		{
		  free ((char *) symbol);
		  goto err_label;
		}

	      ldfile->return_widestr = 1;
	      ldfile->translate_strings = 1;

	      /* Finally the string with the replacement.  */
	      arg = lr_token (ldfile, charmap, repertoire, verbose);

	      ldfile->return_widestr = 0;
	      ldfile->translate_strings = 0;

	      if (arg->tok != tok_string)
		goto err_label;

	      if (!ignore_content && symbol != NULL)
		{
		  /* The name is already defined.  */
		  if (check_duplicate (ldfile, collate, charmap,
				       repertoire, symbol, symbol_len))
		    goto col_elem_free;

		  if (arg->val.str.startmb != NULL)
		    insert_entry (&collate->elem_table, symbol, symbol_len,
				  new_element (collate,
					       arg->val.str.startmb,
					       arg->val.str.lenmb - 1,
					       arg->val.str.startwc,
					       symbol, symbol_len, 0));
		}
	      else
		{
		col_elem_free:
		  if (symbol != NULL)
		    free ((char *) symbol);
		  if (arg->val.str.startmb != NULL)
		    free (arg->val.str.startmb);
		  if (arg->val.str.startwc != NULL)
		    free (arg->val.str.startwc);
		}
	      lr_ignore_rest (ldfile, 1);
	    }
	  break;

	case tok_collating_symbol:
	  /* 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 (state != 0 && state != 2)
	    goto err_label;

	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok != tok_bsymbol)
	    goto err_label;
	  else
	    {
	      char *symbol = arg->val.str.startmb;
	      size_t symbol_len = arg->val.str.lenmb;
	      char *endsymbol = NULL;
	      size_t endsymbol_len = 0;
	      enum token_t ellipsis = tok_none;

	      arg = lr_token (ldfile, charmap, repertoire, verbose);
	      if (arg->tok == tok_ellipsis2 || arg->tok == tok_ellipsis4)
		{
		  ellipsis = arg->tok;

		  arg = lr_token (ldfile, charmap, repertoire, verbose);
		  if (arg->tok != tok_bsymbol)
		    {
		      free (symbol);
		      goto err_label;
		    }

		  endsymbol = arg->val.str.startmb;
		  endsymbol_len = arg->val.str.lenmb;

		  lr_ignore_rest (ldfile, 1);
		}
	      else if (arg->tok != tok_eol)
		{
		  free (symbol);
		  goto err_label;
		}

	      if (!ignore_content)
		{
		  if (symbol == NULL
		      || (ellipsis != tok_none && endsymbol == NULL))
		    {
		      lr_error (ldfile, _("\
%s: unknown character in collating symbol name"),
				"LC_COLLATE");
		      goto col_sym_free;
		    }
		  else if (ellipsis == tok_none)
		    {
		      /* A single symbol, no ellipsis.  */
		      if (check_duplicate (ldfile, collate, charmap,
					   repertoire, symbol, symbol_len))
			/* The name is already defined.  */
			goto col_sym_free;

		      insert_entry (&collate->sym_table, symbol, symbol_len,
				    new_symbol (collate, symbol, symbol_len));
		    }
		  else if (symbol_len != endsymbol_len)
		    {
		    col_sym_inv_range:
		      lr_error (ldfile,
				_("invalid names for character range"));
		      goto col_sym_free;
		    }
		  else
		    {
		      /* Oh my, we have to handle an ellipsis.  First, as
			 usual, determine the common prefix and then
			 convert the rest into a range.  */
		      size_t prefixlen;
		      unsigned long int from;
		      unsigned long int to;
		      char *endp;

		      for (prefixlen = 0; prefixlen < symbol_len; ++prefixlen)
			if (symbol[prefixlen] != endsymbol[prefixlen])
			  break;

		      /* Convert the rest into numbers.  */
		      symbol[symbol_len] = '\0';
		      from = strtoul (&symbol[prefixlen], &endp,
				      ellipsis == tok_ellipsis2 ? 16 : 10);
		      if (*endp != '\0')
			goto col_sym_inv_range;

		      endsymbol[symbol_len] = '\0';
		      to = strtoul (&endsymbol[prefixlen], &endp,
				    ellipsis == tok_ellipsis2 ? 16 : 10);
		      if (*endp != '\0')
			goto col_sym_inv_range;

		      if (from > to)
			goto col_sym_inv_range;

		      /* Now loop over all entries.  */
		      while (from <= to)
			{
			  char *symbuf;

			  symbuf = (char *) obstack_alloc (&collate->mempool,
							   symbol_len + 1);

			  /* Create the name.  */
			  sprintf (symbuf,
				   ellipsis == tok_ellipsis2
				   ? "%.*s%.*lX" : "%.*s%.*lu",
				   (int) prefixlen, symbol,
				   (int) (symbol_len - prefixlen), from);

			  if (check_duplicate (ldfile, collate, charmap,
					       repertoire, symbuf, symbol_len))
			    /* The name is already defined.  */
			    goto col_sym_free;

			  insert_entry (&collate->sym_table, symbuf,
					symbol_len,
					new_symbol (collate, symbuf,
						    symbol_len));

			  /* Increment the counter.  */
			  ++from;
			}

		      goto col_sym_free;
		    }
		}
	      else
		{
		col_sym_free:
		  if (symbol != NULL)
		    free (symbol);
		  if (endsymbol != NULL)
		    free (endsymbol);
		}
	    }
	  break;

	case tok_symbol_equivalence:
	  /* 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 (state != 0)
	    goto err_label;

	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok != tok_bsymbol)
	    goto err_label;
	  else
	    {
	      const char *newname = arg->val.str.startmb;
	      size_t newname_len = arg->val.str.lenmb;
	      const char *symname;
	      size_t symname_len;
	      struct symbol_t *symval;

	      arg = lr_token (ldfile, charmap, repertoire, verbose);
	      if (arg->tok != tok_bsymbol)
		{
		  if (newname != NULL)
		    free ((char *) newname);
		  goto err_label;
		}

	      symname = arg->val.str.startmb;
	      symname_len = arg->val.str.lenmb;

	      if (newname == NULL)
		{
		  lr_error (ldfile, _("\
%s: unknown character in equivalent definition name"),
			    "LC_COLLATE");

		sym_equiv_free:
		  if (newname != NULL)
		    free ((char *) newname);
		  if (symname != NULL)
		    free ((char *) symname);
		  break;
		}
	      if (symname == NULL)
		{
		  lr_error (ldfile, _("\
%s: unknown character in equivalent definition value"),
			    "LC_COLLATE");
		  goto sym_equiv_free;
		}

	      /* See whether the symbol name is already defined.  */
	      if (find_entry (&collate->sym_table, symname, symname_len,
			      (void **) &symval) != 0)
		{
		  lr_error (ldfile, _("\
%s: unknown symbol `%s' in equivalent definition"),
			    "LC_COLLATE", symname);
		  goto col_sym_free;
		}

	      if (insert_entry (&collate->sym_table,
				newname, newname_len, symval) < 0)
		{
		  lr_error (ldfile, _("\
error while adding equivalent collating symbol"));
		  goto sym_equiv_free;
		}

	      free ((char *) symname);
	    }
	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_script:
	  /* We get told about the scripts we know.  */
	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok != tok_bsymbol)
	    goto err_label;
	  else
	    {
	      struct section_list *runp = collate->known_sections;
	      char *name;

	      while (runp != NULL)
		if (strncmp (runp->name, arg->val.str.startmb,
			     arg->val.str.lenmb) == 0
		    && runp->name[arg->val.str.lenmb] == '\0')
		  break;
		else
		  runp = runp->def_next;

	      if (runp != NULL)
		{
		  lr_error (ldfile, _("duplicate definition of script `%s'"),
			    runp->name);
		  lr_ignore_rest (ldfile, 0);
		  break;
		}

	      runp = (struct section_list *) xcalloc (1, sizeof (*runp));
	      name = (char *) xmalloc (arg->val.str.lenmb + 1);
	      memcpy (name, arg->val.str.startmb, arg->val.str.lenmb);
	      name[arg->val.str.lenmb] = '\0';
	      runp->name = name;

	      runp->def_next = collate->known_sections;
	      collate->known_sections = runp;
	    }
	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_order_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;
	    }

	  if (state != 0 && state != 1)
	    goto err_label;
	  state = 1;

	  /* The 14652 draft does not specify whether all `order_start' lines
	     must contain the same number of sort-rules, but 14651 does.  So
	     we require this here as well.  */
	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok == tok_bsymbol)
	    {
	      /* This better should be a section name.  */
	      struct section_list *sp = collate->known_sections;
	      while (sp != NULL
		     && (sp->name == NULL
			 || strncmp (sp->name, arg->val.str.startmb,
				     arg->val.str.lenmb) != 0
			 || sp->name[arg->val.str.lenmb] != '\0'))
		sp = sp->def_next;

	      if (sp == NULL)
		{
		  lr_error (ldfile, _("\
%s: unknown section name `%s'"),
			    "LC_COLLATE", arg->val.str.startmb);
		  /* We use the error section.  */
		  collate->current_section = &collate->error_section;

		  if (collate->error_section.first == NULL)
		    {
		      /* Insert &collate->error_section at the end of
			 the collate->sections list.  */
		      if (collate->sections == NULL)
			collate->sections = &collate->error_section;
		      else
			{
			  sp = collate->sections;
			  while (sp->next != NULL)
			    sp = sp->next;

			  sp->next = &collate->error_section;
			}
		      collate->error_section.next = NULL;
		    }
		}
	      else
		{
		  /* One should not be allowed to open the same
                     section twice.  */
		  if (sp->first != NULL)
		    lr_error (ldfile, _("\
%s: multiple order definitions for section `%s'"),
			      "LC_COLLATE", sp->name);
		  else
		    {
		      /* Insert sp in the collate->sections list,
			 right after collate->current_section.  */
		      if (collate->current_section == NULL)
			collate->current_section = sp;
		      else
			{
			  sp->next = collate->current_section->next;
			  collate->current_section->next = sp;
			}
		    }

		  /* Next should come the end of the line or a semicolon.  */
		  arg = lr_token (ldfile, charmap, repertoire, verbose);
		  if (arg->tok == tok_eol)
		    {
		      uint32_t cnt;

		      /* This means we have exactly one rule: `forward'.  */
		      if (nrules > 1)
			lr_error (ldfile, _("\
%s: invalid number of sorting rules"),
				  "LC_COLLATE");
		      else
			nrules = 1;
		      sp->rules = obstack_alloc (&collate->mempool,
						 (sizeof (enum coll_sort_rule)
						  * nrules));
		      for (cnt = 0; cnt < nrules; ++cnt)
			sp->rules[cnt] = sort_forward;

		      /* Next line.  */
		      break;
		    }

		  /* Get the next token.  */
		  arg = lr_token (ldfile, charmap, repertoire, verbose);
		}
	    }
	  else
	    {
	      /* There is no section symbol.  Therefore we use the unnamed
		 section.  */
	      collate->current_section = &collate->unnamed_section;

	      if (collate->unnamed_section.first != NULL)
		lr_error (ldfile, _("\
%s: multiple order definitions for unnamed section"),
			  "LC_COLLATE");
	      else
		{
		  /* Insert &collate->unnamed_section at the beginning of
		     the collate->sections list.  */
		  collate->unnamed_section.next = collate->sections;
		  collate->sections = &collate->unnamed_section;
		}
	    }

	  /* Now read the direction names.  */
	  read_directions (ldfile, arg, charmap, repertoire, collate);

	  /* From now we need the strings untranslated.  */
	  ldfile->translate_strings = 0;
	  break;

	case tok_order_end:
	  /* 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 (state != 1)
	    goto err_label;

	  /* Handle ellipsis at end of list.  */
	  if (was_ellipsis != tok_none)
	    {
	      handle_ellipsis (ldfile, NULL, 0, was_ellipsis, charmap,
			       repertoire, collate);
	      was_ellipsis = tok_none;
	    }

	  state = 2;
	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_reorder_after:
	  /* 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 (state == 1)
	    {
	      lr_error (ldfile, _("%s: missing `order_end' keyword"),
			"LC_COLLATE");
	      state = 2;

	      /* Handle ellipsis at end of list.  */
	      if (was_ellipsis != tok_none)
		{
		  handle_ellipsis (ldfile, arg->val.str.startmb,
				   arg->val.str.lenmb, was_ellipsis, charmap,
				   repertoire, collate);
		  was_ellipsis = tok_none;
		}
	    }
	  else if (state != 2 && state != 3)
	    goto err_label;
	  state = 3;

	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok == tok_bsymbol || arg->tok == tok_ucs4)
	    {
	      /* Find this symbol in the sequence table.  */
	      char ucsbuf[10];
	      char *startmb;
	      size_t lenmb;
	      struct element_t *insp;
	      int no_error = 1;

	      if (arg->tok == tok_bsymbol)
		{
		  startmb = arg->val.str.startmb;
		  lenmb = arg->val.str.lenmb;
		}
	      else
		{
		  sprintf (ucsbuf, "U%08X", arg->val.ucs4);
		  startmb = ucsbuf;
		  lenmb = 9;
		}

	      if (find_entry (&collate->seq_table, startmb, lenmb,
			      (void **) &insp) == 0)
		/* Yes, the symbol exists.  Simply point the cursor
		   to it.  */
		collate->cursor = insp;
	      else
		{
		  struct symbol_t *symbp;

		  if (find_entry (&collate->sym_table, startmb, lenmb,
				  (void **) &symbp) == 0)
		    {
		      if (symbp->order->last != NULL
			  || symbp->order->next != NULL)
			collate->cursor = symbp->order;
		      else
			{
			  /* This is a collating symbol but its position
			     is not yet defined.  */
			  lr_error (ldfile, _("\
%s: order for collating symbol %.*s not yet defined"),
				    "LC_COLLATE", (int) lenmb, startmb);
			  collate->cursor = NULL;
			  no_error = 0;
			}
		    }
		  else if (find_entry (&collate->elem_table, startmb, lenmb,
				       (void **) &insp) == 0)
		    {
		      if (insp->last != NULL || insp->next != NULL)
			collate->cursor = insp;
		      else
			{
			  /* This is a collating element but its position
			     is not yet defined.  */
			  lr_error (ldfile, _("\
%s: order for collating element %.*s not yet defined"),
				    "LC_COLLATE", (int) lenmb, startmb);
			  collate->cursor = NULL;
			  no_error = 0;
			}
		    }
		  else
		    {
		      /* This is bad.  The symbol after which we have to
			 insert does not exist.  */
		      lr_error (ldfile, _("\
%s: cannot reorder after %.*s: symbol not known"),
				"LC_COLLATE", (int) lenmb, startmb);
		      collate->cursor = NULL;
		      no_error = 0;
		    }
		}

	      lr_ignore_rest (ldfile, no_error);
	    }
	  else
	    /* This must not happen.  */
	    goto err_label;
	  break;

	case tok_reorder_end:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    break;

	  if (state != 3)
	    goto err_label;
	  state = 4;
	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_reorder_sections_after:
	  /* 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 (state == 1)
	    {
	      lr_error (ldfile, _("%s: missing `order_end' keyword"),
			"LC_COLLATE");
	      state = 2;

	      /* Handle ellipsis at end of list.  */
	      if (was_ellipsis != tok_none)
		{
		  handle_ellipsis (ldfile, NULL, 0, was_ellipsis, charmap,
				   repertoire, collate);
		  was_ellipsis = tok_none;
		}
	    }
	  else if (state == 3)
	    {
	      error (0, 0, _("%s: missing `reorder-end' keyword"),
		     "LC_COLLATE");
	      state = 4;
	    }
	  else if (state != 2 && state != 4)
	    goto err_label;
	  state = 5;

	  /* Get the name of the sections we are adding after.  */
	  arg = lr_token (ldfile, charmap, repertoire, verbose);
	  if (arg->tok == tok_bsymbol)
	    {
	      /* Now find a section with this name.  */
	      struct section_list *runp = collate->sections;

	      while (runp != NULL)
		{
		  if (runp->name != NULL
		      && strlen (runp->name) == arg->val.str.lenmb
		      && memcmp (runp->name, arg->val.str.startmb,
				 arg->val.str.lenmb) == 0)
		    break;

		  runp = runp->next;
		}

	      if (runp != NULL)
		collate->current_section = runp;
	      else
		{
		  /* This is bad.  The section after which we have to
                     reorder does not exist.  Therefore we cannot
                     process the whole rest of this reorder
                     specification.  */
		  lr_error (ldfile, _("%s: section `%.*s' not known"),
			    "LC_COLLATE", (int) arg->val.str.lenmb,
			    arg->val.str.startmb);

		  do
		    {
		      lr_ignore_rest (ldfile, 0);

		      now = lr_token (ldfile, charmap, NULL, verbose);
		    }
		  while (now->tok == tok_reorder_sections_after
			 || now->tok == tok_reorder_sections_end
			 || now->tok == tok_end);

		  /* Process the token we just saw.  */
		  nowtok = now->tok;
		  continue;
		}
	    }
	  else
	    /* This must not happen.  */
	    goto err_label;
	  break;

	case tok_reorder_sections_end:
	  /* Ignore the rest of the line if we don't need the input of
	     this line.  */
	  if (ignore_content)
	    break;

	  if (state != 5)
	    goto err_label;
	  state = 6;
	  lr_ignore_rest (ldfile, 1);
	  break;

	case tok_bsymbol:
	case tok_ucs4:
	  /* 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 (state != 0 && state != 1 && state != 3 && state != 5)
	    goto err_label;

	  if ((state == 0 || state == 5) && nowtok == tok_ucs4)
	    goto err_label;

	  if (nowtok == tok_ucs4)
	    {
	      snprintf (ucs4buf, sizeof (ucs4buf), "U%08X", now->val.ucs4);
	      symstr = ucs4buf;
	      symlen = 9;
	    }
	  else
	    {
	      symstr = arg->val.str.startmb;
	      symlen = arg->val.str.lenmb;
	    }

	  if (state == 0)
	    {
	      /* We are outside an `order_start' region.  This means
                 we must only accept definitions of values for
                 collation symbols since these are purely abstract
                 values and don't need directions associated.  */
	      struct element_t *seqp;

	      if (find_entry (&collate->seq_table, symstr, symlen,
			      (void **) &seqp) == 0)
		{
		  /* It's already defined.  First check whether this
		     is really a collating symbol.  */
		  if (seqp->is_character)
		    goto err_label;

		  goto move_entry;
		}
	      else
		{
		  void *result;

		  if (find_entry (&collate->sym_table, symstr, symlen,
				  &result) != 0)
		    /* No collating symbol, it's an error.  */
		    goto err_label;

		  /* Maybe this is the first time we define a symbol
		     value and it is before the first actual section.  */
		  if (collate->sections == NULL)
		    collate->sections = collate->current_section =
		      &collate->symbol_section;
		}

	      if (was_ellipsis != tok_none)
		{

		  handle_ellipsis (ldfile, symstr, symlen, was_ellipsis,
				   charmap, repertoire, collate);

		  /* Remember that we processed the ellipsis.  */
		  was_ellipsis = tok_none;

		  /* And don't add the value a second time.  */
		  break;
		}
	    }
	  else if (state == 3)
	    {
	      /* It is possible that we already have this collation sequence.
		 In this case we move the entry.  */
	      struct element_t *seqp;
	      void *sym;

	      /* If the symbol after which we have to insert was not found
		 ignore all entries.  */
	      if (collate->cursor == NULL)
		{
		  lr_ignore_rest (ldfile, 0);
		  break;
		}

	      if (find_entry (&collate->seq_table, symstr, symlen,
			      (void **) &seqp) == 0)
		goto move_entry;

	      if (find_entry (&collate->sym_table, symstr, symlen, &sym) == 0
		  && (seqp = ((struct symbol_t *) sym)->order) != NULL)
		goto move_entry;

	      if (find_entry (&collate->elem_table, symstr, symlen,
			      (void **) &seqp) == 0)
		{
		move_entry:
		  /* Remove the entry from the old position.  */
		  if (seqp->last == NULL)
		    collate->start = seqp->next;
		  else
		    seqp->last->next = seqp->next;
		  if (seqp->next != NULL)
		    seqp->next->last = seqp->last;

		  /* We also have to check whether this entry is the
                     first or last of a section.  */
		  if (seqp->section->first == seqp)
		    {
		      if (seqp->section->first == seqp->section->last)
			/* This section has no content anymore.  */
			seqp->section->first = seqp->section->last = NULL;
		      else
			seqp->section->first = seqp->next;
		    }
		  else if (seqp->section->last == seqp)
		    seqp->section->last = seqp->last;

		  /* Now insert it in the new place.  */
		  insert_weights (ldfile, seqp, charmap, repertoire, collate,
				  tok_none);
		  break;
		}

	      /* Otherwise we just add a new entry.  */
	    }
	  else if (state == 5)
	    {
	      /* We are reordering sections.  Find the named section.  */
	      struct section_list *runp = collate->sections;
	      struct section_list *prevp = NULL;

	      while (runp != NULL)
		{
		  if (runp->name != NULL
		      && strlen (runp->name) == symlen
		      && memcmp (runp->name, symstr, symlen) == 0)
		    break;

		  prevp = runp;
		  runp = runp->next;
		}

	      if (runp == NULL)
		{
		  lr_error (ldfile, _("%s: section `%.*s' not known"),
			    "LC_COLLATE", (int) symlen, symstr);
		  lr_ignore_rest (ldfile, 0);
		}
	      else
		{
		  if (runp != collate->current_section)
		    {
		      /* Remove the named section from the old place and
			 insert it in the new one.  */
		      prevp->next = runp->next;

		      runp->next = collate->current_section->next;
		      collate->current_section->next = runp;
		      collate->current_section = runp;
		    }

		  /* Process the rest of the line which might change
                     the collation rules.  */
		  arg = lr_token (ldfile, charmap, repertoire, verbose);
		  if (arg->tok != tok_eof && arg->tok != tok_eol)
		    read_directions (ldfile, arg, charmap, repertoire,
				     collate);
		}
	      break;
	    }
	  else if (was_ellipsis != tok_none)
	    {
	      /* Using the information in the `ellipsis_weight'
                 element and this and the last value we have to handle
                 the ellipsis now.  */
	      assert (state == 1);

	      handle_ellipsis (ldfile, symstr, symlen, was_ellipsis, charmap,
			       repertoire, collate);

	      /* Remember that we processed the ellipsis.  */
	      was_ellipsis = tok_none;

	      /* And don't add the value a second time.  */
	      break;
	    }

	  /* Now insert in the new place.  */
	  insert_value (ldfile, symstr, symlen, charmap, repertoire, collate);
	  break;

	case tok_undefined:
	  /* 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 (state != 1)
	    goto err_label;

	  if (was_ellipsis != tok_none)
	    {
	      lr_error (ldfile,
			_("%s: cannot have `%s' as end of ellipsis range"),
			"LC_COLLATE", "UNDEFINED");

	      unlink_element (collate);
	      was_ellipsis = tok_none;
	    }

	  /* See whether UNDEFINED already appeared somewhere.  */
	  if (collate->undefined.next != NULL
	      || &collate->undefined == collate->cursor)
	    {
	      lr_error (ldfile,
			_("%s: order for `%.*s' already defined at %s:%Zu"),
			"LC_COLLATE", 9, "UNDEFINED",
			collate->undefined.file,
			collate->undefined.line);
	      lr_ignore_rest (ldfile, 0);
	    }
	  else
	    /* Parse the weights.  */
	     insert_weights (ldfile, &collate->undefined, charmap,
			     repertoire, collate, tok_none);
	  break;

	case tok_ellipsis2: /* symbolic hexadecimal ellipsis */
	case tok_ellipsis3: /* absolute ellipsis */
	case tok_ellipsis4: /* symbolic decimal ellipsis */
	  /* This is the symbolic (decimal or hexadecimal) or absolute
             ellipsis.  */
	  if (was_ellipsis != tok_none)
	    goto err_label;

	  if (state != 0 && state != 1 && state != 3)
	    goto err_label;

	  was_ellipsis = nowtok;

	  insert_weights (ldfile, &collate->ellipsis_weight, charmap,
			  repertoire, collate, nowtok);
	  break;

	case tok_end:
	  /* Next we assume `LC_COLLATE'.  */
	  if (!ignore_content)
	    {
	      if (state == 0)
		/* We must either see a copy statement or have
		   ordering values.  */
		lr_error (ldfile,
			  _("%s: empty category description not allowed"),
			  "LC_COLLATE");
	      else if (state == 1)
		{
		  lr_error (ldfile, _("%s: missing `order_end' keyword"),
			    "LC_COLLATE");

		  /* Handle ellipsis at end of list.  */
		  if (was_ellipsis != tok_none)
		    {
		      handle_ellipsis (ldfile, NULL, 0, was_ellipsis, charmap,
				       repertoire, collate);
		      was_ellipsis = tok_none;
		    }
		}
	      else if (state == 3)
		error (0, 0, _("%s: missing `reorder-end' keyword"),
		       "LC_COLLATE");
	      else if (state == 5)
		error (0, 0, _("%s: missing `reorder-sections-end' keyword"),
		       "LC_COLLATE");
	    }
	  arg = lr_token (ldfile, charmap, NULL, verbose);
	  if (arg->tok == tok_eof)
	    break;
	  if (arg->tok == tok_eol)
	    lr_error (ldfile, _("%s: incomplete `END' line"), "LC_COLLATE");
	  else if (arg->tok != tok_lc_collate)
	    lr_error (ldfile, _("\
%1$s: definition does not end with `END %1$s'"), "LC_COLLATE");
	  lr_ignore_rest (ldfile, arg->tok == tok_lc_collate);
	  return;

	default:
	err_label:
	  SYNTAX_ERROR (_("%s: syntax error"), "LC_COLLATE");
	}

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

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