Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
/*	$NetBSD: env.cpp,v 1.1.1.1 2016/01/13 18:41:48 christos Exp $	*/

// -*- C++ -*-
/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005
   Free Software Foundation, Inc.
     Written by James Clark (jjc@jclark.com)

This file is part of groff.

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

groff 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 General Public License
for more details.

You should have received a copy of the GNU General Public License along
with groff; see the file COPYING.  If not, write to the Free Software
Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */

#include "troff.h"
#include "dictionary.h"
#include "hvunits.h"
#include "stringclass.h"
#include "mtsm.h"
#include "env.h"
#include "request.h"
#include "node.h"
#include "token.h"
#include "div.h"
#include "reg.h"
#include "charinfo.h"
#include "macropath.h"
#include "input.h"
#include <math.h>

symbol default_family("T");

enum { ADJUST_LEFT = 0, ADJUST_BOTH = 1, ADJUST_CENTER = 3, ADJUST_RIGHT = 5 };

enum { HYPHEN_LAST_LINE = 2, HYPHEN_LAST_CHARS = 4, HYPHEN_FIRST_CHARS = 8 };

struct env_list {
  environment *env;
  env_list *next;
  env_list(environment *e, env_list *p) : env(e), next(p) {}
};

env_list *env_stack;
const int NENVIRONMENTS = 10;
environment *env_table[NENVIRONMENTS];
dictionary env_dictionary(10);
environment *curenv;
static int next_line_number = 0;
extern int suppress_push;
extern statem *get_diversion_state();

charinfo *field_delimiter_char;
charinfo *padding_indicator_char;

int translate_space_to_dummy = 0;

class pending_output_line {
  node *nd;
  int no_fill;
  int was_centered;
  vunits vs;
  vunits post_vs;
  hunits width;
#ifdef WIDOW_CONTROL
  int last_line;		// Is it the last line of the paragraph?
#endif /* WIDOW_CONTROL */
public:
  pending_output_line *next;

  pending_output_line(node *, int, vunits, vunits, hunits, int,
		      pending_output_line * = 0);
  ~pending_output_line();
  int output();

#ifdef WIDOW_CONTROL
  friend void environment::mark_last_line();
  friend void environment::output(node *, int, vunits, vunits, hunits, int);
#endif /* WIDOW_CONTROL */
};

pending_output_line::pending_output_line(node *n, int nf, vunits v, vunits pv,
					 hunits w, int ce,
					 pending_output_line *p)
: nd(n), no_fill(nf), was_centered(ce), vs(v), post_vs(pv), width(w),
#ifdef WIDOW_CONTROL
  last_line(0),
#endif /* WIDOW_CONTROL */
  next(p)
{
}

pending_output_line::~pending_output_line()
{
  delete_node_list(nd);
}

int pending_output_line::output()
{
  if (trap_sprung_flag)
    return 0;
#ifdef WIDOW_CONTROL
  if (next && next->last_line && !no_fill) {
    curdiv->need(vs + post_vs + vunits(vresolution));
    if (trap_sprung_flag) {
      next->last_line = 0;	// Try to avoid infinite loops.
      return 0;
    }
  }
#endif
  curenv->construct_format_state(nd, was_centered, !no_fill);
  curdiv->output(nd, no_fill, vs, post_vs, width);
  nd = 0;
  return 1;
}

void environment::output(node *nd, int no_fill_flag,
			 vunits vs, vunits post_vs,
			 hunits width, int was_centered)
{
#ifdef WIDOW_CONTROL
  while (pending_lines) {
    if (widow_control && !pending_lines->no_fill && !pending_lines->next)
      break;
    if (!pending_lines->output())
      break;
    pending_output_line *tem = pending_lines;
    pending_lines = pending_lines->next;
    delete tem;
  }
#else /* WIDOW_CONTROL */
  output_pending_lines();
#endif /* WIDOW_CONTROL */
  if (!trap_sprung_flag && !pending_lines
#ifdef WIDOW_CONTROL
      && (!widow_control || no_fill_flag)
#endif /* WIDOW_CONTROL */
      ) {
    curenv->construct_format_state(nd, was_centered, !no_fill_flag);
    curdiv->output(nd, no_fill_flag, vs, post_vs, width);
  } else {
    pending_output_line **p;
    for (p = &pending_lines; *p; p = &(*p)->next)
      ;
    *p = new pending_output_line(nd, no_fill_flag, vs, post_vs, width,
				 was_centered);
  }
}

// a line from .tl goes at the head of the queue

void environment::output_title(node *nd, int no_fill_flag,
			       vunits vs, vunits post_vs,
			       hunits width)
{
  if (!trap_sprung_flag)
    curdiv->output(nd, no_fill_flag, vs, post_vs, width);
  else
    pending_lines = new pending_output_line(nd, no_fill_flag, vs, post_vs,
					    width, 0, pending_lines);
}

void environment::output_pending_lines()
{
  while (pending_lines && pending_lines->output()) {
    pending_output_line *tem = pending_lines;
    pending_lines = pending_lines->next;
    delete tem;
  }
}

#ifdef WIDOW_CONTROL

void environment::mark_last_line()
{
  if (!widow_control || !pending_lines)
    return;
  pending_output_line *p;
  for (p = pending_lines; p->next; p = p->next)
    ;
  if (!p->no_fill)
    p->last_line = 1;
}

void widow_control_request()
{
  int n;
  if (has_arg() && get_integer(&n))
    curenv->widow_control = n != 0;
  else
    curenv->widow_control = 1;
  skip_line();
}

#endif /* WIDOW_CONTROL */

/* font_size functions */

size_range *font_size::size_table = 0;
int font_size::nranges = 0;

extern "C" {

int compare_ranges(const void *p1, const void *p2)
{
  return ((size_range *)p1)->min - ((size_range *)p2)->min;
}

}

void font_size::init_size_table(int *sizes)
{
  nranges = 0;
  while (sizes[nranges*2] != 0)
    nranges++;
  assert(nranges > 0);
  size_table = new size_range[nranges];
  for (int i = 0; i < nranges; i++) {
    size_table[i].min = sizes[i*2];
    size_table[i].max = sizes[i*2 + 1];
  }
  qsort(size_table, nranges, sizeof(size_range), compare_ranges);
}

font_size::font_size(int sp)
{
  for (int i = 0; i < nranges; i++) {
    if (sp < size_table[i].min) {
      if (i > 0 && size_table[i].min - sp >= sp - size_table[i - 1].max)
	p = size_table[i - 1].max;
      else
	p = size_table[i].min;
      return;
    }
    if (sp <= size_table[i].max) {
      p = sp;
      return;
    }
  }
  p = size_table[nranges - 1].max;
}

int font_size::to_units()
{
  return scale(p, units_per_inch, sizescale*72);
}

// we can't do this in a static constructor because various dictionaries
// have to get initialized first

void init_environments()
{
  curenv = env_table[0] = new environment("0");
}

void tab_character()
{
  curenv->tab_char = get_optional_char();
  skip_line();
}

void leader_character()
{
  curenv->leader_char = get_optional_char();
  skip_line();
}

void environment::add_char(charinfo *ci)
{
  int s;
  node *gc_np = 0;
  if (interrupted)
    ;
  // don't allow fields in dummy environments
  else if (ci == field_delimiter_char && !dummy) {
    if (current_field)
      wrap_up_field();
    else
      start_field();
  }
  else if (current_field && ci == padding_indicator_char)
    add_padding();
  else if (current_tab) {
    if (tab_contents == 0)
      tab_contents = new line_start_node;
    if (ci != hyphen_indicator_char)
      tab_contents = tab_contents->add_char(ci, this, &tab_width, &s, &gc_np);
    else
      tab_contents = tab_contents->add_discretionary_hyphen();
  }
  else {
    if (line == 0)
      start_line();
#if 0
    fprintf(stderr, "current line is\n");
    line->debug_node_list();
#endif
    if (ci != hyphen_indicator_char)
      line = line->add_char(ci, this, &width_total, &space_total, &gc_np);
    else
      line = line->add_discretionary_hyphen();
  }
#if 0
  fprintf(stderr, "now after we have added character the line is\n");
  line->debug_node_list();
#endif
  if ((!suppress_push) && gc_np) {
    if (gc_np && (gc_np->state == 0)) {
      gc_np->state = construct_state(0);
      gc_np->push_state = get_diversion_state();
    }
    else if (line && (line->state == 0)) {
      line->state = construct_state(0);
      line->push_state = get_diversion_state();
    }
  }
#if 0
  fprintf(stderr, "now we have possibly added the state the line is\n");
  line->debug_node_list();
#endif
}

node *environment::make_char_node(charinfo *ci)
{
  return make_node(ci, this);
}

void environment::add_node(node *n)
{
  if (n == 0)
    return;
  if (!suppress_push) {
    if (n->is_special && n->state == NULL)
      n->state = construct_state(0);
    n->push_state = get_diversion_state();
  }

  if (current_tab || current_field)
    n->freeze_space();
  if (interrupted) {
    delete n;
  }
  else if (current_tab) {
    n->next = tab_contents;
    tab_contents = n;
    tab_width += n->width();
  }
  else {
    if (line == 0) {
      if (discarding && n->discardable()) {
	// XXX possibly: input_line_start -= n->width();
	delete n;
	return;
      }
      start_line();
    }
    width_total += n->width();
    space_total += n->nspaces();
    n->next = line;
    line = n;
    construct_new_line_state(line);
  }
}

void environment::add_hyphen_indicator()
{
  if (current_tab || interrupted || current_field
      || hyphen_indicator_char != 0)
    return;
  if (line == 0)
    start_line();
  line = line->add_discretionary_hyphen();
}

int environment::get_hyphenation_flags()
{
  return hyphenation_flags;
}

int environment::get_hyphen_line_max()
{
  return hyphen_line_max;
}

int environment::get_hyphen_line_count()
{
  return hyphen_line_count;
}

int environment::get_center_lines()
{
  return center_lines;
}

int environment::get_right_justify_lines()
{
  return right_justify_lines;
}

void environment::add_italic_correction()
{
  if (current_tab) {
    if (tab_contents)
      tab_contents = tab_contents->add_italic_correction(&tab_width);
  }
  else if (line)
    line = line->add_italic_correction(&width_total);
}

void environment::space_newline()
{
  assert(!current_tab && !current_field);
  if (interrupted)
    return;
  hunits x = H0;
  hunits sw = env_space_width(this);
  hunits ssw = env_sentence_space_width(this);
  if (!translate_space_to_dummy) {
    x = sw;
    if (node_list_ends_sentence(line) == 1)
      x += ssw;
  }
  width_list *w = new width_list(sw, ssw);
  if (node_list_ends_sentence(line) == 1)
    w->next = new width_list(sw, ssw);
  if (line != 0 && line->merge_space(x, sw, ssw)) {
    width_total += x;
    return;
  }
  add_node(new word_space_node(x, get_fill_color(), w));
  possibly_break_line(0, spread_flag);
  spread_flag = 0;
}

void environment::space()
{
  space(env_space_width(this), env_sentence_space_width(this));
}

void environment::space(hunits space_width, hunits sentence_space_width)
{
  if (interrupted)
    return;
  if (current_field && padding_indicator_char == 0) {
    add_padding();
    return;
  }
  hunits x = translate_space_to_dummy ? H0 : space_width;
  node *p = current_tab ? tab_contents : line;
  hunits *tp = current_tab ? &tab_width : &width_total;
  if (p && p->nspaces() == 1 && p->width() == x
      && node_list_ends_sentence(p->next) == 1) {
    hunits xx = translate_space_to_dummy ? H0 : sentence_space_width;
    if (p->merge_space(xx, space_width, sentence_space_width)) {
      *tp += xx;
      return;
    }
  }
  if (p && p->merge_space(x, space_width, sentence_space_width)) {
    *tp += x;
    return;
  }
  add_node(new word_space_node(x,
			       get_fill_color(),
			       new width_list(space_width,
					      sentence_space_width)));
  possibly_break_line(0, spread_flag);
  spread_flag = 0;
}

node *do_underline_special(int);

void environment::set_font(symbol nm)
{
  if (interrupted)
    return;
  if (nm == symbol("P") || nm.is_empty()) {
    if (family->make_definite(prev_fontno) < 0)
      return;
    int tem = fontno;
    fontno = prev_fontno;
    prev_fontno = tem;
  }
  else {
    prev_fontno = fontno;
    int n = symbol_fontno(nm);
    if (n < 0) {
      n = next_available_font_position();
      if (!mount_font(n, nm))
	return;
    }
    if (family->make_definite(n) < 0)
      return;
    fontno = n;
  }
  if (underline_spaces && fontno != prev_fontno) {
    if (fontno == get_underline_fontno())
      add_node(do_underline_special(1));
    if (prev_fontno == get_underline_fontno())
      add_node(do_underline_special(0));
  }
}

