File: hc.c

package info (click to toggle)
xfractint 20.4.10-2
  • links: PTS
  • area: non-free
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 4,712 kB
  • ctags: 8,020
  • sloc: ansic: 77,316; asm: 430; cpp: 425; makefile: 379; sh: 38
file content (3941 lines) | stat: -rw-r--r-- 100,815 bytes parent folder | download | duplicates (3)
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
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941

/*
 * hc.c
 *
 * Stand-alone FRACTINT help compiler.  Compile in the COMPACT memory model.
 *
 * See HC.DOC for source file syntax.
 *
 *
 * Revision History:
 *
 *   02-26-91 EAN     Initial version.
 *
 *   03-21-91 EAN     Modified for automatic paragraph formatting.
 *                    Added several new commands:
 *                       Format[+/-]  Enable/disable paragraph formatting
 *                       Doc[+/-]     Enable/disable output to document.
 *                       Online[+/-]  Enable/disable output to online help.
 *                       Label=       Defines a label. Replaces ~(...)
 *                       FF           Forces a form-feed.  Replaces ~~
 *                       FormatExclude=val Exclude lines past val from
 *                                    formatting.  If before any topic sets
 *                                    global default, otherwise set local.
 *                       FormatExclude= Set to global default.
 *                       FormatExclude=n Disable exclusion. (global or local)
 *                       FormatExclude[+/-] Enable/disable format exclusion.
 *                       Center[+/-]  Enable/disable centering of text.
 *                       \ before nl  Forces the end of a paragraph
 *                    Support for commands embedded in text with new
 *                    ~(...) format.
 *                    Support for multiple commands on a line separated by
 *                    commas.
 *                    Support for implict links; explicit links must now
 *                    start with an equal sign.
 *   04-03-91 EAN     Added "include" command (works like #include)
 *   04-10-91 EAN     Added support for "data" topics.
 *                    Added Comment/EndComment commands for multi-line
 *                       comments.
 *                    Added CompressSpaces[+/-] command.
 *                    Added DocContents command for document printing.
 *                    Added BinInc command which includes a binary file
 *                       in a data topic.
 *                    Fixed tables to flow down instead of across the page.
 *                       Makes no allowances for page breaks within tables.
 *   11-03-94 TIW     Increased buffer size.
 *
 */


#define HC_C

#define INCLUDE_COMMON  /* tell helpcom.h to include common code */


#ifdef XFRACT
#define strupr strlwr
#else
#include <io.h>
#endif

#ifndef USE_VARARGS
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#include <fcntl.h>
#include <string.h>
#include <ctype.h>

#ifdef __TURBOC__
#   include <dir.h>
#   define FNSPLIT fnsplit
#else
#   define MAXFILE FILE_MAX_FNAME
#   define MAXEXT  FILE_MAX_EXT
#   define FNSPLIT _splitpath
#endif


#include <assert.h>
  /* see Fractint.c for a description of the "include"  hierarchy */
#include "port.h"
#include "helpcom.h"

#ifdef XFRACT
#ifndef HAVESTRI
extern int stricmp(char *, char *);
extern int strnicmp(char *, char *, int);
#endif
extern int filelength(int);
extern int _splitpath(char *,char *,char *,char *,char *);
#endif

/*
 * When defined, SHOW_ERROR_LINE will cause the line number in HC.C where
 * errors/warnings/messages are generated to be displayed at the start of
 * the line.
 *
 * Used when debugging HC.  Also useful for finding the line (in HC.C) that
 * generated a error or warning.
 */

#ifndef XFRACT
#define SHOW_ERROR_LINE
#endif


#define DEFAULT_SRC_FNAME "help.src"
#define DEFAULT_HLP_FNAME "fractint.hlp"
#define DEFAULT_EXE_FNAME "fractint.exe"
#define DEFAULT_DOC_FNAME "fractint.doc"

#define TEMP_FNAME        "HC.$$$"
#define SWAP_FNAME        "HCSWAP.$$$"

#define MAX_ERRORS        (25)   /* stop after this many errors */
#define MAX_WARNINGS      (25)   /* stop after this many warnings */
                                 /* 0 = never stop */

#define INDEX_LABEL       "HELP_INDEX"
#define DOCCONTENTS_TITLE "DocContent"



/* #define BUFFER_SIZE   (24*1024) */
#define BUFFER_SIZE   (30*1024)


typedef struct
   {
   int      type;            /* 0 = name is topic title, 1 = name is label, */
                             /*   2 = "special topic"; name is NULL and */
                             /*   topic_num/topic_off is valid */
   int      topic_num;       /* topic number to link to */
   unsigned topic_off;       /* offset into topic to link to */
   int      doc_page;        /* document page # to link to */
   char    *name;            /* name of label or title of topic to link to */
   char    *srcfile;         /* .SRC file link appears in */
   int      srcline;         /* .SRC file line # link appears in */
   } LINK;


typedef struct
   {
   unsigned offset;     /* offset from start of topic text */
   unsigned length;     /* length of page (in chars) */
   int      margin;     /* if > 0 then page starts in_para and text */
                        /* should be indented by this much */
   } PAGE;


/* values for TOPIC.flags */

#define TF_IN_DOC  (1)       /* 1 if topic is part of the printed document */
#define TF_DATA    (2)       /* 1 if it is a "data" topic */


typedef struct
   {
   unsigned  flags;          /* see #defines for TF_??? */
   int       doc_page;       /* page number in document where topic starts */
   unsigned  title_len;      /* length of title */
   char     *title;          /* title for this topic */
   int       num_page;       /* number of pages */
   PAGE     *page;           /* list of pages */
   unsigned  text_len;       /* lenth of topic text */
   long      text;           /* topic text (all pages) */
   long      offset;         /* offset to topic from start of file */
   } TOPIC;


typedef struct
   {
   char    *name;            /* its name */
   int      topic_num;       /* topic number */
   unsigned topic_off;       /* offset of label in the topic's text */
   int      doc_page;
   } LABEL;


/* values for CONTENT.flags */

#define CF_NEW_PAGE  (1)     /* true if section starts on a new page */


#define MAX_CONTENT_TOPIC (10)


typedef struct
   {
   unsigned  flags;
   char     *id;
   char     *name;
   int       doc_page;
   unsigned  page_num_pos;
   int       num_topic;
   char      is_label[MAX_CONTENT_TOPIC];
   char     *topic_name[MAX_CONTENT_TOPIC];
   int       topic_num[MAX_CONTENT_TOPIC];
   char     *srcfile;
   int       srcline;
   } CONTENT;


struct help_sig_info
   {
   unsigned long sig;
   int           version;
   unsigned long base;
   } ;


int      num_topic        = 0;    /* topics */
TOPIC   *topic;

int      num_label        = 0;    /* labels */
LABEL   *label;

int      num_plabel       = 0;    /* private labels */
LABEL   *plabel;

int      num_link         = 0;    /* all links */
LINK    *a_link           = 0;

int      num_contents     = 0;    /* the table-of-contents */
CONTENT *contents;

int      quiet_mode       = 0;    /* true if "/Q" option used */

int      max_pages        = 0;    /* max. pages in any topic */
int      max_links        = 0;    /* max. links on any page */
int      num_doc_pages    = 0;    /* total number of pages in document */

FILE    *srcfile;                 /* .SRC file */
int      srcline          = 0;    /* .SRC line number (used for errors) */
int      srccol           = 0;    /* .SRC column. */

int      version          = -1;   /* help file version */

int      errors           = 0,    /* number of errors reported */
         warnings         = 0;    /* number of warnings reported */

char     src_fname[81]    = "";   /* command-line .SRC filename */
char     hdr_fname[81]    = "";   /* .H filename */
char     hlp_fname[81]    = "";   /* .HLP filename */
char    *src_cfname       = NULL; /* current .SRC filename */

int      format_exclude   = 0;    /* disable formatting at this col, 0 to */
                                  /*    never disable formatting */
FILE    *swapfile;
long     swappos;

char    *buffer;                  /* alloc'ed as BUFFER_SIZE bytes */
char    *curr;                    /* current position in the buffer */
char     cmd[128];                /* holds the current command */
int      compress_spaces;
int      xonline;
int      xdoc;

#define  MAX_INCLUDE_STACK (5)    /* allow 5 nested includes */

struct
   {
   char *fname;
   FILE *file;
   int   line;
   int   col;
   } include_stack[MAX_INCLUDE_STACK];
int include_stack_top = -1;


#define CHK_BUFFER(off) { if ((long)(curr+(off)) - (long)buffer >= (BUFFER_SIZE-1024)) fatal(0,"Buffer overflowed -- Help topic too large."); }

#ifdef __WATCOMC__
#define putw( x1, x2 )  fprintf( x2, "%c%c", x1&0xFF, x1>>8 );
#endif

#ifdef XFRACT
#define putw( x1, x2 )  fwrite( &(x1), 1, sizeof(int), x2);
#endif

/*
 * error/warning/message reporting functions.
 */


void report_errors(void)
   {
   printf("\n");
   printf("Compiler Status:\n");
   printf("%8d Error%c\n",       errors,   (errors==1)   ? ' ' : 's');
   printf("%8d Warning%c\n",     warnings, (warnings==1) ? ' ' : 's');
   }


void print_msg(char *type, int lnum, char *format, va_list arg)
   {
   if (type != NULL)
      {
      printf("   %s", type);
      if (lnum>0)
         printf(" %s %d", src_cfname, lnum);
      printf(": ");
      }
   vprintf(format, arg);
   printf("\n");
   }


#ifndef USE_VARARGS
void fatal(int diff, char *format, ...)
#else
void fatal(va_alist)
    va_dcl
#endif
   {
   va_list arg;

#ifndef USE_VARARGS
   va_start(arg, format);
#else
   int diff;
   char *format;
   va_start(arg);
   diff = va_arg(arg,int);
   format = va_arg(arg,char *);
#endif

   print_msg("Fatal", srcline-diff, format, arg);
   va_end(arg);

   if ( errors || warnings )
      report_errors();

   exit( errors + 1 );
   }


#ifndef USE_VARARGS
void error(int diff, char *format, ...)
#else
void error(va_alist)
    va_dcl
#endif
   {
   va_list arg;

#ifndef USE_VARARGS
   va_start(arg, format);
#else
   int diff;
   char *format;
   va_start(arg);
   diff = va_arg(arg,int);
   format = va_arg(arg,char *);
#endif
   print_msg("Error", srcline-diff, format, arg);
   va_end(arg);

   if (++errors >= MAX_ERRORS && MAX_ERRORS > 0)
      fatal(0,"Too many errors!");
   }


#ifndef USE_VARARGS
void warn(int diff, char *format, ...)
#else
void warn(va_alist)
   va_dcl
#endif
   {
   va_list arg;
#ifndef USE_VARARGS
   va_start(arg, format);
#else
   int diff;
   char *format;
   va_start(arg);
   diff = va_arg(arg, int);
   format = va_arg(arg, char *);
#endif
   print_msg("Warning", srcline-diff, format, arg);
   va_end(arg);

   if (++warnings >= MAX_WARNINGS && MAX_WARNINGS > 0)
      fatal(0,"Too many warnings!");
   }


#ifndef USE_VARARGS
void notice(char *format, ...)
#else
void notice(va_alist)
    va_dcl
#endif
   {
   va_list arg;
#ifndef USE_VARARGS
   va_start(arg, format);
#else
   char *format;

   va_start(arg);
   format = va_arg(arg,char *);
#endif
   print_msg("Note", srcline, format, arg);
   va_end(arg);
   }


#ifndef USE_VARARGS
void msg(char *format, ...)
#else
void msg(va_alist)
va_dcl
#endif
   {
   va_list arg;
#ifdef USE_VARARGS
   char *format;
#endif

   if (quiet_mode)
      return;
#ifndef USE_VARARGS
   va_start(arg, format);
#else
   va_start(arg);
   format = va_arg(arg,char *);
#endif
   print_msg(NULL, 0, format, arg);
   va_end(arg);
   }


#ifdef SHOW_ERROR_LINE
#   define fatal  (printf("[%04d] ", __LINE__), fatal)
#   define error  (printf("[%04d] ", __LINE__), error)
#   define warn   (printf("[%04d] ", __LINE__), warn)
#   define notice (printf("[%04d] ", __LINE__), notice)
#   define msg    (printf((quiet_mode)?"":"[%04d] ", __LINE__), msg)
#endif


