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

\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\topmargin}{0in}
\addtolength{\topmargin}{-\headheight}
\addtolength{\topmargin}{-\headsep}
\setlength{\textheight}{8.9in}
\setlength{\textwidth}{6.5in}
\setlength{\marginparwidth}{0.5in}

\title{Nuweb \\ A Simple Literate Programming Tool}
\author{\sl Preston Briggs}
\date{(Version 0.6pc)}

\begin{document}
\maketitle
\tableofcontents

\chapter{Introduction}

In 1984, Knuth introduced the idea of {\em literate programming\/} and
described a pair of tools to support the practise~\cite{knuth:84}.
His approach was to combine Pascal code with \TeX\ documentation to
produce a new language, {\tt WEB}, that offered programmers a superior
approach to programming. He wrote several programs in {\tt WEB},
including {\tt weave} and {\tt tangle}, the programs used to support
literate programming.
The idea was that a programmer wrote one document, the web file, that
combined documentation (written in \TeX~\cite{texbook}) with code
(written in Pascal).

Running {\tt tangle} on the web file would produce a complete
Pascal program, ready for compilation by an ordinary Pascal compiler.
The primary function of {\tt tangle} is to allow the programmer to
present elements of the program in any desired order, regardless of
the restrictions imposed by the programming language. Thus, the
programmer is free to present his program in a top-down fashion,
bottom-up fashion, or whatever seems best in terms of promoting
understanding and maintenance.

Running {\tt weave} on the web file would produce a \TeX\ file, ready
to be processed by \TeX\@@. The resulting document included a variety of
automatically generated indices and cross-references that made it much
easier to navigate the code. Additionally, all of the code sections
were automatically pretty printed, resulting in a quite impressive
document. 

Knuth also wrote the programs for \TeX\ and {\small\sf METAFONT}
entirely in {\tt WEB}, eventually publishing them in book
form~\cite{tex:program,metafont:program}. These are probably the
largest programs ever published in a readable form.

Inspired by Knuth's example, many people have experimented with
{\tt WEB}\@@. Some people have even built web-like tools for their own
favorite combinations of programming language and typesetting
language. For example, {\tt CWEB}, Knuth's current system of choice,
works with a combination of C and \TeX~\cite{levy:90}. Another system,
FunnelWeb, is independent of any programming language and only mildly
dependent on \TeX~\cite{funnelweb}. Inspired by the versatility
of FunnelWeb and by the daunting size of its documentation, I decided
to write my own, very simple, tool for literate programming.%
\footnote{There is another system similar to mine, written by Norman
Ramsey, called {\em noweb}~\cite{noweb}. It perhaps suffers from being
overly Unix-dependent and requiring several programs to use. On the
other hand, its command syntax is very nice. In any
case, nuweb certainly owes its name to his inspiration.}


\section{Nuweb}

Nuweb works with any programming language and \LaTeX~\cite{latex}. I
wanted to use \LaTeX\ because it supports a multi-level sectioning
scheme and has facilities for drawing figures. I wanted to be able to
work with arbitrary programming languages because my friends and I
write programs in many languages (and sometimes combinations of
several languages), {\em e.g.,} C, Fortran, C++, yacc, lex, Scheme,
assembly, Postscript, and so forth. The need to support arbitrary
programming languages has many consequences:
\begin{description}
\item[No pretty printing] Both {\tt WEB} and {\tt CWEB} are able to
  pretty print the code sections of their documents because they
  understand the language well enough to parse it. Since we want to use
  {\em any\/} language, we've got to abandon this feature.
\item[No index of identifiers] Because {\tt WEB} knows about Pascal,
  it is able to construct an index of all the identifiers occurring in
  the code sections (filtering out keywords and the standard type
  identifiers). Unfortunately, this isn't as easy in our case. We don't
  know what an identifiers looks like in each language and we certainly
  don't know all the keywords. (On the other hand, see the end of
  Section~1.3)
\end{description}
Of course, we've got to have some compensation for our losses or the
whole idea would be a waste. Here are the advantages I can see:
\begin{description}
\item[Simplicity] The majority of the commands in {\tt WEB} are
  concerned with control of the automatic pretty printing. Since we
  don't pretty print, many commands are eliminated. A further set of
  commands is subsumed by \LaTeX\  and may also be eliminated. As a
  result, our set of commands is reduced to only four members (explained
  in the next section). This simplicity is also reflected in
  the size of this tool, which is quite a bit smaller than the tools
  used with other approaches.
\item[No pretty printing] Everyone disagrees about how their code
  should look, so automatic formatting annoys many people. One approach
  is to provide ways to control the formatting. Our approach is simpler
  -- we perform no automatic formatting and therefore allow the
  programmer complete control of code layout.
\item[Control] We also offer the programmer complete control of the
  layout of his output files (the files generated during tangling). Of
  course, this is essential for languages that are sensitive to layout;
  but it is also important in many practical situations, {\em e.g.,}
  debugging.
\item[Speed] Since nuweb doesn't do to much, the nuweb tool runs
  quickly. I combine the functions of {\tt tangle} and {\tt weave} into
  a single program that performs both functions at once.
\item[Multiple file output] The programmer may specify more than one
  output file in a single nuweb file. This is required when constructing
  programs in a combination of languages (say, Fortran and C)\@@. It's also
  an advantage when constructing very large programs that would require
  a lot of compile time.
\end{description}
This last point is very important. By allowing the creation of
multiple output files, we avoid the need for monolithic programs.
Thus we support the creation of very large programs by groups of
people. 

A further reduction in compilation time is achieved by first
writing each output file to a temporary location, then comparing the
temporary file with the old version of the file. If there is no
difference, the temporary file can be deleted. If the files differ,
the old version is deleted and the temporary file renamed. This
approach works well in combination with {\tt make} (or similar tools),
since {\tt make} will avoid recompiling untouched output files.


\section{Writing Nuweb}

The bulk of a nuweb file will be ordinary \LaTeX\@@. In fact, any \LaTeX\
file can serve as input to nuweb and will be simply copied through
unchanged to the {\tt .tex} file -- unless a nuweb command is
discovered. All nuweb commands begin with an ``at-sign'' ({\tt @@}).
Therefore, a file without at-signs will be copied unchanged.
Nuweb commands are used to specify {\em output files,} define 
{\em macros,} and delimit {\em scraps}. These are the basic features
of interest to the nuweb tool -- all else is simply text to be copied
to the {\tt .tex} file.

\subsection{The Major Commands}

Files and macros are defined with the following commands:
\begin{description}
\item[\tt @@o {\em file-name flags scrap\/}] Output a file. The file name is
  terminated by whitespace.
\item[\tt @@d {\em macro-name scrap\/}] Define a macro. The macro name
  is terminated by a return or the beginning of a scrap.
\end{description}
A specific file may be specified several times, with each definition
being written out, one after the other, in the order they appear.
The definitions of macros may be similarly divided.

\subsubsection{Scraps}

Scraps have specific begin markers and end markers to allow precise
control over the contents and layout. Note that any amount of
whitespace (including carriage returns) may appear between a name and
the beginning of a scrap.
\begin{description}
\item[\tt @@\{{\em anything\/}@@\}] where the scrap body includes every
  character in {\em anything\/} -- all the blanks, all the tabs, all the
  carriage returns.
\end{description}
Inside a scrap, we may invoke a macro.
\begin{description}
\item[\tt @@<{\em macro-name\/}@@>] Causes the macro {\em macro-name\/}
  to be expanded inline as the code is written out to a file. It is an
  error to specify recursive macro invocations.
\end{description}
Note that macro names may be abbreviated, either during invocation or
definition. For example, it would be very tedious to have to
repeatedly type the macro name
\begin{quote}
{\tt @@d Check for terminating at-sequence and return name if found}
\end{quote}
Therefore, we provide a mechanism (stolen from Knuth) of indicating
abbreviated names.
\begin{quote}
{\tt @@d Check for terminating...}
\end{quote}
Basically, the programmer need only type enough characters to uniquely
identify the macro name, followed by three periods. An abbreviation
may even occur before the full version; nuweb simply preserves the
longest version of a macro name. Note also that blanks and tabs are
insignificant in a macro name; any string of them are replaced by a
single blank.

When scraps are written to an output or {\tt .tex} file, tabs are
expanded into spaces by default. Currently, I assume tab stops are set
every eight characters. Furthermore, when a macro is expanded in a scrap,
the body of the macro is indented to match the indentation of the
macro invocation. Therefore, care must be taken with languages 
({\em e.g.,} Fortran) that are sensitive to indentation.
These default behaviors may be changed for each output file (see
below).

\subsubsection{Flags}

When defining an output file, the programmer has the option of using
flags to control output of a particular file. The flags are intended
to make life a little easier for programmers using certain languages.
They introduce little language dependences; however, they do so only
for a particular file. Thus it is still easy to mix languages within a
single document. There are three ``per-file'' flags:
\begin{description}
\item[\tt -d] Forces the creation of \verb|#line| directives in the
  output file. These are useful with C (and sometimes C++ and Fortran) on
  many Unix systems since they cause the compiler's error messages to
  refer to the web file rather than the output file. Similarly, they
  allow source debugging in terms of the web file.
\item[\tt -i] Suppresses the indentation of macros. That is, when a
  macro is expanded in a scrap, it will {\em not\/} be indented to
  match the indentation of the macro invocation. This flag would seem
  most useful for Fortran programmers.
