summaryrefslogtreecommitdiff
path: root/language/xettex/waits-xe.ch
blob: 9cf606f28c9849f5fd73be73cba35bb0b4eea714 (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
@x Tell WEAVE to print only the changes:
  \def\?##1]{\hbox to 1in{\hfil##1.\ }}
  }
@y
  \def\?##1]{\hbox{Changes to \hbox to 1em{\hfil##1}.\ }}
  }
\let\maybe=\iffalse
@z

@x WAITS's banner: [2]
@d banner=='This is TeX, Version 2.0' {printed when \TeX\ starts}
@y
@d banner=='This is TeX-XeT, WAITS Version 2.0' {printed when \TeX\ starts}
@z

@x Switches for debugging and statistics: (7)
@d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering
  usage statistics}
@d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering
  usage statistics}
@f stat==begin
@f tats==end
@y
@d debug== {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed== {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat== {change this to `$\\{stat}\equiv\.{@@\{}$' when not
  gathering usage statistics}
@d tats== {change this to `$\\{tats}\equiv\.{@@\}}$' when not
  gathering usage statistics}
@f stat==begin
@f tats==end
@z

@x The INITEX switch: (8)
@d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version}
@d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version}
@y [In this case, no change is made]
@d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version}
@d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version}
@z