void environment::set_font(int n)
{
  if (interrupted)
    return;
  if (is_good_fontno(n)) {
    prev_fontno = fontno;
    fontno = n;
  }
  else
    warning(WARN_FONT, "bad font number");
}

void environment::set_family(symbol fam)
{
  if (interrupted)
    return;
  if (fam.is_null() || fam.is_empty()) {
    if (prev_family->make_definite(fontno) < 0)
      return;
    font_family *tem = family;
    family = prev_family;
    prev_family = tem;
  }
  else {
    font_family *f = lookup_family(fam);
    if (f->make_definite(fontno) < 0)
      return;
    prev_family = family;
    family = f;
  }
}

void environment::set_size(int n)
{
  if (interrupted)
    return;
  if (n == 0) {
    font_size temp = prev_size;
    prev_size = size;
    size = temp;
    int temp2 = prev_requested_size;
    prev_requested_size = requested_size;
    requested_size = temp2;
  }
  else {
    prev_size = size;
    size = font_size(n);
    prev_requested_size = requested_size;
    requested_size = n;
  }
}

void environment::set_char_height(int n)
{
  if (interrupted)
    return;
  if (n == requested_size || n <= 0)
    char_height = 0;
  else
    char_height = n;
}

void environment::set_char_slant(int n)
{
  if (interrupted)
    return;
  char_slant = n;
}

color *environment::get_prev_glyph_color()
{
  return prev_glyph_color;
}

color *environment::get_glyph_color()
{
  return glyph_color;
}

color *environment::get_prev_fill_color()
{
  return prev_fill_color;
}

color *environment::get_fill_color()
{
  return fill_color;
}

void environment::set_glyph_color(color *c)
{
  if (interrupted)
    return;
  curenv->prev_glyph_color = curenv->glyph_color;
  curenv->glyph_color = c;
}

void environment::set_fill_color(color *c)
{
  if (interrupted)
    return;
  curenv->prev_fill_color = curenv->fill_color;
  curenv->fill_color = c;
}

environment::environment(symbol nm)
: dummy(0),
  prev_line_length((units_per_inch*13)/2),
  line_length((units_per_inch*13)/2),
  prev_title_length((units_per_inch*13)/2),
  title_length((units_per_inch*13)/2),
  prev_size(sizescale*10),
  size(sizescale*10),
  requested_size(sizescale*10),
  prev_requested_size(sizescale*10),
  char_height(0),
  char_slant(0),
  space_size(12),
  sentence_space_size(12),
  adjust_mode(ADJUST_BOTH),
  fill(1),
  interrupted(0),
  prev_line_interrupted(0),
  center_lines(0),
  right_justify_lines(0),
  prev_vertical_spacing(points_to_units(12)),
  vertical_spacing(points_to_units(12)),
  prev_post_vertical_spacing(0),
  post_vertical_spacing(0),
  prev_line_spacing(1),
  line_spacing(1),
  prev_indent(0),
  indent(0),
  temporary_indent(0),
  have_temporary_indent(0),
  underline_lines(0),
  underline_spaces(0),
  input_trap_count(0),
  continued_input_trap(0),
  line(0),
  prev_text_length(0),
  width_total(0),
  space_total(0),
  input_line_start(0),
  line_tabs(0),
  current_tab(TAB_NONE),
  leader_node(0),
  tab_char(0),
  leader_char(charset_table['.']),
  current_field(0),
  discarding(0),
  spread_flag(0),
  margin_character_flags(0),
  margin_character_node(0),
  margin_character_distance(points_to_units(10)),
  numbering_nodes(0),
  number_text_separation(1),
  line_number_indent(0),
  line_number_multiple(1),
  no_number_count(0),
  hyphenation_flags(1),
  hyphen_line_count(0),
  hyphen_line_max(-1),
  hyphenation_space(H0),
  hyphenation_margin(H0),
  composite(0),
  pending_lines(0),
#ifdef WIDOW_CONTROL
  widow_control(0),
#endif /* WIDOW_CONTROL */
  glyph_color(&default_color),
  prev_glyph_color(&default_color),
  fill_color(&default_color),
  prev_fill_color(&default_color),
  seen_space(0),
  seen_eol(0),
  suppress_next_eol(0),
  seen_break(0),
  tabs(units_per_inch/2, TAB_LEFT),
  name(nm),
  control_char('.'),
  no_break_control_char('\''),
  hyphen_indicator_char(0)
{
  prev_family = family = lookup_family(default_family);
  prev_fontno = fontno = 1;
  if (!is_good_fontno(1))
    fatal("font number 1 not a valid font");
  if (family->make_definite(1) < 0)
    fatal("invalid default family `%1'", default_family.contents());
  prev_fontno = fontno;
}

environment::environment(const environment *e)
: dummy(1),
  prev_line_length(e->prev_line_length),
  line_length(e->line_length),
  prev_title_length(e->prev_title_length),
  title_length(e->title_length),
  prev_size(e->prev_size),
  size(e->size),
  requested_size(e->requested_size),
  prev_requested_size(e->prev_requested_size),
  char_height(e->char_height),
  char_slant(e->char_slant),
  prev_fontno(e->prev_fontno),
  fontno(e->fontno),
  prev_family(e->prev_family),
  family(e->family),
  space_size(e->space_size),
  sentence_space_size(e->sentence_space_size),
  adjust_mode(e->adjust_mode),
  fill(e->fill),
  interrupted(0),
  prev_line_interrupted(0),
  center_lines(0),
  right_justify_lines(0),
  prev_vertical_spacing(e->prev_vertical_spacing),
  vertical_spacing(e->vertical_spacing),
  prev_post_vertical_spacing(e->prev_post_vertical_spacing),
  post_vertical_spacing(e->post_vertical_spacing),
  prev_line_spacing(e->prev_line_spacing),
  line_spacing(e->line_spacing),
  prev_indent(e->prev_indent),
  indent(e->indent),
  temporary_indent(0),
  have_temporary_indent(0),
  underline_lines(0),
  underline_spaces(0),
  input_trap_count(0),
  continued_input_trap(0),
  line(0),
  prev_text_length(e->prev_text_length),
  width_total(0),
  space_total(0),
  input_line_start(0),
  line_tabs(e->line_tabs),
  current_tab(TAB_NONE),
  leader_node(0),
  tab_char(e->tab_char),
  leader_char(e->leader_char),
  current_field(0),
  discarding(0),
  spread_flag(0),
  margin_character_flags(e->margin_character_flags),
  margin_character_node(e->margin_character_node),
  margin_character_distance(e->margin_character_distance),
  numbering_nodes(0),
  number_text_separation(e->number_text_separation),
  line_number_indent(e->line_number_indent),
  line_number_multiple(e->line_number_multiple),
  no_number_count(e->no_number_count),
  hyphenation_flags(e->hyphenation_flags),
  hyphen_line_count(0),
  hyphen_line_max(e->hyphen_line_max),
  hyphenation_space(e->hyphenation_space),
  hyphenation_margin(e->hyphenation_margin),
  composite(0),
  pending_lines(0),
#ifdef WIDOW_CONTROL
  widow_control(e->widow_control),
#endif /* WIDOW_CONTROL */
  glyph_color(e->glyph_color),
  prev_glyph_color(e->prev_glyph_color),
  fill_color(e->fill_color),
  prev_fill_color(e->prev_fill_color),
  seen_space(e->seen_space),
  seen_eol(e->seen_eol),
  suppress_next_eol(e->suppress_next_eol),
  seen_break(e->seen_break),
  tabs(e->tabs),
  name(e->name),		// so that eg `.if "\n[.ev]"0"' works
  control_char(e->control_char),
  no_break_control_char(e->no_break_control_char),
  hyphen_indicator_char(e->hyphen_indicator_char)
{
}

void environment::copy(const environment *e)
{
  prev_line_length = e->prev_line_length;
  line_length = e->line_length;
  prev_title_length = e->prev_title_length;
  title_length = e->title_length;
  prev_size = e->prev_size;
  size = e->size;
  prev_requested_size = e->prev_requested_size;
  requested_size = e->requested_size;
  char_height = e->char_height;
  char_slant = e->char_slant;
  space_size = e->space_size;
  sentence_space_size = e->sentence_space_size;
  adjust_mode = e->adjust_mode;
  fill = e->fill;
  interrupted = 0;
  prev_line_interrupted = 0;
  center_lines = 0;
  right_justify_lines = 0;
  prev_vertical_spacing = e->prev_vertical_spacing;
  vertical_spacing = e->vertical_spacing;
  prev_post_vertical_spacing = e->prev_post_vertical_spacing,
  post_vertical_spacing = e->post_vertical_spacing,
  prev_line_spacing = e->prev_line_spacing;
  line_spacing = e->line_spacing;
  prev_indent = e->prev_indent;
  indent = e->indent;
  have_temporary_indent = 0;
  temporary_indent = 0;
  underline_lines = 0;
  underline_spaces = 0;
  input_trap_count = 0;
  continued_input_trap = 0;
  prev_text_length = e->prev_text_length;
  width_total = 0;
  space_total = 0;
  input_line_start = 0;
  control_char = e->control_char;
  no_break_control_char = e->no_break_control_char;
  hyphen_indicator_char = e->hyphen_indicator_char;
  spread_flag = 0;
  line = 0;
  pending_lines = 0;
  discarding = 0;
  tabs = e->tabs;
  line_tabs = e->line_tabs;
  current_tab = TAB_NONE;
  current_field = 0;
  margin_character_flags = e->margin_character_flags;
  margin_character_node = e->margin_character_node;
  margin_character_distance = e->margin_character_distance;
  numbering_nodes = 0;
  number_text_separation = e->number_text_separation;
  line_number_multiple = e->line_number_multiple;
  line_number_indent = e->line_number_indent;
  no_number_count = e->no_number_count;
  tab_char = e->tab_char;
  leader_char = e->leader_char;
  hyphenation_flags = e->hyphenation_flags;
  fontno = e->fontno;
  prev_fontno = e->prev_fontno;
  dummy = e->dummy;
  family = e->family;
  prev_family = e->prev_family;
  leader_node = 0;
#ifdef WIDOW_CONTROL
  widow_control = e->widow_control;
#endif /* WIDOW_CONTROL */
  hyphen_line_max = e->hyphen_line_max;
  hyphen_line_count = 0;
  hyphenation_space = e->hyphenation_space;
  hyphenation_margin = e->hyphenation_margin;
  composite = 0;
  glyph_color= e->glyph_color;
  prev_glyph_color = e->prev_glyph_color;
  fill_color = e->fill_color;
  prev_fill_color = e->prev_fill_color;
}

environment::~environment()
{
  delete leader_node;
  delete_node_list(line);
  delete_node_list(numbering_nodes);
}

hunits environment::get_input_line_position()
{
  hunits n;
  if (line == 0)
    n = -input_line_start;
  else
    n = width_total - input_line_start;
  if (current_tab)
    n += tab_width;
  return n;
}

void environment::set_input_line_position(hunits n)
{
  input_line_start = line == 0 ? -n : width_total - n;
  if (current_tab)
    input_line_start += tab_width;
}

hunits environment::get_line_length()
{
  return line_length;
}

hunits environment::get_saved_line_length()
{
  if (line)
    return target_text_length + saved_indent;
  else
    return line_length;
}

vunits environment::get_vertical_spacing()
{
  return vertical_spacing;
}

vunits environment::get_post_vertical_spacing()
{
  return post_vertical_spacing;
}

int environment::get_line_spacing()
{
  return line_spacing;
}

vunits environment::total_post_vertical_spacing()
{
  vunits tem(post_vertical_spacing);
  if (line_spacing > 1)
    tem += (line_spacing - 1)*vertical_spacing;
  return tem;
}

int environment::get_bold()
{
  return get_bold_fontno(fontno);
}

hunits environment::get_digit_width()
{
  return env_digit_width(this);
} 

int environment::get_adjust_mode()
{
  return adjust_mode;
}

int environment::get_fill()
{
  return fill;
}

hunits environment::get_indent()
{
  return indent;
}

hunits environment::get_saved_indent()
{
  if (line)
    return saved_indent;
  else if (have_temporary_indent)
    return temporary_indent;
  else
    return indent;
}

hunits environment::get_temporary_indent()
{
  return temporary_indent;
}

hunits environment::get_title_length()
{
  return title_length;
}

node *environment::get_prev_char()
{
  for (node *n = current_tab ? tab_contents : line; n; n = n->next) {
    node *last = n->last_char_node();
    if (last)
      return last;
  }
  return 0;
}

hunits environment::get_prev_char_width()
{
  node *last = get_prev_char();
  if (!last)
    return H0;
  return last->width();
}

hunits environment::get_prev_char_skew()
{
  node *last = get_prev_char();
  if (!last)
    return H0;
  return last->skew();
}

vunits environment::get_prev_char_height()
{
  node *last = get_prev_char();
  if (!last)
    return V0;
  vunits min, max;
  last->vertical_extent(&min, &max);
  return -min;
}

