File: binpdb.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (2897 lines) | stat: -rw-r--r-- 91,238 bytes parent folder | download | duplicates (3)
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
/*
    BINPDB.C
    Define Yorick interface to PDB files

    $Id: binpdb.c,v 1.1 1993/08/27 18:32:09 munro Exp $
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#ifdef NOT_YORICK
#include "binio.h"
#else
/* Only problem is that RemoveStruct uses Unref... */
#include "ydata.h"
#endif
#include "yio.h"
#include "defstr.h"
#include "defmem.h"

/* Uses: strtok, strtol, strcspn, strspn, strcmp, strncmp, strchr, strlen */
extern char *Ytimestamp(void);  /* 24 character return */

extern DataLayout bigendLayout[6], sun3Layout[6], cchybridLayout[6],
  ibmpcLayout[6], mac2Layout[7], vaxLayout[7], pdbLayout[6];

extern Converter PDBconvert;

extern int yPDBopen;
int yPDBopen= 010;  /* option bits to alter PDB behavior on open:

		       if either of bits 001 or 002 is set, when a
		       PDB file is opened, its Major-Order: extra
		       may be altered as follows:
		       001  Major-Order:102 always 
		       002  Major-Order:  opposite from what file says
		       003  Major-Order:101 always

		       004  Strip Basis @... suffices from variable names
		       010  Use Basis @history convention on input
		       */

static int yPDBclose;  /* options for closing a PDB file, selected by
			  which close hook (YPDBcloser) is selected as the
			  CloseHook:
			  001  Write Major-Order 102 PDB file
			  002  Write PDB style history data
			       The following are no-ops unless bit 002 is set:
			  004  Use Basis @history convention on output
                          010  Do NOT pack all history record variables into
			       a single structure instance.
			  */

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/*
 * Based on code bearing the following copyright notices:
 *
 * Copyright Stewart A. Brown 1987-1990 (all rights reserved)
 *
 * The U.S. Government, for itself and others acting on its behalf, has
 * been granted a royalty-free, nonexclusive, irrevocable, world-wide
 * license for Governmental purposes to publish, distribute, translate,
 * duplicate, exhibit, and perform this material.
 *
 */

/*--------------------------------------------------------------------------*/

/* Here are the interface routines for opening an existing PDB file
   (possibly with DMIPDB-style history records), and initializing a newly
   created file.  */
extern int YtestPDB(IOStream *file, int familyOK);
extern int YinitPDB(IOStream *file, int close102);

extern void YPDBpointers(IOStream *file, long size, int align);

extern void (*YPDBcloser[16])(IOStream *file);

/* Data type names must be altered slightly:
   Name in PDB file             Name in IOStream
   ----------------             ----------------
   integer                      int
   int                          integer
   "any *"                      "char*"    (for variables -- size==0)
                                "char *"   (for members -- size>0)
 */

extern void YErrorIO(const char *msg);

/*--------------------------------------------------------------------------*/

/* required by PDBconvert to handle pointee headers */
extern char *PDBvarType(char *typeName);

extern void CopyLayouts(IOStream *file, DataLayout *src);

static int DecodePrimitives(IOStream *file,
			    unsigned char *buffer, char *token);
static void SetConverter(StructDef *base, int isReal);
static int DecodeOrder(int *order, unsigned char *permute, long n);
static void DecodeMachine(IOStream *file, long machine);
static void RDextras(IOStream *file, long address);
static void CheckOrderScratch(long size);
static int RDchart(IOStream *file, long address);
static char *FlopIntegerAndInt(char *typeName);
static int RDsymtab(IOStream *file, long address);
static int GrabSymbol(char *dataName, IOStream *file, int goofCount,
		      long offset, char **context);
static void CheckDMIScratch(long maxLength);
static void DMIhistory(IOStream *file, long addrChrt, long address,
		       int familyOK);
static void RemoveStruct(IOStream *file, char *baseName);
static int FindSymtab(IOStream *file, long *addrSymt, long *addrChrt);
static int FindRecords(IOStream *file, long addrSymt,
		       long *addrTimes, long *addrCycles, int style);
static int BurstRecord(IOStream *file, StructDef *base);
static void GrabDMIData(IOStream *file, long firstAddr, long firstLength,
			long numbAddr);
static int DMIcheckSymbol(char *dataName);
static int HasDMIrecords(void);
static int IsDMIvariable(char *dataName);
static long IsDMIrecord(char *dataName);
static char *FindAddresses(IOStream *file, long addrSymt, char *baseName,
			   int style);
static int VerifyRecType(IOStream *file, long addrChrt,
			 char *baseName, long baseSize);

static char *EncodePrimitives(IOStream *file, char *buffer);
static long EncodeOrder(unsigned char *buf, int order, long size);
static int WRchart(IOStream *file);
static int WRstructure(char **names, char *name, long size, long nMembers,
		       char **mNames, Member *members, long *offsets);
static void PutDimList(Dimension *dims, int fastest1st);
static void Put1Dim(Dimension *dims, int firstPass);
static int DoStructName(char *name, long size, int putEnd);
static int WRsymtab(IOStream *file);
static int WRsymbol(char *name, char *typeName, long number, long address,
		    Dimension *dims);
static void Put102Dims(Dimension *dims);
static int WRextras(IOStream *file);
static int WRheader(IOStream *file, long newChart, long newSymtab);
static int WRhistory(HistoryInfo *history);

static int fastestFirst;    /* flag set if fastest dimension first */
static long defaultOrigin;  /* default minimum array index */
static int gotAlignment;    /* flag set if Alignment: extra exists */

static char *memberPointer= "char *";
static char *variablePointer= "char*";

static long mptrSize;
static int mptrAlign;

/* state information for DMI-style record structure in PDB file */
static long firstLength, nSeq, nSeq2, nTimes, nCycles;
static long firstAddr, numbAddr, timeAddr, ncycAddr;

/* PDB header token to uniquely identify as PDB file */
#define HEADTOK "!<<PDB:II>>!"
#define OLDHEADTOK "!<><PDB><>!"

/* PDB recognizes any of 3 possible types of newline character */
#define ANY_NEWLINE "\012\015\037"
#define SOH_OR_NEWLINE "\001\012\015\037"

/* Error returns:
   1  -> PDB header not found (either old or new)
   2  -> unable to read primitive formats
   3  -> unable to read structure defintions
   4  -> unable to read symbol table
   5  -> unable to read extras tags
 */

/* InitBuffer initializes a the state information and allocates a buffer
              - first read will be at specified address
   ClearBuffer deallocates the buffer
   ReadUntil scans until the next occurance of any of the delimiters
             (but at most buffer size characters), replaces it with
	     a '\0', and returns the resulting string
	     \0 bytes in the file do NOT terminate the scan
   ReadBlock advances the reader nBytes bytes for the next ReadBlock or
             ReadUntil (nBytes must not be larger than buffer size)
   CheckBuffer checks whether there are the required number of bytes
             remaining in the buffer -- returns non-0 iff buffer too small,
	     otherwise, may write the buffer and reset nextByte
   nextByte is a pointer into the next word of the buffer which will be
             read by ReadUntil or ReadBlock, or which should be written to
	     after CheckBuffer
   ReadBuffer is an internal routine -- don't call it
   WriteBuffer writes the buffer to disk
   NextWRAddress returns next address to be written in buffer
 */
static void InitBuffer(IOStream *file, long size, long address);
static int ClearBuffer(int value);
static char *ReadUntil(char *delimiters);
static char *ReadNonblank(char *delimiters);
static char *ReadBlock(long nBytes);
static int CheckBuffer(long nRequired);
static char *nextByte;
static int ReadBuffer(void);
static void WriteBuffer(void);
static long NextWRAddress(int afterRead);

/* Stuff to handle new PDB history files and Basis deviations.  */
static HashTable blocksTable;
static long nBlocks, *blocksAddrs;
static void BlocksInit(void);
static int BlocksVerify(void);
static int BlocksLine(char *line);

static int TokenAsLong(char *token, long *result);

/* returns 0 on success, 1 if header indicates not a PDB,
   2 if the file's primitive data types were modified, but the routine
     was unable to read the structure chart and/or symbol table */
int YtestPDB(IOStream *file, int familyOK)
{
  char *token;
  long chartAddress, symtabAddress;
  int i;

  if (!(file->permissions&1)) return 1;

  InitBuffer(file, 280L, 0L);

/*   The total number of bytes in a PDB header is less than:

        43 + sizeof(float) + sizeof(double) +
        2*(number of decimal digits in float or double exponent bias) +
        2*(number of decimal digits in the size of the file in bytes)

     A 10 Gigabyte file with 12-byte floating point values with an
     decimal exponent range of plus or minus 32767 would therefore
     require no more than 97 bytes of PDB header information.
 */

  /* The first dozen bytes of the file should be HEADTOK or OLDHEADTOK */
  token= ReadUntil(ANY_NEWLINE);
  if (!token || !(token= strtok(token, " "))) return ClearBuffer(1);

  if (strcmp(token, HEADTOK) == 0) {
    /* a PDB_SYSTEM_VERSION 2 or later style file */

    /* primitive data type format information begins at next byte
       -- get everything except exponent biases and alignments
       next byte is a byte count which determines the location of the
       ASCII exponent biases -- the intervening bytes contain the
       rest of the primitive data type format information */
    if (!(token= ReadBlock(1L)) ||
	!(token= ReadBlock(token[0]-1L)) ||
	DecodePrimitives(file, (unsigned char *)token,
			 ReadUntil(ANY_NEWLINE))) return ClearBuffer(2);

  } else if (strcmp(token, OLDHEADTOK) == 0) {
    /* a pre-PDB_SYSTEM_VERSION 2 style file */

    /* the second token is the machine type that wrote the file
     * set the file->std for machine type
     * for PD_open the file->std is always the PDBfile standard
     * alignment issues are not properly handled before PDB_SYSTEM_VERSION 3
     * but do the best that we can
     */
    token = strtok(0, " ");
    if (!token) return ClearBuffer(2);

    DecodeMachine(file, strtol(token, (char **)0, 10));

  } else {
    return ClearBuffer(1);  /* bad header */
  }

  /* chart and symtab addresses are stored with "%ld\001%ld\001\n" */
  token= ReadUntil(ANY_NEWLINE);
  if (!token) return ClearBuffer(2);
  if (TokenAsLong(token, &chartAddress)) return ClearBuffer(2);
  if (TokenAsLong(0, &symtabAddress)) return ClearBuffer(2);

  /* An IOStream is born with 8 primitive data types--
     add memberPointer and variablePointer now.  */
  mptrAlign= mptrSize;  /* will be modified when Alignment: read */
  YPDBpointers(file, mptrSize, mptrAlign);

  /* The extras section must be read first, since it contains alignments,
     primitive data types, and dimension list order required in order
     to read both the chart and the symtab.  This is inefficient, since
     the symtab must be scanned to get to the extras, but so be it.
     The variables fastestFirst, gotAlignment, and defaultOrigin are set.
     The Alignment:, Primitive-Types:, Major-Order:, and Offset: extras
     alter the meaning of the chart and symtab.

     While scanning through the symbol table, RDextras notes the presence
     or absence of the DMI-style special record variables FirstRecordNames,
     NumberOfRecords, times, and ncycs.  The first pass through the symbol
     table thereby allows RDsymtab to function more efficiently, since
     the record-related variables can be correctly added to the history
     child, rather than being moved in a later pass.

     RDExtras also builds the blocksTable if the file contains PDB-style
     history data.  If present, and if DMI-style history data is not
     present, RDsymtab uses this blocksTable to identify history data.  */
  BlocksInit();
  yPDBclose= 06;   /* initialize file style here */
  RDextras(file, symtabAddress);
  if (yPDBopen&3) {
    if ((yPDBopen&3)==1) fastestFirst= 1;
    else if ((yPDBopen&3)==3) fastestFirst= 0;
    else if (fastestFirst) fastestFirst= 0;
    else fastestFirst= 1;
  }
  yPDBclose|= fastestFirst;

  /* Now that the extras section has been read, the specification of the
     primitive data types is complete, and a determination of whether
     a converter is required may be made.  */
  for (i=1 ; i<6 ; i++) SetConverter(file->structList[i], i>3);

  /* Read the structure chart.  */
  RDchart(file, chartAddress);

  /* Read the symbol table.  This is the second pass (RDextras was first);
     the variables are actually added here.  */
  RDsymtab(file, symtabAddress);

  file->CloseHook= YPDBcloser[yPDBclose];

  /* Check for DMI-style time history records.
     Revises file dataTable and adds history child if found.
     All files in a family are scanned here.
     Whether or not there are history records, DMIhistory tries to
     set file->nextAddress intelligently...  */
  DMIhistory(file, chartAddress, symtabAddress, familyOK);

  BlocksInit();
  return ClearBuffer(0);
}

