File: pilrc.c

package info (click to toggle)
pilrc 2.4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 524 kB
  • ctags: 1,121
  • sloc: ansic: 6,584; asm: 137; makefile: 55
file content (2935 lines) | stat: -rw-r--r-- 70,513 bytes parent folder | download
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
/*-----------------------------------------------------------------------------
| pilrc.c -- a resource compiler for the US Robotics Pilot
|
|	Wes Cherry - wesc@ricochet.net
|	29 Oct 1996
|
|	Known bugs:
|	[fixed] Prevbottom doesn't work if previous item is a LIST
|	LIST DISABLED doesn't work.  Seems to be a Palm bug.
|	FIELD: MAXCHARS required for edit to work
|	
|
| See pilrc.htm for documentation
-------------------------------------------------------------WESC------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#define EMITRWT	
#include "pilrc.h"
#include "bitmap.h"
#include "font.h"

#define idAutoInit 9999


#define idPalmOSReservedMin 10000

/*-----------------------------------------------------------------------------
|	 Globals
-------------------------------------------------------------WESC------------*/
/* Are we PilRCUI -- this was an attempt to merge pilrc and pilrcui */
BOOL vfWinGUI;
/* Quiet ouptput */
BOOL vfQuiet;
/* Translations */
char *szLanguage;
TE *pteFirst;
/* RTL For Hebrew*/
BOOL vfRTL = fFalse;

/* next auto id */
int idAutoMac = idAutoInit;
BOOL vfAutoId;

/* Warning about duplicates in forms and menus */
/* if == 2 then we warn about duplicate labels too */
BOOL vfCheckDupes;

/* Form globals */
FRM *vpfrm;
//RCFORM form;	/* current form being parsed */
//#define cobjMax 256
//RCFORMOBJLIST rglt[cobjMax];


/* Menu globals */
RCMENUBAR menu;
#define imiMax 128	
RCMENUITEM rgmi[imiMax];
#define impdMax 32
RCMENUPULLDOWN rgmpd[impdMax];
int imiMac;	
int idMenu;
#define iidMenuMax 128
int iidMenuMac;
int rgidMenu[iidMenuMax];

#define iidStringMax 512
int iidStringMac;
int rgidString[iidStringMax];
#define iidAlertMax 512
int iidAlertMac;
int rgidAlert[iidAlertMax];



/* Symbol table */
SYM *psymFirst;

/* Parse globals */
BOOL fTokUngotten;
TOK tokPrev;
TOK tok;
int iline;
char szLine[4096];


/* Rect for Prev* keywords */
RCRECT rcPrev;

/*-----------------------------------------------------------------------------
|	AddSym
|	
|		Add symbol with value wVal to symbol table
|
|		Note! no check is made for previous existence
-------------------------------------------------------------WESC------------*/
VOID AddSym(char *sz, int wVal)
	{
	SYM *psym;

	psym = calloc(1, sizeof(SYM));
	psym->sz = strdup(sz);
	psym->wVal = wVal;
	psym->psymNext = psymFirst;
	psymFirst = psym;
	}



/*-----------------------------------------------------------------------------
|	PsymLookup
|	
|		Lookup symbol based on sz -- case sensitive
-------------------------------------------------------------WESC------------*/
SYM *PsymLookup(char *sz)
	{
	SYM *psym;

	for (psym = psymFirst; psym != NULL; psym = psym->psymNext)
		if (strcmp(psym->sz, sz) == 0)
			return psym;
	return NULL;
	}


int IdGetAutoId()
	{
	return idAutoMac--;
	}

SYM *PsymAddSymAutoId(char *sz)
	{
	SYM *psym;
	
	AddSym(sz, IdGetAutoId());
	psym = PsymLookup(tok.lex.szId);
	psym->fAutoId = fTrue;
	return psym;
	}

/*-----------------------------------------------------------------------------
|	FreeSymTable
|	
|	Free up all memory allocated for the symbol table
-------------------------------------------------------------WESC------------*/
VOID FreeSymTable()
	{
	SYM *psym;
	SYM *psymNext;

	for (psym = psymFirst; psym != NULL; psym = psymNext)
		{
		psymNext = psym->psymNext;
		free(psym->sz);
		free(psym);
		}
	psymFirst = NULL;
	}

/*-----------------------------------------------------------------------------
|	PchFromRw
|	
|		Maps a reserved word into a string
-------------------------------------------------------------WESC------------*/
char *PchFromRw(int rw, BOOL fTrySecondName)
	{
	RWT *prwt;
	
	prwt = rgrwt;
	while (prwt->sz1 != NULL)
		{
		if (prwt->rw == rw)
			{
			if (fTrySecondName && prwt->sz2 != NULL)
				return prwt->sz2;
			return prwt->sz1;
			}
		prwt++;
		}
	return NULL;
	}

	

/*-----------------------------------------------------------------------------
|	RwFromLex
|	
|		Looks up lex.id in reserved word table.  returns rwNil if not found
-------------------------------------------------------------WESC------------*/
RW RwFromLex(LEX *plex)
	{
	RWT *prwt;
	
	if (plex->lt != ltId)
		return rwNil;
	prwt = rgrwt;
	while (prwt->sz1 != NULL)
		{
		if (FSzEqI(plex->szId, prwt->sz1) || (prwt->sz2 != NULL && FSzEqI(plex->szId, prwt->sz2)))
			return prwt->rw;	
		prwt++;
		}
	return rwNil;
	}	


/*-----------------------------------------------------------------------------
|	PteFromSz
|	
|		Look up a language tranlation entry. All strings in the .rcp file
|	potentially get translated
-------------------------------------------------------------WESC------------*/
TE *PteFromSz(char *sz)
	{
	TE *pte;

	for (pte = pteFirst; pte != NULL; pte = pte->pteNext)
		if (strcmp(pte->szOrig, sz) == 0)
			return pte;
	return NULL;
	}

/*-----------------------------------------------------------------------------
|	FreeTranslations
|	
|		Free up the translation table
-------------------------------------------------------------WESC------------*/
VOID FreeTranslations()
	{
	TE *pte;
	TE *pteNext;

	for (pte = pteFirst; pte != NULL; pte = pteNext)
		{
		pteNext = pte->pteNext;
		free(pte->szOrig);
		free(pte->szTrans);
		free(pte);
		}
	pteFirst = NULL;
	}

/*-----------------------------------------------------------------------------
|	NextLine
|
|		Skip lexer ahead to the next line
-------------------------------------------------------------WESC------------*/
static BOOL NextLine(void)
	{
	BOOL    retval;

	retval = fFalse;

	szLine[0] = '\0';		/* just in case there is nothing to be gotten   */
	if (fgets(szLine, sizeof(szLine), vfhIn) != NULL)
		{
		iline++;
		retval = fTrue;
		}
	FInitLexer(szLine, fTrue);	/* so program can shut down gracefully      */

	return(retval);
	}

/*-----------------------------------------------------------------------------
|	FGetTok
|
|		Get the next token.  returns fFalse on EOF
|
|	Consistency issue -- takes a ptok, but some other other routines don't.
|	only one global tok...
-------------------------------------------------------------WESC------------*/
static BOOL FGetTok(TOK *ptok)
	{
	BOOL    fInComment;


	if (fTokUngotten)
		{
		*ptok = tokPrev;
		fTokUngotten = fFalse;
		return fTrue;
		}

	ptok->rw = rwNil;
	fInComment = fFalse;
	for (;;)
		{
		while (!FGetLex(&ptok->lex, fInComment))
			{
			if (!NextLine())
				{
				if (fInComment)
					ErrorLine("unexpected end of file during C-style comment");
				return fFalse;
				}
			}

		if (ptok->lex.lt == ltCComment)
			fInComment = fTrue;
		else if (ptok->lex.lt != ltEndCComment)
			break;
		else
			fInComment	= fFalse;
		}

	if (ptok->lex.lt == ltId)
		{
		/* check if it is a reserved word */
		ptok->rw = RwFromLex(&ptok->lex);
		}
	else if (ptok->lex.lt == ltStr)
		{
		TE *pte;
		/* attempt translation */
		pte = PteFromSz(ptok->lex.szId);
		if (pte != NULL)
			strcpy(ptok->lex.szId, pte->szTrans);
		}

	tokPrev = *ptok;
	return fTrue;
	}

/*-----------------------------------------------------------------------------
|	UngetTok
|	
|		Pushback one token.  Note! that this is 1 level only!
-------------------------------------------------------------WESC------------*/
VOID UngetTok()
	{
	tok = tokPrev;
	fTokUngotten = fTrue;
	}	


	
	
/*-----------------------------------------------------------------------------
|	GetExpectLt
|	
|		Get a token and expect a particular lex type.  Emit szErr if it isn't
|	what's expected
-------------------------------------------------------------WESC------------*/
VOID GetExpectLt(TOK *ptok, LT lt, char *szErr)
	{
	FGetTok(ptok);
	if (ptok->lex.lt != lt)
		{
		if (szErr == NULL)
			{
			if (lt == ltId)
				ErrorLine2("Syntax error : expecting identifier, got", ptok->lex.szId);
			else if (lt == ltStr)
				ErrorLine2("Syntax error : expecting string, got", ptok->lex.szId);			
			else if (lt == ltConst)
				ErrorLine2("Syntax error : expecting constant, got", ptok->lex.szId);
			else
				ErrorLine2("syntax error: ", ptok->lex.szId);
			}
		else
			{
			char szT[128];
			sprintf(szT, "expecting: %s, got", szErr);
			ErrorLine2(szT, ptok->lex.szId);
			}
		}
	}
	
/*-----------------------------------------------------------------------------
|	GetExpectRw
|	
|		Get and expect a particular  reserved word.  Emit "Expecting..." if next
|	token isn't rw
-------------------------------------------------------------WESC------------*/
VOID GetExpectRw(RW rw)
	{
	TOK tok;
	
	FGetTok(&tok);
	if (tok.rw != rw)
		{
		char szErr[64];
		sprintf(szErr, "%s expected, got", PchFromRw(rw, fTrue));
		ErrorLine2(szErr, tok.lex.szId);
		}
	}

/*-----------------------------------------------------------------------------
|	PchGetSz
|	
|		Get a quoted string.  return dup'ed string.  (remember to free!)
-------------------------------------------------------------WESC------------*/
char *PchGetSz(char *szErr)
	{
	GetExpectLt(&tok, ltStr, szErr);
	return strdup(tok.lex.szId);
	}