vunits environment::get_prev_char_depth()
{
  node *last = get_prev_char();
  if (!last)
    return V0;
  vunits min, max;
  last->vertical_extent(&min, &max);
  return max;
}

hunits environment::get_text_length()
{
  hunits n = line == 0 ? H0 : width_total;
  if (current_tab)
    n += tab_width;
  return n;
}

hunits environment::get_prev_text_length()
{
  return prev_text_length;
}


static int sb_reg_contents = 0;
static int st_reg_contents = 0;
static int ct_reg_contents = 0;
static int rsb_reg_contents = 0;
static int rst_reg_contents = 0;
static int skw_reg_contents = 0;
static int ssc_reg_contents = 0;

void environment::width_registers()
{
  // this is used to implement \w; it sets the st, sb, ct registers
  vunits min = 0, max = 0, cur = 0;
  int character_type = 0;
  ssc_reg_contents = line ? line->subscript_correction().to_units() : 0;
  skw_reg_contents = line ? line->skew().to_units() : 0;
  line = reverse_node_list(line);
  vunits real_min = V0;
  vunits real_max = V0;
  vunits v1, v2;
  for (node *tem = line; tem; tem = tem->next) {
    tem->vertical_extent(&v1, &v2);
    v1 += cur;
    if (v1 < real_min)
      real_min = v1;
    v2 += cur;
    if (v2 > real_max)
      real_max = v2;
    if ((cur += tem->vertical_width()) < min)
      min = cur;
    else if (cur > max)
      max = cur;
    character_type |= tem->character_type();
  }
  line = reverse_node_list(line);
  st_reg_contents = -min.to_units();
  sb_reg_contents = -max.to_units();
  rst_reg_contents = -real_min.to_units();
  rsb_reg_contents = -real_max.to_units();
  ct_reg_contents = character_type;
}

node *environment::extract_output_line()
{
  if (current_tab)
    wrap_up_tab();
  node *n = line;
  line = 0;
  return n;
}

/* environment related requests */

void environment_switch()
{
  int pop = 0;	// 1 means pop, 2 means pop but no error message on underflow
  if (curenv->is_dummy())
    error("can't switch environments when current environment is dummy");
  else if (!has_arg())
    pop = 1;
  else {
    symbol nm;
    if (!tok.delimiter()) {
      // It looks like a number.
      int n;
      if (get_integer(&n)) {
	if (n >= 0 && n < NENVIRONMENTS) {
	  env_stack = new env_list(curenv, env_stack);
	  if (env_table[n] == 0)
	    env_table[n] = new environment(i_to_a(n));
	  curenv = env_table[n];
	}
	else
	  nm = i_to_a(n);
      }
      else
	pop = 2;
    }
    else {
      nm = get_long_name(1);
      if (nm.is_null())
	pop = 2;
    }
    if (!nm.is_null()) {
      environment *e = (environment *)env_dictionary.lookup(nm);
      if (!e) {
	e = new environment(nm);
	(void)env_dictionary.lookup(nm, e);
      }
      env_stack = new env_list(curenv, env_stack);
      curenv = e;
    }
  }
  if (pop) {
    if (env_stack == 0) {
      if (pop == 1)
	error("environment stack underflow");
    }
    else {
      int seen_space = curenv->seen_space;
      int seen_eol   = curenv->seen_eol;
      int suppress_next_eol = curenv->suppress_next_eol;
      curenv = env_stack->env;
      curenv->seen_space = seen_space;
      curenv->seen_eol   = seen_eol;
      curenv->suppress_next_eol = suppress_next_eol;
      env_list *tem = env_stack;
      env_stack = env_stack->next;
      delete tem;
    }
  }
  skip_line();
}

void environment_copy()
{
  symbol nm;
  environment *e=0;
  tok.skip();
  if (!tok.delimiter()) {
    // It looks like a number.
    int n;
    if (get_integer(&n)) {
      if (n >= 0 && n < NENVIRONMENTS)
	e = env_table[n];
      else
	nm = i_to_a(n);
    }
  }
  else
    nm = get_long_name(1);
  if (!e && !nm.is_null())
    e = (environment *)env_dictionary.lookup(nm);
  if (e == 0) {
    error("No environment to copy from");
    return;
  }
  else
    curenv->copy(e);
  skip_line();
}

void fill_color_change()
{
  symbol s = get_name();
  if (s.is_null())
    curenv->set_fill_color(curenv->get_prev_fill_color());
  else
    do_fill_color(s);
  skip_line();
}

void glyph_color_change()
{
  symbol s = get_name();
  if (s.is_null())
    curenv->set_glyph_color(curenv->get_prev_glyph_color());
  else
    do_glyph_color(s);
  skip_line();
}

static symbol P_symbol("P");

void font_change()
{
  symbol s = get_name();
  int is_number = 1;
  if (s.is_null() || s == P_symbol) {
    s = P_symbol;
    is_number = 0;
  }
  else {
    for (const char *p = s.contents(); p != 0 && *p != 0; p++)
      if (!csdigit(*p)) {
	is_number = 0;
	break;
      }
  }
  if (is_number)
    curenv->set_font(atoi(s.contents()));
  else
    curenv->set_font(s);
  skip_line();
}

void family_change()
{
  symbol s = get_name();
  curenv->set_family(s);
  skip_line();
}

void point_size()
{
  int n;
  if (has_arg() && get_number(&n, 'z', curenv->get_requested_point_size())) {
    if (n <= 0)
      n = 1;
    curenv->set_size(n);
  }
  else
    curenv->set_size(0);
  skip_line();
}

void override_sizes()
{
  int n = 16;
  int *sizes = new int[n];
  int i = 0;
  char *buf = read_string();
  if (!buf)
    return;
  char *p = strtok(buf, " \t");
  for (;;) {
    if (!p)
      break;
    int lower, upper;
    switch (sscanf(p, "%d-%d", &lower, &upper)) {
    case 1:
      upper = lower;
      // fall through
    case 2:
      if (lower <= upper && lower >= 0)
	break;
      // fall through
    default:
      warning(WARN_RANGE, "bad size range `%1'", p);
      return;
    }
    if (i + 2 > n) {
      int *old_sizes = sizes;
      sizes = new int[n*2];
      memcpy(sizes, old_sizes, n*sizeof(int));
      n *= 2;
      a_delete old_sizes;
    }
    sizes[i++] = lower;
    if (lower == 0)
      break;
    sizes[i++] = upper;
    p = strtok(0, " \t");
  }
  font_size::init_size_table(sizes);
}

void space_size()
{
  int n;
  if (get_integer(&n)) {
    curenv->space_size = n;
    if (has_arg() && get_integer(&n))
      curenv->sentence_space_size = n;
    else
      curenv->sentence_space_size = curenv->space_size;
  }
  skip_line();
}

void fill()
{
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break();
  curenv->fill = 1;
  tok.next();
}

void no_fill()
{
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break();
  curenv->fill = 0;
  curenv->suppress_next_eol = 1;
  tok.next();
}

void center()
{
  int n;
  if (!has_arg() || !get_integer(&n))
    n = 1;
  else if (n < 0)
    n = 0;
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break();
  curenv->right_justify_lines = 0;
  curenv->center_lines = n;
  curdiv->modified_tag.incl(MTSM_CE);
  tok.next();
}

void right_justify()
{
  int n;
  if (!has_arg() || !get_integer(&n))
    n = 1;
  else if (n < 0)
    n = 0;
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break();
  curenv->center_lines = 0;
  curenv->right_justify_lines = n;
  curdiv->modified_tag.incl(MTSM_RJ);
  tok.next();
}

void line_length()
{
  hunits temp;
  if (has_arg() && get_hunits(&temp, 'm', curenv->line_length)) {
    if (temp < H0) {
      warning(WARN_RANGE, "bad line length %1u", temp.to_units());
      temp = H0;
    }
  }
  else
    temp = curenv->prev_line_length;
  curenv->prev_line_length = curenv->line_length;
  curenv->line_length = temp;
  curdiv->modified_tag.incl(MTSM_LL);
  skip_line();
}

void title_length()
{
  hunits temp;
  if (has_arg() && get_hunits(&temp, 'm', curenv->title_length)) {
    if (temp < H0) {
      warning(WARN_RANGE, "bad title length %1u", temp.to_units());
      temp = H0;
    }
  }
  else
    temp = curenv->prev_title_length;
  curenv->prev_title_length = curenv->title_length;
  curenv->title_length = temp;
  skip_line();
}

void vertical_spacing()
{
  vunits temp;
  if (has_arg() && get_vunits(&temp, 'p', curenv->vertical_spacing)) {
    if (temp < V0) {
      warning(WARN_RANGE, "vertical spacing must not be negative");
      temp = vresolution;
    }
  }
  else
    temp = curenv->prev_vertical_spacing;
  curenv->prev_vertical_spacing = curenv->vertical_spacing;
  curenv->vertical_spacing = temp;
  skip_line();
}

void post_vertical_spacing()
{
  vunits temp;
  if (has_arg() && get_vunits(&temp, 'p', curenv->post_vertical_spacing)) {
    if (temp < V0) {
      warning(WARN_RANGE,
	      "post vertical spacing must be greater than or equal to 0");
      temp = V0;
    }
  }
  else
    temp = curenv->prev_post_vertical_spacing;
  curenv->prev_post_vertical_spacing = curenv->post_vertical_spacing;
  curenv->post_vertical_spacing = temp;
  skip_line();
}

void line_spacing()
{
  int temp;
  if (has_arg() && get_integer(&temp)) {
    if (temp < 1) {
      warning(WARN_RANGE, "value %1 out of range: interpreted as 1", temp);
      temp = 1;
    }
  }
  else
    temp = curenv->prev_line_spacing;
  curenv->prev_line_spacing = curenv->line_spacing;
  curenv->line_spacing = temp;
  skip_line();
}

void indent()
{
  hunits temp;
  if (has_arg() && get_hunits(&temp, 'm', curenv->indent)) {
    if (temp < H0) {
      warning(WARN_RANGE, "indent cannot be negative");
      temp = H0;
    }
  }
  else
    temp = curenv->prev_indent;
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break();
  curenv->have_temporary_indent = 0;
  curenv->prev_indent = curenv->indent;
  curenv->indent = temp;
  curdiv->modified_tag.incl(MTSM_IN);
  tok.next();
}

void temporary_indent()
{
  int err = 0;
  hunits temp;
  if (!get_hunits(&temp, 'm', curenv->get_indent()))
    err = 1;
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break();
  if (temp < H0) {
    warning(WARN_RANGE, "total indent cannot be negative");
    temp = H0;
  }
  if (!err) {
    curenv->temporary_indent = temp;
    curenv->have_temporary_indent = 1;
    curdiv->modified_tag.incl(MTSM_TI);
  }
  tok.next();
}

node *do_underline_special(int underline_spaces)
{
  macro m;
  m.append_str("x u ");
  m.append(underline_spaces + '0');
  return new special_node(m, 1);
}

void do_underline(int underline_spaces)
{
  int n;
  if (!has_arg() || !get_integer(&n))
    n = 1;
  if (n <= 0) {
    if (curenv->underline_lines > 0) {
      curenv->prev_fontno = curenv->fontno;
      curenv->fontno = curenv->pre_underline_fontno;
      if (underline_spaces) {
	curenv->underline_spaces = 0;
	curenv->add_node(do_underline_special(0));
      }
    }
    curenv->underline_lines = 0;
  }
  else {
    curenv->underline_lines = n;
    curenv->pre_underline_fontno = curenv->fontno;
    curenv->fontno = get_underline_fontno();
    if (underline_spaces) {
      curenv->underline_spaces = 1;
      curenv->add_node(do_underline_special(1));
    }
  }
  skip_line();
}

void continuous_underline()
{
  do_underline(1);
}

void underline()
{
  do_underline(0);
}

void control_char()
{
  curenv->control_char = '.';
  if (has_arg()) {
    if (tok.ch() == 0)
      error("bad control character");
    else
      curenv->control_char = tok.ch();
  }
  skip_line();
}

void no_break_control_char()
{
  curenv->no_break_control_char = '\'';
  if (has_arg()) {
    if (tok.ch() == 0)
      error("bad control character");
    else
      curenv->no_break_control_char = tok.ch();
  }
  skip_line();
}

void margin_character()
{
  while (tok.space())
    tok.next();
  charinfo *ci = tok.get_char();
  if (ci) {
    // Call tok.next() only after making the node so that
    // .mc \s+9\(br\s0 works.
    node *nd = curenv->make_char_node(ci);
    tok.next();
    if (nd) {
      delete curenv->margin_character_node;
      curenv->margin_character_node = nd;
      curenv->margin_character_flags = (MARGIN_CHARACTER_ON
					|MARGIN_CHARACTER_NEXT);
      hunits d;
      if (has_arg() && get_hunits(&d, 'm'))
	curenv->margin_character_distance = d;
    }
  }
  else {
    check_missing_character();
    curenv->margin_character_flags &= ~MARGIN_CHARACTER_ON;
    if (curenv->margin_character_flags == 0) {
      delete curenv->margin_character_node;
      curenv->margin_character_node = 0;
    }
  }
  skip_line();
}