/*
 * store-topic-text-to-disk stuff.
 */


void alloc_topic_text(TOPIC *t, unsigned size)
   {
   t->text_len = size;
   t->text = swappos;
   swappos += size;
   fseek(swapfile, t->text, SEEK_SET);
   fwrite(buffer, 1, t->text_len, swapfile);
   }


char *get_topic_text(TOPIC *t)
   {
   fseek(swapfile, t->text, SEEK_SET);
   fread(buffer, 1, t->text_len, swapfile);
   return (buffer);
   }


void release_topic_text(TOPIC *t, int save)
   {
   if ( save )
      {
      fseek(swapfile, t->text, SEEK_SET);
      fwrite(buffer, 1, t->text_len, swapfile);
      }
   }


/*
 * memory-allocation functions.
 */


#define new(item)    (item *)newx(sizeof(item))
#define delete(item) free(item)


VOIDPTR newx(unsigned size)
   {
   VOIDPTR ptr;

   ptr = malloc(size);

   if (ptr == NULL)
      fatal(0,"Out of memory!");

   return (ptr);
   }


VOIDPTR renewx(VOIDPTR ptr, unsigned size)
   {
   ptr = realloc(ptr, size);

   if (ptr == NULL)
      fatal(0,"Out of memory!");

   return (ptr);
   }


char *dupstr(char *s, unsigned len)
   {
   char *ptr;

   if (len == 0)
      len = strlen(s) + 1;

   ptr = newx(len);

   memcpy(ptr, s, len);

   return (ptr);
   }


#define LINK_ALLOC_SIZE (16)


int add_link(LINK *l)
   {
   if (num_link == 0)
      a_link = newx( sizeof(LINK)*LINK_ALLOC_SIZE );

   else if (num_link%LINK_ALLOC_SIZE == 0)
      a_link = renewx(a_link, sizeof(LINK) * (num_link+LINK_ALLOC_SIZE) );

   a_link[num_link] = *l;

   return( num_link++ );
   }


#define PAGE_ALLOC_SIZE (4)


int add_page(TOPIC *t, PAGE *p)
   {
   if (t->num_page == 0)
      t->page = newx( sizeof(PAGE)*PAGE_ALLOC_SIZE );

   else if (t->num_page%PAGE_ALLOC_SIZE == 0)
      t->page = renewx(t->page, sizeof(PAGE) * (t->num_page+PAGE_ALLOC_SIZE) );

   t->page[t->num_page] = *p;

   return ( t->num_page++ );
   }


#define TOPIC_ALLOC_SIZE (16)


int add_topic(TOPIC *t)
   {
   if (num_topic == 0)
      topic = newx( sizeof(TOPIC)*TOPIC_ALLOC_SIZE );

   else if (num_topic%TOPIC_ALLOC_SIZE == 0)
      topic = renewx(topic, sizeof(TOPIC) * (num_topic+TOPIC_ALLOC_SIZE) );

   topic[num_topic] = *t;

   return ( num_topic++ );
   }


#define LABEL_ALLOC_SIZE (16)


int add_label(LABEL *l)
   {
   if (l->name[0] == '@')    /* if it's a private label... */
      {
      if (num_plabel == 0)
         plabel = newx( sizeof(LABEL)*LABEL_ALLOC_SIZE );

      else if (num_plabel%LABEL_ALLOC_SIZE == 0)
         plabel = renewx(plabel, sizeof(LABEL) * (num_plabel+LABEL_ALLOC_SIZE) );

      plabel[num_plabel] = *l;

      return ( num_plabel++ );
      }
   else
      {
      if (num_label == 0)
         label = newx( sizeof(LABEL)*LABEL_ALLOC_SIZE );

      else if (num_label%LABEL_ALLOC_SIZE == 0)
         label = renewx(label, sizeof(LABEL) * (num_label+LABEL_ALLOC_SIZE) );

      label[num_label] = *l;

      return ( num_label++ );
      }
   }


#define CONTENTS_ALLOC_SIZE (16)


int add_content(CONTENT *c)
   {
   if (num_contents == 0)
      contents = newx( sizeof(CONTENT)*CONTENTS_ALLOC_SIZE );

   else if (num_contents%CONTENTS_ALLOC_SIZE == 0)
      contents = renewx(contents, sizeof(CONTENT) * (num_contents+CONTENTS_ALLOC_SIZE) );

   contents[num_contents] = *c;

   return ( num_contents++ );
   }


/*
 * read_char() stuff
 */


#define READ_CHAR_BUFF_SIZE (32)


int  read_char_buff[READ_CHAR_BUFF_SIZE];
int  read_char_buff_pos = -1;
int  read_char_sp       = 0;


void unread_char(int ch)
   /*
    * Will not handle new-lines or tabs correctly!
    */
   {
   if (read_char_buff_pos+1 >= READ_CHAR_BUFF_SIZE)
      fatal(0,"Compiler Error -- Read char buffer overflow!");

   read_char_buff[++read_char_buff_pos] = ch;

   --srccol;
   }


void unread_string(char *s)
   {
   int p = strlen(s);

   while (p-- > 0)
      unread_char(s[p]);
   }


int eos(void)    /* end-of-source ? */
   {
   return ( !((read_char_sp==0) && (read_char_buff_pos==0) && feof(srcfile)) );
   }


int _read_char(void)
   {
   int ch;

   if (srcline <= 0)
      {
      srcline = 1;
      srccol = 0;
      }

   if (read_char_buff_pos >= 0)
      {
      ++srccol;
      return ( read_char_buff[read_char_buff_pos--] );
      }

   if (read_char_sp > 0)
      {
      --read_char_sp;
      return (' ');
      }

   if ( feof(srcfile) )
      return (-1);

   while (1)
      {
      ch = getc(srcfile);

      switch (ch)
         {
         case '\t':    /* expand a tab */
            {
            int diff = ( ( (srccol/8) + 1 ) * 8 ) - srccol;

            srccol += diff;
            read_char_sp += diff;
            break;
            }

         case ' ':
            ++srccol;
            ++read_char_sp;
            break;

         case '\n':
            read_char_sp = 0;   /* delete spaces before a \n */
            srccol = 0;
            ++srcline;
            return ('\n');

         case -1:               /* EOF */
            if (read_char_sp > 0)
               {
               --read_char_sp;
               return (' ');
               }
            return (-1);

         default:
            if (read_char_sp > 0)
               {
               ungetc(ch, srcfile);
               --read_char_sp;
               return (' ');
               }

            ++srccol;
            return (ch);

         } /* switch */
      }
   }


int read_char(void)
   {
   int ch;

   ch = _read_char();

   while (ch == ';' && srccol==1)    /* skip over comments */
      {
      ch = _read_char();

      while (ch!='\n' && ch!=-1 )
         ch = _read_char();

      ch = _read_char();
      }

   if (ch == '\\')   /* process an escape code */
      {
      ch = _read_char();

      if (ch >= '0' && ch <= '9')
         {
         char buff[4];
         int  ctr;

         for (ctr=0; ; ctr++)
            {
            if ( ch<'0' || ch>'9' || ch==-1 || ctr>=3 )
               {
               unread_char(ch);
               break;
               }
            buff[ctr] = ch;
            ch = _read_char();
            }
         buff[ctr] = '\0';
         ch = atoi(buff);
         }

#ifdef XFRACT
   /* Convert graphics arrows into keyboard chars */
       if (ch>=24 && ch<=27) {
           ch = "KJHL"[ch-24];
       }
#endif
      ch |= 0x100;
      }

   if ( (ch & 0xFF) == 0 )
      {
      error(0,"Null character (\'\\0\') not allowed!");
      ch = 0x1FF; /* since we've had an error the file will not be written; */
                  /*   the value we return doesn't really matter */
      }

   return(ch);
   }


/*
 * misc. search functions.
 */


LABEL *find_label(char *name)
   {
   int    l;
   LABEL *lp;

   if (*name == '@')
      {
      for (l=0, lp=plabel; l<num_plabel; l++, lp++)
         if ( strcmp(name, lp->name) == 0 )
            return (lp);
      }
   else
      {
      for (l=0, lp=label; l<num_label; l++, lp++)
         if ( strcmp(name, lp->name) == 0 )
            return (lp);
      }

   return (NULL);
   }


int find_topic_title(char *title)
   {
   int t;
   int len;

   while (*title == ' ')
      ++title;

   len = strlen(title) - 1;
   while ( title[len] == ' ' && len > 0 )
      --len;

   ++len;

   if ( len > 2 && title[0] == '\"' && title[len-1] == '\"' )
      {
      ++title;
      len -= 2;
      }

   for (t=0; t<num_topic; t++)
      if ( strlen(topic[t].title) == len &&
           strnicmp(title, topic[t].title, len) == 0 )
         return (t);

   return (-1);   /* not found */
   }


/*
 * .SRC file parser stuff
 */


int validate_label_name(char *name)
   {
   if ( !isalpha(*name) && *name!='@' && *name!='_' )
      return (0);  /* invalid */

   while (*(++name) != '\0')
      if ( !isalpha(*name) && !isdigit(*name) && *name!='_' )
         return(0);  /* invalid */

   return (1);  /* valid */
   }


char *read_until(char *buff, int len, char *stop_chars)
   {
   int ch;

   while ( --len > 0 )
      {
      ch = read_char();

      if ( ch == -1 )
         {
         *buff++ = '\0';
         break;
         }

      if ( (ch&0xFF) <= MAX_CMD )
         *buff++ = CMD_LITERAL;

      *buff++ = ch;

      if ( (ch&0x100)==0 && strchr(stop_chars, ch) != NULL )
         break;
      }

   return ( buff-1 );
   }


void skip_over(char *skip)
   {
   int ch;

   while (1)
      {
      ch = read_char();

      if ( ch == -1 )
         break;

      else if ( (ch&0x100) == 0 && strchr(skip, ch) == NULL )
         {
         unread_char(ch);
         break;
         }
      }
   }


char *pchar(int ch)
   {
   static char buff[16];

   if ( ch >= 0x20 && ch <= 0x7E )
      sprintf(buff, "\'%c\'", ch);
   else
      sprintf(buff, "\'\\x%02X\'", ch&0xFF);

   return (buff);
   }


void put_spaces(int how_many)
   {
   if (how_many > 2 && compress_spaces)
      {
      if (how_many > 255)
         {
         error(0,"Too many spaces (over 255).");
         how_many = 255;
         }

      *curr++ = CMD_SPACE;
      *curr++ = (BYTE)how_many;
      }
   else
      {
      while (how_many-- > 0)
         *curr++ = ' ';
      }
   }


int get_next_item(void)   /* used by parse_contents() */
   {
   int   last;
   char *ptr;

   skip_over(" \t\n");
   ptr = read_until(cmd, 128, ",}");
   last = (*ptr == '}');
   --ptr;
   while ( ptr >= cmd && strchr(" \t\n",*ptr) )   /* strip trailing spaces */
      --ptr;
   *(++ptr) = '\0';

   return (last);
   }