/*--------------------------------------------------------------------------*/

int YinitPDB(IOStream *file, int close102)
{
  long headerAddress, headLen, i;
  StructDef *longS= file->structList[3];

  if ((file->permissions&3)!=3) return 1;

  yPDBclose= close102 & 017;

  /* An IOStream is born with 8 primitive data types--
     add memberPointer and variablePointer now.  */
  YPDBpointers(file, longS->size, longS->alignment);

  InitBuffer(file, 512L, 0L);

  sprintf(nextByte, "%s\012", HEADTOK);
  nextByte+= strlen(nextByte);

  nextByte= EncodePrimitives(file, nextByte);

  headerAddress= NextWRAddress(0);

  /* write a dummy chart and symtab address, leaving 128 bytes for
     this header line to use later */
  sprintf(nextByte, "%ld\001%ld\001\012",
	  headerAddress+128, headerAddress+128);
  headLen= strlen(nextByte);
  nextByte+= headLen;
  for (i=headLen ; i<=128 ; i++) *(nextByte++)= '\0';

  WriteBuffer();

  /* force struct-like data alignment to ensure that record data can be
     described as data structures -- this is NOT standard PDBLib practice */
  file->dataAlign= 0;

  file->CloseHook= YPDBcloser[yPDBclose];

  return ClearBuffer(0);
}

void YPDBpointers(IOStream *file, long size, int align)
{
  /* Each IOStream is "born" with 8 primitive data types
     (char, short, int, long, float, double, string, and pointer).
     Add the "char *" and "char*" PDB pointer types now.  */
  StructDef *base;
  if (!HashFind(&file->structTable, memberPointer, 0L)) {
    base= AddStruct(file, memberPointer, 0L);
    DefinePrimitive(base, size, align, 2, 0, (FPLayout *)0,
		    &pointerStruct, &PDBconvert);
    InstallStruct(base, (StructDef *)0);
  }
  if (!HashFind(&file->structTable, variablePointer, 0L)) {
    base= AddStruct(file, variablePointer, 0L);
    DefinePrimitive(base, 0L, 1, 2, 0, (FPLayout *)0,
		    &pointerStruct, &PDBconvert);
    InstallStruct(base, (StructDef *)0);
  }
}

/*--------------------------------------------------------------------------*/

/* YclosePDB is meant to be installed as the IOStream CloseHook
   for a PDB file.  It must write the PDB structure chart, symbol table,
   and extras section to the file.  Additionally, for a time history file,
   the time history variables must be created and written before the
   chart, symtab, and extras.  */

static void ClosePDB(IOStream *file);

static void Y00close(IOStream *), Y01close(IOStream *), Y02close(IOStream *);
static void Y03close(IOStream *);
static void Y12close(IOStream *), Y13close(IOStream *);
static void Y16close(IOStream *), Y17close(IOStream *);

void (*YPDBcloser[16])(IOStream *file)= { &Y00close, &Y01close, &Y02close,
  &Y03close, &Y00close, &Y01close, &Y02close, &Y03close, &Y00close,
  &Y01close, &Y12close, &Y13close, &Y00close, &Y01close, &Y16close,
  &Y17close };

static void Y00close(IOStream *file) { yPDBclose=000; ClosePDB(file); }
static void Y01close(IOStream *file) { yPDBclose=001; ClosePDB(file); }
static void Y02close(IOStream *file) { yPDBclose=002; ClosePDB(file); }
static void Y03close(IOStream *file) { yPDBclose=003; ClosePDB(file); }
static void Y12close(IOStream *file) { yPDBclose=012; ClosePDB(file); }
static void Y13close(IOStream *file) { yPDBclose=013; ClosePDB(file); }
static void Y16close(IOStream *file) { yPDBclose=016; ClosePDB(file); }
static void Y17close(IOStream *file) { yPDBclose=017; ClosePDB(file); }

static void ClosePDB(IOStream *file)
{
  long symtabAddress;
  long chartAddress;
  int notOK;

  if (!(file->permissions&2) ||
      (file->history &&
       file->history->fileNumber!=file->history->nFamily-1)) return;

  FreeClogFile(file);  /* don't want to log this stuff... */

  notOK= WRhistory(file->history);
  if (file->history) chartAddress= file->history->child->nextAddress;
  else chartAddress= file->nextAddress;

  InitBuffer(file, 4096L, chartAddress);

  notOK|= WRchart(file);

  symtabAddress= NextWRAddress(0);
  notOK|= WRsymtab(file);

  notOK|= WRextras(file);

  WriteBuffer();
  ClearBuffer(0);

  notOK|= WRheader(file, chartAddress, symtabAddress);

  if (notOK) {
    /* don't try to call YclosePDB again */
    if (!file->history) file->CloseHook= 0;
    else file->history->parent->CloseHook= file->history->child->CloseHook= 0;
    YErrorIO("failed (but tried) to properly close PDB file");
  } else {
    ZapClogFile(file);
  }
}

/*--------------------------------------------------------------------------*/

/*                            READ ROUTINES                                 */

/*--------------------------------------------------------------------------*/

static int DecodePrimitives(IOStream *file,
			    unsigned char *buffer, char *token)
{
  int i, numberSize;
  FPLayout ffpLayout, dfpLayout;

  StructDef **structList= file->structList;
  /* StructDef *charS= structList[0]; */
  StructDef *shortS= structList[1];
  StructDef *intS= structList[2];
  StructDef *longS= structList[3];
  StructDef *floatS= structList[4];
  StructDef *doubleS= structList[5];
  StructDef *stringS= structList[6];
  StructDef *pointerS= structList[7];

  /* get the byte lengths */
  mptrSize= *(buffer++);       /* PDB-style pointer, type "char *" */
  shortS->size= *(buffer++);
  intS->size= *(buffer++);
  longS->size= *(buffer++);
  floatS->size= *(buffer++);
  doubleS->size= *(buffer++);

  /* get the integral types byte order */
  shortS->order= (*(buffer++)==1)? 1 : -1;
  intS->order= (*(buffer++)==1)? 1 : -1;
  longS->order= (*(buffer++)==1)? 1 : -1;

  /* pointer and string must track long */
  pointerS->size= stringS->size= longS->size;
  pointerS->order= stringS->order= longS->order;

  /* get the float byte order */
  if (DecodeOrder(&floatS->order, buffer, floatS->size)) {
    floatS->order= 0;
    YWarning("PDB floating point format weird -- float data will be bad");
  }
  buffer+= floatS->size;

  /* get the double byte order */
  if (DecodeOrder(&doubleS->order, buffer, doubleS->size)) {
    doubleS->order= 0;
    YWarning("PDB floating point format weird -- double data will be bad");
  }
  buffer+= doubleS->size;

 /*               - Floating Point Format Descriptor
  *               -   format[0] = # of bits per number
  *               -   format[1] = # of bits in exponent
  *               -   format[2] = # of bits in mantissa
  *               -   format[3] = start bit of sign
  *               -   format[4] = start bit of exponent
  *               -   format[5] = start bit of mantissa
  *               -   format[6] = high order mantissa bit (CRAY needs this)
  *               -       bias of exponent written separately
  */

  /* get the float format data (except exponent bias) */
  numberSize= *(buffer++);
  ffpLayout.expSize= *(buffer++);
  ffpLayout.manSize= *(buffer++);
  ffpLayout.sgnAddr= *(buffer++);
  ffpLayout.expAddr= *(buffer++);
  ffpLayout.manAddr= *(buffer++);
  ffpLayout.manNorm= *(buffer++);

  /* get the double format data (except exponent bias) */
  numberSize= *(buffer++);
  dfpLayout.expSize= *(buffer++);
  dfpLayout.manSize= *(buffer++);
  dfpLayout.sgnAddr= *(buffer++);
  dfpLayout.expAddr= *(buffer++);
  dfpLayout.manAddr= *(buffer++);
  dfpLayout.manNorm= *(buffer++);

  /* exponent biases are stored with "%ld\001%ld\001\n" */
  if (!token) return 1;

  token= strtok(token, "\001");
  if (!token) return 2;
  ffpLayout.expBias= strtol(token, (char **)0, 10);
  token= strtok(0, "\001");
  if (!token) return 3;
  dfpLayout.expBias= strtol(token, (char **)0, 10);

  /* either recognize the formats or copy them */
  FreeFPLayout(floatS->fpLayout);
  floatS->fpLayout= MakeFPLayout(&ffpLayout, floatS->size);
  FreeFPLayout(doubleS->fpLayout);
  doubleS->fpLayout= MakeFPLayout(&dfpLayout, doubleS->size);

  /* make a wild guess at alignment for now --
     alignments are actually retrieved from the extras section later */
  for (i=0 ; i<8 ; i++) structList[i]->alignment= structList[i]->size;

  return 0;
}

static void SetConverter(StructDef *base, int isReal)
{
  StructDef *model= base->model;
  if (base->size!=model->size || base->order!=model->order) {
    base->Convert= isReal? &ConvertF : &ConvertI;
  } else if (isReal && !SameFPLayout(base->fpLayout, model->fpLayout)) {
    base->Convert= &ConvertF;
  } else {
    base->Convert= 0;
  }
}

static int DecodeOrder(int *order, unsigned char *permute, long n)
{
  int i, lswFirst=0, wordSize, w, b;

  /* first, scan for the "1" or "n" in the permutation */
  for (i=0 ; i<n ; i++) {
    if (permute[i]==1) {
      lswFirst= 0;
      break;
    } else if (permute[i]==n) {
      lswFirst= 1;
      break;
    }
  }
  if (i<n) {
    wordSize= i+1;
  } else {
    /* this will return an error below */
    lswFirst= 0;
    wordSize= 1;
  }

  *order= lswFirst? -wordSize : wordSize;

  /* n must be a multiple of imputed wordSize */
  if (n%wordSize) return 1;

  /* check to be sure that entire permutation is in fact described by
     lswFirst and wordSize */
  if (lswFirst) {
    for (w=n-1 ; w>=0 ; w-=wordSize)
      for (b=1 ; b<=wordSize ; b++) if ((*permute++)!=(w+b)) return 1;
  } else {
    for (w=0 ; w<n ; w+=wordSize)
      for (b=wordSize ; b>0 ; b--) if ((*permute++)!=(w+b)) return 1;
  }

  return 0;
}

#define IEEE_32_64   1        /* IEEE standard 32 bit float : 64 bit double */
#define INTEL_X86    2                        /* Intel 80x86 class machines */
#define CRAY_64      3                               /* CRAY class machines */
#define VAX_11       4                                /* VAX class machines */
#define IEEE_32_96   6        /* IEEE standard 32 bit float : 96 bit double */

static void DecodeMachine(IOStream *file, long machine)
{
  switch (machine) {
  case IEEE_32_64:
    CopyLayouts(file, sun3Layout);
    break;
  case IEEE_32_96:
    CopyLayouts(file, mac2Layout);
    break;
  case INTEL_X86:
    CopyLayouts(file, ibmpcLayout);
    break;
  case CRAY_64:
    CopyLayouts(file, cchybridLayout);
    /* original code (PDBLib) would have used
       CopyLayouts(file, crayLayout);
       if COMPILED on non-Livermore Computer Center machine */
    break;
  case VAX_11:
    CopyLayouts(file, vaxLayout);
    /* original code (PDBLib) would have used fp8gvax instead of fp8hvax
       if COMPILED with GFLOAT floating point switch set */
    break;
  default:
    CopyLayouts(file, pdbLayout);
    /* probably wrong?? */
    break;
  }
}

void CopyLayouts(IOStream *file, DataLayout *src)
{
  int i;
  StructDef **structList= file->structList;

  for (i=0 ; i<6 ; i++) {
    structList[i]->alignment= src[i].alignment;
    structList[i]->size= src[i].size;
    structList[i]->order= src[i].order;
    FreeFPLayout(structList[i]->fpLayout);
    structList[i]->fpLayout= Ref(src[i].fpLayout);
  }

  /* all of the particular machines in DecodeMachine have: */
  mptrSize= structList[3]->size;

  /* string and pointer must track long */
  structList[6]->size= structList[7]->size= structList[3]->size;
  structList[6]->alignment= structList[7]->alignment=
    structList[3]->alignment;
  structList[6]->order= structList[7]->order= structList[3]->order;
}

/*--------------------------------------------------------------------------*/

static unsigned char *fltOrder= 0;
static long orderSize= 0;