\item[\tt -t] Suppresses expansion of tabs in the output file. This
  feature seems important when generating {\tt make} files.
\end{description}


\subsection{The Minor Commands}

We have two very low-level utility commands that may appear anywhere
in the web file.
\begin{description}
\item[\tt @@@@] Causes a single ``at sign'' to be copied into the output.
\item[\tt @@i {\em file-name\/}] Includes a file. Includes may be
  nested, though there is currently a limit of 10~levels. The file name
  should be complete (no extension will be appended) and should be
  terminated by a carriage return.
\end{description}
Finally, there are three commands used to create indices to the macro
names, file definitions, and user-specified identifiers.
\begin{description}
\item[\tt @@f] Create an index of file names.
\item[\tt @@m] Create an index of macro name.
\item[\tt @@u] Create an index of user-specified identifiers.
\end{description}
I usually put these in their own section
in the \LaTeX\ document; for example, see Chapter~\ref{indices}.

Identifiers must be explicitely specified for inclusion in the 
{\tt @@u} index. By convention, each identifier is marked at the point
of its definition; all references to each identifier (inside scraps)
will be discovered automatically. To ``mark'' an identifier for
inclusion in the index, we must mention it at the end of a scrap.
For example,
\begin{quote}
\begin{verbatim}
@@d a scrap @@{
Let's pretend we're declaring the variables FOO and BAR
inside this scrap.
@@| FOO BAR @@}
\end{verbatim}
\end{quote}
I've used alphabetic identifiers in this example, but any string of
characters (not including whitespace) will do.  Therefore, it's
possible to add index entries for things like {\tt <<=} if desired.
An identifier may be declared in more than one scrap.

In the generated index, each identifier appears with a list of all the
scraps using and defining it, where the defining scraps are
distinguished by underlining. Note that the identifier doesn't
actually have to appear in the defining scrap; it just has to be in
the list of definitions at the end of a scrap.


\section{Running Nuweb}

Nuweb is invoked using the following command:
\begin{quote}
{\tt nuweb} {\em flags file-name}\ldots
\end{quote}
One or more files may be processed at a time. If a file name has no
extension, {\tt .w} will be appended. While a file name may specify a
file in another directory, the resulting {\tt .tex} file will always
be created in the current directory. For example,
\begin{quote}
{\tt nuweb /foo/bar/quux}
\end{quote}
will take as input the file {\tt /foo/bar/quux.w} and will create the
file {\tt quux.tex} in the current directory.

By default, nuweb performs both tangling and weaving at the same time.
Normally, this is not a bottleneck in the compilation process;
however, it's possible to achieve slightly faster throughput by
avoiding one or another of the default functions using command-line
flags. There are currently three possible flags:
\begin{description}
\item[\tt -t] Suppress generation of the {\tt .tex} file.
\item[\tt -o] Suppress generation of the output files.
\item[\tt -c] Avoid testing output files for change before updating them.
\end{description}
Thus, the command
\begin{quote}
{\tt nuweb -to /foo/bar/quux}
\end{quote}
would simply scan the input and produce no output at all.


\section{Restrictions and Extensions}