void process_contents(void)
   {
   CONTENT c;
   char   *ptr;
   int     indent;
   int     ch;
   TOPIC   t;

   t.flags     = 0;
   t.title_len = strlen(DOCCONTENTS_TITLE)+1;
   t.title     = dupstr(DOCCONTENTS_TITLE, t.title_len);
   t.doc_page  = -1;
   t.num_page  = 0;

   curr = buffer;

   c.flags = 0;
   c.id = dupstr("",1);
   c.name = dupstr("",1);
   c.doc_page = -1;
   c.page_num_pos = 0;
   c.num_topic = 1;
   c.is_label[0] = 0;
   c.topic_name[0] = dupstr(DOCCONTENTS_TITLE,0);
   c.srcline = -1;
   add_content(&c);

   while (1)
      {
      ch = read_char();

      if (ch == '{')   /* process a CONTENT entry */
         {
         int last;

         c.flags = 0;
         c.num_topic = 0;
         c.doc_page = -1;
         c.srcfile = src_cfname;
         c.srcline = srcline;

         if ( get_next_item() )
            {
            error(0,"Unexpected end of DocContent entry.");
            continue;
            }
         c.id = dupstr(cmd,0);

         if ( get_next_item() )
            {
            error(0,"Unexpected end of DocContent entry.");
            continue;
            }
         indent = atoi(cmd);

         last = get_next_item();

         if ( cmd[0] == '\"' )
            {
            ptr = cmd+1;
            if (ptr[strlen(ptr)-1] == '\"')
               ptr[strlen(ptr)-1] = '\0';
            else
               warn(0,"Missing ending quote.");

            c.is_label[c.num_topic] = 0;
            c.topic_name[c.num_topic] = dupstr(ptr,0);
            ++c.num_topic;
            c.name = dupstr(ptr,0);
            }
         else
            c.name = dupstr(cmd,0);

         /* now, make the entry in the buffer */

         sprintf(curr, "%-5s %*.0s%s", c.id, indent*2, "", c.name);
         ptr = curr + strlen(curr);
         while ( (ptr-curr) < PAGE_WIDTH-10 )
            *ptr++ = '.';
         c.page_num_pos = (unsigned) ( (ptr-3) - buffer );
         curr = ptr;

         while (!last)
            {
            last = get_next_item();

            if ( stricmp(cmd, "FF") == 0 )
               {
               if ( c.flags & CF_NEW_PAGE )
                  warn(0,"FF already present in this entry.");
               c.flags |= CF_NEW_PAGE;
               continue;
               }

            if (cmd[0] == '\"')
               {
               ptr = cmd+1;
               if (ptr[strlen(ptr)-1] == '\"')
                  ptr[strlen(ptr)-1] = '\0';
               else
                  warn(0,"Missing ending quote.");

               c.is_label[c.num_topic] = 0;
               c.topic_name[c.num_topic] = dupstr(ptr,0);
               }
            else
               {
               c.is_label[c.num_topic] = 1;
               c.topic_name[c.num_topic] = dupstr(cmd,0);
               }

            if ( ++c.num_topic >= MAX_CONTENT_TOPIC )
               {
               error(0,"Too many topics in DocContent entry.");
               break;
               }
            }

         add_content(&c);
         }

      else if (ch == '~')   /* end at any command */
         {
         unread_char(ch);
         break;
         }

      else
         *curr++ = ch;

      CHK_BUFFER(0);
      }

   alloc_topic_text(&t, (unsigned) (curr - buffer) );
   add_topic(&t);
   }


int parse_link(void)   /* returns length of link or 0 on error */
   {
   char *ptr;
   char *end;
   int   bad = 0;
   int   len;
   LINK  l;
   int   lnum;
   int   err_off;

   l.srcfile  = src_cfname;
   l.srcline  = srcline;
   l.doc_page = -1;

   end = read_until(cmd, 128, "}\n");   /* get the entire hot-link */

   if (*end == '\0')
      {
      error(0,"Unexpected EOF in hot-link.");
      return (0);
      }

   if (*end == '\n')
      {
      err_off = 1;
      warn(1,"Hot-link has no closing curly-brace (\'}\').");
      }
   else
      err_off = 0;

   *end = '\0';

   if (cmd[0] == '=')   /* it's an "explicit" link to a label or "special" */
      {
      ptr = strchr(cmd, ' ');

      if (ptr == NULL)
         ptr = end;
      else
         *ptr++ = '\0';

      len = (int) (end - ptr);

      if ( cmd[1] == '-' )
         {
         l.type      = 2;          /* type 2 = "special" */
         l.topic_num = atoi(cmd+1);
         l.topic_off = 0;
         l.name      = NULL;
         }
      else
         {
         l.type = 1;           /* type 1 = to a label */
         if ((int)strlen(cmd) > 32)
            warn(err_off, "Label is long.");
         if (cmd[1] == '\0')
            {
            error(err_off, "Explicit hot-link has no Label.");
            bad = 1;
            }
         else
            l.name = dupstr(cmd+1,0);
         }
      if (len == 0)
         warn(err_off, "Explicit hot-link has no title.");
      }
   else
      {
      ptr = cmd;
      l.type = 0;   /* type 0 = topic title */
      len = (int) (end - ptr);
      if (len == 0)
         {
         error(err_off, "Implicit hot-link has no title.");
         bad = 1;
         }
      l.name = dupstr(ptr,len+1);
      l.name[len] = '\0';
      }

   if ( !bad )
      {
      CHK_BUFFER(1+3*sizeof(int)+len+1)
      lnum = add_link(&l);
      *curr++ = CMD_LINK;
      setint(curr,lnum);
      curr += 3*sizeof(int);
      memcpy(curr, ptr, len);
      curr += len;
      *curr++ = CMD_LINK;
      return (len);
      }
   else
      return (0);
   }


#define MAX_TABLE_SIZE (100)


int create_table(void)
   {
   char  *ptr;
   int    width;
   int    cols;
   int    start_off;
   int    first_link;
   int    rows;
   int    r, c;
   int    ch;
   int    done;
   int    len;
   int    lnum;
   int    count;
   char  *title[MAX_TABLE_SIZE];
   char  *table_start;

   ptr = strchr(cmd, '=');

   if (ptr == NULL)
      return (0);   /* should never happen! */

   ptr++;

   len = sscanf(ptr, " %d %d %d", &width, &cols, &start_off);

   if (len < 3)
      {
      error(1,"Too few arguments to Table.");
      return (0);
      }

   if (width<=0 || width > 78 || cols<=0 || start_off<0 || start_off > 78)
      {
      error(1,"Argument out of range.");
      return (0);
      }

   done = 0;

   first_link = num_link;
   table_start = curr;
   count = 0;

   /* first, read all the links in the table */

   do
      {

      do
         ch = read_char();
      while ( ch=='\n' || ch == ' ' );

      if (done)
         break;

      switch (ch)
         {
         case -1:
            error(0,"Unexpected EOF in a Table.");
            return(0);

         case '{':
            if (count >= MAX_TABLE_SIZE)
               fatal(0,"Table is too large.");
            len = parse_link();
            curr = table_start;   /* reset to the start... */
            title[count] = dupstr(curr+3*sizeof(int)+1, len+1);
            if (len >= width)
               {
               warn(1,"Link is too long; truncating.");
               len = width-1;
               }
            title[count][len] = '\0';
            ++count;
            break;

         case '~':
            {
            int imbedded;

            ch = read_char();

            if (ch=='(')
               imbedded = 1;
            else
               {
               imbedded = 0;
               unread_char(ch);
               }

            ptr = read_until(cmd, 128, ")\n,");

            ch = *ptr;
            *ptr = '\0';

            if  ( stricmp(cmd, "EndTable") == 0 )
               done = 1;
            else
               {
               error(1,"Unexpected command in table \"%s\"", cmd);
               warn(1,"Command will be ignored.");
               }

            if (ch == ',')
               {
               if (imbedded)
                  unread_char('(');
               unread_char('~');
               }
            }
            break;

         default:
            error(0,"Unexpected character %s.", pchar(ch));
            break;
         }
      }
   while (!done);

   /* now, put all the links into the buffer... */

   rows = 1 + ( count / cols );

   for (r=0; r<rows; r++)
      {
      put_spaces(start_off);
      for (c=0; c<cols; c++)
         {
         lnum = c*rows + r;

         if ( first_link+lnum >= num_link )
            break;

         len = strlen(title[lnum]);
         *curr++ = CMD_LINK;
         setint(curr,first_link+lnum);
         curr += 3*sizeof(int);
         memcpy(curr, title[lnum], len);
         curr += len;
         *curr++ = CMD_LINK;

         delete(title[lnum]);

         if ( c < cols-1 )
            put_spaces( width-len );
         }
      *curr++ = '\n';
      }

   return (1);
   }


void process_comment(void)
   {
   int ch;

   while ( 1 )
      {
      ch = read_char();

      if (ch == '~')
         {
         int   imbedded;
         char *ptr;

         ch = read_char();

         if (ch=='(')
            imbedded = 1;
         else
            {
            imbedded = 0;
            unread_char(ch);
            }

         ptr = read_until(cmd, 128, ")\n,");

         ch = *ptr;
         *ptr = '\0';

         if  ( stricmp(cmd, "EndComment") == 0 )
            {
            if (ch == ',')
               {
               if (imbedded)
                  unread_char('(');
               unread_char('~');
               }
            break;
            }
         }

      else if ( ch == -1 )
         {
         error(0,"Unexpected EOF in Comment");
         break;
         }
      }
   }


void process_bininc(void)
   {
   int  handle;
   long len;

   if ( (handle=open(cmd+7, O_RDONLY|O_BINARY)) == -1 )
      {
      error(0,"Unable to open \"%s\"", cmd+7);
      return ;
      }

   len = filelength(handle);

   if ( len >= BUFFER_SIZE )
      {
      error(0,"File \"%s\" is too large to BinInc (%dK).", cmd+7, (int)(len>>10));
      close(handle);
      return ;
      }

   /*
    * Since we know len is less than BUFFER_SIZE (and therefore less then
    * 64K) we can treat it as an unsigned.
    */

   CHK_BUFFER((unsigned)len);

   read(handle, curr, (unsigned)len);

   curr += (unsigned)len;

   close(handle);
   }


void start_topic(TOPIC *t, char *title, int title_len)
   {
   t->flags = 0;
   t->title_len = title_len;
   t->title = dupstr(title, title_len+1);
   t->title[title_len] = '\0';
   t->doc_page = -1;
   t->num_page = 0;
   curr = buffer;
   }


void end_topic(TOPIC *t)
   {
   alloc_topic_text(t, (unsigned) (curr - buffer) );
   add_topic(t);
   }


int end_of_sentence(char *ptr)  /* true if ptr is at the end of a sentence */
   {
   if ( *ptr == ')')
      --ptr;

   if ( *ptr == '\"')
      --ptr;

   return ( *ptr=='.' || *ptr=='?' || *ptr=='!' );
   }


void add_blank_for_split(void)   /* add space at curr for merging two lines */
   {
   if ( !is_hyphen(curr-1) )   /* no spaces if it's a hyphen */
      {
      if ( end_of_sentence(curr-1) )
         *curr++ = ' ';  /* two spaces at end of a sentence */
      *curr++ = ' ';
      }
   }


void put_a_char(int ch, TOPIC *t)
   {
   if (ch == '{' && !(t->flags & TF_DATA) )   /* is it a hot-link? */
      parse_link();
   else
      {
      if ( (ch&0xFF) <= MAX_CMD)
         *curr++ = CMD_LITERAL;
      *curr++ = ch;
      }
   }


enum STATES   /* states for FSM's */
   {
   S_Start,                 /* initial state, between paragraphs           */
   S_StartFirstLine,        /* spaces at start of first line               */
   S_FirstLine,             /* text on the first line                      */
   S_FirstLineSpaces,       /* spaces on the first line                    */
   S_StartSecondLine,       /* spaces at start of second line              */
   S_Line,                  /* text on lines after the first               */
   S_LineSpaces,            /* spaces on lines after the first             */
   S_StartLine,             /* spaces at start of lines after second       */
   S_FormatDisabled,        /* format automatically disabled for this line */
   S_FormatDisabledSpaces,  /* spaces in line which format is disabled     */
   S_Spaces
   } ;


void check_command_length(int eoff, int len)
   {
   if (strlen(cmd) != len)
      error(eoff, "Invalid text after a command \"%s\"", cmd+len);
   }


