1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
|
/*
* registry.c --
*
* Implements the C level procedures handling the registry
*
*
* Copyright (c) 1996-1999 Andreas Kupries (andreas_kupries@users.sourceforge.net)
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL I LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
* SOFTWARE AND ITS DOCUMENTATION, EVEN IF I HAVE BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* I SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
* I HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*
* CVS: $Id: registry.c,v 1.58 2009/05/07 05:30:35 andreas_kupries Exp $
*/
#include "transformInt.h"
/*
* Code used to associate the registry with an interpreter.
*/
#define ASSOC "binTrf"
#ifdef TRF_DEBUG
int n = 0;
#endif
/*
* Possible values for 'flags' field in control structure.
*/
#define CHANNEL_ASYNC (1<<0) /* non-blocking mode */
/*
* Number of milliseconds to wait before firing an event to flush
* out information waiting in buffers (fileevent support).
*
* Relevant for only Tcl 8.0 and beyond.
*/
#define TRF_DELAY (5)
/*
* Structures used by an attached transformation procedure
*
* => Information stored for a single direction of the channel.
* => Information required by a result buffer.
* => Information stored for the complete channel.
*/
typedef struct _DirectionInfo_ {
Trf_ControlBlock control; /* control block of transformation */
Trf_Vectors* vectors; /* vectors used during the transformation */
} DirectionInfo;
/*
* Definition of the structure containing the information about the
* internal input buffer.
*/
typedef struct _SeekState_ SeekState;
typedef struct _ResultBuffer_ {
unsigned char* buf; /* Reference to the buffer area */
int allocated; /* Allocated size of the buffer area */
int used; /* Number of bytes in the buffer, <= allocated */
SeekState* seekState;
} ResultBuffer;
typedef struct _SeekConfig_ {
int overideAllowed; /* Boolean flag. If set the user may overide the
* standard policy with his own choice */
Trf_SeekInformation natural; /* Natural seek policy, copied from the
* transform definition */
Trf_SeekInformation chosen; /* Seek policy chosen from natural policy
* and the underlying channels; */
int identity; /* Flag, set if 'identity' was forced by the
* user. */
} SeekConfig;
struct _SeekState_ {
/* -- Integrity conditions --
*
* BufStartLoc == BufEndLoc implies ResultLength(&result) == 0.
* BufStartLoc == BufEndLoc implies UpLoc == BufStart.
*
* UP_CONVERT (DownLoc - AheadOffset) == BufEndLoc
*
* UpXLoc % seekState.used.numBytesTransform == 0
* <=> Transform may seek only in multiples of its input tuples.
*
* (DownLoc - AheadOffset) % seekState.used.numBytesDown == 0
* <=> Downstream channel operates in multiples of the transformation
* output tuples, except for possible offsets because of read ahead.
*
* UP_CONVERT (DownZero) == 0
*
* -- Integrity conditions --
*/
Trf_SeekInformation used; /* Seek policy currently in effect, might
* be chosen by user */
int allowed; /* Flag. Set for seekable transforms. Derived
* from the contents of 'used'. */
int upLoc; /* Current location of file pointer in the
* transformed stream. */
int upBufStartLoc; /* Same as above, for start of read buffer (result) */
int upBufEndLoc; /* See above, for the character after the end of the
* buffer. */
int downLoc; /* Current location of the file pointer in the channel
* downstream. */
int downZero; /* location downstream equivalent to UpLoc == 0 */
int aheadOffset; /* #Bytes DownLoc is after the down location of
* BufEnd. Values > 0 indicate incomplete data in the
* transform buffer itself. */
int changed; /* Flag, set if seeking occured with 'identity' set */
};
/** XXX change definition for 8.2, at compile time */
typedef struct _TrfTransformationInstance_ {
#ifdef USE_TCL_STUBS
int patchVariant; /* See transformInt.h, Trf_Registry */
#endif
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com> */
Tcl_Channel self; /* Our own channel handle */
Tcl_Channel parent; /* The channel we are stacked upon. Relevant
* only for values PATCH_ORIG and PATCH_832 of
* 'patchVariant', see above. */
int readIsFlushed; /* flag to note wether in.flushProc was called or not */
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com> */
int flags; /* currently CHANNEL_ASYNC or zero */
int watchMask; /* current TrfWatch mask */
int mode; /* mode of parent channel,
* OR'ed combination of
* TCL_READABLE, TCL_WRITABLE */
/* Tcl_Transformation standard; data required for all transformation
* instances.
*/
DirectionInfo in; /* information for transformation of read data */
DirectionInfo out; /* information for transformation of written data */
ClientData clientData; /* copy from entry->trfType->clientData */
/*
* internal result buffer used during transformations of incoming data.
* Stores results waiting for retrieval too, i.e. state information
* carried from call to call.
*/
ResultBuffer result;
/* Number of bytes written during a down transformation.
*/
int lastWritten;
/* Number of bytes stored during an up transformation
*/
int lastStored;
/* Timer for automatic push out of information sitting in various channel
* buffers. Used by the fileevent support. See 'ChannelHandler'.
*/
Tcl_TimerToken timer;
/* Information about the chosen and used seek policy and wether the user
* is allowed to change it. Runtime configuration.
*/
SeekConfig seekCfg;
/* More seek information, runtime state.
*/
SeekState seekState;
#ifdef TRF_STREAM_DEBUG
char* name; /* Name of transformation command */
unsigned long inCounter; /* Number of bytes read from below */
unsigned long outCounter; /* Number of bytes stored in 'result' */
#endif
} TrfTransformationInstance;
#ifdef TRF_STREAM_DEBUG
#define STREAM_IN(trans,blen,buf) {int i; for (i=0;i<(blen);i++,(trans)->inCounter++) {printf ("%p:%s:in_\t%d\t%02x\n", (trans), (trans)->name, (trans)->inCounter, 0xff & ((buf) [i]));}}
#define STREAM_OUT(trans,blen,buf) {int i; for (i=0;i<(blen);i++,(trans)->outCounter++) {printf ("%p:%s:out\t%d\t%02x\n", (trans), (trans)->name, (trans)->outCounter, 0xff & ((buf) [i]));}}
#else
#define STREAM_IN(t,bl,b)
#define STREAM_OUT(t,bl,b)
#endif
#define INCREMENT (512)
#define READ_CHUNK_SIZE 4096
#define TRF_UP_CONVERT(trans,k) \
(((k) / trans->seekState.used.numBytesDown) * trans->seekState.used.numBytesTransform)
#define TRF_DOWN_CONVERT(trans,k) \
(((k) / trans->seekState.used.numBytesTransform) * trans->seekState.used.numBytesDown)
#define TRF_IS_UNSEEKABLE(si) \
(((si).numBytesTransform == 0) || ((si).numBytesDown == 0))
#define TRF_SET_UNSEEKABLE(si) \
{(si).numBytesTransform = 0 ; (si).numBytesDown = 0;}
/*
* forward declarations of all internally used procedures.
*/
static Tcl_ChannelType*
AllocChannelType _ANSI_ARGS_ ((int* sizePtr));
static Tcl_ChannelType*
InitializeChannelType _ANSI_ARGS_ ((CONST char* name, int patchVariant));
static int
TrfUnregister _ANSI_ARGS_ ((Tcl_Interp* interp,
Trf_RegistryEntry* entry));
static void
TrfDeleteRegistry _ANSI_ARGS_ ((ClientData clientData, Tcl_Interp *interp));
static int
TrfExecuteObjCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp* interp,
int objc, struct Tcl_Obj* CONST objv []));
static void
TrfDeleteCmd _ANSI_ARGS_((ClientData clientData));
#if 0
static int
TrfInfoObjCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp* interp,
int objc, struct Tcl_Obj* CONST objv []));
#endif
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
*/
static int
TrfBlock _ANSI_ARGS_ ((ClientData instanceData, int mode));
static int
TrfClose _ANSI_ARGS_ ((ClientData instanceData, Tcl_Interp* interp));
static int
TrfInput _ANSI_ARGS_ ((ClientData instanceData,
char* buf, int toRead,
int* errorCodePtr));
static int
TrfOutput _ANSI_ARGS_ ((ClientData instanceData,
CONST84 char* buf, int toWrite,
int* errorCodePtr));
static int
TrfSeek _ANSI_ARGS_ ((ClientData instanceData, long offset,
int mode, int* errorCodePtr));
static void
TrfWatch _ANSI_ARGS_ ((ClientData instanceData, int mask));
static int
TrfGetFile _ANSI_ARGS_ ((ClientData instanceData, int direction,
ClientData* handlePtr));
static int
TrfGetOption _ANSI_ARGS_ ((ClientData instanceData, Tcl_Interp* interp,
CONST84 char* optionName, Tcl_DString* dsPtr));
static int
TrfSetOption _ANSI_ARGS_((ClientData instanceData, Tcl_Interp* interp,
CONST char* optionName, CONST char* value));
#ifdef USE_TCL_STUBS
static int
TrfNotify _ANSI_ARGS_((ClientData instanceData, int interestMask));
#endif
static int
TransformImmediate _ANSI_ARGS_ ((Tcl_Interp* interp, Trf_RegistryEntry* entry,
Tcl_Channel source, Tcl_Channel destination,
struct Tcl_Obj* CONST in,
Trf_Options optInfo));
static int
AttachTransform _ANSI_ARGS_ ((Trf_RegistryEntry* entry,
Trf_BaseOptions* baseOpt,
Trf_Options optInfo,
Tcl_Interp* interp));
static int
PutDestination _ANSI_ARGS_ ((ClientData clientData,
unsigned char* outString, int outLen,
Tcl_Interp* interp));
static int
PutDestinationImm _ANSI_ARGS_ ((ClientData clientData,
unsigned char* outString, int outLen,
Tcl_Interp* interp));
static int
PutTrans _ANSI_ARGS_ ((ClientData clientData,
unsigned char* outString, int outLen,
Tcl_Interp* interp));
static int
PutInterpResult _ANSI_ARGS_ ((ClientData clientData,
unsigned char* outString, int outLen,
Tcl_Interp* interp));
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
*/
static void
ChannelHandler _ANSI_ARGS_ ((ClientData clientData, int mask));
static void
ChannelHandlerTimer _ANSI_ARGS_ ((ClientData clientData));
#ifdef USE_TCL_STUBS
static Tcl_Channel
DownChannel _ANSI_ARGS_ ((TrfTransformationInstance* ctrl));
static int
DownSeek _ANSI_ARGS_ ((TrfTransformationInstance* ctrl, int offset, int mode));
static int
DownRead _ANSI_ARGS_ ((TrfTransformationInstance* ctrl,
char* buf, int toRead));
static int
DownWrite _ANSI_ARGS_ ((TrfTransformationInstance* ctrl,
char* buf, int toWrite));
static int
DownSOpt _ANSI_ARGS_ ((Tcl_Interp* interp,
TrfTransformationInstance* ctrl,
CONST char* optionName, CONST char* value));
static int
DownGOpt _ANSI_ARGS_ ((Tcl_Interp* interp,
TrfTransformationInstance* ctrl,
CONST84 char* optionName, Tcl_DString* dsPtr));
#define DOWNC(trans) (DownChannel (trans))
#define TELL(trans) (SEEK (trans, 0, SEEK_CUR))
#define SEEK(trans,off,mode) (DownSeek ((trans), (off), (mode)))
#define READ(trans,buf,toRead) (DownRead ((trans), (buf), (toRead)))
#define WRITE(trans,buf,toWrite) (DownWrite ((trans), (buf), (toWrite)))
#define SETOPT(i,trans,opt,val) (DownSOpt ((i), (trans), (opt), (val)))
#define GETOPT(i,trans,opt,ds) (DownGOpt ((i), (trans), (opt), (ds)))
#else
#define DOWNC(trans) ((trans)->parent)
#define TELL(trans) (SEEK (trans, 0, SEEK_CUR))
#define SEEK(trans,off,mode) (Tcl_Seek ((trans)->parent, (off), (mode)))
#define READ(trans,buf,toRead) (Tcl_Read ((trans)->parent, (buf), (toRead)))
#define WRITE(trans,buf,toWrite) (Tcl_Write ((trans)->parent, (buf), (toWrite)))
#define SETOPT(i,trans,opt,val) (Tcl_SetChannelOption ((i), (trans)->parent, (opt), (val)))
#define GETOPT(i,trans,opt,ds) (Tcl_GetChannelOption ((i), (trans)->parent, (opt), (ds)))
#endif
/* Convenience macro for allocation
* of new transformation instances.
*/
#define NEW_TRANSFORM \
(TrfTransformationInstance*) ckalloc (sizeof (TrfTransformationInstance));
/* Procedures to handle the internal timer.
*/
static void
TimerKill _ANSI_ARGS_ ((TrfTransformationInstance* trans));
static void
TimerSetup _ANSI_ARGS_ ((TrfTransformationInstance* trans));
static void
ChannelHandlerKS _ANSI_ARGS_ ((TrfTransformationInstance* trans, int mask));
/* Procedures to handle the internal read buffer.
*/
static void ResultClear _ANSI_ARGS_ ((ResultBuffer* r));
static void ResultInit _ANSI_ARGS_ ((ResultBuffer* r));
static int ResultLength _ANSI_ARGS_ ((ResultBuffer* r));
static int ResultCopy _ANSI_ARGS_ ((ResultBuffer* r,
unsigned char* buf, int toRead));
static void ResultDiscardAtStart _ANSI_ARGS_ ((ResultBuffer* r,
int n));
static void ResultAdd _ANSI_ARGS_ ((ResultBuffer* r,
unsigned char* buf, int toWrite));
/*
* Procedures to handle seeking information.
*/
static void
SeekCalculatePolicies _ANSI_ARGS_ ((TrfTransformationInstance* trans));
static void
SeekInitialize _ANSI_ARGS_ ((TrfTransformationInstance* trans));
static void
SeekClearBuffer _ANSI_ARGS_ ((TrfTransformationInstance* trans, int which));
static void
SeekSynchronize _ANSI_ARGS_ ((TrfTransformationInstance* trans,
Tcl_Channel parent));
static Tcl_Obj*
SeekStateGet _ANSI_ARGS_ ((Tcl_Interp* interp, SeekState* state));
static Tcl_Obj*
SeekConfigGet _ANSI_ARGS_ ((Tcl_Interp* interp, SeekConfig* cfg));
static void
SeekPolicyGet _ANSI_ARGS_ ((TrfTransformationInstance* trans,
char* policy));
#ifdef TRF_DEBUG
static void
SeekDump _ANSI_ARGS_ ((TrfTransformationInstance* trans, CONST char* place));
#define SEEK_DUMP(str) SeekDump (trans, #str)
#else
#define SEEK_DUMP(str)
#endif
/*
*------------------------------------------------------*
*
* TrfGetRegistry --
*
* ------------------------------------------------*
* Accessor to the interpreter associated registry
* of transformations.
* ------------------------------------------------*
*
* Sideeffects:
* Allocates and initializes the hashtable
* during the first call and associates it
* with the specified interpreter.
*
* Result:
* The internal registry of transformations.
*
*------------------------------------------------------*
*/
Trf_Registry*
TrfGetRegistry (interp)
Tcl_Interp* interp;
{
Trf_Registry* registry;
START (TrfGetRegistry);
registry = TrfPeekForRegistry (interp);
if (registry == (Trf_Registry*) NULL) {
registry = (Trf_Registry*) ckalloc (sizeof (Trf_Registry));
registry->registry = (Tcl_HashTable*) ckalloc (sizeof (Tcl_HashTable));
Tcl_InitHashTable (registry->registry, TCL_STRING_KEYS);
Tcl_SetAssocData (interp, ASSOC, TrfDeleteRegistry,
(ClientData) registry);
}
DONE (TrfGetRegistry);
return registry;
}
/*
*------------------------------------------------------*
*
* TrfPeekForRegistry --
*
* ------------------------------------------------*
* Accessor to the interpreter associated registry
* of transformations. Does not create the registry
* (in contrast to 'TrfGetRegistry').
* ------------------------------------------------*
*
* Sideeffects:
* None.
*
* Result:
* The internal registry of transformations.
*
*------------------------------------------------------*
*/
Trf_Registry*
TrfPeekForRegistry (interp)
Tcl_Interp* interp;
{
Tcl_InterpDeleteProc* proc;
START (TrfPeekForRegistry);
proc = TrfDeleteRegistry;
DONE (TrfPeekForRegistry);
return (Trf_Registry*) Tcl_GetAssocData (interp, ASSOC, &proc);
}
/*
*------------------------------------------------------*
*
* Trf_Register --
*
* ------------------------------------------------*
* Announce a transformation to the registry associated
* with the specified interpreter.
* ------------------------------------------------*
*
* Sideeffects:
* May create the registry. Allocates and
* initializes the structure describing
* the announced transformation.
*
* Result:
* A standard TCL error code.
*
*------------------------------------------------------*
*/
int
Trf_Register (interp, type)
Tcl_Interp* interp;
CONST Trf_TypeDefinition* type;
{
Trf_Registry* registry;
Trf_RegistryEntry* entry;
Tcl_HashEntry* hPtr;
int new;
START (Trf_Register);
PRINT ("(%p, \"%s\")\n", type, type->name); FL;
registry = TrfGetRegistry (interp);
/*
* Already defined ?
*/
hPtr = Tcl_FindHashEntry (registry->registry, (char*) type->name);
if (hPtr != (Tcl_HashEntry*) NULL) {
PRINT ("Already defined!\n"); FL;
DONE (Trf_Register);
return TCL_ERROR;
}
/*
* Check validity of given structure
*/
#define IMPLY(a,b) ((! (a)) || (b))
/* assert (type->options); */
assert (IMPLY(type->options != NULL, type->options->createProc != NULL));
assert (IMPLY(type->options != NULL, type->options->deleteProc != NULL));
assert (IMPLY(type->options != NULL, type->options->checkProc != NULL));
assert (IMPLY(type->options != NULL,
(type->options->setProc != NULL) ||
(type->options->setObjProc != NULL)));
assert (IMPLY(type->options != NULL, type->options->queryProc != NULL));
assert (type->encoder.createProc);
assert (type->encoder.deleteProc);
assert ((type->encoder.convertProc != NULL) ||
(type->encoder.convertBufProc != NULL));
assert (type->encoder.flushProc);
assert (type->encoder.clearProc);
assert (type->decoder.createProc);
assert (type->decoder.deleteProc);
assert ((type->decoder.convertProc != NULL) ||
(type->decoder.convertBufProc != NULL));
assert (type->decoder.flushProc);
assert (type->decoder.clearProc);
/*
* Generate command to execute transformations immediately or to generate
* filters.
*/
entry = (Trf_RegistryEntry*) ckalloc (sizeof (Trf_RegistryEntry));
entry->registry = registry;
entry->trfType = (Trf_TypeDefinition*) type;
entry->interp = interp;
#ifndef USE_TCL_STUBS
entry->transType = InitializeChannelType (type->name, -1);
#else
entry->transType = InitializeChannelType (type->name,
registry->patchVariant);
#endif
entry->trfCommand = Tcl_CreateObjCommand (interp, (char*) type->name,
TrfExecuteObjCmd,
(ClientData) entry, TrfDeleteCmd);
/*
* Add entry to internal registry.
*/
hPtr = Tcl_CreateHashEntry (registry->registry, (char*) type->name, &new);
Tcl_SetHashValue (hPtr, entry);
DONE (Trf_Register);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* Trf_Unregister --
*
* ------------------------------------------------*
* Removes the transformation from the registry
* ------------------------------------------------*
*
* Sideeffects:
* Releases the memory allocated in 'Trf_Register'.
*
* Result:
* A standard TCL error code.
*
*------------------------------------------------------*
*/
static int
TrfUnregister (interp, entry)
Tcl_Interp* interp;
Trf_RegistryEntry* entry;
{
Trf_Registry* registry;
Tcl_HashEntry* hPtr;
START (Trf_Unregister);
registry = TrfGetRegistry (interp);
hPtr = Tcl_FindHashEntry (registry->registry,
(char*) entry->trfType->name);
ckfree ((char*) entry->transType);
ckfree ((char*) entry);
Tcl_DeleteHashEntry (hPtr);
DONE (Trf_Unregister);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* TrfDeleteRegistry --
*
* ------------------------------------------------*
* Trap handler. Called by the Tcl core during
* interpreter destruction. Destroys the registry
* of transformations.
* ------------------------------------------------*
*
* Sideeffects:
* Releases the memory allocated in 'TrfGetRegistry'.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
TrfDeleteRegistry (clientData, interp)
ClientData clientData;
Tcl_Interp* interp;
{
Trf_Registry* registry = (Trf_Registry*) clientData;
START (TrfDeleteRegistry);
/*
* The commands are already deleted, therefore the hashtable is empty here.
*/
Tcl_DeleteHashTable (registry->registry);
ckfree ((char*) registry);
DONE (TrfDeleteRegistry);
}
/* (readable) shortcuts for calling the option processing vectors.
*/
#define CLT (entry->trfType->clientData)
#define OPT (entry->trfType->options)
#define CREATE_OPTINFO (OPT ? (*OPT->createProc) (CLT) : NULL)
#define DELETE_OPTINFO if (optInfo) (*OPT->deleteProc) (optInfo, CLT)
#define CHECK_OPTINFO(baseOpt) (optInfo ? (*OPT->checkProc) (optInfo, interp, &baseOpt, CLT) : TCL_OK)
#define SET_OPTION(opt,optval) (optInfo ? (*OPT->setProc) (optInfo, interp, opt, optval, CLT) : TCL_ERROR)
#define SET_OPTION_OBJ(opt,optval) (optInfo ? (*OPT->setObjProc) (optInfo, interp, opt, optval, CLT) : TCL_ERROR)
#define ENCODE_REQUEST(entry,optInfo) (optInfo ? (*OPT->queryProc) (optInfo, CLT) : 1)
/*
*------------------------------------------------------*
*
* TrfExecuteObjCmd --
*
* ------------------------------------------------*
* Implementation procedure for all transformations.
* Equivalent to 'TrfExecuteCmd', but using the new
* Object interfaces.
* ------------------------------------------------*
*
* Sideeffects:
* See 'TrfExecuteCmd'.
*
* Result:
* A standard TCL error code.
*
*------------------------------------------------------*
*/
static int
TrfExecuteObjCmd (clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp* interp;
int objc;
struct Tcl_Obj* CONST * objv;
{
/* (readable) shortcuts for calling the option processing vectors.
* as defined in 'TrfExecuteCmd'.
*/
int res, len;
/* Tcl_Channel source, destination;*/
/* int src_mode, dst_mode;*/
const char* cmd;
const char* option;
struct Tcl_Obj* optarg;
Trf_RegistryEntry* entry;
Trf_Options optInfo;
Trf_BaseOptions baseOpt;
int mode;
int wrong_mod2;
int wrong_number;
START (TrfExecuteObjCmd);
#ifdef TRF_DEBUG
{
int i;
for (i = 0; i < objc; i++) {
PRINT ("Argument [%03d] = \"%s\"\n",
i, Tcl_GetStringFromObj (objv [i], NULL)); FL;
}
}
#endif
baseOpt.attach = (Tcl_Channel) NULL;
baseOpt.attach_mode = 0;
baseOpt.source = (Tcl_Channel) NULL;
baseOpt.destination = (Tcl_Channel) NULL;
baseOpt.policy = (Tcl_Obj*) NULL;
entry = (Trf_RegistryEntry*) clientData;
cmd = Tcl_GetStringFromObj (objv [0], NULL);
objc --;
objv ++;
optInfo = CREATE_OPTINFO;
PRINT ("Processing options...\n"); FL; IN;
while ((objc > 0) && (*Tcl_GetStringFromObj (objv [0], NULL) == '-')) {
/*
* Process options, as long as they are found
*/
option = Tcl_GetStringFromObj (objv [0], NULL);
if (0 == strcmp (option, "--")) {
/* end of option list */
objc--, objv++;
break;
}
wrong_number = (objc < 2); /* option, but without argument */
optarg = objv [1];
objc -= 2;
objv += 2;
len = strlen (option);
if (len < 2)
goto unknown_option;
switch (option [1])
{
case 'a':
if (0 != strncmp (option, "-attach", len))
goto check_for_trans_option;
if (wrong_number) {
Tcl_AppendResult (interp, cmd, ": wrong # args, option \"", option, "\" requires an argument", (char*) NULL);
OT;
goto cleanup_after_error;
}
baseOpt.attach = Tcl_GetChannel (interp,
Tcl_GetStringFromObj (optarg, NULL),
&baseOpt.attach_mode);
if (baseOpt.attach == (Tcl_Channel) NULL) {
OT;
goto cleanup_after_error;
}
break;
case 'i':
if (0 != strncmp (option, "-in", len))
goto check_for_trans_option;
if (wrong_number) {
Tcl_AppendResult (interp, cmd, ": wrong # args, option \"", option, "\" requires an argument", (char*) NULL);
OT;
goto cleanup_after_error;
}
baseOpt.source = Tcl_GetChannel (interp,
Tcl_GetStringFromObj (optarg, NULL),
&mode);
if (baseOpt.source == (Tcl_Channel) NULL)
goto cleanup_after_error;
if (! (mode & TCL_READABLE)) {
Tcl_AppendResult (interp, cmd,
": source-channel not readable",
(char*) NULL);
OT;
goto cleanup_after_error;
}
break;
case 'o':
if (0 != strncmp (option, "-out", len))
goto check_for_trans_option;
if (wrong_number) {
Tcl_AppendResult (interp, cmd, ": wrong # args, option \"", option, "\" requires an argument", (char*) NULL);
OT;
goto cleanup_after_error;
}
baseOpt.destination = Tcl_GetChannel (interp,
Tcl_GetStringFromObj (optarg,
NULL),
&mode);
if (baseOpt.destination == (Tcl_Channel) NULL) {
OT;
goto cleanup_after_error;
}
if (! (mode & TCL_WRITABLE)) {
Tcl_AppendResult (interp, cmd,
": destination-channel not writable",
(char*) NULL);
OT;
goto cleanup_after_error;
}
break;
case 's':
if (0 != strncmp (option, "-seekpolicy", len))
goto check_for_trans_option;
if (wrong_number) {
Tcl_AppendResult (interp, cmd, ": wrong # args, option \"", option, "\" requires an argument", (char*) NULL);
OT;
goto cleanup_after_error;
}
baseOpt.policy = optarg;
Tcl_IncrRefCount (optarg);
break;
default:
check_for_trans_option:
if (wrong_number) {
Tcl_AppendResult (interp, cmd, ": wrong # args, all options require an argument", (char*) NULL);
OT;
goto cleanup_after_error;
}
if ((*OPT->setObjProc) == NULL) {
res = SET_OPTION (option, Tcl_GetStringFromObj (optarg, NULL));
} else {
res = SET_OPTION_OBJ (option, optarg);
}
if (res != TCL_OK) {
OT;
goto cleanup_after_error;
}
break;
} /* switch option */
} /* while options */
OT;
/*
* Check argument restrictions, insert defaults if necessary,
* execute the required operation.
*/
if ((baseOpt.attach != (Tcl_Channel) NULL) &&
((baseOpt.source != (Tcl_Channel) NULL) ||
(baseOpt.destination != (Tcl_Channel) NULL))) {
Tcl_AppendResult (interp, cmd,
": inconsistent options, -in/-out not allowed with -attach",
(char*) NULL);
PRINT ("Inconsistent options\n"); FL;
goto cleanup_after_error;
}
if ((baseOpt.attach == (Tcl_Channel) NULL) &&
baseOpt.policy != (Tcl_Obj*) NULL) {
Tcl_AppendResult (interp, cmd,
": inconsistent options, -seekpolicy ",
"not allowed without -attach",
(char*) NULL);
PRINT ("Inconsistent options\n"); FL;
goto cleanup_after_error;
}
if ((baseOpt.source == (Tcl_Channel) NULL) &&
(baseOpt.attach == (Tcl_Channel) NULL))
wrong_mod2 = 0;
else
wrong_mod2 = 1;
if (wrong_mod2 == (objc % 2)) {
Tcl_AppendResult (interp, cmd, ": wrong # args", (char*) NULL);
PRINT ("Wrong # args\n"); FL;
goto cleanup_after_error;
}
res = CHECK_OPTINFO (baseOpt);
if (res != TCL_OK) {
DELETE_OPTINFO;
PRINT ("Options contain errors\n"); FL;
DONE (TrfExecuteObjCmd);
return TCL_ERROR;
}
if (baseOpt.attach == (Tcl_Channel) NULL) /* TRF_IMMEDIATE */ {
/*
* Immediate execution of transformation requested.
*/
res = TransformImmediate (interp, entry,
baseOpt.source, baseOpt.destination,
objv [0], optInfo);
} else /* TRF_ATTACH */ {
/*
* User requested attachment of transformation procedure to a channel.
* In case of a stub-aware interpreter use that to check for the
* existence of the necessary patches ! Bail out if not.
*/
#ifdef USE_TCL_STUBS
if (Tcl_StackChannel == NULL) {
Tcl_AppendResult (interp, cmd, ": this feature (-attach) is not ",
"available as the required patch to the core ",
"was not applied", (char*) NULL);
DELETE_OPTINFO;
PRINT ("-attach not available\n"); FL;
DONE (TrfExecuteObjCmd);
return TCL_ERROR;
}
#endif
res = AttachTransform (entry, &baseOpt, optInfo, interp);
if (baseOpt.policy != (Tcl_Obj*) NULL) {
Tcl_DecrRefCount (baseOpt.policy);
baseOpt.policy = (Tcl_Obj*) NULL;
}
}
DELETE_OPTINFO;
DONE (TrfExecuteObjCmd);
return res;
unknown_option:
PRINT ("Unknown option \"%s\"\n", option); FL; OT;
Tcl_AppendResult (interp, cmd, ": unknown option '", option, "', should be '-attach/in/out' or '-seekpolicy'",
(char*) NULL);
/* fall through to cleanup */
cleanup_after_error:
DELETE_OPTINFO;
DONE (TrfExecuteObjCmd);
return TCL_ERROR;
}
/*
*------------------------------------------------------*
*
* TrfDeleteCmd --
*
* ------------------------------------------------*
* Trap handler. Called by the Tcl core during
* destruction of the command for invocation of a
* transformation.
* ------------------------------------------------*
*
* Sideeffects:
* Removes the transformation from the registry.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
TrfDeleteCmd (clientData)
ClientData clientData;
{
Trf_RegistryEntry* entry;
START (TrfDeleteCmd);
entry = (Trf_RegistryEntry*) clientData;
TrfUnregister (entry->interp, entry);
DONE (TrfDeleteCmd);
}
/*
*----------------------------------------------------------------------
*
* TrfInfoObjCmd --
*
* This procedure is invoked to process the "trfinfo" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
#if 0
static int
TrfInfoObjCmd (notUsed, interp, objc, objv)
ClientData notUsed; /* Not used. */
Tcl_Interp* interp; /* Current interpreter. */
int objc;
struct Tcl_Obj* CONST * objv;
{
/*
* trfinfo <channel>
*/
static char* subcmd [] = {
"seekstate", "seekcfg", NULL
};
enum subcmd {
TRFINFO_SEEKSTATE, TRFINFO_SEEKCFG
};
Tcl_Channel chan;
int mode, pindex;
char* chanName;
TrfTransformationInstance* trans;
if ((objc < 2) || (objc > 3)) {
Tcl_AppendResult (interp,
"wrong # args: should be \"trfinfo cmd channel\"",
(char*) NULL);
return TCL_ERROR;
}
chanName = Tcl_GetStringFromObj (objv [2], NULL);
chan = Tcl_GetChannel (interp, chanName, &mode);
if (chan == (Tcl_Channel) NULL) {
return TCL_ERROR;
}
if (Tcl_GetChannelType (chan)->seekProc != TrfSeek) {
/* No trf transformation, info not applicable.
*/
Tcl_AppendResult (interp,
"channel \"", chanName,
"\" is no transformation from trf",
(char*) NULL);
return TCL_ERROR;
}
/* Peek into the instance structure and return the requested
* information.
*/
if (Tcl_GetIndexFromObj(interp, objv [1], subcmd, "subcommand", 0,
&pindex) != TCL_OK) {
return TCL_ERROR;
}
trans = (TrfTransformationInstance*) Tcl_GetChannelInstanceData (chan);
switch (pindex) {
case TRFINFO_SEEKSTATE:
{
Tcl_Obj* state = SeekStateGet (interp, &trans->seekState);
if (state == NULL)
return TCL_ERROR;
Tcl_SetObjResult (interp, state);
return TCL_OK;
}
break;
case TRFINFO_SEEKCFG:
{
Tcl_Obj* cfg = SeekConfigGet (interp, &trans->seekCfg);
if (cfg == NULL)
return TCL_ERROR;
Tcl_SetObjResult (interp, cfg);
return TCL_OK;
}
break;
default:
/* impossible */
return TCL_ERROR;
}
/* We should not come to this place */
return TCL_ERROR;
}
#endif
/*
*------------------------------------------------------*
*
* TrfInit_Info --
*
* ------------------------------------------------*
* Register the 'info' command.
* ------------------------------------------------*
*
* Sideeffects:
* As of 'Tcl_CreateObjCommand'.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
int
TrfInit_Info (interp)
Tcl_Interp* interp;
{
#if 0
Tcl_CreateObjCommand (interp, "trfinfo", TrfInfoObjCmd,
(ClientData) NULL,
(Tcl_CmdDeleteProc *) NULL);
#endif
return TCL_OK;
}
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
*/
/*
*------------------------------------------------------*
*
* TrfBlock --
*
* ------------------------------------------------*
* Trap handler. Called by the generic IO system
* during option processing to change the blocking
* mode of the channel.
* ------------------------------------------------*
*
* Sideeffects:
* Forwards the request to the underlying
* channel.
*
* Result:
* 0 if successful, errno when failed.
*
*------------------------------------------------------*
*/
static int
TrfBlock (instanceData, mode)
ClientData instanceData;
int mode;
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
char block [2] = {0,0};
Tcl_Channel parent;
START (TrfBlock);
PRINT ("Mode = %d\n", mode); FL;
parent = DOWNC (trans);
if (mode == TCL_MODE_NONBLOCKING) {
trans->flags |= CHANNEL_ASYNC;
block [0] = '0';
} else {
trans->flags &= ~(CHANNEL_ASYNC);
block [0] = '1';
}
#ifndef USE_TCL_STUBS
Tcl_SetChannelOption (NULL, parent, "-blocking", block);
#else
if ((trans->patchVariant == PATCH_ORIG) ||
(trans->patchVariant == PATCH_82)) {
/*
* Both old-style patch and first integrated version of the patch
* require the transformation to pass the blocking mode to the
* channel downstream. The newest implementation (PATCH_832)
* handles this in the core.
*/
Tcl_SetChannelOption (NULL, parent, "-blocking", block);
}
#endif
DONE (TrfBlock);
return 0;
}
/*
*------------------------------------------------------*
*
* TrfClose --
*
* ------------------------------------------------*
* Trap handler. Called by the generic IO system
* during destruction of the transformation channel.
* ------------------------------------------------*
*
* Sideeffects:
* Releases the memory allocated in
* 'AttachTransform'.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static int
TrfClose (instanceData, interp)
ClientData instanceData;
Tcl_Interp* interp;
{
/*
* The parent channel will be removed automatically
* (if necessary and/or desired).
*/
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
Tcl_Channel parent;
START (TrfClose);
#ifndef USE_TCL_STUBS
if ((trans == (TrfTransformationInstance*) NULL) ||
(interp == (Tcl_Interp*) NULL)) {
/* Hack, prevent 8.0 from crashing upon exit if channels
* with transformations were left open during exit
*
* Suggested by Mikhail Teterin <mi@aldan.algebra.com> 25.11.1999.
*/
DONE (TrfClose);
return TCL_OK;
}
#endif
parent = DOWNC (trans);
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
* Remove event handler to underlying channel, this could
* be because we are closing for real, or being "unstacked".
*/
#ifndef USE_TCL_STUBS
Tcl_DeleteChannelHandler (parent, ChannelHandler, (ClientData) trans);
#else
if ((trans->patchVariant == PATCH_ORIG) ||
(trans->patchVariant == PATCH_82)) {
Tcl_DeleteChannelHandler (parent, ChannelHandler, (ClientData) trans);
}
/*
* PATCH_832 doesn't use channelhandlers for communication of events
* between the channels of stack anymore.
*/
#endif
TimerKill (trans);
/*
* Flush data waiting in transformation buffers to output.
* Flush input too, maybe there are side effects other
* parts do rely on (-> message digests).
*/
if (trans->mode & TCL_WRITABLE) {
PRINT ("out.flushproc\n"); FL;
trans->out.vectors->flushProc (trans->out.control,
(Tcl_Interp*) NULL,
trans->clientData);
}
if (trans->mode & TCL_READABLE) {
if (!trans->readIsFlushed) {
PRINT ("in_.flushproc\n"); FL;
trans->readIsFlushed = 1;
trans->in.vectors->flushProc (trans->in.control,
(Tcl_Interp*) NULL,
trans->clientData);
}
}
if (trans->mode & TCL_WRITABLE) {
PRINT ("out.deleteproc\n"); FL;
trans->out.vectors->deleteProc (trans->out.control, trans->clientData);
}
if (trans->mode & TCL_READABLE) {
PRINT ("in_.deleteproc\n"); FL;
trans->in.vectors->deleteProc (trans->in.control, trans->clientData);
}
ResultClear (&trans->result);
/*
* Complement to NEW_TRANSFORM in AttachChannel.
* [Bug 2788106].
*/
ckfree(trans);
DONE (TrfClose);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* TrfInput --
*
* ------------------------------------------------*
* Called by the generic IO system to convert read data.
* ------------------------------------------------*
*
* Sideeffects:
* As defined by the conversion.
*
* Result:
* A transformed buffer.
*
*------------------------------------------------------*
*/
static int
TrfInput (instanceData, buf, toRead, errorCodePtr)
ClientData instanceData;
char* buf;
int toRead;
int* errorCodePtr;
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
int gotBytes, read, i, res, copied, maxRead;
Tcl_Channel parent;
START (TrfInput);
PRINT ("trans = %p, toRead = %d\n", trans, toRead); FL;
parent = DOWNC (trans);
/* should assert (trans->mode & TCL_READABLE) */
gotBytes = 0;
SEEK_DUMP (TrfInput; Start);
while (toRead > 0) {
/* Loop until the request is satisfied
* (or no data available from below, possibly EOF).
*/
SEEK_DUMP (TrfInput; Loop_);
/* The position may be inside the buffer, and not at its start.
* Remove the superfluous data now. There was no need to do it
* earlier, as intervening seeks and writes could have discarded
* the buffer completely, seeked back to an earlier point in it, etc.
* We can be sure that the location is not behind its end!
* And for an empty buffer location and buffer start are identical,
* bypassing this code. See integrity constraints listed in the
* description of Trf_TransformationInstance.
*/
if (trans->seekState.upLoc > trans->seekState.upBufStartLoc) {
ResultDiscardAtStart (&trans->result,
trans->seekState.upLoc - trans->seekState.upBufStartLoc);
}
/* Assertion: UpLoc == UpBufStartLoc now. */
SEEK_DUMP (TrfInput; Disc<);
copied = ResultCopy (&trans->result, (unsigned char*) buf, toRead);
toRead -= copied;
buf += copied;
gotBytes += copied;
trans->seekState.upLoc += copied;
SEEK_DUMP (TrfInput; Copy<);
if (toRead == 0) {
PRINT ("Got %d, satisfied from result buffer\n", gotBytes); FL;
DONE (TrfInput);
return gotBytes;
}
/* The buffer is exhausted, but the caller wants even more. We now have
* to go to the underlying channel, get more bytes and then transform
* them for delivery. We may not get that we want (full EOF or temporary
* out of data). This part has to manipulate the various seek locations
* in a more complicated way to keep everything in sync.
*/
/* Assertion: UpLoc == UpBufEndLoc now (and == UpBufStartLoc).
* Additionally: UP_CONVERT (DownLoc - AheadOffset) == BufEndLoc
*/
/*
* Length (trans->result) == 0, toRead > 0 here Use 'buf'! as target
* to store the intermediary information read from the parent channel.
*
* Ask the transform how much data it allows us to read from
* the underlying channel. This feature allows the transform to
* signal EOF upstream although there is none downstream. Useful
* to control an unbounded 'fcopy' for example, either through counting
* bytes, or by pattern matching.
*/
if (trans->in.vectors->maxReadProc == (Trf_QueryMaxRead*) NULL)
maxRead = -1;
else
maxRead = trans->in.vectors->maxReadProc (trans->in.control,
trans->clientData);
if (maxRead >= 0) {
if (maxRead < toRead) {
toRead = maxRead;
}
} /* else: 'maxRead < 0' == Accept the current value of toRead */
if (toRead <= 0) {
PRINT ("Got %d, constrained by script\n", gotBytes); FL;
DONE (TrfInput);
return gotBytes;
}
PRINT ("Read from parent %p\n", parent);
IN; IN;
read = READ (trans, buf, toRead);
OT; OT;
PRINT ("................\n");
/*PRTSTR ("Retrieved = {%d, \"%s\"}\n", read, buf);*/
PRINT ("Retrieved = %d {\n", read);
DUMP (read, buf);
PRINT ("}\n");
STREAM_IN (trans, read, buf);
if (read < 0) {
/* Report errors to caller.
* The state of the seek system is unchanged!
*/
if ((Tcl_GetErrno () == EAGAIN) && (gotBytes > 0)) {
/* EAGAIN is a special situation. If we had some data
* before we report that instead of the request to re-try.
*/
PRINT ("Got %d, read < 0, <EAGAIN>\n", gotBytes);
FL; DONE (TrfInput);
return gotBytes;
}
*errorCodePtr = Tcl_GetErrno ();
PRINT ("Got %d, read < 0, report error %d\n", gotBytes, *errorCodePtr);
FL; DONE (TrfInput);
return -1;
}
if (read == 0) {
/* Check wether we hit on EOF in 'parent' or
* not. If not differentiate between blocking and
* non-blocking modes. In non-blocking mode we ran
* temporarily out of data. Signal this to the caller
* via EWOULDBLOCK and error return (-1). In the other
* cases we simply return what we got and let the
* caller wait for more. On the other hand, if we got
* an EOF we have to convert and flush all waiting
* partial data.
*/
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
*/
if (! Tcl_Eof (parent)) {
/* The state of the seek system is unchanged! */
if (gotBytes == 0 && trans->flags & CHANNEL_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
PRINT ("Got %d, report EWOULDBLOCK\n", gotBytes); FL;
DONE (TrfInput);
return -1;
} else {
PRINT ("(Got = %d || not async)\n", gotBytes); FL;
DONE (TrfInput);
return gotBytes;
}
} else {
PRINT ("EOF in downstream channel\n"); FL;
if (trans->readIsFlushed) {
/* The state of the seek system is unchanged! */
/* already flushed, nothing to do anymore */
PRINT ("Got %d, !read flushed\n", gotBytes); FL;
DONE (TrfInput);
return gotBytes;
}
/* Now this is a bit different. The partial data waiting is converted
* and returned. So the 'AheadOffset' changes despite the location
* downstream not changing at all. It is now the negative of its
* additive inverse modulo 'numBytesDown':
* -((-k)%n) == -((n-1)-k) == k+1-n.
*/
PRINT ("in_.flushproc\n"); FL;
trans->readIsFlushed = 1;
trans->lastStored = 0;
res = trans->in.vectors->flushProc (trans->in.control,
(Tcl_Interp*) NULL,
trans->clientData);
if (trans->seekState.allowed &&
trans->seekState.used.numBytesDown > 1) {
trans->seekState.aheadOffset += -trans->seekState.used.numBytesDown;
}
SEEK_DUMP (TrfInput; AhdC<);
if (ResultLength (&trans->result) == 0) {
/* we had nothing to flush */
PRINT ("Got %d, read flushed / no result\n", gotBytes); FL;
DONE (TrfInput);
return gotBytes;
}
continue; /* at: while (toRead > 0) */
}
} /* read == 0 */
/* Transform the read chunk, which was not empty.
* The transformation processes 'read + aheadOffset' bytes.
* So UP_CONVERT (read+ahead) == #bytes produced == ResultLength!
* And (read+ahead) % #down == #bytes now waiting == new ahead.
*/
SEEK_DUMP (TrfInput; Read<);
trans->lastStored = 0;
if (trans->in.vectors->convertBufProc){
PRINT ("in_.convertbufproc\n"); FL;
res = trans->in.vectors->convertBufProc (trans->in.control,
(unsigned char*) buf, read,
(Tcl_Interp*) NULL,
trans->clientData);
} else {
PRINT ("in_.convertproc\n"); FL;
res = TCL_OK;
for (i=0; i < read; i++) {
res = trans->in.vectors->convertProc (trans->in.control, buf [i],
(Tcl_Interp*) NULL,
trans->clientData);
if (res != TCL_OK) {
break;
}
}
}
if (res != TCL_OK) {
*errorCodePtr = EINVAL;
PRINT ("Got %d, report error in transform (EINVAL)\n", gotBytes); FL;
DONE (TrfInput);
return -1;
}
/* Assert: UP_CONVERT (read+ahead) == ResultLength! */
trans->seekState.downLoc += read;
if (trans->seekState.allowed) {
trans->seekState.aheadOffset += (read % trans->seekState.used.numBytesDown);
trans->seekState.aheadOffset %= trans->seekState.used.numBytesDown;
}
} /* while toRead > 0 */
SEEK_DUMP (TrfInput; Loop<);
PRINT ("Got %d, after loop\n", gotBytes); FL;
DONE (TrfInput);
return gotBytes;
}
/*
*------------------------------------------------------*
*
* TrfOutput --
*
* ------------------------------------------------*
* Called by the generic IO system to convert data
* waiting to be written.
* ------------------------------------------------*
*
* Sideeffects:
* As defined by the transformation.
*
* Result:
* A transformed buffer.
*
*------------------------------------------------------*
*/
static int
TrfOutput (instanceData, buf, toWrite, errorCodePtr)
ClientData instanceData;
CONST84 char* buf;
int toWrite;
int* errorCodePtr;
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
int i, res;
Tcl_Channel parent;
START (TrfOutput);
parent = DOWNC (trans);
/* should assert (trans->mode & TCL_WRITABLE) */
/*
* transformation results are automatically written to
* the parent channel ('PutDestination' was configured
* as write procedure in 'AttachTransform').
*/
if (toWrite == 0) {
/* Nothing came in to write, ignore the call
*/
PRINT ("Nothing to write\n"); FL; DONE (TrfOutput);
return 0;
}
SEEK_DUMP (TrfOutput; Start);
/* toWrite / seekState.used.numBytesTransform = #tuples converted.
* toWrite % seekState.used.numBytesTransform = #Bytes waiting in the transform.
*/
SeekSynchronize (trans, parent);
SEEK_DUMP (TrfOutput; Syncd);
trans->lastWritten = 0;
if (trans->out.vectors->convertBufProc){
PRINT ("out.convertbufproc\n"); FL;
res = trans->out.vectors->convertBufProc (trans->out.control,
(unsigned char*) buf, toWrite,
(Tcl_Interp*) NULL,
trans->clientData);
} else {
PRINT ("out.convertproc\n"); FL;
res = TCL_OK;
for (i=0; i < toWrite; i++) {
res = trans->out.vectors->convertProc (trans->out.control, buf [i],
(Tcl_Interp*) NULL,
trans->clientData);
if (res != TCL_OK) {
break;
}
}
}
if (res != TCL_OK) {
*errorCodePtr = EINVAL;
PRINT ("error EINVAL\n"); FL; DONE (TrfInput);
return -1;
}
/* Update seek state to new location
* Assert: lastWritten == TRF_DOWN_CONVERT (trans, toWrite)
*/
trans->seekState.upLoc += toWrite;
trans->seekState.upBufStartLoc = trans->seekState.upLoc;
trans->seekState.upBufEndLoc = trans->seekState.upLoc;
trans->seekState.downLoc += trans->lastWritten;
trans->lastWritten = 0;
SEEK_DUMP (TrfOutput; Done_);
/* In the last statement above the integer division automatically
* strips off the #bytes waiting in the transform.
*/
PRINT ("Written: %d\n", toWrite); FL; DONE (TrfOutput);
return toWrite;
}
/*
*------------------------------------------------------*
*
* TrfSeek --
*
* ------------------------------------------------*
* This procedure is called by the generic IO level
* to move the access point in a channel.
* ------------------------------------------------*
*
* Sideeffects:
* Moves the location at which the channel
* will be accessed in future operations.
* Flushes all transformation buffers, then
* forwards it to the underlying channel.
*
* Result:
* -1 if failed, the new position if
* successful. An output argument contains
* the POSIX error code if an error
* occurred, or zero.
*
*------------------------------------------------------*
*/
static int
TrfSeek (instanceData, offset, mode, errorCodePtr)
ClientData instanceData; /* The channel to manipulate */
long offset; /* Size of movement. */
int mode; /* How to move */
int* errorCodePtr; /* Location of error flag. */
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
int result;
Tcl_Channel parent;
int newLoc;
START (TrfSeek);
PRINT ("(Mode = %d, Offset = %ld)\n", mode, offset); FL;
parent = DOWNC (trans);
/*
* Several things to look at before deciding what to do.
* Is it a tell request ?
* Is the channel unseekable ?
* If not, are we in pass-down mode ?
* If not, check buffer boundaries, etc. before discarding buffers, etc.
*/
if ((offset == 0) && (mode == SEEK_CUR)) {
/* Tell location.
*/
PRINT ("[Tell], Location = %d\n", trans->seekState.upLoc); FL;
DONE (TrfSeek);
return trans->seekState.upLoc;
}
if (!trans->seekState.allowed) {
*errorCodePtr = EINVAL;
PRINT ("[Unseekable]\n"); FL; DONE (TrfSeek);
return -1;
}
/* Assert: seekState.allowed, numBytesDown > 0, numBytesTransform > 0 */
if (trans->seekCfg.identity) {
/* Pass down mode. Pass request and record the change. This is used after
* restoration of constrained seek to force the usage of a new zero-point.
*/
PRINT ("[Passing down]\n"); FL;
SeekClearBuffer (trans, TCL_WRITABLE | TCL_READABLE);
trans->seekState.changed = 1;
result = SEEK (trans, offset, mode);
*errorCodePtr = (result == -1) ? Tcl_GetErrno () : 0;
SEEK_DUMP (TrfSeek; Pass<);
DONE (TrfSeek);
return result;
}
/* Constrained seeking, as specified by the transformation.
*/
if (mode == SEEK_SET) {
/* Convert and handle absolute from start as relative to current
* location.
*/
PRINT ("[Seek from start] => Seek relative\n"); FL;
result = TrfSeek (trans, offset - trans->seekState.upLoc, SEEK_CUR,
errorCodePtr);
DONE (TrfSeek);
return result;
}
if (mode == SEEK_END) {
/* Can't do that right now! TODO */
*errorCodePtr = EINVAL;
PRINT ("[Seek from end not available]"); FL; DONE (TrfSeek);
return -1;
}
/* Seeking relative to the current location.
*/
newLoc = trans->seekState.upLoc + offset;
if (newLoc % trans->seekState.used.numBytesTransform) {
/* Seek allowed only to locations which are multiples of the input.
*/
*errorCodePtr = EINVAL;
PRINT ("Seek constrained to multiples of input tuples\n"); FL;
DONE (TrfSeek);
return -1;
}
if (newLoc < 0) {
*errorCodePtr = EINVAL;
PRINT ("[Seek relative], cannot seek before start of stream\n"); FL;
DONE (TrfSeek);
return -1;
}
if ((newLoc < trans->seekState.upBufStartLoc) ||
(trans->seekState.upBufEndLoc <= newLoc)) {
/* We are seeking out of the read buffer.
* Discard it, adjust our position and seek the channel below to the
* equivalent position.
*/
int offsetDown, newDownLoc;
PRINT ("[Seek relative], beyond read buffer\n"); FL;
newDownLoc = trans->seekState.downZero + TRF_DOWN_CONVERT (trans, newLoc);
offsetDown = newDownLoc - trans->seekState.downLoc;
SeekClearBuffer (trans, TCL_WRITABLE | TCL_READABLE);
if (offsetDown != 0) {
result = SEEK (trans, offsetDown, SEEK_CUR);
*errorCodePtr = (result == -1) ? Tcl_GetErrno () : 0;
}
trans->seekState.downLoc += offsetDown;
trans->seekState.upLoc = newLoc;
trans->seekState.upBufStartLoc = newLoc;
trans->seekState.upBufEndLoc = newLoc;
SEEK_DUMP (TrfSeek; NoBuf);
DONE (TrfSeek);
return newLoc;
}
/* We are still inside the buffer, adjust the position
* and clear out incomplete data waiting in the write
* buffers, they are now invalid.
*/
SeekClearBuffer (trans, TCL_WRITABLE);
trans->seekState.upLoc = newLoc;
SEEK_DUMP (TrfSeek; Base_);
DONE (TrfSeek);
return newLoc;
}
/*
*------------------------------------------------------*
*
* TrfWatch --
*
* ------------------------------------------------*
* Initialize the notifier to watch Tcl_Files from
* this channel.
* ------------------------------------------------*
*
* Sideeffects:
* Sets up the notifier so that a future
* event on the channel will be seen by Tcl.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
/* ARGSUSED */
static void
TrfWatch (instanceData, mask)
ClientData instanceData; /* Channel to watch */
int mask; /* Events of interest */
{
/*
* 08/01/2000 - Completely rewritten to support as many versions of
* the core and their different implementation s of stacked channels.
*/
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
* Added the comments. */
/* The caller expressed interest in events occuring for this
* channel. Instead of forwarding the call to the underlying
* channel we now express our interest in events on that
* channel. This will ripple through all stacked channels to
* the bottom-most real one actually able to generate events
* (files, sockets, pipes, ...). The improvement beyond the
* simple forwarding is that the generated events will ripple
* back up to us, until they reach the channel the user
* expressed his interest in (via fileevent). This way the
* low-level events are propagated upward to the place where
* the real event script resides, something which does not
* happen in the simple forwarding model. It loses these events.
*/
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
START (TrfWatch);
#ifndef USE_TCL_STUBS
/* 8.0.x. Original patch. */
if (mask == trans->watchMask) {
/* No changes in the expressed interest, skip this call.
*/
DONE (TrfWatch);
return;
}
ChannelHandlerKS (trans, mask);
#else
/* 8.1. and up */
if ((trans->patchVariant == PATCH_ORIG) ||
(trans->patchVariant == PATCH_82)) {
if (mask == trans->watchMask) {
/* No changes in the expressed interest, skip this call.
*/
DONE (TrfWatch);
return;
}
ChannelHandlerKS (trans, mask);
} else if (trans->patchVariant == PATCH_832) {
/* 8.3.2 and up */
Tcl_DriverWatchProc* watchProc;
Tcl_Channel parent;
trans->watchMask = mask;
/* No channel handlers any more. We will be notified automatically
* about events on the channel below via a call to our
* 'TransformNotifyProc'. But we have to pass the interest down now.
* We are allowed to add additional 'interest' to the mask if we want
* to. But this transformation has no such interest. It just passes
* the request down, unchanged.
*/
parent = DOWNC (trans);
watchProc = Tcl_ChannelWatchProc (Tcl_GetChannelType (parent));
(*watchProc) (Tcl_GetChannelInstanceData(parent), mask);
} else {
Tcl_Panic ("Illegal value for 'patchVariant'");
}
#endif
/*
* Management of the internal timer.
*/
if (!(mask & TCL_READABLE) || (ResultLength(&trans->result) == 0)) {
/* A pending timer may exist, but either is there no (more)
* interest in the events it generates or nothing is available
* for reading. Remove it, if existing.
*/
TimerKill (trans);
} else {
/* There might be no pending timer, but there is interest in
* readable events and we actually have data waiting, so
* generate a timer to flush that if it does not exist.
*/
TimerSetup (trans);
}
DONE (TrfWatch);
}
/*
*------------------------------------------------------*
*
* TrfGetFile --
*
* ------------------------------------------------*
* Called from Tcl_GetChannelHandle to retrieve
* OS specific file handle from inside this channel.
* ------------------------------------------------*
*
* Sideeffects:
* None.
*
* Result:
* The appropriate Tcl_File or NULL if not
* present.
*
*------------------------------------------------------*
*/
static int
TrfGetFile (instanceData, direction, handlePtr)
ClientData instanceData; /* Channel to query */
int direction; /* Direction of interest */
ClientData* handlePtr; /* Place to store the handle into */
{
/*
* return handle belonging to parent channel
*/
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
Tcl_Channel parent;
START (TrfGetFile);
parent = DOWNC (trans);
DONE (TrfGetFile);
return Tcl_GetChannelHandle (parent, direction, handlePtr);
}
/*
*------------------------------------------------------*
*
* TrfSetOption --
*
* ------------------------------------------------*
* Called by the generic layer to handle the reconfi-
* guration of channel specific options. Unknown
* options are passed downstream.
* ------------------------------------------------*
*
* Sideeffects:
* As defined by the channel downstream.
*
* Result:
* A standard TCL error code.
*
*------------------------------------------------------*
*/
static int
TrfSetOption (instanceData, interp, optionName, value)
ClientData instanceData;
Tcl_Interp* interp;
CONST char* optionName;
CONST char* value;
{
/* Recognized options:
*
* -seekpolicy Accepted values: unseekable, identity, {}
*/
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
START (TrfSetOption);
if (0 == strcmp (optionName, "-seekpolicy")) {
/* The seekpolicy is about to be changed. Make sure that we got a valid
* value and that it really changes the used policy. Failing the first
* test causes an error, failing the second causes the system to silently
* ignore this request. Reconfiguration will fail for a non-overidable
* policy too.
*/
if (!trans->seekCfg.overideAllowed) {
Tcl_SetErrno (EINVAL);
Tcl_AppendResult (interp, "It is not allowed to overide ",
"the seek policy used by this channel.", NULL);
DONE (TrfSetOption);
return TCL_ERROR;
}
if (0 == strcmp (value, "unseekable")) {
if (!trans->seekState.allowed) {
/* Ignore the request if the channel already uses this policy.
*/
DONE (TrfSetOption);
return TCL_OK;
}
TRF_SET_UNSEEKABLE (trans->seekState.used);
trans->seekState.allowed = 0;
trans->seekCfg.identity = 0;
/* Changed is not touched! We might have been forced to identity
* before, and have to remember this for any restoration.
*/
} else if (0 == strcmp (value, "identity")) {
if (trans->seekState.allowed &&
(trans->seekState.used.numBytesTransform == 1) &&
(trans->seekState.used.numBytesDown == 1)) {
/* Ignore the request if the channel already uses this policy.
*/
DONE (TrfSetOption);
return TCL_OK;
}
trans->seekState.used.numBytesTransform = 1;
trans->seekState.used.numBytesDown = 1;
trans->seekState.allowed = 1;
trans->seekCfg.identity = 1;
trans->seekState.changed = 0;
} else if (0 == strcmp (value, "")) {
if ((trans->seekState.used.numBytesTransform ==
trans->seekCfg.chosen.numBytesTransform) &&
(trans->seekState.used.numBytesDown ==
trans->seekCfg.chosen.numBytesDown)) {
/* Ignore the request if the channel already uses hios chosen policy.
*/
DONE (TrfSetOption);
return TCL_OK;
}
trans->seekState.used.numBytesTransform =
trans->seekCfg.chosen.numBytesTransform;
trans->seekState.used.numBytesDown =
trans->seekCfg.chosen.numBytesDown;
trans->seekState.allowed = !TRF_IS_UNSEEKABLE (trans->seekState.used);
if (trans->seekState.changed) {
/* Define new base location. Resync up and down to get the
* proper location without read-ahead. Reinitialize the
* upper location.
*/
Tcl_Channel parent = DOWNC (trans);
SeekSynchronize (trans, parent);
trans->seekState.downLoc = TELL (trans);
#ifdef USE_TCL_STUBS
if (trans->patchVariant == PATCH_832) {
trans->seekState.downLoc -= Tcl_ChannelBuffered (parent);
}
#endif
trans->seekState.downZero = trans->seekState.downLoc;
trans->seekState.aheadOffset = 0;
trans->seekState.upLoc = 0;
trans->seekState.upBufStartLoc = 0;
trans->seekState.upBufEndLoc = ResultLength (&trans->result);
}
trans->seekCfg.identity = 0;
trans->seekState.changed = 0;
} else {
Tcl_SetErrno (EINVAL);
Tcl_AppendResult (interp, "Invalid value \"", value,
"\", must be one of 'unseekable', 'identity' or ''.",
NULL);
DONE (TrfSetOption);
return TCL_ERROR;
}
} else {
int res;
res = SETOPT (interp, trans, optionName, value);
DONE (TrfSetOption);
return res;
}
DONE (TrfSetOption);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* TrfGetOption --
*
* ------------------------------------------------*
* Called by generic layer to handle requests for
* the values of channel specific options. As this
* channel type does not have such, it simply passes
* all requests downstream.
* ------------------------------------------------*
*
* Sideeffects:
* Adds characters to the DString refered by
* 'dsPtr'.
*
* Result:
* A standard TCL error code.
*
*------------------------------------------------------*
*/
static int
TrfGetOption (instanceData, interp, optionName, dsPtr)
ClientData instanceData;
Tcl_Interp* interp;
CONST84 char* optionName;
Tcl_DString* dsPtr;
{
/* Recognized options:
*
* -seekcfg
* -seekstate
* -seekpolicy
*/
TrfTransformationInstance* trans = (TrfTransformationInstance*) instanceData;
if (optionName == (char*) NULL) {
/* A list of options and their values was requested,
*/
Tcl_Obj* tmp;
char policy [20];
SeekPolicyGet (trans, policy);
Tcl_DStringAppendElement (dsPtr, "-seekpolicy");
Tcl_DStringAppendElement (dsPtr, policy);
Tcl_DStringAppendElement (dsPtr, "-seekcfg");
tmp = SeekConfigGet (interp, &trans->seekCfg);
Tcl_DStringAppendElement (dsPtr, Tcl_GetStringFromObj (tmp, NULL));
Tcl_DecrRefCount (tmp);
Tcl_DStringAppendElement (dsPtr, "-seekstate");
tmp = SeekStateGet (interp, &trans->seekState);
Tcl_DStringAppendElement (dsPtr, Tcl_GetStringFromObj (tmp, NULL));
Tcl_DecrRefCount (tmp);
/* Pass the request down to all channels below so that we may a complete
* state.
*/
return GETOPT (interp, trans, optionName, dsPtr);
} else if (0 == strcmp (optionName, "-seekpolicy")) {
/* Deduce the policy in effect, use chosen/used
* policy and identity to do this. Use a helper
* procedure to allow easy reuse in the code above.
*/
char policy [20];
SeekPolicyGet (trans, policy);
Tcl_DStringAppend (dsPtr, policy, -1);
return TCL_OK;
} else if (0 == strcmp (optionName, "-seekcfg")) {
Tcl_Obj* tmp;
tmp = SeekConfigGet (interp, &trans->seekCfg);
Tcl_DStringAppend (dsPtr, Tcl_GetStringFromObj (tmp, NULL), -1);
Tcl_DecrRefCount (tmp);
return TCL_OK;
} else if (0 == strcmp (optionName, "-seekstate")) {
Tcl_Obj* tmp;
tmp = SeekStateGet (interp, &trans->seekState);
Tcl_DStringAppend (dsPtr, Tcl_GetStringFromObj (tmp, NULL), -1);
Tcl_DecrRefCount (tmp);
return TCL_OK;
} else {
/* Unknown option. Pass it down to the channels below, maybe one
* of them is able to handle this request.
*/
return GETOPT (interp, trans, optionName, dsPtr);
#if 0
Tcl_SetErrno (EINVAL);
return Tcl_BadChannelOption (interp, optionName, "seekcfg seekstate");
#endif
}
}
#ifdef USE_TCL_STUBS
/*
*------------------------------------------------------*
*
* TrfNotify --
*
* ------------------------------------------------*
* Called by the generic layer of 8.3.2 and higher
* to handle events coming from below. We simply pass
* them upward.
* ------------------------------------------------*
*
* Sideeffects:
* None.
*
* Result:
* The unchanged interest mask.
*
*------------------------------------------------------*
*/
static int
TrfNotify (instanceData, interestMask)
ClientData instanceData;
int interestMask;
{
/*
* An event occured in the underlying channel. This transformation
* doesn't process such events thus returns the incoming mask
* unchanged.
*
* We do delete an existing timer. It was not fired, yet we are
* here, so the channel below generated such an event and we don't
* have to. The renewal of the interest after the execution of
* channel handlers will eventually cause us to recreate the timer
* (in TrfWatch).
*/
TimerKill ((TrfTransformationInstance*) instanceData);
return interestMask;
}
#endif
/*
*------------------------------------------------------*
*
* TransformImmediate --
*
* ------------------------------------------------*
* Read from source, apply the specified transformation
* and write the result to destination.
* ------------------------------------------------*
*
* Sideeffects:
* The access points of source and destination
* change, data is added to destination too.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------* */
static int
TransformImmediate (interp, entry, source, destination, in, optInfo)
Tcl_Interp* interp;
Trf_RegistryEntry* entry;
Tcl_Channel source;
Tcl_Channel destination;
struct Tcl_Obj* CONST in;
Trf_Options optInfo;
{
Trf_Vectors* v;
Trf_ControlBlock control;
int res = TCL_OK;
ResultBuffer r;
START (TransformImmediate);
if (ENCODE_REQUEST (entry, optInfo)) {
v = &(entry->trfType->encoder);
} else {
v = &(entry->trfType->decoder);
}
/* Take care of output (channel vs. interpreter result area).
*/
if (destination == (Tcl_Channel) NULL) {
ResultInit (&r);
PRINT ("___.createproc\n"); FL;
control = v->createProc ((ClientData) &r, PutInterpResult,
optInfo, interp,
entry->trfType->clientData);
} else {
PRINT ("___.createproc\n"); FL;
control = v->createProc ((ClientData) destination, PutDestinationImm,
optInfo, interp,
entry->trfType->clientData);
}
if (control == (Trf_ControlBlock) NULL) {
DONE (TransformImmediate);
return TCL_ERROR;
}
/* Now differentiate between immediate value and channel as input.
*/
if (source == (Tcl_Channel) NULL) {
/* Immediate value.
* -- VERSION DEPENDENT CODE --
*/
int length;
unsigned char* buf;
buf = GET_DATA (in, &length);
if (v->convertBufProc) {
/* play it safe, use a copy, avoid clobbering the input. */
unsigned char* tmp;
tmp = (unsigned char*) ckalloc (length);
memcpy (tmp, buf, length);
PRINT ("___.convertbufproc\n"); FL;
res = v->convertBufProc (control, tmp, length, interp,
entry->trfType->clientData);
ckfree ((char*) tmp);
} else {
unsigned int i, c;
PRINT ("___.convertproc\n"); FL;
for (i=0; i < ((unsigned int) length); i++) {
c = buf [i];
res = v->convertProc (control, c, interp,
entry->trfType->clientData);
if (res != TCL_OK)
break;
}
}
if (res == TCL_OK) {
PRINT ("___.flushproc\n"); FL;
res = v->flushProc (control, interp, entry->trfType->clientData);
}
} else {
/* Read from channel.
*/
unsigned char* buf;
int actuallyRead;
buf = (unsigned char*) ckalloc (READ_CHUNK_SIZE);
while (1) {
if (Tcl_Eof (source))
break;
actuallyRead = Tcl_Read (source, (char*) buf, READ_CHUNK_SIZE);
if (actuallyRead <= 0)
break;
if (v->convertBufProc) {
PRINT ("___.convertbufproc\n"); FL;
res = v->convertBufProc (control, buf, actuallyRead, interp,
entry->trfType->clientData);
} else {
unsigned int i, c;
PRINT ("___.convertproc\n"); FL;
for (i=0; i < ((unsigned int) actuallyRead); i++) {
c = buf [i];
res = v->convertProc (control, c, interp,
entry->trfType->clientData);
if (res != TCL_OK)
break;
}
}
if (res != TCL_OK)
break;
}
ckfree ((char*) buf);
if (res == TCL_OK)
res = v->flushProc (control, interp, entry->trfType->clientData);
}
PRINT ("___.deleteproc\n"); FL;
v->deleteProc (control, entry->trfType->clientData);
if (destination == (Tcl_Channel) NULL) {
/* Now write into interpreter result area.
*/
if (res == TCL_OK) {
Tcl_ResetResult (interp);
if (r.buf != NULL) {
Tcl_Obj* o = NEW_DATA (r);
Tcl_IncrRefCount (o);
Tcl_SetObjResult (interp, o);
Tcl_DecrRefCount (o);
}
}
ResultClear (&r);
}
DONE (TransformImmediate);
return res;
}
/*
*------------------------------------------------------*
*
* AttachTransform --
*
* ------------------------------------------------*
* Create an instance of a transformation and
* associate as filter it with the specified channel.
* ------------------------------------------------*
*
* Sideeffects:
* Allocates memory, changes the internal
* state of the channel.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
AttachTransform (entry, baseOpt, optInfo, interp)
Trf_RegistryEntry* entry;
Trf_BaseOptions* baseOpt;
Trf_Options optInfo;
Tcl_Interp* interp;
{
TrfTransformationInstance* trans;
trans = NEW_TRANSFORM;
START (AttachTransform);
#ifdef TRF_STREAM_DEBUG
trans->inCounter = 0;
trans->outCounter = 0;
trans->name = (char*) entry->trfType->name;
#endif
#ifdef USE_TCL_STUBS
trans->patchVariant = entry->registry->patchVariant;
#endif
/* trans->standard.typePtr = entry->transType; */
trans->clientData = entry->trfType->clientData;
if (trans->patchVariant == PATCH_832) {
trans->parent = Tcl_GetTopChannel (baseOpt->attach);
} else {
trans->parent = baseOpt->attach;
}
trans->readIsFlushed = 0;
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
*/
trans->flags = 0;
trans->watchMask = 0;
/* 03/28/2000 Added by DNew@Invisible.Net because Purify says so. */
trans->lastStored = 0;
trans->mode = Tcl_GetChannelMode (baseOpt->attach);
trans->timer = (Tcl_TimerToken) NULL;
if (ENCODE_REQUEST (entry, optInfo)) {
/* ENCODE on write
* DECODE on read
*/
trans->out.vectors = ((trans->mode & TCL_WRITABLE) ?
&entry->trfType->encoder :
NULL);
trans->in.vectors = ((trans->mode & TCL_READABLE) ?
&entry->trfType->decoder :
NULL);
} else /* mode == DECODE */ {
/* DECODE on write
* ENCODE on read
*/
trans->out.vectors = ((trans->mode & TCL_WRITABLE) ?
&entry->trfType->decoder :
NULL);
trans->in.vectors = ((trans->mode & TCL_READABLE) ?
&entry->trfType->encoder :
NULL);
}
/* 'PutDestination' is ok for write, only read
* requires 'PutTrans' and its internal buffer.
*/
if (trans->mode & TCL_WRITABLE) {
PRINT ("out.createproc\n"); FL;
trans->out.control = trans->out.vectors->createProc ((ClientData) trans,
PutDestination,
optInfo, interp,
trans->clientData);
if (trans->out.control == (Trf_ControlBlock) NULL) {
ckfree ((char*) trans);
DONE (AttachTransform);
return TCL_ERROR;
}
}
if (trans->mode & TCL_READABLE) {
PRINT ("in_.createproc\n"); FL;
trans->in.control = trans->in.vectors->createProc ((ClientData) trans,
PutTrans,
optInfo, interp,
trans->clientData);
if (trans->in.control == (Trf_ControlBlock) NULL) {
ckfree ((char*) trans);
DONE (AttachTransform);
return TCL_ERROR;
}
}
ResultInit (&trans->result);
trans->result.seekState = &trans->seekState;
/*
* Build channel from converter definition and stack it upon the one we
* shall attach to.
*/
/* Discard information dangerous for the integrated patch.
* (This makes sure that we don't miss any place using this pointer
* without generating a crash (instead of some silent failure, like
* thrashing far away memory)).
*/
#ifndef USE_TCL_STUBS
trans->self = Tcl_StackChannel (interp, entry->transType,
(ClientData) trans, trans->mode,
trans->parent);
#else
if ((trans->patchVariant == PATCH_ORIG) ||
(trans->patchVariant == PATCH_832)) {
trans->self = Tcl_StackChannel (interp, entry->transType,
(ClientData) trans, trans->mode,
trans->parent);
} else if (trans->patchVariant == PATCH_82) {
trans->parent = NULL;
trans->self = baseOpt->attach;
Tcl_StackChannel (interp, entry->transType,
(ClientData) trans, trans->mode,
trans->self);
} else {
Tcl_Panic ("Illegal value for 'patchVariant'");
}
#endif
if (trans->self == (Tcl_Channel) NULL) {
ckfree ((char*) trans);
Tcl_AppendResult (interp, "internal error in Tcl_StackChannel",
(char*) NULL);
DONE (AttachTransform);
return TCL_ERROR;
}
/* Initialize the seek subsystem.
*/
PRINTLN ("Initialize Seeking");
PRINTLN ("Copy configuration");
trans->seekCfg.natural.numBytesTransform =
entry->trfType->naturalSeek.numBytesTransform;
trans->seekCfg.natural.numBytesDown =
entry->trfType->naturalSeek.numBytesDown;
if (optInfo && (*OPT->seekQueryProc != (Trf_SeekQueryOptions*) NULL)) {
PRINTLN ("Query seekQueryProc");
(*OPT->seekQueryProc) (interp, optInfo, &trans->seekCfg.natural, CLT);
}
PRINTLN ("Determine Policy");
SeekCalculatePolicies (trans);
PRINTLN (" Initialize");
SeekInitialize (trans);
/* Check for options overiding the policy. If they do despite being not
* allowed to do so we have to remove the transformation and break it down.
* We do this by calling 'Unstack', which does all the necessary things for
* us.
*/
PRINTLN (" Policy options ?");
if (baseOpt->policy != (Tcl_Obj*) NULL) {
if (TCL_OK != TrfSetOption ((ClientData) trans, interp, "-seekpolicy",
Tcl_GetStringFromObj (baseOpt->policy,
NULL))) {
/* An error prevented setting a policy. Save the resulting error
* message across the necessary unstacking of the now faulty
* transformation.
*/
#if GT81
Tcl_SavedResult ciSave;
Tcl_SaveResult (interp, &ciSave);
Tcl_UnstackChannel (interp, trans->self);
Tcl_RestoreResult (interp, &ciSave);
#else
Tcl_UnstackChannel (interp, trans->self);
#endif
DONE (AttachTransform);
return TCL_ERROR;
}
}
/* Tcl_RegisterChannel (interp, new); */
Tcl_AppendResult (interp, Tcl_GetChannelName (trans->self),
(char*) NULL);
DONE (AttachTransform);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* PutDestination --
*
* ------------------------------------------------*
* Handler used by a transformation to write its results.
* ------------------------------------------------*
*
* Sideeffects:
* Writes to the channel.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
PutDestination (clientData, outString, outLen, interp)
ClientData clientData;
unsigned char* outString;
int outLen;
Tcl_Interp* interp;
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) clientData;
int res;
Tcl_Channel parent;
START (PutDestination);
/*PRTSTR ("Data = {%d, \"%s\"}\n", outLen, outString);*/
PRINT ("Data = %d {\n", outLen);
DUMP (outLen, outString);
PRINT ("}\n");
parent = DOWNC (trans);
trans->lastWritten += outLen;
res = WRITE (trans, (char*) outString, outLen);
if (res < 0) {
if (interp) {
Tcl_AppendResult (interp, "error writing \"",
Tcl_GetChannelName (parent),
"\": ", Tcl_PosixError (interp),
(char*) NULL);
}
PRINT ("ERROR /written = %d, errno = %d, (%d) %s\n",
res, Tcl_GetErrno (), EACCES, strerror (Tcl_GetErrno ()));
DONE (PutDestination);
return TCL_ERROR;
}
DONE (PutDestination);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* PutDestinationImm --
*
* ------------------------------------------------*
* Handler used during an immediate transformation
* to write its results into the -out channel.
* ------------------------------------------------*
*
* Sideeffects:
* Writes to the channel.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
PutDestinationImm (clientData, outString, outLen, interp)
ClientData clientData;
unsigned char* outString;
int outLen;
Tcl_Interp* interp;
{
int res;
Tcl_Channel destination = (Tcl_Channel) clientData;
START (PutDestinationImm);
/*PRTSTR ("Data = {%d, \"%s\"}\n", outLen, outString);*/
PRINT ("Data = %d {\n", outLen);
DUMP (outLen, outString);
PRINT ("}\n");
res = Tcl_Write (destination, (char*) outString, outLen);
if (res < 0) {
if (interp) {
Tcl_AppendResult (interp, "error writing \"",
Tcl_GetChannelName (destination),
"\": ", Tcl_PosixError (interp),
(char*) NULL);
}
DONE (PutDestinationImm);
return TCL_ERROR;
}
DONE (PutDestinationImm);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* PutTrans --
*
* ------------------------------------------------*
* Handler used by a transformation to write its
* results (to be read later). Used by transformations
* acting as filter.
* ------------------------------------------------*
*
* Sideeffects:
* May allocate memory.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
PutTrans (clientData, outString, outLen, interp)
ClientData clientData;
unsigned char* outString;
int outLen;
Tcl_Interp* interp;
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) clientData;
START (PutTrans);
/*PRTSTR ("Data = {%d, \"%s\"}\n", outLen, outString);*/
PRINT ("Data = %d {\n", outLen);
DUMP (outLen, outString);
PRINT ("}\n");
STREAM_OUT (trans, outLen, outString);
trans->lastStored += outLen;
ResultAdd (&trans->result, outString, outLen);
DONE (PutTrans);
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* PutInterpResult --
*
* ------------------------------------------------*
* Handler used by a transformation to write its
* results into the interpreter result area.
* ------------------------------------------------*
*
* Sideeffects:
* changes the contents of the interpreter
* result area.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
PutInterpResult (clientData, outString, outLen, interp)
ClientData clientData;
unsigned char* outString;
int outLen;
Tcl_Interp* interp;
{
ResultBuffer* r = (ResultBuffer*) clientData;
START (PutInterpResult);
/*PRTSTR ("Data = {%d, \"%s\"}\n", outLen, outString);*/
PRINT ("Data = %d {\n", outLen);
DUMP (outLen, outString);
PRINT ("}\n");
ResultAdd (r, outString, outLen);
DONE (PutInterpResult);
return TCL_OK;
}
/* 04/13/1999 Fileevent patch from Matt Newman <matt@novadigm.com>
*/
/*
*------------------------------------------------------*
*
* ChannelHandler --
*
* ------------------------------------------------*
* Handler called by Tcl as a result of
* Tcl_CreateChannelHandler - to inform us of activity
* on the underlying channel.
* ------------------------------------------------*
*
* Sideeffects:
* May generate subsequent calls to
* Tcl_NotifyChannel.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ChannelHandler (clientData, mask)
ClientData clientData;
int mask;
{
/*
* An event occured in the underlying channel. Forward it to
* ourself. This will either execute an attached event script
* (fileevent) or an intermediate handler like this one propagating
* the event further upward.
*
* This procedure is called only for the original and the 8.2
* patch. The 8.2.3 patch uses a new vector in the driver to get and
* handle events coming from below.
*/
TrfTransformationInstance* trans = (TrfTransformationInstance*) clientData;
#ifndef USE_TCL_STUBS
/*
* Core 8.0.x. Forward the event to ourselves.
*/
Tcl_NotifyChannel (trans->self, mask);
#else
/*
* Check for the correct variants first. Forwarding the event is not
* required for the 8.2 patch. For that variant the core,
* i.e. Tcl_NotifyChannel loops over all channels in the stack by
* itself.
*/
if (trans->patchVariant == PATCH_832) {
Tcl_Panic ("Illegal value for 'patchVariant' in ChannelHandler");
}
if (trans->patchVariant == PATCH_ORIG) {
Tcl_NotifyChannel (trans->self, mask);
}
#endif
/*
* Check the I/O-Buffers of this channel for waiting information.
* Setup a timer generating an artificial event for us if we have
* such. We could call Tcl_NotifyChannel directly, but this would
* starve other event sources, so a timer is used to prevent that.
*/
TimerKill (trans);
/* Check for waiting data, flush it out with a timer.
*/
#ifndef USE_TCL_STUBS
if ((mask & TCL_READABLE) && ((ResultLength (&trans->result) > 0) ||
(Tcl_InputBuffered (trans->self) > 0))) {
TimerSetup (trans);
}
#else
if (trans->patchVariant != PATCH_ORIG) {
if ((mask & TCL_READABLE) && (ResultLength (&trans->result) > 0)) {
TimerSetup (trans);
}
} else {
if ((mask & TCL_READABLE) && ((ResultLength (&trans->result) > 0) ||
(Tcl_InputBuffered (trans->self) > 0))) {
TimerSetup (trans);
}
}
#endif
}
/*
*------------------------------------------------------*
*
* ChannelHandlerTimer --
*
* ------------------------------------------------*
* Called by the notifier (-> timer) to flush out
* information waiting in channel buffers.
* ------------------------------------------------*
*
* Sideeffects:
* As of 'ChannelHandler'.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ChannelHandlerTimer (clientData)
ClientData clientData; /* Transformation to query */
{
TrfTransformationInstance* trans = (TrfTransformationInstance*) clientData;
trans->timer = (Tcl_TimerToken) NULL;
#ifndef USE_TCL_STUBS
/* 8.0.x.
* Use the channel handler itself to do the necessary actions
*/
ChannelHandler (clientData, trans->watchMask);
#else
if ((trans->patchVariant == PATCH_82) ||
(trans->patchVariant == PATCH_832)) {
/*
* Use the standard notification mechanism to invoke all channel
* handlers.
*/
Tcl_NotifyChannel (trans->self, TCL_READABLE);
} else {
/* PATCH_ORIG, seee 8.0.x
*/
ChannelHandler (clientData, trans->watchMask);
}
#endif
}
#ifdef USE_TCL_STUBS
/*
*------------------------------------------------------*
*
* DownSOpt --
*
* Helper procedure. Writes an option to the downstream channel.
*
* Sideeffects:
* As of Tcl_SetChannelOption
*
* Result:
* A standard tcl error code.
*
*------------------------------------------------------*
*/
static int
DownSOpt (interp, ctrl, optionName, value)
Tcl_Interp* interp;
TrfTransformationInstance* ctrl;
CONST char* optionName;
CONST char* value;
{
Tcl_Channel parent = DOWNC (ctrl);
if (ctrl->patchVariant == PATCH_832) {
/*
* The newly written patch forces direct use of the driver.
*/
Tcl_DriverSetOptionProc *setOptionProc =
Tcl_ChannelSetOptionProc (Tcl_GetChannelType (parent));
if (setOptionProc != NULL) {
return (*setOptionProc) (Tcl_GetChannelInstanceData (parent),
interp, optionName, value);
} else {
return TCL_ERROR;
}
} else {
return Tcl_SetChannelOption (interp, parent, optionName, value);
}
}
/*
*------------------------------------------------------*
*
* DownGOpt --
*
* Helper procedure. Reads options from the downstream channel.
*
* Sideeffects:
* As of Tcl_GetChannelOption
*
* Result:
* A standard tcl error code.
*
*------------------------------------------------------*
*/
static int
DownGOpt (interp, ctrl, optionName, dsPtr)
Tcl_Interp* interp;
TrfTransformationInstance* ctrl;
CONST84 char* optionName;
Tcl_DString* dsPtr;
{
Tcl_Channel parent = DOWNC (ctrl);
if (ctrl->patchVariant == PATCH_832) {
/*
* The newly written patch forces direct use of the driver.
*/
Tcl_DriverGetOptionProc *getOptionProc =
Tcl_ChannelGetOptionProc (Tcl_GetChannelType (parent));
if (getOptionProc != NULL) {
return (*getOptionProc) (Tcl_GetChannelInstanceData (parent),
interp, optionName, dsPtr);
}
/*
* Downstream channel has no driver to get options. Fall back on
* some default behaviour. A query for all options is ok. A
* request for a specific unknown option OTOH has to fail.
*/
if (optionName == (char*) NULL) {
return TCL_OK;
} else {
return TCL_ERROR;
}
} else {
return Tcl_GetChannelOption (interp, parent, optionName, dsPtr);
}
}
/*
*------------------------------------------------------*
*
* DownWrite --
*
* Helper procedure. Writes to the downstream channel.
*
* Sideeffects:
* As of TclWrite / Tcl_WriteRaw
*
* Result:
* The number of bytes written.
*
*------------------------------------------------------*
*/
static int
DownWrite (ctrl, buf, toWrite)
TrfTransformationInstance* ctrl;
char* buf;
int toWrite;
{
Tcl_Channel parent = DOWNC (ctrl);
if (ctrl->patchVariant == PATCH_832) {
/*
* The newly written patch forces use of the new raw-API.
*/
PRINT ("WriteRaw %p %s\n", parent, Tcl_GetChannelType (parent)->typeName);
return Tcl_WriteRaw (parent, buf, toWrite);
} else {
return Tcl_Write (parent, buf, toWrite);
}
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* DownRead --
*
* Helper procedure. Reads from the downstream channel.
*
* Sideeffects:
* As of TclRead / Tcl_ReadRaw
*
* Result:
* The number of bytes read.
*
*------------------------------------------------------*
*/
static int
DownRead (ctrl, buf, toRead)
TrfTransformationInstance* ctrl;
char* buf;
int toRead;
{
Tcl_Channel parent = DOWNC (ctrl);
if (ctrl->patchVariant == PATCH_832) {
/*
* The newly written patch forces use of the new raw-API.
*/
return Tcl_ReadRaw (parent, buf, toRead);
} else {
return Tcl_Read (parent, buf, toRead);
}
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* DownSeek --
*
* Helper procedure. Asks the downstream channel
* to seek, or for its current location.
*
* Sideeffects:
* None.
*
* Result:
* The location in the downstream channel
*
*------------------------------------------------------*
*/
static int
DownSeek (ctrl, offset, mode)
TrfTransformationInstance* ctrl;
int offset;
int mode;
{
Tcl_Channel parent = DOWNC (ctrl);
if (ctrl->patchVariant == PATCH_832) {
/*
* The newly rewritten patch forces the transformation into
* directly using the seek-proc of the downstream driver. Tcl_Seek
* would compensate for the stack and cause and infinite recursion
* blowing the stack.
*/
Tcl_ChannelType* parentType = Tcl_GetChannelType (parent);
Tcl_DriverSeekProc* parentSeekProc = Tcl_ChannelSeekProc (parentType);
int errorCode;
if (parentSeekProc == (Tcl_DriverSeekProc*) NULL) {
return -1;
}
return (*parentSeekProc) (Tcl_GetChannelInstanceData (parent),
offset, mode, &errorCode);
}
/*
* (ctrl->patchVariant == PATCH_ORIG)
* (ctrl->patchVariant == PATCH_82)
*
* Both the original patch for stacked channels and rewritten
* implementation for 8.2. have the same simple semantics for
* getting at the location of the downstream channel.
*
* Just use the standard 'Tcl_Seek'.
*/
return (int) Tcl_Seek (parent, offset, mode);
}
/*
*------------------------------------------------------*
*
* DownChannel --
*
* Helper procedure. Finds the downstream channel.
*
* Sideeffects:
* May modify 'self'.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static Tcl_Channel
DownChannel (ctrl)
TrfTransformationInstance* ctrl;
{
Tcl_Channel self;
Tcl_Channel next;
if ((ctrl->patchVariant == PATCH_ORIG) ||
(ctrl->patchVariant == PATCH_832)) {
/*
* Both the original patch for stacked channels and rewritten
* implementation for 8.3.2. have simple semantics for getting at
* the parent of a channel.
*/
return ctrl->parent;
}
/*
* The first rewrite of the stacked channel patch initially included
* in 8.2. requires that a transformation searches it's channel in
* the whole stack. Only for the versions of the core using this
* implementation, 8.2 till 8.3.1, the comments below apply.
*/
/* The reason for the existence of this procedure is
* the fact that stacking a transform over another
* transform will leave our internal pointer unchanged,
* and thus pointing to the new transform, and not the
* Channel structure containing the saved state of this
* transform. This is the price to pay for leaving
* Tcl_Channel references intact. The only other solution
* is an extension of Tcl_ChannelType with another driver
* procedure to notify a Channel about the (un)stacking.
*
* It walks the chain of Channel structures until it
* finds the one pointing having 'ctrl' as instanceData
* and then returns the superceding channel to that.
*/
self = ctrl->self;
while ((ClientData) ctrl != Tcl_GetChannelInstanceData (self)) {
next = Tcl_GetStackedChannel (self);
if (next == (Tcl_Channel) NULL) {
/* 09/24/1999 Unstacking bug, found by Matt Newman <matt@sensus.org>.
*
* We were unable to find the channel structure for this
* transformation in the chain of stacked channel. This
* means that we are currently in the process of unstacking
* it *and* there were some bytes waiting which are now
* flushed. In this situation the pointer to the channel
* itself already refers to the parent channel we have to
* write the bytes into, so we return that.
*/
return ctrl->self;
}
self = next;
}
return Tcl_GetStackedChannel (self);
}
#endif
/*
*------------------------------------------------------*
*
* ResultClear --
*
* Deallocates any memory allocated by 'ResultAdd'.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ResultClear (r)
ResultBuffer* r; /* Reference to the buffer to clear out */
{
r->used = 0;
if (r->allocated) {
ckfree ((char*) r->buf);
r->buf = (unsigned char*) NULL;
r->allocated = 0;
}
if (r->seekState != (SeekState*) NULL) {
r->seekState->upBufStartLoc = r->seekState->upLoc;
r->seekState->upBufEndLoc = r->seekState->upLoc;
}
}
/*
*------------------------------------------------------*
*
* ResultInit --
*
* Initializes the specified buffer structure. The
* structure will contain valid information for an
* emtpy buffer.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ResultInit (r)
ResultBuffer* r; /* Reference to the structure to initialize */
{
r->used = 0;
r->allocated = 0;
r->buf = (unsigned char*) NULL;
r->seekState = (SeekState*) NULL;
}
/*
*------------------------------------------------------*
*
* ResultLength --
*
* Returns the number of bytes stored in the buffer.
*
* Sideeffects:
* None.
*
* Result:
* An integer, see above too.
*
*------------------------------------------------------*
*/
static int
ResultLength (r)
ResultBuffer* r; /* The structure to query */
{
return r->used;
}
/*
*------------------------------------------------------*
*
* ResultCopy --
*
* Copies the requested number of bytes from the
* buffer into the specified array and removes them
* from the buffer afterward. Copies less if there
* is not enough data in the buffer.
*
* Sideeffects:
* See above.
*
* Result:
* The number of actually copied bytes,
* possibly less than 'toRead'.
*
*------------------------------------------------------*
*/
static int
ResultCopy (r, buf, toRead)
ResultBuffer* r; /* The buffer to read from */
unsigned char* buf; /* The buffer to copy into */
int toRead; /* Number of requested bytes */
{
int copied;
START (ResultCopy);
PRINT ("request = %d, have = %d\n", toRead, r->used); FL;
if (r->used == 0) {
/* Nothing to copy in the case of an empty buffer.
*/
copied = 0;
goto done;
}
if (r->used == toRead) {
/* We have just enough. Copy everything to the caller.
*/
memcpy ((VOID*) buf, (VOID*) r->buf, toRead);
r->used = 0;
copied = toRead;
goto done;
}
if (r->used > toRead) {
/* The internal buffer contains more than requested.
* Copy the requested subset to the caller, and shift
* the remaining bytes down.
*/
memcpy ((VOID*) buf, (VOID*) r->buf, toRead);
memmove ((VOID*) r->buf, (VOID*) (r->buf + toRead), r->used - toRead);
r->used -= toRead;
copied = toRead;
goto done;
}
/* There is not enough in the buffer to satisfy the caller, so
* take everything.
*/
memcpy ((VOID*) buf, (VOID*) r->buf, r->used);
toRead = r->used;
r->used = 0;
copied = toRead;
/* -- common postwork code ------- */
done:
if ((copied > 0) &&
(r->seekState != (SeekState*) NULL)) {
r->seekState->upBufStartLoc += copied;
}
DONE (ResultCopy);
return copied;
}
/*
*------------------------------------------------------*
*
* ResultDiscardAtStart --
*
* Removes the n bytes at the beginning of the buffer
* from it. Clears the buffer if n is greater than
* its length.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ResultDiscardAtStart (r, n)
ResultBuffer* r; /* The buffer to manipulate */
int n; /* Number of bytes to remove */
{
START (ResultDiscardAtStart);
PRINT ("n = %d, have = %d\n", n, r->used); FL;
if (r->used == 0) {
/* Nothing to remove in the case of an empty buffer.
*/
DONE (ResultDiscardAtStart);
return;
}
if (n > r->used) {
ResultClear (r);
DONE (ResultDiscardAtStart);
return;
}
/* Shift remaining information down */
memmove ((VOID*) r->buf, (VOID*) (r->buf + n), r->used - n);
r->used -= n;
if (r->seekState != (SeekState*) NULL) {
r->seekState->upBufStartLoc += n;
}
DONE (ResultCopy);
}
/*
*------------------------------------------------------*
*
* ResultAdd --
*
* Adds the bytes in the specified array to the
* buffer, by appending it.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ResultAdd (r, buf, toWrite)
ResultBuffer* r; /* The buffer to extend */
unsigned char* buf; /* The buffer to read from */
int toWrite; /* The number of bytes in 'buf' */
{
START (ResultAdd);
PRINT ("have %d, adding %d\n", r->used, toWrite); FL;
if ((r->used + toWrite + 1) > r->allocated) {
/* Extension of the internal buffer is required.
*/
if (r->allocated == 0) {
r->allocated = toWrite + INCREMENT;
r->buf = (unsigned char*) ckalloc (r->allocated);
} else {
r->allocated += toWrite + INCREMENT;
r->buf = (unsigned char*) ckrealloc((char*) r->buf,
r->allocated);
}
}
/* now copy data */
memcpy (r->buf + r->used, buf, toWrite);
r->used += toWrite;
if (r->seekState != (SeekState*) NULL) {
r->seekState->upBufEndLoc += toWrite;
}
DONE (ResultAdd);
}
/*
*------------------------------------------------------*
*
* SeekCalculatePolicies --
*
* Computes standard and used policy from the natural
* policy of the transformation, all transformations
* below and its base channel.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
SeekCalculatePolicies (trans)
TrfTransformationInstance* trans;
{
/* Define seek related runtime configuration.
* seekCfg.overideAllowed, seekCfg.chosen, seekState.used
*
* i. some transformation below unseekable ? not-overidable unseekable
* ii. base channel unseekable ? see above
* iii. naturally unseekable ? overidable unseekable.
*
* WARNING: For 8.0 and 8.1 we will always return 'unseekable'. Due to a
* missing 'Tcl_GetStackedChannel' we are unable to go down through the
* stack of transformations.
*/
#ifndef USE_TCL_STUBS
START (SeekCalculatePolicies);
PRINTLN ("8.0., no Tcl_GetStackedChannel, unseekable, no overide");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 0;
#else
Tcl_Channel self = trans->self;
Tcl_Channel next;
int stopped = 0;
START (SeekCalculatePolicies);
if (trans->patchVariant == PATCH_ORIG) {
PRINTLN ("8.1., no Tcl_GetStackedChannel, unseekable, no overide");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 0;
goto done;
}
/* 8.2 or higher */
while (self != (Tcl_Channel) NULL) {
PRINT ("Check %p\n", self); FL;
#if GT81
next = Tcl_GetStackedChannel (self);
#else
/* In case of 8.1 and higher we can use the (integrated or patched)
* 'Tcl_GetStackedChannel' to find the next transform in a general
* way. Else we have to check the type of 'next' itself before trying
* to peek into its structure. If it is no Trf transform we cannot go
* deeper into the stack. But that is not necessary, as the result of
* 'unseekable' will not change anymore.
*/
if (Tcl_GetChannelType (self)->seekProc != TrfSeek) {
PRINTLN ("Can't go further down, unseekable, disallow overide");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 0;
stopped = 1;
break;
}
next = ((TrfTransformationInstance*)
Tcl_GetChannelInstanceData (self))->parent;
#endif
if (next == (Tcl_Channel) NULL) {
/* self points to base channel (ii).
*/
if (Tcl_GetChannelType (self)->seekProc == (Tcl_DriverSeekProc*) NULL) {
/* Base is unseekable.
*/
PRINTLN ("Base is unseekable");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 0;
stopped = 1;
break;
}
} else {
/* 'next' points to a transformation.
*/
Tcl_Channel nextAfter;
#if GT81
nextAfter = Tcl_GetStackedChannel (next);
#else
nextAfter = ((TrfTransformationInstance*)
Tcl_GetChannelInstanceData (next))->parent;
#endif
if (nextAfter != (Tcl_Channel) NULL) {
/* next points to a transformation below the top (i).
* Assume unseekable for a non-trf transformation, else peek directly
* into the relevant structure
*/
if (Tcl_GetChannelType (next)->seekProc != TrfSeek) {
PRINTLN ("Unknown type of transform, unseekable, no overide");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 0;
stopped = 1;
} else {
TrfTransformationInstance* down =
(TrfTransformationInstance*) Tcl_GetChannelInstanceData (next);
if (!down->seekState.allowed) {
PRINTLN ("Trf transform, unseekable");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 0;
stopped = 1;
}
}
} else {
/* Next points to the base channel */
/* assert (0); */
}
}
self = next;
}
PRINTLN ("Looping done");
if (!stopped) {
PRINTLN ("Search went through, check natural policy");
if (TRF_IS_UNSEEKABLE (trans->seekCfg.natural)) {
/* Naturally unseekable (iii)
*/
PRINTLN ("Naturally unseekable");
TRF_SET_UNSEEKABLE (trans->seekCfg.chosen);
trans->seekCfg.overideAllowed = 1;
} else {
/* Take the natural ratio.
*/
PRINTLN ("naturally seekable");
trans->seekCfg.chosen.numBytesTransform =
trans->seekCfg.natural.numBytesTransform;
trans->seekCfg.chosen.numBytesDown =
trans->seekCfg.natural.numBytesDown;
trans->seekCfg.overideAllowed = 1;
}
}
#endif
PRINTLN ("Copy ratio chosen :- used");
#ifdef USE_TCL_STUBS
done:
#endif
trans->seekState.used.numBytesTransform =
trans->seekCfg.chosen.numBytesTransform;
trans->seekState.used.numBytesDown =
trans->seekCfg.chosen.numBytesDown;
trans->seekState.allowed =
!TRF_IS_UNSEEKABLE (trans->seekState.used);
DONE (SeekCalculatePolicies);
}
/*
*------------------------------------------------------*
*
* SeekInitialize --
*
* Initialize the runtime state of the seek mechanisms
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
SeekInitialize (trans)
TrfTransformationInstance* trans;
{
trans->seekState.upLoc = 0;
trans->seekState.upBufStartLoc = 0;
trans->seekState.upBufEndLoc = 0;
if (trans->seekState.allowed) {
trans->seekState.downLoc = TELL (trans);
#ifdef USE_TCL_STUBS
if (trans->patchVariant == PATCH_832) {
trans->seekState.downLoc -= Tcl_ChannelBuffered (DOWNC (trans));
}
#endif
trans->seekState.downZero = trans->seekState.downLoc;
trans->seekState.aheadOffset = 0;
} else {
trans->seekState.downLoc = 0;
trans->seekState.downZero = 0;
trans->seekState.aheadOffset = 0;
}
trans->seekCfg.identity = 0;
trans->seekState.changed = 0;
SEEK_DUMP (Seek Initialized);
}
/*
*------------------------------------------------------*
*
* SeekClearBuffer --
*
* Clear read / write buffers of the transformation,
* as specified by the second argument.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
SeekClearBuffer (trans, which)
TrfTransformationInstance* trans;
int which;
{
/*
* Discard everything in the input and output buffers, both
* in the transformation and in the generic layer of Trf.
*/
if (trans->mode & which & TCL_WRITABLE) {
PRINT ("out.clearproc\n"); FL;
trans->out.vectors->clearProc (trans->out.control, trans->clientData);
}
if (trans->mode & which & TCL_READABLE) {
PRINT ("in.clearproc\n"); FL;
trans->in.vectors->clearProc (trans->in.control, trans->clientData);
trans->readIsFlushed = 0;
ResultClear (&trans->result);
}
}
/*
*------------------------------------------------------*
*
* SeekSynchronize --
*
* Discard an existing read buffer and annulate the
* read ahead in the downstream channel.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
SeekSynchronize (trans, parent)
TrfTransformationInstance* trans;
Tcl_Channel parent;
{
int offsetDown;
if (!trans->seekState.allowed) {
/* No synchronisation required for an unseekable transform */
return;
}
if ((trans->seekState.upLoc == trans->seekState.upBufEndLoc) &&
(trans->seekState.aheadOffset == 0)) {
/* Up and down locations are in sync, nothing to do. */
return;
}
PRINT ("in.clearproc\n"); FL;
trans->in.vectors->clearProc (trans->in.control, trans->clientData);
trans->readIsFlushed = 0;
offsetDown = TRF_DOWN_CONVERT (trans,
trans->seekState.upLoc - trans->seekState.upBufEndLoc);
offsetDown -= trans->seekState.aheadOffset; /* !! */
ResultClear (&trans->result);
if (offsetDown != 0) {
SEEK (trans, offsetDown, SEEK_CUR);
}
trans->seekState.downLoc += offsetDown;
}
/*
*------------------------------------------------------*
*
* SeekPolicyGet --
*
* Compute the currently used policy and store its
* name into the character buffer.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
SeekPolicyGet (trans, policy)
TrfTransformationInstance* trans;
char* policy;
{
if (trans->seekCfg.identity) {
/* identity forced */
strcpy (policy, "identity");
return;
}
if (!trans->seekState.allowed &&
((trans->seekState.used.numBytesTransform !=
trans->seekCfg.chosen.numBytesTransform) ||
(trans->seekState.used.numBytesDown !=
trans->seekCfg.chosen.numBytesDown))) {
/* unseekable forced */
strcpy (policy, "unseekable");
return;
}
/* chosen policy in effect */
strcpy (policy, "");
return;
}
/*
*------------------------------------------------------*
*
* SeekConfigGet --
*
* Generates a list containing the current configuration
* of the seek system in a readable manner.
*
* Sideeffects:
* See above.
*
* Result:
* An Tcl_Obj, or NULL.
*
*------------------------------------------------------*
*/
static Tcl_Obj*
SeekConfigGet (interp, cfg)
Tcl_Interp* interp;
SeekConfig* cfg;
{
int res;
Tcl_Obj* list = (Tcl_Obj*) NULL;
Tcl_Obj* sub1 = (Tcl_Obj*) NULL;
Tcl_Obj* sub2 = (Tcl_Obj*) NULL;
list = Tcl_NewListObj (0, NULL);
if (list == (Tcl_Obj*) NULL) {
goto error;
}
LIST_ADDSTR (error, list, "ratioNatural");
sub1 = Tcl_NewListObj (0, NULL);
if (sub1 == (Tcl_Obj*) NULL) {
goto error;
}
LIST_ADDINT (error, sub1, cfg->natural.numBytesTransform);
LIST_ADDINT (error, sub1, cfg->natural.numBytesDown);
LIST_ADDOBJ (error, list, sub1);
LIST_ADDSTR (error, list, "ratioChosen");
sub2 = Tcl_NewListObj (0, NULL);
if (sub2 == (Tcl_Obj*) NULL) {
goto error;
}
LIST_ADDINT (error, sub2, cfg->chosen.numBytesTransform);
LIST_ADDINT (error, sub2, cfg->chosen.numBytesDown);
LIST_ADDOBJ (error, list, sub2);
LIST_ADDSTR (error, list, "overideAllowed");
LIST_ADDINT (error, list, cfg->overideAllowed);
LIST_ADDSTR (error, list, "identityForced");
LIST_ADDINT (error, list, cfg->identity);
return list;
error:
/* Cleanup any remnants of errors above */
if (list != (Tcl_Obj*) NULL) {
Tcl_DecrRefCount (list);
}
if (sub1 != (Tcl_Obj*) NULL) {
Tcl_DecrRefCount (sub1);
}
if (sub2 != (Tcl_Obj*) NULL) {
Tcl_DecrRefCount (sub2);
}
return NULL;
}
/*
*------------------------------------------------------*
*
* SeekStateGet --
*
* Generates a list containing the current state of
* the seek system in a readable manner.
*
* Sideeffects:
* See above.
*
* Result:
* An Tcl_Obj, or NULL.
*
*------------------------------------------------------*
*/
static Tcl_Obj*
SeekStateGet (interp, state)
Tcl_Interp* interp;
SeekState* state;
{
int res;
Tcl_Obj* list = (Tcl_Obj*) NULL;
Tcl_Obj* sub = (Tcl_Obj*) NULL;
list = Tcl_NewListObj (0, NULL);
if (list == (Tcl_Obj*) NULL) {
goto error;
}
LIST_ADDSTR (error, list, "seekable");
LIST_ADDINT (error, list, state->allowed);
LIST_ADDSTR (error, list, "ratio");
sub = Tcl_NewListObj (0, NULL);
if (sub == (Tcl_Obj*) NULL) {
goto error;
}
LIST_ADDINT (error, sub, state->used.numBytesTransform);
LIST_ADDINT (error, sub, state->used.numBytesDown);
LIST_ADDOBJ (error, list, sub);
LIST_ADDSTR (error, list, "up");
LIST_ADDINT (error, list, state->upLoc);
LIST_ADDSTR (error, list, "upBufStart");
LIST_ADDINT (error, list, state->upBufStartLoc);
LIST_ADDSTR (error, list, "upBufEnd");
LIST_ADDINT (error, list, state->upBufEndLoc);
LIST_ADDSTR (error, list, "down");
LIST_ADDINT (error, list, state->downLoc);
LIST_ADDSTR (error, list, "downBase");
LIST_ADDINT (error, list, state->downZero);
LIST_ADDSTR (error, list, "downAhead");
LIST_ADDINT (error, list, state->aheadOffset);
LIST_ADDSTR (error, list, "changed");
LIST_ADDINT (error, list, state->changed);
return list;
error:
/* Cleanup any remnants of errors above */
if (list != (Tcl_Obj*) NULL) {
Tcl_DecrRefCount (list);
}
if (sub != (Tcl_Obj*) NULL) {
Tcl_DecrRefCount (sub);
}
return NULL;
}
#ifdef TRF_DEBUG
/*
*------------------------------------------------------*
*
* PrintString --
*
* Defined only in debug mode, enforces correct
* printing of strings by adding a \0 after its value.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
void
PrintString (fmt,len,bytes)
char* fmt;
int len;
char* bytes;
{
char* tmp = (char*) ckalloc (len+1);
memcpy (tmp, bytes, len);
tmp [len] = '\0';
PRINT (fmt, len, tmp);
ckfree (tmp);
}
/*
*------------------------------------------------------*
*
* DumpString --
*
* Defined only in debug mode, dumps information
* in hex blocks
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
void
DumpString (n,len,bytes)
int n;
int len;
char* bytes;
{
int i, c;
for (i=0, c=0; i < len; i++, c++) {
if (c == 0) {
BLNKS;
}
printf (" %02x", (0xff & bytes [i]));
if (c == 16) {
c = -1;
printf ("\n");
}
}
if (c != 0) {
printf ("\n");
}
}
/*
*------------------------------------------------------*
*
* SeekDump --
*
* Defined only in debug mode, dumps the complete
* state of all seek variables.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
SeekDump (trans, place)
TrfTransformationInstance* trans;
CONST char* place;
{
int loc;
Tcl_Channel parent = DOWNC (trans);
loc = TELL (trans);
#if 0
PRINT ("SeekDump (%s) {\n", place); FL; IN;
PRINT ("ratio up:down %d : %d\n",
trans->seekState.used.numBytesTransform,
trans->seekState.used.numBytesDown); FL;
PRINT ("seekable %d\n",
trans->seekState.allowed); FL;
PRINT ("up %d [%d .. %d]\n",
trans->seekState.upLoc,
trans->seekState.upBufStartLoc,
trans->seekState.upBufEndLoc); FL;
PRINT ("down %d [%d] | %d\n",
trans->seekState.downLoc,
trans->seekState.aheadOffset,
loc); FL;
PRINT ("base %d\n",
trans->seekState.downZero); FL;
PRINT ("identity force %d\n",
trans->seekCfg.identity); FL;
PRINT ("seek while ident %d\n",
trans->seekState.changed); FL;
PRINT ("read buffer %d\n",
ResultLength (&trans->result)); FL;
OT ; PRINT ("}\n"); FL;
#else
PRINT ("SkDmp (%s) ", place); FL;
#if 0
NPRINT ("(%2d : %2d) | ",
trans->seekCfg.natural.numBytesTransform,
trans->seekCfg.natural.numBytesDown); FL;
NPRINT ("(%2d : %2d) | ",
trans->seekCfg.chosen.numBytesTransform,
trans->seekCfg.chosen.numBytesDown); FL;
#endif
NPRINT ("%2d:%2d /%1d |r %5d |u %5d [%5d..%5d] |d %5d [%2d] %5d | %5d | %1d %1d",
trans->seekState.used.numBytesTransform,
trans->seekState.used.numBytesDown,
trans->seekState.allowed,
ResultLength (&trans->result),
trans->seekState.upLoc,
trans->seekState.upBufStartLoc,
trans->seekState.upBufEndLoc,
trans->seekState.downLoc,
trans->seekState.aheadOffset,
loc,
trans->seekState.downZero,
trans->seekCfg.identity,
trans->seekState.changed
); FL;
NPRINT ("\n"); FL;
#endif
}
#endif
/*
*------------------------------------------------------*
*
* AllocChannelType --
*
* Allocates a new ChannelType structure.
*
*
* Sideeffects:
* See above.
*
* Result:
* A reference to the new structure.
*
*------------------------------------------------------*
*/
static Tcl_ChannelType*
AllocChannelType (sizePtr)
int* sizePtr;
{
/*
* Allocation of a new channeltype structure is not easy, because of
* the various version of the core and subsequent changes to the
* structure. The main challenge is to allocate enough memory for
* modern versions even if this extension is compiled against one
* of the older variants!
*
* (1) Versions before stubs (8.0.x) are simple, because they are
* supported only if the extension is compiled against exactly
* that version of the core.
*
* (2) With stubs we just determine the difference between the older
* and modern variant and overallocate accordingly if compiled
* against an older variant.
*/
int size = sizeof(Tcl_ChannelType); /* Base size */
#ifdef USE_TCL_STUBS
/*
* Size of a procedure pointer. We assume that all procedure
* pointers are of the same size, regardless of exact type
* (arguments and return values).
*
* 8.1. First version containing close2proc. Baseline.
* 8.3.2 Three additional vectors. Moved blockMode, new flush- and
* handlerProc's.
* 8.4+ wide seek, and thread action.
*
* => Compilation against earlier version has to overallocate five
* procedure pointers.
*/
#if !(GT832)
size += 5 * procPtrSize;
#endif
#endif
if (sizePtr != (int*) NULL) {
*sizePtr = size;
}
return (Tcl_ChannelType*) ckalloc (size);
}
/*
*------------------------------------------------------*
*
* InitializeChannelType --
*
* Initializes a new ChannelType structure.
*
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static Tcl_ChannelType*
InitializeChannelType (name, patchVariant)
CONST char* name;
int patchVariant;
{
Tcl_ChannelType* tct;
int size;
/*
* Initialization of a new channeltype structure is not easy,
* because of the various version of the core and subsequent changes
* to the structure. The main problem is if compiled against an
* older version how to access the elements of the structure not
* known in that version. It is made a bit easier because the
* allocation routine returns the allocated size. This allows us to
* clear out the entire structure. So we just have to deal with the
* elements to set and not the ones left alone.
*/
tct = AllocChannelType (&size);
tct->typeName = (char*) name;
memset ((VOID*) tct, '\0', size);
/*
* Common elements of the structure (no changes in location or name)
*/
tct->closeProc = TrfClose;
tct->inputProc = TrfInput;
tct->outputProc = TrfOutput;
tct->seekProc = TrfSeek;
tct->setOptionProc = TrfSetOption;
tct->getOptionProc = TrfGetOption;
tct->watchProc = TrfWatch;
tct->getHandleProc = TrfGetFile;
/*
* No need to handle close2Proc. Already cleared with the 'memset'
* above.
*/
/*
* blockModeProc is a twister. For 8.0.x we can access it
* immediately. For the higher versions we have to make some
* runtime-choices, and their implementation depends on the version
* we compile against.
*/
#ifndef USE_TCL_STUBS
/* 8.0.x */
tct->blockModeProc = TrfBlock;
#else
#if GT832
/* 8.3.2. and higher. Direct access to all elements possible. Use
*'patchVariant' information to select the values to use.
*/
if ((patchVariant == PATCH_ORIG) ||
(patchVariant == PATCH_82)) {
/* The 'version' element of 8.3.2 is in the the place of the
* blockModeProc. For the original patch in 8.1.x and the firstly
* included (8.2) we have to set our blockModeProc into this
* place.
*/
tct->version = (Tcl_ChannelTypeVersion) TrfBlock;
} else /* patchVariant == PATCH_832 */ {
/* For the 8.3.2 core we present ourselves as a version 2
* driver. This means a speciial value in version (ex
* blockModeProc), blockModeProc in a different place and of
* course usage of the handlerProc.
*/
tct->version = TCL_CHANNEL_VERSION_2;
tct->blockModeProc = TrfBlock;
tct->handlerProc = TrfNotify;
}
#else
/* Same as above, but as we are compiling against an older core we
* have to create some definitions for the new elements as the compiler
* does not know them by name.
*/
if ((patchVariant == PATCH_ORIG) ||
(patchVariant == PATCH_82)) {
/* The 'version' element of 8.3.2 is in the the place of the
* blockModeProc. For the original patch in 8.1.x and the firstly
* included (8.2) we have to set our blockModeProc into this
* place.
*/
tct->blockModeProc = TrfBlock;
} else /* patchVariant == PATCH_832 */ {
/* For the 8.3.2 core we present ourselves as a version 2
* driver. This means a special value in version (ex
* blockModeProc), blockModeProc in a different place and of
* course usage of the handlerProc.
*/
#define TRF_CHANNEL_VERSION_2 ((TrfChannelTypeVersion) 0x2)
#define BMP (*((Tcl_DriverBlockModeProc**) (&(tct->close2Proc) + 1)))
#define HP (*((TrfDriverHandlerProc**) (&(tct->close2Proc) + 3)))
typedef struct TrfChannelTypeVersion_* TrfChannelTypeVersion;
typedef int (TrfDriverHandlerProc) _ANSI_ARGS_((ClientData instanceData,
int interestMask));
tct->blockModeProc = (Tcl_DriverBlockModeProc*) TRF_CHANNEL_VERSION_2;
BMP = TrfBlock;
HP = TrfNotify;
#undef BMP
#undef HP
#undef TRF_CHANNEL_VERSION_2
}
#endif
#endif
return tct;
}
/*
*------------------------------------------------------*
*
* TimerKill --
*
* Timer management. Removes the internal timer
* if it exists.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
TimerKill (trans)
TrfTransformationInstance* trans;
{
if (trans->timer != (Tcl_TimerToken) NULL) {
/* Delete an existing flush-out timer,
* prevent it from firing on removed channel.
*/
Tcl_DeleteTimerHandler (trans->timer);
trans->timer = (Tcl_TimerToken) NULL;
PRINT ("Timer deleted ..."); FL;
}
}
/*
*------------------------------------------------------*
*
* TimerSetup --
*
* Timer management. Creates the internal timer
* if it does not exist.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
TimerSetup (trans)
TrfTransformationInstance* trans;
{
if (trans->timer == (Tcl_TimerToken) NULL) {
trans->timer = Tcl_CreateTimerHandler (TRF_DELAY, ChannelHandlerTimer,
(ClientData) trans);
}
}
/*
*------------------------------------------------------*
*
* ChannelHandlerKS --
*
* Management of channel handlers. Deletes/Recreates
* as required by the specified mask.
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ChannelHandlerKS (trans, mask)
TrfTransformationInstance* trans;
int mask;
{
/*
* This procedure is called only for the original and the 8.2
* patch. The new 8.2.3 patch does not use channel handlers but a
* separate NotifyHandler in the driver.
*/
Tcl_Channel parent = DOWNC (trans);
if (trans->watchMask) {
/*
* Remove event handler to underlying channel, this could
* be because we are closing for real, or being "unstacked".
*/
Tcl_DeleteChannelHandler (parent, ChannelHandler,
(ClientData) trans);
}
trans->watchMask = mask;
if (trans->watchMask) {
/*
* Setup active monitor for events on underlying Channel
*/
Tcl_CreateChannelHandler (parent, trans->watchMask,
ChannelHandler, (ClientData) trans);
}
}
|