Because nuweb is intended to be a simple tool, I've established a few
restrictions. Over time, some of these may be eliminated; others seem
fundamental.
\begin{itemize}
\item The scraps don't print well with older versions of \TeX\ 
  (pre~3.0). I'm not sure why and I'm not sure it's always true
  (I don't have a version to experiment with).
\item The handling of errors is not completely ideal. In some cases, I
  simply warn of a problem and continue; in other cases I halt
  immediately. This behavior should be regularized.
\item I warn about references to macros that haven't been defined, but
  don't halt. This seems most convenient for development, but may change
  in the future.
\item File names and index entries should not contain any {\tt @@}
  signs.
\item Macro names may be (almost) any well-formed \TeX\ string.
  It makes sense to change fonts or use math mode; however, care should
  be taken to ensure matching braces, brackets, and dollar signs.
\item Anything is allowed in the body of a scrap; however, very
  long scraps (horizontally or vertically) may not typeset well.
\end{itemize}
Very long scraps may be allowed to break across a page if declared
with {\tt @@O} or {\tt @@D} (instead of {\tt @@o} or {\tt @@d}).
This doesn't work very well as a default, since far too many short
scraps will be broken across pages; however, as a user-controlled
option, it seems very useful.


\chapter{The Overall Structure}

Processing a web requires three major steps:
\begin{enumerate}
\item Read the source, accumulating file names, macro names, scraps,
and lists of cross-references.
\item Reread the source, copying out to the {\tt .tex} file, with
protection and cross-reference information for all the scraps.
\item Traverse the list of files names. For each file name:
\begin{enumerate}
\item Dump all the defining scraps into a temporary file. 
\item If the file already exists and is unchanged, delete the
temporary file; otherwise, rename the temporary file.
\end{enumerate}
\end{enumerate}


\section{Files}

I have divided the program into several files for quicker
recompilation during development. Rather than use {\tt .h} files
for shared declarations, I'll simply create a shared scrap containing
all the global declarations. This isn't really best from the point of
view of disk space; but it does simplify the makefile (and allow me to
play with my new toy).
@d Shared declarations
@{@<Include files@>
@<Type declarations@>
@<Global variables@>
@<Function prototypes@>
@}

We'll need at least two of the standard system include files.
@d Include files
@{#include <stdio.h>
#include <string.h>
#include <ctype.h>
@| FILE stderr exit fprintf fputs fopen fclose getc putc strlen
toupper isupper islower isgraph isspace @}

\newpage
\noindent
I also like to use {\tt TRUE} and {\tt FALSE} in my code.
I'd use an {\tt enum} here, except that some systems seem to provide
definitions of {\tt TRUE} and {\tt FALSE} be default.  The following
code seems to work on all the local systems.
@d Type dec...
@{#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!0)
#endif
@| FALSE TRUE @}


\subsection{The Main Files}

The code is divided into four main files (introduced here) and five
support files (introduced in the next section).
The file {\tt main.c} will contain the driver for the whole program
(see Section~\ref{main-routine}).
@o main.c
@{@<Shared...@>
@}


The first pass over the source file is contained in {\tt pass1.c}.
It handles collection of all the file names, macros names, and scraps
(see Section~\ref{pass-one}).
@o pass1.c
@{@<Shared...@>
@}

The {\tt .tex} file is created during a second pass over the source
file. The file {\tt latex.c} contains the code controlling the
construction of the {\tt .tex} file 
(see Section~\ref{latex-file}).
@o latex.c
@{@<Shared...@>
@}


The code controlling the creation of the output files is in {\tt output.c}
(see Section~\ref{output-files}).
@o output.c
@{@<Shared...@>
@}


\subsection{Support Files}

The support files contain a variety of support routines used to define
and manipulate the major data abstractions.
The file {\tt input.c} holds all the routines used for referring to
source files (see Section~\ref{source-files}).
@o input.c
@{@<Shared...@>
@}

Creation and lookup of scraps is handled by routines in {\tt scraps.c}
(see Section~\ref{scraps}).
@o scraps.c
@{@<Shared...@>
@}


The handling of file names and macro names is detailed in {\tt names.c}
(see Section~\ref{names}).
@o names.c
@{@<Shared...@>
@}

Memory allocation and deallocation is handled by routines in {\tt arena.c}
(see Section~\ref{memory-management}). I don't bother including all
the shared declarations since they aren't required.
@o arena.c
@{@<Include...@>
@}


\section{The Main Routine} \label{main-routine}

The main routine is quite simple in structure.
It wades through the optional command-line arguments,
then handles any files listed on the command line.
@o main.c
@{void main(argc, argv)
     int argc;
     char **argv;
{
  int arg = 1;
  @<Interpret command-line arguments@>
  @<Process the remaining arguments (file names)@>
  exit(0);
}
@| main @}


\subsection{Command-Line Arguments}

There are three possible command-line arguments:
\begin{description}
\item[{\tt -t}] Suppresses generation of the {\tt .tex} file.
\item[{\tt -o}] Suppresses generation of the output files.
\item[{\tt -c}] Forces output files to overwrite old files of the same
name without comparing for equality first.
\item[{\tt -v}] Prints the current version of {\tt nuweb}.
\end{description}
Global flags are declared for each of the arguments.
@d Global...
@{int tex_flag;      /* if FALSE, don't emit the .tex file */
int output_flag;   /* if FALSE, don't emit the output files */
int compare_flag;  /* if FALSE, overwrite without comparison */
@| tex_flag output_flag compare_flag @}

\newpage
\noindent
The flags are all initialized to {\tt TRUE} for correct default behavior.
@d Interpret com...
@{tex_flag = TRUE;
output_flag = TRUE;
compare_flag = TRUE;
@}


We save the invocation name of the command in a global variable
{\tt command\_name} for use in error messages.
@d Global...
@{char *command_name;
@| command_name @}

The invocation name is conventionally passed in {\tt argv[0]}.
@d Interpret com...
@{command_name = argv[0];
@}


We need to examine the remaining entries in {\tt argv}, looking for
command-line arguments.
@d Interpret com...
@{while (arg < argc) {
  char *s = argv[arg];
  if (*s++ == '-') {
    @<Interpret the argument string {\tt s}@>
    arg++;
  }
  else break;
}@}

\newpage
\noindent
Several flags can be stacked behind a single minus sign; therefore,
we've got to loop through the string, handling them all.
@d Interpret the...
@{{
  char c = *s++;
  while (c) {
    switch (c) {
      case 'c': compare_flag = FALSE;
		break;
      case 'o': output_flag = FALSE;
		break;
      case 't': tex_flag = FALSE;
	 	break;
      case 'v': printf("nuweb version 0.6\n");
		break;
      default:  fprintf(stderr, "%s: unexpected argument ignored.  ",
			command_name);
		fprintf(stderr, "Usage is: %s [-cot] file...\n",
			command_name);
		break;
    }
    c = *s++;
  }
}@}


\subsection{File Names}

We expect at least one file name. While a missing file name might be
ignored without causing any problems, we take the opportunity to report
the usage convention.
@d Process the remaining...
@{if (arg < argc)
  do {
    @<Handle the file name in {\tt argv[arg]}@>
    arg++;
  } while (arg < argc);
else {
  fprintf(stderr, "%s: expected a file name.  ", command_name);
  fprintf(stderr, "Usage is: %s [-cot] file-name...\n", command_name);
  exit(-1);
}@}

\newpage
\noindent
The code to handle a particular file name is rather more tedious than
the actual processing of the file. A file name may be an arbitrarily
complicated path name, with an optional extension. If no extension is
present, we add {\tt .w} as a default. The extended path name will be
kept in a local variable {\tt source\_name}. The resulting {\tt .tex}
file will be written in the current directory; its name will be kept
in the variable {\tt tex\_name}.
@d Handle the file...
@{{
  char source_name[100];
  char tex_name[100];
  @<Build {\tt source\_name} and {\tt tex\_name}@>
  @<Process a file@>
}@}


I bump the pointer {\tt p} through all the characters in {\tt argv[arg]},
copying all the characters into {\tt source\_name} (via the pointer
{\tt q}). 

At each slash, I update {\tt trim} to point just past the
slash in {\tt source\_name}. The effect is that {\tt trim} will point
at the file name without any leading directory specifications.

The pointer {\tt dot} is made to point at the file name extension, if
present. If there is no extension, we add {\tt .w} to the source name.
In any case, we create the {\tt tex\_name} from {\tt trim}, taking
care to get the correct extension.
@d Build {\tt sou...
@{{
  char *p = argv[arg];
  char *q = source_name;
  char *trim = q;
  char *dot = NULL;
  char c = *p++;
  while (c) {
    *q++ = c;
    if (c == '/') {
      trim = q;
      dot = NULL;
    }
    else if (c == '.')
      dot = q - 1;
    c = *p++;
  }
  *q = '\0';
  if (dot) {
    *dot = '\0';
    sprintf(tex_name, "%s.tex", trim);
    *dot = '.';
  }
  else {
    sprintf(tex_name, "%s.tex", trim);
    *q++ = '.';
    *q++ = 'w';
    *q = '\0';
  }
}@}

Now that we're finally ready to process a file, it's not really too
complex.  We bundle most of the work into three routines {\tt pass1}
(see Section~\ref{pass-one}), {\tt write\_tex} (see
Section~\ref{latex-file}), and {\tt write\_files} (see
Section~\ref{output-files}). After we're finished with a
particular file, we must remember to release its storage (see
Section~\ref{memory-management}).
@d Process a file
@{{
  pass1(source_name);
  if (tex_flag)
    write_tex(source_name, tex_name);
  if (output_flag)
    write_files(file_names);
  arena_free();
}@}


\section{Pass One} \label{pass-one}

During the first pass, we scan the file, recording the definitions of
each macro and file and accumulating all the scraps. 

@d Function pro...
@{void pass1();
@}


The routine {\tt pass1} takes a single argument, the name of the
source file. It opens the file, then initializes the scrap structures
(see Section~\ref{scraps}) and the roots of the file-name tree, the
macro-name tree, and the tree of user-specified index entries (see 
Section~\ref{names}). After completing all the
necessary preparation, we make a pass over the file, filling in all
our data structures. Next, we seach all the scraps for references to
the user-specified index entries. Finally, we must reverse all the
cross-reference lists accumulated while scanning the scraps.
@o pass1.c
@{void pass1(file_name)
     char *file_name;
{
  source_open(file_name);
  init_scraps();
  macro_names = NULL;
  file_names = NULL;
  user_names = NULL;
  @<Scan the source file, looking for at-sequences@>
  if (tex_flag)
    search();
  @<Reverse cross-reference lists@>
}
@| pass1 @}

\newpage
\noindent
The only thing we look for in the first pass are the command
sequences. All ordinary text is skipped entirely.
@d Scan the source file, look...
@{{
  int c = source_get();
  while (c != EOF) {
    if (c == '@@')
      @<Scan at-sequence@>
    c = source_get();
  }
}@}

Only four of the at-sequences are interesting during the first pass.
We skip past others immediately; warning if unexpected sequences are
discovered.
@d Scan at-sequence
@{{
  c = source_get();
  switch (c) {
    case 'O':
    case 'o': @<Build output file definition@>
	      break;
    case 'D':
    case 'd': @<Build macro definition@>
	      break;
    case '@@':
    case 'u':
    case 'm':
    case 'f': /* ignore during this pass */
	      break;
    default:  fprintf(stderr,
		      "%s: unexpected @@ sequence ignored (%s, line %d)\n",
		      command_name, source_name, source_line);
	      break;
  }
}@}


\subsection{Accumulating Definitions}

There are three steps required to handle a definition:
\begin{enumerate}
\item Build an entry for the name so we can look it up later.
\item Collect the scrap and save it in the table of scraps.
\item Attach the scrap to the name.
\end{enumerate}
We go through the same steps for both file names and macro names.
@d Build output file definition
@{{
  Name *name = collect_file_name(); /* returns a pointer to the name entry */
  int scrap = collect_scrap();	    /* returns an index to the scrap */
  @<Add {\tt scrap} to...@>
}@}


@d Build macro definition
@{{
  Name *name = collect_macro_name();
  int scrap = collect_scrap();
  @<Add {\tt scrap} to...@>
}@}

Since a file or macro may be defined by many scraps, we maintain them
in a simple linked list. The list is actually built in reverse order,
with each new definition being added to the head of the list.
@d Add {\tt scrap} to {\tt name}'s definition list
@{{
  Scrap_Node *def = (Scrap_Node *) arena_getmem(sizeof(Scrap_Node));
  def->scrap = scrap;
  def->next = name->defs;
  name->defs = def;
}@}


\subsection{Fixing the Cross References}

Since the definition and reference lists for each name are accumulated
in reverse order, we take the time at the end of {\tt pass1} to
reverse them all so they'll be simpler to print out prettily.
The code for {\tt reverse\_lists} appears in Section~\ref{names}.
@d Reverse cross-reference lists
@{{
  reverse_lists(file_names);
  reverse_lists(macro_names);
  reverse_lists(user_names);
}@}



\section{Writing the Latex File} \label{latex-file}

The second pass (invoked via a call to {\tt write\_tex}) copies most of
the text from the source file straight into a {\tt .tex} file.
Definitions are formatted slightly and cross-reference information is
printed out.

Note that all the formatting is handled in this section.
If you don't like the format of definitions or indices or whatever,
it'll be in this section somewhere. Similarly, if someone wanted to
modify nuweb to work with a different typesetting system, this would
be the place to look.

@d Function...
@{void write_tex();
@}

\newpage
\noindent
We need a few local function declarations before we get into the body
of {\tt write\_tex}.

@o latex.c
@{static void copy_scrap();		/* formats the body of a scrap */
static void print_scrap_numbers();	/* formats a list of scrap numbers */
static void format_entry();		/* formats an index entry */
static void format_user_entry();
@}

The routine {\tt write\_tex} takes two file names as parameters: the
name of the web source file and the name of the {\tt .tex} output file.
@o latex.c
@{void write_tex(file_name, tex_name)
     char *file_name;
     char *tex_name;
{
  FILE *tex_file = fopen(tex_name, "w");
  if (tex_file) {
    source_open(file_name);
    @<Copy {\tt source\_file} into {\tt tex\_file}@>
    fclose(tex_file);
  }
  else
    fprintf(stderr, "%s: can't open %s\n", command_name, tex_name);
}
@| write_tex @}

We make our second (and final) pass through the source web, this time
copying characters straight into the {\tt .tex} file. However, we keep
an eye peeled for {\tt @@}~characters, which signal a command sequence.

@d Copy {\tt source\_file}...
@{{
  int scraps = 1;
  int c = source_get();
  while (c != EOF) {
    if (c == '@@')
      @<Interpret at-sequence@>
    else {
      putc(c, tex_file);
      c = source_get();
    }
  }
}@}

@d Interpret at-sequence
@{{
  int big_definition = FALSE;
  c = source_get();
  switch (c) {
    case 'O': big_definition = TRUE;
    case 'o': @<Write output file definition@>
	      break;
    case 'D': big_definition = TRUE;
    case 'd': @<Write macro definition@>
	      break;
    case 'f': @<Write index of file names@>
	      break;
    case 'm': @<Write index of macro names@>
	      break;
    case 'u': @<Write index of user-specified names@>
	      break;
    case '@@': putc(c, tex_file);
    default:  c = source_get();
	      break;
  }
}@}


\subsection{Formatting Definitions}

We go through a fair amount of effort to format a file definition.
I've derived most of the \LaTeX\ commands experimentally; it's quite
likely that an expert could do a better job. The \LaTeX\ for
the previous macro definition should look like this (perhaps modulo
the scrap numbers):
\begin{verbatim}
\begin{flushleft}
\begin{minipage}{\linewidth}
$\langle$Interpret at-sequence {\footnotesize 35}$\rangle\equiv$
\vspace{-1.5ex}
\begin{quote}
\verb@@{@@\\
\verb@@  c = source_get();@@\\
\verb@@  switch (c) {@@\\
\verb@@    case 'O': big_definition = TRUE;@@\\
\verb@@    case 'o': @@$\langle$Write output file definition {\footnotesize 36}$\rangle$\verb@@@@\\
\end{verbatim}
\vdots
\begin{verbatim}
\verb@@    case '@@{\tt @@}\verb@@': putc(c, tex_file);@@\\
\verb@@    default:  c = source_get();@@\\
\verb@@              break;@@\\
\verb@@  }@@\\
\verb@@}@@$\Diamond$
\end{quote}
\vspace{-2ex}
{\footnotesize Macro referenced in scrap 34.}
\end{minipage}\\[4ex]
\end{flushleft}
\end{verbatim}
The {\em flushleft\/} environment is used to avoid \LaTeX\ warnings
about underful lines. The {\em minipage\/} environment is used to
avoid page breaks in the middle of scraps. The {\em verb\/} command
allows arbitrary characters to be printed (however, note the special
handling of the {\tt @@} case in the switch statement).

Macro and file definitions are formatted nearly identically.
I've factored the common parts out into separate scraps.

@d Write output file definition
@{{
  Name *name = collect_file_name();
  @<Begin the scrap environment@>
  fprintf(tex_file, "\\verb@@\"%s\"@@", name->spelling);
  fprintf(tex_file, " {\\footnotesize %d }$\\equiv$\n", scraps++);
  @<Fill in the middle of the scrap environment@>
  @<Write file defs@>
  @<Finish the scrap environment@>
}@}

I don't format a macro name at all specially, figuring the programmer
might want to use italics or bold face in the midst of the name.

@d Write macro definition
@{{
  Name *name = collect_macro_name();
  @<Begin the scrap environment@>
  fprintf(tex_file, "$\\langle$%s", name->spelling);
  fprintf(tex_file, " {\\footnotesize %d}$\\rangle\\equiv$\n", scraps++);
  @<Fill in the middle of the scrap environment@>
  @<Write macro defs@>
  @<Write macro refs@>
  @<Finish the scrap environment@>
}@}


@d Begin the scrap environment
@{{
  fputs("\\begin{flushleft}\n", tex_file);
  if (!big_definition)
    fputs("\\begin{minipage}{\\linewidth}\n", tex_file);
}@}

The interesting things here are the $\Diamond$ inserted at the end of
each scrap and the various spacing commands. The diamond helps to
clearly indicate the end of a scrap. The spacing commands were derived
empirically; they may be adjusted to taste.

@d Fill in the middle of the scrap environment
@{{
  fputs("\\vspace{-1.5ex}\n\\begin{quote}\n", tex_file);
  copy_scrap(tex_file);
  fputs("$\\Diamond$\n\\end{quote}\n\\vspace{-2ex}\n", tex_file);
}@}

\newpage
\noindent
We've got one last spacing command, controlling the amount of white
space after a scrap.

Note also the whitespace eater. I use it to remove any blank lines
that appear after a scrap in the source file. This way, text following
a scrap will not be indented. Again, this is a matter of personal taste.

@d Finish the scrap environment
@{{
  if (!big_definition)
    fputs("\\end{minipage}\\\\[4ex]\n", tex_file);
  fputs("\\end{flushleft}\n", tex_file);
  do
    c = source_get();
  while (isspace(c));
}@}


\subsubsection{Formatting Cross References}

@d Write file defs
@{{
  if (name->defs->next) {
    fputs("{\\footnotesize File defined by scraps ", tex_file);
    print_scrap_numbers(tex_file, name->defs);
    fputs("}\n", tex_file);
  }
}@}

@d Write macro defs
@{{
  fputs("{\\footnotesize ", tex_file);
  if (name->defs->next) {
    fputs("Macro defined by scraps ", tex_file);
    print_scrap_numbers(tex_file, name->defs);
    fputs("\\\\[-1ex]\n", tex_file);
  }
}@}

@d Write macro refs
@{{
  if (name->uses) {
    if (name->uses->next) {
      fputs("Macro referenced in scraps ", tex_file);
      print_scrap_numbers(tex_file, name->uses);
      fputs("}\n", tex_file);
    }
    else
      fprintf(tex_file, "Macro referenced in scrap %d.}\n", name->uses->scrap);
  }
  else {
    fputs("Macro never referenced.}\n", tex_file);
    fprintf(stderr, "%s: <%s> never referenced.\n",
	    command_name, name->spelling);
  }
}@}


@o latex.c
@{static void print_scrap_numbers(tex_file, scraps)
     FILE *tex_file;
     Scrap_Node *scraps;
{
  fprintf(tex_file, "%d", scraps->scrap);
  scraps = scraps->next;
  while (scraps->next) {
    fprintf(tex_file, ", %d", scraps->scrap);
    scraps = scraps->next;
  }
  fprintf(tex_file, " and~%d.", scraps->scrap);
}
@| print_scrap_numbers @}


\subsubsection{Formatting a Scrap}

@o latex.c
@{static void copy_scrap(file)
     FILE *file;
{
  int indent = 0;
  int c = source_get();
  fputs("\\verb@@", file);
  while (1) {
    switch (c) {
      case '@@':  @<Check at-sequence for end-of-scrap@>
		 break;
      case '\n': fputs("@@\\\\\n\\verb@@", file);
		 indent = 0;
		 break;
      case '\t': @<Expand tab into spaces@>
		 break;
      default:   putc(c, file);
		 indent++;
		 break;
    }
    c = source_get();
  }
}
@| copy_scrap @}


@d Expand tab into spaces
@{{
  int delta = 8 - (indent % 8);
  indent += delta;
  while (delta > 0) {
    putc(' ', file);
    delta--;
  }
}@}

@d Check at-sequence...
@{{
  c = source_get();
  switch (c) {
    case '@@': fputs("@@{\\tt @@}\\verb@@", file);
	      break;
    case '|': @<Skip over index entries@>
    case '}': putc('@@', file);
	      return;
    case '<': @<Format macro name@>
	      break;
    default:  /* ignore these since pass1 will have warned about them */
	      break;
  }
}@}

There's no need to check for errors here, since we will have already
pointed out any during the first pass.
@d Skip over index entries
@{{
  do {
    do
      c = source_get();
    while (c != '@@');
    c = source_get();
  } while (c != '}');
}@}


@d Format macro name
@{{
  Name *name = collect_scrap_name();
  fprintf(file, "@@$\\langle$%s {\\footnotesize ", name->spelling);
  if (name->defs)
    fprintf(file, "%d", name->defs->scrap);
  else {
    putc('?', file);
    fprintf(stderr, "%s: scrap never defined <%s>\n",
	    command_name, name->spelling);
  }
  fputs("}$\\rangle$\\verb@@", file);
}@}



\subsection{Generating the Indices}

@d Write index of file names
@{{
  if (file_names) {
    fputs("\n\\begin{list}{}{\\setlength{\\itemsep}{-\\parsep}}\n", tex_file);
    format_entry(file_names, tex_file, TRUE);
    fputs("\\end{list}", tex_file);
  }
  c = source_get();
}@}


@d Write index of macro names
@{{
  if (macro_names) {
    fputs("\n\\begin{list}{}{\\setlength{\\itemsep}{-\\parsep}}\n", tex_file);
    format_entry(macro_names, tex_file, FALSE);
    fputs("\\end{list}", tex_file);
  }
  c = source_get();
}@}


@o latex.c
@{static void format_entry(name, tex_file, file_flag)
     Name *name;
     FILE *tex_file;
     int file_flag;
{
  while (name) {
    format_entry(name->llink, tex_file, file_flag);
    @<Format an index entry@>
    name = name->rlink;
  }
}
@| format_entry @}


@d Format an index entry
@{{
  fputs("\\item \\hspace{-\\leftmargin}", tex_file);
  if (file_flag) {
    fprintf(tex_file, "\\verb@@\"%s\"@@ ", name->spelling);
    @<Write file's defining scrap numbers@>
  }
  else {
    fprintf(tex_file, "$\\langle$%s {\\footnotesize ", name->spelling);
    @<Write defining scrap numbers@>
    fputs("}$\\rangle$ ", tex_file);
    @<Write referencing scrap numbers@>
  }
  putc('\n', tex_file);
}@}


@d Write file's defining scrap numbers
@{{
  Scrap_Node *p = name->defs;
  fputs("{\\footnotesize Defined by scrap", tex_file);
  if (p->next) {
    fputs("s ", tex_file);
    print_scrap_numbers(tex_file, p);
  }
  else
    fprintf(tex_file, " %d.", p->scrap);
  putc('}', tex_file);
}@}

@d Write defining scrap numbers
@{{
  Scrap_Node *p = name->defs;
  if (p) {
    while (1) {
      fprintf(tex_file, "%d", p->scrap);
      p = p->next;
      if (!p) break;
      fputs(", ", tex_file);
    }
  }
  else
    putc('?', tex_file);
}@}

@d Write referencing scrap numbers
@{{
  Scrap_Node *p = name->uses;
  fputs("{\\footnotesize ", tex_file);
  if (p) {
    fputs("Referenced in scrap", tex_file);
    if (p->next) {
      fputs("s ", tex_file);
      print_scrap_numbers(tex_file, p);
    }
    else
      fprintf(tex_file, " %d.", p->scrap);
  }
  else
    fputs("Not referenced.", tex_file);
  putc('}', tex_file);
}@}


@d Write index of user-specified names
@{{
  if (user_names) {
    fputs("\n\\begin{list}{}{\\setlength{\\itemsep}{-\\parsep}}\n", tex_file);
    format_user_entry(user_names, tex_file);
    fputs("\\end{list}", tex_file);
  }
  c = source_get();
}@}


@o latex.c
@{static void format_user_entry(name, tex_file)
     Name *name;
     FILE *tex_file;
{
  while (name) {
    format_user_entry(name->llink, tex_file);
    @<Format a user index entry@>
    name = name->rlink;
  }
}
@| format_user_entry @}


@d Format a user index entry
@{{
  Scrap_Node *uses = name->uses;
  Scrap_Node *defs = name->defs;
  fprintf(tex_file, "\\item \\hspace{-\\leftmargin}\\verb@@%s@@: ",
	  name->spelling);
  do {
    if (uses && (!defs || uses->scrap < defs->scrap)) {
      fprintf(tex_file, "%d", uses->scrap);
      uses = uses->next;
    }
    else if (defs && (!uses || defs->scrap <= uses->scrap)) {
      if (uses && defs->scrap == uses->scrap)
	uses = uses->next;
      fprintf(tex_file, "\\underline{%d}", defs->scrap);
      defs = defs->next;
    }
    if (uses || defs) fputs(", ", tex_file);
  } while (uses || defs);
  fputs(".\n", tex_file);
}@}



\section{Writing the Output Files} \label{output-files}

@d Function pro...
@{void write_files();
@}

@o output.c
@{void write_files(files)
     Name *files;
{
  while (files) {
    write_files(files->llink);
    @<Write out {\tt files->spelling}@>
    files = files->rlink;
  }
}
@| write_files @}

Due to the filename restrictions on the PC, just use {\tt nuweb~~~.tmp} as
the temporary filename. Since files are processed serially, this should
cause any harm to unix platforms.

@d Write out {\tt files->spelling}
@{{
  FILE *temp_file;
  char temp_name[12]="nuweb~~~.tmp";
/*  sprintf(temp_name, "%s~", files->spelling);  */
  temp_file = fopen(temp_name, "w");
  if (temp_file) {
    char indent_chars[500];
    write_scraps(temp_file, files->defs, 0, indent_chars,
		 files->debug_flag, files->tab_flag, files->indent_flag);
    fclose(temp_file);
    if (compare_flag)
      @<Compare the temp file and the old file@>
    else
      rename(temp_name, files->spelling);
  }
  else {
    fprintf(stderr, "%s: can't open %s\n", command_name, temp_name);
    exit(-1);
  }
}@}


@d Compare the temp file...
@{{
  FILE *old_file = fopen(files->spelling, "r");
  if (old_file) {
    int x, y;
    temp_file = fopen(temp_name, "r");
    do {
      x = getc(old_file);
      y = getc(temp_file);
    } while (x == y && x != EOF);
    fclose(old_file);
    fclose(temp_file);
    if (x == y)
      unlink(temp_name);
    else
      rename(temp_name, files->spelling);
  }
  else
    rename(temp_name, files->spelling);
}@}



\chapter{The Support Routines}

\section{Source Files} \label{source-files}

\subsection{Global Declarations}

We need two routines to handle reading the source files.
@d Function pro...
@{void source_open(); /* pass in the name of the source file */
int source_get();   /* no args; returns the next char or EOF */
@}


There are also two global variables maintained for use in error
messages and such.
@d Global...
@{char *source_name;  /* name of the current file */
int source_line;    /* current line in the source file */
@| source_name source_line @}

\subsection{Local Declarations}


@o input.c
@{static FILE *source_file;  /* the current input file */
static int source_peek;
static int double_at;
static int include_depth;
@| source_peek source_file double_at include_depth @}


@o input.c
@{struct {
  FILE *file;
  char *name;
  int line;
} stack[10];
@| stack @}


\subsection{Reading a File}

The routine {\tt source\_get} returns the next character from the
current source file. It notices newlines and keeps the line counter 
{\tt source\_line} up to date. It also catches {\tt EOF} and watches
for {\tt @@} characters. All other characters are immediately returned.
@o input.c
@{int source_get()
{
  int c = source_peek;
  switch (c) {
    case EOF:  @<Handle {\tt EOF}@>
	       return c;
    case '@@':  @<Handle an ``at'' character@>
	       return c;
    case '\n': source_line++;
    default:   source_peek = getc(source_file);
	       return c;
  }
}
@| source_get @}


This whole {\tt @@}~character handling mess is pretty annoying.
I want to recognize {\tt @@i} so I can handle include files correctly.
At the same time, it makes sense to recognize illegal {\tt @@}~sequences
and complain; this avoids ever having to check anywhere else.
Unfortunately, I need to avoid tripping over the {\tt @@@@}~sequence;
hence this whole unsatisfactory {\tt double\_at} business.
@d Handle an ``at''...
@{{
  c = getc(source_file);
  if (double_at) {
    source_peek = c;
    double_at = FALSE;
    c = '@@';
  }
  else
    switch (c) {
      case 'i': @<Open an include file@>
		break;
      case 'f': case 'm': case 'u':
      case 'd': case 'o': case 'D': case 'O':
      case '{': case '}': case '<': case '>': case '|':
		source_peek = c;
		c = '@@';
		break;
      case '@@': source_peek = c;
		double_at = TRUE;
		break;
      default:  fprintf(stderr, "%s: bad @@ sequence (%s, line %d)\n",
			command_name, source_name, source_line);
		exit(-1);
    }
}@}

@d Open an include file
@{{
  if (include_depth < 10) {
    char name[100];
    @<Collect include-file name@>
    stack[include_depth].name = source_name;
    stack[include_depth].file = source_file;
    stack[include_depth].line = source_line + 1;
    include_depth++;
    source_line = 1;
    source_name = save_string(name);
    source_file = fopen(source_name, "r");
    if (!source_file) {
      fprintf(stderr, "%s: can't open include file %s\n",
	      command_name, source_name);
      exit(-1);
    }
    source_peek = getc(source_file);
    c = source_get();
  }
  else {
    fprintf(stderr, "%s: include nesting too deep (%s, %d)\n",
	    command_name, source_name, source_line);
    exit(-1);
  }
}@}

@d Collect include-file name
@{{
    char *p = name;
    do 
      c = getc(source_file);
    while (c == ' ' || c == '\t');
    while (isgraph(c)) {
      *p++ = c;
      c = getc(source_file);
    }
    *p = '\0';
    if (c != '\n') {
      fprintf(stderr, "%s: unexpected characters after file name (%s, %d)\n",
	      command_name, source_name, source_line);
      exit(-1);
    }
}@}

If an {\tt EOF} is discovered, the current file must be closed and
input from the next stacked file must be resumed. If no more files are
on the stack, the {\tt EOF} is returned.
@d Handle {\tt EOF}
@{{
  fclose(source_file);
  if (include_depth) {
    include_depth--;
    source_file = stack[include_depth].file;
    source_line = stack[include_depth].line;
    source_name = stack[include_depth].name;
    source_peek = getc(source_file);
    c = source_get();
  }
}@}


\subsection{Opening a File}

The routine {\tt source\_open} takes a file name and tries to open the
file. If unsuccessful, it complains and halts. Otherwise, it sets 
{\tt source\_name}, {\tt source\_line}, and {\tt double\_at}.
@o input.c
@{void source_open(name)
     char *name;
{
  source_file = fopen(name, "r");
  if (!source_file) {
    fprintf(stderr, "%s: couldn't open %s\n", command_name, name);
    exit(-1);
  }
  source_name = name;
  source_line = 1;
  source_peek = getc(source_file);
  double_at = FALSE;
  include_depth = 0;
}
@| source_open @}




\section{Scraps} \label{scraps}


@o scraps.c
@{#define SLAB_SIZE 500

typedef struct slab {
  struct slab *next;
  char chars[SLAB_SIZE];
} Slab;
@| Slab SLAB_SIZE @}


@o scraps.c
@{typedef struct {
  char *file_name;
  int file_line;
  Slab *slab;
} ScrapEntry;
@| ScrapEntry @}

@o scraps.c
@{ScrapEntry *SCRAP[256];

#define scrap_array(i) SCRAP[(i) >> 8][(i) & 255]

int scraps;
@| scraps scrap_array SCRAP @}


@d Function pro...
@{void init_scraps();
int collect_scrap();
int write_scraps();
@}


@o scraps.c
@{void init_scraps()
{
  scraps = 1;
  SCRAP[0] = (ScrapEntry *) arena_getmem(256 * sizeof(ScrapEntry));
}
@| init_scraps @}


@o scraps.c
@{typedef struct {
  Slab *scrap;
  int index;
} Manager;
@| Manager @}



@o scraps.c
@{static void push(c, manager)
     char c;
     Manager *manager;
{
  Slab *scrap = manager->scrap;
  int index = manager->index;
  scrap->chars[index++] = c;
  if (index == SLAB_SIZE) {
    Slab *new = (Slab *) arena_getmem(sizeof(Slab));
    scrap->next = new;
    manager->scrap = new;
    index = 0;
  }
  manager->index = index;
}
@| push @}

@o scraps.c
@{static void pushs(s, manager)
     char *s;
     Manager *manager;
{
  while (*s)
    push(*s++, manager);
}
@| pushs @}


@o scraps.c
@{int collect_scrap()
{
  Manager writer;
  @<Create new scrap...@>
  @<Accumulate scrap and return {\tt scraps++}@>
}
@| collect_scrap @}

@d Create new scrap, managed by {\tt writer}
@{{
  Slab *scrap = (Slab *) arena_getmem(sizeof(Slab));
  if ((scraps & 255) == 0)
    SCRAP[scraps >> 8] = (ScrapEntry *) arena_getmem(256 * sizeof(ScrapEntry));
  scrap_array(scraps).slab = scrap;
  scrap_array(scraps).file_name = save_string(source_name);
  scrap_array(scraps).file_line = source_line;
  writer.scrap = scrap;
  writer.index = 0;
}@}


@d Accumulate scrap...
@{{
  int c = source_get();
  while (1) {
    switch (c) {
      case EOF: fprintf(stderr, "%s: unexpect EOF in scrap (%s, %d)\n",
			command_name, scrap_array(scraps).file_name,
			scrap_array(scraps).file_line);
		exit(-1);
      case '@@': @<Handle at-sign during scrap accumulation@>
		break;
      default:  push(c, &writer);
		c = source_get();
		break;
    }
  }
}@}


@d Handle at-sign during scrap accumulation
@{{
  c = source_get();
  switch (c) {
    case '@@': pushs("@@@@", &writer);
	      c = source_get();
	      break;
    case '|': @<Collect user-specified index entries@>
    case '}': push('\0', &writer);
	      return scraps++;
    case '<': @<Handle macro invocation in scrap@>
	      break;
    default : fprintf(stderr, "%s: unexpected @@%c in scrap (%s, %d)\n",
		      command_name, c, source_name, source_line);
	      exit(-1);
  }
}@}


@d Collect user-specified index entries
@{{
  do {
    char new_name[100];
    char *p = new_name;
    do 
      c = source_get();
    while (isspace(c));
    if (c != '@@') {
      Name *name;
      do {
	*p++ = c;
	c = source_get();
      } while (c != '@@' && !isspace(c));
      *p = '\0';
      name = name_add(&user_names, new_name);
      if (!name->defs || name->defs->scrap != scraps) {
	Scrap_Node *def = (Scrap_Node *) arena_getmem(sizeof(Scrap_Node));
	def->scrap = scraps;
	def->next = name->defs;
	name->defs = def;
      }
    }
  } while (c != '@@');
  c = source_get();
  if (c != '}') {
    fprintf(stderr, "%s: unexpected @@%c in scrap (%s, %d)\n",
	    command_name, c, source_name, source_line);
    exit(-1);
  }
}@}


@d Handle macro invocation in scrap
@{{
  Name *name = collect_scrap_name();
  @<Save macro name@>
  @<Add current scrap to {\tt name}'s uses@>
  c = source_get();
}@}


@d Save macro name
@{{
  char *s = name->spelling;
  int len = strlen(s) - 1;
  pushs("@@<", &writer);
  while (len > 0) {
    push(*s++, &writer);
    len--;
  }
  if (*s == ' ')
    pushs("...", &writer);
  else
    push(*s, &writer);
  pushs("@@>", &writer);
}@}


@d Add current scrap to...
@{{
  if (!name->uses || name->uses->scrap != scraps) {
    Scrap_Node *use = (Scrap_Node *) arena_getmem(sizeof(Scrap_Node));
    use->scrap = scraps;
    use->next = name->uses;
    name->uses = use;
  }
}@}


@o scraps.c
@{static char pop(manager)
     Manager *manager;
{
  Slab *scrap = manager->scrap;
  int index = manager->index;
  char c = scrap->chars[index++];
  if (index == SLAB_SIZE) {
    manager->scrap = scrap->next;
    index = 0;
  }
  manager->index = index;
  return c;
}
@| pop @}



@o scraps.c
@{static Name *pop_scrap_name(manager)
     Manager *manager;
{
  char name[100];
  char *p = name;
  int c = pop(manager);
  while (TRUE) {
    if (c == '@@')
      @<Check for end of scrap name and return@>
    else {
      *p++ = c;
      c = pop(manager);
    }
  }
}
@| pop_scrap_name @}


@d Check for end of scrap name...
@{{
  c = pop(manager);
  if (c == '@@') {
    *p++ = c;
    c = pop(manager);
  }
  else if (c == '>') {
    if (p - name > 3 && p[-1] == '.' && p[-2] == '.' && p[-3] == '.') {
      p[-3] = ' ';
      p -= 2;
    }
    *p = '\0';
    return prefix_add(&macro_names, name);
  }
  else {
    fprintf(stderr, "%s: found an internal problem (1)\n", command_name);
    exit(-1);
  }
}@}


@o scraps.c
@{int write_scraps(file, defs, global_indent, indent_chars,
		   debug_flag, tab_flag, indent_flag)
     FILE *file;
     Scrap_Node *defs;
     int global_indent;
     char *indent_chars;
     char debug_flag;
     char tab_flag;
     char indent_flag;
{
  int indent = 0;
  while (defs) {
    @<Copy {\tt defs->scrap} to {\tt file}@>
    defs = defs->next;
  }
  return indent + global_indent;
}
@| write_scraps @}


@d Copy {\tt defs->scrap}...
@{{
  char c;
  Manager reader;
  int line_number = scrap_array(defs->scrap).file_line;
  @<Insert debugging information if required@>
  reader.scrap = scrap_array(defs->scrap).slab;
  reader.index = 0;
  c = pop(&reader);
  while (c) {
    switch (c) {
      case '@@':  @<Check for macro invocation in scrap@>
		 break;
      case '\n': putc(c, file);
		 line_number++;
		 @<Insert appropriate indentation@>
		 break;
      case '\t': @<Handle tab...@>
		 break;
      default:	 putc(c, file);
		 indent_chars[global_indent + indent] = ' ';
		 indent++;
		 break;
    }
    c = pop(&reader);
  }
}@}


@d Insert debugging information if required
@{if (debug_flag) {
  fprintf(file, "\n#line %d \"%s\"\n",
	  line_number, scrap_array(defs->scrap).file_name);
  @<Insert appropr...@>
}@}


@d Insert approp...
@{{
  if (indent_flag) {
    if (tab_flag)
      for (indent=0; indent<global_indent; indent++)
	putc(' ', file);
    else
      for (indent=0; indent<global_indent; indent++)
	putc(indent_chars[indent], file);
  }
  indent = 0;
}@}


@d Handle tab characters on output
@{{
  if (tab_flag)
    @<Expand tab...@>
  else {
    putc('\t', file);
    indent_chars[global_indent + indent] = '\t';
    indent++;
  }
}@}



@d Check for macro invocation...
@{{
  c = pop(&reader);
  switch (c) {
    case '@@': putc(c, file);
	      indent_chars[global_indent + indent] = ' ';
	      indent++;
	      break;
    case '<': @<Copy macro into {\tt file}@>
	      @<Insert debugging information if required@>
	      break;
    default:  /* ignore, since we should already have a warning */
	      break;
  }
}@}


@d Copy macro into...
@{{
  Name *name = pop_scrap_name(&reader);
  if (name->mark) {
    fprintf(stderr, "%s: recursive macro discovered involving <%s>\n",
	    command_name, name->spelling);
    exit(-1);
  }
  if (name->defs) {
    name->mark = TRUE;
    indent = write_scraps(file, name->defs, global_indent + indent,
			  indent_chars, debug_flag, tab_flag, indent_flag);
    indent -= global_indent;
    name->mark = FALSE;
  }
  else if (!tex_flag)
    fprintf(stderr, "%s: macro never defined <%s>\n",
	    command_name, name->spelling);
}@}


\section{Names} \label{names}

@d Type de...
@{typedef struct scrap_node {
  struct scrap_node *next;
  int scrap;
} Scrap_Node;
@| Scrap_Node @}


@d Type de...
@{typedef struct name {
  char *spelling;
  struct name *llink;
  struct name *rlink;
  Scrap_Node *defs;
  Scrap_Node *uses;
  int mark;
  char tab_flag;
  char indent_flag;
  char debug_flag;
} Name;
@| Name @}

@d Global...
@{Name *file_names;
Name *macro_names;
Name *user_names;
@| file_names macro_names user_names @}

@d Function pro...
@{Name *collect_file_name();
Name *collect_macro_name();
Name *collect_scrap_name();
Name *name_add();
Name *prefix_add();
char *save_string();
void reverse_lists();
@}

@o names.c
@{enum { LESS, GREATER, EQUAL, PREFIX, EXTENSION };

static int compare(x, y)
     char *x;
     char *y;
{
  int len, result;
  int xl = strlen(x);
  int yl = strlen(y);
  int xp = x[xl - 1] == ' ';
  int yp = y[yl - 1] == ' ';
  if (xp) xl--;
  if (yp) yl--;
  len = xl < yl ? xl : yl;
  result = strncmp(x, y, len);
  if (result < 0) return GREATER;
  else if (result > 0) return LESS;
  else if (xl < yl) {
    if (xp) return EXTENSION;
    else return LESS;
  }
  else if (xl > yl) {
    if (yp) return PREFIX;
    else return GREATER;
  }
  else return EQUAL;
}
@| compare LESS GREATER EQUAL PREFIX EXTENSION @}


@o names.c
@{char *save_string(s)
     char *s;
{
  char *new = (char *) arena_getmem((strlen(s) + 1) * sizeof(char));
  strcpy(new, s);
  return new;
}
@| save_string @}

@o names.c
@{Name *prefix_add(root, spelling)
     Name **root;
     char *spelling;
{
  Name *node = *root;
  while (node) {
    switch (compare(node->spelling, spelling)) {
    case GREATER:   root = &node->rlink;
		    break;
    case LESS:      root = &node->llink;
		    break;
    case EQUAL:     return node;
    case EXTENSION: node->spelling = save_string(spelling);
		    return node;
    case PREFIX:    @<Check for ambiguous prefix@>
		    return node;
    }
    node = *root;
  }
  @<Create new name entry@>
}
@| prefix_add @}

Since a very short prefix might match more than one macro name, I need
to check for other matches to avoid mistakes. Basically, I simply
continue the search down {\em both\/} branches of the tree.

@d Check for ambiguous prefix
@{{
  if (ambiguous_prefix(node->llink, spelling) ||
      ambiguous_prefix(node->rlink, spelling))
    fprintf(stderr,
	    "%s: ambiguous prefix @@<%s...@@> (%s, line %d)\n",
	    command_name, spelling, source_name, source_line);
}@}

@o names.c
@{static int ambiguous_prefix(node, spelling)
     Name *node;
     char *spelling;
{
  while (node) {
    switch (compare(node->spelling, spelling)) {
    case GREATER:   node = node->rlink;
		    break;
    case LESS:      node = node->llink;
		    break;
    case EQUAL:
    case EXTENSION:
    case PREFIX:    return TRUE;
    }
  }
  return FALSE;
}
@}

Rob Shillingsburg suggested that I organize the index of
user-specified identifiers more traditionally; that is, not relying on
strict {\small ASCII} comparisons via {\tt strcmp}. Ideally, we'd like
to see the index ordered like this:
\begin{quote}
\begin{flushleft}
aardvark \\
Adam \\
atom \\
Atomic \\
atoms
\end{flushleft}
\end{quote}
The function {\tt robs\_strcmp} implements the desired predicate.

@o names.c
@{static int robs_strcmp(x, y)
     char *x;
     char *y;
{
  char *xx = x;
  char *yy = y;
  int xc = toupper(*xx);
  int yc = toupper(*yy);
  while (xc == yc && xc) {
    xx++;
    yy++;
    xc = toupper(*xx);
    yc = toupper(*yy);
  }
  if (xc != yc) return xc - yc;
  xc = *x;
  yc = *y;
  while (xc == yc && xc) {
    x++;
    y++;
    xc = *x;
    yc = *y;
  }
  if (isupper(xc) && islower(yc))
    return xc * 2 - (toupper(yc) * 2 + 1);
  if (islower(xc) && isupper(yc))
    return toupper(xc) * 2 + 1 - yc * 2;
  return xc - yc;
}
@| robs_strcmp @}

@o names.c
@{Name *name_add(root, spelling)
     Name **root;
     char *spelling;
{
  Name *node = *root;
  while (node) {
    int result = robs_strcmp(node->spelling, spelling);
    if (result > 0)
      root = &node->llink;
    else if (result < 0)
      root = &node->rlink;
    else
      return node;
    node = *root;
  }
  @<Create new name entry@>
}
@| name_add @}


@d Create new name...
@{{
  node = (Name *) arena_getmem(sizeof(Name));
  node->spelling = save_string(spelling);
  node->mark = FALSE;
  node->llink = NULL;
  node->rlink = NULL;
  node->uses = NULL;
  node->defs = NULL;
  node->tab_flag = TRUE;
  node->indent_flag = TRUE;
  node->debug_flag = FALSE;
  *root = node;
  return node;
}@}


Name terminated by whitespace.  Also check for ``per-file'' flags. Keep
skipping white space until we reach scrap.
@o names.c
@{Name *collect_file_name()
{
  Name *new_name;
  char name[100];
  char *p = name;
  int start_line = source_line;
  int c = source_get();
  while (isspace(c))
    c = source_get();
  while (isgraph(c)) {
    *p++ = c;
    c = source_get();
  }
  if (p == name) {
    fprintf(stderr, "%s: expected file name (%s, %d)\n",
	    command_name, source_name, start_line);
    exit(-1);
  }
  *p = '\0';
  new_name = name_add(&file_names, name);
  @<Handle optional per-file flags@>
  if (c == '@@' && source_get() == '{')
    return new_name;
  else {
    fprintf(stderr, "%s: expected @@{ after file name (%s, %d)\n",
	    command_name, source_name, start_line);
    exit(-1);
  }
}
@| collect_file_name @}

@d Handle optional per-file flags
@{{
  while (1) {
    while (isspace(c))
      c = source_get();
    if (c == '-') {
      c = source_get();
      do {
	switch (c) {
	  case 't': new_name->tab_flag = FALSE;
		    break;
	  case 'd': new_name->debug_flag = TRUE;
		    break;
	  case 'i': new_name->indent_flag = FALSE;
		    break;
	  default : fprintf(stderr, "%s: unexpected per-file flag (%s, %d)\n",
			    command_name, source_name, source_line);
		    break;
	}
	c = source_get();
      } while (!isspace(c));
    }
    else break;
  }
}@}



Name terminated by \verb+\n+ or \verb+@@{+; but keep skipping until \verb+@@{+
@o names.c
@{Name *collect_macro_name()
{
  char name[100];
  char *p = name;
  int start_line = source_line;
  int c = source_get();
  while (isspace(c))
    c = source_get();
  while (c != EOF) {
    switch (c) {
      case '@@':  @<Check for terminating at-sequence and return name@>
		 break;
      case '\t':
      case ' ':  *p++ = ' ';
		 do
		   c = source_get();
		 while (c == ' ' || c == '\t');
		 break;
      case '\n': @<Skip until scrap begins, then return name@>
      default:	 *p++ = c;
		 c = source_get();
		 break;
    }
  }
  fprintf(stderr, "%s: expected macro name (%s, %d)\n",
	  command_name, source_name, start_line);
  exit(-1);
}
@| collect_macro_name @}


@d Check for termina...
@{{
  c = source_get();
  switch (c) {
    case '@@': *p++ = c;
	      break;
    case '{': @<Cleanup and install name@>
    default:  fprintf(stderr,
		      "%s: unexpected @@%c in macro name (%s, %d)\n",
		      command_name, c, source_name, start_line);
	      exit(-1);
  }
}@}


@d Cleanup and install name
@{{
  if (p > name && p[-1] == ' ')
    p--;
  if (p - name > 3 && p[-1] == '.' && p[-2] == '.' && p[-3] == '.') {
    p[-3] = ' ';
    p -= 2;
  }
  if (p == name || name[0] == ' ') {
    fprintf(stderr, "%s: empty scrap name (%s, %d)\n",
	    command_name, source_name, source_line);
    exit(-1);
  }
  *p = '\0';
  return prefix_add(&macro_names, name);
}@}


@d Skip until scrap...
@{{
  do
    c = source_get();
  while (isspace(c));
  if (c == '@@' && source_get() == '{')
    @<Cleanup and install name@>
  else {
    fprintf(stderr, "%s: expected @@{ after macro name (%s, %d)\n",
	    command_name, source_name, start_line);
    exit(-1);
  }
}@}


Terminated by \verb+@@>+
@o names.c
@{Name *collect_scrap_name()
{
  char name[100];
  char *p = name;
  int c = source_get();
  while (c == ' ' || c == '\t')
    c = source_get();
  while (c != EOF) {
    switch (c) {
      case '@@':  @<Look for end of scrap name and return@>
		 break;
      case '\t':
      case ' ':  *p++ = ' ';
		 do
		   c = source_get();
		 while (c == ' ' || c == '\t');
		 break;
      default:	 if (isgraph(c)) {
		   *p++ = c;
		   c = source_get();
		 }
		 else {
		   fprintf(stderr,
			   "%s: unexpected character in macro name (%s, %d)\n",
			   command_name, source_name, source_line);
		   exit(-1);
		 }
		 break;
    }
  }
  fprintf(stderr, "%s: unexpected end of file (%s, %d)\n",
	  command_name, source_name, source_line);
  exit(-1);
}
@| collect_scrap_name @}


@d Look for end of scrap name...
@{{
  c = source_get();
  switch (c) {
    case '@@': *p++ = c;
	      c = source_get();
	      break;
    case '>': @<Cleanup and install name@>
    default:  fprintf(stderr,
		      "%s: unexpected @@%c in macro name (%s, %d)\n",
		      command_name, c, source_name, source_line);
	      exit(-1);
  }
}@}


@o names.c
@{static Scrap_Node *reverse();	/* a forward declaration */

void reverse_lists(names)
     Name *names;
{
  while (names) {
    reverse_lists(names->llink);
    names->defs = reverse(names->defs);
    names->uses = reverse(names->uses);
    names = names->rlink;
  }
}
@| reverse_lists @}

Just for fun, here's a non-recursive version of the traditional list
reversal code. Note that it reverses the list in place; that is, it
does no new allocations.
@o names.c
@{static Scrap_Node *reverse(a)
     Scrap_Node *a;
{
  if (a) {
    Scrap_Node *b = a->next;
    a->next = NULL;
    while (b) {
      Scrap_Node *c = b->next;
      b->next = a;
      a = b;
      b = c;
    }
  }
  return a;
}
@| reverse @}


\section{Searching for Index Entries} \label{search}

Given the array of scraps and a set of index entries, we need to
search all the scraps for occurences of each entry. The obvious
approach to this problem would be quite expensive for large documents;
however, there is an interesting  paper describing an efficient
solution~\cite{aho:75}.


@o scraps.c
@{
typedef struct name_node {
  struct name_node *next;
  Name *name;
} Name_Node;

typedef struct goto_node {
  struct goto_node *next;	/* next goto node with same depth */
  struct goto_node *fail;
  struct move_node *moves;
  Name_Node *output;
} Goto_Node;

typedef struct move_node {
  struct move_node *next;
  Goto_Node *state;
  char c;
} Move_Node;
@| Goto_Node Move_Node Name_Node @}

@o scraps.c
@{
static Goto_Node *root[128];
static int max_depth;
static Goto_Node **depths;
@| root max_depth @}


@o scraps.c
@{static Goto_Node *goto_lookup(c, g)
     char c;
     Goto_Node *g;
{
  Move_Node *m = g->moves;
  while (m && m->c != c)
    m = m->next;
  if (m)
    return m->state;
  else
    return NULL;
}
@| goto_lookup @}


\subsection{Building the Automata}


@o scraps.c
@{
static void build_gotos();

void search()
{
  int i;
  for (i=0; i<128; i++)
    root[i] = NULL;
  max_depth = 10;
  depths = (Goto_Node **) arena_getmem(max_depth * sizeof(Goto_Node *));
  for (i=0; i<max_depth; i++)
    depths[i] = NULL;
  build_gotos(user_names);
  @<Build failure functions@>
  @<Search scraps@>
}
@| search @}



@o scraps.c
@{static void build_gotos(tree)
     Name *tree;
{
  while (tree) {
    @<Extend goto graph with {\tt tree->spelling}@>
    build_gotos(tree->rlink);
    tree = tree->llink;
  }
}
@| build_gotos @}

@d Extend goto...
@{{
  int depth = 2;
  char *p = tree->spelling;
  char c = *p++;
  Goto_Node *q = root[c];
  if (!q) {
    q = (Goto_Node *) arena_getmem(sizeof(Goto_Node));
    root[c] = q;
    q->moves = NULL;
    q->fail = NULL;
    q->moves = NULL;
    q->output = NULL;
    q->next = depths[1];
    depths[1] = q;
  }
  while (c = *p++) {
    Goto_Node *new = goto_lookup(c, q);
    if (!new) {
      Move_Node *new_move = (Move_Node *) arena_getmem(sizeof(Move_Node));
      new = (Goto_Node *) arena_getmem(sizeof(Goto_Node));
      new->moves = NULL;
      new->fail = NULL;
      new->moves = NULL;
      new->output = NULL;
      new_move->state = new;
      new_move->c = c;
      new_move->next = q->moves;
      q->moves = new_move;
      if (depth == max_depth) {
	int i;
	Goto_Node **new_depths =
	    (Goto_Node **) arena_getmem(2*depth*sizeof(Goto_Node *));
	max_depth = 2 * depth;
	for (i=0; i<depth; i++)
	  new_depths[i] = depths[i];
	depths = new_depths;
	for (i=depth; i<max_depth; i++)
	  depths[i] = NULL;
      }
      new->next = depths[depth];
      depths[depth] = new;
    }
    q = new;
    depth++;
  }
  q->output = (Name_Node *) arena_getmem(sizeof(Name_Node));
  q->output->next = NULL;
  q->output->name = tree;
}@}


@d Build failure functions
@{{
  int depth;
  for (depth=1; depth<max_depth; depth++) {
    Goto_Node *r = depths[depth];
    while (r) {
      Move_Node *m = r->moves;
      while (m) {
	char a = m->c;
	Goto_Node *s = m->state;
	Goto_Node *state = r->fail;
	while (state && !goto_lookup(a, state))
	  state = state->fail;
	if (state)
	  s->fail = goto_lookup(a, state);
	else
	  s->fail = root[a];
	if (s->fail) {
	  Name_Node *p = s->fail->output;
	  while (p) {
	    Name_Node *q = (Name_Node *) arena_getmem(sizeof(Name_Node));
	    q->name = p->name;
	    q->next = s->output;
	    s->output = q;
	    p = p->next;
	  }
	}
	m = m->next;
      }
      r = r->next;
    }
  }
}@}


\subsection{Searching the Scraps}

@d Search scraps
@{{
  for (i=1; i<scraps; i++) {
    char c;
    Manager reader;
    Goto_Node *state = NULL;
    reader.scrap = scrap_array(i).slab;
    reader.index = 0;
    c = pop(&reader);
    while (c) {
      while (state && !goto_lookup(c, state))
	state = state->fail;
      if (state)
	state = goto_lookup(c, state);
      else
	state = root[c];
      if (state && state->output) {
	Name_Node *p = state->output;
	do {
	  Name *name = p->name;
	  if (!name->uses || name->uses->scrap != i) {
	    Scrap_Node *new_use =
		(Scrap_Node *) arena_getmem(sizeof(Scrap_Node));
	    new_use->scrap = i;
	    new_use->next = name->uses;
	    name->uses = new_use;
	  }
	  p = p->next;
	} while (p);
      }
      c = pop(&reader);
    }
  }
}@}


\section{Memory Management} \label{memory-management}

I manage memory using a simple scheme inspired by Hanson's idea of
{\em arenas\/}~\cite{hanson:90}.
Basically, I allocate all the storage required when processing a
source file (primarily for names and scraps) using calls to 
{\tt arena\_getmem(n)}, where {\tt n} specifies the number of bytes to
be allocated. When the storage is no longer required, the entire arena
is freed with a single call to  {\tt arena\_free()}. Both operations
are quite fast.
@d Function p...
@{void *arena_getmem();
void arena_free();
@}


@o arena.c
@{typedef struct chunk {
  struct chunk *next;
  char *limit;
  char *avail;
} Chunk;
@| Chunk @}


We define an empty chunk called {\tt first}. The variable {\tt arena} points
at the current chunk of memory; it's initially pointed at {\tt first}.
As soon as some storage is required, a ``real'' chunk of memory will
be allocated and attached to {\tt first->next}; storage will be
allocated from the new chunk (and later chunks if necessary).
@o arena.c
@{Chunk first = { NULL, NULL, NULL };
Chunk *arena = &first;
@| first arena @}


\subsection{Allocating Memory}

The routine {\tt arena\_getmem(n)} returns a pointer to (at least) 
{\tt n} bytes of memory. Note that {\tt n} is rounded up to ensure
that returned pointers are always aligned.
@o arena.c
@{void *arena_getmem(n)
     int n;
{
  char *q;
  char *p = arena->avail;
  n = (n + 3) & ~3;		/* ensuring alignment to 4 bytes */
  q = p + n;
  if (q <= arena->limit) {
    arena->avail = q;
    return p;
  }
  @<Find a new chunk of memory@>
}
@| arena_getmem @}


If the current chunk doesn't have adequate space (at least {\tt n}
bytes) we examine the rest of the list of chunks (starting at 
{\tt arena->next}) looking for a chunk with adequate space. If {\tt n}
is very large, we may not find it right away or we may not find a
suitable chunk at all.
@d Find a new chunk...
@{{
  Chunk *ap = arena;
  Chunk *np = ap->next;
  while (np) {
    char *v = sizeof(Chunk) + (char *) np;
    if (v + n <= np->limit) {
      np->avail = v + n;
      arena = np;
      return v;
    }
    ap = np;
    np = ap->next;
  }
  @<Allocate a new chunk of memory@>
}@}


If there isn't a suitable chunk of memory on the free list, then we
need to allocate a new one.
@d Allocate a new ch...
@{{
  int m = n + 10000;
  np = (Chunk *) malloc(m);
  np->limit = m + (char *) np;
  np->avail = n + sizeof(Chunk) + (char *) np;
  np->next = NULL;
  ap->next = np;
  arena = np;
  return sizeof(Chunk) + (char *) np;
}@}


\subsection{Freeing Memory}

To free all the memory in the arena, we need only point {\tt arena}
back to the first empty chunk.
@o arena.c
@{void arena_free()
{
  arena = &first;
}
@| arena_free @}


\chapter{Indices} \label{indices}

Three sets of indices can be created automatically: an index of file
names, an index of macro names, and an index of user-specified
identifiers. An index entry includes the name of the entry, where it
was defined, and where it was referenced.



\section{Files}

@f

\section{Macros}

@m

\section{Identifiers}

Knuth prints his index of indentifiers in a two-column format.
I could force this automatically by emitting the \verb|\twocolumn|
command; but this has the side effect of forcing a new page.
Therefore, it seems better to leave it this up to the user.

@u

\bibliographystyle{plain}
\bibliography{literate}

\end{document}