void number_lines()
{
  delete_node_list(curenv->numbering_nodes);
  curenv->numbering_nodes = 0;
  if (has_arg()) {
    node *nd = 0;
    for (int i = '9'; i >= '0'; i--) {
      node *tem = make_node(charset_table[i], curenv);
      if (!tem) {
	skip_line();
	return;
      }
      tem->next = nd;
      nd = tem;
    }
    curenv->numbering_nodes = nd;
    curenv->line_number_digit_width = env_digit_width(curenv);
    int n;
    if (!tok.delimiter()) {
      if (get_integer(&n, next_line_number)) {
	next_line_number = n;
	if (next_line_number < 0) {
	  warning(WARN_RANGE, "negative line number");
	  next_line_number = 0;
	}
      }
    }
    else
      while (!tok.space() && !tok.newline() && !tok.eof())
	tok.next();
    if (has_arg()) {
      if (!tok.delimiter()) {
	if (get_integer(&n)) {
	  if (n <= 0) {
	    warning(WARN_RANGE, "negative or zero line number multiple");
	  }
	  else
	    curenv->line_number_multiple = n;
	}
      }
      else
	while (!tok.space() && !tok.newline() && !tok.eof())
	  tok.next();
      if (has_arg()) {
	if (!tok.delimiter()) {
	  if (get_integer(&n))
	    curenv->number_text_separation = n;
	}
	else
	  while (!tok.space() && !tok.newline() && !tok.eof())
	    tok.next();
	if (has_arg() && !tok.delimiter() && get_integer(&n))
	  curenv->line_number_indent = n;
      }
    }
  }
  skip_line();
}

void no_number()
{
  int n;
  if (has_arg() && get_integer(&n))
    curenv->no_number_count = n > 0 ? n : 0;
  else
    curenv->no_number_count = 1;
  skip_line();
}

void no_hyphenate()
{
  curenv->hyphenation_flags = 0;
  skip_line();
}

void hyphenate_request()
{
  int n;
  if (has_arg() && get_integer(&n))
    curenv->hyphenation_flags = n;
  else
    curenv->hyphenation_flags = 1;
  skip_line();
}

void hyphen_char()
{
  curenv->hyphen_indicator_char = get_optional_char();
  skip_line();
}

void hyphen_line_max_request()
{
  int n;
  if (has_arg() && get_integer(&n))
    curenv->hyphen_line_max = n;
  else
    curenv->hyphen_line_max = -1;
  skip_line();
}

void environment::interrupt()
{
  if (!dummy) {
    add_node(new transparent_dummy_node);
    interrupted = 1;
  }
}

void environment::newline()
{
  int was_centered = 0;
  if (underline_lines > 0) {
    if (--underline_lines == 0) {
      prev_fontno = fontno;
      fontno = pre_underline_fontno;
      if (underline_spaces) {
        underline_spaces = 0;
        add_node(do_underline_special(0));
      }
    }
  }
  if (current_field)
    wrap_up_field();
  if (current_tab)
    wrap_up_tab();
  // strip trailing spaces
  while (line != 0 && line->discardable()) {
    width_total -= line->width();
    space_total -= line->nspaces();
    node *tem = line;
    line = line->next;
    delete tem;
  }
  node *to_be_output = 0;
  hunits to_be_output_width;
  prev_line_interrupted = 0;
  if (dummy)
    space_newline();
  else if (interrupted) {
    interrupted = 0;
    // see environment::final_break
    prev_line_interrupted = exit_started ? 2 : 1;
  }
  else if (center_lines > 0) {
    --center_lines;
    hunits x = target_text_length - width_total;
    if (x > H0)
      saved_indent += x/2;
    to_be_output = line;
    was_centered = 1;
    to_be_output_width = width_total;
    line = 0;
  }
  else if (right_justify_lines > 0) {
    --right_justify_lines;
    hunits x = target_text_length - width_total;
    if (x > H0)
      saved_indent += x;
    to_be_output = line;
    to_be_output_width = width_total;
    line = 0;
  }
  else if (fill)
    space_newline();
  else {
    to_be_output = line;
    to_be_output_width = width_total;
    line = 0;
  }
  input_line_start = line == 0 ? H0 : width_total;
  if (to_be_output) {
    if (is_html && !fill) {
      curdiv->modified_tag.incl(MTSM_EOL);
      if (suppress_next_eol)
	suppress_next_eol = 0;
      else
	seen_eol = 1;
    }

    output_line(to_be_output, to_be_output_width, was_centered);
    hyphen_line_count = 0;
  }
  if (input_trap_count > 0) {
    if (!(continued_input_trap && prev_line_interrupted))
      if (--input_trap_count == 0)
	spring_trap(input_trap);
  }
}

void environment::output_line(node *n, hunits width, int was_centered)
{
  prev_text_length = width;
  if (margin_character_flags) {
    hunits d = line_length + margin_character_distance - saved_indent - width;
    if (d > 0) {
      n = new hmotion_node(d, get_fill_color(), n);
      width += d;
    }
    margin_character_flags &= ~MARGIN_CHARACTER_NEXT;
    node *tem;
    if (!margin_character_flags) {
      tem = margin_character_node;
      margin_character_node = 0;
    }
    else
      tem = margin_character_node->copy();
    tem->next = n;
    n = tem;
    width += tem->width();
  }
  node *nn = 0;
  while (n != 0) {
    node *tem = n->next;
    n->next = nn;
    nn = n;
    n = tem;
  }
  if (!saved_indent.is_zero())
    nn = new hmotion_node(saved_indent, get_fill_color(), nn);
  width += saved_indent;
  if (no_number_count > 0)
    --no_number_count;
  else if (numbering_nodes) {
    hunits w = (line_number_digit_width
		*(3+line_number_indent+number_text_separation));
    if (next_line_number % line_number_multiple != 0)
      nn = new hmotion_node(w, get_fill_color(), nn);
    else {
      hunits x = w;
      nn = new hmotion_node(number_text_separation * line_number_digit_width,
			    get_fill_color(), nn);
      x -= number_text_separation*line_number_digit_width;
      char buf[30];
      sprintf(buf, "%3d", next_line_number);
      for (char *p = strchr(buf, '\0') - 1; p >= buf && *p != ' '; --p) {
	node *gn = numbering_nodes;
	for (int count = *p - '0'; count > 0; count--)
	  gn = gn->next;
	gn = gn->copy();
	x -= gn->width();
	gn->next = nn;
	nn = gn;
      }
      nn = new hmotion_node(x, get_fill_color(), nn);
    }
    width += w;
    ++next_line_number;
  }
  output(nn, !fill, vertical_spacing, total_post_vertical_spacing(), width,
	 was_centered);
}

void environment::start_line()
{
  assert(line == 0);
  discarding = 0;
  line = new line_start_node;
  if (have_temporary_indent) {
    saved_indent = temporary_indent;
    have_temporary_indent = 0;
  }
  else
    saved_indent = indent;
  target_text_length = line_length - saved_indent;
  width_total = H0;
  space_total = 0;
}

hunits environment::get_hyphenation_space()
{
  return hyphenation_space;
}

void hyphenation_space_request()
{
  hunits n;
  if (get_hunits(&n, 'm')) {
    if (n < H0) {
      warning(WARN_RANGE, "hyphenation space cannot be negative");
      n = H0;
    }
    curenv->hyphenation_space = n;
  }
  skip_line();
}

hunits environment::get_hyphenation_margin()
{
  return hyphenation_margin;
}

void hyphenation_margin_request()
{
  hunits n;
  if (get_hunits(&n, 'm')) {
    if (n < H0) {
      warning(WARN_RANGE, "hyphenation margin cannot be negative");
      n = H0;
    }
    curenv->hyphenation_margin = n;
  }
  skip_line();
}

breakpoint *environment::choose_breakpoint()
{
  hunits x = width_total;
  int s = space_total;
  node *n = line;
  breakpoint *best_bp = 0;	// the best breakpoint so far
  int best_bp_fits = 0;
  while (n != 0) {
    x -= n->width();
    s -= n->nspaces();
    breakpoint *bp = n->get_breakpoints(x, s);
    while (bp != 0) {
      if (bp->width <= target_text_length) {
	if (!bp->hyphenated) {
	  breakpoint *tem = bp->next;
	  bp->next = 0;
	  while (tem != 0) {
	    breakpoint *tem1 = tem;
	    tem = tem->next;
	    delete tem1;
	  }
	  if (best_bp_fits
	      // Decide whether to use the hyphenated breakpoint.
	      && (hyphen_line_max < 0
		  // Only choose the hyphenated breakpoint if it would not
		  // exceed the maximum number of consecutive hyphenated
		  // lines.
		  || hyphen_line_count + 1 <= hyphen_line_max)
	      && !(adjust_mode == ADJUST_BOTH
		   // Don't choose the hyphenated breakpoint if the line
		   // can be justified by adding no more than
		   // hyphenation_space to any word space.
		   ? (bp->nspaces > 0
		      && (((target_text_length - bp->width
			    + (bp->nspaces - 1)*hresolution)/bp->nspaces)
			  <= hyphenation_space))
		   // Don't choose the hyphenated breakpoint if the line
		   // is no more than hyphenation_margin short.
		   : target_text_length - bp->width <= hyphenation_margin)) {
	    delete bp;
	    return best_bp;
	  }
	  if (best_bp)
	    delete best_bp;
	  return bp;
	}
	else {
	  if ((adjust_mode == ADJUST_BOTH
	       ? hyphenation_space == H0
	       : hyphenation_margin == H0)
	      && (hyphen_line_max < 0
		  || hyphen_line_count + 1 <= hyphen_line_max)) {
	    // No need to consider a non-hyphenated breakpoint.
	    if (best_bp)
	      delete best_bp;
	    breakpoint *tem = bp->next;
	    bp->next = 0;
	    while (tem != 0) {
	      breakpoint *tem1 = tem;
	      tem = tem->next;
	      delete tem1;
	    }
	    return bp;
	  }
	  // It fits but it's hyphenated.
	  if (!best_bp_fits) {
	    if (best_bp)
	      delete best_bp;
	    best_bp = bp;
	    bp = bp->next;
	    best_bp_fits = 1;
	  }
	  else {
	    breakpoint *tem = bp;
	    bp = bp->next;
	    delete tem;
	  }
	}
      }
      else {
	if (best_bp)
	  delete best_bp;
	best_bp = bp;
	bp = bp->next;
      }
    }
    n = n->next;
  }
  if (best_bp) {
    if (!best_bp_fits)
      output_warning(WARN_BREAK, "can't break line");
    return best_bp;
  }
  return 0;
}

void environment::hyphenate_line(int start_here)
{
  assert(line != 0);
  hyphenation_type prev_type = line->get_hyphenation_type();
  node **startp;
  if (start_here)
    startp = &line;
  else
    for (startp = &line->next; *startp != 0; startp = &(*startp)->next) {
      hyphenation_type this_type = (*startp)->get_hyphenation_type();
      if (prev_type == HYPHEN_BOUNDARY && this_type == HYPHEN_MIDDLE)
	break;
      prev_type = this_type;
    }
  if (*startp == 0)
    return;
  node *tem = *startp;
  do {
    tem = tem->next;
  } while (tem != 0 && tem->get_hyphenation_type() == HYPHEN_MIDDLE);
  int inhibit = (tem != 0 && tem->get_hyphenation_type() == HYPHEN_INHIBIT);
  node *end = tem;
  hyphen_list *sl = 0;
  tem = *startp;
  node *forward = 0;
  int i = 0;
  while (tem != end) {
    sl = tem->get_hyphen_list(sl, &i);
    node *tem1 = tem;
    tem = tem->next;
    tem1->next = forward;
    forward = tem1;
  }
  if (!inhibit) {
    // this is for characters like hyphen and emdash
    int prev_code = 0;
    for (hyphen_list *h = sl; h; h = h->next) {
      h->breakable = (prev_code != 0
		      && h->next != 0
		      && h->next->hyphenation_code != 0);
      prev_code = h->hyphenation_code;
    }
  }
  if (hyphenation_flags != 0
      && !inhibit
      // this may not be right if we have extra space on this line
      && !((hyphenation_flags & HYPHEN_LAST_LINE)
	   && (curdiv->distance_to_next_trap()
	       <= vertical_spacing + total_post_vertical_spacing()))
      && i >= 4)
    hyphenate(sl, hyphenation_flags);
  while (forward != 0) {
    node *tem1 = forward;
    forward = forward->next;
    tem1->next = 0;
    tem = tem1->add_self(tem, &sl);
  }
  *startp = tem;
}