#ifdef DOESNTWORK
/* attempt at allowing GCC preprocessed string files */
/*-----------------------------------------------------------------------------
|	PchGetSzMultiLine
|	
|   gets strings on multiple lines w/ \ continuation character.
-------------------------------------------------------------WESC------------*/
char *PchGetSzMultiLine(char *szErr)
	{
	char sz[16384];

	GetExpectLt(&tok, ltStr, szErr);
	strcpy(sz, tok.lex.szId);
	while (FGetTok(&tok))
		{
		if (tok.lex.lt == ltStr)
			{
			strcat(sz, tok.lex.szId);
			}
		else if (tok.lex.lt != ltBSlash)
			{
			UngetTok();
			break;
			}
		else
			{
			GetExpectLt(&tok, ltStr, szErr);
			strcat(sz, tok.lex.szId);
			}
		}
	return strdup(sz);
	}
#else
/*-----------------------------------------------------------------------------
|	PchGetSzMultiLine
|	
|   gets strings on multiple lines w/ \ continuation character.
-------------------------------------------------------------WESC------------*/
char *PchGetSzMultiLine(char *szErr)
	{
	char sz[8192];

	GetExpectLt(&tok, ltStr, szErr);
	strcpy(sz, tok.lex.szId);
	while (FGetTok(&tok))
		{
		if (tok.lex.lt != ltBSlash)
			{
			UngetTok();
			break;
			}
		GetExpectLt(&tok, ltStr, szErr);
		strcat(sz, tok.lex.szId);
		}
	return strdup(sz);
	}
#endif

/*-----------------------------------------------------------------------------
|	PchGetId
|	
|		Epect and get an ident. (ltId)
-------------------------------------------------------------WESC------------*/
char *PchGetId(char *szErr)
	{
	GetExpectLt(&tok, ltId, szErr);
	return strdup(tok.lex.szId);
	}


/*-----------------------------------------------------------------------------
|	WGetConst
|	
|		Get an integer constant or one of the Prev* reserved words, returning
|	valu	e.
|		
-------------------------------------------------------------WESC------------*/
int WGetConst(char *szErr)
	{
	char sz[256];

	if (!FGetTok(&tok))
		ErrorLine("unexpected end of file");
	switch (tok.rw)
		{
	default:
		if (tok.lex.lt == ltId)
			{
			SYM *psym;

			psym = PsymLookup(tok.lex.szId);
			if (psym == NULL)
				{
				if (vfAutoId)
					{
					psym = PsymAddSymAutoId(tok.lex.szId);
					}
				else
					{
					sprintf(sz, "Expecting %s, got unknown symbol:", szErr);
					ErrorLine2(sz, tok.lex.szId);
					}
				}
			return psym->wVal;
			}
		if (tok.lex.lt != ltConst)
			{
			sprintf(sz, "%s expected, got", szErr);
			ErrorLine2(sz, tok.lex.szId);
			}
		return tok.lex.val;
	case rwPrevLeft:
		return  rcPrev.topLeft.x;
	case rwPrevRight:
		return  rcPrev.topLeft.x+rcPrev.extent.x;
	case rwPrevWidth:
		return rcPrev.extent.x;
	case rwPrevTop:
		return  rcPrev.topLeft.y;
	case rwPrevBottom:
		return  rcPrev.topLeft.y+rcPrev.extent.y;
	case rwPrevHeight:
		return rcPrev.extent.y;
		}
	}


	
int WGetConstEx(char *szErr);
	
	
/*-----------------------------------------------------------------------------
| WGetConstExFactor
| 
| Get a constant expression -- parens allowed left to right associativity
-------------------------------------------------------------WESC------------*/
int WGetConstExFactor(char *szErr) 
	{
	int wVal;

	if (FGetTok(&tok)) 
		{
		if (tok.lex.lt == ltLParen) 
			{
			wVal = WGetConstEx(szErr);
			if (!FGetTok(&tok) || tok.lex.lt != ltRParen)
				ErrorLine("Expected but didn't get ')'!");
			} 
		else 
			{
			UngetTok();
			wVal = WGetConst(szErr);
			}
		}
	else
		{
		wVal = WGetConst(szErr); /* aka hack */
		}

	return wVal;
	}
		
		
		
/*-----------------------------------------------------------------------------
| WGetConstEx
| 
| Get a constant expression -- parens allowed left to right associativity
-------------------------------------------------------------WESC------------*/
int WGetConstEx(char *szErr)
	{
	int wValT;
	int wVal;
	LT ltOp;
	
	wVal = WGetConstExFactor(szErr);


	for (;;)
		{
		if(!FGetTok(&tok)) 
			return wVal;
			
		switch (tok.lex.lt) 
			{
		default:
			UngetTok();
			return wVal;
    
		case ltPlus:
		case ltMinus:
		case ltMult:
		case ltDiv:
		case ltPipe:
			ltOp = tok.lex.lt;
			wValT = WGetConstExFactor(szErr);
			switch (ltOp) 
				{
			case ltPlus:
				wVal += wValT;
				break;
			case ltMinus:
				wVal -= wValT;
				break;
			case ltMult:
				wVal *= wValT;
				break;
			case ltDiv:
				if (wValT == 0)
					ErrorLine("Divide By Zero!");
				wVal /= wValT;
				break;
			case ltPipe:
				if (vfRTL)
					wVal = wValT;
				break;
				}
			}
	}
	return wVal;
	}
	

/*-----------------------------------------------------------------------------
|	Various Konstant types -- basically deferred evaluation of constants
|	mainly for AUTO and CENTER because we can't evaluate them until we know
|	the font for the particular item.
-------------------------------------------------------------WESC------------*/

/* Konstant Type */
typedef enum _kt
	{
	ktConst,
	ktCenter,
	ktAuto,
	ktCenterAt,
	ktRightAt,
	ktBottomAt,
	} KT;

/* Konstant */
typedef struct _k
	{
	KT kt;
	int wVal;
	} K;

/* Konstant Point */
typedef struct _kpt
	{
	K kX;
	K kY;
	} KPT;

/* Konstant Rect */
typedef struct _krc
	{
	KPT kptUpperLeft;
	KPT kptExtent;
	} KRC;

	

/*-----------------------------------------------------------------------------
|	KtGetK
|	
|		Get a Konstant, returning the type.
-------------------------------------------------------------WESC------------*/
KT KtGetK(K *pk, char *szErr)
	{
	if(!FGetTok(&tok))
		ErrorLine("Unexpected end of file");
	switch (tok.rw)
		{
	default:
		UngetTok();
		pk->kt = ktConst;
		pk->wVal = WGetConstEx(szErr);
		break;
	case rwCenter:
		pk->kt = ktCenter;
		if (FGetTok(&tok))
			{
			if (tok.lex.lt == ltAt)
				{
				pk->wVal = WGetConstEx("CENTER@ position")*2;	/* we store extent */
				pk->kt = ktCenterAt;
				}
			else
				UngetTok();
			}
			
		break;
	case rwAuto:
		pk->kt = ktAuto;
		break;
	case rwRight:
		pk->kt = ktRightAt;
		GetExpectLt(&tok, ltAt, "@");
		pk->wVal = WGetConstEx("RIGHT@ position");	
		break;				
	case rwBottom:
		pk->kt = ktBottomAt;
		GetExpectLt(&tok, ltAt, "@");
		pk->wVal = WGetConstEx("BOTTOM@ position");	
		break;				
		}
	return pk->kt;
	}


/*-----------------------------------------------------------------------------
|	ParseKpt
|	
|		Parse a point (not the AT and () parts).
-------------------------------------------------------------WESC------------*/
VOID ParseKpt(KPT *pkpt)
	{
	KtGetK(&pkpt->kX, "x pos");
	KtGetK(&pkpt->kY, "y pos");
	}

/*-----------------------------------------------------------------------------
|	ParseKrc
|	
|		Parse a rect (not the AT and () parts).
-------------------------------------------------------------WESC------------*/
VOID ParseKrc(KRC *pkrc)
	{
	KtGetK(&pkrc->kptUpperLeft.kX, "rect left");
	KtGetK(&pkrc->kptUpperLeft.kY, "rect top");
	KtGetK(&pkrc->kptExtent.kX, "rect width");
	KtGetK(&pkrc->kptExtent.kY, "rect height");
	}


/*-----------------------------------------------------------------------------
|	ITM
|		an item in a form -- grif and grif2 define the syntax of the item
|	and what to expect.
-------------------------------------------------------------WESC------------*/
typedef struct _itm
	{
	int grif;
	int grifOut;
	int grif2;
	int grif2Out;
	char *text;
	int cbText;		/* length of text including nul terminator */
	int id;
	int listid;
	KRC krc;
	/* RectangleType rc; */
	RCRECT rc;
	KPT kpt;
	/*PointType pt; */
	RCPOINT pt;
	BOOL usable;
	BOOL leftAnchor;
	int frame;
	BOOL enabled;
	BOOL on;	/* checked */
	BOOL editable;
	BOOL underlined;
	BOOL singleLine;
	BOOL dynamicSize;
	int justification;
	int maxChars;
	int autoShift;
	BOOL hasScrollBar;
	BOOL numeric;
	int numItems;
	int cvis;
	int group;
	int font;
	int rscID;
	BOOL modal;
	BOOL saveBehind;
	int helpId;
	int defaultBtnId;
	int menuId;
	int numRows;
	int numColumns;
	int rgdxcol[64];
	int value;	/* scrollbar */
	int minValue; /* scrollbar */
	int maxValue; /* scrollbar */
	int pageSize; /* scrollbar */
	} ITM;

/* Item Flags */
#define ifText         0x00000001
#define ifMultText     0x00000002
#define ifId           0x00000004
#define ifRc           0x00000008
#define ifPt           0x00000010
#define ifUsable       0x00000020
#define ifAnchor       0x00000040
#define ifFrame        0x00000080
#define ifEnabled      0x00000100
#define ifOn           0x00000200
#define ifEditable     0x00000400
#define ifSingleLine   0x00000800
#define ifDynamicSize  0x00001000
#define ifMaxChars     0x00002000
#define ifCvis         0x00004000
#define ifGroup        0x00008000
#define ifFont         0x00010000
#define ifAlign        0x00020000
#define ifUnderlined   0x00040000
#define ifListId       0x00080000
#define ifBitmap       0x00100000

/* Form ifs */
#define ifModal        0x00200000
#define ifSaveBehind   0x00400000
#define ifHelpId       0x00800000
#define ifDefaultBtnId 0x01000000
#define ifMenuId       0x02000000

/* Ifs defining margins -- extra width to add to an item in addition to it's string width */
#define ifSmallMargin  0x80000000
#define ifBigMargin    0x40000000

/* if2s -- ran out of bits in if! */
#define if2Null        0x00000000
#define if2NumColumns  0x00000001
#define if2NumRows     0x00000002
#define if2ColumnWidths 0x00000004
#define if2Value        0x00000008
#define if2MinValue     0x00000010
#define if2MaxValue     0x00000020
#define if2PageSize     0x00000040
#define if2AutoShift    0x00000080
#define if2Scrollbar    0x00000100
#define if2Numeric      0x00000200