static void RDextras(IOStream *file, long address)
{
  char *line, *token;
  long version= 0;
  int dateBugCheck= 0;

  /* set default values for Major-Order, Offset, and Alignment extras */
  fastestFirst= 0;
  defaultOrigin= 0;
  gotAlignment= 0;

  InitBuffer(file, 4096L, address);

  /* skip over the symbol table to get to the extras--
     two consecutive newlines marks end of symtab
     Check for DMI-style history records during this pass.  */
  firstLength= nSeq= nSeq2= nTimes= nCycles= 0;
  firstAddr= numbAddr= timeAddr= ncycAddr= 0;
  while ((line= ReadUntil(ANY_NEWLINE)) && line[0])
    DMIcheckSymbol(strtok(line, "\001"));

  /* two consecutive newlines marks end of extras */
  while ((line= ReadUntil(ANY_NEWLINE)) && (line[0] || dateBugCheck)) {
    if (!line[0]) {
      /* The date in the Version: extra can easily have an erronious
	 newline character.  Check for this common bug before giving up.  */
      dateBugCheck= 0;
      if (!(line= ReadUntil(ANY_NEWLINE)) || !line[0]) break;
    }

    if (strncmp(line, "Offset:", 7L)==0) {
      /* The default offset is meaningless to Yorick -- the correct
	 minimum index for every variable in the file has already been
	 written.  However, it may be needed to interpret the dimension
	 list of a structure member when reading the chart.
	 Actually, the best idea might be to recognize variables
	 with minimum index equal to defaultOrigin, and translate them
	 to 1-origin...  */
      TokenAsLong(&line[7], &defaultOrigin);

    } else if (strncmp(line, "Alignment:", 10L)==0) {
      StructDef **structList= file->structList;
      structList[0]->alignment= line[10];
      mptrAlign= line[11];
      if (HashFind(&file->structTable, memberPointer, 0L))
	structList[hashIndex]->alignment= mptrAlign;
      structList[1]->alignment= line[12];
      structList[2]->alignment= line[13];
      structList[3]->alignment= /* long, string, and pointer alignment */
	structList[6]->alignment= structList[7]->alignment= line[14];
      structList[4]->alignment= line[15];
      structList[5]->alignment= line[16];
      gotAlignment= 1;

    } else if (strncmp(line, "Struct-Align:", 13L)==0 ||
	       strncmp(line, "Struct-Alignment:", 17L)==0) {
      long structAlign;
      TokenAsLong(line[13]!='e'? &line[13]:&line[17], &structAlign);
      if (structAlign<=0) structAlign= 1;
      file->structAlign= (int)structAlign;

    } else if (strncmp(line, "Version:", 8L)==0) {
      TokenAsLong(&line[8], &version);
      /* The version number is followed by a vertical bar "|", then the
	 date in ASCII.  Ignore them.  */
      dateBugCheck= 1;

    } else if (strncmp(line, "Blocks:", 7L)==0) {
      /* This one reads several more ANY_NEWLINE delimited lines...  */
      /* The blocks table may be meaningless to Yorick, but it may represent
	 time history data.  It ends with "\002".  */
      BlocksVerify();

    } else if (strncmp(line, "Casts:", 6L)==0) {
      /* This one reads several more ANY_NEWLINE delimited lines...  */
      /* The casts table is totally meaningless to Yorick, since the
	 correct (cast) data type is written at the beginning of every
	 pointee int the file.  Just skip it all (until "\002" line).  */
      while ((line= ReadUntil(ANY_NEWLINE))) if (line[0]=='\002') break;

    } else if (strncmp(line, "Major-Order:", 12L)==0) {
      /* If the fastest varying dimension is listed first, the Dimension
	 linked lists in the dataTable will need to be reversed, and the
	 dimensioned members of a struct will be read differently.  */
      long code;
      if (TokenAsLong(&line[12], &code)==0 && code==102) fastestFirst= 1;

    } else if (strncmp(line, "Primitive-Types:", 16L)==0) {
      /* This one reads several more ANY_NEWLINE delimited lines...  */
      char *typeName;
      long size, align, order_flag, i, value;
      int order, isFloat;
      FPLayout fpLayout;
      StructDef *base;

      while ((line= ReadUntil(ANY_NEWLINE)) && line[0]!='\002') {
	typeName= FlopIntegerAndInt(strtok(line, "\001"));
	if (TokenAsLong(0, &size) ||
	    TokenAsLong(0, &align) ||
	    TokenAsLong(0, &order_flag)) break;
	/* note- order_flag is 1 for MSB first, 2 for LSB first */

	if (!(token= strtok(0, "\001"))) break;
	if (strcmp(token, "ORDER")==0) {
	  CheckOrderScratch(size);
	  for (i=0 ; i<size ; i++) {
	    if (TokenAsLong(0, &value)) break;
	    fltOrder[i]= value;
	  }
	  if (i<size) break;
	  if (DecodeOrder(&order, fltOrder, size)) break;
	} else { /* else token should be "DEFORDER" */
	  order= order_flag==1? 1 : -1;
	}

	if (!(token= strtok(0, "\001"))) break;
	isFloat= 0;
	if (strcmp(token, "FLOAT")==0) {
	  isFloat= 1;
	  if (TokenAsLong(0, &value)) break;  /* 1st unused */
	  if (TokenAsLong(0, &value)) break;
	  fpLayout.expSize= value;
	  if (TokenAsLong(0, &value)) break;
	  fpLayout.manSize= value;
	  if (TokenAsLong(0, &value)) break;
	  fpLayout.sgnAddr= value;
	  if (TokenAsLong(0, &value)) break;
	  fpLayout.expAddr= value;
	  if (TokenAsLong(0, &value)) break;
	  fpLayout.manAddr= value;
	  if (TokenAsLong(0, &value)) break;
	  fpLayout.manNorm= value;
	  if (TokenAsLong(0, &fpLayout.expBias)) break;
	} else if (strcmp(token, "NO-CONV")==0) {
	  order= 0;
	} /* else token should be "FIX" */

	/* install the new primitive type */
	if ((strcmp(typeName, "string")==0 ||
	     strcmp(typeName, "pointer")==0) && !isFloat) {
	  StructDef *longDef= file->structList[3];
	  if (size==longDef->size && align==longDef->alignment &&
	      order==longDef->order) continue;
	}
	if (strcmp(typeName, "*")==0 ||
	    (HashFind(&file->structTable, typeName, 0L) && hashIndex<6))
	  continue;   /* PDBLib duplicates basic types here */
	base= AddStruct(file, typeName, 0L);
	if (!base) continue;
	/* fool DefinePrimitive and InstallStruct by resetting
	   base->references and base->dataOps */
	isFloat= DefinePrimitive(base, size, align, 1, order,
				 isFloat? &fpLayout : 0,
				 (StructDef *)0, (Converter *)0);
	/* a common problem is a NO-CONV object, which will return 5
	   here -- just let this blow up if the guy actually tries to
	   read or write one, the warnings become too tedious */
	if (isFloat && (order || isFloat!=5))
	  YWarning("illegal primitive type definition in PDB extras section");
	InstallStruct(base, (StructDef *)0);
      }
      if (line && line[0]!='\002') {
	/* attempt to resynchronize after garbled primitive type */
	YWarning("Primitive-Types PDB extra garbled -- resynchronizing");
	while ((line= ReadUntil(ANY_NEWLINE))) if (line[0]=='\002') break;
      }

    } else {
      /* An unknown extra has been found-- presumably defined after
	 this code was written.  Skip over it.
	 Extras must be of the form:
	   Extra-Name: blah-blah-blah \n
	 or
	   Extra-Name:\n
	   blah\n
	   blah\n
	   \n
	 or
	   Extra-Name:\n
	   blah\n
	   blah\n
	   \002 blah-blah-blah \n
       */
      line+= strcspn(line, ":");  /* skip the Extra-Name */
      if (!line[0]) {
	/* this is a multi-line extra */
	while ((line= ReadUntil(ANY_NEWLINE)))
	  if (!line[0] || line[0]=='\002') break;
      }
    }
  }
}

static void CheckOrderScratch(long size)
{
  if (size>orderSize) {
    fltOrder= Yrealloc(fltOrder, size+8);
    orderSize= size+8;
  }
}

/*--------------------------------------------------------------------------*/

/* Read the PDB structure chart.
   Since the Alignment and PrimitiveType extras have not yet been
   read, the sizes, alignments, and member offsets will not be correct.
   A second pass corrects them, after the extras have been read.
   The biggest complication here is the use of a PrimitiveType as the
   data type of a member -- a blank definition is inserted here.  */
static int RDchart(IOStream *file, long address)
{
  char *typeName, *descriptor, *mType, *mName, *token;
  StructDef *base;
  long size, number, origin;
  Dimension *dims, *next;
  int warned= gotAlignment;
  int goofCount= 0, goofed= 0;

  InitBuffer(file, 4096L, address);

  while ((typeName= ReadUntil(SOH_OR_NEWLINE)) && typeName[0]!='\002') {
    if (!warned) {
      YWarning("no PDB Alignments: extra -- beware of structure instances");
      warned= 1;
    }
    goofed= 0;

    /* get the size of an instance of this structure --
       this had better be at least the size implicit from the descriptor
       list, and a multiple of the alignment calculated therefrom */
    descriptor= ReadUntil(SOH_OR_NEWLINE);
    if (!descriptor) {
      if (goofCount<10) YWarning("illegal struct size in PDB chart");
      goofCount++;
      continue;
    }

    base= 0;
    size= strtol(descriptor, (char **)0, 10);

    /* Member descriptors are separated by "\001", and the decsriptor
       list ends with one of the legal NEWLINE characters, hence a
       "\001\n" marks the end of the member descriptor list.  */
    /* NOTE: This code differs in detail from the original PDBLib code,
             but is closer to the recently documented PDB file format.  */
    while ((descriptor= ReadUntil(SOH_OR_NEWLINE)) && descriptor[0]) {
      if (goofed) continue;

      if (strchr(descriptor, '*')) {
	mType= memberPointer;
	strtok(descriptor, " *\t"); /* initialize strtok */
      } else {
	mType= strtok(descriptor, " \t");
	if (!mType[0]) {
	  if (goofCount<10) YWarning("bad member type in PDB chart");
	  goofed= 1;
	  continue;
	}
	mType= FlopIntegerAndInt(mType);
      }

      mName= strtok(0, " *\t([");
      if (!mName[0]) {
	if (goofCount<10) YWarning("bad member name in PDB chart");
	goofed= 1;
	continue;
      }

      dims= tmpDims;
      tmpDims= 0;
      FreeDimension(dims);
      number= 1;
      while ((token= strtok(0, " \t[(,)]"))) {
	number= strtol(token, (char **)0, 10);
	token+= strcspn(token, ":");
	if (token[0]) {
	  origin= number;
	  number= strtol(token+1, (char **)0, 10) - origin + 1;
	} else {
	  /* The other possibility here is defaultOrigin for the file.
	     I prefer to suppose that if no origin is explicitly
	     mentioned, then the preference of the person reading
	     the file makes more sense than the preference of the one
	     writing the file.  */
	  origin= 1L;
	}
	if (number<=0) break;
	if (fastestFirst) {
	  /* add new dimension to head of list */
	  tmpDims= NewDimension(number, origin, tmpDims);
	} else {
	  /* add faster dimension to tail of list */
	  next= NewDimension(number, origin, (Dimension *)0);
	  if (tmpDims) dims->next= next;
	  else tmpDims= next;
	  dims= next;
	}
      }
      if (number<=0) {
	if (goofCount<10) YWarning("bad member dimension list in PDB chart");
	goofed= 1;
      }

      /* create new StructDef on first pass (first member) */
      if (goofed) continue;
      if (!base) {
	base= AddStruct(file, typeName, 0L);
	if (!base) {
	  if (goofCount<10) YWarning("illegal struct name in PDB chart");
	  goofed= 1;
	  continue;
	}
      }

      /* add new member to this StructDef */
      if (!HashFind(&file->structTable, mType, 0L) ||
	  AddMember(base, -1L, mName, file->structList[hashIndex], tmpDims)) {
	if (goofCount<10) YWarning("bad member descriptor in PDB chart");
	goofed= 1;
      }
    }

    if (!base) {
      /* primitive -- already handled in RDextras or DecodePrimitives */
      if (!goofed) {
	if (typeName[0]=='*' && !typeName[1]) typeName= memberPointer;
	/* the condition here works around a bug in a previous version of
	   Yorick, which wrote files with the integer data type called
	   int instead of integer -- plain else would be more correct, but
	   then a warning is generated when a file written by the old
	   version is opened -- sigh */
	else if (strcmp(typeName,"int") ||
		 HashFind(&file->structTable, "integer", 0L))
	  typeName= FlopIntegerAndInt(typeName);
	if (HashFind(&file->structTable, typeName, 0L)) {
	  base= file->structList[hashIndex];
	} else {
	  if (goofCount<10) YWarning("unknown primitive in PDB chart");
	  goofed= 1;
	}
      }

    } else {
      /* install the completed structure definition in the structTable */
      InstallStruct(base, (StructDef *)0);
    }

    if (goofed) {
      goofCount++;
    } else if (base->size!=size) {
      if (goofCount<10) YWarning("struct size inconsistency in PDB chart");
      goofCount++;
    }
  }

  return goofCount>0;
}