static node *node_list_reverse(node *n)
{
  node *res = 0;
  while (n) {
    node *tem = n;
    n = n->next;
    tem->next = res;
    res = tem;
  }
  return res;
}

static void distribute_space(node *n, int nspaces, hunits desired_space,
			     int force_reverse = 0)
{
  static int reverse = 0;
  if (force_reverse || reverse)
    n = node_list_reverse(n);
  if (!force_reverse && nspaces > 0 && spread_limit >= 0
      && desired_space.to_units() > 0) {
    hunits em = curenv->get_size();
    double Ems = (double)desired_space.to_units() / nspaces
		 / (em.is_zero() ? hresolution : em.to_units());
    if (Ems > spread_limit)
      output_warning(WARN_BREAK, "spreading %1m per space", Ems);
  }
  for (node *tem = n; tem; tem = tem->next)
    tem->spread_space(&nspaces, &desired_space);
  if (force_reverse || reverse)
    (void)node_list_reverse(n);
  if (!force_reverse)
    reverse = !reverse;
  assert(desired_space.is_zero() && nspaces == 0);
}

void environment::possibly_break_line(int start_here, int forced)
{
  int was_centered = center_lines > 0;
  if (!fill || current_tab || current_field || dummy)
    return;
  while (line != 0
	 && (forced
	     // When a macro follows a paragraph in fill mode, the
	     // current line should not be empty.
	     || (width_total - line->width()) > target_text_length)) {
    hyphenate_line(start_here);
    breakpoint *bp = choose_breakpoint();
    if (bp == 0)
      // we'll find one eventually
      return;
    node *pre, *post;
    node **ndp = &line;
    while (*ndp != bp->nd)
      ndp = &(*ndp)->next;
    bp->nd->split(bp->index, &pre, &post);
    *ndp = post;
    hunits extra_space_width = H0;
    switch(adjust_mode) {
    case ADJUST_BOTH:
      if (bp->nspaces != 0)
	extra_space_width = target_text_length - bp->width;
      else if (bp->width > 0 && target_text_length > 0
	       && target_text_length > bp->width)
	output_warning(WARN_BREAK, "cannot adjust line");
      break;
    case ADJUST_CENTER:
      saved_indent += (target_text_length - bp->width)/2;
      was_centered = 1;
      break;
    case ADJUST_RIGHT:
      saved_indent += target_text_length - bp->width;
      break;
    }
    distribute_space(pre, bp->nspaces, extra_space_width);
    hunits output_width = bp->width + extra_space_width;
    input_line_start -= output_width;
    if (bp->hyphenated)
      hyphen_line_count++;
    else
      hyphen_line_count = 0;
    delete bp;
    space_total = 0;
    width_total = 0;
    node *first_non_discardable = 0;
    node *tem;
    for (tem = line; tem != 0; tem = tem->next)
      if (!tem->discardable())
	first_non_discardable = tem;
    node *to_be_discarded;
    if (first_non_discardable) {
      to_be_discarded = first_non_discardable->next;
      first_non_discardable->next = 0;
      for (tem = line; tem != 0; tem = tem->next) {
	width_total += tem->width();
	space_total += tem->nspaces();
      }
      discarding = 0;
    }
    else {
      discarding = 1;
      to_be_discarded = line;
      line = 0;
    }
    // Do output_line() here so that line will be 0 iff the
    // the environment will be empty.
    output_line(pre, output_width, was_centered);
    while (to_be_discarded != 0) {
      tem = to_be_discarded;
      to_be_discarded = to_be_discarded->next;
      input_line_start -= tem->width();
      delete tem;
    }
    if (line != 0) {
      if (have_temporary_indent) {
	saved_indent = temporary_indent;
	have_temporary_indent = 0;
      }
      else
	saved_indent = indent;
      target_text_length = line_length - saved_indent;
    }
  }
}

/*
Do the break at the end of input after the end macro (if any).

Unix troff behaves as follows:  if the last line is

foo bar\c

it will output foo on the current page, and bar on the next page;
if the last line is

foo\c

or

foo bar

everything will be output on the current page.  This behaviour must be
considered a bug.

The problem is that some macro packages rely on this.  For example,
the ATK macros have an end macro that emits \c if it needs to print a
table of contents but doesn't do a 'bp in the end macro; instead the
'bp is done in the bottom of page trap.  This works with Unix troff,
provided that the current environment is not empty at the end of the
input file.

The following will make macro packages that do that sort of thing work
even if the current environment is empty at the end of the input file.
If the last input line used \c and this line occurred in the end macro,
then we'll force everything out on the current page, but we'll make
sure that the environment isn't empty so that we won't exit at the
bottom of this page.
*/

void environment::final_break()
{
  if (prev_line_interrupted == 2) {
    do_break();
    add_node(new transparent_dummy_node);
  }
  else
    do_break();
}

node *environment::make_tag(const char *nm, int i)
{
  if (is_html) {
    /*
     * need to emit tag for post-grohtml
     * but we check to see whether we can emit specials
     */
    if (curdiv == topdiv && topdiv->before_first_page)
      topdiv->begin_page();
    macro *m = new macro;
    m->append_str("devtag:");
    for (const char *p = nm; *p; p++)
      if (!invalid_input_char((unsigned char)*p))
	m->append(*p);
    m->append(' ');
    m->append_int(i);
    return new special_node(*m);
  }
  return 0;
}

void environment::dump_troff_state()
{
#define SPACES "                                            "
  fprintf(stderr, SPACES "register `in' = %d\n", curenv->indent.to_units());
  if (curenv->have_temporary_indent)
    fprintf(stderr, SPACES "register `ti' = %d\n",
	    curenv->temporary_indent.to_units());
  fprintf(stderr, SPACES "centered lines `ce' = %d\n", curenv->center_lines);
  fprintf(stderr, SPACES "register `ll' = %d\n",
	  curenv->line_length.to_units());
  fprintf(stderr, SPACES "fill `fi=1/nf=0' = %d\n", curenv->fill);
  fprintf(stderr, SPACES "page offset `po' = %d\n",
	  topdiv->get_page_offset().to_units());
  fprintf(stderr, SPACES "seen_break = %d\n", curenv->seen_break);
  fprintf(stderr, SPACES "seen_space = %d\n", curenv->seen_space);
  fflush(stderr);
#undef SPACES
}

statem *environment::construct_state(int only_eol)
{
  if (is_html) {
    statem *s = new statem();
    if (!only_eol) {
      s->add_tag(MTSM_IN, indent);
      s->add_tag(MTSM_LL, line_length);
      s->add_tag(MTSM_PO, topdiv->get_page_offset().to_units());
      s->add_tag(MTSM_RJ, right_justify_lines);
      if (have_temporary_indent)
	s->add_tag(MTSM_TI, temporary_indent);
      s->add_tag_ta();
      if (seen_break)
	s->add_tag(MTSM_BR);
      if (seen_space != 0)
	s->add_tag(MTSM_SP, seen_space);
      seen_break = 0;
      seen_space = 0;
    }
    if (seen_eol) {
      s->add_tag(MTSM_EOL);
      s->add_tag(MTSM_CE, center_lines);
    }
    seen_eol = 0;
    return s;
  }
  else
    return NULL;
}

void environment::construct_format_state(node *n, int was_centered,
					 int filling)
{
  if (is_html) {
    // find first glyph node which has a state.
    while (n != 0 && n->state == 0)
      n = n->next;
    if (n == 0 || (n->state == 0))
      return;
    if (seen_space != 0)
      n->state->add_tag(MTSM_SP, seen_space);
    if (seen_eol && topdiv == curdiv)
      n->state->add_tag(MTSM_EOL);
    seen_space = 0;
    seen_eol = 0;
    if (was_centered)
      n->state->add_tag(MTSM_CE, center_lines+1);
    else
      n->state->add_tag_if_unknown(MTSM_CE, 0);
    n->state->add_tag_if_unknown(MTSM_FI, filling);
    n = n->next;
    while (n != 0) {
      if (n->state != 0) {
	n->state->sub_tag_ce();
	n->state->add_tag_if_unknown(MTSM_FI, filling);
      }
      n = n->next;
    }
  }
}

void environment::construct_new_line_state(node *n)
{
  if (is_html) {
    // find first glyph node which has a state.
    while (n != 0 && n->state == 0)
      n = n->next;
    if (n == 0 || n->state == 0)
      return;
    if (seen_space != 0)
      n->state->add_tag(MTSM_SP, seen_space);
    if (seen_eol && topdiv == curdiv)
      n->state->add_tag(MTSM_EOL);
    seen_space = 0;
    seen_eol = 0;
  }
}

extern int global_diverted_space;

void environment::do_break(int do_spread)
{
  int was_centered = 0;
  if (curdiv == topdiv && topdiv->before_first_page) {
    topdiv->begin_page();
    return;
  }
  if (current_tab)
    wrap_up_tab();
  if (line) {
    // this is so that hyphenation works
    line = new space_node(H0, get_fill_color(), line);
    space_total++;
    possibly_break_line(0, do_spread);
  }
  while (line != 0 && line->discardable()) {
    width_total -= line->width();
    space_total -= line->nspaces();
    node *tem = line;
    line = line->next;
    delete tem;
  }
  discarding = 0;
  input_line_start = H0;
  if (line != 0) {
    if (fill) {
      switch (adjust_mode) {
      case ADJUST_CENTER:
	saved_indent += (target_text_length - width_total)/2;
	was_centered = 1;
	break;
      case ADJUST_RIGHT:
	saved_indent += target_text_length - width_total;
	break;
      }
    }
    node *tem = line;
    line = 0;
    output_line(tem, width_total, was_centered);
    hyphen_line_count = 0;
  }
  prev_line_interrupted = 0;
#ifdef WIDOW_CONTROL
  mark_last_line();
  output_pending_lines();
#endif /* WIDOW_CONTROL */
  if (!global_diverted_space) {
    curdiv->modified_tag.incl(MTSM_BR);
    seen_break = 1;
  }
}

int environment::is_empty()
{
  return !current_tab && line == 0 && pending_lines == 0;
}

void do_break_request(int spread)
{
  while (!tok.newline() && !tok.eof())
    tok.next();
  if (break_flag)
    curenv->do_break(spread);
  tok.next();
}

void break_request()
{
  do_break_request(0);
}

void break_spread_request()
{
  do_break_request(1);
}

void title()
{
  if (curdiv == topdiv && topdiv->before_first_page) {
    handle_initial_title();
    return;
  }
  node *part[3];
  hunits part_width[3];
  part[0] = part[1] = part[2] = 0;
  environment env(curenv);
  environment *oldenv = curenv;
  curenv = &env;
  read_title_parts(part, part_width);
  curenv = oldenv;
  curenv->size = env.size;
  curenv->prev_size = env.prev_size;
  curenv->requested_size = env.requested_size;
  curenv->prev_requested_size = env.prev_requested_size;
  curenv->char_height = env.char_height;
  curenv->char_slant = env.char_slant;
  curenv->fontno = env.fontno;
  curenv->prev_fontno = env.prev_fontno;
  curenv->glyph_color = env.glyph_color;
  curenv->prev_glyph_color = env.prev_glyph_color;
  curenv->fill_color = env.fill_color;
  curenv->prev_fill_color = env.prev_fill_color;
  node *n = 0;
  node *p = part[2];
  while (p != 0) {
    node *tem = p;
    p = p->next;
    tem->next = n;
    n = tem;
  }
  hunits length_title(curenv->title_length);
  hunits f = length_title - part_width[1];
  hunits f2 = f/2;
  n = new hmotion_node(f2 - part_width[2], curenv->get_fill_color(), n);
  p = part[1];
  while (p != 0) {
    node *tem = p;
    p = p->next;
    tem->next = n;
    n = tem;
  }
  n = new hmotion_node(f - f2 - part_width[0], curenv->get_fill_color(), n);
  p = part[0];
  while (p != 0) {
    node *tem = p;
    p = p->next;
    tem->next = n;
    n = tem;
  }
  curenv->output_title(n, !curenv->fill, curenv->vertical_spacing,
		       curenv->total_post_vertical_spacing(), length_title);
  curenv->hyphen_line_count = 0;
  tok.next();
}  

void adjust()
{
  curenv->adjust_mode |= 1;
  if (has_arg()) {
    switch (tok.ch()) {
    case 'l':
      curenv->adjust_mode = ADJUST_LEFT;
      break;
    case 'r':
      curenv->adjust_mode = ADJUST_RIGHT;
      break;
    case 'c':
      curenv->adjust_mode = ADJUST_CENTER;
      break;
    case 'b':
    case 'n':
      curenv->adjust_mode = ADJUST_BOTH;
      break;
    default:
      int n;
      if (get_integer(&n)) {
	if (n < 0)
	  warning(WARN_RANGE, "negative adjustment mode");
	else if (n > 5) {
	  curenv->adjust_mode = 5;
	  warning(WARN_RANGE, "adjustment mode `%1' out of range", n);
	}
	else
	  curenv->adjust_mode = n;
      }
    }
  }
  skip_line();
}

