aboutsummaryrefslogtreecommitdiffstats
path: root/bwbasic.c
blob: 45494950040b0f68c68a8b920d3f99c546094c79 (plain) (blame)
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
/***************************************************************
  
        bwbasic.c       Main Program File
                        for Bywater BASIC Interpreter
  
                        Copyright (c) 1993, Ted A. Campbell
                        Bywater Software
  
                        email: tcamp@delphi.com
  
        Copyright and Permissions Information:
  
        All U.S. and international rights are claimed by the author,
        Ted A. Campbell.
  
   This software is released under the terms of the GNU General
   Public License (GPL), which is distributed with this software
   in the file "COPYING".  The GPL specifies the terms under
   which users may copy and use the software in this distribution.
  
   A separate license is available for commercial distribution,
   for information on which you should contact the author.
  
***************************************************************/

/*---------------------------------------------------------------*/
/* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
/* 11/1995 (eidetics@cerf.net).                                  */
/*                                                               */
/* Those additionally marked with "DD" were at the suggestion of */
/* Dale DePriest (daled@cadence.com).                            */
/*                                                               */
/* Version 3.00 by Howard Wulf, AF5NE                            */
/*                                                               */
/* Version 3.10 by Howard Wulf, AF5NE                            */
/*                                                               */
/* Version 3.20 by Howard Wulf, AF5NE                            */
/*                                                               */
/*---------------------------------------------------------------*/



#include "bwbasic.h"

static void break_handler (void);
static void break_mes (int x);
static int bwb_init (void);
static void bwb_initialize_warnings (void);
static void bwb_interact (void);
static int bwb_ladd (char *buffer, LineType * p, int IsUser);
static void bwb_single_step (char *buffer);
static void bwb_xtxtline (char *buffer);
static int bwx_signon (void);
static void execute_profile (char *FileName);
static void execute_program (char *FileName);
static char *FindClassicStatementEnd (char *C);
static int FixQuotes (char *tbuf);
static void ImportClassicIfThenElse (char *InBuffer);
static int is_ln (char *buffer);
static int is_numconst (char *buffer);
static void mark_preset_variables (void);
static FILE *nice_open (char *BaseName);
static void process_basic_line (char *buffer);

GlobalType *My = NULL;

static char *Banner[] = {
  "########  ##    ## ##      ##    ###    ######## ######## ########            ",
  "##     ##  ##  ##  ##  ##  ##   ## ##      ##    ##       ##     ##           ",
  "##     ##   ####   ##  ##  ##  ##   ##     ##    ##       ##     ##           ",
  "########     ##    ##  ##  ## ##     ##    ##    ######   ########            ",
  "##     ##    ##    ##  ##  ## #########    ##    ##       ##   ##             ",
  "##     ##    ##    ##  ##  ## ##     ##    ##    ##       ##    ##            ",
  "########     ##     ###  ###  ##     ##    ##    ######## ##     ##           ",
  "                                                                              ",
  "                                                                              ",
  "                                    ########     ###     ######  ####  ###### ",
  "                                    ##     ##   ## ##   ##    ##  ##  ##    ##",
  "                                    ##     ##  ##   ##  ##        ##  ##      ",
  "                                    ########  ##     ##  ######   ##  ##      ",
  "                                    ##     ## #########       ##  ##  ##      ",
  "                                    ##     ## ##     ## ##    ##  ##  ##    ##",
  "                                    ########  ##     ##  ######  ####  ###### ",
  "                                                                              ",
  "Bywater BASIC Interpreter, version 3.20                                       ",
  "Copyright (c) 1993, Ted A. Campbell                                           ",
  "Copyright (c) 1995-1997, Jon B. Volkoff                                       ",
  "Copyright (c) 2014-2017, Howard Wulf, AF5NE                                   ",
  "                                                                              ",
  NULL
};

#define NUM_WARNINGS 80

static char *ERROR4[NUM_WARNINGS];

static void
bwb_initialize_warnings (void)
{
  int i;
  for (i = 0; i < NUM_WARNINGS; i++)
  {
    ERROR4[i] = NULL;
  }
  ERROR4[1] = "NEXT without FOR";
  ERROR4[2] = "Syntax error";
  ERROR4[3] = "RETURN without GOSUB";
  ERROR4[4] = "Out of DATA";
  ERROR4[5] = "Illegal function call";
  ERROR4[6] = "Overflow";
  ERROR4[7] = "Out of memory";
  ERROR4[8] = "Undefined line";
  ERROR4[9] = "Subscript out of range";
  ERROR4[10] = "Redimensioned array";
  ERROR4[11] = "Division by zero";
  ERROR4[12] = "Illegal direct";
  ERROR4[13] = "Type mismatch";
  ERROR4[14] = "Out of string space";
  ERROR4[15] = "String too long";
  ERROR4[16] = "String formula too complex";
  ERROR4[17] = "Can't continue";
  ERROR4[18] = "Undefined user function";
  ERROR4[19] = "No RESUME";
  ERROR4[20] = "RESUME without error";
  ERROR4[21] = "Unprintable error";
  ERROR4[22] = "Missing operand";
  ERROR4[23] = "Line buffer overflow";
  ERROR4[26] = "FOR without NEXT";
  ERROR4[27] = "Bad DATA";
  ERROR4[29] = "WHILE without WEND";
  ERROR4[30] = "WEND without WHILE";
  ERROR4[31] = "EXIT FUNCTION without FUNCTION";
  ERROR4[32] = "END FUNCTION without FUNCTION";
  ERROR4[33] = "EXIT SUB without SUB";
  ERROR4[34] = "END SUB without SUB";
  ERROR4[35] = "EXIT FOR without FOR";
  ERROR4[50] = "Field overflow";
  ERROR4[51] = "Internal error";
  ERROR4[52] = "Bad file number";
  ERROR4[53] = "File not found";
  ERROR4[54] = "Bad file mode";
  ERROR4[55] = "File already open";
  ERROR4[57] = "Disk I/O error";
  ERROR4[58] = "File already exists";
  ERROR4[61] = "Disk full";
  ERROR4[62] = "Input past end";
  ERROR4[63] = "Bad record number";
  ERROR4[64] = "Bad file name";
  ERROR4[66] = "Direct statement in file";
  ERROR4[67] = "Too many files";
  ERROR4[70] = "Variable Not Declared";
  ERROR4[73] = "Advanced Feature";
}

/***************************************************************
  
      FUNCTION:       bwx_terminate()
  
   DESCRIPTION:    This function terminates program execution.
  
***************************************************************/

void
bwx_terminate (void)
{
   
  exit (0);
}



/***************************************************************
  
   FUNCTION:       break_handler()
  
   DESCRIPTION:    This function is called by break_mes()
         and handles program interruption by break
         (or by the STOP command).
  
***************************************************************/
static void
break_handler (void)
{
  /*
   **
   **
   */
  assert( My != NULL );

  My->AutomaticLineNumber = 0;
  My->AutomaticLineIncrement = 0;

  if (My->IsInteractive)
  {
    /* INTERACTIVE: terminate program */

    /* reset all stack counters */
    bwb_clrexec ();
    SetOnError (0);

    My->ERR = -1;                /* in break_handler() */


    /* reset the break handler */
    signal (SIGINT, break_mes);

    longjmp (My->mark, -1);


    return;
  }
  /* NOT INTERACTIVE:  terminate immediately */
  bwx_terminate ();
}

/***************************************************************
  
   FUNCTION:       break_mes()
  
   DESCRIPTION:    This function is called (a) by a SIGINT
         signal or (b) by bwb_STOP via bwx_STOP.
         It prints an error message then calls
         break_handler() to terminate the program.
  
***************************************************************/

static void
break_mes (int x /* Parameter 'x' is never used */ )
{
  /*
   **
   ** break_mes is FATAL.
   ** x == SIGINT for control-C
   ** x == 0 for bwx_STOP
   **
   */
  assert( My != NULL );
  assert( My->SYSOUT != NULL );
  assert( My->SYSOUT->cfp != NULL );
  assert( My->CurrentVersion != NULL );

   
  if (My->ERR < 0)                /* in break_mes(), do not make a bad situation worse */
  {
    /* an error has already ben reported */
  }
  else
  {
    fputc ('\n', My->SYSOUT->cfp);
    ResetConsoleColumn ();
    if (My->CurrentVersion->OptionVersionValue & (C77))
    {
      if (is_empty_string (My->ProgramFilename) == FALSE)
      {
        fprintf (My->SYSOUT->cfp, "FILE:%s, ", My->ProgramFilename);
      }
    }
    fprintf (My->SYSOUT->cfp, "Program interrupted");
    if (My->ThisLine)                /* break_mes */
    {
      if (My->ThisLine->LineFlags & (LINE_USER))        /* break_mes */
      {
        /* don't print the line number */
      }
      else
      {
        fprintf (My->SYSOUT->cfp, " at line %d", My->ThisLine->number);        /* break_mes */
      }
    }
    fputc ('\n', My->SYSOUT->cfp);
    ResetConsoleColumn ();
    fflush (My->SYSOUT->cfp);
  }
  break_handler ();
}

extern void
bwx_STOP (int IsShowMessage)
{
   
  if (IsShowMessage)
  {
    break_mes (0);
  }
  else
  {
    break_handler ();
  }
}

static int
bwx_signon (void)
{
  /*
   **
   ** Display a sign-on banner.
   ** NOT called if a file is provided on the command line.
   **
   */
  int i;
  assert( My != NULL );
  assert( My->SYSOUT != NULL );
  assert( My->SYSOUT->cfp != NULL );

   
  for (i = 0; Banner[i] != NULL; i++)
  {
    fprintf (My->SYSOUT->cfp, "%s\n", Banner[i]);
  }
  ResetConsoleColumn ();
  return TRUE;
}



DoubleType
bwx_TIMER (DoubleType Seconds)
{
  /*
   **
   ** Return a number representing Seconds in the future.  Seconds >= 0.
   ** Seconds may be non-integer, such as 0.001 or 86399.999. The
   ** maximum allowed Seconds is MAXDBL. This is used two ways: 
   **
   ** 1) in bwb_TIMER(), the ON TIMER count (>0) is used to determine 
   ** the expiration time.   In this case, simply return what the value 
   ** will be in the future.  Note that ON TIMER enforces 
   ** Seconds > (1 / CLOCKS_PER_SEC), and
   **
   ** 2) in bwb_execline(), zero (=0) is used to determine the current 
   ** time and compare it to #1. In this case, simply return what the 
   ** value is now.
   **
   ** Both the resolution of the timer, and the frequency of update, 
   ** are implementation defined. 
   **
   */
   
  if (Seconds < 0)
  {
    WARN_INTERNAL_ERROR;
    return 0;
  }
  else
  {
    DoubleType Result;
    Result = clock ();
    assert (CLOCKS_PER_SEC > 0);
    Result /= CLOCKS_PER_SEC;
    if (Seconds > 0)
    {
      Result += Seconds;
    }
    return Result;
  }
}

void
CleanLine (char *buffer)
{
  /* 
   **
   ** cleanup the line, so it is easier to parse 
   **
   */
  char *newbuffer;
   


  if (is_empty_string (buffer))
  {
    /* do nothing */
    return;
  }

  /* remove CR/LF */
  newbuffer = bwb_strchr (buffer, '\r');
  if (newbuffer != NULL)
  {
    *newbuffer = NulChar;
  }
  newbuffer = bwb_strchr (buffer, '\n');
  if (newbuffer != NULL)
  {
    *newbuffer = NulChar;
  }

  /* remove ALL embedded control characters */
  /* if you want a control character, then use CHR$ */
  newbuffer = buffer;
  while (*newbuffer != NulChar)
  {
    if (bwb_isprint (*newbuffer))
    {
      /* OK */
    }
    else
    {
      *newbuffer = ' ';
    }
    newbuffer++;
  }
  /* LTRIM$ */
  newbuffer = buffer;
  if (*newbuffer != NulChar)
  {
    /* not an empty line, so remove one (or more) leading spaces */
    while (*newbuffer == ' ')
    {
      newbuffer++;
    }
    if (newbuffer > buffer)
    {
      bwb_strcpy (buffer, newbuffer);
    }
  }
  /* RTRIM$ */
  newbuffer = buffer;
  if (*newbuffer != NulChar)
  {
    /* not an empty line, so remove one (or more) trailing spaces */
    char *E;

    E = bwb_strchr (newbuffer, NulChar);
    E--;
    while (E >= newbuffer && *E == ' ')
    {
      *E = NulChar;
      E--;
    }
  }
}