static char *intPDB= "integer";
static char *integerPDB= "int";

static char *FlopIntegerAndInt(char *typeName)
{
  if (typeName[0]=='i' && typeName[1]=='n' && typeName[2]=='t') {
    if (typeName[3]==0) return intPDB;
    else if (strcmp(&typeName[3], "eger")==0) return integerPDB;
  }
  return typeName;
}

char *PDBvarType(char *typeName)
{
  if (typeName) {
    if (strchr(typeName, '*')) {
      /* This is a pointer.
	 The data type is irrelevant, since the true type is written
	 into the file at the location of the pointee (just before the
	 pointee data).  */
      typeName= variablePointer;
    } else {
      /* clean any spurious blanks out of type name --
	 Should tabs be included as well as blanks?
	 (_PD_member_base_type does NOT allow tabs as of PDB version 7) */
      typeName+= strspn(typeName, " ");
      typeName[strcspn(typeName, " ")]= '\0';
      typeName= FlopIntegerAndInt(typeName);
    }
  }

  return typeName;
}

/*--------------------------------------------------------------------------*/

static char *dmiName= 0;
static long dmiMaxLength= -1;
static long dmiBase, dmiFirst, dmiNumber;

static char atHistory[]= "@history";

/* Read PDB symbol table.  */
static int RDsymtab(IOStream *file, long address)
{
  char *line, *dataName, *token;
  int hasHistory, goofCount= 0;
  long addr, minAddr=0, n=0;

  InitBuffer(file, 4096L, address);

  hasHistory= HasDMIrecords();  /* check results of RDextras */
  if (hasHistory) {
    GrabDMIData(file, firstAddr, firstLength, numbAddr);
    if (dmiNumber<0) hasHistory= 0;
    else if (nBlocks) BlocksInit();
    yPDBclose&= 1;
  }

  /* two consecutive newlines marks end of symtab */
  while((line= ReadUntil(ANY_NEWLINE)) && line[0]) {

    /* first token is variable name */
    dataName= strtok(line, "\001");
    if (!dataName) {
      if (goofCount<10) YWarning("bad variable name in PDB symtab");
      goofCount++;
      continue;
    }

    /* skip record and DMI-descriptive variables -- they are intended
       for the history child */
    if (hasHistory) {
      if (IsDMIvariable(dataName) || IsDMIrecord(dataName)>=0) continue;
    } else if (nBlocks) {
      /* PDB style history variables are skipped as well */
      if (HashFind(&blocksTable, dataName, 0L)) {
	n++;
	continue;
      }
    } else if (yPDBopen&010) {
      /* Basis PDB style history variables must be skipped, but can
         be identified certainly only by "@history" at end of name.  */
      long i, len= strlen(dataName)-1;
      for (i=7 ; i>=0 && len>0 ; i--,len--)
	if (dataName[len]!=atHistory[i]) break;
      if (i<0) {
	if (HashAdd(&blocksTable, dataName, 0L)) {
	  if (goofCount<10)
	    YWarning("duplicate @history variable name in PDB symtab");
	  goofCount++;
	}
	if (!strtok(0, "\001")) continue;
	token= strtok(0, "\001");
	if (!token || strtol(token, (char**)0, 10)<=0) continue;
	dataName= strtok(0, "\001");
	if (!dataName || (addr=strtol(dataName, (char**)0, 10))<0) continue;
	if (!minAddr || addr<minAddr) minAddr= addr;
	n++;
	continue;
      }
    }

    goofCount= GrabSymbol(dataName, file, goofCount, 0L, (char **)0);
  }

  if (n && !nBlocks) {
    nBlocks= 1;
    blocksAddrs= (long *)Ymalloc(sizeof(long));
    blocksAddrs[0]= minAddr;
  }

  if (nBlocks) {
    /* guess at how file should be closed */
    long i, len= strlen(blocksTable.names[0])-1;
    for (i=7 ; i>=0 && len>0 ; i--,len--)
      if (blocksTable.names[0][len]!=atHistory[i]) break;
    if (i<0) yPDBclose|= 4;
    else yPDBclose&= 013;
  }

  if (nBlocks && n!=blocksTable.nItems) {
    if (goofCount<10) YWarning("PDB symtab and extras Blocks: inconsistent");
    goofCount++;
  }

  return goofCount>0;
}

static int GrabSymbol(char *dataName, IOStream *file, int goofCount,
		      long offset, char **context)
{
  char *token, *typeName;
  long dataAddress, origin, length, nitems;
  Dimension *dims, *next;
  StructDef *base;

  /* second token is type name -- pointer if followed by any *'s */
  typeName= strtok(0, "\001");
  if (!typeName) {
    if (goofCount<10) YWarning("bad variable type in PDB symtab");
    return goofCount+1;
  }
  typeName= PDBvarType(typeName);

  /* third token is total number of items of specified type */
  token= strtok(0, "\001");
  if (!token || (nitems=strtol(token, (char**)0, 10))<=0) {
    if (goofCount<10) YWarning("bad item count in PDB symtab");
    return goofCount+1;
  }

  /* fourth token is data address (for pointers, this is address of the
     first pointee) */
  token= strtok(0, "\001");
  if (!token || (dataAddress=strtol(token, (char**)0, 10))<0) {
    if (goofCount<10) YWarning("bad data address in PDB symtab");
    return goofCount+1;
  }

  /* fifth token and beyond, if present, are dimensions -- ordinarily
     slowest varying to fastest varying, but might need to be adjusted
     to the other order later */
  dims= tmpDims;
  tmpDims= 0;
  FreeDimension(dims);
  while ((token= strtok(0, "\001"))) {
    origin= strtol(token, (char**)0, 10);
    /* shift arrays with the stated default origin to user's preference
       -- this is not quite ideal, since there is no way for the file
       format to force these to the defaultOrigin... */
    if (origin==defaultOrigin) origin= 1L;
    token= strtok(0, "\001");
    if (!token || (length=strtol(token, (char**)0, 10))<=0) {
      nitems= 0;
      break;
    }
    if (fastestFirst) {
      /* add new dimension to head of list */
      tmpDims= NewDimension(length, origin, tmpDims);
    } else {
      next= NewDimension(length, origin, (Dimension *)0);
      if (tmpDims) dims->next= next;
      else tmpDims= next;
      dims= next;
    }
    nitems/= length;
  }
  if (nitems!=1) {
    if (!tmpDims && nitems>1) {
      tmpDims= NewDimension(nitems, 1L, (Dimension *)0);
    } else {
      if (goofCount<10) YWarning("bad dimension list in PDB symtab");
      return goofCount+1;
    }
  }

  /* remove fake trailing dimension from PDB style history variables */
  if (offset && tmpDims && tmpDims->number==1) {
    dims= tmpDims;
    tmpDims= dims->next;
    dims->next= 0;
    FreeDimension(dims);
  }

  if (!HashFind(&file->structTable, typeName, 0L)) {
    if (goofCount<10) YWarning("non-existent type name in PDB symtab");
    return goofCount+1;
  }
  base= file->structList[hashIndex];

  /* Optionally strip Basis @... suffices if possible.  */
  if (yPDBopen&4) {
    char *c= dataName;
    long len;
    while (c[0] && c[0]!='@') c++;
    len= c-dataName;
    if (c[0] && len) {
      /* strip suffix if possible */
      HistoryInfo *history= file->history;
      IOStream *f;
      /* discard variable completely if @macro or @funct */
      if (!strcmp(c,"@macro") || !strcmp(c,"@funct"))
	return goofCount;
      if (history) f= history->parent;
      else f= file;
      if (!HashFind(&f->dataTable, dataName, len)) {
	if (!history ||
	    !HashFind(&history->child->dataTable, dataName, len))
	  c[0]= '\0';
      }
    }
  }

  if (context) {
    if (blocksTable.nItems==1 && !tmpDims) {
      /* This is a single scalar struct instance, used as a history
	 record.  Break it open and record its members as separate
	 variables.  */
      if (!BurstRecord(file, base)) return goofCount; /* Success, ... */
      /* ...otherwise, this was not a compound struct. */
      *context= StructName(base);
    }
    if (yPDBopen&010) {
      long i, len= strlen(dataName)-1;
      for (i=7 ; i>=0 && len>0 ; i--,len--)
	if (dataName[len]!=atHistory[i]) break;
      if (i<0) dataName[len+1]= '\0';
    }
  }

  /* Make the entry in the dataTable for this variable.  */
  if (AddVariable(file, dataAddress-offset, dataName, base, tmpDims)) {
    if (goofCount<10) YWarning("illegal variable in PDB symtab");
    goofCount++;
  }

  return goofCount;
}

/*--------------------------------------------------------------------------*/

static void CheckDMIScratch(long maxLength)
{
  if (maxLength>dmiMaxLength) {
    StrFree(dmiName);
    dmiName= StrAlloc(maxLength);
  }
}

static int CheckTorN(IOStream *file, char *name, long type);
static void GrabTorN(HistoryInfo *history, IOStream *child,
		     long timeOffset, long ncycOffset);

static void DMIhistory(IOStream *file, long addrChrt, long address,
		       int familyOK)
{
  HistoryInfo *history;
  IOStream *child;

  long addrTimes= 0, addrCycles= 0, addrSymt;
  long nRecords, r, index, *offsets;
  long timeOffset=-1, ncycOffset=-1;
  char *baseName;
  StructDef *base= 0;
  int dmiStyle= HasDMIrecords();

  if (!dmiStyle && !nBlocks) {
    /* set the nextAddress as PDBLib would set it.  */
    file->nextAddress= addrChrt;
    return;
  }

  /* make this file contain history records */
  history= AddHistory(file, 0L);
  child= history->child;

  /* third pass through symbol table reads record addresses --
     this also creates the child dataTable by transferring the
     record data structure to the child */
  baseName= FindAddresses(child, address, (char *)0, dmiStyle);

  /* fill in the times and ncycs as appropriate */
  if (dmiStyle) {
    addrTimes= timeAddr;
    addrCycles= ncycAddr;
    if (baseName && addrTimes)
      YRead(history->time, addrTimes,
	    child->structList[5], dmiNumber, (Strider *)0);
    if (baseName && addrCycles)
      YRead(history->ncyc, addrCycles,
	    child->structList[3], dmiNumber, (Strider *)0);
  } else {
    addrTimes= addrCycles= 0;
    if (CheckTorN(child, "time", 5L))
      timeOffset= child->addresses[hashIndex];
    if (CheckTorN(child, "ncyc", 3L))
      ncycOffset= child->addresses[hashIndex];
    GrabTorN(history, child, timeOffset, ncycOffset);
  }

  /* optionally look for more files in this family --
     requires two passes thru symtab, first to get dmiNumber, second
     to get record addresses (this allows the records to be added in
     the proper order, since in files written using PDBLib, the
     symtab is in random order) */
  while (familyOK && AddNextFile(history, (char *)0, 0)==0) {
    if (FindSymtab(child, &addrSymt, &addrChrt) ||
	FindRecords(child, addrSymt, addrTimes? &addrTimes:0,
		    addrCycles? &addrCycles:0, dmiStyle)) continue;

    /* rescan symtab to find record addresses */
    nRecords= history->nRecords;
    baseName= FindAddresses(child, addrSymt,
			    baseName? baseName : (char *)child, dmiStyle);

    /* verify that record type has the expected byte size */
    if (baseName) {
      if (base==0) {
	if (HashFind(&child->structTable, baseName, 0L))
	  base= child->structList[hashIndex];
	else
	  YErrorIO("DMI-style record structure vanished opening PDB file");
      }
      if (VerifyRecType(child, addrChrt, baseName, base->size)) {
	YWarning("skipping PDB family file -- record struct changed");
	continue;
      }

      /* read true times and cycles if provided */
      if (addrTimes)
	YRead(history->time+nRecords, addrTimes,
	      child->structList[5], dmiNumber, (Strider *)0);
      if (addrCycles)
	YRead(history->ncyc+nRecords, addrCycles,
	      child->structList[3], dmiNumber, (Strider *)0);
    }
    if (!dmiStyle) GrabTorN(history, child, timeOffset, ncycOffset);
  }

  /* clean up records */
  nRecords= history->nRecords;
  offsets= history->offset;
  index= 0;
  for (r=0 ; r<nRecords ; r++) {
    if (offsets[r]) {
      /*  this record is OK */
      if (index<r) {
	offsets[index]= offsets[r];
	history->ifile[index]= history->ifile[r];
	if (addrTimes) history->time[index]= history->time[r];
	if (addrCycles) history->ncyc[index]= history->ncyc[r];
      }
      index++;
    }
  }
  if (index<nRecords)
    YWarning("some DMI-style records in PDB file were lost");
  history->nRecords= nRecords= index;

  /* try to fix nextAddress in parent
     to reflect actual end of non-record data */
  index= file->nextAddress;
  for (r=0 ; r<nRecords ; r++) {
    if (history->ifile[r]) break;
    if (offsets[r]<index) index= offsets[r];
  }
  file->nextAddress= index;

  /* check to see whether record data structure can be removed
     from structTables */
  if (baseName) {
    RemoveStruct(file, baseName);
    RemoveStruct(child, baseName);
  }

  /* Position file to first record.  */
  JumpRecord(history, 0);
}