void no_adjust()
{
  curenv->adjust_mode &= ~1;
  skip_line();
}

void do_input_trap(int continued)
{
  curenv->input_trap_count = 0;
  if (continued)
    curenv->continued_input_trap = 1;
  int n;
  if (has_arg() && get_integer(&n)) {
    if (n <= 0)
      warning(WARN_RANGE,
	      "number of lines for input trap must be greater than zero");
    else {
      symbol s = get_name(1);
      if (!s.is_null()) {
	curenv->input_trap_count = n;
	curenv->input_trap = s;
      }
    }
  }
  skip_line();
}

void input_trap()
{
  do_input_trap(0);
}

void input_trap_continued()
{
  do_input_trap(1);
}

/* tabs */

// must not be R or C or L or a legitimate part of a number expression
const char TAB_REPEAT_CHAR = 'T';

struct tab {
  tab *next;
  hunits pos;
  tab_type type;
  tab(hunits, tab_type);
  enum { BLOCK = 1024 };
  static tab *free_list;
  void *operator new(size_t);
  void operator delete(void *);
};

tab *tab::free_list = 0;

void *tab::operator new(size_t n)
{
  assert(n == sizeof(tab));
  if (!free_list) {
    free_list = (tab *)new char[sizeof(tab)*BLOCK];
    for (int i = 0; i < BLOCK - 1; i++)
      free_list[i].next = free_list + i + 1;
    free_list[BLOCK-1].next = 0;
  }
  tab *p = free_list;
  free_list = (tab *)(free_list->next);
  p->next = 0;
  return p;
}

#ifdef __GNUG__
/* cfront can't cope with this. */
inline
#endif
void tab::operator delete(void *p)
{
  if (p) {
    ((tab *)p)->next = free_list;
    free_list = (tab *)p;
  }
}

tab::tab(hunits x, tab_type t) : next(0), pos(x), type(t)
{
}

tab_stops::tab_stops(hunits distance, tab_type type) 
: initial_list(0)
{
  repeated_list = new tab(distance, type);
}

tab_stops::~tab_stops()
{
  clear();
}

tab_type tab_stops::distance_to_next_tab(hunits curpos, hunits *distance)
{
  hunits nextpos;

  return distance_to_next_tab(curpos, distance, &nextpos);
}

tab_type tab_stops::distance_to_next_tab(hunits curpos, hunits *distance,
					 hunits *nextpos)
{
  hunits lastpos = 0;
  tab *tem;
  for (tem = initial_list; tem && tem->pos <= curpos; tem = tem->next)
    lastpos = tem->pos;
  if (tem) {
    *distance = tem->pos - curpos;
    *nextpos  = tem->pos;
    return tem->type;
  }
  if (repeated_list == 0)
    return TAB_NONE;
  hunits base = lastpos;
  for (;;) {
    for (tem = repeated_list; tem && tem->pos + base <= curpos; tem = tem->next)
      lastpos = tem->pos;
    if (tem) {
      *distance = tem->pos + base - curpos;
      *nextpos  = tem->pos + base;
      return tem->type;
    }
    assert(lastpos > 0);
    base += lastpos;
  }
  return TAB_NONE;
}

const char *tab_stops::to_string()
{
  static char *buf = 0;
  static int buf_size = 0;
  // figure out a maximum on the amount of space we can need
  int count = 0;
  tab *p;
  for (p = initial_list; p; p = p->next)
    ++count;
  for (p = repeated_list; p; p = p->next)
    ++count;
  // (10 for digits + 1 for u + 1 for 'C' or 'R') + 2 for ' &' + 1 for '\0'
  int need = count*12 + 3;
  if (buf == 0 || need > buf_size) {
    if (buf)
      a_delete buf;
    buf_size = need;
    buf = new char[buf_size];
  }
  char *ptr = buf;
  for (p = initial_list; p; p = p->next) {
    strcpy(ptr, i_to_a(p->pos.to_units()));
    ptr = strchr(ptr, '\0');
    *ptr++ = 'u';
    *ptr = '\0';
    switch (p->type) {
    case TAB_LEFT:
      break;
    case TAB_RIGHT:
      *ptr++ = 'R';
      break;
    case TAB_CENTER:
      *ptr++ = 'C';
      break;
    case TAB_NONE:
    default:
      assert(0);
    }
  }
  if (repeated_list)
    *ptr++ = TAB_REPEAT_CHAR;
  for (p = repeated_list; p; p = p->next) {
    strcpy(ptr, i_to_a(p->pos.to_units()));
    ptr = strchr(ptr, '\0');
    *ptr++ = 'u';
    *ptr = '\0';
    switch (p->type) {
    case TAB_LEFT:
      break;
    case TAB_RIGHT:
      *ptr++ = 'R';
      break;
    case TAB_CENTER:
      *ptr++ = 'C';
      break;
    case TAB_NONE:
    default:
      assert(0);
    }
  }
  *ptr++ = '\0';
  return buf;
}

tab_stops::tab_stops() : initial_list(0), repeated_list(0)
{
}

tab_stops::tab_stops(const tab_stops &ts) 
: initial_list(0), repeated_list(0)
{
  tab **p = &initial_list;
  tab *t = ts.initial_list;
  while (t) {
    *p = new tab(t->pos, t->type);
    t = t->next;
    p = &(*p)->next;
  }
  p = &repeated_list;
  t = ts.repeated_list;
  while (t) {
    *p = new tab(t->pos, t->type);
    t = t->next;
    p = &(*p)->next;
  }
}

void tab_stops::clear()
{
  while (initial_list) {
    tab *tem = initial_list;
    initial_list = initial_list->next;
    delete tem;
  }
  while (repeated_list) {
    tab *tem = repeated_list;
    repeated_list = repeated_list->next;
    delete tem;
  }
}

void tab_stops::add_tab(hunits pos, tab_type type, int repeated)
{
  tab **p;
  for (p = repeated ? &repeated_list : &initial_list; *p; p = &(*p)->next)
    ;
  *p = new tab(pos, type);
}


void tab_stops::operator=(const tab_stops &ts)
{
  clear();
  tab **p = &initial_list;
  tab *t = ts.initial_list;
  while (t) {
    *p = new tab(t->pos, t->type);
    t = t->next;
    p = &(*p)->next;
  }
  p = &repeated_list;
  t = ts.repeated_list;
  while (t) {
    *p = new tab(t->pos, t->type);
    t = t->next;
    p = &(*p)->next;
  }
}
    
void set_tabs()
{
  hunits pos;
  hunits prev_pos = 0;
  int first = 1;
  int repeated = 0;
  tab_stops tabs;
  while (has_arg()) {
    if (tok.ch() == TAB_REPEAT_CHAR) {
      tok.next();
      repeated = 1;
      prev_pos = 0;
    }
    if (!get_hunits(&pos, 'm', prev_pos))
      break;
    tab_type type = TAB_LEFT;
    if (tok.ch() == 'C') {
      tok.next();
      type = TAB_CENTER;
    }
    else if (tok.ch() == 'R') {
      tok.next();
      type = TAB_RIGHT;
    }
    else if (tok.ch() == 'L') {
      tok.next();
    }
    if (pos <= prev_pos && !first)
      warning(WARN_RANGE,
	      "positions of tab stops must be strictly increasing");
    else {
      tabs.add_tab(pos, type, repeated);
      prev_pos = pos;
      first = 0;
    }
  }
  curenv->tabs = tabs;
  curdiv->modified_tag.incl(MTSM_TA);
  skip_line();
}

const char *environment::get_tabs()
{
  return tabs.to_string();
}

tab_type environment::distance_to_next_tab(hunits *distance)
{
  return line_tabs
    ? curenv->tabs.distance_to_next_tab(get_text_length(), distance)
    : curenv->tabs.distance_to_next_tab(get_input_line_position(), distance);
}

tab_type environment::distance_to_next_tab(hunits *distance, hunits *leftpos)
{
  return line_tabs
    ? curenv->tabs.distance_to_next_tab(get_text_length(), distance, leftpos)
    : curenv->tabs.distance_to_next_tab(get_input_line_position(), distance,
					leftpos);
}

void field_characters()
{
  field_delimiter_char = get_optional_char();
  if (field_delimiter_char)
    padding_indicator_char = get_optional_char();
  else
    padding_indicator_char = 0;
  skip_line();
}

void line_tabs_request()
{
  int n;
  if (has_arg() && get_integer(&n))
    curenv->line_tabs = n != 0;
  else
    curenv->line_tabs = 1;
  skip_line();
}

int environment::get_line_tabs()
{
  return line_tabs;
}

void environment::wrap_up_tab()
{
  if (!current_tab)
    return;
  if (line == 0)
    start_line();
  hunits tab_amount;
  switch (current_tab) {
  case TAB_RIGHT:
    tab_amount = tab_distance - tab_width;
    line = make_tab_node(tab_amount, line);
    break;
  case TAB_CENTER:
    tab_amount = tab_distance - tab_width/2;
    line = make_tab_node(tab_amount, line);
    break;
  case TAB_NONE:
  case TAB_LEFT:
  default:
    assert(0);
  }
  width_total += tab_amount;
  width_total += tab_width;
  if (current_field) {
    if (tab_precedes_field) {
      pre_field_width += tab_amount;
      tab_precedes_field = 0;
    }
    field_distance -= tab_amount;
    field_spaces += tab_field_spaces;
  }
  if (tab_contents != 0) {
    node *tem;
    for (tem = tab_contents; tem->next != 0; tem = tem->next)
      ;
    tem->next = line;
    line = tab_contents;
  }
  tab_field_spaces = 0;
  tab_contents = 0;
  tab_width = H0;
  tab_distance = H0;
  current_tab = TAB_NONE;
}

node *environment::make_tab_node(hunits d, node *next)
{
  if (leader_node != 0 && d < 0) {
    error("motion generated by leader cannot be negative");
    delete leader_node;
    leader_node = 0;
  }
  if (!leader_node)
    return new hmotion_node(d, 1, 0, get_fill_color(), next);
  node *n = new hline_node(d, leader_node, next);
  leader_node = 0;
  return n;
}

void environment::handle_tab(int is_leader)
{
  hunits d;
  hunits absolute;
  if (current_tab)
    wrap_up_tab();
  charinfo *ci = is_leader ? leader_char : tab_char;
  delete leader_node;
  leader_node = ci ? make_char_node(ci) : 0;
  tab_type t = distance_to_next_tab(&d, &absolute);
  switch (t) {
  case TAB_NONE:
    return;
  case TAB_LEFT:
    add_node(make_tag("tab L", absolute.to_units()));
    add_node(make_tab_node(d));
    return;
  case TAB_RIGHT:
    add_node(make_tag("tab R", absolute.to_units()));
    break;
  case TAB_CENTER:
    add_node(make_tag("tab C", absolute.to_units()));
    break;
  default:
    assert(0);
  }
  tab_width = 0;
  tab_distance = d;
  tab_contents = 0;
  current_tab = t;
  tab_field_spaces = 0;
}

void environment::start_field()
{
  assert(!current_field);
  hunits d;
  if (distance_to_next_tab(&d) != TAB_NONE) {
    pre_field_width = get_text_length();
    field_distance = d;
    current_field = 1;
    field_spaces = 0;
    tab_field_spaces = 0;
    for (node *p = line; p; p = p->next)
      if (p->nspaces()) {
	p->freeze_space();
	space_total--;
      }
    tab_precedes_field = current_tab != TAB_NONE;
  }
  else
    error("zero field width");
}

void environment::wrap_up_field()
{
  if (!current_tab && field_spaces == 0)
    add_padding();
  hunits padding = field_distance - (get_text_length() - pre_field_width);
  if (current_tab && tab_field_spaces != 0) {
    hunits tab_padding = scale(padding, 
			       tab_field_spaces, 
			       field_spaces + tab_field_spaces);
    padding -= tab_padding;
    distribute_space(tab_contents, tab_field_spaces, tab_padding, 1);
    tab_field_spaces = 0;
    tab_width += tab_padding;
  }
  if (field_spaces != 0) {
    distribute_space(line, field_spaces, padding, 1);
    width_total += padding;
    if (current_tab) {
      // the start of the tab has been moved to the right by padding, so
      tab_distance -= padding;
      if (tab_distance <= H0) {
	// use the next tab stop instead
	current_tab = tabs.distance_to_next_tab(get_input_line_position()
						- tab_width,
						&tab_distance);
	if (current_tab == TAB_NONE || current_tab == TAB_LEFT) {
	  width_total += tab_width;
	  if (current_tab == TAB_LEFT) {
	    line = make_tab_node(tab_distance, line);
	    width_total += tab_distance;
	    current_tab = TAB_NONE;
	  }
	  if (tab_contents != 0) {
	    node *tem;
	    for (tem = tab_contents; tem->next != 0; tem = tem->next)
	      ;
	    tem->next = line;
	    line = tab_contents;
	    tab_contents = 0;
	  }
	  tab_width = H0;
	  tab_distance = H0;
	}
      }
    }
  }
  current_field = 0;
}