void read_src(char *fname)
   {
   int    ch;
   char  *ptr;
   TOPIC  t;
   LABEL  lbl;
   char  *margin_pos = NULL;
   int    in_topic   = 0,
          formatting = 1,
          state      = S_Start,
          num_spaces = 0,
          margin     = 0,
          in_para    = 0,
          centering  = 0,
          lformat_exclude = format_exclude,
          again;

   xonline = xdoc = 0;

   src_cfname = fname;

   if ( (srcfile = fopen(fname, "rt")) == NULL )
      fatal(0,"Unable to open \"%s\"", fname);

   msg("Compiling: %s", fname);

   in_topic = 0;

   curr = buffer;

   while ( 1 )
      {

      ch = read_char();

      if ( ch == -1 )   /* EOF? */
         {
         if ( include_stack_top >= 0)
            {
            fclose(srcfile);
            src_cfname = include_stack[include_stack_top].fname;
            srcfile = include_stack[include_stack_top].file;
            srcline = include_stack[include_stack_top].line;
            srccol  = include_stack[include_stack_top].col;
            --include_stack_top;
            continue;
            }
         else
            {
            if (in_topic)  /* if we're in a topic, finish it */
               end_topic(&t);
            if (num_topic == 0)
               warn(0,".SRC file has no topics.");
            break;
            }
         }

      if (ch == '~')   /* is is a command? */
         {
         int imbedded;
         int eoff;
         int done;

         ch = read_char();
         if (ch == '(')
            {
            imbedded = 1;
            eoff = 0;
            }
         else
            {
            imbedded = 0;
            eoff=0;
            unread_char(ch);
            }

         done = 0;

         while ( !done )
            {
            do
               ch = read_char();
            while (ch == ' ');
            unread_char(ch);

            if (imbedded)
               ptr = read_until(cmd, 128, ")\n,");
            else
               ptr = read_until(cmd, 128, "\n,");

            done = 1;

            if ( *ptr == '\0' )
               {
               error(0,"Unexpected EOF in command.");
               break;
               }

            if (*ptr == '\n')
               ++eoff;

            if ( imbedded && *ptr == '\n' )
               error(eoff,"Imbedded command has no closing parend (\')\')");

            done = (*ptr != ',');   /* we done if it's not a comma */

            if ( *ptr != '\n' && *ptr != ')' && *ptr != ',' )
               {
               error(0,"Command line too long.");
               break;
               }

            *ptr = '\0';


            /* commands allowed anytime... */

            if ( strnicmp(cmd, "Topic=", 6) == 0 )
               {
               if (in_topic)  /* if we're in a topic, finish it */
                  end_topic(&t);
               else
                  in_topic = 1;

               if (cmd[6] == '\0')
                  warn(eoff,"Topic has no title.");

               else if ((int)strlen(cmd+6) > 70)
                  error(eoff,"Topic title is too long.");

               else if ((int)strlen(cmd+6) > 60)
                  warn(eoff,"Topic title is long.");

               if ( find_topic_title(cmd+6) != -1 )
                  error(eoff,"Topic title already exists.");

               start_topic(&t, cmd+6, (unsigned)(ptr-(cmd+6)));
               formatting = 1;
               centering = 0;
               state = S_Start;
               in_para = 0;
               num_spaces = 0;
               xonline = xdoc = 0;
               lformat_exclude = format_exclude;
               compress_spaces = 1;
               continue;
               }

            else if ( strnicmp(cmd, "Data=", 5) == 0 )
               {
               if (in_topic)  /* if we're in a topic, finish it */
                  end_topic(&t);
               else
                  in_topic = 1;

               if (cmd[5] == '\0')
                  warn(eoff,"Data topic has no label.");

               if ( !validate_label_name(cmd+5) )
                  {
                  error(eoff,"Label \"%s\" contains illegal characters.", cmd+5);
                  continue;
                  }

               if ( find_label(cmd+5) != NULL )
                  {
                  error(eoff,"Label \"%s\" already exists", cmd+5);
                  continue;
                  }

               if ( cmd[5] == '@' )
                  warn(eoff, "Data topic has a local label.");

               start_topic(&t, "", 0);
               t.flags |= TF_DATA;

               if ((int)strlen(cmd+5) > 32)
                  warn(eoff,"Label name is long.");

               lbl.name      = dupstr(cmd+5, 0);
               lbl.topic_num = num_topic;
               lbl.topic_off = 0;
               lbl.doc_page  = -1;
               add_label(&lbl);

               formatting = 0;
               centering = 0;
               state = S_Start;
               in_para = 0;
               num_spaces = 0;
               xonline = xdoc = 0;
               lformat_exclude = format_exclude;
               compress_spaces = 0;
               continue;
               }

            else if ( strnicmp(cmd, "DocContents", 11) == 0 )
               {
               check_command_length(eoff, 11);
               if (in_topic)  /* if we're in a topic, finish it */
                  end_topic(&t);
               if (!done)
                  {
                  if (imbedded)
                     unread_char('(');
                  unread_char('~');
                  done = 1;
                  }
               compress_spaces = 1;
               process_contents();
               in_topic = 0;
               continue;
               }

            else if ( stricmp(cmd, "Comment") == 0 )
               {
               process_comment();
               continue;
               }

            else if ( strnicmp(cmd, "FormatExclude", 13) == 0 )
               {
               if (cmd[13] == '-')
                  {
                  check_command_length(eoff, 14);
                  if ( in_topic )
                     {
                     if (lformat_exclude > 0)
                        lformat_exclude = -lformat_exclude;
                     else
                        warn(eoff,"\"FormatExclude-\" is already in effect.");
                     }
                  else
                     {
                     if (format_exclude > 0)
                        format_exclude = -format_exclude;
                     else
                        warn(eoff,"\"FormatExclude-\" is already in effect.");
                     }
                  }
               else if (cmd[13] == '+')
                  {
                  check_command_length(eoff,14);
                  if ( in_topic )
                     {
                     if (lformat_exclude < 0)
                        lformat_exclude = -lformat_exclude;
                     else
                        warn(eoff,"\"FormatExclude+\" is already in effect.");
                     }
                  else
                     {
                     if (format_exclude < 0)
                        format_exclude = -format_exclude;
                     else
                        warn(eoff,"\"FormatExclude+\" is already in effect.");
                     }
                  }
               else if (cmd[13] == '=')
                  {
                  if (cmd[14] == 'n' || cmd[14] == 'N')
                     {
                     check_command_length(eoff,15);
                     if (in_topic)
                        lformat_exclude = 0;
                     else
                       format_exclude = 0;
                     }
                  else if (cmd[14] == '\0')
                     lformat_exclude = format_exclude;
                  else
                     {
                     int n = ( ( (in_topic) ? lformat_exclude : format_exclude) < 0 ) ? -1 : 1;

                     lformat_exclude = atoi(cmd+14);

                     if ( lformat_exclude <= 0 )
                        {
                        error(eoff,"Invalid argument to FormatExclude=");
                        lformat_exclude = 0;
                        }

                     lformat_exclude *= n;

                     if ( !in_topic )
                        format_exclude = lformat_exclude;
                     }
                  }
               else
                  error(eoff,"Invalid format for FormatExclude");

               continue;
               }

            else if ( strnicmp(cmd, "Include ", 8) == 0 )
               {
               if (include_stack_top >= MAX_INCLUDE_STACK-1)
                  error(eoff, "Too many nested Includes.");
               else
                  {
                  ++include_stack_top;
                  include_stack[include_stack_top].fname = src_cfname;
                  include_stack[include_stack_top].file = srcfile;
                  include_stack[include_stack_top].line = srcline;
                  include_stack[include_stack_top].col  = srccol;
                  strupr(cmd+8);
                  if ( (srcfile = fopen(cmd+8, "rt")) == NULL )
                     {
                     error(eoff, "Unable to open \"%s\"", cmd+8);
                     srcfile = include_stack[include_stack_top--].file;
                     }
                  src_cfname = dupstr(cmd+8,0);  /* never deallocate! */
                  srcline = 1;
                  srccol = 0;
                  }

               continue;
               }


            /* commands allowed only before all topics... */

            if ( !in_topic )
               {
               if ( strnicmp(cmd, "HdrFile=", 8) == 0 )
                  {
                  if (hdr_fname[0] != '\0')
                     warn(eoff,"Header Filename has already been defined.");
                  strcpy(hdr_fname, cmd+8);
                  strupr(hdr_fname);
                  }

               else if ( strnicmp(cmd, "HlpFile=", 8) == 0 )
                  {
                  if (hlp_fname[0] != '\0')
                     warn(eoff,"Help Filename has already been defined.");
                  strcpy(hlp_fname, cmd+8);
                  strupr(hlp_fname);
                  }

               else if ( strnicmp(cmd, "Version=", 8) == 0 )
                  {
                  if (version != -1)   /* an unlikely value */
                     warn(eoff,"Help version has already been defined");
                  version = atoi(cmd+8);
                  }

               else
                  error(eoff,"Bad or unexpected command \"%s\"", cmd);

               continue;
               }


            /* commands allowed only in a topic... */

            else
               {
               if (strnicmp(cmd, "FF", 2) == 0 )
                  {
                  check_command_length(eoff,2);
                  if ( in_para )
                     *curr++ = '\n';  /* finish off current paragraph */
                  *curr++ = CMD_FF;
                  state = S_Start;
                  in_para = 0;
                  num_spaces = 0;
                  }

               else if (strnicmp(cmd, "DocFF", 5) == 0 )
                  {
                  check_command_length(eoff,5);
                  if ( in_para )
                     *curr++ = '\n';  /* finish off current paragraph */
                  if (!xonline)
                     *curr++ = CMD_XONLINE;
                  *curr++ = CMD_FF;
                  if (!xonline)
                     *curr++ = CMD_XONLINE;
                  state = S_Start;
                  in_para = 0;
                  num_spaces = 0;
                  }

               else if (strnicmp(cmd, "OnlineFF", 8) == 0 )
                  {
                  check_command_length(eoff,8);
                  if ( in_para )
                     *curr++ = '\n';  /* finish off current paragraph */
                  if (!xdoc)
                     *curr++ = CMD_XDOC;
                  *curr++ = CMD_FF;
                  if (!xdoc)
                     *curr++ = CMD_XDOC;
                  state = S_Start;
                  in_para = 0;
                  num_spaces = 0;
                  }

               else if ( strnicmp(cmd, "Label=", 6) == 0 )
                  {
                  if ((int)strlen(cmd+6) <= 0)
                     error(eoff,"Label has no name.");

                  else if ( !validate_label_name(cmd+6) )
                     error(eoff,"Label \"%s\" contains illegal characters.", cmd+6);

                  else if ( find_label(cmd+6) != NULL )
                     error(eoff,"Label \"%s\" already exists", cmd+6);

                  else
                     {
                     if ((int)strlen(cmd+6) > 32)
                        warn(eoff,"Label name is long.");

                    if ( (t.flags & TF_DATA) && cmd[6] == '@' )
                       warn(eoff, "Data topic has a local label.");

                     lbl.name      = dupstr(cmd+6, 0);
                     lbl.topic_num = num_topic;
                     lbl.topic_off = (unsigned)(curr - buffer);
                     lbl.doc_page  = -1;
                     add_label(&lbl);
                     }
                  }

               else if ( strnicmp(cmd, "Table=", 6) == 0 )
                  {
                  if ( in_para )
                     {
                     *curr++ = '\n';  /* finish off current paragraph */
                     in_para = 0;
                     num_spaces = 0;
                     state = S_Start;
                     }

                  if (!done)
                     {
                     if (imbedded)
                        unread_char('(');
                     unread_char('~');
                     done = 1;
                     }

                  create_table();
                  }

               else if ( strnicmp(cmd, "FormatExclude", 12) == 0 )
                  {
                  if (cmd[13] == '-')
                     {
                     check_command_length(eoff,14);
                     if (lformat_exclude > 0)
                        lformat_exclude = -lformat_exclude;
                     else
                        warn(0,"\"FormatExclude-\" is already in effect.");
                     }
                  else if (cmd[13] == '+')
                     {
                     check_command_length(eoff,14);
                     if (lformat_exclude < 0)
                        lformat_exclude = -lformat_exclude;
                     else
                        warn(0,"\"FormatExclude+\" is already in effect.");
                     }
                  else
                     error(eoff,"Unexpected or invalid argument to FormatExclude.");
                  }

               else if ( strnicmp(cmd, "Format", 6) == 0 )
                  {
                  if (cmd[6] == '+')
                     {
                     check_command_length(eoff,7);
                     if ( !formatting )
                        {
                        formatting = 1;
                        in_para = 0;
                        num_spaces = 0;
                        state = S_Start;
                        }
                     else
                        warn(eoff,"\"Format+\" is already in effect.");
                     }
                  else if (cmd[6] == '-')
                     {
                     check_command_length(eoff,7);
                     if ( formatting )
                        {
                        if ( in_para )
                           *curr++ = '\n';  /* finish off current paragraph */
                        state = S_Start;
                        in_para = 0;
                        formatting = 0;
                        num_spaces = 0;
                        state = S_Start;
                        }
                     else
                        warn(eoff,"\"Format-\" is already in effect.");
                     }
                  else
                     error(eoff,"Invalid argument to Format.");
                  }

               else if ( strnicmp(cmd, "Online", 6) == 0 )
                  {
                  if (cmd[6] == '+')
                     {
                     check_command_length(eoff,7);

                     if ( xonline )
                        {
                        *curr++ = CMD_XONLINE;
                        xonline = 0;
                        }
                     else
                        warn(eoff,"\"Online+\" already in effect.");
                     }
                  else if (cmd[6] == '-')
                     {
                     check_command_length(eoff,7);
                     if ( !xonline )
                        {
                        *curr++ = CMD_XONLINE;
                        xonline = 1;
                        }
                     else
                        warn(eoff,"\"Online-\" already in effect.");
                     }
                  else
                     error(eoff,"Invalid argument to Online.");
                  }

               else if ( strnicmp(cmd, "Doc", 3) == 0 )
                  {
                  if (cmd[3] == '+')
                     {
                     check_command_length(eoff,4);
                     if ( xdoc )
                        {
                        *curr++ = CMD_XDOC;
                        xdoc = 0;
                        }
                     else
                        warn(eoff,"\"Doc+\" already in effect.");
                     }
                  else if (cmd[3] == '-')
                     {
                     check_command_length(eoff,4);
                     if ( !xdoc )
                        {
                        *curr++ = CMD_XDOC;
                        xdoc = 1;
                        }
                     else
                        warn(eoff,"\"Doc-\" already in effect.");
                     }
                  else
                     error(eoff,"Invalid argument to Doc.");
                  }

               else if ( strnicmp(cmd, "Center", 6) == 0 )
                  {
                  if (cmd[6] == '+')
                     {
                     check_command_length(eoff,7);
                     if ( !centering )
                        {
                        centering = 1;
                        if ( in_para )
                           {
                           *curr++ = '\n';
                           in_para = 0;
                           }
                        state = S_Start;  /* for centering FSM */
                        }
                     else
                        warn(eoff,"\"Center+\" already in effect.");
                     }
                  else if (cmd[6] == '-')
                     {
                     check_command_length(eoff,7);
                     if ( centering )
                        {
                        centering = 0;
                        state = S_Start;  /* for centering FSM */
                        }
                     else
                        warn(eoff,"\"Center-\" already in effect.");
                     }
                  else
                     error(eoff,"Invalid argument to Center.");
                  }

               else if ( strnicmp(cmd, "CompressSpaces", 14) == 0 )
                  {
                  check_command_length(eoff,15);

                  if ( cmd[14] == '+' )
                     {
                     if ( compress_spaces )
                        warn(eoff,"\"CompressSpaces+\" is already in effect.");
                     else
                        compress_spaces = 1;
                     }
                  else if ( cmd[14] == '-' )
                     {
                     if ( !compress_spaces )
                        warn(eoff,"\"CompressSpaces-\" is already in effect.");
                     else
                        compress_spaces = 0;
                     }
                  else
                     error(eoff,"Invalid argument to CompressSpaces.");
                  }

               else if ( strnicmp("BinInc ", cmd, 7) == 0 )
                  {
                  if ( !(t.flags & TF_DATA) )
                     error(eoff,"BinInc allowed only in Data topics.");
                  else
                     process_bininc();
                  }

               else
                  error(eoff,"Bad or unexpected command \"%s\".", cmd);
               } /* else */

            } /* while (!done) */

         continue;
         }

      if ( !in_topic )
         {
         cmd[0] = ch;
         ptr = read_until(cmd+1, 127, "\n~");
         if (*ptr == '~')
            unread_char('~');
         *ptr = '\0';
         error(0,"Text outside of any topic \"%s\".", cmd);
         continue;
         }

      if ( centering )
         {
         do
            {
            again = 0;   /* default */

            switch (state)
               {
               case S_Start:
                  if (ch == ' ')
                     ; /* do nothing */
                  else if ( (ch&0xFF) == '\n' )
                     *curr++ = ch;  /* no need to center blank lines. */
                  else
                     {
                     *curr++ = CMD_CENTER;
                     state = S_Line;
                     again = 1;
                     }
                  break;

               case S_Line:
                  put_a_char(ch, &t);
                  if ( (ch&0xFF) == '\n')
                     state = S_Start;
                  break;
               } /* switch */
            }
         while (again);
         }

      else if ( formatting )
         {
         int again;

         do
            {
            again = 0;   /* default */

            switch (state)
               {
               case S_Start:
                  if ( (ch&0xFF) == '\n' )
                     *curr++ = ch;
                  else
                     {
                     state = S_StartFirstLine;
                     num_spaces = 0;
                     again = 1;
                     }
                  break;

               case S_StartFirstLine:
                  if ( ch == ' ')
                     ++num_spaces;

                  else
                     {
                     if (lformat_exclude > 0 && num_spaces >= lformat_exclude )
                        {
                        put_spaces(num_spaces);
                        num_spaces = 0;
                        state = S_FormatDisabled;
                        again = 1;
                        }
                     else
                        {
                        *curr++ = CMD_PARA;
                        *curr++ = (char)num_spaces;
                        *curr++ = (char)num_spaces;
                        margin_pos = curr - 1;
                        state = S_FirstLine;
                        again = 1;
                        in_para = 1;
                        }
                     }
                  break;

               case S_FirstLine:
                  if (ch == '\n')
                     {
                     state = S_StartSecondLine;
                     num_spaces = 0;
                     }
                  else if (ch == ('\n'|0x100) )   /* force end of para ? */
                     {
                     *curr++ = '\n';
                     in_para = 0;
                     state = S_Start;
                     }
                  else if ( ch == ' ' )
                     {
                     state = S_FirstLineSpaces;
                     num_spaces = 1;
                     }
                  else
                     put_a_char(ch, &t);
                  break;

               case S_FirstLineSpaces:
                  if (ch == ' ')
                     ++num_spaces;
                  else
                     {
                     put_spaces(num_spaces);
                     state = S_FirstLine;
                     again = 1;
                     }
                  break;

               case S_StartSecondLine:
                  if ( ch == ' ')
                     ++num_spaces;
                  else if ((ch&0xFF) == '\n') /* a blank line means end of a para */
                     {
                     *curr++ = '\n';   /* end the para */
                     *curr++ = '\n';   /* for the blank line */
                     in_para = 0;
                     state = S_Start;
                     }
                  else
                     {
                     if (lformat_exclude > 0 && num_spaces >= lformat_exclude )
                        {
                        *curr++ = '\n';
                        in_para = 0;
                        put_spaces(num_spaces);
                        num_spaces = 0;
                        state = S_FormatDisabled;
                        again = 1;
                        }
                     else
                        {
                        add_blank_for_split();
                        margin = num_spaces;
                        *margin_pos = (char)num_spaces;
                        state = S_Line;
                        again = 1;
                        }
                     }
                  break;

               case S_Line:   /* all lines after the first */
                  if (ch == '\n')
                     {
                     state = S_StartLine;
                     num_spaces = 0;
                     }
                  else if (ch == ('\n' | 0x100) )   /* force end of para ? */
                     {
                     *curr++ = '\n';
                     in_para = 0;
                     state = S_Start;
                     }
                  else if ( ch == ' ' )
                     {
                     state = S_LineSpaces;
                     num_spaces = 1;
                     }
                  else
                     put_a_char(ch, &t);
                  break;

               case S_LineSpaces:
                  if (ch == ' ')
                     ++num_spaces;
                  else
                     {
                     put_spaces(num_spaces);
                     state = S_Line;
                     again = 1;
                     }
                  break;

               case S_StartLine:   /* for all lines after the second */
                  if ( ch == ' ')
                     ++num_spaces;
                  else if ((ch&0xFF) == '\n') /* a blank line means end of a para */
                     {
                     *curr++ = '\n';   /* end the para */
                     *curr++ = '\n';   /* for the blank line */
                     in_para = 0;
                     state = S_Start;
                     }
                  else
                     {
                     if ( num_spaces != margin )
                        {
                        *curr++ = '\n';
                        in_para = 0;
                        state = S_StartFirstLine;  /* with current num_spaces */
                        again = 1;
                        }
                     else
                        {
                        add_blank_for_split();
                        state = S_Line;
                        again = 1;
                        }
                     }
                  break;

               case S_FormatDisabled:
                  if ( ch == ' ' )
                     {
                     state = S_FormatDisabledSpaces;
                     num_spaces = 1;
                     }
                  else
                     {
                     if ( (ch&0xFF) == '\n' )
                        state = S_Start;
                     put_a_char(ch, &t);
                     }
                  break;

               case S_FormatDisabledSpaces:
                  if ( ch == ' ' )
                     ++num_spaces;
                  else
                     {
                     put_spaces(num_spaces);
                     num_spaces = 0;    /* is this needed? */
                     state = S_FormatDisabled;
                     again = 1;
                     }
                  break;

               } /* switch (state) */
            }
         while (again);
         }

      else
         {
         do
            {
            again = 0;   /* default */

            switch (state)
               {
               case S_Start:
                  if ( ch == ' ' )
                     {
                     state = S_Spaces;
                     num_spaces = 1;
                     }
                  else
                     put_a_char(ch, &t);
                  break;

               case S_Spaces:
                  if (ch == ' ')
                     ++num_spaces;
                  else
                     {
                     put_spaces(num_spaces);
                     num_spaces = 0;     /* is this needed? */
                     state = S_Start;
                     again = 1;
                     }
                  break;
               } /* switch */
            }
         while (again);
         }

      CHK_BUFFER(0)
      } /* while ( 1 ) */

   fclose(srcfile);

   srcline = -1;
   }


