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
|
% Copyright 2005-2018 Cisco Systems, Inc.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
\chapter{Input/Output Operations\label{CHPTIO}}
This chapter describes {\ChezScheme}'s generic port facility,
operations on ports, and various {\ChezScheme} extensions to the
standard set of input/output operations.
See Chapter~\ref{TSPL:CHPTIO} of {\TSPLFOUR} or the Revised$^6$ Report
on Scheme for a description of standard input/output operations.
Definitions of a few sample generic ports are given in
Section~\ref{SECTPORTEXAMPLES}.
{\ChezScheme} closes file ports automatically after they become
inaccessible to the program or when the Scheme program exits, but it is
best to close ports explicitly whenever possible.
\section{Generic Ports}
{\ChezScheme}'s ``\index{generic port}generic port'' facility
allows the programmer to add new types of textual ports with
arbitrary input/output semantics.
It may be used, for example, to define any of the built-in
Common Lisp~\cite{Steele:common} stream types, i.e.,
\index{synonym streams}synonym streams,
\index{broadcast streams}broadcast streams,
\index{concatenated streams}concatenated streams,
\index{two-way streams}two-way streams,
\index{echo streams}echo streams, and
\index{string streams}string streams.
It may also be used to define more exotic ports, such as ports that
represent windows on a bit-mapped display or ports that represent
processes connected to the current process via pipes or sockets.
Each port has an associated \emph{port handler}.
A port handler is a procedure that accepts messages in an
object-oriented style.
Each message corresponds to one of the low-level Scheme operations
on ports, such as \scheme{read-char} and \scheme{close-input-port} (but not
\scheme{read}, which is defined in terms of the lower-level operations).
Most of these operations simply call the handler immediately with
the corresponding message.
Standard messages adhere to the following conventions: the message name is
the first argument to the handler.
It is always a symbol, and it is always the name of a primitive
Scheme operation on ports.
The additional arguments are the same as the arguments to the
primitive procedure and occur in the same order.
(The port argument to some of the primitive procedures is optional;
in the case of the messages passed to a handler, the port argument
is always supplied.)
The following messages are defined for built-in ports:
\schemedisplay
block-read \var{port} \var{string} \var{count}
block-write \var{port} \var{string} \var{count}
char-ready? \var{port}
clear-input-port \var{port}
clear-output-port \var{port}
close-port \var{port}
file-position \var{port}
file-position \var{port} \var{position}
file-length \var{port}
flush-output-port \var{port}
peek-char \var{port}
port-name \var{port}
read-char \var{port}
unread-char \var{char} \var{port}
write-char \var{char} \var{port}
\endschemedisplay
\noindent
Additional messages may be accepted by user-defined ports.
{\ChezScheme} input and output is normally buffered for efficiency.
To support buffering, each input port contains an input buffer and
each output port contains an output buffer.
Bidirectional ports, ports that are both input ports and output
ports, contain both input and output buffers.
Input is not buffered if the input buffer is the empty string,
and output is not buffered if the output buffer is the empty
string.
In the case of unbuffered input and output, calls to \scheme{read-char},
\scheme{write-char}, and similar messages cause the handler to be invoked
immediately with the corresponding message.
For buffered input and output, calls to these procedures cause the
buffer to be updated, and the handler is not called under normal
circumstances until the buffer becomes empty (for input) or full (for
output).
Handlers for buffered ports must \var{not} count
on the buffer being empty or full when \scheme{read-char}, \scheme{write-char}, and
similar messages are received, however, due to the possibility that (a)
the handler is invoked through some other mechanism, or (b) the
call to the handler is interrupted.
In the presence of keyboard, timer, and other interrupts, it is
possible for a call to a port handler to be interrupted or for the
handler itself to be interrupted.
If the port is accessible outside of the interrupted code, there
is a possibility that the interrupt handler will cause input or
output to be performed on the port.
This is one reason, as stated above, that port handlers must not count
on the input buffer being empty or output buffer being full when a
\scheme{read-char}, \scheme{write-char}, or similar message is received.
In addition, port handlers may need to manipulate the buffers only
with interrupts disabled (using \scheme{with-interrupts-disabled}).
Generic ports are created via one of the port construction
procedures \scheme{make-input-port},
\scheme{make-output-port}, and \scheme{make-input/output-port} defined
later in this chapter.
Ports have seven accessible fields:
\begin{description}
\item[\var{handler},] accessed with \scheme{port-handler};
\item[\var{output-buffer},] accessed with \scheme{port-output-buffer},
\item[\var{output-size},] accessed with \scheme{port-output-size},
\item[\var{output-index},] accessed with \scheme{port-output-index},
\item[\var{input-buffer},] accessed with \scheme{port-input-buffer},
\item[\var{input-size},] accessed with \scheme{port-input-size}, and
\item[\var{input-index},] accessed with \scheme{port-input-index}.
\end{description}
\noindent
The output-size and output-index fields are valid only for output
ports, and the input-size and input-index fields are valid only for
input ports.
The output and input size and index fields may be updated as well
using the corresponding ``\scheme{set-\var{field}!}'' procedure.
A port's output size determines how much of the port's output buffer is
actually available for writing by \scheme{write-char}.
The output size is often the same as the string length of the port's
output buffer, but it can be set to less (but no less than zero) at the
discretion of the programmer.
The output index determines to which position in the port's
buffer the next character will be written.
The output index should be between $0$ and the output size,
inclusive.
If no output has occurred since the buffer was last flushed, the
output index should be $0$.
If the index is less than the size, \scheme{write-char}
stores its character argument into the specified character
position within the buffer and increments the index.
If the index is equal to the size, \scheme{write-char} leaves the fields of
the port unchanged and invokes the handler.
A port's input size determines how much of the port's input buffer is
actually available for reading by \scheme{read-char}.
A port's input size and input index are constrained in the same manner
as output size and index, i.e., the input size must be between
$0$ and the string length of the input buffer (inclusive), and the input
index must be between $0$ and the input size (inclusive).
Often, the input size is less than the length of the input buffer
because there are fewer characters available to read than would fit
in the buffer.
The input index determines from which position in the input buffer the
next character will be read.
If the index is less than the size, \scheme{read-char} extracts the character
in this position, increments the index, and returns the character.
If the index is equal to the size, \scheme{read-char} leaves the fields of
the port unchanged and invokes the handler.
The operation of \scheme{peek-char} is similar to that of \scheme{read-char}, except
that it does not increment the input index.
\scheme{unread-char} decrements the input index if it is greater than $0$,
otherwise it invokes the handler.
\scheme{char-ready?} returns \scheme{#t} if the input index is less than the input
size, otherwise it invokes the handler.
Although the fields shown and discussed above are logically present in
a port, actual implementation details may differ.
The current {\ChezScheme} implementation uses a different representation
that allows \scheme{read-char}, \scheme{write-char}, and similar operations to be
open-coded with minimal overhead.
The access and assignment operators perform the conversion between the
actual representation and the one shown above.
Port handlers receiving a message must return a value appropriate for
the corresponding operation.
For example, a handler receiving a \scheme{read-char} message must return a
character or eof object (if it returns).
For operations that return unspecified values, such as \scheme{close-port},
the handler is not required to return any particular value.
\section{File Options\label{SECTFILEOPTIONS}}
The Revised$^6$ Report requires that the universe of a file-options
enumeration set must include \scheme{no-create}, \scheme{no-fail},
and \scheme{no-truncate}, whose meanings are described within the
description of the \scheme{file-options} syntax in
Section~\ref{TSPL:SECTOPENINGFILES} of {\TSPLFOUR}.
{\ChezScheme} defines a number of additional file options:
\begin{description}
\item[\var{compressed}:]
An output file should be compressed when written; and a compressed input
file should be decompressed when read. The compression format for output
is determined by the \index{\scheme{compress-format}}\scheme{compress-format}
parameter, while the compression format on input is inferred.
The compression level, which is relevant only for output, is determined
by the \index{\scheme{compress-level}}\scheme{compress-level} parameter.
\item[\var{replace}:]
For output files only, replace (remove and recreate) the existing file if
it exists.
\item[\var{exclusive}:]
For output files only, lock the file for exclusive access.
On some systems the lock is advisory, i.e., it inhibits access by
other processes only if they also attempt to open exclusively.
\item[\var{append}:]
For output files only, position the output port at the end of the file
before each write so that output to the port is always appended to the
file.
\item[\var{perm-set-user-id}:]
For newly created output files under Unix-based systems only, set
user-id bit.
\item[\var{perm-set-group-id}:]
For newly created output files under Unix-based systems only, set
group-id bit.
\item[\var{perm-sticky}:]
For newly created output files under Unix-based systems only, set
sticky bit.
\item[\var{perm-no-user-read}:]
For newly created output files under Unix-based systems only,
do not set user read bit.
(User read bit is set by default, unless masked by the process umask.)
\item[\var{perm-no-user-write}:]
For newly created output files under Unix-based systems only,
do not set user write bit.
(User write bit is set by default, unless masked by the process umask.)
\item[\var{perm-user-execute}:]
For newly created output files under Unix-based systems only,
set user execute bit unless masked by process umask.
(User execute bit is not set by default.)
\item[\var{perm-no-group-read}:]
For newly created output files under Unix-based systems only,
do not set group read bit.
(Group read bit is set by default, unless masked by the process umask.)
\item[\var{perm-no-group-write}:]
For newly created output files under Unix-based systems only,
do not set group write bit.
(Group write bit is set by default, unless masked by the process umask.)
\item[\var{perm-group-execute}:]
For newly created output files under Unix-based systems only,
set group execute bit unless masked by process umask.
(Group execute bit is not set by default.)
\item[\var{perm-no-other-read}:]
For newly created output files under Unix-based systems only,
do not set other read bit.
(Other read bit is set by default, unless masked by the process umask.)
\item[\var{perm-no-other-write}:]
For newly created output files under Unix-based systems only,
do not set other write bit.
(Other write bit is set by default, unless masked by the process umask.)
\item[\var{perm-other-execute}:]
For newly created output files under Unix-based systems only,
set other execute bit unless masked by process umask.
(Other execute bit is not set by default.)
\end{description}
\section{Transcoders\label{SECTTRANSCODERS}}
The language of the Revised$^6$ Report provides three built-in codecs:
a latin-1 codec, a utf-8 codec, and a utf-16 codec.
{\ChezScheme} provides three additional codecs: a utf-16le codec,
utf-16be codec, and an ``iconv'' codec for non-Unicode character sets.
It also provides an alternative to the standard utf-16 codec that
defaults to little-endian format rather than the default big-endian
format.
This section describes these codecs, plus a \scheme{current-transcoder}
parameter that allows the programmer to determine the transcoder
used for a textual port whenever the transcoder is implicit, as for
\scheme{open-input-file} or \scheme{load}, along with the
predicate \scheme{transcoder?}, which should be standard but is not.
%----------------------------------------------------------------------------
\entryheader
\formdef{utf-16-codec}{\categoryprocedure}{(utf-16-codec)}
\formdef{utf-16-codec}{\categoryprocedure}{(utf-16-codec \var{endianness})}
\formdef{utf-16le-codec}{\categoryprocedure}{(utf-16le-codec)}
\formdef{utf-16be-codec}{\categoryprocedure}{(utf-16be-codec)}
\returns a codec
\listlibraries
\endentryheader
\var{endianness} must be the symbol \scheme{big} or the symbol
\scheme{little}.
The codec returned by \scheme{utf-16-codec} can be used to create and
process data written UTF-16 format.
When called without the \var{endianness} argument or with \var{endianness}
\scheme{big}, \scheme{utf-16-codec} returns a codec for standard UTF-16
data, i.e., one that defaults to big-endian format if no byte-order mark
(BOM) is found.
When output is transcoded with a transcoder based on this codec, a BOM is
emitted just before the first character written, and each character is
written as a UTF-16 character in big-endian format.
For input, a BOM is looked for at the start of the
input and, if present, controls the byte order of the remaining
UTF-16 characters.
If no BOM is present, big-endian order is assumed.
For input-output ports, the BOM is not emitted if the file is
read before written, and a BOM is not looked for if the file is written
before read.
For textual ports created via \scheme{transcoded-port}, a BOM written or
read via the transcoder appears at the beginning of the underlying data
stream or file only if the binary port passed to \scheme{transcoded-port}
is positioned at the start of the data stream or file.
When the transcoder can determine this is the case, it sets a flag that
causes \scheme{set-port-position!}) to position the port beyond the BOM if
an attempt is made to reposition the port to the start of the data stream
or file, so that the BOM is preserved.
When called with \var{endianness} \scheme{little}, \scheme{utf-16-codec}
returns a codec that defaults to the little-endian format both for reading
and for writing.
For output-only streams or input/output streams that are written before read,
the result is standard UTF-16, with a BOM that specifies little-endian
format followed by characters in little-endian byte order.
For input-only streams or input/output streams that are read before written,
this codec allows programs to read from input streams that either
begin with a BOM or are encoded in UTF-16LE format.
This is particularly useful for handling files that might have been
produced by older Windows applications that claim to produce UTF-16 files
but actually produce UTF-16LE files.
The Revised$^6$ Report version of \scheme{utf-16-codec} lacks the
optional \var{endianness} argument.
The codecs returned by \scheme{utf-16le-codec} and \scheme{utf-16be-codec}
are used to read and write data in the UTF-16LE and UTF-16BE formats,
i.e., UTF-16 with little-endian or big-endian byte order and no BOM.
For output, these codecs are useful for controlling whether and where
the BOM is emitted, since no BOM is emitted implicitly and a BOM
can be emitted explicitly as an ordinary character.
For input, these codecs are useful for processing files known to be
in little-endian or big-endian format with no BOM.
%----------------------------------------------------------------------------
\entryheader
\formdef{iconv-codec}{\categoryprocedure}{(iconv-codec \var{code-page})}
\returns a codec
\listlibraries
\endentryheader
\var{code-page} must be a string and should identify a codec accepted by
the \scheme{iconv} library installed on the target machine.
The codec returned by this procedure can be used to convert from the
non-Unicode single- and multiple-byte character sets supported by
\scheme{iconv}.
When used in the input direction, the codec converts byte sequences
into Scheme strings, and when used in the output direction, it converts
Scheme strings to byte sequences.
The set of supported code pages depends on the version of
\scheme{iconv} available; consult the \scheme{iconv} documentation
or use the shell command \scheme{iconv --list} to obtain a list
of supported code pages.
While the Windows operating system does not supply an \scheme{iconv}
library, it is possible to use \scheme{iconv-codec} on Windows systems by
supplying an \scheme{iconv} dynamic-link library (named \scheme{iconv.dll},
\scheme{libiconv.dll}, or \scheme{libiconv-2.dll}) that provides
Posix-conformant \scheme{iconv_open}, \scheme{iconv}, and
\scheme{iconv_close} entry points either under those names or under the
alternative names \scheme{libiconv_open}, \scheme{libiconv}, and
\scheme{libiconv_close}.
The dll must be located in a standard location for dlls or in the
current directory of the process the first time \scheme{iconv-codec}
is called.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-transcoder}{\categorythreadparameter}{current-transcoder}
\listlibraries
\endentryheader
The transcoder value of the \scheme{current-transcoder} parameter is used
whenever a textual file is opened with an implicit transcoder, e.g., by
\scheme{open-input-file} and other convenience I/O procedures,
\scheme{compile-file} \scheme{include}, \scheme{load}, and
\scheme{pretty-file}.
Its initial value is the value of the \scheme{native-transcoder} procedure.
%----------------------------------------------------------------------------
\entryheader
\formdef{transcoder?}{\categoryprocedure}{(transcoder? \var{obj})}
\returns \scheme{#t} if \var{obj} is a transcoder, \scheme{#f} otherwise
\listlibraries
\endentryheader
\section{Port Operations\label{SECTPORTOPERATIONS}}
The procedures used to create, access, and alter ports directly
are described in this section.
Also described are several nonstandard operations on ports.
Unless otherwise specified, procedures requiring either input ports or
output ports as arguments accept input/output ports as well, i.e., an
input/output port is both an input port and an output port.
%----------------------------------------------------------------------------
\entryheader
\formdef{make-input-port}{\categoryprocedure}{(make-input-port \var{handler} \var{input-buffer})}
\formdef{make-output-port}{\categoryprocedure}{(make-output-port \var{handler} \var{output-buffer})}
\formdef{make-input/output-port}{\categoryprocedure}{(make-input/output-port \var{handler} \var{input-buffer} \var{output-buffer})}
\returns a new textual port
\listlibraries
\endentryheader
\noindent
\var{handler} must be a procedure, and
\var{input-buffer} and \var{output-buffer} must be strings.
Each procedure creates a \index{generic port}generic port.
The handler associated with the port is \var{handler}, the
input buffer is \var{input-buffer}, and the
output buffer is \var{output-buffer}.
For \scheme{make-input-port}, the output buffer is undefined, and for
\scheme{make-output-port}, the input buffer is undefined.
The input size of an input or input/output port is initialized to the
string length of the input buffer, and the input index is set to $0$.
The output size and index of an output or input/output port are
initialized similarly.
The length of an input or output buffer may be zero, in which case
buffering is effectively disabled.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-handler}{\categoryprocedure}{(port-handler \var{port})}
\returns a procedure
\listlibraries
\endentryheader
\noindent
For generic ports, \scheme{port-handler} returns the handler passed to one
of the generic port creation procedures described above.
For ports created by \scheme{open-input-file} and similar procedures,
\scheme{port-handler} returns an internal handler that may be invoked in
the same manner as any other handler.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-input-buffer}{\categoryprocedure}{(port-input-buffer \var{input-port})}
\formdef{port-input-size}{\categoryprocedure}{(port-input-size \var{input-port})}
\formdef{port-input-index}{\categoryprocedure}{(port-input-index \var{input-port})}
\formdef{textual-port-input-buffer}{\categoryprocedure}{(textual-port-input-buffer \var{textual-input-port})}
\formdef{textual-port-input-size}{\categoryprocedure}{(textual-port-input-size \var{textual-input-port})}
\formdef{textual-port-input-index}{\categoryprocedure}{(textual-port-input-index \var{textual-input-port})}
\formdef{binary-port-input-buffer}{\categoryprocedure}{(binary-port-input-buffer \var{binary-input-port})}
\formdef{binary-port-input-size}{\categoryprocedure}{(binary-port-input-size \var{binary-input-port})}
\formdef{binary-port-input-index}{\categoryprocedure}{(binary-port-input-index \var{binary-input-port})}
\returns see below
\listlibraries
\endentryheader
\noindent
These procedures return the input buffer, size, or index
of the input port.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
%----------------------------------------------------------------------------
\entryheader
\formdef{set-port-input-index!}{\categoryprocedure}{(set-port-input-index! \var{input-port} \var{n})}
\formdef{set-port-input-size!}{\categoryprocedure}{(set-port-input-size! \var{input-port} \var{n})}
\formdef{set-port-input-buffer!}{\categoryprocedure}{(set-port-input-buffer! \var{input-port} \var{x})}
\formdef{set-textual-port-input-index!}{\categoryprocedure}{(set-textual-port-input-index! \var{textual-input-port} \var{n})}
\formdef{set-textual-port-input-size!}{\categoryprocedure}{(set-textual-port-input-size! \var{textual-input-port} \var{n})}
\formdef{set-textual-port-input-buffer!}{\categoryprocedure}{(set-textual-port-input-buffer! \var{textual-input-port} \var{string})}
\formdef{set-binary-port-input-index!}{\categoryprocedure}{(set-binary-port-input-index! \var{binary-input-port} \var{n})}
\formdef{set-binary-port-input-size!}{\categoryprocedure}{(set-binary-port-input-size! \var{binary-input-port} \var{n})}
\formdef{set-binary-port-input-buffer!}{\categoryprocedure}{(set-binary-port-input-buffer! \var{binary-input-port} \var{bytevector})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
The procedure \scheme{set-port-input-index!} sets the input index field of
\var{input-port} to $n$, which must be a nonnegative integer less than
or equal to the port's input size.
The procedure \scheme{set-port-input-size!} sets the input size field of
\var{input-port} to $n$, which must be a nonnegative integer less than
or equal to the string length of the port's input buffer.
It also sets the input index to $0$.
The procedure \scheme{set-port-input-buffer!} sets the input buffer field of
\var{input-port} to $x$, which must be a string for textual ports and a
bytevector for binary ports.
It also sets the input size to the length of the string or bytevector
and the input index to $0$.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-input-count}{\categoryprocedure}{(port-input-count \var{input-port})}
\formdef{textual-port-input-count}{\categoryprocedure}{(textual-port-input-count \var{textual-input-port})}
\formdef{binary-port-input-count}{\categoryprocedure}{(binary-port-input-count \var{binary-input-port})}
\returns see below
\listlibraries
\endentryheader
These procedures return an exact integer representing the number of
characters or bytes left to be read from the port's input buffer, i.e.,
the difference between the buffer size and index.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterpart.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-input-empty?}{\categoryprocedure}{(port-input-empty? \var{input-port})}
\returns \scheme{#t} if the port's input buffer contains no more data, otherwise \scheme{#f}
\listlibraries
\endentryheader
This procedure determines whether the port's input count is zero without
computing or returning the actual count.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-output-buffer}{\categoryprocedure}{(port-output-buffer \var{output-port})}
\formdef{port-output-size}{\categoryprocedure}{(port-output-size \var{output-port})}
\formdef{port-output-index}{\categoryprocedure}{(port-output-index \var{output-port})}
\formdef{textual-port-output-buffer}{\categoryprocedure}{(textual-port-output-buffer \var{output-port})}
\formdef{textual-port-output-size}{\categoryprocedure}{(textual-port-output-size \var{output-port})}
\formdef{textual-port-output-index}{\categoryprocedure}{(textual-port-output-index \var{output-port})}
\formdef{binary-port-output-buffer}{\categoryprocedure}{(binary-port-output-buffer \var{output-port})}
\formdef{binary-port-output-size}{\categoryprocedure}{(binary-port-output-size \var{output-port})}
\formdef{binary-port-output-index}{\categoryprocedure}{(binary-port-output-index \var{output-port})}
\returns see below
\listlibraries
\endentryheader
\noindent
These procedures return the output buffer, size, or index
of the output port.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
%----------------------------------------------------------------------------
\entryheader
\formdef{set-port-output-index!}{\categoryprocedure}{(set-port-output-index! \var{output-port} \var{n})}
\formdef{set-port-output-size!}{\categoryprocedure}{(set-port-output-size! \var{output-port} \var{n})}
\formdef{set-port-output-buffer!}{\categoryprocedure}{(set-port-output-buffer! \var{output-port} \var{x})}
\formdef{set-textual-port-output-index!}{\categoryprocedure}{(set-textual-port-output-index! \var{textual-output-port} \var{n})}
\formdef{set-textual-port-output-size!}{\categoryprocedure}{(set-textual-port-output-size! \var{textual-output-port} \var{n})}
\formdef{set-textual-port-output-buffer!}{\categoryprocedure}{(set-textual-port-output-buffer! \var{textual-output-port} \var{string})}
\formdef{set-binary-port-output-index!}{\categoryprocedure}{(set-binary-port-output-index! \var{output-port} \var{n})}
\formdef{set-binary-port-output-size!}{\categoryprocedure}{(set-binary-port-output-size! \var{output-port} \var{n})}
\formdef{set-binary-port-output-buffer!}{\categoryprocedure}{(set-binary-port-output-buffer! \var{binary-output-port} \var{bytevector})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
The procedure \scheme{set-port-output-index!} sets the output index field of
the output port to $n$, which must be a nonnegative integer less than
or equal to the port's output size.
The procedure \scheme{set-port-output-size!} sets the output size field of
the output port to $n$, which must be a nonnegative integer less than
or equal to the string length of the port's output buffer.
It also sets the output index to $0$.
The procedure \scheme{set-port-output-buffer!} sets the output buffer field of
\var{output-port} to $x$, which must be a string for textual ports and a
bytevector for binary ports.
It also sets the output size to the length of the string or bytevector
and the output index to $0$.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-output-count}{\categoryprocedure}{(port-output-count \var{output-port})}
\formdef{textual-port-output-count}{\categoryprocedure}{(textual-port-output-count \var{textual-output-port})}
\formdef{binary-port-output-count}{\categoryprocedure}{(binary-port-output-count \var{binary-output-port})}
\returns see below
\listlibraries
\endentryheader
These procedures return an exact integer representing the amount of
space in characters or bytes available to be written in the
port's output buffer, i.e., the difference between the buffer size
and index.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterpart.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-output-full?}{\categoryprocedure}{(port-output-full? \var{output-port})}
\returns \scheme{#t} if the port's input buffer has no more room, otherwise \scheme{#f}
\listlibraries
\endentryheader
This procedure determines whether the port's output count is zero without
computing or returning the actual count.
%----------------------------------------------------------------------------
\entryheader
\formdef{mark-port-closed!}{\categoryprocedure}{(mark-port-closed! \var{port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
This procedure directly marks the port closed so that no further
input or output operations are allowed on it.
It is typically used by handlers upon receipt of a \scheme{close-port}
message.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-closed?}{\categoryprocedure}{(port-closed? \var{port})}
\returns \scheme{#t} if \var{port} is closed, \scheme{#f} otherwise
\listlibraries
\endentryheader
\schemedisplay
(let ([p (open-output-string)])
(port-closed? p)) ;=> #f
(let ([p (open-output-string)])
(close-port p)
(port-closed? p)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader\label{desc:set-port-bol}
\formdef{set-port-bol!}{\categoryprocedure}{(set-port-bol! \var{output-port} \var{obj})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
When \var{obj} is \scheme{#f}, the port's beginning-of-line (BOL)
flag is cleared; otherwise, the port's BOL flag is set.
The BOL flag is consulted by \scheme{fresh-line}
(page~\pageref{desc:fresh-line}) to determine if it needs to emit a
newline.
This flag is maintained automatically for file output ports, string output
ports, and transcript ports.
The flag is set for newly created file and string output ports, except
for file output ports created with the \scheme{append} option, for which
the flag is reset.
The BOL flag is clear for newly created generic ports and never set
automatically, but may be set explicitly using \scheme{set-port-bol!}.
The port is always flushed immediately before the flag is consulted, so it
need not be maintained on a per-character basis for buffered ports.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-bol?}{\categoryprocedure}{(port-bol? \var{port})}
\returns \scheme{#t} if \var{port}'s BOL flag is set, \scheme{#f} otherwise
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{set-port-eof!}{\categoryprocedure}{(set-port-eof! \var{input-port} \var{obj})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
When \var{obj} is not \scheme{#f}, \scheme{set-port-eof!} marks \var{input-port}
so that, once its buffer is empty, the port is treated as if it were at
eof even if more data is available in the underlying byte or character
stream.
Once this artificial eof has been read, the eof mark is cleared, making
any additional data in the stream available beyond the eof.
This feature can be used by a generic port to simulate a stream consisting
of multiple input files.
When \var{obj} is \scheme{#f}, the eof mark is cleared.
The following example assumes /dev/zero provides an infinite stream of
zero bytes.
\schemedisplay
(define p
(parameterize ([file-buffer-size 3])
(open-file-input-port "/dev/zero")))
(set-port-eof! p #t)
(get-u8 p) ;=> #!eof
(get-u8 p) ;=> 0
(set-port-eof! p #t)
(get-u8 p) ;=> 0
(get-u8 p) ;=> 0
(get-u8 p) ;=> #!eof
(get-u8 p) ;=> 0
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{port-name}{\categoryprocedure}{(port-name \var{port})}
\returns the name associated with \var{port}
\listlibraries
\endentryheader
\noindent
The name may be any object but is usually a string or \scheme{#f}
(denoting no name).
For file ports, the name is typically a string naming the file.
\schemedisplay
(let ([p (open-input-file "myfile.ss")])
(port-name p)) ;=> "myfile.ss"
(let ([p (open-output-string)])
(port-name p)) ;=> "string"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{set-port-name!}{\categoryprocedure}{(set-port-name! \var{port} \var{obj})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
This procedure sets \var{port}'s name to \var{obj}, which should be
a string or \scheme{#f} (denoting no name).
%----------------------------------------------------------------------------
\entryheader
\formdef{port-length}{\categoryprocedure}{(port-length \var{port})}
\formdef{file-length}{\categoryprocedure}{(file-length \var{port})}
\returns the length of the file or other object to which \var{port} refers
\formdef{port-has-port-length?}{\categoryprocedure}{(port-has-port-length? \var{port})}
\returns \scheme{#t} if the port supports \scheme{port-length}, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
A port may allow the length of the underlying stream of characters or bytes
to be determined.
If so, the procedure \scheme{port-has-port-length?} returns
\scheme{#t} and \scheme{port-length} returns the current length.
For binary ports, the length is always an exact nonnegative integer byte
count.
For textual ports, the representation of a length is unspecified; it
may not be an exact nonnegative integer and, even if it is, it may not
represent either a byte or character count.
The length may be used at some later time to reset the length if the
port supports \scheme{set-port-length!}.
If \scheme{port-length} is called on a port that does not support it,
an exception with condition type \scheme{&assertion} is raised.
File lengths beyond $2^{32}$ might not be reported property
for compressed files on 32-bit versions of the system.
\scheme{file-length} is identical to \scheme{port-length}.
%----------------------------------------------------------------------------
\entryheader
\formdef{set-port-length!}{\categoryprocedure}{(set-port-length! \var{port} \var{len})}
\returns unspecified
\formdef{port-has-set-port-length!?}{\categoryprocedure}{(port-has-set-port-length!? \var{port})}
\returns \scheme{#t} if the port supports \scheme{set-port-length!}, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
A port may allow the length of the underlying stream of characters or bytes
to be set, i.e., extended or truncated.
If so, the procedure \scheme{port-has-set-port-length!?} returns
\scheme{#t} and \scheme{set-port-length!} changes the length.
For binary ports, the length \var{len} must be an exact nonnegative integer byte
count.
For textual ports, the representation of a length is unspecified, as
described in the entry for \scheme{port-length} above, but \var{len} must be
an appropriate length for the textual port, which is usually guaranteed
to be the case only if it was obtained from a call to \scheme{port-length}
on the same port.
If \scheme{set-port-length!} is called on a port that does not support it,
an exception with condition type \scheme{&assertion} is raised.
It is not possible to set the length of a port opened with compression
to an arbitrary position, and the result of an attempt to set the length
of a compressed file beyond $2^{32}$ on 32-bit versions of the system is
undefined.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-nonblocking?}{\categoryprocedure}{(port-nonblocking? \var{port})}
\returns \scheme{#t} if the port is in nonblocking mode, \scheme{#f} otherwise
\formdef{port-has-port-nonblocking??}{\categoryprocedure}{(port-has-port-nonblocking?? \var{port})}
\returns \scheme{#t} if the port supports \scheme{port-nonblocking?}, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
A port may allow the nonblocking status of the port to be determined.
If so, the procedure \scheme{port-has-port-nonblocking??} returns
\scheme{#t} and \scheme{port-nonblocking?} returns a boolean value
reflecting whether the port is in nonblocking mode.
%----------------------------------------------------------------------------
\entryheader
\formdef{set-port-nonblocking!}{\categoryprocedure}{(set-port-nonblocking! \var{port} \var{obj})}
\returns unspecified
\formdef{port-has-set-port-nonblocking!?}{\categoryprocedure}{(port-has-set-port-nonblocking!? \var{port})}
\returns \scheme{#t} if the port supports \scheme{set-port-nonblocking!}, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
A port may allow reads or writes to be performed in a ``nonblocking'' fashion.
If so, the procedure \scheme{port-has-set-port-nonblocking!?} returns
\scheme{#t} and \scheme{set-port-nonblocking!} sets the port to nonblocking
mode (if \var{obj} is a true value) or blocking mode (if \var{obj} is \scheme{#f}).
If \scheme{set-port-nonblocking!} is called on a port that does not support it,
an exception with condition type \scheme{&assertion} is raised.
Ports created by the standard Revised$^6$ port opening procedures are
initially set in blocking mode by default.
The same is true for most of the procedures described in this document.
A generic port based on a nonblocking source may be nonblocking
initially.
A port returned by \scheme{open-fd-input-port},
\scheme{open-fd-output-port}, or \scheme{open-fd-input/output-port}
is initially in nonblocking mode if the file-descriptor passed in is in
nonblocking mode.
Similarly, a port returned by \scheme{standard-input-port},
\scheme{standard-output-port}, or \scheme{standard-error-port} is
initially in nonblocking mode if the underlying stdin, stdout,
or stderr file descriptor is in nonblocking mode.
Although \scheme{get-bytevector-some} and \scheme{get-string-some} normally
cannot return an empty bytevector or empty string, they can if the port
is in nonblocking mode and no input is available.
Also, \scheme{get-bytevector-some!} and \scheme{get-string-some!}
may not read any data if the port is in nonblocking mode and
no data is available.
Similarly, \scheme{put-bytevector-some} and \scheme{put-string-some}
may not write any data if the port is in nonblocking mode and
no room is available.
Nonblocking mode is not supported under Windows.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-position}{\categoryprocedure}{(file-position \var{port})}
\formdef{file-position}{\categoryprocedure}{(file-position \var{port} \var{pos})}
\returns see below
\listlibraries
\endentryheader
When the second argument is omitted, this procedure behaves like the R6RS
\scheme{port-position} procedure, and when present, like the R6RS
\scheme{set-port-position!} procedure.
For compressed files opened with the \scheme{compressed} flag,
\scheme{file-position} returns the position in the
uncompressed stream of data.
Changing the position of a compressed input file opened with the
\scheme{compressed} flag generally requires rewinding and rereading the
file and might thus be slow.
The position of a compressed output file opened with the
\scheme{compressed} flag can be moved forward only; this is
accomplished by writing a (compressed) sequence of zeros.
File positions beyond $2^{32}$ might not be reported property
for compressed files on 32-bit versions of the system.
%----------------------------------------------------------------------------
\entryheader
\formdef{clear-input-port}{\categoryprocedure}{(clear-input-port)}
\formdef{clear-input-port}{\categoryprocedure}{(clear-input-port \var{input-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
If \var{input-port} is not supplied, it defaults to the current input port.
This procedure discards any data in the buffer associated
with \var{input-port}.
This may be necessary, for example, to clear any type-ahead from the keyboard
in preparation for an urgent query.
%----------------------------------------------------------------------------
\entryheader
\formdef{clear-output-port}{\categoryprocedure}{(clear-output-port)}
\formdef{clear-output-port}{\categoryprocedure}{(clear-output-port \var{output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
If \var{output-port} is not supplied, it defaults to the current output port.
This procedure discards any data in the buffer associated
with \var{output-port}.
This may be necessary, for example, to clear any pending output on an
interactive port in preparation for an urgent message.
%----------------------------------------------------------------------------
\entryheader
\formdef{flush-output-port}{\categoryprocedure}{(flush-output-port)}
\formdef{flush-output-port}{\categoryprocedure}{(flush-output-port \var{output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
If \var{output-port} is not supplied, it defaults to the current output port.
This procedure forces any data in the buffer associated
with \var{output-port} to be printed immediately.
The console output port is automatically flushed after a newline and before
input from the console input port; all ports are automatically flushed when
they are closed.
\scheme{flush-output-port} may be necessary, however, to force a message
without a newline to be sent to the console output port or to force output
to appear on a file without delay.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-file-compressed!}{\categoryprocedure}{(port-file-compressed! \var{port})}
\returns unspecified
\listlibraries
\endentryheader
\var{port} must be an input or an output port, but not an input/output port.
It must be a file port pointing to a regular file, i.e., a file on disk rather
than, e.g., a socket.
The port can be a binary or textual port.
If the port is an output port, subsequent output sent to the port
will be compressed.
If the port is an input port, subsequent input will be decompressed
if and only if the port is currently pointing at compressed data.
The compression format for output
is determined by the \index{\scheme{compress-format}}\scheme{compress-format}
parameter, while the compression format on input is inferred.
The compression level, which is relevant only for output, is determined
by the \index{\scheme{compress-level}}\scheme{compress-level} parameter.
This procedure has no effect if the port is already set for compression.
%----------------------------------------------------------------------------
\entryheader
\formdef{compress-format}{\categorythreadparameter}{compress-format}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{compress-format} determines the
compression algorithm and format used for output. Currently,
the possible values of the parameter are the symbols \scheme{lz4} (the default)
and \scheme{gzip}.
The \scheme{lz4} format uses the LZ4 compression library developed by
Yann Collet.
It is therefore compatible with the \scheme{lz4} program, which
means that \scheme{lz4} may be used to uncompress files produced
by {\ChezScheme} and visa versa.
The \scheme{gzip} format uses the zlib compression library developed by
Jean-loup Gailly and Mark Adler.
It is therefore compatible with the \scheme{gzip} program, which
means that \scheme{gzip} may be used to uncompress files produced
by {\ChezScheme} and visa versa.
Reading \scheme{lz4}-compressed data tends to be much faster than reading
\scheme{gzip}-compressed data, while \scheme{gzip}-compressed data tends to
be significantly smaller.
%----------------------------------------------------------------------------
\entryheader
\formdef{compress-level}{\categorythreadparameter}{compress-level}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{compress-level} determines the amount of effort spent on
compression and is thus relevant only for output.
It can be set to one of the symbols \scheme{minimum}, \scheme{low},
\scheme{medium}, \scheme{high}, or \scheme{maximum}, which are
listed in order from shortest to longest expected compression time
and least to greatest expected effectiveness.
Its default value is \scheme{medium}.
\section{String Ports\label{SECTIOSTRINGPORTS}}
String ports allow the creation and manipulation of strings via port
operations.
The procedure
\scheme{open-input-string} converts a string into a textual input port,
allowing the characters in the string to be read in sequence via input
operations such as \scheme{read-char} or \scheme{read}.
The procedure
\scheme{open-output-string} allows new strings to be built up with
output operations such as \scheme{write-char} and \scheme{write}.
While string ports could be defined as generic ports, they are instead
supported as primitive by the implementation.
%----------------------------------------------------------------------------
\entryheader
\formdef{open-input-string}{\categoryprocedure}{(open-input-string \var{string})}
\returns a new string input port
\listlibraries
\endentryheader
\noindent
A \index{string input port}string input port is similar to a file input port, except that
characters and objects drawn from the port come from \var{string}
rather than from a file.
A string port is at ``end of file'' when the port reaches the end of the
string.
It is not necessary to close a string port, although it is okay to do so.
\schemedisplay
(let ([p (open-input-string "hi mom!")])
(let ([x (read p)])
(list x (read p)))) ;=> (hi mom!)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{with-input-from-string}{\categoryprocedure}{(with-input-from-string \var{string} \var{thunk})}
\returns the values returned by \var{thunk}
\listlibraries
\endentryheader
\noindent
\var{thunk} must be a procedure and should accept zero arguments.
\scheme{with-input-from-string} parameterizes the current input port to be the
result of opening \var{string} for input during the
application of \var{thunk}.
\schemedisplay
(with-input-from-string "(cons 3 4)"
(lambda ()
(eval (read)))) ;=> (3 . 4)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{open-output-string}{\categoryprocedure}{(open-output-string)}
\returns a new string output port
\listlibraries
\endentryheader
\noindent
A \index{string output port}string output port is similar to a file output port, except that
characters and objects written to the port are placed in a string
(which grows as needed) rather than to a file.
The string built by writing to a string output port may be obtained
with \scheme{get-output-string}.
See the example given for \scheme{get-output-string} below.
It is not necessary to close a string port, although it is okay to do so.
%----------------------------------------------------------------------------
\entryheader
\formdef{get-output-string}{\categoryprocedure}{(get-output-string \var{string-output-port})}
\returns the string associated with \var{string-output-port}
\listlibraries
\endentryheader
\noindent
\var{string-output-port} must be an port returned by \scheme{open-output-string}.
As a side effect, \scheme{get-output-string} resets \var{string-output-port}
so that subsequent output to \var{string-output-port} is placed
into a fresh string.
\schemedisplay
(let ([p (open-output-string)])
(write 'hi p)
(write-char #\space p)
(write 'mom! p)
(get-output-string p)) ;=> "hi mom!"
\endschemedisplay
\noindent
An implementation of \scheme{format} (Section~\ref{SECTFORMAT}) might be
written using string-output ports to produce string output.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-output-to-string}{\categoryprocedure}{(with-output-to-string \var{thunk})}
\returns a string containing the output
\listlibraries
\endentryheader
\noindent
\var{thunk} must be a procedure and should accept zero arguments.
\scheme{with-output-to-string} parameterizes the current output port to
a new string output port during the
application of \var{thunk}.
If \var{thunk} returns, the string associated with the new string output
port is returned, as with \scheme{get-output-string}.
\schemedisplay
(with-output-to-string
(lambda ()
(display "Once upon a time ...")
(newline))) ;=> "Once upon a time ...\n"
\endschemedisplay
\section{File Ports\label{SECTIOFILEPORTS}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{file-buffer-size}{\categorythreadparameter}{file-buffer-size}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{file-buffer-size} is a parameter that determines the size of each
buffer created when the buffer mode is not \scheme{none} for a port
created by one of the file open operations, e.g., \scheme{open-input-file}
or \scheme{open-file-output-port}.
When called with no arguments, the parameter returns the
current buffer size.
When called with a positive fixnum \var{k},
it sets the current buffer size to \var{k}.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-port?}{\categoryprocedure}{(file-port? \var{port})}
\returns \scheme{#t} if \var{port} is a file port, \scheme{#f} otherwise
\listlibraries
\endentryheader
A file port is any port based directly
on an O/S file descriptor, e.g., one created by \scheme{open-file-input-port},
\scheme{open-output-port}, \scheme{open-fd-input-port}, etc., but not
a string, bytevector, or custom port.
%----------------------------------------------------------------------------
\entryheader
\formdef{port-file-descriptor}{\categoryprocedure}{(port-file-descriptor \var{port})}
\returns the file descriptor associated with \var{port}
\listlibraries
\endentryheader
\var{port} must be a file port, i.e., a port for which \var{file-port?}
returns \scheme{#t}.
\section{Custom Ports\label{SECTIOCUSTOMPORTS}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{custom-port-buffer-size}{\categorythreadparameter}{custom-port-buffer-size}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{custom-port-buffer-size} is a parameter that determines the sizes
of the buffers associated with newly created custom ports.
When called with no arguments, the parameter returns the
current buffer size.
When called with a positive fixnum \var{k},
it sets the current buffer size to \var{k}.
\section{Input Operations\label{SECTIOINPUT}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{console-input-port}{\categoryglobalparameter}{console-input-port}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{console-input-port} is a parameter that determines the
input port used by the waiter and interactive debugger.
When called with no arguments, it returns the
console input port.
When called with one argument, which must be a textual input port,
it changes the value of the console
input port.
The initial value of this parameter is a port tied to the standard
input (stdin) stream of the Scheme process.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-input-port}{\categorythreadparameter}{current-input-port}
\listlibraries
\endentryheader
\noindent
\scheme{current-input-port} is a parameter that determines the
default port argument for most input procedures, including
\scheme{read-char}, \scheme{peek-char}, and \scheme{read},
When called with no arguments, \scheme{current-input-port} returns the
current input port.
When called with one argument, which must be a textual input port,
it changes the value of the current
input port.
The Revised$^6$ Report version of \scheme{current-input-port} accepts
only zero arguments, i.e., it cannot be used to change the current input
port.
The initial value of this parameter is the same port as the initial
value of \scheme{console-input-port}.
%----------------------------------------------------------------------------
\entryheader
\formdef{open-input-file}{\categoryprocedure}{(open-input-file \var{path})}
\formdef{open-input-file}{\categoryprocedure}{(open-input-file \var{path} \var{options})}
\returns a new input port
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{open-input-file} opens a textual input port for the file named by
\var{path}.
An exception is raised with condition type
\scheme{&i/o-filename} if the file does not exist or cannot be
opened for input.
\var{options}, if present, is a symbolic option name or option list.
Possible symbolic option names are
\scheme{compressed}, \scheme{uncompressed}, \scheme{buffered},
and \scheme{unbuffered}.
An option list is a list containing zero or more symbolic option names.
The mutually exclusive \scheme{compressed} and
\scheme{uncompressed} options determine whether the input file
should be decompressed if it is compressed (where the compression
format is inferred).
(See \scheme{open-output-file}.)
The default is \scheme{uncompressed}, so the \scheme{uncompressed}
option is useful only as documentation.
The mutually exclusive \scheme{buffered} and \scheme{unbuffered}
options determine whether input is buffered.
When input is buffered, it is read in large blocks and buffered internally
for efficiency to reduce the number of operating system requests.
When the \scheme{unbuffered} option is specified, input is unbuffered,
but not fully, since one character of buffering is required to support
\scheme{peek-char} and \scheme{unread-char}.
Input is buffered by default, so the \scheme{buffered} option is useful
only as documentation.
% flushed this: posix lockf works only on file descriptors open for writing
%The mutually exclusive \scheme{exclusive} and \scheme{nonexclusive}
%options determine whether access to the file is ``exclusive.''
%When the exclusive option is specified, the file is locked until
%the port is closed to prevent access by other processes.
%On some systems the lock is advisory, i.e., it inhibits access by
%other processes only if they also attempt to open exclusively.
%Nonexclusive access is the default, so the \scheme{nonexclusive} option
%is useful only as documentation.
For example, the call
\schemedisplay
(open-input-file "frob" '(compressed))
\endschemedisplay
opens the file frob with decompression enabled.
The Revised$^6$ Report version of \scheme{open-input-file} does not
support the optional \var{options} argument.
%----------------------------------------------------------------------------
\entryheader
\formdef{call-with-input-file}{\categoryprocedure}{(call-with-input-file \var{path} \var{procedure})}
\formdef{call-with-input-file}{\categoryprocedure}{(call-with-input-file \var{path} \var{procedure} \var{options})}
\returns the values returned by \var{procedure}
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\var{procedure} should accept one argument.
\scheme{call-with-input-file} creates a new input port for the file named
by \var{path}, as if with \scheme{open-input-file}, and passes this port to \var{procedure}.
If \var{procedure} returns normally, \scheme{call-with-input-file} closes the input port
and returns the values returned by \var{procedure}.
\scheme{call-with-input-file} does not automatically close the input
port if a continuation created outside of \var{procedure} is invoked, since it
is possible that another continuation created inside of \var{procedure} will be
invoked at a later time, returning control to \var{procedure}.
If \var{procedure} does not return, an implementation is free to close the
input port only if it can prove that the input port is no longer accessible.
As shown in Section~\ref{TSPL:SECTCONTINUATIONS} of {\TSPLFOUR}, \scheme{dynamic-wind} may be used to
ensure that the port is closed if a continuation created outside of
\var{procedure} is invoked.
See \scheme{open-input-file} above for a description of the optional
\var{options} argument.
The Revised$^6$ Report version of \scheme{call-with-input-file} does not
support the optional \var{input} argument.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-input-from-file}{\categoryprocedure}{(with-input-from-file \var{path} \var{thunk})}
\formdef{with-input-from-file}{\categoryprocedure}{(with-input-from-file \var{path} \var{thunk} \var{options})}
\returns the values returned by \var{thunk}
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\var{thunk} must be a procedure and should accept zero arguments.
\scheme{with-input-from-file} temporarily changes the current input port to be the
result of opening the file named by \var{path}, as if with \scheme{open-input-file}, during the
application of \var{thunk}.
If \var{thunk} returns, the port is closed and the current input port
is restored to its old value.
The behavior of \scheme{with-input-from-file} is unspecified
if a continuation created outside
of \var{thunk} is invoked before \var{thunk} returns.
An implementation may close the port and restore the current input
port to its old value---but it may not.
See \scheme{open-input-file} above for a description of the optional
\var{options} argument.
The Revised$^6$ Report version of \scheme{with-input-from-file} does not
support the optional \var{options} argument.
%----------------------------------------------------------------------------
\entryheader
\formdef{open-fd-input-port}{\categoryprocedure}{(open-fd-input-port \var{fd})}
\formdef{open-fd-input-port}{\categoryprocedure}{(open-fd-input-port \var{fd} \var{b-mode})}
\formdef{open-fd-input-port}{\categoryprocedure}{(open-fd-input-port \var{fd} \var{b-mode} \var{?transcoder})}
\returns a new input port for the file descriptor \var{fd}
\listlibraries
\endentryheader
\noindent
\var{fd} must be a nonnegative exact integer and should be a valid
open file descriptor.
If \var{?transcoder} is present and not \scheme{#f}, it must be a
transcoder, and this procedure returns a textual input port
whose transcoder is \var{?transcoder}.
Otherwise, this procedure returns a binary input port.
See the lead-in to Section~\ref{TSPL:SECTOPENINGFILES} of {\TSPLFOUR}
for a description of the constraints on and effects of the other
arguments.
The file descriptor is closed when the port is closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{standard-input-port}{\categoryprocedure}{(standard-input-port)}
\formdef{standard-input-port}{\categoryprocedure}{(standard-input-port \var{b-mode})}
\formdef{standard-input-port}{\categoryprocedure}{(standard-input-port \var{b-mode} \var{?transcoder})}
\returns a new input port connected to the process's standard input
\listlibraries
\endentryheader
\noindent
If \var{?transcoder} is present and not \scheme{#f}, it must be a
transcoder, and this procedure returns a textual input port
whose transcoder is \var{?transcoder}.
Otherwise, this procedure returns a binary input port.
The buffer mode \var{b-mode} defaults to \scheme{block}.
The Revised$^6$ Report version of this procedure does not accept the
optional \var{b-mode} and \var{?transcoder} arguments, which limits
it to an implementation-dependent buffering mode (\scheme{block} in
{\ChezScheme}) and binary output.
%----------------------------------------------------------------------------
\entryheader
\formdef{get-string-some}{\categoryprocedure}{(get-string-some \var{textual-input-port})}
\returns a nonempty string or the eof object
\listlibraries
\endentryheader
\noindent
If \var{textual-input-port} is at end of file, the eof object is returned.
Otherwise, \scheme{get-string-some} reads (as if with \scheme{get-u8})
at least one character and possibly more, and returns a string containing
these characters.
The port's position is advanced past the characters read.
The maximum number of characters read by this operation is implementation-dependent.
An exception to the ``at least one character'' guarantee occurs
if the port is in nonblocking mode (see \scheme{set-port-nonblocking!})
and no input is ready.
In this case, an empty string is returned.
%----------------------------------------------------------------------------
\entryheader
\formdef{get-string-some!}{\categoryprocedure}{(get-string-some! \var{textual-input-port} \var{string} \var{start} \var{n})}
\returns the count of characters read, as an exact nonnegative integer, or the eof object
\listlibraries
\endentryheader
\noindent
\var{start} and \var{n} must be exact nonnegative integers, and the sum of
\var{start} and \var{n} must not exceed the length of \var{string}.
If \var{n} is 0, this procedure returns zero without attempting to
read from \var{textual-input-port} and without modifying \var{string}.
Otherwise, if \var{textual-input-port} is at end of file, this procedure
returns the eof object, except it returns zero when the port is in nonblocking mode
(see \scheme{set-port-nonblocking!}) and the port cannot be determined
to be at end of file without blocking.
In either case, \var{string} is not modified.
Otherwise, this procedure reads (as if with \scheme{get-char})
up to \var{n} characters from the port, stores the characters in consecutive locations of \var{string}
starting at \var{start}, advances the port's position just past the characters read, and
returns the count of characters read.
If the port is in nonblocking mode, this procedure reads no more
than it can without blocking and thus might read zero characters;
otherwise, it reads at least one character but no more than are available
when the first character becomes available.
%----------------------------------------------------------------------------
\entryheader
\formdef{get-bytevector-some!}{\categoryprocedure}{(get-bytevector-some! \var{binary-input-port} \var{bytevector} \var{start} \var{n})}
\returns the count of bytes read, as an exact nonnegative integer, or the eof object
\listlibraries
\endentryheader
\noindent
\var{start} and \var{n} must be exact nonnegative integers, and the sum of
\var{start} and \var{n} must not exceed the length of \var{bytevector}.
If \var{n} is 0, this procedure returns zero without attempting to
read from \var{binary-input-port} and without modifying \var{bytevector}.
Otherwise, if \var{binary-input-port} is at end of file, this procedure
returns the eof object, except it returns zero when the port is in nonblocking mode
(see \scheme{set-port-nonblocking!}) and the port cannot be determined
to be at end of file without blocking.
In either case, \var{bytevector} is not modified.
Otherwise, this procedure reads (as if with \scheme{get-u8})
up to \var{n} bytes from the port, stores the bytes in consecutive locations of \var{bytevector}
starting at \var{start}, advances the port's position just past the bytes read, and
returns the count of bytes read.
If the port is in nonblocking mode, this procedure reads no more
than it can without blocking and thus might read zero bytes;
otherwise, it reads at least one byte but no more than are available
when the first byte becomes available.
%----------------------------------------------------------------------------
\entryheader
\formdef{unread-char}{\categoryprocedure}{(unread-char \var{char})}
\formdef{unread-char}{\categoryprocedure}{(unread-char \var{char} \var{textual-input-port})}
\formdef{unget-char}{\categoryprocedure}{(unget-char \var{textual-input-port} \var{char})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
For \scheme{unread-char}, if \var{textual-input-port} is not supplied, it
defaults to the current input port.
These procedures ``unread'' the last character read from
\scheme{textual-input-port}.
\var{char} may or may not be ignored, depending upon the implementation.
In any case, \var{char} should be last character read from the port.
A character should not be unread twice on the same port
without an intervening call to \scheme{read-char} or \scheme{get-char}.
\scheme{unread-char} and \scheme{unget-char} are provided for applications
requiring one character of lookahead and may be used in place of, or even
in combination with, \scheme{peek-char} or \scheme{lookahead-char}.
One character of lookahead is required in the procedure
\scheme{read-word}, which is defined below in terms of \scheme{unread-char}.
\scheme{read-word} returns the next word from a textual input port as a string, where
a word is defined to be a sequence of alphabetic characters.
Since it does not know until it reads one character too many that it has
read the entire word, \scheme{read-word} uses \scheme{unread-char} to
return the character to the input port.
\schemedisplay
(define read-word
(lambda (p)
(list->string
(let f ([c (read-char p)])
(cond
[(eof-object? c) '()]
[(char-alphabetic? c)
(cons c (f (read-char p)))]
[else
(unread-char c p)
'()])))))
\endschemedisplay
\noindent
In the alternate version below, \scheme{peek-char} is used instead of
\scheme{unread-char}.
\schemedisplay
(define read-word
(lambda (p)
(list->string
(let f ([c (peek-char p)])
(cond
[(eof-object? c) '()]
[(char-alphabetic? c)
(read-char p)
(cons c (f (peek-char p)))]
[else '()])))))
\endschemedisplay
\noindent
The advantage of \scheme{unread-char} in this situation is that only
one call to \scheme{unread-char} per word is required, whereas one
call to \scheme{peek-char} is required for each character in the word
plus the first character beyond.
In many cases, \scheme{unread-char} and \scheme{unget-char} do not enjoy
this advantage, and \scheme{peek-char} or \scheme{lookahead-char}
should be used instead.
%----------------------------------------------------------------------------
\entryheader
\formdef{unget-u8}{\categoryprocedure}{(unget-u8 \var{binary-input-port} \var{octet})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
This procedures ``unreads'' the last byte read from
\scheme{binary-input-port}.
\var{octet} may or may not be ignored, depending upon the implementation.
In any case, \var{octet} should be last byte read from the port.
A byte should not be unread twice on the same port
without an intervening call to \scheme{get-u8}.
%----------------------------------------------------------------------------
\entryheader
\formdef{input-port-ready?}{\categoryprocedure}{(input-port-ready? \var{input-port})}
\returns \scheme{#t} if data is available on \var{input-port}, \scheme{#f} otherwise
\listlibraries
\endentryheader
\scheme{input-port-ready?} allows a program to check to see if input is
available on a textual or binary input port without hanging.
If input is available or the port is at end of file,
\scheme{input-port-ready?} returns \scheme{#t}.
If it cannot determine from the port whether input is ready,
\scheme{input-port-ready?} raises an exception with condition type
\scheme{&i/o-read-error}.
Otherwise, it returns \scheme{#f}.
%----------------------------------------------------------------------------
\entryheader
\formdef{char-ready?}{\categoryprocedure}{(char-ready?)}
\formdef{char-ready?}{\categoryprocedure}{(char-ready? \var{textual-input-port})}
\returns \scheme{#t} if a character is available on \var{textual-input-port}, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
If \var{textual-input-port} is not supplied, it defaults to the current input port.
\scheme{char-ready?} is like \scheme{input-port-ready?} except it is
restricted to textual input ports.
%----------------------------------------------------------------------------
\entryheader
\formdef{block-read}{\categoryprocedure}{(block-read \var{textual-input-port} \var{string})}
\formdef{block-read}{\categoryprocedure}{(block-read \var{textual-input-port} \var{string} \var{count})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{count} must be a nonnegative fixnum less than or equal to the
length of \var{string}.
If not provided, it defaults to the length of \var{string}.
If \var{textual-input-port} is at end-of-file, an eof object is returned.
Otherwise, \var{string} is filled with as many characters as are
available for reading from \var{textual-input-port} up to \var{count},
and the number of characters placed in the string is returned.
If \var{textual-input-port} is buffered and the buffer is nonempty,
the buffered input or a portion thereof is returned; otherwise
\scheme{block-read} bypasses the buffer entirely.
%----------------------------------------------------------------------------
\entryheader
\formdef{read-token}{\categoryprocedure}{(read-token)}
\formdef{read-token}{\categoryprocedure}{(read-token \var{textual-input-port})}
\formdef{read-token}{\categoryprocedure}{(read-token \var{textual-input-port} \var{sfd} \var{bfp})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{sfd} must be a source-file descriptor.
\var{bfp} must be an exact nonnegative integer and should be the
character position of the next character to be read from
\var{textual-input-port}.
Parsing of a Scheme datum is conceptually performed in two steps.
First, the sequence of characters that form the datum are grouped into
\scheme{tokens}, such as symbols, numbers, left parentheses, and
double quotes.
During this first step, whitespace and comments are discarded.
Second, these tokens are grouped into data.
\scheme{read} performs both of these steps and creates an internal
representation of each datum it parses.
\scheme{read-token} may be used to perform the first step only, one
token at a time.
\scheme{read-token} is intended to be used by editors and program
formatters that must be able to parse a program or datum without
actually reading it.
If \var{textual-input-port} is not supplied, it defaults to the current input port.
One token is read from the input port and returned as four values:
\begin{description}
\item[\var{type}:] a symbol describing the type of token read,
\item[\var{value}:] the token value,
\item[\var{start}:] the position of the first character of the token,
relative to the starting position of the input port (or \scheme{#f},
if the position cannot be determined), and
\item[\var{end}:] the first position beyond the token,
relative to the starting position of the input port (or \scheme{#f},
if the position cannot be determined).
\end{description}
\noindent
The input port is left pointing to the first character position beyond
the token.
When the token type fully specifies the token,
\scheme{read-token} returns \scheme{#f} for the value.
The token types are listed below with the corresponding \var{value}
in parentheses.
\begin{description}
\item[\scheme{atomic}] (\var{atom}) an atomic value, i.e.,
a symbol, boolean, number, character, \scheme{#!eof},
or \scheme{#!bwp}
\item[\scheme{box}] (\scheme{#f}) box prefix, i.e., \scheme{#&}
\item[\scheme{dot}] (\scheme{#f}) dotted pair separator, i.e., \scheme{.}
\item[\scheme{eof}] (\scheme{#!eof}) end of file
\item[\scheme{fasl}] (\scheme{#f}) fasl prefix, i.e., \scheme{#@}
\item[\scheme{insert}] (\var{n}) graph reference, i.e., \scheme{#\var{n}#}
\item[\scheme{lbrack}] (\scheme{#f}) open square bracket
\item[\scheme{lparen}] (\scheme{#f}) open parenthesis
\item[\scheme{mark}] (\var{n}) graph mark, i.e., \scheme{#\var{n}=}
\item[\scheme{quote}] (\scheme{quote}, \scheme{quasiquote},
\scheme{syntax}, \scheme{unquote}, \scheme{unquote-splicing},
or \scheme{datum-comment})
an abbreviation mark, e.g., \scheme{'} or \scheme{,@} or
datum-comment prefix
\item[\scheme{rbrack}] (\scheme{#f}) close square bracket
\item[\scheme{record-brack}] (\scheme{#f}) record open bracket, i.e., \scheme{#[}
\item[\scheme{rparen}] (\scheme{#f}) close parenthesis
\item[\scheme{vfxnparen}] (\var{n}) fxvector prefix, i.e., \scheme{#\var{n}vfx(}
\item[\scheme{vfxparen}] (\scheme{#f}) fxvector prefix, i.e., \scheme{#vfx(}
\item[\scheme{vnparen}] (\var{n}) vector prefix, i.e., \scheme{#\var{n}(}
\item[\scheme{vparen}] (\scheme{#f}) vector prefix, i.e., \scheme{#(}
\item[\scheme{vu8nparen}] (\var{n}) bytevector prefix, i.e., \scheme{#\var{n}vu8(}
\item[\scheme{vu8paren}] (\scheme{#f}) bytevector prefix, i.e., \scheme{#vu8(}
\end{description}
\noindent
The set of token types is likely to change in future releases of the
system; check the release notes for details on such changes.
Specifying \var{sfd} and \var{bfp} improves the quality of error messages,
guarantees \var{start} and \var{end} can be determined,
and eliminates the overhead of asking for a file position on each call
to \scheme{read-token}.
In most cases, \var{bfp} should be 0 for the first call
to \scheme{read-token} at the start of a file,
and it should be the fourth return value (\var{end}) of the preceding
call to \scheme{read-token} for each subsequent
call.
This protocol is necessary to handle files containing multiple-byte
characters, since file positions do not necessarily correspond
to character positions.
\schemedisplay
(define s (open-input-string "(a b c)"))
(read-token s) ;=> lparen
#f
0
1
(define s (open-input-string "abc 123"))
(read-token s) ;=> atomic
abc
0
3
(define s (open-input-string ""))
(read-token s) ;=> eof
#!eof
0
0
(define s (open-input-string "#7=#7#"))
(read-token s) ;=> mark
7
0
3
(read-token s) ;=> insert
7
3
6
\endschemedisplay
The information \scheme{read-token} returns is not always
sufficient for reconstituting the exact sequence of characters that
make up a token.
For example, \scheme{1.0} and \scheme{1e0} both return
\var{type} \scheme{atomic} with \var{value} \scheme{1.0}.
The exact sequence of characters may be obtained only by repositioning
the port and reading a block of characters of the appropriate length,
using the relative positions given by \var{start} and \var{end}.
\section{Output Operations\label{SECTIOOUTPUT}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{console-output-port}{\categoryglobalparameter}{console-output-port}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{console-output-port} is a parameter that determines the
output port used by the waiter and interactive debugger.
When called with no arguments, it returns the
console output port.
When called with one argument, which must be a textual output port,
it changes the value of the console
output port.
The initial value of this parameter is a port tied to the standard
output (stdout) stream of the Scheme process.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-output-port}{\categorythreadparameter}{current-output-port}
\listlibraries
\endentryheader
\noindent
\scheme{current-output-port} is a parameter that determines the
default port argument for most output procedures,
including \scheme{write-char}, \scheme{newline}, \scheme{write},
\scheme{display}, and \scheme{pretty-print}.
When called with no arguments, \scheme{current-output-port} returns the
current output port.
When called with one argument, which must be a textual output port,
it changes the value of the current
output port.
The Revised$^6$ Report version of \scheme{current-output-port} accepts
only zero arguments, i.e., it cannot be used to change the current output
port.
The initial value of this parameter is the same port as the initial
value of \scheme{console-output-port}.
%----------------------------------------------------------------------------
\entryheader
\formdef{console-error-port}{\categorythreadparameter}{console-error-port}
\listlibraries
\endentryheader
\scheme{console-error-port} is a parameter that can be used to set
or obtain the console error port, which determines the port to which
conditions and other messages are printed by the default exception
handler.
When called with no arguments, \scheme{console-error-port} returns the
console error port.
When called with one argument, which must be a textual output port,
it changes the value of the console
error port.
If the system determines that the standard output (stdout) and standard
error (stderr) streams refer to the same file, socket, pipe, virtual
terminal, device, etc., this parameter is initially set to the same value
as the parameter \scheme{console-output-port}.
Otherwise, this parameter is initially set to a different port tied to the
standard error (stderr) stream of the Scheme process.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-error-port}{\categorythreadparameter}{current-error-port}
\listlibraries
\endentryheader
\scheme{current-error-port} is a parameter that can be used to set
or obtain the current error port.
When called with no arguments, \scheme{current-error-port} returns the
current error port.
When called with one argument, which must be a textual output port,
it changes the value of the current error port.
The Revised$^6$ Report version of \scheme{current-error-port} accepts
only zero arguments, i.e., it cannot be used to change the current error
port.
The initial value of this parameter is the same port as the initial
value of \scheme{console-error-port}.
%----------------------------------------------------------------------------
\entryheader
\formdef{open-output-file}{\categoryprocedure}{(open-output-file \var{path})}
\formdef{open-output-file}{\categoryprocedure}{(open-output-file \var{path} \var{options})}
\returns a new output port
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{open-output-file} opens a textual output port for the file named by
\var{path}.
\var{options}, if present, is a symbolic option name or option list.
Possible symbolic option names are
\scheme{error}, \scheme{truncate}, \scheme{replace}, \scheme{append},
\scheme{compressed}, \scheme{uncompressed}, \scheme{buffered},
\scheme{unbuffered}, \scheme{exclusive}, and \scheme{nonexclusive}.
An option list is a list containing zero or more symbolic option names
and possibly the two-element
option \scheme{mode \var{mode}}.
The mutually exclusive \scheme{error}, \scheme{truncate},
\scheme{replace}, and \scheme{append} options are used to direct what happens when
the file to be opened already exists.
\begin{description}
\item[\scheme{error}:] An exception is raised with condition-type \scheme{&i/o-filename}.
\item[\scheme{replace}:] The existing file is deleted before the new file
is opened.
\item[\scheme{truncate}:] The existing file is opened
and truncated to zero length.
\item[\scheme{append}:]
The existing file is opened
and the output port is positioned at the end of the file before each write
so that output to the port is always appended to the file.
\end{description}
The default behavior is to raise an exception.
The mutually exclusive \scheme{compressed} and
\scheme{uncompressed} options determine whether the output file is to
be compressed.
The compression format and level are determined by the
\index{\scheme{compress-format}}\scheme{compress-format}
and
\index{\scheme{compress-level}}\scheme{compress-level}
parameters.
Files are uncompressed by default, so the \scheme{uncompressed}
option is useful only as documentation.
The mutually exclusive \scheme{buffered} and \scheme{unbuffered}
options determine whether output is buffered.
Unbuffered output is sent immediately to the file, whereas buffered
output not written until the port's output buffer is filled or the
port is flushed (via \scheme{flush-output-port}) or closed (via
\scheme{flush-output-port} or by the storage management system when
the port becomes inaccessible).
Output is buffered by default for efficiency, so the
\scheme{buffered} option is useful only as documentation.
The mutually exclusive \scheme{exclusive} and \scheme{nonexclusive}
options determine whether access to the file is ``exclusive.''
When the exclusive option is specified, the file is locked until
the port is closed to prevent access by other processes.
On some systems the lock is advisory, i.e., it inhibits access by
other processes only if they also attempt to open exclusively.
Nonexclusive access is the default, so the \scheme{nonexclusive} option
is useful only as documentation.
The \scheme{mode} option determines the permission bits
on Unix systems when the file is created by the operation, subject
to the process umask.
The subsequent element in the options list must be an exact integer
specifying the permissions in the manner of the Unix \scheme{open}
function.
The mode option is ignored under Windows.
For example, the call
\schemedisplay
(open-output-file "frob" '(compressed truncate mode #o644))
\endschemedisplay
opens the file frob with compression enabled.
If frob already exists it is truncated.
On Unix-based systems, if frob does not already exist, the permission
bits on the newly created file are set to logical and of \scheme{#o644} and the
process's umask.
The Revised$^6$ Report version of \scheme{open-output-file} does not
support the optional \var{options} argument.
%----------------------------------------------------------------------------
\entryheader
\formdef{call-with-output-file}{\categoryprocedure}{(call-with-output-file \var{path} \var{procedure})}
\formdef{call-with-output-file}{\categoryprocedure}{(call-with-output-file \var{path} \var{procedure} \var{options})}
\returns the values returned by \var{procedure}
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\var{procedure} should accept one argument.
\scheme{call-with-output-file} creates a new output port for the file named
by \var{path}, as if with \scheme{open-output-file}, and passes this port to \var{procedure}.
If \var{procedure} returns, \scheme{call-with-output-file} closes the output port
and returns the values returned by \var{procedure}.
\scheme{call-with-output-file} does not automatically close the output
port if a continuation created outside of \var{procedure} is invoked, since it
is possible that another continuation created inside of \var{procedure} will be
invoked at a later time, returning control to \var{procedure}.
If \var{procedure} does not return, an implementation is free to close the
output port only if it can prove that the output port is no longer accessible.
As shown in Section~\ref{TSPL:SECTCONTINUATIONS} of {\TSPLFOUR}, \scheme{dynamic-wind} may be used to
ensure that the port is closed if a continuation created outside of
\var{procedure} is invoked.
See \scheme{open-output-file} above for a description of the optional
\var{options} argument.
The Revised$^6$ Report version of \scheme{call-with-output-file} does not
support the optional \var{options} argument.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-output-to-file}{\categoryprocedure}{(with-output-to-file \var{path} \var{thunk})}
\formdef{with-output-to-file}{\categoryprocedure}{(with-output-to-file \var{path} \var{thunk} \var{options})}
\returns the value returned by \var{thunk}
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\var{thunk} must be a procedure and should accept zero arguments.
\scheme{with-output-to-file} temporarily rebinds the current output port to be the
result of opening the file named by \var{path}, as if with \scheme{open-output-file},
during the application of \var{thunk}.
If \var{thunk} returns, the port is closed and the current output port
is restored to its old value.
The behavior of \scheme{with-output-to-file} is unspecified if a
continuation created outside of \var{thunk} is invoked before
\var{thunk} returns.
An implementation may close the port and restore the current output
port to its old value---but it may not.
See \scheme{open-output-file} above for a description of the optional
\var{options} argument.
The Revised$^6$ Report version of \scheme{with-output-to-file} does not
support the optional \var{options} argument.
%----------------------------------------------------------------------------
\entryheader
\formdef{open-fd-output-port}{\categoryprocedure}{(open-fd-output-port \var{fd})}
\formdef{open-fd-output-port}{\categoryprocedure}{(open-fd-output-port \var{fd} \var{b-mode})}
\formdef{open-fd-output-port}{\categoryprocedure}{(open-fd-output-port \var{fd} \var{b-mode} \var{?transcoder})}
\returns a new output port for the file descriptor \var{fd}
\listlibraries
\endentryheader
\noindent
\var{fd} must be a nonnegative exact integer and should be a valid
open file descriptor.
If \var{?transcoder} is present and not \scheme{#f}, it must be a
transcoder, and this procedure returns a textual output port
whose transcoder is \var{?transcoder}.
Otherwise, this procedure returns a binary output port.
See the lead-in to Section~\ref{TSPL:SECTOPENINGFILES} of {\TSPLFOUR}
for a description of the constraints on and effects of the other
arguments.
The file descriptor is closed when the port is closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{standard-output-port}{\categoryprocedure}{(standard-output-port)}
\formdef{standard-output-port}{\categoryprocedure}{(standard-output-port \var{b-mode})}
\formdef{standard-output-port}{\categoryprocedure}{(standard-output-port \var{b-mode} \var{?transcoder})}
\returns a new output port connected to the process's standard output
\listlibraries
\endentryheader
\noindent
If \var{?transcoder} is present and not \scheme{#f}, it must be a
transcoder, and this procedure returns a textual output port
whose transcoder is \var{?transcoder}.
Otherwise, this procedure returns a binary output port.
The buffer mode \var{b-mode} defaults to \scheme{line}, which differs from
\scheme{block} in {\ChezScheme} only for textual output ports.
The Revised$^6$ Report version of this procedure does not accept the
optional \var{b-mode} and \var{?transcoder} arguments, which limits
it to an implementation-dependent buffering mode (\scheme{line} in
{\ChezScheme}) and binary output.
%----------------------------------------------------------------------------
\entryheader
\formdef{standard-error-port}{\categoryprocedure}{(standard-error-port)}
\formdef{standard-error-port}{\categoryprocedure}{(standard-error-port \var{b-mode})}
\formdef{standard-error-port}{\categoryprocedure}{(standard-error-port \var{b-mode} \var{?transcoder})}
\returns a new output port connected to the process's standard error
\listlibraries
\endentryheader
\noindent
If \var{?transcoder} is present and not \scheme{#f}, it must be a
transcoder, and this procedure returns a textual output port
whose transcoder is \var{?transcoder}.
Otherwise, this procedure returns a binary output port.
The buffer mode \var{b-mode} defaults to \scheme{none}.
See the lead-in to Section~\ref{TSPL:SECTOPENINGFILES} of {\TSPLFOUR}
for a description of the constraints on and effects of the other
arguments.
The Revised$^6$ Report version of this procedure does not accept the
optional \var{b-mode} and \var{?transcoder} arguments, which limits
it to an implementation-dependent buffering mode (\scheme{none} in
{\ChezScheme}) and binary output.
%----------------------------------------------------------------------------
\entryheader
\formdef{put-bytevector-some}{\categoryprocedure}{(put-bytevector-some \var{binary-output-port} \var{bytevector})}
\formdef{put-bytevector-some}{\categoryprocedure}{(put-bytevector-some \var{binary-output-port} \var{bytevector} \var{start})}
\formdef{put-bytevector-some}{\categoryprocedure}{(put-bytevector-some \var{binary-output-port} \var{bytevector} \var{start} \var{n})}
\returns the number of bytes written
\listlibraries
\endentryheader
\noindent
\var{start} and \var{n} must be nonnegative exact integers, and the sum of
\var{start} and \var{n} must not exceed the length of \var{bytevector}.
If not supplied, \var{start} defaults to zero and \var{n} defaults to
the difference between the length of \var{bytevector} and \var{start}.
This procedure normally writes the \var{n} bytes of \var{bytevector}
starting at \var{start} to the port and advances the its position past the
end of the bytes written.
If the port is in nonblocking mode (see \scheme{set-port-nonblocking!}),
however, the number of bytes written may be less than \var{n}, if
the system would have to block to write more bytes.
%----------------------------------------------------------------------------
\entryheader
\formdef{put-string-some}{\categoryprocedure}{(put-string-some \var{textual-output-port} \var{string})}
\formdef{put-string-some}{\categoryprocedure}{(put-string-some \var{textual-output-port} \var{string} \var{start})}
\formdef{put-string-some}{\categoryprocedure}{(put-string-some \var{textual-output-port} \var{string} \var{start} \var{n})}
\returns the number of characters written
\listlibraries
\endentryheader
\noindent
\var{start} and \var{n} must be nonnegative exact integers, and the sum of
\var{start} and \var{n} must not exceed the length of \var{string}.
If not supplied, \var{start} defaults to zero and \var{n} defaults to
the difference between the length of \var{string} and \var{start}.
This procedure normally writes the \var{n} characters of \var{string}
starting at \var{start} to the port and advances the its position past the
end of the characters written.
If the port is in nonblocking mode (see \scheme{set-port-nonblocking!}),
however, the number of characters written may be less than \var{n}, if
the system would have to block to write more characters.
%----------------------------------------------------------------------------
\entryheader
\formdef{display-string}{\categoryprocedure}{(display-string \var{string})}
\formdef{display-string}{\categoryprocedure}{(display-string \var{string} \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{display-string} writes the characters contained within
\var{string} to \var{textual-output-port} or to the current-output port
if \scheme{textual-output-port} is not specified.
The enclosing string quotes are not printed, and special characters
within the string are not escaped.
\scheme{display-string} is a more efficient alternative to
\scheme{display} for displaying strings.
%----------------------------------------------------------------------------
\entryheader
\formdef{block-write}{\categoryprocedure}{(block-write \var{textual-output-port} \var{string})}
\formdef{block-write}{\categoryprocedure}{(block-write \var{textual-output-port} \var{string} \var{count})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{count} must be a nonnegative fixnum less than or equal to the
length of \var{string}.
If not provided, it defaults to the length of \var{string}.
\scheme{block-write} writes the first \var{count} characters of \var{string}
to \var{textual-output-port}.
If the port is buffered and the buffer is nonempty, the
buffer is flushed before the contents of \var{string} are written.
In any case, the contents of \var{string} are written immediately,
without passing through the buffer.
%----------------------------------------------------------------------------
\entryheader
\formdef{truncate-port}{\categoryprocedure}{(truncate-port \var{output-port})}
\formdef{truncate-port}{\categoryprocedure}{(truncate-port \var{output-port} \var{pos})}
\formdef{truncate-file}{\categoryprocedure}{(truncate-file \var{output-port})}
\formdef{truncate-file}{\categoryprocedure}{(truncate-file \var{output-port} \var{pos})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{truncate-port} and \scheme{truncate-file} are identical.
\var{pos} must be an exact nonnegative integer. It defaults to 0.
These procedures truncate the file or other object associated with
\var{output-port} to \var{pos} and repositions the port
to that position, i.e., it combines the functionality of
\scheme{set-port-length!} and \scheme{set-port-position!} and
can be called on a port only if \scheme{port-has-set-port-length!?} and
\scheme{port-has-set-port-position!?} are both true of the port.
%----------------------------------------------------------------------------
\entryheader\label{desc:fresh-line}
\formdef{fresh-line}{\categoryprocedure}{(fresh-line)}
\formdef{fresh-line}{\categoryprocedure}{(fresh-line \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
If \var{textual-output-port} is not supplied, it defaults to the current output port.
This procedure behaves like \scheme{newline}, i.e., sends a newline
character to \var{textual-output-port}, unless it can determine that the port
is already positioned at the start of a line.
It does this by flushing the port and consulting the
``beginning-of-line'' (BOL) flag associated with the port.
(See page~\pageref{desc:set-port-bol}.)
\section{Input/Output Operations\label{SECTIOINPUTOUTPUT}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{open-input-output-file}{\categoryprocedure}{(open-input-output-file \var{path})}
\formdef{open-input-output-file}{\categoryprocedure}{(open-input-output-file \var{path} \var{options})}
\returns a new input-output port
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{open-input-output-file} opens a textual input-output port for the file named by
\var{path}.
The port may be used to read from or write to the named file.
The file is created if it does not already exist.
\var{options}, if present, is a symbolic option name or option list.
Possible symbolic option names are
\scheme{buffered},
\scheme{unbuffered}, \scheme{exclusive}, and \scheme{nonexclusive}.
An option list is a list containing zero or more symbolic option names
and possibly the two-element
option \scheme{mode \var{mode}}.
See the description of \scheme{open-output-file} for an explanation
of these options.
Input/output files are usually closed using \scheme{close-port}
but may also be closed with either
\scheme{close-input-port} or
\scheme{close-output-port}.
%----------------------------------------------------------------------------
\entryheader
\formdef{open-fd-input/output-port}{\categoryprocedure}{(open-fd-input/output-port \var{fd})}
\formdef{open-fd-input/output-port}{\categoryprocedure}{(open-fd-input/output-port \var{fd} \var{b-mode})}
\formdef{open-fd-input/output-port}{\categoryprocedure}{(open-fd-input/output-port \var{fd} \var{b-mode} \var{?transcoder})}
\returns a new input/output port for the file descriptor \var{fd}
\listlibraries
\endentryheader
\noindent
\var{fd} must be a nonnegative exact integer and should be a valid
open file descriptor.
If \var{?transcoder} is present and not \scheme{#f}, it must be a
transcoder, and this procedure returns a textual input/output port
whose transcoder is \var{?transcoder}.
Otherwise, this procedure returns a binary input/output port.
See the lead-in to Section~\ref{TSPL:SECTOPENINGFILES} of {\TSPLFOUR}
for a description of the constraints on and effects of the other
arguments.
The file descriptor is closed when the port is closed.
\section{Non-Unicode Bytevector/String Conversions\label{SECTMBCONVS}}
The procedures described in this section convert bytevectors containing
single- and multiple-byte sequences in non-Unicode character sets to and
from Scheme strings.
They are available only under Windows.
Under other operating systems, and when an \scheme{iconv} DLL is
available under Windows, \scheme{bytevector->string} and
\scheme{string->bytevector} can be used with a transcoder based
on a codec constructed via
\index{\scheme{iconv-codec}}\scheme{iconv-codec}
to achieve the same results, with more control over the handling
of invalid characters and line endings.
%----------------------------------------------------------------------------
\entryheader
\formdef{multibyte->string}{\categoryprocedure}{(multibyte->string \var{code-page} \var{bytevector})}
\returns a string containing the characters encoded in \var{bytevector}
\formdef{string->multibyte}{\categoryprocedure}{(string->multibyte \var{code-page} \var{string})}
\returns a bytevector containing the encodings of the characters in \var{string}
\listlibraries
\endentryheader
These procedures are available only under Windows.
The procedure \scheme{multibyte->string} is a wrapper for the Windows API
\scheme{MultiByteToWideChar} function,
and \scheme{string->multibyte} is a wrapper for the Windows API
\scheme{WideCharToMultiByte} function.
\var{code-page} declares the encoding of the byte sequences in the input
or output bytevectors.
It must be an exact nonnegative integer identifying a code page or one of
the symbols \scheme{cp-acp}, \scheme{cp-maccp}, \scheme{cp-oemcp},
\scheme{cp-symbol}, \scheme{cp-thread-acp}, \scheme{cp-utf7}, or
\scheme{cp-utf8}, which have the same meanings as the API function
meanings for the like-named constants.
\section{Pretty Printing\label{SECTPRETTY}}
The pretty printer is a version of the \scheme{write} procedure that
produces more human-readable output via introduced whitespace, i.e.,
line breaks and indentation.
The pretty printer is the default printer used by the read-eval-print
loop (waiter) to print the output(s) of each evaluated form.
The pretty printer may also be invoked explicitly by calling the
procedure \scheme{pretty-print}.
The pretty printer's operation can be controlled via the \scheme{pretty-format}
procedure described later in this section, which allows the
programmer to specify how specific forms are to be printed, various
pretty-printer controls, also described later in this section, and also
by the generic input/output controls described in Section~\ref{SECTMISCIOCONTROL}.
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-print}{\categoryprocedure}{(pretty-print \var{obj})}
\formdef{pretty-print}{\categoryprocedure}{(pretty-print \var{obj} \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
If \var{textual-output-port} is not supplied, it defaults to the current output port.
\scheme{pretty-print} is similar to \scheme{write} except that it uses
any number of spaces and newlines in order to print \var{obj} in a
style that is pleasing to look at and which shows the nesting level via
indentation.
For example,
\schemedisplay
(pretty-print '(define factorial (lambda (n) (let fact ((i n) (a 1))
(if (= i 0) a (fact (- i 1) (* a i)))))))
\endschemedisplay
\noindent
might produce
\schemedisplay
(define factorial
(lambda (n)
(let fact ([i n] [a 1])
(if (= i 0) a (fact (- i 1) (* a i))))))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-file}{\categoryprocedure}{(pretty-file \var{ifn} \var{ofn})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{ifn} and \var{ofn} must be strings.
\scheme{pretty-file} reads each object in turn from the file named by
\var{ifn} and pretty prints the object to the file named by \var{ofn}.
Comments present in the input are discarded by the reader and so do
not appear in the output file.
If the file named by \var{ofn} already exists, it is replaced.
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-format}{\categoryprocedure}{(pretty-format \var{sym})}
\returns see below
\formdef{pretty-format}{\categoryprocedure}{(pretty-format \var{sym} \var{fmt})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
By default, the pretty printer uses a generic algorithm for printing
each form.
This procedure is used to override this default and guide the
pretty-printers treatment of specific forms.
The symbol \var{sym} names a syntactic form or procedure.
With just one argument, \scheme{pretty-format} returns the current
format associated with \var{sym}, or \scheme{#f} if no format is
associated with \var{sym}.
In the two-argument case, the format \var{fmt} is associated with
\var{sym} for future invocations of the pretty printer.
\var{fmt} must be in the formatting language described below.
\begin{grammar}
\ang{fmt}\longis\scheme{(quote \var{symbol})}\\
\orbar\scheme{var}\\
\orbar\scheme{\var{symbol}}\\
\orbar\scheme{(read-macro \var{string} \var{symbol})}\\
\orbar\scheme{(meta)}\\
\orbar\scheme{(bracket . \var{fmt-tail})}\\
\orbar\scheme{(alt \var{fmt} \var{fmt}*)}\\
\orbar\scheme{\var{fmt-tail}}\\
\var{fmt-tail}\longis\scheme{()}\\
\orbar\scheme{(\var{tab} \var{fmt} ...)}\\
\orbar\scheme{(\var{fmt} \var{tab} ...)}\\
\orbar\scheme{(\var{tab} \var{fmt} . \var{fmt-tail})}\\
\orbar\scheme{(\var{fmt} ...)}\\
\orbar\scheme{(\var{fmt} . \var{fmt-tail})}\\
\orbar\scheme{(fill \var{tab} \var{fmt} ...)}\\
\var{tab}\longis\scheme{\var{int}}\\
\orbar\scheme{#f}
\end{grammar}
Some of the format forms are used for matching when there are multiple
alternatives, while others are used for matching and control indentation
or printing.
A description of each \var{fmt} is given below.
\begin{description}
\item[\scheme{(quote \var{symbol})}:]
This matches only the symbol \var{symbol}.
\item[\scheme{var}:]
This matches any symbol.
\item[\scheme{\var{symbol}}:]
This matches any input.
\item[\scheme{(read-macro \var{string} \var{symbol})}:]
This is used for read macros like \scheme{quote} and \var{syntax}.
It matches any input of the form (\var{symbol} \var{subform}).
For forms that match, the pretty printer prints
\var{string} immediately followed by \var{subform}.
\item[\scheme{(meta)}:]
This is a special case used for the \scheme{meta} keyword
(Section~\ref{SECTSYNTAXMETA}) which is used as a keyword prefix of
another form.
\item[\scheme{(alt \var{fmt} \var{fmt}*)}:]
This compares the input against the specified formats and uses the
one that is the closest match.
Most often, one of the formats will match exactly, but in other
cases, as when input is malformed or appears in abstract form in the
template of a syntactic abstraction, none of the formats will match
exactly.
\item[\scheme{(bracket . \var{fmt-tail})}:]
This matches any list-structured input and prints the input enclosed
in square brackets, i.e.,
\index{\scheme{[}}\scheme{[} and \index{\scheme{]}}\scheme{]},
rather than parentheses.
\item[\scheme{\var{fmt-tail}}:]
This matches any list-structured input.
\end{description}
Indentation of list-structured forms is determined via the
\var{fmt-tail} specifier used to the last two cases above.
A description of each \var{fmt-tail} is given below.
\begin{description}
\item[\scheme{()}:]
This matches an empty list tail.
\item[\scheme{(\var{tab} \var{fmt} ...)}:]
This matches the tail of any proper list; if the tail is nonempty
and the list does not fit entirely on the current line, a line break is
inserted before the first subform of the tail and \var{tab} (see
below) determines the amount by which this and all subsequent subforms
are indented.
\item[\scheme{(\var{fmt} \var{tab} ...)}:]
This matches the tail of any proper list; if the tail is nonempty
and the list does not fit entirely on the current line, a line break is
inserted after the first subform of the tail and \var{tab} (see
below) determines the amount by which all subsequent subforms are
indented.
\item[\scheme{(\var{tab} \var{fmt} . \var{fmt-tail})}:]
This matches a nonempty tail if the tail of the tail matches \var{fmt-tail}.
If the list does not fit entirely on the current line, a line break is
inserted before the first subform of the tail and \var{tab} (see
below) determines the amount by which the subform is indented.
\item[\scheme{(\var{fmt} ...)}:]
This matches the tail of any proper list and specified that no
line breaks are to be inserted before or after the current or
subsequent subforms.
\item[\scheme{(\var{fmt} . \var{fmt-tail})}:]
This matches a nonempty tail if the tail of the tail matches \var{fmt-tail}
and specifies that no line break is to be inserted before or after
the current subform.
\item[\scheme{(fill \var{tab} \var{fmt} ...)}:]
This matches the tail of any proper list and invokes a fill mode in
which the forms are packed with as many as will fit on each line.
\end{description}
A \var{tab} determines the amount by which a list subform is indented.
If \var{tab} is a nonnegative exact integer \var{int}, the subform
is indented \var{int} spaces in from the character position just after
the opening parenthesis or bracket of the parent form.
If \var{tab} is \scheme{#f}, the standard indentation is used.
The standard indentation can be determined or changed via the parameter
\scheme{pretty-standard-indent}, which is described later in this
section.
In cases where a format is given that doesn't quite match, the pretty
printer tries to use the given format as far as it can.
For example, if a format matches a list-structured form with a specific
number of subforms, but more or fewer subform are given, the pretty
printer will discard or replicate subform formats as necessary.
Here is an example showing the formatting of \var{let} might be specified.
\schemedisplay
(pretty-format 'let
'(alt (let ([bracket var x] 0 ...) #f e #f e ...)
(let var ([bracket var x] 0 ...) #f e #f e ...)))
\endschemedisplay
Since \scheme{let} comes in two forms, named and unnamed, two alternatives
are specified.
In either case, the \scheme{bracket} \var{fmt} is used to enclose the
bindings in square brackets, with all bindings after the first appearing
just below the first (and just after the enclosing opening parenthesis),
if they don't all fit on one line.
Each body form is indented by the standard indentation.
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-line-length}{\categorythreadparameter}{pretty-line-length}
\formdef{pretty-one-line-limit}{\categorythreadparameter}{pretty-one-line-limit}
\listlibraries
\endentryheader
\noindent
The value of each of these parameters must be a positive fixnum.
The parameters \scheme{pretty-line-length} and
\scheme{pretty-one-line-limit} control the output produced by
\index{\scheme{pretty-print}}\scheme{pretty-print}.
\scheme{pretty-line-length} determines after which character position (starting
from the first) on a line the pretty printer attempts to cut off output.
This is a soft limit only; if necessary, the pretty-printer will go beyond
\scheme{pretty-line-length}.
\scheme{pretty-one-line-limit} is similar to
\scheme{pretty-line-length}, except that it is relative to the first
nonblank position on each line of output.
It is also a soft limit.
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-initial-indent}{\categorythreadparameter}{pretty-initial-indent}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a nonnegative fixnum.
The parameter \scheme{pretty-initial-indent} is used to tell
\index{\scheme{pretty-print}}\scheme{pretty-print} where on an output
line it has been called.
If \scheme{pretty-initial-indent} is zero (the default), \scheme{pretty-print}
assumes that the first line of output it produces will start at the
beginning of the line.
If set to a nonzero value \var{n}, \scheme{pretty-print} assumes that the first
line will appear at character position \var{n} and will adjust its
printing of subsequent lines.
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-standard-indent}{\categorythreadparameter}{pretty-standard-indent}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a nonnegative fixnum.
This determines the amount by which
\scheme{pretty-print} indents subexpressions of most forms, such as \scheme{let}
expressions, from the form's keyword or first subexpression.
%----------------------------------------------------------------------------
\entryheader
\formdef{pretty-maximum-lines}{\categorythreadparameter}{pretty-maximum-lines}
\listlibraries
\endentryheader
\noindent
The parameter \scheme{pretty-maximum-lines} controls how many lines
\scheme{pretty-print} prints when it is called.
If set to \scheme{#f} (the default), no limit is imposed; if set to a
nonnegative fixnum \var{n}, at most \var{n} lines are printed.
\section{Formatted Output\label{SECTFORMAT}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{format}{\categoryprocedure}{(format \var{format-string} \var{obj} \dots)}
\formdef{format}{\categoryprocedure}{(format #f \var{format-string} \var{obj} \dots)}
\formdef{format}{\categoryprocedure}{(format #t \var{format-string} \var{obj} \dots)}
\formdef{format}{\categoryprocedure}{(format \var{textual-output-port} \var{format-string} \var{obj} \dots)}
\returns see below
\listlibraries
\endentryheader
\noindent
\index{formatted output}%
When the first argument to format is a string or \scheme{#f} (first and
second forms above),
\scheme{format} constructs an output string from \var{format-string} and the
objects \scheme{\var{obj} \dots}.
Characters are copied from \var{format-string} to the output string from
left to right, until \var{format-string} is exhausted.
The format string may contain one or more \var{format directives}, which are
multi-character sequences prefixed by a tilde (~\scheme{~}~).
Each directive is replaced by some other text, often involving one or more
of the \scheme{\var{obj} \dots} arguments, as determined by the semantics
of the directive.
When the first argument is \scheme{#t}, output is sent to the current output
port instead, as with \scheme{printf}.
When the first argument is a port, output is sent to that port, as with
\scheme{fprintf}.
\scheme{printf} and \scheme{fprintf} are described later in this section.
{\ChezScheme}'s implementation of \scheme{format} supports all of the
Common Lisp~\cite{Steele:common} format directives except for those specific
to the Common Lisp pretty printer.
Please consult a Common Lisp reference or the
\hyperlink{http://www.lispworks.com/documentation/HyperSpec/Front/index.htm}{Common Lisp Hyperspec},
for complete documentation.
A few of the most useful directives are described below.
% \begin{itemize}
% \item \scheme{~s}
% is replaced by the printed representation of the next \var{obj}, which
% may be any object, in machine-readable format, as with \scheme{write}.
% ``s'' is used for compatibility with \index{Common Lisp}Common Lisp, where it stands for
% ``s-expression,'' the Lisp term for object.
%
% \item \scheme{~a}
% is replaced by the printed representation of the next \var{obj}, which
% may be any object, in human-readable format, as with \scheme{display}.
% ``a'' is used for compatibility with \index{Common Lisp}Common Lisp, where it stands for
% ``ascii.''
%
% \item \scheme{~c}
% is replaced by the next \var{obj}, which must be a character, as with
% \scheme{write-char}.
%
% \item \scheme{~%}
% is replaced by a newline character, as with \scheme{newline}.
%
% \item \scheme{~~}
% is replaced by a single tilde.
% \end{itemize}
%
% An exception is raised with condition-type \scheme{&assertion}
% if more or fewer \var{objs} are given than required by
% a \var{format-string} that requires a fixed number of objects, or if the
% format string is malformed in any way.
Absent any format directives, \scheme{format} simply displays its string
argument.
\schemedisplay
(format "hi there") ;=> "hi there"
\endschemedisplay
The \scheme{~s} directive is replaced by the printed representation of
the next \var{obj}, which may be any object, in machine-readable format,
as with \scheme{write}.
\schemedisplay
(format "hi ~s" 'mom) ;=> "hi mom"
(format "hi ~s" "mom") ;=> "hi \"mom\""
(format "hi ~s~s" 'mom #\!) ;=> "hi mom#\\!"
\endschemedisplay
The general form of a \scheme{~s} directive is actually
\scheme{~\var{mincol},\var{colinc},\var{minpad},\var{padchar}s},
and the \scheme{s} can be preceded by an at sign (~\scheme{@}~)
modifier.
These additional parameters are used to control padding in the
output, with at least \var{minpad} copies of \var{padchar}
plus an integer multiple of \var{colinc} copies of \var{padchar}
to make the total width, including the written object,
\var{mincol} characters wide.
The padding is placed on the left if the \var{@} modifier is
present, otherwise on the right.
\var{mincol} and \var{minpad} default to 0, \var{colinc} defaults
to 1, and \var{padchar} defaults to space.
If specified, \var{padchar} is prefixed by a single quote mark.
\schemedisplay
(format "~10s" 'hello) ;=> "hello "
(format "~10@s" 'hello) ;=> " hello"
(format "~10,,,'*@s" 'hello) ;=> "*****hello"
\endschemedisplay
The \scheme{~a} directive is similar, but prints the object as with
\scheme{display}.
\schemedisplay
(format "hi ~s~s" "mom" #\!) ;=> "hi \"mom\"#\\!"
(format "hi ~a~a" "mom" #\!) ;=> "hi mom!"
\endschemedisplay
A tilde may be inserted into the output with \scheme{~~}, and a newline
may be inserted with \scheme{~%} (or embedded in the string with
\scheme{\n}).
\schemedisplay
(format "~~line one,~%line two.~~") ;=> "~line one,\nline two.~"
(format "~~line one,\nline two.~~") ;=> "~line one,\nline two.~"
\endschemedisplay
Real numbers may be printed in floating-point notation with \scheme{~f}.
\schemedisplay
(format "~f" 3.14159) ;=> 3.14159
\endschemedisplay
Exact numbers may printed as well as inexact numbers in this manner; they
are simply converted to inexact first as if with \scheme{exact->inexact}.
\schemedisplay
(format "~f" 1/3) ;=> "0.3333333333333333"
\endschemedisplay
The general form is actually \scheme{~\var{w},\var{d},\var{k},\var{overflowchar},\var{padchar}f}.
If specified, \var{w} determines the overall width of the output,
and \var{d} the number of digits to the right of the decimal point.
\var{padchar}, which defaults to space, is the pad character used
if padding is needed.
Padding is always inserted on the left.
The number is scaled by $10^k$ when printed; \var{k} defaults to zero.
The entire \var{w}-character field is filled with copies of
\var{overflowchar} if \var{overflowchar} is specified and the number
cannot be printed in \var{w} characters.
\var{k} defaults to 1
If an \scheme{@} modifier is present, a plus sign is printed before the
number for nonnegative inputs; otherwise, a sign is printed only if the
number is negative.
\schemedisplay
(format "~,3f" 3.14159) ;=> "3.142"
(format "~10f" 3.14159) ;=> " 3.14159"
(format "~10,,,'#f" 1e20) ;=> "##########"
\endschemedisplay
Real numbers may also be printed with \scheme{~e} for scientific
notation or with \scheme{~g}, which uses either floating-point or
scientific notation based on the size of the input.
\schemedisplay
(format "~e" 1e23) ;=> "1.0e+23"
(format "~g" 1e23) ;=> "1.0e+23"
\endschemedisplay
A real number may also be printed with \scheme{~$}, which uses
monetary notation defaulting to two digits to the right of the
decimal point.
\schemedisplay
(format "$~$" (* 39.95 1.06)) ;=> "$42.35"
(format "~$USD" 1/3) ;=> "0.33USD"
\endschemedisplay
Words can be pluralized automatically using \scheme{p}.
\schemedisplay
(format "~s bear~:p in ~s den~:p" 10 1) ;=> "10 bears in 1 den"
\endschemedisplay
Numbers may be printed out in words or roman numerals using variations
on \scheme{~r}.
\schemedisplay
(format "~r" 2599) ;=> "two thousand five hundred ninety-nine"
(format "~:r" 99) ;=> "ninety-ninth"
(format "~@r" 2599) ;=> "MMDXCIX"
\endschemedisplay
Case conversions can be performed by bracketing a portion of the
format string with the \scheme{~@(} and \scheme{~)} directives.
\schemedisplay
(format "~@(~r~)" 2599) ;=> "Two thousand five hundred ninety-nine"
(format "~@:(~a~)" "Ouch!") ;=> "OUCH!"
\endschemedisplay
\noindent
Some of the directives shown above have more options and parameters, and
there are other directives as well, including directives for conditionals,
iteration, indirection, and justification.
Again, please consult a Common Lisp reference for complete documentation.
An implementation of a greatly simplified version of \scheme{format}
appears in Section~\ref{TSPL:SECTEXPRINTF} of {\TSPLFOUR}.
%----------------------------------------------------------------------------
\entryheader
\formdef{printf}{\categoryprocedure}{(printf \var{format-string} \var{obj} \dots)}
\formdef{fprintf}{\categoryprocedure}{(fprintf \var{textual-output-port} \var{format-string} \var{obj} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
These procedures are simple wrappers for \scheme{format}.
\scheme{printf} prints the formatted output to the current output,
as with a first-argument of \scheme{#t} to \scheme{format}, and
\scheme{fprintf} prints the formatted output to the \var{textual-output-port},
as when the first argument to \scheme{format} is a port.
\section{Input/Output Control Operations\label{SECTMISCIOCONTROL}}
The I/O control operations described in this section are used to
control how the reader reads and printer writes, displays, or
pretty-prints
characters,
symbols,
gensyms,
numbers,
vectors,
long or deeply nested lists or vectors,
and graph-structured objects.
%----------------------------------------------------------------------------
\entryheader\label{desc:char-name}
\formdef{char-name}{\categoryprocedure}{(char-name \var{obj})}
\returns see below
\formdef{char-name}{\categoryprocedure}{(char-name \var{name} \var{char})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{char-name} is used to associate names (symbols) with characters
or to retrieve the most recently associated name or character for a
given character or name.
A name can map to only one character, but more than one name
can map to the same character.
The name most recently associated with a character determines
how that character prints, and each name associated with a character
may be used after the \scheme{#\} character prefix to name that
character on input.
Character associations created by \scheme{char-name} are ignored by the
printer unless the parameter \scheme{print-char-name} is set to a true
value.
The reader recognizes character names established by \scheme{char-name}
except after \scheme{#!r6rs}, which is implied within a library or
R6RS top-level program.
In the one-argument form, \var{obj} must be a symbol or character.
If it is a symbol and a character is associated with the
symbol, \scheme{char-name} returns that character.
If it is a symbol and no character is associated with the symbol,
\scheme{char-name} returns \scheme{#f}.
Similarly, if \var{obj} is a character, \scheme{char-name} returns the
most recently associated symbol for the character or \scheme{#f} if
no name is associated with the character.
For example, with the default set of character names:
\schemedisplay
(char-name #\space) ;=> space
(char-name 'space) ;=> #\space
(char-name 'nochar) ;=> #f
(char-name #\a) ;=> #f
\endschemedisplay
When passed two arguments, \var{name} is added to the set of names
associated with \var{char}, and any other association for \var{name}
is dropped.
\var{char} may be \scheme{#f}, in which case any other association
for \var{name} is dropped and no new association is formed.
In either case, any other names associated with \var{char} remain
associated with \var{char}.
The following interactive session demonstrates the use of
\scheme{char-name} to establish and remove associations between
characters and names, including the association of more than
one name with a character.
\schemedisplay
(print-char-name #t)
(char-name 'etx) ;=> #f
(char-name 'etx #\x3)
(char-name 'etx) ;=> #\etx
(char-name #\x3) ;=> etx
#\etx ;=> #\etx
(eq? #\etx #\x3) ;=> #t
#!r6rs #\etx ;=> \var{exception: invalid character name etx}
#!chezscheme #\etx ;=> #\etx
(char-name 'etx #\space)
(char-name #\x3) ;=> #f
(char-name 'etx) ;=> #\etx
#\space ;=> #\etx
(char-name 'etx #f)
#\etx ;=> \var{exception: invalid character name etx}
#\space ;=> #\space
\endschemedisplay
(When using the expression editor, it is necessary to type Control-J to
force the editor to read the erroneous \scheme{#\etx} input on the two
inputs above that result in read errors, since typing Enter
causes the expression editor to read the input only if the input is
well-formed.)
The reader does not recognize hex scalar value escapes in character names,
as it does in symbols, so \scheme{#\new\x6c;ine} is not equivalent
to \scheme{#\newline}.
In general, programmers should avoid the use of character name symbols
that cannot be entered without the use of hex scalar value escapes or
other symbol-name escape mechanisms, since such character names will
not be readable.
%----------------------------------------------------------------------------
\entryheader
\formdef{print-char-name}{\categorythreadparameter}{print-char-name}
\listlibraries
\endentryheader
\noindent
When \scheme{print-char-name} is set to \scheme{#f} (the default), associations
created by \scheme{char-name} are ignored by \scheme{write},
\scheme{put-datum}, \scheme{pretty-print}, and the \scheme{format}
``\scheme{~s}'' directive.
Otherwise, these procedures use the names established by
\scheme{char-name} when printing character objects.
\schemedisplay
(char-name 'etx #\x3)
(format "~s" #\x3) ;=> "#\\x3"
(parameterize ([print-char-name #t])
(format "~s" #\x3)) ;=> "#\\etx"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{case-sensitive}{\categorythreadparameter}{case-sensitive}
\listlibraries
\endentryheader
\noindent
The \scheme{case-sensitive} parameter determines whether the
reader is case-sensitive with respect to symbol and character names.
When set to true (the default, as required by the Revised$^6$ Report)
the case of alphabetic characters
within symbol names is significant.
When set to \scheme{#f}, case is insignificant.
More precisely, when set to \scheme{#f}, symbol and character names are
folded (as if by \scheme{string-foldcase}); otherwise, they are left
as they appear in the input.
The value of the \scheme{case-sensitive} matters only
if neither \scheme{#!fold-case} nor \scheme{#!no-fold-case} has appeared
previously in the same input stream.
That is, symbol and character name are folded if \scheme{#!fold-case} has
been seen.
They are not folded if \scheme{#!no-fold-case} has been seen.
If neither has been seen, they are folded if and only if
\scheme{(case-sensitive)} is \scheme{#f}.
\schemedisplay
(case-sensitive) ;=> #t
(eq? 'abc 'ABC) ;=> #f
'ABC ;=> ABC
(case-sensitive #f)
'ABC ;=> abc
(eq? 'abc 'ABC) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-graph}{\categorythreadparameter}{print-graph}
\listlibraries
\endentryheader
\noindent
When \scheme{print-graph} is set to a true value,
\scheme{write} and \scheme{pretty-print}
locate and print objects with shared structure, including
cycles, in a notation that may be read subsequently with \scheme{read}.
This notation employs the syntax
\index{#= (graph mark)@\scheme{#\var{n}=} (graph mark)}``\scheme{#\var{n}=\var{obj}},''
where \var{n}
is a nonnegative integer and \var{obj} is the printed representation
of an object, to label the first occurrence of \var{obj} in the output.
The syntax
\index{## (graph reference)@\scheme{#\var{n}#} (graph reference)}``\scheme{#\var{n}#}''
is used to refer to the object labeled by
\var{n} thereafter in the output.
\scheme{print-graph} is set to \scheme{#f} by default.
If graph printing is not enabled,
the settings of \scheme{print-length} and \scheme{print-level}
are insufficient to force finite output,
and \scheme{write} or \scheme{pretty-print} detects a cycle in an
object it is given to print,
a warning is issued (an exception with condition type \scheme{&warning} is
raised) and the object is printed as if
\scheme{print-graph} were enabled.
Since objects printed through the \scheme{~s} option in the format control
strings of \scheme{format}, \scheme{printf}, and \scheme{fprintf} are printed as with
\scheme{write}, the printing of such objects is also affected by \scheme{print-graph}.
\schemedisplay
(parameterize ([print-graph #t])
(let ([x (list 'a 'b)])
(format "~s" (list x x)))) ;=> "(#0=(a b) #0#)"
(parameterize ([print-graph #t])
(let ([x (list 'a 'b)])
(set-car! x x)
(set-cdr! x x)
(format "~s" x))) ;=> "#0=(#0# . #0#)"
\endschemedisplay
\noindent
The graph syntax is understood by the procedure
\index{\scheme{read}}\scheme{read}, allowing graph structures
to be printed and read consistently.
%----------------------------------------------------------------------------
\entryheader
\formdef{print-level}{\categorythreadparameter}{print-level}
\formdef{print-length}{\categorythreadparameter}{print-length}
\listlibraries
\endentryheader
\noindent
These parameters can be used to limit the extent to which nested
or multiple-element structures are printed.
When called without arguments, \scheme{print-level} returns the current
print level and \scheme{print-length} returns the current print length.
When called with one argument, which must be a nonnegative fixnum or
\scheme{#f}, \scheme{print-level} sets the current print level and
\scheme{print-length} sets the current print length to the argument.
When \scheme{print-level} is set to a nonnegative integer \var{n},
\scheme{write} and \scheme{pretty-print}
traverse only \var{n} levels deep into nested structures.
If a structure being printed exceeds \var{n} levels of nesting,
the substructure beyond that point is replaced in the output by an
\index{\scheme{...}~(ellipses)}\index{ellipses (~\scheme{...}~)}ellipsis
(~\scheme{...}~).
\scheme{print-level} is set to \scheme{#f} by default, which places
no limit on the number of levels printed.
When \scheme{print-length} is set to a nonnegative integer \var{n}, the
procedures \scheme{write} and \scheme{pretty-print}
print only \var{n} elements of a list or vector,
replacing the remainder of the list or vector with an
\index{\scheme{...}~(ellipses)}\index{ellipses (~\scheme{...}~)}ellipsis
(~\scheme{...}~).
\scheme{print-length} is set to \scheme{#f} by default, which places
no limit on the number of elements printed.
Since objects printed through the \scheme{~s} option in
the format control strings of \scheme{format}, \scheme{printf}, and \scheme{fprintf} are
printed as with \scheme{write},
the printing of such objects is also affected by \scheme{print-level}
and \scheme{print-length}.
The parameters \scheme{print-level} and \scheme{print-length} are useful for
controlling the volume of output in contexts where only a small portion
of the output is needed to identify the object being printed.
They are also useful in situations where circular structures may be
printed (see also \scheme{print-graph}).
\schemedisplay
(format "~s" '((((a) b) c) d e f g)) ;=> "((((a) b) c) d e f g)"
(parameterize ([print-level 2])
(format "~s" '((((a) b) c) d e f g))) ;=> "(((...) c) d e f g)"
(parameterize ([print-length 3])
(format "~s" '((((a) b) c) d e f g))) ;=> "((((a) b) c) d e ...)"
(parameterize ([print-level 2]
[print-length 3])
(format "~s" '((((a) b) c) d e f g))) ;=> "(((...) c) d e ...)"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-radix}{\categorythreadparameter}{print-radix}
\listlibraries
\endentryheader
\noindent
The \scheme{print-radix} parameter determines the radix in which
numbers are printed by \scheme{write}, \scheme{pretty-print}, and
\scheme{display}.
Its value should be an integer between 2 and 36, inclusive.
Its default value is 10.
When the value of \scheme{print-radix} is not 10, \scheme{write} and
\scheme{pretty-print} print a radix prefix before the number
(\scheme{#b} for radix 2, \scheme{#o} for radix 8, \scheme{#x} for
radix 16, and \scheme{#\var{n}r} for any other radix \var{n}).
Since objects printed through the \scheme{~s} and
\scheme{~a} options in the format control strings of
\scheme{format}, \scheme{printf}, and \scheme{fprintf} are printed as
with \scheme{write} and \scheme{display}, the printing of such objects
is also affected by \scheme{print-radix}.
\schemedisplay
(format "~s" 11242957) ;=> "11242957"
(parameterize ([print-radix 16])
(format "~s" 11242957)) ;=> "#xAB8DCD"
(parameterize ([print-radix 16])
(format "~a" 11242957)) ;=> "AB8DCD"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-gensym}{\categorythreadparameter}{print-gensym}
\listlibraries
\endentryheader
\noindent
\index{\scheme{gensym}}\index{\scheme{#:} (gensym prefix)}When
\scheme{print-gensym} is set to \scheme{#t} (the default)
gensyms are printed with an
extended symbol syntax that includes both the pretty name and the unique
name of the gensym:
\index{\scheme{#\schlbrace} (gensym prefix)}\scheme{#\schlbrace\var{pretty-name} \var{unique-name}\schrbrace}.
When set to \scheme{pretty}, the pretty name only is shown, with the
prefix \index{\scheme{#:} (gensym prefix)}\scheme{#:}.
When set to \scheme{pretty/suffix},
the printer prints the gensym's ``pretty'' name along with a
suffix based on the gensym's ``unique'' name, separated by a dot (~"."~).
If the gensym's unique name is generated automatically during the current
session, the suffix is that portion of the unique name that is not common
to all gensyms created during the current session.
Otherwise, the suffix is the entire unique name.
When set to \scheme{#f}, the pretty name only is shown, with no
prefix.
Since objects printed through the \scheme{~s} option in the
format control strings of \scheme{format}, \scheme{printf},
\scheme{errorf}, etc., are printed as with \scheme{write}, the printing of
such objects is also affected by \scheme{print-gensym}.
When printing an object that may contain more than one occurrence of a
gensym and \scheme{print-graph} is set to \scheme{pretty} or \scheme{#f},
it is useful to set \scheme{print-graph} to \scheme{#t} so that
multiple occurrences of the same gensym are marked as identical in
the output.
\schemedisplay
(let ([g (gensym)])
(format "~s" g)) ;=> "#{g0 bdids2xl6v49vgwe-a}"
(let ([g (gensym)])
(parameterize ([print-gensym 'pretty])
(format "~s" g))) ;=> "#:g1
(let ([g (gensym)])
(parameterize ([print-gensym #f])
(format "~s" g))) ;=> "g2"
(let ([g (gensym)])
(parameterize ([print-graph #t] [print-gensym 'pretty])
(format "~s" (list g g)))) ;=> "(#0=#:g3 #0#)"
(let ([g1 (gensym "x")]
[g2 (gensym "x")]
[g3 (gensym "y")])
(parameterize ([print-gensym 'pretty/suffix])
(format "~s ~s ~s" g1 g2 g3))) ;=> "x.1 x.2 y.3"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-brackets}{\categorythreadparameter}{print-brackets}
\listlibraries
\endentryheader
\noindent
When \scheme{print-brackets} is set to a true value, the pretty printer
(see \index{\scheme{pretty-print}}\scheme{pretty-print}) uses square
brackets rather than parentheses around certain subexpressions of
common control structures, e.g., around \scheme{let} bindings and
\scheme{cond} clauses.
\scheme{print-brackets} is set to \scheme{#t} by default.
\schemedisplay
(let ([p (open-output-string)])
(pretty-print '(let ([x 3]) x) p) ;=> "(let ([x 3]) x)
(get-output-string p)) ;== "
(parameterize ([print-brackets #f])
(let ([p (open-output-string)])
(pretty-print '(let ([x 3]) x) p) ;=> "(let ((x 3)) x)
(get-output-string p))) ;== "
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-extended-identifiers}{\categorythreadparameter}{print-extended-identifiers}
\listlibraries
\endentryheader
Chez Scheme extends the syntax of identifiers as described in
Section~\ref{SECTINTROSYNTAX}, except within a set of forms prefixed by
\scheme{#!r6rs} (which is implied by in a library or top-level program).
When this parameter is set to false (the default), identifiers in the
extended set are printed with hex scalar value escapes as necessary to
conform to the R6RS syntax for identifiers.
When this parameter is set to a true value, identifiers in the
extended set are printed without the escapes.
Identifiers whose names fall outside of both syntaxes are printed with
the escapes regardless of the setting of this parameter.
For example:
\schemedisplay
(parameterize ([print-extended-identifiers #f])
(printf "~s\n~s\n"
'(1+ --- { } .xyz)
(string->symbol "123")))
\endschemedisplay
prints
\schemedisplay
(\x31;+ \x2D;-- \x7B; \x7D; \x2E;xyz)
\x31;23
\endschemedisplay
while
\schemedisplay
(parameterize ([print-extended-identifiers #t])
(printf "~s\n~s\n"
'(1+ --- { } .xyz)
(string->symbol "123")))
\endschemedisplay
prints
\schemedisplay
(1+ --- { } .xyz)
\x31;23
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-vector-length}{\categorythreadparameter}{print-vector-length}
\listlibraries
\endentryheader
\noindent
When \scheme{print-vector-length} is set to a true value,
\scheme{write}, \scheme{put-datum}, and \scheme{pretty-print} includes the length
for all vectors between the ``\scheme{#}'' and open parenthesis,
all bytevectors between the ``\scheme{#vu8}'' and open parenthesis,
and all fxvectors between the ``\scheme{#vfx}'' and open parenthesis.
This parameter is set to \scheme{#f} by default.
\index{vector printing}When \scheme{print-vector-length} is set to a
true value, \scheme{write}, \scheme{put-datum}, and \scheme{pretty-print}
also suppress duplicated trailing elements in the vector to
reduce the amount of output.
This form is also recognized by the reader.
Since objects printed through the \scheme{~s} option in the
format control strings of \scheme{format}, \scheme{printf}, and
\scheme{fprintf} are printed as with \scheme{write}, the printing of
such objects is also affected by the setting of
\scheme{print-vector-length}.
\schemedisplay
(format "~s" (vector 'a 'b 'c 'c 'c)) ;=> "#(a b c c c)"
(parameterize ([print-vector-length #t])
(format "~s" (vector 'a 'b 'c 'c 'c))) ;=> "#5(a b c)"
(parameterize ([print-vector-length #t])
(format "~s" (bytevector 1 2 3 4 4 4))) ;=> "#6vu8(1 2 3 4)"
(parameterize ([print-vector-length #t])
(format "~s" (fxvector 1 2 3 4 4 4))) ;=> "#6vfx(1 2 3 4)"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{print-precision}{\categorythreadparameter}{print-precision}
\listlibraries
\endentryheader
\noindent
When \scheme{print-precision} is set to \scheme{#f} (the default),
\scheme{write}, \scheme{put-datum}, \scheme{pretty-print}, and the
\scheme{format} ``\scheme{~s}'' directive do not include the
vertical-bar ``mantissa-width'' syntax after each floating-point
number.
When set to a nonnegative exact integer, the mantissa width is
included, as per the precision argument to
\scheme{number->string}.
%----------------------------------------------------------------------------
\entryheader
\formdef{print-unicode}{\categorythreadparameter}{print-unicode}
\listlibraries
\endentryheader
\noindent
When \scheme{print-unicode} is set to \scheme{#f},
\scheme{write}, \scheme{put-datum}, \scheme{pretty-print}, and the
\scheme{format} ``\scheme{~s}'' directive display Unicode characters
with encodings $80_{16}$ (128) and above that appear
within character objects, symbols, and strings
using hexadecimal character escapes.
When set to a true value (the default), they are displayed like
other printing characters, as if by \scheme{put-char}.
\schemedisplay
(format "~s" #\x3bb) ;=> "#\\\raw{$\lambda$}"
(parameterize ([print-unicode #f])
(format "~s" #\x3bb)) ;=> "#\\x3BB"
\endschemedisplay
\section{Fasl Output\label{SECTFASL}}
\index{fast loading format}%
The procedures \scheme{write} and \scheme{pretty-print} print objects in a
human readable format.
For objects with external datum representations, the output produced by
\scheme{write} and \scheme{pretty-print} is also machine-readable with
\scheme{read}.
Objects with external datum representations include pairs, symbols,
vectors, strings, numbers, characters, booleans, and records but not
procedures and ports.
An alternative \emph{fast loading}, or \emph{fasl}, format may be used for
objects with external datum representations.
The fasl format is not human readable, but it is machine readable and both
more compact and more quickly processed by \scheme{read}.
This format is always used for compiled code generated by
\scheme{compile-file}, but it may also be used for data that needs to be
written and read quickly, such as small databases encoded with Scheme data
structures.
Objects are printed in fasl format with \scheme{fasl-write}.
Because the fasl format is a binary format, fasl output must be written
to a binary port.
For this reason, it is not possible to include data written in fasl
format with textual data in the same file, as was the case in
earlier versions of {\ChezScheme}.
Similarly, the (textual) reader does not handle objects written in
fasl format; the procedure \scheme{fasl-read}, which requires a binary
input port, must be used instead.
%----------------------------------------------------------------------------
\entryheader
\formdef{fasl-write}{\categoryprocedure}{(fasl-write \var{obj} \var{binary-output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{fasl-write} writes the fasl representation of \var{obj} to
\var{binary-output-port}.
An exception is raised with condition-type \scheme{&assertion} if
\var{obj} or any portion of \var{obj} has no external fasl representation,
e.g., if \var{obj} is or contains a procedure.
The fasl representation of \var{obj} is compressed if the parameter
\scheme{fasl-compressed}, described below, is set to \scheme{#t},
its default value.
For this reason, \var{binary-output-port} generally should not be opened
with the compressed option.
A warning is issued (an exception with condition type \scheme{&warning}
is raised) on the first attempt to write fasl objects to or read
fasl objects from a compressed file.
\schemedisplay
(define bop (open-file-output-port "tmp.fsl"))
(fasl-write '(a b c) bop)
(close-port bop)
(define bip (open-file-input-port "tmp.fsl"))
(fasl-read bip) ;=> (a b c)
(fasl-read bip) ;=> #!eof
(close-port bip)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fasl-read}{\categoryprocedure}{(fasl-read \var{binary-input-port})}
\formdef{fasl-read}{\categoryprocedure}{(fasl-read \var{binary-input-port \var{situation}})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
If present, \var{situation} must be one of the symbols \scheme{load},
\scheme{visit}, or \scheme{revisit}.
It defaults to \scheme{load}.
\scheme{fasl-read} reads one object from
\var{binary-input-port}, which must be positioned at the
front of an object written in fasl format.
\scheme{fasl-read} returns the eof object if the file is positioned
at the end of file.
If the situation is \scheme{visit}, \scheme{fasl-read} skips over
any revisit (run-time-only) objects, and
if the situation is \scheme{revisit}, \scheme{fasl-read} skips over
any visit (compile-time-only) objects.
It doesn't skip any if the situation is \scheme{load}.
Similarly, objects marked as both visit and revisit (e.g., object code
corresponding to source code within an \scheme{eval-when} form with
situation \scheme{load} or situations \scheme{visit} and \scheme{revisit})
are never skipped.
\scheme{fasl-read} automatically decompresses the representation
of each fasl object written in compressed format by \scheme{fasl-write}.
Thus, \var{binary-input-port} generally should not be opened with
the compressed option.
A warning is issued (an exception with condition type \scheme{&warning}
is raised) on the first attempt to write fasl objects to or read
fasl objects from a compressed file.
\schemedisplay
(define bop (open-file-output-port "tmp.fsl"))
(fasl-write '(a b c) bop)
(close-port bop)
(define bip (open-file-input-port "tmp.fsl"))
(fasl-read bip) ;=> (a b c)
(fasl-read bip) ;=> #!eof
(close-port bip)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fasl-compressed}{\categorythreadparameter}{fasl-compressed}
\listlibraries
\endentryheader
\noindent
When this parameter is set to its default value, \scheme{#t},
\scheme{fasl-write} compresses the representation of each object
as it writes it, often resulting in substantially smaller output
but possibly taking more time to write and read.
The compression format and level are determined by the
\index{\scheme{compress-format}}\scheme{compress-format}
and
\index{\scheme{compress-level}}\scheme{compress-level}
parameters.
%----------------------------------------------------------------------------
\entryheader
\formdef{fasl-file}{\categoryprocedure}{(fasl-file \var{ifn} \var{ofn})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{ifn} and \var{ofn} must be strings.
\scheme{fasl-file} may be used to convert a file in human-readable
format into an equivalent
file written in fasl format.
\scheme{fasl-file} reads each object in turn from the file named by
\var{ifn} and writes the fasl format for the object onto the file
named by \var{ofn}.
If the file named by \var{ofn} already exists, it is replaced.
\section{File System Interface\label{SECTIOFILESYSTEM}}
This section describes operations on files, directories, and pathnames.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-directory}{\categoryglobalparameter}{current-directory}
\formdef{cd}{\categoryglobalparameter}{cd}
\listlibraries
\endentryheader
\noindent
When invoked without arguments, \scheme{current-directory} returns a string
representing the current working directory.
Otherwise, the current working directory is changed to the directory
specified by the argument, which must be a string representing a valid
directory pathname.
\scheme{cd} is bound to the same parameter.
%----------------------------------------------------------------------------
\entryheader
\formdef{directory-list}{\categoryprocedure}{(directory-list \var{path})}
\returns a list of file names
\listlibraries
\endentryheader
\var{path} must be a string.
The return value is a list of strings representing the names of
files found in the directory named by \var{path}.
\scheme{directory-list} raises an exception with condition
type \scheme{&i/o-filename} if \var{path} does not name a directory
or if the process cannot list the directory.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-exists?}{\categoryprocedure}{(file-exists? \var{path})}
\formdef{file-exists?}{\categoryprocedure}{(file-exists? \var{path} \var{follow?})}
\returns \scheme{#t} if the file named by \var{path} exists, \scheme{#f} otherwise
\listlibraries
\endentryheader
\var{path} must be a string.
If the optional \var{follow?} argument is true (the default),
\scheme{file-exists?} follows symbolic links; otherwise it does not.
Thus, \scheme{file-exists?} will return \scheme{#f} when handed the
pathname of a broken symbolic link unless \var{follow?} is provided
and is \scheme{#f}.
The Revised$^6$ Report \scheme{file-exists?} does not accept the
optional \var{follow?} argument.
Whether it follows symbolic links is unspecified.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-regular?}{\categoryprocedure}{(file-regular? \var{path})}
\formdef{file-regular?}{\categoryprocedure}{(file-regular? \var{path} \var{follow?})}
\returns \scheme{#t} if the file named by \var{path} is a regular file, \scheme{#f} otherwise
\listlibraries
\endentryheader
\var{path} must be a string.
If the optional \var{follow?} argument is true (the default),
\scheme{file-regular?} follows symbolic links; otherwise it does not.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-directory?}{\categoryprocedure}{(file-directory? \var{path})}
\formdef{file-directory?}{\categoryprocedure}{(file-directory? \var{path} \var{follow?})}
\returns \scheme{#t} if the file named by \var{path} is a directory, \scheme{#f} otherwise
\listlibraries
\endentryheader
\var{path} must be a string.
If the optional \var{follow?} argument is true (the default),
this procedure follows symbolic links; otherwise it does not.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-symbolic-link?}{\categoryprocedure}{(file-symbolic-link? \var{path})}
\returns \scheme{#t} if the file named by \var{path} is a symbolic link, \scheme{#f} otherwise
\listlibraries
\endentryheader
\var{path} must be a string.
\scheme{file-symbolic-link?} never follows symbolic links in making its
determination.
%----------------------------------------------------------------------------
\entryheader
\formdef{file-access-time}{\categoryprocedure}{(file-access-time \var{path/port})}
\formdef{file-access-time}{\categoryprocedure}{(file-access-time \var{path/port} \var{follow?})}
\returns the access time of the specified file
\formdef{file-change-time}{\categoryprocedure}{(file-change-time \var{path/port})}
\formdef{file-change-time}{\categoryprocedure}{(file-change-time \var{path/port} \var{follow?})}
\returns the change time of the specified file
\formdef{file-modification-time}{\categoryprocedure}{(file-modification-time \var{path/port})}
\formdef{file-modification-time}{\categoryprocedure}{(file-modification-time \var{path/port} \var{follow?})}
\returns the modification time of the specified file
\listlibraries
\endentryheader
\var{path/port} must be a string or port.
If \var{path/port} is a string, the time returned is for the file named
by the string, and the optional \var{follow?} argument determines whether
symbolic links are followed.
If \var{follow?} is true (the default),
this procedure follows symbolic links; otherwise it does not.
If \var{path/port} is a port, it must be a file port, and the time returned
is for the associated file.
In this case, \var{follow?} is ignored.
The returned times are represented as time objects
(Section~\ref{SECTSYSTEMTIMESNDATES}).
%----------------------------------------------------------------------------
\entryheader
\formdef{mkdir}{\categoryprocedure}{(mkdir \var{path})}
\formdef{mkdir}{\categoryprocedure}{(mkdir \var{path} \var{mode})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\var{mode} must be a fixnum.
\scheme{mkdir} creates a directory with the name given by \var{path}.
All \var{path} path components leading up to the last must already
exist.
If the optional \var{mode} argument is present, it overrides the default
permissions for the new directory.
Under Windows, the \var{mode} argument is ignored.
\scheme{mkdir} raises an exception with condition
type \scheme{&i/o-filename} if the directory cannot be created.
%----------------------------------------------------------------------------
\entryheader
\formdef{delete-file}{\categoryprocedure}{(delete-file \var{path})}
\formdef{delete-file}{\categoryprocedure}{(delete-file \var{path} \var{error?})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{delete-file} removes the file named by \var{path}.
If the optional \var{error?} argument is \scheme{#f} (the default),
\scheme{delete-file} returns a boolean value: \scheme{#t} if the
operation is successful and \scheme{#f} if it is not.
Otherwise, \scheme{delete-file} returns an unspecified value if the
operation is successful and raises an exception with condition
type \scheme{&i/o-filename} if it is not.
The Revised$^6$ Report \scheme{delete-file} does not accept the
optional \var{error?} argument but behaves as if \var{error?}
is true.
%----------------------------------------------------------------------------
\entryheader
\formdef{delete-directory}{\categoryprocedure}{(delete-directory \var{path})}
\formdef{delete-directory}{\categoryprocedure}{(delete-directory \var{path} \var{error?})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{delete-directory} removes the directory named by \var{path}.
If the optional \var{error?} argument is \scheme{#f} (the default),
\scheme{delete-directory} returns a boolean value: \scheme{#t} if the
operation is successful and \scheme{#f} if it is not.
Otherwise, \scheme{delete-directory} returns an unspecified value if the
operation is successful and raises an exception with condition
type \scheme{&i/o-filename} if it is not.
The behavior is unspecified if the directory is not empty, but on
most systems the operations will not succeed.
%----------------------------------------------------------------------------
\entryheader
\formdef{rename-file}{\categoryprocedure}{(rename-file \var{old-pathname} \var{new-pathname})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{old-pathname} and \var{new-pathname} must be strings.
\scheme{rename-file} changes the name of the file named by \var{old-pathname}
to \var{new-pathname}.
If the file does not exist or cannot be renamed,
an exception is raised with condition type \scheme{&i/o-filename}.
%----------------------------------------------------------------------------
\entryheader
\formdef{chmod}{\categoryprocedure}{(chmod \var{path} \var{mode})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\var{mode} must be a fixnum.
\scheme{chmod} sets the permissions on the file named by
\var{path} to \var{mode}.
Bits 0, 1, and~2 of \var{mode} are the execute, write, and read permission bits
for users other than the file's owner who are not in the file's group.
Bits 3-5 are the execute, write, and read permission bits for users other
than the file's owner but in the file's group.
Bits 6-8 are the execute, write, and read permission bits
for the file's owner.
Bits 7-9 are the Unix sticky, set-group-id, and set-user-id bits.
Under Windows, all but the user ``write'' bit are ignored.
If the file does not exist or the permissions cannot be changed,
an exception is raised with condition type \scheme{&i/o-filename}.
%----------------------------------------------------------------------------
\entryheader
\formdef{get-mode}{\categoryprocedure}{(get-mode \var{path})}
\formdef{get-mode}{\categoryprocedure}{(get-mode \var{path} \var{follow?})}
\returns the current permissions mode for \var{path}
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{get-mode} retrieves the permissions on the file named by
\var{path} and returns them as a fixnum in the same form as the \var{mode}
argument to \scheme{chmod}.
If the optional \var{follow?} argument is true (the default),
this procedure follows symbolic links; otherwise it does not.
%----------------------------------------------------------------------------
\entryheader
\formdef{directory-separator?}{\categoryprocedure}{(directory-separator? \var{char})}
\returns \scheme{#t} if \var{char} is a directory separator, \scheme{#f} otherwise
\listlibraries
\endentryheader
The character \scheme{#\/} is a directory separator on all
current machine types, and \scheme{#\\} is a directory separator
under Windows.
%----------------------------------------------------------------------------
\entryheader
\formdef{directory-separator}{\categoryprocedure}{(directory-separator)}
\returns the preferred directory separator
\listlibraries
\endentryheader
The preferred directory separator is \scheme{#\\} for Windows and
\scheme{#\/} for other systems.
%----------------------------------------------------------------------------
\entryheader
\formdef{path-first}{\categoryprocedure}{(path-first \var{path})}
\formdef{path-rest}{\categoryprocedure}{(path-rest \var{path})}
\formdef{path-last}{\categoryprocedure}{(path-last \var{path})}
\formdef{path-parent}{\categoryprocedure}{(path-parent \var{path})}
\formdef{path-extension}{\categoryprocedure}{(path-extension \var{path})}
\formdef{path-root}{\categoryprocedure}{(path-root \var{path})}
\returns the specified component of \var{path}
\formdef{path-absolute?}{\categoryprocedure}{(path-absolute? \var{path})}
\returns \scheme{#t} if \var{path} is absolute, otherwise \scheme{#f}
\listlibraries
\endentryheader
\var{path} must be a string.
The return value is also a (possibly empty) string.
The path first component is the first directory in the path, or the
empty string if the path consists only of a single filename.
The path rest component is the portion of the path that does not
include the path first component or the directory separator (if
any) that separates it from the rest of the path.
The path last component is the last (filename) portion of \var{path}.
The path parent component is the portion of \var{path} that does not
include the path last component, if any, or the directory separator that
separates it from the rest of the path.
If the first component of the path names a root directory (including drives
and shares under Windows), home directory
(e.g., \scheme{~/abc} or \scheme{~user/abc}),
the current directory (\scheme{.}), or the parent directory
(\scheme{..}), \scheme{path-first} returns that component.
For paths that consist only of such a directory,
both \scheme{path-first} and \scheme{path-parent} act as
identity procedures, while \scheme{path-rest} and \scheme{path-last}
return the empty string.
The path extension component is the portion of \var{path} that follows
the last dot (period) in the last component of a path name.
The path root component is the portion of \var{path} that does not
include the extension, if any, or the dot that precedes it.
If the first component names a root directory (including drives
and shares under Windows) or home directory,
\scheme{path-absolute?} returns \scheme{#t}.
Otherwise, \scheme{path-absolute?} returns \scheme{#f}.
The tables below identify the components for several example paths,
with underscores representing empty strings.
\begin{tabular}{llllllll}
path & abs & first & rest & parent & last & root & ext \\
\scheme{a} & \scheme{#f} & \scheme{_} & \scheme{a} & \scheme{_} & \scheme{a} & \scheme{a} & \scheme{_} \\
\scheme{a/} & \scheme{#f} & \scheme{a} & \scheme{_} & \scheme{a} & \scheme{_} & \scheme{a/} & \scheme{_} \\
\scheme{a/b} & \scheme{#f} & \scheme{a} & \scheme{b} & \scheme{a} & \scheme{b} & \scheme{a/b} & \scheme{_} \\
\scheme{a/b.c} & \scheme{#f} & \scheme{a} & \scheme{b.c} & \scheme{a} & \scheme{b.c} & \scheme{a/b} & \scheme{c} \\
\scheme{/} & \scheme{#t} & \scheme{/} & \scheme{_} & \scheme{/} & \scheme{_} & \scheme{/} & \scheme{_} \\
\scheme{/a/b.c} & \scheme{#t} & \scheme{/} & \scheme{a/b.c} & \scheme{/a} & \scheme{b.c} & \scheme{/a/b} & \scheme{c} \\
\scheme{~/a/b.c} & \scheme{#t} & \scheme{~} & \scheme{a/b.c} & \scheme{~/a} & \scheme{b.c} & \scheme{~/a/b} & \scheme{c} \\
\scheme{~u/a/b.c} & \scheme{#t} & \scheme{~u} & \scheme{a/b.c} & \scheme{~u/a} & \scheme{b.c} & \scheme{~u/a/b} & \scheme{c} \\
\scheme{../..} & \scheme{#f} & \scheme{..} & \scheme{..} & \scheme{..} & \scheme{..} & \scheme{../..} & \scheme{_} \\
\end{tabular}
The second table shows the components when Windows drives and shares
are involved.
\begin{tabular}{llllllll}
path & abs & first & rest & parent & last & root & ext \\
\scheme{c:} & \scheme{#f} & \scheme{c:} & \scheme{_} & \scheme{c:} & \scheme{_} & \scheme{c:} & \scheme{_} \\
\scheme{c:/} & \scheme{#t} & \scheme{c:/} & \scheme{_} & \scheme{c:/} & \scheme{_} & \scheme{c:/} & \scheme{_} \\
\scheme{c:a/b} & \scheme{#f} & \scheme{c:} & \scheme{a/b} & \scheme{c:a} & \scheme{b} & \scheme{c:a/b} & \scheme{_} \\
\scheme{//s/a/b.c} & \scheme{#t} & \scheme{//s} & \scheme{a/b.c} & \scheme{//s/a} & \scheme{b.c} & \scheme{//s/a/b} & \scheme{c} \\
\scheme{//s.com} & \scheme{#t} & \scheme{//s.com} & \scheme{_} & \scheme{//s.com} & \scheme{_} & \scheme{//s.com} & \scheme{_} \\
\end{tabular}
The following procedure can be used to reproduce the tables above.
\schemedisplay
(define print-table
(lambda path*
(define print-row
(lambda (abs? path first rest parent last root extension)
(printf "~a~11t~a~17t~a~28t~a~39t~a~50t~a~61t~a~73t~a\n"
abs? path first rest parent last root extension)))
(print-row "path" "abs" "first" "rest" "parent" "last" "root" "ext")
(for-each
(lambda (path)
(define uscore (lambda (s) (if (eqv? s "") "_" s)))
(apply print-row path
(map (lambda (s) (if (eqv? s "") "_" s))
(list (path-absolute? path) (path-first path)
(path-rest path) (path-parent path) (path-last path)
(path-root path) (path-extension path)))))
path*)))
\endschemedisplay
For example, the first table can be produced with:
\schemedisplay
(print-table "a" "a/" "a/b" "a/b.c" "/" "/a/b.c" "~/a/b.c"
"~u/a/b.c" "../..")
\endschemedisplay
while the second can be produced (under Windows) with:
\schemedisplay
(print-table "c:" "c:/" "c:a/b" "//s/a/b.c" "//s.com")
\endschemedisplay
\section{Generic Port Examples\label{SECTPORTEXAMPLES}}
This section presents the definitions for three types of generic ports:
two-way ports, transcript ports, and process ports.
\parheader{Two-way ports}
\index{two-way ports}The first example defines \scheme{make-two-way-port}, which constructs a
textual input/output port from a given pair of textual input and output ports.
For example:
\schemedisplay
(define ip (open-input-string "this is the input"))
(define op (open-output-string))
(define p (make-two-way-port ip op))
\endschemedisplay
\noindent
The port returned by \scheme{make-two-way-port} is both an input and an output
port, and it is also a textual port:
\schemedisplay
(port? p) ;=> #t
(input-port? p) ;=> #t
(output-port? p) ;=> #t
(textual-port? p) ;=> #t
\endschemedisplay
\noindent
Items read from a two-way port come from the constituent input port,
and items written to a two-way port go to the constituent output
port:
\schemedisplay
(read p) ;=> this
(write 'hello p)
(get-output-string op) ;=> hello
\endschemedisplay
\noindent
The definition of \scheme{make-two-way-port} is straightforward.
To keep the example simple,
no local buffering is performed,
although it would be more efficient to do so.
\schemedisplay
(define make-two-way-port
(lambda (ip op)
(define handler
(lambda (msg . args)
(record-case (cons msg args)
[block-read (p s n) (block-read ip s n)]
[block-write (p s n) (block-write op s n)]
[char-ready? (p) (char-ready? ip)]
[clear-input-port (p) (clear-input-port ip)]
[clear-output-port (p) (clear-output-port op)]
[close-port (p) (mark-port-closed! p)]
[flush-output-port (p) (flush-output-port op)]
[file-position (p . pos) (apply file-position ip pos)]
[file-length (p) (file-length ip)]
[peek-char (p) (peek-char ip)]
[port-name (p) "two-way"]
[read-char (p) (read-char ip)]
[unread-char (c p) (unread-char c ip)]
[write-char (c p) (write-char c op)]
[else (assertion-violationf 'two-way-port
"operation ~s not handled"
msg)])))
(make-input/output-port handler "" "")))
\endschemedisplay
\noindent
Most of the messages are passed directly to one of the constituent
ports.
Exceptions are \scheme{close-port}, which is handled directly by
marking the port closed, \scheme{port-name}, which is also handled directly.
\scheme{file-position} and \scheme{file-length} are rather arbitrarily
passed off to the input port.
\parheader{Transcript ports}
\index{transcript ports}The next example defines \scheme{make-transcript-port}, which constructs a
textual input/output port from three ports: a textual input port \var{ip} and two textual
output ports, \var{op} and \var{tp}.
Input read from a transcript port comes from \var{ip}, and output
written to a transcript port goes to \var{op}.
In this manner, transcript ports are similar to two-way ports.
Unlike two-way ports, input from \var{ip} and output to \var{op} is
also written to \var{tp}, so that \var{tp} reflects both input from
\var{ip} and output to \var{op}.
Transcript ports may be used to define the Scheme procedures
\scheme{transcript-on} and \scheme{transcript-off}, or the {\ChezScheme} procedure
\scheme{transcript-cafe}.
For example, here is a definition of \scheme{transcript-cafe}:
\schemedisplay
(define transcript-cafe
(lambda (pathname)
(let ([tp (open-output-file pathname 'replace)])
(let ([p (make-transcript-port
(console-input-port)
(console-output-port)
tp)])
; set both console and current ports so that
; the waiter and read/write will be in sync
(parameterize ([console-input-port p]
[console-output-port p]
[current-input-port p]
[current-output-port p])
(let-values ([vals (new-cafe)])
(close-port p)
(close-port tp)
(apply values vals)))))))
\endschemedisplay
The implementation of transcript ports is significantly more complex
than the implementation of two-way ports defined above, primarily
because it buffers input and output locally.
Local buffering is needed to allow the transcript file to reflect
accurately the actual input and output performed in the presence of
\scheme{unread-char}, \scheme{clear-output-port}, and \scheme{clear-input-port}.
Here is the code:
% exercise: Try rewriting make-transcript-port without local buffering.
% Why does this not work satisfactorily?
\schemedisplay
(define make-transcript-port
(lambda (ip op tp)
(define (handler msg . args)
(record-case (cons msg args)
[block-read (p str cnt)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(if (< i s)
(let ([cnt (fxmin cnt (fx- s i))])
(do ([i i (fx+ i 1)]
[j 0 (fx+ j 1)])
((fx= j cnt)
(set-port-input-index! p i)
cnt)
(string-set! str j (string-ref b i))))
(let ([cnt (block-read ip str cnt)])
(unless (eof-object? cnt)
(block-write tp str cnt))
cnt))))]
[char-ready? (p)
(or (< (port-input-index p) (port-input-size p))
(char-ready? ip))]
[clear-input-port (p)
; set size to zero rather than index to size
; in order to invalidate unread-char
(set-port-input-size! p 0)]
[clear-output-port (p)
(set-port-output-index! p 0)]
[close-port (p)
(with-interrupts-disabled
(flush-output-port p)
(set-port-output-size! p 0)
(set-port-input-size! p 0)
(mark-port-closed! p))]
[file-position (p . pos)
(if (null? pos)
(most-negative-fixnum)
(assertion-violationf 'transcript-port "cannot reposition"))]
[flush-output-port (p)
(with-interrupts-disabled
(let ([b (port-output-buffer p)]
[i (port-output-index p)])
(unless (fx= i 0)
(block-write op b i)
(block-write tp b i)
(set-port-output-index! p 0)
(set-port-bol! p
(char=? (string-ref b (fx- i 1)) #\newline))))
(flush-output-port op)
(flush-output-port tp))]
[peek-char (p)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(if (fx< i s)
(string-ref b i)
(begin
(flush-output-port p)
(let ([s (block-read ip b)])
(if (eof-object? s)
s
(begin
(block-write tp b s)
(set-port-input-size! p s)
(string-ref b 0))))))))]
[port-name (p) "transcript"]
[constituent-ports (p) (values ip op tp)]
[read-char (p)
(with-interrupts-disabled
(let ([c (peek-char p)])
(unless (eof-object? c)
(set-port-input-index! p
(fx+ (port-input-index p) 1)))
c))]
[unread-char (c p)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(when (fx= i 0)
(assertion-violationf 'unread-char
"tried to unread too far on ~s"
p))
(set-port-input-index! p (fx- i 1))
; following could be skipped; it's supposed
; to be the same character anyway
(string-set! b (fx- i 1) c)))]
[write-char (c p)
(with-interrupts-disabled
(let ([b (port-output-buffer p)]
[i (port-output-index p)]
[s (port-output-size p)])
(string-set! b i c)
; could check here to be sure that we really
; need to flush; we may end up here even if
; the buffer isn't full
(block-write op b (fx+ i 1))
(block-write tp b (fx+ i 1))
(set-port-output-index! p 0)
(set-port-bol! p (char=? c #\newline))))]
[block-write (p str cnt)
(with-interrupts-disabled
; flush buffered data
(let ([b (port-output-buffer p)]
[i (port-output-index p)])
(unless (fx= i 0)
(block-write op b i)
(block-write tp b i)
(set-port-output-index! p 0)
(set-port-bol! p (char=? (string-ref b (fx- i 1)) #\newline))))
; write new data
(unless (fx= cnt 0)
(block-write op str cnt)
(block-write tp str cnt)
(set-port-bol! p
(char=? (string-ref str (fx- cnt 1)) #\newline))))]
[else (assertion-violationf 'transcript-port
"operation ~s not handled"
msg)]))
(let ([ib (make-string 1024)] [ob (make-string 1024)])
(let ([p (make-input/output-port handler ib ob)])
(set-port-input-size! p 0)
(set-port-output-size! p (fx- (string-length ob) 1))
p))))
\endschemedisplay
\noindent
The chosen length of both the input and output ports is the same; this
is not necessary.
They could have different lengths, or one could be buffered locally and
the other not buffered locally.
Local buffering could be disabled effectively by providing zero-length
buffers.
After we create the port, the input size is set to zero since there
is not yet any data to be read.
The port output size is set to one less than the length of the buffer.
This is done so that \scheme{write-char} always has one character position
left over into which to write its character argument.
Although this is not necessary, it does simplify the code somewhat
while allowing the buffer to be flushed as soon as the last character
is available.
Block reads and writes are performed on the constituent ports for
efficiency and (in the case of writes) to ensure that the operations
are performed immediately.
The call to \scheme{flush-output-port} in the handling of \scheme{read-char} insures
that all output written to \scheme{op} appears before input is read from
\scheme{ip}.
Since \scheme{block-read} is typically used to support higher-level operations
that are performing their own buffering, or for direct input and output
in support of I/O-intensive applications, the flush call has been
omitted from that part of the handler.
Critical sections are used whenever the handler manipulates one of the
buffers, to protect against untimely interrupts that could lead to
reentry into the handler.
The critical sections are unnecessary if no such reentry is possible,
i.e., if only one ``thread'' of the computation can have access to the
port.
\parheader{Process ports}
\index{process ports}\index{sockets}The final example
demonstrates how to incorporate the socket interface
defined in Section~\ref{SECTFOREIGNSOCKETS} into a generic port that
allows transparent communication with subprocesses via normal Scheme
input/output operations.
A process port is created with \scheme{open-process}, which accepts a
shell command as a string.
\scheme{open-process} sets up a socket, forks a child process, sets up
two-way communication via the socket, and invokes the command in a
subprocess.
The sample session below demonstrates the use of \scheme{open-process},
running and communicating with another Scheme process started with the
``\scheme{-q}'' switch to suppress the greeting and prompts.
\schemedisplay
> (define p (open-process "exec scheme -q"))
> (define s (make-string 1000 #\nul))
> (pretty-print '(+ 3 4) p)
> (read p)
7
> (pretty-print '(define (f x) (if (= x 0) 1 (* x (f (- x 1))))) p)
> (pretty-print '(f 10) p)
> (read p)
3628800
> (pretty-print '(exit) p)
> (read p)
#!eof
> (close-port p)
\endschemedisplay
\noindent
Since process ports, like transcript ports, are two-way, the implementation
is somewhat similar.
The main difference is that a transcript port reads from and writes to its
subordinate ports, whereas a process port reads from and writes to a socket.
When a process port is opened, the socket is created and subprocess
invoked, and when the port is closed, the socket is closed and the
subprocess is terminated.
\schemedisplay
(define open-process
(lambda (command)
(define handler
(lambda (pid socket)
(define (flush-output who p)
(let ([i (port-output-index p)])
(when (fx> i 0)
(check who (c-write socket (port-output-buffer p) i))
(set-port-output-index! p 0))))
(lambda (msg . args)
(record-case (cons msg args)
[block-read (p str cnt)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(if (< i s)
(let ([cnt (fxmin cnt (fx- s i))])
(do ([i i (fx+ i 1)]
[j 0 (fx+ j 1)])
((fx= j cnt)
(set-port-input-index! p i)
cnt)
(string-set! str j (string-ref b i))))
(begin
(flush-output 'block-read p)
(let ([n (check 'block-read
(c-read socket str cnt))])
(if (fx= n 0)
#!eof
n))))))]
[char-ready? (p)
(or (< (port-input-index p) (port-input-size p))
(bytes-ready? socket))]
[clear-input-port (p)
; set size to zero rather than index to size
; in order to invalidate unread-char
(set-port-input-size! p 0)]
[clear-output-port (p) (set-port-output-index! p 0)]
[close-port (p)
(with-interrupts-disabled
(flush-output 'close-port p)
(set-port-output-size! p 0)
(set-port-input-size! p 0)
(mark-port-closed! p)
(terminate-process pid))]
[file-length (p) 0]
[file-position (p . pos)
(if (null? pos)
(most-negative-fixnum)
(assertion-violationf 'process-port "cannot reposition"))]
[flush-output-port (p)
(with-interrupts-disabled
(flush-output 'flush-output-port p))]
[peek-char (p)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(if (fx< i s)
(string-ref b i)
(begin
(flush-output 'peek-char p)
(let ([s (check 'peek-char
(c-read socket b (string-length b)))])
(if (fx= s 0)
#!eof
(begin (set-port-input-size! p s)
(string-ref b 0))))))))]
[port-name (p) "process"]
[read-char (p)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(if (fx< i s)
(begin
(set-port-input-index! p (fx+ i 1))
(string-ref b i))
(begin
(flush-output 'peek-char p)
(let ([s (check 'read-char
(c-read socket b (string-length b)))])
(if (fx= s 0)
#!eof
(begin (set-port-input-size! p s)
(set-port-input-index! p 1)
(string-ref b 0))))))))]
[unread-char (c p)
(with-interrupts-disabled
(let ([b (port-input-buffer p)]
[i (port-input-index p)]
[s (port-input-size p)])
(when (fx= i 0)
(assertion-violationf 'unread-char
"tried to unread too far on ~s"
p))
(set-port-input-index! p (fx- i 1))
; following could be skipped; supposed to be
; same character
(string-set! b (fx- i 1) c)))]
[write-char (c p)
(with-interrupts-disabled
(let ([b (port-output-buffer p)]
[i (port-output-index p)]
[s (port-output-size p)])
(string-set! b i c)
(check 'write-char (c-write socket b (fx+ i 1)))
(set-port-output-index! p 0)))]
[block-write (p str cnt)
(with-interrupts-disabled
; flush buffered data
(flush-output 'block-write p)
; write new data
(check 'block-write (c-write socket str cnt)))]
[else
(assertion-violationf 'process-port
"operation ~s not handled"
msg)]))))
(let* ([server-socket-name (tmpnam 0)]
[server-socket (setup-server-socket server-socket-name)])
(dofork
(lambda () ; child
(check 'close (close server-socket))
(let ([sock (setup-client-socket server-socket-name)])
(dodup 0 sock)
(dodup 1 sock))
(check 'execl (execl4 "/bin/sh" "/bin/sh" "-c" command))
(assertion-violationf 'open-process "subprocess exec failed"))
(lambda (pid) ; parent
(let ([sock (accept-socket server-socket)])
(check 'close (close server-socket))
(let ([ib (make-string 1024)] [ob (make-string 1024)])
(let ([p (make-input/output-port
(handler pid sock)
ib ob)])
(set-port-input-size! p 0)
(set-port-output-size! p (fx- (string-length ob) 1))
p))))))))
\endschemedisplay
|