void environment::add_padding()
{
  if (current_tab) {
    tab_contents = new space_node(H0, get_fill_color(), tab_contents);
    tab_field_spaces++;
  }
  else {
    if (line == 0)
      start_line();
    line = new space_node(H0, get_fill_color(), line);
    field_spaces++;
  }
}

typedef int (environment::*INT_FUNCP)();
typedef vunits (environment::*VUNITS_FUNCP)();
typedef hunits (environment::*HUNITS_FUNCP)();
typedef const char *(environment::*STRING_FUNCP)();

class int_env_reg : public reg {
  INT_FUNCP func;
 public:
  int_env_reg(INT_FUNCP);
  const char *get_string();
  int get_value(units *val);
};

class vunits_env_reg : public reg {
  VUNITS_FUNCP func;
 public:
  vunits_env_reg(VUNITS_FUNCP f);
  const char *get_string();
  int get_value(units *val);
};


class hunits_env_reg : public reg {
  HUNITS_FUNCP func;
 public:
  hunits_env_reg(HUNITS_FUNCP f);
  const char *get_string();
  int get_value(units *val);
};

class string_env_reg : public reg {
  STRING_FUNCP func;
public:
  string_env_reg(STRING_FUNCP);
  const char *get_string();
};

int_env_reg::int_env_reg(INT_FUNCP f) : func(f)
{
}

int int_env_reg::get_value(units *val)
{
  *val = (curenv->*func)();
  return 1;
}

const char *int_env_reg::get_string()
{
  return i_to_a((curenv->*func)());
}
 
vunits_env_reg::vunits_env_reg(VUNITS_FUNCP f) : func(f)
{
}

int vunits_env_reg::get_value(units *val)
{
  *val = (curenv->*func)().to_units();
  return 1;
}

const char *vunits_env_reg::get_string()
{
  return i_to_a((curenv->*func)().to_units());
}

hunits_env_reg::hunits_env_reg(HUNITS_FUNCP f) : func(f)
{
}

int hunits_env_reg::get_value(units *val)
{
  *val = (curenv->*func)().to_units();
  return 1;
}

const char *hunits_env_reg::get_string()
{
  return i_to_a((curenv->*func)().to_units());
}

string_env_reg::string_env_reg(STRING_FUNCP f) : func(f)
{
}

const char *string_env_reg::get_string()
{
  return (curenv->*func)();
}

class horizontal_place_reg : public general_reg {
public:
  horizontal_place_reg();
  int get_value(units *);
  void set_value(units);
};

horizontal_place_reg::horizontal_place_reg()
{
}

int horizontal_place_reg::get_value(units *res)
{
  *res = curenv->get_input_line_position().to_units();
  return 1;
}

void horizontal_place_reg::set_value(units n)
{
  curenv->set_input_line_position(hunits(n));
}

const char *environment::get_font_family_string()
{
  return family->nm.contents();
}

const char *environment::get_glyph_color_string()
{
  return glyph_color->nm.contents();
}

const char *environment::get_fill_color_string()
{
  return fill_color->nm.contents();
}

const char *environment::get_font_name_string()
{
  symbol f = get_font_name(fontno, this);
  return f.contents();
}

const char *environment::get_style_name_string()
{
  symbol f = get_style_name(fontno);
  return f.contents();
}

const char *environment::get_name_string()
{
  return name.contents();
}

// Convert a quantity in scaled points to ascii decimal fraction.

const char *sptoa(int sp)
{
  assert(sp > 0);
  assert(sizescale > 0);
  if (sizescale == 1)
    return i_to_a(sp);
  if (sp % sizescale == 0)
    return i_to_a(sp/sizescale);
  // See if 1/sizescale is exactly representable as a decimal fraction,
  // ie its only prime factors are 2 and 5.
  int n = sizescale;
  int power2 = 0;
  while ((n & 1) == 0) {
    n >>= 1;
    power2++;
  }
  int power5 = 0;
  while ((n % 5) == 0) {
    n /= 5;
    power5++;
  }
  if (n == 1) {
    int decimal_point = power5 > power2 ? power5 : power2;
    if (decimal_point <= 10) {
      int factor = 1;
      int t;
      for (t = decimal_point - power2; --t >= 0;)
	factor *= 2;
      for (t = decimal_point - power5; --t >= 0;)
	factor *= 5;
      if (factor == 1 || sp <= INT_MAX/factor)
	return if_to_a(sp*factor, decimal_point);
    }
  }
  double s = double(sp)/double(sizescale);
  double factor = 10.0;
  double val = s;
  int decimal_point = 0;
  do  {
    double v = ceil(s*factor);
    if (v > INT_MAX)
      break;
    val = v;
    factor *= 10.0;
  } while (++decimal_point < 10);
  return if_to_a(int(val), decimal_point);
}

const char *environment::get_point_size_string()
{
  return sptoa(curenv->get_point_size());
}

const char *environment::get_requested_point_size_string()
{
  return sptoa(curenv->get_requested_point_size());
}

#define init_int_env_reg(name, func) \
  number_reg_dictionary.define(name, new int_env_reg(&environment::func))

#define init_vunits_env_reg(name, func) \
  number_reg_dictionary.define(name, new vunits_env_reg(&environment::func))

#define init_hunits_env_reg(name, func) \
  number_reg_dictionary.define(name, new hunits_env_reg(&environment::func))

#define init_string_env_reg(name, func) \
  number_reg_dictionary.define(name, new string_env_reg(&environment::func))

void init_env_requests()
{
  init_request("ad", adjust);
  init_request("br", break_request);
  init_request("brp", break_spread_request);
  init_request("c2", no_break_control_char);
  init_request("cc", control_char);
  init_request("ce", center);
  init_request("cu", continuous_underline);
  init_request("ev", environment_switch);
  init_request("evc", environment_copy);
  init_request("fam", family_change);
  init_request("fc", field_characters);
  init_request("fi", fill);
  init_request("fcolor", fill_color_change);
  init_request("ft", font_change);
  init_request("gcolor", glyph_color_change);
  init_request("hc", hyphen_char);
  init_request("hlm", hyphen_line_max_request);
  init_request("hy", hyphenate_request);
  init_request("hym", hyphenation_margin_request);
  init_request("hys", hyphenation_space_request);
  init_request("in", indent);
  init_request("it", input_trap);
  init_request("itc", input_trap_continued);
  init_request("lc", leader_character);
  init_request("linetabs", line_tabs_request);
  init_request("ll", line_length);
  init_request("ls", line_spacing);
  init_request("lt", title_length);
  init_request("mc", margin_character);
  init_request("na", no_adjust);
  init_request("nf", no_fill);
  init_request("nh", no_hyphenate);
  init_request("nm", number_lines);
  init_request("nn", no_number);
  init_request("ps", point_size);
  init_request("pvs", post_vertical_spacing);
  init_request("rj", right_justify);
  init_request("sizes", override_sizes);
  init_request("ss", space_size);
  init_request("ta", set_tabs);
  init_request("ti", temporary_indent);
  init_request("tc", tab_character);
  init_request("tl", title);
  init_request("ul", underline);
  init_request("vs", vertical_spacing);
#ifdef WIDOW_CONTROL
  init_request("wdc", widow_control_request);
#endif /* WIDOW_CONTROL */
  init_int_env_reg(".b", get_bold);
  init_vunits_env_reg(".cdp", get_prev_char_depth);
  init_int_env_reg(".ce", get_center_lines);
  init_vunits_env_reg(".cht", get_prev_char_height);
  init_hunits_env_reg(".csk", get_prev_char_skew);
  init_string_env_reg(".ev", get_name_string);
  init_int_env_reg(".f", get_font);
  init_string_env_reg(".fam", get_font_family_string);
  init_string_env_reg(".fn", get_font_name_string);
  init_int_env_reg(".height", get_char_height);
  init_int_env_reg(".hlc", get_hyphen_line_count);
  init_int_env_reg(".hlm", get_hyphen_line_max);
  init_int_env_reg(".hy", get_hyphenation_flags);
  init_hunits_env_reg(".hym", get_hyphenation_margin);
  init_hunits_env_reg(".hys", get_hyphenation_space);
  init_hunits_env_reg(".i", get_indent);
  init_hunits_env_reg(".in", get_saved_indent);
  init_int_env_reg(".int", get_prev_line_interrupted);
  init_int_env_reg(".linetabs", get_line_tabs);
  init_hunits_env_reg(".lt", get_title_length);
  init_int_env_reg(".j", get_adjust_mode);
  init_hunits_env_reg(".k", get_text_length);
  init_int_env_reg(".L", get_line_spacing);
  init_hunits_env_reg(".l", get_line_length);
  init_hunits_env_reg(".ll", get_saved_line_length);
  init_string_env_reg(".M", get_fill_color_string);
  init_string_env_reg(".m", get_glyph_color_string);
  init_hunits_env_reg(".n", get_prev_text_length);
  init_int_env_reg(".ps", get_point_size);
  init_int_env_reg(".psr", get_requested_point_size);
  init_vunits_env_reg(".pvs", get_post_vertical_spacing);
  init_int_env_reg(".rj", get_right_justify_lines);
  init_string_env_reg(".s", get_point_size_string);
  init_int_env_reg(".slant", get_char_slant);
  init_int_env_reg(".ss", get_space_size);
  init_int_env_reg(".sss", get_sentence_space_size);
  init_string_env_reg(".sr", get_requested_point_size_string);
  init_string_env_reg(".sty", get_style_name_string);
  init_string_env_reg(".tabs", get_tabs);
  init_int_env_reg(".u", get_fill);
  init_vunits_env_reg(".v", get_vertical_spacing);
  init_hunits_env_reg(".w", get_prev_char_width);
  number_reg_dictionary.define("ct", new variable_reg(&ct_reg_contents));
  number_reg_dictionary.define("hp", new horizontal_place_reg);
  number_reg_dictionary.define("ln", new variable_reg(&next_line_number));
  number_reg_dictionary.define("rsb", new variable_reg(&rsb_reg_contents));
  number_reg_dictionary.define("rst", new variable_reg(&rst_reg_contents));
  number_reg_dictionary.define("sb", new variable_reg(&sb_reg_contents));
  number_reg_dictionary.define("skw", new variable_reg(&skw_reg_contents));
  number_reg_dictionary.define("ssc", new variable_reg(&ssc_reg_contents));
  number_reg_dictionary.define("st", new variable_reg(&st_reg_contents));
}

// Hyphenation - TeX's hyphenation algorithm with a less fancy implementation.

struct trie_node;

class trie {
  trie_node *tp;
  virtual void do_match(int len, void *val) = 0;
  virtual void do_delete(void *) = 0;
  void delete_trie_node(trie_node *);
public:
  trie() : tp(0) {}
  virtual ~trie();		// virtual to shut up g++
  void insert(const char *, int, void *);
  // find calls do_match for each match it finds
  void find(const char *pat, int patlen);
  void clear();
};

class hyphen_trie : private trie {
  int *h;
  void do_match(int i, void *v);
  void do_delete(void *v);
  void insert_pattern(const char *pat, int patlen, int *num);
  void insert_hyphenation(dictionary *ex, const char *pat, int patlen);
  int hpf_getc(FILE *f);
public:
  hyphen_trie() {}
  ~hyphen_trie() {}
  void hyphenate(const char *word, int len, int *hyphens);
  void read_patterns_file(const char *name, int append, dictionary *ex);
};

struct hyphenation_language {
  symbol name;
  dictionary exceptions;
  hyphen_trie patterns;
  hyphenation_language(symbol nm) : name(nm), exceptions(501) {}
  ~hyphenation_language() { }
};

dictionary language_dictionary(5);
hyphenation_language *current_language = 0;

static void set_hyphenation_language()
{
  symbol nm = get_name(1);
  if (!nm.is_null()) {
    current_language = (hyphenation_language *)language_dictionary.lookup(nm);
    if (!current_language) {
      current_language = new hyphenation_language(nm);
      (void)language_dictionary.lookup(nm, (void *)current_language);
    }
  }
  skip_line();
}

const int WORD_MAX = 256;	// we use unsigned char for offsets in
				// hyphenation exceptions