/*
 * stuff to resolve hot-link references.
 */


void make_hot_links(void)
   /*
    * calculate topic_num/topic_off for each link.
    */
   {
   LINK    *l;
   LABEL   *lbl;
   int      lctr;
   int      t;
   CONTENT *c;
   int      ctr;

   msg("Making hot-links.");

   /*
    * Calculate topic_num for all entries in DocContents.  Also set
    * "TF_IN_DOC" flag for all topics included in the document.
    */

   for (lctr=0, c=contents; lctr<num_contents; lctr++, c++)
      {
      for (ctr=0; ctr<c->num_topic; ctr++)
         {
         if ( c->is_label[ctr] )
            {
            lbl = find_label(c->topic_name[ctr]);
            if (lbl == NULL)
               {
               src_cfname = c->srcfile;
               srcline = c->srcline;
               error(0,"Cannot find DocContent label \"%s\".", c->topic_name[ctr]);
               srcline = -1;
               }
            else
               {
               if ( topic[lbl->topic_num].flags & TF_DATA )
                  {
                  src_cfname = c->srcfile;
                  srcline = c->srcline;
                  error(0,"Label \"%s\" is a data-only topic.", c->topic_name[ctr]);
                  srcline = -1;
                  }
               else
                  {
                  c->topic_num[ctr] = lbl->topic_num;
                  if ( topic[lbl->topic_num].flags & TF_IN_DOC )
                     warn(0,"Topic \"%s\" appears in document more than once.",
                          topic[lbl->topic_num].title);
                  else
                     topic[lbl->topic_num].flags |= TF_IN_DOC;
                  }
               }

            }
         else
            {
            t = find_topic_title(c->topic_name[ctr]);

            if (t == -1)
               {
               src_cfname = c->srcfile;
               srcline = c->srcline;
               error(0,"Cannot find DocContent topic \"%s\".", c->topic_name[ctr]);
               srcline = -1;  /* back to reality */
               }
            else
               {
               c->topic_num[ctr] = t;
               if ( topic[t].flags & TF_IN_DOC )
                  warn(0,"Topic \"%s\" appears in document more than once.",
                       topic[t].title);
               else
                  topic[t].flags |= TF_IN_DOC;
               }
            }
         }
      }

   /*
    * Find topic_num and topic_off for all hot-links.  Also flag all hot-
    * links which will (probably) appear in the document.
    */

   for (lctr=0, l=a_link; lctr<num_link; l++, lctr++)
      {
      switch ( l->type )
         {
         case 0:      /* name is the title of the topic */
            t = find_topic_title(l->name);
            if (t == -1)
               {
               src_cfname = l->srcfile;
               srcline = l->srcline; /* pretend we are still in the source... */
               error(0,"Cannot find implicit hot-link \"%s\".", l->name);
               srcline = -1;  /* back to reality */
               }
            else
               {
               l->topic_num = t;
               l->topic_off = 0;
               l->doc_page = (topic[t].flags & TF_IN_DOC) ? 0 : -1;
               }
            break;

         case 1:  /* name is the name of a label */
            lbl = find_label(l->name);
            if (lbl == NULL)
               {
               src_cfname = l->srcfile;
               srcline = l->srcline; /* pretend again */
               error(0,"Cannot find explicit hot-link \"%s\".", l->name);
               srcline = -1;
               }
            else
               {
               if ( topic[lbl->topic_num].flags & TF_DATA )
                  {
                  src_cfname = l->srcfile;
                  srcline = l->srcline;
                  error(0,"Label \"%s\" is a data-only topic.", l->name);
                  srcline = -1;
                  }
               else
                  {
                  l->topic_num = lbl->topic_num;
                  l->topic_off = lbl->topic_off;
                  l->doc_page  = (topic[lbl->topic_num].flags & TF_IN_DOC) ? 0 : -1;
                  }
               }
            break;

         case 2:   /* it's a "special" link; topic_off already has the value */
            break;
         }
      }

   }


