1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
|
/*
* dpsclient.c -- Implementation of the Display PostScript Client Library.
*
* (c) Copyright 1988-1994 Adobe Systems Incorporated.
* All rights reserved.
*
* Permission to use, copy, modify, distribute, and sublicense this software
* and its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notices appear in all copies and that
* both those copyright notices and this permission notice appear in
* supporting documentation and that the name of Adobe Systems Incorporated
* not be used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. No trademark license
* to use the Adobe trademarks is hereby granted. If the Adobe trademark
* "Display PostScript"(tm) is used to describe this software, its
* functionality or for any other purpose, such use shall be limited to a
* statement that this software works in conjunction with the Display
* PostScript system. Proper trademark attribution to reflect Adobe's
* ownership of the trademark shall be given whenever any such reference to
* the Display PostScript system is made.
*
* ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
* ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
* ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON- INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL ADOBE BE LIABLE
* TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ADOBE WILL NOT
* PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
*
* Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
* Incorporated which may be registered in certain jurisdictions
*
* Author: Adobe Systems Incorporated
*/
/* $XFree86$ */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "dpsXpriv.h"
#include "DPS/dpsclient.h"
#include "dpsprivate.h"
#include "dpsdict.h"
#include "DPS/dpsexcept.h"
#include "dpsassert.h"
#ifdef XDPS
#include "dpsXint.h"
#endif /* XDPS */
#if defined(SVR4) || defined(SYSV) || defined(SystemV)
#define os_bcopy(f,t,c) memcpy(t,f,c)
#else
#define os_bcopy(f,t,c) bcopy(f,t,c)
#endif
#if !IEEEFLOAT
extern void IEEEHighToNative(/* FloatRep *from, real *to */);
/* Converts from IEEE high-byte-first real to native real. */
extern void NativeToIEEEHigh(/* real *from, FloatRep *to */);
/* Converts from native real to IEEE high-byte-first real. */
extern void IEEELowToNative(/* FloatRep *from, real *to */);
/* Converts from IEEE low-byte-first real to native real. */
extern void NativeToIEEELow(/* real *from, FloatRep *to */);
/* Converts from native real to IEEE low-byte-first real. */
#endif /* !IEEEFLOAT */
typedef union { /* 32 bit number representations */
unsigned int u;
int i;
float f;
unsigned char bytes[4];
/* raw bytes, in whatever order they appear in memory */
} Swap32Rec;
typedef union { /* 16 bit number representations */
unsigned short u;
short i;
unsigned char bytes[2];
} Swap16Rec;
#if SWAPBITS
#define Copy4SrcLo(src, dst)\
(((Swap32Rec *) dst)->bytes[0] = ((Swap32Rec *) src)->bytes[0], \
((Swap32Rec *) dst)->bytes[1] = ((Swap32Rec *) src)->bytes[1], \
((Swap32Rec *) dst)->bytes[2] = ((Swap32Rec *) src)->bytes[2], \
((Swap32Rec *) dst)->bytes[3] = ((Swap32Rec *) src)->bytes[3] )
#define Copy4SrcHi(src, dst)\
(((Swap32Rec *) dst)->bytes[0] = ((Swap32Rec *) src)->bytes[3], \
((Swap32Rec *) dst)->bytes[1] = ((Swap32Rec *) src)->bytes[2], \
((Swap32Rec *) dst)->bytes[2] = ((Swap32Rec *) src)->bytes[1], \
((Swap32Rec *) dst)->bytes[3] = ((Swap32Rec *) src)->bytes[0] )
#define Copy2SrcLo(src, dst)\
(((Swap16Rec *) dst)->bytes[0] = ((Swap16Rec *) src)->bytes[0], \
((Swap16Rec *) dst)->bytes[1] = ((Swap16Rec *) src)->bytes[1] )
#define Copy2SrcHi(src, dst)\
(((Swap16Rec *) dst)->bytes[0] = ((Swap16Rec *) src)->bytes[1], \
((Swap16Rec *) dst)->bytes[1] = ((Swap16Rec *) src)->bytes[0] )
#else /* SWAPBITS */
#define Copy4SrcHi(src, dst)\
(((Swap32Rec *) dst)->bytes[0] = ((Swap32Rec *) src)->bytes[0], \
((Swap32Rec *) dst)->bytes[1] = ((Swap32Rec *) src)->bytes[1], \
((Swap32Rec *) dst)->bytes[2] = ((Swap32Rec *) src)->bytes[2], \
((Swap32Rec *) dst)->bytes[3] = ((Swap32Rec *) src)->bytes[3] )
#define Copy4SrcLo(src, dst)\
(((Swap32Rec *) dst)->bytes[0] = ((Swap32Rec *) src)->bytes[3], \
((Swap32Rec *) dst)->bytes[1] = ((Swap32Rec *) src)->bytes[2], \
((Swap32Rec *) dst)->bytes[2] = ((Swap32Rec *) src)->bytes[1], \
((Swap32Rec *) dst)->bytes[3] = ((Swap32Rec *) src)->bytes[0] )
#define Copy2SrcHi(src, dst)\
(((Swap16Rec *) dst)->bytes[0] = ((Swap16Rec *) src)->bytes[0], \
((Swap16Rec *) dst)->bytes[1] = ((Swap16Rec *) src)->bytes[1] )
#define Copy2SrcLo(src, dst)\
(((Swap16Rec *) dst)->bytes[0] = ((Swap16Rec *) src)->bytes[1], \
((Swap16Rec *) dst)->bytes[1] = ((Swap16Rec *) src)->bytes[0] )
#endif /* SWAPBITS */
#define DPS_ERR_TAG 250
#define CONTEXTBUFFERSIZE 256
#define MAXQUEUEDBUFFERS 5
DPSContext DPSGlobalContext;
Globals DPSglobals = NULL;
char *DPScalloc(integer e, integer n)
{
char *p;
while (!(p = (char *)calloc(e, n))) {
DPSOutOfMemory();
}
return p;
}
void DPSSafeSetLastNameIndex(DPSContext ctxt)
{
/* we're about to call the error handler, so roll back the
lastNameIndex to the last known valid index */
DPSCheckInitClientGlobals();
if (ctxt != dummyCtx && ctxt->space != NIL)
((DPSPrivContext)ctxt)->lastNameIndex = ((DPSPrivSpace)(ctxt->space))->lastNameIndex;
}
void DPSCheckInitClientGlobals(void)
{
if (!DPSglobals) {
DPSglobals = (Globals)DPScalloc(sizeof(GlobalsRec), 1);
globLastNameIndex = -1;
}
}
/**************************************************************/
/* Procedures that support the DPSCreateContext context procs */
/**************************************************************/
/* ARGSUSED */
static void ReleaseInput(
char *unused, char *buffer)
{
ContextBuffer cb = (ContextBuffer)buffer;
if (cb == NIL) return;
cb->next = contextBuffers;
contextBuffers = cb;
queuedBuffers--;
}
/* ARGSUSED */
static void StuffResultVal(
DPSContext ctxt,
DPSResults result,
integer tag,
DPSBinObj obj)
{
integer type = obj->attributedType & ~DPS_EXEC;
integer nObjs = 1;
/* check if array */
if (type == DPS_ARRAY) {
nObjs = obj->length;
if (nObjs < 1) return;
if (result->count == -1 && nObjs != 1) {
DPSSafeSetLastNameIndex(ctxt);
if (ctxt->errorProc != NIL)
(*ctxt->errorProc)(ctxt, dps_err_resultTypeCheck, (long unsigned)obj, 0);
return;
}
obj = (DPSBinObj) ((char *)obj + obj->val.arrayVal);
type = obj->attributedType & ~DPS_EXEC;
}
do {
integer bump = 0;
if (result->count == 0) return;
switch (result->type) {
case dps_tBoolean:
if (type == DPS_BOOL) {
int *b = (int *) result->value;
*b = (int) obj->val.booleanVal;
bump = sizeof(int);
}
break;
case dps_tFloat: {
float *f = (float *) result->value;
if (type == DPS_REAL) {
*f = obj->val.realVal;
bump = sizeof(float);
}
else if (type == DPS_INT) {
*f = (float) obj->val.integerVal;
bump = sizeof(float);
}
break;
}
case dps_tDouble: {
double *d = (double *) result->value;
if (type == DPS_REAL) {
*d = (double) obj->val.realVal;
bump = sizeof(double);
}
else if (type == DPS_INT) {
*d = (double) obj->val.integerVal;
bump = sizeof(double);
}
break;
}
case dps_tShort:
if (type == DPS_INT) {
short *s = (short *) result->value;
*s = (short) obj->val.integerVal;
bump = sizeof(short);
}
break;
case dps_tUShort:
if (type == DPS_INT) {
unsigned short *us = (unsigned short *) result->value;
*us = (unsigned short) obj->val.integerVal;
bump = sizeof(unsigned short);
}
break;
case dps_tInt:
if (type == DPS_INT) {
int *i = (int *) result->value;
*i = (int) obj->val.integerVal;
bump = sizeof(int);
}
break;
case dps_tUInt:
if (type == DPS_INT) {
unsigned int *ui = (unsigned int *) result->value;
*ui = (unsigned int) obj->val.integerVal;
bump = sizeof(unsigned int);
}
break;
case dps_tLong:
if (type == DPS_INT) {
long int *li = (long int *) result->value;
*li = obj->val.integerVal;
bump = sizeof(long int);
}
break;
case dps_tULong:
if (type == DPS_INT) {
unsigned long *u = (unsigned long *) result->value;
*u = (unsigned long) obj->val.integerVal;
bump = sizeof(unsigned long);
}
break;
case dps_tChar:
case dps_tUChar:
if (nObjs != 1) {
DPSSafeSetLastNameIndex(ctxt);
if (ctxt->errorProc != NIL)
(*ctxt->errorProc)(ctxt, dps_err_resultTypeCheck, (long unsigned)obj, 0);
}
else if (type == DPS_STRING) {
if (result->count == -1) {
/* char * result, copy first, ignore subsequent, null terminate */
os_bcopy(((integer)(obj->val.stringVal)) + (char *)obj,
result->value, obj->length);
(result->value)[obj->length] = '\0';
result->count = 0;
}
else {
unsigned slen;
if (result->count >= (int) obj->length) {
/* copy entire string into char array */
slen = obj->length;
}
else if (result->count > 0) {
/* copy partial string into char array */
slen = result->count;
}
else return; /* ignore string result, no room left */
os_bcopy(((integer)(obj->val.stringVal)) + (char *)obj,
result->value, slen);
result->value += slen;
result->count -= slen;
}
return;
}
break;
default:
DPSSafeSetLastNameIndex(ctxt);
if (ctxt->errorProc != NIL)
(*ctxt->errorProc)(ctxt, dps_err_resultTypeCheck, (long unsigned)obj, 0);
} /* switch (result->type) */
if (bump == 0) {
DPSSafeSetLastNameIndex(ctxt);
if (ctxt->errorProc != NIL)
(*ctxt->errorProc)(ctxt, dps_err_resultTypeCheck, (long unsigned)obj, 0);
return;
}
if (result->count != -1) {
result->count--;
result->value += bump;
}
obj += 1;
nObjs--;
} while (nObjs > 0); /* do */
} /* StuffResultVal */
static void NumFormatFromTokenType(
unsigned char t,
DPSNumFormat *numFormat)
{
switch (t) {
case DPS_HI_IEEE:
*numFormat = dps_ieee;
break;
case DPS_LO_IEEE:
*numFormat = dps_ieee;
break;
case DPS_HI_NATIVE:
*numFormat = dps_native;
break;
case DPS_LO_NATIVE:
*numFormat = dps_native;
break;
default: DPSCantHappen();
}
}
#if !IEEEFLOAT
/* called to deal with results from the server */
static void ConvSeqInPlace(nObjs, currObj, base, tokenType)
integer nObjs;
DPSBinObj currObj;
char *base;
unsigned char tokenType;
{
DPSNumFormat numFormat;
NumFormatFromTokenType(tokenType, &numFormat);
while (nObjs--) {
unsigned char t = currObj->attributedType & 0x07f;
integer i;
switch (t) {
case DPS_REAL: {
float f;
if (numFormat == dps_ieee) {
if (DPSDefaultByteOrder == dps_hiFirst)
IEEEHighToNative(&currObj->val.realVal, &f);
else
IEEELowToNative(&currObj->val.realVal, &f);
}
else break; /* switch */
currObj->val.realVal = f;
break;
}
case DPS_ARRAY:
if (currObj->length > 0)
ConvSeqInPlace(currObj->length, (DPSBinObj)(base + currObj->val.arrayVal), base, tokenType);
break;
case DPS_NAME:
case DPS_STRING:
break;
default:;
} /* end switch */
++currObj;
} /* end while */
}
#endif /* !IEEEFLOAT */
boolean DPSKnownContext(DPSContext ctxt)
{
DPSPrivContext cc, c = (DPSPrivContext) ctxt;
DPSPrivSpace ss;
for (ss = spaces; ss != NIL; ss = ss->next)
for (cc = ss->firstContext; cc != NIL; cc = cc->next)
if (cc == c) return true;
return false;
}
boolean DPSKnownSpace(DPSSpace space)
{
DPSPrivSpace ss, s = (DPSPrivSpace) space;
for (ss = spaces; ss != NIL; ss = ss->next)
if (ss == s) return true;
return false;
}
void DPSclientPrintProc (
DPSContext ctxt,
char *buf,
unsigned nch)
{
DPSPrivContext cc = (DPSPrivContext) ctxt;
#define DPS_SEQ_MIN 2
DPSCheckInitClientGlobals();
if (cc == NIL) cc = (DPSPrivContext)dummyCtx;
if (cc == NIL) return;
if (nch == 0) { /* this is an EOF */
DPSAssertWarn(buf == NIL, cc, "non-nil output buffer with 0 length");
cc->eofReceived = true;
if (cc->objBuf) {
/* we were buffering; drop buffered chars on the floor */
free(cc->objBuf);
cc->objBuf = NIL;
cc->nObjBufChars = 0;
}
}
while (nch > 0) {
char *oldBuf = NIL;
unsigned oldNch = 0;
unsigned n;
if (cc->objBuf) { /* we're buffering */
unsigned long int m;
char *b = cc->objBuf + cc->nObjBufChars;
integer minSize;
while (cc->nObjBufChars < DPS_SEQ_MIN) {
if (nch == 0) return;
*b++ = *buf++;
++cc->nObjBufChars;
--nch;
}
b = cc->objBuf;
minSize = (*(b+1) == 0) ? DPS_EXT_HEADER_SIZE : DPS_HEADER_SIZE;
if (cc->nObjBufChars < minSize) {
if (nch + cc->nObjBufChars < (unsigned) minSize) {
os_bcopy(buf, b + cc->nObjBufChars, nch);
cc->nObjBufChars += nch;
return;
}
else {
os_bcopy(buf, b + cc->nObjBufChars, minSize - cc->nObjBufChars);
buf += minSize - cc->nObjBufChars;
nch -= minSize - cc->nObjBufChars;
cc->nObjBufChars = minSize;
}
}
if (minSize == DPS_HEADER_SIZE) {
unsigned short *sizeP = (unsigned short *)(cc->objBuf+2);
m = *sizeP;
}
else {
unsigned long *extSizeP = (unsigned long *)(cc->objBuf+4);
m = *extSizeP;
}
/* here with m = BOS total length in bytes, b = cc->objBuf */
cc->objBuf = (char *)realloc(b, m);
if (nch + cc->nObjBufChars < m) {
os_bcopy(buf, cc->objBuf + cc->nObjBufChars, nch);
cc->nObjBufChars += nch;
return;
}
else {
os_bcopy(buf, cc->objBuf + cc->nObjBufChars, m - cc->nObjBufChars);
buf += m - cc->nObjBufChars;
nch -= m - cc->nObjBufChars;
cc->nObjBufChars = m;
}
/* we're here only if cc->objBuf contains a complete BOS */
oldBuf = buf;
oldNch = nch;
buf = cc->objBuf;
nch = cc->nObjBufChars;
cc->objBuf = NIL;
cc->nObjBufChars = 0;
} /* if we're buffering */
/* dispose of any plain text. If no binary conversion, all output
is plain text */
if (cc->contextFlags & DPS_FLAG_NO_BINARY_CONVERSION) n = nch;
else {
for (n = 0; n < nch &&
((unsigned char) buf[n] < 128 || (unsigned char) buf[n] > 159); n++);
}
if ((n > 0) && (cc->textProc != NIL)) {
(*cc->textProc)((DPSContext)cc, buf, n);
}
buf += n;
nch -= n;
if (nch != 0) {
/* here with the next binary object sequence from a server */
DPSExtendedBinObjSeq bos;
DPSExtendedBinObjSeqRec bosRec;
DPSBinObj firstObj;
unsigned t;
unsigned long int m;
unsigned minSize;
if (nch < DPS_SEQ_MIN) {
/* gotta buffer it */
DPSAssertWarn(nch == 1 && !oldBuf, cc, "illegal binary output from context (oldBuf)");
cc->objBuf = (char *)DPScalloc(DPS_EXT_HEADER_SIZE, 1);
cc->nObjBufChars = nch;
*cc->objBuf = *buf;
return;
}
/* check for quadbyte alignment */
if ((long int)buf & (MIN_POINTER_ALIGN - 1)) {
/* not aligned, we gotta copy the buffer */
/* we assert that we can't have an oldBuf if we're not aligned,
since we only get an oldBuf if we copied to a new buffer,
and we have already tested nch to be at least DPS_SEQ_MIN */
DPSAssertWarn(!oldBuf && nch > 1, cc, "return values garbled (oldBuf||nch<DPS_SEQ_MIN");
/* copy DPS_SEQ_MIN bytes, so we can use existing buffering code */
cc->objBuf = (char *)DPScalloc(DPS_EXT_HEADER_SIZE, 1);
cc->nObjBufChars = DPS_SEQ_MIN;
os_bcopy(buf, cc->objBuf, cc->nObjBufChars);
buf += DPS_SEQ_MIN;
nch -= DPS_SEQ_MIN;
/* now go to top of loop and go through the buffer update code */
continue;
}
bos = (DPSExtendedBinObjSeq) buf;
t = bos->tokenType;
minSize = (bos->escape == 0) ? DPS_EXT_HEADER_SIZE : DPS_HEADER_SIZE;
if (nch < minSize) {
/* gotta buffer it */
char *tb;
DPSAssertWarn(!oldBuf, cc, "return values garbled (oldBuf)");
tb = cc->objBuf = (char *)DPScalloc(minSize, 1);
cc->nObjBufChars = nch;
while (nch--) *tb++ = *buf++;
return;
}
else if (minSize == DPS_HEADER_SIZE) {
/* this is not an extended BOS */
DPSBinObjSeq seqHead = (DPSBinObjSeq) buf;
bos = &bosRec;
bos->tokenType = t;
bos->nTopElements = seqHead->nTopElements;
bos->length = seqHead->length;
firstObj = &(seqHead->objects[0]);
}
else firstObj = &(bos->objects[0]);
m = bos->length;
if (nch < m) {
/* gotta buffer it */
DPSAssertWarn(!oldBuf, cc, "return values garbled (oldBuf&&nch<m");
cc->objBuf = (char *)DPScalloc(bos->length, 1);
cc->nObjBufChars = nch;
os_bcopy(buf, cc->objBuf, nch);
return;
}
DPSAssertWarn(bos->nTopElements == 1, cc, "illegal binary output detected (bos->nTopElements!=1)");
#if !IEEEFLOAT
if (t != DPS_DEF_TOKENTYPE)
ConvSeqInPlace(1, firstObj, firstObj, bos->tokenType);
#endif /* !IEEEFLOAT */
t = firstObj->tag;
if (t == DPS_ERR_TAG) {
cc->resultTable = NIL;
DPSSafeSetLastNameIndex((DPSContext)cc);
DURING
if (cc->errorProc != NIL)
(*cc->errorProc)((DPSContext)cc, dps_err_ps, (unsigned long)buf, m);
HANDLER
if (oldBuf) free(buf);
RERAISE;
END_HANDLER
}
else { /* dispatch this result */
if (!cc->resultTable || t > cc->resultTableLength) {
if (cc->chainParent == NIL && cc->errorProc != NIL) {
DPSSafeSetLastNameIndex((DPSContext)cc);
(*cc->errorProc)((DPSContext)cc, dps_err_resultTagCheck, (unsigned long)buf, m);
}
}
else if (t == cc->resultTableLength) {
cc->resultTable = NIL;
}
else {
StuffResultVal((DPSContext)cc, &cc->resultTable[t], t, firstObj);
}
}
if (!oldBuf)
buf += m;
nch -= m;
} /* if (nch != 0) ... the next binary object sequence from a server */
if (oldBuf) {
DPSAssertWarn(nch == 0, cc, "some return values/data lost (nch)");
free(buf);
buf = oldBuf;
nch = oldNch;
}
} /* while (nch > 0) */
} /* DPSclientPrintProc */
/**************************************/
/* Context procs for DPSCreateContext */
/**************************************/
static void procWaitContext(DPSContext ctxt)
{
typedef struct {
unsigned char tokenType;
unsigned char topLevelCount;
unsigned short int nBytes;
DPSBinObjGeneric obj0;
DPSBinObjGeneric obj1;
DPSBinObjGeneric obj2;
DPSBinObjGeneric obj3;
} DPSQ;
static DPSQ dpsF = {
DPS_DEF_TOKENTYPE, 4, sizeof(DPSQ),
{DPS_LITERAL|DPS_INT, 0, 0, -23}, /* arbitrary int */
{DPS_LITERAL|DPS_INT, 0, 0, 0}, /* termination tag = 0 */
{DPS_EXEC|DPS_NAME, 0, DPSSYSNAME, 119}, /* printobject */
{DPS_EXEC|DPS_NAME, 0, DPSSYSNAME, 70}, /* flush */
}; /* DPSQ */
DPSResultsRec DPSR;
if (DPSPrivateCheckWait(ctxt)) return;
ctxt->resultTable = &DPSR; /* must be non-null for handler to work */
ctxt->resultTableLength = 0; /* same value as termination tag */
DPSBinObjSeqWrite(ctxt, (char *) &dpsF,sizeof(DPSQ));
DPSAwaitReturnValues(ctxt);
}
static void procUpdateNameMap(DPSContext ctxt)
{
integer i;
DPSPrivContext c = (DPSPrivContext) ctxt;
DPSPrivSpace s = (DPSPrivSpace) ctxt->space;
DPSContext children = ctxt->chainChild;
/* unlink context from chain temporarily, so DPSPrintf can be called */
if (children != NIL) ctxt->chainChild = NIL;
DURING
for (i = s->lastNameIndex+1; i <= globLastNameIndex; i++)
DPSPrintf(ctxt, "%d /%s defineusername\n", i, userNames[i]);
HANDLER
if (children != NIL) ctxt->chainChild = children;
RERAISE;
END_HANDLER
c->lastNameIndex = globLastNameIndex;
if (children != NIL) {
/* update any children */
ctxt->chainChild = children;
DPSUpdateNameMap(ctxt->chainChild);
}
}
static void procWriteData(
DPSContext ctxt,
char *buf,
unsigned int count)
{
/* safe to call with chain */
DPSinnerProcWriteData(ctxt, buf, count);
if (ctxt->chainChild != NIL) DPSWriteData(ctxt->chainChild, buf, count);
}
static void procBinObjSeqWrite(
DPSContext ctxt,
char *buf,
unsigned int count)
{
if (((DPSPrivContext)ctxt)->lastNameIndex < globLastNameIndex) DPSUpdateNameMap(ctxt);
DPSinnerProcWriteData(ctxt, buf, count);
if (ctxt->chainChild != NIL) DPSBinObjSeqWrite(ctxt->chainChild, buf, count);
}
static void procWriteStringChars(
DPSContext ctxt,
char *buf,
unsigned int count)
{
DPSinnerProcWriteData(ctxt, buf, count);
if (ctxt->chainChild != NIL) DPSWriteStringChars(ctxt->chainChild, buf, count);
}
static void procWritePostScript(
DPSContext ctxt,
char *buf,
unsigned int count)
{
DPSinnerProcWriteData(ctxt, buf, count);
if (ctxt->chainChild != NIL) DPSWritePostScript(ctxt->chainChild, buf, count);
}
static void innerProcWriteNumstring(
DPSContext ctxt,
DPSDefinedType type,
char *data,
unsigned int size,
int scale,
void (*writeProc)(DPSContext, char *, unsigned))
{
unsigned char HNumHeader[4];
register int i;
#define NBUFSIZE 10
int ibuf[NBUFSIZE]; /* This needs to be a 32 bit datatype */
HNumHeader[0] = 149;
switch (type) {
case dps_tLong:
HNumHeader[1] = (DPS_DEF_TOKENTYPE % 2) * 128 + scale;
break;
case dps_tInt:
HNumHeader[1] = ((sizeof(int) >= 4) ? 0 : 32)
+ ((DPS_DEF_TOKENTYPE % 2) * 128) + scale;
break;
case dps_tShort:
HNumHeader[1] = 32 + ((DPS_DEF_TOKENTYPE % 2) * 128) + scale;
break;
case dps_tFloat:
HNumHeader[1] = 48 + ((DPS_DEF_TOKENTYPE % 2) * 128)
+ ((DPS_DEF_TOKENTYPE >= 130) ? 1 : 0);
break;
default:
break;
}
HNumHeader[(DPS_DEF_TOKENTYPE % 2) ? 2 : 3] = (unsigned char) size;
HNumHeader[(DPS_DEF_TOKENTYPE % 2) ? 3 : 2] = (unsigned char) (size >> 8);
(*writeProc)(ctxt, (char *)HNumHeader, 4);
switch (type) {
case dps_tLong:
if (sizeof(long) == 4) {
(*writeProc)(ctxt, (char *) data, size * 4);
} else {
while (size > 0) {
for (i = 0; i < NBUFSIZE && (unsigned) i < size; i++) {
ibuf[i] = ((long *) data)[i];
}
(*writeProc)(ctxt, (char *) ibuf,
4 * (size < NBUFSIZE ? size : NBUFSIZE));
size -= NBUFSIZE;
}
}
break;
case dps_tInt:
(*writeProc)(ctxt, (char *) data, size * sizeof(int));
break;
case dps_tShort:
(*writeProc)(ctxt, (char *) data, size * sizeof(short));
break;
case dps_tFloat:
(*writeProc)(ctxt, (char *) data, size * sizeof(float));
default:
break;
}
} /* innerProcWriteNumstring */
static void procWriteNumstring(
DPSContext ctxt,
DPSDefinedType type,
char *data,
unsigned int size,
int scale)
{
innerProcWriteNumstring(ctxt, type, data, size, scale, DPSinnerProcWriteData);
if (ctxt->chainChild != NIL) DPSWriteNumString(ctxt->chainChild, type, data, size, scale);
}
static void writeTypedObjectArray(
DPSContext ctxt,
DPSDefinedType type,
char *array,
unsigned int length)
{
#define DPSMAX_SEQ 10
unsigned int i;
DPSPrivContext c = (DPSPrivContext)(ctxt);
static DPSBinObjGeneric bboolObj[DPSMAX_SEQ] = {
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
{DPS_LITERAL | DPS_BOOL, 0,0,0},
};
static DPSBinObjReal rrealObj[DPSMAX_SEQ] = {
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
{DPS_LITERAL | DPS_REAL, 0,0,0},
};
static DPSBinObjGeneric iintObj[DPSMAX_SEQ] = {
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
{DPS_LITERAL | DPS_INT, 0,0,0},
};
if (DPSCheckShared(c)) return;
switch (type) {
case dps_tChar:
case dps_tUChar:
DPSCantHappen();
break;
case dps_tBoolean: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
bboolObj[i].val = *((int *)array);
array += sizeof(int);
}
DPSWritePostScript(ctxt, (char *) bboolObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
case dps_tFloat: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
rrealObj[i].realVal = *((float *)array);
array += sizeof(float);
}
DPSWritePostScript(ctxt, (char *) rrealObj, sizeof(DPSBinObjReal) * i);
length -= i;
}
break;
}
case dps_tDouble: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
rrealObj[i].realVal = *((double *)array);
array += sizeof(double);
}
DPSWritePostScript(ctxt, (char *) rrealObj, sizeof(DPSBinObjReal) * i);
length -= i;
}
break;
}
case dps_tShort: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
iintObj[i].val = *((short *)array);
array += sizeof(short);
}
DPSWritePostScript(ctxt, (char *) iintObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
case dps_tUShort: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
iintObj[i].val = *((unsigned short *)array);
array += sizeof(unsigned short);
}
DPSWritePostScript(ctxt, (char *) iintObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
case dps_tInt: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
iintObj[i].val = *((int *)array);
array += sizeof(int);
}
DPSWritePostScript(ctxt, (char *) iintObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
case dps_tUInt: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
iintObj[i].val = *((unsigned int *)array);
array += sizeof(unsigned int);
}
DPSWritePostScript(ctxt, (char *) iintObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
case dps_tLong: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
iintObj[i].val = *((long *)array);
array += sizeof(long);
}
DPSWritePostScript(ctxt, (char *) iintObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
case dps_tULong: {
while (length > 0) {
for (i = 0; i < MIN(DPSMAX_SEQ, length); i++) {
iintObj[i].val = *((unsigned long *)array);
array += sizeof(unsigned long);
}
DPSWritePostScript(ctxt, (char *) iintObj, sizeof(DPSBinObjGeneric) * i);
length -= i;
}
break;
}
default:;
}
} /* writeTypedObjectArray */
static void procDestroyContext(DPSContext ctxt)
{
DPSPrivContext c = (DPSPrivContext)ctxt, cc, prev;
DPSPrivSpace ss = (DPSPrivSpace)(c->space);
if (c->buf != NIL) {
ContextBuffer b = (ContextBuffer)(c->buf);
b->next = contextBuffers;
contextBuffers = b;
c->buf = NIL;
}
if (c->objBuf != NIL) {
free(c->objBuf);
c->objBuf = NIL;
}
DPSUnchainContext(ctxt);
prev = NIL;
DPSAssert(ss != NIL);
for (cc = ss->firstContext; (cc != NIL) && (cc != c); cc = cc->next)
prev = cc;
DPSAssert(cc != NIL);
DPSAssert(cc != prev);
if (prev == NIL) ss->firstContext = cc->next;
else {
prev->next = cc->next;
DPSAssert(prev->next != prev);
}
DPSPrivateDestroyContext(ctxt);
free(c);
}
static void procDestroySpace(DPSSpace space)
{
DPSPrivSpace ns, prevS, ss = (DPSPrivSpace)space;
integer sid = ss->sid;
while (ss->firstContext != NIL)
DPSDestroyContext((DPSContext)(ss->firstContext));
prevS = NIL;
for (ns = spaces; (ns != NIL) && (ns->sid != sid); ns = ns->next)
prevS = ns;
DPSAssert(ns != NIL);
DPSAssert(ns == ss);
if (prevS == NIL) spaces = ns->next;
else {
prevS->next = ns->next;
DPSAssert(prevS->next != prevS);
}
DPSPrivateDestroySpace(space);
free(ss);
}
static void procInterrupt(DPSContext ctxt)
{
DPSPrivContext c = (DPSPrivContext)ctxt;
DPSSendInterrupt((XDPSPrivContext)c->wh, c->cid, DPSclientPrintProc);
if (ctxt->chainChild != NIL) DPSInterruptContext(ctxt->chainChild);
}
/****************************************************************/
/* Procedures that support the DPSCreateTextContext context procs */
/****************************************************************/
/* precondition: (c >= 129 && c <= 159), which means that c is the
first byte of either a binary token or a binary object sequence */
static boolean IsBinaryToken(unsigned char c)
{
return (c != DPS_HI_IEEE && c != DPS_LO_IEEE
&& c != DPS_HI_NATIVE && c != DPS_LO_NATIVE);
}
/* returns the number of initial bytes of a given goody that are
required to figure out how many bytes are needed for the entire goody */
static unsigned GetHdrNBytes(unsigned char *t)
{
if (!IsBinaryToken(*t)) {
if (*(++t) == 0) return DPS_EXT_HEADER_SIZE;
else return DPS_HEADER_SIZE;
}
switch (*t) {
case 137:
case 142:
return(2);
case 143:
case 144:
return(3);
case 149:
return(4);
default:
if (*t > 149 && *t < 160) return(1); /* unassigned */
else return(1);
}
}
static void WriteHomogeneousArrayAsASCII(
DPSContext ctxt,
register unsigned char *buf)
{
Swap32Rec n32;
Swap16Rec n16;
register unsigned char *b;
unsigned char r = *buf++;
float f;
boolean hi = (r < 128);
if (!hi) r -= 128;
DPSPrintf(ctxt, "[ ");
b = n16.bytes;
if (hi) {Copy2SrcHi(buf, b);} else {Copy2SrcLo(buf, b);}
buf += 2;
if (r < 32) /* 32-bit fixed */
while (n16.u--) {
b = n32.bytes;
if (hi) {Copy4SrcHi(buf, b);} else {Copy4SrcLo(buf, b);}
if (r == 0)
DPSPrintf(ctxt, "%d ", n32.i);
else {
f = n32.i; /* convert to float */
n32.f = f / (1 << r); /* scale */
DPSPrintf(ctxt, "%g ", n32.f);
}
buf += 4;
}
else if (r < 48) { /* 16-bit fixed */
unsigned l = n16.u;
r -= 32;
while (l--) {
b = n16.bytes;
if (hi) {Copy2SrcHi(buf, b);} else {Copy2SrcLo(buf, b);}
if (r == 0)
DPSPrintf(ctxt, "%d ", n16.i);
else {
f = n16.i; /* convert to float */
n32.f = f / (1 << r); /* scale */
DPSPrintf(ctxt, "%g ", n32.f);
}
buf += 2;
}
}
else if (r == 48) /* 32-bit IEEE */
while (n16.u--) {
#if IEEEFLOAT
b = n32.bytes;
if (hi) {Copy4SrcHi(buf, b);} else {Copy4SrcLo(buf, b);}
#else /* IEEEFLOAT */
if (hi) IEEEHighToNative(buf, &n32.f);
else IEEELowToNative(buf, &n32.f);
#endif /* IEEEFLOAT */
DPSPrintf(ctxt, "%g ", n32.f);
buf += 4;
}
else if (r == 49) /* 32-bit native */
while (n16.u--) {
b = n32.bytes;
*b++ = *buf++,*b++ = *buf++,*b++ = *buf++,*b = *buf++;
DPSPrintf(ctxt, "%g ", n32.f);
}
else DPSCantHappen();
DPSPrintf(ctxt, "\n] ");
} /* WriteHomogeneousArrayAsASCII */
/* returns the number of bytes needed for the entire goody. buf points
to enough initial bytes of the goody to figure this out (see above). */
static integer GetNBytes(register unsigned char *buf)
{
unsigned short int nBytes;
register unsigned char *r = (unsigned char *)&nBytes;
switch (*buf) {
case DPS_HI_IEEE:
case DPS_HI_NATIVE:
if (*(buf+1) == 0) {
unsigned int nb;
r = (unsigned char *)&nb;
buf += 4;
Copy4SrcHi(buf, r);
return(nb);
}
else {
buf += 2;
Copy2SrcHi(buf, r);
}
break;
case DPS_LO_IEEE:
case DPS_LO_NATIVE:
if (*(buf+1) == 0) {
unsigned int nb;
r = (unsigned char *)&nb;
buf += 4;
Copy4SrcLo(buf, r);
return(nb);
}
else {
buf += 2;
Copy2SrcLo(buf, r);
}
break;
case 132:
case 133:
case 138:
case 139:
case 140:
nBytes = 5; break;
case 134:
case 135:
nBytes = 3; break;
case 136:
case 141:
case 145:
case 146:
case 147:
case 148:
nBytes = 2; break;
case 137: {
unsigned int l = *(buf+1);
if (l < 32) {nBytes = 6; break;}
if (l < 48) {nBytes = 4; break;}
if (l-128 < 32) {nBytes = 6; break;}
if (l-128 < 48) {nBytes = 4; break;}
DPSCantHappen();
}
case 142:
nBytes = *(buf+1);
nBytes += 2;
break;
case 143:
buf++;
Copy2SrcHi(buf, r);
nBytes += 3;
break;
case 144:
buf++;
Copy2SrcLo(buf, r);
nBytes += 3;
break;
case 149: {
unsigned char scale = *(buf+1);
buf += 2;
if (scale < 128)
Copy2SrcHi(buf, r);
else {
scale -= 128;
Copy2SrcLo(buf, r);
}
if (scale < 32)
nBytes *= 4;
else if (scale < 48)
nBytes *= 2;
else
nBytes *= 4;
nBytes += 4;
break;
}
default: nBytes = 1; /* unassigned */
}
return(nBytes);
}
static void WriteSeqAsAscii(
DPSContext ctxt,
char *base,
DPSBinObj currObj,
unsigned int nObjs,
unsigned char tokenType,
int *numstringOffsets)
{
integer nLineObjs = 0;
DPSNumFormat numFormat;
float f;
long int i;
unsigned short int length;
/* NOTE: It's ok to call DPSPrintf (which calls DPSWritePostScript)
from here since we are only sending text, so there's no problem
with re-entering DPSWritePostScript. Caller guarantees
that this context has no children. */
NumFormatFromTokenType(tokenType, &numFormat);
while (nObjs--) {
unsigned char type = currObj->attributedType & ~DPS_EXEC;
boolean lit = ((currObj->attributedType & DPS_EXEC) == 0);
switch (type) {
case DPS_NULL:
break;
case DPS_BOOL:
i = currObj->val.booleanVal;
if (i)
DPSPrintf(ctxt, "true ");
else
DPSPrintf(ctxt, "false ");
break;
case DPS_INT:
i = currObj->val.integerVal;
DPSPrintf(ctxt, "%d ", i);
break;
case DPS_REAL:
#if IEEEFLOAT
f = currObj->val.realVal;
#else /* IEEEFLOAT */
if (numFormat != DPSDefaultNumFormat)
#if SWAPBITS
IEEELowToNative(&currObj->val.realVal, &f);
#else /* SWAPBITS */
IEEEHighToNative(&currObj->val.realVal, &f);
#endif /* SWAPBITS */
else
f = currObj->val.realVal;
#endif /* IEEEFLOAT */
DPSPrintf(ctxt, "%g ", f);
break;
case DPS_NAME: {
char *p = 0;
integer index;
index = currObj->val.nameVal;
length = currObj->length;
if (lit) DPSPrintf(ctxt, "/");
if (length == DPSSYSNAME) { /* system name index */
if (index <= DPS_LAST_COMMON_SYSNAME) {
if (!lit && (ctxt->contextFlags & DPS_FLAG_USE_ABBREVS)) {
p = DPSGetSysnameAbbrev(index);
if (p == NULL) p = DPSSysNames[index];
} else p = DPSSysNames[index];
} else if (DPS_FIRST_AUX_SYSNAME <= index
&& index <= DPS_LAST_AUX_SYSNAME)
p = DPSSysNamesAux[index - DPS_FIRST_AUX_SYSNAME];
else DPSCantHappen();
length = strlen(p);
}
else if (length == 0) { /* user name index */
p = DPSNameFromIndex(index);
length = strlen(p);
}
else
p = base + index;
DPSWriteData(ctxt, p, length);
DPSPrintf(ctxt, " ");
break;
}
case DPS_ARRAY:
DPSPrintf(ctxt, (lit ? "[ " : "{ "));
WriteSeqAsAscii(
ctxt,
base,
(DPSBinObj)(base+currObj->val.arrayVal),
currObj->length,
tokenType, numstringOffsets);
DPSPrintf(ctxt, (lit ? " ] " : " } "));
break;
case DPS_MARK:
if (lit)
DPSPrintf(ctxt, "/mark ");
else
DPSPrintf(ctxt, "mark ");
break;
case DPS_STRING: {
char *p;
int j;
i = currObj->val.stringVal;
length = currObj->length;
p = base + i;
if (numstringOffsets != NULL) {
for (j = 2; j < numstringOffsets[1] &&
numstringOffsets[j] != i; j++) ;
if (numstringOffsets[j] == i) {
DPSAssert(*(unsigned char *) p++ == 149);
WriteHomogeneousArrayAsASCII(ctxt, (unsigned char *) p);
break;
}
}
DPSPrintf(ctxt, "(");
#ifdef VMS
WriteVMSStringBody(ctxt, p, length); /* write this */
#else /* VMS */
/* render string bytes correctly */
while (length--) {
char c = *p++;
if (c == '(' || c == ')' || c == '\\')
DPSPrintf(ctxt, "\\%c", c);
else if (c == '\n')
DPSPrintf(ctxt, "\\n");
else if (!(isascii(c) && isprint(c)))
DPSPrintf(ctxt, "\\%03.3o", (unsigned char) c);
else DPSWriteData(ctxt, &c, 1);
}
#endif /* VMS */
DPSPrintf(ctxt, ") ");
break;
}
default: DPSCantHappen();
}
currObj++;
if (++nLineObjs == 15) {
nLineObjs = 0;
DPSPrintf(ctxt, "\n ");
}
} /* end while */
DPSPrintf(ctxt, "\n");
} /* WriteSeqAsAscii */
static void ConvertAndWriteSeqAsData(
DPSContext ctxt,
char *bosBuf,
int pass)
{
DPSPrivContext cc = (DPSPrivContext) ctxt;
DPSExtendedBinObjSeq bos;
DPSExtendedBinObjSeqRec bosRec;
DPSNumFormat numFormat;
unsigned int offset, firstCharOffset;
unsigned int nameOffset;
unsigned int len = 0;
DPSBinObj currObj;
integer headSize;
char *seqBase;
if (*(bosBuf+1) != 0) {
/* not an extended BOS */
DPSBinObjSeq seqHead = (DPSBinObjSeq) bosBuf;
bos = &bosRec;
bos->tokenType = seqHead->tokenType;
bos->escape = seqHead->nTopElements;
bos->nTopElements = seqHead->nTopElements;
bos->length = seqHead->length;
currObj = &(seqHead->objects[0]);
seqBase = (char *) &(seqHead->objects[0]);
headSize = DPS_HEADER_SIZE;
}
else {
bos = (DPSExtendedBinObjSeq) bosBuf;
currObj = &(bos->objects[0]);
seqBase = (char *) &(bos->objects[0]);
headSize = DPS_EXT_HEADER_SIZE;
}
firstCharOffset = bos->length - headSize;
nameOffset = firstCharOffset;
/* Caller guarantees that this context has no children,
so it is okay to call DPSWriteData */
NumFormatFromTokenType(bos->tokenType, &numFormat);
/* Pass 0: If we're expanding name indices, find all name objects,
lookup their strings, sum the lengths.
Write the modified sequence header regardless.
Pass 1: we're converting indices to strings and/or
converting numbers. Write each modified object, and
subsidiary strings.
Pass 2: Find all name objects, lookup and write the strings. */
if (pass == 0 && ctxt->nameEncoding != dps_strings)
/* we're just converting numbers, so skip while loop */
offset = firstCharOffset;
else
offset = 0;
while (offset < firstCharOffset) {
DPSBinObjRec newObj;
unsigned char type = (currObj->attributedType & ~DPS_EXEC);
newObj = *currObj;
#if !IEEEFLOAT
if (type == DPS_REAL) {
if (numFormat != cc->numFormat) {
if (numFormat == dps_ieee) {
if (DPSDefaultByteOrder == dps_loFirst)
IEEELowToNative(&currObj->val.realVal, &newObj.val.realVal);
else
IEEEHighToNative(&currObj->val.realVal, &newObj.val.realVal);
}
else { /* numFormat is native */
if (DPSDefaultByteOrder == dps_loFirst)
NativeToIEEELow(&currObj->val.realVal, &newObj.val.realVal);
else
NativeToIEEEHigh(&currObj->val.realVal, &newObj.val.realVal);
}
}
}
#endif /* !IEEEFLOAT */
if (type == DPS_STRING && newObj.length > 0) {
/* keep track of where strings start */
firstCharOffset = ((unsigned) newObj.val.stringVal < firstCharOffset)
? newObj.val.stringVal : firstCharOffset;
}
if (type == DPS_NAME) {
if (newObj.length == DPSSYSNAME) { /* system name index, never expand to string body */
if (pass != 1) goto next_obj;
}
else if (newObj.length == 0) { /* user name index */
register char *p = DPSNameFromIndex(newObj.val.nameVal);
switch (pass) {
case 0: len += strlen(p); goto next_obj;
case 1:
if (ctxt->nameEncoding == dps_strings) {
newObj.length = strlen(p);
newObj.val.nameVal = nameOffset;
nameOffset += newObj.length;
}
break;
case 2: DPSWriteData(ctxt, p, strlen(p)); goto next_obj;
default:;
}
}
else { /* name is already a string */
/* keep track of where strings start */
firstCharOffset = ((unsigned) newObj.val.nameVal < firstCharOffset)
? newObj.val.nameVal : firstCharOffset;
}
} /* end if type == DPS_NAME */
if (pass == 1) {
DPSWriteData(ctxt, (char *) &newObj, sizeof(newObj));
}
next_obj:
offset += sizeof(newObj);
++currObj;
} /* end while */
/* finish pass */
switch (pass) {
case 0: {
unsigned char t;
/* write modified seqHead */
if (DPSDefaultByteOrder == dps_hiFirst && cc->numFormat == dps_ieee)
t = DPS_HI_IEEE;
else if (DPSDefaultByteOrder == dps_loFirst && cc->numFormat == dps_ieee)
t = DPS_LO_IEEE;
else if (DPSDefaultByteOrder == dps_hiFirst && cc->numFormat == dps_native)
t = DPS_HI_NATIVE;
else
t = DPS_LO_NATIVE;
DPSWriteData(ctxt, (char *) &t, 1);
if (headSize == DPS_HEADER_SIZE) {
unsigned short int nBytes;
unsigned char c = bos->nTopElements;
/* write top level count */
DPSWriteData(ctxt, (char *) &c, 1);
/* write nBytes */
nBytes = (ctxt->nameEncoding == dps_strings) ? bos->length + len :
bos->length;
DPSWriteData(ctxt, (char *)&nBytes, 2);
}
else {
unsigned int nBytes;
/* write escape code & top level count */
DPSWriteData(ctxt, (char *)&bos->escape, 3);
/* write nBytes */
nBytes = (ctxt->nameEncoding == dps_strings) ? bos->length + len :
bos->length;
DPSWriteData(ctxt, (char *)&nBytes, 4);
}
break;
}
case 1: {
char *stringStart = seqBase + firstCharOffset;
DPSWriteData(ctxt, stringStart, (bos->length - headSize - firstCharOffset));
break;
}
default:;
}
} /* ConvertAndWriteSeqAsData */
#define MIN16 -32768
#define MAX16 32767
/* TestHomogeneous will return a non-negative representation code 'r'
if all of the array elements are "integers", or all are "reals".
Will return -1 for any other case. */
static integer TestHomogeneous(
DPSBinObj aryObj,
unsigned short length,
DPSNumFormat numFormat)
{
integer tmp, r = -1;
while (length--) {
switch (aryObj->attributedType & ~DPS_EXEC) {
case DPS_INT:
#if SWAPBITS
tmp = (aryObj->val.integerVal < MIN16
|| aryObj->val.integerVal > MAX16) ? 128 : 128+32;
#else /* SWAPBITS */
tmp = (aryObj->val.integerVal < MIN16
|| aryObj->val.integerVal > MAX16) ? 0 : 32;
#endif /* SWAPBITS */
if ((r == -1) || ((r & 0x07F) == 32 && (tmp & 0x07F) == 0))
r = tmp; /* first element, or was 16-bit => bump to 32-bit */
else if ((r & 0x07F) == 0 && (tmp & 0x07F) == 32)
goto bump_obj; /* is 32-bit => stay 32-bit */
else if (r != tmp)
return(-1);
/* else fall thru, r == tmp */
break;
case DPS_REAL:
#if SWAPBITS
tmp = (numFormat == dps_ieee) ? 128+48 : 128+49;
#else /* SWAPBITS */
tmp = (numFormat == dps_ieee) ? 48 : 49;
#endif /* SWAPBITS */
if (r == -1)
r = tmp;
else if (r != tmp) return(-1);
break;
default: return(-1);
}
bump_obj:
++aryObj;
}
return(r);
} /* TestHomogeneous */
static void WriteSeqAsTokens(
DPSContext ctxt,
char *base,
DPSBinObj currObj,
unsigned int nObjs,
unsigned char tokenType,
int *numstringOffsets)
{
int nLineObjs = 0;
DPSNumFormat numFormat;
unsigned short length;
Swap32Rec n32;
Swap16Rec n16;
unsigned char c;
#define PB(byte) c = (byte),DPSWriteData(ctxt, (char *) &c, 1)
#if SWAPBITS
#define WTT(byte) PB((byte)+1)
#else /* SWAPBITS */
#define WTT(byte) PB(byte)
#endif /* SWAPBITS */
NumFormatFromTokenType(tokenType, &numFormat);
while (nObjs--) {
unsigned char type = currObj->attributedType & ~DPS_EXEC;
boolean lit = ((currObj->attributedType & DPS_EXEC) == 0);
switch (type) {
case DPS_NULL:
break;
case DPS_BOOL:
PB(141); /* boolean */
if (currObj->val.booleanVal)
PB(true);
else
PB(false);
break;
case DPS_INT:
n32.i = currObj->val.integerVal;
if (n32.i < MIN16 || n32.i > MAX16) {
WTT(132); /* 32-bit int */
DPSWriteData(ctxt, (char *) n32.bytes, 4);
}
else if (n32.i < -128 || n32.i > 127) {
WTT(134); /* 16-bit int */
n16.i = n32.i;
DPSWriteData(ctxt, (char *) n16.bytes, 2);
}
else {
WTT(136); /* 8-bit int */
PB(n32.i);
}
break;
case DPS_REAL:
#if IEEEFLOAT
WTT(138); /* 32-bit IEEE float */
#else /* IEEEFLOAT */
if (numFormat != DPSDefaultNumFormat)
/* then it must be IEEE */
WTT(138); /* 32-bit IEEE float */
else
PB(140); /* 32-bit native float */
#endif /* IEEEFLOAT */
DPSWriteData(ctxt, (char *) &currObj->val.realVal, 4);
break;
case DPS_NAME: {
char *p = 0;
integer index = currObj->val.nameVal;
length = currObj->length;
if (length == DPSSYSNAME) {/* system name index */
if (index >= 0 && index < 256) {
if (lit)
PB(145); /* literal system name */
else
PB(146); /* exec. system name */
PB(index);
goto next_obj;
}
else if (DPS_FIRST_AUX_SYSNAME <= index
&& index <= DPS_LAST_AUX_SYSNAME)
p = DPSSysNamesAux[index - DPS_FIRST_AUX_SYSNAME];
else DPSCantHappen();
length = strlen(p);
}
else if (length == 0) { /* user name index */
if (ctxt->nameEncoding == dps_indexed && index < 256) {
if (lit)
PB(147); /* literal user name index */
else
PB(148); /* executable user name index */
PB(index);
goto next_obj;
}
else {
p = DPSNameFromIndex(index);
length = strlen(p);
}
}
else
p = base + index;
if (lit) DPSPrintf(ctxt, "/");
DPSWriteData(ctxt, p, length);
DPSPrintf(ctxt, " ");
break;
}
case DPS_ARRAY: {
DPSBinObj aryObj = (DPSBinObj)(base+currObj->val.arrayVal);
integer r;
length = currObj->length;
if (lit && (r = TestHomogeneous(aryObj, length, numFormat)) != -1) {
PB(149); /* homogeneous number array */
PB(r);
DPSWriteData(ctxt, (char *) &length, 2);
if (r > 127) r -= 128;
while (length--) {
switch (r) {
case 0:
DPSWriteData(ctxt, (char *) &aryObj->val.integerVal, 4);
break;
case 32:
n16.i = aryObj->val.integerVal;
DPSWriteData(ctxt, (char *) n16.bytes, 2);
break;
case 48:
case 49:
DPSWriteData(ctxt, (char *) &aryObj->val.realVal, 4);
break;
default: DPSCantHappen();
}
++aryObj;
}
}
else {
DPSPrintf(ctxt, (lit ? "[ " : "{ "));
WriteSeqAsTokens(ctxt, base, aryObj, length, tokenType,
numstringOffsets);
DPSPrintf(ctxt, (lit ? " ] " : " } "));
}
break; }
case DPS_MARK:
if (lit)
DPSPrintf(ctxt, "/mark ");
else
DPSPrintf(ctxt, "mark ");
break;
case DPS_STRING: {
char *p = base + currObj->val.stringVal;
int i;
if (numstringOffsets != NULL) {
for (i = 2; i < numstringOffsets[1] &&
numstringOffsets[i] != currObj->val.stringVal; i++) ;
if (numstringOffsets[i] == currObj->val.stringVal) {
DPSAssert(*(unsigned char *) p == 149);
DPSWriteData(ctxt, p, length);
break;
}
}
length = currObj->length;
if (length < 256) {
PB(142); /* short string */
PB(length);
}
else {
WTT(143); /* long string */
DPSWriteData(ctxt, (char *) &length, 2);
}
DPSWriteData(ctxt, p, length);
break;
}
default: DPSCantHappen();
}
next_obj:
++currObj;
if (++nLineObjs == 15) {
nLineObjs = 0;
DPSPrintf(ctxt, "\n ");
}
} /* end while */
DPSPrintf(ctxt, "\n");
} /* WriteSeqAsTokens */
static void WriteTokenAsAscii(
DPSContext ctxt,
register unsigned char *buf)
{
Swap32Rec n32;
Swap16Rec n16;
register unsigned char *b;
switch (*buf++) {
case 132: /* 32-bit int, hi */
b = n32.bytes;
Copy4SrcHi(buf, b);
DPSPrintf(ctxt, "%d ", n32.i);
break;
case 133: /* 32-bit int, lo */
b = n32.bytes;
Copy4SrcLo(buf, b);
DPSPrintf(ctxt, "%d ", n32.i);
break;
case 134: /* 16-bit int, hi */
b = n16.bytes;
Copy2SrcHi(buf, b);
DPSPrintf(ctxt, "%d ", n16.i);
break;
case 135: /* 16-bit int, lo */
b = n16.bytes;
Copy2SrcLo(buf, b);
DPSPrintf(ctxt, "%d ", n16.i);
break;
case 136: /* 8-bit int, signed */
n32.i = (char) *buf;
DPSPrintf(ctxt, "%d ", n32.i);
break;
case 137: { /* 16 or 32-bit fixed */
unsigned char r = *buf++;
float f = 0.0;
boolean hi = (r < 128);
if (!hi) r -= 128;
if (r < 32) { /* 32-bit */
b = n32.bytes;
if (hi) {Copy4SrcHi(buf, b);} else {Copy4SrcLo(buf, b);}
if (r == 0) {
DPSPrintf(ctxt, "%d ", n32.i);
break;
}
else {
f = n32.i; /* convert to float */
goto do_scale;
}
}
else if (r < 48) { /* 16-bit */
b = n16.bytes;
if (hi) {Copy2SrcHi(buf, b);} else {Copy2SrcLo(buf, b);};
if (r == 0) {
DPSPrintf(ctxt, "%d ", n16.i);
break;
}
else {
r -= 32;
f = n16.i; /* convert to float */
goto do_scale;
}
}
else DPSCantHappen();
do_scale:
n32.f = f / (1 << r); /* scale */
DPSPrintf(ctxt, "%g ", n32.f);
break;
}
case 138: /* 32-bit IEEE float, hi */
#if IEEEFLOAT
b = n32.bytes;
Copy4SrcHi(buf, b);
#else /* IEEEFLOAT */
IEEEHighToNative(buf, &n32.f);
#endif /* IEEEFLOAT */
DPSPrintf(ctxt, "%g ", n32.f);
break;
case 139: /* 32-bit IEEE float, lo */
#if IEEEFLOAT
b = n32.bytes;
Copy4SrcLo(buf, b);
#else /* IEEEFLOAT */
IEEELowToNative(buf, &n32.f);
#endif /* IEEEFLOAT */
DPSPrintf(ctxt, "%g ", n32.f);
break;
case 140: /* 32-bit native float */
b = n32.bytes;
*b++ = *buf++,*b++ = *buf++,*b++ = *buf++,*b = *buf;
DPSPrintf(ctxt, "%g ", n32.f);
break;
case 141: /* boolean */
if (*buf)
DPSPrintf(ctxt, "true ");
else
DPSPrintf(ctxt, "false ");
break;
case 142: /* short string */
DPSPrintf(ctxt, "(");
n16.u = *buf++;
goto share_str_code;
case 143: /* long string, hi */
b = n16.bytes;
Copy2SrcHi(buf, b);
DPSPrintf(ctxt, "(");
buf += 2;
goto share_str_code;
case 144: /* long string, lo */
b = n16.bytes;
Copy2SrcLo(buf, b);
DPSPrintf(ctxt, "(");
buf += 2;
share_str_code:
#ifdef VMS
WriteVMSStringBody(ctxt, buf, n16.u); /* write this */
#else /* VMS */
/* render string bytes correctly */
while (n16.u--) {
unsigned char c = *buf++;
if (c == '(' || c == ')' || c == '\\')
DPSPrintf(ctxt, "\\%c", c);
else if (c == '\n')
DPSPrintf(ctxt, "\\n");
else if (!(isascii(c) && isprint(c)))
DPSPrintf(ctxt, "\\%03.3o", c);
else DPSWriteData(ctxt, (char *) &c, 1);
}
#endif /* VMS */
DPSPrintf(ctxt, ") ");
break;
case 145: /* literal system name index */
DPSPrintf(ctxt, "/%s ", DPSSysNames[*buf]);
break;
case 146: /* executable system name index */
DPSPrintf(ctxt, "%s ", DPSSysNames[*buf]);
break;
case 147: /* literal user name index */
DPSPrintf(ctxt, "/%s ", DPSNameFromIndex(*buf));
break;
case 148: /* executable user name index */
DPSPrintf(ctxt, "%s ", DPSNameFromIndex(*buf));
break;
case 149: { /* homogeneous number array */
WriteHomogeneousArrayAsASCII(ctxt, buf);
break;
}
default:; /* unassigned */
}
} /* WriteTokenAsAscii */
/* WriteEntireGoody converts an entire binary token or binary object
sequence as specified by ctxt's encoding parameters. Write the
converted bytes via DPSWriteData. buf points to the complete goody. */
static void WriteEntireGoody(
DPSContext ctxt,
unsigned char *buf,
int *numstringOffsets)
{
DPSExtendedBinObjSeq bos = (DPSExtendedBinObjSeq) buf;
DPSExtendedBinObjSeqRec bosRec;
DPSBinObj currObj;
DPSPrivContext cc = (DPSPrivContext) ctxt;
if (IsBinaryToken(*buf)) {
/* only supported conversion is binary token to ASCII */
WriteTokenAsAscii(ctxt, buf);
if (numstringOffsets) numstringOffsets[1] = 2;
return;
}
if (bos->escape != 0) {
/* not extended BOS */
DPSBinObjSeq seqHead = (DPSBinObjSeq) buf;
bos = &bosRec;
bos->tokenType = seqHead->tokenType;
bos->escape = seqHead->nTopElements;
bos->nTopElements = seqHead->nTopElements;
bos->length = seqHead->length;
currObj = &(seqHead->objects[0]);
}
else currObj = &(bos->objects[0]);
switch (ctxt->programEncoding) {
case dps_binObjSeq:
if (ctxt->nameEncoding == dps_strings) {
/* takes three passes to do conversions */
ConvertAndWriteSeqAsData(ctxt, (char *) buf, 0);
ConvertAndWriteSeqAsData(ctxt, (char *) buf, 1);
ConvertAndWriteSeqAsData(ctxt, (char *) buf, 2);
}
else if (bos->tokenType != DPS_DEF_TOKENTYPE
|| cc->numFormat != DPSDefaultNumFormat) {
/* first pass just writes modified seqHead */
ConvertAndWriteSeqAsData(ctxt, (char *) buf, 0);
/* second pass converts numbers and writes the sequence */
ConvertAndWriteSeqAsData(ctxt, (char *) buf, 1);
}
else DPSWriteData(ctxt, (char *) buf, bos->length);
break;
case dps_ascii:
case dps_encodedTokens: {
if (ctxt->programEncoding == dps_ascii)
{
WriteSeqAsAscii(
ctxt, (char *)currObj, currObj, bos->nTopElements,
bos->tokenType, numstringOffsets);
}
else
WriteSeqAsTokens(
ctxt, (char *)currObj, currObj, bos->nTopElements,
bos->tokenType, numstringOffsets);
DPSWriteData(ctxt, "\n", 1);
break;
}
default:;
}
if (numstringOffsets) numstringOffsets[1] = 2;
} /* WriteEntireGoody */
/**************************************/
/* Context procs for DPSCreateTextContext */
/**************************************/
static void textWriteData(DPSContext ctxt, char *buf, unsigned int nch)
{
(*ctxt->textProc)(ctxt, buf, nch);
if (ctxt->chainChild != NIL) DPSWriteData(ctxt->chainChild, buf, nch);
}
static void textFlushContext(DPSContext ctxt)
{
if (ctxt->chainChild != NIL) DPSFlushContext(ctxt->chainChild);
}
static void textInterruptContext(DPSContext ctxt)
{
if (ctxt->chainChild != NIL) DPSInterruptContext(ctxt->chainChild);
}
static void textDestroyContext(DPSContext ctxt)
{
DPSPrivContext c = (DPSPrivContext)ctxt;
DPSUnchainContext(ctxt);
free(c);
}
static void textInnerWritePostScript(
DPSContext ctxt, char *buf, unsigned int nch)
{
DPSPrivContext cc = (DPSPrivContext)ctxt;
while (nch > 0) {
char *oldBuf = NIL;
integer oldNch = 0;
unsigned n;
if (cc->outBuf) { /* we're buffering */
unsigned m;
integer bst;
if (!IsBinaryToken(cc->outBuf[0]) && cc->nOutBufChars < DPS_SEQ_MIN) {
char *tb = cc->outBuf + cc->nOutBufChars;
integer nn = DPS_SEQ_MIN - cc->nOutBufChars;
DPSAssert(nn == 1);
cc->nOutBufChars += nn;
nch -= nn;
*tb++ = *buf++;
}
bst = GetHdrNBytes((unsigned char *) cc->outBuf);
/* # bytes needed to determine size */
if (cc->nOutBufChars < bst) {
char *b = cc->outBuf;
if (nch + cc->nOutBufChars < (unsigned) bst) {
os_bcopy(buf, cc->outBuf + cc->nOutBufChars, nch);
cc->nOutBufChars += nch;
return;
}
os_bcopy(buf, b + cc->nOutBufChars, bst - cc->nOutBufChars);
buf += bst - cc->nOutBufChars;
nch -= bst - cc->nOutBufChars;
cc->nOutBufChars = bst;
m = GetNBytes((unsigned char *) cc->outBuf);
cc->outBuf = (char *)DPScalloc(m, 1);
os_bcopy(b, cc->outBuf, bst);
free(b);
}
else m = GetNBytes((unsigned char *) cc->outBuf);
/* here with size of entire goody in m and outBuf set up */
if (nch + cc->nOutBufChars < m) {
os_bcopy(buf, cc->outBuf + cc->nOutBufChars, nch);
cc->nOutBufChars += nch;
return;
}
os_bcopy(buf, cc->outBuf + cc->nOutBufChars, m - cc->nOutBufChars);
buf += m - cc->nOutBufChars;
nch -= m - cc->nOutBufChars;
cc->nOutBufChars = m;
oldBuf = buf;
oldNch = nch;
buf = cc->outBuf;
nch = cc->nOutBufChars;
cc->outBuf = NIL;
cc->nOutBufChars = 0;
} /* if (cc->outBuf) */
/* dispose of any plain text. If no binary conversion, all output
is plain text */
if (cc->contextFlags & DPS_FLAG_NO_BINARY_CONVERSION) n = nch;
else {
for (n = 0; n < nch &&
((unsigned char) buf[n] < 128 || (unsigned char) buf[n] > 159); n++);
}
if (n > 0) {
/* Assumes below that any error proc called uses dpsexcept.h
if it rips control away */
DURING
DPSWriteData((DPSContext)cc, buf, n);
HANDLER
if (oldBuf) free(buf);
RERAISE;
END_HANDLER
}
buf += n;
nch -= n;
if (nch != 0) {
/* here with the next binary object sequence or encoded token */
unsigned m = 0;
integer bst;
if (!IsBinaryToken(buf[0]) && nch < DPS_SEQ_MIN) {
/* gotta buffer it */
DPSAssertWarn(nch == 1 && !oldBuf, cc, "problem converting binary token/sequence (nch!=1||oldBuf)");
cc->outBuf = (char *)DPScalloc(DPS_EXT_HEADER_SIZE, 1);
cc->nOutBufChars = nch;
cc->outBuf[0] = *buf;
return;
}
bst = GetHdrNBytes((unsigned char *) buf);
if (nch < (unsigned)bst || nch < (m = GetNBytes((unsigned char *) buf))) {
/* gotta buffer it */
DPSAssertWarn(!oldBuf, cc, "problem converting binary token/sequence (oldBuf)");
if (nch < (unsigned) bst) {
cc->outBuf = (char *)DPScalloc(bst, 1);
}
else {
cc->outBuf = (char *)DPScalloc(m, 1);
}
cc->nOutBufChars = nch;
os_bcopy(buf, cc->outBuf, nch);
return;
}
/* Assumes below that any error proc called uses dpsexcept.h
if it rips control away */
DURING
WriteEntireGoody(ctxt, (unsigned char *) buf, cc->numstringOffsets);
HANDLER
if (oldBuf) {
DPSAssertWarn(nch == m, cc, "some converted PostScript language may be lost during error recovery (nch!=m)");
free(buf);
}
RERAISE;
END_HANDLER
if (oldBuf) {
DPSAssertWarn(nch == m, cc, "some converted PostScript language may be lost (nch!=m)");
free(buf);
buf = oldBuf;
nch = oldNch;
oldBuf = NIL;
}
else {
buf += m;
nch -= m;
}
} /* if (nch != 0) */
} /* while (nch > 0) */
} /* textInnerWritePostScript */
static void textWritePostScript(
DPSContext ctxt, char *buf, unsigned int nch)
{
DPSContext children = ctxt->chainChild;
/* disconnect temporarily so that high level procs can
be called safely */
if (children != NIL) ctxt->chainChild = NIL;
DURING
textInnerWritePostScript(ctxt, buf, nch);
HANDLER
if (children != NIL) ctxt->chainChild = children;
RERAISE;
END_HANDLER
if (children != NIL) {
ctxt->chainChild = children;
DPSWritePostScript(ctxt->chainChild, buf, nch);
}
}
static void textWriteStringChars(
DPSContext ctxt, char *buf, unsigned int nch)
{
DPSContext children = ctxt->chainChild;
if (DPSCheckShared((DPSPrivContext)ctxt)) return;
/* disconnect temporarily so that high level procs can
be called safely */
if (children != NIL) ctxt->chainChild = NIL;
DURING
textInnerWritePostScript(ctxt, buf, nch);
HANDLER
if (children != NIL) ctxt->chainChild = children;
RERAISE;
END_HANDLER
if (children != NIL) {
ctxt->chainChild = children;
DPSWriteStringChars(ctxt->chainChild, buf, nch);
}
}
static void textBinObjSeqWrite(
DPSContext ctxt, char *buf, unsigned int nch)
{
DPSContext children = ctxt->chainChild;
DPSPrivContext c = (DPSPrivContext) ctxt;
if (DPSCheckShared(c)) return;
if (c->lastNameIndex < globLastNameIndex)
DPSUpdateNameMap(ctxt);
/* disconnect temporarily so that high level procs can
be called safely */
if (children != NIL) ctxt->chainChild = NIL;
DURING
textInnerWritePostScript(ctxt, buf, nch);
HANDLER
if (children != NIL) ctxt->chainChild = children;
RERAISE;
END_HANDLER
if (children != NIL) {
ctxt->chainChild = children;
DPSBinObjSeqWrite(ctxt->chainChild, buf, nch);
}
}
static void textWriteNumstring(
DPSContext ctxt,
DPSDefinedType type,
char *data,
unsigned int size,
int scale)
{
DPSPrivContext cc = (DPSPrivContext) ctxt;
#define BUFFER_GROW 10
if (cc->contextFlags & DPS_FLAG_CONVERT_NUMSTRINGS) {
if (cc->numstringOffsets == NULL) {
cc->numstringOffsets = (int *) DPScalloc(sizeof(int),
BUFFER_GROW + 2);
cc->numstringOffsets[0] = BUFFER_GROW + 2; /* Current size */
cc->numstringOffsets[1] = 2; /* Next slot */
} else if (cc->numstringOffsets[1] >= cc->numstringOffsets[0]) {
cc->numstringOffsets[0] += BUFFER_GROW;
cc->numstringOffsets =
(int *) realloc(cc->numstringOffsets,
sizeof(int)* cc->numstringOffsets[0]);
}
/* Subtract 4 because of binary object sequence header */
cc->numstringOffsets[cc->numstringOffsets[1]] = cc->nOutBufChars - 4;
cc->numstringOffsets[1] += 1;
}
innerProcWriteNumstring(ctxt, type, data, size, scale, textInnerWritePostScript);
#undef BUFFER_GROW
} /* textWriteNumstring */
/*********************/
/* Public procedures */
/*********************/
/********************************************/
/* Public procs for dealing with user names */
char *DPSNameFromIndex(long int index)
{
if (DPSglobals == NIL || index < 0 || index > globLastNameIndex
|| userNameDict == NIL)
return NIL;
return (char *) userNames[index];
}
void DPSMapNames(
DPSContext ctxt,
unsigned int nNames,
char **names,
int **indices)
{
unsigned i;
char *last = names[0];
DPSCheckInitClientGlobals();
#define USERNAMEDICTLENGTH 100
if (userNameDict == NIL) {
userNameDict = DPSCreatePSWDict(USERNAMEDICTLENGTH);
userNames = (char **)DPScalloc(sizeof(char *), USERNAMEDICTLENGTH);
userNamesLength = USERNAMEDICTLENGTH;
}
for (i = 0; i < nNames; i++) {
integer j;
char *n = names[i];
DPSContext c;
if (n == NIL)
n = last;
else
last = n;
DPSAssert(n != NIL);
if (strlen(n) > 128) {
DPSSafeSetLastNameIndex(ctxt);
(*ctxt->errorProc)(ctxt, dps_err_nameTooLong, (unsigned long) n, strlen(n));
return;
}
j = DPSWDictLookup(userNameDict, n);
if (j >= 0) {
*(indices[i]) = j;
/* handle the case where another context in another space has
defined this name */
if (((DPSPrivContext)ctxt)->lastNameIndex < j)
DPSUpdateNameMap(ctxt);
}
else {
/* handle other cases where another context in another
space has defined names */
if (((DPSPrivContext)ctxt)->lastNameIndex < globLastNameIndex)
DPSUpdateNameMap(ctxt);
globLastNameIndex++;
if (((globLastNameIndex + 1) > userNamesLength)) {
char **t = (char **)DPScalloc(sizeof(char *),
userNamesLength + USERNAMEDICTLENGTH);
for (j = 0; j < userNamesLength; j++) {
t[j] = userNames[j];
}
free(userNames);
userNames = t;
userNamesLength += USERNAMEDICTLENGTH;
}
userNames[globLastNameIndex] = n;
DPSWDictEnter(userNameDict, n, globLastNameIndex);
*(indices[i]) = globLastNameIndex;
DPSPrintf(ctxt, "%d /%s defineusername\n", globLastNameIndex, n);
for (c = ctxt; c != NIL; c = c->chainChild)
((DPSPrivContext)c)->lastNameIndex = globLastNameIndex;
}
} /* for */
}
/**********************/
/* Other public procs */
void DPSDefaultErrorProc(
DPSContext ctxt,
DPSErrorCode errorCode,
long unsigned int arg1, long unsigned int arg2)
{
DPSTextProc textProc = DPSGetCurrentTextBackstop();
char *prefix = "%%[ Error: ";
char *suffix = " ]%%\n";
char *infix = "; OffendingCommand: ";
char *nameinfix = "User name too long; Name: ";
char *contextinfix = "Invalid context: ";
char *taginfix = "Unexpected wrap result tag: ";
char *typeinfix = "Unexpected wrap result type; tag: ";
switch (errorCode) {
case dps_err_ps: {
char *buf = (char *)arg1;
DPSBinObj ary = (DPSBinObj) (buf+DPS_HEADER_SIZE);
DPSBinObj elements;
char *error, *errorName;
integer errorCount, errorNameCount;
boolean resyncFlg;
if ((ary->attributedType & 0x7f) != DPS_ARRAY
|| ary->length != 4) {
DPSHandleBogusError(ctxt, prefix, suffix);
}
elements = (DPSBinObj)(((char *) ary) + ary->val.arrayVal);
errorName = (char *)(((char *) ary) + elements[1].val.nameVal);
errorNameCount = elements[1].length;
error = (char *)(((char *) ary) + elements[2].val.nameVal);
errorCount = elements[2].length;
resyncFlg = elements[3].val.booleanVal;
if (textProc != NIL) {
(*textProc)(ctxt, prefix, strlen(prefix));
(*textProc)(ctxt, errorName, errorNameCount);
(*textProc)(ctxt, infix, strlen(infix));
(*textProc)(ctxt, error, errorCount);
(*textProc)(ctxt, suffix, strlen(suffix));
}
if (resyncFlg && (ctxt != dummyCtx) && (ctxt != NULL)) {
#if 0 /* Postpone the raise 'til later to avoid RAISEing through Xlib */
RAISE(dps_err_ps, (char *) ctxt);
DPSCantHappen();
#else
DPSPrivContext cc = (DPSPrivContext) ctxt;
cc->resyncing = true;
#endif
}
break;
}
case dps_err_nameTooLong:
if (textProc != NIL) {
char *buf = (char *)arg1;
(*textProc)(ctxt, prefix, strlen(prefix));
(*textProc)(ctxt, nameinfix, strlen(nameinfix));
(*textProc)(ctxt, buf, arg2);
(*textProc)(ctxt, suffix, strlen(suffix));
}
break;
case dps_err_invalidContext:
if (textProc != NIL) {
char m[100];
(void) sprintf(m, "%s%s%ld%s", prefix, contextinfix, arg1, suffix);
(*textProc)(ctxt, m, strlen(m));
}
break;
case dps_err_resultTagCheck:
if (textProc != NIL) {
char m[100];
unsigned char tag = *((unsigned char *) arg1+1);
(void) sprintf(m, "%s%s%d%s", prefix, taginfix, tag, suffix);
(*textProc)(ctxt, m, strlen(m));
}
break;
case dps_err_resultTypeCheck:
if (textProc != NIL) {
char m[100];
unsigned char tag = *((unsigned char *) arg1+1);
(void) sprintf(m, "%s%s%d%s", prefix, typeinfix, tag, suffix);
(*textProc)(ctxt, m, strlen(m));
}
break;
default:
DPSDefaultPrivateHandler(ctxt, errorCode, arg1, arg2, prefix, suffix);
break;
}
} /* DPSDefaultErrorProc */
void DPSCheckRaiseError(DPSContext c)
{
DPSPrivContext cc = (DPSPrivContext) c;
if (cc != NULL && cc->resyncing) {
cc->resyncing = false;
RAISE(dps_err_ps, (char *) c);
DPSCantHappen();
}
}
/**************************************/
/* Public procs for creating contexts */
/* dps_err_invalidAccess is now defined for all clients. */
static void textAwaitReturnValues(DPSContext ctxt)
{
(*ctxt->errorProc)(ctxt, dps_err_invalidAccess, 0, 0);
}
static void Noop(void)
{
}
DPSContext DPSCreateTextContext(
DPSTextProc textProc, DPSErrorProc errorProc)
{
DPSPrivContext c;
if (DPSInitialize () != 0) return((DPSContext) NIL);
if (!textCtxProcs) {
textCtxProcs = (DPSProcs)DPScalloc(sizeof(DPSProcsRec), 1);
DPSInitCommonTextContextProcs(textCtxProcs);
DPSInitSysNames();
}
c = (DPSPrivContext)DPScalloc(sizeof(DPSPrivContextRec), 1);
c->textProc = textProc;
c->procs = textCtxProcs;
c->textProc = textProc;
c->errorProc = errorProc;
c->programEncoding = dps_ascii;
c->nameEncoding = dps_strings; /* don't write user name indices on a file */
c->contextFlags = DPS_FLAG_CONVERT_NUMSTRINGS; /* Convert by default */
c->numFormat = DPSDefaultNumFormat;
c->numstringOffsets = NULL;
c->lastNameIndex = -1;
/* Setup a dummy space */
if (textSpace == NIL)
{
textSpace = (DPSPrivSpace) DPScalloc(sizeof (DPSPrivSpaceRec), 1);
textSpace->procs = (DPSSpaceProcs) DPScalloc(sizeof (DPSSpaceProcsRec), 1);
textSpace->procs->DestroySpace = (DPSSpaceProc) Noop;
textSpace->lastNameIndex = -1;
DPSInitPrivateSpaceFields(textSpace);
}
c->space = (DPSSpace) textSpace;
DPSInitPrivateTextContextFields(c, textSpace);
return (DPSContext)c;
} /* DPSCreateTextContext */
static DPSContext CreateDummyContext(void)
{
DPSPrivContext c;
DPSCheckInitClientGlobals();
if (!dummyCtxProcs) {
dummyCtxProcs = (DPSProcs)DPScalloc(sizeof(DPSProcsRec), 1);
dummyCtxProcs->BinObjSeqWrite = (DPSContextBufProc) Noop;
dummyCtxProcs->WriteTypedObjectArray = (DPSContextTypedArrayProc) Noop;
dummyCtxProcs->WriteStringChars = (DPSContextBufProc) Noop;
dummyCtxProcs->WritePostScript = (DPSContextBufProc) Noop;
dummyCtxProcs->WriteData = (DPSContextBufProc) Noop;
dummyCtxProcs->FlushContext = (DPSContextProc) Noop;
dummyCtxProcs->ResetContext = (DPSContextProc) Noop;
dummyCtxProcs->WaitContext = (DPSContextProc) Noop;
dummyCtxProcs->UpdateNameMap = (DPSContextProc) Noop;
dummyCtxProcs->AwaitReturnValues = (DPSContextProc) Noop;
dummyCtxProcs->Interrupt = (DPSContextProc) Noop;
dummyCtxProcs->DestroyContext = (DPSContextProc) Noop;
dummyCtxProcs->WriteNumString = (DPSWriteNumStringProc) Noop;
}
c = (DPSPrivContext)DPScalloc(sizeof(DPSPrivContextRec), 1);
c->procs = dummyCtxProcs;
c->programEncoding = DPSDefaultProgramEncoding;
c->nameEncoding = DPSDefaultNameEncoding; /* don't care */
c->numFormat = DPSDefaultNumFormat;
c->lastNameIndex = -1;
c->numstringOffsets = NULL;
return (DPSContext)c;
} /* CreateDummyContext */
void DPSSetTextBackstop(DPSTextProc textProc)
{
DPSCheckInitClientGlobals();
if (!dummyCtx) dummyCtx = CreateDummyContext();
dummyCtx->textProc = textProc;
}
DPSTextProc DPSGetCurrentTextBackstop(void)
{
DPSCheckInitClientGlobals();
if (dummyCtx == NIL) return NIL;
else return dummyCtx->textProc;
}
void DPSSetErrorBackstop(DPSErrorProc errorProc)
{
DPSCheckInitClientGlobals();
if (!dummyCtx) dummyCtx = CreateDummyContext();
dummyCtx->errorProc = errorProc;
}
DPSErrorProc DPSGetCurrentErrorBackstop(void)
{
DPSCheckInitClientGlobals();
if (dummyCtx == NIL) return NIL;
else return dummyCtx->errorProc;
}
static void NoteInitFailure(DPSContext ctxt, char *buf, long unsigned length)
{
initFailed = -1;
}
int DPSInitialize(void)
{
DPSCheckInitClientGlobals();
if (!clientStarted) {
clientStarted = true;
initFailed = 0;
DPSInitClient(NoteInitFailure, ReleaseInput); /* may call DPSCreateContext */
/* textProc will not be used unless DPS initialization fails */
}
return initFailed;
}
DPSContext DPSCreateContext(
char *wh,
DPSTextProc textProc,
DPSErrorProc errorProc,
DPSSpace space)
{
DPSPrivSpace ss;
DPSPrivContext c;
if (DPSInitialize() != 0) return NIL;
if (!ctxProcs) {
ctxProcs = (DPSProcs)DPScalloc(sizeof(DPSProcsRec), 1);
ctxProcs->BinObjSeqWrite = procBinObjSeqWrite;
ctxProcs->WriteTypedObjectArray = writeTypedObjectArray;
ctxProcs->WriteStringChars = procWriteStringChars;
ctxProcs->WritePostScript = procWritePostScript;
ctxProcs->WriteData = procWriteData;
ctxProcs->UpdateNameMap = procUpdateNameMap;
ctxProcs->Interrupt = procInterrupt;
ctxProcs->WriteNumString = (DPSWriteNumStringProc) procWriteNumstring;
}
if (!spaceProcs) {
spaceProcs = (DPSSpaceProcs)DPScalloc(sizeof(DPSSpaceProcsRec), 1);
DPSInitCommonSpaceProcs(spaceProcs);
}
ss = (DPSPrivSpace)space;
if (ss == NIL) {
ss = (DPSPrivSpace)DPScalloc(sizeof(DPSPrivSpaceRec), 1);
ss->procs = spaceProcs;
ss->lastNameIndex = -1;
ss->next = spaces;
DPSAssert(ss->next != ss);
spaces = ss;
DPSInitPrivateSpaceFields(ss);
}
if (ss->wh == NIL) ss->wh = wh; /* KLUDGE to support DPSSendDestroySpace */
c = (DPSPrivContext)DPScalloc(sizeof(DPSPrivContextRec), 1);
c->procs = ctxProcs;
c->wh = wh;
c->textProc = textProc;
c->errorProc = errorProc;
c->programEncoding = DPSDefaultProgramEncoding;
c->nameEncoding = DPSDefaultNameEncoding;
c->lastNameIndex = -1;
c->space = (DPSSpace)ss;
c->numstringOffsets = NULL;
c->next = ss->firstContext;
DPSAssert(c->next != c);
ss->firstContext = c;
DPSInitPrivateContextFields(c, ss);
c->numFormat = DPSCreatePrivContext(
(XDPSPrivContext)wh,
(DPSContext)c,
(ContextPSID *)&c->cid,
(SpaceXID *)&ss->sid,
(space == NIL), DPSclientPrintProc);
if (c->numFormat == (DPSNumFormat) -1)
{ /* can't create the context */
if (space == NIL) {
spaces = ss->next;
free(ss);
}
else ss->firstContext = c->next;
free(c);
return NIL;
}
else return (DPSContext)c;
} /* DPSCreateContext */
char *DPSSetWh(
DPSContext ctxt,
char *newWh)
{
DPSPrivContext cc = (DPSPrivContext) ctxt;
char *tmp = cc->wh;
cc->wh = newWh;
return(tmp);
}
/*
The chainParent field is non-NIL if this context automatically receives
a copy of the PostScript code sent to the referenced (parent) context.
The chainChild field is non-NIL if this context automatically sends along
to the referenced (child) context a copy of any PostScript code received.
*/
int DPSChainContext(DPSContext parent, DPSContext child)
{
DPSContext cc = child->chainChild;
if (child->chainParent != NIL)
return -1; /* report an error */
/* insert new child between parent and existing children */
child->chainChild = parent->chainChild;
if (parent->chainChild != NIL) {
DPSAssertWarn(parent->chainChild->chainParent == parent, (DPSPrivContext)parent, "attempting to chain context on invalid chain");
child->chainChild->chainParent = child;
}
child->chainParent = parent;
parent->chainChild = child;
/* if child has children, recursively chain them */
if (cc != NIL) {
cc->chainParent = NIL;
(void) DPSChainContext(child, cc);
}
return 0;
}
void DPSUnchainContext(DPSContext ctxt)
{
DPSContext p = ctxt->chainParent;
DPSContext c = ctxt->chainChild;
if (p != NIL) { /* remove ctxt from parent's chain */
DPSAssertWarn(p->chainChild == ctxt, (DPSPrivContext)p, "attempting to unchain context from wrong chain (parent)");
p->chainChild = c;
ctxt->chainParent = NIL;
}
if (c != NIL) { /* remove ctxt's child (if any) from ctxt's chain */
DPSAssertWarn(c->chainParent == ctxt, (DPSPrivContext)c, "attempting to unchain context from wrong chain (child)");
c->chainParent = p;
ctxt->chainChild = NIL;
}
}
/****************/
/* Veneer procs */
void DPSAwaitReturnValues(DPSContext ctxt)
{
(*(ctxt)->procs->AwaitReturnValues)((ctxt));
}
void DPSDestroyContext(DPSContext ctxt)
{
(*(ctxt)->procs->DestroyContext)((ctxt));
}
void DPSDestroySpace(DPSSpace spc)
{
(*(spc)->procs->DestroySpace)((spc));
}
void DPSFlushContext(DPSContext ctxt)
{
(*(ctxt)->procs->FlushContext)((ctxt));
}
DPSContext DPSGetCurrentContext(void) { return DPSGlobalContext; }
void DPSInterruptContext(DPSContext ctxt)
{
(*(ctxt)->procs->Interrupt)((ctxt));
}
DPSContext DPSPrivCurrentContext(void) { return DPSGlobalContext; }
void DPSResetContext(DPSContext ctxt)
{
(*(ctxt)->procs->ResetContext)((ctxt));
}
void DPSSetResultTable(
register DPSContext ctxt,
DPSResults tbl,
unsigned int len)
{
(ctxt)->resultTable = (tbl);
(ctxt)->resultTableLength = (len);
}
void DPSSetContext(
DPSContext ctxt)
{
DPSGlobalContext = ctxt;
}
void DPSUpdateNameMap(
DPSContext ctxt)
{
(*(ctxt)->procs->UpdateNameMap)((ctxt));
}
void DPSWaitContext(
DPSContext ctxt)
{
(*(ctxt)->procs->WaitContext)(ctxt);
}
void DPSBinObjSeqWrite(
DPSContext ctxt,
char *buf,
unsigned int count)
{
(*(ctxt)->procs->BinObjSeqWrite)((ctxt), (buf), (count));
}
void DPSWriteData(
DPSContext ctxt,
char *buf,
unsigned int count)
{
(*(ctxt)->procs->WriteData)((ctxt), (buf), (count));
}
void DPSWritePostScript(
DPSContext ctxt,
char *buf,
unsigned int count)
{
(*(ctxt)->procs->WritePostScript)((ctxt), (buf), (count));
}
void DPSWriteStringChars(
DPSContext ctxt,
char *buf,
unsigned int count)
{
(*(ctxt)->procs->WriteStringChars)((ctxt), (buf), (count));
}
void DPSWriteNumString(ctxt, type, data, size, scale)
DPSContext ctxt;
DPSDefinedType type;
char *data;
unsigned int size;
int scale;
{
(*(ctxt)->procs->WriteNumString)((ctxt), (type), (data), (size), (scale));
}
void DPSWriteTypedObjectArray(ctxt, type, array, length)
DPSContext ctxt;
DPSDefinedType type;
char *array;
unsigned int length; {
(*(ctxt)->procs->WriteTypedObjectArray)((ctxt), (type), (array), (length));
}
void DPSInitCommonTextContextProcs(DPSProcs p)
{
p->BinObjSeqWrite = textBinObjSeqWrite;
p->WriteTypedObjectArray = writeTypedObjectArray;
p->WriteStringChars = textWriteStringChars;
p->WritePostScript = textWritePostScript;
p->WriteData = textWriteData;
p->FlushContext = textFlushContext;
p->ResetContext = (DPSContextProc) Noop;
p->WaitContext = (DPSContextProc) Noop;
p->UpdateNameMap = procUpdateNameMap;
p->AwaitReturnValues = textAwaitReturnValues;
p->Interrupt = textInterruptContext;
p->DestroyContext = textDestroyContext;
p->WriteNumString = (DPSWriteNumStringProc) textWriteNumstring;
}
void DPSInitCommonContextProcs(DPSProcs p)
{
p->BinObjSeqWrite = procBinObjSeqWrite;
p->WriteTypedObjectArray = writeTypedObjectArray;
p->WriteStringChars = procWriteStringChars;
p->WritePostScript = procWritePostScript;
p->WaitContext = procWaitContext;
p->DestroyContext = procDestroyContext;
p->WriteData = procWriteData;
p->UpdateNameMap = procUpdateNameMap;
p->Interrupt = procInterrupt;
p->WriteNumString = (DPSWriteNumStringProc) procWriteNumstring;
}
void DPSInitCommonSpaceProcs(DPSSpaceProcs p)
{
p->DestroySpace = procDestroySpace;
}
void DPSSetNumStringConversion(ctxt, flag)
DPSContext ctxt;
int flag;
{
if (flag) ctxt->contextFlags |= DPS_FLAG_CONVERT_NUMSTRINGS;
else ctxt->contextFlags &= ~DPS_FLAG_CONVERT_NUMSTRINGS;
}
void DPSSetWrapSynchronization(ctxt, flag)
DPSContext ctxt;
int flag;
{
if (flag) ctxt->contextFlags |= DPS_FLAG_SYNC;
else ctxt->contextFlags &= ~DPS_FLAG_SYNC;
}
void DPSSuppressBinaryConversion(ctxt, flag)
DPSContext ctxt;
int flag;
{
if (flag) ctxt->contextFlags |= DPS_FLAG_NO_BINARY_CONVERSION;
else ctxt->contextFlags &= ~DPS_FLAG_NO_BINARY_CONVERSION;
}
void DPSSetAbbrevMode(ctxt, flag)
DPSContext ctxt;
int flag;
{
if (flag) ctxt->contextFlags |= DPS_FLAG_USE_ABBREVS;
else ctxt->contextFlags &= ~DPS_FLAG_USE_ABBREVS;
}
DPSContextExtensionRec *DPSGetContextExtensionRec(ctxt, extensionId)
DPSContext ctxt;
int extensionId;
{
DPSContextExtensionRec *r = ctxt->extension;
while (r != NULL && r->extensionId != extensionId) r = r->next;
return r;
}
void DPSAddContextExtensionRec(ctxt, rec)
DPSContext ctxt;
DPSContextExtensionRec *rec;
{
rec->next = ctxt->extension;
ctxt->extension = rec;
}
DPSContextExtensionRec *DPSRemoveContextExtensionRec(ctxt, extensionId)
DPSContext ctxt;
int extensionId;
{
DPSContextExtensionRec *rret, **r = &ctxt->extension;
while (*r != NULL && (*r)->extensionId != extensionId) {
r = &(*r)->next;
}
rret = *r;
if (*r != NULL) *r = (*r)->next;
return rret;
}
int DPSGenerateExtensionRecID(void)
{
static int id = 1;
return id++;
}
DPSContextType DPSGetContextType(ctxt)
DPSContext ctxt;
{
DPSPrivContext c = (DPSPrivContext) ctxt;
if (c->procs == textCtxProcs) return dps_context_text;
else return dps_context_execution;
}
|