static void GrabTorN(HistoryInfo *history, IOStream *child,
		     long timeOffset, long ncycOffset)
{
  if (timeOffset>=0 || ncycOffset>=0) {
    double *times= (timeOffset>=0)?
      history->time+history->nRecords-nBlocks : 0;
    long *ncycs= (ncycOffset>=0)?
      history->ncyc+history->nRecords-nBlocks : 0;
    long i;
    /* should child->blockSize be reduced to, say, 0x0fff (4096) here?
       -- big blocks might slow it down a lot */
    for (i=0 ; i<nBlocks ; i++) {
      if (times)
	YRead(&times[i], blocksAddrs[i]+timeOffset,
	      child->structList[5], 1L, (Strider *)0);
      if (ncycs)
	YRead(&ncycs[i], blocksAddrs[i]+ncycOffset,
	      child->structList[3], 1L, (Strider *)0);
    }
  }
}

static void RemoveStruct(IOStream *file, char *baseName)
{
  if (HashFind(&file->structTable, baseName, 0L)) {
    StructDef **structList= file->structList;
    StructDef *base= structList[hashIndex];
    long r;
    /* only safe to remove if no outstanding references */
    if (!base->references) {
      long nItems= file->structTable.nItems-1;
      base= file->structList[hashIndex];
      file->structList[hashIndex]= 0;
      for (r=hashIndex ; r<nItems ; r++) structList[r]= structList[r+1];
      HashRemove(&file->structTable, hashIndex);
      Unref(base);
    }
  }
}

static int FindSymtab(IOStream *file, long *addrSymt, long *addrChrt)
{
  char *token;

  InitBuffer(file, 280L, 0L);

  token= ReadUntil(ANY_NEWLINE);
  if (!token || !(token= strtok(token, " "))) return ClearBuffer(1);

  if (strcmp(token, HEADTOK) == 0) {
    if (!(token= ReadBlock(1L)) ||
	!(token= ReadBlock((long)token[0]-1L))) return ClearBuffer(2);
    token= ReadUntil(ANY_NEWLINE);  /* skip exponent biases */

  } else if (strcmp(token, OLDHEADTOK) == 0) {
    token = strtok(0, " ");
    if (!token) return ClearBuffer(2);

  } else {
    return ClearBuffer(1);
  }

  /* chart and symtab addresses are stored with "%ld\001%ld\001\n" */
  token= ReadUntil(ANY_NEWLINE);
  if (!token) return ClearBuffer(2);
  if (TokenAsLong(token, addrChrt)) return ClearBuffer(2);
  if (TokenAsLong(0, addrSymt)) return ClearBuffer(2);

  return ClearBuffer(0);
}

/* WARNING-- FindRecords depends on fastestFirst being properly set.  */
static int FindRecords(IOStream *file, long addrSymt,
		       long *addrTimes, long *addrCycles, int style)
{
  char *line, *token;
  int mask, gotMask;
  long minAddr=0, addr=0, n=0, offset=0;
  long need=-1;

  InitBuffer(file, 4096L, addrSymt);

  firstLength= nSeq= nSeq2= nTimes= nCycles= 0;
  firstAddr= numbAddr= timeAddr= ncycAddr= 0;

  /* two consecutive newlines marks end of symtab */
  gotMask= 0;
  if (!addrTimes) gotMask|= 4;
  if (!addrCycles) gotMask|= 8;
  while((line= ReadUntil(ANY_NEWLINE)) && line[0]) {
    token= strtok(line, "\001");
    if (!token) continue;
    if (style) {
      /* DMI style history data */
      mask= DMIcheckSymbol(token);
      gotMask|= mask;
      if (gotMask==0xf) break;

    } else if (nBlocks && HashFind(&blocksTable, token, 0L)) {
      /* PDB style history data */
      char *dataName= token;
      if (!strtok(0, "\001")) continue;
      token= strtok(0, "\001");
      if (!token || strtol(token, (char**)0, 10)<=0) continue;
      token= strtok(0, "\001");
      if (!token || (addr=strtol(token, (char**)0, 10))<0) continue;
      if (!HashFind(&file->dataTable, dataName, 0L)) {
	/* @history may have been stripped, need to check (sigh) */
	long i, len= strlen(dataName)-1;
	for (i=7 ; i>=0 && len>0 ; i--,len--)
	  if (dataName[len]!=atHistory[i]) break;
	if (i<0 &&
	    HashFind(&file->dataTable, dataName, len+1)) dataName= 0;
      } else {
	dataName= 0;
      }
      if (!dataName) {
	if (n) {
	  if (addr<minAddr) minAddr= addr;
	  if (addr!=file->addresses[hashIndex]+offset) continue;
	} else {
	  need= file->dataTable.nItems;
	  offset= addr-file->addresses[hashIndex];
	  minAddr= addr;
	}
	n++;
      } else if (blocksTable.nItems==1 && !(yPDBclose&010)) {
	/* records are not burst */
	need= 1;
	minAddr= addr;
	n++;
      }
    }
  }

  if (style) {
    /* DMI style history data */
    ClearBuffer(0);
    if (!HasDMIrecords()) return 0;

    /* grab dmiNumber and dmiName */
    GrabDMIData(file, firstAddr, firstLength, numbAddr);

    /* timeAddr and ncycAddr have been updated */
    if (addrTimes) *addrTimes= timeAddr;
    if (addrCycles) *addrCycles= ncycAddr;

    return dmiNumber<1;

  } else if (n==need) {
    /* PDB style history data -- now positioned at extras section,
       look for Blocks: extra to check for consistency */
    int dateBugCheck= 0;
    while ((line= ReadUntil(ANY_NEWLINE)) && (line[0] || dateBugCheck)) {
      if (!line[0]) {
	dateBugCheck= 0;
	if (!(line= ReadUntil(ANY_NEWLINE)) || !line[0]) break;
      }
      if (strncmp(line, "Version:", 8L)==0) {
	dateBugCheck= 1;
      } else if (strncmp(line, "Blocks:", 7L)==0) {
	if (BlocksVerify()) break;
	if (!nBlocks) {
	  /* this file must have only one record */
	  nBlocks= 1;
	  blocksAddrs= (long *)Yrealloc(blocksAddrs, sizeof(long));
	  blocksAddrs[0]= minAddr;
	}
	return 0;
      }
    }
    return 1;
  } else {
    YWarning("history family record variable(s) missing");
    return 1;
  }
}

static void GrabDMIData(IOStream *file, long firstAddr, long firstLength,
			long numbAddr)
{
  char c, *seqNum;
  int i, dmiDigits;

  CheckDMIScratch(firstLength);

  YRead(dmiName, firstAddr,
	file->structList[0], firstLength, (Strider *)0);

  /* last 5 characters of dmiName must be record sequence number */
  dmiName[firstLength]= '\0';
  dmiDigits= strlen(dmiName);
  seqNum= dmiName+dmiDigits;
  if (dmiDigits>5) dmiDigits= 5;
  for (i=0 ; i<dmiDigits && seqNum>dmiName ; i++) {
    c= *(--seqNum);
    if (c<'0' || c>'9') {
      seqNum++;
      break;
    }
  }

  YRead(&dmiNumber, numbAddr, file->structList[3], 1, (Strider *)0);

  if (i==5) {
    if (timeAddr && nTimes<dmiNumber) timeAddr= 0;
    if (ncycAddr && nCycles<dmiNumber) ncycAddr= 0;
    dmiBase= seqNum-dmiName;
    dmiFirst= strtol(seqNum, (char **)0, 10);
  } else {
    timeAddr= ncycAddr= 0;
    nSeq= nSeq2= 0;
    dmiNumber= -1;
  }
}

static int DMIcheckSymbol(char *dataName)
{
  char *token, *typeName;
  int mask, nDims;
  long dataAddr, nitems, origin, length, lDims[2];

  /* first token is variable name */
  if (!dataName) return 0;

  /* skip anything except the four variables of interest */
  mask= IsDMIvariable(dataName);
  if (!mask) return 0;

  /* second token is type name -- pointer if followed by any *'s */
  typeName= strtok(0, " \001");
  if (!typeName) return 0;

  if (mask&1) {
    if (strcmp(typeName, "char")) return 0;
  } else if (mask&2 || mask&8) {
    if (strcmp(typeName, "long")) return 0;
  } else if (mask&4) {
    if (strcmp(typeName, "double")) return 0;
  }

  /* third token is total number of items of specified type */
  token= strtok(0, "\001");
  if (!token || (nitems=strtol(token, (char**)0, 10))<=0) return 0;

  /* fourth token is data address (for pointers, this is address of the
     first pointee) */
  token= strtok(0, "\001");
  if (!token || (dataAddr=strtol(token, (char**)0, 10))<0) return 0;

  if (mask&1) firstAddr= dataAddr;
  else if (mask&2) numbAddr= dataAddr;
  else if (mask&4) timeAddr= dataAddr;
  else if (mask&8) ncycAddr= dataAddr;

  nDims= 0;
  while ((token= strtok(0, "\001"))) {
    origin= strtol(token, (char**)0, 10);
    token= strtok(0, "\001");
    if (!token || (length=strtol(token, (char**)0, 10))<=0) {
      nitems= 0;
      nDims= 0;
      break;
    }
    if (nDims<2) lDims[nDims]= length;
    nDims++;
  }

  if (mask&1) {
    if (nDims!=2) return 0;
    /* firstLength and nSeq reversed if fastestFirst -- see HasDMIrecords */
    firstLength= lDims[1];
    nSeq= lDims[0];
  } else if (mask&2) {
    if (nDims!=1) return 0;
    nSeq2= lDims[0];
  } else if (mask&4) {
    if (nDims!=1) return 0;
    nTimes= lDims[0];
  } else if (mask&8) {
    if (nDims!=1) return 0;
    nCycles= lDims[0];
  }

  return mask;
}

static int HasDMIrecords(void)
{
  /* assure that firstAddr, firstLength, and numbAddr have been set to
     legal FirstRecordNames and NumberOfRecords variables */
  if (nSeq2) {
    if (nSeq==nSeq2) return 1;
    if (firstLength==nSeq2 && fastestFirst) {
      firstLength= nSeq;
      nSeq= nSeq2;
      return 1;
    }
  }
  return 0;
}

static int IsDMIvariable(char *dataName)
{
  if (strcmp(dataName, "FirstRecordNames")==0) return 1;
  if (strcmp(dataName, "NumberOfRecords")==0) return 2;
  if (strcmp(dataName, "times")==0) return 4;
  if (strcmp(dataName, "ncycs")==0) return 8;
  return 0;
}

static long IsDMIrecord(char *dataName)
{
  char *token;
  long recNumber;

  if (strncmp(dataName, dmiName, dmiBase)) return -1;

  token= dataName+dmiBase;
  if (strlen(token)!=5) return -1;
  while (token[0]>='0' && token[0]<='9') token++;
  if (token[0]) return -1;

  recNumber= strtol(dataName+dmiBase, (char **)0, 10);
  if (recNumber<dmiFirst || recNumber>=dmiFirst+dmiNumber) return -1;
  return recNumber;
}