/*
 * online help pagination stuff
 */


void add_page_break(TOPIC *t, int margin, char *text, char *start, char *curr, int num_links)
   {
   PAGE p;

   p.offset = (unsigned) (start - text);
   p.length = (unsigned) (curr - start);
   p.margin = margin;
   add_page(t, &p);

   if (max_links < num_links)
      max_links = num_links;
   }


void paginate_online(void)    /* paginate the text for on-line help */
   {                   /* also calculates max_pages and max_links */
   int       lnum;
   char     *start;
   char     *curr;
   char     *text;
   TOPIC    *t;
   int       tctr;
   unsigned  len;
   int       skip_blanks;
   int       num_links;
   int       col;
   int       tok;
   int       size,
             width;
   int       start_margin;

   msg("Paginating online help.");

   for (t=topic, tctr=0; tctr<num_topic; t++, tctr++)
      {
      if ( t->flags & TF_DATA )
         continue;    /* don't paginate data topics */

      text = get_topic_text(t);
      curr = text;
      len  = t->text_len;

      start = curr;
      skip_blanks = 0;
      lnum = 0;
      num_links = 0;
      col = 0;
      start_margin = -1;

      while (len > 0)
         {
         tok = find_token_length(ONLINE, curr, len, &size, &width);

         switch ( tok )
            {
            case TOK_PARA:
               {
               int indent,
                   margin;

               ++curr;

               indent = *curr++;
               margin = *curr++;

               len -= 3;

               col = indent;

               while (1)
                  {
                  tok = find_token_length(ONLINE, curr, len, &size, &width);

                  if (tok == TOK_DONE || tok == TOK_NL || tok == TOK_FF )
                     break;

                  if ( tok == TOK_PARA )
                     {
                     col = 0;   /* fake a nl */
                     ++lnum;
                     break;
                     }

                  if (tok == TOK_XONLINE || tok == TOK_XDOC )
                     {
                     curr += size;
                     len -= size;
                     continue;
                     }

                  /* now tok is TOK_SPACE or TOK_LINK or TOK_WORD */

                  if (col+width > SCREEN_WIDTH)
                     {          /* go to next line... */
                     if ( ++lnum >= SCREEN_DEPTH )
                        {           /* go to next page... */
                        add_page_break(t, start_margin, text, start, curr, num_links);
                        start = curr + ( (tok == TOK_SPACE) ? size : 0 );
                        start_margin = margin;
                        lnum = 0;
                        num_links = 0;
                        }
                     if ( tok == TOK_SPACE )
                        width = 0;   /* skip spaces at start of a line */

                     col = margin;
                     }

                  col += width;
                  curr += size;
                  len -= size;
                  }

               skip_blanks = 0;
               width = size = 0;
               break;
               }

            case TOK_NL:
               if (skip_blanks && col == 0)
                  {
                  start += size;
                  break;
                  }
               ++lnum;
               if ( lnum >= SCREEN_DEPTH || (col == 0 && lnum==SCREEN_DEPTH-1) )
                  {
                  add_page_break(t, start_margin, text, start, curr, num_links);
                  start = curr + size;
                  start_margin = -1;
                  lnum = 0;
                  num_links = 0;
                  skip_blanks = 1;
                  }
               col = 0;
               break;

            case TOK_FF:
               col = 0;
               if (skip_blanks)
                  {
                  start += size;
                  break;
                  }
               add_page_break(t, start_margin, text, start, curr, num_links);
               start_margin = -1;
               start = curr + size;
               lnum = 0;
               num_links = 0;
               break;

            case TOK_DONE:
            case TOK_XONLINE:   /* skip */
            case TOK_XDOC:      /* ignore */
            case TOK_CENTER:    /* ignore */
               break;

            case TOK_LINK:
               ++num_links;

               /* fall-through */

            default:    /* TOK_SPACE, TOK_LINK, TOK_WORD */
               skip_blanks = 0;
               break;

            } /* switch */

         curr += size;
         len  -= size;
         col  += width;
         } /* while */

      if (!skip_blanks)
         add_page_break(t, start_margin, text, start, curr, num_links);

      if (max_pages < t->num_page)
         max_pages = t->num_page;

      release_topic_text(t, 0);
      } /* for */
   }


/*
 * paginate document stuff
 */


#define CNUM           0
#define TNUM           1
#define LINK_DEST_WARN 2


typedef struct
   {
   int      cnum,  /* must match above #defines so pd_get_info() will work */
            tnum,
            link_dest_warn;

   char far *start;
   CONTENT  *c;
   LABEL    *lbl;

   } PAGINATE_DOC_INFO;


LABEL *find_next_label_by_topic(int t)
   {
   LABEL *temp, *g, *p;
   int    ctr;

   g = p = NULL;

   for (temp=label, ctr=0; ctr<num_label; ctr++, temp++)
      if ( temp->topic_num == t && temp->doc_page == -1 )
         {
         g = temp;
         break;
         }
      else if (temp->topic_num > t)
         break;

   for (temp=plabel, ctr=0; ctr<num_plabel; ctr++, temp++)
      if ( temp->topic_num == t && temp->doc_page == -1 )
         {
         p = temp;
         break;
         }
      else if (temp->topic_num > t)
         break;

   if ( p == NULL )
      return (g);

   else if ( g == NULL )
      return (p);

   else
      return ( (g->topic_off < p->topic_off) ? g : p );
   }


void set_hot_link_doc_page(void)
   /*
    * Find doc_page for all hot-links.
    */
   {
   LINK  *l;
   LABEL *lbl;
   int    lctr;
   int    t;

   for (lctr=0, l=a_link; lctr<num_link; l++, lctr++)
      {
      switch ( l->type )
         {
         case 0:      /* name is the title of the topic */
            t = find_topic_title(l->name);
            if (t == -1)
               {
               src_cfname = l->srcfile;
               srcline = l->srcline; /* pretend we are still in the source... */
               error(0,"Cannot find implicit hot-link \"%s\".", l->name);
               srcline = -1;  /* back to reality */
               }
            else
               l->doc_page = topic[t].doc_page;
            break;

         case 1:  /* name is the name of a label */
            lbl = find_label(l->name);
            if (lbl == NULL)
               {
               src_cfname = l->srcfile;
               srcline = l->srcline; /* pretend again */
               error(0,"Cannot find explicit hot-link \"%s\".", l->name);
               srcline = -1;
               }
            else
               l->doc_page = lbl->doc_page;
            break;

         case 2:   /* special topics don't appear in the document */
            break;
         }
      }
   }


void set_content_doc_page(void)
   /*
    * insert page #'s in the DocContents
    */
   {
   CONTENT *c;
   TOPIC   *t;
   char    *base;
   int      tnum;
   int      ctr;
   char     buf[4];
   int      len;

   tnum = find_topic_title(DOCCONTENTS_TITLE);
   assert(tnum>=0);
   t = &topic[tnum];

   base = get_topic_text(t);

   for (ctr=1, c=contents+1; ctr<num_contents; ctr++, c++)
      {
      assert(c->doc_page>=1);
      sprintf(buf, "%d", c->doc_page);
      len = strlen(buf);
      assert(len<=3);
      memcpy(base+c->page_num_pos+(3-len), buf, len);
      }

   release_topic_text(t, 1);
   }


int pd_get_info(int cmd, PD_INFO *pd, int *info)
   {             /* this funtion also used by print_document() */
   CONTENT *c;

   switch (cmd)
      {
      case PD_GET_CONTENT:
         if ( ++info[CNUM] >= num_contents )
            return (0);
         c = &contents[info[CNUM]];
         info[TNUM] = -1;
         pd->id       = c->id;
         pd->title    = c->name;
         pd->new_page = (c->flags & CF_NEW_PAGE) ? 1 : 0;
         return (1);

      case PD_GET_TOPIC:
         c = &contents[info[CNUM]];
         if ( ++info[TNUM] >= c->num_topic )
            return (0);
         pd->curr = get_topic_text( &topic[c->topic_num[info[TNUM]]] );
         pd->len = topic[c->topic_num[info[TNUM]]].text_len;
         return (1);

      case PD_GET_LINK_PAGE:
         if ( a_link[getint(pd->s)].doc_page == -1 )
            {
            if ( info[LINK_DEST_WARN] )
               {
               src_cfname = a_link[getint(pd->s)].srcfile;
               srcline    = a_link[getint(pd->s)].srcline;
               warn(0,"Hot-link destination is not in the document.");
               srcline = -1;
               }
            return (0);
            }
         pd->i = a_link[getint(pd->s)].doc_page;
         return (1);

      case PD_RELEASE_TOPIC:
         c = &contents[info[CNUM]];
         release_topic_text(&topic[c->topic_num[info[TNUM]]], 0);
         return (1);

      default:
         return (0);
      }
   }


int paginate_doc_output(int cmd, PD_INFO *pd, PAGINATE_DOC_INFO *info)
   {
   switch (cmd)
      {
      case PD_FOOTING:
      case PD_PRINT:
      case PD_PRINTN:
      case PD_PRINT_SEC:
         return (1);

      case PD_HEADING:
         ++num_doc_pages;
         return (1);

      case PD_START_SECTION:
         info->c = &contents[info->cnum];
         return (1);

      case PD_START_TOPIC:
         info->start = pd->curr;
         info->lbl = find_next_label_by_topic(info->c->topic_num[info->tnum]);
         return (1);

      case PD_SET_SECTION_PAGE:
         info->c->doc_page = pd->pnum;
         return (1);

      case PD_SET_TOPIC_PAGE:
         topic[info->c->topic_num[info->tnum]].doc_page = pd->pnum;
         return (1);

      case PD_PERIODIC:
         while ( info->lbl != NULL && (unsigned)(pd->curr - info->start) >= info->lbl->topic_off)
            {
            info->lbl->doc_page = pd->pnum;
            info->lbl = find_next_label_by_topic(info->c->topic_num[info->tnum]);
            }
         return (1);

      default:
         return (0);
      }
   }


void paginate_document(void)
   {
   PAGINATE_DOC_INFO info;

   if (num_contents == 0)
      return ;

   msg("Paginating document.");

   info.cnum = info.tnum = -1;
   info.link_dest_warn = 1;

   process_document((PD_FUNC)pd_get_info, (PD_FUNC)paginate_doc_output, &info);

   set_hot_link_doc_page();
   set_content_doc_page();
   }


/*
 * label sorting stuff
 */

int fcmp_LABEL(VOIDCONSTPTR a, VOIDCONSTPTR b)
   {
   char *an = ((LABEL *)a)->name,
        *bn = ((LABEL *)b)->name;
   int   diff;

   /* compare the names, making sure that the index goes first */

   if ( (diff=strcmp(an,bn)) == 0 )
      return (0);

   if ( strcmp(an, INDEX_LABEL) == 0 )
      return (-1);

   if ( strcmp(bn, INDEX_LABEL) == 0 )
      return (1);

   return ( diff );
   }


void sort_labels(void)
   {
   qsort(label,  num_label,  sizeof(LABEL), fcmp_LABEL);
   qsort(plabel, num_plabel, sizeof(LABEL), fcmp_LABEL);
   }


/*
 * file write stuff.
 */