/* Semi-arbitrary margins */
#define dxObjSmallMargin 3
#define dxObjBigMargin 6


/*-----------------------------------------------------------------------------
|	WResolveK
|	
|		Resolve a Konstant to it's real value, returning it.
-------------------------------------------------------------WESC------------*/
int WResolveK(K *pk, ITM *pitm, int dxyExtent, BOOL fHoriz)
	{
	int wVal;
	int dxyCenterAcross;

	switch (pk->kt)
		{
	default:
		Assert(fFalse);
		return 0;
	case ktConst:
		return pk->wVal;
	case ktAuto:
		if (fHoriz)
			{
			wVal = DxCalcExtent((unsigned char *)pitm->text, pitm->font);
			if (pitm->grif & ifSmallMargin)
				wVal += 2*dxObjSmallMargin; 
			if (pitm->grif & ifBigMargin)
				wVal += 2*dxObjBigMargin; 
			}
		else
			wVal = DyFont(pitm->font)+1;	
		return wVal;
	case ktCenter:
		if (fHoriz)
			dxyCenterAcross = vpfrm->form.window.windowBounds.extent.x;
		else
			dxyCenterAcross = vpfrm->form.window.windowBounds.extent.y;
		wVal = (dxyCenterAcross-dxyExtent)/2;
		return wVal;
	case ktCenterAt:
		dxyCenterAcross = pk->wVal;
		wVal = (dxyCenterAcross-dxyExtent)/2;
		return wVal;
	case ktRightAt:
	case ktBottomAt:
		wVal = pk->wVal-dxyExtent;
		return wVal;
		}
	Assert(fFalse);
	}

/*-----------------------------------------------------------------------------
|	ResolveKrcKpt
|	
|		Resolve a Krc and/or Kpt, setting rcPrev
-------------------------------------------------------------WESC------------*/
VOID ResolveKrcKpt(ITM *pitm)
	{
	if (pitm->grif & ifRc)
		{
		pitm->rc.extent.x = WResolveK(&pitm->krc.kptExtent.kX, pitm, 0, fTrue);
		pitm->rc.extent.y = WResolveK(&pitm->krc.kptExtent.kY, pitm, 0, fFalse);
		
		pitm->rc.topLeft.x = WResolveK(&pitm->krc.kptUpperLeft.kX, pitm, pitm->rc.extent.x, fTrue);
		pitm->rc.topLeft.y = WResolveK(&pitm->krc.kptUpperLeft.kY, pitm, pitm->rc.extent.y, fFalse);
		rcPrev = pitm->rc;
		}

	if (pitm->grif & ifPt)
		{
		K kT;
		int xT;
		int yT;

		kT.kt = ktAuto;
		xT = WResolveK(&kT, pitm, 0, fTrue);
		yT = WResolveK(&kT, pitm, 0, fFalse);

		pitm->pt.x = WResolveK(&pitm->kpt.kX, pitm, xT, fTrue);
		pitm->pt.y = WResolveK(&pitm->kpt.kY, pitm, yT, fFalse);
		rcPrev.topLeft = pitm->pt;
		rcPrev.extent.x = xT;
		rcPrev.extent.y = yT;
		}
	}


/*-----------------------------------------------------------------------------
|	DoCheckGrif
|	
| Check if ifP is set in grif -- error if not
-------------------------------------------------------------WESC------------*/
VOID DoCheckGrif(int grif, int ifP)
	{
	if (!(grif & ifP))
		{
		ErrorLine2(tok.lex.szId, "unexpected");
		}
	}

/*-----------------------------------------------------------------------------
|	WGetId
|	
|		Parse ID <const>
-------------------------------------------------------------WESC------------*/
int WGetId(char *szErr, BOOL fAutoIDOk)
	{
	int w;

	fAutoIDOk = fAutoIDOk; /* shut up whiney compilers */
	if (!FGetTok(&tok))
		return 0;
	if (tok.rw == rwAutoId)
		w = IdGetAutoId();
	else
		{
		if (tok.rw != rwId)
			UngetTok();
		w = WGetConstEx(szErr);
		}
	if (w >= idPalmOSReservedMin)
		WarningLine("ID conflicts with PalmOS reserved ID range (valid values: 0-9999, Ok for predefined IDs for Edit menu)");
	return w;
	}

#define CheckGrif(ifP) do {DoCheckGrif(pitm->grif, ifP); pitm->grifOut |= ifP;} while (0);
#define CheckGrif2(ifP) do {DoCheckGrif(pitm->grif2, ifP); pitm->grif2Out |= ifP;} while (0);
/*-----------------------------------------------------------------------------
|	ParseItm
|	
|		Parse an item in a form
|	Arguments:
|		ITM *pitm
|		int grif : valid attributes for this item
|		int grif2: more valid attributes for this item
-------------------------------------------------------------WESC------------*/
void ParseItm(ITM *pitm, int grif, int grif2)
	{
	BOOL fAt;
	int icol;

	memset(pitm, 0, sizeof(ITM));
	
	pitm->grif = grif;
	pitm->grif2 = grif2;
	/* defaults */
	pitm->usable = 1;
	pitm->frame = 1;
	pitm->enabled = 1;
	pitm->justification = leftAlign;
	pitm->singleLine = 1;
	pitm->editable = 1;
	pitm->cvis = -1;
	pitm->saveBehind = 1;
	
	if (grif & ifText)
		{
		pitm->grifOut |= ifText;
		pitm->text = PchGetSz("item string");
		pitm->cbText = strlen(pitm->text)+1;
		}
	if (grif & ifMultText)
		{
		char *pch;
		char rgb[16384];

		pitm->grifOut |= ifMultText;
		GetExpectLt(&tok, ltStr, "text");
		pch = rgb;
		for(;;)
			{
			strcpy(pch, tok.lex.szId);
			pch = pch+strlen(tok.lex.szId)+1;
			pitm->numItems++;
			if (!FGetTok(&tok))
				return;
			if (tok.lex.lt != ltStr)
				break;
			}
		pitm->text = malloc(pch-rgb);
		pitm->cbText = pch-rgb;
		memcpy(pitm->text, rgb, pch-rgb);
		UngetTok();
		}

	if (grif & ifId)
		{
		pitm->grifOut |= ifId;
		pitm->id = WGetId("ItemId", fTrue);
		}
	if (grif & ifListId)
		{
		pitm->grifOut |= ifListId;
		pitm->listid = WGetConstEx("ListId");
		}

	fAt = fFalse;
	if (grif & (ifRc|ifPt))
		{
		if (!FGetTok(&tok))
			return;
		if (tok.rw == rwAt)
			{
			fAt = fTrue;
			GetExpectLt(&tok, ltLParen, "(");
			}
		else
			UngetTok();
		}
	if (grif & ifRc)
		{
		pitm->grifOut |= ifRc;
		ParseKrc(&pitm->krc);
		}
	if (grif & ifPt)
		{
		pitm->grifOut |= ifPt;
		ParseKpt(&pitm->kpt);
		}
	if (fAt)
		GetExpectLt(&tok, ltRParen, ")");
	
	while (FGetTok(&tok))
		{
		switch (tok.rw)
			{
		default:
			ResolveKrcKpt(pitm);
			UngetTok();
			return;
		case rwUsable:								
		case rwNonUsable:
			CheckGrif(ifUsable);
			pitm->usable = tok.rw == rwUsable;
			break;
		case rwLeftAnchor:	/* default? */
		case rwRightAnchor:
			CheckGrif(ifAnchor);
			pitm->leftAnchor = tok.rw == rwLeftAnchor;
			break;
		case rwGroup:
			CheckGrif(ifGroup);
			pitm->group = WGetConstEx("GroupId"); 
			break;
		case rwFont:	/* Add rwBoldFont, rwLargeFont? */
			CheckGrif(ifFont);
			pitm->font = WGetConstEx("FontId");
			break;
		case rwFrame:					/* BUG! other frame types */
		case rwNoFrame:
			CheckGrif(ifFrame);
			pitm->frame = tok.rw == rwFrame;
			break;
		case rwBoldFrame:
			CheckGrif(ifFrame);
			pitm->frame = boldButtonFrame;
			break;
		case rwDisabled:
		case rwEnabled:	
			CheckGrif(ifEnabled);
			pitm->enabled = tok.rw == rwEnabled;
			break;
		case rwChecked:
			CheckGrif(ifOn);
			pitm->on = fTrue;
			break;
		case rwEditable:
		case rwNonEditable:
			CheckGrif(ifEditable);
			pitm->editable = tok.rw == rwEditable;
			break;
		case rwSingleLine:
		case rwMultipleLines:
			CheckGrif(ifSingleLine);
			pitm->singleLine = tok.rw == rwSingleLine;
			break;
		case rwDynamicSize:
			CheckGrif(ifDynamicSize);
			pitm->dynamicSize = 1;
			break;
		case rwMaxChars:
			CheckGrif(ifMaxChars);
			pitm->maxChars = WGetConstEx("MaxChars");
			break;
		case rwLeftAlign:	/* default? */
			CheckGrif(ifAlign);
			pitm->justification = leftAlign;
			break;
		case rwRightAlign:
			CheckGrif(ifAlign);
			pitm->justification = rightAlign;
			break;
		case rwUnderlined:
			CheckGrif(ifUnderlined);
			pitm->underlined = 1;	/* WES! try 2 & 3! */
			break;
		case rwVisibleItems:
			CheckGrif(ifCvis);
			pitm->cvis = WGetConstEx("VisibleItems");
			break;
		case rwValue:
			CheckGrif2(if2Value);
			pitm->value = WGetConstEx("Value");
			break;
		case rwMinValue:
			CheckGrif2(if2MinValue);
			pitm->minValue = WGetConstEx("MinValue");
			break;
		case rwMaxValue:
			CheckGrif2(if2MaxValue);
			pitm->maxValue = WGetConstEx("MaxValue");
			break;
		case rwPageSize:
			CheckGrif2(if2PageSize);
			pitm->pageSize = WGetConstEx("PageSize");
			break;

		case rwBitmap:
			CheckGrif(ifBitmap);
			pitm->rscID = WGetConstEx("BitmapId");
			break;
		case rwModal:
			CheckGrif(ifModal);
			pitm->modal = 1;
			break;
		case rwSaveBehind:
			CheckGrif(ifSaveBehind);
			pitm->saveBehind = 1;
			break;
		case rwNoSaveBehind:
			CheckGrif(ifSaveBehind);
			pitm->saveBehind = 1;
			break;
		case rwHelpId:
			CheckGrif(ifHelpId);
			pitm->helpId = WGetConstEx("HelpId");
			break;
		case rwDefaultBtnId:
			CheckGrif(ifDefaultBtnId);
			pitm->defaultBtnId = WGetConstEx("DefaultButtonId");
			break;
		case rwMenuId:
			CheckGrif(ifDefaultBtnId);
			pitm->menuId = WGetConstEx("MenuId");
			break;
		case rwNumColumns:
			CheckGrif2(if2NumColumns);
			pitm->numColumns = WGetConstEx("NumColumns");
			break;
		case rwNumRows:
			CheckGrif2(if2NumRows);
			pitm->numRows = WGetConstEx("NumRows");
			break;
		case rwColumnWidths:
			CheckGrif2(if2ColumnWidths);
			for (icol = 0; icol < pitm->numColumns; icol++)
				pitm->rgdxcol[icol] = WGetConstEx("ColumnWidth"); 
			break;
		case rwAutoShift:
			CheckGrif2(if2AutoShift);
			pitm->autoShift = 1;
			break;
//		case rwSCL:
			//CheckGrif2(if2Scrollbar);
			//pitm->hasScrollBar = 1;
			//break;
		case rwNumeric:
			CheckGrif2(if2Numeric);
			pitm->numeric = 1;
			break;
			}
		}			
	}
		

