[r3155]: trunk / csl / cslbase / csl.c Maximize Restore History

Download this file

csl.c    4433 lines (4151 with data), 152.4 kB

   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
/* csl.c Copyright (C) 1989-2014 Codemist Ltd */
/*
* This is Lisp system for use when delivering Lisp applications
* (such as REDUCE) on pretty-well any computer that has an ANSI
* C compiler.
*/
/**************************************************************************
* Copyright (C) 2014, Codemist Ltd. A C Norman *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are *
* met: *
* *
* * Redistributions of source code must retain the relevant *
* copyright notice, this list of conditions and the following *
* disclaimer. *
* * Redistributions in binary form must reproduce the above *
* copyright notice, this list of conditions and the following *
* disclaimer in the documentation and/or other materials provided *
* with the distribution. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
* COPYRIGHT OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
* DAMAGE. *
*************************************************************************/
/*
* When built using wxWidgets this code will use the Latin Modern
* fonts, which are subject to the LaTeX Project Public License. This
* comment is places somewhere I view as pretty prominent - ie at the head
* of the source of the main part of the code. I have further comments
* and explanation lower in this file such that invoking CSL or
* Reduce with an option "--help" will generate the required credits and
* notices, so if this matters to you search this file for the string
* "LaTeX" for more information.
*/
/* $Id: csl.c 3058 2015-03-29 21:55:44Z arthurcnorman $ */
/*
* There are a number of comments here introduced with exclamation
* mark after the "/" and "*" that start them. These contain material
* to be extracted to form documentation.
*/
/*!!! csl
* \documentclass[a4paper,11pt]{article}
* \title{CSL reference}
* \author{A C Norman}
* \usepackage{makeidx}
* \makeindex
* \begin{document}
* \maketitle
*/
/*!! intro [00] \section{Introduction}
* This is reference material for CSL. The Lisp identifiers mentioned here
* are the ones that are initially present in a raw CSL image. Some
* proportion of them are not really intended to be used by end-users but
* are merely the internal components of some feature.
*/
/*!! options [01] \section{Command-line options}
* The items shown here are the ones that are recognized on the CSL command
* line. In general an option that requires an argument can be written as either
* {\ttfamily -x yyy} or as {\ttfamily -xyyy}. Arguments should be case
* insensitive.
* \begin{description}
*/
/*! options [~~~~~~~~] \end{description} % end of options section [csl.c]
*/
/*!! predef [02] \section{Predefined variables}
* \begin{description}
*/
/*! predef [~~~~~~~~] \end{description} % end of predef section [csl.c]
*/
/*!! flags [04] \section{Flags and Properties}
*
* Most of tags here are probably not much use to end-users, but I am
* noting them as a matter of completeness.
*
* \begin{description}
*/
/*! flags [~~~~~~~~] \end{description} % end of flags section [csl.c]
*/
/*!! index [~~~~~~~~] % The index will go here
* \printindex
* \end{document}
*/
#define INCLUDE_ERROR_STRING_TABLE 1
#include "headers.h"
#undef INCLUDE_ERROR_STRING_TABLE
#include "version.h"
#ifdef SOCKETS
#include "sockhdr.h"
#endif
#ifndef WIN32
#include <sys/wait.h>
#endif
#ifdef HAVE_UNISTD_H
#include <sys/unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifndef HAVE_FWIN
/*
* During startup on a windowed system if I needed to report an error
* but the window was minimised I need to restore it...
*/
#define fwin_restore()
#endif
#ifdef SOCKETS
#if 0
/* Being removed */
static int port_number, remote_store, current_users, max_users;
SOCKET socket_server;
clock_t cpu_timeout;
time_t elapsed_timeout;
static int char_to_socket(int c);
#endif
int sockets_ready = 0;
#endif
/*
* These flags are used to ensure that protected symbols don't get
* overwritten by default, and that the system keeps quiet about it.
*/
CSLbool symbol_protect_flag = YES;
CSLbool warn_about_protected_symbols = NO;
#if defined WINDOW_SYSTEM && !defined EMBEDDED
CSLbool use_wimp;
#endif
#ifdef USE_MPI
int32_t mpi_rank,mpi_size;
#endif
/*****************************************************************************/
/* Error reporting and recovery */
/*****************************************************************************/
volatile char stack_contents_temp = 0;
#ifdef CHECK_STACK
/*
* Some computers are notably unhelpful about their behaviour when the system
* stack overflows. As a debugging tool on such machines I can do limited
* software checking by inserting explicit calls to this function in places
* I think may be critical. I impose an arbitrary limit on the stack size,
* but that is better than no checking and random corruption - maybe. Please
* do not enable CHECK_STACK unless it is really necessary to hunt a bug,
* since it is miserably expensive and crude. I appear to observe that on
* my Windows versions (both 32 and 64-bit) there is around 2M of stack, while
* on Linux the amount of stack is set via ulimit and is not fixed.
*/
#define C_STACK_ALLOCATION 1000000
static int spset = 0;
static intptr_t spbase = 0, spmin;
static uintptr_t stack_depth[C_STACK_ALLOCATION];
static int stack_line[C_STACK_ALLOCATION];
static char *stack_file[C_STACK_ALLOCATION];
static uintptr_t c_stack_ptr = 0;
int check_stack(char *file, int line)
{
uintptr_t temp = (intptr_t)&temp;
char *file1;
int first = 1;
if (!spset)
{ spbase = spmin = temp;
spset = 1;
c_stack_ptr = 0;
stack_depth[0] = temp;
stack_line[0] = line;
stack_file[0] = file;
}
if (temp < stack_depth[c_stack_ptr] && c_stack_ptr<C_STACK_ALLOCATION-2)
c_stack_ptr++;
else while (temp > stack_depth[c_stack_ptr] && c_stack_ptr>0)
c_stack_ptr--;
stack_depth[c_stack_ptr] = temp;
stack_line[c_stack_ptr] = line;
file1 = strrchr(file, '/');
stack_file[c_stack_ptr] = (file1 == NULL ? file : file1+1);
if (temp < spmin-250) /* Only check at granularity of 250 bytes */
{ int i, j=0;
term_printf("Stack depth %u at file %s line %d\n",
(unsigned int)(spbase-temp), file, line);
term_printf("c_stack_ptr = %d\n", c_stack_ptr);
for (i=c_stack_ptr; i>=0; i--)
{ if (first || stack_file[i][0] != '@')
term_printf(" %s:%d", stack_file[i], stack_line[i]);
if (stack_file[i][0] != '@') first = 0;
if ((++j)%5 ==4) term_printf("\n");
}
term_printf("\n");
spmin = temp;
if (temp < spbase-C_STACK_ALLOCATION ||
temp < C_stack_limit) return 1;
}
return 0;
}
void show_stack()
{
int i, j=0;
term_printf("\n+++ Stack overflow traceback...\n");
for (i=c_stack_ptr; i>=0; i--)
{ if (j++ > 10)
{ term_printf("\n");
j = 0;
}
term_printf(" %d) %s:%d", i, stack_file[i], stack_line[i]);
}
term_printf("\n");
}
#endif
/*
* error_message_table was defined in cslerror.h since that way I can keep its
* contents textually close to the definitions of the message codes that it
* has to relate to.
*/
#define errcode(n) error_message_table[n]
Lisp_Object MS_CDECL error(int nargs, int code, ...)
/*
* nargs indicates how many values have been provided AFTER the
* code. Thus nargs==0 will just display a simple message, nargs==1
* will be a message plus a value and so on. I will expect that the
* number of actual args here is well within any limits that I ought to
* impose.
*/
{
va_list a;
int i;
Lisp_Object nil = C_nil, w1;
Lisp_Object *w = (Lisp_Object *)&work_1;
if (nargs > ARG_CUT_OFF) nargs = ARG_CUT_OFF;
if (miscflags & HEADLINE_FLAG)
{ err_printf("\n+++ Error %s: ", errcode(code));
/*
* There is now some painful shuffling around to get all the args
* to error() moved over onto the Lisp stack so that is garbage collection
* is triggered during printing all will be well.
*/
va_start(a, code);
for (i=0; i<nargs; i++) *w++ = va_arg(a, Lisp_Object);
va_end(a);
for (i=0; i<nargs; i++) push(*--w);
if (code != err_stack_overflow) /* Be cautious here! */
{ stackcheck0(nargs);
}
for (i=0; i<nargs; i++)
{ Lisp_Object p;
pop(p);
loop_print_error(p);
err_printf("\n");
}
}
if ((w1 = qvalue(break_function)) != nil &&
symbolp(w1) &&
qfn1(w1) != undefined1)
{ (*qfn1(w1))(qenv(w1), nil);
ignore_exception();
}
/*
* After doing this is is necessary to be VERY careful, since nil is
* used as a base register for lots of things... Still this is the
* cheapest way I can see to mark the need for unwinding.
*/
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
return nil;
}
Lisp_Object MS_CDECL cerror(int nargs, int code1, int code2, ...)
/*
* nargs indicated the number of EXTRA args after code1 & code2.
*/
{
Lisp_Object nil = C_nil, w1;
va_list a;
int i;
Lisp_Object *w = (Lisp_Object *)&work_1;
if (nargs > ARG_CUT_OFF) nargs = ARG_CUT_OFF;
if (miscflags & HEADLINE_FLAG)
{ err_printf("\n+++ Error %s, %s: ", errcode(code1), errcode(code2));
va_start(a, code2);
for (i=0; i<nargs; i++) *w++ = va_arg(a, Lisp_Object);
va_end(a);
for (i=0; i<nargs; i++) push(*--w);
stackcheck0(nargs-2);
nil = C_nil;
for (i=0; i<nargs; i++)
{ Lisp_Object p;
pop(p);
loop_print_error(p);
err_printf("\n");
}
}
if ((w1 = qvalue(break_function)) != nil &&
symbolp(w1) &&
qfn1(w1) != undefined1)
{ (*qfn1(w1))(qenv(w1), nil);
ignore_exception();
}
/*
* After doing this is is necessary to be VERY careful, since nil is
* used as a base register for lots of things... Still this is the
* cheapest way I can see to mark the need for unwinding.
*/
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
return nil;
}
/*
* This can be used when a resource expires...
*/
Lisp_Object resource_exceeded()
{
Lisp_Object nil = C_nil;
exit_reason = UNWIND_RESOURCE;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
return nil;
}
Lisp_Object interrupted(Lisp_Object p)
/*
* Could return onevalue(p) to proceed from the interrupt event...
*/
{
Lisp_Object nil = C_nil, w;
/*
* If I have a windowed system I expect that the mechanism for
* raising an exception will have had a menu that gave me a chance
* to decide whether to proceed or abort. Thus the following code
* is only needed if there is no window system active. On some systems
* this may be an active check.
*/
#ifdef HAVE_FWIN
if ((fwin_windowmode() & FWIN_IN_WINDOW) == 0)
#else
#if defined WINDOW_SYSTEM && !defined EMBEDDED
if (!use_wimp)
#endif
#endif
{
if (clock_stack == &consolidated_time[0])
{ clock_t t0 = read_clock();
/*
* On at least some (Unix) systems clock_t is a 32-bit signed value
* and CLOCKS_PER_SEC = 1000000. The effect is that integer overflow
* occurs after around 35 minutes of running. I must therefore check the
* clock and move information into a floating point variable at least
* every half-hour. With luck I will do it more like 20 times per second
* because I have code muck like this in a tick handler that is activated
* on a rather regular basis on at least some systems. On others this
* clock overfow issue is a bit of a pain and I really ought just to use
* a different low-level API for timing that can not so suffer. But
* as a bit of a fall-back I will see if the garbage collector can
* consolidate time for me and since I ignore time spent waiting for the
* keyboard overflows due to 35 minutes of activity there will not hurt so
* I am probably at worst at risk if I can compute for a solid half
* hour without triggering garbage collection.
*/
double delta = (double)(t0 - base_time)/(double)CLOCKS_PER_SEC;
base_time = t0;
consolidated_time[0] += delta;
}
#ifndef NAG
#ifdef HAVE_FWIN
term_printf("\n");
#else
term_printf(
"\n+++ [%.2f+%.2f] Type C to continue, A to abort, X to exit\n",
consolidated_time[0], gc_time);
#endif
ensure_screen(); nil = C_nil;
if (exception_pending()) return nil;
push(prompt_thing);
prompt_thing = nil; /* switch off the regular prompts */
#ifdef HAVE_FWIN
fwin_set_prompt("+++ Type C to continue, A to abort, X to exit: ");
#endif
other_read_action(READ_FLUSH, lisp_terminal_io);
for (;;)
{ int c = char_from_terminal(nil);
/*
* Note that I explicitly say "char_from_terminal()" here - this is because
* I do not expect to be interrupted unless there was a terminal available
* to send the interrupt! This is in fact a slightly marginal assumption.
*/
switch (c)
{
case 'c': case 'C': /* proceed as if no interrupt */
pop(prompt_thing);
#ifdef HAVE_FWIN
fwin_set_prompt(prompt_string);
#endif
return onevalue(p);
case 'a': case 'A': /* raise an exception */
break;
case 'x': case 'X':
my_exit(EXIT_FAILURE); /* Rather abrupt */
case '\n':
#ifndef HAVE_FWIN
term_printf("C to continue, A to abort, X to exit: ");
#endif
ensure_screen(); nil = C_nil;
if (exception_pending()) return nil;
continue;
default: /* wait for A or C */
continue;
}
break;
}
pop(prompt_thing);
#ifdef HAVE_FWIN
fwin_set_prompt(prompt_string);
#endif
#endif
}
/*
* now for the common code to be used in all cases.
*/
if (miscflags & HEADLINE_FLAG)
err_printf("+++ Interrupted\n");
if ((w = qvalue(break_function)) != nil &&
symbolp(w) &&
qfn1(w) != undefined1)
{ (*qfn1(w))(qenv(w), nil);
ignore_exception();
}
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
return nil;
}
Lisp_Object aerror(char *s)
{
Lisp_Object nil = C_nil, w;
if (miscflags & HEADLINE_FLAG)
err_printf("+++ Error bad args for %s\n", s);
if ((w = qvalue(break_function)) != nil &&
symbolp(w) &&
qfn1(w) != undefined1)
{ (*qfn1(w))(qenv(w), nil);
ignore_exception();
}
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
return nil;
}
Lisp_Object aerror0(char *s)
{
Lisp_Object nil = C_nil, w;
if (miscflags & HEADLINE_FLAG)
err_printf("+++ Error: %s\n", s);
if ((w = qvalue(break_function)) != nil &&
symbolp(w) &&
qfn1(w) != undefined1)
{ (*qfn1(w))(qenv(w), nil);
ignore_exception();
}
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
#ifdef COMMON
/*
* This is to help me debug in the face of low level system crashes
*/
if (spool_file) fflush(spool_file);
#endif
return nil;
}
Lisp_Object aerror1(char *s, Lisp_Object a)
{
Lisp_Object nil = C_nil, w;
if (miscflags & HEADLINE_FLAG)
{ err_printf("+++ Error: %s ", s);
loop_print_error(a);
err_printf("\n");
}
if ((w = qvalue(break_function)) != nil &&
symbolp(w) &&
qfn1(w) != undefined1)
{ (*qfn1(w))(qenv(w), nil);
ignore_exception();
}
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
#ifdef COMMON
/*
* This is to help me debug in the face of low level system crashes
*/
if (spool_file) fflush(spool_file);
#endif
return nil;
}
Lisp_Object aerror2(char *s, Lisp_Object a, Lisp_Object b)
{
Lisp_Object nil = C_nil, w;
if (miscflags & HEADLINE_FLAG)
{ err_printf("+++ Error: %s ", s);
loop_print_error(a);
err_printf(" ");
loop_print_error(b);
err_printf("\n");
}
if ((w = qvalue(break_function)) != nil &&
symbolp(w) &&
qfn1(w) != undefined1)
{ (*qfn1(w))(qenv(w), nil);
ignore_exception();
}
exit_reason = (miscflags & ARGS_FLAG) ? UNWIND_ERROR :
(miscflags & FNAME_FLAG) ? UNWIND_FNAME :
UNWIND_UNWIND;
exit_value = exit_tag = nil;
exit_count = 0;
flip_exception();
#ifdef COMMON
/*
* This is to help me debug in the face of low level system crashes
*/
if (spool_file) fflush(spool_file);
#endif
return nil;
}
static Lisp_Object wrong(int wanted, int given, Lisp_Object env)
{
char msg[64];
Lisp_Object nil = C_nil;
CSL_IGNORE(nil);
sprintf(msg, "Function called with %d args where %d wanted", given, wanted);
if (is_cons(env)) env = qcdr(env);
if ((miscflags & HEADLINE_FLAG) && is_vector(env))
{ Lisp_Object fname = elt(env, 0);
err_printf("\nCalling ");
loop_print_error(fname);
err_printf("\n");
}
return aerror(msg);
}
Lisp_Object too_few_2(Lisp_Object env, Lisp_Object a1)
{
CSL_IGNORE(a1);
return wrong(2, 1, env);
}
Lisp_Object too_many_1(Lisp_Object env, Lisp_Object a1, Lisp_Object a2)
{
CSL_IGNORE(a1);
CSL_IGNORE(a2);
return wrong(1, 2, env);
}
Lisp_Object wrong_no_0a(Lisp_Object env, Lisp_Object a1)
{
CSL_IGNORE(a1);
return wrong(0, 1, env);
}
Lisp_Object wrong_no_0b(Lisp_Object env, Lisp_Object a1, Lisp_Object a2)
{
CSL_IGNORE(a1);
CSL_IGNORE(a2);
return wrong(0, 2, env);
}
Lisp_Object wrong_no_3a(Lisp_Object env, Lisp_Object a1)
{
CSL_IGNORE(a1);
return wrong(3, 1, env);
}
Lisp_Object wrong_no_3b(Lisp_Object env, Lisp_Object a1, Lisp_Object a2)
{
CSL_IGNORE(a1);
CSL_IGNORE(a2);
return wrong(3, 2, env);
}
Lisp_Object wrong_no_na(Lisp_Object env, Lisp_Object a1)
{
CSL_IGNORE(a1);
if (is_cons(env) && is_bps(qcar(env)))
return wrong(((unsigned char *)data_of_bps(qcar(env)))[0], 1, env);
else return aerror("function called with 1 arg when 0 or >= 3 wanted");
}
Lisp_Object wrong_no_nb(Lisp_Object env, Lisp_Object a1, Lisp_Object a2)
{
CSL_IGNORE(a1);
CSL_IGNORE(a2);
if (is_cons(env) && is_bps(qcar(env)))
return wrong(((unsigned char *)data_of_bps(qcar(env)))[0], 2, env);
else return aerror("function called with 2 args when 0 or >= 3 wanted");
}
Lisp_Object MS_CDECL wrong_no_1(Lisp_Object env, int nargs, ...)
{
CSL_IGNORE(env);
CSL_IGNORE(nargs);
return wrong(1, nargs, env);
}
Lisp_Object MS_CDECL wrong_no_2(Lisp_Object env, int nargs, ...)
{
CSL_IGNORE(env);
CSL_IGNORE(nargs);
return wrong(2, nargs, env);
}
Lisp_Object MS_CDECL bad_specialn(Lisp_Object env, int nargs, ...)
{
CSL_IGNORE(env);
CSL_IGNORE(nargs);
return aerror("call to special form");
}
void MS_CDECL fatal_error(int code, ...)
{
/*
* Note that FATAL error messages are sent to the terminal, not to the
* error output stream. This is because the error output stream may be
* corrupted in such dire circumstances.
*/
term_printf("+++ Fatal error %s\n", errcode(code));
if (spool_file != NULL)
{
#ifdef COMMON
fprintf(spool_file, "\nFinished dribbling to %s.\n", spool_file_name);
#else
fprintf(spool_file, "\n+++ Transcript terminated after error +++\n");
#endif
fclose(spool_file);
spool_file = NULL;
}
my_exit(EXIT_FAILURE);
}
static char *dependency_file = NULL;
static char **dependency_map = NULL;
static int dependency_count = 0, dependency_capacity = 0;
void report_file(const char *s)
{
char *c;
const char *s1;
int i;
if (dependency_file == NULL) return;
/*
* In a really odd way I will avoid recording inline-defs.dat as a
* dependency and insist that if it is to be one that the Makefile should
* list that explicitly. This helps me avoid some dependency circularities.
* but note that inline-defs.dat may not be in the current directory.
* Use of that file-name for any other purposes may cause issues!
* Here I find the final component of the path - ie the bit following the
* last "/" or "\" present if there is one of those.
*/
if ((s1 = strrchr(s, '/')) != NULL) s1++;
else if ((s1 = strrchr(s, '\\')) != NULL) s1++;
else s1 = s;
if (strcmp(s1, "inline-defs.dat") == 0) return;
if (dependency_count >= dependency_capacity)
{ dependency_capacity = 2*dependency_capacity + 40;
dependency_map = (char **)realloc(dependency_map,
dependency_capacity*sizeof(char *));
if (dependency_map == NULL)
{ dependency_capacity = dependency_count = 0;
return;
}
}
for (i=0; i<dependency_count; i++)
{ if (strcmp(s, dependency_map[i]) == 0) return; /* already recorded */
}
c = (char *)malloc(strlen(s) + 1);
if (c == NULL) return;
strcpy(c, s);
dependency_map[dependency_count++] = c;
}
static int alphorder(const void *a, const void *b)
{
char *aa = *(char **)a;
char *bb = *(char **)b;
return strcmp(aa, bb);
}
static void report_dependencies()
{
FILE *f;
int i, c;
char *p;
if (dependency_file == NULL) return;
f = fopen(dependency_file, "w");
if (f == NULL) return;
p = strrchr(dependency_file, '.');
fprintf(f, "%.*sdep = \\\n", (int)(p==NULL ? strlen(dependency_file) :
(p - dependency_file)),
dependency_file);
qsort(dependency_map, dependency_count,
sizeof(char *), alphorder);
for (i=0; i<dependency_count; i++)
{ p = dependency_map[i];
putc('\t', f);
/*
* If I am on Windows some files may at this stage be shown with names
* of the form "X:/..." where X denotes the drive. But the dependencies I
* am creating are for the benefit of the cygwin version of GNU make, and
* that will get seriously upset by the colon, thinking it is indicating that
* I have multiple targets. So I will map "X:/" onto "/cygdrive/x/".
*/
if (p[0] != 0 &&
p[1] == ':' &&
(p[2] == '/' || p[2] == '\\'))
{ fprintf(f, "/cygdrive/%c", (char)tolower((unsigned char)p[0]));
p+=2;
}
while ((c = *p++) != 0)
{ if (c == ' ') putc('\\', f); /* for spaces in file-name */
putc(c == '\\' ? '/' : c, f);
}
if (i < dependency_count-1)
{ putc(' ', f);
putc('\\', f);
}
putc('\n', f);
}
putc('\n', f);
fclose(f);
dependency_file = NULL;
}
#ifndef __cplusplus
#ifdef USE_SIGALTSTACK
sigjmp_buf my_exit_buffer;
#else
jmp_buf my_exit_buffer;
#endif
volatile int my_return_code = 0;
#endif
void my_exit(int n)
{
/*
* This all seems a HORRID MESS. It is here because of a general need to
* tidy up at the end of a run, and the fact that I may be running as
* a sub-task of some other package so I can not let atexit() take the
* strain since although I am exiting CSL here I may not be (quite yet)
* leaving the whole of the current application.
*/
report_dependencies();
#ifdef USE_MPI
MPI_Finalize();
#endif
ensure_screen();
#ifdef SOCKETS
if (sockets_ready) WSACleanup();
#endif
#ifdef WINDOW_SYSTEM
pause_for_user();
#endif
#ifdef HAVE_FWIN
#ifdef __cplusplus
throw n;
#else
/*
* When I am compiling in C I will be ultra-cautions and only ever use
* "1" as the second argument to longjmp. Here, which is the only place
* where I want to hand back a value I might (often!) want to hand back the
* value "0", so I put it in a static variable (and make that volatile to
* help it survive setjmp/longjmp). Doing things this was is also a valuable
* temporary expedient for the 64-bit variant on mingw at a stage where that
* is not fully settled!
*/
my_return_code = n;
#ifdef USE_SIGALTSTACK
siglongjmp(my_exit_buffer, 1);
#else
longjmp(my_exit_buffer, 1);
#endif
#endif
#else
#if defined(WIN32) && defined(NAG)
{ extern void sys_abort(int);
sys_abort(n);
}
#else
exit(n);
#endif
#endif
}
static int return_code = 0;
CSLbool segvtrap = YES;
CSLbool batch_flag = NO;
CSLbool ignore_restart_fn = NO;
#ifdef USE_SIGALTSTACK
static unsigned char signal_stack_block[SIGSTKSZ];
stack_t signal_stack;
#endif
#ifdef HAVE_CRLIBM
static unsigned long long crlibstatus = 0;
static void tidy_up_crlibm()
{
crlibm_exit(crlibstatus);
}
#endif
#if defined __linux__ && defined DEBUG
/*
* This is an experiment as to a possible return to generating a
* tick-stream using a system timer rather than "software ticks".
*/
volatile int blipflag = 0;
int64_t blipcount = 0, startblip = 0;
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
void alarm_handler(int signum)
{
blipflag = 1;
}
#endif
#ifdef DEBUG
char debug_trail[32][32] =
{
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", ""
};
char debug_trail_file[32][32] =
{
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", ""
};
int debug_trail_line[32] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
int debug_trailp = 0;
void debug_record_raw(const char *data, const char *file, int line)
{
const char *f1 = strrchr(file, '/');
if (f1 != NULL) f1++;
else f1 = file;
if (data != NULL)
{ strncpy(debug_trail[debug_trailp], data, 32);
strncpy(debug_trail_file[debug_trailp], f1, 32);
debug_trail_line[debug_trailp] = line;
debug_trailp = (debug_trailp+1)%32;
}
}
void debug_record_int_raw(const char *data, int n, const char *file, int line)
{
const char *f1 = strrchr(file, '/');
if (f1 != NULL) f1++;
else f1 = file;
if (data != NULL)
{ sprintf(debug_trail[debug_trailp], "%s%d", data, n);
strncpy(debug_trail_file[debug_trailp], f1, 32);
debug_trail_line[debug_trailp] = line;
debug_trailp = (debug_trailp+1)%32;
}
}
void debug_show_trail_raw(const char *msg, const char *file, int line)
{
int i;
const char *f1 = strrchr(file, '/');
if (f1 != NULL) f1++;
printf("\n+++++ Debug trail %s\n", msg);
for (i=0; i<32; i++)
{ if (debug_trail[debug_trailp][0] == 0) continue;
printf("%d: %.32s at %.32s:%d\n", i, debug_trail[debug_trailp],
debug_trail_file[debug_trailp], debug_trail_line[debug_trailp]);
debug_trailp = (debug_trailp+1)%32;
}
printf("Current at %d:%d", f1, line);
fflush(stdout);
}
#endif
static void lisp_main(void)
{
Lisp_Object nil;
int i;
#if defined __linux__ && defined DEBUG
struct itimerval tv;
blipflag = 0;
blipcount = 0;
signal(SIGALRM, alarm_handler);
tv.it_interval.tv_usec = 900000;
tv.it_interval.tv_sec = 0;
tv.it_value.tv_usec = 900000;
tv.it_value.tv_sec = 0;
setitimer(ITIMER_REAL, &tv, NULL);
#endif
#ifdef USE_SIGALTSTACK
/*
* If I get a SIGSEGV that is caused by a stack overflow then I am in
* a world of pain because the regular stack does not have space to run my
* exception handler. So where I can I will arrange that the exception
* handler runs in its own small stack. This may itself lead to pain,
* but perhaps less?
*/
signal_stack.ss_sp = (void *)signal_stack_block;
signal_stack.ss_size = SIGSTKSZ;
signal_stack.ss_flags = 0;
sigaltstack(&signal_stack, (stack_t *)0);
#endif
#ifndef __cplusplus
/*
* The setjmp here is to provide a long-stop for untrapped
* floating point exceptions.
*/
#ifdef USE_SIGALTSTACK
sigjmp_buf this_level, *save_level = errorset_buffer;
#else
jmp_buf this_level, *save_level = errorset_buffer;
#endif
#endif
#ifdef HAVE_CRLIBM
crlibstatus = crlibm_init();
atexit(tidy_up_crlibm);
#endif
tty_count = 0;
while (YES)
/*
* The sole purpose of the while loop here is to allow me to proceed
* for a second try if I get a (cold-start) call.
*/
{ Lisp_Object *save = stack;
nil = C_nil;
#ifndef __cplusplus
errorset_buffer = &this_level;
#endif
errorset_msg = NULL;
#ifdef __cplusplus
try
#else
#ifdef USE_SIGALTSTACK
if (!sigsetjmp(this_level, -1))
#else
if (!setjmp(this_level))
#endif
#endif
{ nil = C_nil;
terminal_pushed = NOT_CHAR;
if (supervisor != nil && !ignore_restart_fn)
{ miscflags |= BACKTRACE_MSG_BITS;
/*
* Here I reconstruct the argument that I passed in (restart_csl f a).
*/
if (exit_charvec != NULL)
{ Lisp_Object a = read_from_vector(exit_charvec);
nil = C_nil;
if (exception_pending())
{ flip_exception();
a = nil;
}
free(exit_charvec);
exit_charvec = NULL;
push(a);
#ifndef NO_BYTECOUNT
name_of_caller = "restart function";
#endif
apply(supervisor, 1, nil, supervisor, 0);
}
else
{
#ifndef NO_BYTECOUNT
name_of_caller = "restart function";
#endif
apply(supervisor, 0, nil, supervisor, 0);
}
}
/*
* Here the default read-eval-print loop used if the user has not provided
* a supervisor function.
*/
else read_eval_print(lisp_true);
}
#ifdef __cplusplus
catch (char *)
#else
else
#endif
{ nil = C_nil;
if (errorset_msg != NULL)
{ term_printf("\n%s detected\n", errorset_msg);
errorset_msg = NULL;
}
unwind_stack(save, NO);
exit_reason = UNWIND_ERROR;
flip_exception();
#ifndef UNDER_CE
signal(SIGFPE, low_level_signal_handler);
#ifdef USE_SIGALTSTACK
/* SIGSEGV will be handled on the alternative stack */
{ struct sigaction sa;
sa.sa_handler = low_level_signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK | SA_RESETHAND;
if (segvtrap) sigaction(SIGSEGV, &sa, NULL);
}
#else
if (segvtrap) signal(SIGSEGV, low_level_signal_handler);
#endif
#ifdef SIGBUS
if (segvtrap) signal(SIGBUS, low_level_signal_handler);
#endif
#ifdef SIGILL
if (segvtrap) signal(SIGILL, low_level_signal_handler);
#endif
#endif
}
nil = C_nil;
if (exception_pending())
{ flip_exception();
if (exit_reason == UNWIND_RESTART)
{ if (exit_tag == fixnum_of_int(0)) /* "stop" */
return_code = (int)int_of_fixnum(exit_value);
else if (exit_tag == fixnum_of_int(1)) /* "preserve" */
{ char *msg = "";
int len = 0;
return_code = EXIT_SUCCESS;
compression_worth_while = 128;
if (is_vector(exit_value) &&
type_of_header(vechdr(exit_value)) == TYPE_STRING)
{ msg = &celt(exit_value, 0);
len = (int)(length_of_header(vechdr(exit_value)) - CELL);
}
preserve(msg, len);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return_code = EXIT_FAILURE;
}
}
else if (exit_tag == fixnum_of_int(3)) /* "preserve & restart" */
{ char *msg = "";
int len = 0;
int32_t fd = stream_pushed_char(lisp_terminal_io);
Lrds(nil, nil);
Lwrs(nil, nil);
return_code = EXIT_SUCCESS;
compression_worth_while = 128;
if (is_vector(exit_value) &&
type_of_header(vechdr(exit_value)) == TYPE_STRING)
{ msg = &celt(exit_value, 0);
len = (int)(length_of_header(vechdr(exit_value)) - CELL);
}
preserve(msg, len);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return_code = EXIT_FAILURE;
}
for (i=0; i<pages_count; i++)
{ char *w = (char *)pages[i];
if (!(w > big_chunk_start && w <= big_chunk_end))
continue;
pages[i] = pages[--pages_count];
i--;
}
while (vheap_pages_count != 0)
{ char *w = (char *)vheap_pages[--vheap_pages_count];
if (!(w > big_chunk_start && w <= big_chunk_end))
pages[pages_count++] = w;
}
while (heap_pages_count != 0)
{ char *w = (char *)heap_pages[--heap_pages_count];
if (!(w > big_chunk_start && w <= big_chunk_end))
pages[pages_count++] = w;
}
while (bps_pages_count != 0)
{ char *w = (char *)bps_pages[--bps_pages_count];
if (!(w > big_chunk_start && w <= big_chunk_end))
pages[pages_count++] = w;
}
{ char *w = big_chunk_start + NIL_SEGMENT_SIZE;
char *w1 = w + CSL_PAGE_SIZE + 16;
while (w1 <= big_chunk_end)
{ if (w != (char *)stacksegment)
pages[pages_count++] = w;
w = w1;
w1 = w + CSL_PAGE_SIZE + 16;
}
}
CSL_MD5_Init();
CSL_MD5_Update((unsigned char *)"Initial State", 13);
IreInit();
setup(1, 0.0); /* warm start mode */
exit_tag = exit_value = nil;
exit_reason = UNWIND_NULL;
stream_pushed_char(lisp_terminal_io) = fd;
interrupt_pending = already_in_gc = NO;
tick_pending = tick_on_gc_exit = NO;
continue;
}
else /* "restart" */
{ int32_t fd = stream_pushed_char(lisp_terminal_io);
char new_module[64], new_fn[64]; /* Limited name length */
int cold_start;
cold_start = (exit_value == nil);
/*
* Of course a tick may very well have happened rather recently - so
* I will flush it out now just to clear the air.
*/
if (stack >= stacklimit)
{ reclaim(nil, "stack", GC_STACK, 0);
ignore_exception();
}
cold_start = (exit_value == nil);
Lrds(nil, nil);
Lwrs(nil, nil);
/*
* If either of the above two calls to rds/wrs were to fail I would
* be in a big mess.
*/
if (!cold_start)
{ new_module[0] = 0;
new_fn[0] = 0;
if (exit_value != lisp_true)
{ Lisp_Object modname = nil;
if (is_cons(exit_value))
{ modname = qcar(exit_value);
exit_value = qcdr(exit_value);
if (is_cons(exit_value))
exit_value = qcar(exit_value);
}
if (symbolp(modname) && modname != nil)
{ modname = get_pname(modname);
if (exception_pending()) ignore_exception();
else
{ Header h = vechdr(modname);
int32_t len = length_of_header(h) - CELL;
if (len > 63) len = 63;
memcpy(new_module,
(char *)modname + (CELL - TAG_VECTOR),
(size_t)len);
new_module[len] = 0;
}
}
if (symbolp(exit_value) && exit_value != nil)
{ exit_value = get_pname(exit_value);
if (exception_pending()) ignore_exception();
else
{ Header h = vechdr(exit_value);
int32_t len = length_of_header(h) - CELL;
if (len > 63) len = 63;
memcpy(new_fn,
(char *)exit_value + (CELL - TAG_VECTOR),
(size_t)len);
new_fn[len] = 0;
}
}
}
}
/*
* This puts all recorded heap pages back in the main pool, but there is
* as of March 2010 a concern in the case that an initial heap image came
* from a 32-bit machine and I am now running on a 64-bit one. That concerns
* both extra pages allocated at the first startup to give the effect of
* temporarily double-sized pages (there is a space leak there that may
* lose all those pages!) and the expectation there that pages in the map
* are neatly contiguous in memory (and when returned to the pool here that
* expectation usually fails). Oh dear!
* As a rather grungy recovery what I will do is to recycle all of the
* pages that are NOT in the contiguous chunk and then re-create a neat
* map of contiguous space from the bits that are. So first I must remove
* any contiguous pages from the list of ones marked as free...
*/
for (i=0; i<pages_count; i++)
{ char *w = (char *)pages[i];
if (!(w > big_chunk_start && w <= big_chunk_end))
continue;
/*
* Here the page shown as free is one in the contiguous block. Move in
* the final page to fill the gap here and try again.
*/
pages[i] = pages[--pages_count];
i--;
}
/*
* Next recycle all the non-contiguous pages that have been in use.
*/
while (vheap_pages_count != 0)
{ char *w = (char *)vheap_pages[--vheap_pages_count];
if (!(w > big_chunk_start && w <= big_chunk_end))
pages[pages_count++] = w;
}
while (heap_pages_count != 0)
{ char *w = (char *)heap_pages[--heap_pages_count];
if (!(w > big_chunk_start && w <= big_chunk_end))
pages[pages_count++] = w;
}
while (bps_pages_count != 0)
{ char *w = (char *)bps_pages[--bps_pages_count];
if (!(w > big_chunk_start && w <= big_chunk_end))
pages[pages_count++] = w;
}
/*
* Finally rebuild a contiguous block of pages from the wholesale block.
*/
{ char *w = big_chunk_start + NIL_SEGMENT_SIZE;
char *w1 = w + CSL_PAGE_SIZE + 16;
while (w1 <= big_chunk_end)
{ if (w != (char *)stacksegment)
pages[pages_count++] = w;
w = w1;
w1 = w + CSL_PAGE_SIZE + 16;
}
}
/*
* When I call restart-csl I will leave the random number generator where it
* was. Anybody who wants to reset if either to a freshly randomised
* configuration or to a defined condition must do so for themselves. For
* people who do not care too much what I do here is probably acceptable!
*/
CSL_MD5_Init();
CSL_MD5_Update((unsigned char *)"Initial State", 13);
IreInit();
setup(cold_start ? 0 : 1, 0.0);
exit_tag = exit_value = nil;
exit_reason = UNWIND_NULL;
stream_pushed_char(lisp_terminal_io) = fd;
interrupt_pending = already_in_gc = NO;
tick_pending = tick_on_gc_exit = NO;
if (!cold_start && new_fn[0] != 0)
{ Lisp_Object w;
if (new_module[0] != 0)
{ w = make_undefined_symbol(new_module);
Lload_module(nil, w);
ignore_exception();
}
w = make_undefined_symbol(new_fn);
nil = C_nil;
if (exception_pending()) ignore_exception();
else supervisor = w;
}
continue;
}
}
}
/*
* In all normal cases when read_eval_print exits (i.e. all cases except
* if it terminates after (cold-start)) I exit here.
*/
#ifndef __cplusplus
errorset_buffer = save_level;
#endif
break;
}
}
#if !defined HAVE_FWIN || defined EMBEDDED
#ifndef UNDER_CE
CSLbool sigint_must_longjmp = NO;
#ifndef __cplusplus
#ifdef USE_SIGALTSTACK
sigjmp_buf sigint_buf;
#else
jmp_buf sigint_buf;
#endif
#endif
void sigint_handler(int code)
{
/*
* Note that the only things that I am really allowed to do in a routine
* like this involve setting variables of type sig_atomic_t, which can not
* be viewed as much more than boolean. The code actually used here is
* somewhat more ambitious (== non-portable!) so must be viewed as delicate.
* ANSI guarantee that longjmp-ing out of a non-nested signal handler
* is valid, but some earlier C libraries have not supported this. Note that
* with C++ I will use throw rather than longjmp.
*/
/*
* tick_pending etc allow a steady stream of clock events to
* be handed to Lisp.
*/
interrupt_pending = 1;
signal(SIGINT, sigint_handler);
if (sigint_must_longjmp)
{
sigint_must_longjmp = NO;
#ifdef __cplusplus
throw((int *)0);
#else
#ifdef USE_SIGALTSTACK
siglongjmp(sigint_buf, 1);
#else
longjmp(sigint_buf, 1);
#endif
#endif
}
/*
* If there is not a separate regular stream of ticks I will simulate
* the receipt of a tick here. Thus I need to be able to recognize "ticks"
* even on systems where there are no "real" ones.
*/
if (!tick_pending)
{
if (already_in_gc) tick_on_gc_exit = YES;
else
{
#ifndef NILSEG_EXTERNS
Lisp_Object nil = C_nil;
CSLbool xxx = NO;
if (exception_pending()) flip_exception(), xxx = YES;
#endif
tick_pending = YES;
saveheaplimit = heaplimit;
heaplimit = fringe;
savevheaplimit = vheaplimit;
vheaplimit = vfringe;
savecodelimit = codelimit;
codelimit = codefringe;
savestacklimit = stacklimit;
stacklimit = stackbase;
#ifndef NILSEG_EXTERNS
if (xxx) flip_exception();
#endif
}
}
return;
}
#endif /* UNDER_CE */
#endif /* HAVE_FWIN */
/*
* OK, I need to write a short essay on "software ticks". A major issue
* for me is synchronization between the worker and the GUI tasks. Lisp
* code can not easily be unilaterally interrupted since it needs to
* preserve GC safety. The easiest way of making progress that I have come up
* with is to arrange that the worker thead (ie the Lisp engine) arranges
* to poll the GUI on a fairly regular basis. I achieve this by making it
* count down in a variable called "countdown" and when that reaches zero
* it deems that a poll is due. I put instructions to decrement countdown in
* a number of places that I expect to be used often enough, and would like
* to have these within all possible loops and such that they are performed
* uniformly over time. These are IDEALS not reality! The countdown overflow
* may happen at somewhat irregular intervals and often at places in the
* code where I am not GC safe. So what I do is to set heap fringes and
* stack fringes so that the next time I try to allocate memory or check
* the stack the situation is noticed and I enter the GC. Once there I
* rapidly detect that this is not a genuine case of having run out of
* memory so I do not do a full GC: I just reset the varios fringes and
* proceed. But while there I know I am in a tidy situation and I can
* exchange information with the GUI. Perhaps as clear-cut case of
* consequence that can arise is that I may respond to a GUI request to
* interrupt what I was doing.
* I try to tune the value that I count down from to get a rate of polling
* that I count as "reasonable" - ie a few per second.
*
* deal_with_tick() is called when the countdown overflows. It resets the
* fringe variables to provoke a GC.
*
* handle_tick() is then a call back out from the GC and could do more
* as required.
*/
int32_t software_ticks = 3000;
int32_t number_of_ticks = 0;
int32_t countdown = 3000;
int deal_with_tick(void)
{
#ifdef PENDING_TICK_SUPPORT
printf("(!)"); fflush(stdout);
number_of_ticks++;
if (!tick_pending)
{
if (already_in_gc) tick_on_gc_exit = YES;
else
{
#ifndef NILSEG_EXTERNS
Lisp_Object nil = C_nil;
CSLbool xxx = NO;
if (exception_pending()) flip_exception(), xxx = YES;
#endif
tick_pending = YES;
saveheaplimit = heaplimit;
heaplimit = fringe;
savevheaplimit = vheaplimit;
vheaplimit = vfringe;
savecodelimit = codelimit;
codelimit = codefringe;
savestacklimit = stacklimit;
stacklimit = stackbase;
#ifndef NILSEG_EXTERNS
if (xxx) flip_exception();
#endif
}
}
#endif
countdown = software_ticks;
return 1;
}
static long int initial_random_seed, seed2;
char *files_to_read[MAX_INPUT_FILES],
*symbols_to_define[MAX_SYMBOLS_TO_DEFINE],
*fasl_paths[MAX_FASL_PATHS];
int output_directory;
character_reader *procedural_input;
character_writer *procedural_output;
CSLbool undefine_this_one[MAX_SYMBOLS_TO_DEFINE];
int number_of_input_files = 0,
number_of_symbols_to_define = 0,
number_of_fasl_paths = 0,
init_flags = 0;
#ifdef WINDOW_SYSTEM
FILE *alternative_stdout = NULL;
#endif
/*
* standard_directory holds the name of the default image file that CSL
* would load.
*/
char *standard_directory;
/*
* If non-NULL the string module_enquiry is a name presenetd on the
* command line in a "-T name" request, and this will cause the system
* to behave in a totally odd manner - it does not run Lisp at all but
* performs a directory enquiry within the image file.
*/
static char *module_enquiry = NULL;
/*
* The next lines mean that (if you can get in before cslstart is
* called) you can get memory allocation done in a custom way.
*/
static void *CSL_malloc(size_t n)
{
return malloc(n);
}
static void CSL_free(void *p)
{
free(p);
}
static void *CSL_realloc(void *p, size_t n)
{
return realloc(p, n);
}
malloc_function *malloc_hook = CSL_malloc;
realloc_function *realloc_hook = CSL_realloc;
free_function *free_hook = CSL_free;
int errorset_min = 0, errorset_max = 3;
int load_count = 0, load_limit = 0x7fffffff;
int csl_argc;
char **csl_argv;
CSLbool restartp;
char *C_stack_base = NULL, *C_stack_limit = NULL;
double max_store_size = 0.0;
#ifndef HAVE_CILK
#ifdef WIN32
HANDLE kara_thread1, kara_thread2;
#elif defined HAVE_LIBPTHREAD
pthread_t kara_thread1, kara_thread2;
#endif
#endif
static int kparallel = -1;
void cslstart(int argc, char *argv[], character_writer *wout)
{
int i;
double store_size = 0.0;
volatile Lisp_Object sp;
#ifdef CONSERVATIVE
C_stackbase = (Lisp_Object *)&sp;
#endif
C_stack_base = (char *)&sp;
C_stack_limit = NULL;
max_store_size = 0.0;
#if (defined HAVE_LIBPTHREAD || defined WIN32) && !HAVE_CILK
if (number_of_processors() >= 3)
{
#ifdef MACINTOSH
sem_unlink("/sem1a");
if ((kara_sem1a = sem_open("/sem1a", O_CREAT, 0644, 0)) == SEM_FAILED)
{ perror("sem_open");
exit(EXIT_FAILURE);
}
sem_unlink("/sem1b");
if ((kara_sem1b = sem_open("/sem1b", O_CREAT, 0644, 0)) == SEM_FAILED)
{ perror("sem_open");
exit(EXIT_FAILURE);
}
sem_unlink("/sem1c");
if ((kara_sem1c = sem_open("/sem1c", O_CREAT, 0644, 0)) == SEM_FAILED)
{ perror("sem_open");
exit(EXIT_FAILURE);
}
sem_unlink("/sem2a");
if ((kara_sem2a = sem_open("/sem2a", O_CREAT, 0644, 0)) == SEM_FAILED)
{ perror("sem_open");
exit(EXIT_FAILURE);
}
sem_unlink("/sem2b");
if ((kara_sem2b = sem_open("/sem2b", O_CREAT, 0644, 0)) == SEM_FAILED)
{ perror("sem_open");
exit(EXIT_FAILURE);
}
sem_unlink("/sem2c");
if ((kara_sem2c = sem_open("/sem2c", O_CREAT, 0644, 0)) == SEM_FAILED)
{ perror("sem_open");
exit(EXIT_FAILURE);
}
#else /* MACINTOSH */
sem_init(&kara_sem1a, 0, 0);
sem_init(&kara_sem1b, 0, 0);
sem_init(&kara_sem1c, 0, 0);
sem_init(&kara_sem2a, 0, 0);
sem_init(&kara_sem2b, 0, 0);
sem_init(&kara_sem2c, 0, 0);
#endif /* MACINTOSH */
#ifdef WIN32
kara_thread1 = CreateThread(NULL, 0, kara_worker1, NULL, 0, NULL);
kara_thread2 = CreateThread(NULL, 0, kara_worker2, NULL, 0, NULL);
#else
pthread_create(&kara_thread1, NULL, kara_worker1, NULL);
pthread_create(&kara_thread2, NULL, kara_worker2, NULL);
#endif
}
karatsuba_parallel = 0x7fffffff;
#elif defined HAVE_CILK
karatsuba_parallel = 0x7fffffff;
#endif /* thread support */
#ifdef EMBEDDED
/* This provides a fixed limit in the embedded build */
C_stack_limit = C_stack_base - 2*1024*1024 + 0x10000;
#else /* EMBEDDED */
#ifdef WIN32
{ HMODULE h = GetModuleHandle(NULL); /* For current executable */
if (h != NULL)
{ IMAGE_DOS_HEADER *dh = (IMAGE_DOS_HEADER*)h;
IMAGE_NT_HEADERS *NTh =
(IMAGE_NT_HEADERS*)((BYTE*)dh + dh->e_lfanew);
int64_t stackLimit =
(int64_t)NTh->OptionalHeader.SizeOfStackReserve;
/*
* If the limit recovered above is under 200K I will pretend it is
* just plain wrong and increase it to that. The effect may be that I
* end up with an untidy stack overflow but at least I get closer to
* using all the space that I have.
*/
if (stackLimit < 200*1024) stackLimit = 200*1024;
/* I also assume that any figure over 20 Mbytes is a mess so ignore it */
if (stackLimit <= 20*1024*1024)
{ /* I try to give myself 64K spare... */
C_stack_limit = C_stack_base - stackLimit + 0x10000;
#ifdef DEBUG
fprintf(stderr, "[debug] stack %dK\n", (int)(stackLimit/1024));
#endif
}
}
}
#else /* WIN32 */
#ifdef HAVE_SYS_RESOURCE_H
{ struct rlimit r;
if (getrlimit(RLIMIT_STACK, &r) == 0)
{ int64_t stackLimit = (int64_t)r.rlim_cur;
/*
* If the user has used ulimit to remove all stack limits I will
* nevertheless apply one at 20 Mbytes. That is SO much higher than any
* default as to not hurt ordinary people - and if anybody NEEDS gigabytes
* of stack they need to ensure that getrlimit returns a finite indication
* of that! Results of RLIM_SAVED_MAX represent a sort of "give up" from
* getrlimit so I will treat them as failure.
*/
#if HAVE_DECL_RLIM_SAVED_MAX
if (stackLimit == RLIM_SAVED_MAX &&
RLIM_SAVED_MAX != RLIM_INFINITY)
{ /* do nothing */ }
else
#endif
#if HAVE_DECL_RLIM_SAVED_CUR
if (stackLimit == RLIM_SAVED_CUR &&
RLIM_SAVED_CUR != RLIM_INFINITY)
{ /* do nothing */ }
else
#endif
if (stackLimit == RLIM_INFINITY) stackLimit = 20*1024*1024;
/* I view values under 200K as silly and ignore them! */
if (stackLimit >= 200*1024)
{ C_stack_limit = C_stack_base - stackLimit + 0x10000;
#ifdef DEBUG
fprintf(stderr, "[debug] stack %dK\n", (int)(stackLimit/1024));
#endif
}
}
}
#endif /* HAVE_SYS_RESOURCE_H */
#endif /* WIN32 */
/* If I can not read a value then I will set a limit at 4 Mbytes... */
if (C_stack_limit == NULL)
{ C_stack_limit = C_stack_base - 4*1024*1024 + 0x10000;
#ifdef DEBUG
fprintf(stderr, "[debug] stack defaulting to 4Mb\n");
#endif
}
#endif /* EMBEDDED */
#ifdef EMBEDDED
fwin_set_lookup(look_in_lisp_variable);
#endif
errorset_min = 0;
errorset_max = 3;
stack_segsize = 1;
module_enquiry = NULL;
countdown = 0x7fffffff;
/* put resource limiting info in a tidy or at least safe state */
time_base = space_base = io_base = errors_base = 0;
time_now = space_now = io_now = errors_now = 0;
time_limit = space_limit = io_limit = errors_limit = -1;
/*
* Note that I will set up clock_stack AGAIN later on! The one further down
* happens after command line options have been decoded and is where I really
* want to consider Lisp to be starting. The setting here is because
* if I call ensure_screen() it can push and pop the clock stack, and
* especially if I have an error in my options I may print to the terminal
* and then flush it. Thus I need SOMETHING set up early to prevent any
* possible frivolous disasters in that area.
*/
base_time = read_clock();
consolidated_time[0] = gc_time = 0.0;
clock_stack = &consolidated_time[0];
#if defined WINDOW_SYSTEM && !defined EMBEDDED
use_wimp = YES;
#endif
#ifdef HAVE_FWIN
/*
* On fwin the "-w" flag should disable all attempts at use of the wimp.
*/
for (i=1; i<argc; i++)
{ char *opt = argv[i];
if (opt == NULL) continue;
else if (strcmp(argv[i], "--args")==0) break;
#if defined WINDOW_SYSTEM && !defined EMBEDDED
if (opt[0] == '-' && (char)tolower((unsigned char)opt[1]) == 'w')
{ use_wimp = !use_wimp;
break;
}
#endif
}
fwin_pause_at_end = 1;
#endif
#ifdef SOCKETS
#if 0
sockets_ready = 0;
socket_server = 0;
#endif
#endif
/*
* Now that the window manager is active I can send output through
* xx_printf() and get it on the screen neatly.
*/
procedural_input = NULL;
procedural_output = wout;
standard_directory = find_image_directory(argc, argv);
restartp = YES;
ignore_restart_fn = NO;
spool_file = NULL;
spool_file_name[0] = 0;
output_directory = 0x80000000;
number_of_input_files = 0;
number_of_symbols_to_define = 0;
number_of_fasl_paths = 0;
fasl_output_file = NO;
initial_random_seed = seed2 = 0;
init_flags = INIT_EXPANDABLE;
return_code = EXIT_SUCCESS;
segvtrap = YES;
batch_flag = NO;
load_count = 0;
load_limit = 0x7fffffff;
CSL_MD5_Init();
CSL_MD5_Update((unsigned char *)"Initial State", 13);
#ifdef MEMORY_TRACE
car_counter = 0x7fffffff;
car_low = 0;
car_high = 0xffffffff;
#endif
/* I save the args so that setup can make a lisp variable out of them */
csl_argc = argc;
csl_argv = argv;
argc--;
for (i=1; i<=argc; i++)
{ char *opt = argv[i];
/*
* The next line ought never to be activated, but I have sometimes seen
* unwanted NULL args on the end of command lines so I filter them out
* here as a matter of security.
*/
if (opt == NULL || *opt == 0) continue;
/*
* The keyword "--args" terminates CSL's scanning of the arguments so any
* material beyond there is merely for use by the application code.
*/
else if (strcmp(opt, "--args")==0) break;
/*
* Note that I do not treat an isolated "-" as introducing an "option".
* Instead it is treated as a file-name and it indicates the "standard"
* input. There may be amusing consequences for using this several times
* in one call, but I hope it will make sense in several sane cases.
*/
if (opt[0] == '-' && opt[1] != 0)
{ char *w;
int c1 = opt[1], c2 = opt[2];
if (isupper((unsigned char)c1))
c1 = (char)tolower((unsigned char)c1);
switch (c1)
{
/*
* -- <outfile> arranges that output is sent to the indicated file. It is
* intended to behave a little like "> outfile" as command-line output
* redirection, but is for use in windowed environments (in particular
* Windows NT) where this would not work. I had intended to use "->" here,
* but then the ">" tends to get spotted as a command-line request for
* redirection, and I would not be using this if command-line redirection
* worked properly! Actually use of "--" here was a BAD choice since it
* clashes with the tradition now common elsewhere that fully spelt-out
* options can be written as "--option". To start to mend that I will
* now make
* -- filename
* redirect the standard output, but detect
* --option
* as an extended option. This is, I guesss, an incompatible change to CSL's
* behaviour but I rather believe it will be a good one to make and I can
* issue a message about unrecognised options that will help anybody caught
* by it.
*/
/*! options [--] \item [{\ttfamily --}] \index{{\ttfamily --}}
* If the application is run in console mode then its standard output could
* be redirected to a file using shell facilities. But the {\ttfamily --}
* directive (followed by a file name) redirects output within the Lisp rather
* than outside it. If this is done a very limited capability for sending
* progress or status reports to stderr (or the title-bar when running in windowed
* mode) remains via the {\ttfamily report!-right} function.
*
* The {\ttfamily -w} option may frequently make sense in such cases, but if that
* is not used and the system tries to run in a window it will create it
* starting off minimised.
*/
case '-':
if (c2 != 0)
{ w = &opt[2];
/*! options [--cygwin] \item [{\ttfamily --cygwin}] \index{{\ttfamily --cygwin}}
* On Windows this flag is used to specify that a cygwin rather that a native
* windows version of Reduce is needed. This can be the situation in some
* cases where the foreign function interface is to be used.
*/
if (strcmp(w, "cygwin") == 0)
{ }
/*! options [--texmacs] \item [{\ttfamily --texmacs}] \index{{\ttfamily --texmacs}}
* If CSL/Reduce is launched from texmacs this command-line flag should be
* used to arrange that the {\ttfamily texmacs} flag is set in
* {\ttfamily lispsystem!*}, and the code may then do special things.
*/
else if (strcmp(w, "texmacs") == 0)
{ }
/*! options [--gui] \item [{\ttfamily --gui}] \index{{\ttfamily --gui}}
* Encourage the system to run in its own window. Similar behaviour
* to {\ttfamily -w+}.
*/
else if (strcmp(w, "gui") == 0)
{ }
/*! options [--nogui] \item [{\ttfamily --nogui}] \index{{\ttfamily --nogui}}
* Encourage the system to run as a console-style application. Similar
* to {\ttfamily -w-} or just simply {\ttfamily -w}.
*/
else if (strcmp(w, "nogui") == 0)
{ }
/*! options [--guimin] \item [{\ttfamily --guimin}] \index{{\ttfamily --guimin}}
* Encourage the system to run as in its own window, but
* start that window off minimised. Similar
* to {\ttfamily -w.}.
*/
else if (strcmp(w, "guimin") == 0)
{ }
/*! options [--help] \item [{\ttfamily --help}] \index{{\ttfamily --help}}
* It is probably obvious what this option does! Note that on Windows the
* application was linked as a windows binary so it carefully creates a
* console to display the help text in, and organizes a delay to give
* people a chance to read it.
*/
else if (strcmp(w, "help") == 0)
{
/*
* A comments here as a horrible warning. For dubious reasons term_printf
* can ONLY cope when the output it generates is at most 256 bytes long.
* Beyond that there can be an internal buffer overflow. Hence each line
* of text here is printed as a separate call. If I was certain that
* a vsnprintf function was ALWAYS available the internal behaviour could
* at least be a bit safer...
*/
term_printf(
"Options:\n");
term_printf(
"-a do not use. Flips meaning of the Lisp \"batchp\" function.\n");
term_printf(
"-b do not colour prompts. -bOIP sets colours for output,\n");
term_printf(
" input and prompt, using rgbcmyk\n");
term_printf(
" for Red, Green, Blue, Cyan etc.\n");
term_printf(
"-c display something that is not a Copyright statement (because of LGPL).\n");
term_printf(
"-d VVV or -d VVV=VVV define a Lisp symbol as the system start\n");
term_printf(
"-e enable some feature that is at present an experiment. Not for users!\n");
term_printf(
"-f or -f nnn listen on socket 1206 or nnn to run a remote session.\n");
term_printf(
" This option is not for normal users.\n");
term_printf(
"-g enable some options that help when debugging. You get backtraces.\n");
term_printf(
"-h on X windows this may use x-terminal fonts rather than ones\n");
term_printf(
" used via Xft that live with the application. Not recommended.\n");
term_printf(
"-i <image file> specific the location of the initial image file explicitly\n");
term_printf(
" You may have multiple image files, seached for modules in\n");
term_printf(
" the order listed.\n");
term_printf(
"-j used for depencency tracking. '-j fileuse.dat' notes what files\n");
term_printf(
" are accessed during this run in the indicated place.\n");
term_printf(
"-k nnnK or -knnnM or -knnnG suggest heap-size to use. Often not needed\n");
term_printf(
"-l logfile keep transcript of session for you.\n");
term_printf(
"-m a memory trace option not for ordinary use.\n");
term_printf(
"-n ignore the restart function in an image file so that the system.\n");
term_printf(
" starts up in raw Lisp. Sometimes useful if image file is broken.\n");
term_printf(
"-o <image file> specified where newly created compiled modules and\n");
term_printf(
" saved heap images should go. Default is in the standard image.\n");
term_printf(
"-p reserved for a potential profile option.\n");
term_printf(
"-q tend to be Quiet. see also -v.\n");
term_printf(
"-r nnn or -r nnn,mmm sets initial random seed. Passing 0 means use\n");
term_printf(
" current time of day and similar nonrepeatable stuff. May be\n");
term_printf(
" used to force repeatability of code that uses randomness.\n");
term_printf(
"-s causes compiler to display \"assembly code\".\n");
term_printf(
"-t modulename prints the timestamp of the given module and exits.\n");
term_printf(
"-u VVV undefines the Lisp symbol VVV at the start of the run.\n");
term_printf(
"-v runs in a slighly more verbose mode.\n");
term_printf(
"-w controls if code runs in a window or in console. Also -w+ and -w-\n");
term_printf(
" can override cases where the system really wants to go one way.\n");
term_printf(
" Prefered usage is --gui, --nogui or --guimin rather than the\n");
term_printf(
" short-form versions.\n");
term_printf(
"-x avoid trapping exceptions so you can use a low-level debugger\n");
term_printf(
" to sort out errors in the kernel.\n");
term_printf(
"-y At one stage this enabled Japanese character support. Not now\n");
term_printf(
" maintained.\n");
term_printf(
"-z when the code starts up it is just a basic raw Lisp core without\n");
term_printf(
" even a compiler. Used to bootstrap the system.\n");
term_printf(
"-- filename redirect output to the given file so it does not appear\n");
term_printf(
" on the screen.\n");
term_printf(
"--cygwin [on Windows] try to use the cygwin version of Reduce rather\n");
term_printf(
" than a native Windows version, regardless of other circumstances.\n");
term_printf(
"--texmacs run in texmacs mode. You must use the plugin from the\n");
term_printf(
" cslbase/texmacs-plugin directory.\n");
term_printf(
"--<other> reserved for additional extended options.\n");
term_printf(
"--version display some version information then exit.\n");
term_printf(
"--help this output!\n");
#ifdef WX
/*
* NOTE that the LaTeX Project Public License requires that every
* component of a derived work contain priminent notices logging
* changes of LPPL material and that it also provides information
* sufficient to obtain complete, unmodified versions of the original.
* Having text that can be displated via the "--help" command-line option
* will (I hope) satisfy these requirements for binary versions of this
* code. Those who read the source can find this comment as well, which
* notes that csl/support-packages contains original archives of
* font-related files that I have used, and that csl/cslbase/wxfonts
* contains both fonts and documentation explaining what might have
* changed. And in case anybody receives this file separate from
* other parts of the code, I will note that all these files can be
* downloaded from reduce-algebra.sf.net.
*/
term_printf(
"This software contains code that is subject to the LaTeX Project\n");
term_printf(
"Public License. In accordance with Clause 6 of that (which governs\n");
term_printf(
"distribution, there are README in a directory called wxfonts or\n");
term_printf(
"reduce.wxfonts present as part of this distribution that explain\n");
term_printf(
"both changes that have been made and where to obtain copies of\n");
term_printf(
"relevant complete, unmodified original font-related material.\n");
#endif
my_exit(0);
}
/*! options [--version] \item [{\ttfamily --version}] \index{{\ttfamily --version}}
* It is probably obvious what this option does! But note that there is an
* issue here about whether one is looking at the CSL version identification or
* one for the Lisp application from an image file (eg often Reduce).
*/
else if (strcmp(w, "version") == 0)
{
#ifndef COMMON
term_printf("Codemist Standard Lisp %s for %s: %s\n",
VERSION, IMPNAME, __DATE__);
#else
term_printf("Codemist Common Lisp %s for %s: %s\n",
VERSION, IMPNAME, __DATE__);
#endif
my_exit(0);
}
else if (strcmp(w, "maxmem") == 0)
{ if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
{ char buffer[16];
int i = 0;
while ((*w != '/') &&
(*w != 'k') && (*w != 'K') &&
(*w != 'm') && (*w != 'M') &&
(*w != 'g') && (*w != 'G') &&
(*w != 0) &&
(i<sizeof(buffer)-1))
buffer[i++] = *w++;
buffer[i] = 0;
max_store_size = atof(buffer);
switch (*w)
{
case 'k': case 'K':
break;
case 'g': case 'G':
max_store_size *= 1024.0*1024.0;
break;
default: /* megabytes by default */
max_store_size *= 1024.0;
break;
}
#if PAGE_BITS==18
if (max_store_size < 10000.0) max_store_size = 10000.0;
#else
if (max_store_size < 32000.0) store_size = 32000.0;
#endif
if ((!SIXTY_FOUR_BIT &&
(max_store_size > 2.0*1024.0*1024.0)) ||
(max_store_size > 500.0*1024.0*1024.0))
{
fwin_restore();
term_printf(
"Memory specifier \"--maxmem %s%s\" is too large (= %.4g)\n",
buffer, w, max_store_size/1024.0);
term_printf("Please specify as -KnnnK, -KnnnM or -KnnnG\n");
term_printf("for Kilobytes, Megabytes or Gigabytes\n");
}
}
}
/*
* I do not really want this options heavily documented, since it is intended
* for use by those maintaining CSL not for the general public. By default
* log multiplication can use a threaded implementation (to exploit multi-core
* machines). This happens when numbers get bigger than about
* 2^(31*KARATSUBA_PARALLEL_CUTOFF). This option allows one to override the
* default threshold so that performance effects can be measured and the
* cut-off adjusted to suit the machine involved.
*/
else if (strcmp(w, "kara") == 0)
{ if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
kparallel = atoi(w);
if (kparallel < KARATSUBA_CUTOFF)
kparallel = KARATSUBA_CUTOFF;
}
else
{
fwin_restore();
term_printf("Unknown extended option \"--%s\"\n", w);
term_printf("NB: use \"-- filename\" (with whitespace)\n");
term_printf(" for output redirection now.\n");
}
continue;
}
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
{ char filename[LONGEST_LEGAL_FILENAME];
FILE *f;
memset(filename, 0, sizeof(filename));
#ifdef WINDOW_SYSTEM
f = open_file(filename, w, strlen(w), "w", NULL);
if (f == NULL)
{
/*
* Under FWIN if there is a "--" among the arguments I will start off
* with the main window minimized. Thus if an error is detected at a
* stage that the transcript file is not properly opened I need to
* maximize the window so I can see the error! Note that I will need to
* ensure that fwin only uses "-- file" not "--option" to do this...
*/
fwin_restore();
term_printf("Unable to write to \"%s\"\n", filename);
continue;
}
else
{ term_printf("Output redirected to \"%s\"\n",
filename);
}
if (alternative_stdout != NULL)
fclose(alternative_stdout);
alternative_stdout = f;
#else
/*
* I use freopen() on stdout here to get my output sent elsewhere. Quite
* what sort of mess I am in if the freopen fails is hard to understand!
* Thus I write a message to stderr and exit promptly in case of trouble.
* I print a message explaining what I am doing BEFORE actually performing
* the redirection.
*/
fprintf(stderr, "Output to be redirected to \"%s\"\n", w);
f = open_file(filename, w, strlen(w), "w", stdout);
if (f == NULL)
{ fprintf(stderr, "Unable to write to \"%s\"\n",
filename);
#ifdef HAVE_FWIN
#ifdef __cplusplus
throw EXIT_FAILURE;
#else
my_return_code = EXIT_FAILURE;
#ifdef USE_SIGALTSTACK
siglongjmp(my_exit_buffer, 1);
#else
longjmp(my_exit_buffer, 1);
#endif
#endif
#else
exit(EXIT_FAILURE);
#endif
}
#endif
}
continue;
/*! options [--my-path] \item [{\ttfamily --my-path}] \index{{\ttfamily --my-path}}
* At some time I had felt the need for this option, but I now forget what I
* expected to use it for! It leads the executable to display the fully
* rooted name of the directory it was in and then terminate. It may be useful
* in some script?
*/
/*! options [-a] \item [{\ttfamily -a}] \index{{\ttfamily -a}}
* {\ttfamily -a} is a curious option, not intended for general or casual use.
* If given it causes the {\ttfamily (batchp)} function to return the opposite
* result from normal! Without ``{attfamily -a}'' {\ttfamily (batchp)} returns
* {\ttfamily T} either if at least one file was specified on the command line,
* or if the standard input is ``not a tty'' (under some operating systems this
* makes sense -- for instance the standard input might not be a ``tty'' if it
* is provided via file redirection). Otherwise (ie primary input is directly
* from a keyboard) {\ttfamily (batchp)} returns {\ttfamily nil}. Sometimes
* this judgement about how ``batch'' the current run is will be wrong or
* unhelpful, so {\ttfamily -a} allows the user to coax the system into better
* behaviour. I hope that this is never used!
*/
case 'a':
batch_flag = YES;
continue;
/*! options [-b] \item [{\ttfamily -b}] \index{{\ttfamily -b}}
* {\ttfamily -b} tells the system to avoid any attempt to recolour prompts
* and input text. It will mainly be needed on X terminals that have been
* set up so that they use colours that make the defaults here unhelpful.
* Specifically white-on-black and so on.
* {\ttfamily -b} can be followed by colour specifications to make things yet
* more specific. It is supposed to be the idea that three colours can be
* specified after it for output, input and prompts, with the letters KRGYbMCW
* standing for blacK, Red, Green, Yellow, blue, Magenta, Cyan and White.
* This may not fully work yet!
*/
case 'b':
/*
* Actually "-b" is detected and processed by fwin (if present) before
* this bit of the code is invoked (much as "-w" is). Thus I do not have
* to do anything here!
*/
continue;
/*! options [-c] \item [{\ttfamily -c}] \index{{\ttfamily -c}}
* Displays a notice relating to the authorship of CSL. Note that this
* is an authorship statement not a Copyright notice, because if any
* (L)GPL code is involved that would place requirements on what was
* displayed in a Copyright Notice.
*/
case 'c':
fwin_restore();
term_printf("\nCSL was coded by Codemist Ltd, 1988-2012\n");
term_printf("Distributed under the Modified BSD License\n");
term_printf("See also --help\n");
continue;
/*! options [-d] \item [{\ttfamily -d}] \index{{\ttfamily -d}}
* A command line entry {\ttfamily -Dname=value} or {\ttfamily -D name=value}
* sets the value of the named lisp variable to the value (as a string).
* Note that the value set is a {\em string} so if you wish to retrieve
* it and use it as a symbold or number within your code you will have to
* perform some conversion.
*/
case 'd':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
symbols_to_define[number_of_symbols_to_define] = w,
undefine_this_one[number_of_symbols_to_define++] = NO;
else
{
fwin_restore();
term_printf("Too many \"-D\" requests: ignored\n");
}
continue;
/*! options [-e] \item [{\ttfamily -e}] \index{{\ttfamily -e}}
* A ``spare'' option used from time to time to activate experiments within
* CSL.
*/
case 'e':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break;
if (sscanf(w, "%d", &load_limit) != 1)
load_limit = 0x7fffffff;
continue;
#if 0
/*! options [-f] \item [{\ttfamily -f}] \index{{\ttfamily -f}}
* At one stage CSL could run as a socket server, and {\ttfamily -f portnumber}
* activated that mode. {\ttfamily -f-} used a default port, 1206 (a number
* inspired by an account number on Titan that I used in the 1960s). The code
* that supports this may be a useful foundation to others who want to make a
* network service out of this code-base, but is currently disabled.
*/
#ifdef SOCKETS
case 'f':
/*
* -F
* This is used with syntax -Fnnn or -F nnn (with nnn a number above
* 1000 but less than 65536) to cause the system to run not as a normal
* interactive application but as a server listening on the indicated port.
* The case -F- (which could of course be "-F -") indicates use of the
* default port for CSL, which I hereby declare to be 1206. This number may
* need to be changed later if I find it conflicts with some other common
* package or usage, but was selected in memory of the project number
* at one time allocated to the Archimedeans Computing Group.
* On some systems if I want to set up a server that can serve multiple
* clients I may need to re-invoke CSL afresh for each new client, and in
* such cases the internally generated tasks can be passed information
* from their parent task using -F followed by non-numeric information.
* Any user who attempts such usage will get "what they deserve".
*/
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
port_number = default_csl_server_port;
remote_store = REMOTE_STORE;
max_users = MAX_USERS;
if (strcmp(w, "-") == 0)
port_number = default_csl_server_port;
else if (sscanf(w, "%d:%d:%d",
&port_number, &max_users, &remote_store) < 1 ||
port_number <= 1000 ||
port_number >= 65536 ||
max_users < 2 || max_users > 50 ||
remote_store < 4000 || remote_store > 20000)
{
fwin_restore();
term_printf("\"%s\" is valid (want port:users:store\n", w);
continue;
}
store_size = (double)remote_store;
init_flags &= ~INIT_EXPANDABLE;
current_users = 0;
/*
* The code here is probably a bit painfully system-specific, and so one
* could argue that it should go in a separate file. However a LOT of the
* socket interface is the same regardless of the host, or a few simple
* macros can have made it so. So if SOCKETS has been defined I will
* suppose I can continue here on that basis. I do quite want to put the
* basic socket code in csl.c since it is concerned with system startup and
* the selection of sources and sinks for IO.
*/
if (ensure_sockets_ready() == 0)
{ SOCKET sock1, sock2;
struct sockaddr_in server_address, client_address;
#ifdef HAVE_SOCKLEN_T
socklen_t sin_size;
#else
int sin_size;
#endif
sock1 = socket(AF_INET, SOCK_STREAM, 0);
if (sock1 == SOCKET_ERROR)
{
fwin_restore();
term_printf("Unable to create a socket\n");
continue;
}
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port_number);
server_address.sin_addr.s_addr = INADDR_ANY;
memset((char *)&(server_address.sin_zero), 0, 8);
if (bind(sock1, (struct sockaddr *)&server_address,
sizeof(struct sockaddr)) == SOCKET_ERROR)
{
fwin_restore();
term_printf("Unable to bind socket to port %d\n",
port_number);
closesocket(sock1);
continue;
}
if (listen(sock1, PERMITTED_BACKLOG) == SOCKET_ERROR)
{
fwin_restore();
term_printf("Failure in listen() on port %d\n",
port_number);
closesocket(sock1);
continue;
}
for (;;)
{ struct hostent *h;
time_t t0;
sin_size = sizeof(struct sockaddr_in);
sock2 = accept(sock1,
(struct sockaddr *)&client_address,
&sin_size);
if (sock2 == SOCKET_ERROR)
{
fwin_restore();
term_printf("Trouble with accept()\n");
continue; /* NB local continue here */
}
t0 = time(NULL);
term_printf("%.24s from %s",
ctime(&t0),
inet_ntoa(client_address.sin_addr));
h = gethostbyaddr((char *)&client_address.sin_addr,
sizeof(client_address.sin_addr), AF_INET);
if (h != NULL)
term_printf(" = %s", h->h_name);
else term_printf(" [unknown host]");
/*
* Here I have a bit of a mess. Under Unix I can do a fork() so that the
* requests that are coming in are handled by a separate process. The
* code is pretty easy. However with Windows I can only create a fresh process
* by re-launching CSL from the file it was originally fetched from. I
* will try to do that in a while, but for now I will leave the
* Windows version of this code only able to handle a single client
* session.
*/
#ifdef WIN32
closesocket(sock1);
socket_server = sock2;
cpu_timeout = clock() + CLOCKS_PER_SEC*MAX_CPU_TIME;
elapsed_timeout = time(NULL) + 60*MAX_ELAPSED_TIME;
procedural_output = char_to_socket;
term_printf("Welcome to the Codemist server\n");
ensure_screen();
break;
#else /* WIN32 */
while (waitpid(-1, NULL, WNOHANG) > 0) current_users--;
if (current_users >= max_users)
{ term_printf(" refused\n");
socket_server = sock2;
ensure_screen();
procedural_output = char_to_socket;
term_printf(
"\nSorry, there are already %d people using this service\n",
current_users);
term_printf("Please try again later.\n");
ensure_screen();
procedural_output = NULL;
closesocket(socket_server);
socket_server = 0;
continue;
}
else term_printf(" %d of %d\n",
++current_users, max_users);
ensure_screen();
if (!fork())
{ /* Child process here */
closesocket(sock1);
fcntl(sock2, F_SETFL, O_NONBLOCK);
socket_server = sock2;
cpu_timeout = clock() + CLOCKS_PER_SEC*MAX_CPU_TIME;
elapsed_timeout = time(NULL) + 60*MAX_ELAPSED_TIME;
ensure_screen();
procedural_output = char_to_socket;
term_printf("Welcome, you are user %d of %d\n",
current_users, max_users);
term_printf(
"You have been allocated %d seconds CPU time"
" and %d minutes elapsed time\n",
MAX_CPU_TIME, MAX_ELAPSED_TIME);
break;
}
else
{ closesocket(sock2);
if (current_users < 0) current_users = 0;
continue;
/*
* This loops serving as many clients as happen to come along. Having said
* "csl -fnnn" it will be necessary (in due course) to kill the daemon
* by interrupting it with a ^C or some such. When the master process is
* terminated in that way any clients that remain active may continue to
* hang around until they have finished in the usual way.
*/
}
#endif /* WIN32 */
}
}
/*
* The "continue" here gets executed when I have been contacted by some
* client and have an active socket open. It parses the rest of the
* command line and then completes the process of getting CSL running.
*/
continue;
#endif /* SOCKETS */
#endif /* 0 */
/*! options [-g] \item [{\ttfamily -g}] \index{{\ttfamily -g}}
* In line with the implication of this option for C compilers, this enables
* a debugging mode. It sets a lisp variable {\ttfamily !*backtrace} and
* arranges that all backtraces are displayed notwithstanding use of
* {\ttfamily errorset}.
*/
case 'g':
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
symbols_to_define[number_of_symbols_to_define] =
"*backtrace",
undefine_this_one[number_of_symbols_to_define++] = NO;
else
{
fwin_restore();
term_printf("Too many requests: \"-G\" ignored\n");
}
errorset_min = 3;
errorset_max = 3;
continue;
/*! options [-h] \item [{\ttfamily -h}] \index{{\ttfamily -h}}
* This option is a left-over. When the X-windows version of the code first
* started to use Xft it viewed that as optional and could allow a build even when
* it was not available. And then even if Xft was detected and liable to be used
* by default it provided this option to disable its use. The remnants of the
* switch that disabled use of Xft (relating to fonts living on the Host or
* the Server) used this switch, but it now has no effect.
*/
case 'h':
#ifndef HAVE_LIBWX
/*
* If I am using wxWidgets this option is not supported.
*/
fwin_use_xft = 0;
#endif
/*
* Actually, like the "-w" option, it is TOO LATE to do this here because
* lower-level parts of fwin may already have adjusted font paths using
* mechanisms based on whether Xft is to be activated or not. So fwin
* checks for "-h" and "-H" and interprets what it finds. So what I do here
* is just a redundant reminder. Ugh.
*/
continue;
/*! options [-i] \item [{\ttfamily -i}] \index{{\ttfamily -i}}
* CSL and Reduce use image files to keep both initial heap images and
* ``fasl'' loadable modules. By default if the executable launched has some name,
* say xxx, then an image file xxx.img is used. But to support greater
* generality {\ttfamily -i} introduces a new image, {\ttfamily -i-} indicates
* the default one and a sequence of such directives list image files that are
* searched in the order given. These are read-only. The similar option
* {\ttfamily -o} equally introduces image files that are scanned for input, but
* that can also be used for output. Normally there would only be one
* {\ttfamily -o} directive.
*/
case 'i':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
if (w[0] == '-' && w[1] == 0) w = standard_directory;
if (number_of_fasl_paths < MAX_FASL_PATHS-1)
fasl_paths[number_of_fasl_paths++] = w;
else
{
fwin_restore();
term_printf("Too many \"-I/-O\" requests: ignored\n");
}
continue;
/*! options [-j] \item [{\ttfamily -j}] \index{{\ttfamily -j}}
* Follow this directive with a file-name, and a record of all the files read
* during the Lisp run will be dumped there with a view that it can be included
* in a Makefile to document dependencies.
*/
case 'j':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
dependency_file = w;
continue;
/*! options [-k] \item [{\ttfamily -k}] \index{{\ttfamily -k}}
* {\ttfamily -K nnn} sets the size of heap to be used. If it is given then that much
* memory will be allocated and the heap will never expand. Without this
* option a default amount is used, and (on many machines) it will grow
* if space seems tight.
*
* The extended version of this option is {\ttfamily -K nnn/ss} and then ss is the
* number of ``CSL pages'' to be allocated to the Lisp stack. The default
* value (which is 1) should suffice for almost all users, and it should
* be noted that the C stack is separate from and independent of this one and
* it too could overflow.
*
* A suffix K, M or G on the number indicates units of kilobytes, megabytes or
* gigabytes, with megabytes being the default. So {\ttfamily -K200M} might
* represent typical usage for common-sized computations. In general CSL
* will automatically expand its heap, and so it should normally never be
* necessary to use this option.
*/
case 'k':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
{ char buffer[16];
int i = 0;
while ((*w != '/') &&
(*w != 'k') && (*w != 'K') &&
(*w != 'm') && (*w != 'M') &&
(*w != 'g') && (*w != 'G') &&
(*w != 0) &&
(i<sizeof(buffer)-1))
buffer[i++] = *w++;
buffer[i] = 0;
/*
* store size gets set here: 0.0 is left if either that is specified
* explictly or if no -K option is given. That will be treated as
* indicating "use default, and expand memory as you go"
*/
store_size = atof(buffer);
if (store_size == 0.0) init_flags |= INIT_EXPANDABLE;
else
{ init_flags &= ~INIT_EXPANDABLE;
/*
* If an explicit store size has been indicated I will see if it had one
* of the letters K, M or G after it (note that I allow it to be a floating
* point value.
*/
switch (*w)
{
case 'k': case 'K':
break;
case 'g': case 'G':
store_size *= 1024.0*1024.0;
break;
default: /* megabytes by default */
store_size *= 1024.0;
break;
}
/*
* Now the measure is adjusted so it is in units of kilobytes. I will
* set a lower limit to how much can be asked for to try to prevent
* utter congestion. I will also set an upper limit to provide some minor
* protection.
*/
#if PAGE_BITS==18
if (store_size < 10000.0) store_size = 10000.0;
#else
if (store_size < 32000.0) store_size = 32000.0;
#endif
/*
* At present I limit even 64-bit systems to 500 Gbytes. I do not guarantee
* that things will behave sensibly if you approach that ...
* ... and 32-bit systems to 2.0 Gbytes because I need to have all my
* addresses having the same sign bit, so 2G is an ABSOLUTE limit to my heap
* size on a 32-bit machine.
*/
if ((!SIXTY_FOUR_BIT &&
(store_size > 2.0*1024.0*1024.0)) ||
(store_size > 500.0*1024.0*1024.0))
{
fwin_restore();
term_printf(
"Memory specifier \"-K%s%s\" is too large (= %.4g)\n",
buffer, w, store_size/1024.0);
term_printf("Please specify as -KnnnK, -KnnnM or -KnnnG\n");
term_printf("for Kilobytes, Megabytes or Gigabytes\n");
}
}
while (*w!=0 && *w!='/') w++;
if (*w == '/')
{ stack_segsize = atoi(w+1);
if (stack_segsize < 1 || stack_segsize > 10)
stack_segsize = 1;
}
}
continue;
/*! options [-l] \item [{\ttfamily -l}] \index{{\ttfamily -l}}
* This is to send a copy of the standard output to a named log file. It is
* very much as if the Lisp function {\ttfamily (spool ``logfile'')} had been
* invoked at the start of the run.
*/
case 'l':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
{ char filename[LONGEST_LEGAL_FILENAME];
memset(filename, 0, sizeof(filename));
spool_file = open_file(filename, w,
strlen(w), "w", NULL);
if (spool_file == NULL)
{
fwin_restore();
term_printf("Unable to write to \"%s\"\n", filename);
}
else
{ time_t t0 = time(NULL);
strncpy(spool_file_name, filename, 32);
spool_file_name[31] = 0;
#ifdef COMMON
fprintf(spool_file,
"Starts dribbling to %s (%.24s).\n",
spool_file_name, ctime(&t0));
#else
fprintf(spool_file,
"+++ Transcript to %s started at %.24s +++\n",
spool_file_name, ctime(&t0));
#endif
}
}
continue;
#ifdef MEMORY_TRACE
/*
* If MEMORY_TRACE is set up then I can cause an exception by providing
* an option -M n:l:h
* This interrupts after n memory records when a reference in the (inclusive)
* range l..h is next made.
*/
/*! options [-m] \item [{\ttfamily -m}] \index{{\ttfamily -m}}
* Memory trace mode. An option that represents an experiment from the past,
* and no longer reliably in use. It make it possible to force an
* exception at stages whene reference to a specified part of memory was made
* and that could be useful for some low level debugging. It is not supported
* at present.
*/
case 'm':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
switch(sscanf(w, "%ld:%lu:%lu",
&car_counter, &car_low, &car_high))
{
case 0: car_counter = 0x7fffffff;
case 1: car_low = 0;
case 2: car_high = 0xffffffff;
default:break;
}
continue;
#endif
/*! options [-n] \item [{\ttfamily -n}] \index{{\ttfamily -n}}
* Normally when the system is started it will run a ``restart function'' as
* indicated in its heap image. There can be cases where a heap image has been
* created in a bad way such that the saved restart function always fails
* abruptly, and hence working out what was wrong becomes hard. In such cases
* it may be useful to give the {\ttfamily -n} option that forces CSL to
* ignore any startup function and merely always begin in a minimal Lisp-style
* read-eval-print loop. This is intended for experts to do disaster recovery
* and diagnosis of damaged image files.
*/
case 'n': /* Ignore restart function (-N) */
ignore_restart_fn = YES;
continue;
/*! options [-o] \item [{\ttfamily -o}] \index{{\ttfamily -o}}
* See {\ttfamily -i}. This specifies an image file used for output via
* {\ttfamily faslout} and {\ttfamily reserve}.
*/
case 'o':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
if (w[0] == '-' && w[1] == 0) w = standard_directory;
if (number_of_fasl_paths < MAX_FASL_PATHS-1)
{ output_directory = number_of_fasl_paths;
fasl_paths[number_of_fasl_paths++] = w;
}
else
{
fwin_restore();
term_printf("Too many \"-I/-O\" requests: ignored\n");
}
continue;
/*! options [-p] \item [{\ttfamily -p}] \index{{\ttfamily -p}}
* If a suitable profile option gets implemented one day this will activate it,
* but for now it has no effect.
*/
case 'p':
/*
* Please implement something for your favourite system here... what I would
* like would be a call to monitor() or some such...
*/
fwin_restore();
term_printf("Unimplemented option \"-%c\"\n", c1);
continue;
/*! options [-q] \item [{\ttfamily -q}] \index{{\ttfamily -q}}
* This option sets {\ttfamily !*echo} to {\ttfamily nil} and switches off
* garbage collector messages to give a slightly quieter run.
*/
case 'q':
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
/*
* symbols_to_define[number_of_symbols_to_define] =
* "*echo=nil",
* undefine_this_one[number_of_symbols_to_define++] = NO,
*/
init_flags &= ~INIT_VERBOSE,
init_flags |= INIT_QUIET;
else
{
fwin_restore();
term_printf("Too many requests: \"-Q\" ignored\n");
}
continue;
/*! options [-r] \item [{\ttfamily -r}] \index{{\ttfamily -r}}
* The random-number generator in CSL is normally initialised to a value
* based on the time of day and is hence not reproducible from run to run.
* In many cases that behavious is desirable, but for debugging it can be useful
* to force a seed. The directive {\ttfamily -r nnn,mmm} sets the seed to
* up to 64 bits taken from the values nnn and mmm. The second value if optional,
* and specifying {\ttfamily -r0} explicitly asks for the non-reproducible
* behaviour (I hope). Note that the main Reduce-level random number source is
* coded at a higher level and does not get reset this way -- this is the
* lower level CSL generator.
*/
case 'r':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
if (sscanf(w, "%ld,%ld", &initial_random_seed, &seed2) != 2)
{ initial_random_seed = seed2 = 0;
sscanf(w, "%ld", &initial_random_seed);
}
continue;
/*! options [-s] \item [{\ttfamily -s}] \index{{\ttfamily -s}}
* Sets the Lisp variable {\ttfamily !*plap} and hence the compiler generates
* an assembly listing.
*/
case 's':
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
symbols_to_define[number_of_symbols_to_define] =
"*plap",
undefine_this_one[number_of_symbols_to_define++] = NO;
else
{
fwin_restore();
term_printf("Too many requests: \"-S\" ignored\n");
}
continue;
/*! options [-t] \item [{\ttfamily -t}] \index{{\ttfamily -t}}
* {\ttfamily -t name} reports the time-stamp on the named module, and then
* exits. This is for use in perl scripts and the like, and is
* needed because the stamps on modules within an image or
* library file are not otherwise instantly available.
*
* Note that especially on windowed systems it may be necessary to use this
* with {\ttfamily -- filename} since the information generated here goes to
* the default output, which in some cases is just the screen.
*/
case 't':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
module_enquiry = w;
continue;
/*! options [-u] \item [{\ttfamily -u}] \index{{\ttfamily -u}}
* See {\ttfamily -d}, but this forcibly undefines a symbol. There are probably
* very very few cases where it is useful since I do not have a large
* number of system-specific predefined names.
*/
case 'u':
if (c2 != 0) w = &opt[2];
else if (i != argc) w = argv[++i];
else break; /* Illegal at end of command-line */
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
symbols_to_define[number_of_symbols_to_define] = w,
undefine_this_one[number_of_symbols_to_define++] = YES;
else
{
fwin_restore();
term_printf("Too many \"-U\" requests: ignored\n");
}
continue;
/*! options [-v] \item [{\ttfamily -v}] \index{{\ttfamily -v}}
* An option to make things mildly more verbose. It displays more of a banner
* at startup and switches garbage collection messages on.
*/
case 'v':
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
/*
* symbols_to_define[number_of_symbols_to_define] =
* "*echo",
* undefine_this_one[number_of_symbols_to_define++] = NO,
*/
init_flags &= ~INIT_QUIET,
init_flags |= INIT_VERBOSE;
else
{
fwin_restore();
term_printf("Too many requests: \"-V\" ignored\n");
}
continue;
#ifdef WINDOW_SYSTEM
/*! options [-w] \item [{\ttfamily -w}] \index{{\ttfamily -w}}
* On a typical system if the system is launched it creates a new window and uses
* its own windowed intarface in that. If it is run such that at startup the
* standard input or output are associated with a file or pipe, or under X the
* variable {\ttfamily DISPLAY} is not set it will try to start up in console
* mode. The flag {\ttfamily -w} indicates that the system should run in console
* more regadless, while {\ttfamily -w+} attempts a window even if that seems
* doomed to failure. When running the system to obey a script it will often make
* sense to use the {\ttfamily -w} option. Note that on Windows the system is
* provided as two separate (but almost identical) binaries. For example the
* file {\ttfamily csl.exe} is linked in windows mode. A result is that if
* launched from the command line it detaches from its console, and if launched
* by double-clicking it does not create a console. It is in fact very ugly when
* double clicking on an application causes an unwanted console window to appear.
* In contrast {\ttfamily csl.com} is a console mode version of just the same
* program, so when launched from a command line it can communicate with the
* console in the ordinary expected manner.
*/
case 'w':
/*
* I need to detect and process this flag especially early, and so by the time
* I get to regular command decoding there is nothing to be done.
* Within fwin the option "-w" says "do NOT try to use a window, ie
* run as a console style application", while "-w+" says "Even if
* all the rest of the schemes that I have indicate that you should
* run in console mode (eg if standard input is from a pipe, which it
* will be when running under some debuggers) try to create and use a
* window.
*/
continue;
#endif
/*! options [-x] \item [{\ttfamily -x}] \index{{\ttfamily -x}}
* {\ttfamily -x} is an option intended for use only by system
* support experts -- it disables trapping if segment violations by
* errorset and so makes it easier to track down low level disasters --
* maybe! This can be valuable when running under a debugger since if the
* code traps signals in its usual way and tries to recover it can make it a lot
* harder to find out just what was going wrong.
*/
case 'x':
segvtrap = NO;
continue;
/*
* -Y currently unused!
*/
/*! options [-y] \item [{\ttfamily -y}] \index{{\ttfamily -y}}
* {\ttfamily -y } is at present unused.
*/
case 'y':
if (number_of_symbols_to_define < MAX_SYMBOLS_TO_DEFINE)
symbols_to_define[number_of_symbols_to_define] =
"*hankaku",
undefine_this_one[number_of_symbols_to_define++] = NO;
else
term_printf("Too many requests: \"-Y\" ignored\n");
continue;
/*! options [-z] \item [{\ttfamily -z}] \index{{\ttfamily -z}}
* When bootstrapping it is necessary to start up the system for one initial time
* without the benefit of any image file at all. The option {\ttfamily -z} makes
* this happen, so when it is specified the system starts up with a minimal
* environment and only those capabilities that are present in the CSL
* kernel. It will normally make sense to start loading some basic Lisp
* definitions rather rapidly. The files {\ttfamily compat.lsp},
* {\ttfamily extras.lsp} and {\ttfamily compiler.lsp} have Lisp source for the
* main things I use, and once they are loaded the Lisp compiler can be used
* to compile itself.
*/
case 'z': /* Cold start option -z */
restartp = NO;
continue;
default:
fwin_restore();
term_printf("Unrecognized option \"-%c\"\n", c1);
continue;
}
/*
* I do a "break" out of the switch() block if a key occurs at the end
* of the command line in an invalid manner.
*/
fwin_restore();
term_printf("Option \"-%c\" needs an argument: ignored\n", c1);
break;
}
else files_to_read[number_of_input_files++] = opt;
}
if (number_of_fasl_paths == 0)
{ char *p = standard_directory, *p1;
char cur[LONGEST_LEGAL_FILENAME];
memset(cur, 0, sizeof(cur));
/*
* If the user does not specify any image files then the behaviour
* defaults as follows:
* Suppose that the current executable is xxx/yyy/zzz then the
* system behaves as if the user had written
* zzz -o zzz.img -i xxx/yyy/zzz.img
* however if the executable seemed to be in the current directory
* already this is reduced to just
* zzz -o zzz.img
* so that I do not have two different handles on the same image file
* (with the potential muddle that that could result in).
*
* NOTE: this used very generally mean that you ended up with an empty image
* file (eg csl.img or reduce.img) in whatever directory you run this
* code from. This could be avoided by running it as
* xxx -i-
* that explicitly sets up the normal image file as the one to use with
* no extras. However these days I try to arrange that an output image file
* only ever gets created if somebody calls FASLOUT or PRESERVE, so what
* I describe here will usually not cause confusion....
*
* The "image" here can now be in onw of two forms. The one that I historically
* used with CSL was that an image "xxx.img" was a single file that was set
* up with internal directories so that it acted as a composite unit holding
* many sub-files within it. That has the potential convenience that it is
* a single file to distribute and it is hard for there to be confuson about
* corruption or loss of the various sub-files within it. This scheme will
* still be used if xxx.img is a single file. But if on start-up xxx.img is a
* directory, or if it does not exist to start with and is named in the form
* "xxx.img/" with a trailing directory separator then the various sub-items
* will merely be stored within that directory. That moves the strain of
* managing them onto the operating system, but means that the operating system
* will maintain date-stamps on each sub-file and this may be useful if you
* wish to use "make" to maintain a project. It will be possible to have both
* a single-file and a directory based image open at the same time and copy
* at least some modules from one to another under program control.
*
* At present I still view the one-file solution as neater and so it is the
* default. The names used for files within a directory are discussed in the
* places where I manage them.
*/
if (standard_directory[0] == '.' &&
(standard_directory[1] == '/' ||
standard_directory[1] == '\\')) strcpy(cur, standard_directory);
else get_current_directory(cur, sizeof(cur));
p += strlen(p);
while (p != standard_directory &&
*--p != '/' &&
*p != '\\') /* nothing */;
if (strncmp(standard_directory, cur, p-standard_directory) != 0)
p1 = (char *)(*malloc_hook)(strlen(p));
else p1 = NULL;
if (p == standard_directory || p1 == NULL)
{ fasl_paths[0] = standard_directory;
/*
* If output_directory has the 0x40000000 bit set then the directory
* involved is one that should be opened now if it exists, but if
* it does not its creation should be deferred for as long as possible.
*/
output_directory = 0x40000000 + 0;
number_of_fasl_paths = 1;
if (p1 != NULL) (*free_hook)(p1);
}
else
{ strcpy(p1, p+1);
fasl_paths[0] = p1;
fasl_paths[1] = standard_directory;
output_directory = 0x40000000 + 0;
number_of_fasl_paths = 2;
}
}
Iinit(); /* Initialise "file system" for image files */
if (module_enquiry != NULL)
{ char datestamp[32], fullname[LONGEST_LEGAL_FILENAME];
int32_t size;
int i;
Lisp_Object nil;
memset(fullname, 0, sizeof(fullname));
/*
* Imodulep expects input_libraries to be set up. So I will fudge the
* creation of something that looks sufficiently like a list to pass muster
* here despite the full system not being loaded. I use references to the
* nil-segment and cons().
*/
nilsegment = (Lisp_Object *)my_malloc(NIL_SEGMENT_SIZE);
#ifdef COMMON
nil = doubleword_align_up(nilsegment) + TAG_CONS + 8;
#else
nil = doubleword_align_up(nilsegment) + TAG_SYMBOL;
#endif
C_nil = nil;
pages_count = heap_pages_count = vheap_pages_count =
bps_pages_count = native_pages_count = 0;
stacksegment = (Lisp_Object *)my_malloc(CSL_PAGE_SIZE);
/*
* I am lazy about protection against malloc failure here.
*/
heaplimit = doubleword_align_up(stacksegment);
fringe = heaplimit + CSL_PAGE_SIZE - 16;
input_libraries = heaplimit + 16 + TAG_SYMBOL;
heaplimit += 64;
/*
* I have now fudged up enough simulation of a Lisp heap that maybe I can
* build the library search-list.
*/
qheader(input_libraries) |= SYM_SPECIAL_FORM;
qvalue(input_libraries) = nil;
for (i=number_of_fasl_paths-1; i>=0; i--)
qvalue(input_libraries) = cons(SPID_LIBRARY + (((int32_t)i)<<20),
qvalue(input_libraries));
if (Imodulep(module_enquiry, strlen(module_enquiry),
datestamp, &size, fullname))
{ strcpy(datestamp, "unknown");
size = 0;
strcpy(fullname, module_enquiry);
}
term_printf("%.24s size=%ld file=%s\n",
datestamp, (long)size, fullname);
init_flags &= ~INIT_VERBOSE;
#ifdef HAVE_FWIN
fwin_pause_at_end = 0;
#endif
}
else
{ base_time = read_clock();
consolidated_time[0] = gc_time = 0.0;
clock_stack = &consolidated_time[0];
push_clock();
if (init_flags & INIT_VERBOSE)
{
#ifndef WINDOW_SYSTEM
/*
* If I do NOT have a window system I will print a newline here so that I
* can be very certain that my banner appears at the start of a line.
* With a window system I should have a brand-new fresh window for output
* and the newline would intrude as an initial blank line.
*/
term_printf("\n");
#endif
#ifndef COMMON
term_printf("Codemist Standard Lisp %s for %s: %s\n",
VERSION, IMPNAME, __DATE__);
#else
term_printf("Codemist Common Lisp %s for %s: %s\n",
VERSION, IMPNAME, __DATE__);
#endif
}
#ifdef MEMORY_TRACE
if (car_counter != 0x7fffffff)
term_printf("Stop after %ld %lu..%lu\n",
car_counter, car_low, car_high);
#endif
#ifdef WINDOW_SYSTEM
ensure_screen();
/* If the user hits the close button here I may be in trouble */
#endif
#if defined HAVE_LIBPTHREAD || defined WIN32 || defined HAVE_CILK
if (number_of_processors() >= 3)
{ karatsuba_parallel = KARATSUBA_PARALLEL_CUTOFF;
if (kparallel > 0) karatsuba_parallel = kparallel;
}
#endif
/*
* Now dynamic code detects the floating point representation that is in use.
* I thought/hoped that doing it this way would be safer than relying on having
* pre-defined symbols that tracked the machine architecture.
*/
{ volatile union fpch { double d; unsigned char c[8]; } d;
/*
* The following looks at the floating point representation of the
* number 1/7 (in double precision) and picks out two bytes from
* the middle of the first word - where I hope that rounding issues
* will be remote. Investigation shows that these two bytes can be
* used to discriminate among at least a worthwhile range of
* representations, and I will exploit this to help me re-load
* heap-images in a way that allows images to be portable across
* different architectures.
*/
int w;
d.d = 1.0/7.0;
/* The coding here was adjusted to survive a Debian ARM Linux port. */
w = ((d.c[1] & 0xff) << 8) | (d.c[2] & 0xff);
switch (w)
{
/*
* At one stage I detected (on of the) VAX representations and the one used
* by the IBM s60/s370. These days I am only going to recognise cases that
* use IEEE layout. Even with that the example machines noted here reveal
* that evenb though IEEE explains what bits should be in the floating point
* value different manufacturers pack the words and bytes in a variety of
* ways! Well the mere shuffling of bytes is something I can deal with. If I
* really needed to make image files portable to old-style IBM mainframes
* or on a xArch machine set up to use hexadecimal floating point mode then
* what I have here would moan. But if I just override the moan I will
* be able to build images and reload them on that particular machine.
*/
case 0x2449: current_fp_rep = 0;
break; /* Intel, MIPS */
case 0x49c2: current_fp_rep = FP_WORD_ORDER;
break; /* ARM */
case 0x4924: current_fp_rep = FP_BYTE_ORDER;
break; /* may never happen? */
case 0xc249: current_fp_rep = FP_WORD_ORDER|FP_BYTE_ORDER;
break; /* SPARC */
/*
* The next line is probably not very good under a window manager, but
* it is a case that ought never to arise, so I will not bother.
*/
default: term_printf("Unknown floating point format %.4x\n", w);
my_exit(EXIT_FAILURE);
}
}
/*
* Up until the time I call setup() I may only use term_printf for
* output, because the other relevant streams will not have been set up.
* 1 bit: 0 for cold, 1 for warm.
* 2 bit: 1 to request initial allocation of memory.
*/
setup(restartp ? 3 : 2, store_size);
{ nil_as_base
/*
* If the user had used "-g" on the command line that will have set
* errorset_min and I use that to trigger during on gc and fasl messages.
*/
if (errorset_min == 3) miscflags |= GC_MESSAGES | FASL_MESSAGES;
}
#ifndef COMMON
#ifdef HAVE_FWIN
fwin_menus(loadable_packages, switches, review_switch_settings);
#endif
#endif
/*
* Now that setup is complete (and I have done any authorisation I want to)
* I will seed the random number generator as requested by the user. The
* default will be to put it in an unpredictable (well hard to predict!)
* state
*/
Csrand((uint32_t)initial_random_seed, (uint32_t)seed2);
gc_time += pop_clock();
countdown = software_ticks;
interrupt_pending = already_in_gc = NO;
tick_pending = tick_on_gc_exit = NO;
#ifndef HAVE_FWIN
/*
* "^C" trapping and handling happens within fwin if that is available.
*/
#ifndef UNDER_CE
sigint_must_longjmp = NO;
signal(SIGINT, sigint_handler);
#endif
#endif
ensure_screen();
procedural_output = NULL;
#ifdef HAVE_FWIN
/*
* OK, if I get this far I will suppose that any messages that report utter
* disasters will have reached the user, so I can allow FWIN to terminate
* rather more promptly.
*/
fwin_pause_at_end = 0;
#endif
}
#ifdef HAVE_FWIN
#ifdef HAVE_LIBFOX
/*
* Activate the BREAK and BACKTRACE menu items. Note not needed unless
* FOX is used and so there is a prospect of theer actually being menus!
*/
/*
* The next line causes a MOAN using Sun's compiler, ending up with
* an undefined reference to fwin_callback_to_interrupt!
*/
fwin_callback_to_interrupt(async_interrupt);
#endif /* HAVE_LIBFOX */
#endif /* HAVE_FWIN */
}
#if 0
#ifdef SOCKETS
#define SOCKET_BUFFER_SIZE 1024
/*
* The following two "character codes" are used when CSL is run as
* a socket server and wrap around prompt text. This could be in
* conflict with any code that tries to use these codes for other
* purposes or that handles prompts itself...
*/
#define CH_PROMPT 0x9a
#define CH_ENDPROMPT 0x9c
static char socket_in[SOCKET_BUFFER_SIZE], socket_out[SOCKET_BUFFER_SIZE];
static int socket_in_p = 0, socket_in_n = 0,
socket_out_p = 0, socket_prev = '\n';
static int char_from_socket(void)
{
int c;
clock_t c0;
time_t t0;
if (socket_server == 0)
{ socket_prev = ' ';
return EOF;
}
/*
* I generate a prompt whenever I am about to read the character that
* follows a newline. The prompt is issued surrounded by control
* characters 0x9a and 0x9c. That curious arrangement is inherited from
* internal behaviour in my Windows interface code and could be altered
* if something truly better could be invented.
*/
if (socket_prev == '\n')
{ term_printf("%c%s%c", CH_PROMPT, prompt_string, CH_ENDPROMPT);
ensure_screen();
}
if (socket_in_n == 0)
{ for (;;)
{ socket_in_n = recv(socket_server, socket_in, SOCKET_BUFFER_SIZE, 0);
c0 = clock();
t0 = time(NULL);
if (c0 > cpu_timeout || t0 > elapsed_timeout)
{ cpu_timeout = c0 + 20;
elapsed_timeout = t0 + 20;
term_printf(
"\nSorry: timeout on this session. Closing down.\n");
socket_prev = ' ';
return EOF;
}
if (socket_in_n <= 0)
#ifndef EWOULDBLOCK
# define EWOULDBLOCK WSAEWOULDBLOCK
#endif
{ if (errno == EWOULDBLOCK)
{
#ifdef WIN32
Sleep(1000); /* Arg in milliseconds here */
#else
sleep(1); /* Delay 1 second before re-polling */
#endif
continue;
}
closesocket(socket_server);
socket_server = 0;
socket_prev = ' ';
return EOF;
}
else break;
}
socket_in_p = 0;
}
c = socket_in[socket_in_p++];
if (c == 0x0a || c == 0x0d) c = '\n';
socket_in_n--;
socket_prev = c;
return c & 0xff;
}
static int char_to_socket(int c)
{
if (socket_server == 0) return 1;
socket_out[socket_out_p++] = (char)c;
if (c == '\n' || socket_out_p == SOCKET_BUFFER_SIZE)
{ if (send(socket_server, socket_out, socket_out_p, 0) < 0)
{ closesocket(socket_server);
socket_server = 0;
return 1;
}
socket_out_p = 0;
}
return 0;
}
void flush_socket(void)
{
if (socket_server == 0) return;
if (send(socket_server, socket_out, socket_out_p, 0) < 0)
{ closesocket(socket_server);
socket_server = 0;
}
socket_out_p = 0;
}
#endif
#endif
static void cslaction(void)
/*
* This is the "standard" route into CSL activity - it uses file-names
* from the decoded command-line as files to be read and processed
* unless the system was launched with the flag that says it ought to try
* to provide a network service on some socket.
*/
{
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
#ifdef __cplusplus
errorset_msg = NULL;
try
#else
#ifdef USE_SIGALTSTACK
sigjmp_buf this_level;
#else
jmp_buf this_level;
#endif
errorset_buffer = &this_level;
errorset_msg = NULL;
#ifdef USE_SIGALTSTACK
if (!sigsetjmp(this_level, -1))
#else
if (!setjmp(this_level))
#endif
#endif
{
#ifndef UNDER_CE
signal(SIGFPE, low_level_signal_handler);
if (segvtrap) signal(SIGSEGV, low_level_signal_handler);
#ifdef SIGBUS
if (segvtrap) signal(SIGBUS, low_level_signal_handler);
#endif
#ifdef SIGILL
if (segvtrap) signal(SIGILL, low_level_signal_handler);
#endif
#endif
non_terminal_input = NULL;
#if 0
#ifdef SOCKETS
if (socket_server)
{ ensure_screen();
procedural_input = char_from_socket;
procedural_output = char_to_socket;
lisp_main();
ensure_screen();
procedural_input = NULL;
procedural_output = NULL;
}
else
#endif
#endif
#ifdef WINDOW_SYSTEM
terminal_eof_seen = 0;
#endif
if (number_of_input_files == 0) lisp_main();
else
{ int i;
for (i=0; i<number_of_input_files; i++)
{ if (strcmp(files_to_read[i], "-") == 0)
{ non_terminal_input = NULL;
#ifdef WINDOW_SYSTEM
terminal_eof_seen = 0;
#endif
lisp_main();
}
else
{ char filename[LONGEST_LEGAL_FILENAME];
FILE *f;
memset(filename, 0, sizeof(filename));
f = open_file(filename, files_to_read[i],
strlen(files_to_read[i]), "r", NULL);
if (f == NULL)
err_printf("\n+++ Could not read file \"%s\"\n",
files_to_read[i]);
else
{ if (init_flags & INIT_VERBOSE)
term_printf("\n+++ About to read file \"%s\"\n",
files_to_read[i]);
report_file(filename);
non_terminal_input = f;
lisp_main();
fclose(f);
}
}
}
}
}
#ifdef __cplusplus
catch (char *)
#else
else
#endif
{ if (errorset_msg != NULL)
{ term_printf("\n%s detected\n", errorset_msg);
errorset_msg = NULL;
}
return;
}
}
int cslfinish(character_writer *w)
{
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
procedural_output = w;
if (Ifinished())
term_printf("\n+++ Errors on checkpoint-image file\n");
#ifdef TRACED_EQUAL
dump_equals();
#endif
/*
* clock_t is an arithmetic type but I do not know what sort - so I
* widen to double to do arithmetic on it. Actually what I MUST do is
* to compute a time difference in the type clock_t and hope I never
* get a difference that that overflows. The worst case I know of overflows
* after 35 minutes.
*/
if (init_flags & INIT_VERBOSE)
{ long int t = (long int)(100.0 * (consolidated_time[0] +
(double)(read_clock() - base_time)/
(double)CLOCKS_PER_SEC));
long int gct = (long int)(100.0 * gc_time);
term_printf("\n\nEnd of Lisp run after %ld.%.2ld+%ld.%.2ld seconds\n",
t/100, t%100, gct/100, gct%100);
}
#ifdef DEBUG_SOFTWARE_TICKS
term_printf("%d ticks processed (%d)\n",
number_of_ticks, SOFTWARE_TICKS);
#endif
drop_heap_segments();
if (spool_file != NULL)
{
#ifdef COMMON
fprintf(spool_file, "\nFinished dribbling to %s.\n", spool_file_name);
#else
fprintf(spool_file, "\n+++ Transcript closed at end of run +++\n");
#endif
#ifndef DEBUG
fclose(spool_file);
spool_file = NULL;
#endif
}
ensure_screen();
procedural_output = NULL;
return return_code;
}
int execute_lisp_function(char *fname,
character_reader *r,
character_writer *w)
{
Lisp_Object nil;
Lisp_Object ff;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
ff = make_undefined_symbol(fname);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to make the symbol */
}
procedural_input = r;
procedural_output = w;
Lapply0(nil, ff);
ensure_screen();
procedural_input = NULL;
procedural_output = NULL;
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failure during evaluation */
}
return 0;
}
/*
* People who want to use this in an embedded context can predefine
* NO_STARTUP_CODE and provide their own entrypoint...
*/
#ifndef NO_STARTUP_CODE
/*
* The next fragment of code is to help with the use of CSL (and hence
* packages written in Lisp and supported under CSL) as OEM products
* embedded within larger C-coded packages. There is (of course) a
* significant issue about clashes between the names of external symbols
* if CSL is to be linked with anything else, but I will not worry about that
* just yet.
* The protocol for calling Lisp code from C is as follows:
*
* cslstart(argc, argv, writer);allocate memory and Lisp heap etc. Args
* should be "as if" CSL was being called
* directly and this was the main entrypoint.
* The extra arg accepts output from this
* stage. Use NULL to get standard I/O.
* execute_lisp_function(fname, reader, writer);
* fname is a (C) string that names a Lisp
* function of 0 args. This is called with
* stdin/stdout access redirected to use the
* two character-at-a-time functions passed
* down. [Value returned indicates if
* the evaluation succeeded?]
* cslfinish(writer); Tidies up ready to stop.
*/
#ifdef SAMPLE_OF_PROCEDURAL_INTERFACE
static char ibuff[100], obuff[100];
static int ibufp = 0, obufp = 0;
static int iget()
{
int c = ibuff[ibufp++];
if (c == 0) return EOF;
else return c;
}
static void iput(int c)
{
if (obufp < sizeof(obuff)-1)
{ obuff[obufp++] = c;
obuff[obufp] = 0;
}
}
#endif
static int submain(int argc, char *argv[])
{
cslstart(argc, argv, NULL);
#ifdef SAMPLE_OF_PROCEDURAL_INTERFACE
strcpy(ibuff, "(print '(a b c d))");
execute_lisp_function("oem-supervisor", iget, iput);
printf("Buffered output is <%s>\n", obuff);
#else
if (module_enquiry == NULL) cslaction();
#endif
my_exit(cslfinish(NULL));
/*
* The "return 0" here is unreachable but it still quietens down as many
* C compilers as it causes to moan noisily!
*/
return 0;
}
#if defined HAVE_FWIN && !defined EMBEDDED
#define ENTRYPOINT fwin_main
extern int ENTRYPOINT(int argc, char *argv[]);
int main(int argc, char *argv[])
{
fwin_set_lookup(look_in_lisp_variable);
return fwin_startup(argc, argv, ENTRYPOINT);
}
#else
#define ENTRYPOINT main
#endif
int ENTRYPOINT(int argc, char *argv[])
{
int res;
#ifdef EMBEDDED
if (find_program_directory(argv[0]))
{ fprintf(stderr, "Unable to identify program name and directory\n");
return 1;
}
#endif
#ifdef USE_MPI
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD,&mpi_size);
printf("I am mpi instance %d of %d.\n", mpi_rank+1, mpi_size);
#endif
#ifdef HAVE_FWIN
strcpy(about_box_title, "About CSL");
strcpy(about_box_description, "Codemist Standard Lisp");
#endif
#ifdef __cplusplus
try { res = submain(argc, argv); }
catch(int r) { res = r; }
#else
#ifdef USE_SIGALTSTACK
if (!sigsetjmp(my_exit_buffer, -1)) res = submain(argc, argv);
#else
if (!setjmp(my_exit_buffer)) res = submain(argc, argv);
#endif
else res = my_return_code;
#endif
#ifdef USE_MPI
MPI_Finalize();
#endif
return res;
}
#endif /* NO_STARTUP_CODE */
/*
* And here are some functions that may help use Reduce, as an alternative
* to the very general escape that execute_lisp_function provides... If
* these return an integer it will genarlly be zero for success and non-
* zero for failure.
*/
/*
* After having called cslstart() you can set the I/O callback functions
* using this. If you set one or both to NULL this indicates use of
* stdin/stdout as per usual rather than an callback, otherwise whenever
* anybody wants to read or write they use these procedures. It is then
* your responsibility to cope with whatever text gets exchanged!
*/
int PROC_set_callbacks(character_reader *r,
character_writer *w)
{
procedural_input = r;
procedural_output = w;
return 0; /* can never report failure */
}
int PROC_load_package(char *name)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil, w1 = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
w1 = make_undefined_symbol("load-package");
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to make the load-package */
}
push(w1);
w = make_undefined_symbol(name);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to make name */
}
pop(w1);
Lapply1(nil, w1, w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* Failed to load the package */
}
return 0;
}
int PROC_set_switch(char *name, int val)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil, w1 = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
w1 = make_undefined_symbol("onoff");
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to make the onoff */
}
push(w1);
w = make_undefined_symbol(name);
pop(w1);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to make name */
}
Lapply2(nil, 3, w1, w, val == 0 ? nil : lisp_true);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* Failed to set the switch */
}
return 0;
}
int PROC_gc_messages(int n)
{
Lisp_Object nil = C_nil;
Lverbos(nil, fixnum_of_int(n)); /* can not fail */
return 0;
}
/*
* Expressions are entered in Reverse Polish Notation, This call clears
* the stack. It is probably only wanted if there has been an error
* of some sort.
*/
int PROC_clear_stack()
{
Lisp_Object nil = C_nil;
procstack = nil;
return 0; /* can never fail! */
}
/*
* The RPN stack is used to build a prefix-form expression for
* evaluation. This code creates a Lisp symbol and pushes it.
*/
int PROC_push_symbol(const char *name)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
w = make_undefined_symbol(name);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to make the symbol */
}
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
/*
* stack = the-string . stack;
*/
int PROC_push_string(const char *data)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
w = make_string(data);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to make the string */
}
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
/*
* Return a handle to the top item on the stack, and pop the stack.
* The value here will be a RAW LISP structure and NOT at all necessarily
* anything neat.
*/
/*
* Push an integer, which should fit within the constraints of a
* 28-bit fixnum.
*/
int PROC_push_small_integer(int32_t n)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
w = make_lisp_integer32(n);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to create number */
}
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
int PROC_push_big_integer(const char *n)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
int len = 0;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
/* Here I need to parse a C string to obtain a Lisp number. */
boffop = 0;
while (*n != 0)
{ packbyte(*n++);
len++;
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* boffo trouble */
}
}
w = intern(len, 0);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* conversion to number */
}
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
int PROC_push_floating(double n)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
/* Here I have to construct a Lisp (boxed) float */
w = make_boxfloat(n, TYPE_DOUBLE_FLOAT);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 1; /* Failed to create the float */
}
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
/*
* To make an expression
* (f a1 a2 a3)
* you go
* push(a1)
* push(a2)
* push(a3)
* make_function_call("f", 3)
*/
int PROC_make_function_call(const char *name, int n)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil, w1 = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
while (n > 0)
{ if (procstack == nil) return 1; /* Not enough args available */
w = cons(qcar(procstack), w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = qcdr(procstack);
n--;
}
push(w);
w1 = make_undefined_symbol(name);
pop(w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* Failed to create function name */
}
w = cons(w1, w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 4; /* Failed to cons on function name */
}
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 5; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
/*
* Take the top item on the stack and save it in location n (0 <= n <= 99).
*/
int PROC_save(int n)
{
Lisp_Object nil = C_nil;
if (n < 0 || n > 99) return 1; /* index out of range */
if (procstack == nil) return 2; /* Nothing available to save */
elt(procmem, n) = qcar(procstack);
procstack = qcdr(procstack);
return 0;
}
/*
* Push onto the stack the value saved at location n. See PROC_save.
*/
int PROC_load(int n)
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
if (n < 0 || n > 99) return 1; /* index out of range */
w = elt(procmem, n);
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
/*
* Duplicate the top item on the stack.
*/
int PROC_dup()
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
if (procstack == nil) return 1; /* no item to duplicate */
w = qcar(procstack);
w = cons(w, procstack);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed to push onto stack */
}
procstack = w;
return 0;
}
int PROC_pop()
{
Lisp_Object nil = C_nil;
if (procstack == nil) return 1; /* stack is empty */
procstack = qcdr(procstack);
return 0;
}
/*
* Replaces the top item on the stack with a simplified version of
* itself. For experts on Reduce internals I note that this wraps
* the simplified form up in a prefix-like "!*sq" wrapper so it can
* still be used in a prefix context.
*/
int PROC_simplify()
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil, w1 = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
if (procstack == nil) return 1; /* stack is empty */
w = make_undefined_symbol("simp");
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed find "simp" */
}
w = Lapply1(nil, w, qcar(procstack));
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* Call to simp failed */
}
push(w);
w1 = make_undefined_symbol("mk*sq");
pop(w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 4; /* Failed to find "mk!*sq" */
}
w = Lapply1(nil, w1, w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 5; /* Call to mk!*sq failed */
}
qcar(procstack) = w;
return 0;
}
/*
* Replace the top item on the stack with whatever is obtained by using
* the Lisp EVAL operatio on it. Note that this is not intended for
* casual use - if there is any functionality that you need PLEASE ask
* me to put in a cleaner abstraction to support it.
*/
static void PROC_standardise_gensyms(Lisp_Object w)
{
Lisp_Object nil = C_nil;
if (consp(w))
{ push(qcdr(w));
PROC_standardise_gensyms(qcar(w));
pop(w);
errexitv();
PROC_standardise_gensyms(w);
return;
}
/*
* Now w is atomic. The only case that concerns me is if it is a gensym.
*/
if (symbolp(w)) get_pname(w); /* allocates gensym name if needed. */
}
int PROC_lisp_eval()
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
if (procstack == nil) return 1; /* stack is empty */
w = Ceval(qcar(procstack), nil);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Evaluation failed */
}
push(w);
PROC_standardise_gensyms(w);
pop(w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* gensym patchup failed */
}
qcar(procstack) = w;
return 0;
}
static Lisp_Object PROC_standardise_printed_form(Lisp_Object w)
{
Lisp_Object nil = C_nil, w1;
if (consp(w))
{ push(qcdr(w));
w1 = PROC_standardise_printed_form(qcar(w));
pop(w);
errexit();
push(w1);
w = PROC_standardise_printed_form(w);
pop(w1);
errexit();
return cons(w1, w);
}
/*
* Now w is atomic. There are two interesting cases - an unprinted gensym
* and a bignum.
*/
if (symbolp(w))
{ push(w);
get_pname(w); /* allocates gensym name if needed. Otherwise cheap! */
pop(w);
errexit();
return w;
}
else if (is_numbers(w) && is_bignum(w))
{ w = Lexplode(nil, w); /* Bignum to list of digits */
errexit();
w = Llist_to_string(nil, w); /* list to string */
errexit();
return w;
}
else return w;
}
/*
* Replaces the top item on the stack with version that is in
* a simple prefix form. This prefix form should be viewed as
* unsuitable for inclusion in any further expression.
*/
int PROC_make_printable()
{
Lisp_Object nil = C_nil;
Lisp_Object w = nil, w1 = nil;
#ifdef CONSERVATIVE
volatile Lisp_Object sp;
C_stackbase = (Lisp_Object *)&sp;
#endif
if (procstack == nil) return 1; /* stack is empty */
/*
* I want to use "simp" again so that I can then use prepsq!
*/
w = make_undefined_symbol("simp");
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 2; /* Failed find "simp" */
}
w = Lapply1(nil, w, qcar(procstack));
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 3; /* Call to simp failed */
}
push(w);
w1 = make_undefined_symbol("prepsq");
pop(w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 4; /* Failed to find "prepsq" */
}
w = Lapply1(nil, w1, w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 5; /* Call to prepsq failed */
}
/*
* There are going to be two things I do next. One is to ensure that
* all gensyms have print-names, the other is to convert bignums into
* strings. Both of these could be viewed as mildly obscure!
*/
w = PROC_standardise_printed_form(w);
nil = C_nil;
if (exception_pending())
{ flip_exception();
return 6; /* standardise_printed_form failed */
}
qcar(procstack) = w;
return 0;
}
PROC_handle PROC_get_value()
{
Lisp_Object nil = C_nil;
Lisp_Object w;
if (procstack == nil) w = fixnum_of_int(0);
else
{ w = qcar(procstack);
procstack = qcdr(procstack);
}
return (PROC_handle)w;
}
PROC_handle PROC_get_raw_value()
{
Lisp_Object nil = C_nil;
Lisp_Object w;
if (procstack == C_nil) w = nil;
else
{ w = qcar(procstack);
procstack = qcdr(procstack);
}
return (PROC_handle)w;
}
/*
* return true if the expression is atomic.
*/
int PROC_atom(PROC_handle p)
{
return !consp((Lisp_Object)p);
}
/*
* return true if the expression is NIL.
*/
int PROC_null(PROC_handle p)
{
return ((Lisp_Object)p) == C_nil;
}
/*
* Return true if it is a small integer.
*/
int PROC_fixnum(PROC_handle p)
{
return is_fixnum((Lisp_Object)p);
}
int PROC_string(PROC_handle p)
{
return is_vector((Lisp_Object)p) &&
type_of_header(vechdr((Lisp_Object)p)) == TYPE_STRING;
}
int PROC_floatnum(PROC_handle p)
{
/*
* I ignore the "sfloat" representation that would be relevant in Common
* Lisp mode. It is not used with Reduce.
*/
return is_bfloat((Lisp_Object)p);
}
/*
* Return true if it is a symbol.
*/
int PROC_symbol(PROC_handle p)
{
return symbolp((Lisp_Object)p);
}
/*
* Given that it is a small integer return the integer value
*/
int32_t PROC_integer_value(PROC_handle p)
{
return (int32_t)int_of_fixnum((Lisp_Object)p);
}
double PROC_floating_value(PROC_handle p)
{
return double_float_val((Lisp_Object)p);
}
/*
* Given that it is a symbol, return a string that is its name. Note
* that this must not be too long, and that the value returned is in
* a static buffer. Note that this would crash if the item was a
* "gensym" that had not been printed before, and so I take care to
* sort that out in PROC_make_printable.
* Hmmm the name-length restriction here is ugly _ will wait and see how
* long it is before somebody falls foul of it!
*/
static char PROC_name[256];
const char *PROC_symbol_name(PROC_handle p)
{
Lisp_Object w = (Lisp_Object)p;
int n;
w = qpname(w);
n = length_of_header(vechdr(w)) - CELL;
if (n > sizeof(PROC_name)-1) n = sizeof(PROC_name)-1;
strncpy(PROC_name, &celt(w, 0), n);
PROC_name[n] = 0;
return &PROC_name[0];
}
const char *PROC_string_data(PROC_handle p)
{
Lisp_Object w = (Lisp_Object)p;
int n;
n = length_of_header(vechdr(w)) - CELL;
/*
* NOTE that I truncate long strings here. Boo Hiss! This may make a mess
* of dealing with big numbers, so in due course I will need to fix it!
*/
if (n > sizeof(PROC_name)-1) n = sizeof(PROC_name)-1;
strncpy(PROC_name, &celt(w, 0), n);
PROC_name[n] = 0;
return &PROC_name[0];
}
/*
* First and rest allow list traversal.
*/
PROC_handle PROC_first(PROC_handle p)
{
return (PROC_handle)qcar((Lisp_Object)p);
}
PROC_handle PROC_rest(PROC_handle p)
{
return (PROC_handle)qcdr((Lisp_Object)p);
}
/* End of csl.c */