int compare_files(FILE *f1, FILE *f2) /* returns TRUE if different */
   {
   if ( filelength(fileno(f1)) != filelength(fileno(f2)) )
      return (1);   /* different if sizes are not the same */

   while ( !feof(f1) && !feof(f2) )
      if ( getc(f1) != getc(f2) )
         return (1);

   return ( ( feof(f1) && feof(f2) ) ? 0 : 1);
   }


void _write_hdr(char *fname, FILE *file)
   {
   int ctr;
   char nfile[MAXFILE],
        next[MAXEXT];

   FNSPLIT(fname, NULL, NULL, nfile, next);
   fprintf(file, "\n/*\n * %s%s\n", nfile, next);
   FNSPLIT(src_fname, NULL, NULL, nfile, next);
   fprintf(file, " *\n * Contains #defines for help.\n *\n");
   fprintf(file, " * Generated by HC from: %s%s\n *\n */\n\n\n", nfile, next);

   fprintf(file, "/* current help file version */\n");
   fprintf(file, "\n");
   fprintf(file, "#define %-32s %3d\n", "HELP_VERSION", version);
   fprintf(file, "\n\n");

   fprintf(file, "/* labels */\n\n");

   for (ctr=0; ctr<num_label; ctr++)
      if (label[ctr].name[0] != '@')  /* if it's not a local label... */
         {
         fprintf(file, "#define %-32s %3d", label[ctr].name, ctr);
         if ( strcmp(label[ctr].name, INDEX_LABEL) == 0 )
            fprintf(file, "        /* index */");
         fprintf(file, "\n");
         }

   fprintf(file, "\n\n");
   }


void write_hdr(char *fname)
   {
   FILE *temp,
        *hdr;

   hdr = fopen(fname, "rt");

   if (hdr == NULL)
      {         /* if no prev. hdr file generate a new one */
      hdr = fopen(fname, "wt");
      if (hdr == NULL)
         fatal(0,"Cannot create \"%s\".", fname);
      msg("Writing: %s", fname);
      _write_hdr(fname, hdr);
      fclose(hdr);
      notice("FRACTINT must be re-compiled.");
      return ;
      }

   msg("Comparing: %s", fname);

   temp = fopen(TEMP_FNAME, "wt");

   if (temp == NULL)
      fatal(0,"Cannot create temporary file: \"%s\".", TEMP_FNAME);

   _write_hdr(fname, temp);

   fclose(temp);
   temp = fopen(TEMP_FNAME, "rt");

   if (temp == NULL)
      fatal(0,"Cannot open temporary file: \"%s\".", TEMP_FNAME);

   if ( compare_files(temp, hdr) )   /* if they are different... */
      {
      msg("Updating: %s", fname);
      fclose(temp);
      fclose(hdr);
      unlink(fname);               /* delete the old hdr file */
      rename(TEMP_FNAME, fname);   /* rename the temp to the hdr file */
      notice("FRACTINT must be re-compiled.");
      }
   else
      {   /* if they are the same leave the original alone. */
      fclose(temp);
      fclose(hdr);
      unlink(TEMP_FNAME);      /* delete the temp */
      }
   }


void calc_offsets(void)    /* calc file offset to each topic */
   {
   int      t;
   TOPIC   *tp;
   long     offset;
   CONTENT *cp;
   int      c;

   /* NOTE: offsets do NOT include 6 bytes for signature & version! */

   offset = sizeof(int) +           /* max_pages */
            sizeof(int) +           /* max_links */
            sizeof(int) +           /* num_topic */
            sizeof(int) +           /* num_label */
            sizeof(int) +           /* num_contents */
            sizeof(int) +           /* num_doc_pages */
            num_topic*sizeof(long) +/* offsets to each topic */
            num_label*2*sizeof(int);/* topic_num/topic_off for all public labels */

   for (c=0, cp=contents; c<num_contents; c++, cp++)
      offset += sizeof(int) +       /* flags */
                1 +                 /* id length */
                strlen(cp->id) +    /* id text */
                1 +                 /* name length */
                strlen(cp->name) +  /* name text */
                1 +                 /* number of topics */
                cp->num_topic*sizeof(int);    /* topic numbers */

   for (t=0, tp=topic; t<num_topic; t++, tp++)
      {
      tp->offset = offset;
      offset += (long)sizeof(int) + /* topic flags */
                sizeof(int) +       /* number of pages */
                tp->num_page*3*sizeof(int) +   /* page offset, length & starting margin */
                1 +                 /* length of title */
                tp->title_len +     /* title */
                sizeof(int) +       /* length of text */
                tp->text_len;       /* text */
      }

   }


void insert_real_link_info(char *curr, unsigned len)
   /*
    * Replaces link indexes in the help text with topic_num, topic_off and
    * doc_page info.
    */
   {
   int       size;
   int       tok;
   LINK     *l;

   while (len > 0)
      {
      tok = find_token_length(0, curr, len, &size, NULL);

      if ( tok == TOK_LINK )
         {
         l = &a_link[ getint(curr+1) ];
         setint(curr+1,l->topic_num);
         setint(curr+1+sizeof(int),l->topic_off);
         setint(curr+1+2*sizeof(int),l->doc_page);
         }

      len -= size;
      curr += size;
      }
   }


void _write_help(FILE *file)
   {
   int                   t, p, l, c;
   char                 *text;
   TOPIC                *tp;
   CONTENT              *cp;
   struct help_sig_info  hs;

   /* write the signature and version */

   hs.sig = HELP_SIG; /* Edit line 17 of helpcom.h if this is a syntax error */
   hs.version = version;

   fwrite(&hs, sizeof(long)+sizeof(int), 1, file);

   /* write max_pages & max_links */

   putw(max_pages, file);
   putw(max_links, file);

   /* write num_topic, num_label and num_contents */

   putw(num_topic, file);
   putw(num_label, file);
   putw(num_contents, file);

   /* write num_doc_page */

   putw(num_doc_pages, file);

   /* write the offsets to each topic */

   for (t=0; t<num_topic; t++)
      fwrite(&topic[t].offset, sizeof(long), 1, file);

   /* write all public labels */

   for (l=0; l<num_label; l++)
      {
      putw(label[l].topic_num, file);
      putw(label[l].topic_off, file);
      }

   /* write contents */

   for (c=0, cp=contents; c<num_contents; c++, cp++)
      {
      putw(cp->flags, file);

      t = strlen(cp->id);
      putc((BYTE)t, file);
      fwrite(cp->id, 1, t, file);

      t = strlen(cp->name);
      putc((BYTE)t, file);
      fwrite(cp->name, 1, t, file);

      putc((BYTE)cp->num_topic, file);
      fwrite(cp->topic_num, sizeof(int), cp->num_topic, file);
      }

   /* write topics */

   for (t=0, tp=topic; t<num_topic; t++, tp++)
      {
      /* write the topics flags */

      putw(tp->flags, file);

      /* write offset, length and starting margin for each page */

      putw(tp->num_page, file);
      for (p=0; p<tp->num_page; p++)
         {
         putw(tp->page[p].offset, file);
         putw(tp->page[p].length, file);
         putw(tp->page[p].margin, file);
         }

      /* write the help title */

      putc((BYTE)tp->title_len, file);
      fwrite(tp->title, 1, tp->title_len, file);

      /* insert hot-link info & write the help text */

      text = get_topic_text(tp);

      if ( !(tp->flags & TF_DATA) )   /* don't process data topics... */
         insert_real_link_info(text, tp->text_len);

      putw(tp->text_len, file);
      fwrite(text, 1, tp->text_len, file);

      release_topic_text(tp, 0);  /* don't save the text even though        */
                                  /* insert_real_link_info() modified it    */
                                  /* because we don't access the info after */
                                  /* this.                                  */

      }
   }


void write_help(char *fname)
   {
   FILE *hlp;

   hlp = fopen(fname, "wb");

   if (hlp == NULL)
      fatal(0,"Cannot create .HLP file: \"%s\".", fname);

   msg("Writing: %s", fname);

   _write_help(hlp);

   fclose(hlp);
   }


/*
 * print document stuff.
 */


typedef struct
   {

   /*
    * Note: Don't move these first three or pd_get_info will work not
    *       correctly.
    */

   int      cnum;
   int      tnum;
   int      link_dest_warn;   /* = 0 */

   FILE    *file;
   int      margin;
   int      start_of_line;
   int      spaces;
   } PRINT_DOC_INFO;


void printerc(PRINT_DOC_INFO *info, int c, int n)
   {
   while ( n-- > 0 )
      {
      if (c==' ')
         ++info->spaces;

      else if (c=='\n' || c=='\f')
         {
         info->start_of_line = 1;
         info->spaces = 0;   /* strip spaces before a new-line */
         putc(c, info->file);
         }

      else
         {
         if (info->start_of_line)
            {
            info->spaces += info->margin;
            info->start_of_line = 0;
            }

         while (info->spaces > 0)
            {
            fputc(' ', info->file);
            --info->spaces;
            }

         fputc(c, info->file);
         }
      }
   }


void printers(PRINT_DOC_INFO *info, char far *s, int n)
   {
   if (n > 0)
      {
      while ( n-- > 0 )
         printerc(info, *s++, 1);
      }
   else
      {
      while ( *s != '\0' )
         printerc(info, *s++, 1);
      }
   }


int print_doc_output(int cmd, PD_INFO *pd, PRINT_DOC_INFO *info)
   {
   switch (cmd)
      {
      case PD_HEADING:
         {
         char buff[20];

         info->margin = 0;
         printers(info, "\n                     Fractint Version xx.xx                     Page ", 0);
         sprintf(buff, "%d\n\n", pd->pnum);
         printers(info, buff, 0);
         info->margin = PAGE_INDENT;
         return (1);
         }

      case PD_FOOTING:
         info->margin = 0;
         printerc(info, '\f', 1);
         info->margin = PAGE_INDENT;
         return (1);

      case PD_PRINT:
         printers(info, pd->s, pd->i);
         return (1);

      case PD_PRINTN:
         printerc(info, *pd->s, pd->i);
         return (1);

      case PD_PRINT_SEC:
         info->margin = TITLE_INDENT;
         if (pd->id[0] != '\0')
            {
            printers(info, pd->id, 0);
            printerc(info, ' ', 1);
            }
         printers(info, pd->title, 0);
         printerc(info, '\n', 1);
         info->margin = PAGE_INDENT;
         return (1);

      case PD_START_SECTION:
      case PD_START_TOPIC:
      case PD_SET_SECTION_PAGE:
      case PD_SET_TOPIC_PAGE:
      case PD_PERIODIC:
         return (1);

      default:
         return (0);
      }
   }


void print_document(char *fname)
   {
   PRINT_DOC_INFO info;

   if (num_contents == 0)
      fatal(0,".SRC has no DocContents.");

   msg("Printing to: %s", fname);

   info.cnum = info.tnum = -1;
   info.link_dest_warn = 0;

   if ( (info.file = fopen(fname, "wt")) == NULL )
      fatal(0,"Couldn't create \"%s\"", fname);

   info.margin = PAGE_INDENT;
   info.start_of_line = 1;
   info.spaces = 0;

   process_document((PD_FUNC)pd_get_info, (PD_FUNC)print_doc_output, &info);

   fclose(info.file);
   }


/*
 * compiler status and memory usage report stuff.
 */