#define CondEmitB(b) do {if (fEmit) EmitB(b);} while (0)
#define CondEmitW(w) do {if (fEmit) EmitW(w);} while (0)
#define CondEmitL(l) do {if (fEmit) EmitL(l);} while (0)

/*-----------------------------------------------------------------------------
|	CbEmitStruct
|	
|		Emit a struct (RC*).  It is assumed that the members of the struct
|	are integers or pointers only.  Further it is assumed that pointers and
|	integers are the same size.
|	
|	Arguments:
|		void *pv : struct to emit
|		char *szPic: format picture -- see pilrc.h for format
|		char **ppchText: if szPic contains a 'p' then return that pointer here
|		BOOL fEmit: Actually emit the bytes
|	
|	Returns:
|		size of the structure
-------------------------------------------------------------WESC------------*/
int CbEmitStruct(void *pv, char *szPic, char **ppchText, BOOL fEmit)
	{
	char *pch;
	int *pi;
	int ibit;
	unsigned char byte;
	int cb;

	if (ppchText != NULL)
		*ppchText = NULL;
	cb = 0;
	ibit = 0;
	byte = 0;
	Assert(sizeof(char *) == sizeof(int));
	pi = (int *)pv;
	for (pch = szPic; *pch != 0;)
		{
		int ch;
		int c;
		BOOL fZero;

		if (*pch == ',')
			{
			pch++;
			continue;
			}
		fZero = *pch == 'z';
		if (fZero)
			pch++;
		ch = *pch++;
		c = 0;
		while (isdigit(*pch))
			{
			c = 10*c + (*pch-'0');
			pch++;
			}
		if (c == 0)
			c = 1;
		switch (ch)
			{
		default:
			Assert(fFalse);
			break;
		case 'b':	/* byte (8 bit) */
			while(c--)
				{
				if (fZero)
					CondEmitB(0);
				else
					{
					CondEmitB((unsigned char) *pi);
					pi++;
					}
				cb += 1;
				}
			break;
		case 'w':	/* word (16 bit) */
			while(c--)
				{
				if (fZero)
					CondEmitW(0);
				else
					{
					CondEmitW((unsigned short) *pi);
					pi++;
					}
				cb += 2;
				}
			break;
		case 'p':	/* text (pointer) */
			Assert(c == 1);
			if (ppchText != NULL)
				*ppchText = *(char **)pi;
			if (!fZero)
				pi++;
			fZero = fTrue;
			/* fall thru */
		case 'l':	/* long (32bit) */
			while(c--)
				{
				if (fZero)
					CondEmitL(0);
				else
					{
					CondEmitL(*pi);
					pi++;
					}
				cb += 4;
				}
			break;
		case 't':	/* bit */
			ibit += c;
			byte = (unsigned char)(byte << c);
			if (!fZero && pi != NULL)
				byte |= *pi++;
			if (ibit == 8)
				{
				CondEmitB(byte);
				cb++;
				byte = 0;
				ibit = 0;
				}
			break;
			}
		}
	Assert(ibit == 0);
	return cb;
	}

/*-----------------------------------------------------------------------------
|	CbStruct
|		
|		returns the size of a pilot struct given it's format pic
-------------------------------------------------------------WESC------------*/
int CbStruct(char *szPic)
	{
	return CbEmitStruct(NULL, szPic, NULL, fFalse); 
	}



/* mapping of form object types to resource ids */
char * mpotszEmit[] =
	{
	szRCFIELD,	/* 'tFLD', */
	szRCCONTROL,	/* 'tCTL',	 need to special case this one! */
	szRCLIST,	/* 'tLST', */
	szRCTABLE, /*	szRCTBL'tTBL', */
	szRCFORMBITMAP, /* 'tFBM', */
	NULL, /* 'tLIN',		 bogus */
	NULL, /* 'tFRA',		 bogus */
	NULL, /* 'tREC',      bogus */
	szRCFORMLABEL, /* 'tLBL', */
	szRCFORMTITLE, /* 'tTTL', */
	szRCFORMPOPUP, /* 'tPUL', */
	szRCFORMGRAFFITISTATE, /* 'tGSI', */
	szRCFORMGADGET, /* 'tGDT' */
	szRCSCROLLBAR,  /* 'tSCL' */
	};




/*-----------------------------------------------------------------------------
|	CbFromLt
|	
|		Return emitted size of the form item.  if fText then include the
|	length of the associated item text
-------------------------------------------------------------WESC------------*/
int CbFromLt(RCFORMOBJLIST *plt, int fText)
	{
	int cb;
	RCFORMOBJECT *pobj;
	char *pchText;
	
	cb = 0;
	pobj = &plt->u.object;
	pchText = NULL;
	cb = CbEmitStruct(pobj->ptr, mpotszEmit[plt->objectType], &pchText, fFalse);
	switch (plt->objectType)
		{
	case frmListObj:
		pchText = NULL;
		cb += pobj->list->numItems * 4 + pobj->list->cbListItems;
		break;
	case frmTableObj:
		pchText = NULL;
		cb += pobj->table->numColumns*CbStruct(szRCTABLECOLUMNATTR)+pobj->table->numRows*CbStruct(szRCTABLEROWATTR)+
			pobj->table->numColumns*pobj->table->numRows*CbStruct(szRCTABLEPADDING);
			;
		break;
		}

	if (fText && pchText != NULL)
		cb += strlen(pchText)+1;	
	if (cb & 1)
		cb++;
	return cb;
	}
	


	
/*-----------------------------------------------------------------------------
|	DumpForm
-------------------------------------------------------------WESC------------*/
void DumpForm(FRM *pfrm)
	{
	int cbDirectory;
	int clt;
	int ilt;
	RCFORMOBJLIST lt;
	int ib;
	int il;


	OpenOutput("tFRM", pfrm->form.formId);
	clt = pfrm->form.numObjects;	
	Assert(PlexGetCount(&pfrm->pllt) == clt);

	CbEmitStruct(&pfrm->form, szRCFORM, NULL, fTrue);
    
	cbDirectory = CbEmitStruct(&lt, szRCFORMOBJLIST, NULL, fFalse)*clt;
	ib = IbOut()+cbDirectory;
	for (ilt = 0; ilt < clt; ilt++)
		{
		int cb;
		
		lt = *(RCFORMOBJLIST *)PlexGetElementAt(&pfrm->pllt, ilt);
		cb = CbFromLt(&lt, 1);
		lt.u.ibobj = ib;
		ib += cb;
		CbEmitStruct(&lt, szRCFORMOBJLIST, NULL, fTrue);
		}
	/* now dump the controls themselves		 */
	for (ilt = 0; ilt < clt; ilt++)
		{
		int cbLt;
		char *pchText;						  
		RCFORMOBJECT *pobj;
		RCFORMOBJLIST *plt;
		
		PadWordBoundary();					
		plt = PlexGetElementAt(&pfrm->pllt, ilt);
		
		pobj = &plt->u.object;
		cbLt = CbFromLt(plt, 0);
		pchText = NULL;
		CbEmitStruct(pobj->ptr, mpotszEmit[plt->objectType], &pchText, fTrue);
		switch (plt->objectType)
			{
		case frmListObj:
			pchText = NULL;	/* we dump it ourselves */
			for (il = 0; il < pobj->list->numItems; il++)
				DumpBytes(rgbZero, sizeof(unsigned long));
			DumpBytes(pobj->list->itemsText, pobj->list->cbListItems);
			break;
		case frmTableObj:
			{
			RCTABLECOLUMNATTR colattr;
			RCTABLEROWATTR rwattr;
			int irw;
			int icol;

			memset(&colattr, 0, sizeof(colattr));
			colattr.spacing = 1;	/* ?? */
			for (icol = 0; icol < pobj->table->numColumns; icol++)
				{
				colattr.width = pobj->table->rgdxcol[icol];
				CbEmitStruct(&colattr, szRCTABLECOLUMNATTR, NULL, fTrue);
				}
			memset(&rwattr, 0, sizeof(rwattr));
			rwattr.height = 0x0b;	/* ?? */
			rwattr.usable = 1;
			rwattr.selectable = 1;
			for (irw = 0; irw < pobj->table->numRows; irw++)
				{
				CbEmitStruct(&rwattr, szRCTABLEROWATTR, NULL, fTrue);
				}
			/* emit bogus padding */
			for (irw = 0; irw < pobj->table->numRows*pobj->table->numColumns; irw++)
				CbEmitStruct(NULL, szRCTABLEPADDING, NULL, fTrue);

			break;
			}
			}
		if (pchText != NULL)
			{
			PadWordBoundary();					
			DumpBytes(pchText, strlen(pchText)+1);
			}
		}
	CloseOutput();
	}

BOOL FIdFormObject(FormObjectKind kind, BOOL fIncludeLabels)
	{	
	switch (kind)
		{
	case frmFieldObj:
	case frmControlObj:
	case frmListObj:
	case frmTableObj:
	
	case frmScrollbarObj:
	case frmGadgetObj:
		return fTrue;
	case frmLabelObj:
		return fIncludeLabels;
	case frmTitleObj:
	case frmPopupObj:
	case frmLineObj:
	case frmFrameObj:
	case frmRectangleObj:
	case frmGraffitiStateObj:	
	case frmBitmapObj:
		return fFalse;
		}
	Assert(fFalse);
	return fFalse;
	}		