static char *FindAddresses(IOStream *file, long addrSymt, char *baseName,
			   int style)
{
  long recNumber, *offset= 0;
  char *line, *token, *dataName, *typeName;
  long dataAddr;
  int goofCount= 0;
  int gotName= 0;

  /* NB- When called on the first file of a family, baseName==0; this
         rountine returns it.  Also, file is always the history child.
	 For style!=0, baseName is just a non-zero pointer for subsequent
	 files in a family; its value is (char *)file.  */

  InitBuffer(file, 4096L, addrSymt);

  /* two consecutive newlines marks end of symtab */
  while ((line= ReadUntil(ANY_NEWLINE)) && line[0]) {

    /* first token is variable name */
    dataName= strtok(line, "\001");
    if (!dataName) continue;

    if (style) {
      recNumber= IsDMIrecord(dataName);
      if (recNumber<0) continue;

      /* second token is type name -- check that it is expected baseName */
      typeName= strtok(0, " \001");
      if (!typeName) continue;

      if (baseName) {
	if (strcmp(typeName, baseName)) continue;

      } else if (HashFind(&file->structTable, typeName, 0L)) {
	/* must build child dataTable now by transfering from StructDef */
	StructDef *base= file->structList[hashIndex];
	if (BurstRecord(file, base)) return 0;

	baseName= StructName(base);

      } else {
	continue;
      }

      /* third token is total number of items -- check for 1 */
      token= strtok(0, "\001");
      if (!token || strtol(token, (char**)0, 10)!=1) continue;

      /* fourth token is record address */
      token= strtok(0, "\001");
      if (!token || (dataAddr=strtol(token, (char**)0, 10))<0) continue;

      /* on first pass, extend record lists as required */
      if (!offset) {
	HistoryInfo *history= file->history;
	long r, nRecords= history->nRecords; /* remember initial nRecords */
	/* times and ncycs lists already found by RDextras */
	int flags= (timeAddr>=0? 1 : 0) | (ncycAddr>=0? 2 : 0);
	for (r=0 ; r<dmiNumber ; r++) {
	  /* add records at disk address 0 for now -- fixed in a few lines */
	  if (AddRecord(history, flags, 0.0, 0L, 0L))
	    YErrorIO("failed to add record scanning DMI-style PDB file");
	}
	offset= history->offset+nRecords;
      }

      /* set correct record address now */
      offset[recNumber-dmiFirst]= dataAddr;

    } else {
      /* PDB style history variables */
      if (!HashFind(&blocksTable, dataName, 0L)) continue;
      if (!baseName) {
	/* first file in family -- construct child symbol table */
	goofCount= GrabSymbol(dataName, file, goofCount, blocksAddrs[0],
			      &baseName);
	if (baseName) gotName= 1;
      }
    }
  }

  ClearBuffer(0);

  if (!style) {
    /* add records for PDB style history data */
    HistoryInfo *history= file->history;
    long r;
    int flags= (CheckTorN(file,"time",5L)? 1 : 0) |
               (CheckTorN(file,"ncyc",3L)? 2 : 0);
    for (r=0 ; r<nBlocks ; r++) {
      /* add all records at zero time and ncyc for now */
      if (AddRecord(history, flags, 0.0, 0L, blocksAddrs[r]))
	YErrorIO("failed to add record scanning DMI-style PDB file");
    }
  }

  return (style||gotName)? baseName : 0;
}

static int CheckTorN(IOStream *file, char *name, long type)
{
  return HashFind(&file->dataTable, name, 0L) &&
    file->structList[type]==file->types[hashIndex].base &&
    !file->types[hashIndex].dims;
}

static int BurstRecord(IOStream *file, StructDef *base)
{
  long r, nItems= base->table.nItems;
  char **names= base->table.names;
  Member *members= base->members;
  long *offsets= base->offsets;

  /* transfer record structure to history child */
  if (nItems<=0) return 1;
  yPDBclose|= 010;
  for (r=0 ; r<nItems ; r++) {
    if (AddVariable(file, offsets[r],
		    names[r], members[r].base, members[r].dims))
      YErrorIO("failed to add variable scanning DMI-style PDB file");
  }

  return 0;
}

static int VerifyRecType(IOStream *file, long addrChrt,
			 char *baseName, long baseSize)
{
  char *token, *typeName;

  InitBuffer(file, 4096L, addrChrt);

  while ((typeName= ReadUntil(SOH_OR_NEWLINE)) && typeName[0]!='\002') {
    if (strcmp(typeName, baseName)) continue;

    token= ReadUntil(SOH_OR_NEWLINE);
    if (token &&
	strtol(token, (char **)0, 10) == baseSize) return ClearBuffer(0);
    break;
  }

  return ClearBuffer(1);
}

/*--------------------------------------------------------------------------*/

/*                              WRITE ROUTINES                              */

/*--------------------------------------------------------------------------*/

static char *EncodePrimitives(IOStream *file, char *buffer)
{
  StructDef *shortDef= file->structList[1];
  StructDef *intDef= file->structList[2];
  StructDef *longDef= file->structList[3];
  StructDef *floatDef= file->structList[4];
  StructDef *doubleDef= file->structList[5];
  StructDef *ptrDef;
  unsigned char *buf= (unsigned char *)buffer;

  /* byte count + 1 of what follows (until exponent biases) */
  *(buf++)= 24+floatDef->size+doubleDef->size;

  if (HashFind(&file->structTable, memberPointer, 0L))
    ptrDef= file->structList[hashIndex];
  else
    ptrDef= file->structList[3];

  *(buf++)= ptrDef->size;
  *(buf++)= shortDef->size;
  *(buf++)= intDef->size;
  *(buf++)= longDef->size;
  *(buf++)= floatDef->size;
  *(buf++)= doubleDef->size;

  buf+= EncodeOrder(buf, shortDef->order, 0L);
  buf+= EncodeOrder(buf, intDef->order, 0L);
  buf+= EncodeOrder(buf, longDef->order, 0L);

  buf+= EncodeOrder(buf, floatDef->order, floatDef->size);
  buf+= EncodeOrder(buf, doubleDef->order, doubleDef->size);

  *(buf++)= floatDef->size<<3;
  *(buf++)= floatDef->fpLayout->expSize;
  *(buf++)= floatDef->fpLayout->manSize;
  *(buf++)= floatDef->fpLayout->sgnAddr;
  *(buf++)= floatDef->fpLayout->expAddr;
  *(buf++)= floatDef->fpLayout->manAddr;
  *(buf++)= floatDef->fpLayout->manNorm;

  *(buf++)= doubleDef->size<<3;
  *(buf++)= doubleDef->fpLayout->expSize;
  *(buf++)= doubleDef->fpLayout->manSize;
  *(buf++)= doubleDef->fpLayout->sgnAddr;
  *(buf++)= doubleDef->fpLayout->expAddr;
  *(buf++)= doubleDef->fpLayout->manAddr;
  *(buf++)= doubleDef->fpLayout->manNorm;

  /* poke byte count back into 1st byte */
  buffer[0]= (int)(buf - (unsigned char *)buffer);

  /* write exponent biases in ASCII */
  sprintf((char *)buf, "%ld\001%ld\001\012",
	  floatDef->fpLayout->expBias, doubleDef->fpLayout->expBias);

  return (char *)buf + strlen((char *)buf);
}

static long EncodeOrder(unsigned char *buf, int order, long size)
{
  if (size) {  /* floating point order */
    int w, b;
    if (order>0) {
      for (w=0 ; w<size ; w+=order)
	for (b=order ; b>0 ; b--) *(buf++)= w+b;
    } else if (order<0) {
      for (w=size-1 ; w>=0 ; w+=order)
	for (b=1 ; b<=-order ; b++) *(buf++)= w+b;
    } else {
      for (w=0 ; w<size ; w++) *(buf++)= 1;
    }
    return size;

  } else {     /* integer order */
    *buf= order>=0? 1 : 2;
    return 1;
  }
}

/*--------------------------------------------------------------------------*/

static char *dimFormat[]= {",%ld", "(%ld"};
static char *odimFormat[]= {",%ld:%ld", "(%ld:%ld"};

static int WRchart(IOStream *file)
{
  long i, nItems= file->structTable.nItems;
  char **names= file->structTable.names;
  StructDef **structList= file->structList;
  HistoryInfo *history= file->history;
  StructDef *base;

  int notOK= 0;

  /* fill in basic primitives in order used by PDBLib */
  if (HashFind(&file->structTable, memberPointer, 0L))
    notOK|= DoStructName("*", structList[hashIndex]->size, 1);
  else
    notOK|= DoStructName("*", structList[3]->size, 1);
  notOK|= DoStructName("short", structList[1]->size, 1);
  notOK|= DoStructName("int", structList[2]->size, 1);
  notOK|= DoStructName("long", structList[3]->size, 1);
  notOK|= DoStructName("float", structList[4]->size, 1);
  notOK|= DoStructName("double", structList[5]->size, 1);
  notOK|= DoStructName("char", structList[0]->size, 1);

  /* loop on structure definitions */
  for (i=6 ; i<nItems ; i++) {
    base= structList[i];
    notOK|= WRstructure(names, names[i], base->size, base->table.nItems,
			base->table.names, base->members, base->offsets);
  }

  /* history records also treated as a data structure --
     NOTE that file MUST be the history child in this case.  */
  if (history && (!(yPDBclose&2) || !(yPDBclose&010))) {
    long n= file->dataTable.nItems;
    long recordSize= history->recordSize;
    int recordAlign= 1;
    if (HashFind(&file->structTable, "__", 0L)) {
      base= structList[hashIndex];
      nItems= base->table.nItems;
      for (i=0 ; i<nItems ; i++) {
	if (i>=n || base->offsets[i]!=file->addresses[i] ||
	    base->members[i].base!=file->types[i].base ||
	    base->members[i].number!=file->types[i].number) break;
      }
      if (i<nItems || i!=n)
	YWarning("duplicate use of struct name __ in PDB file");
    }
    /* The record size is a knotty problem, since it must be a multiple
       of the record alignment in order to follow the PDB format rules for
       struct definitions.  Unfortunately, recordAlign had to be taken as
       the most restrictive alignment of any struct at the time the first
       record was created, rather than the PDB-struct-like definition of
       the most restrictive element actually present in the structure.
       (In fact, if dataAlign!=0, it is impossible to correctly write a
       PDB file with history records.)  Since the recordSize has NOT been
       adjusted to be a multiple of the recordAlign, we can compute the
       correct alignment, then adjust recordSize here.  */
    nItems= file->dataTable.nItems;
    for (i=0 ; i<nItems ; i++)
      if (file->types[i].base->alignment > recordAlign)
	recordAlign= file->types[i].base->alignment;
    recordSize= AlignAdjust(recordSize, recordAlign);
    notOK|= WRstructure(names, "__", recordSize, n,
			file->dataTable.names, file->types, file->addresses);
  }

  *(nextByte++)= '\002';   /* "\002\n" marks end of whole chart */
  *(nextByte++)= '\012';

  return notOK;
}

static int WRstructure(char **names, char *name, long size, long nMembers,
		       char **mNames, Member *members, long *offsets)
{
  int notOK= 0;
  StructDef *mBase;
  char *typeName;
  long j, expectedOffset= 0;

  if (nMembers<=0) return 0;   /* don't touch primitives here */

  if (DoStructName(name, size, 0)) return 1;

  /* loop is on members of structure */
  for (j=0 ; j<nMembers ; j++) {
    mBase= members[j].base;
    if (mBase->Convert!=&PDBconvert) typeName= names[mBase->index];
    else typeName= memberPointer;
    typeName= FlopIntegerAndInt(typeName);
    if (CheckBuffer(5+strlen(typeName)+strlen(mNames[j]))) {
      YWarning("garbled struct member skipped closing PDB file");
      return notOK= 1;
    }
    sprintf(nextByte, "%s %s", typeName, mNames[j]);
    nextByte+= strlen(nextByte);

    /* handle dimensions of one member */
    PutDimList(members[j].dims, yPDBclose&1);
    *(nextByte++)= '\001';	/* marks end of member */

    expectedOffset= AlignAdjust(expectedOffset, mBase->alignment);
    if (expectedOffset!=offsets[j]) {
      YWarning("scrambled struct members closing PDB file -- data corrupted");
      notOK= 1;
    }
    expectedOffset+= mBase->size*members[j].number;
  }
  *(nextByte++)= '\012';	/* marks end of structure */

  return notOK;
}

static void PutDimList(Dimension *dims, int fastest1st)
{
  int firstPass;
  if (!dims) return;
  if (!fastest1st) {
    firstPass= 1;
    do {
      Put1Dim(dims, firstPass);
      firstPass= 0;
    } while ((dims=dims->next));
    *(nextByte++)= ')';
  } else {
    firstPass= (dims->next==0);
    if (!firstPass) PutDimList(dims->next, 256);
    Put1Dim(dims, firstPass);
    if (!(fastest1st&256)) *(nextByte++)= ')';
  }
}