static int
bwb_init (void)
{
  /*
   **
   ** initialize Bywater BASIC
   **
   */
  int n;
  static char start_buf[] = "";
  static char end_buf[] = "";

  /* Memory allocation */
  if ((My = (GlobalType *) calloc (1, sizeof (GlobalType))) == NULL)
  {
    return FALSE;
  }
  if ((My->MaxLenBuffer =
       (char *) calloc (MAXLEN + 1 /* NulChar */ , sizeof (char))) == NULL)
  {
    return FALSE;
  }
  if ((My->NumLenBuffer =
       (char *) calloc (NUMLEN + 1 /* NulChar */ , sizeof (char))) == NULL)
  {
    return FALSE;
  }
  if ((My->ConsoleOutput =
       (char *) calloc (MAX_LINE_LENGTH + 1 /* NulChar */ ,
                        sizeof (char))) == NULL)
  {
    return FALSE;
  }
  if ((My->ConsoleInput =
       (char *) calloc (MAX_LINE_LENGTH + 1 /* NulChar */ ,
                        sizeof (char))) == NULL)
  {
    return FALSE;
  }
  if ((My->SYSIN = (FileType *) calloc (1, sizeof (FileType))) == NULL)
  {
    return FALSE;
  }
  if ((My->SYSOUT = (FileType *) calloc (1, sizeof (FileType))) == NULL)
  {
    return FALSE;
  }
  if ((My->SYSPRN = (FileType *) calloc (1, sizeof (FileType))) == NULL)
  {
    return FALSE;
  }
  if ((My->StartMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
  {
    return FALSE;
  }
  if ((My->UserMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
  {
    return FALSE;
  }
  if ((My->EndMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
  {
    return FALSE;
  }
  if ((My->EndMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
  {
    return FALSE;
  }
  if ((My->ERROR4 =
       (char *) calloc (MAX_ERR_LENGTH + 1 /* NulChar */ ,
                        sizeof (char))) == NULL)
  {
    return FALSE;
  }

  My->CurrentVersion = &bwb_vertable[0];
  My->IsInteractive = TRUE;
  My->OptionSleepDouble = 1;
  My->OptionIndentInteger = 2;
  My->OptionTerminalType = C_OPTION_TERMINAL_NONE;
  My->OptionRoundType = C_OPTION_ROUND_BANK;
  My->NextValidLineNumber = MINLIN;
  My->IncludeLevel = 0;                /* %INCLUDE */
  My->IsCommandLineFile = FALSE;
  My->ExternalInputFile = NULL;        /* for automated testing, --TAPE command line parameter */
  My->IsPrinter = FALSE;        /* CBASIC-II: CONSOLE and LPRINTER commands */
  My->OptionPromptString = DEF_PROMPT;
  My->OptionEditString = DEF_EDITOR;
  My->OptionFilesString = DEF_FILES;
  My->OptionRenumString = DEF_RENUM;
  My->OptionExtensionString = DEF_EXTENSION;
  My->OptionScaleInteger = 0;
  My->OptionDigitsInteger = SIGNIFICANT_DIGITS;
  My->OptionZoneInteger = ZONE_WIDTH;
  My->UseParameterString = NULL;
  My->ProgramFilename = NULL;

  My->StartMarker->number = MINLIN - 1;
  My->StartMarker->next = My->EndMarker;
  My->StartMarker->position = 0;
  My->StartMarker->buffer = start_buf;

  My->EndMarker->number = MAXLIN + 1;
  My->EndMarker->next = My->EndMarker;
  My->EndMarker->position = 0;
  My->EndMarker->buffer = end_buf;

  My->UserMarker->number = MINLIN - 1;
  My->UserMarker->next = My->EndMarker;
  My->UserMarker->position = 0;
  My->UserMarker->buffer = NULL;

  My->DataLine = My->EndMarker;
  My->DataPosition = 0;

  My->StackHead = NULL;
  My->StackDepthInteger = 0;

  My->FieldHead = NULL;

  My->VirtualHead = NULL;
  My->FileHead = NULL;
  My->ThisLine = My->StartMarker;        /* bwb_init */

  My->SYSIN->DevMode = DEVMODE_INPUT;
  My->SYSIN->width = 80;
  My->SYSIN->col = 1;
  My->SYSIN->row = 1;
  My->SYSIN->delimit = ',';
  My->SYSIN->cfp = stdin;

  My->SYSOUT->DevMode = DEVMODE_OUTPUT;
  My->SYSOUT->width = 80;
  My->SYSOUT->col = 1;
  My->SYSOUT->row = 1;
  My->SYSOUT->delimit = ',';
  My->SYSOUT->cfp = stdout;

  My->SYSPRN->DevMode = DEVMODE_OUTPUT;
  My->SYSPRN->width = 80;
  My->SYSPRN->col = 1;
  My->SYSPRN->row = 1;
  My->SYSPRN->delimit = ',';
  My->SYSPRN->cfp = stderr;

  My->LPRINT_NULLS = 0;
  My->SCREEN_ROWS = 24;

  /* OPEN #0 is an ERROR. */
  /* CLOSE #0 is an ERROR. */
  /* WIDTH #0, 80 is the same as WIDTH 80. */
  /* LPRINT and LLIST are sent to bwx_LPRINT() */

  /* default variable type */
  for (n = 0; n < 26; n++)
  {
    My->DefaultVariableType[n] = DoubleTypeCode;
  }
  /* default COMMAND$(0-9) */
  for (n = 0; n < 10; n++)
  {
    My->COMMAND4[n] = NULL;
  }

  /* initialize tables of variables, functions */
  bwb_initialize_warnings ();
  SortAllCommands ();
  SortAllFunctions ();
  SortAllOperators ();
  var_init ();
  IntrinsicFunction_init ();
  UserFunction_init ();
  OptionVersionSet (0);
  /* OK */
  return TRUE;
}

/***************************************************************
  
        FUNCTION:       main()
  
        DESCRIPTION:    As in any C program, main() is the basic
                        function from which the rest of the
                        program is called. Some environments,
         however, provide their own main() functions
         (Microsoft Windows (tm) is an example).
         In these cases, the following code will
         have to be included in the initialization
         function that is called by the environment.
  
***************************************************************/

static void
process_basic_line (char *buffer)
{
  CleanLine (buffer);
  if (is_empty_string (buffer))
  {
    /* empty -- do nothing */
  }
  else if (is_ln (buffer) == FALSE)
  {
    /* If there is no line number, then execute the line as received */
    /* RUN */
    WARN_CLEAR;                        /* process_basic_line */
    SetOnError (0);
    bwb_xtxtline (buffer);        /* process_basic_line */
  }
  else if (is_numconst (buffer) == TRUE)
  {
      /*-----------------------------------------------------------------*/
    /* Another possibility: if buffer is a numeric constant,           */
    /* then delete the indicated line number (JBV)                     */
      /*-----------------------------------------------------------------*/
    /* 100 */
    int LineNumber;
    LineNumber = atoi (buffer);
    WARN_CLEAR;                        /* process_basic_line */
    SetOnError (0);
    sprintf (buffer, "delete %d", LineNumber);
    bwb_xtxtline (buffer);        /* process_basic_line */
  }
  else
  {
    /* If there is a line number, then add it to the BASIC program */
    /* 100 REM */
    bwb_ladd (buffer, My->StartMarker, FALSE);
  }
}
static void
bwb_single_step (char *buffer)
{
  assert( My != NULL );
  assert (buffer != NULL);

  process_basic_line (buffer);
  while (My->StackHead != NULL)
  {
    bwb_execline ();
  }
}

static void
mark_preset_variables (void)
{
  /* mark all existing variables as preset */
  /* this includes all variables declared in any PROFILE */
  VariableType *v;
  assert( My != NULL );
   

  for (v = My->VariableHead; v != NULL; v = v->next)
  {
    v->VariableFlags |= VARIABLE_PRESET;
    v->VariableFlags |= VARIABLE_COMMON;
  }
}


static void
execute_profile (char *FileName)
{
  FILE *profile;
  assert( My != NULL );
  assert (FileName != NULL);

  My->NextValidLineNumber = MINLIN;
  profile = fopen (FileName, "r");
  if (profile == NULL)
  {
    /* NOT FOUND */
    /* OPTIONAL */
    return;
  }
  /* FOUND */
  if (My->IsInteractive)
  {
    /* 
     **
     ** set a buffer for jump: program execution returns to this point in
     ** case of a jump (error, interrupt, or finish program) 
     **
     */
    My->program_run = 0;
    signal (SIGINT, break_mes);
    setjmp (My->mark);
    if (My->program_run > 0)
    {
      /* error in PROFILE */
      exit (1);
    }
    My->program_run++;
  }

  /* 
     The profile only exists to allow executing OPTION ... commands.  No other use is supported. 
   */
  {
    char *tbuf;
    int tlen;

    tbuf = My->ConsoleInput;
    tlen = MAX_LINE_LENGTH;
    while (fgets (tbuf, tlen, profile))        /* execute_profile */
    {
      tbuf[tlen] = NulChar;
      bwb_single_step (tbuf);        /* in execute_profile() */
    }
    bwb_fclose (profile);
    mark_preset_variables ();
  }
}



static void
execute_program (char *FileName)
{
  assert( My != NULL );
  assert( My->SYSOUT != NULL );
  assert( My->SYSOUT->cfp != NULL );
   
  assert (FileName != NULL);

  My->NextValidLineNumber = MINLIN;
  My->IsCommandLineFile = TRUE;
  if (bwb_fload (FileName) == FALSE)
  {
    fprintf (My->SYSOUT->cfp, "Failed to open file %s\n", FileName);
    /* the file load has failed, so do NOT run the program */
    exit (1);
  }
  if (My->ERR < 0 /* in execute_program(), file load failed */ )
  {
    /* the file load has failed, so do NOT run the program */
    exit (1);
  }
  bwb_RUN (My->StartMarker);
}

extern int
main (int argc, char **argv)
{
  int i;
  assert (argc >= 0);
  assert (argv != NULL);

  if (bwb_init () == FALSE)
  {
    /* FATAL */
    puts ("Out of memory");
    return 1;
  }
  assert( My != NULL );
  assert( My->SYSOUT != NULL );
  assert( My->SYSOUT->cfp != NULL );
  assert( My->SYSIN != NULL );
  assert( My->SYSIN->cfp != NULL );

  /* Signon message banner */
  if (argc < 2)
  {
    /* no parameters */
    if (My->IsInteractive)
    {
      bwx_signon ();
    }
    else
    {
      /* if INTERACTIVE is FALSE, then we must have a program file */
      fputs ("Program file not specified\n", My->SYSOUT->cfp);
      return 1;
    }
  }

  if (My->IsInteractive)
  {
    /* 
     **
     ** set a buffer for jump: program execution returns to this point in
     ** case of a jump (error, interrupt, or finish program) 
     **
     */
    My->program_run = 0;
    signal (SIGINT, break_mes);
    setjmp (My->mark);
    if (My->program_run > 0)
    {
      /* error in profile */
      return 1;
    }
    My->program_run++;
  }

#if PROFILE
  execute_profile (PROFILENAME);
#endif

  /* check to see if there is a program file: but do this only the first time around! */
  for (i = 1; i < argc; i++)
  {
    /* 
       SYNTAX:  bwbasic [ --profile profile.bas ] [ --tape tapefile.inp ] [ program.bas ]
     */
    if (bwb_stricmp (argv[i], "--profile") == 0
        || bwb_stricmp (argv[i], "-p") == 0
        || bwb_stricmp (argv[i], "/profile") == 0
        || bwb_stricmp (argv[i], "/p") == 0)
    {
      i++;
      if (i < argc)
      {
        /* --profile profile.bas */
        execute_profile (argv[i]);
      }
    }
    else
      if (bwb_stricmp (argv[i], "--tape") == 0
          || bwb_stricmp (argv[i], "-t") == 0
          || bwb_stricmp (argv[i], "/tape") == 0
          || bwb_stricmp (argv[i], "/t") == 0)
    {
      i++;
      if (i < argc)
      {
        /* --tape tapefile.inp */
        My->ExternalInputFile = fopen (argv[i], "r");
      }
    }
    else
    {
      /* program.bas */
      {
        int j;
        int n;

        j = i;
        for (n = 0; n < 10 && j < argc; n++, j++)
        {
          My->COMMAND4[n] = argv[j];
        }
      }
      execute_program (argv[i]);
      break;
    }
  }

  if (My->IsInteractive)
  {
    /* 
     **
     ** set a buffer for jump: program execution returns to this point in
     ** case of a jump (error, interrupt, or finish program) 
     **
     */
    My->program_run = 0;
    signal (SIGINT, break_mes);
    setjmp (My->mark);
    if (My->program_run > 0)
    {
      /* error in console mode */
    }
    My->program_run++;
  }

  /* main program loop */
  My->NextValidLineNumber = MINLIN;
  while (!feof (My->SYSIN->cfp))        /* condition !feof( My->SYSIN->cfp ) added in v1.11 */
  {
    bwb_mainloop ();
  }

  bwx_terminate ();                /* allow ^D (Unix) exit with grace */

  return 0;
}



/***************************************************************
  
   FUNCTION:       bwb_interact()
  
   DESCRIPTION:   This function gets a line from the user
         and processes it.
  
***************************************************************/

static void
bwb_interact (void)
{
  char *tbuf;
  int tlen;
  assert( My != NULL );
   

  tbuf = My->ConsoleInput;
  tlen = MAX_LINE_LENGTH;
  My->NextValidLineNumber = MINLIN;
  /* take input from keyboard */
  if (My->AutomaticLineNumber > 0 && My->AutomaticLineIncrement > 0)
  {
    /* AUTO 100, 10 */
    char *zbuf;                        /* end of the prompt, start of the response */
    int zlen;                        /* length of the prompt */
    char LineExists;
    LineType *l;

    LineExists = ' ';
    for (l = My->StartMarker->next; l != My->EndMarker; l = l->next)
    {
      if (l->number == My->AutomaticLineNumber)
      {
        /* FOUND */
        LineExists = '*';
        break;
      }
      else if (l->number > My->AutomaticLineNumber)
      {
        /* NOT FOUND */
        LineExists = ' ';
        break;
      }
    }
    sprintf (tbuf, "%d%c", My->AutomaticLineNumber, LineExists);
    zbuf = bwb_strchr (tbuf, NulChar);
    zlen = bwb_strlen (tbuf);
    bwx_input (tbuf, FALSE, zbuf, tlen - zlen);
    zbuf[-1] = ' ';                /* remove LineExists indicator */
    CleanLine (zbuf);                /* JBV */
    if (is_empty_string (zbuf))
    {
      /* empty response */
      if (LineExists == '*')
      {
        /*
           An empty response with an existing line,
           causes AUTO to continue with the next line,
           leaving the current line intact.
         */
        My->AutomaticLineNumber += My->AutomaticLineIncrement;
      }
      else
      {
        /* 
           An empty response with a non-existing line,
           causes AUTO to terminate.
         */
        My->AutomaticLineNumber = 0;
        My->AutomaticLineIncrement = 0;
      }
    }
    else
    {
      /* non-empty response */
      if (bwb_stricmp (zbuf, "MAN") == 0)
      {
        /* MAN terminates AUTO mode */
        My->AutomaticLineNumber = 0;
        My->AutomaticLineIncrement = 0;
      }
      else
      {
        /* overwrite any existing line */
        bwb_ladd (tbuf, My->StartMarker, FALSE);
        My->AutomaticLineNumber += My->AutomaticLineIncrement;
      }
    }
  }
  else
  {
    bwx_input (My->OptionPromptString, FALSE, tbuf, tlen);
    process_basic_line (tbuf);
  }
}



/***************************************************************
  
      FUNCTION:       bwb_fload()
  
      DESCRIPTION:   This function loads a BASIC program
         file into memory given a FILE pointer.
  
***************************************************************/

static int
FixQuotes (char *tbuf)
{
  /* fix unbalanced quotes */
  /* 'tbuf' shall be declared "char tbuf[ tlen + 1]". */
  int p;
  int QuoteCount;
  int tlen;
  assert( My != NULL );
  assert( My->CurrentVersion != NULL );
   
  assert (tbuf != NULL);

  QuoteCount = 0;
  tlen = MAX_LINE_LENGTH;
  tbuf[tlen] = NulChar;
  p = 0;
  while (tbuf[p])
  {
    if (tbuf[p] == My->CurrentVersion->OptionQuoteChar)
    {
      QuoteCount++;
    }
    p++;
  }
  if (QuoteCount & 1)
  {
    /* odd == missing trailing quote */
    if (p < tlen)
    {
      /* we have room to put the missig quote */
      tbuf[p] = My->CurrentVersion->OptionQuoteChar;
      p++;
      tbuf[p] = NulChar;
    }
    else
    {
      /* we cannot fix it */
      return FALSE;
    }
  }
  /* OK */
  return TRUE;
}

static FILE *
nice_open (char *BaseName)
{
  FILE *file;
  assert( My != NULL );

  if (BaseName == NULL)
  {
    BaseName = My->ProgramFilename;
  }
  if (is_empty_string (BaseName))
  {
    WARN_BAD_FILE_NAME;
    return NULL;
  }

  file = fopen (BaseName, "r");
  if (file == NULL)
  if (is_empty_string (My->OptionExtensionString) == FALSE)
  {
    char *FileName;

    FileName = bwb_strdup2 (BaseName, My->OptionExtensionString);
    if (FileName == NULL)
    {
      WARN_OUT_OF_MEMORY;
      return NULL;
    }
    file = fopen (FileName, "r");
    if (file == NULL)
    {
      free (FileName);
    }
    else if (BaseName == My->ProgramFilename)
    {
      if (My->ProgramFilename != NULL)
      {
        free (My->ProgramFilename);
      }
      My->ProgramFilename = FileName;
    }
  }
  return file;
}

extern int
bwb_fload (char *FileName)
{
  /*
   **
   ** Load a BASIC program from the specified 'FileName'.
   ** If 'FileName' is NULL, then load from My->ProgramFilename,
   **
   */
  char *Magic_Word;                /* SYNTAX: %INCLUDE literal.file.name */
  int Magic_Length;
  FILE *file;
  char *tbuf;
  int tlen;
   



  Magic_Word = "%INCLUDE ";        /* SYNTAX: %INCLUDE literal.file.name */
  Magic_Length = bwb_strlen (Magic_Word);
  tbuf = My->MaxLenBuffer;
  tlen = MAXLEN;


  /*
     Just in case you are wondering...
     Although this handles the most common cases, it does not handle all possible cases.
     The correct solution is to provide the actual filename (with extension),
     as it exists in the operating system.
   */
  file = nice_open (FileName);
  if (file == NULL)
  {
    WARN_BAD_FILE_NAME;
    return FALSE;
  }
  My->NextValidLineNumber = MINLIN;
  while (fgets (tbuf, tlen, file))        /* bwb_fload */
  {
    tbuf[tlen] = NulChar;
    CleanLine (tbuf);
    if (is_empty_string (tbuf))
    {
      /* ignore */
    }
    else if (bwb_strnicmp (tbuf, Magic_Word, Magic_Length) == 0)
    {
      /* %iNCLUDE */
      int Result;
      int p;
      char varname[NameLengthMax + 1];

      p = Magic_Length;
      if (buff_read_varname (tbuf, &p, varname) == FALSE)
      {
        fprintf (My->SYSOUT->cfp, "%s Filename\n", Magic_Word);
        ResetConsoleColumn ();
        return FALSE;
      }
      My->IncludeLevel++;        /* %INCLUDE */
      Result = bwb_fload (varname);
      My->IncludeLevel--;        /* %INCLUDE */
      if (Result == FALSE)
      {
        fprintf (My->SYSOUT->cfp, "%s %s Failed\n", Magic_Word, varname);
        ResetConsoleColumn ();
        return FALSE;
      }
    }
    else
    {
      /* normal */
      bwb_ladd (tbuf, My->StartMarker, FALSE);
    }
  }

  /* close file stream */

  bwb_fclose (file);                /* file != NULL */

  My->NextValidLineNumber = MINLIN;

  return TRUE;
}


static char *
FindClassicStatementEnd (char *C)
{
  /* 
   ** find the end of the current statement
   */
  assert( My != NULL );
  assert( My->CurrentVersion != NULL );
   
  assert (C != NULL);


  if (My->CurrentVersion->OptionStatementChar == NulChar
      && My->CurrentVersion->OptionCommentChar == NulChar)
  {
    /* DO NOTHING: Multi-statment lines are not possible */
    return NULL;
  }
  /* skip line number */
  while (bwb_isdigit (*C))
  {
    C++;
  }
  /* skip spaces */
  while (*C == ' ')
  {
    C++;
  }
  if (IS_CHAR (*C, My->CurrentVersion->OptionCommentChar))
  {
    /* The entire line is a comment */
    return NULL;
  }
  if (bwb_strnicmp (C, "REM", 3) == 0)
  {
    /* The entire line is a comment */
    return NULL;
  }
  if ((My->CurrentVersion->OptionFlags & OPTION_LABELS_ON)
      && (My->CurrentVersion->OptionStatementChar != NulChar))
  {
    /* determine if this line is a LABEL */
    int p;
    char label[NameLengthMax + 1];

    p = 0;
    if (buff_read_label (C, &p, label))
    {
      if (buff_skip_char (C, &p, My->CurrentVersion->OptionStatementChar))
      {
        if (buff_is_eol (C, &p))
        {
          /* The entire line is a label */
          /* LABEL : \0 */
          return NULL;
        }
      }
    }
  }
  /* not a special case, so split on the first unquoted OptionCommentChar or OptionStatementChar */
  while (*C != NulChar)
  {
    if (*C == My->CurrentVersion->OptionQuoteChar)
    {
      /* skip leading quote */
      C++;
      while (*C != NulChar && *C != My->CurrentVersion->OptionQuoteChar)
      {
        /* skip string constant */
        C++;
      }
      if (*C == My->CurrentVersion->OptionQuoteChar)
      {
        /* skip trailing quote */
        C++;
      }
    }
    else if (IS_CHAR (*C, My->CurrentVersion->OptionCommentChar) /* ', ! */ )
    {
      /* FOUND */
      return C;
    }
    else
      if (IS_CHAR (*C, My->CurrentVersion->OptionStatementChar) /* :, \ */ )
    {
      /* FOUND */
      return C;
    }
    else
    {
      C++;
    }
  }
  /* NOT FOUND */
  return NULL;
}


static void
ImportClassicIfThenElse (char *InBuffer)
{
/*
**
** Determine the type of IF command:
**
** a) STANDARD: 
**    IF x THEN line ELSE line
**
** b) CLASSIC:
**    IF x THEN stmt(s) ELSE stmt(s)
**
** c) STRUCTURED:
**    IF x THEN
**       stmts
**    ELSE
**      stmts
**    END IF
**
** The STANDARD and STRUCTURED forms
** are natively supported.
**
** The CLASSIC form is converted to
** the STRUCTURED form.
**
*/

  int i;

  int nIF = 0;
  int nTHEN = 0;
  int nELSE = 0;
  int nENDIF = 0;

#define NO_COMMAND    0
#define IF_COMMAND    1
#define THEN_COMMAND  2
#define ELSE_COMMAND  3
#define ENDIF_COMMAND 4
  int LastCommand = NO_COMMAND;

  const char *REM = "REM ";
  const char *IF = "IF ";
  const char *THEN = "THEN ";
  const char *THEN2 = "THEN";
  const char *ELSE = "ELSE ";
  const char *ENDIF = "END IF";
  const char *GOTO = "GOTO ";
  const char *DATA = "DATA ";
  const char *CASE = "CASE ";
  char *OutBuffer = My->ConsoleOutput;
  char *Input;
  char *Output;
  char LastChar = My->CurrentVersion->OptionStatementChar;

  int REM_len = bwb_strlen (REM);
  int IF_len = bwb_strlen (IF);
  int THEN_len = bwb_strlen (THEN);
  int THEN2_len = bwb_strlen (THEN2);
  int ELSE_len = bwb_strlen (ELSE);
  int ENDIF_len = bwb_strlen (ENDIF);
  int GOTO_len = bwb_strlen (GOTO);
  int DATA_len = bwb_strlen (DATA);
  int CASE_len = bwb_strlen (CASE);

#define OUTPUTCHAR( c ) { *Output = c; Output++; }
#define COPYCHAR  { LastChar = *Input; *Output = *Input; Output++; Input++; }
#define COPY_LINENUMBER  while( bwb_isdigit( *Input )       ) COPYCHAR;
#define COPY_SPACES      while( *Input == ' '     ) COPYCHAR;
#define COPY_IF          for( i = 0; i < IF_len; i++    ) COPYCHAR;
#define COPY_THEN        for( i = 0; i < THEN_len; i++  ) COPYCHAR;
#define COPY_THEN2       for( i = 0; i < THEN2_len; i++ ) COPYCHAR;
#define COPY_ELSE        for( i = 0; i < ELSE_len; i++  ) COPYCHAR;
#define COPY_ENDIF       for( i = 0; i < ENDIF_len; i++ ) COPYCHAR;
#define FORCE_ENDIF      for( i = 0; i < ENDIF_len; i++ ) OUTPUTCHAR( ENDIF[ i ] );
#define FORCE_GOTO       for( i = 0; i < GOTO_len; i++  ) OUTPUTCHAR( GOTO[ i ] );
#define FORCE_COLON if( LastChar != My->CurrentVersion->OptionStatementChar ) OUTPUTCHAR( My->CurrentVersion->OptionStatementChar );

  assert( My != NULL );
  assert( My->CurrentVersion != NULL );
  assert (InBuffer != NULL);


  Input = InBuffer;
  Output = OutBuffer;





  if (My->CurrentVersion->OptionStatementChar == NulChar)
  {
    /* DO NOTHING: All IFs must be STANDARD or STRUCTURED */
    return;
  }


  COPY_LINENUMBER;
  COPY_SPACES;
  LastChar = My->CurrentVersion->OptionStatementChar;


  while (*Input != NulChar)
  {
    if (*Input == My->CurrentVersion->OptionCommentChar)
    {
      /* comment */
      break;
    }
    else if (*Input == My->CurrentVersion->OptionQuoteChar)
    {
      /* string constant */
      COPYCHAR;
      while (*Input != NulChar
             && *Input != My->CurrentVersion->OptionQuoteChar)
      {
        COPYCHAR;
      }
      if (*Input == My->CurrentVersion->OptionQuoteChar)
      {
        COPYCHAR;
      }
      else
      {
        /* add missing Quote */
        OUTPUTCHAR (My->CurrentVersion->OptionQuoteChar);
      }
      COPY_SPACES;
    }
    else if (bwb_isalnum (LastChar))
    {
      /* can NOT be the start of a command */
      COPYCHAR;
    }
    else if (!bwb_isalpha (*Input))
    {
      /* can NOT be the start of a command */
      COPYCHAR;
    }
    else if (bwb_strnicmp (Input, REM, REM_len) == 0)
    {
      break;
    }
    else if (bwb_strnicmp (Input, DATA, DATA_len) == 0)
    {
      /* DATA ... */
      break;
    }
    else if (bwb_strnicmp (Input, CASE, CASE_len) == 0)
    {
      /* CASE ... */
      break;
    }
    else if (bwb_strnicmp (Input, IF, IF_len) == 0)
    {
      /* IF */
      LastCommand = IF_COMMAND;
      nIF++;
      COPY_IF;
      COPY_SPACES;
    }
    else if (bwb_strnicmp (Input, GOTO, GOTO_len) == 0 && nIF > nTHEN)
    {
      /* IF x GOTO line ELSE line */
      LastCommand = THEN_COMMAND;
      nTHEN++;
      COPY_THEN;
      COPY_SPACES;
      COPY_LINENUMBER;
      COPY_SPACES;
      if (bwb_strnicmp (Input, ELSE, ELSE_len) == 0)
      {
        /* ELSE line */
        COPY_ELSE;
        COPY_SPACES;
        COPY_LINENUMBER;
        COPY_SPACES;
      }
      /* IS STANDARD, NOT CLASSIC */
      nENDIF++;
      LastCommand = ENDIF_COMMAND;
    }
    else if (bwb_strnicmp (Input, THEN, THEN_len) == 0)
    {
      /* THEN */
      LastCommand = THEN_COMMAND;
      nTHEN++;
      COPY_THEN;
      COPY_SPACES;
      if (bwb_isdigit (*Input))
      {
        /* STANDARD: IF x THEN line ELSE line */
        char *SavedInput;
        char *SavedOutput;
        SavedInput = Input;
        SavedOutput = Output;

        COPY_LINENUMBER;
        COPY_SPACES;
        if (bwb_strnicmp (Input, ELSE, ELSE_len) == 0)
        {
          /* ELSE line */
          COPY_ELSE;
          COPY_SPACES;
          if (bwb_isdigit (*Input))
          {
            COPY_LINENUMBER;
            COPY_SPACES;
            /* IS STANDARD, NOT CLASSIC */
            nENDIF++;
            LastCommand = ENDIF_COMMAND;
          }
          else
          {
            /* IF x THEN line ELSE stmts */
            Input = SavedInput;
            Output = SavedOutput;
            FORCE_COLON;
            FORCE_GOTO;
            COPY_LINENUMBER;
            COPY_SPACES;
          }
        }
        else
        {
          /* IS STANDARD, NOT CLASSIC */
          nENDIF++;
          LastCommand = ENDIF_COMMAND;
        }
      }
      else
        if (*Input == My->CurrentVersion->OptionCommentChar
            || *Input == NulChar)
      {
        /* IS STRUCTURED, NOT CLASSIC */
        nENDIF++;
        LastCommand = ENDIF_COMMAND;
      }
      else
      {
        /* CLASSIC: IF x THEN stmts ELSE stmts */
      }
      FORCE_COLON;
    }
    else if (bwb_strnicmp (Input, THEN2, THEN2_len) == 0)
    {
      /* trailing THEN ? */
      char *PeekInput;

      PeekInput = Input;
      PeekInput += THEN2_len;
      while (*PeekInput == ' ')
      {
        PeekInput++;
      }
      if (*PeekInput == My->CurrentVersion->OptionCommentChar
          || *PeekInput == NulChar)
      {
        /* IS STRUCTURED, NOT CLASSIC */
        nTHEN++;
        COPY_THEN2;
        nENDIF++;
        LastCommand = ENDIF_COMMAND;
        FORCE_COLON;
      }
      else
      {
        /* THEN line */
      }
    }
    else if (bwb_strnicmp (Input, ELSE, ELSE_len) == 0)
    {
      /* ELSE */
      if (LastCommand == ELSE_COMMAND)
      {
        /* we need an ENDIF here */
        FORCE_COLON;
        FORCE_ENDIF;
        FORCE_COLON;
        nENDIF++;
      }
      LastCommand = ELSE_COMMAND;
      nELSE++;
      FORCE_COLON;
      COPY_ELSE;
      FORCE_COLON;
      COPY_SPACES;
      if (bwb_isdigit (*Input))
      {
        /* IF x THEN stmts ELSE line */
        FORCE_GOTO;
        COPY_LINENUMBER;
        COPY_SPACES;
      }
      FORCE_COLON;
    }
    else if (bwb_strnicmp (Input, ENDIF, ENDIF_len) == 0)
    {
      /* ENDIF */
      LastCommand = ENDIF_COMMAND;
      nENDIF++;
      COPY_ENDIF;
      FORCE_COLON;
    }
    else
    {
      /* was NOT the start of a command */
      COPYCHAR;
    }
  }
  /* end of input */
  if (nENDIF < nIF)
  {
    while (nENDIF < nIF)
    {
      /* we need trailing ENDIF's */
      nENDIF++;
      FORCE_COLON;
      FORCE_ENDIF;
    }
  }
  /* fixup trailing REMark command */
  if (bwb_strnicmp (Input, REM, REM_len) == 0)
  {
    /* REMark */
    /* 100 A=1 REMark */
    /* 100 A=1:REMark */
    /* 100 A=1'REMark */
    FORCE_COLON;
  }
  /* copy the comments */
  while (*Input != NulChar)
  {
    COPYCHAR;
    /* cppcheck: (style) Variable 'LastChar' is assigned a value that is never used. */
  }
  OUTPUTCHAR (NulChar);
  bwb_strcpy (InBuffer, OutBuffer);
}


/***************************************************************
  
        FUNCTION:       bwb_ladd()
  
        DESCRIPTION:    This function adds a new line (in the
                        buffer) to the program in memory.
  
***************************************************************/
static int
bwb_ladd (char *buffer, LineType * p, int IsUser)
{
  LineType *l;
  LineType *previous;
  char *newbuffer;
  char *NextStatement;
  char *ThisStatement;
  int Replace;
  char BreakChar;

  assert( My != NULL );
  assert( My->SYSOUT != NULL );
  assert( My->SYSOUT->cfp != NULL );
  assert( My->CurrentVersion != NULL );
  assert (buffer != NULL);
  assert (p != NULL);


  Replace = TRUE;
  BreakChar = NulChar;
  CleanLine (buffer);
  if (is_empty_string (buffer))
  {
    /* silengtly ignore blank lines */
    return FALSE;
  }
  /*
     from here, the line WILL be added so the user can EDIT it,  
     we just complain and refuse to run if any error is detected.
   */
  My->IsScanRequired = TRUE;        /* program needs to be scanned again */

  /* AUTO-FIX UNBALANCED QUOTES */
  if (FixQuotes (buffer) == FALSE)
  {
    /* ERROR */
    fprintf (My->SYSOUT->cfp, "UNBALANCED QUOTES: %s\n", buffer);
    ResetConsoleColumn ();
    My->ERR = -1;                /* bwb_ladd, UNBALANCED QUOTES */
  }

  if (IS_CHAR (*buffer, My->CurrentVersion->OptionStatementChar))
  {
    /* part of a multi-statement line */
  }
  else if (IS_CHAR (*buffer, My->CurrentVersion->OptionCommentChar))
  {
    /* part of a multi-statement line */
  }
  else
  {
    ImportClassicIfThenElse (buffer);
  }
  ThisStatement = buffer;
  NextStatement = NULL;
  BreakChar = NulChar;

  do
  {
    if (BreakChar == NulChar)
    {
      /* first pass thru the do{} while loop, do nothing */
    }
    else if (IS_CHAR (BreakChar, My->CurrentVersion->OptionCommentChar))
    {
      /* ThisStatment will turn out to be the last */
      *ThisStatement = My->CurrentVersion->OptionCommentChar;
    }
    else if (IS_CHAR (BreakChar, My->CurrentVersion->OptionStatementChar))
    {
      /* we are NOT the last statement, skip over the OptionStatementChar */
      ThisStatement++;
    }
    else
    {
      /* Internal Error */
    }

    if (BreakChar == NulChar
        && IS_CHAR (*buffer, My->CurrentVersion->OptionStatementChar))
    {
      /* first pass thru and line begins with colon */
      /* part of a multi-statement line */
      NextStatement = NULL;
      if (My->NextValidLineNumber > 1)
      {
        My->NextValidLineNumber--;
      }
      Replace = FALSE;
    }
    else
      if (BreakChar == NulChar
          && IS_CHAR (*buffer, My->CurrentVersion->OptionCommentChar))
    {
      /* first pass thru and line begins with apostrophe */
      /* part of a multi-statement line */
      NextStatement = NULL;
      if (My->NextValidLineNumber > 1)
      {
        My->NextValidLineNumber--;
      }
      Replace = FALSE;
    }
    else
    {
      NextStatement = FindClassicStatementEnd (ThisStatement);
    }

    if (NextStatement == NULL)
    {
      /* we are the last statement */
    }
    else
    {
      /* another statement follows */
      BreakChar = *NextStatement;
      *NextStatement = NulChar;
    }
    CleanLine (ThisStatement);
    if (is_empty_string (ThisStatement) == FALSE)
    {

      /* get memory for this line */
      if ((l = (LineType *) calloc (1, sizeof (LineType))) == NULL)
      {
        /* ERROR */
        fprintf (My->SYSOUT->cfp, "Out of memory\n");
        ResetConsoleColumn ();
        My->ERR = -1;                /* bwb_ladd, Out of memory */
        return FALSE;
      }

      /* this line has not been executed or numbered */
      l->LineFlags = 0;
      if (IsUser)
      {
        l->LineFlags |= LINE_USER;
      }
      l->IncludeLevel = My->IncludeLevel;        /* %INCLUDE */
      l->position = 0;

      /*
       **
       ** ALL lines have a line number.  
       ** If a line number is not provided, 
       ** then the next available line number is assigned.
       **
       */
      newbuffer = ThisStatement;
      l->number = My->NextValidLineNumber;

      if (buff_read_line_number (newbuffer, &(l->position), &l->number))
      {
        if (l->number < My->NextValidLineNumber)
        {
          /* ERROR */
          fprintf (My->SYSOUT->cfp, "%d < %d - LINE OUT OF ORDER: %s\n",
                   l->number, My->NextValidLineNumber, buffer);
          ResetConsoleColumn ();
          My->ERR = -1;                /* bwb_ladd, LINE OUT OF ORDER */
          l->number = My->NextValidLineNumber;        /* sane default */
        }
        else if (l->number < MINLIN || l->number > MAXLIN)
        {
          /* ERROR */
          fprintf (My->SYSOUT->cfp, "INVALID LINE NUMBER: %s\n", buffer);
          ResetConsoleColumn ();
          My->ERR = -1;                /* bwb_ladd, INVALID LINE NUMBER */
          l->number = My->NextValidLineNumber;        /* sane default */
        }
        else
        {
          /* OK */
          My->NextValidLineNumber = l->number;
          l->LineFlags |= LINE_NUMBERED;        /* line was manually numbered */
        }
        /* A SPACE IS REQUIRED AFTER THE LINE NUMBER -- NO EXCEPTIONS */
        if (newbuffer[l->position] != ' ')
        {
          /* ERROR */
          fprintf (My->SYSOUT->cfp, "MISSING SPACE AFTER LINE NUMBER: %s\n",
                   buffer);
          ResetConsoleColumn ();
          My->ERR = -1;                /* bwb_ladd, MISSING SPACE AFTER LINE NUMBER */
        }
        /* newuffer does NOT contain the line number */
        newbuffer += l->position;
      }
      /* the next valid line number is this line number plus one */
      CleanLine (newbuffer);
      if (*newbuffer != NulChar
          && *newbuffer == My->CurrentVersion->OptionStatementChar)
      {
        /* this is part of a multi-statement line */
        newbuffer++;
        CleanLine (newbuffer);
      }
      /* 
       **
       ** copy into the line buffer 
       **
       */
      if (l->buffer != NULL)
      {
        free (l->buffer);
        l->buffer = NULL;        /* JBV */
      }
      if ((l->buffer =
           (char *) calloc (bwb_strlen (newbuffer) + 1 /* NulChar */ ,
                            sizeof (char))) == NULL)
      {
        /* ERROR */
        fprintf (My->SYSOUT->cfp, "Out of memory\n");
        ResetConsoleColumn ();
        My->ERR = -1;                /* bwb_ladd, Out of memory */
        return FALSE;                /* we cannot determine the command */
      }
      bwb_strcpy (l->buffer, newbuffer);
      /*
       **
       ** determine command
       **
       */
      line_start (l);
      if (l->cmdnum)
      {
        /* OK */
      }
      else
      {
        /* ERROR */
        fprintf (My->SYSOUT->cfp,
                 "ILLEGAL COMMAND AFTER LINE NUMBER: %d %s\n", l->number,
                 l->buffer);
        ResetConsoleColumn ();
        My->ERR = -1;                /* bwb_ladd, ILLEGAL COMMAND AFTER LINE NUMBER */
      }
      /*
       **
       ** add the line to the linked-list of lines
       **
       */
      for (previous = p; previous != My->EndMarker; previous = previous->next)
      {
        if (previous->number == l->number)
        {
          if (Replace == TRUE)
          {
            /* REPLACE 'previous' */
            while (previous->number == l->number)
            {
              LineType *z;

              z = previous;
              previous = previous->next;
              bwb_freeline (z);
            }
            l->next = previous;
            p->next = l;

          }
          else
          {
            /* APPEND after 'previous' */
            while (previous->next->number == l->number)
            {
              previous = previous->next;
            }
            l->next = previous->next;
            previous->next = l;
          }
          break;
        }
        else
          if (previous->number < l->number
              && l->number < previous->next->number)
        {
          /* INSERT BETWEEN 'previous' AND 'previous->next' */
          l->next = previous->next;
          previous->next = l;
          break;
        }
        p = previous;
      }

    }
    /* another statement may follow */
    ThisStatement = NextStatement;
    Replace = FALSE;
  }
  while (ThisStatement != NULL);
  My->NextValidLineNumber++;
  return TRUE;
}

/***************************************************************
  
        FUNCTION:       bwb_xtxtline()
  
        DESCRIPTION:    This function executes a text line, i.e.,
                        places it in memory and then relinquishes
                        control.
  
***************************************************************/

static void
bwb_xtxtline (char *buffer)
{
  assert( My != NULL );
  assert (buffer != NULL);

  /* remove old program from memory */
  bwb_xnew (My->UserMarker);

  CleanLine (buffer);
  if (is_empty_string (buffer))
  {
    /* silengtly ignore blank lines */
    return;
  }

  /* add to the user line list */
  bwb_ladd (buffer, My->UserMarker, TRUE);

  /* execute the line as BASIC command line */
  if (bwb_incexec ())
  {
    My->StackHead->line = My->UserMarker->next;        /* and set current line in it */
    My->StackHead->ExecCode = EXEC_NORM;
  }
}

/***************************************************************
  
        FUNCTION:       bwb_incexec()
  
        DESCRIPTION:    This function increments the EXEC
         stack counter.
  
***************************************************************/

int
bwb_incexec (void)
{
  StackType *StackItem;
  assert( My != NULL );
   

  if (My->StackDepthInteger >= EXECLEVELS)
  {
    WARN_OUT_OF_MEMORY;
    return FALSE;
  }
  if ((StackItem = (StackType *) calloc (1, sizeof (StackType))) == NULL)
  {
    WARN_OUT_OF_MEMORY;
    return FALSE;
  }
  StackItem->ExecCode = EXEC_NORM;        /* sane default */
  StackItem->line = My->ThisLine;        /* bwb_incexec */
  StackItem->LoopTopLine = NULL;
  StackItem->local_variable = NULL;
  StackItem->OnErrorGoto = 0;
  StackItem->next = My->StackHead;
  My->StackHead = StackItem;
  My->StackDepthInteger++;
  return TRUE;
}

/***************************************************************
  
        FUNCTION:       bwb_decexec()
  
        DESCRIPTION:    This function decrements the EXEC
         stack counter.
  
***************************************************************/
void
bwb_clrexec (void)
{
  assert( My != NULL );
   
  while (My->StackHead != NULL)
  {
    bwb_decexec ();
  }
}

void
bwb_decexec (void)
{
  StackType *StackItem;
  assert( My != NULL );
   

  if (My->StackHead == NULL)
  {
    WARN_RETURN_WITHOUT_GOSUB;
    return;
  }
  StackItem = My->StackHead;
  My->StackHead = StackItem->next;
  free (StackItem);
  My->StackDepthInteger--;
}

/***************************************************************
  
        FUNCTION:       bwb_mainloop()
  
        DESCRIPTION:    This C function performs one iteration
                        of the interpreter. In a non-preemptive
                        scheduler, this function should be called
                        by the scheduler, not by bwBASIC code.
  
***************************************************************/

void
bwb_mainloop (void)
{
  assert( My != NULL );
   

  if (My->StackHead)
  {
    /* BASIC program running */
    bwb_execline ();                /* execute one line of program */
    return;
  }
  /* BASIC program completed */

  if (My->ExternalInputFile != NULL)
  {
    /* for automated testing, --TAPE command line parameter */
    if (bwb_is_eof (My->ExternalInputFile) == FALSE)
    {
      /* --TAPE command line parameter is active */
      bwb_interact ();                /* get user interaction */
      return;
    }
  }
  /* TAPE command inactive or completed */

  if (My->IsCommandLineFile)
  {
    /* BASIC program was started from command line */
    bwx_terminate ();
    return;
  }
  /* BASIC program was not started from command line */

  if (My->IsInteractive)
  {
    /* interactive */
    bwb_interact ();                /* get user interaction */
    return;
  }
  /* non-interactive */

  bwx_terminate ();
}

/***************************************************************
  
        FUNCTION:       bwb_execline()
  
        DESCRIPTION:    This function executes a single line of
                        a program in memory. This function is
         called by bwb_mainloop().
  
***************************************************************/

extern int
bwx_Error (int ERR, char *ErrorMessage)
{
  /*
     ERR is the error number
     ErrorMessage is used to override the default error message, and is usually NULL
   */
  assert( My != NULL );
   
  switch (ERR)
  {
  case 0:
    /* 
     **
     ** Clear any existing error 
     **
     */
    My->IsErrorPending = FALSE;        /* bwx_Error, ERR == 0 */
    My->ERR = 0;                /* bwx_Error, ERR == 0 */
    My->ERL = NULL;                /* bwx_Error, ERR == 0 */
    bwb_strcpy (My->ERROR4, "");        /* bwx_Error, ERR == 0 */
    return FALSE;
  case 6:                        /* WARN_OVERFLOW */
  case 11:                        /* WARN_DIVISION_BY_ZERO */
  case 15:                        /* WARN_STRING_TOO_LONG */
    /* 
     **
     ** Behavior depends upon whether an Error handler is active.
     **
     */
    if (GetOnError () == 0)
    {
      /*
       **
       ** Error handler is NOT active.  
       ** Do NOT set ERL, ERR, and ERROR$.
       ** Continue processing.
       **
       */
      if (ErrorMessage == NULL)
      {
        /* use the default error message */
        if (ERR > 0 && ERR < NUM_WARNINGS)
        {
          ErrorMessage = ERROR4[ERR];
        }
      }
      if (ErrorMessage != NULL)
      {
        if (bwb_strlen (ErrorMessage) > 0)
        {
          fprintf (My->SYSOUT->cfp, "%s\n", ErrorMessage);
          ResetConsoleColumn ();
        }
      }
      return FALSE;                /* continue processing */
    }
    /*
     **
     ** Error handler IS active.  
     ** Fall-thru to set ERL, ERR, and ERROR$. 
     ** Abort processing.
     **
     */
  }
  if (My->IsErrorPending == FALSE)        /* no errors pending */
  {
    /* 
     **
     ** only keep the first pending error to occur 
     **
     */
    My->IsErrorPending = TRUE;        /* bwx_Error, ERR != 0 */
    My->ERR = ERR;                /* bwx_Error, ERR != 0 */
    My->ERL = NULL;                /* bwx_Error, ERR != 0 */
    bwb_strcpy (My->ERROR4, "");        /* bwx_Error, ERR != 0 */
    if (My->StackHead)
    {
      My->ERL = My->StackHead->line;
    }
    if (ErrorMessage == NULL)
    {
      /* use the default error message */
      if (ERR > 0 && ERR < NUM_WARNINGS)
      {
        ErrorMessage = ERROR4[ERR];
      }
    }
    if (ErrorMessage != NULL)
    {
      if (bwb_strlen (ErrorMessage) > MAX_ERR_LENGTH)
      {
        ErrorMessage[MAX_ERR_LENGTH] = NulChar;
      }
      bwb_strcpy (My->ERROR4, ErrorMessage);
    }
  }
  return TRUE;                        /* abort processing */
}

void
bwb_execline (void)
{
  LineType *r, *l;
  assert( My != NULL );
  assert( My->SYSOUT != NULL );
  assert( My->SYSOUT->cfp != NULL );
  assert( My->CurrentVersion != NULL );

  if (My->StackHead == NULL)        /* in bwb_execline(), FATAL ERROR PENDING */
  {
    return;
  }

  l = My->StackHead->line;

  /* if the line is My->EndMarker, then break out of EXEC loops */
  if (l == NULL || l == My->EndMarker || My->ERR < 0)        /* in bwb_execline(), FATAL ERROR PENDING */
  {
    bwb_clrexec ();
    return;
  }

  My->ThisLine = l;                /* bwb_execline */

  /* Print line number if trace is on */
  if (My->IsTraceOn == TRUE)
  {
    if (l->LineFlags & (LINE_USER))
    {
      /* USER line in console */
    }
    else if (l->number > 0)
    {
      fprintf (My->SYSOUT->cfp, "[ %d %s ]", l->number, l->buffer);
    }
  }
  l->position = l->Startpos;

  /* if there is a BASIC command in the line, execute it here */
  if (l->cmdnum)
  {
    /* OK */
  }
  else
  {
    WARN_ILLEGAL_DIRECT;
    l->cmdnum = C_REM;
  }
  /* l->cmdnum != 0 */

  if (l->LineFlags & LINE_BREAK)
  {
    /* BREAK line */
    l->LineFlags &= ~LINE_BREAK;
    My->ContinueLine = l;
    bwx_STOP (TRUE);
    return;
  }

  /* advance beyond whitespace */
  line_skip_spaces (l);                /* keep this */

  /* execute the command vector */
  if (My->CurrentVersion->OptionFlags & (OPTION_COVERAGE_ON))
  {
    /* We do this here so "END" and "STOP" are marked */
    if (l->cmdnum == C_DATA)
    {
      /* DATA lines are marked when they are READ */
    }
    else
    {
      /* this line was executed */
      l->LineFlags |= LINE_EXECUTED;
    }
  }
  r = bwb_vector (l);
  if (r == NULL)
  {
    WARN_INTERNAL_ERROR;
    return;
  }
  assert (r != NULL);

  if (My->ERR < 0)                /* in bwb_execline(), FATAL ERROR PENDING */
  {
    /* FATAL */
    bwb_clrexec ();
    return;
  }

  if (My->IsErrorPending /* Keep This */ )
  {
    /* we are probably not at the end-of-the-line */
  }
  else if (r == l)
  {
    /* we should be at the end-of-the-line */
    if (line_is_eol (l))
    {
      /* OK */
    }
    else
    {
      WARN_SYNTAX_ERROR;
      return;
    }
  }
  else
  {
    /* we are probably not at the end-of-the-line */
  }

  if (My->IsErrorPending /* Keep This */ )
  {
    /* 
     **
     ** a NON-FATAL ERROR has occurred. ERR, ERL, and ERROR$ were
     ** already set using bwb_warning(ERR,ERROR$) 
     **
     */
    int err_gotol;
    My->IsErrorPending = FALSE;        /* Error Recognized */
    err_gotol = GetOnError ();
    if (l->LineFlags & (LINE_USER))
    {
      /*
       **
       ** -------------------------------------------------------------------------
       ** USER line in console
       ** -------------------------------------------------------------------------
       **
       ** fall thru to the DEFAULT ERROR HANDLER 
       **
       */
    }
    else if (l->number == 0)
    {
      /* fall thru to the DEFAULT ERROR HANDLER */
    }
    else if (My->ERL == NULL)
    {
      /* fall thru to the DEFAULT ERROR HANDLER */
    }
    else if (My->ERL->number == 0)
    {
      /* fall thru to the DEFAULT ERROR HANDLER */
    }
    else if (err_gotol == -1)
    {
      /*
       **
       ** -------------------------------------------------------------------------
       ** ON ERROR RESUME NEXT
       ** -------------------------------------------------------------------------
       **
       */
      assert (r != NULL);
      assert (r->next != NULL);

      r->next->position = 0;
      assert (My->StackHead != NULL);
      My->StackHead->line = r->next;
      return;
    }
    else if (err_gotol == 0)
    {
      /*
       **
       ** -------------------------------------------------------------------------
       ** ON ERROR GOTO 0
       ** -------------------------------------------------------------------------
       **
       ** fall thru to the DEFAULT ERROR HANDLER
       **
       */
    }
    else if (err_gotol == My->ERL->number)
    {
      /*
       **
       ** -------------------------------------------------------------------------
       ** RECURSION
       ** -------------------------------------------------------------------------
       **
       ** For example: 
       ** 10 ON ERROR GOTO 20
       ** 20 ERROR 1
       **
       ** fall thru to the DEFAULT ERROR HANDLER
       **
       */
    }
    else
    {
      /* USER ERROR HANDLER SPECIFIED */
      /* find the user-specified error handler and jump there */
      LineType *x;
      for (x = My->StartMarker->next; x != My->EndMarker; x = x->next)
      {
        if (x->number == err_gotol)
        {
          /* FOUND */
          if (My->CurrentVersion->OptionFlags & (OPTION_ERROR_GOSUB))
          {
            /* 
             **
             ** -------------------------------------------------------------------------
             ** OPTION ERROR GOSUB
             ** -------------------------------------------------------------------------
             **
             ** RETURN should act like RESUME NEXT...
             ** Execution resumes at the statement immediately following the one which caused the error. 
             ** For structured commands, this is the bottom line of the structure.
             **
             */
            switch (l->cmdnum)
            {
            case C_IF8THEN:
              /* skip to END_IF */
              assert (l->OtherLine != NULL);
              for (l = l->OtherLine; l->cmdnum != C_END_IF; l = l->OtherLine);
              break;
            case C_SELECT_CASE:
              /* skip to END_SELECT */
              assert (l->OtherLine != NULL);
              for (l = l->OtherLine; l->cmdnum != C_END_SELECT;
                   l = l->OtherLine);
              break;
            default:
              l = l->next;
            }
            l->position = 0;
            My->StackHead->line = l;
            if (bwb_incexec ())
            {
              x->position = 0;
              assert (My->StackHead != NULL);
              My->StackHead->line = x;
              My->StackHead->ExecCode = EXEC_GOSUB;
            }
            else
            {
              /* ERROR -- OUT OF MEMORY */
              assert (My->StackHead != NULL);
              My->StackHead->line = My->EndMarker;
            }
          }
          else
          {
            /*
             **
             ** -------------------------------------------------------------------------
             ** OPTION ERROR GOTO
             ** -------------------------------------------------------------------------
             **
             */
            x->position = 0;        /* start of line */
            assert (My->StackHead != NULL);
            My->StackHead->line = x;
          }
          return;
        }
      }
      /* NOT FOUND */
      /* fall thru to the DEFAULT ERROR HANDLER */
    }
    /*
     **
     ** -------------------------------------------------------------------------
     **                           DEFAULT ERROR HANDLER (FATAL)
     ** -------------------------------------------------------------------------
     **
     */
    /*
     **
     ** display error message
     **
     */
    if (l->LineFlags & (LINE_USER) || l->number <= 0)
    {
      /* USER line in console */
      fprintf (My->SYSOUT->cfp, "\nERROR: %s\n", My->ERROR4);
      ResetConsoleColumn ();
    }
    else
    {
      /* BASIC program line */
      fprintf (My->SYSOUT->cfp, "\nERROR in line %d: %s\n", l->number,
               My->ERROR4);
      ResetConsoleColumn ();
      /*
       **
       ** display stack trace
       **
       */
      if (My->CurrentVersion->OptionFlags & (OPTION_TRACE_ON))
      {
        /* 
         ** Dump the BASIC stack trace when a FATAL error occurs.
         ** First line is the TOP of the stack.
         ** Last line is the BOTTOM of the stack.
         */
        StackType *StackItem;
        fprintf (My->SYSOUT->cfp, "\nSTACK TRACE:\n");
        for (StackItem = My->StackHead; StackItem != NULL;
             StackItem = StackItem->next)
        {
          LineType *l;

          l = StackItem->line;
          if (l != NULL)
          {
            if (MINLIN <= l->number && l->number <= MAXLIN)
            {
              /* BASIC program line */
              if (l->buffer == NULL)
              {
                fprintf (My->SYSOUT->cfp, "%d\n", l->number);
              }
              else
              {
                fprintf (My->SYSOUT->cfp, "%d:%s\n", l->number, l->buffer);
              }
            }
          }
        }
        ResetConsoleColumn ();
      }
    }

    My->AutomaticLineNumber = 0;
    My->AutomaticLineIncrement = 0;

    if (My->IsInteractive)
    {
      /* INTERACTIVE: terminate program */

      /* reset all stack counters */
      bwb_clrexec ();
      SetOnError (0);

      My->ERR = -1;                /* in bwb_execline(), default error handler */


      /* reset the break handler */
      signal (SIGINT, break_mes);


      return;
    }
    /* NOT INTERACTIVE:  terminate immediately */
    bwx_terminate ();
    return;                        /* never reached */
  }
  if (l->number > 0)
  {
    /* These events only occur in running programs */
    if (My->IsTimerOn)
    {
      /* TIMER ON */
      if (My->OnTimerCount > 0)
      {
        if (bwx_TIMER (0) > My->OnTimerExpires)
        {
          My->IsTimerOn = FALSE;
          if (My->OnTimerLineNumber > 0)
          {
            /* ON TIMER( My->OnTimerCount ) GOSUB My->OnTimerLineNumber */
            LineType *x;

            for (x = My->StartMarker->next; x != My->EndMarker; x = x->next)
            {
              if (x->number == My->OnTimerLineNumber)
              {
                /* FOUND */
                /* save current stack level */
                assert (My->StackHead != NULL);
                My->StackHead->line = r;
                /* increment exec stack */
                if (bwb_incexec ())
                {
                  /* set the new position to x and return x */
                  x->position = 0;
                  assert (My->StackHead != NULL);
                  My->StackHead->line = x;
                  My->StackHead->ExecCode = EXEC_GOSUB;
                }
                else
                {
                  /* ERROR */
                  assert (My->StackHead != NULL);
                  My->StackHead->line = My->EndMarker;
                }
                return;
              }
            }
            /* NOT FOUND */
          }
        }
      }
    }
  }
  /* check for end of line: if so, advance to next line and return */
  if (r == l)
  {
    /* advance to the next line */
    l->next->position = 0;
    r = l->next;
  }
  else if (line_is_eol (r))
  {
    /* we could be at the end-of-the-line, added for RETURN */
    /* advance to the next line */
    r->next->position = 0;
    r = r->next;
  }
  /* else reset with the value in r */
  assert (My->StackHead != NULL);
  My->StackHead->line = r;
}

/***************************************************************
  
   FUNCTION:       is_ln()
  
   DESCRIPTION:    This function determines whether a program
         line (in buffer) begins with a line number.
  
***************************************************************/

static int
is_ln (char *buffer)
{
  int position;
   
  assert (buffer != NULL);

  position = 0;
  buff_skip_spaces (buffer, &position);        /* keep this */
  if (bwb_isdigit (buffer[position]))
  {
    return TRUE;
  }
  return FALSE;
}

/***************************************************************
  
        FUNCTION:       is_numconst()
  
        DESCRIPTION:    This function reads the string in <buffer>
                        and returns TRUE if it is a numerical
                        constant and FALSE if it is not. At
                        this point, only decimal (base 10)
                        constants are detected.
  
***************************************************************/

static int
is_numconst (char *buffer)
{
  char *p;
   
  assert (buffer != NULL);

  /* Return FALSE for empty buffer */

  if (buffer[0] == NulChar)
  {
    return FALSE;
  }
  /* else check digits */

  p = buffer;
  while (*p != NulChar)
  {
    switch (*p)
    {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      break;
    default:
      return FALSE;
    }
    p++;
  }

  /* only numerical characters detected */

  return TRUE;

}

/* SWITCH */
LineType *
bwb_vector( LineType *l )
{
   LineType *r;
   assert (l != NULL);
   switch( l->cmdnum )
   {
   case C_DEF8LBL:
      r = bwb_DEF8LBL( l );
      break;
   case C_APPEND:
      r = bwb_APPEND( l );
      break;
   case C_AS:
      r = bwb_AS( l );
      break;
   case C_AUTO:
      r = bwb_AUTO( l );
      break;
   case C_BACKSPACE:
      r = bwb_BACKSPACE( l );
      break;
   case C_BREAK:
      r = bwb_BREAK( l );
      break;
   case C_BUILD:
      r = bwb_BUILD( l );
      break;
   case C_BYE:
      r = bwb_BYE( l );
      break;
   case C_CALL:
      r = bwb_CALL( l );
      break;
   case C_CASE:
      r = bwb_CASE( l );
      break;
   case C_CASE_ELSE:
      r = bwb_CASE_ELSE( l );
      break;
   case C_CHAIN:
      r = bwb_CHAIN( l );
      break;
   case C_CHANGE:
      r = bwb_CHANGE( l );
      break;
   case C_CLEAR:
      r = bwb_CLEAR( l );
      break;
   case C_CLOAD:
      r = bwb_CLOAD( l );
      break;
   case C_CLOAD8:
      r = bwb_CLOAD8( l );
      break;
   case C_CLOSE:
      r = bwb_CLOSE( l );
      break;
   case C_CLR:
      r = bwb_CLR( l );
      break;
   case C_CMDS:
      r = bwb_CMDS( l );
      break;
   case C_COMMON:
      r = bwb_COMMON( l );
      break;
   case C_CONSOLE:
      r = bwb_CONSOLE( l );
      break;
   case C_CONST:
      r = bwb_CONST( l );
      break;
   case C_CONT:
      r = bwb_CONT( l );
      break;
   case C_CONTINUE:
      r = bwb_CONTINUE( l );
      break;
   case C_COPY:
      r = bwb_COPY( l );
      break;
   case C_CREATE:
      r = bwb_CREATE( l );
      break;
   case C_CSAVE:
      r = bwb_CSAVE( l );
      break;
   case C_CSAVE8:
      r = bwb_CSAVE8( l );
      break;
   case C_DATA:
      r = bwb_DATA( l );
      break;
   case C_DEC:
      r = bwb_DEC( l );
      break;
   case C_DEF:
      r = bwb_DEF( l );
      break;
   case C_DEFBYT:
      r = bwb_DEFBYT( l );
      break;
   case C_DEFCUR:
      r = bwb_DEFCUR( l );
      break;
   case C_DEFDBL:
      r = bwb_DEFDBL( l );
      break;
   case C_DEFINT:
      r = bwb_DEFINT( l );
      break;
   case C_DEFLNG:
      r = bwb_DEFLNG( l );
      break;
   case C_DEFSNG:
      r = bwb_DEFSNG( l );
      break;
   case C_DEFSTR:
      r = bwb_DEFSTR( l );
      break;
   case C_DELETE:
      r = bwb_DELETE( l );
      break;
   case C_DELIMIT:
      r = bwb_DELIMIT( l );
      break;
   case C_DIM:
      r = bwb_DIM( l );
      break;
   case C_DISPLAY:
      r = bwb_DISPLAY( l );
      break;
   case C_DO:
      r = bwb_DO( l );
      break;
   case C_DOS:
      r = bwb_DOS( l );
      break;
   case C_DSP:
      r = bwb_DSP( l );
      break;
   case C_EDIT:
      r = bwb_EDIT( l );
      break;
   case C_ELSE:
      r = bwb_ELSE( l );
      break;
   case C_ELSEIF:
      r = bwb_ELSEIF( l );
      break;
   case C_END:
      r = bwb_END( l );
      break;
   case C_END_FUNCTION:
      r = bwb_END_FUNCTION( l );
      break;
   case C_END_IF:
      r = bwb_END_IF( l );
      break;
   case C_END_SELECT:
      r = bwb_END_SELECT( l );
      break;
   case C_END_SUB:
      r = bwb_END_SUB( l );
      break;
   case C_ERASE:
      r = bwb_ERASE( l );
      break;
   case C_EXCHANGE:
      r = bwb_EXCHANGE( l );
      break;
   case C_EXIT:
      r = bwb_EXIT( l );
      break;
   case C_EXIT_DO:
      r = bwb_EXIT_DO( l );
      break;
   case C_EXIT_FOR:
      r = bwb_EXIT_FOR( l );
      break;
   case C_EXIT_FUNCTION:
      r = bwb_EXIT_FUNCTION( l );
      break;
   case C_EXIT_REPEAT:
      r = bwb_EXIT_REPEAT( l );
      break;
   case C_EXIT_SUB:
      r = bwb_EXIT_SUB( l );
      break;
   case C_EXIT_WHILE:
      r = bwb_EXIT_WHILE( l );
      break;
   case C_FEND:
      r = bwb_FEND( l );
      break;
   case C_FIELD:
      r = bwb_FIELD( l );
      break;
   case C_FILE:
      r = bwb_FILE( l );
      break;
   case C_FILES:
      r = bwb_FILES( l );
      break;
   case C_FLEX:
      r = bwb_FLEX( l );
      break;
   case C_FNCS:
      r = bwb_FNCS( l );
      break;
   case C_FNEND:
      r = bwb_FNEND( l );
      break;
   case C_FOR:
      r = bwb_FOR( l );
      break;
   case C_FUNCTION:
      r = bwb_FUNCTION( l );
      break;
   case C_GET:
      r = bwb_GET( l );
      break;
   case C_GO:
      r = bwb_GO( l );
      break;
   case C_GO_SUB:
      r = bwb_GO_SUB( l );
      break;
   case C_GO_TO:
      r = bwb_GO_TO( l );
      break;
   case C_GOODBYE:
      r = bwb_GOODBYE( l );
      break;
   case C_GOSUB:
      r = bwb_GOSUB( l );
      break;
   case C_GOTO:
      r = bwb_GOTO( l );
      break;
   case C_HELP:
      r = bwb_HELP( l );
      break;
   case C_IF:
      r = bwb_IF( l );
      break;
   case C_IF_END:
      r = bwb_IF_END( l );
      break;
   case C_IF_MORE:
      r = bwb_IF_MORE( l );
      break;
   case C_IF8THEN:
      r = bwb_IF8THEN( l );
      break;
   case C_IMAGE:
      r = bwb_IMAGE( l );
      break;
   case C_INC:
      r = bwb_INC( l );
      break;
   case C_INPUT:
      r = bwb_INPUT( l );
      break;
   case C_INPUT_LINE:
      r = bwb_INPUT_LINE( l );
      break;
   case C_LET:
      r = bwb_LET( l );
      break;
   case C_LINE:
      r = bwb_LINE( l );
      break;
   case C_LINE_INPUT:
      r = bwb_LINE_INPUT( l );
      break;
   case C_LIST:
      r = bwb_LIST( l );
      break;
   case C_LISTNH:
      r = bwb_LISTNH( l );
      break;
   case C_LLIST:
      r = bwb_LLIST( l );
      break;
   case C_LOAD:
      r = bwb_LOAD( l );
      break;
   case C_LOCAL:
      r = bwb_LOCAL( l );
      break;
   case C_LOOP:
      r = bwb_LOOP( l );
      break;
   case C_LPRINT:
      r = bwb_LPRINT( l );
      break;
   case C_LPRINTER:
      r = bwb_LPRINTER( l );
      break;
   case C_LPT:
      r = bwb_LPT( l );
      break;
   case C_LSET:
      r = bwb_LSET( l );
      break;
   case C_MAINTAINER:
      r = bwb_MAINTAINER( l );
      break;
   case C_MAINTAINER_CMDS:
      r = bwb_MAINTAINER_CMDS( l );
      break;
   case C_MAINTAINER_CMDS_HTML:
      r = bwb_MAINTAINER_CMDS_HTML( l );
      break;
   case C_MAINTAINER_CMDS_ID:
      r = bwb_MAINTAINER_CMDS_ID( l );
      break;
   case C_MAINTAINER_CMDS_MANUAL:
      r = bwb_MAINTAINER_CMDS_MANUAL( l );
      break;
   case C_MAINTAINER_CMDS_SWITCH:
      r = bwb_MAINTAINER_CMDS_SWITCH( l );
      break;
   case C_MAINTAINER_CMDS_TABLE:
      r = bwb_MAINTAINER_CMDS_TABLE( l );
      break;
   case C_MAINTAINER_DEBUG:
      r = bwb_MAINTAINER_DEBUG( l );
      break;
   case C_MAINTAINER_DEBUG_OFF:
      r = bwb_MAINTAINER_DEBUG_OFF( l );
      break;
   case C_MAINTAINER_DEBUG_ON:
      r = bwb_MAINTAINER_DEBUG_ON( l );
      break;
   case C_MAINTAINER_FNCS:
      r = bwb_MAINTAINER_FNCS( l );
      break;
   case C_MAINTAINER_FNCS_HTML:
      r = bwb_MAINTAINER_FNCS_HTML( l );
      break;
   case C_MAINTAINER_FNCS_ID:
      r = bwb_MAINTAINER_FNCS_ID( l );
      break;
   case C_MAINTAINER_FNCS_MANUAL:
      r = bwb_MAINTAINER_FNCS_MANUAL( l );
      break;
   case C_MAINTAINER_FNCS_SWITCH:
      r = bwb_MAINTAINER_FNCS_SWITCH( l );
      break;
   case C_MAINTAINER_FNCS_TABLE:
      r = bwb_MAINTAINER_FNCS_TABLE( l );
      break;
   case C_MAINTAINER_MANUAL:
      r = bwb_MAINTAINER_MANUAL( l );
      break;
   case C_MAINTAINER_STACK:
      r = bwb_MAINTAINER_STACK( l );
      break;
   case C_MARGIN:
      r = bwb_MARGIN( l );
      break;
   case C_MAT:
      r = bwb_MAT( l );
      break;
   case C_MAT_GET:
      r = bwb_MAT_GET( l );
      break;
   case C_MAT_INPUT:
      r = bwb_MAT_INPUT( l );
      break;
   case C_MAT_PRINT:
      r = bwb_MAT_PRINT( l );
      break;
   case C_MAT_PUT:
      r = bwb_MAT_PUT( l );
      break;
   case C_MAT_READ:
      r = bwb_MAT_READ( l );
      break;
   case C_MAT_WRITE:
      r = bwb_MAT_WRITE( l );
      break;
   case C_MERGE:
      r = bwb_MERGE( l );
      break;
   case C_MID4:
      r = bwb_MID4( l );
      break;
   case C_MON:
      r = bwb_MON( l );
      break;
   case C_NAME:
      r = bwb_NAME( l );
      break;
   case C_NEW:
      r = bwb_NEW( l );
      break;
   case C_NEXT:
      r = bwb_NEXT( l );
      break;
   case C_OF:
      r = bwb_OF( l );
      break;
   case C_OLD:
      r = bwb_OLD( l );
      break;
   case C_ON:
      r = bwb_ON( l );
      break;
   case C_ON_ERROR:
      r = bwb_ON_ERROR( l );
      break;
   case C_ON_ERROR_GOSUB:
      r = bwb_ON_ERROR_GOSUB( l );
      break;
   case C_ON_ERROR_GOTO:
      r = bwb_ON_ERROR_GOTO( l );
      break;
   case C_ON_ERROR_RESUME:
      r = bwb_ON_ERROR_RESUME( l );
      break;
   case C_ON_ERROR_RESUME_NEXT:
      r = bwb_ON_ERROR_RESUME_NEXT( l );
      break;
   case C_ON_ERROR_RETURN:
      r = bwb_ON_ERROR_RETURN( l );
      break;
   case C_ON_ERROR_RETURN_NEXT:
      r = bwb_ON_ERROR_RETURN_NEXT( l );
      break;
   case C_ON_TIMER:
      r = bwb_ON_TIMER( l );
      break;
   case C_OPEN:
      r = bwb_OPEN( l );
      break;
   case C_OPTION:
      r = bwb_OPTION( l );
      break;
   case C_OPTION_ANGLE:
      r = bwb_OPTION_ANGLE( l );
      break;
   case C_OPTION_ANGLE_DEGREES:
      r = bwb_OPTION_ANGLE_DEGREES( l );
      break;
   case C_OPTION_ANGLE_GRADIANS:
      r = bwb_OPTION_ANGLE_GRADIANS( l );
      break;
   case C_OPTION_ANGLE_RADIANS:
      r = bwb_OPTION_ANGLE_RADIANS( l );
      break;
   case C_OPTION_ARITHMETIC:
      r = bwb_OPTION_ARITHMETIC( l );
      break;
   case C_OPTION_ARITHMETIC_DECIMAL:
      r = bwb_OPTION_ARITHMETIC_DECIMAL( l );
      break;
   case C_OPTION_ARITHMETIC_FIXED:
      r = bwb_OPTION_ARITHMETIC_FIXED( l );
      break;
   case C_OPTION_ARITHMETIC_NATIVE:
      r = bwb_OPTION_ARITHMETIC_NATIVE( l );
      break;
   case C_OPTION_BASE:
      r = bwb_OPTION_BASE( l );
      break;
   case C_OPTION_BUGS:
      r = bwb_OPTION_BUGS( l );
      break;
   case C_OPTION_BUGS_BOOLEAN:
      r = bwb_OPTION_BUGS_BOOLEAN( l );
      break;
   case C_OPTION_BUGS_OFF:
      r = bwb_OPTION_BUGS_OFF( l );
      break;
   case C_OPTION_BUGS_ON:
      r = bwb_OPTION_BUGS_ON( l );
      break;
   case C_OPTION_COMPARE:
      r = bwb_OPTION_COMPARE( l );
      break;
   case C_OPTION_COMPARE_BINARY:
      r = bwb_OPTION_COMPARE_BINARY( l );
      break;
   case C_OPTION_COMPARE_DATABASE:
      r = bwb_OPTION_COMPARE_DATABASE( l );
      break;
   case C_OPTION_COMPARE_TEXT:
      r = bwb_OPTION_COMPARE_TEXT( l );
      break;
   case C_OPTION_COVERAGE:
      r = bwb_OPTION_COVERAGE( l );
      break;
   case C_OPTION_COVERAGE_OFF:
      r = bwb_OPTION_COVERAGE_OFF( l );
      break;
   case C_OPTION_COVERAGE_ON:
      r = bwb_OPTION_COVERAGE_ON( l );
      break;
   case C_OPTION_DATE:
      r = bwb_OPTION_DATE( l );
      break;
   case C_OPTION_DIGITS:
      r = bwb_OPTION_DIGITS( l );
      break;
   case C_OPTION_DISABLE:
      r = bwb_OPTION_DISABLE( l );
      break;
   case C_OPTION_DISABLE_COMMAND:
      r = bwb_OPTION_DISABLE_COMMAND( l );
      break;
   case C_OPTION_DISABLE_FUNCTION:
      r = bwb_OPTION_DISABLE_FUNCTION( l );
      break;
   case C_OPTION_DISABLE_OPERATOR:
      r = bwb_OPTION_DISABLE_OPERATOR( l );
      break;
   case C_OPTION_EDIT:
      r = bwb_OPTION_EDIT( l );
      break;
   case C_OPTION_ENABLE:
      r = bwb_OPTION_ENABLE( l );
      break;
   case C_OPTION_ENABLE_COMMAND:
      r = bwb_OPTION_ENABLE_COMMAND( l );
      break;
   case C_OPTION_ENABLE_FUNCTION:
      r = bwb_OPTION_ENABLE_FUNCTION( l );
      break;
   case C_OPTION_ENABLE_OPERATOR:
      r = bwb_OPTION_ENABLE_OPERATOR( l );
      break;
   case C_OPTION_ERROR:
      r = bwb_OPTION_ERROR( l );
      break;
   case C_OPTION_ERROR_GOSUB:
      r = bwb_OPTION_ERROR_GOSUB( l );
      break;
   case C_OPTION_ERROR_GOTO:
      r = bwb_OPTION_ERROR_GOTO( l );
      break;
   case C_OPTION_EXPLICIT:
      r = bwb_OPTION_EXPLICIT( l );
      break;
   case C_OPTION_EXTENSION:
      r = bwb_OPTION_EXTENSION( l );
      break;
   case C_OPTION_FILES:
      r = bwb_OPTION_FILES( l );
      break;
   case C_OPTION_IMPLICIT:
      r = bwb_OPTION_IMPLICIT( l );
      break;
   case C_OPTION_INDENT:
      r = bwb_OPTION_INDENT( l );
      break;
   case C_OPTION_LABELS:
      r = bwb_OPTION_LABELS( l );
      break;
   case C_OPTION_LABELS_OFF:
      r = bwb_OPTION_LABELS_OFF( l );
      break;
   case C_OPTION_LABELS_ON:
      r = bwb_OPTION_LABELS_ON( l );
      break;
   case C_OPTION_PROMPT:
      r = bwb_OPTION_PROMPT( l );
      break;
   case C_OPTION_PUNCT:
      r = bwb_OPTION_PUNCT( l );
      break;
   case C_OPTION_PUNCT_AT:
      r = bwb_OPTION_PUNCT_AT( l );
      break;
   case C_OPTION_PUNCT_BYTE:
      r = bwb_OPTION_PUNCT_BYTE( l );
      break;
   case C_OPTION_PUNCT_COMMENT:
      r = bwb_OPTION_PUNCT_COMMENT( l );
      break;
   case C_OPTION_PUNCT_CURRENCY:
      r = bwb_OPTION_PUNCT_CURRENCY( l );
      break;
   case C_OPTION_PUNCT_DOUBLE:
      r = bwb_OPTION_PUNCT_DOUBLE( l );
      break;
   case C_OPTION_PUNCT_FILENUM:
      r = bwb_OPTION_PUNCT_FILENUM( l );
      break;
   case C_OPTION_PUNCT_IMAGE:
      r = bwb_OPTION_PUNCT_IMAGE( l );
      break;
   case C_OPTION_PUNCT_INPUT:
      r = bwb_OPTION_PUNCT_INPUT( l );
      break;
   case C_OPTION_PUNCT_INTEGER:
      r = bwb_OPTION_PUNCT_INTEGER( l );
      break;
   case C_OPTION_PUNCT_LONG:
      r = bwb_OPTION_PUNCT_LONG( l );
      break;
   case C_OPTION_PUNCT_LPAREN:
      r = bwb_OPTION_PUNCT_LPAREN( l );
      break;
   case C_OPTION_PUNCT_PRINT:
      r = bwb_OPTION_PUNCT_PRINT( l );
      break;
   case C_OPTION_PUNCT_QUOTE:
      r = bwb_OPTION_PUNCT_QUOTE( l );
      break;
   case C_OPTION_PUNCT_RPAREN:
      r = bwb_OPTION_PUNCT_RPAREN( l );
      break;
   case C_OPTION_PUNCT_SINGLE:
      r = bwb_OPTION_PUNCT_SINGLE( l );
      break;
   case C_OPTION_PUNCT_STATEMENT:
      r = bwb_OPTION_PUNCT_STATEMENT( l );
      break;
   case C_OPTION_PUNCT_STRING:
      r = bwb_OPTION_PUNCT_STRING( l );
      break;
   case C_OPTION_RECLEN:
      r = bwb_OPTION_RECLEN( l );
      break;
   case C_OPTION_RENUM:
      r = bwb_OPTION_RENUM( l );
      break;
   case C_OPTION_ROUND:
      r = bwb_OPTION_ROUND( l );
      break;
   case C_OPTION_ROUND_BANK:
      r = bwb_OPTION_ROUND_BANK( l );
      break;
   case C_OPTION_ROUND_MATH:
      r = bwb_OPTION_ROUND_MATH( l );
      break;
   case C_OPTION_ROUND_TRUNCATE:
      r = bwb_OPTION_ROUND_TRUNCATE( l );
      break;
   case C_OPTION_SCALE:
      r = bwb_OPTION_SCALE( l );
      break;
   case C_OPTION_SLEEP:
      r = bwb_OPTION_SLEEP( l );
      break;
   case C_OPTION_STDERR:
      r = bwb_OPTION_STDERR( l );
      break;
   case C_OPTION_STDIN:
      r = bwb_OPTION_STDIN( l );
      break;
   case C_OPTION_STDOUT:
      r = bwb_OPTION_STDOUT( l );
      break;
   case C_OPTION_STRICT:
      r = bwb_OPTION_STRICT( l );
      break;
   case C_OPTION_STRICT_OFF:
      r = bwb_OPTION_STRICT_OFF( l );
      break;
   case C_OPTION_STRICT_ON:
      r = bwb_OPTION_STRICT_ON( l );
      break;
   case C_OPTION_TERMINAL:
      r = bwb_OPTION_TERMINAL( l );
      break;
   case C_OPTION_TERMINAL_ADM:
      r = bwb_OPTION_TERMINAL_ADM( l );
      break;
   case C_OPTION_TERMINAL_ANSI:
      r = bwb_OPTION_TERMINAL_ANSI( l );
      break;
   case C_OPTION_TERMINAL_NONE:
      r = bwb_OPTION_TERMINAL_NONE( l );
      break;
   case C_OPTION_TIME:
      r = bwb_OPTION_TIME( l );
      break;
   case C_OPTION_TRACE:
      r = bwb_OPTION_TRACE( l );
      break;
   case C_OPTION_TRACE_OFF:
      r = bwb_OPTION_TRACE_OFF( l );
      break;
   case C_OPTION_TRACE_ON:
      r = bwb_OPTION_TRACE_ON( l );
      break;
   case C_OPTION_USING:
      r = bwb_OPTION_USING( l );
      break;
   case C_OPTION_USING_ALL:
      r = bwb_OPTION_USING_ALL( l );
      break;
   case C_OPTION_USING_COMMA:
      r = bwb_OPTION_USING_COMMA( l );
      break;
   case C_OPTION_USING_DIGIT:
      r = bwb_OPTION_USING_DIGIT( l );
      break;
   case C_OPTION_USING_DOLLAR:
      r = bwb_OPTION_USING_DOLLAR( l );
      break;
   case C_OPTION_USING_EXRAD:
      r = bwb_OPTION_USING_EXRAD( l );
      break;
   case C_OPTION_USING_FILLER:
      r = bwb_OPTION_USING_FILLER( l );
      break;
   case C_OPTION_USING_FIRST:
      r = bwb_OPTION_USING_FIRST( l );
      break;
   case C_OPTION_USING_LENGTH:
      r = bwb_OPTION_USING_LENGTH( l );
      break;
   case C_OPTION_USING_LITERAL:
      r = bwb_OPTION_USING_LITERAL( l );
      break;
   case C_OPTION_USING_MINUS:
      r = bwb_OPTION_USING_MINUS( l );
      break;
   case C_OPTION_USING_PERIOD:
      r = bwb_OPTION_USING_PERIOD( l );
      break;
   case C_OPTION_USING_PLUS:
      r = bwb_OPTION_USING_PLUS( l );
      break;
   case C_OPTION_VERSION:
      r = bwb_OPTION_VERSION( l );
      break;
   case C_OPTION_ZONE:
      r = bwb_OPTION_ZONE( l );
      break;
   case C_PAUSE:
      r = bwb_PAUSE( l );
      break;
   case C_PDEL:
      r = bwb_PDEL( l );
      break;
   case C_POP:
      r = bwb_POP( l );
      break;
   case C_PRINT:
      r = bwb_PRINT( l );
      break;
   case C_PTP:
      r = bwb_PTP( l );
      break;
   case C_PTR:
      r = bwb_PTR( l );
      break;
   case C_PUT:
      r = bwb_PUT( l );
      break;
   case C_QUIT:
      r = bwb_QUIT( l );
      break;
   case C_READ:
      r = bwb_READ( l );
      break;
   case C_RECALL:
      r = bwb_RECALL( l );
      break;
   case C_REM:
      r = bwb_REM( l );
      break;
   case C_RENAME:
      r = bwb_RENAME( l );
      break;
   case C_RENUM:
      r = bwb_RENUM( l );
      break;
   case C_RENUMBER:
      r = bwb_RENUMBER( l );
      break;
   case C_REPEAT:
      r = bwb_REPEAT( l );
      break;
   case C_REPLACE:
      r = bwb_REPLACE( l );
      break;
   case C_RESET:
      r = bwb_RESET( l );
      break;
   case C_RESTORE:
      r = bwb_RESTORE( l );
      break;
   case C_RESUME:
      r = bwb_RESUME( l );
      break;
   case C_RETURN:
      r = bwb_RETURN( l );
      break;
   case C_RSET:
      r = bwb_RSET( l );
      break;
   case C_RUN:
      r = bwb_RUN( l );
      break;
   case C_RUNNH:
      r = bwb_RUNNH( l );
      break;
   case C_SAVE:
      r = bwb_SAVE( l );
      break;
   case C_SCRATCH:
      r = bwb_SCRATCH( l );
      break;
   case C_SELECT:
      r = bwb_SELECT( l );
      break;
   case C_SELECT_CASE:
      r = bwb_SELECT_CASE( l );
      break;
   case C_STEP:
      r = bwb_STEP( l );
      break;
   case C_STOP:
      r = bwb_STOP( l );
      break;
   case C_STORE:
      r = bwb_STORE( l );
      break;
   case C_SUB:
      r = bwb_SUB( l );
      break;
   case C_SUB_END:
      r = bwb_SUB_END( l );
      break;
   case C_SUB_EXIT:
      r = bwb_SUB_EXIT( l );
      break;
   case C_SUBEND:
      r = bwb_SUBEND( l );
      break;
   case C_SUBEXIT:
      r = bwb_SUBEXIT( l );
      break;
   case C_SWAP:
      r = bwb_SWAP( l );
      break;
   case C_SYSTEM:
      r = bwb_SYSTEM( l );
      break;
   case C_TEXT:
      r = bwb_TEXT( l );
      break;
   case C_THEN:
      r = bwb_THEN( l );
      break;
   case C_TIMER:
      r = bwb_TIMER( l );
      break;
   case C_TIMER_OFF:
      r = bwb_TIMER_OFF( l );
      break;
   case C_TIMER_ON:
      r = bwb_TIMER_ON( l );
      break;
   case C_TIMER_STOP:
      r = bwb_TIMER_STOP( l );
      break;
   case C_TLOAD:
      r = bwb_TLOAD( l );
      break;
   case C_TO:
      r = bwb_TO( l );
      break;
   case C_TRACE:
      r = bwb_TRACE( l );
      break;
   case C_TRACE_OFF:
      r = bwb_TRACE_OFF( l );
      break;
   case C_TRACE_ON:
      r = bwb_TRACE_ON( l );
      break;
   case C_TSAVE:
      r = bwb_TSAVE( l );
      break;
   case C_TTY:
      r = bwb_TTY( l );
      break;
   case C_TTY_IN:
      r = bwb_TTY_IN( l );
      break;
   case C_TTY_OUT:
      r = bwb_TTY_OUT( l );
      break;
   case C_UNTIL:
      r = bwb_UNTIL( l );
      break;
   case C_USE:
      r = bwb_USE( l );
      break;
   case C_VARS:
      r = bwb_VARS( l );
      break;
   case C_WEND:
      r = bwb_WEND( l );
      break;
   case C_WHILE:
      r = bwb_WHILE( l );
      break;
   case C_WRITE:
      r = bwb_WRITE( l );
      break;
   default:
      WARN_INTERNAL_ERROR;
      r = l;
      break;
   }
   return r;
}

/* EOF */
Un proyecto texto-plano.xyz