/* Grrr, the PalmOS developers could have been intelligent created a common header
 for all form objects */
int IdFromObj(RCFORMOBJECT *pobj, FormObjectKind kind)
	{
	switch (kind)
		{
	case frmFieldObj:
		return pobj->field->id;
		
	case frmControlObj:
		return pobj->control->id;
	case frmListObj:
		return pobj->list->id;
	case frmTableObj:
		return pobj->table->id;
	case frmScrollbarObj:
		return pobj->scrollbar->id;
	case frmGadgetObj:
		return pobj->gadget->id;
	case frmLabelObj:
		return pobj->label->id;
		
	case frmTitleObj:
	case frmPopupObj:
	case frmLineObj:
	case frmFrameObj:
	case frmRectangleObj:
	case frmGraffitiStateObj:	
	case frmBitmapObj:
		Assert(fFalse);
		return -1;
		}
	Assert(fFalse);
	return fFalse;
	}
	
/*-----------------------------------------------------------------------------
|	AddObject
|	
|		Add an object (item) to the current form
-------------------------------------------------------------WESC------------*/
VOID AddObject(RCFORMOBJECT *pobj, FormObjectKind kind)
	{
	RCFORMOBJLIST lt;
	int iobj;

//	if (vpfrm->form.numObjects > cobjMax)
		//ErrorLine("Too many objects in current form");
	if (vfCheckDupes && FIdFormObject(kind, fTrue))
		{
		for (iobj = 0; iobj < vpfrm->form.numObjects; iobj++)
			{
			RCFORMOBJLIST *plt;
			plt = PlexGetElementAt(&vpfrm->pllt, iobj);
			if (FIdFormObject(plt->objectType, fTrue) && 
				IdFromObj(pobj, kind) == IdFromObj(&plt->u.object, plt->objectType))
				{
				/* dupe labels are ok */
				if (!(kind == frmLabelObj && plt->objectType == frmLabelObj))
					WarningLine("Duplicate form object id (error is on the previous line)");
				break;
				}
			}
		}
	lt.objectType = kind;	
	lt.u.object = *pobj;
	PlexAddElement(&vpfrm->pllt, &lt);
	vpfrm->form.numObjects++;
	}


/*-----------------------------------------------------------------------------
|	FreeLt
|	
|		Frees stuff pointed to by lt (Doesn't free lt itself)
-------------------------------------------------------------WESC------------*/
VOID FreeLt(RCFORMOBJLIST *plt)
	{
	char *pchText;

	CbEmitStruct(plt->u.object.ptr, mpotszEmit[plt->objectType], &pchText, fFalse);
	if (pchText != NULL)
		free(pchText);
	if (plt->objectType == frmTableObj)
		free(plt->u.object.table->rgdxcol);
	free(plt->u.object.ptr);
	}

/*-----------------------------------------------------------------------------
|	FreeFrm
-------------------------------------------------------------WESC------------*/
void FreeFrm(FRM *pfrm)
	{
	int ilt;

	for (ilt = 0; ilt < PlexGetCount(&pfrm->pllt); ilt++)
		FreeLt(PlexGetElementAt(&pfrm->pllt, ilt));
	PlexFree(&pfrm->pllt);	
	}


void FreeRcpfile(RCPFILE *prcpf)
	{
	int ifrm;

	if (prcpf != NULL)
		{
		for (ifrm = 0; ifrm < PlexGetCount(&prcpf->plfrm); ifrm++)
			FreeFrm((FRM *)PlexGetElementAt(&prcpf->plfrm, ifrm));
		PlexFree(&prcpf->plfrm);
		}
	iidMenuMac = 0;
	iidAlertMac = 0;
	iidStringMac = 0;
	idAutoMac = idAutoInit;
	FreeFontMem();
	}

/*-----------------------------------------------------------------------------
|	FParseObjects
|	
|		Parse the objects (items) in a form
-------------------------------------------------------------WESC------------*/
BOOL FParseObjects()
	{
	RCFORMOBJECT obj;
	RW rwSav;
	ITM itm;
	int icol;
	FormObjectKind fok;
	
	/* add objects to object table until we see a rwEnd */
	while (FGetTok(&tok))
		{
		fok = (FormObjectKind) -1;
		switch(rwSav = tok.rw)
			{
			case rwEnd:
				return fTrue;
			case rwTTL: 	/* Yuck, should just do this in FORM line! */
			  	ParseItm(&itm, ifText, if2Null);
				obj.title = calloc(1, sizeof(RCFORMTITLE));
				obj.title->text = itm.text;
				fok = frmTitleObj;
				break;
			
		 	case rwBTN: /* */
		  		ParseItm(&itm, ifText|ifId|ifRc|ifUsable|ifEnabled|ifFont|ifAnchor|ifFrame|ifBigMargin, if2Null);
				goto Control;
			case rwPBN:	/* pushbutton */
		  		ParseItm(&itm, ifText|ifId|ifRc|ifUsable|ifEnabled|ifFont|ifAnchor|ifGroup|ifSmallMargin, if2Null);
				goto Control;
			case rwCBX:
		  		ParseItm(&itm, ifText|ifId|ifRc|ifUsable|ifEnabled|ifFont|ifAnchor|ifGroup|ifOn|ifBigMargin|ifSmallMargin, if2Null);	/* BUG! need to add checkbox extra! */
				itm.frame = 0;
				goto Control;
			case rwPUT: 
		  		ParseItm(&itm, ifText|ifId|ifRc|ifUsable|ifEnabled|ifFont|ifAnchor|ifBigMargin|ifSmallMargin, if2Null); /* SAME! */
				goto Control;
			case rwSLT:
		  		ParseItm(&itm, ifText|ifId|ifRc|ifUsable|ifEnabled|ifFont|ifAnchor|ifSmallMargin, if2Null);
				goto Control;
			case rwREP: /* repeating control */
		  		ParseItm(&itm, ifText|ifId|ifRc|ifUsable|ifEnabled|ifFrame|ifFont|ifAnchor|ifBigMargin, if2Null);
Control:
				obj.control = calloc(1, sizeof(RCCONTROL));
				obj.control->style = rwSav-rwBTN;
				obj.control->text = itm.text;
				obj.control->bounds = itm.rc;
				obj.control->id = itm.id;
				obj.control->attr.enabled = itm.enabled;
				obj.control->attr.usable = itm.usable;
				obj.control->attr.frame = itm.frame;
				obj.control->attr.leftAnchor = itm.leftAnchor;
				obj.control->group = itm.group; 
				obj.control->font = itm.font;
				obj.control->attr.frame = itm.frame;
				obj.control->attr.on = itm.on;
				fok = frmControlObj;
				break;
			case rwLBL: 
				ParseItm(&itm, ifText|ifId|ifPt|ifFont|ifUsable, if2Null);
				obj.label = calloc(1, sizeof(RCFORMLABEL));
				obj.label->text = itm.text;
				obj.label->id = itm.id;
				obj.label->pos = itm.pt;
				obj.label->attr.usable = itm.usable;
				obj.label->fontID = itm.font;
				fok = frmLabelObj;
				break;
						
			case rwFLD:
				ParseItm(&itm, ifId|ifRc|ifUsable|ifAlign|ifFont|ifEnabled|
					ifUnderlined|ifSingleLine|ifEditable|ifDynamicSize|ifMaxChars, if2AutoShift|if2Scrollbar|if2Numeric);
				obj.field = calloc(1, sizeof(RCFIELD));
				obj.field->id = itm.id;
				obj.field->rect = itm.rc;
				obj.field->attr.editable = itm.editable;
				obj.field->attr.usable = itm.usable;
				
				obj.field->fontID = itm.font;
				obj.field->attr.singleLine = itm.singleLine;
				obj.field->attr.underlined = itm.underlined;
				obj.field->attr.insPtVisible = 1;
				obj.field->attr.justification = itm.justification;
				obj.field->attr.dynamicSize = itm.dynamicSize;
				obj.field->attr.autoShift = itm.autoShift;
				obj.field->attr.hasScrollBar = itm.hasScrollBar;
				obj.field->attr.numeric = itm.numeric;
				obj.field->maxChars = itm.maxChars;
				fok = frmFieldObj;
				break;
			
			case rwPUL:
				ParseItm(&itm, ifId|ifListId, if2Null);
				obj.popup = calloc(1, sizeof(RCFORMPOPUP));
				obj.popup->controlID = itm.id;
				obj.popup->listID = itm.listid;
				fok = frmPopupObj;
				break;
			case rwLST: 
				ParseItm(&itm, ifMultText|ifId|ifRc|ifUsable|ifFont|ifEnabled|ifCvis|ifSmallMargin, if2Null);

				obj.list = calloc(1, sizeof(RCLIST));				
				obj.list->itemsText = itm.text;
				obj.list->cbListItems = itm.cbText;
				obj.list->numItems = itm.numItems;				
				obj.list->id = itm.id;
				obj.list->bounds = itm.rc;
				obj.list->attr.usable = itm.usable;
				obj.list->font = itm.font;
				obj.list->attr.enabled = itm.enabled;	   
				if (itm.cvis != -1)
					{
					obj.list->bounds.extent.y = (obj.list->font == largeFont ? 14 : 11)*itm.cvis;
					rcPrev.extent.y = obj.list->bounds.extent.y;
					}
				fok = frmListObj;
				break;
				
			case rwGSI:
 				ParseItm(&itm, ifPt, if2Null);
				obj.grfState = calloc(1, sizeof(RCFORMGRAFFITISTATE));
				obj.grfState->pos = itm.pt;
				fok = frmGraffitiStateObj;
				break;

			case rwGDT:
				ParseItm(&itm, ifId|ifRc|ifUsable, if2Null);
				obj.gadget = calloc(1, sizeof(RCFORMGADGET));
				obj.gadget->id = itm.id;
				obj.gadget->rect = itm.rc;
				obj.gadget->attr.usable = itm.usable;
				fok = frmGadgetObj;
				break;				

			case rwFBM: 
				ParseItm(&itm, ifPt|ifUsable|ifBitmap, if2Null);
				obj.bitmap = calloc(1, sizeof(RCFORMBITMAP));
				/*obj.bitmap->id = itm.id; */
				obj.bitmap->pos = itm.pt;
				obj.bitmap->attr.usable = itm.usable;
				obj.bitmap->rscID = itm.rscID;
				fok = frmBitmapObj;
				break;
			case rwTBL:
				ParseItm(&itm, ifId|ifRc|ifEditable, if2NumColumns|if2NumRows|if2ColumnWidths);
				obj.table = calloc(1, sizeof(RCTABLE));
				obj.table->id = itm.id;
				obj.table->bounds = itm.rc;
				obj.table->attr.editable = itm.editable;
				obj.table->numColumns = itm.numColumns;
				obj.table->numRows = itm.numRows;
				obj.table->rgdxcol = calloc(obj.table->numColumns, sizeof(int));
				for (icol = 0; icol < obj.table->numColumns; icol++)
					obj.table->rgdxcol[icol] = itm.rgdxcol[icol];
				fok = frmTableObj;
				break;
			case rwSCL:
				ParseItm(&itm, ifId|ifRc|ifUsable, if2Value|if2MinValue|if2MaxValue|if2PageSize);
				obj.scrollbar = calloc(1, sizeof(RCSCROLLBAR));
				obj.scrollbar->id = itm.id;
				obj.scrollbar->bounds = itm.rc;
				if (itm.rc.extent.x != 7)
					WarningLine("Scrollbar width not the recommended 7");
				obj.scrollbar->attr.usable = itm.usable;
				obj.scrollbar->value = itm.value;
				obj.scrollbar->minValue = itm.minValue;
				obj.scrollbar->maxValue = itm.maxValue;
				obj.scrollbar->pageSize = itm.pageSize;
				fok = frmScrollbarObj;
				break;
			default:
				 ErrorLine2("Unknown token:", tok.lex.szId);
				 break;
			}
		if (fok != (FormObjectKind) -1)
			AddObject(&obj, fok);			
		}
	return fFalse;	
	}	