static void Put1Dim(Dimension *dims, int firstPass)
{
  if (dims->origin!=1L) {
    CheckBuffer(47);
    sprintf(nextByte, odimFormat[firstPass],
	    dims->origin, dims->number);
    nextByte+= strlen(nextByte);
  } else {
    CheckBuffer(26);
    sprintf(nextByte, dimFormat[firstPass], dims->number);
    nextByte+= strlen(nextByte);
  }
}

static int DoStructName(char *name, long size, int putEnd)
{
  if (CheckBuffer(25+strlen(name))) {
    YWarning("impossibly long struct name skipped closing PDB file");
    return 1;
  }
  sprintf(nextByte, "%s\001%ld\001", FlopIntegerAndInt(name), size);
  nextByte+= strlen(nextByte);
  if (putEnd) *(nextByte++)= '\012';	/* marks end of structure */
  return 0;
}

/*--------------------------------------------------------------------------*/

static char *recordVar= 0;

static int WRsymtab(IOStream *file)
{
  HistoryInfo *history= file->history;
  IOStream *parent= history? history->parent : file;
  StructDef *base;
  long i;

  int notOK= 0;

  if (!history || history->copyParent) {
    /* there are non-record variables in this file */
    long nItems= parent->dataTable.nItems;
    char **names= parent->dataTable.names;
    Member *members= parent->types;
    long *addresses= parent->addresses;
    long offset= parent->offset;
    char *typeName, **tNames= parent->structTable.names;

    for (i=0 ; i<nItems ; i++) {
      base= members[i].base;
      if (base->Convert!=&PDBconvert) typeName= tNames[base->index];
      else typeName= variablePointer;
      notOK|= WRsymbol(names[i], typeName, members[i].number,
		       addresses[i]+offset, members[i].dims);
    }
  }

  /* NOTE that file MUST be the history child in this case.  */
  if (history) {
    long *offset= history->offset;
    Dimension *dims;

    if (!(yPDBclose&2)) {
      /* already computed dmiFirst and dmiNumber in WRhistory */
      char recordName[8];
      for (i=0 ; i<dmiNumber ; i++) {
	sprintf(recordName, "__%05ld", dmiFirst+i);
	notOK|= WRsymbol(recordName, "__", 1L,
			 offset[dmiFirst+i], (Dimension *)0);
      }

      /* firstAddr, numbAddr, timeAddr, and ncycAddr have already been
	 set by WRhistory */
      dims= tmpDims;
      tmpDims= 0;
      FreeDimension(dims);
      tmpDims= NewDimension(firstLength, 1L, tmpDims);
      tmpDims= NewDimension(1L, 1L, tmpDims);
      if (firstAddr)
	notOK|= WRsymbol("FirstRecordNames", "char", firstLength,
			 firstAddr, tmpDims);
      dims= tmpDims->next;
      tmpDims->next= 0;
      FreeDimension(dims);
      if (numbAddr)
	notOK|= WRsymbol("NumberOfRecords", "long", 1L, numbAddr, tmpDims);
      if (dmiNumber) {
	tmpDims->number= dmiNumber;
	if (timeAddr)
	  notOK|= WRsymbol("times", "double", dmiNumber, timeAddr, tmpDims);
	if (ncycAddr)
	  notOK|= WRsymbol("ncycs", "long", dmiNumber, ncycAddr, tmpDims);
      }

    } else {
      /* history data is to be written in PDB style */
      Dimension *dims= tmpDims;
      tmpDims= 0;
      FreeDimension(dims);
      if (yPDBclose&010) {
	/* write each variable individually */
	char *name= 0;
	IOStream *child= history->child;
	long nItems= child->dataTable.nItems;
	char **names= child->dataTable.names;
	Member *members= child->types;
	long *addresses= child->addresses;
	char *typeName, **tNames= child->structTable.names;

	if (recordVar) {
	  name= recordVar;
	  recordVar= 0;
	  StrFree(name);
	}

	for (i=0 ; i<nItems ; i++) {
	  base= members[i].base;
	  if (base->Convert!=&PDBconvert) typeName= tNames[base->index];
	  else typeName= variablePointer;
	  if (yPDBclose&4) name= recordVar= StrCat(names[i], "@history");
	  else name= names[i];
	  tmpDims= NewDimension(1L, 1L, Ref(members[i].dims));
	  notOK|= WRsymbol(name, typeName, members[i].number,
			   addresses[i]+offset[dmiFirst], tmpDims);
	  dims= tmpDims;
	  tmpDims= 0;
	  FreeDimension(dims);
	  if (yPDBclose&4) {
	    recordVar= 0;
	    StrFree(name);
	  }
	}

      } else {
	/* bundle each record into a single struct instance
	   -- name is __@history whether or not yPDBclose&4 is set */
	tmpDims= NewDimension(1L, 1L, tmpDims);
	notOK|= WRsymbol("__@history", "__", 1,
			 offset[dmiFirst], (Dimension *)0);
      }
    }
  }

  CheckBuffer(1L);
  *(nextByte++)= '\012';
  return notOK;
}

static int WRsymbol(char *name, char *typeName, long number, long address,
		    Dimension *dims)
{
  int notOK= 0;

  if (CheckBuffer(45+strlen(name)+strlen(typeName))) {
    YWarning("impossibly long type+variable name skipped closing PDB file");
    notOK= 1;
  } else {
    sprintf(nextByte, "%s\001%s\001%ld\001%ld\001",
	    name, FlopIntegerAndInt(typeName), number, address);
    nextByte+= strlen(nextByte);
    if (!(yPDBclose&1)) {
      for ( ; dims ; dims=dims->next) {
	CheckBuffer(43);
	sprintf(nextByte, "%ld\001%ld\001", dims->origin, dims->number);
	nextByte+= strlen(nextByte);
      }
    } else {
      if (dims) Put102Dims(dims);
    }
    *(nextByte++)= '\012';
  }

  return notOK;
}

static void Put102Dims(Dimension *dims)
{
  if (dims->next) Put102Dims(dims->next);
  CheckBuffer(43);
  sprintf(nextByte, "%ld\001%ld\001", dims->origin, dims->number);
  nextByte+= strlen(nextByte);
}

/*--------------------------------------------------------------------------*/

extern char *y__pdb_date;
extern int y__pdb_version;

char *y__pdb_date= 0;   /* "Sun Dec  7 06:00:00 1944" --- exactly 24 chars */
int y__pdb_version= 11;

static int WRextras(IOStream *file)
{
  StructDef **structList= file->structList;
  long i, nItems, size;
  int alignment, order, pdbOrder;
  char *typeName;
  FPLayout *fpLayout;
  int notOK= 0;

  CheckBuffer(155+ 24 /* strlen(y__pdb_date) */);

  /* The default index origin is 1L.  */
  sprintf(nextByte, "Offset:%ld\012", 1L);
  nextByte+= strlen(nextByte);

  /* alignments come from the structTable */
  strcpy(nextByte, "Alignment:");
  nextByte+= 10;
  *(nextByte++)= (unsigned char)structList[0]->alignment;
  if (HashFind(&file->structTable, memberPointer, 0L))
    *(nextByte++)= (unsigned char)structList[hashIndex]->alignment;
  else
    *(nextByte++)= (unsigned char)structList[3]->alignment;
  *(nextByte++)= (unsigned char)structList[1]->alignment;
  *(nextByte++)= (unsigned char)structList[2]->alignment;
  *(nextByte++)= (unsigned char)structList[3]->alignment;
  *(nextByte++)= (unsigned char)structList[4]->alignment;
  *(nextByte++)= (unsigned char)structList[5]->alignment;
  *(nextByte++)= '\012';

  /* Set the structAlignment.  */
  sprintf(nextByte, "Struct-Alignment:%d\012",
	  file->structAlign>1? file->structAlign : 0);
  nextByte+= strlen(nextByte);

  /* version number and date don't mean much */
  StrFree(y__pdb_date);
  y__pdb_date= Ytimestamp();
  sprintf(nextByte, "Version:%d|%s\012", y__pdb_version, y__pdb_date);
  nextByte+= strlen(nextByte);

  /* Major-Order must come before Blocks for PDBLib */
  sprintf(nextByte, "Major-Order:%s\012", (yPDBclose&1)? "102" : "101");
  nextByte+= 16;
  sprintf(nextByte, "Has-Directories:0\012");
  nextByte+= 18;

  /* blocks information optional as history mechanism */
  strcpy(nextByte, "Blocks:\012");
  nextByte+= 8;
  /* already computed dmiFirst and dmiNumber in WRhistory */
  if (file->history && dmiNumber>1 && yPDBclose&2) {
    HistoryInfo *history= file->history;
    long *addrs= history->offset+dmiFirst;
    if (yPDBclose&010) {
      /* write each variable individually */
      IOStream *child= history->child;
      char **names= child->dataTable.names;
      Member *members= child->types;
      long j, number, *offset= child->addresses;
      nItems= child->dataTable.nItems;
      for (j=0 ; j<nItems ; j++) {
	CheckBuffer(strlen(names[j])+29);
	strcpy(nextByte, names[j]);
	nextByte+= strlen(nextByte);
	if (yPDBclose&4) {
	  strcpy(nextByte, "@history");
	  nextByte+= 8;
	}
	sprintf(nextByte, "\001%ld", dmiNumber);
	nextByte+= strlen(nextByte);
	number= members[j].number;
	for (i=0 ; i<dmiNumber ; i++) {
	  CheckBuffer(55);   /* 10 bytes for Casts extra... */
	  sprintf(nextByte, " %ld %ld", addrs[i]+offset[j], number);
	  nextByte+= strlen(nextByte);
	}
	strcpy(nextByte, "\012");
	nextByte++;
      }

    } else {
      /* bundle each record into a single struct instance
	 -- name is __@history whether or not yPDBclose&4 is set */
      sprintf(nextByte, "__@history\001%ld", dmiNumber);
      nextByte+= strlen(nextByte);
      for (i=0 ; i<dmiNumber ; i++) {
	CheckBuffer(35);   /* 10 bytes for Casts extra... */
	sprintf(nextByte, " %ld 1", addrs[i]);
	nextByte+= strlen(nextByte);
      }
      strcpy(nextByte, "\012");
      nextByte++;
    }
  }
  strcpy(nextByte, "\002\012");
  nextByte+= 2;

  /* cast information not used */
  strcpy(nextByte, "Casts:\012\002\012");
  nextByte+= 9;

  /* any additional primitive types are written here */
  strcpy(nextByte, "Primitive-Types:\012");
  nextByte+= 17;
  nItems= file->structTable.nItems;
  for (i=6 ; i<nItems ; i++) {
    if (structList[i]->table.nItems ||
	structList[i]->Convert==&PDBconvert ||
	(i<8 && structList[i]->references<1)) continue;
    typeName= FlopIntegerAndInt(file->structTable.names[i]);
    size= structList[i]->size;
    alignment= structList[i]->alignment;
    order= structList[i]->order;
    fpLayout= structList[i]->fpLayout;
    if (fpLayout || order==0) pdbOrder= -1;
    else if (order>0) pdbOrder= 1;
    else pdbOrder= 2;

    if (CheckBuffer(68+strlen(typeName))) {
      YWarning("impossibly long primitive name skipped closing PDB file");
      notOK= 1;
      continue;
    }
    sprintf(nextByte, "%s\001%ld\001%d\001%d\001",
	    typeName, size, alignment, pdbOrder);
    nextByte+= strlen(nextByte);

    if (order && (fpLayout || order>1 || order<-1)) {
      CheckBuffer(6);
      strcpy(nextByte, "ORDER\001");
      nextByte+= 6;
      if (order>0) {
	int w, b;
	for (w=size-1 ; w>=0 ; w-=order) {
	  for (b=1 ; b<=order ; b++) {
	    CheckBuffer(21);
	    sprintf(nextByte, "%d\001", w+b);
	    nextByte+= strlen(nextByte);
	  }
	}
      } else if (order<0) {
	int w, b;
	for (w=0 ; w<size ; w-=order) {
	  for (b=-order ; b>0 ; b--) {
	    CheckBuffer(21);
	    sprintf(nextByte, "%d\001", w+b);
	    nextByte+= strlen(nextByte);
	  }
	}
      }
    } else {
      CheckBuffer(9);
      strcpy(nextByte, "DEFORDER\001");
      nextByte+= 9;
    }

    if (!order) {
      CheckBuffer(11);
      strcpy(nextByte, "NO-CONV\001");
      nextByte+= 8;
    } else if (fpLayout) {
      CheckBuffer(58);
      strcpy(nextByte, "FLOAT\001");
      nextByte+= 6;
      sprintf(nextByte, "%d\001", (int)((size<<3)&0xff));
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%d\001", (fpLayout->expSize)&0xff);
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%d\001", (fpLayout->manSize)&0xff);
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%d\001", (fpLayout->sgnAddr)&0xff);
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%d\001", (fpLayout->expAddr)&0xff);
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%d\001", (fpLayout->manAddr)&0xff);
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%d\001", (fpLayout->manNorm)&0xff);
      nextByte+= strlen(nextByte);
      sprintf(nextByte, "%ld\001", fpLayout->expBias);
      nextByte+= strlen(nextByte);
    } else {
      CheckBuffer(7);
      strcpy(nextByte, "FIX\001");
      nextByte+= 4;
    }

    *(nextByte++)= '\012';              /* mark end of one primitive type */
  }
  *(nextByte++)= '\002';   /* mark end of all primitive type declarations */
  *(nextByte++)= '\012';

  /* Set the structAlign for old Yorick versions.  */
  sprintf(nextByte, "Struct-Align:%d\012", file->structAlign);
  nextByte+= strlen(nextByte);

  /* file ends with two newlines (one is an extra?) */
  *(nextByte++)= '\012';
  *(nextByte++)= '\012';

  return notOK;
}

