summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/bibtex.web
blob: 543467df4c4cbe1eb1a2a890c66a02b07c6c4dba (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
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
% This program is copyright (C) 1985, 1988, 2010 by Oren Patashnik;
% all rights are reserved.
% 
% This program, BibTeX, is available under the same terms as
% Donald Knuth's TeX program.
% 
% (Request to implementors: The WEB system provides for alterations via
% an auxiliary file; the master file should stay intact.)
% 
% See Appendix H of the WEB manual for hints on how to install this program.

% Version 0.98f was released in March 1985.
% Version 0.98g was released in April; it removed some system dependencies
%       (introducing term_in and term_out in place of just tty, and removing
%       some nonlocal goto's) and it gave context for certain parsing errors.
% Version 0.98h was released in April; it patched a bug in the output
%       line-breaking routine that can arise with some nonstandard style files.
% Version 0.98i was released in May; its main change split up the main program
%       and some procedures to help certain compilers cope with size
%       limitations, among other things changing error and warning macros so
%       they'd produce (much) less inline code; it also redefined the class of
%       legal style-file identifiers---although this affects only the bizarre
%       ones, it makes BibTeX's error messages more coherent; and it had many
%       minor changes, including about a 15% speed-up on TOPS-20.
% Version 0.99a was released in January 1988.  Its main changes: allowed the
%       inclusion of entire .bib files (rather than just those entries
%       \cited or \nocited); made the sorting algorithm stable; eliminated
%       any case conversion for file names; allowed concatenation in database
%       fields and string definitions; handled hyphenated names properly;
%       handled accented characters properly; implemented new empty$,
%       preamble$, text.length$, text.prefix$, and warning$ built-in functions;
%       allowed a new cross-referencing feature; and made many minor fixes,
%       including about a 40% speed-up on TOPS-20.
% Version 0.99b was released in February 1988.  It changed text.length$ and
%       text.prefix$ to not count braces as text characters, and it changed
%       text.prefix$ to add any necessary matching right braces.
% Version 0.99c was released in February 1988.  It removed two begin-end pairs
%       that, for convention only, surrounded entire modules, but that elicited
%       label-related complaints from some compilers.
% Version 0.99d was released in March 2010.  It made output lines breakable
%       only at white_space (so that, for example, URLs would not be broken).
%       Other known bugs (all minor) will be fixed in a subsequent release.
% Updated bibtex.web was released on 8 December 2010.  Still version
%       0.99d; this release clarified the license.

% Please report any bugs to biblio@@tug.org

% Although considerable effort has been expended to make the BibTeX program
% correct and reliable, no warranty is implied; the author disclaims any
% obligation or liability for damages, including but not limited to
% special, indirect, or consequential damages arising out of or in
% connection with the use or performance of this software.

% This program was written by Oren Patashnik, in consultation with Leslie
% Lamport, to be used with Lamport's LaTeX document preparation system.
% Some modules were taken from Knuth's TeX and TeXware with his permission.

% Here is TeX material that gets inserted after \input webmac
\def\hang{\hangindent 3em\indent\ignorespaces}
\font\ninerm=cmr9
\let\mc=\ninerm % medium caps for names like PASCAL
\def\PASCAL{{\mc PASCAL}}
\def\ph{{\mc PASCAL-H}}
\def\<#1>{$\langle#1\rangle$}
\def\section{\mathhexbox278}

\def\(#1){} % this is used to make section names sort themselves better
\def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>

% Note: WEAVE will typeset an upper-case `E' in a PASCAL identifier a
% bit strangely so that the `TeX' in the name of this program is typeset
% correctly; if this becomes a problem remove these three lines to get
% normal upper-case `E's in PASCAL identifiers
\def\drop{\kern-.1667em\lower.5ex\hbox{E}\kern-.125em} % middle of TeX
\catcode`E=13 \uppercase{\def E{e}}
\def\\#1{\hbox{\let E=\drop\it#1\/\kern.05em}} % italic type for identifiers

\font\sc=cmcsc10

\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\def\LaTeX{{\rm L\kern-.36em\raise.3ex\hbox{\sc a}\kern-.15em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\def\title{\BibTeX\ }
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\def\topofcontents{\null\vfill
 \def\titlepage{F}
 \centerline{\:\titlefont The {\:\ttitlefont \BibTeX} preprocessor}
 \vskip 15pt \centerline{(Version 0.99d---\today)} \vfill}
\pageno=\contentspagenumber \advance\pageno by 1



@* Introduction.
@^documentation@>
@^space savings@>
@^system dependencies@>
@^wizard@>
@!@:BibTeX}{\BibTeX@>
@!@:BibTeX documentation}{\BibTeX\ documentation@>
@:LaTeX}{\LaTeX@>
\BibTeX\ is a preprocessor (with elements of postprocessing as
explained below) for the \LaTeX\ document-preparation system.  It
handles most of the formatting decisions required to produce a
reference list, outputting a \.{.bbl} file that a user can edit to add
any finishing touches \BibTeX\ isn't designed to handle (in practice,
such editing almost never is needed); with this file \LaTeX\ actually
produces the reference list.

Here's how \BibTeX\ works.  It takes as input (a)~an \.{.aux} file
produced by \LaTeX\ on an earlier run; (b)~a \.{.bst} file (the style
file), which specifies the general reference-list style and specifies
how to format individual entries, and which is written by a style
designer (called a wizard throughout this program) in a
special-purpose language described in the \BibTeX\ documentation---see
the file {\.{btxdoc.tex}}; and (c)~\.{.bib} file(s) constituting a
database of all reference-list entries the user might ever hope to
use.  \BibTeX\ chooses from the \.{.bib} file(s) only those entries
specified by the \.{.aux} file (that is, those given by \LaTeX's
\.{\\cite} or \.{\\nocite} commands), and creates as output a \.{.bbl}
file containing these entries together with the formatting commands
specified by the \.{.bst} file (\BibTeX\ also creates a \.{.blg} log
file, which includes any error or warning messages, but this file
isn't used by any program).  \LaTeX\ will use the \.{.bbl} file,
perhaps edited by the user, to produce the reference list.

Many modules of \BibTeX\ were taken from Knuth's \TeX\ and \TeX ware,
with his permission.  All known system-dependent modules are marked in
the index entry ``system dependencies''; Dave Fuchs helped exorcise
unwanted ones.  In addition, a few modules that can be changed to make
\BibTeX\ smaller are marked in the index entry ``space savings''.

Megathanks to Howard Trickey, for whose suggestions future users and
style writers would be eternally grateful, if only they knew.

The |banner| string defined here should be changed whenever \BibTeX\
gets modified.

@d banner=='This is BibTeX, Version 0.99d' {printed when the program starts}


@
@^system dependencies@>
Terminal output goes to the file |term_out|, while terminal input
comes from |term_in|.  On our system, these (system-dependent) files
are already opened at the beginning of the program, and have the same
real name.

@d term_out == tty
@d term_in == tty


@
@^system dependencies@>
This program uses the term |print| instead of |write| when writing on
both the |log_file| and (system-dependent) |term_out| file, and it
uses |trace_pr| when in |trace| mode, for which it writes on just the
|log_file|.  If you want to change where either set of macros writes
to, you should also change the other macros in this program for that
set; each such macro begins with |print_| or |trace_pr_|.

@d print(#) == begin write(log_file,#); write(term_out,#); end
@d print_ln(#) == begin write_ln(log_file,#); write_ln(term_out,#); end
@d print_newline == print_a_newline
                                {making this a procedure saves a little space}
@#
@d trace_pr(#) == begin write(log_file,#); end
@d trace_pr_ln(#) == begin write_ln(log_file,#); end
@d trace_pr_newline == begin write_ln(log_file); end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_a_newline;
begin
write_ln(log_file);
write_ln(term_out);
end;


@
@^debugging@>
@^statistics@>
Some of the code below is intended to be used only when diagnosing the
strange behavior that sometimes occurs when \BibTeX\ is being
installed or when system wizards are fooling around with \BibTeX\
without quite knowing what they are doing. Such code will not normally
be compiled; it is delimited by the codewords
`$|debug|\ldots|gubed|$', with apologies to people who wish to
preserve the purity of English. Similarly, there is some conditional
code delimited by `$|stat|\ldots|tats|$' that is intended only for use
when statistics are to be kept about \BibTeX's memory/cpu usage,
and there is conditional code delimited by `$|trace|\ldots|ecart|$'
that is intended to be a trace facility for use mainly when debugging
\.{.bst} files.

@d debug == @{          { remove the `|@{|' when debugging }
@d gubed == @t@>@}      { remove the `|@}|' when debugging }
@f debug == begin
@f gubed == end
@#
@d stat == @{           { remove the `|@{|' when keeping statistics }
@d tats == @t@>@}       { remove the `|@}|' when keeping statistics }
@f stat == begin
@f tats == end
@#
@d trace == @{          { remove the `|@{|' when in |trace| mode }
@d ecart == @t@>@}      { remove the `|@}|' when in |trace| mode }
@f trace == begin
@f ecart == end


@
@^system dependencies@>
We assume that |case| statements may include a
default case that applies if no matching label is found,
since most \PASCAL\ compilers have plugged this hole in the language by
incorporating some sort of default mechanism. For example, the \ph\
compiler allows `|others|:' as a default label, and other \PASCAL s allow
syntaxes like `\ignorespaces|else|\unskip' or `\\{otherwise}' or
`\\{otherwise}:', etc. The definitions of |othercases| and |endcases|
should be changed to agree with local conventions.   Note that no semicolon
appears before |endcases| in this program, so the definition of |endcases|
should include a semicolon if the compiler wants one.  (Of course, if no
default mechanism is available, the |case| statements of \BibTeX\ will have
to be laboriously extended by listing all remaining cases. People who are
stuck with such \PASCAL s have in fact done this, successfully but not
happily!)

@d othercases == others:        {default for cases not listed explicitly}
@d endcases == @+end {follows the default case in an extended |case| statement}
@f othercases == else
@f endcases == end


@
Labels are given symbolic names by the following definitions, so that
occasional |goto| statements will be meaningful.  We insert the label
`|exit|:' just before the `\ignorespaces|end|\unskip' of a procedure
in which we have used the `|return|' statement defined below (and this
is the only place `|exit|:' appears).  This label is sometimes used
for exiting loops that are set up with the |loop| construction defined
below.  Another generic label is `|loop_exit|:'; it appears
immediately after a loop.

Incidentally, this program never declares a label that isn't actually used,
because some fussy \PASCAL\ compilers will complain about redundant labels.

@d exit=10              {go here to leave a procedure}
@d loop_exit=15         {go here to leave a loop within a procedure}
@d loop1_exit=16        {the first generic label for a procedure with two}
@d loop2_exit=17        {the second}


@
@^for loops@>
And |while| we're discussing loops: This program makes into |while|
loops many that would otherwise be |for| loops because of Standard
\PASCAL\ limitations (it's a bit complicated---standard \PASCAL\
doesn't allow a global variable as the index of a |for| loop inside a
procedure; furthermore, many compilers have fairly severe limitations
on the size of a block, including the main block of the program; so
most of the code in this program occurs inside procedures, and since
for other reasons this program must use primarily global variables, it
doesn't use many |for| loops).


@
@^program conventions@>
This program uses this convention: If there are several quantities in
a boolean expression, they are ordered by expected frequency (except
perhaps when an error message results) so that execution will be
fastest; this is more an attempt to understand the program than to
make it faster.


@
Here are some macros for common programming idioms.

@d incr(#) == #:=#+1    {increase a variable by unity}
@d decr(#) == #:=#-1    {decrease a variable by unity}
@d loop == @+ while true do@+   {repeat over and over until a |goto| happens}
@f loop == xclause
  {\.{WEB}'s |xclause| acts like `\ignorespaces|while true do|\unskip'}
@d do_nothing ==        {empty statement}
@d return == goto exit  {terminate a procedure call}
@f return == nil
@d empty=0              {symbolic name for a null constant}
@d any_value=0          {this appeases \PASCAL's boolean-evaluation scheme}



@* The main program.
@^system dependencies@>
@:LaTeX}{\LaTeX@>
This program first reads the \.{.aux} file that \LaTeX\ produces,
(\romannumeral1) determining which \.{.bib} file(s) and \.{.bst} file
to read and (\romannumeral2) constructing a list of cite keys in order
of occurrence.  The \.{.aux} file may have other \.{.aux} files nested
within.  Second, it reads and executes the \.{.bst} file,
(\romannumeral1) determining how and in which order to process the
database entries in the \.{.bib} file(s) corresponding to those cite
keys in the list (or in some cases, to all the entries in the \.{.bib}
file(s)), (\romannumeral2) determining what text to be output for each
entry and determining any additional text to be output, and
(\romannumeral3) actually outputting this text to the \.{.bbl} file.
In addition, the program sends error messages and other remarks to the
|log_file| and terminal.

@d close_up_shop=9998           {jump here after fatal errors}
@d exit_program=9999            {jump here if we couldn't even get started}

@p
@t\4@>@<Compiler directives@>@/
program BibTEX;                 {all files are opened dynamically}
label   close_up_shop,@!exit_program @<Labels in the outer block@>;
const   @<Constants in the outer block@>
type    @<Types in the outer block@>
var     @<Globals in the outer block@>@;
@<Procedures and functions for about everything@>@;
@<The procedure |initialize|@>
@#
begin
initialize;
print_ln(banner);@/
@<Read the \.{.aux} file@>;
@<Read and execute the \.{.bst} file@>;
close_up_shop:
@<Clean up and leave@>;
exit_program:
end.


@
@^overflow in arithmetic@>
@^system dependencies@>
If the first character of a \PASCAL\ comment is a dollar sign,
\ph\ treats the comment as a list of ``compiler directives'' that will
affect the translation of this program into machine language.  The
directives shown below specify full checking and inclusion of the \PASCAL\
debugger when \BibTeX\ is being debugged,
but they cause range checking and other
redundant code to be eliminated when the production system is being generated.
Arithmetic overflow will be detected in all cases.

@<Compiler directives@>=
@{@&$C-,A+,D-@}  {no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D+@}@+ gubed            {but turn everything on when debugging}


@
@^bottom up@>
@^gymnastics@>
@^mooning@>
All procedures in this program (except for |initialize|) are grouped
into one of the seven classes below, and these classes are dispersed
throughout the program.  However: Much of this program is written top
down, yet \PASCAL\ wants its procedures bottom up.  Since mooning is
neither a technically nor a socially acceptable solution to the
bottom-up problem, this section instead performs the topological
gymnastics that \.{WEB} allows, ordering these classes to satisfy
\PASCAL\ compilers.  There are a few procedures still out of place
after this ordering, though, and the other modules that complete the
task have ``gymnastics'' as an index entry.

@<Procedures and functions for about everything@>=
@<Procedures and functions for all file I/O, error messages, and such@>@;
@<Procedures and functions for file-system interacting@>@;
@<Procedures and functions for handling numbers, characters, and strings@>@;
@<Procedures and functions for input scanning@>@;
@<Procedures and functions for name-string processing@>@;
@<Procedures and functions for style-file function execution@>@;
@<Procedures and functions for the reading and processing of input files@>


@
This procedure gets things started properly.

@<The procedure |initialize|@>=
procedure initialize;
var @<Local variables for initialization@>
begin
@<Check the ``constant'' values for consistency@>;
if (bad > 0) then
    begin
    write_ln (term_out,bad:0,' is a bad bad');
    goto exit_program;
    end;
@<Set initial values of key variables@>;
pre_def_certain_strings;@/
get_the_top_level_aux_file_name;
end;


@
@^space savings@>
@^system dependencies@>
These parameters can be changed at compile time to extend or reduce
\BibTeX's capacity.  They are set to accommodate about 750 cites when
used with the standard styles, although |pool_size| is usually the
first limitation to be a problem, often when there are 500 cites.

@<Constants in the outer block@>=
@!buf_size=1000; {maximum number of characters in an input line (or string)}
@!min_print_line=3; {minimum \.{.bbl} line length: must be |>=3|}
@!max_print_line=79; {the maximum: must be |>min_print_line| and |<buf_size|}
@!aux_stack_size=20; {maximum number of simultaneous open \.{.aux} files}
@!max_bib_files=20; {maximum number of \.{.bib} files allowed}
@!pool_size=65000; {maximum number of characters in strings}
@!max_strings=4000; {maximum number of strings, including pre-defined;
                                                        must be |<=hash_size|}
@!max_cites=750; {maximum number of distinct cite keys; must be
                                                        |<=max_strings|}
@!min_crossrefs=2; {minimum number of cross-refs required for automatic
                                                        |cite_list| inclusion}
@!wiz_fn_space=3000; {maximum amount of |wiz_defined|-function space}
@!single_fn_space=100; {maximum amount for a single |wiz_defined|-function}
@!max_ent_ints=3000; {maximum number of |int_entry_var|s
                                        (entries $\times$ |int_entry_var|s)}
@!max_ent_strs=3000; {maximum number of |str_entry_var|s
                                        (entries $\times$ |str_entry_var|s)}
@!ent_str_size=100; {maximum size of a |str_entry_var|; must be |<=buf_size|}
@!glob_str_size=1000; {maximum size of a |str_global_var|;
                                                        must be |<=buf_size|}
@!max_fields=17250; {maximum number of fields (entries $\times$ fields,
                                        about |23*max_cites| for consistency)}
@!lit_stk_size=100; {maximum number of literal functions on the stack}


@
@^space savings@>
@^system dependencies@>
These parameters can also be changed at compile time, but they're
needed to define some \.{WEB} numeric macros so they must be so
defined themselves.

@d hash_size=5000       {must be |>= max_strings| and |>= hash_prime|}
@d hash_prime=4253      {a prime number about 85\% of |hash_size| and |>= 128|
                                                and |< @t$2^{14}-2^6$@>|}
@d file_name_size=40    {file names shouldn't be longer than this}
@d max_glob_strs=10     {maximum number of |str_global_var| names}
@d max_glb_str_minus_1 = max_glob_strs-1  {to avoid wasting a |str_global_var|}


@
In case somebody has inadvertently made bad settings of the ``constants,''
\BibTeX\ checks them using a global variable called |bad|.

This is the first of many sections of \BibTeX\ where global variables are
defined.

@<Globals in the outer block@>=
@!bad:integer;          {is some ``constant'' wrong?}


@
Each digit-value of |bad| has a specific meaning.

@<Check the ``constant'' values for consistency@>=
bad := 0;
if (min_print_line < 3) then                    bad:=1;
if (max_print_line <= min_print_line) then      bad:=10*bad+2;
if (max_print_line >= buf_size) then            bad:=10*bad+3;
if (hash_prime < 128) then                      bad:=10*bad+4;
if (hash_prime > hash_size) then                bad:=10*bad+5;
if (hash_prime >= (16384-64)) then              bad:=10*bad+6;
if (max_strings > hash_size) then               bad:=10*bad+7;
if (max_cites > max_strings) then               bad:=10*bad+8;
if (ent_str_size > buf_size) then               bad:=10*bad+9;
if (glob_str_size > buf_size) then              bad:=100*bad+11;
                                                        {well, almost each}


@
A global variable called |history| will contain one of four values at
the end of every run: |spotless| means that no unusual messages were
printed; |warning_message| means that a message of possible interest
was printed but no serious errors were detected; |error_message| means
that at least one error was found; |fatal_message| means that the
program terminated abnormally. The value of |history| does not
influence the behavior of the program; it is simply computed for the
convenience of systems that might want to use such information.

@d spotless=0           {|history| value for normal jobs}
@d warning_message=1    {|history| value when non-serious info was printed}
@d error_message=2      {|history| value when an error was noted}
@d fatal_message=3      {|history| value when we had to stop prematurely}

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure mark_warning;
begin
if (history = warning_message) then
    incr(err_count)
  else if (history = spotless) then
    begin
    history := warning_message;
    err_count := 1;
    end;
end;
@#
procedure mark_error;
begin
if (history < error_message) then
    begin
    history := error_message;
    err_count := 1;
    end
  else  {|history = error_message|}
    incr(err_count);
end;
@#
procedure mark_fatal;
begin
history := fatal_message;
end;


@
For the two states |warning_message| and |error_message| we keep track
of the number of messages given; but since |warning_message|s aren't
so serious, we ignore them once we've seen an |error_message|.  Hence
we need just the single variable |err_count| to keep track.


@<Globals in the outer block@>=
@!history:spotless..fatal_message; {how bad was this run?}
@!err_count:integer;


@
The |err_count| gets set or reset when |history| first changes to
|warning_message| or |error_message|, so we don't need to initialize
it.

@<Set initial values of key variables@>=
history := spotless;



@* The character set.
@^ASCII code@>
(The following material is copied (almost) verbatim from \TeX.
Thus, the same system-dependent changes should be made to both programs.)

In order to make \TeX\ readily portable between a wide variety of
computers, all of its input text is converted to an internal seven-bit
code that is essentially standard ASCII, the ``American Standard Code for
Information Interchange.''  This conversion is done immediately when each
character is read in. Conversely, characters are converted from ASCII to
the user's external representation just before they are output to a
text file.

Such an internal code is relevant to users of \TeX\ primarily because it
governs the positions of characters in the fonts. For example, the
character `\.A' has ASCII code $65=@'101$, and when \TeX\ typesets
this letter it specifies character number 65 in the current font.
If that font actually has `\.A' in a different position, \TeX\ doesn't
know what the real position is; the program that does the actual printing from
\TeX's device-independent files is responsible for converting from ASCII to
a particular font encoding.

\TeX's internal code is relevant also with respect to constants
that begin with a reverse apostrophe.


@
Characters of text that have been converted to \TeX's internal form
are said to be of type |ASCII_code|, which is a subrange of the integers.

@<Types in the outer block@>=
@!ASCII_code=0..127;    {seven-bit numbers}


@
@^character set dependencies@>
@^system dependencies@>
The original \PASCAL\ compiler was designed in the late 60s, when six-bit
character sets were common, so it did not make provision for lower-case
letters. Nowadays, of course, we need to deal with both capital and small
letters in a convenient way, especially in a program for typesetting;
so the present specification of \TeX\ has been written under the assumption
that the \PASCAL\ compiler and run-time system permit the use of text files
with more than 64 distinguishable characters. More precisely, we assume that
the character set contains at least the letters and symbols associated
with ASCII codes @'40 through @'176; all of these characters are now
available on most computer terminals.

Since we are dealing with more characters than were present in the first
\PASCAL\ compilers, we have to decide what to call the associated data
type. Some \PASCAL s use the original name |char| for the
characters in text files, even though there now are more than 64 such
characters, while other \PASCAL s consider |char| to be a 64-element
subrange of a larger data type that has some other name.

In order to accommodate this difference, we shall use the name |text_char|
to stand for the data type of the characters that are converted to and
from |ASCII_code| when they are input and output. We shall also assume
that |text_char| consists of the elements |chr(first_text_char)| through
|chr(last_text_char)|, inclusive. The following definitions should be
adjusted if necessary.

@d text_char == char    {the data type of characters in text files}
@d first_text_char=0    {ordinal number of the smallest element of |text_char|}
@d last_text_char=127   {ordinal number of the largest element of |text_char|}

@<Local variables for initialization@>=
i:0..last_text_char;    {this is the first one declared}


@
The \TeX\ processor converts between ASCII code and
the user's external character set by means of arrays |xord| and |xchr|
that are analogous to \PASCAL's |ord| and |chr| functions.

@<Globals in the outer block@>=
@!xord: array [text_char] of ASCII_code;
  {specifies conversion of input characters}
@!xchr: array [ASCII_code] of text_char;
  {specifies conversion of output characters}


@
@^character set dependencies@>
@^system dependencies@>
Since we are assuming that our \PASCAL\ system is able to read and write the
visible characters of standard ASCII (although not necessarily using the
ASCII codes to represent them), the following assignment statements initialize
most of the |xchr| array properly, without needing any system-dependent
changes. On the other hand, it is possible to implement \TeX\ with
less complete character sets, and in such cases it will be necessary to
change something here.

@<Set initial values of key variables@>=
xchr[@'40]:=' ';
xchr[@'41]:='!';
xchr[@'42]:='"';
xchr[@'43]:='#';
xchr[@'44]:='$';
xchr[@'45]:='%';
xchr[@'46]:='&';
xchr[@'47]:='''';@/
xchr[@'50]:='(';
xchr[@'51]:=')';
xchr[@'52]:='*';
xchr[@'53]:='+';
xchr[@'54]:=',';
xchr[@'55]:='-';
xchr[@'56]:='.';
xchr[@'57]:='/';@/
xchr[@'60]:='0';
xchr[@'61]:='1';
xchr[@'62]:='2';
xchr[@'63]:='3';
xchr[@'64]:='4';
xchr[@'65]:='5';
xchr[@'66]:='6';
xchr[@'67]:='7';@/
xchr[@'70]:='8';
xchr[@'71]:='9';
xchr[@'72]:=':';
xchr[@'73]:=';';
xchr[@'74]:='<';
xchr[@'75]:='=';
xchr[@'76]:='>';
xchr[@'77]:='?';@/
xchr[@'100]:='@@';
xchr[@'101]:='A';
xchr[@'102]:='B';
xchr[@'103]:='C';
xchr[@'104]:='D';
xchr[@'105]:='E';
xchr[@'106]:='F';
xchr[@'107]:='G';@/
xchr[@'110]:='H';
xchr[@'111]:='I';
xchr[@'112]:='J';
xchr[@'113]:='K';
xchr[@'114]:='L';
xchr[@'115]:='M';
xchr[@'116]:='N';
xchr[@'117]:='O';@/
xchr[@'120]:='P';
xchr[@'121]:='Q';
xchr[@'122]:='R';
xchr[@'123]:='S';
xchr[@'124]:='T';
xchr[@'125]:='U';
xchr[@'126]:='V';
xchr[@'127]:='W';@/
xchr[@'130]:='X';
xchr[@'131]:='Y';
xchr[@'132]:='Z';
xchr[@'133]:='[';
xchr[@'134]:='\';
xchr[@'135]:=']';
xchr[@'136]:='^';
xchr[@'137]:='_';@/
xchr[@'140]:='`';
xchr[@'141]:='a';
xchr[@'142]:='b';
xchr[@'143]:='c';
xchr[@'144]:='d';
xchr[@'145]:='e';
xchr[@'146]:='f';
xchr[@'147]:='g';@/
xchr[@'150]:='h';
xchr[@'151]:='i';
xchr[@'152]:='j';
xchr[@'153]:='k';
xchr[@'154]:='l';
xchr[@'155]:='m';
xchr[@'156]:='n';
xchr[@'157]:='o';@/
xchr[@'160]:='p';
xchr[@'161]:='q';
xchr[@'162]:='r';
xchr[@'163]:='s';
xchr[@'164]:='t';
xchr[@'165]:='u';
xchr[@'166]:='v';
xchr[@'167]:='w';@/
xchr[@'170]:='x';
xchr[@'171]:='y';
xchr[@'172]:='z';
xchr[@'173]:='{';
xchr[@'174]:='|';
xchr[@'175]:='}';
xchr[@'176]:='~';@/
xchr[0]:=' '; xchr[@'177]:=' ';
  {ASCII codes 0 and |@'177| do not appear in text}


@
@^character set dependencies@>
@^system dependencies@>
Some of the ASCII codes without visible characters have been given symbolic
names in this program because they are used with a special meaning.  The
|tab| character may be system dependent.

@d null_code=@'0        {ASCII code that might disappear}
@d tab=@'11             {ASCII code treated as |white_space|}
@d space=@'40           {ASCII code treated as |white_space|}
@d invalid_code=@'177   {ASCII code that should not appear}


@
@^character set dependencies@>
@^system dependencies@>
@:TeXbook}{\sl The \TeX book@>
The ASCII code is ``standard'' only to a certain extent, since many
computer installations have found it advantageous to have ready access
to more than 94 printing characters. Appendix~C of {\sl The \TeX book\/}
gives a complete specification of the intended correspondence between
characters and \TeX's internal representation.

If \TeX\ is being used
on a garden-variety \PASCAL\ for which only standard ASCII
codes will appear in the input and output files, it doesn't really matter
what codes are specified in |xchr[1..@'37]|, but the safest policy is to
blank everything out by using the code shown below.

However, other settings of |xchr| will make \TeX\ more friendly on
computers that have an extended character set, so that users can type things
like `\.^^Z' instead of `\.{\\ne}'. At MIT, for example, it would be more
appropriate to substitute the code
$$\hbox{|for i:=1 to @'37 do xchr[i]:=chr(i);|}$$
\TeX's character set is essentially the same as MIT's, even with respect to
characters less than~@'40. People with extended character sets can
assign codes arbitrarily, giving an |xchr| equivalent to whatever
characters the users of \TeX\ are allowed to have in their input files.
It is best to make the codes correspond to the intended interpretations as
shown in Appendix~C whenever possible; but this is not necessary. For
example, in countries with an alphabet of more than 26 letters, it is
usually best to map the additional letters into codes less than~@'40.

@<Set initial values of key variables@>=
for i:=1 to @'37 do xchr[i]:=' ';
xchr[tab]:=chr(tab);


@
This system-independent code makes the |xord| array contain a suitable
inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]|
where |i<j<@'177|, the value of |xord[xchr[i]]| will turn out to be
|j| or more; hence, standard ASCII code numbers will be used instead
of codes below @'40 in case there is a coincidence.

@<Set initial values of key variables@>=
for i:=first_text_char to last_text_char do xord[chr(i)]:=invalid_code;
for i:=1 to @'176 do xord[xchr[i]]:=i;


@
Also, various characters are given symbolic names; all the ones this
program uses are collected here.  We use the sharp sign as the
|concat_char|, rather than something more natural (like an ampersand),
for uniformity of database syntax (ampersand is a valid character in
identifiers).

@d double_quote = """"          {delimits strings}
@d number_sign = "#"            {marks an |int_literal|}
@d comment = "%"                {ignore the rest of a \.{.bst} or \TeX\ line}
@d single_quote = "'"           {marks a quoted function}
@d left_paren = "("             {optional database entry left delimiter}
@d right_paren = ")"            {corresponding right delimiter}
@d comma = ","                  {separates various things}
@d minus_sign = "-"             {for a negative number}
@d equals_sign = "="            {separates a field name from a field value}
@d at_sign = "@@"               {the beginning of a database entry}
@d left_brace = "{"             {left delimiter of many things}
@d right_brace = "}"            {corresponding right delimiter}
@d period = "."                 {these are three}
@d question_mark = "?"          {string-ending characters}
@d exclamation_mark = "!"       {of interest in \.{add.period\$}}
@d tie = "~"                    {the default space char, in \.{format.name\$}}
@d hyphen = "-"                 {like |white_space|, in \.{format.name\$}}
@d star = "*"                   {for including entire database}
@d concat_char = "#"            {for concatenating field tokens}
@d colon = ":"                  {for lower-casing (usually title) strings}
@d backslash = "\"              {used to recognize accented characters}


@
These arrays give a lexical classification for the |ASCII_code|s;
|lex_class| is used for general scanning and |id_class| is used for
scanning identifiers.

@<Globals in the outer block@>=
@!lex_class: array [ASCII_code] of lex_type;
@!id_class: array [ASCII_code] of id_type;


@
Every character has two types of the lexical classifications.  The
first type is general, and the second type tells whether the character
is legal in identifiers.

@d illegal = 0          {the unrecognized |ASCII_code|s}
@d white_space = 1      {things like |space|s that you can't see}
@d alpha = 2            {the upper- and lower-case letters}
@d numeric = 3          {the ten digits}
@d sep_char = 4         {things sometimes treated like |white_space|}
@d other_lex = 5        {when none of the above applies}
@d last_lex = 5         {the same number as on the line above}
@#
@d illegal_id_char = 0  {a few forbidden ones}
@d legal_id_char = 1    {most printing characters}

@<Types in the outer block@>=
@!lex_type = 0..last_lex;@/
@!id_type = 0..1;


@
@^character set dependencies@>
@^system dependencies@>
Now we initialize the system-dependent |lex_class| array.  The |tab|
character may be system dependent.  Note that the order of these
assignments is important here.

@<Set initial values of key variables@>=
for i:=0 to @'177 do lex_class[i] := other_lex;
for i:=0 to @'37 do lex_class[i] := illegal;
lex_class[invalid_code] := illegal;
lex_class[tab] := white_space;
lex_class[space] := white_space;
lex_class[tie] := sep_char;
lex_class[hyphen] := sep_char;
for i:=@'60 to @'71 do lex_class[i] := numeric;
for i:=@'101 to @'132 do lex_class[i] := alpha;
for i:=@'141 to @'172 do lex_class[i] := alpha;


@
@^character set dependencies@>
@^system dependencies@>
And now the |id_class| array.

@<Set initial values of key variables@>=
for i:=0 to @'177 do id_class[i] := legal_id_char;
for i:=0 to @'37 do id_class[i] := illegal_id_char;
id_class[space] := illegal_id_char;
id_class[tab] := illegal_id_char;
id_class[double_quote] := illegal_id_char;
id_class[number_sign] := illegal_id_char;
id_class[comment] := illegal_id_char;
id_class[single_quote] := illegal_id_char;
id_class[left_paren] := illegal_id_char;
id_class[right_paren] := illegal_id_char;
id_class[comma] := illegal_id_char;
id_class[equals_sign] := illegal_id_char;
id_class[left_brace] := illegal_id_char;
id_class[right_brace] := illegal_id_char;


@
The array |char_width| gives relative printing widths of each
|ASCII_code|, and |string_width| will be used later to sum up
|char_width|s in a string.

@<Globals in the outer block@>=
@!char_width : array [ASCII_code] of integer;
@!string_width : integer;


@
@^character set dependencies@>
@^system dependencies@>
Now we initialize the system-dependent |char_width| array, for which
|space| is the only |white_space| character given a nonzero printing
width.  The widths here are taken from Stanford's June~'87
$cmr10$~font and represent hundredths of a point (rounded), but since
they're used only for relative comparisons, the units have no meaning.

@d ss_width = 500               {character |@'31|'s width in the $cmr10$ font}
@d ae_width = 722               {character |@'32|'s width in the $cmr10$ font}
@d oe_width = 778               {character |@'33|'s width in the $cmr10$ font}
@d upper_ae_width = 903         {character |@'35|'s width in the $cmr10$ font}
@d upper_oe_width = 1014        {character |@'36|'s width in the $cmr10$ font}

@<Set initial values of key variables@>=
for i:=0 to @'177 do char_width[i] := 0;
@#
char_width[@'40] := 278;
char_width[@'41] := 278;
char_width[@'42] := 500;
char_width[@'43] := 833;
char_width[@'44] := 500;
char_width[@'45] := 833;
char_width[@'46] := 778;
char_width[@'47] := 278;
char_width[@'50] := 389;
char_width[@'51] := 389;
char_width[@'52] := 500;
char_width[@'53] := 778;
char_width[@'54] := 278;
char_width[@'55] := 333;
char_width[@'56] := 278;
char_width[@'57] := 500;
char_width[@'60] := 500;
char_width[@'61] := 500;
char_width[@'62] := 500;
char_width[@'63] := 500;
char_width[@'64] := 500;
char_width[@'65] := 500;
char_width[@'66] := 500;
char_width[@'67] := 500;
char_width[@'70] := 500;
char_width[@'71] := 500;
char_width[@'72] := 278;
char_width[@'73] := 278;
char_width[@'74] := 278;
char_width[@'75] := 778;
char_width[@'76] := 472;
char_width[@'77] := 472;
char_width[@'100] := 778;
char_width[@'101] := 750;
char_width[@'102] := 708;
char_width[@'103] := 722;
char_width[@'104] := 764;
char_width[@'105] := 681;
char_width[@'106] := 653;
char_width[@'107] := 785;
char_width[@'110] := 750;
char_width[@'111] := 361;
char_width[@'112] := 514;
char_width[@'113] := 778;
char_width[@'114] := 625;
char_width[@'115] := 917;
char_width[@'116] := 750;
char_width[@'117] := 778;
char_width[@'120] := 681;
char_width[@'121] := 778;
char_width[@'122] := 736;
char_width[@'123] := 556;
char_width[@'124] := 722;
char_width[@'125] := 750;
char_width[@'126] := 750;
char_width[@'127] :=1028;
char_width[@'130] := 750;
char_width[@'131] := 750;
char_width[@'132] := 611;
char_width[@'133] := 278;
char_width[@'134] := 500;
char_width[@'135] := 278;
char_width[@'136] := 500;
char_width[@'137] := 278;
char_width[@'140] := 278;
char_width[@'141] := 500;
char_width[@'142] := 556;
char_width[@'143] := 444;
char_width[@'144] := 556;
char_width[@'145] := 444;
char_width[@'146] := 306;
char_width[@'147] := 500;
char_width[@'150] := 556;
char_width[@'151] := 278;
char_width[@'152] := 306;
char_width[@'153] := 528;
char_width[@'154] := 278;
char_width[@'155] := 833;
char_width[@'156] := 556;
char_width[@'157] := 500;
char_width[@'160] := 556;
char_width[@'161] := 528;
char_width[@'162] := 392;
char_width[@'163] := 394;
char_width[@'164] := 389;
char_width[@'165] := 556;
char_width[@'166] := 528;
char_width[@'167] := 722;
char_width[@'170] := 528;
char_width[@'171] := 528;
char_width[@'172] := 444;
char_width[@'173] := 500;
char_width[@'174] :=1000;
char_width[@'175] := 500;
char_width[@'176] := 500;



@* Input and output.
The basic operations we need to do are
(1)~inputting and outputting of text characters to or from a file;
(2)~instructing the operating system to initiate (``open'')
or to terminate (``close'') input or output to or from a specified file; and
(3)~testing whether the end of an input file has been reached.

@<Types in the outer block@>=
@!alpha_file=packed file of text_char;  {files that contain textual data}


@
@^system dependencies@>
Most of what we need to do with respect to input and output can be handled
by the I/O facilities that are standard in \PASCAL, i.e., the routines
called |get|, |put|, |eof|, and so on. But
standard \PASCAL\ does not allow file variables to be associated with file
names that are determined at run time, so it cannot be used to implement
\BibTeX; some sort of extension to \PASCAL's ordinary |reset| and |rewrite|
is crucial for our purposes. We shall assume that |name_of_file| is a variable
of an appropriate type such that the \PASCAL\ run-time system being used to
implement \BibTeX\ can open a file whose external name is specified by
|name_of_file|. \BibTeX\ does no case conversion for file names.

@<Globals in the outer block@>=
@!name_of_file:packed array[1..file_name_size] of char;
                         {on some systems this is a \&{record} variable}
@!name_length:0..file_name_size;
  {this many characters are relevant in |name_of_file| (the rest are blank)}
@!name_ptr:0..file_name_size+1;         {index variable into |name_of_file|}


@
@^system dependencies@>
@:PASCAL H}{\ph@>
The \ph\ compiler with which the present version of \TeX\ was prepared has
extended the rules of \PASCAL\ in a very convenient way. To open file~|f|,
we can write
$$\vbox{\halign{#\hfil\qquad&#\hfil\cr
|reset(f,@t\\{name}@>,'/O')|&for input;\cr
|rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$
The `\\{name}' parameter, which is of type `\ignorespaces|packed
array[@t\<\\{any}>@>] of text_char|', stands for the name of
the external file that is being opened for input or output.
Blank spaces that might appear in \\{name} are ignored.

The `\.{/O}' parameter tells the operating system not to issue its own
error messages if something goes wrong. If a file of the specified name
cannot be found, or if such a file cannot be opened for some other reason
(e.g., someone may already be trying to write the same file), we will have
|@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|.  This allows
\TeX\ to undertake appropriate corrective action.

\TeX's file-opening procedures return |false| if no file identified by
|name_of_file| could be opened.

@d reset_OK(#)==erstat(#)=0
@d rewrite_OK(#)==erstat(#)=0

@<Procedures and functions for file-system interacting@>=
function erstat(var f:file):integer; extern;    {in the runtime library}
@#@t\2@>
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;


@
@^system dependencies@>
Files can be closed with the \ph\ routine `|close(f)|', which should
be used when all input or output with respect to |f| has been
completed.  This makes |f| available to be opened again, if desired;
and if |f| was used for output, the |close| operation makes the
corresponding external file appear on the user's area, ready to be
read.

@<Procedures and functions for file-system interacting@>=
procedure a_close(var f:alpha_file);            {close a text file}
begin close(f);
end;


@
Text output is easy to do with the ordinary \PASCAL\ |put| procedure,
so we don't have to make any other special arrangements.
The treatment of text input is more difficult, however, because
of the necessary translation to |ASCII_code| values, and because
\TeX's conventions should be efficient and they should
blend nicely with the user's operating environment.


@
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| and |last|.  The |buffer| array contains
|ASCII_code| values, and |last| is an index into this array marking
the end of a line of text.  (Occasionally, |buffer| is used for
something else, in which case it is copied to a temporary array.)

@<Globals in the outer block@>=
@!buffer:buf_type;      {usually, lines of characters being read}
@!last:buf_pointer;     {end of the line just input to |buffer|}


@
@^save space@>
@^space savings@>
@^system dependencies@>
The type |buf_type| is used for |buffer|, for saved copies of it, or
for scratch work.  It's not |packed| because otherwise the program
would run much slower on some systems (more than 25 percent slower,
for example, on a TOPS-20 operating system).  But on systems that are
byte-addressable and that have a good compiler, packing |buf_type|
would save lots of space without much loss of speed.  Other modules
that have packable arrays are also marked with a ``space savings''
index entry.

@<Types in the outer block@>=
@!buf_pointer = 0..buf_size;                    {an index into a |buf_type|}
@!buf_type = array[buf_pointer] of ASCII_code;  {for various buffers}


@
@^kludge@>
And while we're at it, we declare another buffer for general use.
Because buffers are not packed and can get large, we use |sv_buffer|
several purposes; this is a bit kludgy, but it helps make the stack
space not overflow on some machines.  It's used when reading the
entire database file (in the \.{read} command) and when doing
name-handling (through the alias |name_buf|) in the |built_in|
functions \.{format.names\$} and \.{num.names\$}.

@<Globals in the outer block@>=
@!sv_buffer : buf_type;
@!sv_ptr1 : buf_pointer;
@!sv_ptr2 : buf_pointer;
@!tmp_ptr,@!tmp_end_ptr : integer; {copy pointers only, usually for buffers}


@
@.BibTeX capacity exceeded@>
When something in the program wants to be bigger or something out
there wants to be smaller, it's time to call it a run.  Here's the
first of several macros that have associated procedures so that they
produce less inline code.

@d overflow(#)==begin           {fatal error---close up shop}
                print_overflow;
                print_ln(#:0);
                goto close_up_shop;
                end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_overflow;
begin
print ('Sorry---you''ve exceeded BibTeX''s ');
mark_fatal;
end;


@
@.this can't happen@>
When something happens that the program thinks is impossible,
call the maintainer.

@d confusion(#)==begin          {fatal error---close up shop}
                 print (#);
                 print_confusion;
                 goto close_up_shop;
                 end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_confusion;
begin
print_ln ('---this can''t happen');
print_ln ('*Please notify the BibTeX maintainer*');
mark_fatal;
end;


@
@:BibTeX capacity exceeded}{\quad buffer size@>
When a buffer overflows, it's time to complain (and then quit).

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure buffer_overflow;
begin
overflow('buffer size ',buf_size);
end;


@
@:BibTeX capacity exceeded}{\quad buffer size@>
The |input_ln| function brings the next line of input from the
specified file into available positions of the buffer array and
returns the value |true|, unless the file has already been entirely
read, in which case it returns |false| and sets |last:=0|.  In
general, the |ASCII_code| numbers that represent the next line of the
file are input into |buffer[0]|, |buffer[1]|, \dots, |buffer[last-1]|;
and the global variable |last| is set equal to the length of the line.
Trailing |white_space| characters are removed from the line
(|white_space| characters are explained in the character-set section%
---most likely they're blanks); thus, either |last=0| (in which case
the line was entirely blank) or |lex_class[buffer[last-1]]<>white_space|.
An overflow error is given if the normal actions of |input_ln| would
make |last>buf_size|.

Standard \PASCAL\ says that a file should have |eoln| immediately
before |eof|, but \BibTeX\ needs only a weaker restriction: If |eof|
occurs in the middle of a line, the system function |eoln| should return
a |true| result (even though |f^| will be undefined).

@<Procedures and functions for all file I/O, error messages, and such@>=
function input_ln(var f:alpha_file) : boolean;
                                {inputs the next line or returns |false|}
label loop_exit;
begin
last:=0;
if (eof(f)) then input_ln:=false
else
  begin
  while (not eoln(f)) do
    begin
    if (last >= buf_size) then
        buffer_overflow;
    buffer[last]:=xord[f^];
    get(f); incr(last);
    end;
  get(f);
  while (last > 0) do           {remove trailing |white_space|}
    if (lex_class[buffer[last-1]] = white_space) then
      decr(last)
     else
      goto loop_exit;
loop_exit:
  input_ln:=true;
  end;
end;



@* String handling.
\BibTeX\ uses variable-length strings of seven-bit characters.
Since \PASCAL\ does not have a well-developed string mechanism,
\BibTeX\ does all its string processing by home-grown
(predominantly \TeX's) methods.
Unlike \TeX, however, \BibTeX\ does not use a |pool_file| for
string storage; it creates its few pre-defined strings at run-time.

The necessary operations are handled with a simple data structure.
The array |str_pool| contains all the (seven-bit) ASCII codes in all
the strings \BibTeX\ must ever search for (generally identifiers
names), and the array |str_start| contains indices of the starting
points of each such string. Strings are referred to by integer
numbers, so that string number |s| comprises the characters
|str_pool[j]| for |str_start[s]<=j<str_start[s+1]|. Additional integer
variables |pool_ptr| and |str_ptr| indicate the number of entries used
so far in |str_pool| and |str_start|; locations |str_pool[pool_ptr]|
and |str_start[str_ptr]| are ready for the next string to be
allocated.  Location |str_start[0]| is unused so that hashing will
work correctly.

Elements of the |str_pool| array must be ASCII codes that can actually be
printed; i.e., they must have an |xchr| equivalent in the local
character set.

@<Globals in the outer block@>=
@!str_pool : packed array[pool_pointer] of ASCII_code;  {the characters}
@!str_start : packed array[str_number] of pool_pointer; {the starting pointers}
@!pool_ptr : pool_pointer;      {first unused position in |str_pool|}
@!str_ptr : str_number;         {start of the current string being created}
@!str_num : str_number;         {general index variable into |str_start|}
@!p_ptr1,@!p_ptr2 : pool_pointer;       {several procedures use these locally}


@
Where |pool_pointer| and |str_number| are pointers into |str_pool| and
|str_start|.

@<Types in the outer block@>=
@!pool_pointer = 0..pool_size;  {for variables that point into |str_pool|}
@!str_number = 0..max_strings;  {for variables that point into |str_start|}


@
These macros send a string in |str_pool| to an output file.

@d max_pop = 3  {---see the |built_in| functions section}
@#
@d print_pool_str(#) == print_a_pool_str(#)
                                {making this a procedure saves a little space}
@#
@d trace_pr_pool_str(#) == begin
                           out_pool_str(log_file,#);
                           end


@
@^kludge@>
@^system dependencies@>
@:this can't happen}{\quad Illegal string number@>
And here are the associated procedures.  Note: The |term_out| file is
system dependent.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure out_pool_str (var f:alpha_file; @!s:str_number);
var i:pool_pointer;
begin   {allowing |str_ptr <= s < str_ptr+max_pop| is a \.{.bst}-stack kludge}
if ((s<0) or (s>=str_ptr+max_pop) or (s>=max_strings)) then
    confusion ('Illegal string number:',s:0);
for i := str_start[s] to str_start[s+1]-1 do
    write(f,xchr[str_pool[i]]);
end;
@#
procedure print_a_pool_str (@!s:str_number);
begin
out_pool_str(term_out,s);
out_pool_str(log_file,s);
end;


@
@.WEB@>
Several of the elementary string operations are performed using \.{WEB}
macros instead of using \PASCAL\ procedures, because many of the
operations are done quite frequently and we want to avoid the
overhead of procedure calls. For example, here is
a simple macro that computes the length of a string.

@d length(#) == (str_start[#+1]-str_start[#])
                        {the number of characters in string number \#}


@
@:BibTeX capacity exceeded}{\quad pool size@>
Strings are created by appending character codes to |str_pool|.
The macro called |append_char|, defined here, does not check to see if the
value of |pool_ptr| has gotten too high; this test is supposed to be
made before |append_char| is used.

To test if there is room to append |l| more characters to |str_pool|,
we shall write |str_room(l)|, which aborts \BibTeX\ and gives an
error message if there isn't enough room.

@d append_char(#) ==            {put |ASCII_code| \# at the end of |str_pool|}
begin str_pool[pool_ptr]:=#; incr(pool_ptr);
end
@#
@d str_room(#) ==               {make sure that the pool hasn't overflowed}
  begin
  if (pool_ptr+# > pool_size) then
      pool_overflow;
  end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure pool_overflow;
begin
overflow('pool size ',pool_size);
end;


@
@:BibTeX capacity exceeded}{\quad number of strings@>
Once a sequence of characters has been appended to |str_pool|, it
officially becomes a string when the function |make_string| is called.
It returns the string number of the string it just made.

@<Procedures and functions for handling numbers, characters, and strings@>=
function make_string : str_number;      {current string enters the pool}
begin
if (str_ptr=max_strings) then
    overflow('number of strings ',max_strings);
incr(str_ptr);
str_start[str_ptr]:=pool_ptr;
make_string := str_ptr - 1;
end;


@
These macros destroy and recreate the string at the end of the pool.

@d flush_string == begin
                   decr(str_ptr);
                   pool_ptr := str_start[str_ptr];
                   end
@#
@d unflush_string == begin
                     incr(str_ptr);
                     pool_ptr := str_start[str_ptr];
                     end


@
This subroutine compares string |s| with another string that appears
in the buffer |buf| between positions |bf_ptr| and |bf_ptr+len-1|; the
result is |true| if and only if the strings are equal.

@<Procedures and functions for handling numbers, characters, and strings@>=
function str_eq_buf (@!s:str_number; var buf:buf_type;
                                        @!bf_ptr,@!len:buf_pointer) : boolean;
  {test equality of strings}
label exit;
var i : buf_pointer;    {running}
@!j : pool_pointer;     {indices}
begin
if (length(s) <> len) then      {strings of unequal length}
    begin
    str_eq_buf := false;
    return;
    end;
i := bf_ptr;
j := str_start[s];
while (j < str_start[s+1]) do
    begin
    if (str_pool[j] <> buf[i]) then
        begin
        str_eq_buf := false;
        return;
        end;
    incr(i);
    incr(j);
    end;
str_eq_buf := true;
exit:
end;


@
This subroutine compares two |str_pool| strings and returns true
|true| if and only if the strings are equal.

@<Procedures and functions for handling numbers, characters, and strings@>=
function str_eq_str (@!s1,@!s2:str_number) : boolean;
label exit;
begin
if (length(s1) <> length(s2)) then
    begin
    str_eq_str := false;
    return;
    end;
p_ptr1 := str_start[s1];
p_ptr2 := str_start[s2];
while (p_ptr1 < str_start[s1+1]) do
    begin
    if (str_pool[p_ptr1] <> str_pool[p_ptr2]) then
        begin
        str_eq_str := false;
        return;
        end;
    incr(p_ptr1);
    incr(p_ptr2);
    end;
str_eq_str:=true;
exit:
end;


@
@:BibTeX capacity exceeded}{\quad file name size@>
This procedure copies file name |file_name| into the beginning of
|name_of_file|, if it will fit.  It also sets the global variable
|name_length| to the appropriate value.

@<Procedures and functions for file-system interacting@>=
procedure start_name (@!file_name:str_number);
var p_ptr: pool_pointer;        {running index}
begin
if (length(file_name) > file_name_size) then
    begin
    print ('File=');
    print_pool_str (file_name);
    print_ln (',');
    file_nm_size_overflow;
    end;
name_ptr := 1;
p_ptr := str_start[file_name];
while (p_ptr < str_start[file_name+1]) do
    begin
    name_of_file[name_ptr] := chr (str_pool[p_ptr]);
    incr(name_ptr); incr(p_ptr);
    end;
name_length := length(file_name);
end;


@
@:BibTeX capacity exceeded}{\quad file name size@>
Yet another complaint-before-quiting.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure file_nm_size_overflow;
begin
overflow('file name size ',file_name_size);
end;


@
@:BibTeX capacity exceeded}{\quad file name size@>
This procedure copies file extension |ext| into the array
|name_of_file| starting at position |name_length+1|.  It also sets the
global variable |name_length| to the appropriate value.

@<Procedures and functions for file-system interacting@>=
procedure add_extension(@!ext:str_number);
var p_ptr: pool_pointer;        {running index}
begin
if (name_length + length(ext) > file_name_size) then
    begin
    print ('File=',name_of_file,', extension=');
    print_pool_str (ext); print_ln (',');
    file_nm_size_overflow;
    end;
name_ptr := name_length + 1;
p_ptr := str_start[ext];
while (p_ptr < str_start[ext+1]) do
    begin
    name_of_file[name_ptr] := chr (str_pool[p_ptr]);
    incr(name_ptr); incr(p_ptr);
    end;
name_length := name_length + length(ext);
name_ptr := name_length+1;
while (name_ptr <= file_name_size) do   {pad with blanks}
    begin
    name_of_file[name_ptr] := ' ';
    incr(name_ptr);
    end;
end;


@
@:BibTeX capacity exceeded}{\quad file name size@>
This procedure copies the default logical area name |area| into the
array |name_of_file| starting at position 1, after shifting up the
rest of the filename.  It also sets the global variable |name_length|
to the appropriate value.

@<Procedures and functions for file-system interacting@>=
procedure add_area(@!area:str_number);
var p_ptr: pool_pointer;        {running index}
begin
if (name_length + length(area) > file_name_size) then
    begin
    print ('File=');
    print_pool_str (area); print (name_of_file,',');
    file_nm_size_overflow;
    end;
name_ptr := name_length;
while (name_ptr > 0) do         {shift up name}
    begin
    name_of_file[name_ptr+length(area)] := name_of_file[name_ptr];
    decr(name_ptr);
    end;
name_ptr := 1;
p_ptr := str_start[area];
while (p_ptr < str_start[area+1]) do
    begin
    name_of_file[name_ptr] := chr (str_pool[p_ptr]);
    incr(name_ptr); incr(p_ptr);
    end;
name_length := name_length + length(area);
end;


@
This system-independent procedure converts upper-case characters to
lower case for the specified part of |buf|.  It is system independent
because it uses only the internal representation for characters.

@d case_difference = "a" - "A"

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure lower_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
var i:buf_pointer;
begin
if (len > 0) then
  for i := bf_ptr to bf_ptr+len-1 do
    if ((buf[i]>="A") and (buf[i]<="Z")) then
        buf[i] := buf[i] + case_difference;
end;


@
This system-independent procedure is the same as the previous except
that it converts lower- to upper-case letters.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure upper_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
var i:buf_pointer;
begin
if (len > 0) then
  for i := bf_ptr to bf_ptr+len-1 do
    if ((buf[i]>="a") and (buf[i]<="z")) then
        buf[i] := buf[i] - case_difference;
end;



@* The hash table.
All static strings that \BibTeX\ might have to search for, generally
identifiers, are stored and retrieved by means of a fairly standard
hash-table algorithm (but slightly altered here) called the method of
``coalescing lists''
(cf.\ Algorithm 6.4C in {\sl The Art of Computer Programming}).
Once a string enters the table, it is never removed.  The actual
sequence of characters forming a string is stored in the |str_pool|
array.

The hash table consists of the four arrays |hash_next|, |hash_text|,
|hash_ilk|, and |ilk_info|.  The first array, |hash_next[p]|, points
to the next identifier belonging to the same coalesced list as the
identifier corresponding to~|p|.  The second, |hash_text[p]|, points
to the |str_start| entry for |p|'s string. If position~|p| of the hash
table is empty, we have |hash_text[p]=0|; if position |p| is either
empty or the end of a coalesced hash list, we have
|hash_next[p]=empty|; an auxiliary pointer variable called |hash_used|
is maintained in such a way that all locations |p>=hash_used| are
nonempty.  The third, |hash_ilk[p]|, tells how this string is used (as
ordinary text, as a variable name, as an \.{.aux} file command, etc).
The fourth, |ilk_info[p]|, contains information specific to the
corresponding |hash_ilk|---for |integer_ilk|s: the integer's value;
for |cite_ilk|s: a pointer into |cite_list|; for |lc_cite_ilk|s: a
pointer to a |cite_ilk| string; for |command_ilk|s: a constant to be
used in a |case| statement; for |bst_fn_ilk|s: function-specific
information; for |macro_ilk|s: a pointer to its definition string; for
|control_seq_ilk|s: a constant for use in a |case| statement; for all
other |ilk|s it contains no information.  This |ilk|-specific
information is set in other parts of the program rather than here in
the hashing routine.

@d hash_base = empty + 1                {lowest numbered hash-table location}
@d hash_max = hash_base + hash_size - 1 {highest numbered hash-table location}
@d hash_is_full == (hash_used=hash_base) {test if all positions are occupied}
@#
@d text_ilk = 0         {a string of ordinary text}
@d integer_ilk = 1      {an integer (possibly with a |minus_sign|)}
@d aux_command_ilk = 2  {an \.{.aux}-file command}
@d aux_file_ilk = 3     {an \.{.aux} file name}
@d bst_command_ilk = 4  {a \.{.bst}-file command}
@d bst_file_ilk = 5     {a \.{.bst} file name}
@d bib_file_ilk = 6     {a \.{.bib} file name}
@d file_ext_ilk = 7     {one of \.{.aux}, \.{.bst}, \.{.bib}, \.{.bbl},
                                                                or \.{.blg}}
@d file_area_ilk = 8    {one of \.{texinputs:} or \.{texbib:}}
@d cite_ilk = 9         {a \.{\\citation} argument}
@d lc_cite_ilk = 10     {a \.{\\citation} argument converted to lower case}
@d bst_fn_ilk = 11      {a \.{.bst} function name}
@d bib_command_ilk = 12 {a \.{.bib}-file command}
@d macro_ilk = 13       {a \.{.bst} macro or a \.{.bib} string}
@d control_seq_ilk = 14 {a control sequence specifying a foreign character}
@d last_ilk = 14        {the same number as on the line above}

@<Types in the outer block@>=
@!hash_loc=hash_base..hash_max;         {a location within the hash table}
@!hash_pointer=empty..hash_max;         {either |empty| or a |hash_loc|}
@#
@!str_ilk=0..last_ilk;  {the legal string types}


@
@<Globals in the outer block@>=
@!hash_next : packed array[hash_loc] of hash_pointer;   {coalesced-list link}
@!hash_text : packed array[hash_loc] of str_number;     {pointer to a string}
@!hash_ilk : packed array[hash_loc] of str_ilk;         {the type of string}
@!ilk_info : packed array[hash_loc] of integer;         {|ilk|-specific info}
@!hash_used : hash_base..hash_max+1;    {allocation pointer for hash table}
@!hash_found : boolean;  {set to |true| if it's already in the hash table}
@!dummy_loc : hash_loc;  {receives |str_lookup| value whenever it's useless}


@
@<Local variables for initialization@>=
@!k:hash_loc;


@
Now it's time to initialize the hash table; note that |str_start[0]|
must be unused if |hash_text[k] := 0| is to have the desired effect.

@<Set initial values of key variables@>=
for k:=hash_base to hash_max do
    begin
    hash_next[k] := empty;
    hash_text[k] := 0;  {thus, no need to initialize |hash_ilk| or |ilk_info|}
    end;
hash_used := hash_max + 1;      {nothing in table initially}


@
Here is the subroutine that searches the hash table for a
(string,~|str_ilk|) pair, where the string is of length |l>=0| and
appears in |buffer[j..(j+l-1)]|.  If it finds the pair, it returns the
corresponding hash-table location and sets the global variable
|hash_found| to |true|.  Otherwise it sets |hash_found| to |false|,
and if the parameter |insert_it| is |true|, it inserts the pair into
the hash table, inserts the string into |str_pool| if not previously
encountered, and returns its location.  Note that two different pairs
can have the same string but different |str_ilk|s, in which case the
second pair encountered, if |insert_it| were |true|, would be inserted
into the hash table though its string wouldn't be inserted into
|str_pool| because it would already be there.

@d max_hash_value = hash_prime+hash_prime-2+127         {|h|'s maximum value}
@d do_insert == true            {insert string if not found in hash table}
@d dont_insert == false         {don't insert string}
@#
@d str_found = 40               {go here when you've found the string}
@d str_not_found = 45           {go here when you haven't}

@<Procedures and functions for handling numbers, characters, and strings@>=
function str_lookup(var buf:buf_type; @!j,@!l:buf_pointer; @!ilk:str_ilk;
                @!insert_it:boolean) : hash_loc;        {search the hash table}
label str_found,@!str_not_found;
var h:0..max_hash_value;        {hash code}
@!p:hash_loc;           {index into |hash_| arrays}
@!k:buf_pointer;        {index into |buf| array}
@!old_string:boolean;   {set to |true| if it's an already encountered string}
@!str_num:str_number;   {pointer to an already encountered string}
begin
@<Compute the hash code |h|@>;
p:=h+hash_base;         {start searching here; note that |0<=h<hash_prime|}
hash_found := false;
old_string := false;
loop
    begin
    @<Process the string if we've already encountered it@>;
    if (hash_next[p]=empty) then        {location |p| may or may not be empty}
        begin
        if (not insert_it) then goto str_not_found;
        @<Insert pair into hash table and make |p| point to it@>;
        goto str_found;
        end;
    p:=hash_next[p];            {old and new locations |p| are not empty}
    end;
str_not_found: do_nothing;      {don't insert pair; function value meaningless}
str_found: str_lookup:=p;
end;


@
@^for loops@>
@.WEB@>
The value of |hash_prime| should be roughly 85\% of |hash_size|, and
it should be a prime number
(it should also be less than $2^{14} + 2^{6} = 16320$ because of
\.{WEB}'s simple-macro bound).  The theory of hashing tells us to expect
fewer than two table probes, on the average, when the search is
successful.

@<Compute the hash code |h|@>=
begin
h := 0;         {note that this works for zero-length strings}
k := j;
while (k < j+l) do      {not a |for| loop in case |j = l = 0|}
    begin
    h:=h+h+buf[k];
    while (h >= hash_prime) do h:=h-hash_prime;
    incr(k);
    end;
end


@
Here we handle the case in which we've already encountered this
string; note that even if we have, we'll still have to insert the pair
into the hash table if |str_ilk| doesn't match.

@<Process the string if we've already encountered it@>=
begin
if (hash_text[p]>0) then                        {there's something here}
    if (str_eq_buf(hash_text[p],buf,j,l)) then  {it's the right string}
        if (hash_ilk[p] = ilk) then             {it's the right |str_ilk|}
            begin
            hash_found := true;
            goto str_found;
            end
          else
            begin                               {it's the wrong |str_ilk|}
            old_string := true;
            str_num := hash_text[p];
            end;
end


@
@^for loops@>
@:BibTeX capacity exceeded}{\quad hash size@>
This code inserts the pair in the appropriate unused location.

@<Insert pair into hash table and make |p| point to it@>=
begin
if (hash_text[p]>0) then                {location |p| isn't empty}
    begin
        repeat if (hash_is_full) then overflow('hash size ',hash_size);
        decr(hash_used);
        until (hash_text[hash_used]=0); {search for an empty location}
    hash_next[p]:=hash_used;
    p:=hash_used;
    end;                        {now location |p| is empty}
if (old_string) then            {it's an already encountered string}
    hash_text[p] := str_num
  else
    begin                       {it's a new string}
    str_room(l);                {make sure it'll fit in |str_pool|}
    k := j;
    while (k < j+l) do          {not a |for| loop in case |j = l = 0|}
        begin
        append_char(buf[k]);
        incr(k);
        end;
    hash_text[p] := make_string;                {and make it official}
    end;
hash_ilk[p] := ilk;
end


@
@^string pool@>
Now that we've defined the hash-table workings we can initialize the
string pool.  Unlike \TeX, \BibTeX\ does not use a |pool_file| for
string storage; instead it inserts its pre-defined strings into
|str_pool|---this makes one file fewer for the \BibTeX\ implementor
to deal with.  This section initializes |str_pool|; the pre-defined
strings will be inserted into it shortly; and other strings are
inserted while processing the input files.

@<Set initial values of key variables@>=
pool_ptr:=0; str_ptr:=1;        {hash table must have |str_start[0]| unused}
str_start[str_ptr]:=pool_ptr;


@
The longest pre-defined string determines type definitions used to
insert the pre-defined strings into |str_pool|.

@d longest_pds=12       {the length of `\.{change.case\$}'}

@<Types in the outer block@>=
@!pds_loc = 1..longest_pds;
@!pds_len = 0..longest_pds;
@!pds_type = packed array [pds_loc] of char;


@
The variables in this program beginning with |s_| specify the
locations in |str_pool| for certain often-used strings.  Those here
have to do with the file system; the next section will actually insert
them into |str_pool|.

@<Globals in the outer block@>=
@!s_aux_extension : str_number; {\.{.aux}}
@!s_log_extension : str_number; {\.{.blg}}
@!s_bbl_extension : str_number; {\.{.bbl}}
@!s_bst_extension : str_number; {\.{.bst}}
@!s_bib_extension : str_number; {\.{.bib}}
@!s_bst_area : str_number;      {\.{texinputs:}}
@!s_bib_area : str_number;      {\.{texbib:}}


@
@^important note@>
@^system dependencies@>
It's time to insert some of the pre-defined strings into |str_pool|
(and thus the hash table).  These system-dependent strings should
contain no upper-case letters, and they must all be exactly
|longest_pds| characters long (even if fewer characters are actually
stored).  The |pre_define| routine appears shortly.

Important notes: These pre-definitions must not have any glitches or
the program may bomb because the |log_file| hasn't been opened yet,
and |text_ilk|s should be pre-defined later, for
\.{.bst}-function-execution purposes.

@<Pre-define certain strings@>=
pre_define('.aux        ',4,file_ext_ilk);
s_aux_extension := hash_text[pre_def_loc];
pre_define('.bbl        ',4,file_ext_ilk);
s_bbl_extension := hash_text[pre_def_loc];
pre_define('.blg        ',4,file_ext_ilk);
s_log_extension := hash_text[pre_def_loc];
pre_define('.bst        ',4,file_ext_ilk);
s_bst_extension := hash_text[pre_def_loc];
pre_define('.bib        ',4,file_ext_ilk);
s_bib_extension := hash_text[pre_def_loc];
pre_define('texinputs:  ',10,file_area_ilk);
s_bst_area := hash_text[pre_def_loc];
pre_define('texbib:     ',7,file_area_ilk);
s_bib_area := hash_text[pre_def_loc];


@
This global variable gives the hash-table location of pre-defined
strings generated by calls to |str_lookup|.

@<Globals in the outer block@>=
@!pre_def_loc : hash_loc;


@
This procedure initializes a pre-defined string of length at most
|longest_pds|.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure pre_define (@!pds:pds_type; @!len:pds_len; @!ilk:str_ilk);
var i : pds_len;
begin
for i:=1 to len do
    buffer[i] := xord[pds[i]];
pre_def_loc := str_lookup(buffer,1,len,ilk,do_insert);
end;


@
These constants all begin with |n_| and are used for the |case|
statement that determines which command to execute.  The variable
|command_num| is set to one of these and is used to do the branching,
but it must have the full |integer| range because at times it can
assume an arbitrary |ilk_info| value (though it will be one of the
values here when we actually use it).

@d n_aux_bibdata = 0    {\.{\\bibdata}}
@d n_aux_bibstyle = 1   {\.{\\bibstyle}}
@d n_aux_citation = 2   {\.{\\citation}}
@d n_aux_input = 3      {\.{\\@@input}}
@#
@d n_bst_entry = 0      {\.{entry}}
@d n_bst_execute = 1    {\.{execute}}
@d n_bst_function = 2   {\.{function}}
@d n_bst_integers = 3   {\.{integers}}
@d n_bst_iterate = 4    {\.{iterate}}
@d n_bst_macro = 5      {\.{macro}}
@d n_bst_read = 6       {\.{read}}
@d n_bst_reverse = 7    {\.{reverse}}
@d n_bst_sort = 8       {\.{sort}}
@d n_bst_strings = 9    {\.{strings}}
@#
@d n_bib_comment = 0    {\.{comment}}
@d n_bib_preamble = 1   {\.{preamble}}
@d n_bib_string = 2     {\.{string}}

@<Globals in the outer block@>=
@!command_num : integer;


@
@^important note@>
Now we pre-define the command strings; they must all be exactly
|longest_pds| characters long.

Important note: These pre-definitions must not have any glitches or
the program may bomb because the |log_file| hasn't been opened yet.

@<Pre-define certain strings@>=
pre_define('\citation   ',9,aux_command_ilk);
ilk_info[pre_def_loc] := n_aux_citation;
pre_define('\bibdata    ',8,aux_command_ilk);
ilk_info[pre_def_loc] := n_aux_bibdata;
pre_define('\bibstyle   ',9,aux_command_ilk);
ilk_info[pre_def_loc] := n_aux_bibstyle;
pre_define('\@@input     ',7,aux_command_ilk);
ilk_info[pre_def_loc] := n_aux_input;
@#
pre_define('entry       ',5,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_entry;
pre_define('execute     ',7,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_execute;
pre_define('function    ',8,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_function;
pre_define('integers    ',8,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_integers;
pre_define('iterate     ',7,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_iterate;
pre_define('macro       ',5,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_macro;
pre_define('read        ',4,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_read;
pre_define('reverse     ',7,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_reverse;
pre_define('sort        ',4,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_sort;
pre_define('strings     ',7,bst_command_ilk);
ilk_info[pre_def_loc] := n_bst_strings;
@#
pre_define('comment     ',7,bib_command_ilk);
ilk_info[pre_def_loc] := n_bib_comment;
pre_define('preamble    ',8,bib_command_ilk);
ilk_info[pre_def_loc] := n_bib_preamble;
pre_define('string      ',6,bib_command_ilk);
ilk_info[pre_def_loc] := n_bib_string;



@* Scanning an input line.
This section describes the various |buffer| scanning routines.  The
two global variables |buf_ptr1| and |buf_ptr2| are used in scanning an
input line.  Between scans, |buf_ptr1| points to the first character
of the current token and |buf_ptr2| points to that of the next.  The
global variable |last|, set by the function |input_ln|, marks the end
of the current line; it equals 0 at the end of the current file.  All
the procedures and functions in this section will indicate an
end-of-line when it's the end of the file.

@d token_len == (buf_ptr2 - buf_ptr1)   {of the current token}
@d scan_char == buffer[buf_ptr2]        {the current character}

@<Globals in the outer block@>=
@!buf_ptr1:buf_pointer; {points to the first position of the current token}
@!buf_ptr2:buf_pointer; {used to find the end of the current token}


@
These macros send the current token, in |buffer[buf_ptr1]| to
|buffer[buf_ptr2-1]|, to an output file.

@d print_token == print_a_token {making this a procedure saves a little space}
@#
@d trace_pr_token == begin
                     out_token(log_file);
                     end


@
@^system dependencies@>
And here are the associated procedures.  Note: The |term_out| file is
system dependent.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure out_token (var f:alpha_file);
var i:buf_pointer;
begin
i := buf_ptr1;
while (i < buf_ptr2) do
    begin
    write(f,xchr[buffer[i]]);
    incr(i);
    end;
end;
@#
procedure print_a_token;
begin
out_token(term_out);
out_token(log_file);
end;


@
This function scans the |buffer| for the next token, starting at the
global variable |buf_ptr2| and ending just before either the single
specified stop-character or the end of the current line, whichever
comes first, respectively returning |true| or |false|; afterward,
|scan_char| is the first character following this token.

@<Procedures and functions for input scanning@>=
function scan1 (@!char1:ASCII_code) : boolean;
begin
buf_ptr1 := buf_ptr2;
                        {scan until end-of-line or the specified character}
while ((scan_char <> char1) and (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (buf_ptr2 < last) then
    scan1 := true
  else
    scan1 := false;
end;


@
This function is the same but stops at |white_space| characters as well.

@<Procedures and functions for input scanning@>=
function scan1_white (@!char1:ASCII_code) : boolean;
begin
buf_ptr1 := buf_ptr2;
        {scan until end-of-line, the specified character, or |white_space|}
while ((lex_class[scan_char] <> white_space) and (scan_char <> char1) and
                                                        (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (buf_ptr2 < last) then
    scan1_white := true
  else
    scan1_white := false;
end;


@
This function is similar to |scan1|, but stops at either of two
stop-characters as well as the end of the current line.

@<Procedures and functions for input scanning@>=
function scan2 (@!char1,@!char2:ASCII_code) : boolean;
begin
buf_ptr1 := buf_ptr2;
                        {scan until end-of-line or the specified characters}
while ((scan_char <> char1) and (scan_char <> char2) and (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (buf_ptr2 < last) then
    scan2 := true
  else
    scan2 := false;
end;


@
This function is the same but stops at |white_space| characters as well.

@<Procedures and functions for input scanning@>=
function scan2_white (@!char1,@!char2:ASCII_code) : boolean;
begin
buf_ptr1 := buf_ptr2;
        {scan until end-of-line, the specified characters, or |white_space|}
while ((scan_char <> char1) and (scan_char <> char2) and
                (lex_class[scan_char] <> white_space) and (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (buf_ptr2 < last) then
    scan2_white := true
  else
    scan2_white := false;
end;


@
This function is similar to |scan2|, but stops at either of three
stop-characters as well as the end of the current line.

@<Procedures and functions for input scanning@>=
function scan3 (@!char1,@!char2,@!char3:ASCII_code) : boolean;
begin
buf_ptr1 := buf_ptr2;
                        {scan until end-of-line or the specified characters}
while ((scan_char <> char1) and (scan_char <> char2) and
                                (scan_char <> char3) and (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (buf_ptr2 < last) then
    scan3 := true
  else
    scan3 := false;
end;


@
This function scans for letters, stopping at the first nonletter; it
returns |true| if there is at least one letter.

@<Procedures and functions for input scanning@>=
function scan_alpha : boolean;
begin
buf_ptr1 := buf_ptr2;
                                        {scan until end-of-line or a nonletter}
while ((lex_class[scan_char] = alpha) and (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (token_len = 0) then
    scan_alpha := false
  else
    scan_alpha := true;
end;


@
These are the possible values for |scan_result|; they're set by the
|scan_identifier| procedure and are described in the next section.

@d id_null = 0
@d specified_char_adjacent = 1
@d other_char_adjacent = 2
@d white_adjacent = 3

@<Globals in the outer block@>=
@!scan_result : id_null..white_adjacent;


@
This procedure scans for an identifier, stopping at the first
|illegal_id_char|, or stopping at the first character if it's
|numeric|.  It sets the global variable |scan_result| to |id_null| if
the identifier is null, else to |white_adjacent| if it ended at a
|white_space| character or an end-of-line, else to
|specified_char_adjacent| if it ended at one of |char1| or |char2| or
|char3|, else to |other_char_adjacent| if it ended at a nonspecified,
non|white_space| |illegal_id_char|.  By convention, when some calling
code really wants just one or two ``specified'' characters, it merely
repeats one of the characters.

@<Procedures and functions for input scanning@>=
procedure scan_identifier (@!char1,@!char2,@!char3:ASCII_code);
begin
buf_ptr1 := buf_ptr2;
if (lex_class[scan_char] <> numeric) then
                        {scan until end-of-line or an |illegal_id_char|}
    while ((id_class[scan_char] = legal_id_char) and (buf_ptr2 < last)) do
        incr(buf_ptr2);
if (token_len = 0) then
    scan_result := id_null
else if ((lex_class[scan_char] = white_space) or (buf_ptr2 = last)) then
    scan_result := white_adjacent
else if ((scan_char = char1) or (scan_char = char2) or (scan_char = char3))
                                                                        then
    scan_result := specified_char_adjacent
else
    scan_result := other_char_adjacent;
end;


@
The next two procedures scan for an integer, setting the global
variable |token_value| to the corresponding integer.

@d char_value == (scan_char - "0")      {the value of the digit being scanned}

@<Globals in the outer block@>=
@!token_value : integer;        {the numeric value of the current token}


@
This function scans for a nonnegative integer, stopping at the first
nondigit; it sets the value of |token_value| accordingly.  It returns
|true| if the token was a legal nonnegative integer (i.e., consisted
of one or more digits).

@<Procedures and functions for input scanning@>=
function scan_nonneg_integer : boolean;
begin
buf_ptr1 := buf_ptr2;
token_value := 0;
                                        {scan until end-of-line or a nondigit}
while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
    begin
    token_value := token_value*10 + char_value;
    incr(buf_ptr2);
    end;
if (token_len = 0) then                 {there were no digits}
    scan_nonneg_integer := false
  else
    scan_nonneg_integer := true;
end;


@
This procedure scans for an integer, stopping at the first nondigit;
it sets the value of |token_value| accordingly.  It returns |true| if
the token was a legal integer (i.e., consisted of an optional
|minus_sign| followed by one or more digits).

@d negative == (sign_length = 1)        {if this integer is negative}

@<Procedures and functions for input scanning@>=
function scan_integer : boolean;
var sign_length : 0..1;         {1 if there's a |minus_sign|, 0 if not}
begin
buf_ptr1 := buf_ptr2;
if (scan_char = minus_sign) then        {it's a negative number}
    begin
    sign_length := 1;
    incr(buf_ptr2);                     {skip over the |minus_sign|}
    end
  else
    sign_length := 0;
token_value := 0;
                                        {scan until end-of-line or a nondigit}
while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
    begin
    token_value := token_value*10 + char_value;
    incr(buf_ptr2);
    end;
if (negative) then
    token_value := -token_value;
if (token_len = sign_length) then       {there were no digits}
    scan_integer := false
  else
    scan_integer := true;
end;


@
This function scans over |white_space| characters, stopping either at
the first nonwhite character or the end of the line, respectively
returning |true| or |false|.

@<Procedures and functions for input scanning@>=
function scan_white_space : boolean;
begin
                                        {scan until end-of-line or a nonwhite}
while ((lex_class[scan_char] = white_space) and (buf_ptr2 < last)) do
    incr(buf_ptr2);
if (buf_ptr2 < last) then
    scan_white_space := true
  else
    scan_white_space := false;
end;


@
The |print_bad_input_line| procedure prints the current input line,
splitting it at the character being scanned: It prints |buffer[0]|,
|buffer[1]|, \dots, |buffer[buf_ptr2-1]| on one line and
|buffer[buf_ptr2]|, \dots, |buffer[last-1]| on the next (and both
lines start with a colon between two |space|s).  Each |white_space|
character is printed as a |space|.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_bad_input_line;
var bf_ptr : buf_pointer;
begin
print (' : ');
bf_ptr := 0;
while (bf_ptr < buf_ptr2) do
    begin
    if (lex_class[buffer[bf_ptr]] = white_space) then
        print (xchr[space])
      else
        print (xchr[buffer[bf_ptr]]);
    incr(bf_ptr);
    end;
print_newline;
print (' : ');
bf_ptr := 0;
while (bf_ptr < buf_ptr2) do
    begin
    print (xchr[space]);
    incr(bf_ptr);
    end;
bf_ptr := buf_ptr2;
while (bf_ptr < last) do
    begin
    if (lex_class[buffer[bf_ptr]] = white_space) then
        print (xchr[space])
      else
        print (xchr[buffer[bf_ptr]]);
    incr(bf_ptr);
    end;
print_newline;@/
bf_ptr := 0;
while ((bf_ptr < buf_ptr2) and (lex_class[buffer[bf_ptr]] = white_space)) do
    incr(bf_ptr);
if (bf_ptr = buf_ptr2) then
    print_ln ('(Error may have been on previous line)');
mark_error;
end;


@
This little procedure exists because it's used by at least two other
procedures and thus saves some space.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_skipping_whatever_remains;
begin
print ('I''m skipping whatever remains of this ');
end;



@* Getting the top-level auxiliary file name.
@^system dependencies@>
These modules read the name of the top-level \.{.aux} file.  Some
systems will try to find this on the command line; if it's not there
it will come from the user's terminal.  In either case, the name goes
into the |char| array |name_of_file|, and the files relevant to this
name are opened.

@d aux_found=41         {go here when the \.{.aux} name is legit}
@d aux_not_found=46     {go here when it's not}

@<Globals in the outer block@>=
@!aux_name_length : 0..file_name_size+1;        {\.{.aux} name sans extension}


@
@^system dependencies@>
@^user abuse@>
I mean, this is truly disgraceful.  A user has to type something in to
the terminal just once during the entire run.  And it's not some
complicated string where you have to get every last punctuation mark
just right, and it's not some fancy list where you get nervous because
if you forget one item you have to type the whole thing again; it's
just a simple, ordinary, file name.  Now you'd think a five-year-old
could do it; you'd think it's so simple a user should be able to do it
in his sleep.  But noooooooooo.  He had to sit there droning on and on
about who knows what until he exceeded the bounds of common sense, and
he probably didn't even realize it.  Just pitiful.  What's this world
coming to?  We should probably just delete all his files and be done
with him.  Note: The |term_out| file is system dependent.

@d sam_you_made_the_file_name_too_long == begin
                                          sam_too_long_file_name_print;
                                          goto aux_not_found;
                                          end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure sam_too_long_file_name_print;
begin
write (term_out,'File name `');
name_ptr := 1;
while (name_ptr <= aux_name_length) do
    begin
    write (term_out,name_of_file[name_ptr]);
    incr(name_ptr);
    end;
write_ln (term_out,''' is too long');
end;


@
@^system dependencies@>
@^user abuse@>
We've abused the user enough for one section; suffice it to
say here that most of what we said last module still applies.
Note: The |term_out| file is system dependent.

@d sam_you_made_the_file_name_wrong == begin
                                       sam_wrong_file_name_print;
                                       goto aux_not_found;
                                       end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure sam_wrong_file_name_print;
begin
write (term_out,'I couldn''t open file name `');
name_ptr := 1;
while (name_ptr <= name_length) do
    begin
    write (term_out,name_of_file[name_ptr]);
    incr(name_ptr);
    end;
write_ln (term_out,'''');
end;


@
@^system dependencies@>
This procedure consists of a loop that reads and processes a (nonnull)
\.{.aux} file name.  It's this module and the next two that must be
changed on those systems using command-line arguments.  Note: The
|term_out| and |term_in| files are system dependent.

@<Procedures and functions for the reading and processing of input files@>=
procedure get_the_top_level_aux_file_name;
label aux_found,@!aux_not_found;
var @<Variables for possible command-line processing@>@/
begin
check_cmnd_line := false;                       {many systems will change this}
loop
    begin
    if (check_cmnd_line) then
        @<Process a possible command line@>
      else
        begin
        write (term_out,'Please type input file name (no extension)--');
        if (eoln(term_in)) then                 {so the first |read| works}
            read_ln (term_in);
        aux_name_length := 0;
        while (not eoln(term_in)) do
            begin
            if (aux_name_length = file_name_size) then
                begin
                while (not eoln(term_in)) do    {discard the rest of the line}
                    get(term_in);
                sam_you_made_the_file_name_too_long;
                end;
            incr(aux_name_length);
            name_of_file[aux_name_length] := term_in^;
            get(term_in);
            end;
        end;
    @<Handle this \.{.aux} name@>;
aux_not_found:
    check_cmnd_line := false;
    end;
aux_found:                      {now we're ready to read the \.{.aux} file}
end;


@
@^system dependencies@>
The switch |check_cmnd_line| tells us whether we're to check for a
possible command-line argument.

@<Variables for possible command-line processing@>=
@!check_cmnd_line : boolean;    {|true| if we're to check the command line}


@
@^system dependencies@>
Here's where we do the real command-line work.  Those systems needing
more than a single module to handle the task should add the extras to
the ``System-dependent changes'' section.

@<Process a possible command line@>=
begin
do_nothing;             {the ``default system'' doesn't use the command line}
end


@
Here we orchestrate this \.{.aux} name's handling: we add the various
extensions, try to open the files with the resulting name, and
store the name strings we'll need later.

@<Handle this \.{.aux} name@>=
begin
if ((aux_name_length + length(s_aux_extension) > file_name_size) or@|
        (aux_name_length + length(s_log_extension) > file_name_size) or@|
        (aux_name_length + length(s_bbl_extension) > file_name_size)) then
    sam_you_made_the_file_name_too_long;
@<Add extensions and open files@>;
@<Put this name into the hash table@>;
goto aux_found;
end


@
Here we set up definitions and declarations for files opened in this
section.  Each element in |aux_list| (except for
|aux_list[aux_stack_size]|, which is always unused) is a pointer to
the appropriate |str_pool| string representing the \.{.aux} file name.
The array |aux_file| contains the corresponding \PASCAL\ |file|
variables.

@d cur_aux_str == aux_list[aux_ptr]  {shorthand for the current \.{.aux} file}
@d cur_aux_file == aux_file[aux_ptr]    {shorthand for the current |aux_file|}
@d cur_aux_line == aux_ln_stack[aux_ptr] {line number of current \.{.aux} file}

@<Globals in the outer block@>=
@!aux_file : array[aux_number] of alpha_file; {open \.{.aux} |file| variables}
@!aux_list : array[aux_number] of str_number;   {the open \.{.aux} file list}
@!aux_ptr : aux_number;         {points to the currently open \.{.aux} file}
@!aux_ln_stack : array[aux_number] of integer;  {open \.{.aux} line numbers}
@#
@!top_lev_str : str_number;     {the top-level \.{.aux} file's name}
@#
@!log_file : alpha_file;        {the |file| variable for the \.{.blg} file}
@!bbl_file : alpha_file;        {the |file| variable for the \.{.bbl} file}


@
Where |aux_number| is the obvious.

@<Types in the outer block@>=
@!aux_number = 0..aux_stack_size;       {gives the |aux_list| range}


@
@^system dependencies@>
We must make sure the (top-level) \.{.aux}, \.{.blg}, and \.{.bbl}
files can be opened.

@<Add extensions and open files@>=
begin
name_length := aux_name_length;         {set to last used position}
add_extension (s_aux_extension);        {this also sets |name_length|}
aux_ptr := 0;                           {initialize the \.{.aux} file stack}
if (not a_open_in(cur_aux_file)) then
    sam_you_made_the_file_name_wrong;
@#
name_length := aux_name_length;
add_extension (s_log_extension);        {this also sets |name_length|}
if (not a_open_out(log_file)) then
    sam_you_made_the_file_name_wrong;
@#
name_length := aux_name_length;
add_extension (s_bbl_extension);        {this also sets |name_length|}
if (not a_open_out(bbl_file)) then
    sam_you_made_the_file_name_wrong;
end


@
@:this can't happen}{\quad Already encountered auxiliary file@>
This code puts the \.{.aux} file name, both with and without the
extension, into the hash table, and it initializes |aux_list|.  Note
that all previous top-level \.{.aux}-file stuff must have been
successful.

@<Put this name into the hash table@>=
begin
name_length := aux_name_length;
add_extension (s_aux_extension);        {this also sets |name_length|}
name_ptr := 1;
while (name_ptr <= name_length) do
    begin
    buffer[name_ptr] := xord[name_of_file[name_ptr]];
    incr(name_ptr);
    end;
top_lev_str := hash_text[
                str_lookup(buffer,1,aux_name_length,text_ilk,do_insert)];
cur_aux_str := hash_text[
                str_lookup(buffer,1,name_length,aux_file_ilk,do_insert)];
                                {note that this has initialized |aux_list|}
if (hash_found) then
    begin
      trace
      print_aux_name;
      ecart@/
    confusion ('Already encountered auxiliary file');
    end;
cur_aux_line := 0;   {this finishes initializing the top-level \.{.aux} file}
end


@
Print the name of the current \.{.aux} file, followed by a |newline|.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_aux_name;
begin
print_pool_str (cur_aux_str);
print_newline;
end;



@* Reading the auxiliary file(s).
@^auxiliary-file commands@>
Now it's time to read the \.{.aux} file.  The only commands we handle
are \.{\\citation} (there can be arbitrarily many, each having
arbitrarily many arguments), \.{\\bibdata} (there can be just one, but
it can have arbitrarily many arguments), \.{\\bibstyle} (there can be
just one, and it can have just one argument), and \.{\\@@input} (there
can be arbitrarily many, each with one argument, and they can be
nested to a depth of |aux_stack_size|).  Each of these commands is
assumed to be on just a single line.  The rest of the \.{.aux} file is
ignored.

@d aux_done=31          {go here when finished with the \.{.aux} files}

@<Labels in the outer block@>=
,@!aux_done


@
We keep reading and processing input lines until none left.  This is
part of the main program; hence, because of the |aux_done| label,
there's no conventional |begin|-|end| pair surrounding the entire
module.

@<Read the \.{.aux} file@>=
print ('The top-level auxiliary file: ');
print_aux_name;
loop
    begin                       {|pop_the_aux_stack| will exit the loop}
    incr(cur_aux_line);
    if (not input_ln(cur_aux_file)) then        {end of current \.{.aux} file}
        pop_the_aux_stack
      else
        get_aux_command_and_process;
    end;
  trace
  trace_pr_ln ('Finished reading the auxiliary file(s)');
  ecart@/
aux_done:
last_check_for_aux_errors;


@
When we find a bug, we print a message and flush the rest of the line.
This macro must be called from within a procedure that has an |exit|
label.

@d aux_err_return == begin
                     aux_err_print;
                     return;            {flush this input line}
                     end

@d aux_err(#) == begin
                 print (#);
                 aux_err_return;
                 end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure aux_err_print;
begin
print ('---line ',cur_aux_line:0,' of file ');
print_aux_name;@/
print_bad_input_line;                   {this call does the |mark_error|}
print_skipping_whatever_remains;
print_ln ('command')
end;


@
@:this can't happen}{\quad Illegal auxiliary-file command@>
Here are a bunch of macros whose print statements are used at least
twice.  Thus we save space by making the statements procedures.  This
macro complains when there's a repeated command that's to be used just
once.

@d aux_err_illegal_another(#) == begin
                                 aux_err_illegal_another_print (#);
                                 aux_err_return;
                                 end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure aux_err_illegal_another_print (@!cmd_num : integer);
begin
print ('Illegal, another \bib');
case (cmd_num) of
    n_aux_bibdata : print ('data');
    n_aux_bibstyle : print ('style');
    othercases
        confusion ('Illegal auxiliary-file command')
endcases;
print (' command');
end;


@
This one complains when a command is missing its |right_brace|.

@d aux_err_no_right_brace == begin
                             aux_err_no_right_brace_print;
                             aux_err_return;
                             end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure aux_err_no_right_brace_print;
begin
print ('No "',xchr[right_brace],'"');
end;


@
This one complains when a command has stuff after its |right_brace|.

@d aux_err_stuff_after_right_brace == begin
                                      aux_err_stuff_after_right_brace_print;
                                      aux_err_return;
                                      end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure aux_err_stuff_after_right_brace_print;
begin
print ('Stuff after "',xchr[right_brace],'"');
end;


@
And this one complains when a command has |white_space| in its
argument.

@d aux_err_white_space_in_argument == begin
                                      aux_err_white_space_in_argument_print;
                                      aux_err_return;
                                      end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure aux_err_white_space_in_argument_print;
begin
print ('White space in argument');
end;


@
@^auxiliary-file commands@>
@:this can't happen}{\quad Unknown auxiliary-file command@>
We're not at the end of an \.{.aux} file, so we see if the current
line might be a command of interest.  A command of interest will be a
line without blanks, consisting of a command name, a |left_brace|, one
or more arguments separated by commas, and a |right_brace|.

@<Scan for and process an \.{.aux} command@>=
procedure get_aux_command_and_process;
label exit;
begin
buf_ptr2 := 0;                          {mark the beginning of the next token}
if (not scan1(left_brace)) then         {no |left_brace|---flush line}
    return;
command_num := ilk_info[
        str_lookup(buffer,buf_ptr1,token_len,aux_command_ilk,dont_insert)];
if (hash_found) then
    case (command_num) of
        n_aux_bibdata : aux_bib_data_command;
        n_aux_bibstyle : aux_bib_style_command;
        n_aux_citation : aux_citation_command;
        n_aux_input : aux_input_command;
        othercases
            confusion ('Unknown auxiliary-file command')
    endcases;
exit:
end;


@
Here we introduce some variables for processing a \.{\\bibdata}
command.  Each element in |bib_list| (except for
|bib_list[max_bib_files]|, which is always unused) is a pointer to the
appropriate |str_pool| string representing the \.{.bib} file name.
The array |bib_file| contains the corresponding \PASCAL\ |file|
variables.

@d cur_bib_str == bib_list[bib_ptr]     {shorthand for current \.{.bib} file}
@d cur_bib_file == bib_file[bib_ptr]    {shorthand for current |bib_file|}

@<Globals in the outer block@>=
@!bib_list : array[bib_number] of str_number;   {the \.{.bib} file list}
@!bib_ptr : bib_number;         {pointer for the current \.{.bib} file}
@!num_bib_files : bib_number;   {the total number of \.{.bib} files}
@!bib_seen : boolean;   {|true| if we've already seen a \.{\\bibdata} command}
@!bib_file : array[bib_number] of alpha_file; {corresponding |file| variables}


@
Where |bib_number| is the obvious.

@<Types in the outer block@>=
@!bib_number = 0..max_bib_files;        {gives the |bib_list| range}


@
@<Set initial values of key variables@>=
bib_ptr := 0;           {this makes |bib_list| empty}
bib_seen := false;      {we haven't seen a \.{\\bibdata} command yet}


@
@:auxiliary-file commands}{\quad \.{\\bibdata}@>
A \.{\\bibdata} command will have its arguments between braces and
separated by commas.  There must be exactly one such command in the
\.{.aux} file(s).  All upper-case letters are converted to lower case.

@<Procedures and functions for the reading and processing of input files@>=
procedure aux_bib_data_command;
label exit;
begin
if (bib_seen) then
    aux_err_illegal_another (n_aux_bibdata);
bib_seen := true;       {now we've seen a \.{\\bibdata} command}
while (scan_char <> right_brace) do
    begin
    incr(buf_ptr2);                     {skip over the previous stop-character}
    if (not scan2_white(right_brace,comma)) then
        aux_err_no_right_brace;
    if (lex_class[scan_char] = white_space) then
        aux_err_white_space_in_argument;
    if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
        aux_err_stuff_after_right_brace;
    @<Open a \.{.bib} file@>;
    end;
exit:
end;


@
Here's a procedure we'll need shortly.  It prints the name of the
current \.{.bib} file, followed by a |newline|.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_bib_name;
begin
print_pool_str (cur_bib_str);
print_pool_str (s_bib_extension);
print_newline;
end;


@
This macro is similar to |aux_err| but it complains specifically about
opening a file for a \.{\\bibdata} command.

@d open_bibdata_aux_err(#) == begin
                              print (#);
                              print_bib_name;
                              aux_err_return;   {this does the |mark_error|}
                              end


@
@:BibTeX capacity exceeded}{\quad number of \.{.bib} files@>
Now we add the just-found argument to |bib_list| if it hasn't already
been encountered as a \.{\\bibdata} argument and if, after appending
the |s_bib_extension| string, the resulting file name can be opened.

@<Open a \.{.bib} file@>=
begin
if (bib_ptr = max_bib_files) then
    overflow('number of database files ',max_bib_files);
cur_bib_str := hash_text[
                str_lookup(buffer,buf_ptr1,token_len,bib_file_ilk,do_insert)];
if (hash_found) then    {already encountered this as a \.{\\bibdata} argument}
    open_bibdata_aux_err ('This database file appears more than once: ');
start_name (cur_bib_str);
add_extension (s_bib_extension);
if (not a_open_in(cur_bib_file)) then
    begin
    add_area (s_bib_area);
    if (not a_open_in(cur_bib_file)) then
        open_bibdata_aux_err ('I couldn''t open database file ');
    end;
  trace
  trace_pr_pool_str (cur_bib_str);
  trace_pr_pool_str (s_bib_extension);
  trace_pr_ln (' is a bibdata file');
  ecart@/
incr(bib_ptr);
end


@
Here we introduce some variables for processing a \.{\\bibstyle}
command.

@<Globals in the outer block@>=
@!bst_seen : boolean;   {|true| if we've already seen a \.{\\bibstyle} command}
@!bst_str : str_number;         {the string number for the \.{.bst} file}
@!bst_file : alpha_file;        {the corresponding |file| variable}


@
And we initialize.

@<Set initial values of key variables@>=
bst_str := 0;           {mark |bst_str| as unused}
bst_seen := false;      {we haven't seen a \.{\\bibstyle} command yet}


@
@:auxiliary-file commands}{\quad \.{\\bibstyle}@>
A \.{\\bibstyle} command will have exactly one argument, and it will
be between braces.  There must be exactly one such command in the
\.{.aux} file(s).  All upper-case letters are converted to lower case.

@<Procedures and functions for the reading and processing of input files@>=
procedure aux_bib_style_command;
label exit;
begin
if (bst_seen) then
    aux_err_illegal_another (n_aux_bibstyle);
bst_seen := true;               {now we've seen a \.{\\bibstyle} command}
incr(buf_ptr2);                 {skip over the |left_brace|}
if (not scan1_white(right_brace)) then
    aux_err_no_right_brace;
if (lex_class[scan_char] = white_space) then
    aux_err_white_space_in_argument;
if (last > buf_ptr2+1) then
    aux_err_stuff_after_right_brace;
@<Open the \.{.bst} file@>;
exit:
end;


@
@:this can't happen}{\quad Already encountered style file@>
Now we open the file whose name is the just-found argument appended
with the |s_bst_extension| string, if possible.

@<Open the \.{.bst} file@>=
begin
bst_str := hash_text[
                str_lookup(buffer,buf_ptr1,token_len,bst_file_ilk,do_insert)];
if (hash_found) then
    begin
      trace
      print_bst_name;
      ecart@/
    confusion ('Already encountered style file');
    end;
start_name (bst_str);
add_extension (s_bst_extension);
if (not a_open_in(bst_file)) then
    begin
    add_area (s_bst_area);
    if (not a_open_in(bst_file)) then
        begin
        print ('I couldn''t open style file ');
        print_bst_name;@/
        bst_str := 0;                           {mark as unused again}
        aux_err_return;
        end;
    end;
print ('The style file: ');
print_bst_name;
end


@
Print the name of the \.{.bst} file, followed by a |newline|.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_bst_name;
begin
print_pool_str (bst_str);
print_pool_str (s_bst_extension);
print_newline;
end;


@
Here we introduce some variables for processing a \.{\\citation}
command.  Each element in |cite_list| (except for
|cite_list[max_cites]|, which is always unused) is a pointer to the
appropriate |str_pool| string.  The cite-key list is kept in order of
occurrence with duplicates removed.

@d cur_cite_str == cite_list[cite_ptr]  {shorthand for the current cite key}

@<Globals in the outer block@>=
@!cite_list : packed array[cite_number] of str_number;  {the cite-key list}
@!cite_ptr : cite_number;       {pointer for the current cite key}
@!entry_cite_ptr : cite_number; {cite pointer for the current entry}
@!num_cites : cite_number;      {the total number of distinct cite keys}
@!old_num_cites : cite_number;  {set to a previous |num_cites| value}
@!citation_seen : boolean;      {|true| if we've seen a \.{\\citation} command}
@!cite_loc : hash_loc;          {the hash-table location of a cite key}
@!lc_cite_loc : hash_loc;       {and of its lower-case equivalent}
@!lc_xcite_loc : hash_loc;      {a second |lc_cite_loc| variable}
@!cite_found : boolean;         {|true| if we've already seen this cite key}
@!all_entries : boolean;        {|true| if we're to use the entire database}
@!all_marker : cite_number;     {we put the other entries in |cite_list| here}


@
Where |cite_number| is the obvious.

@<Types in the outer block@>=
@!cite_number = 0..max_cites;   {gives the |cite_list| range}


@
@<Set initial values of key variables@>=
cite_ptr := 0;          {this makes |cite_list| empty}
citation_seen := false; {we haven't seen a \.{\\citation} command yet}
all_entries := false;   {by default, use just the entries explicitly named}


@
@^case mismatch@>
@^entire database inclusion@>
@^whole database inclusion@>
@:LaTeX}{\LaTeX@>
@:auxiliary-file commands}{\quad \.{\\citation}@>
A \.{\\citation} command will have its arguments between braces and
separated by commas.  Upper/lower cases are considered to be different
for \.{\\citation} arguments, which is the same as the rest of \LaTeX\
but different from the rest of \BibTeX.  A cite key needn't exactly
case-match its corresponding database key to work, although two cite
keys that are case-mismatched will produce an error message.
(A {\sl case mismatch\/} is a mismatch, but only because of a case
difference.)

A \.{\\citation} command having \.{*} as an argument indicates that
the entire database will be included (almost as if a \.{\\nocite}
command that listed every cite key in the database, in order, had been
given at the corresponding spot in the \.{.tex} file).

@d next_cite = 23       {read the next argument}

@<Procedures and functions for the reading and processing of input files@>=
procedure aux_citation_command;
label next_cite,@!exit;
begin
citation_seen := true;          {now we've seen a \.{\\citation} command}
while (scan_char <> right_brace) do
    begin
    incr(buf_ptr2);             {skip over the previous stop-character}
    if (not scan2_white(right_brace,comma)) then
        aux_err_no_right_brace;
    if (lex_class[scan_char] = white_space) then
        aux_err_white_space_in_argument;
    if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
        aux_err_stuff_after_right_brace;
    @<Check the cite key@>;
next_cite:
    end;
exit:
end;


@
@^kludge@>
We must check if (the lower-case version of) this cite key has been
previously encountered, and proceed accordingly.  The alias kludge
helps make the stack space not overflow on some machines.

@d ex_buf1== ex_buf             {an alias, used only in this module}

@<Check the cite key@>=
begin
  trace
  trace_pr_token;
  trace_pr (' cite key encountered');
  ecart@/
@<Check for entire database inclusion (and thus skip this cite key)@>;
tmp_ptr := buf_ptr1;
while (tmp_ptr < buf_ptr2) do
    begin
    ex_buf1[tmp_ptr] := buffer[tmp_ptr];
    incr(tmp_ptr);
    end;
lower_case (ex_buf1, buf_ptr1, token_len);      {convert to `canonical' form}
lc_cite_loc := str_lookup(ex_buf1,buf_ptr1,token_len,lc_cite_ilk,do_insert);
if (hash_found) then    {already encountered this as a \.{\\citation} argument}
    @<Cite seen, don't add a cite key@>
  else
    @<Cite unseen, add a cite key@>;
                                {it's a new cite key---add it to |cite_list|}
end


@
Here we check for a \.{\\citation} command having \.{*} as an
argument, indicating that the entire database will be included.

@<Check for entire database inclusion (and thus skip this cite key)@>=
begin
if (token_len = 1) then
  if (buffer[buf_ptr1] = star) then
    begin
      trace
      trace_pr_ln ('---entire database to be included');
      ecart@/
    if (all_entries) then
        begin
        print_ln ('Multiple inclusions of entire database');
        aux_err_return;
        end
      else
        begin
        all_entries := true;
        all_marker := cite_ptr;
        goto next_cite;
        end;
    end;
end


@
@^case mismatch errors@>
We've previously encountered the lower-case version, so we check that
the actual version exactly matches the actual version of the
previously-encountered cite key(s).

@<Cite seen, don't add a cite key@>=
begin
  trace
  trace_pr_ln (' previously');
  ecart@/
dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
if (not hash_found) then                {case mismatch error}
    begin
    print ('Case mismatch error between cite keys ');
    print_token;
    print (' and ');
    print_pool_str (cite_list[ilk_info[ilk_info[lc_cite_loc]]]);
    print_newline;
    aux_err_return;
    end;
end


@
@:this can't happen}{\quad Cite hash error@>
Now we add the just-found argument to |cite_list| if there isn't
anything funny happening.

@<Cite unseen, add a cite key@>=
begin
  trace
  trace_pr_newline;
  ecart@/
cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
if (hash_found) then
    hash_cite_confusion;
check_cite_overflow (cite_ptr);
cur_cite_str := hash_text[cite_loc];
ilk_info[cite_loc] := cite_ptr;
ilk_info[lc_cite_loc] := cite_loc;
incr(cite_ptr);
end


@
@:this can't happen}{\quad Cite hash error@>
Here's a serious complaint (that is, a bug) concerning hash problems.
This is the first of several similar bug-procedures that exist only
because they save space.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure hash_cite_confusion;
begin
confusion ('Cite hash error');
end;


@
@^fetish@>
@:BibTeX capacity exceeded}{\quad number of cite keys@>
Complain if somebody's got a cite fetish.  This procedure is called
when were about to add another cite key to |cite_list|.  It assumes
that |cite_loc| gives the potential cite key's hash table location.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure check_cite_overflow (@!last_cite : cite_number);
begin
if (last_cite = max_cites) then
    begin
    print_pool_str (hash_text[cite_loc]);
    print_ln (' is the key:');
    overflow('number of cite keys ',max_cites);
    end;
end;


@
@:auxiliary-file commands}{\quad \.{\\\AT!input}@>
An \.{\\@@input} command will have exactly one argument, it will
be between braces, and it must have the |s_aux_extension|.
All upper-case letters are converted to lower case.

@<Procedures and functions for the reading and processing of input files@>=
procedure aux_input_command;
label exit;
var aux_extension_ok : boolean;         {to check for a correct file extension}
begin
incr(buf_ptr2);                         {skip over the |left_brace|}
if (not scan1_white(right_brace)) then
    aux_err_no_right_brace;
if (lex_class[scan_char] = white_space) then
    aux_err_white_space_in_argument;
if (last > buf_ptr2+1) then
    aux_err_stuff_after_right_brace;
@<Push the \.{.aux} stack@>;
exit:
end;


@
@:BibTeX capacity exceeded}{\quad number of \.{.aux} files@>
We must check that this potential \.{.aux} file won't overflow the
stack, that it has the correct extension, that we haven't encountered
it before (to prevent, among other things, an infinite loop).

@<Push the \.{.aux} stack@>=
begin
incr(aux_ptr);
if (aux_ptr = aux_stack_size) then
    begin
    print_token; print (': ');
    overflow('auxiliary file depth ',aux_stack_size);
    end;
aux_extension_ok := true;
if (token_len < length(s_aux_extension)) then@/
    aux_extension_ok := false   {else |str_eq_buf| might bomb the program}
else if (not str_eq_buf(s_aux_extension, buffer,
        buf_ptr2-length(s_aux_extension), length(s_aux_extension))) then
    aux_extension_ok := false;
if (not aux_extension_ok) then
    begin
    print_token;
    print (' has a wrong extension');
    decr(aux_ptr);
    aux_err_return;
    end;
cur_aux_str := hash_text[
                str_lookup(buffer,buf_ptr1,token_len,aux_file_ilk,do_insert)];
if (hash_found) then
    begin
    print ('Already encountered file ');
    print_aux_name;
    decr(aux_ptr);
    aux_err_return;
    end;
@<Open this \.{.aux} file@>;
end


@
We check that this \.{.aux} file can actually be opened, and then open it.

@<Open this \.{.aux} file@>=
begin
start_name (cur_aux_str);       {extension already there for \.{.aux} files}
name_ptr := name_length+1;
while (name_ptr <= file_name_size) do   {pad with blanks}
    begin
    name_of_file[name_ptr] := ' ';
    incr(name_ptr);
    end;
if (not a_open_in(cur_aux_file)) then
    begin
    print ('I couldn''t open auxiliary file ');
    print_aux_name;
    decr(aux_ptr);
    aux_err_return;
    end;
print ('A level-',aux_ptr:0,' auxiliary file: ');
print_aux_name;
cur_aux_line := 0;
end


@
Here we close the current-level \.{.aux} file and go back up a level,
if possible, by decrementing |aux_ptr|.

@<Procedures and functions for the reading and processing of input files@>=
procedure pop_the_aux_stack;
begin
a_close (cur_aux_file);
if (aux_ptr=0) then
    goto aux_done
  else
    decr(aux_ptr);
end;


@
@^gymnastics@>
That's it for processing \.{.aux} commands, except for finishing the
procedural gymnastics.

@<Procedures and functions for the reading and processing of input files@>=
@<Scan for and process an \.{.aux} command@>


@
We must complain if anything's amiss.

@d aux_end_err(#) == begin
                     aux_end1_err_print;
                     print (#);
                     aux_end2_err_print;
                     end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure aux_end1_err_print;
begin
print ('I found no ');
end;
@#
procedure aux_end2_err_print;
begin
print ('---while reading file ');
print_aux_name;
mark_error;
end;


@
Before proceeding, we see if we have any complaints.

@<Procedures and functions for the reading and processing of input files@>=
procedure last_check_for_aux_errors;
begin
num_cites := cite_ptr;          {record the number of distinct cite keys}
num_bib_files := bib_ptr;       {and the number of \.{.bib} files}
if (not citation_seen) then
    aux_end_err ('\citation commands')
  else if ((num_cites = 0) and (not all_entries)) then
    aux_end_err ('cite keys');
if (not bib_seen) then
    aux_end_err ('\bibdata command')
  else if (num_bib_files = 0) then
    aux_end_err ('database files');
if (not bst_seen) then
    aux_end_err ('\bibstyle command')
  else if (bst_str = 0) then
    aux_end_err ('style file');
end;



@* Reading the style file.
This part of the program reads the \.{.bst} file, which consists of a
sequence of commands.  Each \.{.bst} command consists of a name (for
which case differences are ignored) followed by zero or more
arguments, each enclosed in braces.

@d bst_done=32          {go here when finished with the \.{.bst} file}
@d no_bst_file=9932     {go here when skipping the \.{.bst} file}

@<Labels in the outer block@>=
,@!bst_done,@!no_bst_file


@
The |bbl_line_num| gets initialized along with the |bst_line_num|, so
it's declared here too.

@<Globals in the outer block@>=
@!bbl_line_num : integer;       {line number of the \.{.bbl} (output) file}
@!bst_line_num : integer;       {line number of the \.{.bst} file}


@
This little procedure exists because it's used by at least two other
procedures and thus saves some space.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_ln_num_print;
begin
print ('--line ',bst_line_num:0,' of file ');
print_bst_name;
end;


@
When there's a serious error parsing the \.{.bst} file, we flush the
rest of the current command; a blank line is assumed to mark the end
of a command (but for the purposes of error recovery only).  Thus,
error recovery will be better if style designers leave blank lines
between \.{.bst} commands.  This macro must be called from within a
procedure that has an |exit| label.

@d bst_err_print_and_look_for_blank_line_return ==
                begin
                bst_err_print_and_look_for_blank_line;
                return;
                end

@d bst_err(#) == begin          {serious error during \.{.bst} parsing}
                 print (#);
                 bst_err_print_and_look_for_blank_line_return;
                 end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_err_print_and_look_for_blank_line;
begin
print ('-');
bst_ln_num_print;
print_bad_input_line;                   {this call does the |mark_error|}
while (last <> 0) do                    {look for a blank input line}
    if (not input_ln(bst_file)) then    {or the end of the file}
        goto bst_done
      else
        incr(bst_line_num);
buf_ptr2 := last;                       {to input the next line}
end;


@
When there's a harmless error parsing the \.{.bst} file (harmless
syntactically, at least) we give just a |warning_message|.

@d bst_warn(#) == begin         {non-serious error during \.{.bst} parsing}
                  print (#);
                  bst_warn_print;
                  end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_warn_print;
begin
bst_ln_num_print;
mark_warning;
end;


@
Here's the outer loop for reading the \.{.bst} file---it keeps reading
and processing \.{.bst} commands until none left.  This is part of the
main program; hence, because of the |bst_done| label, there's no
conventional |begin|-|end| pair surrounding the entire module.

@<Read and execute the \.{.bst} file@>=
if (bst_str = 0) then   {there's no \.{.bst} file to read}
    goto no_bst_file;   {this is a |goto| so that |bst_done| is not in a block}
bst_line_num := 0;      {initialize things}
bbl_line_num := 1;      {best spot to initialize the output line number}
buf_ptr2 := last;       {to get the first input line}
loop
    begin
    if (not eat_bst_white_space) then   {the end of the \.{.bst} file}
        goto bst_done;
    get_bst_command_and_process;
    end;
bst_done: a_close (bst_file);
no_bst_file: a_close (bbl_file);


@
This \.{.bst}-specific scanning function skips over |white_space|
characters (and comments) until hitting a nonwhite character or the
end of the file, respectively returning |true| or |false|.  It also
updates |bst_line_num|, the line counter.

@<Procedures and functions for input scanning@>=
function eat_bst_white_space : boolean;
label exit;
begin
loop
    begin
    if (scan_white_space) then          {hit a nonwhite character on this line}
        if (scan_char <> comment) then  {it's not a comment character; return}
            begin
            eat_bst_white_space := true;
            return;
            end;
    if (not input_ln(bst_file)) then    {end-of-file; return |false|}
        begin
        eat_bst_white_space := false;
        return;
        end;
    incr(bst_line_num);
    buf_ptr2 := 0;
    end;
exit:
end;


@
It's often illegal to end a \.{.bst} command in certain places, and
this is where we come to check.

@d eat_bst_white_and_eof_check(#) ==
        begin
        if (not eat_bst_white_space) then
            begin
            eat_bst_print;
            bst_err (#);
            end;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure eat_bst_print;
begin
print ('Illegal end of style file in command: ');
end;


@
We must attend to a few details before getting to work on this
\.{.bst} command.

@<Scan for and process a \.{.bst} command@>=
procedure get_bst_command_and_process;
label exit;
begin
if (not scan_alpha) then
    bst_err ('"',xchr[scan_char],'" can''t start a style-file command');
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
command_num := ilk_info[
        str_lookup(buffer,buf_ptr1,token_len,bst_command_ilk,dont_insert)];
if (not hash_found) then
    begin
    print_token;
    bst_err (' is an illegal style-file command');
    end;
@<Process the appropriate \.{.bst} command@>;
exit:
end;


@
@^style-file commands@>
@:this can't happen}{\quad Unknown style-file command@>
Here we determine which \.{.bst} command we're about to process, and
then go to it.

@<Process the appropriate \.{.bst} command@>=
case (command_num) of
    n_bst_entry : bst_entry_command;
    n_bst_execute : bst_execute_command;
    n_bst_function : bst_function_command;
    n_bst_integers : bst_integers_command;
    n_bst_iterate : bst_iterate_command;
    n_bst_macro : bst_macro_command;
    n_bst_read : bst_read_command;
    n_bst_reverse : bst_reverse_command;
    n_bst_sort : bst_sort_command;
    n_bst_strings : bst_strings_command;
    othercases confusion ('Unknown style-file command')
endcases


@
We need data structures for the function definitions, the entry
variables, the global variables, and the actual entries corresponding
to the cite-key list.  First we define the classes of `function's
used.  Functions in all classes are of |bst_fn_ilk| except for
|int_literal|s, which are of |integer_ilk|; and |str_literal|s, which
are of |text_ilk|.

@d built_in = 0         {the `primitive' functions}
@d wiz_defined = 1      {defined in the \.{.bst} file}
@d int_literal = 2      {integer `constants'}
@d str_literal = 3      {string `constants'}
@d field = 4            {things like `author' and `title'}
@d int_entry_var = 5    {integer entry variable}
@d str_entry_var = 6    {string entry variable}
@d int_global_var = 7   {integer global variable}
@d str_global_var = 8   {string global variable}
@d last_fn_class = 8    {the same number as on the line above}


@
@:this can't happen}{\quad Unknown function class@>
Here's another bug report.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure unknwn_function_class_confusion;
begin
confusion ('Unknown function class');
end;


@
@:this can't happen}{\quad Unknown function class@>
Occasionally we'll want to |print| the name of one of these function
classes.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_fn_class (@!fn_loc : hash_loc);
begin
case (fn_type[fn_loc]) of
    built_in : print ('built-in');
    wiz_defined : print ('wizard-defined');
    int_literal : print ('integer-literal');
    str_literal : print ('string-literal');
    field : print ('field');
    int_entry_var : print ('integer-entry-variable');
    str_entry_var : print ('string-entry-variable');
    int_global_var : print ('integer-global-variable');
    str_global_var : print ('string-global-variable');
    othercases unknwn_function_class_confusion
endcases;
end;


@
@:this can't happen}{\quad Unknown function class@>
This version is for printing when in |trace| mode.

@<Procedures and functions for all file I/O, error messages, and such@>=
  trace
  procedure trace_pr_fn_class (@!fn_loc : hash_loc);
  begin
  case (fn_type[fn_loc]) of
    built_in : trace_pr ('built-in');
    wiz_defined : trace_pr ('wizard-defined');
    int_literal : trace_pr ('integer-literal');
    str_literal : trace_pr ('string-literal');
    field : trace_pr ('field');
    int_entry_var : trace_pr ('integer-entry-variable');
    str_entry_var : trace_pr ('string-entry-variable');
    int_global_var : trace_pr ('integer-global-variable');
    str_global_var : trace_pr ('string-global-variable');
    othercases unknwn_function_class_confusion
  endcases;
  end;
  ecart


@
Besides the function classes, we have types based on \BibTeX's
capacity limitations and one based on what can go into the array
|wiz_functions| explained below.

@d quote_next_fn = hash_base - 1  {special marker used in defining functions}
@d end_of_def = hash_max + 1      {another such special marker}

@<Types in the outer block@>=
@!fn_class = 0..last_fn_class;          {the \.{.bst} function classes}
@!wiz_fn_loc = 0..wiz_fn_space;  {|wiz_defined|-function storage locations}
@!int_ent_loc = 0..max_ent_ints;        {|int_entry_var| storage locations}
@!str_ent_loc = 0..max_ent_strs;        {|str_entry_var| storage locations}
@!str_glob_loc = 0..max_glb_str_minus_1; {|str_global_var| storage locations}
@!field_loc = 0..max_fields;            {individual field storage locations}
@!hash_ptr2 = quote_next_fn..end_of_def; {a special marker or a |hash_loc|}


@
@^save space@>
@^space savings@>
@^system dependencies@>
We store information about the \.{.bst} functions in arrays the same
size as the hash-table arrays and in locations corresponding to their
hash-table locations.  The two arrays |fn_info| (an alias of
|ilk_info| described earlier) and |fn_type| accomplish this: |fn_type|
specifies one of the above classes, and |fn_info| gives information
dependent on the class.

Six other arrays give the contents of functions: The array
|wiz_functions| holds definitions for |wiz_defined| functions---each
such function consists of a sequence of pointers to hash-table
locations of other functions (with the two special-marker exceptions
above); the array |entry_ints| contains the current values of
|int_entry_var|s; the array |entry_strs| contains the current values
of |str_entry_var|s; an element of the array |global_strs| contains
the current value of a |str_global_var| if the corresponding
|glb_str_ptr| entry is empty, otherwise the nonempty entry is a
pointer to the string; and the array |field_info|, for each field of
each entry, contains either a pointer to the string or the special
value |missing|.

The array |global_strs| isn't packed (that is, it isn't |array| \dots\
|of packed array| \dots$\,$) to increase speed on some systems;
however, on systems that are byte-addressable and that have a good
compiler, packing |global_strs| would save lots of space without much
loss of speed.

@d fn_info == ilk_info          {an alias used with functions}
@#
@d missing = empty              {a special pointer for missing fields}

@<Globals in the outer block@>=
@!fn_loc : hash_loc;            {the hash-table location of a function}
@!wiz_loc : hash_loc;           {the hash-table location of a wizard function}
@!literal_loc : hash_loc;       {the hash-table location of a literal function}
@!macro_name_loc : hash_loc;    {the hash-table location of a macro name}
@!macro_def_loc : hash_loc;     {the hash-table location of a macro definition}
@!fn_type : packed array[hash_loc] of fn_class;
@!wiz_def_ptr : wiz_fn_loc;     {storage location for the next wizard function}
@!wiz_fn_ptr : wiz_fn_loc;      {general |wiz_functions| location}
@!wiz_functions : packed array[wiz_fn_loc] of hash_ptr2;
@!int_ent_ptr : int_ent_loc;    {general |int_entry_var| location}
@!entry_ints : array[int_ent_loc] of integer;
@!num_ent_ints : int_ent_loc;   {the number of distinct |int_entry_var| names}
@!str_ent_ptr : str_ent_loc;    {general |str_entry_var| location}
@!entry_strs : array[str_ent_loc] of
                                packed array[0..ent_str_size] of ASCII_code;
@!num_ent_strs : str_ent_loc;   {the number of distinct |str_entry_var| names}
@!str_glb_ptr : 0..max_glob_strs;       {general |str_global_var| location}
@!glb_str_ptr : array[str_glob_loc] of str_number;
@!global_strs : array[str_glob_loc] of array[0..glob_str_size] of ASCII_code;
@!glb_str_end : array[str_glob_loc] of 0..glob_str_size;        {end markers}
@!num_glb_strs : 0..max_glob_strs; {number of distinct |str_global_var| names}
@!field_ptr : field_loc;        {general |field_info| location}
@!field_parent_ptr,@!field_end_ptr : field_loc; {two more for doing cross-refs}
@!cite_parent_ptr,@!cite_xptr : cite_number;  {two others for doing cross-refs}
@!field_info : packed array[field_loc] of str_number;
@!num_fields : field_loc;       {the number of distinct field names}
@!num_pre_defined_fields : field_loc;   {so far, just one: \.{crossref}}
@!crossref_num : field_loc;     {the number given to \.{crossref}}
@!no_fields : boolean;          {used for |tr_print|ing entry information}


@
Now we initialize storage for the |wiz_defined| functions and we
initialize variables so that the first |str_entry_var|,
|int_entry_var|, |str_global_var|, and |field| name will be assigned
the number~0.  Note: The variables |num_ent_strs| and |num_fields|
will also be set when pre-defining strings.

@<Set initial values of key variables@>=
wiz_def_ptr := 0;
num_ent_ints := 0;
num_ent_strs := 0;
num_fields := 0;
str_glb_ptr := 0;
while (str_glb_ptr < max_glob_strs) do          {make |str_global_var|s empty}
    begin
    glb_str_ptr[str_glb_ptr] := 0;
    glb_str_end[str_glb_ptr] := 0;
    incr(str_glb_ptr);
    end;
num_glb_strs := 0;



@* Style-file commands.
@^style-file commands@>
There are ten \.{.bst} commands: Five (\.{entry}, \.{function},
\.{integers}, \.{macro}, and \.{strings}) declare and define
functions, one (\.{read}) reads in the \.{.bib}-file entries, and four
(\.{execute}, \.{iterate}, \.{reverse}, and \.{sort})
manipulate the entries and produce output.

The boolean variables |entry_seen| and |read_seen| indicate whether
we've yet encountered an \.{entry} and a \.{read} command.  There must
be exactly one of each of these, and the \.{entry} command, as well as
any \.{macro} command, must precede the \.{read} command.
Furthermore, the \.{read} command must precede the four that
manipulate the entries and produce output.

@<Globals in the outer block@>=
@!entry_seen : boolean; {|true| if we've already seen an \.{entry} command}
@!read_seen : boolean;  {|true| if we've already seen a \.{read} command}
@!read_performed : boolean; {|true| if we started reading the database file(s)}
@!reading_completed : boolean; {|true| if we made it all the way through}
@!read_completed : boolean; {|true| if the database info didn't bomb \BibTeX}


@
And we initialize them.

@<Set initial values of key variables@>=
entry_seen := false;
read_seen := false;
read_performed := false;
reading_completed := false;
read_completed := false;


@
@:this can't happen}{\quad Identifier scanning error@>
Here's another bug.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure id_scanning_confusion;
begin
confusion ('Identifier scanning error');
end;


@
@:this can't happen}{\quad Identifier scanning error@>
This macro is used to scan all \.{.bst} identifiers.  The argument
supplies the \.{.bst} command name.  The associated procedure simply
prints an error message.

@d bst_identifier_scan(#) ==
        begin
        scan_identifier (right_brace,comment,comment);
        if ((scan_result = white_adjacent) or
                                (scan_result = specified_char_adjacent)) then
            do_nothing
        else
            begin
            bst_id_print;
            bst_err (#);
            end;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_id_print;
begin
if (scan_result = id_null) then
    print ('"',xchr[scan_char],'" begins identifier, command: ')
else if (scan_result = other_char_adjacent) then
    print ('"',xchr[scan_char],'" immediately follows identifier, command: ')
else
    id_scanning_confusion;
end;


@
This macro just makes sure we're at a |left_brace|.

@d bst_get_and_check_left_brace(#) ==
        begin
        if (scan_char <> left_brace) then
            begin
            bst_left_brace_print;
            bst_err (#);
            end;
        incr(buf_ptr2);                 {skip over the |left_brace|}
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_left_brace_print;
begin
print ('"',xchr[left_brace],'" is missing in command: ');
end;


@
And this one, a |right_brace|.

@d bst_get_and_check_right_brace(#) ==
        begin
        if (scan_char <> right_brace) then
            begin
            bst_right_brace_print;
            bst_err (#);
            end;
        incr(buf_ptr2);                 {skip over the |right_brace|}
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_right_brace_print;
begin
print ('"',xchr[right_brace],'" is missing in command: ');
end;


@
This macro complains if we've already encountered a function to be
inserted into the hash table.

@d check_for_already_seen_function(#) ==
        begin
        if (hash_found) then  {already encountered this as a \.{.bst} function}
            begin
            already_seen_function_print (#);
            return;
            end;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure already_seen_function_print (@!seen_fn_loc : hash_loc);
label exit;     {so the call to |bst_err| works}
begin
print_pool_str (hash_text[seen_fn_loc]);
print (' is already a type "');
print_fn_class (seen_fn_loc);
print_ln ('" function name');
bst_err_print_and_look_for_blank_line_return;
exit:
end;


@
@:style-file commands}{\quad \.{entry}@>
An \.{entry} command has three arguments, each a (possibly empty) list
of function names between braces (the names are separated by one or
more |white_space| characters).  All function names in this and other
commands must be legal \.{.bst} identifiers.  Upper/lower cases are
considered to be the same for function names in these lists---all
upper-case letters are converted to lower case.  These arguments give
lists of |field|s, |int_entry_var|s, and |str_entry_var|s.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_entry_command;
label exit;
begin
if (entry_seen) then
    bst_err ('Illegal, another entry command');
entry_seen := true;             {now we've seen an \.{entry} command}
eat_bst_white_and_eof_check ('entry');
@<Scan the list of |field|s@>;
eat_bst_white_and_eof_check ('entry');
if (num_fields = num_pre_defined_fields) then
    bst_warn ('Warning--I didn''t find any fields');
@<Scan the list of |int_entry_var|s@>;
eat_bst_white_and_eof_check ('entry');
@<Scan the list of |str_entry_var|s@>;
exit:
end;


@
This module reads a |left_brace|, the list of |field|s, and a
|right_brace|.  The |field|s are those like `author' and `title.'

@<Scan the list of |field|s@>=
begin
bst_get_and_check_left_brace ('entry');
eat_bst_white_and_eof_check ('entry');
while (scan_char <> right_brace) do
    begin
    bst_identifier_scan ('entry');
    @<Insert a |field| into the hash table@>;
    eat_bst_white_and_eof_check ('entry');
    end;
incr(buf_ptr2);                 {skip over the |right_brace|}
end


@
@^secret agent man@>
Here we insert the just found field name into the hash table, record
it as a |field|, and assign it a number to be used in indexing into
the |field_info| array.

@<Insert a |field| into the hash table@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a field');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
check_for_already_seen_function (fn_loc);
fn_type[fn_loc] := field;@/
fn_info[fn_loc] := num_fields;  {give this field a number (take away its name)}
incr(num_fields);
end


@
This module reads a |left_brace|, the list of |int_entry_var|s,
and a |right_brace|.

@<Scan the list of |int_entry_var|s@>=
begin
bst_get_and_check_left_brace ('entry');
eat_bst_white_and_eof_check ('entry');
while (scan_char <> right_brace) do
    begin
    bst_identifier_scan ('entry');
    @<Insert an |int_entry_var| into the hash table@>;
    eat_bst_white_and_eof_check ('entry');
    end;
incr(buf_ptr2);                 {skip over the |right_brace|}
end


@
Here we insert the just found |int_entry_var| name into the hash table
and record it as an |int_entry_var|.  An |int_entry_var| is one that
the style designer wants a separate copy of for each entry.

@<Insert an |int_entry_var| into the hash table@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is an integer entry-variable');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
check_for_already_seen_function (fn_loc);
fn_type[fn_loc] := int_entry_var;@/
fn_info[fn_loc] := num_ent_ints;        {give this |int_entry_var| a number}
incr(num_ent_ints);
end


@
This module reads a |left_brace|, the list of |str_entry_var|s, and a
|right_brace|.  A |str_entry_var| is one that the style designer wants
a separate copy of for each entry.

@<Scan the list of |str_entry_var|s@>=
begin
bst_get_and_check_left_brace ('entry');
eat_bst_white_and_eof_check ('entry');
while (scan_char <> right_brace) do
    begin
    bst_identifier_scan ('entry');
    @<Insert a |str_entry_var| into the hash table@>;
    eat_bst_white_and_eof_check ('entry');
    end;
incr(buf_ptr2);                 {skip over the |right_brace|}
end


@
Here we insert the just found |str_entry_var| name into the hash
table, record it as a |str_entry_var|, and set its pointer into
|entry_strs|.

@<Insert a |str_entry_var| into the hash table@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a string entry-variable');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
check_for_already_seen_function (fn_loc);
fn_type[fn_loc] := str_entry_var;@/
fn_info[fn_loc] := num_ent_strs;        {give this |str_entry_var| a number}
incr(num_ent_strs);
end


@
A legal argument for an \.{execute}, \.{iterate}, or \.{reverse}
command must exist and be |built_in| or |wiz_defined|.
Here's where we check, returning |true| if the argument is illegal.

@<Procedures and functions for the reading and processing of input files@>=
function bad_argument_token : boolean;
label exit;
begin
bad_argument_token := true;     {now it's easy to exit if necessary}
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
if (not hash_found) then                        {unknown \.{.bst} function}
    begin
    print_token;
    bst_err (' is an unknown function');
    end
else if ((fn_type[fn_loc] <> built_in) and
         (fn_type[fn_loc] <> wiz_defined)) then
    begin
    print_token;
    print (' has bad function type ');
    print_fn_class (fn_loc);
    bst_err_print_and_look_for_blank_line_return;
    end;
bad_argument_token := false;
exit:
end;


@
@:style-file commands}{\quad \.{execute}@>
An \.{execute} command has one argument, a single |built_in| or
|wiz_defined| function name between braces.  Upper/lower cases are
considered to be the same---all upper-case letters are converted to
lower case.  Also, we must make sure we've already seen a \.{read}
command.

This module reads a |left_brace|, a single function to be executed,
and a |right_brace|.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_execute_command;
label exit;
begin
if (not read_seen) then
    bst_err ('Illegal, execute command before read command');
eat_bst_white_and_eof_check ('execute');
bst_get_and_check_left_brace ('execute');
eat_bst_white_and_eof_check ('execute');
bst_identifier_scan ('execute');
@<Check the \.{execute}-command argument token@>;
eat_bst_white_and_eof_check ('execute');
bst_get_and_check_right_brace ('execute');
@<Perform an \.{execute} command@>;
exit:
end;


@
Before executing the function, we must make sure it's a legal one.  It
must exist and be |built_in| or |wiz_defined|.

@<Check the \.{execute}-command argument token@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a to be executed function');
  ecart@/
if (bad_argument_token) then
    return;
end


@
@:style-file commands}{\quad \.{function}@>
A \.{function} command has two arguments; the first is a
|wiz_defined| function name between braces.  Upper/lower cases are
considered to be the same---all upper-case letters are converted to
lower case.  The second argument defines this function.  It consists
of a sequence of functions, between braces, separated by |white_space|
characters.  Upper/lower cases are considered to be the same for
function names but not for |str_literal|s.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_function_command;
label exit;
begin
eat_bst_white_and_eof_check ('function');
@<Scan the |wiz_defined| function name@>;
eat_bst_white_and_eof_check ('function');
bst_get_and_check_left_brace ('function');
scan_fn_def(wiz_loc);           {this scans the function definition}
exit:
end;


@
This module reads a |left_brace|, a |wiz_defined| function name, and
a |right_brace|.

@<Scan the |wiz_defined| function name@>=
begin
bst_get_and_check_left_brace ('function');
eat_bst_white_and_eof_check ('function');
bst_identifier_scan ('function');
@<Check the |wiz_defined| function name@>;
eat_bst_white_and_eof_check ('function');
bst_get_and_check_right_brace ('function');
end


@
The function name must exist and be a new one; we mark it as
|wiz_defined|.  Also, see if it's the default entry-type function.

@<Check the |wiz_defined| function name@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a wizard-defined function');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
wiz_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
check_for_already_seen_function (wiz_loc);
fn_type[wiz_loc] := wiz_defined;
if (hash_text[wiz_loc] = s_default) then  {we've found the default entry-type}
    b_default := wiz_loc;       {see the |built_in| functions for |b_default|}
end


@
We're about to start scanning tokens in a function definition.  When a
function token is illegal, we skip until it ends; a |white_space|
character, an end-of-line, a |right_brace|, or a |comment| marks the
end of the current token.

@d next_token=25                {a bad function token; go read the next one}
@#
@d skip_token(#) == begin       {not-so-serious error during \.{.bst} parsing}
                    print (#);
                    skip_token_print;   {also, skip to the current token's end}
                    goto next_token;
                    end

@<Procedures and functions for input scanning@>=
procedure skip_token_print;
begin
print ('-');
bst_ln_num_print;
mark_error;
if (scan2_white(right_brace,comment)) then              {ok if token ends line}
    do_nothing;
end;


@
@^commented-out code@>
@^for a good time, try comment-out code@>
This macro is similar to the last one but is specifically for
recursion in a |wiz_defined| function, which is illegal; it helps save
space.

@d skip_recursive_token == begin
                           print_recursion_illegal;
                           goto next_token;
                           end

@<Procedures and functions for input scanning@>=
procedure print_recursion_illegal;
begin
  trace
  trace_pr_newline;
  ecart@/
print_ln ('Curse you, wizard, before you recurse me:');
print ('function ');
print_token;
print_ln (' is illegal in its own definition');
  @{
  print_recursion_illegal;
  @}@/
skip_token_print;                       {also, skip to the current token's end}
end;


@
Here's another macro for saving some space when there's a problem with
a token.

@d skip_token_unknown_function == begin
                                  skp_token_unknown_function_print;
                                  goto next_token;
                                  end

@<Procedures and functions for input scanning@>=
procedure skp_token_unknown_function_print;
begin
print_token;
print (' is an unknown function');
skip_token_print;                       {also, skip to the current token's end}
end;


@
And another.

@d skip_token_illegal_stuff_after_literal ==
                        begin
                        skip_illegal_stuff_after_token_print;
                        goto next_token;
                        end

@<Procedures and functions for input scanning@>=
procedure skip_illegal_stuff_after_token_print;
begin
print ('"',xchr[scan_char],'" can''t follow a literal');
skip_token_print;                       {also, skip to the current token's end}
end;


@
This recursive function reads and stores the list of functions
(separated by |white_space| characters or ends-of-line) that define
this new function, and reads a |right_brace|.

@<Procedures and functions for input scanning@>=
procedure scan_fn_def (@!fn_hash_loc : hash_loc);
label next_token,@!exit;
type @!fn_def_loc = 0..single_fn_space; {for a single |wiz_defined|-function}
var singl_function : packed array[fn_def_loc] of hash_ptr2;
    @!single_ptr : fn_def_loc;  {next storage location for this definition}
    @!copy_ptr : fn_def_loc;    {dummy variable}
    @!end_of_num : buf_pointer; {the end of an implicit function's name}
    @!impl_fn_loc : hash_loc;   {an implicit function's hash-table location}
begin
eat_bst_white_and_eof_check ('function');
single_ptr := 0;
while (scan_char <> right_brace) do
    begin
    @<Get the next function of the definition@>;
next_token:
    eat_bst_white_and_eof_check ('function');
    end;
@<Complete this function's definition@>;
incr(buf_ptr2);                 {skip over the |right_brace|}
exit:
end;


@
@:BibTeX capacity exceeded}{\quad single function space@>
This macro inserts a hash-table location (or one of the two
special markers |quote_next_fn| and |end_of_def|) into the
|singl_function| array, which will later be copied into the
|wiz_functions| array.

@d insert_fn_loc(#) ==  begin
                        singl_function[single_ptr] := #;
                        if (single_ptr = single_fn_space) then
                            singl_fn_overflow;
                        incr(single_ptr);
                        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure singl_fn_overflow;
begin
overflow('single function space ',single_fn_space);
end;


@
There are five possibilities for the first character of the token
representing the next function of the definition: If it's a
|number_sign|, the token is an |int_literal|; if it's a
|double_quote|, the token is a |str_literal|; if it's a
|single_quote|, the token is a quoted function; if it's a
|left_brace|, the token isn't really a token, but rather the start of
another function definition (which will result in a recursive call to
|scan_fn_def|); if it's anything else, the token is the name of an
already-defined function.  Note: To prevent the wizard from using
recursion, we have to check that neither a quoted function nor an
already-defined-function is actually the currently-being-defined
function (which is stored at |wiz_loc|).

@<Get the next function of the definition@>=
case (scan_char) of
    number_sign : @<Scan an |int_literal|@>;
    double_quote : @<Scan a |str_literal|@>;
    single_quote : @<Scan a quoted function@>;
    left_brace : @<Start a new function definition@>;
    othercases @<Scan an already-defined function@>
endcases


@
An |int_literal| is preceded by a |number_sign|, consists of an
integer (i.e., an optional |minus_sign| followed by one or more
|numeric| characters), and is followed either by a |white_space|
character, an end-of-line, or a |right_brace|.  The array |fn_info|
contains the value of the integer for |int_literal|s.

@<Scan an |int_literal|@>=
begin
incr(buf_ptr2);                         {skip over the |number_sign|}
if (not scan_integer) then
    skip_token ('Illegal integer in integer literal');
  trace
  trace_pr ('#');
  trace_pr_token;
  trace_pr_ln (' is an integer literal with value ',token_value:0);
  ecart@/
literal_loc := str_lookup(buffer,buf_ptr1,token_len,integer_ilk,do_insert);
if (not hash_found) then
    begin
    fn_type[literal_loc] := int_literal;        {set the |fn_class|}
    fn_info[literal_loc] := token_value;        {the value of this integer}
    end;
if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
            (scan_char<>right_brace) and@| (scan_char<>comment)) then
    skip_token_illegal_stuff_after_literal;
insert_fn_loc (literal_loc);    {add this function to |wiz_functions|}
end


@
A |str_literal| is preceded by a |double_quote| and consists of all
characters on this line up to the next |double_quote|.  Also, there
must be either a |white_space| character, an end-of-line, a
|right_brace|, or a |comment| following (since functions in the
definition must be separated by |white_space|).  The array |fn_info|
contains nothing for |str_literal|s.

@<Scan a |str_literal|@>=
begin
incr(buf_ptr2);                         {skip over the |double_quote|}
if (not scan1(double_quote)) then
    skip_token ('No `',xchr[double_quote],''' to end string literal');
  trace
  trace_pr ('"');
  trace_pr_token;
  trace_pr ('"');
  trace_pr_ln (' is a string literal');
  ecart@/
literal_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
fn_type[literal_loc] := str_literal;    {set the |fn_class|}
incr(buf_ptr2);                         {skip over the |double_quote|}
if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
        (scan_char<>right_brace) and@| (scan_char<>comment)) then
    skip_token_illegal_stuff_after_literal;
insert_fn_loc (literal_loc);            {add this function to |wiz_functions|}
end


@
A quoted function is preceded by a |single_quote| and consists of all
characters up to the next |white_space| character, end-of-line,
|right_brace|, or |comment|.

@<Scan a quoted function@>=
begin
incr(buf_ptr2);                                 {skip over the |single_quote|}
if (scan2_white(right_brace,comment)) then              {ok if token ends line}
    do_nothing;
  trace
  trace_pr ('''');
  trace_pr_token;
  trace_pr (' is a quoted function ');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
if (not hash_found) then                        {unknown \.{.bst} function}
    skip_token_unknown_function
else
    @<Check and insert the quoted function@>;
end


@
Here we check that this quoted function is a legal one---the function
name must already exist, but it mustn't be the currently-being-defined
function (which is stored at |wiz_loc|).

@<Check and insert the quoted function@>=
begin
if (fn_loc = wiz_loc) then
    skip_recursive_token
else
    begin
      trace
      trace_pr ('of type ');
      trace_pr_fn_class (fn_loc);
      trace_pr_newline;
      ecart@/
    insert_fn_loc (quote_next_fn);      {add special marker together with}
    insert_fn_loc (fn_loc);             {this function to |wiz_functions|}
    end
end


@
@^kludge@>
@:this can't happen}{\quad Already encountered implicit function@>
This module marks the implicit function as being quoted, generates a
name, and stores it in the hash table.  This name is strictly internal
to this program, starts with a |single_quote| (since that will make
this function name unique), and ends with the variable |impl_fn_num|
converted to ASCII.  The alias kludge helps make the stack space not
overflow on some machines.

@d ex_buf2 == ex_buf            {an alias, used only in this module}

@<Start a new function definition@>=
begin
ex_buf2[0] := single_quote;
int_to_ASCII (impl_fn_num,ex_buf2,1,end_of_num);
impl_fn_loc := str_lookup(ex_buf2,0,end_of_num,bst_fn_ilk,do_insert);
if (hash_found) then
    confusion ('Already encountered implicit function');
  trace
  trace_pr_pool_str (hash_text[impl_fn_loc]);
  trace_pr_ln (' is an implicit function');
  ecart@/
incr(impl_fn_num);
fn_type[impl_fn_loc] := wiz_defined;@/
insert_fn_loc (quote_next_fn);  {all implicit functions are quoted}
insert_fn_loc (impl_fn_loc);    {add it to |wiz_functions|}
incr(buf_ptr2);                 {skip over the |left_brace|}
scan_fn_def (impl_fn_loc);      {this is the recursive call}
end


@
The variable |impl_fn_num| counts the number of implicit functions
seen in the \.{.bst} file.

@<Globals in the outer block@>=
@!impl_fn_num : integer;        {the number of implicit functions seen so far}


@
Now we initialize it.

@<Set initial values of key variables@>=
impl_fn_num := 0;


@
@:BibTeX capacity exceeded}{\quad buffer size@>
This module appends a character to |int_buf| after checking to make
sure it will fit; for use in |int_to_ASCII|.

@d append_int_char(#) == begin
                         if (int_ptr = buf_size) then
                             buffer_overflow;
                         int_buf[int_ptr]:=#;
                         incr(int_ptr);
                         end


@
This procedure takes the integer |int|, copies the appropriate
|ASCII_code| string into |int_buf| starting at |int_begin|, and sets
the |var| parameter |int_end| to the first unused |int_buf| location.
The ASCII string will consist of decimal digits, the first of which
will be not be a~0 if the integer is nonzero, with a prepended minus
sign if the integer is negative.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure int_to_ASCII (@!int:integer; var int_buf:buf_type;
                        @!int_begin:buf_pointer; var int_end:buf_pointer);
var int_ptr,@!int_xptr : buf_pointer;   {pointers into |int_buf|}
  @!int_tmp_val : ASCII_code;           {the temporary element in an exchange}
begin
int_ptr := int_begin;
if (int < 0) then       {add the |minus_sign| and use the absolute value}
    begin
    append_int_char (minus_sign);
    int := -int;
    end;
int_xptr := int_ptr;
repeat                          {copy digits into |int_buf|}
    append_int_char ("0" + (int mod 10));
    int := int div 10;
  until (int = 0);
int_end := int_ptr;             {set the string length}
decr(int_ptr);
while (int_xptr < int_ptr) do   {and reorder (flip) the digits}
    begin
    int_tmp_val := int_buf[int_xptr];
    int_buf[int_xptr] := int_buf[int_ptr];
    int_buf[int_ptr] := int_tmp_val;
    decr(int_ptr);
    incr(int_xptr);
    end
end;


@
An already-defined function consists of all characters up to the next
|white_space| character, end-of-line, |right_brace|, or |comment|.
This function name must already exist, but it mustn't be the
currently-being-defined function (which is stored at |wiz_loc|).

@<Scan an already-defined function@>=
begin
if (scan2_white(right_brace,comment)) then              {ok if token ends line}
    do_nothing;
  trace
  trace_pr_token;
  trace_pr (' is a function ');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
if (not hash_found) then                        {unknown \.{.bst} function}
    skip_token_unknown_function
else if (fn_loc = wiz_loc) then
    skip_recursive_token
else
    begin
      trace
      trace_pr ('of type ');
      trace_pr_fn_class (fn_loc);
      trace_pr_newline;
      ecart@/
    insert_fn_loc (fn_loc);     {add this function to |wiz_functions|}
    end;
end


@
@:BibTeX capacity exceeded}{\quad wizard-defined function space@>
Now we add the |end_of_def| special marker, make sure this function will
fit into |wiz_functions|, and put it there.

@<Complete this function's definition@>=
begin
insert_fn_loc (end_of_def);  {add special marker ending the definition}
if (single_ptr + wiz_def_ptr > wiz_fn_space) then
    begin
    print (single_ptr + wiz_def_ptr : 0,': ');
    overflow('wizard-defined function space ',wiz_fn_space);
    end;
fn_info[fn_hash_loc] := wiz_def_ptr;            {pointer into |wiz_functions|}
copy_ptr := 0;
while (copy_ptr < single_ptr) do                {make this function official}
    begin
    wiz_functions[wiz_def_ptr] := singl_function[copy_ptr];
    incr(copy_ptr);
    incr(wiz_def_ptr);
    end;
end


@
@:style-file commands}{\quad \.{integers}@>
An \.{integers} command has one argument, a list of function names
between braces (the names are separated by one or more |white_space|
characters).  Upper/lower cases are considered to be the same for
function names in these lists---all upper-case letters are converted to
lower case.  Each name in this list specifies an |int_global_var|.
There may be several \.{integers} commands in the \.{.bst} file.

This module reads a |left_brace|, a list of |int_global_var|s, and a
|right_brace|.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_integers_command;
label exit;
begin
eat_bst_white_and_eof_check ('integers');
bst_get_and_check_left_brace ('integers');
eat_bst_white_and_eof_check ('integers');
while (scan_char <> right_brace) do
    begin
    bst_identifier_scan ('integers');
    @<Insert an |int_global_var| into the hash table@>;
    eat_bst_white_and_eof_check ('integers');
    end;
incr(buf_ptr2);                 {skip over the |right_brace|}
exit:
end;


@
Here we insert the just found |int_global_var| name into the hash
table and record it as an |int_global_var|.  Also, we initialize it by
setting |fn_info[fn_loc]| to 0.

@<Insert an |int_global_var| into the hash table@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is an integer global-variable');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
check_for_already_seen_function (fn_loc);
fn_type[fn_loc] := int_global_var;@/
fn_info[fn_loc] := 0;                           {initialize}
end


@
@:style-file commands}{\quad \.{iterate}@>
An \.{iterate} command has one argument, a single |built_in| or
|wiz_defined| function name between braces.  Upper/lower cases are
considered to be the same---all upper-case letters are converted to
lower case.  Also, we must make sure we've already seen a \.{read}
command.

This module reads a |left_brace|, a single function to be iterated,
and a |right_brace|.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_iterate_command;
label exit;
begin
if (not read_seen) then
    bst_err ('Illegal, iterate command before read command');
eat_bst_white_and_eof_check ('iterate');
bst_get_and_check_left_brace ('iterate');
eat_bst_white_and_eof_check ('iterate');
bst_identifier_scan ('iterate');
@<Check the \.{iterate}-command argument token@>;
eat_bst_white_and_eof_check ('iterate');
bst_get_and_check_right_brace ('iterate');
@<Perform an \.{iterate} command@>;
exit:
end;


@
Before iterating the function, we must make sure it's a legal one.  It
must exist and be |built_in| or |wiz_defined|.

@<Check the \.{iterate}-command argument token@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a to be iterated function');
  ecart@/
if (bad_argument_token) then
    return;
end


@
@:style-file commands}{\quad \.{macro}@>
A \.{macro} command, like a \.{function} command, has two arguments;
the first is a macro name between braces.  The name must be a legal
\.{.bst} identifier.  Upper/lower cases are considered to be the
same---all upper-case letters are converted to lower case.  The second
argument defines this macro.  It consists of a
|double_quote|-delimited string (which must be on a single line)
between braces, with optional |white_space| characters between the
braces and the |double_quote|s.  This |double_quote|-delimited string
is parsed exactly as a |str_literal| is for the \.{function} command.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_macro_command;
label exit;
begin
if (read_seen) then
    bst_err ('Illegal, macro command after read command');
eat_bst_white_and_eof_check ('macro');
@<Scan the macro name@>;
eat_bst_white_and_eof_check ('macro');
@<Scan the macro's definition@>;
exit:
end;


@
This module reads a |left_brace|, a macro name, and a |right_brace|.

@<Scan the macro name@>=
begin
bst_get_and_check_left_brace ('macro');
eat_bst_white_and_eof_check ('macro');
bst_identifier_scan ('macro');
@<Check the macro name@>;
eat_bst_white_and_eof_check ('macro');
bst_get_and_check_right_brace ('macro');
end


@
The macro name must be a new one; we mark it as |macro_ilk|.

@<Check the macro name@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a macro');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
macro_name_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
if (hash_found) then
    begin
    print_token;
    bst_err (' is already defined as a macro');
    end;
ilk_info[macro_name_loc]:=hash_text[macro_name_loc]; {default in case of error}
end


@
This module reads a |left_brace|, the |double_quote|-delimited string
that defines this macro, and a |right_brace|.

@<Scan the macro's definition@>=
begin
bst_get_and_check_left_brace ('macro');
eat_bst_white_and_eof_check ('macro');
if (scan_char <> double_quote) then
    bst_err ('A macro definition must be ',xchr[double_quote],'-delimited');
@<Scan the macro definition-string@>;
eat_bst_white_and_eof_check ('macro');
bst_get_and_check_right_brace ('macro');
end


@
A macro definition-string is preceded by a |double_quote| and consists
of all characters on this line up to the next |double_quote|.  The
array |ilk_info| contains a pointer to this string for the macro name.

@<Scan the macro definition-string@>=
begin
incr(buf_ptr2);                         {skip over the |double_quote|}
if (not scan1(double_quote)) then
    bst_err ('There''s no `',xchr[double_quote],''' to end macro definition');
  trace
  trace_pr ('"');
  trace_pr_token;
  trace_pr ('"');
  trace_pr_ln (' is a macro string');
  ecart@/
macro_def_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
fn_type[macro_def_loc] := str_literal;  {set the |fn_class|}
ilk_info[macro_name_loc] := hash_text[macro_def_loc];
incr(buf_ptr2);                         {skip over the |double_quote|}
end


@
@^gymnastics@>
We need to include stuff for \.{.bib} reading here because that's done
by the \.{read} command.

@<Procedures and functions for the reading and processing of input files@>=
@<Scan for and process a \.{.bib} command or database entry@>


@
@:style-file commands}{\quad \.{read}@>
The \.{read} command has no arguments so there's no more parsing to
do.  We must make sure we haven't seen a \.{read} command before and
we've already seen an \.{entry} command.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_read_command;
label exit;
begin
if (read_seen) then
    bst_err ('Illegal, another read command');
read_seen := true;              {now we've seen a \.{read} command}
if (not entry_seen) then
    bst_err ('Illegal, read command before entry command');
sv_ptr1 := buf_ptr2;            {save the contents of the \.{.bst} input line}
sv_ptr2 := last;
tmp_ptr := sv_ptr1;
while (tmp_ptr < sv_ptr2) do
    begin
    sv_buffer[tmp_ptr] := buffer[tmp_ptr];
    incr(tmp_ptr);
    end;
@<Read the \.{.bib} file(s)@>;
buf_ptr2 := sv_ptr1;            {and restore}
last := sv_ptr2;
tmp_ptr := buf_ptr2;
while (tmp_ptr < last) do
    begin
    buffer[tmp_ptr] := sv_buffer[tmp_ptr];
    incr(tmp_ptr);
    end;
exit:
end;


@
@:style-file commands}{\quad \.{reverse}@>
A \.{reverse} command has one argument, a single |built_in| or
|wiz_defined| function name between braces.  Upper/lower cases are
considered to be the same---all upper-case letters are converted to
lower case.  Also, we must make sure we've already seen a \.{read}
command.

This module reads a |left_brace|, a single function to be iterated in
reverse, and a |right_brace|.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_reverse_command;
label exit;
begin
if (not read_seen) then
    bst_err ('Illegal, reverse command before read command');
eat_bst_white_and_eof_check ('reverse');
bst_get_and_check_left_brace ('reverse');
eat_bst_white_and_eof_check ('reverse');
bst_identifier_scan ('reverse');
@<Check the \.{reverse}-command argument token@>;
eat_bst_white_and_eof_check ('reverse');
bst_get_and_check_right_brace ('reverse');
@<Perform a \.{reverse} command@>;
exit:
end;


@
Before iterating the function in reverse, we must make sure it's a
legal one.  It must exist and be |built_in| or |wiz_defined|.

@<Check the \.{reverse}-command argument token@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a to be iterated in reverse function');
  ecart@/
if (bad_argument_token) then
    return;
end


@
@:style-file commands}{\quad \.{sort}@>
The \.{sort} command has no arguments so there's no more parsing to
do, but we must make sure we've already seen a \.{read} command.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_sort_command;
label exit;
begin
if (not read_seen) then
    bst_err ('Illegal, sort command before read command');
@<Perform a \.{sort} command@>;
exit:
end;


@
@:style-file commands}{\quad \.{strings}@>
A \.{strings} command has one argument, a list of function names
between braces (the names are separated by one or more |white_space|
characters).  Upper/lower cases are considered to be the same for
function names in these lists---all upper-case letters are converted to
lower case.  Each name in this list specifies a |str_global_var|.
There may be several \.{strings} commands in the \.{.bst} file.

This module reads a |left_brace|, a list of |str_global_var|s,
and a |right_brace|.

@<Procedures and functions for the reading and processing of input files@>=
procedure bst_strings_command;
label exit;
begin
eat_bst_white_and_eof_check ('strings');
bst_get_and_check_left_brace ('strings');
eat_bst_white_and_eof_check ('strings');
while (scan_char <> right_brace) do
    begin
    bst_identifier_scan ('strings');
    @<Insert a |str_global_var| into the hash table@>;
    eat_bst_white_and_eof_check ('strings');
    end;
incr(buf_ptr2);                 {skip over the |right_brace|}
exit:
end;


@
@:BibTeX capacity exceeded}{\quad number of string global-variables@>
Here we insert the just found |str_global_var| name into the hash
table, record it as a |str_global_var|, set its pointer into
|global_strs|, and initialize its value there to the null string.

@d end_of_string = invalid_code  {this illegal |ASCII_code| ends a string}

@<Insert a |str_global_var| into the hash table@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a string global-variable');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
check_for_already_seen_function (fn_loc);
fn_type[fn_loc] := str_global_var;@/
fn_info[fn_loc] := num_glb_strs;                {pointer into |global_strs|}
if (num_glb_strs = max_glob_strs) then
    overflow('number of string global-variables ',max_glob_strs);
incr(num_glb_strs);
end


@
@^gymnastics@>
That's it for processing \.{.bst} commands, except for finishing the
procedural gymnastics.  Note that this must topologically follow the
stuff for \.{.bib} reading, because that's done by the \.{.bst}'s
\.{read} command.

@<Procedures and functions for the reading and processing of input files@>=
@<Scan for and process a \.{.bst} command@>



@* Reading the database file(s).
This section reads the \.{.bib} file(s), each of which consists of a
sequence of entries (perhaps with a few \.{.bib} commands thrown in,
as explained later).  Each entry consists of an |at_sign|, an entry
type, and, between braces or parentheses and separated by |comma|s, a
database key and a list of fields.  Each field consists of a field
name, an |equals_sign|, and nonempty list of field tokens separated by
|concat_char|s.  Each field token is either a nonnegative number, a
macro name (like `jan'), or a brace-balanced string delimited by
either |double_quote|s or braces.  Finally, case differences are
ignored for all but delimited strings and database keys, and
|white_space| characters and ends-of-line may appear in all reasonable
places (i.e., anywhere except within entry types, database keys, field
names, and macro names); furthermore, comments may appear anywhere
between entries (or before the first or after the last) as long as
they contain no |at_sign|s.


@
These global variables are used while reading the \.{.bib} file(s).
The elements of |type_list|, which indicate an entry's type (book,
article, etc.), point either to a |hash_loc| or are one of two special
markers: |empty|, from which |hash_base = empty + 1| was defined,
means we haven't yet encountered the \.{.bib} entry corresponding to
this cite key; and |undefined| means we've encountered it but it had
an unknown entry type.  Thus the array |type_list| is of type
|hash_ptr2|, also defined earlier.  An element of the boolean array
|entry_exists| whose corresponding entry in |cite_list| gets
overwritten (which happens only when |all_entries| is |true|)
indicates whether we've encountered that entry of |cite_list| while
reading the \.{.bib} file(s); this information is unused for entries
that aren't (or more precisely, that have no chance of being)
overwritten.  When we're reading the database file, the array
|cite_info| contains auxiliary information for |cite_list|.  Later,
|cite_info| will become |sorted_cites|, and this dual role imposes the
(not-very-imposing) restriction |max_strings >= max_cites|.

@d undefined = hash_max + 1     {a special marker used for |type_list|}

@<Globals in the outer block@>=
@!bib_line_num : integer;       {line number of the \.{.bib} file}
@!entry_type_loc : hash_loc;    {the hash-table location of an entry type}
@!type_list : packed array[cite_number] of hash_ptr2;
@!type_exists : boolean;        {|true| if this entry type is \.{.bst}-defined}
@!entry_exists : packed array[cite_number] of boolean;
@!store_entry : boolean;        {|true| if we're to store info for this entry}
@!field_name_loc : hash_loc;    {the hash-table location of a field name}
@!field_val_loc : hash_loc;     {the hash-table location of a field value}
@!store_field : boolean;        {|true| if we're to store info for this field}
@!store_token : boolean;        {|true| if we're to store this macro token}
@!right_outer_delim : ASCII_code; {either a |right_brace| or a |right_paren|}
@!right_str_delim : ASCII_code; {either a |right_brace| or a |double_quote|}
@!at_bib_command : boolean;     {|true| for a command, false for an entry}
@!cur_macro_loc : hash_loc;     {|macro_loc| for a \.{string} being defined}
@!cite_info : packed array[cite_number] of str_number; {extra |cite_list| info}
@!cite_hash_found : boolean;    {set to a previous |hash_found| value}
@!preamble_ptr : bib_number;    {pointer into the |s_preamble| array}
@!num_preamble_strings : bib_number;    {counts the |s_preamble| strings}


@
This little procedure exists because it's used by at least two other
procedures and thus saves some space.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_ln_num_print;
begin
print ('--line ',bib_line_num:0,' of file ');
print_bib_name;
end;


@
When there's a serious error parsing a \.{.bib} file, we flush
everything up to the beginning of the next entry.

@d bib_err(#) == begin          {serious error during \.{.bib} parsing}
                 print (#);
                 bib_err_print;
                 return;
                 end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_err_print;
begin
print ('-');
bib_ln_num_print;
print_bad_input_line;                   {this call does the |mark_error|}
print_skipping_whatever_remains;
if (at_bib_command) then
    print_ln ('command')
  else
    print_ln ('entry');
end;


@
When there's a harmless error parsing a \.{.bib} file, we just give a
warning message.  This is always called after other stuff has been
printed out.

@d bib_warn(#) == begin         {non-serious error during \.{.bst} parsing}
                  print (#);
                  bib_warn_print;
                  end
@#
@d bib_warn_newline(#) == begin         {same as above but with a newline}
                          print_ln (#);
                          bib_warn_print;
                          end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_warn_print;
begin
bib_ln_num_print;
mark_warning;
end;


@
For all |num_bib_files| database files, we keep reading and processing
\.{.bib} entries until none left.

@<Read the \.{.bib} file(s)@>=
begin
@<Final initialization for \.{.bib} processing@>;
read_performed := true;
bib_ptr := 0;
while (bib_ptr < num_bib_files) do
    begin
    print ('Database file #',bib_ptr+1:0,': ');
    print_bib_name;@/
    bib_line_num := 0;          {initialize to get the first input line}
    buf_ptr2 := last;
    while (not eof(cur_bib_file)) do
        get_bib_command_or_entry_and_process;
    a_close (cur_bib_file);
    incr(bib_ptr);
    end;
reading_completed := true;
  trace
  trace_pr_ln ('Finished reading the database file(s)');
  ecart@/
@<Final initialization for processing the entries@>;
read_completed := true;
end


@
We need to initialize the |field_info| array, and also various things
associated with the |cite_list| array (but not |cite_list| itself).

@<Final initialization for \.{.bib} processing@>=
begin
@<Initialize the |field_info|@>;
@<Initialize things for the |cite_list|@>;
end


@
This module initializes all fields of all entries to |missing|, the
value to which all fields are initialized.

@<Initialize the |field_info|@>=
begin
check_field_overflow (num_fields*num_cites);
field_ptr := 0;
while (field_ptr < max_fields) do
    begin
    field_info[field_ptr] := missing;
    incr(field_ptr);
    end;
end


@
@^fetish@>
@:BibTeX capacity exceeded}{\quad total number of fields@>
Complain if somebody's got a field fetish.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure check_field_overflow (@!total_fields : integer);
begin
if (total_fields > max_fields) then
    begin
    print_ln (total_fields:0,' fields:');
    overflow('total number of fields ',max_fields);
    end;
end;


@
We must initialize the |type_list| array so that we can detect
duplicate (or missing) entries for cite keys on |cite_list|.  Also,
when we're to include the entire database, we use the array
|entry_exists| to detect those missing entries whose |cite_list| info
will (or to be more precise, might) be overwritten; and we use the
array |cite_info| to save the part of |cite_list| that will (might) be
overwritten.  We also use |cite_info| for counting cross~references
when it's appropriate---when an entry isn't otherwise to be included
on |cite_list| (that is, the entry isn't \.{\\cite}d or
\.{\\nocite}d).  Such an entry is included on the final |cite_list| if
it's cross~referenced at least |min_crossrefs| times.

@<Initialize things for the |cite_list|@>=
begin
cite_ptr := 0;
while (cite_ptr < max_cites) do
    begin
    type_list[cite_ptr] := empty;@/
    cite_info[cite_ptr] := any_value;  {to appeas \PASCAL's boolean evaluation}
    incr(cite_ptr);
    end;
old_num_cites := num_cites;
if (all_entries) then
    begin
    cite_ptr := all_marker;
    while (cite_ptr < old_num_cites) do
        begin
        cite_info[cite_ptr] := cite_list[cite_ptr];
        entry_exists[cite_ptr] := false;
        incr(cite_ptr);
        end;
    cite_ptr := all_marker;     {we insert the ``other'' entries here}
    end
  else
    begin
    cite_ptr := num_cites;      {we insert the cross-referenced entries here}
    all_marker := any_value;    {to appease \PASCAL's boolean evaluation}
    end;
end


@
Before we actually start the code for reading a database file, we must
define this \.{.bib}-specific scanning function.  It skips over
|white_space| characters until hitting a nonwhite character or the end
of the file, respectively returning |true| or |false|.  It also
updates |bib_line_num|, the line counter.

@<Procedures and functions for input scanning@>=
function eat_bib_white_space : boolean;
label exit;
begin
while (not scan_white_space) do         {no characters left; read another line}
    begin
    if (not input_ln(cur_bib_file)) then        {end-of-file; return |false|}
        begin
        eat_bib_white_space := false;
        return;
        end;
    incr(bib_line_num);
    buf_ptr2 := 0;
    end;
eat_bib_white_space := true;
exit:
end;


@
It's often illegal to end a \.{.bib} command in certain places, and
this is where we come to check.

@d eat_bib_white_and_eof_check ==
        begin
        if (not eat_bib_white_space) then
            begin
            eat_bib_print;
            return;
            end;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure eat_bib_print;
label exit;     {so the call to |bib_err| works}
begin
bib_err ('Illegal end of database file');
exit:
end;


@
And here are a bunch of error-message macros, each called more than
once, that thus save space as implemented.  This one is for when one
of two possible characters is expected while scanning.

@d bib_one_of_two_expected_err(#) ==
        begin
        bib_one_of_two_print (#);
        return;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_one_of_two_print (@!char1,@!char2:ASCII_code);
label exit;     {so the call to |bib_err| works}
begin
bib_err ('I was expecting a `',xchr[char1],''' or a `',xchr[char2],'''');
exit:
end;


@
This one's for an expected |equals_sign|.

@d bib_equals_sign_expected_err ==
        begin
        bib_equals_sign_print;
        return;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_equals_sign_print;
label exit;     {so the call to |bib_err| works}
begin
bib_err ('I was expecting an "',xchr[equals_sign],'"');
exit:
end;


@
This complains about unbalanced braces.

@d bib_unbalanced_braces_err ==
        begin
        bib_unbalanced_braces_print;
        return;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_unbalanced_braces_print;
label exit;     {so the call to |bib_err| works}
begin
bib_err ('Unbalanced braces');
exit:
end;


@
And this one about an overly exuberant field.

@d bib_field_too_long_err ==
        begin
        bib_field_too_long_print;
        return;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_field_too_long_print;
label exit;     {so the call to |bib_err| works}
begin
bib_err ('Your field is more than ',buf_size:0,' characters');
exit:
end;


@
This one is just a warning, not an error.  It's for when something
isn't (or might not be) quite right with a macro name.

@d macro_name_warning(#) ==
        begin
        macro_warn_print;
        bib_warn_newline (#);
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure macro_warn_print;
begin
print ('Warning--string name "');
print_token;
print ('" is ');
end;


@
@:this can't happen}{\quad Identifier scanning error@>
This macro is used to scan all \.{.bib} identifiers.  The argument
tells what was happening at the time.  The associated procedure simply
prints an error message.

@d bib_identifier_scan_check(#) ==
        begin
        if ((scan_result = white_adjacent) or
                                (scan_result = specified_char_adjacent)) then
            do_nothing
        else
            begin
            bib_id_print;
            bib_err (#);
            end;
        end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_id_print;
begin
if (scan_result = id_null) then
    print ('You''re missing ')
else if (scan_result = other_char_adjacent) then
    print ('"',xchr[scan_char],'" immediately follows ')
else
    id_scanning_confusion;
end;


@
This module either reads a database entry, whose three main components
are an entry type, a database key, and a list of fields, or it reads a
\.{.bib} command, whose structure is command dependent and explained
later.

@d cite_already_set = 22        {this gets around \PASCAL\ limitations}
@d first_time_entry = 26        {for checking for repeated database entries}

@<Scan for and process a \.{.bib} command or database entry@>=
procedure get_bib_command_or_entry_and_process;
label cite_already_set,@!first_time_entry,@!loop_exit,@!exit;
begin
at_bib_command := false;@/
@<Skip to the next database entry or \.{.bib} command@>;
@<Scan the entry type or scan and process the \.{.bib} command@>;
eat_bib_white_and_eof_check;
@<Scan the entry's database key@>;
eat_bib_white_and_eof_check;
@<Scan the entry's list of fields@>;
exit:
end;


@
This module skips over everything until hitting an |at_sign| or the
end of the file.  It also updates |bib_line_num|, the line counter.

@<Skip to the next database entry or \.{.bib} command@>=
while (not scan1(at_sign)) do                   {no |at_sign|; get next line}
    begin
    if (not input_ln(cur_bib_file)) then        {end-of-file}
        return;
    incr(bib_line_num);
    buf_ptr2 := 0;
    end


@
@:this can't happen}{\quad An at-sign disappeared@>
This module reads an |at_sign| and an entry type (like `book' or
`article') or a \.{.bib} command.  If it's an entry type, it must be
defined in the \.{.bst} file if this entry is to be included in the
reference list.

@<Scan the entry type or scan and process the \.{.bib} command@>=
begin
if (scan_char <> at_sign) then
    confusion ('An "',xchr[at_sign],'" disappeared');
incr(buf_ptr2);                                 {skip over the |at_sign|}
eat_bib_white_and_eof_check;
scan_identifier (left_brace,left_paren,left_paren);
bib_identifier_scan_check ('an entry type');
  trace
  trace_pr_token;
  trace_pr_ln (' is an entry type or a database-file command');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
command_num := ilk_info[
        str_lookup(buffer,buf_ptr1,token_len,bib_command_ilk,dont_insert)];
if (hash_found) then
    @<Process a \.{.bib} command@>
else
    begin                                       {process an entry type}
    entry_type_loc := str_lookup(
                        buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
    if ((not hash_found) or (fn_type[entry_type_loc]<>wiz_defined)) then@/
        type_exists := false  {no such entry type defined in the \.{.bst} file}
      else
        type_exists := true;
    end;
end


@
@^database-file commands@>
@:this can't happen}{\quad Unknown database-file command@>
Here we determine which \.{.bib} command we're about to process, then
go to it.

@<Process a \.{.bib} command@>=
begin
at_bib_command := true;
case (command_num) of
    n_bib_comment : @<Process a \.{comment} command@>;
    n_bib_preamble : @<Process a \.{preamble} command@>;
    n_bib_string : @<Process a \.{string} command@>;
    othercases bib_cmd_confusion
endcases;
end


@
@:this can't happen}{\quad Unknown database-file command@>
Here's another bug.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bib_cmd_confusion;
begin
confusion ('Unknown database-file command');
end;


@
@:database-file commands}{\quad \.{comment}@>
The \.{comment} command is implemented for SCRIBE compatibility.  It's
not really needed because \BibTeX\ treats (flushes) everything not
within an entry as a comment anyway.

@<Process a \.{comment} command@>=
begin
return;                 {flush comments}
end


@
@:database-file commands}{\quad \.{preamble}@>
The \.{preamble} command lets a user have \TeX\ stuff inserted (by the
standard styles, at least) directly into the \.{.bbl} file.  It is
intended primarily for allowing \TeX\ macro definitions used within
the bibliography entries (for better sorting, for example).  One
\.{preamble} command per \.{.bib} file should suffice.

A \.{preamble} command has either braces or parentheses as outer
delimiters.  Inside is the preamble string, which has the same syntax
as a field value: a nonempty list of field tokens separated by
|concat_char|s.  There are three types of field tokens---nonnegative
numbers, macro names, and delimited strings.

This module does all the scanning (that's not subcontracted), but the
\.{.bib}-specific scanning function
|scan_and_store_the_field_value_and_eat_white| actually stores the
value.

@<Process a \.{preamble} command@>=
begin
if (preamble_ptr = max_bib_files) then
    bib_err ('You''ve exceeded ',max_bib_files:0,' preamble commands');
eat_bib_white_and_eof_check;
if (scan_char = left_brace) then
    right_outer_delim := right_brace
else if (scan_char = left_paren) then
    right_outer_delim := right_paren
else
    bib_one_of_two_expected_err (left_brace,left_paren);
incr(buf_ptr2);                         {skip over the left-delimiter}
eat_bib_white_and_eof_check;
store_field := true;
if (not scan_and_store_the_field_value_and_eat_white) then
    return;
if (scan_char <> right_outer_delim) then
    bib_err ('Missing "',xchr[right_outer_delim],'" in preamble command');
incr(buf_ptr2);                         {skip over the |right_outer_delim|}
return;
end


@
@:database-file commands}{\quad \.{string}@>
The \.{string} command is implemented both for SCRIBE compatibility
and for allowing a user: to override a \.{.bst}-file \.{macro}
command, to define one that the \.{.bst} file doesn't, or to engage in
good, wholesome, typing laziness.

The \.{string} command does mostly the same thing as the
\.{.bst}-file's \.{macro} command (but the syntax is different and the
\.{string} command compresses |white_space|).  In fact, later in this
program, the term ``macro'' refers to either a \.{.bst} ``macro'' or a
\.{.bib} ``string'' (when it's clear from the context that it's not
a \.{WEB} macro).

A \.{string} command has either braces or parentheses as outer
delimiters.  Inside is the string's name (it must be a legal
identifier, and case differences are ignored---all upper-case letters
are converted to lower case), then an |equals_sign|, and the string's
definition, which has the same syntax as a field value: a nonempty
list of field tokens separated by |concat_char|s.  There are three
types of field tokens---nonnegative numbers, macro names, and
delimited strings.

@<Process a \.{string} command@>=
begin
eat_bib_white_and_eof_check;
@<Scan the string's name@>;
eat_bib_white_and_eof_check;
@<Scan the string's definition field@>;
return;
end


@
This module reads a left outer-delimiter and a string name.

@<Scan the string's name@>=
begin
if (scan_char = left_brace) then
    right_outer_delim := right_brace
else if (scan_char = left_paren) then
    right_outer_delim := right_paren
else
    bib_one_of_two_expected_err (left_brace,left_paren);
incr(buf_ptr2);                         {skip over the left-delimiter}
eat_bib_white_and_eof_check;
scan_identifier (equals_sign,equals_sign,equals_sign);
bib_identifier_scan_check ('a string name');
@<Store the string's name@>;
end


@
@^commented-out code@>
This module marks this string as |macro_ilk|; the commented-out code
will give a warning message when overwriting a previously defined
macro.

@<Store the string's name@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a database-defined macro');
  ecart@/
lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
cur_macro_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
ilk_info[cur_macro_loc] := hash_text[cur_macro_loc]; {default in case of error}
  @{
  if (hash_found) then                          {already seen macro}
      macro_name_warning ('having its definition overwritten');
  @}@/
end


@
This module skips over the |equals_sign|, reads and stores the list of
field tokens that defines this macro (compressing |white_space|), and
reads a |right_outer_delim|.

@<Scan the string's definition field@>=
begin
if (scan_char <> equals_sign) then
    bib_equals_sign_expected_err;
incr(buf_ptr2);                         {skip over the |equals_sign|}
eat_bib_white_and_eof_check;
store_field := true;
if (not scan_and_store_the_field_value_and_eat_white) then
    return;
if (scan_char <> right_outer_delim) then
    bib_err ('Missing "',xchr[right_outer_delim],'" in string command');
incr(buf_ptr2);                         {skip over the |right_outer_delim|}
end


@
@^kludge@>
The variables for the function
|scan_and_store_the_field_value_and_eat_white| must be global since
the functions it calls use them too.  The alias kludge helps make the
stack space not overflow on some machines.

@d field_vl_str == ex_buf       {aliases, used ``only'' for this function}
@d field_end == ex_buf_ptr      {the end marker for the field-value string}
@d field_start == ex_buf_xptr   {and the start marker}

@<Globals in the outer block@>=
@!bib_brace_level : integer;    {brace nesting depth (excluding |str_delim|s)}


@
@^gymnastics@>
Since the function |scan_and_store_the_field_value_and_eat_white|
calls several other yet-to-be-described functions (one directly and
two indirectly), we must perform some topological gymnastics.

@<Procedures and functions for input scanning@>=
@<The scanning function |compress_bib_white|@>@;
@<The scanning function |scan_balanced_braces|@>@;
@<The scanning function |scan_a_field_token_and_eat_white|@>


@
This function scans the list of field tokens that define the field
value string.  If |store_field| is |true| it accumulates (indirectly)
in |field_vl_str| the concatenation of all the field tokens,
compressing nonnull |white_space| to a single |space| and, if the
field value is for a field (rather than a string definition), removing
any leading or trailing |white_space|; when it's finished it puts the
string into the hash table.  It returns |false| if there was a serious
syntax error.

@<Procedures and functions for input scanning@>=
function scan_and_store_the_field_value_and_eat_white : boolean;
label exit;
begin
scan_and_store_the_field_value_and_eat_white := false;
                                        {now it's easy to exit if necessary}
field_end := 0;
if (not scan_a_field_token_and_eat_white) then
    return;
while (scan_char = concat_char) do      {scan remaining field tokens}
    begin
    incr(buf_ptr2);                     {skip over the |concat_char|}
    eat_bib_white_and_eof_check;
    if (not scan_a_field_token_and_eat_white) then
        return;
    end;
if (store_field) then
    @<Store the field value string@>;
scan_and_store_the_field_value_and_eat_white := true;
exit:
end;


@
Each field token is either a nonnegative number, a macro name (like
`jan'), or a brace-balanced string delimited by either |double_quote|s
or braces.  Thus there are four possibilities for the first character
of the field token: If it's a |left_brace| or a |double_quote|, the
token (with balanced braces, up to the matching |right_str_delim|) is
a string; if it's |numeric|, the token is a number; if it's anything
else, the token is a macro name (and should thus have been defined by
either the \.{.bst}-file's \.{macro} command or the \.{.bib}-file's
\.{string} command).  This function returns |false| if there was a
serious syntax error.

@<The scanning function |scan_a_field_token_and_eat_white|@>=
function scan_a_field_token_and_eat_white : boolean;
label exit;
begin
scan_a_field_token_and_eat_white := false; {now it's easy to exit if necessary}
case (scan_char) of
    left_brace :
        begin
        right_str_delim := right_brace;
        if (not scan_balanced_braces) then
            return;
        end;
    double_quote :
        begin
        right_str_delim := double_quote;
        if (not scan_balanced_braces) then
            return;
        end;
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" :
        @<Scan a number@>;
    othercases
        @<Scan a macro name@>
endcases;
eat_bib_white_and_eof_check;
scan_a_field_token_and_eat_white := true;
exit:
end;


@
Now we come to the stuff that actually accumulates the field value to
be stored.  This module copies a character into |field_vl_str| if it
will fit; since it's so low level, it's implemented as a macro.

@d copy_char(#) == begin
                   if (field_end = buf_size) then
                       bib_field_too_long_err
                     else
                       begin
                       field_vl_str[field_end] := #;
                       incr(field_end);
                       end;
                   end


@
The \.{.bib}-specific scanning function |compress_bib_white| skips
over |white_space| characters within a string until hitting a nonwhite
character; in fact, it does everything |eat_bib_white_space| does, but
it also adds a |space| to |field_vl_str|.  This function is never
called if there are no |white_space| characters (or ends-of-line) to
be scanned (though the associated macro might be).  The function
returns |false| if there is a serious syntax error.

@d check_for_and_compress_bib_white_space ==
        begin
        if ((lex_class[scan_char]=white_space) or (buf_ptr2=last)) then
            if (not compress_bib_white) then
                return;
        end

@<The scanning function |compress_bib_white|@>=
function compress_bib_white : boolean;
label exit;
begin
compress_bib_white := false;            {now it's easy to exit if necessary}
copy_char (space);
while (not scan_white_space) do         {no characters left; read another line}
    begin
    if (not input_ln(cur_bib_file)) then        {end-of-file; complain}
        begin
        eat_bib_print;
        return;
        end;
    incr(bib_line_num);
    buf_ptr2 := 0;
    end;
compress_bib_white := true;
exit:
end;


@
This \.{.bib}-specific function scans a string with balanced braces,
stopping just past the matching |right_str_delim|.  How much work it
does depends on whether |store_field = true|.  It returns |false| if
there was a serious syntax error.

@<The scanning function |scan_balanced_braces|@>=
function scan_balanced_braces : boolean;
label loop_exit,@!exit;
begin
scan_balanced_braces := false;          {now it's easy to exit if necessary}
incr(buf_ptr2);                         {skip over the left-delimiter}
check_for_and_compress_bib_white_space;
if (field_end > 1) then
  if (field_vl_str[field_end-1] = space) then
    if (field_vl_str[field_end-2] = space) then {remove wrongly added |space|}
        decr(field_end);
bib_brace_level := 0;           {and we're at a non|white_space| character}
if (store_field) then
    @<Do a full brace-balanced scan@>
  else
    @<Do a quick brace-balanced scan@>;
incr(buf_ptr2);                         {skip over the |right_str_delim|}
scan_balanced_braces := true;
exit:
end;


@
This module scans over a brace-balanced string without keeping track
of anything but the brace level.  It starts with |bib_brace_level = 0|
and at a non|white_space| character.

@<Do a quick brace-balanced scan@>=
begin
while (scan_char <> right_str_delim) do {we're at |bib_brace_level = 0|}
    if (scan_char = left_brace) then
        begin
        incr(bib_brace_level);
        incr(buf_ptr2);                 {skip over the |left_brace|}
        eat_bib_white_and_eof_check;
        while (bib_brace_level > 0) do
            @<Do a quick scan with |bib_brace_level > 0|@>;
        end
    else if (scan_char = right_brace) then
        bib_unbalanced_braces_err
    else
        begin
        incr(buf_ptr2);                 {skip over some other character}
        if (not scan3 (right_str_delim, left_brace, right_brace)) then
            eat_bib_white_and_eof_check;
        end
end


@
This module does the same as above but, because |bib_brace_level > 0|, it
doesn't have to look for a |right_str_delim|.

@<Do a quick scan with |bib_brace_level > 0|@>=
begin   {top part of the |while| loop---we're always at a nonwhite character}
if (scan_char = right_brace) then
    begin
    decr(bib_brace_level);
    incr(buf_ptr2);                     {skip over the |right_brace|}
    eat_bib_white_and_eof_check;
    end
else if (scan_char = left_brace) then
    begin
    incr(bib_brace_level);
    incr(buf_ptr2);                     {skip over the |left_brace|}
    eat_bib_white_and_eof_check;
    end
else
    begin
    incr(buf_ptr2);                     {skip over some other character}
    if (not scan2 (right_brace, left_brace)) then
        eat_bib_white_and_eof_check;
    end
end


@
This module scans over a brace-balanced string, compressing multiple
|white_space| characters into a single |space|.  It starts with
|bib_brace_level = 0| and starts at a non|white_space| character.

@<Do a full brace-balanced scan@>=
begin
while (scan_char <> right_str_delim) do
  case (scan_char) of
    left_brace :
        begin
        incr(bib_brace_level);
        copy_char (left_brace);@/
        incr(buf_ptr2);                 {skip over the |left_brace|}
        check_for_and_compress_bib_white_space;@/
        @<Do a full scan with |bib_brace_level > 0|@>;
        end;
    right_brace :
        bib_unbalanced_braces_err;
    othercases
        begin
        copy_char (scan_char);
        incr(buf_ptr2);                 {skip over some other character}
        check_for_and_compress_bib_white_space;
        end
  endcases;
end


@
This module is similar to the last but starts with |bib_brace_level > 0|
(and, like the last, it starts at a non|white_space| character).

@<Do a full scan with |bib_brace_level > 0|@>=
begin
loop
  case (scan_char) of
    right_brace :
        begin
        decr(bib_brace_level);
        copy_char (right_brace);@/
        incr(buf_ptr2);                 {skip over the |right_brace|}
        check_for_and_compress_bib_white_space;
        if (bib_brace_level = 0) then
            goto loop_exit;
        end;
    left_brace :
        begin
        incr(bib_brace_level);
        copy_char (left_brace);@/
        incr(buf_ptr2);                 {skip over the |left_brace|}
        check_for_and_compress_bib_white_space;
        end;
    othercases
        begin
        copy_char (scan_char);
        incr(buf_ptr2);                 {skip over some other character}
        check_for_and_compress_bib_white_space;
        end
  endcases;
loop_exit:
end


@
@:this can't happen}{\quad A digit disappeared@>
This module scans a nonnegative number and copies it to |field_vl_str|
if it's to store the field.

@<Scan a number@>=
begin
if (not scan_nonneg_integer) then
    confusion ('A digit disappeared');
if (store_field) then
    begin
    tmp_ptr := buf_ptr1;
    while (tmp_ptr < buf_ptr2) do
        begin
        copy_char (buffer[tmp_ptr]);
        incr(tmp_ptr);
        end;
    end;
end


@
This module scans a macro name and copies its string to |field_vl_str|
if it's to store the field, complaining if the macro is recursive or
undefined.

@<Scan a macro name@>=
begin
scan_identifier (comma,right_outer_delim,concat_char);
bib_identifier_scan_check ('a field part');
if (store_field) then
    begin
    lower_case (buffer, buf_ptr1, token_len);   {ignore case differences}
    macro_name_loc := str_lookup(
                        buffer,buf_ptr1,token_len,macro_ilk,dont_insert);
    store_token := true;
    if (at_bib_command) then
      if (command_num = n_bib_string) then
        if (macro_name_loc = cur_macro_loc) then
            begin
            store_token := false;
            macro_name_warning ('used in its own definition');
            end;
    if (not hash_found) then
        begin
        store_token := false;
        macro_name_warning ('undefined');
        end;
    if (store_token) then
        @<Copy the macro string to |field_vl_str|@>;
    end;
end


@
The macro definition may have |white_space| that needs compressing,
because it may have been defined in the \.{.bst} file.

@<Copy the macro string to |field_vl_str|@>=
begin
tmp_ptr := str_start[ilk_info[macro_name_loc]];
tmp_end_ptr := str_start[ilk_info[macro_name_loc]+1];
if (field_end = 0) then
  if ((lex_class[str_pool[tmp_ptr]] = white_space) and (tmp_ptr < tmp_end_ptr))
                                                                        then
    begin               {compress leading |white_space| of first nonnull token}
    copy_char (space);
    incr(tmp_ptr);
    while ((lex_class[str_pool[tmp_ptr]] = white_space) and
                                                (tmp_ptr <  tmp_end_ptr)) do
        incr(tmp_ptr);
    end;                {the next remaining character is non|white_space|}
while (tmp_ptr < tmp_end_ptr) do
    begin
    if (lex_class[str_pool[tmp_ptr]] <> white_space) then
        copy_char (str_pool[tmp_ptr])
      else if (field_vl_str[field_end-1] <> space) then
        copy_char (space);
    incr(tmp_ptr);
    end;
end


@
@^ham and eggs@>
Now it's time to store the field value in the hash table, and store an
appropriate pointer to it (depending on whether it's for a database
entry or command).  But first, if necessary, we remove a trailing
|space| and a leading |space| if these exist.  (Hey, if we had some
ham we could make ham-and-eggs if we had some eggs.)

@<Store the field value string@>=
begin
if (not at_bib_command) then            {chop trailing |space| for a field}
  if (field_end > 0) then
    if (field_vl_str[field_end-1] = space) then
        decr(field_end);
if ((not at_bib_command) and (field_vl_str[0] = space) and (field_end > 0))
                                then    {chop leading |space| for a field}
    field_start := 1
  else
    field_start := 0;
field_val_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
                                                        text_ilk,do_insert);
fn_type[field_val_loc] := str_literal;          {set the |fn_class|}
  trace
  trace_pr ('"');
  trace_pr_pool_str (hash_text[field_val_loc]);
  trace_pr_ln ('" is a field value');
  ecart@/
if (at_bib_command) then        {for a \.{preamble} or \.{string} command}
    @<Store the field value for a command@>
  else                                                  {for a database entry}
    @<Store the field value for a database entry@>;
end


@
@:this can't happen}{\quad Unknown database-file command@>
Here's where we store the goods when we're dealing with a command
rather than an entry.

@<Store the field value for a command@>=
begin
case (command_num) of
    n_bib_preamble :
        begin
        s_preamble[preamble_ptr] := hash_text[field_val_loc];
        incr(preamble_ptr);
        end;
    n_bib_string :
        ilk_info[cur_macro_loc] := hash_text[field_val_loc];
    othercases bib_cmd_confusion
endcases;
end


@
And here, an entry.

@<Store the field value for a database entry@>=
begin
field_ptr := entry_cite_ptr * num_fields + fn_info[field_name_loc];
if (field_info[field_ptr] <> missing) then
    begin
    print ('Warning--I''m ignoring ');
    print_pool_str (cite_list[entry_cite_ptr]);
    print ('''s extra "');
    print_pool_str (hash_text[field_name_loc]);
    bib_warn_newline ('" field');
    end
  else
    begin                       {the field was empty, store its new value}
    field_info[field_ptr] := hash_text[field_val_loc];
    if ((fn_info[field_name_loc] = crossref_num) and (not all_entries)) then
        @<Add or update a cross reference on |cite_list| if necessary@>;
    end;
end


@
@^kludge@>
@:this can't happen}{\quad Cite hash error@>
If the cross-referenced entry isn't already on |cite_list| we add it
(at least temporarily); if it is already on |cite_list| we update the
cross-reference count, if necessary.  Note that |all_entries| is
|false| here.  The alias kludge helps make the stack space not
overflow on some machines.

@d extra_buf == out_buf         {an alias, used only in this module}

@<Add or update a cross reference on |cite_list| if necessary@>=
begin
tmp_ptr := field_start;
while (tmp_ptr < field_end) do
    begin
    extra_buf[tmp_ptr] := field_vl_str[tmp_ptr];
    incr(tmp_ptr);
    end;
lower_case (extra_buf, field_start, field_end-field_start);
                                                {convert to `canonical' form}
lc_cite_loc := str_lookup(extra_buf,field_start,field_end-field_start,
                                                        lc_cite_ilk,do_insert);
if (hash_found) then
    begin
    cite_loc := ilk_info[lc_cite_loc];  {even if there's a case mismatch}
    if (ilk_info[cite_loc] >= old_num_cites) then  {a previous \.{crossref}}
        incr(cite_info[ilk_info[cite_loc]]);
    end
  else
    begin                                       {it's a new \.{crossref}}
    cite_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
                                                        cite_ilk,do_insert);
    if (hash_found) then
        hash_cite_confusion;
    add_database_cite (cite_ptr);               {this increments |cite_ptr|}
    cite_info[ilk_info[cite_loc]] := 1; {the first cross-ref for this cite key}
    end;
end


@
This procedure adds (or restores) to |cite_list| a cite key; it is
called only when |all_entries| is |true| or when adding
cross~references, and it assumes that |cite_loc| and |lc_cite_loc| are
set.  It also increments its argument.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure add_database_cite (var new_cite : cite_number);
begin
check_cite_overflow (new_cite);                 {make sure this cite will fit}
check_field_overflow (num_fields*new_cite);
cite_list[new_cite] := hash_text[cite_loc];
ilk_info[cite_loc] := new_cite;
ilk_info[lc_cite_loc] := cite_loc;
incr(new_cite);
end;


@
And now, back to processing an entry (rather than a command).  This
module reads a left outer-delimiter and a database key.

@<Scan the entry's database key@>=
begin
if (scan_char = left_brace) then
    right_outer_delim := right_brace
else if (scan_char = left_paren) then
    right_outer_delim := right_paren
else
    bib_one_of_two_expected_err (left_brace,left_paren);
incr(buf_ptr2);                                 {skip over the left-delimiter}
eat_bib_white_and_eof_check;
if (right_outer_delim = right_paren) then       {to allow it in a database key}
    begin
    if (scan1_white(comma)) then                {ok if database key ends line}
        do_nothing;
    end
  else
    if (scan2_white(comma,right_brace)) then {|right_brace=right_outer_delim|}
        do_nothing;
@<Check for a database key of interest@>;
end


@
@^kludge@>
The lower-case version of this database key must correspond to one in
|cite_list|, or else |all_entries| must be |true|, if this entry is to
be included in the reference list.  Accordingly, this module sets
|store_entry|, which determines whether the relevant information for
this entry is stored.  The alias kludge helps make the stack space not
overflow on some machines.

@d ex_buf3 == ex_buf            {an alias, used only in this module}

@<Check for a database key of interest@>=
begin
  trace
  trace_pr_token;
  trace_pr_ln (' is a database key');
  ecart@/
tmp_ptr := buf_ptr1;
while (tmp_ptr < buf_ptr2) do
    begin
    ex_buf3[tmp_ptr] := buffer[tmp_ptr];
    incr(tmp_ptr);
    end;
lower_case (ex_buf3, buf_ptr1, token_len);      {convert to `canonical' form}
if (all_entries) then
    lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,do_insert)
  else
    lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,
                                                                dont_insert);
if (hash_found) then
    begin
    entry_cite_ptr := ilk_info[ilk_info[lc_cite_loc]];
    @<Check for a duplicate or \.{crossref}-matching database key@>;
    end;
store_entry := true;    {unless |(not hash_found) and (not all_entries)|}
if (all_entries) then
    @<Put this cite key in its place@>
  else if (not hash_found) then
    store_entry := false;       {no such cite key exists on |cite_list|}
if (store_entry) then
    @<Make sure this entry is ok before proceeding@>;
end


@
@:this can't happen}{\quad The cite list is messed up@>
It's illegal to have two (or more) entries with the same database key
(even if there are case differrences), and we skip the rest of the
entry for such a repeat occurrence.  Also, we make this entry's
database key the official |cite_list| key if it's on |cite_list| only
because of cross references.

@<Check for a duplicate or \.{crossref}-matching database key@>=
begin
if ((not all_entries) or (entry_cite_ptr < all_marker)
                                or (entry_cite_ptr >= old_num_cites)) then
    begin
    if (type_list[entry_cite_ptr] = empty) then
        begin
        @<Make sure this entry's database key is on |cite_list|@>;
        goto first_time_entry;
        end;
    end
else if (not entry_exists[entry_cite_ptr]) then
    begin
    @<Find the lower-case equivalent of the |cite_info| key@>;
    if (lc_xcite_loc = lc_cite_loc) then
        goto first_time_entry;
    end;@/
                                {oops---repeated entry---issue a reprimand}
if (type_list[entry_cite_ptr] = empty) then
    confusion ('The cite list is messed up');
bib_err ('Repeated entry');
first_time_entry:  {note that when we leave normally, |hash_found| is |true|}
end


@
An entry that's on |cite_list| only because of cross referencing must
have its database key (rather than one of the \.{crossref} keys) as
the official |cite_list| string.  Here's where we assure that.  The
variable |hash_found| is |true| upon entrance to and exit from this
module.

@<Make sure this entry's database key is on |cite_list|@>=
begin
if ((not all_entries) and (entry_cite_ptr >= old_num_cites)) then
    begin
    cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
    if (not hash_found) then
        begin                   {it's not on |cite_list|---put it there}
        ilk_info[lc_cite_loc] := cite_loc;
        ilk_info[cite_loc] := entry_cite_ptr;
        cite_list[entry_cite_ptr] := hash_text[cite_loc];@/
        hash_found := true;             {restore this value for later use}
        end;
    end;
end


@
@^kludge@>
@:this can't happen}{\quad A cite key disappeared@>
This module, a simpler version of the
|find_cite_locs_for_this_cite_key| function, exists primarily to
compute |lc_xcite_loc|.  When this code is executed we have
|(all_entries) and (entry_cite_ptr >= all_marker) and (not
entry_exists[entry_cite_ptr])|.  The alias kludge helps make the stack
space not overflow on some machines.

@d ex_buf4 == ex_buf            {aliases, used only}
@d ex_buf4_ptr == ex_buf_ptr    {in this module}

@<Find the lower-case equivalent of the |cite_info| key@>=
begin
ex_buf4_ptr := 0;
tmp_ptr := str_start[cite_info[entry_cite_ptr]];
tmp_end_ptr := str_start[cite_info[entry_cite_ptr]+1];
while (tmp_ptr < tmp_end_ptr) do
    begin
    ex_buf4[ex_buf4_ptr] := str_pool[tmp_ptr];
    incr(ex_buf4_ptr);
    incr(tmp_ptr);
    end;
lower_case (ex_buf4, 0, length(cite_info[entry_cite_ptr]));
                                                {convert to `canonical' form}
lc_xcite_loc := str_lookup(ex_buf4,0,length(cite_info[entry_cite_ptr]),
                                                lc_cite_ilk,dont_insert);
if (not hash_found) then
    cite_key_disappeared_confusion;
end


@
@:this can't happen}{\quad A cite key disappeared@>
Here's another bug complaint.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure cite_key_disappeared_confusion;
begin
confusion ('A cite key disappeared');
end;


@
@:this can't happen}{\quad Cite hash error@>
This module, which gets executed only when |all_entries| is |true|,
does one of three things, depending on whether or not, and where, the
cite key appears on |cite_list|: If it's on |cite_list| before
|all_marker|, there's nothing to be done; if it's after |all_marker|,
it must be reinserted (at the current place) and we must note that its
corresponding entry exists; and if it's not on |cite_list| at all, it
must be inserted for the first time.  The |goto| construct must stay
as is, partly because some \PASCAL\ compilers might complain if
``|and|'' were to connect the two boolean expressions (since
|entry_cite_ptr| could be uninitialized when |hash_found| is |false|).

@<Put this cite key in its place@>=
begin
if (hash_found) then
    begin
    if (entry_cite_ptr < all_marker) then
        goto cite_already_set           {that is, do nothing}
      else
        begin
        entry_exists[entry_cite_ptr] := true;
        cite_loc := ilk_info[lc_cite_loc];
        end;
    end
  else
    begin                               {this is a new key}
    cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
    if (hash_found) then
        hash_cite_confusion;
    end;@/
entry_cite_ptr := cite_ptr;
add_database_cite (cite_ptr);           {this increments |cite_ptr|}
cite_already_set:
end


@
@^case mismatch errors@>
@^commented-out code@>
We must give a warning if this entry~type doesn't exist.  Also, we
point the appropriate entry of |type_list| to the entry type just read
above.

For SCRIBE compatibility, the code to give a warning for a case
mismatch between a cite key and a database key has been commented out.
In fact, SCRIBE is the reason that it doesn't produce an error message
outright.  (Note: Case mismatches between two cite keys produce
full-blown errors.)

@<Make sure this entry is ok before proceeding@>=
begin
  @{
  dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
  if (not hash_found) then      {give a warning if there is a case difference}
    begin
    print ('Warning--case mismatch, database key "');
    print_token;
    print ('", cite key "');
    print_pool_str (cite_list[entry_cite_ptr]);
    bib_warn_newline ('"');
    end;
  @}@/
if (type_exists) then
    type_list[entry_cite_ptr] := entry_type_loc
  else
    begin
    type_list[entry_cite_ptr] := undefined;
    print ('Warning--entry type for "');
    print_token;
    bib_warn_newline ('" isn''t style-file defined');
    end;
end


@
This module reads a |comma| and a field as many times as it can, and
then reads a |right_outer_delim|, ending the current entry.

@<Scan the entry's list of fields@>=
begin
while (scan_char <> right_outer_delim) do
    begin
    if (scan_char <> comma) then
        bib_one_of_two_expected_err (comma,right_outer_delim);
    incr(buf_ptr2);                     {skip over the |comma|}
    eat_bib_white_and_eof_check;
    if (scan_char = right_outer_delim) then
        goto loop_exit;
    @<Get the next field name@>;
    eat_bib_white_and_eof_check;
    if (not scan_and_store_the_field_value_and_eat_white) then
        return;
    end;
loop_exit:
incr(buf_ptr2);                         {skip over the |right_outer_delim|}
end


@
This module reads a field name; its contents won't be stored unless it
was declared in the \.{.bst} file and |store_entry = true|.

@<Get the next field name@>=
begin
scan_identifier (equals_sign,equals_sign,equals_sign);
bib_identifier_scan_check ('a field name');
  trace
  trace_pr_token;
  trace_pr_ln (' is a field name');
  ecart@/
store_field := false;
if (store_entry) then
    begin
    lower_case (buffer, buf_ptr1, token_len);   {ignore case differences}
    field_name_loc := str_lookup(
                        buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
    if (hash_found) then
      if (fn_type[field_name_loc]=field) then@/
        store_field := true;  {field name was pre-defined or \.{.bst}-declared}
    end;
eat_bib_white_and_eof_check;
if (scan_char <> equals_sign) then
    bib_equals_sign_expected_err;
incr(buf_ptr2);                 {skip over the |equals_sign|}
end


@
This gets things ready for further \.{.bst} processing.

@<Final initialization for processing the entries@>=
begin
num_cites := cite_ptr;  {to include database and \.{crossref} cite keys, too}
num_preamble_strings := preamble_ptr;   {number of \.{preamble} commands seen}
@<Add cross-reference information@>;
@<Subtract cross-reference information@>;
@<Remove missing entries or those cross referenced too few times@>;
@<Initialize the |int_entry_var|s@>;
@<Initialize the |str_entry_var|s@>;
@<Initialize the |sorted_cites|@>;
end


@
@^child entry@>
@^cross references@>
@^nested cross references@>
@^parent entry@>
Now we update any entry (here called a {\it child\/} entry) that
cross~referenced another (here called a {\it parent\/} entry); this
cross~referencing occurs when the child's \.{crossref} field (value)
consists of the parent's database key.  To do the update, we replace
the child's |missing| fields by the corresponding fields of the
parent.  Also, we make sure the \.{crossref} field contains the
case-correct version.  Finally, although it is technically illegal to
nest cross~references, and although we give a warning (a few modules
hence) when someone tries, we do what we can to accommodate the
attempt.

@<Add cross-reference information@>=
begin
cite_ptr := 0;
while (cite_ptr < num_cites) do
    begin
    field_ptr := cite_ptr * num_fields + crossref_num;
    if (field_info[field_ptr] <> missing) then
      if (find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
        begin
        cite_loc := ilk_info[lc_cite_loc];
        field_info[field_ptr] := hash_text[cite_loc];
        cite_parent_ptr := ilk_info[cite_loc];
        field_ptr := cite_ptr * num_fields + num_pre_defined_fields;
        field_end_ptr := field_ptr - num_pre_defined_fields + num_fields;
        field_parent_ptr := cite_parent_ptr * num_fields
                                                + num_pre_defined_fields;
        while (field_ptr < field_end_ptr) do
            begin
            if (field_info[field_ptr] = missing) then
                field_info[field_ptr] := field_info[field_parent_ptr];
            incr(field_ptr);
            incr(field_parent_ptr);
            end;
        end;
    incr(cite_ptr);
    end;
end


@
@^kludge@>
@^raisin@>
Occasionally we need to figure out the hash-table location of a given
cite-key string and its lower-case equivalent.  This function does
that.  To perform the task it needs to borrow a buffer, a need that
gives rise to the alias kludge---it helps make the stack space not
overflow on some machines (and while it's at it, it'll borrow a
pointer, too).  Finally, the function returns |true| if the cite key
exists on |cite_list|, and its sets |cite_hash_found| according to
whether or not it found the actual version (before |lower_case|ing) of
the cite key; however, its {\sl raison d'\^$\mkern-8mu$etre\/}
(literally, ``to eat a raisin'') is to compute |cite_loc| and
|lc_cite_loc|.

@d ex_buf5 == ex_buf            {aliases, used only}
@d ex_buf5_ptr == ex_buf_ptr    {in this module}

@<Procedures and functions for handling numbers, characters, and strings@>=
function find_cite_locs_for_this_cite_key (@!cite_str : str_number) : boolean;
begin
ex_buf5_ptr := 0;
tmp_ptr := str_start[cite_str];
tmp_end_ptr := str_start[cite_str+1];
while (tmp_ptr < tmp_end_ptr) do
    begin
    ex_buf5[ex_buf5_ptr] := str_pool[tmp_ptr];
    incr(ex_buf5_ptr);
    incr(tmp_ptr);
    end;
cite_loc := str_lookup(ex_buf5,0,length(cite_str),cite_ilk,dont_insert);
cite_hash_found := hash_found;
lower_case (ex_buf5, 0, length(cite_str));      {convert to `canonical' form}
lc_cite_loc := str_lookup(ex_buf5,0,length(cite_str),lc_cite_ilk,dont_insert);
if (hash_found) then
    find_cite_locs_for_this_cite_key := true
  else
    find_cite_locs_for_this_cite_key := false;
end;


@
@:this can't happen}{\quad Cite hash error@>
Here we remove the \.{crossref} field value for each child whose
parent was cross~referenced too few times.  We also issue any
necessary warnings arising from a bad cross~reference.

@<Subtract cross-reference information@>=
begin
cite_ptr := 0;
while (cite_ptr < num_cites) do
    begin
    field_ptr := cite_ptr * num_fields + crossref_num;
    if (field_info[field_ptr] <> missing) then
      if (not find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
        begin                           {the parent is not on |cite_list|}
        if (cite_hash_found) then
            hash_cite_confusion;
        nonexistent_cross_reference_error;
        field_info[field_ptr] := missing;       {remove the \.{crossref} ptr}
        end
      else
        begin                           {the parent exists on |cite_list|}
        if (cite_loc <> ilk_info[lc_cite_loc]) then
            hash_cite_confusion;
        cite_parent_ptr := ilk_info[cite_loc];
        if (type_list[cite_parent_ptr] = empty) then
            begin
            nonexistent_cross_reference_error;@/
            field_info[field_ptr] := missing;   {remove the \.{crossref} ptr}
            end
          else
            begin                       {the parent exists in the database too}
            field_parent_ptr := cite_parent_ptr * num_fields + crossref_num;
            if (field_info[field_parent_ptr] <> missing) then
                @<Complain about a nested cross reference@>;
            if ((not all_entries) and (cite_parent_ptr >= old_num_cites) and
                        (cite_info[cite_parent_ptr] < min_crossrefs)) then@/
                field_info[field_ptr] := missing; {remove the \.{crossref} ptr}
            end;
        end;
    incr(cite_ptr);
    end;
end


@
This procedure exists to save space, since it's used twice---once for
each of the two succeeding modules.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bad_cross_reference_print (@!s:str_number);
begin
print ('--entry "');
print_pool_str (cur_cite_str);
print_ln ('"');
print ('refers to entry "');
print_pool_str (s);
end;


@
When an entry being cross referenced doesn't exist on |cite_list|, we
complain.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure nonexistent_cross_reference_error;
begin
print ('A bad cross reference-');
bad_cross_reference_print (field_info[field_ptr]);
print_ln ('", which doesn''t exist');
mark_error;
end;


@
We also complain when an entry being cross referenced has a
non|missing| \.{crossref} field itself, but this one is just a
warning, not a full-blown error.

@<Complain about a nested cross reference@>=
begin
print ('Warning--you''ve nested cross references');
bad_cross_reference_print (cite_list[cite_parent_ptr]);
print_ln ('", which also refers to something');
mark_warning;
end


@
We remove (and give a warning for) each cite key on the original
|cite_list| without a corresponding database entry.  And we remove any
entry that was included on |cite_list| only because it was
cross~referenced, yet was cross~referenced fewer than |min_crossrefs|
times.  Throughout this module, |cite_ptr| points to the next cite key
to be checked and |cite_xptr| points to the next permanent spot on
|cite_list|.

@<Remove missing entries or those cross referenced too few times@>=
begin
cite_ptr := 0;
while (cite_ptr < num_cites) do
    begin
    if (type_list[cite_ptr] = empty) then
        print_missing_entry (cur_cite_str)
    else if ((all_entries) or (cite_ptr < old_num_cites) or
                                (cite_info[cite_ptr] >= min_crossrefs)) then
        begin
        if (cite_ptr > cite_xptr) then
            @<Slide this cite key down to its permanent spot@>;
        incr(cite_xptr);
        end;
    incr(cite_ptr);
    end;
num_cites := cite_xptr;
if (all_entries) then
    @<Complain about missing entries whose cite keys got overwritten@>;
end


@
When a cite key on the original |cite_list| (or added to |cite_list|
because of cross~referencing) didn't appear in the database, complain.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_missing_entry (@!s:str_number);
begin
print ('Warning--I didn''t find a database entry for "');
print_pool_str (s);
print_ln ('"');
mark_warning;
end;


@
@:this can't happen}{\quad A cite key disappeared@>
@:this can't happen}{\quad Cite hash error@>
We have to move to its final resting place all the entry information
associated with the exact location in |cite_list| of this cite key.

@<Slide this cite key down to its permanent spot@>=
begin
cite_list[cite_xptr] := cite_list[cite_ptr];
type_list[cite_xptr] := type_list[cite_ptr];
if (not find_cite_locs_for_this_cite_key (cite_list[cite_ptr])) then
    cite_key_disappeared_confusion;
if ((not cite_hash_found) or (cite_loc <> ilk_info[lc_cite_loc])) then
    hash_cite_confusion;
ilk_info[cite_loc] := cite_xptr;@/
field_ptr := cite_xptr * num_fields;
field_end_ptr := field_ptr + num_fields;
tmp_ptr := cite_ptr * num_fields;
while (field_ptr < field_end_ptr) do
    begin
    field_info[field_ptr] := field_info[tmp_ptr];
    incr(field_ptr);
    incr(tmp_ptr);
    end;
end


@
We need this module only when we're including the whole database.
It's for missing entries whose cite key originally resided in
|cite_list| at a spot that another cite key (might have) claimed.

@<Complain about missing entries whose cite keys got overwritten@>=
begin
cite_ptr := all_marker;
while (cite_ptr < old_num_cites) do
    begin
    if (not entry_exists[cite_ptr]) then
        print_missing_entry (cite_info[cite_ptr]);
    incr(cite_ptr);
    end;
end


@
@:BibTeX capacity exceeded}{\quad total number of integer entry-variables@>
This module initializes all |int_entry_var|s of all entries to 0, the
value to which all integers are initialized.

@<Initialize the |int_entry_var|s@>=
begin
if (num_ent_ints*num_cites > max_ent_ints) then
    begin
    print (num_ent_ints*num_cites,': ');
    overflow('total number of integer entry-variables ',max_ent_ints);
    end;
int_ent_ptr := 0;
while (int_ent_ptr < num_ent_ints*num_cites) do
    begin
    entry_ints[int_ent_ptr] := 0;
    incr(int_ent_ptr);
    end;
end


@
@:BibTeX capacity exceeded}{\quad total number of string entry-variables@>
This module initializes all |str_entry_var|s of all entries to the
null string, the value to which all strings are initialized.

@<Initialize the |str_entry_var|s@>=
begin
if (num_ent_strs*num_cites > max_ent_strs) then
    begin
    print (num_ent_strs*num_cites,': ');
    overflow('total number of string entry-variables ',max_ent_strs);
    end;
str_ent_ptr := 0;
while (str_ent_ptr < num_ent_strs*num_cites) do
    begin
    entry_strs[str_ent_ptr][0] := end_of_string;
    incr(str_ent_ptr);
    end;
end


@
The array |sorted_cites| initially specifies that the entries are to
be processed in order of cite-key occurrence.  The \.{sort} command
may change this to whatever it likes (which, we hope, is whatever the
style-designer instructs it to like).  We make |sorted_cites| an alias
to save space; this works fine because we're done with |cite_info|.

@d sorted_cites == cite_info    {an alias used for the rest of the program}

@<Initialize the |sorted_cites|@>=
begin
cite_ptr := 0;
while (cite_ptr < num_cites) do
    begin
    sorted_cites[cite_ptr] := cite_ptr;
    incr(cite_ptr);
    end;
end



@* Executing the style file.
This part of the program produces the output by executing the
\.{.bst}-file commands \.{execute}, \.{iterate}, \.{reverse}, and
\.{sort}.  To do this it uses a stack (consisting of the two arrays
|lit_stack| and |lit_stk_type|) for storing literals, a buffer
|ex_buf| for manipulating strings, and an array |sorted_cites|
for holding pointers to the sorted cite keys (|sorted_cites| is an
alias of |cite_info|).

@<Globals in the outer block@>=
@!lit_stack : array[lit_stk_loc] of integer;    {the literal function stack}
@!lit_stk_type : array[lit_stk_loc] of stk_type; {their corresponding types}
@!lit_stk_ptr : lit_stk_loc;    {points just above the top of the stack}
@!cmd_str_ptr : str_number;     {stores value of |str_ptr| during execution}
@!ent_chr_ptr : 0..ent_str_size; {points at a |str_entry_var| character}
@!glob_chr_ptr : 0..glob_str_size; {points at a |str_global_var| character}
@!ex_buf : buf_type;            {a buffer for manipulating strings}
@!ex_buf_ptr : buf_pointer;     {general |ex_buf| location}
@!ex_buf_length : buf_pointer;  {the length of the current string in |ex_buf|}
@!out_buf : buf_type;           {the \.{.bbl} output buffer}
@!out_buf_ptr : buf_pointer;    {general |out_buf| location}
@!out_buf_length : buf_pointer; {the length of the current string in |out_buf|}
@!mess_with_entries : boolean;  {|true| if functions can use entry info}
@!sort_cite_ptr : cite_number;  {a loop index for the sorted cite keys}
@!sort_key_num : str_ent_loc;   {index for the |str_entry_var| \.{sort.key\$}}
@!brace_level : integer;        {the brace nesting depth within a string}


@
Where |lit_stk_loc| is a stack location, and where |stk_type| gives
one of the three types of literals (an integer, a string, or a
function) or a special marker.  If a |lit_stk_type| element is a
|stk_int| then the corresponding |lit_stack| element is an integer; if
a |stk_str|, then a pointer to a |str_pool| string; and if a |stk_fn|,
then a pointer to the function's hash-table location.  However, if the
literal should have been a |stk_str| that was the value of a field
that happened to be |missing|, then the special value
|stk_field_missing| goes on the stack instead; its corresponding
|lit_stack| element is a pointer to the field-name's string.  Finally,
|stk_empty| is the type of a literal popped from an empty stack.

@d stk_int = 0          {an integer literal}
@d stk_str = 1          {a string literal}
@d stk_fn = 2           {a function literal}
@d stk_field_missing = 3 {a special marker: a field value was missing}
@d stk_empty = 4        {another: the stack was empty when this was popped}
@d last_lit_type = 4    {the same number as on the line above}

@<Types in the outer block@>=
@!lit_stk_loc = 0..lit_stk_size;        {the stack range}
@!stk_type = 0..last_lit_type;          {the literal types}


@
And the first output line requires this initialization.

@<Set initial values of key variables@>=
out_buf_length := 0;


@
When there's an error while executing \.{.bst} functions, what we do
depends on whether the function is messing with the entries.
Furthermore this error is serious enough to classify as an
|error_message| instead of a |warning_message|.  These messages (that
is, from |bst_ex_warn|) are meant both for the user and for the style
designer while debugging.

@d bst_ex_warn(#) == begin              {error while executing some function}
                     print (#);
                     bst_ex_warn_print;
                     end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_ex_warn_print;
begin
if (mess_with_entries) then
    begin
    print (' for entry ');
    print_pool_str (cur_cite_str);
    end;
print_newline;
print ('while executing-');
bst_ln_num_print;
mark_error;
end;


@
When an error is so harmless, we print a |warning_message| instead of
an |error_message|.

@d bst_mild_ex_warn(#) == begin         {error while executing some function}
                          print (#);
                          bst_mild_ex_warn_print;
                          end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_mild_ex_warn_print;
begin
if (mess_with_entries) then
    begin
    print (' for entry ');
    print_pool_str (cur_cite_str);
    end;
print_newline;
bst_warn ('while executing');                   {This does the |mark_warning|}
end;


@
It's illegal to mess with the entry information at certain times;
here's a complaint for these times.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_cant_mess_with_entries_print;
begin
bst_ex_warn ('You can''t mess with entries here');
end;


@
This module executes a single specified function once.  It can't do
anything with the entries.

@<Perform an \.{execute} command@>=
begin
init_command_execution;
mess_with_entries := false;
execute_fn (fn_loc);
check_command_execution;
end


@
This module iterates a single specified function for all entries
specified by |cite_list|.

@<Perform an \.{iterate} command@>=
begin
init_command_execution;
mess_with_entries := true;
sort_cite_ptr := 0;
while (sort_cite_ptr < num_cites) do
    begin
    cite_ptr := sorted_cites[sort_cite_ptr];
      trace
      trace_pr_pool_str (hash_text[fn_loc]);
      trace_pr (' to be iterated on ');
      trace_pr_pool_str (cur_cite_str);
      trace_pr_newline;
      ecart@/
    execute_fn (fn_loc);
    check_command_execution;
    incr(sort_cite_ptr);
    end;
end


@
This module iterates a single specified function for all entries
specified by |cite_list|, but does it in reverse order.

@<Perform a \.{reverse} command@>=
begin
init_command_execution;
mess_with_entries := true;
if (num_cites > 0) then
    begin
    sort_cite_ptr := num_cites;
    repeat
        decr(sort_cite_ptr);
        cite_ptr := sorted_cites[sort_cite_ptr];
          trace
          trace_pr_pool_str (hash_text[fn_loc]);
          trace_pr (' to be iterated in reverse on ');
          trace_pr_pool_str (cur_cite_str);
          trace_pr_newline;
          ecart@/
        execute_fn (fn_loc);
        check_command_execution;
      until (sort_cite_ptr = 0);
    end;
end


@
This module sorts the entries based on \.{sort.key\$}; it is a stable
sort.

@<Perform a \.{sort} command@>=
begin
  trace
  trace_pr_ln ('Sorting the entries');
  ecart@/
if (num_cites > 1) then
    quick_sort (0, num_cites-1);
  trace
  trace_pr_ln ('Done sorting');
  ecart@/
end


@
These next two procedures (actually, one procedures and one function,
but who's counting) are subroutines for |quick_sort|, which follows.
The |swap| procedure exchanges the two elements its arguments point
to.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure swap (@!swap1,@!swap2 : cite_number);
var innocent_bystander : cite_number;   {the temporary element in an exchange}
begin
innocent_bystander := sorted_cites[swap2];
sorted_cites[swap2] := sorted_cites[swap1];
sorted_cites[swap1] := innocent_bystander;
end;


@
@:this can't happen}{\quad Duplicate sort key@>
The function |less_than| compares the two \.{sort.key\$}s indirectly
pointed to by its arguments and returns |true| if the first argument's
\.{sort.key\$} is lexicographically less than the second's (that is,
alphabetically earlier).  In case of ties the function compares the
indices |arg1| and |arg2|, which are assumed to be different, and
returns |true| if the first is smaller.  This function uses
|ASCII_code|s to compare, so it might give ``interesting'' results
when handling nonletters.

@d compare_return(#) == begin           {the compare is finished}
                        less_than := #;
                        return;
                        end

@<Procedures and functions for handling numbers, characters, and strings@>=
function less_than (@!arg1,@!arg2 : cite_number) : boolean;
label exit;
var char_ptr : 0..ent_str_size;         {character index into compared strings}
    @!ptr1,@!ptr2 : str_ent_loc;        {the two \.{sort.key\$} pointers}
    @!char1,@!char2 : ASCII_code;       {the two characters being compared}
begin
ptr1 := arg1*num_ent_strs + sort_key_num;
ptr2 := arg2*num_ent_strs + sort_key_num;
char_ptr := 0;
loop
    begin
    char1 := entry_strs[ptr1][char_ptr];
    char2 := entry_strs[ptr2][char_ptr];
    if (char1 = end_of_string) then
        if (char2 = end_of_string) then
            if (arg1 < arg2) then
                compare_return (true)
            else if (arg1 > arg2) then
                compare_return (false)
            else                                {|arg1 = arg2|}
                confusion ('Duplicate sort key')
        else                                    {|char2 <> end_of_string|}
            compare_return (true)
    else                                        {|char1 <> end_of_string|}
        if (char2 = end_of_string) then
            compare_return (false)
    else if (char1 < char2) then
        compare_return (true)
    else if (char1 > char2) then
        compare_return (false);
    incr(char_ptr);
    end;
exit:
end;


@
The recursive procedure |quick_sort| sorts the entries indirectly
pointed to by the |sorted_cites| elements between |left_end| and
|right_end|, inclusive, based on the value of the |str_entry_var|
\.{sort.key\$}.  It's a fairly standard quicksort (for example, see
Algorithm 5.2.2Q in {\sl The Art of Computer Programming}), but uses
the median-of-three method to choose the partition element just in
case the entries are already sorted (or nearly sorted---humans and
ASCII might have different ideas on lexicographic ordering); it is a
stable sort.  This code generally prefers clarity to assembler-type
execution-time efficiency since |cite_list|s will rarely be huge.

The value |short_list|, which must be at least |2*end_offset + 2| for
this code to work, tells us the list-length at which the list is small
enough to warrant switching over to straight insertion sort from the
recursive quicksort.  The values here come from modest empirical tests
aimed at minimizing, for large |cite_list|s (five hundred or so), the
number of comparisons (between keys) plus the number of calls to
|quick_sort|.  The value |end_offset| must be positive; this helps
avoid $n^2$ behavior observed when the list starts out nearly, but not
completely, sorted (and fairly frequently large |cite_list|s come from
entire databases, which fairly frequently are nearly sorted).

@d short_list = 10      {use straight insertion sort at or below this length}
@d end_offset = 4       {the index end-offsets for choosing a median-of-three}

@<Check the ``constant'' values for consistency@>=
if (short_list < 2*end_offset + 2) then bad:=100*bad+22;


@
Here's the actual procedure.

@d next_insert = 24     {now insert the next element}

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure quick_sort (@!left_end,@!right_end : cite_number);
label next_insert;
var left,@!right : cite_number;         {two general |sorted_cites| pointers}
    @!insert_ptr : cite_number;         {the to-be-(straight)-inserted element}
    @!middle : cite_number;     {the |(left_end+right_end) div 2| element}
    @!partition : cite_number;          {the median-of-three partition element}
begin
  trace
  trace_pr_ln ('Sorting ',left_end:0,' through ',right_end:0);
  ecart@/
if (right_end - left_end < short_list) then
    @<Do a straight insertion sort@>
  else
    begin
    @<Draw out the median-of-three partition element@>;
    @<Do the partitioning and the recursive calls@>;
    end;
end;


@
This code sorts the entries between |left_end| and |right_end| when
the difference is less than |short_list|.  Each iteration of the outer
loop inserts the element indicated by |insert_ptr| into its proper
place among the (sorted) elements from |left_end| through
|insert_ptr-1|.

@<Do a straight insertion sort@>=
begin
for insert_ptr := left_end+1 to right_end do
    begin
    for right := insert_ptr downto left_end+1 do
        begin
        if (less_than (sorted_cites[right-1], sorted_cites[right])) then
            goto next_insert;
        swap (right-1, right);
        end;
next_insert:
    end;
end


@
Now we find the median of the three \.{sort.key\$}s to which the three
elements |sorted_cites[left_end+end_offset]|,
|sorted_cites[right_end]-end_offset|, and
|sorted_cites[(left_end+right_end) div 2]| point (a nonzero
|end_offset| avoids using as the leftmost of the three elements the
one that was swapped there when the old partition element was swapped
into its final spot; this turns out to avoid $n^2$ behavior when the
list is nearly sorted to start with).  This code determines which of
the six possible permutations we're dealing with and moves the median
element to |left_end|.  The comments next to the |swap| actions give
the known orderings of the corresponding elements of |sorted_cites|
before the action.

@<Draw out the median-of-three partition element@>=
begin
left := left_end + end_offset;
middle := (left_end+right_end) div 2;
right := right_end - end_offset;
if (less_than (sorted_cites[left], sorted_cites[middle])) then
  if (less_than (sorted_cites[middle], sorted_cites[right])) then
                                        {|left < middle < right|}
        swap(left_end,middle)
    else if (less_than (sorted_cites[left], sorted_cites[right])) then
                                        {|left < right < middle|}
        swap(left_end,right)
      else                              {|right < left < middle|}
        swap(left_end,left)
  else if (less_than (sorted_cites[right], sorted_cites[middle])) then
                                        {|right < middle < left|}
        swap(left_end,middle)
    else if (less_than (sorted_cites[right], sorted_cites[left])) then
                                        {|middle < right < left|}
        swap(left_end,right)
      else                              {|middle < left < right|}
        swap(left_end,left);
end


@
This module uses the median-of-three computed above to partition the
elements into those less than and those greater than the median.
Equal \.{sort.key\$}s are sorted by order of occurrence (in
|cite_list|).

@<Do the partitioning and the recursive calls@>=
begin
partition := sorted_cites[left_end];
left := left_end + 1;
right := right_end;
repeat
    while (less_than (sorted_cites[left], partition)) do
        incr(left);
    while (less_than (partition, sorted_cites[right])) do
        decr(right);
                {now |sorted_cites[right] < partition < sorted_cites[left]|}
    if (left < right) then
        begin
        swap (left,right);
        incr(left);
        decr(right);
        end;
until (left = right+1); {pointers have crossed}
swap (left_end,right);{restoring the partition element to its |right|ful place}
quick_sort (left_end,right-1);
quick_sort (left,right_end);
end


@
@:BibTeX capacity exceeded}{\quad literal-stack size@>
@:this can't happen}{\quad Unknown literal type@>
Ok, that's it for sorting; now we'll play with the literal stack.
This procedure pushes a literal onto the stack, checking for stack
overflow.

@<Procedures and functions for style-file function execution@>=
procedure push_lit_stk (@!push_lt:integer; @!push_type:stk_type);
  trace
  var dum_ptr : lit_stk_loc;    {used just as an index variable}
  ecart@/
begin
lit_stack[lit_stk_ptr] := push_lt;
lit_stk_type[lit_stk_ptr] := push_type;
  trace
  for dum_ptr := 0 to lit_stk_ptr do
    trace_pr ('  ');
  trace_pr ('Pushing ');
  case (lit_stk_type[lit_stk_ptr]) of
    stk_int : trace_pr_ln (lit_stack[lit_stk_ptr]:0);
    stk_str : begin
              trace_pr ('"');
              trace_pr_pool_str (lit_stack[lit_stk_ptr]);
              trace_pr_ln ('"');
              end;
    stk_fn : begin
             trace_pr ('`');
             trace_pr_pool_str (hash_text[lit_stack[lit_stk_ptr]]);
             trace_pr_ln ('''');
             end;
    stk_field_missing : begin
                        trace_pr ('missing field `');
                        trace_pr_pool_str (lit_stack[lit_stk_ptr]);
                        trace_pr_ln ('''');
                        end;
    stk_empty : trace_pr_ln ('a bad literal--popped from an empty stack');
    othercases unknwn_literal_confusion
  endcases;
  ecart@/
if (lit_stk_ptr = lit_stk_size) then
    overflow('literal-stack size ',lit_stk_size);
incr(lit_stk_ptr);
end;


@
@^push the literal stack@>
This macro pushes the last thing, necessarily a string, that was
popped.  And this module, along with others that push the literal
stack without explicitly calling |push_lit_stack|, have an index entry
under ``push the literal stack''; these implicit pushes collectively
speed up the program by about ten percent.

@d repush_string == begin
                    if (lit_stack[lit_stk_ptr] >= cmd_str_ptr) then
                        unflush_string;
                    incr(lit_stk_ptr);
                    end


@
@:this can't happen}{\quad Nontop top of string stack@>
This procedure pops the stack, checking for, and trying to recover
from, stack underflow.  (Actually, this procedure is really a
function, since it returns the two values through its |var|
parameters.)  Also, if the literal being popped is a |stk_str| that's
been created during the execution of the current \.{.bst} command, pop
it from |str_pool| as well (it will be the string corresponding to
|str_ptr-1|).  Note that when this happens, the string is no longer
`officially' available so that it must be used before anything else is
added to |str_pool|.

@<Procedures and functions for style-file function execution@>=
procedure pop_lit_stk (var pop_lit:integer; var pop_type:stk_type);
begin
if (lit_stk_ptr = 0) then
    begin
    bst_ex_warn ('You can''t pop an empty literal stack');@/
    pop_type := stk_empty;      {this is an error recovery attempt}
    end
  else
    begin
    decr(lit_stk_ptr);
    pop_lit := lit_stack[lit_stk_ptr];
    pop_type := lit_stk_type[lit_stk_ptr];
    if (pop_type = stk_str) then
      if (pop_lit >= cmd_str_ptr) then
        begin
        if (pop_lit <> str_ptr-1) then
            confusion ('Nontop top of string stack');
        flush_string;
        end;
    end;
end;


@
@:this can't happen}{\quad Illegal literal type@>
@:this can't happen}{\quad Unknown literal type@>
More bug complaints, this time about bad literals.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure illegl_literal_confusion;
begin
confusion ('Illegal literal type');
end;
@#
procedure unknwn_literal_confusion;
begin
confusion ('Unknown literal type');
end;


@
@:this can't happen}{\quad Illegal literal type@>
@:this can't happen}{\quad Unknown literal type@>
Occasionally we'll want to know what's on the literal stack.  Here we
print out a stack literal, giving its type.  This procedure should
never be called after popping an empty stack.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_stk_lit (@!stk_lt:integer; @!stk_tp:stk_type);
begin
case (stk_tp) of
    stk_int : print (stk_lt:0,' is an integer literal');
    stk_str : begin
              print ('"');
              print_pool_str (stk_lt);
              print ('" is a string literal');
              end;
    stk_fn : begin
             print ('`');
             print_pool_str (hash_text[stk_lt]);
             print (''' is a function literal');
             end;
    stk_field_missing : begin
                        print ('`');
                        print_pool_str (stk_lt);
                        print (''' is a missing field');
                        end;
    stk_empty : illegl_literal_confusion;
    othercases unknwn_literal_confusion
endcases;
end;


@
@:this can't happen}{\quad Illegal literal type@>
@:this can't happen}{\quad Unknown literal type@>
This procedure appropriately chastises the style designer; however, if
the wrong literal came from popping an empty stack, the procedure
|pop_lit_stack| will have already done the chastising (because this
procedure is called only after popping the stack) so there's no need
for more.

@<Procedures and functions for style-file function execution@>=
procedure print_wrong_stk_lit (@!stk_lt:integer; @!stk_tp1,@!stk_tp2:stk_type);
begin
if (stk_tp1 <> stk_empty) then
    begin
    print_stk_lit (stk_lt, stk_tp1);
    case (stk_tp2) of
        stk_int : print (', not an integer,');
        stk_str : print (', not a string,');
        stk_fn : print (', not a function,');
        stk_field_missing,
        stk_empty : illegl_literal_confusion;
        othercases unknwn_literal_confusion
    endcases;
    bst_ex_warn_print;
    end;
end;


@
@:this can't happen}{\quad Illegal literal type@>
@:this can't happen}{\quad Unknown literal type@>
This is similar to |print_stk_lit|, but here we don't give the
literal's type, and here we end with a new line.  This procedure
should never be called after popping an empty stack.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure print_lit (@!stk_lt:integer; @!stk_tp:stk_type);
begin
case (stk_tp) of
    stk_int : print_ln (stk_lt:0);
    stk_str : begin
              print_pool_str (stk_lt);
              print_newline;
              end;
    stk_fn : begin
             print_pool_str (hash_text[stk_lt]);
             print_newline;
             end;
    stk_field_missing : begin
                        print_pool_str (stk_lt);
                        print_newline;
                        end;
    stk_empty : illegl_literal_confusion;
    othercases unknwn_literal_confusion
endcases;
end;


@
This procedure pops and prints the top of the stack; when the stack is
empty the procedure |pop_lit_stk| complains.

@<Procedures and functions for style-file function execution@>=
procedure pop_top_and_print;
var stk_lt : integer;
  @!stk_tp : stk_type;
begin
pop_lit_stk (stk_lt,stk_tp);
if (stk_tp = stk_empty) then
    print_ln ('Empty literal')
  else
    print_lit (stk_lt,stk_tp);
end;


@
This procedure pops and prints the whole stack.

@<Procedures and functions for style-file function execution@>=
procedure pop_whole_stack;
begin
while (lit_stk_ptr > 0) do
    pop_top_and_print;
end;


@
At the beginning of a \.{.bst}-command execution we make the stack
empty and record how much of |str_pool| has been used.

@<Procedures and functions for style-file function execution@>=
procedure init_command_execution;
begin
lit_stk_ptr := 0;       {make the stack empty}
cmd_str_ptr := str_ptr; {we'll check this when we finish command execution}
end;


@
@:this can't happen}{\quad Nonempty empty string stack@>
At the end of a \.{.bst} command-execution we check that the stack and
|str_pool| are still in good shape.

@<Procedures and functions for style-file function execution@>=
procedure check_command_execution;
begin
if (lit_stk_ptr<>0) then
    begin
    print_ln ('ptr=',lit_stk_ptr:0,', stack=');
    pop_whole_stack;
    bst_ex_warn ('---the literal stack isn''t empty');
    end;
if (cmd_str_ptr<>str_ptr) then
    begin
      trace
      print_ln ('Pointer is ',str_ptr:0,' but should be ',cmd_str_ptr:0);
      ecart@/
    confusion ('Nonempty empty string stack');
    end;
end;


@
This procedure adds to |str_pool| the string from |ex_buf[0]| through
|ex_buf[ex_buf_length-1]| if it will fit.  It assumes the global
variable |ex_buf_length| gives the length of the current string in
|ex_buf|.  It then pushes this string onto the literal stack.

@<Procedures and functions for style-file function execution@>=
procedure add_pool_buf_and_push;
begin
str_room (ex_buf_length);               {make sure this string will fit}
ex_buf_ptr := 0;
while (ex_buf_ptr < ex_buf_length) do
    begin
    append_char (ex_buf[ex_buf_ptr]);
    incr(ex_buf_ptr);
    end;
push_lit_stk (make_string, stk_str);    {and push it onto the stack}
end;


@
@:BibTeX capacity exceeded}{\quad buffer size@>
These macros append a character to |ex_buf|.  Which is called depends
on whether the character is known to fit.

@d append_ex_buf_char(#) == begin
                            ex_buf[ex_buf_ptr] := #;
                            incr(ex_buf_ptr);
                            end
@#
@d append_ex_buf_char_and_check(#) ==
                            begin
                            if (ex_buf_ptr = buf_size) then
                                buffer_overflow;
                            append_ex_buf_char(#);
                            end


@
@:BibTeX capacity exceeded}{\quad buffer size@>
This procedure adds to the execution buffer the given string in
|str_pool| if it will fit.  It assumes the global variable
|ex_buf_length| gives the length of the current string in |ex_buf|,
and thus also gives the location of the next character.

@<Procedures and functions for style-file function execution@>=
procedure add_buf_pool (@!p_str : str_number);
begin
p_ptr1 := str_start[p_str];
p_ptr2 := str_start[p_str+1];
if (ex_buf_length+(p_ptr2-p_ptr1) > buf_size) then
    buffer_overflow;
ex_buf_ptr := ex_buf_length;
while (p_ptr1 < p_ptr2) do
    begin                       {copy characters into the buffer}
    append_ex_buf_char (str_pool[p_ptr1]);
    incr(p_ptr1);
    end;
ex_buf_length := ex_buf_ptr;
end;


@
This procedure actually writes onto the \.{.bbl}~file a line of output
(the characters from |out_buf[0]| to |out_buf[out_buf_length-1]|,
after removing trailing |white_space| characters).  It also updates
|bbl_line_num|, the line counter.  It writes a blank line if and only
if |out_buf| is empty.  The program uses this procedure in such a way
that |out_buf| will be nonempty if there have been characters put in
it since the most recent \.{newline\$}.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure output_bbl_line;
label loop_exit,@!exit;
begin
if (out_buf_length <> 0) then           {the buffer's not empty}
    begin
    while (out_buf_length > 0) do       {remove trailing |white_space|}
      if (lex_class[out_buf[out_buf_length-1]] = white_space) then
        decr(out_buf_length)
       else
        goto loop_exit;
loop_exit:
    if (out_buf_length = 0) then        {ignore a line of just |white_space|}
        return;
    out_buf_ptr := 0;
    while (out_buf_ptr < out_buf_length) do
        begin
        write (bbl_file, xchr[out_buf[out_buf_ptr]]);
        incr(out_buf_ptr);
        end;
    end;
write_ln (bbl_file);
incr(bbl_line_num);     {update line number}
out_buf_length := 0;    {make the next line empty}
exit:
end;


@
@:BibTeX capacity exceeded}{\quad output buffer size@>
This procedure adds to the output buffer the given string in
|str_pool|.  It assumes the global variable |out_buf_length| gives the
length of the current string in |out_buf|, and thus also gives the
location for the next character.  If there are enough characters
present in the output buffer, it writes one or more lines out to the
\.{.bbl} file.  It breaks a line only at a |white_space| character,
and when it does, it adds two |space|s to the next output line.

@<Procedures and functions for style-file function execution@>=
procedure add_out_pool (@!p_str : str_number);
label loop1_exit,loop2_exit;
var break_ptr : buf_pointer;    {the first character following the line break}
@!end_ptr : buf_pointer;        {temporary end-of-buffer pointer}
break_pt_found : boolean;       {a suitable |white_space| character}
unbreakable_tail : boolean;     {as it contains no |white_space| character}
begin
p_ptr1 := str_start[p_str];
p_ptr2 := str_start[p_str+1];
if (out_buf_length+(p_ptr2-p_ptr1) > buf_size) then
    overflow('output buffer size ',buf_size);
out_buf_ptr := out_buf_length;
while (p_ptr1 < p_ptr2) do
    begin                       {copy characters into the buffer}
    out_buf[out_buf_ptr] := str_pool[p_ptr1];
    incr(p_ptr1);
    incr(out_buf_ptr);
    end;
out_buf_length := out_buf_ptr;
unbreakable_tail := false;
while ((out_buf_length > max_print_line) and (not unbreakable_tail)) do
    @<Break that line@>;
end;


@
Here we break the line by looking for a |white_space| character,
backwards from |out_buf[max_print_line]| until
|out_buf[min_print_line]|; we break at the |white_space| and indent
the next line two |space|s.  The next module handles things when
there's no |white_space| character to break at.  (It seems that the
annoyances to the average user of a warning message when there's an
output line longer than |max_print_line| outweigh the benefits, so we
don't issue such warnings in the current code.)

@<Break that line@>=
begin
end_ptr := out_buf_length;
out_buf_ptr := max_print_line;
break_pt_found := false;
while ((lex_class[out_buf[out_buf_ptr]] <> white_space) and
                                        (out_buf_ptr >= min_print_line)) do
    decr(out_buf_ptr);
if (out_buf_ptr = min_print_line-1) then        {no |white_space| character}
    @<Break that unbreakably long line@>        {(if |white_space| follows)}
else
    break_pt_found := true;                     {hit a |white_space| character}
if (break_pt_found) then
    begin
    out_buf_length := out_buf_ptr;
    break_ptr := out_buf_length + 1;
    output_bbl_line;                    {output what we can}
    out_buf[0] := space;
    out_buf[1] := space;                {start the next line with two |space|s}
    out_buf_ptr := 2;
    tmp_ptr := break_ptr;
    while (tmp_ptr < end_ptr) do        {and slide the rest down}
        begin
        out_buf[out_buf_ptr] := out_buf[tmp_ptr];
        incr(out_buf_ptr);
        incr(tmp_ptr);
        end;
    out_buf_length := end_ptr - break_ptr + 2;
    end;
end


@
If there's no |white_space| character up through
|out_buf[max_print_line]|, we instead break the line at the first
following |white_space| character, if one exists.  And if, starting
with that |white_space| character, there are multiple consecutive
|white_space| characters, |out_buf_ptr| points to the last of them.
If no |white_space| character exists, we haven't found a viable break
point, so we don't break the line (yet).

@<Break that unbreakably long line@>=
begin
out_buf_ptr := max_print_line + 1;      {|break_pt_found| is still |false|}
while (out_buf_ptr < end_ptr) do
    if (lex_class[out_buf[out_buf_ptr]] <> white_space) then
        incr(out_buf_ptr)
    else
        goto loop1_exit;
loop1_exit:
if (out_buf_ptr = end_ptr) then
    unbreakable_tail := true            {because no |white_space| character}
else                            {at |white_space|, and |out_buf_ptr < end_ptr|}
    begin
    break_pt_found := true;
    while (out_buf_ptr+1 < end_ptr) do  {look for more |white_space|}
        if (lex_class[out_buf[out_buf_ptr+1]] = white_space) then
            incr(out_buf_ptr)           {which then points to |white_space|}
        else
            goto loop2_exit;
loop2_exit:
    end;
end


@
@^Tuesdays@>
@^windows@>
@:this can't happen}{\quad Unknown function class@>
This procedure executes a single specified function; it is the single
execution-primitive that does everything (except windows, and it takes
Tuesdays off).

@<|execute_fn| itself@>=
procedure execute_fn (@!ex_fn_loc : hash_loc);
@<Declarations for executing |built_in| functions@>
@!wiz_ptr : wiz_fn_loc;         {general |wiz_functions| location}
begin
  trace
  trace_pr ('execute_fn `');
  trace_pr_pool_str (hash_text[ex_fn_loc]);
  trace_pr_ln ('''');
  ecart@/
case (fn_type[ex_fn_loc]) of
    built_in : @<Execute a |built_in| function@>;
    wiz_defined : @<Execute a |wiz_defined| function@>;
    int_literal : push_lit_stk (fn_info[ex_fn_loc], stk_int);
    str_literal : push_lit_stk (hash_text[ex_fn_loc], stk_str);
    field : @<Execute a field@>;
    int_entry_var : @<Execute an |int_entry_var|@>;
    str_entry_var : @<Execute a |str_entry_var|@>;
    int_global_var : push_lit_stk (fn_info[ex_fn_loc], stk_int);
    str_global_var : @<Execute a |str_global_var|@>;
    othercases unknwn_function_class_confusion
endcases;
end;


@
To execute a |wiz_defined| function, we just execute all those
functions in its definition, except that the special marker
|quote_next_fn| means we push the next function onto the stack.

@<Execute a |wiz_defined| function@>=
begin
wiz_ptr := fn_info[ex_fn_loc];
while (wiz_functions[wiz_ptr] <> end_of_def) do
    begin
    if (wiz_functions[wiz_ptr] <> quote_next_fn) then
        execute_fn (wiz_functions[wiz_ptr])
      else
        begin
        incr(wiz_ptr);
        push_lit_stk (wiz_functions[wiz_ptr], stk_fn);
        end;
    incr(wiz_ptr);
    end;
end


@
This module pushes the string given by the field onto the literal
stack unless it's |missing|, in which case it pushes a special value
onto the stack.

@<Execute a field@>=
begin
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
  else
    begin
    field_ptr := cite_ptr*num_fields + fn_info[ex_fn_loc];
    if (field_info[field_ptr] = missing) then
        push_lit_stk (hash_text[ex_fn_loc], stk_field_missing)
      else
        push_lit_stk (field_info[field_ptr], stk_str);
    end
end


@
This module pushes the integer given by an |int_entry_var| onto the
literal stack.

@<Execute an |int_entry_var|@>=
begin
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
  else
    push_lit_stk (entry_ints[cite_ptr*num_ent_ints+fn_info[ex_fn_loc]],
                                                                stk_int);
end


@
This module adds the string given by a |str_entry_var| to |str_pool|
via the execution buffer and pushes it onto the literal stack.

@<Execute a |str_entry_var|@>=
begin
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
  else
    begin
    str_ent_ptr := cite_ptr*num_ent_strs + fn_info[ex_fn_loc];@/
    ex_buf_ptr := 0;                    {also serves as |ent_chr_ptr|}
    while (entry_strs[str_ent_ptr][ex_buf_ptr] <> end_of_string) do
                                        {copy characters into the buffer}
        append_ex_buf_char (entry_strs[str_ent_ptr][ex_buf_ptr]);
    ex_buf_length := ex_buf_ptr;
    add_pool_buf_and_push;              {push this string onto the stack}
    end;
end


@
This module pushes the string given by a |str_global_var| onto the
literal stack, but it copies the string to |str_pool| (character by
character) only if it has to---it {\it doesn't\/} have to if the
string is static (that is, if the string isn't at the top, temporary
part of the string pool).

@<Execute a |str_global_var|@>=
begin
str_glb_ptr := fn_info[ex_fn_loc];
if (glb_str_ptr[str_glb_ptr] > 0) then  {we're dealing with a static string}
    push_lit_stk (glb_str_ptr[str_glb_ptr],stk_str)
  else
    begin
    str_room(glb_str_end[str_glb_ptr]);
    glob_chr_ptr := 0;
    while (glob_chr_ptr < glb_str_end[str_glb_ptr]) do  {copy the string}
        begin
        append_char (global_strs[str_glb_ptr][glob_chr_ptr]);
        incr(glob_chr_ptr);
        end;
    push_lit_stk (make_string, stk_str);        {and push it onto the stack}
    end;
end



@* The built-in functions.
@^add a built-in function@>
@^biblical procreation@>
@^grade inflation@>
This section gives the all the code for all the built-in functions
(including pre-defined |field|s, |str_entry_var|s, and
|int_global_var|s, which technically aren't classified as |built_in|).
To modify or add one, we needn't go anywhere else (with one exception:
The constant |max_pop|, which gives the maximum number of literals
that any of these functions pops off the stack, is defined earlier
because it's needed earlier; thus, if we need to update it, which will
happen if some new |built_in| functions uses more than |max_pop|
literals from the stack, we'll have to go outside this section).
Adding a |built_in| function entails modifying (at least four of) the
five modules marked by ``add a built-in function'' in the index, in
addition to adding the code to execute the function.

These variables all begin with |b_| and specify the hash-table
locations of the |built_in| functions, except that |b_default| is
pseudo-|built_in|---either it will point to the no-op \.{skip\$} or to
the \.{.bst}-defined function \.{default.type}; it's used when an
entry has a type that's not defined in the \.{.bst} file.

@<Globals in the outer block@>=
@!b_equals : hash_loc;          {\.{=}}
@!b_greater_than : hash_loc;    {\.{>}}
@!b_less_than : hash_loc;       {\.{<}}
@!b_plus : hash_loc;            {\.{+} (this may be changed to an |a_minus|)}
@!b_minus : hash_loc;           {\.{-}}
@!b_concatenate : hash_loc;     {\.{*}}
@!b_gets : hash_loc;            {\.{:=} (formerly, |b_gat|)}
@!b_add_period : hash_loc;      {\.{add.period\$}}
@!b_call_type : hash_loc;       {\.{call.type\$}}
@!b_change_case : hash_loc;     {\.{change.case\$}}
@!b_chr_to_int : hash_loc;      {\.{chr.to.int\$}}
@!b_cite : hash_loc;            {\.{cite\$}}
@!b_duplicate : hash_loc;       {\.{duplicate\$}}
@!b_empty : hash_loc;           {\.{empty\$}}
@!b_format_name : hash_loc;     {\.{format.name\$}}
@!b_if : hash_loc;              {\.{if\$}}
@!b_int_to_chr : hash_loc;      {\.{int.to.chr\$}}
@!b_int_to_str : hash_loc;      {\.{int.to.str\$}}
@!b_missing : hash_loc;         {\.{missing\$}}
@!b_newline : hash_loc;         {\.{newline\$}}
@!b_num_names : hash_loc;       {\.{num.names\$}}
@!b_pop : hash_loc;             {\.{pop\$}}
@!b_preamble : hash_loc;        {\.{preamble\$}}
@!b_purify : hash_loc;          {\.{purify\$}}
@!b_quote : hash_loc;           {\.{quote\$}}
@!b_skip : hash_loc;            {\.{skip\$}}
@!b_stack : hash_loc;           {\.{stack\$}}
@!b_substring : hash_loc;       {\.{substring\$}}
@!b_swap : hash_loc;            {\.{swap\$}}
@!b_text_length : hash_loc;     {\.{text.length\$}}
@!b_text_prefix : hash_loc;     {\.{text.prefix\$}}
@!b_top_stack : hash_loc;       {\.{top\$}}
@!b_type : hash_loc;            {\.{type\$}}
@!b_warning : hash_loc;         {\.{warning\$}}
@!b_while : hash_loc;           {\.{while\$}}
@!b_width : hash_loc;           {\.{width\$}}
@!b_write : hash_loc;           {\.{write\$}}
@!b_default : hash_loc;         {either \.{skip\$} or \.{default.type}}
@#
  stat
  @!blt_in_loc : array[blt_in_range] of hash_loc; {for execution counts}
  @!execution_count : array[blt_in_range] of integer; {the same}
  @!total_ex_count : integer;           {the sum of all |execution_count|s}
  @!blt_in_ptr : blt_in_range;          {a pointer into |blt_in_loc|}
  tats@/


@
Where |blt_in_range| gives the legal |built_in| function numbers.

@<Types in the outer block@>=
@!blt_in_range = 0..num_blt_in_fns;


@
@^add a built-in function@>
These constants all begin with |n_| and are used for the |case|
statement that determines which |built_in| function to execute.

@d n_equals = 0         {\.{=}}
@d n_greater_than = 1   {\.{>}}
@d n_less_than = 2      {\.{<}}
@d n_plus = 3           {\.{+}}
@d n_minus = 4          {\.{-}}
@d n_concatenate = 5    {\.{*}}
@d n_gets = 6           {\.{:=}}
@d n_add_period = 7     {\.{add.period\$}}
@d n_call_type = 8      {\.{call.type\$}}
@d n_change_case = 9    {\.{change.case\$}}
@d n_chr_to_int = 10    {\.{chr.to.int\$}}
@d n_cite = 11          {\.{cite\$} (this may start a riot)}
@d n_duplicate = 12     {\.{duplicate\$}}
@d n_empty = 13         {\.{empty\$}}
@d n_format_name = 14   {\.{format.name\$}}
@d n_if = 15            {\.{if\$}}
@d n_int_to_chr = 16    {\.{int.to.chr\$}}
@d n_int_to_str = 17    {\.{int.to.str\$}}
@d n_missing = 18       {\.{missing\$}}
@d n_newline = 19       {\.{newline\$}}
@d n_num_names = 20     {\.{num.names\$}}
@d n_pop = 21           {\.{pop\$}}
@d n_preamble = 22      {\.{preamble\$}}
@d n_purify = 23        {\.{purify\$}}
@d n_quote = 24         {\.{quote\$}}
@d n_skip = 25          {\.{skip\$}}
@d n_stack = 26         {\.{stack\$}}
@d n_substring = 27     {\.{substring\$}}
@d n_swap = 28          {\.{swap\$}}
@d n_text_length = 29   {\.{text.length\$}}
@d n_text_prefix = 30   {\.{text.prefix\$}}
@d n_top_stack = 31     {\.{top\$}}
@d n_type = 32          {\.{type\$}}
@d n_warning = 33       {\.{warning\$}}
@d n_while = 34         {\.{while\$}}
@d n_width = 35         {\.{width\$}}
@d n_write = 36         {\.{write\$}}

@<Constants in the outer block@>=
@!num_blt_in_fns = 37;  {one more than the previous number}


@
@^add a built-in function@>
@^important note@>
It's time for us to insert more pre-defined strings into |str_pool|
(and thus the hash table) and to insert the |built_in| functions into
the hash table.  The strings corresponding to these functions should
contain no upper-case letters, and they must all be exactly
|longest_pds| characters long.  The |build_in| routine (to appear
shortly) does the work.

Important note: These pre-definitions must not have any glitches or the
program may bomb because the |log_file| hasn't been opened yet.

@<Pre-define certain strings@>=
build_in('=           ',1,b_equals,n_equals);
build_in('>           ',1,b_greater_than,n_greater_than);
build_in('<           ',1,b_less_than,n_less_than);
build_in('+           ',1,b_plus,n_plus);
build_in('-           ',1,b_minus,n_minus);
build_in('*           ',1,b_concatenate,n_concatenate);
build_in(':=          ',2,b_gets,n_gets);
build_in('add.period$ ',11,b_add_period,n_add_period);
build_in('call.type$  ',10,b_call_type,n_call_type);
build_in('change.case$',12,b_change_case,n_change_case);
build_in('chr.to.int$ ',11,b_chr_to_int,n_chr_to_int);
build_in('cite$       ',5,b_cite,n_cite);
build_in('duplicate$  ',10,b_duplicate,n_duplicate);
build_in('empty$      ',6,b_empty,n_empty);
build_in('format.name$',12,b_format_name,n_format_name);
build_in('if$         ',3,b_if,n_if);
build_in('int.to.chr$ ',11,b_int_to_chr,n_int_to_chr);
build_in('int.to.str$ ',11,b_int_to_str,n_int_to_str);
build_in('missing$    ',8,b_missing,n_missing);
build_in('newline$    ',8,b_newline,n_newline);
build_in('num.names$  ',10,b_num_names,n_num_names);
build_in('pop$        ',4,b_pop,n_pop);
build_in('preamble$   ',9,b_preamble,n_preamble);
build_in('purify$     ',7,b_purify,n_purify);
build_in('quote$      ',6,b_quote,n_quote);
build_in('skip$       ',5,b_skip,n_skip);
build_in('stack$      ',6,b_stack,n_stack);
build_in('substring$  ',10,b_substring,n_substring);
build_in('swap$       ',5,b_swap,n_swap);
build_in('text.length$',12,b_text_length,n_text_length);
build_in('text.prefix$',12,b_text_prefix,n_text_prefix);
build_in('top$        ',4,b_top_stack,n_top_stack);
build_in('type$       ',5,b_type,n_type);
build_in('warning$    ',8,b_warning,n_warning);
build_in('width$      ',6,b_width,n_width);
build_in('while$      ',6,b_while,n_while);
build_in('width$      ',6,b_width,n_width);
build_in('write$      ',6,b_write,n_write);


@
This procedure inserts a |built_in| function into the hash table and
initializes the corresponding pre-defined string (of length at most
|longest_pds|).  The array |fn_info| contains a number from 0 through
the number of |built_in| functions minus 1 (i.e., |num_blt_in_fns - 1|
if we're keeping statistics); this number is used by a |case|
statement to execute this function and is used for keeping execution
counts when keeping statistics.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure build_in (@!pds:pds_type; @!len:pds_len; var fn_hash_loc:hash_loc;
                                        @!blt_in_num:blt_in_range);
begin
pre_define (pds,len,bst_fn_ilk);@/
fn_hash_loc := pre_def_loc;     {the |pre_define| routine sets |pre_def_loc|}
fn_type[fn_hash_loc] := built_in;
fn_info[fn_hash_loc] := blt_in_num;
  stat
  blt_in_loc[blt_in_num] := fn_hash_loc;@/
  execution_count[blt_in_num] := 0; {initialize the function-execution count}
  tats@/
end;


@
This is a procedure so that |initialize| is smaller.

@<Procedures and functions for handling numbers, characters, and strings@>=
procedure pre_def_certain_strings;
begin
@<Pre-define certain strings@>@;
end;


@
These variables all begin with |s_| and specify the locations in
|str_pool| of certain often-used strings that the \.{.bst} commands
need.  The |s_preamble| array is big enough to allow an average of one
\.{preamble\$} command per \.{.bib} file.

@<Globals in the outer block@>=
@!s_null : str_number;          {the null string}
@!s_default : str_number;       {\.{default.type}, for unknown entry types}
@!s_t : str_number;             {\.{t}, for |title_lowers| case conversion}
@!s_l : str_number;             {\.{l}, for |all_lowers| case conversion}
@!s_u : str_number;             {\.{u}, for |all_uppers| case conversion}
@!s_preamble : array[bib_number] of str_number;
                                {for the \.{preamble\$} |built_in| function}


@
These constants all begin with |n_| and are used for the |case|
statement that determines which, if any, control sequence we're
dealing with; a control sequence of interest will be either one of the
undotted characters `\.{\\i}' or `\.{\\j}' or one of the foreign
characters in Table~3.2 of the \LaTeX\ manual.

@d n_i = 0              {\.{i}, for the undotted character \.{\\i}}
@d n_j = 1              {\.{j}, for the undotted character \.{\\j}}
@d n_oe = 2             {\.{oe}, for the foreign character \.{\\oe}}
@d n_oe_upper = 3       {\.{OE}, for the foreign character \.{\\OE}}
@d n_ae = 4             {\.{ae}, for the foreign character \.{\\ae}}
@d n_ae_upper = 5       {\.{AE}, for the foreign character \.{\\AE}}
@d n_aa = 6             {\.{aa}, for the foreign character \.{\\aa}}
@d n_aa_upper = 7       {\.{AA}, for the foreign character \.{\\AA}}
@d n_o = 8              {\.{o}, for the foreign character \.{\\o}}
@d n_o_upper = 9        {\.{O}, for the foreign character \.{\\O}}
@d n_l = 10             {\.{l}, for the foreign character \.{\\l}}
@d n_l_upper = 11       {\.{L}, for the foreign character \.{\\L}}
@d n_ss = 12            {\.{ss}, for the foreign character \.{\\ss}}


@
@^important note@>
@.default.type@>
Here we pre-define a few strings used in executing the \.{.bst} file:
the null string, which is sometimes pushed onto the stack; a string
used for default entry types; and some control sequences used to spot
foreign characters.  We also initialize the |s_preamble| array to
empty.  These pre-defined strings must all be exactly |longest_pds|
characters long.

Important note: These pre-definitions must not have any glitches or
the program may bomb because the |log_file| hasn't been opened yet,
and |text_ilk|s should be pre-defined here, not earlier, for
\.{.bst}-function-execution purposes.

@<Pre-define certain strings@>=
pre_define('            ',0,text_ilk);  s_null := hash_text[pre_def_loc];
fn_type[pre_def_loc] := str_literal;@/
pre_define('default.type',12,text_ilk); s_default := hash_text[pre_def_loc];
fn_type[pre_def_loc] := str_literal;@/
b_default := b_skip;    {this may be changed to the \.{default.type} function}
preamble_ptr := 0;                      {initialize the |s_preamble| array}
pre_define('i           ',1,control_seq_ilk);
ilk_info[pre_def_loc] := n_i;
pre_define('j           ',1,control_seq_ilk);
ilk_info[pre_def_loc] := n_j;
pre_define('oe          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_oe;
pre_define('OE          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_oe_upper;
pre_define('ae          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_ae;
pre_define('AE          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_ae_upper;
pre_define('aa          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_aa;
pre_define('AA          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_aa_upper;
pre_define('o           ',1,control_seq_ilk);
ilk_info[pre_def_loc] := n_o;
pre_define('O           ',1,control_seq_ilk);
ilk_info[pre_def_loc] := n_o_upper;
pre_define('l           ',1,control_seq_ilk);
ilk_info[pre_def_loc] := n_l;
pre_define('L           ',1,control_seq_ilk);
ilk_info[pre_def_loc] := n_l_upper;
pre_define('ss          ',2,control_seq_ilk);
ilk_info[pre_def_loc] := n_ss;


@
@^important note@>
@.crossref@>
@.entry.max\$@>
@.global.max\$@>
@.sort.key\$@>
Now we pre-define any built-in |field|s, |str_entry_var|s, and
|int_global_var|s; these strings must all be exactly |longest_pds|
characters long.  Note that although these are built-in functions, we
classify them (in the |fn_type| array) otherwise.

Important note: These pre-definitions must not have any glitches or
the program may bomb because the |log_file| hasn't been opened yet.

@<Pre-define certain strings@>=
pre_define('crossref    ',8,bst_fn_ilk);
fn_type[pre_def_loc] := field;@/
fn_info[pre_def_loc] := num_fields;     {give this |field| a number}
crossref_num := num_fields;
incr(num_fields);@/
num_pre_defined_fields := num_fields;   {that's it for pre-defined |field|s}
pre_define('sort.key$   ',9,bst_fn_ilk);
fn_type[pre_def_loc] := str_entry_var;
fn_info[pre_def_loc] := num_ent_strs;   {give this |str_entry_var| a number}
sort_key_num := num_ent_strs;
incr(num_ent_strs);@/
pre_define('entry.max$  ',10,bst_fn_ilk);
fn_type[pre_def_loc] := int_global_var;
fn_info[pre_def_loc] := ent_str_size;   {initialize this |int_global_var|}
pre_define('global.max$ ',11,bst_fn_ilk);
fn_type[pre_def_loc] := int_global_var;
fn_info[pre_def_loc] := glob_str_size;  {initialize this |int_global_var|}


@
@^add a built-in function@>
@:this can't happen}{\quad Unknown built-in function@>
This module branches to the code for the appropriate |built_in|
function.  Only three---{\.{call.type\$}}, {\.{if\$}}, and
{\.{while\$}}---do a recursive call.

@<Execute a |built_in| function@>=
begin
  stat          {update this function's execution count}
  incr(execution_count[fn_info[ex_fn_loc]]);
  tats@/
case (fn_info[ex_fn_loc]) of
    n_equals :          x_equals;
    n_greater_than :    x_greater_than;
    n_less_than :       x_less_than;
    n_plus :            x_plus;
    n_minus :           x_minus;
    n_concatenate :     x_concatenate;
    n_gets :            x_gets;
    n_add_period :      x_add_period;
    n_call_type :       @<|execute_fn|({\.{call.type\$}})@>;
    n_change_case :     x_change_case;
    n_chr_to_int :      x_chr_to_int;
    n_cite :            x_cite;
    n_duplicate :       x_duplicate;
    n_empty :           x_empty;
    n_format_name :     x_format_name;
    n_if :              @<|execute_fn|({\.{if\$}})@>;
    n_int_to_chr :      x_int_to_chr;
    n_int_to_str :      x_int_to_str;
    n_missing :         x_missing;
    n_newline :         @<|execute_fn|({\.{newline\$}})@>;
    n_num_names :       x_num_names;
    n_pop :             @<|execute_fn|({\.{pop\$}})@>;
    n_preamble :        x_preamble;
    n_purify :          x_purify;
    n_quote :           x_quote;
    n_skip :            @<|execute_fn|({\.{skip\$}})@>;
    n_stack :           @<|execute_fn|({\.{stack\$}})@>;
    n_substring :       x_substring;
    n_swap :            x_swap;
    n_text_length :     x_text_length;
    n_text_prefix :     x_text_prefix;
    n_top_stack :       @<|execute_fn|({\.{top\$}})@>;
    n_type :            x_type;
    n_warning :         x_warning;
    n_while :           @<|execute_fn|({\.{while\$}})@>;
    n_width :           x_width;
    n_write :           x_write;
    othercases confusion ('Unknown built-in function')
endcases;
end


@
@^add a built-in function@>
@^gymnastics@>
This extra level of module-pointing allows a uniformity of module
names for the |built_in| functions, regardless of whether they do a
recursive call to |execute_fn| or are trivial (a single statement).
Those that do a recursive call are left as part of |execute_fn|,
avoiding \PASCAL's forward procedure mechanism, and those that don't
(except for the single-statement ones) are made into procedures so
that |execute_fn| doesn't get too large.

@<Procedures and functions for style-file function execution@>=
@<|execute_fn|({\.{=}})@>@;
@<|execute_fn|({\.{>}})@>@;
@<|execute_fn|({\.{<}})@>@;
@<|execute_fn|({\.{+}})@>@;
@<|execute_fn|({\.{-}})@>@;
@<|execute_fn|({\.{*}})@>@;
@<|execute_fn|({\.{:=}})@>@;
@<|execute_fn|({\.{add.period\$}})@>@;
@<|execute_fn|({\.{change.case\$}})@>@;
@<|execute_fn|({\.{chr.to.int\$}})@>@;
@<|execute_fn|({\.{cite\$}})@>@;
@<|execute_fn|({\.{duplicate\$}})@>@;
@<|execute_fn|({\.{empty\$}})@>@;
@<|execute_fn|({\.{format.name\$}})@>@;
@<|execute_fn|({\.{int.to.chr\$}})@>@;
@<|execute_fn|({\.{int.to.str\$}})@>@;
@<|execute_fn|({\.{missing\$}})@>@;
@<|execute_fn|({\.{num.names\$}})@>@;
@<|execute_fn|({\.{preamble\$}})@>@;
@<|execute_fn|({\.{purify\$}})@>@;
@<|execute_fn|({\.{quote\$}})@>@;
@<|execute_fn|({\.{substring\$}})@>@;
@<|execute_fn|({\.{swap\$}})@>@;
@<|execute_fn|({\.{text.length\$}})@>@;
@<|execute_fn|({\.{text.prefix\$}})@>@;
@<|execute_fn|({\.{type\$}})@>@;
@<|execute_fn|({\.{warning\$}})@>@;
@<|execute_fn|({\.{width\$}})@>@;
@<|execute_fn|({\.{write\$}})@>@;
@<|execute_fn| itself@>


@
Now it's time to declare some things for executing |built_in|
functions only.  These (and only these) variables are used
recursively, so they can't be global.

@d end_while = 51       {stop executing the \.{while\$} function}

@<Declarations for executing |built_in| functions@>=
label end_while;
var r_pop_lt1,@!r_pop_lt2 : integer;    {stack literals for \.{while\$}}
@!r_pop_tp1,@!r_pop_tp2 : stk_type;     {stack types for \.{while\$}}


@
These are nonrecursive variables that |execute_fn| uses.  Declaring
them here (instead of in the previous module) saves execution time and
stack space on most machines.

@d name_buf == sv_buffer        {an alias, a buffer for manipulating names}

@<Globals in the outer block@>=
@!pop_lit1,@!pop_lit2,@!pop_lit3 : integer;     {stack literals}
@!pop_typ1,@!pop_typ2,@!pop_typ3 : stk_type;    {stack types}
@!sp_ptr : pool_pointer;                {for manipulating |str_pool| strings}
@!sp_xptr1,@!sp_xptr2 : pool_pointer;   {more of the same}
@!sp_end : pool_pointer;                {marks the end of a |str_pool| string}
@!sp_length,sp2_length : pool_pointer;  {lengths of |str_pool| strings}
@!sp_brace_level : integer;             {for scanning |str_pool| strings}
@!ex_buf_xptr,@!ex_buf_yptr : buf_pointer;      {extra |ex_buf| locations}
@!control_seq_loc : hash_loc;   {hash-table loc of a control sequence}
@!preceding_white : boolean;    {used in scanning strings}
@!and_found : boolean;          {to stop the loop that looks for an ``and''}
@!num_names : integer;          {for counting names}
@!name_bf_ptr : buf_pointer;    {general |name_buf| location}
@!name_bf_xptr,@!name_bf_yptr : buf_pointer;    {and two more}
@!nm_brace_level : integer;     {for scanning |name_buf| strings}
@!name_tok : packed array[buf_pointer] of buf_pointer; {name-token ptr list}
@!name_sep_char : packed array[buf_pointer] of ASCII_code; {token-ending chars}
@!num_tokens : buf_pointer;     {this counts name tokens}
@!token_starting : boolean;     {used in scanning name tokens}
@!alpha_found : boolean;        {used in scanning the format string}
@!double_letter,@!end_of_group,@!to_be_written : boolean;       {the same}
@!first_start : buf_pointer;    {start-ptr into |name_tok| for the first name}
@!first_end : buf_pointer;      {end-ptr into |name_tok| for the first name}
@!last_end : buf_pointer;       {end-ptr into |name_tok| for the last name}
@!von_start : buf_pointer;      {start-ptr into |name_tok| for the von name}
@!von_end : buf_pointer;        {end-ptr into |name_tok| for the von name}
@!jr_end : buf_pointer;         {end-ptr into |name_tok| for the jr name}
@!cur_token,@!last_token : buf_pointer; {|name_tok| ptrs for outputting tokens}
@!use_default : boolean;        {for the inter-token intra-name part string}
@!num_commas : buf_pointer;     {used to determine the name syntax}
@!comma1,@!comma2 : buf_pointer;        {ptrs into |name_tok|}
@!num_text_chars : buf_pointer; {special characters count as one}


@
The |built_in| function {\.{=}} pops the top two (integer or string)
literals, compares them, and pushes the integer 1 if they're equal, 0
otherwise.  If they're not either both string or both integer, it
complains and pushes the integer 0.

@<|execute_fn|({\.{=}})@>=
procedure x_equals;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> pop_typ2) then
    begin
    if ((pop_typ1 <> stk_empty) and (pop_typ2 <> stk_empty)) then
        begin
        print_stk_lit (pop_lit1,pop_typ1);
        print (', ');
        print_stk_lit (pop_lit2,pop_typ2);
        print_newline;
        bst_ex_warn ('---they aren''t the same literal types');
        end;
    push_lit_stk (0, stk_int);
    end
else if ((pop_typ1 <> stk_int) and (pop_typ1 <> stk_str)) then
    begin
    if (pop_typ1 <> stk_empty) then
        begin
        print_stk_lit (pop_lit1,pop_typ1);
        bst_ex_warn (', not an integer or a string,');
        end;
    push_lit_stk (0, stk_int);
    end
else if (pop_typ1 = stk_int) then
    if (pop_lit2 = pop_lit1) then
        push_lit_stk (1, stk_int)
      else
        push_lit_stk (0, stk_int)
else
    if (str_eq_str (pop_lit2,pop_lit1)) then
        push_lit_stk (1, stk_int)
      else
        push_lit_stk (0, stk_int);
end;


@
The |built_in| function {\.{>}} pops the top two (integer) literals,
compares them, and pushes the integer 1 if the second is greater than
the first, 0 otherwise.  If either isn't an integer literal, it
complains and pushes the integer 0.

@<|execute_fn|({\.{>}})@>=
procedure x_greater_than;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (0, stk_int);
    end
else if (pop_typ2 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
    push_lit_stk (0, stk_int);
    end
else
    if (pop_lit2 > pop_lit1) then
        push_lit_stk (1, stk_int)
      else
        push_lit_stk (0, stk_int);
end;


@
The |built_in| function {\.{<}} pops the top two (integer) literals,
compares them, and pushes the integer 1 if the second is less than the
first, 0 otherwise.  If either isn't an integer literal, it complains
and pushes the integer 0.

@<|execute_fn|({\.{<}})@>=
procedure x_less_than;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (0, stk_int);
    end
else if (pop_typ2 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
    push_lit_stk (0, stk_int);
    end
else
    if (pop_lit2 < pop_lit1) then
        push_lit_stk (1, stk_int)
      else
        push_lit_stk (0, stk_int);
end;


@
The |built_in| function {\.{+}} pops the top two (integer) literals
and pushes their sum.  If either isn't an integer literal, it
complains and pushes the integer 0.

@<|execute_fn|({\.{+}})@>=
procedure x_plus;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (0, stk_int);
    end
else if (pop_typ2 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
    push_lit_stk (0, stk_int);
    end
else
    push_lit_stk (pop_lit2+pop_lit1, stk_int);
end;


@
The |built_in| function {\.{-}} pops the top two (integer) literals
and pushes their difference (the first subtracted from the second).
If either isn't an integer literal, it complains and pushes the
integer 0.

@<|execute_fn|({\.{-}})@>=
procedure x_minus;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (0, stk_int);
    end
else if (pop_typ2 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
    push_lit_stk (0, stk_int);
    end
else
    push_lit_stk (pop_lit2-pop_lit1, stk_int);
end;


@
The |built_in| function {\.{*}} pops the top two (string) literals,
concatenates them (in reverse order, that is, the order in which
pushed), and pushes the resulting string back onto the stack.  If
either isn't a string literal, it complains and pushes the null
string.

@<|execute_fn|({\.{*}})@>=
procedure x_concatenate;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ2 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else
    @<Concatenate the two strings and push@>;
end;


@
@^push the literal stack@>
Often both strings will be at the top of the string pool, in which
case we just move some pointers.  Furthermore, it's worth doing some
special stuff in case either string is null, since empirically this
seems to happen about $20\%$ of the time.  In any case, we don't need
the execution buffer---we simple move the strings around in the string
pool when necessary.

@<Concatenate the two strings and push@>=
begin
if (pop_lit2 >= cmd_str_ptr) then
    if (pop_lit1 >= cmd_str_ptr) then
        begin
        str_start[pop_lit1] := str_start[pop_lit1+1];
        unflush_string;
        incr(lit_stk_ptr);
        end
    else if (length(pop_lit2) = 0) then
        push_lit_stk (pop_lit1, stk_str)
    else        {|pop_lit2| is nonnull, only |pop_lit1| is below |cmd_str_ptr|}
        begin
        pool_ptr := str_start[pop_lit2+1];
        str_room (length(pop_lit1));
        sp_ptr := str_start[pop_lit1];
        sp_end := str_start[pop_lit1+1];
        while (sp_ptr < sp_end) do
            begin
            append_char (str_pool[sp_ptr]);
            incr(sp_ptr);
            end;
        push_lit_stk (make_string, stk_str);    {and push it onto the stack}
        end
else
    @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>;
end


@
@^push the literal stack@>
We simply continue the previous module.

@<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>=
begin
if (pop_lit1 >= cmd_str_ptr) then
    if (length(pop_lit2) = 0) then
        begin
        unflush_string;
        lit_stack[lit_stk_ptr] := pop_lit1;
        incr(lit_stk_ptr);
        end
    else if (length(pop_lit1) = 0) then
        incr(lit_stk_ptr)
    else        {both strings nonnull, only |pop_lit2| is below |cmd_str_ptr|}
        begin
        sp_length := length(pop_lit1);
        sp2_length := length(pop_lit2);
        str_room (sp_length + sp2_length);
        sp_ptr := str_start[pop_lit1+1];
        sp_end := str_start[pop_lit1];
        sp_xptr1 := sp_ptr + sp2_length;
        while (sp_ptr > sp_end) do              {slide up |pop_lit1|}
            begin
            decr(sp_ptr);
            decr(sp_xptr1);
            str_pool[sp_xptr1] := str_pool[sp_ptr];
            end;
        sp_ptr := str_start[pop_lit2];
        sp_end := str_start[pop_lit2+1];
        while (sp_ptr < sp_end) do              {slide up |pop_lit2|}
            begin
            append_char (str_pool[sp_ptr]);
            incr(sp_ptr);
            end;
        pool_ptr := pool_ptr + sp_length;
        push_lit_stk (make_string, stk_str);    {and push it onto the stack}
        end
else
    @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>;
end


@
@^push the literal stack@>
Again, we simply continue the previous module.

@<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>=
begin
if (length(pop_lit1) = 0) then
    incr(lit_stk_ptr)
else if (length(pop_lit2) = 0) then
    push_lit_stk (pop_lit1, stk_str)
else            {both strings are nonnull, and both are below |cmd_str_ptr|}
    begin
    str_room (length(pop_lit1) + length(pop_lit2));
    sp_ptr := str_start[pop_lit2];
    sp_end := str_start[pop_lit2+1];
    while (sp_ptr < sp_end) do                  {slide up |pop_lit2|}
        begin
        append_char (str_pool[sp_ptr]);
        incr(sp_ptr);
        end;
    sp_ptr := str_start[pop_lit1];
    sp_end := str_start[pop_lit1+1];
    while (sp_ptr < sp_end) do                  {slide up |pop_lit1|}
        begin
        append_char (str_pool[sp_ptr]);
        incr(sp_ptr);
        end;
    push_lit_stk (make_string, stk_str);        {and push it onto the stack}
    end;
end


@
The |built_in| function {\.{:=}} pops the top two literals and assigns
to the first (which must be an |int_entry_var|, a |str_entry_var|, an
|int_global_var|, or a |str_global_var|) the value of the second;
it complains if the value isn't of the appropriate type.

@<|execute_fn|({\.{:=}})@>=
procedure x_gets;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_fn) then
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
else if ((not mess_with_entries) and
        ((fn_type[pop_lit1] = str_entry_var) or
         (fn_type[pop_lit1] = int_entry_var))) then
    bst_cant_mess_with_entries_print
else
    case (fn_type[pop_lit1]) of
        int_entry_var : @<Assign to an |int_entry_var|@>;
        str_entry_var : @<Assign to a |str_entry_var|@>;
        int_global_var : @<Assign to an |int_global_var|@>;
        str_global_var : @<Assign to a |str_global_var|@>;
        othercases begin
                   print ('You can''t assign to type ');
                   print_fn_class (pop_lit1);
                   bst_ex_warn (', a nonvariable function class');
                   end
    endcases;
end;


@
This module checks that what we're about to assign is really an
integer, and then assigns.

@<Assign to an |int_entry_var|@>=
if (pop_typ2 <> stk_int) then
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
  else
    entry_ints[cite_ptr*num_ent_ints+fn_info[pop_lit1]] := pop_lit2


@
@.String size exceeded@>
It's time for a complaint if either of the two (entry or global)
string lengths is exceeded.

@d bst_string_size_exceeded(#) == begin
                                  bst_1print_string_size_exceeded;
                                  print (#);
                                  bst_2print_string_size_exceeded;
                                  end

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure bst_1print_string_size_exceeded;
begin
print ('Warning--you''ve exceeded ');
end;
@#
procedure bst_2print_string_size_exceeded;
begin
print ('-string-size,');
bst_mild_ex_warn_print;
print_ln ('*Please notify the bibstyle designer*');
end;


@
@.entry string size exceeded@>
@:String size exceeded}{\quad entry string size@>
This module checks that what we're about to assign is really a
string, and then assigns.

@<Assign to a |str_entry_var|@>=
begin
if (pop_typ2 <> stk_str) then
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
  else
    begin
    str_ent_ptr := cite_ptr*num_ent_strs + fn_info[pop_lit1];
    ent_chr_ptr := 0;
    sp_ptr := str_start[pop_lit2];
    sp_xptr1 := str_start[pop_lit2+1];
    if (sp_xptr1-sp_ptr > ent_str_size) then
        begin
        bst_string_size_exceeded (ent_str_size:0,', the entry');
        sp_xptr1 := sp_ptr + ent_str_size;
        end;
    while (sp_ptr < sp_xptr1) do
        begin                   {copy characters into |entry_strs|}
        entry_strs[str_ent_ptr][ent_chr_ptr] := str_pool[sp_ptr];
        incr(ent_chr_ptr);
        incr(sp_ptr);
        end;
    entry_strs[str_ent_ptr][ent_chr_ptr] := end_of_string;
    end
end


@
This module checks that what we're about to assign is really an
integer, and then assigns.

@<Assign to an |int_global_var|@>=
if (pop_typ2 <> stk_int) then
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
  else
    fn_info[pop_lit1] := pop_lit2


@
@.global string size exceeded@>
@:String size exceeded}{\quad global string size@>
This module checks that what we're about to assign is really a
string, and then assigns.

@<Assign to a |str_global_var|@>=
begin
if (pop_typ2 <> stk_str) then
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
  else
    begin
    str_glb_ptr := fn_info[pop_lit1];
    if (pop_lit2 < cmd_str_ptr) then
        glb_str_ptr[str_glb_ptr] := pop_lit2
      else
        begin
        glb_str_ptr[str_glb_ptr] := 0;
        glob_chr_ptr := 0;
        sp_ptr := str_start[pop_lit2];
        sp_end := str_start[pop_lit2+1];
        if (sp_end - sp_ptr > glob_str_size) then
            begin
            bst_string_size_exceeded (glob_str_size:0,', the global');
            sp_end := sp_ptr + glob_str_size;
            end;
        while (sp_ptr < sp_end) do
            begin                       {copy characters into |global_strs|}
            global_strs[str_glb_ptr][glob_chr_ptr] := str_pool[sp_ptr];
            incr(glob_chr_ptr);
            incr(sp_ptr);
            end;
        glb_str_end[str_glb_ptr] := glob_chr_ptr;
        end;
    end
end


@
The |built_in| function {\.{add.period\$}} pops the top (string)
literal, adds a |period| to a nonnull string if its last
non|right_brace| character isn't a |period|, |question_mark|, or
|exclamation_mark|, and pushes this resulting string back onto the
stack.  If the literal isn't a string, it complains and pushes the
null string.

@<|execute_fn|({\.{add.period\$}})@>=
procedure x_add_period;
label loop_exit;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else if (length(pop_lit1) = 0) then     {don't add |period| to the null string}
    push_lit_stk (s_null, stk_str)
else
    @<Add the |period|, if necessary, and push@>;
end;


@
@^push the literal stack@>
Here we scan backwards from the end of the string, skipping
non|right_brace| characters, to see if we have to add the |period|.

@<Add the |period|, if necessary, and push@>=
begin
sp_ptr := str_start[pop_lit1+1];
sp_end := str_start[pop_lit1];
while (sp_ptr > sp_end) do                      {find a non|right_brace|}
    begin
    decr(sp_ptr);
    if (str_pool[sp_ptr] <> right_brace) then
        goto loop_exit;
    end;
loop_exit:
case (str_pool[sp_ptr]) of
    period,
    question_mark,
    exclamation_mark :
        repush_string;
    othercases
        @<Add the |period| (it's necessary) and push@>
endcases;
end


@
Ok guys, we really have to do it.

@<Add the |period| (it's necessary) and push@>=
begin
if (pop_lit1 < cmd_str_ptr) then
    begin
    str_room (length(pop_lit1)+1);
    sp_ptr := str_start[pop_lit1];
    sp_end := str_start[pop_lit1+1];
    while (sp_ptr < sp_end) do          {slide |pop_lit1| atop the string pool}
        begin
        append_char (str_pool[sp_ptr]);
        incr(sp_ptr);
        end;
    end
else                                    {the string is already there}
    begin
    pool_ptr := str_start[pop_lit1+1];
    str_room (1);
    end;
append_char (period);
push_lit_stk (make_string, stk_str);
end


@
The |built_in| function {\.{call.type\$}} executes the function
specified in |type_list| for this entry unless it's |undefined|, in
which case it executes the default function \.{default.type} defined
in the \.{.bst} file, or unless it's |empty|, in which case it does
nothing.

@<|execute_fn|({\.{call.type\$}})@>=
begin
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
  else
    if (type_list[cite_ptr] = undefined) then
        execute_fn (b_default)
    else if (type_list[cite_ptr] = empty) then
        do_nothing
    else
        execute_fn (type_list[cite_ptr]);
end


@
The |built_in| function {\.{change.case\$}} pops the top two (string)
literals; it changes the case of the second according to the
specifications of the first, as follows.  (Note: The word `letters' in
the next sentence refers only to those at brace-level~0, the top-most
brace level; no other characters are changed, except perhaps for
special characters, described shortly.)  If the first literal is the
string~\.{t}, it converts to lower case all letters except the very
first character in the string, which it leaves alone, and except the
first character following any |colon| and then nonnull |white_space|,
which it also leaves alone; if it's the string~\.{l}, it converts all
letters to lower case; if it's the string~\.{u}, it converts all
letters to upper case; and if it's anything else, it complains and
does no conversion.  It then pushes this resulting string.  If either
type is incorrect, it complains and pushes the null string; however,
if both types are correct but the specification string (i.e., the
first string) isn't one of the legal ones, it merely pushes the second
back onto the stack, after complaining.  (Another note: It ignores
case differences in the specification string; for example, the strings
\.{t} and \.{T} are equivalent for the purposes of this |built_in|
function.)

@d ok_pascal_i_give_up = 21

@<|execute_fn|({\.{change.case\$}})@>=
procedure x_change_case;
label ok_pascal_i_give_up;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ2 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    @<Determine the case-conversion type@>;
    ex_buf_length := 0;
    add_buf_pool (pop_lit2);
    @<Perform the case conversion@>;
    add_pool_buf_and_push;              {push this string onto the stack}
    end;
end;


@
First we define a few variables for case conversion.  The constant
definitions, to be used in |case| statements, are in order of probable
frequency.

@d title_lowers = 0     {representing the string \.{t}}
@d all_lowers = 1       {representing the string \.{l}}
@d all_uppers = 2       {representing the string \.{u}}
@d bad_conversion = 3   {representing any illegal case-conversion string}

@<Globals in the outer block@>=
@!conversion_type : 0..bad_conversion;  {the possible cases}
@!prev_colon : boolean;                 {|true| if just past a |colon|}


@
Now we determine which of the three case-conversion types we're
dealing with: \.{t},~\.{l}, or~\.{u}.

@<Determine the case-conversion type@>=
begin
case (str_pool[str_start[pop_lit1]]) of
    "t","T" : conversion_type := title_lowers;
    "l","L" : conversion_type := all_lowers;
    "u","U" : conversion_type := all_uppers;
    othercases conversion_type := bad_conversion
endcases;
if ((length(pop_lit1) <> 1) or (conversion_type = bad_conversion)) then
    begin
    conversion_type := bad_conversion;
    print_pool_str (pop_lit1);
    bst_ex_warn (' is an illegal case-conversion string');
    end;
end


@
This procedure complains if the just-encountered |right_brace| would
make |brace_level| negative.

@<Procedures and functions for name-string processing@>=
procedure decr_brace_level (@!pop_lit_var : str_number);
begin
if (brace_level = 0) then
    braces_unbalanced_complaint (pop_lit_var)
  else
    decr(brace_level);
end;


@
This complaint often arises because the style designer has to type
lots of braces.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure braces_unbalanced_complaint (@!pop_lit_var : str_number);
begin
print ('Warning--"');
print_pool_str (pop_lit_var);
bst_mild_ex_warn ('" isn''t a brace-balanced string');
end;


@
This one makes sure that |brace_level=0| (it's called at a point in a
string where braces must be balanced).

@<Procedures and functions for name-string processing@>=
procedure check_brace_level (@!pop_lit_var : str_number);
begin
if (brace_level > 0) then
    braces_unbalanced_complaint (pop_lit_var);
end;


@
Here's where we actually go through the string and do the case
conversion.

@<Perform the case conversion@>=
begin
brace_level := 0;       {this is the top level}
ex_buf_ptr := 0;        {we start with the string's first character}
while (ex_buf_ptr < ex_buf_length) do
    begin
    if (ex_buf[ex_buf_ptr] = left_brace) then
        begin
        incr(brace_level);
        if (brace_level <> 1) then
            goto ok_pascal_i_give_up;
        if (ex_buf_ptr + 4 > ex_buf_length) then
            goto ok_pascal_i_give_up
          else if (ex_buf[ex_buf_ptr+1] <> backslash) then
            goto ok_pascal_i_give_up;
        if (conversion_type = title_lowers) then
          if (ex_buf_ptr = 0) then
            goto ok_pascal_i_give_up
          else if ((prev_colon) and
                        (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
            goto ok_pascal_i_give_up;
        @<Convert a special character@>;
ok_pascal_i_give_up:
        prev_colon := false;
        end
    else if (ex_buf[ex_buf_ptr] = right_brace) then
        begin
        decr_brace_level (pop_lit2);
        prev_colon := false;
        end
    else
        if (brace_level = 0) then
            @<Convert a |brace_level = 0| character@>;
    incr(ex_buf_ptr);
    end;
check_brace_level (pop_lit2);
end


@
@^special character@>
We're dealing with a special character (usually either an undotted
`\i' or `\j', or an accent like one in Table~3.1 of the \LaTeX\
manual, or a foreign character like one in Table~3.2) if the first
character after the |left_brace| is a |backslash|; the special
character ends with the matching |right_brace|.  How we handle what's
in between depends on the special character.  In general, this code
will do reasonably well if there is other stuff, too, between braces,
but it doesn't try to do anything special with |colon|s.

@<Convert a special character@>=
begin
incr(ex_buf_ptr);                       {skip over the |left_brace|}
while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
    begin
    incr(ex_buf_ptr);                   {skip over the |backslash|}
    ex_buf_xptr := ex_buf_ptr;
    while ((ex_buf_ptr < ex_buf_length) and
                (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do
        incr(ex_buf_ptr);               {this scans the control sequence}
    control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,ex_buf_ptr-ex_buf_xptr,
                                                control_seq_ilk,dont_insert);
    if (hash_found) then
        @<Convert the accented or foreign character, if necessary@>;
    ex_buf_xptr := ex_buf_ptr;
    while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
                                        (ex_buf[ex_buf_ptr] <> backslash)) do
        begin                   {this scans to the next control sequence}
        if (ex_buf[ex_buf_ptr] = right_brace) then
            decr(brace_level)
        else if (ex_buf[ex_buf_ptr] = left_brace) then
            incr(brace_level);
        incr(ex_buf_ptr);
        end;
    @<Convert a noncontrol sequence@>;
    end;
decr(ex_buf_ptr);               {unskip the |right_brace|}
end


@
@^control sequence@>
@:this can't happen}{\quad Unknown type of case conversion@>
A control sequence, for the purposes of this program, consists just of
the consecutive alphabetic characters following the |backslash|; it
might be empty (although ones in this section aren't).

@<Convert the accented or foreign character, if necessary@>=
begin
case (conversion_type) of
    title_lowers,
    all_lowers :
        case (ilk_info[control_seq_loc]) of
            n_l_upper,
            n_o_upper,
            n_oe_upper,
            n_ae_upper,
            n_aa_upper :
                lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
            othercases
                do_nothing
        endcases;
    all_uppers :
        case (ilk_info[control_seq_loc]) of
            n_l,
            n_o,
            n_oe,
            n_ae,
            n_aa :
                upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
            n_i,
            n_j,
            n_ss :
                @<Convert, then remove the control sequence@>;
            othercases
                do_nothing
        endcases;
    bad_conversion :
        do_nothing;
    othercases
        case_conversion_confusion
endcases;
end


@
@:this can't happen}{\quad Unknown type of case conversion@>
Another bug complaint.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure case_conversion_confusion;
begin
confusion ('Unknown type of case conversion');
end;


@
After converting the control sequence, we need to remove the preceding
|backslash| and any following |white_space|.

@<Convert, then remove the control sequence@>=
begin
upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
while (ex_buf_xptr < ex_buf_ptr) do
    begin                       {remove preceding |backslash| and shift down}
    ex_buf[ex_buf_xptr-1] := ex_buf[ex_buf_xptr];
    incr(ex_buf_xptr);
    end;
decr(ex_buf_xptr);
while ((ex_buf_ptr < ex_buf_length) and
                (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
    incr(ex_buf_ptr);           {remove |white_space| trailing the control seq}
tmp_ptr := ex_buf_ptr;
while (tmp_ptr < ex_buf_length) do
    begin                       {more shifting down}
    ex_buf[tmp_ptr-(ex_buf_ptr-ex_buf_xptr)] := ex_buf[tmp_ptr];
    incr(tmp_ptr)
    end;
ex_buf_length := tmp_ptr - (ex_buf_ptr - ex_buf_xptr);
ex_buf_ptr := ex_buf_xptr;
end


@
@:this can't happen}{\quad Unknown type of case conversion@>
There are no control sequences in what we're about to convert,
so a straight conversion suffices.

@<Convert a noncontrol sequence@>=
begin
case (conversion_type) of
    title_lowers,
    all_lowers :
        lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
    all_uppers :
        upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
    bad_conversion :
        do_nothing;
    othercases
        case_conversion_confusion
endcases;
end


@
@:this can't happen}{\quad Unknown type of case conversion@>
This code does any needed conversion for an ordinary character; it
won't touch nonletters.

@<Convert a |brace_level = 0| character@>=
begin
case (conversion_type) of
    title_lowers :
        begin
        if (ex_buf_ptr = 0) then
            do_nothing
        else if ((prev_colon) and
                        (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
            do_nothing
        else
            lower_case (ex_buf, ex_buf_ptr, 1);
        if (ex_buf[ex_buf_ptr] = colon) then
            prev_colon := true
        else if (lex_class[ex_buf[ex_buf_ptr]] <> white_space) then
            prev_colon := false;
        end;
    all_lowers :
        lower_case (ex_buf, ex_buf_ptr, 1);
    all_uppers :
        upper_case (ex_buf, ex_buf_ptr, 1);
    bad_conversion :
        do_nothing;
    othercases
        case_conversion_confusion
endcases;
end


@
The |built_in| function {\.{chr.to.int\$}} pops the top (string)
literal, makes sure it's a single character, converts it to the
corresponding |ASCII_code| integer, and pushes this integer.  If the
literal isn't an appropriate string, it complains and pushes the
integer~0.

@<|execute_fn|({\.{chr.to.int\$}})@>=
procedure x_chr_to_int;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (0, stk_int);
    end
else if (length(pop_lit1) <> 1) then
    begin
    print ('"');
    print_pool_str (pop_lit1);
    bst_ex_warn ('" isn''t a single character');
    push_lit_stk (0, stk_int);
    end
else
    push_lit_stk (str_pool[str_start[pop_lit1]], stk_int);
                                        {push the (|ASCII_code|) integer}
end;


@
The |built_in| function {\.{cite\$}} pushes the appropriate string
from |cite_list| onto the stack.

@<|execute_fn|({\.{cite\$}})@>=
procedure x_cite;
begin
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
  else
    push_lit_stk (cur_cite_str, stk_str);
end;


@
@^push the literal stack@>
The |built_in| function {\.{duplicate\$}} pops the top literal from
the stack and pushes two copies of it.

@<|execute_fn|({\.{duplicate\$}})@>=
procedure x_duplicate;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    push_lit_stk (pop_lit1, pop_typ1);
    push_lit_stk (pop_lit1, pop_typ1);
    end
  else
    begin
    repush_string;
    if (pop_lit1 < cmd_str_ptr) then
        push_lit_stk (pop_lit1, pop_typ1)
      else
        begin
        str_room (length(pop_lit1));
        sp_ptr := str_start[pop_lit1];
        sp_end := str_start[pop_lit1+1];
        while (sp_ptr < sp_end) do
            begin
            append_char (str_pool[sp_ptr]);
            incr(sp_ptr);
            end;
        push_lit_stk (make_string, stk_str);    {and push it onto the stack}
        end;
    end;
end;


@
The |built_in| function {\.{empty\$}} pops the top literal and pushes
the integer 1 if it's a missing field or a string having no
non|white_space| characters, 0 otherwise.  If the literal isn't a
missing field or a string, it complains and pushes 0.

@<|execute_fn|({\.{empty\$}})@>=
procedure x_empty;
label exit;
begin
pop_lit_stk (pop_lit1,pop_typ1);
case (pop_typ1) of
    stk_str : @<Push 0 if the string has a non|white_space| char, else 1@>;
    stk_field_missing : push_lit_stk (1, stk_int);
    stk_empty : push_lit_stk (0, stk_int);
    othercases
        begin
        print_stk_lit (pop_lit1,pop_typ1);
        bst_ex_warn (', not a string or missing field,');
        push_lit_stk (0, stk_int);
        end
endcases;
exit:
end;


@
When we arrive here we're dealing with a legitimate string.  If it has
no characters, or has nothing but |white_space| characters, we push~1,
otherwise we push~0.

@<Push 0 if the string has a non|white_space| char, else 1@>=
begin
sp_ptr := str_start[pop_lit1];
sp_end := str_start[pop_lit1+1];
while (sp_ptr < sp_end) do
    begin
    if (lex_class[str_pool[sp_ptr]] <> white_space) then
        begin
        push_lit_stk (0, stk_int);
        return;
        end;
    incr(sp_ptr);
    end;
push_lit_stk (1, stk_int);
end


@
The |built_in| function {\.{format.name\$}} pops the top three
literals (they are a string, an integer, and a string literal, in that
order).  The last string literal represents a name list (each name
corresponding to a person), the integer literal specifies which name
to pick from this list, and the first string literal specifies how to
format this name, as described in the \BibTeX\ documentation.
Finally, this function pushes the formatted name.  If any of the types
is incorrect, it complains and pushes the null string.

@d von_found = 52               {for when a von token is found}

@<|execute_fn|({\.{format.name\$}})@>=
procedure x_format_name;
label loop1_exit,@!loop2_exit,@!von_found;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
pop_lit_stk (pop_lit3,pop_typ3);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ2 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ3 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    ex_buf_length := 0;
    add_buf_pool (pop_lit3);
    @<Isolate the desired name@>;
    @<Copy name and count |comma|s to determine syntax@>;
    @<Find the parts of the name@>;
    ex_buf_length := 0;
    add_buf_pool (pop_lit1);
    figure_out_the_formatted_name;@/
    add_pool_buf_and_push;      {push the formatted string onto the stack}
    end;
end;


@
This module skips over undesired names in |pop_lit3| and it throws
away the ``and'' from the end of the name if it exists.  When it's
done, |ex_buf_xptr| points to its first character and |ex_buf_ptr|
points just past its last.

@<Isolate the desired name@>=
begin
ex_buf_ptr := 0;
num_names := 0;
while ((num_names < pop_lit2) and (ex_buf_ptr < ex_buf_length)) do
    begin
    incr(num_names);
    ex_buf_xptr := ex_buf_ptr;
    name_scan_for_and (pop_lit3);
    end;
if (ex_buf_ptr < ex_buf_length) then            {remove the ``and''}
    ex_buf_ptr := ex_buf_ptr - 4;
if (num_names < pop_lit2) then
    begin
    if (pop_lit2 = 1) then
        print ('There is no name in "')
      else
        print ('There aren''t ',pop_lit2:0,' names in "');
    print_pool_str (pop_lit3);
    bst_ex_warn ('"');
    end
end


@
This module, starting at |ex_buf_ptr|, looks in |ex_buf| for an
``and'' surrounded by nonnull |white_space|.  It stops either at
|ex_buf_length| or just past the ``and'', whichever comes first,
setting |ex_buf_ptr| accordingly.  Its parameter |pop_lit_var| is
either |pop_lit3| or |pop_lit1|, depending on whether
{\.{format.name\$}} or {\.{num.names\$}} calls it.

@<Procedures and functions for name-string processing@>=
procedure name_scan_for_and (@!pop_lit_var : str_number);
begin
brace_level := 0;
preceding_white := false;
and_found := false;
while ((not and_found) and (ex_buf_ptr < ex_buf_length)) do
  case (ex_buf[ex_buf_ptr]) of
    "a", "A" :
        begin
        incr(ex_buf_ptr);
        if (preceding_white) then
            @<See if we have an ``and''@>;      {if so, |and_found := true|}
        preceding_white := false;
        end;
    left_brace :
        begin
        incr(brace_level);
        incr(ex_buf_ptr);
        @<Skip over |ex_buf| stuff at |brace_level > 0|@>;
        preceding_white := false;
        end;
    right_brace :
        begin
        decr_brace_level (pop_lit_var);         {this checks for an error}
        incr(ex_buf_ptr);
        preceding_white := false;
        end;
    othercases
        if (lex_class[ex_buf[ex_buf_ptr]] = white_space) then
            begin
            incr(ex_buf_ptr);
            preceding_white := true;
            end
        else
            begin
            incr(ex_buf_ptr);
            preceding_white := false;
            end
  endcases;
check_brace_level (pop_lit_var);
end;


@
When we come here |ex_buf_ptr| is just past the |left_brace|, and when
we leave it's either at |ex_buf_length| or just past the matching
|right_brace|.

@<Skip over |ex_buf| stuff at |brace_level > 0|@>=
while ((brace_level > 0) and (ex_buf_ptr < ex_buf_length)) do
    begin
    if (ex_buf[ex_buf_ptr] = right_brace) then
        decr(brace_level)
    else if (ex_buf[ex_buf_ptr] = left_brace) then
        incr(brace_level);
    incr(ex_buf_ptr);
    end


@
When we come here |ex_buf_ptr| is just past the ``a'' or ``A'', and when
we leave it's either at the same place or, if we found an ``and'', at
the following |white_space| character.

@<See if we have an ``and''@>=
begin
if (ex_buf_ptr <= (ex_buf_length - 3)) then     {enough characters are left}
    if ((ex_buf[ex_buf_ptr] = "n") or (ex_buf[ex_buf_ptr] = "N")) then
        if ((ex_buf[ex_buf_ptr+1] = "d") or (ex_buf[ex_buf_ptr+1] = "D")) then
            if (lex_class[ex_buf[ex_buf_ptr+2]] = white_space) then
                begin
                ex_buf_ptr := ex_buf_ptr + 2;
                and_found := true;
                end;
end


@
When we arrive here, the desired name is in |ex_buf[ex_buf_xptr]|
through |ex_buf[ex_buf_ptr-1]|.  This module does its thing for
characters only at |brace_level = 0|; the rest get processed verbatim.
It removes leading |white_space| (and |sep_char|s), and trailing
|white_space| (and |sep_char|s) and |comma|s, complaining for each
trailing |comma|.  It then copies the name into |name_buf|, removing
all |white_space|, |sep_char|s and |comma|s, counting |comma|s, and
constructing a list of name tokens, which are sequences of characters
separated (at |brace_level=0|) by |white_space|, |sep_char|s or
|comma|s.  Each name token but the first has an associated
|name_sep_char|, the character that separates it from the preceding
token.  If there are too many (more than two) |comma|s, a complaint is
in order.

@<Copy name and count |comma|s to determine syntax@>=
begin
@<Remove leading and trailing junk, complaining if necessary@>;
name_bf_ptr := 0;
num_commas := 0;
num_tokens := 0;@/
token_starting := true;         {to indicate that a name token is starting}
while (ex_buf_xptr < ex_buf_ptr) do
    case (ex_buf[ex_buf_xptr]) of
        comma : @<Name-process a |comma|@>;
        left_brace : @<Name-process a |left_brace|@>;
        right_brace : @<Name-process a |right_brace|@>;
        othercases
            case (lex_class[ex_buf[ex_buf_xptr]]) of
                white_space : @<Name-process a |white_space|@>;
                sep_char : @<Name-process a |sep_char|@>;
                othercases @<Name-process some other character@>
            endcases
    endcases;
name_tok[num_tokens] := name_bf_ptr;    {this is an end-marker}
end


@
This module removes all leading |white_space| (and |sep_char|s), and
trailing |white_space| (and |sep_char|s) and |comma|s.  It complains
for each trailing |comma|.

@<Remove leading and trailing junk, complaining if necessary@>=
begin
while ((ex_buf_xptr < ex_buf_ptr) and
                        (lex_class[ex_buf[ex_buf_ptr]] = white_space) and
                        (lex_class[ex_buf[ex_buf_ptr]] = sep_char)) do
        incr(ex_buf_xptr);                      {this removes leading stuff}
while (ex_buf_ptr > ex_buf_xptr) do             {now remove trailing stuff}
    case (lex_class[ex_buf[ex_buf_ptr-1]]) of
        white_space,
        sep_char :
            decr(ex_buf_ptr);
        othercases
            if (ex_buf[ex_buf_ptr-1] = comma) then
                begin
                print ('Name ',pop_lit2:0,' in "');
                print_pool_str (pop_lit3);
                print ('" has a comma at the end');
                bst_ex_warn_print;
                decr(ex_buf_ptr);
                end
            else
                goto loop1_exit
    endcases;
loop1_exit:

end

@
Here we mark the token number at which this comma has occurred.

@<Name-process a |comma|@>=
begin
if (num_commas = 2) then
    begin
    print ('Too many commas in name ',pop_lit2:0,' of "');
    print_pool_str (pop_lit3);
    print ('"');
    bst_ex_warn_print;
    end
  else
    begin
    incr(num_commas);
    if (num_commas = 1) then
        comma1 := num_tokens
      else
        comma2 := num_tokens;                   {|num_commas = 2|}
    name_sep_char[num_tokens] := comma;
    end;
incr(ex_buf_xptr);
token_starting := true;
end


@
We copy the stuff up through the matching |right_brace| verbatim.

@<Name-process a |left_brace|@>=
begin
incr(brace_level);
if (token_starting) then
    begin
    name_tok[num_tokens] := name_bf_ptr;
    incr(num_tokens);
    end;
name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
incr(name_bf_ptr);
incr(ex_buf_xptr);
while ((brace_level > 0) and (ex_buf_xptr < ex_buf_ptr)) do
    begin
    if (ex_buf[ex_buf_xptr] = right_brace) then
        decr(brace_level)
    else if (ex_buf[ex_buf_xptr] = left_brace) then
        incr(brace_level);
    name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
    incr(name_bf_ptr);
    incr(ex_buf_xptr);
    end;
token_starting := false;
end


@
We don't copy an extra |right_brace|; this code will almost never be
executed.

@<Name-process a |right_brace|@>=
begin
if (token_starting) then
    begin
    name_tok[num_tokens] := name_bf_ptr;
    incr(num_tokens);
    end;
print ('Name ',pop_lit2:0,' of "');
print_pool_str (pop_lit3);
bst_ex_warn ('" isn''t brace balanced');
incr(ex_buf_xptr);
token_starting := false;
end


@
A token will be starting soon in a buffer near you, one way$\ldots$

@<Name-process a |white_space|@>=
begin
if (not token_starting) then
    name_sep_char[num_tokens] := space;
incr(ex_buf_xptr);
token_starting := true;
end


@
@^user abuse@>
or another.  If one of the valid |sep_char|s appears between tokens,
we usually use it instead of a |space|.  If the user has been silly
enough to have multiple |sep_char|s, or to have both |white_space| and
a |sep_char|, we use the first such character.

@<Name-process a |sep_char|@>=
begin
if (not token_starting) then
    name_sep_char[num_tokens] := ex_buf[ex_buf_xptr];
incr(ex_buf_xptr);
token_starting := true;
end


@
For ordinary characters, we just copy the character.

@<Name-process some other character@>=
begin
if (token_starting) then
    begin
    name_tok[num_tokens] := name_bf_ptr;
    incr(num_tokens);
    end;
name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
incr(name_bf_ptr);
incr(ex_buf_xptr);
token_starting := false;
end


@
@:this can't happen}{\quad Illegal number of comma,s@>
Here we set all the pointers for the various parts of the name,
depending on which of the three possible syntaxes this name uses.

@<Find the parts of the name@>=
begin
if (num_commas = 0) then
    begin
    first_start := 0;
    last_end := num_tokens;
    jr_end := last_end;
    @<Determine where the first name ends and von name starts and ends@>;
    end
else if (num_commas = 1) then
    begin
    von_start := 0;
    last_end := comma1;
    jr_end := last_end;
    first_start := jr_end;
    first_end := num_tokens;
    von_name_ends_and_last_name_starts_stuff;
    end
else if (num_commas = 2) then
    begin
    von_start := 0;
    last_end := comma1;
    jr_end := comma2;
    first_start := jr_end;
    first_end := num_tokens;
    von_name_ends_and_last_name_starts_stuff;
    end
else
    confusion ('Illegal number of comma,s');
end


@
When there are no brace-level-0 |comma|s in the name, the von name
starts with the first nonlast token whose first brace-level-0 letter
is in lower case (for the purposes of this determination, an accented
or foreign character at brace-level-1 that's in lower case will do, as
well).  A module following this one determines where the von name ends
and the last starts.

@<Determine where the first name ends and von name starts and ends@>=
begin
von_start := 0;
while (von_start < last_end-1) do
    begin
    name_bf_ptr := name_tok[von_start];
    name_bf_xptr := name_tok[von_start+1];
    if (von_token_found) then
        begin
        von_name_ends_and_last_name_starts_stuff;
        goto von_found;
        end;
    incr(von_start);
    end;                        {there's no von name, so}
while (von_start > 0) do        {backtrack if there are connected tokens}
    begin
    if ((lex_class[name_sep_char[von_start]] <> sep_char) or
                        (name_sep_char[von_start] = tie)) then
        goto loop2_exit;
    decr(von_start);
    end;
loop2_exit:
von_end := von_start;
von_found:
first_end := von_start;
end


@
@^special character@>
It's a von token if there exists a first brace-level-0 letter (or
brace-level-1 special character), and it's in lower case; in this case
we return |true|.  The token is in |name_buf|, starting at
|name_bf_ptr| and ending just before |name_bf_xptr|.

@d return_von_found ==  begin
                        von_token_found := true;
                        return;
                        end

@<Procedures and functions for name-string processing@>=
function von_token_found : boolean;
label exit;
begin
nm_brace_level := 0;
von_token_found := false;               {now it's easy to exit if necessary}
while (name_bf_ptr < name_bf_xptr) do
    if ((name_buf[name_bf_ptr] >= "A") and
                        (name_buf[name_bf_ptr] <= "Z")) then
        return
    else if ((name_buf[name_bf_ptr] >= "a") and
                        (name_buf[name_bf_ptr] <= "z")) then
        return_von_found
    else if (name_buf[name_bf_ptr] = left_brace) then
        begin
        incr(nm_brace_level);
        incr(name_bf_ptr);
        if ((name_bf_ptr + 2 < name_bf_xptr) and
                                (name_buf[name_bf_ptr] = backslash)) then
            @<Check the special character (and |return|)@>
          else
            @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>;
        end
    else
        incr(name_bf_ptr);
exit:
end;


@
@^special character@>
When we come here |name_bf_ptr| is just past the |left_brace|,
but we always leave by |return|ing.

@<Check the special character (and |return|)@>=
begin
incr(name_bf_ptr);                      {skip over the |backslash|}
name_bf_yptr := name_bf_ptr;
while ((name_bf_ptr < name_bf_xptr) and
                (lex_class[name_buf[name_bf_ptr]] = alpha)) do
    incr(name_bf_ptr);                  {this scans the control sequence}
control_seq_loc := str_lookup(name_buf,name_bf_yptr,name_bf_ptr-name_bf_yptr,
                                                control_seq_ilk,dont_insert);
if (hash_found) then
    @<Handle this accented or foreign character (and |return|)@>;
while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
    begin
    if ((name_buf[name_bf_ptr] >= "A") and
                        (name_buf[name_bf_ptr] <= "Z")) then
        return
    else if ((name_buf[name_bf_ptr] >= "a") and
                        (name_buf[name_bf_ptr] <= "z")) then
        return_von_found
    else if (name_buf[name_bf_ptr] = right_brace) then
        decr(nm_brace_level)
    else if (name_buf[name_bf_ptr] = left_brace) then
        incr(nm_brace_level);
    incr(name_bf_ptr);
    end;
return;
end


@
@:this can't happen}{\quad Control-sequence hash error@>
The accented or foreign character is either `\.{\\i}' or `\.{\\j}' or
one of the eleven alphabetic foreign characters in Table~3.2 of the
\LaTeX\ manual.

@<Handle this accented or foreign character (and |return|)@>=
begin
case (ilk_info[control_seq_loc]) of
    n_oe_upper,
    n_ae_upper,
    n_aa_upper,
    n_o_upper,
    n_l_upper :
        return;
    n_i,
    n_j,
    n_oe,
    n_ae,
    n_aa,
    n_o,
    n_l,
    n_ss :
        return_von_found;
    othercases
        confusion ('Control-sequence hash error')
endcases;
end


@
When we come here |name_bf_ptr| is just past the |left_brace|; when we
leave it's either at |name_bf_xptr| or just past the matching
|right_brace|.

@<Skip over |name_buf| stuff at |nm_brace_level > 0|@>=
while ((nm_brace_level > 0) and (name_bf_ptr < name_bf_xptr)) do
    begin
    if (name_buf[name_bf_ptr] = right_brace) then
        decr(nm_brace_level)
    else if (name_buf[name_bf_ptr] = left_brace) then
        incr(nm_brace_level);
    incr(name_bf_ptr);
    end


@
@^Casey Stengel would be proud@>
@^special character@>
@^Tuesdays@>
The last name starts just past the last token, before the first
|comma| (if there is no |comma|, there is deemed to be one at the end
of the string), for which there exists a first brace-level-0 letter
(or brace-level-1 special character), and it's in lower case, unless
this last token is also the last token before the |comma|, in which
case the last name starts with this token (unless this last token is
connected by a |sep_char| other than a |tie| to the previous token, in
which case the last name starts with as many tokens earlier as are
connected by non|tie|s to this last one (except on Tuesdays
$\ldots\,$), although this module never sees such a case).  Note that
if there are any tokens in either the von or last names, then the last
name has at least one, even if it starts with a lower-case letter.

@<Procedures and functions for name-string processing@>=
procedure von_name_ends_and_last_name_starts_stuff;
label exit;
begin                           {there may or may not be a von name}
von_end := last_end - 1;
while (von_end > von_start) do
    begin
    name_bf_ptr := name_tok[von_end-1];
    name_bf_xptr := name_tok[von_end];
    if (von_token_found) then
        return;
    decr(von_end);
    end;
exit:
end;


@
This module uses the information in |pop_lit1| to format the name.
Everything at |sp_brace_level = 0| is copied verbatim to the formatted
string; the rest is described in the succeeding modules.

@<Figure out the formatted name@>=
begin
ex_buf_ptr := 0;
sp_brace_level := 0;
sp_ptr := str_start[pop_lit1];
sp_end := str_start[pop_lit1+1];
while (sp_ptr < sp_end) do
    if (str_pool[sp_ptr] = left_brace) then
        begin
        incr(sp_brace_level);
        incr(sp_ptr);
        @<Format this part of the name@>;
        end
    else if (str_pool[sp_ptr] = right_brace) then
        begin
        braces_unbalanced_complaint (pop_lit1);
        incr(sp_ptr);
        end
    else
        begin
        append_ex_buf_char_and_check (str_pool[sp_ptr]);
        incr(sp_ptr);
        end;
if (sp_brace_level > 0) then
    braces_unbalanced_complaint (pop_lit1);
ex_buf_length := ex_buf_ptr;
end


@
When we arrive here we're at |sp_brace_level = 1|, just past the
|left_brace|.  Letters at this |sp_brace_level| other than those
denoting the parts of the name (i.e., the first letters of `first,'
`last,' `von,' and `jr,' ignoring case) are illegal.  We do two passes
over this group; the first determines whether we're to output
anything, and, if we are, the second actually outputs it.

@<Format this part of the name@>=
begin
sp_xptr1 := sp_ptr;
alpha_found := false;
double_letter := false;
end_of_group := false;
to_be_written := true;
while ((not end_of_group) and (sp_ptr < sp_end)) do
    if (lex_class[str_pool[sp_ptr]] = alpha) then
        begin
        incr(sp_ptr);
        @<Figure out what this letter means@>;
        end
    else if (str_pool[sp_ptr] = right_brace) then
        begin
        decr(sp_brace_level);
        incr(sp_ptr);
        end_of_group := true;
        end
    else if (str_pool[sp_ptr] = left_brace) then
        begin
        incr(sp_brace_level);
        incr(sp_ptr);
        skip_stuff_at_sp_brace_level_greater_than_one;
        end
    else
        incr(sp_ptr);
if ((end_of_group) and (to_be_written)) then    {do the second pass}
    @<Finally format this part of the name@>;
end


@
When we come here |sp_ptr| is just past the |left_brace|, and when we
leave it's either at |sp_end| or just past the matching |right_brace|.

@<Procedures and functions for name-string processing@>=
procedure skip_stuff_at_sp_brace_level_greater_than_one;
begin
while ((sp_brace_level > 1) and (sp_ptr < sp_end)) do
    begin
    if (str_pool[sp_ptr] = right_brace) then
        decr(sp_brace_level)
    else if (str_pool[sp_ptr] = left_brace) then
        incr(sp_brace_level);
    incr(sp_ptr);
    end;
end;


@
We won't output anything for this part of the name if this is a second
occurrence of an |sp_brace_level = 1| letter, if it's an illegal
letter, or if there are no tokens corresponding to this part.  We also
determine if we're we to output complete tokens (indicated by a double
letter).

@<Figure out what this letter means@>=
begin
if (alpha_found) then
    begin
    brace_lvl_one_letters_complaint;
    to_be_written := false;
    end
  else
    begin
    case (str_pool[sp_ptr-1]) of
        "f","F" : @<Figure out what tokens we'll output for the `first' name@>;
        "v","V" : @<Figure out what tokens we'll output for the `von' name@>;
        "l","L" : @<Figure out what tokens we'll output for the `last' name@>;
        "j","J" : @<Figure out what tokens we'll output for the `jr' name@>;
        othercases
                begin
                brace_lvl_one_letters_complaint;
                to_be_written := false;
                end
    endcases;
    if (double_letter) then
        incr(sp_ptr);
    end;
alpha_found := true;
end


@
At most one of the important letters, perhaps doubled, may appear at
|sp_brace_level = 1|.

@<Procedures and functions for name-string processing@>=
procedure brace_lvl_one_letters_complaint;
begin
print ('The format string "');
print_pool_str (pop_lit1);
bst_ex_warn ('" has an illegal brace-level-1 letter');
end;


@
Here we set pointers into |name_tok| and note whether we'll be dealing
with a full first-name tokens (|double_letter = true|) or
abbreviations (|double_letter = false|).

@<Figure out what tokens we'll output for the `first' name@>=
begin
cur_token := first_start;
last_token := first_end;
if (cur_token = last_token) then
    to_be_written := false;
if ((str_pool[sp_ptr] = "f") or (str_pool[sp_ptr] = "F")) then
    double_letter := true;
end


@
The same as above but for von-name tokens.

@<Figure out what tokens we'll output for the `von' name@>=
begin
cur_token := von_start;
last_token := von_end;
if (cur_token = last_token) then
    to_be_written := false;
if ((str_pool[sp_ptr] = "v") or (str_pool[sp_ptr] = "V")) then
    double_letter := true;
end


@
The same as above but for last-name tokens.

@<Figure out what tokens we'll output for the `last' name@>=
begin
cur_token := von_end;
last_token := last_end;
if (cur_token = last_token) then
    to_be_written := false;
if ((str_pool[sp_ptr] = "l") or (str_pool[sp_ptr] = "L")) then
    double_letter := true;
end


@
The same as above but for jr-name tokens.

@<Figure out what tokens we'll output for the `jr' name@>=
begin
cur_token := last_end;
last_token := jr_end;
if (cur_token = last_token) then
    to_be_written := false;
if ((str_pool[sp_ptr] = "j") or (str_pool[sp_ptr] = "J")) then
    double_letter := true;
end


@
This is the second pass over this part of the name; here we actually
write stuff out to |ex_buf|.

@<Finally format this part of the name@>=
begin
ex_buf_xptr := ex_buf_ptr;
sp_ptr := sp_xptr1;
sp_brace_level := 1;
while (sp_brace_level > 0) do
    if ((lex_class[str_pool[sp_ptr]] = alpha) and (sp_brace_level = 1)) then
        begin
        incr(sp_ptr);
        @<Figure out how to output the name tokens, and do it@>;
        end
    else if (str_pool[sp_ptr] = right_brace) then
        begin
        decr(sp_brace_level);
        incr(sp_ptr);
        if (sp_brace_level > 0) then
            append_ex_buf_char_and_check (right_brace);
        end
    else if (str_pool[sp_ptr] = left_brace) then
        begin
        incr(sp_brace_level);
        incr(sp_ptr);
        append_ex_buf_char_and_check (left_brace);
        end
    else
        begin
        append_ex_buf_char_and_check (str_pool[sp_ptr]);
        incr(sp_ptr);
        end;
if (ex_buf_ptr > 0) then
  if (ex_buf[ex_buf_ptr-1] = tie) then
    @<Handle a discretionary |tie|@>;
end


@
When we come here, |sp_ptr| is just past the letter indicating the
part of the name for which we're about to output tokens.  When we
leave, it's at the first character of the rest of the group.

@<Figure out how to output the name tokens, and do it@>=
begin
if (double_letter) then
    incr(sp_ptr);
use_default := true;
sp_xptr2 := sp_ptr;
if (str_pool[sp_ptr] = left_brace) then         {find the inter-token string}
    begin
    use_default := false;
    incr(sp_brace_level);
    incr(sp_ptr);
    sp_xptr1 := sp_ptr;
    skip_stuff_at_sp_brace_level_greater_than_one;
    sp_xptr2 := sp_ptr - 1;
    end;
@<Finally output the name tokens@>;
if (not use_default) then
    sp_ptr := sp_xptr2 + 1;
end


@
Here, for each token in this part, we output either a full or an
abbreviated token and the inter-token string for all but the last
token of this part.

@<Finally output the name tokens@>=
while (cur_token < last_token) do
    begin
    if (double_letter) then
        @<Finally output a full token@>
      else
        @<Finally output an abbreviated token@>;
    incr(cur_token);
    if (cur_token < last_token) then
        @<Finally output the inter-token string@>;
    end


@
@:BibTeX capacity exceeded}{\quad buffer size@>
Here we output all the characters in the token, verbatim.

@<Finally output a full token@>=
begin
name_bf_ptr := name_tok[cur_token];
name_bf_xptr := name_tok[cur_token+1];
if (ex_buf_length+(name_bf_xptr-name_bf_ptr) > buf_size) then
    buffer_overflow;
while (name_bf_ptr < name_bf_xptr) do
    begin
    append_ex_buf_char (name_buf[name_bf_ptr]);
    incr(name_bf_ptr);
    end;
end


@
@^special character@>
Here we output the first alphabetic or special character of the token;
brace level is irrelevant for an alphabetic (but not a special)
character.

@<Finally output an abbreviated token@>=
begin
name_bf_ptr := name_tok[cur_token];
name_bf_xptr := name_tok[cur_token+1];
while (name_bf_ptr < name_bf_xptr) do
    begin
    if (lex_class[name_buf[name_bf_ptr]] = alpha) then
        begin
        append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
        goto loop_exit;
        end
    else if ((name_buf[name_bf_ptr] = left_brace) and
                                (name_bf_ptr + 1 < name_bf_xptr)) then
      if (name_buf[name_bf_ptr+1] = backslash) then
        @<Finally output a special character and exit loop@>;
    incr(name_bf_ptr);
    end;
loop_exit:
end


@
@^special character@>
@^user abuse@>
@:BibTeX capacity exceeded}{\quad buffer size@>
We output a special character here even if the user has been silly
enough to make it nonalphabetic (and even if the user has been sillier
still by not having a matching |right_brace|).

@<Finally output a special character and exit loop@>=
begin
if (ex_buf_ptr + 2 > buf_size) then
    buffer_overflow;
append_ex_buf_char (left_brace);
append_ex_buf_char (backslash);
name_bf_ptr := name_bf_ptr + 2;
nm_brace_level := 1;
while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
    begin
    if (name_buf[name_bf_ptr] = right_brace) then
        decr(nm_brace_level)
    else if (name_buf[name_bf_ptr] = left_brace) then
        incr(nm_brace_level);
    append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
    incr(name_bf_ptr);
    end;
goto loop_exit;
end


@
@:BibTeX capacity exceeded}{\quad buffer size@>
Here we output either the \.{.bst} given string if it exists, or else
the \.{.bib} |sep_char| if it exists, or else the default string.  A
|tie| is the default space character between the last two tokens of
the name part, and between the first two tokens if the first token is
short enough; otherwise, a |space| is the default.

@d long_token = 3       {a token this length or longer is ``long''}

@<Finally output the inter-token string@>=
begin
if (use_default) then
    begin
    if (not double_letter) then
        append_ex_buf_char_and_check (period);
    if (lex_class[name_sep_char[cur_token]] = sep_char) then
        append_ex_buf_char_and_check (name_sep_char[cur_token])
    else if ((cur_token = last_token-1) or
                        (not enough_text_chars (long_token))) then
        append_ex_buf_char_and_check (tie)
    else
        append_ex_buf_char_and_check (space);
    end
  else
    begin
    if (ex_buf_length+(sp_xptr2-sp_xptr1) > buf_size) then
        buffer_overflow;
    sp_ptr := sp_xptr1;
    while (sp_ptr < sp_xptr2) do
        begin
        append_ex_buf_char (str_pool[sp_ptr]);
        incr(sp_ptr);
        end
    end;
end


@
@^special character@>
This function looks at the string in |ex_buf|, starting at
|ex_buf_xptr| and ending just before |ex_buf_ptr|, and it returns
|true| if there are |enough_chars|, where a special character (even if
it's missing its matching |right_brace|) counts as a single charcter.
This procedure is called only for strings that don't have too many
|right_brace|s.

@<Procedures and functions for name-string processing@>=
function enough_text_chars (@!enough_chars : buf_pointer) : boolean;
begin
num_text_chars := 0;
ex_buf_yptr := ex_buf_xptr;
while ((ex_buf_yptr < ex_buf_ptr) and (num_text_chars < enough_chars)) do
    begin
    incr(ex_buf_yptr);
    if (ex_buf[ex_buf_yptr-1] = left_brace) then
        begin
        incr(brace_level);
        if ((brace_level = 1) and (ex_buf_yptr < ex_buf_ptr)) then
          if (ex_buf[ex_buf_yptr] = backslash) then
            begin
            incr(ex_buf_yptr);                  {skip over the |backslash|}
            while ((ex_buf_yptr < ex_buf_ptr) and (brace_level > 0)) do
                begin
                if (ex_buf[ex_buf_yptr] = right_brace) then
                    decr(brace_level)
                else if (ex_buf[ex_buf_yptr] = left_brace) then
                    incr(brace_level);
                incr(ex_buf_yptr);
                end;
            end;
        end
    else if (ex_buf[ex_buf_yptr-1] = right_brace) then
        decr(brace_level);
    incr(num_text_chars);
    end;
if (num_text_chars < enough_chars) then
    enough_text_chars := false
  else
    enough_text_chars := true;
end;


@
If the last character output for this name part is a |tie| but the
previous character it isn't, we're dealing with a discretionary |tie|;
thus we replace it by a |space| if there are enough characters in the
rest of the name part.

@d long_name = 3                {a name this length or longer is ``long''}

@<Handle a discretionary |tie|@>=
begin
decr(ex_buf_ptr);                       {remove the previous |tie|}
if (ex_buf[ex_buf_ptr-1] = tie) then    {it's not a discretionary |tie|}
    do_nothing
else if (not enough_text_chars (long_name)) then {this is a short name part}
    incr(ex_buf_ptr)                    {so restore the |tie|}
else                                    {replace it by a |space|}
    append_ex_buf_char (space);
end


@
This is a procedure so that |x_format_name| is smaller.

@<Procedures and functions for name-string processing@>=
procedure figure_out_the_formatted_name;
label loop_exit;
begin
@<Figure out the formatted name@>;
end;


@
The |built_in| function {\.{if\$}} pops the top three literals (they
are two function literals and an integer literal, in that order); if
the integer is greater than 0, it executes the second literal, else it
executes the first.  If any of the types is incorrect, it complains
but does nothing else.

@<|execute_fn|({\.{if\$}})@>=
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
pop_lit_stk (pop_lit3,pop_typ3);
if (pop_typ1 <> stk_fn) then
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
else if (pop_typ2 <> stk_fn) then
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_fn)
else if (pop_typ3 <> stk_int) then
    print_wrong_stk_lit (pop_lit3,pop_typ3,stk_int)
else
    if (pop_lit3 > 0) then
        execute_fn (pop_lit2)
      else
        execute_fn (pop_lit1);
end


@
The |built_in| function {\.{int.to.chr\$}} pops the top (integer)
literal, interpreted as the |ASCII_code| of a single character,
converts it to the corresponding single-character string, and pushes
this string.  If the literal isn't an appropriate integer, it
complains and pushes the null string.

@<|execute_fn|({\.{int.to.chr\$}})@>=
procedure x_int_to_chr;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (s_null, stk_str);
    end
else if ((pop_lit1 < 0) or (pop_lit1 > 127)) then
    begin
    bst_ex_warn (pop_lit1:0,' isn''t valid ASCII');
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    str_room(1);
    append_char (pop_lit1);
    push_lit_stk (make_string, stk_str);
    end;
end;


@
The |built_in| function {\.{int.to.str\$}} pops the top (integer)
literal, converts it to its (unique) string equivalent, and pushes
this string.  If the literal isn't an integer, it complains and pushes
the null string.

@<|execute_fn|({\.{int.to.str\$}})@>=
procedure x_int_to_str;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    int_to_ASCII (pop_lit1, ex_buf, 0, ex_buf_length);@/
    add_pool_buf_and_push;              {push this string onto the stack}
    end;
end;


@
The |built_in| function {\.{missing\$}} pops the top literal and
pushes the integer 1 if it's a missing field, 0 otherwise.  If the
literal isn't a missing field or a string, it complains and pushes 0.
Unlike \.{empty\$}, this function should be called only when
|mess_with_entries| is true.

@<|execute_fn|({\.{missing\$}})@>=
procedure x_missing;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
else if ((pop_typ1 <> stk_str) and (pop_typ1 <> stk_field_missing)) then
    begin
    if (pop_typ1 <> stk_empty) then
        begin
        print_stk_lit (pop_lit1,pop_typ1);
        bst_ex_warn (', not a string or missing field,');
        end;
    push_lit_stk (0, stk_int);
    end
else
    if (pop_typ1 = stk_field_missing) then
        push_lit_stk (1, stk_int)
      else
        push_lit_stk (0, stk_int);
end;


@
The |built_in| function {\.{newline\$}} writes whatever has
accumulated in the output buffer |out_buf| onto the \.{.bbl} file.

@<|execute_fn|({\.{newline\$}})@>=
begin
output_bbl_line;
end


@
The |built_in| function {\.{num.names\$}} pops the top (string)
literal; it pushes the number of names the string represents---one
plus the number of occurrences of the substring ``and'' (ignoring case
differences) surrounded by nonnull |white_space| at the top brace
level.  If the literal isn't a string, it complains and pushes the
value 0.

@<|execute_fn|({\.{num.names\$}})@>=
procedure x_num_names;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (0, stk_int);
    end
else
    begin
    ex_buf_length := 0;
    add_buf_pool (pop_lit1);
    @<Determine the number of names@>;
    push_lit_stk (num_names, stk_int);
    end;
end;


@
This module, while scanning the list of names, counts the occurrences
of ``and'' (ignoring case differences) surrounded by nonnull
|white_space|, and adds 1.

@<Determine the number of names@>=
begin
ex_buf_ptr := 0;
num_names := 0;
while (ex_buf_ptr < ex_buf_length) do
    begin
    name_scan_for_and (pop_lit1);
    incr(num_names);
    end;
end


@
The |built_in| function {\.{pop\$}} pops the top of the stack but
doesn't print it.

@<|execute_fn|({\.{pop\$}})@>=
begin
pop_lit_stk (pop_lit1,pop_typ1);
end


@
The |built_in| function {\.{preamble\$}} pushes onto the stack the
concatenation of all the \.{preamble} strings read from the database
files.

@<|execute_fn|({\.{preamble\$}})@>=
procedure x_preamble;
begin
ex_buf_length := 0;
preamble_ptr := 0;
while (preamble_ptr < num_preamble_strings) do
    begin
    add_buf_pool (s_preamble[preamble_ptr]);
    incr(preamble_ptr);
    end;
add_pool_buf_and_push;          {push the concatenation string onto the stack}
end;


@
@^special character@>
The |built_in| function {\.{purify\$}} pops the top (string) literal,
removes nonalphanumeric characters except for |white_space| and
|sep_char| characters (these get converted to a |space|) and removes
certain alphabetic characters contained in the control sequences
associated with a special character, and pushes the resulting string.
If the literal isn't a string, it complains and pushes the null
string.

@<|execute_fn|({\.{purify\$}})@>=
procedure x_purify;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    ex_buf_length := 0;
    add_buf_pool (pop_lit1);
    @<Perform the purification@>;
    add_pool_buf_and_push;              {push this string onto the stack}
    end;
end;


@
@^special character@>
The resulting string has nonalphanumeric characters removed, and each
|white_space| or |sep_char| character converted to a |space|.  The next
module handles special characters.  This code doesn't complain if the
string isn't brace balanced.

@<Perform the purification@>=
begin
brace_level := 0;       {this is the top level}
ex_buf_xptr := 0;       {this pointer is for the purified string}
ex_buf_ptr := 0;        {and this one is for the original string}
while (ex_buf_ptr < ex_buf_length) do
    begin
    case (lex_class[ex_buf[ex_buf_ptr]]) of
        white_space,
        sep_char :
            begin
            ex_buf[ex_buf_xptr] := space;
            incr(ex_buf_xptr);
            end;
        alpha,
        numeric :
            begin
            ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
            incr(ex_buf_xptr);
            end;
        othercases
            if (ex_buf[ex_buf_ptr] = left_brace) then
                begin
                incr(brace_level);
                if ((brace_level = 1) and
                                (ex_buf_ptr + 1 < ex_buf_length)) then
                  if (ex_buf[ex_buf_ptr+1] = backslash) then
                    @<Purify a special character@>;
                end
            else if (ex_buf[ex_buf_ptr] = right_brace) then
                if (brace_level > 0) then
                    decr(brace_level)
    endcases;
    incr(ex_buf_ptr);
    end;
ex_buf_length := ex_buf_xptr;
end


@
@^special character@>
Special characters (even without a matching |right_brace|) are
purified by removing the control sequences (but restoring the correct
thing for `\.{\\i}' and `\.{\\j}' as well as the eleven alphabetic
foreign characters in Table~3.2 of the \LaTeX\ manual) and removing
all nonalphanumeric characters (including |white_space| and
|sep_char|s).

@<Purify a special character@>=
begin
incr(ex_buf_ptr);                       {skip over the |left_brace|}
while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
    begin
    incr(ex_buf_ptr);                   {skip over the |backslash|}
    ex_buf_yptr := ex_buf_ptr;  {mark the beginning of the control sequence}
    while ((ex_buf_ptr < ex_buf_length) and
                (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
        incr(ex_buf_ptr);               {this scans the control sequence}
    control_seq_loc := str_lookup(ex_buf,ex_buf_yptr,ex_buf_ptr-ex_buf_yptr,
                                                control_seq_ilk,dont_insert);
    if (hash_found) then
        @<Purify this accented or foreign character@>;
    while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
                                        (ex_buf[ex_buf_ptr] <> backslash)) do
        begin                   {this scans to the next control sequence}
        case (lex_class[ex_buf[ex_buf_ptr]]) of
            alpha,
            numeric :
                begin
                ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
                incr(ex_buf_xptr);
                end;
            othercases
                if (ex_buf[ex_buf_ptr] = right_brace) then
                    decr(brace_level)
                else if (ex_buf[ex_buf_ptr] = left_brace) then
                    incr(brace_level)
        endcases;
        incr(ex_buf_ptr);
        end;
    end;
decr(ex_buf_ptr);               {unskip the |right_brace| (or last character)}
end


@
We consider the purified character to be either the first alphabetic
character of its control sequence, or perhaps both alphabetic
characters.

@<Purify this accented or foreign character@>=
begin
ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr]; {the first alphabetic character}
incr(ex_buf_xptr);
case (ilk_info[control_seq_loc]) of
    n_oe,
    n_oe_upper,
    n_ae,
    n_ae_upper,
    n_ss :
        begin                                   {and the second}
        ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr+1];
        incr(ex_buf_xptr);
        end;
    othercases
        do_nothing
endcases;
end


@
The |built_in| function {\.{quote\$}} pushes the string consisting of
the |double_quote| character.

@<|execute_fn|({\.{quote\$}})@>=
procedure x_quote;
begin
str_room(1);
append_char (double_quote);
push_lit_stk (make_string, stk_str);
end;


@
The |built_in| function {\.{skip\$}} is a no-op.

@<|execute_fn|({\.{skip\$}})@>=
begin
do_nothing;
end


@
The |built_in| function {\.{stack\$}} pops and prints the whole stack;
it's meant to be used for style designers while debugging.

@<|execute_fn|({\.{stack\$}})@>=
begin
pop_whole_stack;
end


@
@^push the literal stack@>
The |built_in| function {\.{substring\$}} pops the top three literals
(they are the two integers literals |pop_lit1| and |pop_lit2| and a
string literal, in that order).  It pushes the substring of the (at
most) |pop_lit1| consecutive characters starting at the |pop_lit2|th
character (assuming 1-based indexing) if |pop_lit2| is positive, and
ending at the |-pop_lit2|th character from the end if |pop_lit2| is
negative (where the first character from the end is the last
character).  If any of the types is incorrect, it complain and pushes
the null string.

@<|execute_fn|({\.{substring\$}})@>=
procedure x_substring;
label exit;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
pop_lit_stk (pop_lit3,pop_typ3);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ2 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ3 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    sp_length := length(pop_lit3);
    if (pop_lit1 >= sp_length) then
      if ((pop_lit2 = 1) or (pop_lit2 = -1)) then
        begin
        repush_string;
        return;
        end;
    if ((pop_lit1 <= 0) or (pop_lit2 = 0) or (pop_lit2 > sp_length) or
                                        (pop_lit2 < -sp_length)) then
        begin
        push_lit_stk (s_null, stk_str);
        return;
        end
      else
        @<Form the appropriate substring@>;
    end;
exit:
end;


@
@^push the literal stack@>
This module finds the substring as described in the last section,
and slides it into place in the string pool, if necessary.

@<Form the appropriate substring@>=
begin
if (pop_lit2 > 0) then
    begin
    if (pop_lit1 > sp_length - (pop_lit2-1)) then
        pop_lit1 := sp_length - (pop_lit2-1);
    sp_ptr := str_start[pop_lit3] + (pop_lit2-1);
    sp_end := sp_ptr + pop_lit1;
    if (pop_lit2 = 1) then
      if (pop_lit3 >= cmd_str_ptr) then {no shifting---merely change pointers}
        begin
        str_start[pop_lit3+1] := sp_end;
        unflush_string;
        incr(lit_stk_ptr);
        return;
        end;
    end
else                                    {|-ex_buf_length <= pop_lit2 < 0|}
    begin
    pop_lit2 := -pop_lit2;
    if (pop_lit1 > sp_length - (pop_lit2-1)) then
        pop_lit1 := sp_length - (pop_lit2-1);
    sp_end := str_start[pop_lit3+1] - (pop_lit2-1);
    sp_ptr := sp_end - pop_lit1;
    end;
while (sp_ptr < sp_end) do                      {shift the substring}
    begin
    append_char (str_pool[sp_ptr]);
    incr(sp_ptr);
    end;
push_lit_stk (make_string, stk_str);            {and push it onto the stack}
end


@
The |built_in| function {\.{swap\$}} pops the top two literals from
the stack and pushes them back swapped.

@<|execute_fn|({\.{swap\$}})@>=
procedure x_swap;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if ((pop_typ1 <> stk_str) or (pop_lit1 < cmd_str_ptr)) then
    begin
    push_lit_stk (pop_lit1, pop_typ1);
    if ((pop_typ2 = stk_str) and (pop_lit2 >= cmd_str_ptr)) then
        unflush_string;
    push_lit_stk (pop_lit2, pop_typ2);
    end
else if ((pop_typ2 <> stk_str) or (pop_lit2 < cmd_str_ptr)) then
    begin
    unflush_string;                     {this is |pop_lit1|}
    push_lit_stk (pop_lit1, stk_str);
    push_lit_stk (pop_lit2, pop_typ2);
    end
else                                    {bummer, both are recent strings}
    @<Swap the two strings (they're at the end of |str_pool|)@>;
end;


@
We have to swap both (a)~the strings at the end of the string pool,
and (b)~their pointers on the literal stack.

@<Swap the two strings (they're at the end of |str_pool|)@>=
begin
ex_buf_length := 0;
add_buf_pool (pop_lit2);                {save the second string}
sp_ptr := str_start[pop_lit1];
sp_end := str_start[pop_lit1+1];
while (sp_ptr < sp_end) do              {slide the first string down}
    begin
    append_char (str_pool[sp_ptr]);
    incr(sp_ptr);
    end;
push_lit_stk (make_string, stk_str);    {and push it onto the stack}
add_pool_buf_and_push;                  {push second string onto the stack}
end


@
@^special character@>
The |built_in| function {\.{text.length\$}} pops the top (string)
literal, and pushes the number of text characters it contains, where
an accented character (more precisely, a ``special character''$\!$,
defined earlier) counts as a single text character, even if it's
missing its matching |right_brace|, and where braces don't count as
text characters.  If the literal isn't a string, it complains and
pushes the null string.

@<|execute_fn|({\.{text.length\$}})@>=
procedure x_text_length;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else
    begin
    num_text_chars := 0;
    @<Count the text characters@>;
    push_lit_stk (num_text_chars, stk_int);     {and push it onto the stack}
    end;
end;


@
@^special character@>
Here we determine the number of text characters in the string, where
an entire special character counts as a single text character (even if
it's missing its matching |right_brace|), and where braces don't count
as text characters.

@<Count the text characters@>=
begin
sp_ptr := str_start[pop_lit1];
sp_end := str_start[pop_lit1+1];
sp_brace_level := 0;
while (sp_ptr < sp_end) do
    begin
    incr(sp_ptr);
    if (str_pool[sp_ptr-1] = left_brace) then
        begin
        incr(sp_brace_level);
        if ((sp_brace_level = 1) and (sp_ptr < sp_end)) then
          if (str_pool[sp_ptr] = backslash) then
            begin
            incr(sp_ptr);               {skip over the |backslash|}
            while ((sp_ptr < sp_end) and (sp_brace_level > 0)) do
                begin
                if (str_pool[sp_ptr] = right_brace) then
                    decr(sp_brace_level)
                else if (str_pool[sp_ptr] = left_brace) then
                    incr(sp_brace_level);
                incr(sp_ptr);
                end;
            incr(num_text_chars);
            end;
        end
    else if (str_pool[sp_ptr-1] = right_brace) then
        begin
        if (sp_brace_level > 0) then
            decr(sp_brace_level);
        end
    else
        incr(num_text_chars);
    end;
end


@
@^special character@>
The |built_in| function {\.{text.prefix\$}} pops the top two literals
(the integer literal |pop_lit1| and a string literal, in that order).
It pushes the substring of the (at most) |pop_lit1| consecutive text
characters starting from the beginning of the string.  This function
is similar to {\.{substring\$}}, but this one considers an accented
character (or more precisely, a ``special character''$\!$, even if
it's missing its matching |right_brace|) to be a single text character
(rather than however many |ASCII_code| characters it actually
comprises), and this function doesn't consider braces to be text
characters; furthermore, this function appends any needed matching
|right_brace|s.  If any of the types is incorrect, it complains and
pushes the null string.

@<|execute_fn|({\.{text.prefix\$}})@>=
procedure x_text_prefix;
label exit;
begin
pop_lit_stk (pop_lit1,pop_typ1);
pop_lit_stk (pop_lit2,pop_typ2);
if (pop_typ1 <> stk_int) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_typ2 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
    push_lit_stk (s_null, stk_str);
    end
else if (pop_lit1 <= 0) then
    begin
    push_lit_stk (s_null, stk_str);
    return;
    end
else
    @<Form the appropriate prefix@>;
exit:
end;


@
@^push the literal stack@>
This module finds the prefix as described in the last section, and
appends any needed matching |right_brace|s.

@<Form the appropriate prefix@>=
begin
sp_ptr := str_start[pop_lit2];
sp_end := str_start[pop_lit2+1];        {this may change}
@<Scan the appropriate number of characters@>;
if (pop_lit2 >= cmd_str_ptr) then       {no shifting---merely change pointers}
    pool_ptr := sp_end
else
    while (sp_ptr < sp_end) do          {shift the substring}
        begin
        append_char (str_pool[sp_ptr]);
        incr(sp_ptr);
        end;
while (sp_brace_level > 0) do           {add matching |right_brace|s}
    begin
    append_char (right_brace);
    decr(sp_brace_level);
    end;
push_lit_stk (make_string, stk_str);    {and push it onto the stack}
end


@
@^special character@>
This section scans |pop_lit1| text characters, where an entire special
character counts as a single text character (even if it's missing its
matching |right_brace|), and where braces don't count as text
characters.

@<Scan the appropriate number of characters@>=
begin
num_text_chars := 0;
sp_brace_level := 0;
sp_xptr1 := sp_ptr;
while ((sp_xptr1 < sp_end) and (num_text_chars < pop_lit1)) do
    begin
    incr(sp_xptr1);
    if (str_pool[sp_xptr1-1] = left_brace) then
        begin
        incr(sp_brace_level);
        if ((sp_brace_level = 1) and (sp_xptr1 < sp_end)) then
          if (str_pool[sp_xptr1] = backslash) then
            begin
            incr(sp_xptr1);             {skip over the |backslash|}
            while ((sp_xptr1 < sp_end) and (sp_brace_level > 0)) do
                begin
                if (str_pool[sp_xptr1] = right_brace) then
                    decr(sp_brace_level)
                else if (str_pool[sp_xptr1] = left_brace) then
                    incr(sp_brace_level);
                incr(sp_xptr1);
                end;
            incr(num_text_chars);
            end;
        end
    else if (str_pool[sp_xptr1-1] = right_brace) then
        begin
        if (sp_brace_level > 0) then
            decr(sp_brace_level);
        end
    else
        incr(num_text_chars);
    end;
sp_end := sp_xptr1;
end


@
The |built_in| function {\.{top\$}} pops and prints the top of the
stack.

@<|execute_fn|({\.{top\$}})@>=
begin
pop_top_and_print;
end


@
The |built_in| function {\.{type\$}} pushes the appropriate string
from |type_list| onto the stack (unless either it's |undefined| or
|empty|, in which case it pushes the null string).

@<|execute_fn|({\.{type\$}})@>=
procedure x_type;
begin
if (not mess_with_entries) then
    bst_cant_mess_with_entries_print
  else
    if ((type_list[cite_ptr] = undefined) or
                                (type_list[cite_ptr] = empty)) then
        push_lit_stk (s_null, stk_str)
      else
        push_lit_stk (hash_text[type_list[cite_ptr]], stk_str);
end;


@
The |built_in| function {\.{warning\$}} pops the top (string) literal
and prints it following a warning message.  This is implemented as a
special |built_in| function rather than using the {\.{top\$}} function
so that it can |mark_warning|.

@<|execute_fn|({\.{warning\$}})@>=
procedure x_warning;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
else
    begin
    print ('Warning--');
    print_lit (pop_lit1,pop_typ1);
    mark_warning;
    end;
end;


@
The |built_in| function {\.{while\$}} pops the top two (function)
literals, and keeps executing the second as long as the (integer)
value left on the stack by executing the first is greater than 0.  If
either type is incorrect, it complains but does nothing else.

@<|execute_fn|({\.{while\$}})@>=
begin
pop_lit_stk (r_pop_lt1,r_pop_tp1);
pop_lit_stk (r_pop_lt2,r_pop_tp2);
if (r_pop_tp1 <> stk_fn) then
    print_wrong_stk_lit (r_pop_lt1,r_pop_tp1,stk_fn)
else if (r_pop_tp2 <> stk_fn) then
    print_wrong_stk_lit (r_pop_lt2,r_pop_tp2,stk_fn)
else
    loop
        begin
        execute_fn (r_pop_lt2);                 {this is the \.{while\$} test}
        pop_lit_stk (pop_lit1,pop_typ1);
        if (pop_typ1 <> stk_int) then
            begin
            print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
            goto end_while;
            end
        else
            if (pop_lit1 > 0) then
                execute_fn (r_pop_lt1)          {this is the \.{while\$} body}
              else
                goto end_while;
        end;
end_while:      {justifies this |mean_while|}
end


@
@^literal literal@>
@^special character@>
The |built_in| function {\.{width\$}} pops the top (string) literal
and pushes the integer that represents its width in units specified by
the |char_width| array.  This function takes the literal literally;
that is, it assumes each character in the string is to be printed as
is, regardless of whether the character has a special meaning to \TeX,
except that special characters (even without their |right_brace|s) are
handled specially.  If the literal isn't a string, it complains and
pushes~0.

@<|execute_fn|({\.{width\$}})@>=
procedure x_width;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    begin
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
    push_lit_stk (0, stk_int);
    end
else
    begin
    ex_buf_length := 0;
    add_buf_pool (pop_lit1);
    string_width := 0;
    @<Add up the |char_width|s in this string@>;
    push_lit_stk (string_width, stk_int);
    end
end;


@
We use the natural width for all but special characters, and we
complain if the string isn't brace-balanced.

@<Add up the |char_width|s in this string@>=
begin
brace_level := 0;                       {we're at the top level}
ex_buf_ptr := 0;                        {and the beginning of string}
while (ex_buf_ptr < ex_buf_length) do
    begin
    if (ex_buf[ex_buf_ptr] = left_brace) then
        begin
        incr(brace_level);
        if ((brace_level = 1) and (ex_buf_ptr + 1 < ex_buf_length)) then
            if (ex_buf[ex_buf_ptr+1] = backslash) then
                @<Determine the width of this special character@>
              else
                string_width := string_width + char_width[left_brace]
          else
            string_width := string_width + char_width[left_brace];
        end
    else if (ex_buf[ex_buf_ptr] = right_brace) then
        begin
        decr_brace_level (pop_lit1);
        string_width := string_width + char_width[right_brace];
        end
    else
        string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
    incr(ex_buf_ptr);
    end;
check_brace_level (pop_lit1);
end


@
@^special character@>
We use the natural widths of all characters except that some
characters have no width: braces, control sequences (except for the
usual 13 accented and foreign characters, whose widths are given in
the next module), and |white_space| following control sequences (even
a null control sequence).

@<Determine the width of this special character@>=
begin
incr(ex_buf_ptr);                               {skip over the |left_brace|}
while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
    begin
    incr(ex_buf_ptr);                   {skip over the |backslash|}
    ex_buf_xptr := ex_buf_ptr;
    while ((ex_buf_ptr < ex_buf_length) and
                (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
        incr(ex_buf_ptr);               {this scans the control sequence}
    if ((ex_buf_ptr < ex_buf_length) and (ex_buf_ptr = ex_buf_xptr)) then
        incr(ex_buf_ptr)                {this skips a nonalpha control seq}
      else
        begin
        control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,
                        ex_buf_ptr-ex_buf_xptr,control_seq_ilk,dont_insert);
        if (hash_found) then
            @<Determine the width of this accented or foreign character@>;
        end;
    while ((ex_buf_ptr < ex_buf_length) and
                (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
    incr(ex_buf_ptr);                   {this skips following |white_space|}
    while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
                                        (ex_buf[ex_buf_ptr] <> backslash)) do
        begin                   {this scans to the next control sequence}
        if (ex_buf[ex_buf_ptr] = right_brace) then
            decr(brace_level)
        else if (ex_buf[ex_buf_ptr] = left_brace) then
            incr(brace_level)
        else
            string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
        incr(ex_buf_ptr);
        end;
    end;
decr(ex_buf_ptr);                       {unskip the |right_brace|}
end


@
Five of the 13 possibilities resort to special information not present
in the |char_width| array; the other eight simply use |char_width|'s
information for the first letter of the control sequence.

@<Determine the width of this accented or foreign character@>=
begin
case (ilk_info[control_seq_loc]) of
    n_ss : string_width := string_width + ss_width;
    n_ae : string_width := string_width + ae_width;
    n_oe : string_width := string_width + oe_width;
    n_ae_upper : string_width := string_width + upper_ae_width;
    n_oe_upper : string_width := string_width + upper_oe_width;
    othercases
        string_width := string_width + char_width[ex_buf[ex_buf_xptr]]
endcases;
end


@
The |built_in| function {\.{write\$}} pops the top (string) literal
and writes it onto the output buffer |out_buf| (which will result in
stuff being written onto the \.{.bbl} file if the buffer fills up).  If
the literal isn't a string, it complains but does nothing else.

@<|execute_fn|({\.{write\$}})@>=
procedure x_write;
begin
pop_lit_stk (pop_lit1,pop_typ1);
if (pop_typ1 <> stk_str) then
    print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
else
    add_out_pool (pop_lit1);
end;



@* Cleaning up.
@^clich\'e-\`a-trois@>
@^fat lady@>
@^turn out lights@>
@^Yogi@>
This section does any last-minute printing and ends the program.

@<Clean up and leave@>=
begin
if ((read_performed) and (not reading_completed)) then
    begin
    print ('Aborted at line ',bib_line_num:0,' of file ');
    print_bib_name;
    end;
trace_and_stat_printing;
@<Print the job |history|@>;
a_close (log_file);
{turn out the lights, the fat lady has sung; it's over, Yogi}
end


@
Here we print |trace| and/or |stat| information, if desired.

@<Procedures and functions for all file I/O, error messages, and such@>=
procedure trace_and_stat_printing;
begin
@#
  trace
  @<Print all \.{.bib}- and \.{.bst}-file information@>;
  @<Print all |cite_list| and entry information@>;
  @<Print the |wiz_defined| functions@>;
  @<Print the string pool@>;
  ecart@/
@#
  stat
  @<Print usage statistics@>;
  tats@/
@#
end;


@
This prints information obtained from the \.{.aux} file about the
other files.

@<Print all \.{.bib}- and \.{.bst}-file information@>=
begin
if (num_bib_files = 1) then
    trace_pr_ln ('The 1 database file is')
  else
    trace_pr_ln ('The ',num_bib_files:0,' database files are');
if (num_bib_files = 0) then
    trace_pr_ln ('   undefined')
  else
    begin
    bib_ptr := 0;
    while (bib_ptr < num_bib_files) do
        begin
        trace_pr ('   ');
        trace_pr_pool_str (cur_bib_str);
        trace_pr_pool_str (s_bib_extension);
        trace_pr_newline;
        incr(bib_ptr);
        end;
    end;
trace_pr ('The style file is ');
if (bst_str = 0) then
    trace_pr_ln ('undefined')
  else
    begin
    trace_pr_pool_str (bst_str);
    trace_pr_pool_str (s_bst_extension);
    trace_pr_newline;
    end;
end


@
In entry-sorted order, this prints an entry's |cite_list| string and,
indirectly, its entry type and entry variables.

@<Print all |cite_list| and entry information@>=
begin
if (all_entries) then
    trace_pr ('all_marker=',all_marker:0,', ');
if (read_performed) then
    trace_pr_ln ('old_num_cites=',old_num_cites:0)
  else
    trace_pr_newline;
trace_pr ('The ',num_cites:0);
if (num_cites = 1) then
    trace_pr_ln (' entry:')
  else
    trace_pr_ln (' entries:');
if (num_cites = 0) then
    trace_pr_ln ('   undefined')
  else
    begin
    sort_cite_ptr := 0;
    while (sort_cite_ptr < num_cites) do
        begin
        if (not read_completed) then    {we didn't finish the \.{read} command}
            cite_ptr := sort_cite_ptr
          else
            cite_ptr := sorted_cites[sort_cite_ptr];
        trace_pr_pool_str (cur_cite_str);
        if (read_performed) then
            @<Print entry information@>
          else
            trace_pr_newline;
        incr(sort_cite_ptr);
        end;
    end;
end


@
This prints information gathered while reading the \.{.bst} and
\.{.bib} files.

@<Print entry information@>=
begin
trace_pr (', entry-type ');
if (type_list[cite_ptr] = undefined) then
    undefined : trace_pr ('unknown')
else if (type_list[cite_ptr] = empty) then
    trace_pr ('--- no type found')
else
    trace_pr_pool_str (hash_text[type_list[cite_ptr]]);
trace_pr_ln (', has entry strings');
@<Print entry strings@>;
trace_pr ('  has entry integers');
@<Print entry integers@>;
trace_pr_ln ('  and has fields');
@<Print fields@>;
end


@
This prints, for the current entry, the strings declared by the
\.{entry} command.

@<Print entry strings@>=
begin
if (num_ent_strs = 0) then
    trace_pr_ln ('    undefined')
else if (not read_completed) then
    trace_pr_ln ('    uninitialized')
else
    begin
    str_ent_ptr := cite_ptr * num_ent_strs;
    while (str_ent_ptr < (cite_ptr+1)*num_ent_strs) do
        begin
        ent_chr_ptr := 0;
        trace_pr ('    "');
        while (entry_strs[str_ent_ptr][ent_chr_ptr] <> end_of_string) do
            begin
            trace_pr (xchr[entry_strs[str_ent_ptr][ent_chr_ptr]]);
            incr(ent_chr_ptr);
            end;
        trace_pr_ln ('"');
        incr(str_ent_ptr);
        end;
    end;
end


@
This prints, for the current entry, the integers declared by the
\.{entry} command.

@<Print entry integers@>=
begin
if (num_ent_ints = 0) then
    trace_pr (' undefined')
else if (not read_completed) then
    trace_pr (' uninitialized')
else
    begin
    int_ent_ptr := cite_ptr*num_ent_ints;
    while (int_ent_ptr < (cite_ptr+1)*num_ent_ints) do
        begin
        trace_pr (' ',entry_ints[int_ent_ptr]:0);
        incr(int_ent_ptr);
        end;
    end;
trace_pr_newline;
end


@
This prints the fields stored for the current entry.

@<Print fields@>=
begin
if (not read_performed) then
    trace_pr_ln ('    uninitialized')
  else
    begin
    field_ptr := cite_ptr * num_fields;
    field_end_ptr := field_ptr + num_fields;
    no_fields := true;
    while (field_ptr < field_end_ptr) do
        begin
        if (field_info[field_ptr] <> missing) then
            begin
            trace_pr ('    "');
            trace_pr_pool_str (field_info[field_ptr]);
            trace_pr_ln ('"');
            no_fields := false;
            end;
        incr(field_ptr);
        end;
    if (no_fields) then
        trace_pr_ln ('    missing');
    end;
end


@
This gives all the |wiz_defined| functions that appeared in the
\.{.bst} file.

@<Print the |wiz_defined| functions@>=
begin
trace_pr_ln ('The wiz-defined functions are');
if (wiz_def_ptr = 0) then
    trace_pr_ln ('   nonexistent')
  else
    begin
    wiz_fn_ptr := 0;
    while (wiz_fn_ptr < wiz_def_ptr) do
        begin
        if (wiz_functions[wiz_fn_ptr] = end_of_def) then
            trace_pr_ln (wiz_fn_ptr:0,'--end-of-def--')
        else if (wiz_functions[wiz_fn_ptr] = quote_next_fn) then
            trace_pr (wiz_fn_ptr:0,'  quote_next_function    ')
        else
            begin
            trace_pr (wiz_fn_ptr:0,'  `');
            trace_pr_pool_str (hash_text[wiz_functions[wiz_fn_ptr]]);
            trace_pr_ln ('''');
            end;
        incr(wiz_fn_ptr);
        end;
   end;
end


@
This includes all the `static' strings (that is, those that are also
in the hash table), but none of the dynamic strings (that is, those
put on the stack while executing \.{.bst} commands).

@<Print the string pool@>=
begin
trace_pr_ln ('The string pool is');
str_num := 1;
while (str_num < str_ptr) do
    begin
    trace_pr (str_num:4, str_start[str_num]:6,' "');
    trace_pr_pool_str (str_num);
    trace_pr_ln ('"');
    incr(str_num);
    end;
end


@
@^statistics@>
These statistics can help determine how large some of the constants
should be and can tell how useful certain |built_in| functions are.
They are written to the same files as tracing information.

@d stat_pr == trace_pr
@d stat_pr_ln == trace_pr_ln
@d stat_pr_pool_str == trace_pr_pool_str

@<Print usage statistics@>=
begin
stat_pr ('You''ve used ',num_cites:0);
if (num_cites = 1) then
    stat_pr_ln (' entry,')
  else
    stat_pr_ln (' entries,');
stat_pr_ln ('            ',wiz_def_ptr:0,' wiz_defined-function locations,');
stat_pr_ln ('            ',str_ptr:0,' strings with ',str_start[str_ptr]:0,
                                                        ' characters,');
blt_in_ptr := 0;
total_ex_count := 0;
while (blt_in_ptr < num_blt_in_fns) do
    begin
    total_ex_count := total_ex_count + execution_count[blt_in_ptr];
    incr(blt_in_ptr);
    end;
stat_pr_ln ('and the built_in function-call counts, ', total_ex_count:0,
                                                        ' in all, are:');
blt_in_ptr := 0;
while (blt_in_ptr < num_blt_in_fns) do
    begin
    stat_pr_pool_str (hash_text[blt_in_loc[blt_in_ptr]]);
    stat_pr_ln (' -- ',execution_count[blt_in_ptr]:0);
    incr(blt_in_ptr);
    end;
end


@
@^bunk, history@>
@^system dependencies@>
@:this can't happen}{\quad History is bunk@>
Some implementations may wish to pass the |history| value to the
operating system so that it can be used to govern whether or not other
programs are started. Here we simply report the history to the user.

@<Print the job |history|@>=
case (history) of
    spotless : do_nothing;
    warning_message : begin
                      if (err_count = 1) then
                          print_ln ('(There was 1 warning)')
                        else
                          print_ln ('(There were ',err_count:0,' warnings)');
                      end;
    error_message : begin
                    if (err_count = 1) then
                        print_ln ('(There was 1 error message)')
                      else
                        print_ln ('(There were ',err_count:0,
                                                        ' error messages)');
                    end;
    fatal_message : print_ln ('(That was a fatal error)');
    othercases begin
               print ('History is bunk');
               print_confusion;
               end
endcases



@* System-dependent changes.
@^system dependencies@>
This section should be replaced, if necessary, by changes to the program
that are necessary to make \BibTeX\ 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 printed program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.



@* Index.
@.this can't happen@>
Here is where you can find all uses of each identifier in the program,
with underlined entries pointing to where the identifier was defined.
If the identifier is only one letter long, however, you get to see only
the underlined entries. All references are to section numbers instead of
page numbers.

This index also lists a few error messages and other aspects of the
program that you might want to look up some day. For example, the
entry for ``system dependencies'' lists all sections that should
receive special attention from people who are installing \TeX\ in a
new operating environment. A list of various things that can't happen
appears under ``this can't happen''$\!$.