void InitFrm(FRM *pfrm)
	{
	PlexInit(&pfrm->pllt, sizeof(RCFORMOBJLIST), 10, 10);
	}
/*-----------------------------------------------------------------------------
|	FParseForm
-------------------------------------------------------------WESC------------*/
BOOL FParseForm(RCPFILE *prcpf)
	{
	ITM itm;
	BOOL f;
	FRM frm;

	memset(&frm, 0, sizeof(FRM));
	InitFrm(&frm);
	vpfrm = &frm;
//	memset(&form, 0, sizeof(RCFORM));
//	memset(rglt, 0, cobjMax*sizeof(RCFORMOBJLIST));

	vpfrm->form.window.windowFlags.focusable = 1;
	ParseItm(&itm, ifId|ifRc|ifUsable|ifFrame|ifModal|ifSaveBehind|ifHelpId|ifDefaultBtnId|ifMenuId, if2Null);
	vpfrm->form.formId = itm.id;
	vpfrm->form.window.windowBounds = itm.rc;
	vpfrm->form.window.frameType.width = itm.frame;
	if (itm.modal)
		{
		vpfrm->form.window.windowFlags.modal = fTrue;
		vpfrm->form.window.windowFlags.dialog = fTrue;
		/* vpfrm->form.window.frameType.word = dialogFrame;		 Need to parse this!!! */
		vpfrm->form.window.frameType.cornerDiam = 3;
		vpfrm->form.window.frameType.width = 2;
		}
	vpfrm->form.attr.saveBehind = itm.saveBehind;
	/*vpfrm->form.attr.saveBehind = 1;*/
	vpfrm->form.attr.usable = itm.usable;
	vpfrm->form.attr.enabled = itm.enabled;
	vpfrm->form.helpRscId = itm.helpId;
	vpfrm->form.defaultButton = itm.defaultBtnId;
	vpfrm->form.menuRscId = itm.menuId;

	GetExpectRw(rwBegin);
	f = FParseObjects();
//	if (ifrmMac > ifrmMax)
//		Error("Too many forms!");
	if (vfCheckDupes)
		{
		int ifrm;

		for (ifrm = 0; ifrm < PlexGetCount(&prcpf->plfrm); ifrm++)
			{
			FRM *pfrm;
			pfrm = (FRM *)PlexGetElementAt(&prcpf->plfrm, ifrm);
			if (pfrm->form.formId == vpfrm->form.formId)
				{
				ErrorLine("Duplicate Form Resource IDs");
				break;
				}
			}
		}
//	frm.rglt = calloc(sizeof(RCFORMOBJLIST), vpfrm->form.numObjects);
	//memcpy(frm.rglt, rglt, sizeof(RCFORMOBJLIST) * vpfrm->form.numObjects);
	
	return PlexAddElement(&prcpf->plfrm, &frm);
//	rgfrm[ifrmMac].form = form;
//	rgfrm[ifrmMac].rglt = calloc(sizeof(RCFORMOBJLIST), vpfrm->form.numObjects);
//	memcpy(rgfrm[ifrmMac].rglt, rglt, sizeof(RCFORMOBJLIST) * vpfrm->form.numObjects);
	/* clone pointers?  yuck */
//	ifrmMac++;
	return f;
	}
										



/*-----------------------------------------------------------------------------
|	DumpMenu
|	
| This could be done more efficiently by emitting the strings differently, but I want to make the
| emitted bytes the same as the USR tools.
-------------------------------------------------------------WESC------------*/
VOID DumpMenu()
	{
	int cmpd;
	int impd;
	int imi;
	int ibCommands;
	int ibStrings;
	
	OpenOutput("MBAR", idMenu);
	cmpd = menu.numMenus;
	CbEmitStruct(&menu, szRCMENUBAR, NULL, fTrue);
	imi = 0;
	ibCommands = CbStruct(szRCMENUBAR)+cmpd*CbStruct(szRCMENUPULLDOWN);
	Assert(!(ibCommands & 1));
	ibStrings = ibCommands + imiMac*CbStruct(szRCMENUITEM);
	Assert(!(ibStrings & 1));
	for (impd = 0; impd < cmpd; impd++)
		{
		int imiT;
		RCMENUPULLDOWN mpd;
		
		mpd = rgmpd[impd];
		mpd.title = (char *) ibStrings;
		mpd.items = (RCMENUITEM *) ibCommands;
		CbEmitStruct(&mpd, szRCMENUPULLDOWN, NULL, fTrue);

		ibCommands += CbStruct(szRCMENUITEM)*rgmpd[impd].numItems;
		ibStrings += strlen(rgmpd[impd].title)+1;
		/* word align? */
		for (imiT = 0; imiT < rgmpd[impd].numItems; imiT++)
			{
			ibStrings += strlen(rgmi[imi].itemStr)+1;
			imi++;
			}
		}
	Assert(imi == imiMac);
	/* emit menuitems */
	ibCommands = CbStruct(szRCMENUBAR)+cmpd*CbStruct(szRCMENUPULLDOWN);
	Assert(!(ibCommands & 1));
	ibStrings = ibCommands + imiMac*CbStruct(szRCMENUITEM);
	Assert(!(ibStrings & 1));
	imi = 0;	
	for (impd = 0; impd < cmpd; impd++)
		{
		int imiT;
		
		ibStrings += strlen(rgmpd[impd].title)+1;
		/* word align? */
		for (imiT = 0; imiT < rgmpd[impd].numItems; imiT++)
			{
			RCMENUITEM mi;
			
			mi = rgmi[imi];
			mi.itemStr = (char *) ibStrings;
			CbEmitStruct(&mi, szRCMENUITEM, NULL, fTrue);
			ibStrings += strlen(rgmi[imi].itemStr)+1;
			imi++;
			}
		}
	/* now finally emit the dang strings		 */
	imi = 0;		
	for (impd = 0; impd < cmpd; impd++)
		{
		int imiT;

		DumpBytes(rgmpd[impd].title, strlen(rgmpd[impd].title)+1);
		/* word align? */
		for (imiT = 0; imiT < rgmpd[impd].numItems; imiT++)
			{
			DumpBytes(rgmi[imi].itemStr, strlen(rgmi[imi].itemStr)+1);
			imi++;
			}
		}
	CloseOutput();
	
	}

	
/*-----------------------------------------------------------------------------
|	AssignMenuRects
|	
|		Compute the menu screen rect
-------------------------------------------------------------WESC------------*/
VOID AssignMenuRects()
	{
	int cmpd;
	int impd;
	int imi;
	int xTitle;
	
	cmpd = menu.numMenus;
	imi = 0;
	xTitle = 4;
	for (impd = 0; impd < cmpd; impd++)
		{
		int imiT;
		int dxMac;
		int dx;
		RCMENUPULLDOWN *pmpd;
		
		pmpd = &rgmpd[impd];
		dx = DxCalcExtent((unsigned char *)pmpd->title, fTrue)+9;
		pmpd->titleBounds.topLeft.x = xTitle;
		pmpd->titleBounds.topLeft.y = 0;
		pmpd->titleBounds.extent.y = 12;
		xTitle += pmpd->titleBounds.extent.x = dx;

		pmpd->bounds.topLeft.y = 14;
		pmpd->bounds.extent.y = 0;
		dxMac = 0;
		for (imiT = 0; imiT < rgmpd[impd].numItems; imiT++)
			{
			int dxT;

			dxT = DxCalcExtent((unsigned char *)rgmi[imi].itemStr, fTrue)+8;
			if (rgmi[imi].command != 0)
				{
				char szT[2];
				dxT += 6 + 12;
				szT[0] = (char) rgmi[imi].command;
				szT[1] = 0;
				dxT += DxCalcExtent((unsigned char *)szT, fTrue);
				}
			dxMac = WMax(dxMac, dxT);
			if (strcmp(rgmi[imi].itemStr, "-") == 0)
				pmpd->bounds.extent.y += 5;
			else
				pmpd->bounds.extent.y += 11;
			imi++;
			}
   		pmpd->bounds.extent.x = dxMac;
		pmpd->bounds.topLeft.x = WMin(pmpd->titleBounds.topLeft.x + 2, dxScreen-dxMac-2);		
		}
	
	}	

/*-----------------------------------------------------------------------------
|	FParsePullDown
-------------------------------------------------------------WESC------------*/
BOOL FParsePullDown()
	{
	RCMENUPULLDOWN mpd;
	RCMENUITEM mi;
	
	memset(&mpd, 0, sizeof(RCMENUPULLDOWN));
	mpd.title = PchGetSz("Popup title");
	GetExpectRw(rwBegin);
	while (FGetTok(&tok))
		{
	 	switch (tok.rw)
			{
		default:
			ErrorLine("END or MENUITEM expected");
			break;
		case rwMenuItem:
			memset(&mi, 0, sizeof(RCMENUITEM));
			if (!FGetTok(&tok))
				return fFalse;
			if (tok.rw == rwSeparator)
				{
				mi.itemStr = strdup("-");
				/*mi.id = 999; */
				}
			else
				{
				int cch;

				UngetTok();
				mi.itemStr = PchGetSz("Item Text");
				cch = strlen(mi.itemStr);
				/* pilot has a special ... character */
				if (cch >= 3 && strcmp(&mi.itemStr[cch-3], "...") == 0)
					{
					mi.itemStr[cch-3] = (char) 0x85;
					mi.itemStr[cch-2] = 0;
					}
				mi.id = WGetId("CommandId", fFalse);			
				if (FGetTok(&tok))
					{
					if (tok.rw == rwNil && tok.lex.lt == ltStr)
						{
						if (strlen(tok.lex.szId) != 1)
							ErrorLine("CommandKey must be 1 character");
						mi.command = toupper(tok.lex.szId[0]);
						}				
					else
						UngetTok();
					}
				if (vfCheckDupes)
					{
					int imi;

					for (imi = 0; imi < imiMac; imi++)
						{
						if (strcmp(rgmi[imi].itemStr, "-") != 0)
							{
							if (mi.id == rgmi[imi].id)
								{
								WarningLine("Duplicate menu item id");
								break;
								}
							}
						}
					}
				}				

			if (imiMac > imiMax)
				ErrorLine("Too many menu items");
			rgmi[imiMac++] = mi;
			mpd.numItems++;				
			break;
		case rwEnd:
			if (menu.numMenus > impdMax)
				ErrorLine("Too many menus");
			rgmpd[menu.numMenus++] = mpd;
			return fTrue; 		
			}
		}
	return fTrue;
	}
	
	