static void hyphen_word()
{
  if (!current_language) {
    error("no current hyphenation language");
    skip_line();
    return;
  }
  char buf[WORD_MAX + 1];
  unsigned char pos[WORD_MAX + 2];
  for (;;) {
    tok.skip();
    if (tok.newline() || tok.eof())
      break;
    int i = 0;
    int npos = 0;
    while (i < WORD_MAX && !tok.space() && !tok.newline() && !tok.eof()) {
      charinfo *ci = tok.get_char(1);
      if (ci == 0) {
	skip_line();
	return;
      }
      tok.next();
      if (ci->get_ascii_code() == '-') {
	if (i > 0 && (npos == 0 || pos[npos - 1] != i))
	  pos[npos++] = i;
      }
      else {
	unsigned char c = ci->get_hyphenation_code();
	if (c == 0)
	  break;
	buf[i++] = c;
      }
    }
    if (i > 0) {
      pos[npos] = 0;
      buf[i] = 0;
      unsigned char *tem = new unsigned char[npos + 1];
      memcpy(tem, pos, npos + 1);
      tem = (unsigned char *)current_language->exceptions.lookup(symbol(buf),
								 tem);
      if (tem)
	a_delete tem;
    }
  }
  skip_line();
}

struct trie_node {
  char c;
  trie_node *down;
  trie_node *right;
  void *val;
  trie_node(char, trie_node *);
};

trie_node::trie_node(char ch, trie_node *p) 
: c(ch), down(0), right(p), val(0)
{
}

trie::~trie()
{
  clear();
}

void trie::clear()
{
  delete_trie_node(tp);
  tp = 0;
}


void trie::delete_trie_node(trie_node *p)
{
  if (p) {
    delete_trie_node(p->down);
    delete_trie_node(p->right);
    if (p->val)
      do_delete(p->val);
    delete p;
  }
}

void trie::insert(const char *pat, int patlen, void *val)
{
  trie_node **p = &tp;
  assert(patlen > 0 && pat != 0);
  for (;;) {
    while (*p != 0 && (*p)->c < pat[0])
      p = &((*p)->right);
    if (*p == 0 || (*p)->c != pat[0])
      *p = new trie_node(pat[0], *p);
    if (--patlen == 0) {
      (*p)->val = val;
      break;
    }
    ++pat;
    p = &((*p)->down);
  }
}

void trie::find(const char *pat, int patlen)
{
  trie_node *p = tp;
  for (int i = 0; p != 0 && i < patlen; i++) {
    while (p != 0 && p->c < pat[i])
      p = p->right;
    if (p != 0 && p->c == pat[i]) {
      if (p->val != 0)
	do_match(i+1, p->val);
      p = p->down;
    }
    else
      break;
  }
}

struct operation {
  operation *next;
  short distance;
  short num;
  operation(int, int, operation *);
};

operation::operation(int i, int j, operation *op)
: next(op), distance(j), num(i)
{
}

void hyphen_trie::insert_pattern(const char *pat, int patlen, int *num)
{
  operation *op = 0;
  for (int i = 0; i < patlen+1; i++)
    if (num[i] != 0)
      op = new operation(num[i], patlen - i, op);
  insert(pat, patlen, op);
}

void hyphen_trie::insert_hyphenation(dictionary *ex, const char *pat,
				     int patlen)
{
  char buf[WORD_MAX + 1];
  unsigned char pos[WORD_MAX + 2];
  int i = 0, j = 0;
  int npos = 0;
  while (j < patlen) {
    unsigned char c = pat[j++];
    if (c == '-') {
      if (i > 0 && (npos == 0 || pos[npos - 1] != i))
	pos[npos++] = i;
    }
    else
      buf[i++] = hpf_code_table[c];
  }
  if (i > 0) {
    pos[npos] = 0;
    buf[i] = 0;
    unsigned char *tem = new unsigned char[npos + 1];
    memcpy(tem, pos, npos + 1);
    tem = (unsigned char *)ex->lookup(symbol(buf), tem);
    if (tem)
      a_delete tem;
  }
}

void hyphen_trie::hyphenate(const char *word, int len, int *hyphens)
{
  int j;
  for (j = 0; j < len + 1; j++)
    hyphens[j] = 0;
  for (j = 0; j < len - 1; j++) {
    h = hyphens + j;
    find(word + j, len - j);
  }
}

inline int max(int m, int n)
{
  return m > n ? m : n;
}

void hyphen_trie::do_match(int i, void *v)
{
  operation *op = (operation *)v;
  while (op != 0) {
    h[i - op->distance] = max(h[i - op->distance], op->num);
    op = op->next;
  }
}

void hyphen_trie::do_delete(void *v)
{
  operation *op = (operation *)v;
  while (op) {
    operation *tem = op;
    op = tem->next;
    delete tem;
  }
}

/* We use very simple rules to parse TeX's hyphenation patterns.

   . `%' starts a comment even if preceded by `\'.

   . No support for digraphs and like `\$'.

   . `^^xx' (`x' is 0-9 or a-f), and `^^x' (character code of `x' in the
     range 0-127) are recognized; other use of `^' causes an error.

   . No macro expansion.

   . We check for the expression `\patterns{...}' (possibly with
     whitespace before and after the braces).  Everything between the
     braces is taken as hyphenation patterns.  Consequently, `{' and `}'
     are not allowed in patterns.

   . Similarly, `\hyphenation{...}' gives a list of hyphenation
     exceptions.

   . `\endinput' is recognized also.

   . For backwards compatibility, if `\patterns' is missing, the
     whole file is treated as a list of hyphenation patterns (only
     recognizing `%' as the start of a comment.

*/

int hyphen_trie::hpf_getc(FILE *f)
{
  int c = getc(f);
  int c1;
  int cc = 0;
  if (c != '^')
    return c;
  c = getc(f);
  if (c != '^')
    goto fail;
  c = getc(f);
  c1 = getc(f);
  if (((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))
      && ((c1 >= '0' && c1 <= '9') || (c1 >= 'a' && c1 <= 'f'))) {
    if (c >= '0' && c <= '9')
      c -= '0';
    else
      c = c - 'a' + 10;
    if (c1 >= '0' && c1 <= '9')
      c1 -= '0';
    else
      c1 = c1 - 'a' + 10;
    cc = c * 16 + c1;
  }
  else {
    ungetc(c1, f);
    if (c >= 0 && c <= 63)
      cc = c + 64;
    else if (c >= 64 && c <= 127)
      cc = c - 64;
    else
      goto fail;
  }
  return cc;
fail:
  error("invalid ^, ^^x, or ^^xx character in hyphenation patterns file");
  return c;
}

void hyphen_trie::read_patterns_file(const char *name, int append,
				     dictionary *ex)
{
  if (!append)
    clear();
  char buf[WORD_MAX];
  for (int i = 0; i < WORD_MAX; i++)
    buf[i] = 0;
  int num[WORD_MAX+1];
  errno = 0;
  char *path = 0;
  FILE *fp = mac_path->open_file(name, &path);
  if (fp == 0) {
    error("can't find hyphenation patterns file `%1'", name);
    return;
  }
  int c = hpf_getc(fp);
  int have_patterns = 0;	// we've seen \patterns
  int final_pattern = 0;	// 1 if we have a trailing closing brace
  int have_hyphenation = 0;	// we've seen \hyphenation
  int final_hyphenation = 0;	// 1 if we have a trailing closing brace
  int have_keyword = 0;		// we've seen either \patterns or \hyphenation
  int traditional = 0;		// don't handle \patterns
  for (;;) {
    for (;;) {
      if (c == '%') {		// skip comments
	do {
	  c = getc(fp);
	} while (c != EOF && c != '\n');
      }
      if (c == EOF || !csspace(c))
	break;
      c = hpf_getc(fp);
    }
    if (c == EOF) {
      if (have_keyword || traditional)	// we are done
	break;
      else {				// rescan file in `traditional' mode
	rewind(fp);
	traditional = 1;
	c = hpf_getc(fp);
	continue;
      }
    }
    int i = 0;
    num[0] = 0;
    if (!(c == '{' || c == '}')) {	// skip braces at line start
      do {				// scan patterns
	if (csdigit(c))
	  num[i] = c - '0';
	else {
	  buf[i++] = c;
	  num[i] = 0;
	}
	c = hpf_getc(fp);
      } while (i < WORD_MAX && c != EOF && !csspace(c)
	       && c != '%' && c != '{' && c != '}');
    }
    if (!traditional) {
      if (i >= 9 && !strncmp(buf + i - 9, "\\patterns", 9)) {
	while (csspace(c))
	  c = hpf_getc(fp);
	if (c == '{') {
	  if (have_patterns || have_hyphenation)
	    error("\\patterns not allowed inside of %1 group",
		  have_patterns ? "\\patterns" : "\\hyphenation");
	  else {
	    have_patterns = 1;
	    have_keyword = 1;
	  }
	  c = hpf_getc(fp);
	  continue;
	}
      }
      else if (i >= 12 && !strncmp(buf + i - 12, "\\hyphenation", 12)) {
	while (csspace(c))
	  c = hpf_getc(fp);
	if (c == '{') {
	  if (have_patterns || have_hyphenation)
	    error("\\hyphenation not allowed inside of %1 group",
		  have_patterns ? "\\patterns" : "\\hyphenation");
	  else {
	    have_hyphenation = 1;
	    have_keyword = 1;
	  }
	  c = hpf_getc(fp);
	  continue;
	}
      }
      else if (strstr(buf, "\\endinput")) {
	if (have_patterns || have_hyphenation)
	  error("found \\endinput inside of %1 group",
		have_patterns ? "\\patterns" : "\\hyphenation");
	break;
      }
      else if (c == '}') {
	if (have_patterns) {
	  have_patterns = 0;
	  if (i > 0)
	    final_pattern = 1;
	}
	else if (have_hyphenation) {
	  have_hyphenation = 0;
	  if (i > 0)
	    final_hyphenation = 1;
	}
	c = hpf_getc(fp);
      }
      else if (c == '{') {
	if (have_patterns || have_hyphenation)
	  error("`{' not allowed within %1 group",
		have_patterns ? "\\patterns" : "\\hyphenation");
	c = hpf_getc(fp);		// skipped if not starting \patterns
					// or \hyphenation
      }
    }
    else {
      if (c == '{' || c == '}')
	c = hpf_getc(fp);
    }
    if (i > 0) {
      if (have_patterns || final_pattern || traditional) {
	for (int j = 0; j < i; j++)
	  buf[j] = hpf_code_table[(unsigned char)buf[j]];
	insert_pattern(buf, i, num);
	final_pattern = 0;
      }
      else if (have_hyphenation || final_hyphenation) {
	insert_hyphenation(ex, buf, i);
	final_hyphenation = 0;
      }
    }
  }
  fclose(fp);
  a_delete path;
  return;
}

void hyphenate(hyphen_list *h, unsigned flags)
{
  if (!current_language)
    return;
  while (h) {
    while (h && h->hyphenation_code == 0)
      h = h->next;
    int len = 0;
    char hbuf[WORD_MAX+2];
    char *buf = hbuf + 1;
    hyphen_list *tem;
    for (tem = h; tem && len < WORD_MAX; tem = tem->next) {
      if (tem->hyphenation_code != 0)
	buf[len++] = tem->hyphenation_code;
      else
	break;
    }
    hyphen_list *nexth = tem;
    if (len > 2) {
      buf[len] = 0;
      unsigned char *pos
	= (unsigned char *)current_language->exceptions.lookup(buf);
      if (pos != 0) {
	int j = 0;
	int i = 1;
	for (tem = h; tem != 0; tem = tem->next, i++)
	  if (pos[j] == i) {
	    tem->hyphen = 1;
	    j++;
	  }
      }
      else {
	hbuf[0] = hbuf[len+1] = '.';
	int num[WORD_MAX+3];
	current_language->patterns.hyphenate(hbuf, len+2, num);
	int i;
	num[2] = 0;
	if (flags & 8)
	  num[3] = 0;
	if (flags & 4)
	  --len;
	for (i = 2, tem = h; i < len && tem; tem = tem->next, i++)
	  if (num[i] & 1)
	    tem->hyphen = 1;
      }
    }
    h = nexth;
  }
}

static void do_hyphenation_patterns_file(int append)
{
  symbol name = get_long_name(1);
  if (!name.is_null()) {
    if (!current_language)
      error("no current hyphenation language");
    else
      current_language->patterns.read_patterns_file(
			  name.contents(), append,
			  &current_language->exceptions);
  }
  skip_line();
}

static void hyphenation_patterns_file()
{
  do_hyphenation_patterns_file(0);
}

static void hyphenation_patterns_file_append()
{
  do_hyphenation_patterns_file(1);
}

class hyphenation_language_reg : public reg {
public:
  const char *get_string();
};

const char *hyphenation_language_reg::get_string()
{
  return current_language ? current_language->name.contents() : "";
}

void init_hyphen_requests()
{
  init_request("hw", hyphen_word);
  init_request("hla", set_hyphenation_language);
  init_request("hpf", hyphenation_patterns_file);
  init_request("hpfa", hyphenation_patterns_file_append);
  number_reg_dictionary.define(".hla", new hyphenation_language_reg);
}