/*--------------------------------------------------------------------------*/

static int WRheader(IOStream *file, long newChart, long newSymtab)
{
  /* Need to read a few words near the beginning of the file to find
     the header.  */
  char *token, *buffer;
  long chartAddress, symtabAddress;
  long headerAddress;

  InitBuffer(file, 280L, 0L);
  token= ReadUntil(ANY_NEWLINE);
  if (!token || !(token= strtok(token, " "))) return ClearBuffer(1);

  if (strcmp(token, HEADTOK) == 0) {
    if (!(token= ReadBlock(1L)) ||
	!(token= ReadBlock((long)token[0]-1L)) ||
	!ReadUntil(ANY_NEWLINE)) return ClearBuffer(2);

  } else if (strcmp(token, OLDHEADTOK) != 0) {
    return ClearBuffer(2);
  }

  /* nextByte now points to buffer location containing current chart
     and symtab addresses.  Compute corresponding disk address.  */
  headerAddress= NextWRAddress(1);
  buffer= nextByte;

  /* read current value of chart address */
  token= ReadUntil(ANY_NEWLINE);
  if (!token || TokenAsLong(token, &chartAddress) || newChart!=chartAddress ||
      TokenAsLong(0, &symtabAddress) || newSymtab!=symtabAddress) {
    sprintf(buffer, "%ld\001%ld\001\012", newChart, newSymtab);
    YcWrite(file, buffer, headerAddress, strlen(buffer));
  }

  return ClearBuffer(0);
}

/*--------------------------------------------------------------------------*/

static int WRhistory(HistoryInfo *history)
{
  IOStream *child;
  StructDef **structList;
  long i, n;
  int *ifile, thisFile, alignment;
  char recordName[8];

  int notOK= 0;

  /* intialize state information required by WRchart and WRsymtab */
  firstLength= dmiFirst= dmiNumber= 0;
  firstAddr= numbAddr= timeAddr= ncycAddr= 0;
  if (!history) return 0;

  /* be sure this is last member of family */
  thisFile= history->fileNumber;  /* already checked that this is
				     nFamily-1 in YclosePDB */

  n= history->nRecords;
  ifile= history->ifile;
  dmiNumber= 0;
  dmiFirst= -1;
  for (i=0 ; i<n ; i++) {
    if (ifile[i]==thisFile) {
      if (dmiFirst<0) dmiFirst= i;
      dmiNumber++;
    } else if (dmiNumber) {
      YWarning("skipped scrambled records closing PDB file");
      notOK= 1;
      break;
    }
  }
  if (!dmiNumber) dmiFirst= 0;

  if (!(yPDBclose&2)) {
    sprintf(recordName, "__%05ld", dmiFirst);
    firstLength= 8;

    child= history->child;
    structList= child->structList;

    /* write FirstRecordNames variable */
    if (child->dataAlign) alignment= child->dataAlign;
    else alignment= structList[0]->alignment;
    firstAddr= AlignAdjust(child->nextAddress, alignment);
    YWrite(recordName, firstAddr, structList[0], 8L, (Strider *)0);

    /* write NumberOfRecords variable */
    if (!child->dataAlign) alignment= structList[3]->alignment;
    numbAddr= AlignAdjust(child->nextAddress, alignment);
    YWrite(&dmiNumber, numbAddr, structList[3], 1L, (Strider *)0);

    /* write times variable */
    if (dmiNumber && history->time) {
      if (!child->dataAlign) alignment= structList[3]->alignment;
      timeAddr= AlignAdjust(child->nextAddress, alignment);
      YWrite(&history->time[dmiFirst], timeAddr,
	     structList[5], dmiNumber, (Strider *)0);
    }

    /* write ncycs variable */
    if (dmiNumber && history->ncyc) {
      if (!child->dataAlign) alignment= structList[3]->alignment;
      ncycAddr= AlignAdjust(child->nextAddress, alignment);
      YWrite(&history->ncyc[dmiFirst], ncycAddr,
	     structList[3], dmiNumber, (Strider *)0);
    }
  }

  return notOK;
}

/*--------------------------------------------------------------------------*/

static int TokenAsLong(char *token, long *result)
{
  token= strtok(token, "\001");
  if (!token) return 1;
  *result= strtol(token, (char **)0, 10);
  return 0;
}

/* These routines are designed to be able to read through the "line-oriented"
   sections of a PDB file.  Some of the quirks are required to deal with
   quirks in the PDB format itself...  */

/* state data for reader routines */
static char *firstByte, *thisByte, *lastByte;
static long bufSize, bufAddress, nextAddress;
static int bufAtEOF;
static IOStream *bufFile;

static void InitBuffer(IOStream *file, long size, long address)
{
  ClearBuffer(0);
  bufSize= size;
  nextByte= thisByte= lastByte= firstByte= Ymalloc(bufSize+1);
  nextByte[0]= '\0';
  nextAddress= address;
  bufFile= file;
  bufAddress= address-1;  /* not equal to nextAddress */
  bufAtEOF= 0;
}

static int ClearBuffer(int value)
{
  /* failsafe freeing of firstByte scratch space --
     guards against asynchronous interrupts */
  char *scratch= firstByte;
  nextByte= thisByte= lastByte= firstByte= 0;
  Yfree(scratch);
  bufFile= 0;
  return value;
}

static char *ReadUntil(char *delimiters)
{
  long len= 0;

  for (;;) {
    /* scan until one of the delimiters is found */
    len+= strcspn(&nextByte[len], delimiters);
    if (nextByte[len]) {
      nextByte[len++]= '\0';     /* overwrite the delimiter */
      break;
    }

    /* skip over any imbedded \0 bytes (a PDB quirk)
       Actually, none are known, but PDBLib would skip them...  */
    while (nextByte+len<lastByte) if (nextByte[++len]) break;

    /* If this is the end of the input buffer, reread starting from
       the address at the beginning of this line (nextAddress).  */
    if (nextByte+len>=lastByte && ReadBuffer()) return 0;
  }

  thisByte= nextByte;
  nextByte+= len;
  nextAddress+= len;
  return thisByte;
}

static char *ReadNonblank(char *delimiters)
{
  char *line= ReadUntil(delimiters);
  while (!line[0]) line= ReadUntil(delimiters);
  return line;
}

static char *ReadBlock(long nBytes)
{
  if (nextByte+nBytes>lastByte && ReadBuffer()) return 0;
  thisByte= nextByte;
  nextByte+= nBytes;
  nextAddress+= nBytes;
  return thisByte;
}

static int ReadBuffer(void)
{
  long nBytes;
  /* quit if EOF or if this request has same address as previous */
  if (bufAtEOF || nextAddress==bufAddress) return 1;
  nBytes= YcRead(bufFile, firstByte, nextAddress, bufSize);
  lastByte= &firstByte[nBytes];
  lastByte[0]= '\0';
  bufAtEOF= nBytes<bufSize;
  bufAddress= nextAddress;
  nextByte= thisByte= firstByte;
  return 0;
}

static int CheckBuffer(long nRequired)
{
  if ((bufSize-(nextByte-firstByte))>=nRequired) return 0;
  else if (nRequired>bufSize) return 1;
  WriteBuffer();
  return 0;
}

static void WriteBuffer(void)
{
  long nBytes= nextByte-firstByte;
  YcWrite(bufFile, firstByte, nextAddress, nBytes);
  nextAddress+= nBytes;
  nextByte= firstByte;
  nextByte[0]= '\0';
}

static long NextWRAddress(int afterRead)
{
  return (nextByte-firstByte) + (afterRead? bufAddress : nextAddress);
}

/*--------------------------------------------------------------------------*/

static void BlocksInit(void)
{
  long *addrs= blocksAddrs;
  HashClear(&blocksTable);
  nBlocks= 0;
  blocksAddrs= 0;
  Yfree(addrs);
}

static int BlocksVerify(void)
{
  char *line;
  int illegal=0, skip=0, hadBlocks=0;
  long i, n, count, addr, minAddr=0, delAddr=0, nGoofs=0;
  long have=0, need= blocksTable.nItems;

  if (need) {
    /* throw out record addresses from previous file */
    long *addrs= blocksAddrs;
    nBlocks= 0;
    blocksAddrs= 0;
    Yfree(addrs);
    hadBlocks= 1;
  }

  /* loop on blocks */
  while ((line= ReadUntil(SOH_OR_NEWLINE))) {
    if (line[0]=='\002') break;  /* only legal exit */
    illegal= 1;

    /* add variable name to blocksTable */
    if (!skip) {
      if (!need) {
	if (HashAdd(&blocksTable, line, 0L)) {
	  if (nGoofs++ < 10)
	    YWarning("duplicate history variable name in PDB Blocks extra");
	}
      } else {
	if (!HashFind(&blocksTable, line, 0L)) {
	  YWarning("unknown history history family record variable");
	  skip= 1;
	}
      }
    }

    /* read number of blocks */
    if (BlocksLine(line= ReadNonblank(" " ANY_NEWLINE))) break;
    TokenAsLong(line, &n);
    if (nBlocks || n<=0) {
      if (n!=nBlocks || n<=0) {
	YWarning("Yorick-incompatible PDB Blocks extra (1)");
	skip= 1;
      }
    } else {
      /* this is the first variable (after BlocksInit) */
      nBlocks= n;
      blocksAddrs= (long *)Yrealloc(blocksAddrs, n*sizeof(long));
    }

    /* read blocks addresses for this variable, ignoring count fields */
    for (i=0 ; i<n ; i++) {
      if (BlocksLine(line= ReadNonblank(" " ANY_NEWLINE))) break;
      TokenAsLong(line, &addr);
      if (BlocksLine(line= ReadNonblank(" " ANY_NEWLINE))) break;
      TokenAsLong(line, &count);
      if (skip) continue;
      if (minAddr) {
	/* not the first variable */
	if (i) {
	  /* not the first block -- check that this block has same
	     offset of the equivalent block in the first variable */
	  if (blocksAddrs[i]!=addr+delAddr) {
	    YWarning("Yorick-incompatible PDB Blocks extra (1)");
	    skip= 1;
	  }
	} else {
	  /* the first block -- record offset for this variable,
	     and check whether it has smallest address */
	  delAddr= blocksAddrs[0]-addr;
	  if (addr<minAddr) minAddr= addr;
	}
      } else {
	/* just record the first variable addresses */
	blocksAddrs[i]= addr;
      }
    }
    if (i<n) break;
    if (!skip) {
      if (!minAddr) minAddr= blocksAddrs[0];
      have++;
    }

    illegal= skip;
  }

  if (nBlocks && !illegal) {
    /* adjust blocksAddrs to reflect the variable which was written first,
       rather than the one which appears first in the Blocks: extra */
    if (!need || have==need) {
      minAddr-= blocksAddrs[0];
      for (i=0 ; i<nBlocks ; i++) blocksAddrs[i]+= minAddr;
    } else {
      YWarning("history family record variable blocks missing");
      illegal= 1;
    }
  } else if (!hadBlocks) {
    BlocksInit();
  }

  return illegal;
}

static int BlocksLine(char *line)
{
  if (!line || line[0]=='\002') {
    YWarning("bad Blocks format in PDB extras section");
    return 1;
  } else {
    return 0;
  }
}

/*--------------------------------------------------------------------------*/