/*-----------------------------------------------------------------------------
|	FParseMenu
-------------------------------------------------------------WESC------------*/
BOOL FParseMenu()
	{
	/* init menu globals */
	memset(&menu, 0, sizeof(RCMENUBAR));
	imiMac = 0;

	idMenu = WGetId("MenuId", fFalse);

	if (vfCheckDupes)
		{
		int iid;

		for (iid = 0; iid < iidMenuMac; iid++)
			{
			if (rgidMenu[iid] == idMenu)
				{
				ErrorLine("Duplicate Menu Resource ID");
				}
			}
		}

	rgidMenu[iidMenuMac++] = idMenu;

	menu.curItem = -1;	/* all resource I've seen have this set */
	GetExpectRw(rwBegin);
	while (FGetTok(&tok))
		{
		switch (tok.rw)
			{
		default:
			ErrorLine2("Unknown identifier:", tok.lex.szId);
			break;			
		case rwPullDown:
			FParsePullDown();
			break;
		case rwEnd:
			return fTrue;
			break;			
			}			
		}
	return fFalse;		
	}				

/*-----------------------------------------------------------------------------
|	FreeMenu
-------------------------------------------------------------WESC------------*/
void FreeMenu()
	{
	int impd;
	int imi;

	for (impd = 0; impd < menu.numMenus; impd++)
		{
		RCMENUPULLDOWN *pmpd;

		pmpd = &rgmpd[impd];
		free(pmpd->title);
		}
	for (imi = 0; imi < imiMac; imi++)
		{
		free(rgmi[imi].itemStr);
		}
	memset(&menu, 0, sizeof(RCMENUBAR));
	imiMac = 0;
	}

/*-----------------------------------------------------------------------------
|	ParseDumpAlert
-------------------------------------------------------------WESC------------*/
void ParseDumpAlert()
	{
	ITM itm;
	RCALERTTEMPLATE at;
	int idAlert;
	char *pchTitle;
	char *pchMessage;

	memset(&at, 0, sizeof(at));
	memset(&itm, 0, sizeof(itm));
	pchTitle = rgbZero;
	pchMessage = rgbZero;
	idAlert = WGetId("AlertId", fFalse);

	if (vfCheckDupes)
		{
		int iid;

		for (iid = 0; iid < iidAlertMac; iid++)
			{
			if (rgidAlert[iid] == idAlert)
				{
				ErrorLine("Duplicate Alert Resource ID");
				}
			}
		}
	if (iidAlertMac < iidAlertMax)
		rgidAlert[iidAlertMac++] = idAlert;

	
	while (FGetTok(&tok))
		{
		switch (tok.rw)
			{
		default:
			ErrorLine("Unexpected end of file");
			break;
		case rwInformation:
		case rwConfirmation:
		case rwWarning:
		case rwError:
			at.alertType = tok.rw-rwInformation;
			break;
		case rwHelpId:
			at.helpRscID = WGetConstEx("HelpId");
			break;
 		case rwDefaultBtn:
 			at.defaultButton = WGetConstEx("DefaultButton");
 			break;
		case rwBegin:
			while (FGetTok(&tok))
				{
				switch(tok.rw)
					{
				default:
					ErrorLine("END expected");
				case rwEnd:
					goto WriteAlert;
				case rwTTL:
					pchTitle = PchGetSz("Title Text");
					break;
				case rwMessage:
					pchMessage = PchGetSzMultiLine("Message Text");
					break;
				case rwBTN:
				case rwButtons:	/* button */
					ParseItm(&itm, ifMultText, if2Null);
					break;
					}
				}
			break;
			}
		}
WriteAlert:
	at.numButtons = itm.numItems;
 	if (at.numButtons > 0 && at.defaultButton >= at.numButtons)
 		ErrorLine("Invalid default button number");

	OpenOutput("Talt", idAlert);
	CbEmitStruct(&at, szRCALERTTEMPLATE, NULL, fTrue);
	DumpBytes(pchTitle, strlen(pchTitle)+1);
	DumpBytes(pchMessage, strlen(pchMessage)+1);
	if (itm.text != NULL)
		DumpBytes(itm.text, itm.cbText);
	CloseOutput();
	if (pchTitle != rgbZero)
		free(pchTitle);
	if (pchMessage != rgbZero)
		free(pchMessage);
	}	

/*-----------------------------------------------------------------------------
|	ParseDumpVersion
-------------------------------------------------------------WESC------------*/
void ParseDumpVersion()
	{
	int id;
	char *pchVersion;

	id = WGetId("Version ResourceId", fFalse);
	pchVersion = PchGetSz("Version Text");
	OpenOutput("tver", id);
	DumpBytes(pchVersion, strlen(pchVersion)+1);
	CloseOutput();
	free(pchVersion);
	}

/*-----------------------------------------------------------------------------
|	ParseDumpString
-------------------------------------------------------------WESC------------*/
void ParseDumpString()
	{
	int id;
	char *pchString;

	id = WGetId("String ResourceId", fFalse);

	if (vfCheckDupes)
		{
		int iid;

		for (iid = 0; iid < iidMenuMac; iid++)
			{
			if (rgidString[iid] == id)
				{
				ErrorLine("Duplicate String Resource ID");
				}
			}
		}
	if (iidStringMac < iidStringMax)
		rgidString[iidStringMac++] = id;

	if (!FGetTok(&tok))
		{
		ErrorLine("String Text or FILE Expected");
		}
		
	if (tok.rw == rwFile)
		{
		FILE *fh;
		int cch;
		
		pchString = malloc(16384);
		GetExpectLt(&tok, ltStr, "String filename");
		fh = fopen(tok.lex.szId, "rt");
		if (fh == NULL)
			ErrorLine2("Unable to open String file ", tok.lex.szId);
		cch = fread(pchString, 1, 16384, fh);				
		if (cch == 16384)
			ErrorLine("String too long!");
		pchString[cch] = 0;
		fclose(fh);		
		}
	else
		{
		UngetTok();
		pchString = PchGetSzMultiLine("String Text");
		}
	OpenOutput("tSTR", id);
	DumpBytes(pchString, strlen(pchString)+1);
	CloseOutput();
	free(pchString);
	}
/*-----------------------------------------------------------------------------
|       ParseDumpCategories
-------------------------------------------------------------JOHNL-----------*/
void ParseDumpCategories()
	{
	int id, len, count;
	char *string;

	id = WGetId("Categories ResourceId", fFalse);

	if (vfCheckDupes)
		{
		int iid;

		for (iid = 0; iid < iidMenuMac; iid++)
			{
			if (rgidString[iid] == id)
				{
				ErrorLine("Duplicate Categories Resource ID");
				}
			}
		}
	if (iidStringMac < iidStringMax)
		rgidString[iidStringMac++] = id;

	OpenOutput("tAIS", id);

	count = 0;
	GetExpectLt(&tok, ltStr, "String Text");
	do
		{
		/* Only read up to maxCategories strings */
		if (count < maxCategories) 
			{
			string = tok.lex.szId;
			len = strlen(tok.lex.szId);
			/* Check the size of the string and only write 15 character max */
			if (len >= categoryLength)
				len = categoryLength - 1;
			DumpBytes(string, len);
			EmitB(0);
			}
		else
			{
			if (count == maxCategories)
				WarningLine("Too many strings in Categories. Extra strings will be ignored");
			}
		count++;

		if (!FGetTok(&tok))
			break;
		} 
	while(tok.lex.lt == ltStr);

	if (tok.lex.lt != ltNil)
	    UngetTok();

	/* The AppInfo category structure expects exactly maxCategories 
         * strings, so write a null byte for any unspecified strings */
	for (; count < maxCategories; count++) 
		EmitB(0);

	CloseOutput();

	}


/*-----------------------------------------------------------------------------
|	ParseDumpBitmapFile
-------------------------------------------------------------DAVE------------*/
void ParseDumpBitmapFile(BOOL fGrey)
	{
	int compress;
	int id;
	char *pchFileName;


	id = WGetId("Bitmap ResourceId", fFalse);
	pchFileName = PchGetSz("Bitmap Filename");

	compress = rwNoCompress;
	if (FGetTok(&tok)) 
		{
		if (tok.rw == rwNoCompress || tok.rw == rwAutoCompress || tok.rw == rwForceCompress) 
			{
			compress = tok.rw;
			}
		else 
			{
			UngetTok();
			}
		}

	OpenOutput("Tbmp", id);
	DumpBitmap( pchFileName, fFalse, compress, fGrey );
	CloseOutput();

	free(pchFileName);
	}

/*-----------------------------------------------------------------------------
|	ParseDumpIcon
-------------------------------------------------------------DAVE------------*/
void ParseDumpIcon(BOOL fSmall)
	{
	char *pchFileName;

	pchFileName = PchGetSz("Icon Filename");

	OpenOutput("tAIB", fSmall ? 1001 : 1000);
	DumpBitmap(pchFileName, fSmall ? 2 : 1, rwNoCompress, fFalse);
	CloseOutput();

	free(pchFileName);
	}


/*-----------------------------------------------------------------------------
|	ParseDumpApplicationIconName
-------------------------------------------------------------WESC------------*/
void ParseDumpApplicationIconName()
	{
	int id;
	char *pchString;

	id = WGetId("Application IconName ResourceId", fFalse);
	pchString = PchGetSz("Icon Name Text");
	OpenOutput("tAIN", id);
	DumpBytes(pchString, strlen(pchString)+1);
	CloseOutput();
	free(pchString);
	}