@x Compiler directives: (9)
@{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
@y
@{@&$C-,A+,D-,W+,H:0@}
  {no range check, catch arithmetic overflow, no debug overhead, no heap}
@!debug @{@&$C+,D:5,W+@}@+ gubed {but turn everything on when debugging}
{the `|W+|' switch catches more syntax errors}
{the `\ignorespaces|D:5|' avoids initial stop for the debugger}
@z

@x Compile-time constants: [11]
@!mem_max=30000; {greatest index in \TeX's internal |mem| array;
  must be strictly less than |max_halfword|;
  must be equal to |mem_top| in \.{INITEX}, otherwise |>=mem_top|}
@!mem_min=0; {smallest index in \TeX's internal |mem| array;
  must be |min_halfword| or more;
  must be equal to |mem_bot| in \.{INITEX}, otherwise |<=mem_bot|}
@!buf_size=500; {maximum number of characters simultaneously present in
  current lines of open files and in control sequences between
  \.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|}
@!error_line=72; {width of context lines on terminal error messages}
@!half_error_line=42; {width of first lines of contexts in terminal
  error messages; should be between 30 and |error_line-15|}
@!max_print_line=79; {width of longest text lines output; should be at least 60}
@!stack_size=200; {maximum number of simultaneous input sources}
@!max_in_open=6; {maximum number of input files and error insertions that
  can be going on simultaneously}
@!font_max=75; {maximum internal font number; must not exceed |max_quarterword|
  and must be at most |font_base+256|}
@!font_mem_size=20000; {number of words of |font_info| for all fonts}
@!param_size=60; {maximum number of simultaneous macro parameters}
@!nest_size=40; {maximum number of semantic levels simultaneously active}
@!max_strings=3000; {maximum number of strings; must not exceed |max_halfword|}
@!string_vacancies=8000; {the minimum number of characters that should be
  available for the user's control sequences and font names,
  after \TeX's own error messages are stored}
@!pool_size=32000; {maximum number of characters in strings, including all
  error messages and help texts, and the names of all fonts and
  control sequences; must exceed |string_vacancies| by the total
  length of \TeX's own strings, which is currently about 23000}
@!save_size=600; {space for saving values outside of current group; must be
  at most |max_halfword|}
@!trie_size=8000; {space for hyphenation patterns; should be larger for
  \.{INITEX} than it is in production versions of \TeX}
@!dvi_buf_size=800; {size of the output buffer; must be a multiple of 8}
@!file_name_size=40; {file names shouldn't be longer than this}
@!pool_name='TeXformats:TEX.POOL                     ';
  {string of length |file_name_size|; tells where the string pool appears}
@.TeXformats@>
@y
@!mem_max=32000; {greatest index in \TeX's internal |mem| array;
  must be strictly less than |max_halfword|;
  must be equal to |mem_top| in \.{INITEX}, otherwise |>=mem_top|}
@!mem_min=16000; {smallest index in \TeX's internal |mem| array;
  must be |min_halfword| or more;
  must be equal to |mem_bot| in \.{INITEX}, otherwise |<=mem_bot|}
@!buf_size=500; {maximum number of characters simultaneously present in
  current lines of open files and in control sequences between
  \.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|}
@!error_line=80; {width of context lines on terminal error messages}
@!half_error_line=50; {width of first lines of contexts in terminal
  error messages; should be between 30 and |error_line-15|}
@!max_print_line=80; {width of longest text lines output; should be at least 60}
@!stack_size=200; {maximum number of simultaneous input sources}
@!max_in_open=6; {maximum number of input files and error insertions that
  can be going on simultaneously}
@!font_max=100;
  {maximum internal font number; must not exceed |max_quarterword|}
@!font_mem_size=23000; {number of words of |font_info| for all fonts}
@!param_size=60; {maximum number of simultaneous macro parameters}
@!nest_size=40; {maximum number of semantic levels simultaneously active}
@!max_strings=3500; {maximum number of strings}
@!string_vacancies=8000; {the minimum number of characters that should be
  available for the user's control sequences and font names,
  after \TeX's own error messages are stored}
@!pool_size=40000; {maximum number of characters in strings, including all
  error messages and help texts, and the names of all fonts and
  control sequences; must exceed |string_vacancies| by the total
  length of \TeX's own strings, which is currently about 22500}
@!save_size=300; {space for saving values outside of current group; must be
  at most |max_halfword|}
@!trie_size=8000; {space for hyphenation patterns; should be larger for
  \.{INITEX} than it is in production versions of \TeX}
@!dvi_buf_size=800; {size of the output buffer; must be a multiple of 8}
@!file_name_size=23; {file names shouldn't be longer than this}
@!pool_name='XET.POOL[XET,DEK]      ';
  {string of length |file_name_size|; tells where the string pool appears}
@z

@x TANGLE-time constants: (12)
@d mem_bot=0 {smallest index in the |mem| array dumped by \.{INITEX};
  must not be less than |mem_min|}
@d mem_top==30000 {largest index in the |mem| array dumped by \.{INITEX};
  must be substantially larger than |mem_bot|
  and not greater than |mem_max|}
@d font_base=0 {smallest internal font number; must not be less
  than |min_quarterword|}
@d hash_size=2100 {maximum number of control sequences; it should be at most
  about |(mem_max-mem_min)/10|, but 2100 is already quite generous}
@d hash_prime=1777 {a prime number equal to about 85\% of |hash_size|}
@d hyph_size=307 {another prime; the number of \.{\\hyphenation} exceptions}
@y
@d mem_bot=16000 {smallest index in the |mem| array dumped by \.{INITEX};
  must not be less than |mem_min|}
@d mem_top==32000 {largest index in the |mem| array dumped by \.{INITEX};
  must be substantially larger than |mem_bot|
  and not greater than |mem_max|}
@d font_base=0 {smallest internal font number; must not be less
  than |min_quarterword|}
@d hash_size=2500 {maximum number of control sequences; it should be at most
  about |(mem_max-mem_min)/10|, but 2100 is already quite generous}
@d hash_prime=2113 {a prime number equal to about 85\% of |hash_size|}
@d hyph_size=307 {another prime; the number of \.{\\hyphenation} exceptions}
@z

@x System-dependent character set changes: (23)
@^character set dependencies@>
@^system dependencies@>

@<Set init...@>=
for i:=1 to @'37 do xchr[i]:=' ';
@y
@^character set dependencies@>
@^system dependencies@>

The code shown here is intended to be used on the Stanford {\sc SAIL} system,
and at other installations like CMU and ISI where essentially the same
extended character set is used. The fact that {\mc SAIL} has |'}'| in the
wrong place turns out to cause no difficulty in this case.

@<Set initial values...@>=
for i:=1 to @'37 do xchr[i]:=chr(i);
xchr[@'30]:=chr(@'137);
xchr[@'32]:=chr(@'33); {|not_equal| sign}
xchr[@'33]:=chr(@'176);
@z

@x Opening files: (27)
@d reset_OK(#)==erstat(#)=0
@d rewrite_OK(#)==erstat(#)=0

@p function a_open_in(var f:alpha_file):boolean;
  {open a text file for input}
begin reset(f,name_of_file,'/O'); a_open_in:=reset_OK(f);
end;
@#
function a_open_out(var f:alpha_file):boolean;
  {open a text file for output}
begin rewrite(f,name_of_file,'/O'); a_open_out:=rewrite_OK(f);
end;
@#
function b_open_in(var f:byte_file):boolean;
  {open a binary file for input}
begin reset(f,name_of_file,'/O'); b_open_in:=reset_OK(f);
end;
@#
function b_open_out(var f:byte_file):boolean;
  {open a binary file for output}
begin rewrite(f,name_of_file,'/O'); b_open_out:=rewrite_OK(f);
end;
@#
function w_open_in(var f:word_file):boolean;
  {open a word file for input}
begin reset(f,name_of_file,'/O'); w_open_in:=reset_OK(f);
end;
@#
function w_open_out(var f:word_file):boolean;
  {open a word file for output}
begin rewrite(f,name_of_file,'/O'); w_open_out:=rewrite_OK(f);
end;
@y
@d reset_OK(#)==erstat(#) mod @'20000 = 0
@d rewrite_OK(#)==erstat(#) mod @'20000 = 0

@p function erstat(var f:file):integer; extern;@t/2@>
@#
function a_open_in(var f:alpha_file):boolean;
  {open a text file for input}
begin reset(f,name_of_file,'/E/O/N:9');
  {the \.{/E} switch distinguishes |form_feed| from |carriage_return|;
  the \.{/O} switch gives error control to us;
  and the \.{/N:9} switch specifies 9 buffers, which
  seems to work satisfactorily at {\mc SAIL}}
a_open_in:=reset_OK(f);
end;
@#
function a_open_out(var f:alpha_file):boolean;
  {open a text file for output}
begin rewrite(f,name_of_file,'/O/N:2'); a_open_out:=rewrite_OK(f);
end; {two buffers seems adequate for text output files}
@#
function b_open_in(var f:byte_file):boolean;
  {open a binary file for input}
begin reset(f,name_of_file,'/B:8/O/N:2'); b_open_in:=reset_OK(f);
end;  {the \.{/B} switch is necessary to get byte packing}
@#
function b_open_out(var f:byte_file):boolean;
  {open a binary file for output}
begin rewrite(f,name_of_file,'/O/N:9/P:256'); b_open_out:=rewrite_OK(f);
end;    {here we use |ary_out| so the \.{/B} switch isn't appropriate}
  {the \.{/P:256} sets file protection to `dump never'}
@#
function w_open_in(var f:word_file):boolean;
  {open a word file for input}
begin reset(f,name_of_file,'/O/N:9'); w_open_in:=reset_OK(f);
end;
@#
function w_open_out(var f:word_file):boolean;
  {open a word file for output}
begin rewrite(f,name_of_file,'/O/N:9'); w_open_out:=rewrite_OK(f);
end;
@z

@x New input_ln: (31)
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables called
|buffer|, |first|, and |last| that will be described in detail later; for
now, it suffices for us to know that |buffer| is an array of |ASCII_code|
values, and that |first| and |last| are indices into this array
representing the beginning and ending of a line of text.

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@y
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables
called |buffer|, |first|, and |last| that will be described in detail
later; for now, it suffices for us to know that |buffer| is an array of
|ASCII_code| values, and that |first| and |last| are indices into this
array representing the beginning and ending of a line of text.

We will read the lines first into an auxiliary buffer, in order to
save the running time of procedure-call overhead. This uses a nice
feature of \ph\ that Knuth chose not to mention in \TeX82.
@^Knuth, Donald Ervin@>

At {\mc SAIL} we want to recognize page marks (indicated by |form_feed|
characters), and keep track of the current page number.

@d form_feed=@'14 {ASCII code used at end of a page}

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@!aux_buf:array[0..70] of text_char; {where the characters go first}
@^system dependencies@>
@z
@x
@p function input_ln(var f:alpha_file;@!bypass_eoln:boolean):boolean;
  {inputs the next line or returns |false|}
var last_nonblank:0..buf_size; {|last| with trailing blanks removed}
begin if bypass_eoln then if not eof(f) then get(f);
  {input the first character of the line into |f^|}
last:=first; {cf.\ Matthew 19\thinspace:\thinspace30}
if eof(f) then input_ln:=false
else  begin last_nonblank:=first;
  while not eoln(f) do
    begin if last>=max_buf_stack then
      begin max_buf_stack:=last+1;
      if max_buf_stack=buf_size then
        overflow("buffer size",buf_size);
@:TeX capacity exceeded buffer size}{\quad buffer size@>
      end;
    buffer[last]:=xord[f^]; get(f); incr(last);
    if buffer[last-1]<>" " then last_nonblank:=last;
    end;
  last:=last_nonblank; input_ln:=true;
  end;
end;
@y
@p function input_ln(var f:alpha_file;@!bypass_eoln:boolean):boolean;
  {inputs the next line or returns |false|}
label 1,done;
var n: integer;
@!k,@!m: 0..buf_size; {indices into |buffer|}
begin if bypass_eoln then {input the first character of the line into |f^|}
  begin if not eof(f) then get(f);
  if not eof(f) then if f^=chr(@'12) then get(f); {skip past a |line_feed|}
  end;
last:=first;
if eof(f) then input_ln:=false
else  begin read(f,aux_buf:n);
  if buffer[first]=form_feed then {previous line was end-of-page}
    begin incr(page); line:=1; {adjust line and page numbers}
    end;
1:  if last+n>max_buf_stack then
    if last+n>=buf_size then
      begin max_buf_stack:=buf_size;
      overflow("buffer size",buf_size);
@:TeX capacity exceeded buffer size}{\quad buffer size@>
      end
    else max_buf_stack:=last+n;
  if n>0 then
    begin m:=last;
    if n=72 then last:=m+71@+else last:=m+n;
    for k:=m to last-1 do buffer[k]:=xord[aux_buf[k-m]];
    if n=72 then {there's more on this line}
      begin read(f,aux_buf:n); goto 1;
      end;
    end
  else if f^=chr(form_feed) then {end of page}
    begin aux_buf[0]:=f^; n:=1; goto 1;
    end;
  loop@+  begin if last=first then goto done;
    if buffer[last-1]<>" " then goto done;
    decr(last);
    end;
done:  input_ln:=true;
  end;
end;
@^system dependencies@>
@z

@x Terminal I/O: (32)
is considered an output file the file variable is |term_out|.
@^system dependencies@>

@<Glob...@>=
@!term_in:alpha_file; {the terminal as an input file}
@!term_out:alpha_file; {the terminal as an output file}

@ Here is how to open the terminal files
in \ph. The `\.{/I}' switch suppresses the first |get|.
@^system dependencies@>

@d t_open_in==reset(term_in,'TTY:','/O/I') {open the terminal for text input}
@d t_open_out==rewrite(term_out,'TTY:','/O') {open the terminal for text output}
@y
is considered an output file the file variable is |term_out|.
On WAITS, this point is moot, since we use the built-in |TTY| file.
@^system dependencies@>

@d term_in==TTY {the terminal as an input file}
@d term_out==TTY {the terminal as an output file}

@ Here is how to open the terminal files on WAITS: we don't do anything,
since |TTY| is always open.  Note that |eoln(term_in)| is initially |true|.
@^system dependencies@>

@d t_open_in==do_nothing {open the terminal for text input}
@d t_open_out==do_nothing {open the terminal for text output}
@z

@x Special terminal controls: (34)
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == do_nothing {cancel the user's cancellation of output}
@y
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == begin if inskp0 then end
  {cancel the user's cancellation of output}

@<Error handling procedures@>=
function inskp0:boolean; extern;
@z

@x Initializing the terminal: (37)
@ The following program does the required initialization
without retrieving a possible command line.
It should be clear how to modify this routine to deal with command lines,
if the system permits them.
@^system dependencies@>

@p function init_terminal:boolean; {gets the terminal input started}
label exit;
begin t_open_in;
loop@+begin wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
  if not input_ln(term_in,true) then {this shouldn't happen}
    begin write_ln(term_out);
    write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
    init_terminal:=false; return;
    end;
  loc:=first;
  while (loc<last)and(buffer[loc]=" ") do incr(loc);
  if loc<last then
    begin init_terminal:=true;
    return; {return unless the line was all blank}
    end;
  write_ln(term_out,'Please type the name of your input file.');
  end;
exit:end;
@y
@ The following program does the required initialization
and accepts interrupts and also retrieves a possible command line, using
new system routines due to David R. Fuchs.
@^Fuchs, David Raymond@>

@d pto_chr(#)==ptwr1w(0,ord(#)) {put a character in the line editor}

@p procedure esci(var x:integer); extern; @t\2@>@;
  {increments |x| each time the user types escape-I or break-I;
  the program can change |x| whenever it wants to, but |x| had
  better be a global variable}
@#
function rescan:boolean; extern; @t\2@>@;
  {puts the command line into the terminal buffer,
  or returns |false| if there was no command line}
@#
function tmp_in(f:string;var s:string):integer; extern;
  @t\2@>@;
  {reads \.{TMPCOR} file |f| into |s|, and returns its length
    (|<=0| means error)}
@#
function cclsw: boolean; extern; @t\2@>@;
  {was program started with \.{RUN} offset of 1 (i.e., from \.{SNAIL})?}
@#
procedure ptwr1w(pty,c:integer); extern; @t\2@>@;
  {simulates typing of a character on a \.{PTY}}
@#
function init_terminal:boolean; {gets the terminal files started}
label exit;
var l:integer; {length returned by |tmp_in|}
@!line_found:boolean; {have we scanned a line?}
@!tmp_cor_buf:packed array[0..100] of char; {where |tmp_in| puts things}
begin t_open_in;
esci(interrupt);
last:=first;
if cclsw then {started by \.{TEX} monitor command}
  begin l:=tmp_in('TEX',tmp_cor_buf);
  loc:=1;
  while (loc<l)and(tmp_cor_buf[loc]<>':=') do incr(loc);
  incr(loc);
  while loc<l do
    begin if tmp_cor_buf[loc]>' ' then
      begin buffer[last]:=xord[tmp_cor_buf[loc]]; incr(last);
      end;
    incr(loc);
    end;
  end
else
@!debug if false then@;@+gubed@;@/
if rescan then
  begin read_ln(term_in); {get first character into |term_in^|}
  while (not eoln(term_in))and(term_in^<>';') do get(term_in);
  if term_in^=';' then
    begin get(term_in);
    while not eoln(term_in) do
      begin buffer[last]:=xord[term_in^]; incr(last); get(term_in);
      end;
    end;
  end;
line_found:=(last>first);
loop@+  begin loc:=first;
  while (loc<last)and(buffer[loc]=" ") do incr(loc);
  if loc<last then
    begin init_terminal:=true;
    return; {return unless the line was all blank}
    end;
  if line_found then
    write_ln(term_out,'Please type the name of your input file.');
  wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
  buffer[first]:=0; {|input_ln| may look at |buffer[first]|}
  if not input_ln(term_in,true) then {this shouldn't happen}
    begin write_ln(term_out);
    write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
    init_terminal:=false; return;
    end;
  line_found:=true;
  end;
exit:end;
@^system dependencies@>
@z

@x Making special characters printable: (49)
@<Character |k| cannot be printed@>=
  (k<" ")or(k>"~")
@y
@<Character |k| cannot be printed@>=
  (k=@'177)or(k in [0,@'11..@'15,@'33])
@z

@x Terminal input: (71)
begin update_terminal; {Now the user sees the prompt for sure}
@y
begin update_terminal; {Now the user sees the prompt for sure}
buffer[first]:=0; {makes sure |input_ln| doesn't find a |form_feed|}
@z

@x The `E' option: (84)
line ready to be edited. But such an extension requires some system
wizardry, so the present implementation simply types out what file should be
edited and the relevant line number.
@y
line ready to be edited. The present implementation does this by loading
the line editor with the appropriate call to the editor. We treat `\.T' the
same as `\.E', because other programs on this system invoke the editor
when the user says `\.T'.
@z
@x
"E": if base_ptr>0 then
  begin print_nl("You want to edit file ");
@.You want to edit file x@>
  print(input_stack[base_ptr].name_field);
  print(" at line "); print_int(line);
  interaction:=scroll_mode; jump_out;
  end;
@y
"E","T": if base_ptr>0 then
  begin selector:=new_string; pool_ptr:=str_start[str_ptr];
  print("et "); print(input_stack[base_ptr].name_field);
  print_char("/"); print_int(page); print("p/");
  print_int(line); print_char("l"); print_char(carriage_return);
  if str_ptr<max_strings then
    begin pseudo_typein:=str_ptr; incr(str_ptr);
    str_start[str_ptr]:=pool_ptr;
    end; {|make_string| not declared |forward|}
  selector:=term_and_log; interaction:=scroll_mode; jump_out;
  end;
@z

@x Changes for 36-bit machines: (110)
The values defined here are recommended for most 32-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=255 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==65535 {largest allowable value in a |halfword|}
@y
The values defined here are recommended for most 36-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=511 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==262143 {largest allowable value in a |halfword|}
@z

@x Eliminating addition/subtraction of zero: (112)
The inner loop of \TeX\ will run faster with respect to compilers
that don't optimize expressions like `|x+0|' and `|x-0|', if these
macros are simplified in the obvious way when |min_quarterword=0|.
@^inner loop@>@^system dependencies@>

@d qi(#)==#+min_quarterword
  {to put an |eight_bits| item into a quarterword}
@d qo(#)==#-min_quarterword
  {to take an |eight_bits| item out of a quarterword}
@d hi(#)==#+min_halfword
  {to put a sixteen-bit item into a halfword}
@d ho(#)==#-min_halfword
  {to take a sixteen-bit item from a halfword}
@y
The inner loop of \TeX\ will run faster with respect to compilers
that don't optimize expressions like `|x+0|' and `|x-0|', if these
macros are simplified in the obvious way when |min_quarterword=0|.
So they have been simplified here in the obvious way.
@^inner loop@>@^system dependencies@>

@d qi(#)==# {to put an |eight_bits| item into a quarterword}
@d qo(#)==# {to take an |eight_bits| item from a quarterword}
@d hi(#)==# {to put a sixteen-bit item into a halfword}
@d ho(#)==# {to take a sixteen-bit item from a halfword}
@z

@x Kludge to get around Pascal bug (with large array lower bounds) (116)
@!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area}
@y
@z

@x make room for a new creation procedure [161]
\TeX's other algorithms intact.
@^system dependencies@>
@y
\TeX's other algorithms intact.
@^system dependencies@>
@p@<Declare functions needed for special kinds of nodes@>
@z

@x New command code [208-209]
@d max_non_prefixed_command=69 {largest command code that can't be \.{\\global}}

@ The next codes are special; they all relate to mode-independent
assignment of values to \TeX's internal registers or tables.
Codes that are |max_internal| or less represent internal quantities
that might be expanded by `\.{\\the}'.

@d toks_register=70 {token list register ( \.{\\toks} )}
@d assign_toks=71 {special token list ( \.{\\output}, \.{\\everypar}, etc.~)}
@d assign_int=72 {user-defined integer ( \.{\\tolerance}, \.{\\day}, etc.~)}
@d assign_dimen=73 {user-defined length ( \.{\\hsize}, etc.~)}
@d assign_glue=74 {user-defined glue ( \.{\\baselineskip}, etc.~)}
@d assign_mu_glue=75 {user-defined muglue ( \.{\\thinmuskip}, etc.~)}
@d assign_font_dimen=76 {user-defined font dimension ( \.{\\fontdimen} )}
@d assign_font_int=77 {user-defined font integer ( \.{\\hyphenchar},
  \.{\\skewchar} )}
@d set_aux=78 {specify state info ( \.{\\spacefactor}, \.{\\prevdepth} )}
@d set_prev_graf=79 {specify state info ( \.{\\prevgraf} )}
@d set_page_dimen=80 {specify state info ( \.{\\pagegoal}, etc.~)}
@d set_page_int=81 {specify state info ( \.{\\deadcycles},
  \.{\\insertpenalties} )}
@d set_box_dimen=82 {change dimension of box ( \.{\\wd}, \.{\\ht}, \.{\\dp} )}
@d set_shape=83 {specify fancy paragraph shape ( \.{\\parshape} )}
@d def_code=84 {define a character code ( \.{\\catcode}, etc.~)}
@d def_family=85 {declare math fonts ( \.{\\textfont}, etc.~)}
@d set_font=86 {set current font ( font identifiers )}
@d def_font=87 {define a font file ( \.{\\font} )}
@d register=88 {internal register ( \.{\\count}, \.{\\dimen}, etc.~)}
@d max_internal=88 {the largest code that can follow \.{\\the}}
@d advance=89 {advance a register or parameter ( \.{\\advance} )}
@d multiply=90 {multiply a register or parameter ( \.{\\multiply} )}
@d divide=91 {divide a register or parameter ( \.{\\divide} )}
@d prefix=92 {qualify a definition ( \.{\\global}, \.{\\long}, \.{\\outer} )}
@d let=93 {assign a command code ( \.{\\let}, \.{\\futurelet} )}
@d shorthand_def=94 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
@d read_to_cs=95 {read into a control sequence ( \.{\\read} )}
@d def=96 {macro definition ( \.{\\def}, \.{\\gdef}, \.{\\xdef}, \.{\\edef} )}
@d set_box=97 {set a box ( \.{\\setbox} )}
@d hyph_data=98 {hyphenation data ( \.{\\hyphenation}, \.{\\patterns} )}
@d set_interaction=99 {define level of interaction ( \.{\\batchmode}, etc.~)}
@d max_command=99 {the largest command code seen at |big_switch|}
@y
@d LR=70 {text direction ( \.{\\beginL}, \.{\\beginR}, \.{\\endL}, \.{\\endR} )}
@d max_non_prefixed_command=70 {largest command code that can't be \.{\\global}}

@ The next codes are special; they all relate to mode-independent
assignment of values to \TeX's internal registers or tables.
Codes that are |max_internal| or less represent internal quantities
that might be expanded by `\.{\\the}'.

@d toks_register=71 {token list register ( \.{\\toks} )}
@d assign_toks=72 {special token list ( \.{\\output}, \.{\\everypar}, etc.~)}
@d assign_int=73 {user-defined integer ( \.{\\tolerance}, \.{\\day}, etc.~)}
@d assign_dimen=74 {user-defined length ( \.{\\hsize}, etc.~)}
@d assign_glue=75 {user-defined glue ( \.{\\baselineskip}, etc.~)}
@d assign_mu_glue=76 {user-defined muglue ( \.{\\thinmuskip}, etc.~)}
@d assign_font_dimen=77 {user-defined font dimension ( \.{\\fontdimen} )}
@d assign_font_int=78 {user-defined font integer ( \.{\\hyphenchar},
  \.{\\skewchar} )}
@d set_aux=79 {specify state info ( \.{\\spacefactor}, \.{\\prevdepth} )}
@d set_prev_graf=80 {specify state info ( \.{\\prevgraf} )}
@d set_page_dimen=81 {specify state info ( \.{\\pagegoal}, etc.~)}
@d set_page_int=82 {specify state info ( \.{\\deadcycles},
  \.{\\insertpenalties} )}
@d set_box_dimen=83 {change dimension of box ( \.{\\wd}, \.{\\ht}, \.{\\dp} )}
@d set_shape=84 {specify fancy paragraph shape ( \.{\\parshape} )}
@d def_code=85 {define a character code ( \.{\\catcode}, etc.~)}
@d def_family=86 {declare math fonts ( \.{\\textfont}, etc.~)}
@d set_font=87 {set current font ( font identifiers )}
@d def_font=88 {define a font file ( \.{\\font} )}
@d register=89 {internal register ( \.{\\count}, \.{\\dimen}, etc.~)}
@d max_internal=89 {the largest code that can follow \.{\\the}}
@d advance=90 {advance a register or parameter ( \.{\\advance} )}
@d multiply=91 {multiply a register or parameter ( \.{\\multiply} )}
@d divide=92 {divide a register or parameter ( \.{\\divide} )}
@d prefix=93 {qualify a definition ( \.{\\global}, \.{\\long}, \.{\\outer} )}
@d let=94 {assign a command code ( \.{\\let}, \.{\\futurelet} )}
@d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
@d read_to_cs=96 {read into a control sequence ( \.{\\read} )}
@d def=97 {macro definition ( \.{\\def}, \.{\\gdef}, \.{\\xdef}, \.{\\edef} )}
@d set_box=98 {set a box ( \.{\\setbox} )}
@d hyph_data=99 {hyphenation data ( \.{\\hyphenation}, \.{\\patterns} )}
@d set_interaction=100 {define level of interaction ( \.{\\batchmode}, etc.~)}
@d max_command=100 {the largest command code seen at |big_switch|}
@z

@x Special form_feed initialization: (232)
cat_code("\"):=escape; cat_code("%"):=comment;
@y
cat_code("\"):=escape; cat_code("%"):=comment;
cat_code(form_feed):=car_ret;
@z

@x Date and time: (241)
Since standard \PASCAL\ cannot provide such information, something special
is needed. The program here simply specifies July 4, 1776, at noon; but
users probably want a better approximation to the truth.

@p procedure fix_date_and_time;
begin time:=12*60; {minutes since midnight}
day:=4; {fourth day of the month}
month:=7; {seventh month of the year}
year:=1776; {Anno Domini}
end;
@y
It uses a {\mc WAITS} monitor call that puts the date in the left 18 bits
and the time in the right 18 bits.

@p procedure fix_date_and_time;
var t:integer; {accumulator}
date:integer; {raw date}
g:boolean; {garbage}
begin call_i(@'400101,,t,t,g); {that's \.{ACCTIM}}
date:=t div @'1000000;
time:=(t mod @'1000000) div 60;
day:=(date mod 31)+1;
month:=((date div 31) mod 12)+1;
year:=(date div (31*12))+1964;
end;
@z

@x Page number maintenance: (304)
If more information about the input state is needed, it can be
included in small arrays like those shown here. For example,
the current page or segment number in the input file might be
put into a variable |@!page|, maintained for enclosing levels in
`\ignorespaces|@!page_stack:array[1..max_in_open] of integer|\unskip'
by analogy with |line_stack|.
@y
Similarly, we maintain a global variable |page| and a corresponding
|page_stack|.
@z
@x
@!line_stack : array[1..max_in_open] of integer;
@y
@!line_stack : array[1..max_in_open] of integer;
@!page : integer; {current page number in the current source file}
@!page_stack : array[1..max_in_open] of integer;
@z

@x Printing the page number: (313)
else  begin print_nl("l."); print_int(line);
@y
else  begin if page>1 then
    begin print_nl("p."); print_int(page); print(",l.");
    end
  else print_nl("l.");
  print_int(line);
@z

@x More page number maintenance: (328)
or |limit| or |line|.
@y
or |limit| or |line| or |page|.
@z
@x
line_stack[index]:=line; start:=first; state:=mid_line;
@y
line_stack[index]:=line; start:=first; state:=mid_line;
page_stack[index]:=page;
@z
@x
begin first:=start; line:=line_stack[index];
@y
begin first:=start; page:=page_stack[index]; line:=line_stack[index];
@z

@x More page number printing: (336)
    print("; all text was ignored after line "); print_int(skip_line);
@y
    print("; all text was ignored after line "); print_int(skip_line);
    if skip_page>1 then
      begin print(", p."); print_int(skip_page);
      end;
@z

@x Pausing on input: (363)
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected, each line of input is displayed
on the terminal and the transcript file, followed by `\.{=>}'.
\TeX\ waits for a response. If the response is simply |carriage_return|, the
line is accepted as it stands, otherwise the line typed is
used instead of the line in the file.

@p procedure firm_up_the_line;
var k:0..buf_size; {an index into |buffer|}
begin limit:=last;
if pausing>0 then if interaction>nonstop_mode then
  begin wake_up_terminal; print_ln;
  if start<limit then for k:=start to limit-1 do print(buffer[k]);
  first:=limit; prompt_input("=>"); {wait for user response}
@.=>@>
  if last>first then
    begin for k:=first to last-1 do {move line down in buffer}
      buffer[k+start-first]:=buffer[k];
    limit:=start+last-first;
    end;
  end;
end;
@y
@ If the user}
@d wake_up_terminal == do_nothing {cancel the user's cancellation of output}
@y
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == begin if inskp0 then end
  {cancel the user's cancellation of output}

@<Error handling procedures@>=
function inskp0:boolean; extern;
@z

@x Initializing the terminal: (37)
@ The following program does the required initialization
without retrieving a possible command line.
It should be clear how to modify this routine to deal with command lines,
if the system permits them.
@^system dependencies@>

@p function init_terminal:boolean; {gets the terminal input started}
label exit;
begin t_open_in;
loop@+begin wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
  if not input_ln(term_in,true) then {this shouldn't happen}
    begin write_ln(term_out);
    write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
    init_terminal:=false; return;
    end;
  loc:=first;
  while (loc<last)and(buffer[loc]=" ") do incr(loc);
  if loc<last then
    begin init_terminal:=true;
    return; {return unless the line was all blank}
    end;
  write_ln(term_out,'Please type the name of your input file.');
  end;
exit:end;
@y
@ The following program does the required initialization
and accepts interrupts and also retrieves a possible command line, using
new system routines due to David R. Fuchs.
@^Fuchs, David Raymond@>

@d pto_chr(#)==ptwr1w(0,ord(#)) {put a character in the line editor}

@p procedure esci(var x:integer); extern; @t\2@>@;
  {increments |x| each time the user types escape-I or break-I;
  the program can change |x| whenever it wants to, but |x| had
  better be a global variable}
@#
function rescan:boolean; extern; @t\2@>@;
  {puts the command line into the terminal buffer,
  or returns |false| if there was no command line}
@#
function tmp_in(f:string;var s:string):integer; extern;
  @t\2@>@;
  {reads \.{TMPCOR} file |f| into |s|, and returns its length
    (|<=0| means error)}
@#
function cclsw: boolean; extern; @t\2@>@;
  {was program started with \.{RUN} offset of 1 (i.e., from \.{SNAIL})?}
@#
procedure ptwr1w(pty,c:integer); extern; @t\2@>@;
  {simulates typing of a character on a \.{PTY}}
@#
function init_terminal:boolean; {gets the terminal files started}
label exit;
var l:integer; {length returned by |tmp_in|}
@!line_found:boolean; {have we scanned a line?}
@!tmp_cor_buf:packed array[0..100] of char; {where |tmp_in| puts things}
begin t_open_in;
esci(interrupt);
last:=first;
if cclsw then {started by \.{TEX} monitor command}
  begin l:=tmp_in('TEX',tmp_cor_buf);
  loc:=1;
  while (loc<l)and(tmp_cor_buf[loc]<>':=') do incr(loc);
  incr(loc);
  while loc<l do
    begin if tmp_cor_buf[loc]>' ' then
      begin buffer[last]:=xord[tmp_cor_buf[loc]]; incr(last);
      end;
    incr(loc);
    end;
  end
else
@!debug if false then@;@+gubed@;@/
if rescan then
  begin read_ln(term_in); {get first character into |term_in^|}
  while (not eoln(term_in))and(term_in^<>';') do get(term_in);
  if term_in^=';' then
    begin get(term_in);
    while not eoln(term_in) do
      begin buffer[last]:=xord[term_in^]; incr(last); get(term_in);
      end;
    end;
  end;
line_found:=(last>first);
loop@+  begin loc:=first;
  while (loc<last)and(buffer[loc]=" ") do incr(loc);
  if loc<last then
    begin init_terminal:=true;
    return; {return unless the line was all blank}
    end;
  if line_found then
    write_ln(term_out,'Please type the name of your input file.');
  wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
  buffer[first]:=0; {|input_ln| may look at |buffer[first]|}
  if not input_ln(term_in,true) then {this shouldn't happen}
    begin write_ln(term_out);
    write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
    init_terminal:=false; return;
    end;
  line_found:=true;
  end;
exit:end;
@^system dependencies@>
@z

@x Making special characters printable: (49)
@<Character |k| cannot be printed@>=
  (k<" ")or(k>"~")
@y
@<Character |k| cannot be printed@>=
  (k=@'177)or(k in [0,@'11..@'15,@'33])
@z

@x Terminal input: (71)
begin update_terminal; {Now the user sees the prompt for sure}
@y
begin update_terminal; {Now the user sees the prompt for sure}
buffer[first]:=0; {makes sure |input_ln| doesn't find a |form_feed|}
@z

@x The `E' option: (84)
line ready to be edited. But such an extension requires some system
wizardry, so the present implementation simply types out what file should be
edited and the relevant line number.
@y
line ready to be edited. The present implementation does this by loading
the line editor with the appropriate call to the editor. We treat `\.T' the
same as `\.E', because other programs on this system invoke the editor
when the user says `\.T'.
@z
@x
"E": if base_ptr>0 then
  begin print_nl("You want to edit file ");
@.You want to edit file x@>
  print(input_stack[base_ptr].name_field);
  print(" at line "); print_int(line);
  interaction:=scroll_mode; jump_out;
  end;
@y
"E","T": if base_ptr>0 then
  begin selector:=new_string; pool_ptr:=str_start[str_ptr];
  print("et "); print(input_stack[base_ptr].name_field);
  print_char("/"); print_int(page); print("p/");
  print_int(line); print_char("l"); print_char(carriage_return);
  if str_ptr<max_strings then
    begin pseudo_typein:=str_ptr; incr(str_ptr);
    str_start[str_ptr]:=pool_ptr;
    end; {|make_string| not declared |forward|}
  selector:=term_and_log; interaction:=scroll_mode; jump_out;
  end;
@z

@x Changes for 36-bit machines: (110)
The values defined here are recommended for most 32-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=255 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==65535 {largest allowable value in a |halfword|}
@y
The values defined here are recommended for most 36-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=511 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==262143 {largest allowable value in a |halfword|}
@z

@x Eliminating addition/subtraction of zero: (112)
The inner loop of \TeX\ will run faster with respect to compilers
that don't optimize expressions like `|x+0|' and `|x-0|', if these
macros are simplified in the obvious way when |min_quarterword=0|.
@^inner loop@>@^system dependencies@>

@d qi(#)==#+min_quarterword
  {to put an |eight_bits| item into a quarterword}
@d qo(#)==#-min_quarterword
  {to take an |eight_bits| item out of a quarterword}
@d hi(#)==#+min_halfword
  {to put a sixteen-bit item into a halfword}
@d ho(#)==#-min_halfword
  {to take a sixteen-bit item from a halfword}
@y
The inner loop of \TeX\ will run faster with respect to compilers
that don't optimize expressions like `|x+0|' and `|x-0|', if these
macros are simplified in the obvious way when |min_quarterword=0|.
So they have been simplified here in the obvious way.
@^inner loop@>@^system dependencies@>

@d qi(#)==# {to put an |eight_bits| item into a quarterword}
@d qo(#)==# {to take an |eight_bits| item from a quarterword}
@d hi(#)==# {to put a sixteen-bit item into a halfword}
@d ho(#)==# {to take a sixteen-bit item from a halfword}
@z

@x Kludge to get around Pascal bug (with large array lower bounds) (116)
@!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area}
@y
@z

@x make room for a new creation procedure [161]
\TeX's other algorithms intact.
@^system dependencies@>
@y
\TeX's other algorithms intact.
@^system dependencies@>
@p@<Declare functions needed for special kinds of nodes@>
@z

@x New command code [208-209]
@d max_non_prefixed_command=69 {largest command code that can't be \.{\\global}}

@ The next codes are special; they all relate to mode-independent
assignment of values to \TeX's internal registers or tables.
Codes that are |max_internal| or less represent internal quantities
that might be expanded by `\.{\\the}'.

@d toks_register=70 {token list register ( \.{\\toks} )}
@d assign_toks=71 {special token list ( \.{\\output}, \.{\\everypar}, etc.~)}
@d assign_int=72 {user-defined integer ( \.{\\tolerance}, \.{\\day}, etc.~)}
@d assign_dimen=73 {user-defined length ( \.{\\hsize}, etc.~)}
@d assign_glue=74 {user-defined glue ( \.{\\baselineskip}, etc.~)}
@d assign_mu_glue=75 {user-defined muglue ( \.{\\thinmuskip}, etc.~)}
@d assign_font_dimen=76 {user-defined font dimension ( \.{\\fontdimen} )}
@d assign_font_int=77 {user-defined font integer ( \.{\\hyphenchar},
  \.{\\skewchar} )}
@d set_aux=78 {specify state info ( \.{\\spacefactor}, \.{\\prevdepth} )}
@d set_prev_graf=79 {specify state info ( \.{\\prevgraf} )}
@d set_page_dimen=80 {specify state info ( \.{\\pagegoal}, etc.~)}
@d set_page_int=81 {specify state info ( \.{\\deadcycles},
  \.{\\insertpenalties} )}
@d set_box_dimen=82 {change dimension of box ( \.{\\wd}, \.{\\ht}, \.{\\dp} )}
@d set_shape=83 {specify fancy paragraph shape ( \.{\\parshape} )}
@d def_code=84 {define a character code ( \.{\\catcode}, etc.~)}
@d def_family=85 {declare math fonts ( \.{\\textfont}, etc.~)}
@d set_font=86 {set current font ( font identifiers )}
@d def_font=87 {define a font file ( \.{\\font} )}
@d register=88 {internal register ( \.{\\count}, \.{\\dimen}, etc.~)}
@d max_internal=88 {the largest code that can follow \.{\\the}}
@d advance=89 {advance a register or parameter ( \.{\\advance} )}
@d multiply=90 {multiply a register or parameter ( \.{\\multiply} )}
@d divide=91 {divide a register or parameter ( \.{\\divide} )}
@d prefix=92 {qualify a definition ( \.{\\global}, \.{\\long}, \.{\\outer} )}
@d let=93 {assign a command code ( \.{\\let}, \.{\\futurelet} )}
@d shorthand_def=94 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
@d read_to_cs=95 {read into a control sequence ( \.{\\read} )}
@d def=96 {macro definition ( \.{\\def}, \.{\\gdef}, \.{\\xdef}, \.{\\edef} )}
@d set_box=97 {set a box ( \.{\\setbox} )}
@d hyph_data=98 {hyphenation data ( \.{\\hyphenation}, \.{\\patterns} )}
@d set_interaction=99 {define level of interaction ( \.{\\batchmode}, etc.~)}
@d max_command=99 {the largest command code seen at |big_switch|}
@y
@d LR=70 {text direction ( \.{\\beginL}, \.{\\beginR}, \.{\\endL}, \.{\\endR} )}
@d max_non_prefixed_command=70 {largest command code that can't be \.{\\global}}

@ The next codes are special; they all relate to mode-independent
assignment of values to \TeX's internal registers or tables.
Codes that are |max_internal| or less represent internal quantities
that might be expanded by `\.{\\the}'.

@d toks_register=71 {token list register ( \.{\\toks} )}
@d assign_toks=72 {special token list ( \.{\\output}, \.{\\everypar}, etc.~)}
@d assign_int=73 {user-defined integer ( \.{\\tolerance}, \.{\\day}, etc.~)}
@d assign_dimen=74 {user-defined length ( \.{\\hsize}, etc.~)}
@d assign_glue=75 {user-defined glue ( \.{\\baselineskip}, etc.~)}
@d assign_mu_glue=76 {user-defined muglue ( \.{\\thinmuskip}, etc.~)}
@d assign_font_dimen=77 {user-defined font dimension ( \.{\\fontdimen} )}
@d assign_font_int=78 {user-defined font integer ( \.{\\hyphenchar},
  \.{\\skewchar} )}
@d set_aux=79 {specify state info ( \.{\\spacefactor}, \.{\\prevdepth} )}
@d set_prev_graf=80 {specify state info ( \.{\\prevgraf} )}
@d set_page_dimen=81 {specify state info ( \.{\\pagegoal}, etc.~)}
@d set_page_int=82 {specify state info ( \.{\\deadcycles},
  \.{\\insertpenalties} )}
@d set_box_dimen=83 {change dimension of box ( \.{\\wd}, \.{\\ht}, \.{\\dp} )}
@d set_shape=84 {specify fancy paragraph shape ( \.{\\parshape} )}
@d def_code=85 {define a character code ( \.{\\catcode}, etc.~)}
@d def_family=86 {declare math fonts ( \.{\\textfont}, etc.~)}
@d set_font=87 {set current font ( font identifiers )}
@d def_font=88 {define a font file ( \.{\\font} )}
@d register=89 {internal register ( \.{\\count}, \.{\\dimen}, etc.~)}
@d max_internal=89 {the largest code that can follow \.{\\the}}
@d advance=90 {advance a register or parameter ( \.{\\advance} )}
@d multiply=91 {multiply a register or parameter ( \.{\\multiply} )}
@d divide=92 {divide a register or parameter ( \.{\\divide} )}
@d prefix=93 {qualify a definition ( \.{\\global}, \.{\\long}, \.{\\outer} )}
@d let=94 {assign a command code ( \.{\\let}, \.{\\futurelet} )}
@d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
@d read_to_cs=96 {read into a control sequence ( \.{\\read} )}
@d def=97 {macro definition ( \.{\\def}, \.{\\gdef}, \.{\\xdef}, \.{\\edef} )}
@d set_box=98 {set a box ( \.{\\setbox} )}
@d hyph_data=99 {hyphenation data ( \.{\\hyphenation}, \.{\\patterns} )}
@d set_interaction=100 {define level of interaction ( \.{\\batchmode}, etc.~)}
@d max_command=100 {the largest command code seen at |big_switch|}
@z

@x Special form_feed initialization: (232)
cat_code("\"):=escape; cat_code("%"):=comment;
@y
cat_code("\"):=escape; cat_code("%"):=comment;
cat_code(form_feed):=car_ret;
@z

@x Date and time: (241)
Since standard \PASCAL\ cannot provide such information, something special
is needed. The program here simply specifies July 4, 1776, at noon; but
users probably want a better approximation to the truth.

@p procedure fix_date_and_time;
begin time:=12*60; {minutes since midnight}
day:=4; {fourth day of the month}
month:=7; {seventh month of the year}
year:=1776; {Anno Domini}
end;
@y
It uses a {\mc WAITS} monitor call that puts the date in the left 18 bits
and the time in the right 18 bits.

@p procedure fix_date_and_time;
var t:integer; {accumulator}
date:integer; {raw date}
g:boolean; {garbage}
begin call_i(@'400101,,t,t,g); {that's \.{ACCTIM}}
date:=t div @'1000000;
time:=(t mod @'1000000) div 60;
day:=(date mod 31)+1;
month:=((date div 31) mod 12)+1;
year:=(date div (31*12))+1964;
end;
@z

@x Page number maintenance: (304)
If more information about the input state is needed, it can be
included in small arrays like those shown here. For example,
the current page or segment number in the input file might be
put into a variable |@!page|, maintained for enclosing levels in
`\ignorespaces|@!page_stack:array[1..max_in_open] of integer|\unskip'
by analogy with |line_stack|.
@y
Similarly, we maintain a global variable |page| and a corresponding
|page_stack|.
@z
@x
@!line_stack : array[1..max_in_open] of integer;
@y
@!line_stack : array[1..max_in_open] of integer;
@!page : integer; {current page number in the current source file}
@!page_stack : array[1..max_in_open] of integer;
@z

@x Printing the page number: (313)
else  begin print_nl("l."); print_int(line);
@y
else  begin if page>1 then
    begin print_nl("p."); print_int(page); print(",l.");
    end
  else print_nl("l.");
  print_int(line);
@z

@x More page number maintenance: (328)
or |limit| or |line|.
@y
or |limit| or |line| or |page|.
@z
@x
line_stack[index]:=line; start:=first; state:=mid_line;
@y
line_stack[index]:=line; start:=first; state:=mid_line;
page_stack[index]:=page;
@z
@x
begin first:=start; line:=line_stack[index];
@y
begin first:=start; page:=page_stack[index]; line:=line_stack[index];
@z

@x More page number printing: (336)
    print("; all text was ignored after line "); print_int(skip_line);
@y
    print("; all text was ignored after line "); print_int(skip_line);
    if skip_page>1 then
      begin print(", p."); print_int(skip_page);
      end;
@z

@x Pausing on input: (363)
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected, each line of input is displayed
on the terminal and the transcript file, followed by `\.{=>}'.
\TeX\ waits for a response. If the response is simply |carriage_return|, the
line is accepted as it stands, otherwise the line typed is
used instead of the line in the file.

@p procedure firm_up_the_line;
var k:0..buf_size; {an index into |buffer|}
begin limit:=last;
if pausing>0 then if interaction>nonstop_mode then
  begin wake_up_terminal; print_ln;
  if start<limit then for k:=start to limit-1 do print(buffer[k]);
  first:=limit; prompt_input("=>"); {wait for user response}
@.=>@>
  if last>first then
    begin for k:=first to last-1 do {move line down in buffer}
      buffer[k+start-first]:=buffer[k];
    limit:=start+last-first;
    end;
  end;
end;
@y
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected,
each line of input is displayed in the transcript file, followed by `\.{=>}',
and also put into the user's line-editor buffer.
\TeX\ waits for the line to be edited, and the next line received is
used instead of the line in the file.

@p procedure firm_up_the_line;
var k:0..buf_size; {an index into |buffer|}
begin limit:=last;
if pausing>0 then if interaction>nonstop_mode then
 if buffer[start]<>form_feed then
  begin wake_up_terminal; print_ln;
  if start=limit then {empty line will be made nonempty so that it's visible}
    begin buffer[start]:=" "; incr(limit);
    end;
  decr(selector); {inhibit terminal output temporarily}
  for k:=start to limit-1 do
    begin print_char(buffer[k]);
    pto_chr(xchr[buffer[k]]);
    end;
  print("=>"); first:=start;
  if not input_ln(term_in,true) then
    fatal_error("End of file on the terminal!");
@.End of file on the terminal@>
  if last>first then for k:=first to last-1 do print_char(buffer[k]);
  limit:=last; print_ln; incr(selector);
  end;
end;
@z

@x Reading the first line of a file: (485)
@ The first line of a file must be treated specially, since |input_ln|
must be told not to start with |get|.
@^system dependencies@>

@<Input the first line of |read_file[m]|@>=
if input_ln(read_file[m],false) then read_open[m]:=normal
else  begin a_close(read_file[m]); read_open[m]:=closed;
  end
@y
@ The first line of a file must be treated specially, since |input_ln|
must be told not to |get|. Furthermore we want to omit the optional file
directory page present at {\mc SAIL}.
@^system dependencies@>

@<Input the first line of |read_file[m]|@>=
if not input_ln(read_file[m],false) then
  begin a_close(read_file[m]); read_open[m]:=closed;
  end
else  begin
  if(last-start=29)and(buffer[start]="C")and(buffer[start+8]=@'26) then
    begin while (read_file[m]^<>chr(form_feed))and(not eof(read_file[m])) do
      begin read_ln(read_file[m]); read(read_file[m],aux_buf:temp_ptr);
      end; {skip the directory}
    buffer[start]:=form_feed; last:=start+1;
    end;
  read_open[m]:=normal;
  end
@z

@x Still more page number maintenance: (493)
where skipping began, for use in error messages.

@<Glob...@>=
@!skip_line:integer; {skipping began here}
@y
where skipping began, for use in error messages. Also the page number.

@<Glob...@>=
@!skip_line,@!skip_page:integer; {skipping began here}
@z
@x
skip_line:=line;
@y
skip_line:=line; skip_page:=page;
@z

@x Parsing file names: (513)
@ The file names we shall deal with for illustrative purposes have the
following structure:  If the name contains `\.>' or `\.:', the file area
consists of all characters up to and including the final such character;
otherwise the file area is null.  If the remaining file name contains
`\..', the file extension consists of all such characters from the first
remaining `\..' to the end, otherwise the file extension is null.
@^system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.>' or `\.:', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@y
@ The file names we shall deal with have the following structure:
If the name contains `\.[', the file area consists of all characters
from this character to the end; otherwise the file area is null.
If the remaining file name contains `\..', the file extension consists of all
such characters from this character to the end, otherwise the file extension
is null. We can assume that there is at most one `\.[' and at most one `\..'.
@^system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.[', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@z
@x
@d TEX_area=="TeXinputs:"
@.TeXinputs@>
@d TEX_font_area=="TeXfonts:"
@.TeXfonts@>
@y
@d TEX_area=="[tex,sys]"
@d TEX_font_area=="[tfm,sys]"
@z
@x
else  begin if (c=">")or(c=":") then
    begin area_delimiter:=pool_ptr; ext_delimiter:=0;
    end
  else if (c=".")and(ext_delimiter=0) then ext_delimiter:=pool_ptr;
@y
else  begin if c="[" then area_delimiter:=pool_ptr
  else if c="." then ext_delimiter:=pool_ptr;
@z
@x
if area_delimiter=0 then cur_area:=""
else  begin cur_area:=str_ptr; incr(str_ptr);
  str_start[str_ptr]:=area_delimiter+1;
  end;
if ext_delimiter=0 then
  begin cur_ext:=""; cur_name:=make_string;
  end
else  begin cur_name:=str_ptr; incr(str_ptr);
  str_start[str_ptr]:=ext_delimiter; cur_ext:=make_string;
  end;
@y
cur_name:=str_ptr;
if ext_delimiter=0 then cur_ext:=""
else  begin incr(str_ptr);
  str_start[str_ptr]:=ext_delimiter; cur_ext:=str_ptr;
  end;
if area_delimiter<=str_start[str_ptr] then
  begin cur_area:=""; incr(str_ptr); str_start[str_ptr]:=pool_ptr;
  end
else  begin incr(str_ptr);
  str_start[str_ptr]:=area_delimiter; cur_area:=make_string;
  end;
@z

@x Printing file names: (518)
begin print(a); print(n); print(e);
@y
begin print(n); print(e); print(a);
@z

@x Converting file names to Pascal strings: (519)
allows both lowercase and uppercase letters in the file name.
@^system dependencies@>

@d append_to_name(#)==begin c:=#; incr(k);
@y
converts lowercase letters to uppercase.

A special convention is used here with respect to font metric files: If the
file name is longer than six characters (e.g., `\.{helvetica}' or
`\.{oldenglish}'), we abbreviate it by retaining the first three and
last three characters (e.g., `\.{helica}' or `\.{oldish}').
@^system dependencies@>

@d append_to_name(#)==begin c:=#; incr(k);
  if (c>="a")and(c<="z") then c:=c-@'40; {convert to uppercase}
@z
@x
for j:=str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
for j:=str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j:=str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
@y
if (e=".tfm")and(length(n)>6) then
  begin for j:=str_start[n] to str_start[n]+2 do
    append_to_name(str_pool[j]);
  for j:=str_start[n+1]-3 to str_start[n+1]-1 do
    append_to_name(str_pool[j]);
  end
else for j:=str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j:=str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
for j:=str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
@z

@x Parsing file names in the buffer: (520)
@d format_default_length=20 {length of the |TEX_format_default| string}
@d format_area_length=11 {length of its area part}
@d format_ext_length=4 {length of its `\.{.fmt}' part}
@y
@d format_default_length=18 {length of the |TEX_format_default| string}
@d format_area_length=9 {length of its area part}
@d format_ext_length=4 {length of its `\.{.fmt}' part}
@z
@x
TEX_format_default:='TeXformats:plain.fmt';
@.TeXformats@>
@y
TEX_format_default:='plain.fmt[tex,sys]';
@z
@x
|TEX_format_default|.
@y
|TEX_format_default|; but it actually switches stuff around to keep the
file area last.
@z
@x
@!j:integer; {index into |buffer| or |TEX_format_default|}
@y
@!j:integer; {index into |buffer| or |TEX_format_default|}
@!d:integer; {a kludge}
@z
@x
for j:=1 to n do append_to_name(xord[TEX_format_default[j]]);
for j:=a to b do append_to_name(buffer[j]);
for j:=format_default_length-format_ext_length+1 to format_default_length do
  append_to_name(xord[TEX_format_default[j]]);
@y
for j:=a to b do append_to_name(buffer[j]);
if b=0 then
  begin d:=format_default_length-format_area_length+1;
  n:=format_default_length;
  end
else d:=format_default_length-format_area_length-format_ext_length+1;
for j:=d to format_default_length-format_area_length do
  append_to_name(xord[TEX_format_default[j]]);
for j:=format_default_length-n+1 to format_default_length do
  append_to_name(xord[TEX_format_default[j]]);
@z

@x The real file names: (525)
@ Operating systems often make it possible to determine the exact name (and
possible version number) of a file that has been opened. The following routine,
which simply makes a \TeX\ string from the value of |name_of_file|, should
ideally be changed to deduce the full name of file~|f|, which is the file
most recently opened, if it is possible to do this in a \PASCAL\ program.
@^system dependencies@>

This routine might be called after string memory has overflowed, hence
we dare not use `|str_room|'.

@p function make_name_string:str_number;
var k:1..file_name_size; {index into |name_of_file|}
begin if (pool_ptr+name_length>pool_size)or(str_ptr=max_strings) then
  make_name_string:="?"
else  begin for k:=1 to name_length do append_char(xord[name_of_file[k]]);
  make_name_string:=make_string;
  end;
end;
function a_make_name_string(var f:alpha_file):str_number;
begin a_make_name_string:=make_name_string;
end;
function b_make_name_string(var f:byte_file):str_number;
begin b_make_name_string:=make_name_string;
end;
function w_make_name_string(var f:word_file):str_number;
begin w_make_name_string:=make_name_string;
end;
@y
@ Operating systems often make it possible to determine the exact name (and
possible version number) of a file that has been opened. The following routine,
due to David Fuchs, deduces the full name of file~|f| on {\mc WAITS}:
@^system dependencies@>
@^Fuchs, David Raymond@>

@p procedure cur_nam(var chan:f@&i@&l@&e;var s:string); extern; @t\2@>@/
function make_name_string(var f:f@&i@&l@&e):str_number;
var s:packed array[1..24] of char;
@!k:1..24; {file names at {\sc SAIL} have at most 23 characters}
begin if (pool_ptr+24>pool_size)or(str_ptr=max_strings) then
  make_name_string:="?"
else  begin cur_nam(f,s); k:=1;
  while ord(s[k])<>0 do
    begin append_char(xord[s[k]]); incr(k);
    end;
  make_name_string:=make_string;
  end;
end;
function a_make_name_string(var f:alpha_file):str_number;
begin a_make_name_string:=make_name_string(f);
end;
function b_make_name_string(var f:byte_file):str_number;
begin b_make_name_string:=make_name_string(f);
end;
function w_make_name_string(var f:word_file):str_number;
begin w_make_name_string:=make_name_string(f);
end;
@z

@x Line editor gets misspelled file name: (530)
begin if interaction=scroll_mode then wake_up_terminal;
@y
@!i:pool_pointer; {index into |str_pool|}
begin if interaction=scroll_mode then wake_up_terminal;
@z
@x
clear_terminal; prompt_input(": "); @<Scan file name in the buffer@>;
@y
clear_terminal; {now we'll fill the line editor's buffer with the old name}
for i:=str_start[cur_name] to str_start[cur_name+1]-1 do
  pto_chr(xchr[str_pool[i]]);
for i:=str_start[cur_ext] to str_start[cur_ext+1]-1 do
  pto_chr(xchr[str_pool[i]]);
for i:=str_start[cur_area] to str_start[cur_area+1]-1 do
  pto_chr(xchr[str_pool[i]]);
ptwr1w(0,@'214); {control-formfeed returns cursor to start of line}
prompt_input(": "); @<Scan file name in the buffer@>;
@z

@x Reading the first line of a file, again: (538)
@ Here we have to remember to tell the |input_ln| routine not to
start with a |get|. If the file is empty, it is considered to
contain a single blank line.
@^system dependencies@>
@^empty line at end of file@>

@<Read the first line...@>=
begin if not input_ln(cur_file,false) then do_nothing;
@y
@ Here we have to remember to tell |input_ln| not to do the first |get|,
and we also want to skip over a file directory page. An empty file
is considered to contain a single blank line.
@^system dependencies@>
@^empty line at end of file@>

@<Read the first line...@>=
begin if input_ln(cur_file,false) then
  begin if(last-start=29)and(buffer[start]="C")and(buffer[start+8]=@'26) then
    begin while (cur_file^<>chr(form_feed))and(not eof(cur_file)) do
      begin read_ln(cur_file); read(cur_file,aux_buf:temp_ptr);
      end; {skip the directory}
    buffer[start]:=form_feed; last:=start+1;
    end;
  end;
page:=1;
@z

@x new DVI opcodes [585-586]
\yskip\noindent Commands 250--255 are undefined at the present time.

@ @d set_char_0=0 {typeset character 0 and move right}
@y
\yskip\hang|begin_reflect| 250. Begin a (possibly recursive) reflected segment.

\yskip\hang|end_reflect| 251. End a (possibly recursive) reflected segment.

\yskip\noindent Commands 250--255 are undefined in normal \.{DVI} files,
but 250 and 251 are permitted in the special `\.{DVI-IVD}' files
produced by this variant of \TeX.

@ @d begin_reflect=250 {begin a reflected segment (not normally allowed)}
@d end_reflect=251 {end a reflected segment (not normally allowed)}
@d set_char_0=0 {typeset character 0 and move right}
@z

@x The DVI output buffer: (594)
@!dvi_index=0..dvi_buf_size; {an index into the output buffer}
@y
@!dvi_index=0..dvi_buf_size; {an index into the output buffer}
@!packed_bytes=packed array[dvi_index] of eight_bits;
  {buffer for \.{DVI} output}
@z
@x
@!dvi_buf:array[dvi_index] of eight_bits; {buffer for \.{DVI} output}
@y
@!dvi_buf:packed_bytes; {buffer for \.{DVI} output}
@z
@x
@p procedure write_dvi(@!a,@!b:dvi_index);
var k:dvi_index;
begin for k:=a to b do write(dvi_file,dvi_buf[k]);
end;
@y
@p procedure ary_out(var f:file;@!b:packed_bytes; @!o,@!c:integer);
  extern;@t\2@>@#
procedure write_dvi(@!a,@!b:dvi_index);
begin ary_out(dvi_file,dvi_buf,a div 4,(b+1-a)div 4);
end;
@z

@x changes to ship_out [638]
begin if tracing_output>0 then
@y
begin LR_ptr:=get_avail; info(LR_ptr):=0; {|begin_L_code| at outer level}
if tracing_output>0 then
@z
@x (module 639)
flush_node_list(p);
@y
flush_node_list(p); @<Flush the LR stack@>;
@z

@x "r DVIdover": (642)
  b_close(dvi_file);
@y
  b_close(dvi_file);
  if pseudo_typein=0 then
    begin k:=selector; selector:=new_string;
    pool_ptr:=str_start[str_ptr];
    print("r DVIdover;"); print(output_file_name);
    selector:=k;
    if pool_ptr<pool_size then
     if str_ptr<max_strings then {|overflow| can't occur}
      pseudo_typein:=make_string;
    end;
@z

@x hpack changes [649]
begin r:=get_node(box_node_size); type(r):=hlist_node;
@y
@!LR_ptr,@!LR_tmp:pointer; {for LR stack maintenance}
@!LR_problems:integer; {counts missing begins and ends}
begin LR_ptr:=null; LR_problems:=0;
r:=get_node(box_node_size); type(r):=hlist_node;
@z
@x (module 649, continued)
exit: hpack:=r;
@y
exit: @<Check for LR anomalies at the end of |hpack|@>;
hpack:=r;
@z

@x Page numbers on over/underfull box messages: (663)
  print_int(line);
@y
  print_int(line);
  if page>1 then
    begin print(", p."); print_int(page);
    end;
@z
@x
  print_int(line);
@y
  print_int(line);
  if page>1 then
    begin print(", p."); print_int(page);
    end;
@z

@x post_line_break stuff [877]
begin @<Reverse the links of the relevant passive nodes, setting |cur_p| to the
@y
@!LR_ptr,@!LR_tmp:pointer; {for LR stack maintenance}
begin LR_ptr:=null;
 @<Reverse the links of the relevant passive nodes, setting |cur_p| to the
@z
@x (module 877, continued)
prev_graf:=best_line-1;
@y
prev_graf:=best_line-1;
@<Flush the LR stack@>;
@z
@x (module 880)
@<Modify the end of the line to reflect the nature of the break and to include
  \.{\\rightskip}; also set the proper value of |disc_break|@>;
@y
@<Insert LR nodes at the beginning of the current line@>;
@<Adjust the LR stack based on LR nodes in this line@>;
@<Modify the end of the line to reflect the nature of the break and to include
  \.{\\rightskip}; also set the proper value of |disc_break|@>;
@<Insert LR nodes at the end of the current line@>;
@z

@x new horizontal command [1090]
   vmode+ex_space:@t@>@;@/
@y
   vmode+ex_space,vmode+LR:@t@>@;@/
@z

@x math in text is L-text [module 1196]
begin tail_append(new_math(math_surround,before));
@y
begin tail_append(new_math(math_surround,before));
@<Append a |begin_L| to the tail of the current list@>;
@z
@x (module 1196, continued)
tail_append(new_math(math_surround,after));
@y
@<Append an |end_L| to the tail of the current list@>;
tail_append(new_math(math_surround,after));
@z

@x The endgame: (1335)
@<Last-minute...@>=
@y
The new stuff at {\mc SAIL} has to do with preparing for what the user
presumably wants to do next, by typing it for him/her.

@<Last-minute...@>=
@z
@x
end;
@y
if (pseudo_typein<>0)and(interaction>batch_mode) then
  begin write_ln(term_out);
  for k:=str_start[pseudo_typein] to str_start[pseudo_typein+1]-1 do
    pto_chr(xchr[str_pool[k]]);
  end;
end;
@z

@x whatsit processing [1341]
@d special_node=3 {|subtype| in whatsits that represent \.{\\special} things}
@y
@d special_node=3 {|subtype| in whatsits that represent \.{\\special} things}
@d LR_node=4 {|subtype| in whatsits that represent \.{\\beginL}, etc.}
@d LR_type(#)==mem[#+1].int {the sub-subtype}
@z
@x (module 1344)
@d immediate_code=4 {command modifier for \.{\\immediate}}

@<Put each...@>=
@y
@d immediate_code=4 {command modifier for \.{\\immediate}}
@d begin_L_code=0 {command modifier for \.{\\beginL}}
@d begin_R_code=1 {command modifier for \.{\\beginR}}
@d end_L_code=2 {command modifier for \.{\\endL}}
@d end_R_code=3 {command modifier for \.{\\endR}}
@d begin_LR(#)==(LR_type(#)<end_L_code)
@d begin_LR_type(#)==(LR_type(#)-end_L_code)

@<Put each...@>=
primitive("beginL",LR,begin_L_code);@/
@!@:begin_L_}{\.{\\beginL} primitive@>
primitive("beginR",LR,begin_R_code);@/
@!@:begin_R_}{\.{\\beginR} primitive@>
primitive("endL",LR,end_L_code);@/
@!@:end_L_}{\.{\\endL} primitive@>
primitive("endR",LR,end_R_code);@/
@!@:end_R_}{\.{\\endR} primitive@>
@z
@x (module 1346)
@ @<Cases of |print_cmd_chr|...@>=
@y
@ @<Cases of |print_cmd_chr|...@>=
LR: case chr_code of
begin_L_code: print_esc("beginL");
begin_R_code: print_esc("beginR");
end_L_code: print_esc("endL");
othercases print_esc("endR")
endcases;
@z
@x (module 1356)
othercases print("whatsit?")
@y
LR_node: case LR_type(p) of
begin_L_code: print_esc("beginL");
begin_R_code: print_esc("beginR");
end_L_code: print_esc("endL");
othercases print_esc("endR")
endcases;
othercases print("whatsit?")
@z
@x (module 1357)
close_node: begin r:=get_node(small_node_size); words:=small_node_size;
@y
close_node,LR_node: begin r:=get_node(small_node_size); words:=small_node_size;
@z
@x (module 1358)
close_node: free_node(p,small_node_size);
@y
close_node,LR_node: free_node(p,small_node_size);
@z
@x (module 1360)
@ @<Incorporate a whatsit node into an hbox@>=do_nothing
@y
@ @<Incorporate a whatsit node into an hbox@>=
if subtype(p)=LR_node then @<Adjust the LR stack for the |hpack| routine@>
@z
@x (module 1366)
@ @<Output the whatsit node |p| in an hlist@>=
out_what(p)
@y
@ @<Output the whatsit node |p| in an hlist@>=
if subtype(p)<>LR_node then out_what(p)
else @<Output a reflection instruction if the direction has changed@>
@z

@x Final system-dependent changes: [1376]
This section should be replaced, if necessary, by changes to the program
that are necessary to make \TeX\ work at a particular installation.
It is usually best to design your change file so that all changes to
previous sections preserve the section numbering; then everybody's version
will be consistent with the published program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@^system dependencies@>
@y
Now we do the main work required for mixed-direction texts.

@<Cases of |main_control| that build...@>=
hmode+LR: begin new_whatsit(LR_node,small_node_size); LR_type(tail):=cur_chr;
  end;
mmode+LR: report_illegal_case;

@ @<Glob...@>=
@!LR_ptr,@!LR_tmp:pointer; {stack of LR codes and temp for manipulation}

@ @<Declare functions needed for special kinds of nodes@>=
function new_LR(@!s:small_number): pointer;
var p: pointer; {the new node}
begin p:=get_node(small_node_size); type(p):=whatsit_node; subtype(p):=LR_node;
LR_type(p):=s; new_LR:=p;
end;

@ @<Declare functions needed for special kinds of nodes@>=
function safe_info(@!p:pointer): integer;
begin if p=null then safe_info:=-1@+else safe_info:=info(p);
end;

@ @<Append a |begin_L| to the tail of the current list@>=
tail_append(new_LR(begin_L_code))

@ @<Append an |end_L| to the tail of the current list@>=
tail_append(new_LR(end_L_code))

@ A number of routines are based on a stack of one-word nodes whose |info|
fields contain either |begin_L_code| or |begin_R_code|. The top of the
stack is pointed to by |LR_ptr|, and an auxiliary variable |LR_tmp| is
available for stack manipulation.

@d push_LR(#)==begin LR_tmp:=get_avail; info(LR_tmp):=LR_type(#);
  link(LR_tmp):=LR_ptr; LR_ptr:=LR_tmp;
  end
@d pop_LR==begin LR_tmp:=LR_ptr; LR_ptr:=link(LR_tmp); free_avail(LR_tmp);
  end

@<Flush the LR stack@>=while LR_ptr<>null do pop_LR

@ @<Insert LR nodes at the beginning of the current line@>=
while LR_ptr<>null do
  begin LR_tmp:=new_LR(info(LR_ptr)); link(LR_tmp):=link(temp_head);
  link(temp_head):=LR_tmp; pop_LR;
  end

@ @<Adjust the LR stack based on LR nodes in this line@>=
q:=link(temp_head);
while q<>cur_break(cur_p) do
  begin if notis_char_node(q) then if type(q)=whatsit_node then
   if subtype(q)=LR_node then
    if begin_LR(q) then push_LR(q)
    else if LR_ptr<>null then
     if info(LR_ptr)=begin_LR_type(q) then pop_LR;
  q:=link(q);
  end

@ We use the fact that |q| now points to the node with \.{\\rightskip} glue.

@<Insert LR nodes at the end of the current line@>=
if LR_ptr<>null then
  begin s:=temp_head; r:=link(s);
  while r<>q do
    begin s:=r; r:=link(s);
    end;
  r:=LR_ptr;
  while r<>null do
    begin LR_tmp:=new_LR(info(r)+end_L_code);
    link(s):=LR_tmp; s:=LR_tmp; r:=link(r);
    end;
  link(s):=q;
  end

@ @<Adjust the LR stack for the |hpack| routine@>=
if begin_LR(p) then push_LR(p)
else if safe_info(LR_ptr)=begin_LR_type(p) then pop_LR
else  begin incr(LR_problems);
  while link(q)<>p do q:=link(q);
  link(q):=link(p); free_node(p,small_node_size); p:=q;
  end

@ @<Check for LR anomalies at the end of |hpack|@>=
if LR_ptr<>null then
  begin while link(q)<>null do q:=link(q);
  repeat link(q):=new_LR(info(LR_ptr)+end_L_code); q:=link(q);
  LR_problems:=LR_problems+10000; pop_LR;
  until LR_ptr=null;
  end;
if LR_problems>0 then
  begin print_ln; print_nl("\endL or \endR problem (");@/
  print_int(LR_problems div 10000); print(" missing, ");@/
  print_int(LR_problems mod 10000); print(" extra");@/
  LR_problems:=0; goto common_ending;
  end

@ @<Output a reflection instruction if the direction has changed@>=
if begin_LR(p) then
  begin if safe_info(LR_ptr)<>LR_type(p) then
    begin synch_h; synch_v; dvi_out(begin_reflect);
    end;
  push_LR(p);
  end
else if safe_info(LR_ptr)=begin_LR_type(p) then
  begin pop_LR;
  if info(LR_ptr)+end_L_code<>LR_type(p) then
    begin synch_h; synch_v; dvi_out(end_reflect);
    end;
  end
else confusion("LR")
@:this can't happen LR}{\quad LR@>


@ Finally, here are the remaining things needed to make the implementation
complete at {\mc SAIL}.
@^system dependencies@>

@ The |pseudo_typein| variable is set nonzero if the |error| routine
uses the `\.E' option to exit and edit.

@<Glob...@>=
@!pseudo_typein:str_number;

@ @<Set init...@>=
pseudo_typein:=0; page:=0;

@ The |mem| array is declared after the other global variables, so
that our friendly \PASCAL\ will compile things correctly.

@<Glob...@>=
@!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area}
@z