void report_memory(void)
   {
   long string = 0,   /* bytes in strings */
        text   = 0,   /* bytes in topic text (stored on disk) */
        data   = 0,   /* bytes in active data structure */
        dead   = 0;   /* bytes in unused data structure */
   int  ctr, ctr2;

   for (ctr=0; ctr<num_topic; ctr++)
      {
      data   += sizeof(TOPIC);
      string += topic[ctr].title_len;
      text   += topic[ctr].text_len;
      data   += topic[ctr].num_page * sizeof(PAGE);

      dead   += (PAGE_ALLOC_SIZE-(topic[ctr].num_page%PAGE_ALLOC_SIZE)) * sizeof(PAGE);
      }

   for (ctr=0; ctr<num_link; ctr++)
      {
      data += sizeof(LINK);
      string += strlen(a_link[ctr].name);
      }

   if (num_link > 0)
      dead += (LINK_ALLOC_SIZE-(num_link%LINK_ALLOC_SIZE)) * sizeof(LINK);

   for (ctr=0; ctr<num_label; ctr++)
      {
      data   += sizeof(LABEL);
      string += strlen(label[ctr].name) + 1;
      }

   if (num_label > 0)
      dead += (LABEL_ALLOC_SIZE-(num_label%LABEL_ALLOC_SIZE)) * sizeof(LABEL);

   for (ctr=0; ctr<num_plabel; ctr++)
      {
      data   += sizeof(LABEL);
      string += strlen(plabel[ctr].name) + 1;
      }

   if (num_plabel > 0)
      dead += (LABEL_ALLOC_SIZE-(num_plabel%LABEL_ALLOC_SIZE)) * sizeof(LABEL);

   for (ctr=0; ctr<num_contents; ctr++)
      {
      int t;

      t = ( MAX_CONTENT_TOPIC - contents[ctr].num_topic ) *
          ( sizeof(contents[0].is_label[0])   +
            sizeof(contents[0].topic_name[0]) +
            sizeof(contents[0].topic_num[0])     );
      data += sizeof(CONTENT) - t;
      dead += t;
      string += strlen(contents[ctr].id) + 1;
      string += strlen(contents[ctr].name) + 1;
      for (ctr2=0; ctr2<contents[ctr].num_topic; ctr2++)
         string += strlen(contents[ctr].topic_name[ctr2]) + 1;
      }

   dead += (CONTENTS_ALLOC_SIZE-(num_contents%CONTENTS_ALLOC_SIZE)) * sizeof(CONTENT);

   printf("\n");
   printf("Memory Usage:\n");
   printf("%8ld Bytes in buffers.\n", (long)BUFFER_SIZE);
   printf("%8ld Bytes in strings.\n", string);
   printf("%8ld Bytes in data.\n", data);
   printf("%8ld Bytes in dead space.\n", dead);
   printf("--------\n");
   printf("%8ld Bytes total.\n", (long)BUFFER_SIZE+string+data+dead);
   printf("\n");
   printf("Disk Usage:\n");
   printf("%8ld Bytes in topic text.\n", text);
   }


void report_stats(void)
   {
   int  pages = 0;
   int      t;

   for (t=0; t<num_topic; t++)
      pages += topic[t].num_page;

   printf("\n");
   printf("Statistics:\n");
   printf("%8d Topics\n", num_topic);
   printf("%8d Links\n", num_link);
   printf("%8d Labels\n", num_label);
   printf("%8d Private labels\n", num_plabel);
   printf("%8d Table of contents (DocContent) entries\n", num_contents);
   printf("%8d Online help pages\n", pages);
   printf("%8d Document pages\n", num_doc_pages);
   }


/*
 * add/delete help from .EXE functions.
 */


void add_hlp_to_exe(char *hlp_fname, char *exe_fname)
   {
   int                  exe,   /* handles */
                        hlp;
   long                 len,
                        count;
   int                  size;
   struct help_sig_info hs;

   if ( (exe=open(exe_fname, O_RDWR|O_BINARY)) == -1 )
      fatal(0,"Unable to open \"%s\"", exe_fname);

   if ( (hlp=open(hlp_fname, O_RDONLY|O_BINARY)) == -1 )
      fatal(0,"Unable to open \"%s\"", hlp_fname);

   msg("Appending %s to %s", hlp_fname, exe_fname);

   /* first, check and see if any help is currently installed */

   lseek(exe, filelength(exe) - sizeof(struct help_sig_info), SEEK_SET);

   read(exe, (char *)&hs, 10);

   if ( hs.sig == HELP_SIG )
      warn(0,"Overwriting previous help. (Version=%d)", hs.version);
   else
      hs.base = filelength(exe);

   /* now, let's see if their help file is for real (and get the version) */

   read(hlp, (char *)&hs, sizeof(long)+sizeof(int));

   if (hs.sig != HELP_SIG )
      fatal(0,"Help signature not found in %s", hlp_fname);

   msg("Help file %s Version=%d", hlp_fname, hs.version);

   /* append the help stuff, overwriting old help (if any) */

   lseek(exe, hs.base, SEEK_SET);

   len = filelength(hlp) - sizeof(long) - sizeof(int); /* adjust for the file signature & version */

   for (count=0; count<len; )
      {
      size = (int) min((long)BUFFER_SIZE, len-count);
      read(hlp, buffer, size);
      write(exe, buffer, size);
      count += size;
      }

   /* add on the signature, version and offset */

   write(exe, (char *)&hs, 10);

   chsize(exe, lseek(exe,0L,SEEK_CUR));/* truncate if old help was longer */

   close(exe);
   close(hlp);
   }


void delete_hlp_from_exe(char *exe_fname)
   {
   int   exe;   /* file handle */
   struct help_sig_info hs;

   if ( (exe=open(exe_fname, O_RDWR|O_BINARY)) == -1 )
      fatal(0,"Unable to open \"%s\"", exe_fname);

   msg("Deleting help from %s", exe_fname);

   /* see if any help is currently installed */

#ifndef XFRACT
   lseek(exe, filelength(exe) - 10, SEEK_SET);
   read(exe, (char *)&hs, 10);
#else
   lseek(exe, filelength(exe) - 12, SEEK_SET);
   read(exe, (char *)&hs, 12);
#endif

   if ( hs.sig == HELP_SIG )
      {
      chsize(exe, hs.base);   /* truncate at the start of the help */
      close(exe);
      }
   else
      {
      close(exe);
      fatal(0,"No help found in %s", exe_fname);
      }
   }


/*
 * command-line parser, etc.
 */


#define MODE_COMPILE 1
#define MODE_PRINT   2
#define MODE_APPEND  3
#define MODE_DELETE  4


int main(int argc, char *argv[])
   {
   int    show_stats = 0,
          show_mem   = 0;
   int    mode       = 0;

   char **arg;
   char   fname1[81],
          fname2[81];
   char   swappath[81];

   fname1[0] = fname2[0] = swappath[0] = 0;

   printf("HC - FRACTINT Help Compiler.\n\n");

   buffer = malloc(BUFFER_SIZE);

   if (buffer == NULL)
      fatal(0,"Not enough memory to allocate buffer.");

   for (arg= &argv[1]; argc>1; argc--, arg++)
      {
      switch ( (*arg)[0] )
         {
         case '/':
         case '-':
            switch ( (*arg)[1] )
               {
               case 'c':
                  if (mode == 0)
                     mode = MODE_COMPILE;
                  else
                     fatal(0,"Cannot have /c with /a, /d or /p");
                  break;

               case 'a':
                  if (mode == 0)
                     mode = MODE_APPEND;
                  else
                     fatal(0,"Cannot have /a with /c, /d or /p");
                  break;

               case 'd':
                  if (mode == 0)
                     mode = MODE_DELETE;
                  else
                     fatal(0,"Cannot have /d with /c, /a or /p");
                  break;

               case 'p':
                  if (mode == 0)
                     mode = MODE_PRINT;
                  else
                     fatal(0,"Cannot have /p with /c, /a or /d");
                  break;

               case 'm':
                  if (mode == MODE_COMPILE)
                     show_mem = 1;
                  else
                     fatal(0,"/m switch allowed only when compiling (/c)");
                  break;

               case 's':
                  if (mode == MODE_COMPILE)
                     show_stats = 1;
                  else
                     fatal(0,"/s switch allowed only when compiling (/c)");
                  break;

               case 'r':
                  if (mode == MODE_COMPILE || mode == MODE_PRINT)
                     strcpy(swappath, (*arg)+2);
                  else
                     fatal(0,"/r switch allowed when compiling (/c) or printing (/p)");
                  break;

               case 'q':
                  quiet_mode = 1;
                  break;

               default:
                  fatal(0,"Bad command-line switch /%c", (*arg)[1]);
                  break;
               }
            break;

         default:   /* assume it is a fname */
            if (fname1[0] == '\0')
               strcpy(fname1, *arg);
            else if (fname2[0] == '\0')
               strcpy(fname2, *arg);
            else
               fatal(0,"Unexpected command-line argument \"%s\"", *arg);
            break;
         } /* switch */
      } /* for */

   strupr(fname1);
   strupr(fname2);
   strupr(swappath);

   switch (mode)
      {
      case 0:
         printf( "To compile a .SRC file:\n");
         printf( "      HC /c [/s] [/m] [/r[path]] [src_file]\n");
         printf( "         /s       = report statistics.\n");
         printf( "         /m       = report memory usage.\n");
         printf( "         /r[path] = set swap file path.\n");
         printf( "         src_file = .SRC file.  Default is \"%s\"\n", DEFAULT_SRC_FNAME);
         printf( "To print a .SRC file:\n");
         printf( "      HC /p [/r[path]] [src_file] [out_file]\n");
         printf( "         /r[path] = set swap file path.\n");
         printf( "         src_file = .SRC file.  Default is \"%s\"\n", DEFAULT_SRC_FNAME);
         printf( "         out_file = Filename to print to. Default is \"%s\"\n",
         DEFAULT_DOC_FNAME);
         printf( "To append a .HLP file to an .EXE file:\n");
         printf( "      HC /a [hlp_file] [exe_file]\n");
         printf( "         hlp_file = .HLP file.  Default is \"%s\"\n", DEFAULT_HLP_FNAME);
         printf( "         exe_file = .EXE file.  Default is \"%s\"\n", DEFAULT_EXE_FNAME);
         printf( "To delete help info from an .EXE file:\n");
         printf( "      HC /d [exe_file]\n");
         printf( "         exe_file = .EXE file.  Default is \"%s\"\n", DEFAULT_EXE_FNAME);
         printf( "\n");
         printf( "Use \"/q\" for quiet mode. (No status messages.)\n");
         break;

      case MODE_COMPILE:
         if (fname2[0] != '\0')
            fatal(0,"Unexpected command-line argument \"%s\"", fname2);

         strcpy(src_fname, (fname1[0]=='\0') ? DEFAULT_SRC_FNAME : fname1);

         strcat(swappath, SWAP_FNAME);

         if ( (swapfile=fopen(swappath, "w+b")) == NULL )
            fatal(0,"Cannot create swap file \"%s\"", swappath);
         swappos = 0;

         read_src(src_fname);

         if (hdr_fname[0] == '\0')
            error(0,"No .H file defined.  (Use \"~HdrFile=\")");
         if (hlp_fname[0] == '\0')
            error(0,"No .HLP file defined.  (Use \"~HlpFile=\")");
         if (version == -1)
            warn(0,"No help version has been defined.  (Use \"~Version=\")");

         /* order of these is very important... */

         make_hot_links();  /* do even if errors since it may report */
                            /* more... */

         if ( !errors )     paginate_online();
         if ( !errors )     paginate_document();
         if ( !errors )     calc_offsets();
         if ( !errors )     sort_labels();
         if ( !errors )     write_hdr(hdr_fname);
         if ( !errors )     write_help(hlp_fname);

         if ( show_stats )
            report_stats();

         if ( show_mem )
            report_memory();

         if ( errors || warnings )
            report_errors();

         fclose(swapfile);
         remove(swappath);

         break;

      case MODE_PRINT:
         strcpy(src_fname, (fname1[0]=='\0') ? DEFAULT_SRC_FNAME : fname1);

         strcat(swappath, SWAP_FNAME);

         if ( (swapfile=fopen(swappath, "w+b")) == NULL )
            fatal(0,"Cannot create swap file \"%s\"", swappath);
         swappos = 0;

         read_src(src_fname);

         make_hot_links();

         if ( !errors )     paginate_document();
         if ( !errors )     print_document( (fname2[0]=='\0') ? DEFAULT_DOC_FNAME : fname2 );

         if ( errors || warnings )
            report_errors();

         fclose(swapfile);
         remove(swappath);

         break;

      case MODE_APPEND:
         add_hlp_to_exe( (fname1[0]=='\0') ? DEFAULT_HLP_FNAME : fname1,
                         (fname2[0]=='\0') ? DEFAULT_EXE_FNAME : fname2);
         break;

      case MODE_DELETE:
         if (fname2[0] != '\0')
            fatal(0,"Unexpected argument \"%s\"", fname2);
         delete_hlp_from_exe((fname1[0]=='\0') ? DEFAULT_EXE_FNAME : fname1);
         break;
      }

   free(buffer);

   return ( errors );   /* return the number of errors */
   }