/*-----------------------------------------------------------------------------
|	ParseDumpApplication
-------------------------------------------------------------WESC------------*/
void ParseDumpApplication()
	{
	int id;
	char *pchString;

	id = WGetId("APPL ResourceId", fFalse);
	pchString = PchGetSz("APPL");
	if (strlen(pchString) != 4)
		ErrorLine("APPL resource must be 4 chars");
	OpenOutput("APPL", id);
	DumpBytes(pchString, 4);
	CloseOutput();
	free(pchString);
	}

/*-----------------------------------------------------------------------------
|	ParseDumpTrap
-------------------------------------------------------------WESC------------*/
void ParseDumpTrap()
	{
	int id;
	int wTrap;

	id = WGetId("TRAP ResourceId", fFalse);
	wTrap = WGetConst("Trap number");
	if (id < 1000)
		ErrorLine("TRAP resource id must be >= 1000, see HackMaster documentation");
	OpenOutput("TRAP", id);
	EmitW((unsigned short)wTrap);
	CloseOutput();
	}


/*-----------------------------------------------------------------------------
|     ParseDumpFont
-------------------------------------------------------------DPT-------------*/
void ParseDumpFont()
	{
	int id;
	int fontid;
	char *pchFileName;
	
	id = WGetId("Font ResourceId", fFalse);
	GetExpectRw(rwFontId);
	fontid = WGetId("FontId", fFalse);
	if (fontid < 128 || fontid > 255)
		ErrorLine("FontID invalid.  valid values: 128<=FontID<=255");
	pchFileName = PchGetSz("Font Filename");
	
	OpenOutput("NFNT", id);
	DumpFont(pchFileName, fontid);
	CloseOutput();
	
	free(pchFileName);
	}

/*-----------------------------------------------------------------------------
|	ParseTranslation
-------------------------------------------------------------WESC------------*/
void ParseTranslation()
	{
	BOOL fAddTranslation;
	char *pch;
	TE *pte;

	GetExpectLt(&tok, ltStr, "Language");
	fAddTranslation = szLanguage != NULL && FSzEqI(tok.lex.szId, szLanguage);
	GetExpectRw(rwBegin);
	while (FGetTok(&tok))
		{
		switch (tok.rw)
			{
		case rwEnd:
			return;
		case rwNil:
			if (tok.lex.lt != ltStr)
				ErrorLine("String Expected");
			if (fAddTranslation)
				{
				pte = malloc(sizeof(TE));
				pte->szOrig = strdup(tok.lex.szId);
				}
			GetExpectLt(&tok, ltAssign, "=");
			pch = PchGetSzMultiLine("Translation");
			if (fAddTranslation)
				{
				pte->szTrans = pch;
				pte->pteNext = pteFirst;
				pteFirst = pte;
				}
			else
				free(pch);
			}
		}
	}

/*-----------------------------------------------------------------------------
|	OpenInputFile
-------------------------------------------------------------WESC------------*/
VOID OpenInputFile(char *szIn)
	{
	extern char szInFile[];

	strcpy( szInFile, FindAndOpenFile( szIn, "rt", &vfhIn ));
	iline = 0;
	}

/*-----------------------------------------------------------------------------
|	ParseCInclude
|	
|		Supports #define foo const and foo equ const
-------------------------------------------------------------WESC------------*/
void ParseCInclude( char *szIncludeFile )
	{
	FILE *fhInSav;
	int ilineSav;
	char szInFileSav[256];
	char szId[256];
	int wIdVal;
	extern char szInFile[];

	/* tok contains filename */

	fhInSav = vfhIn;
	ilineSav = iline;
	strcpy(szInFileSav, szInFile);

	OpenInputFile(szIncludeFile);

	while (FGetTok(&tok))
		{
		switch (tok.lex.lt)
			{
		case ltPound:
			if (!FGetTok(&tok))
				ErrorLine("preprocessor directive expected");
			if (tok.rw != rwDefine)
				{
				NextLine();
				break;
				}
			GetExpectLt(&tok, ltId, "identifier");
			strcpy(szId, tok.lex.szId);
GetVal:
			wIdVal = WGetConstEx("Constant");
			AddSym(szId, wIdVal);
			break;
		case ltId:
			strcpy(szId, tok.lex.szId);
			GetExpectRw(rwEqu);
			goto GetVal;
			break;
		case ltSemi:
		case ltDoubleSlash:
			NextLine();
			break;			
		default:
			ErrorLine("Identifier expected");
			break;
			}
		}	
	fclose(vfhIn);
	strcpy(szInFile, szInFileSav);
	iline = ilineSav;
	vfhIn = fhInSav;	
	}


/*-----------------------------------------------------------------------------
|	ParseJavaInclude
|	
|		Supports a file of the EXACT form:
|
    package <pkgname>;
	
	public class <classname>
	{
		public static final short <idname> = <idnumber>;
	}
-------------------------------------------------------------DAVE------------*/
void ParseJavaInclude( char *szIncludeFile )
	{
	FILE *fhInSav;
	int ilineSav;
	char szInFileSav[256];
	char szId[256];
	int wIdVal;
	extern char szInFile[];

	/* tok contains filename */

	fhInSav = vfhIn;
	ilineSav = iline;
	strcpy(szInFileSav, szInFile);

	OpenInputFile(szIncludeFile);
	
	/* Skip to class xxx { */
	while (FGetTok(&tok) && 
		   (tok.lex.lt != ltLBrace))
		{
		}

	if ( tok.lex.lt != ltLBrace ) 
		{
		ErrorLine("expected class definition in java file");
		}

	while (FGetTok(&tok))
		{
		switch (tok.rw)
			{
			case rwPublic:
			case rwShort:			
			case rwInt:
			case rwStatic:		
			case rwFinal:
				continue;
			}
			
		switch (tok.lex.lt)
			{
		case ltId:
			strcpy(szId, tok.lex.szId);
			GetExpectLt(&tok, ltAssign, "[public] [static] [final] [short|int] Name = number;");

			wIdVal = WGetConstEx("Constant");
			AddSym(szId, wIdVal);
			break;
		case ltSemi:
		case ltDoubleSlash:
			NextLine();
			break;
		case ltRBrace:
			goto endOfClass;
		default:
			ErrorLine("Identifier definition expected");
			break;
			}
		}	

endOfClass:

	fclose(vfhIn);
	strcpy(szInFile, szInFileSav);
	iline = ilineSav;
	vfhIn = fhInSav;	
	}


void WriteIncFile(char *szFile)
	{
	FILE *fh;
	SYM *psym;
	
	fh = fopen(szFile, "wt");
	if (fh == NULL)
		Error3("Unable to open:", szFile, strerror(errno));
	if (!vfQuiet)
		printf("writing include file: %s\n", szFile);
	fprintf(fh, "/* pilrc generated file.  Do not edit!*/\n");
	for (psym = psymFirst; psym != NULL; psym = psym->psymNext)
		{
		if (psym->fAutoId)
			fprintf(fh, "#define %s %d\n", psym->sz, psym->wVal);
		}			
	fclose(fh);
	}

/*-----------------------------------------------------------------------------
|	ParseDirectives
-------------------------------------------------------------DAVE------------*/

void ParseDirectives()
	{
	TOK tok;
	
	FGetTok(&tok);
	switch (tok.rw) 
		{
	case rwInclude:
		{
		char *szFileName;
		char *pchExt;

		GetExpectLt(&tok, ltStr, "include filename");
		szFileName = tok.lex.szId;

		pchExt = strrchr(szFileName,'.');
		if (!pchExt) 
			{
			/* Assume it's a .h file */
			ParseCInclude(szFileName);
			break;
			}

		pchExt++;
		if (FSzEqI(pchExt, "java") || FSzEqI(pchExt, "jav"))
			{
			ParseJavaInclude(szFileName);
			}
		else 
			{
			ParseCInclude(szFileName);
			}
		break;
		}

	/* Add new directives here	*/

	default:
		ErrorLine2("directive not supported (try #include):", tok.lex.szId);
		break;
		}

	}

VOID InitRcpfile(RCPFILE *prcpfile, int fontType)
	{
	PlexInit(&prcpfile->plfrm, sizeof(FRM), 10, 10);
	InitFontMem(fontType);
	}

/*-----------------------------------------------------------------------------
|	ParseFile
-------------------------------------------------------------WESC------------*/
RCPFILE *ParseFile(char *szIn, char *szOutDir, char *szResFile, char *szIncFile, int fontType)
	{
	RCPFILE *prcpfile;
	
	prcpfile = calloc(1, sizeof(RCPFILE));
	InitRcpfile(prcpfile, fontType);
	
	
	SetOutFileDir(szOutDir);
	OpenInputFile(szIn);
	OpenResFile(szResFile);
	FInitLexer(NULL, fTrue);

	if (!FGetTok(&tok))
		Error("bogus source file");
	do
		{
		switch(tok.rw)
			{
		case rwForm:
			FParseForm(prcpfile);
			DumpForm(PlexGetElementAt(&prcpfile->plfrm, PlexGetCount(&prcpfile->plfrm)-1));
			break;
		case rwMenu:
			FParseMenu();
			AssignMenuRects();
			DumpMenu();
			FreeMenu();
			break;
		case rwAlert:
			ParseDumpAlert();
			break;
		case rwVersion:
			ParseDumpVersion();
			break;
		case rwString:
			ParseDumpString();
			break;
		case rwCategories:
			ParseDumpCategories();
			break;
		case rwApplicationIconName:
			ParseDumpApplicationIconName();
			break;
		case rwApplication:
			ParseDumpApplication();
			break;
		case rwTranslation:
			ParseTranslation();
			break;
		case rwBitmap:
		case rwBitmapGrey:
			ParseDumpBitmapFile(tok.rw == rwBitmapGrey);
			break;
		case rwIcon:
			ParseDumpIcon(fFalse);
			break;
		case rwIconSmall:
			ParseDumpIcon(fTrue);
			break;
		case rwTrap:
			ParseDumpTrap();
			break;
		case rwFont:
			ParseDumpFont();
			break;

		/* Add a rw here, remember to add to error message below */
		default:
			if (tok.lex.lt == ltPound)
				{
				ParseDirectives();
				}
			else if (tok.lex.lt == ltDoubleSlash)
				NextLine();
			else
				ErrorLine("FORM, MENU, ALERT, VERSION, STRING, CATEGORIES, APPLICATIONICONNAME, APPLICATION, BITMAP, SMALLICON, ICON, TRAP, FONT or TRANSLATION expected");
			}
		}
	while (FGetTok(&tok));
	fclose(vfhIn);
	if (szIncFile != NULL)
		{
		WriteIncFile(szIncFile);
		}

	FreeSymTable();
	FreeTranslations();
	CloseResFile();
	return prcpfile;
	}