1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339
|
/*
* utils.c - miscellaneous utilities
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zsh.mdh"
#include "utils.pro"
/* name of script being sourced */
/**/
mod_export char *scriptname; /* is sometimes a function name */
/* filename of script or other file containing code source e.g. autoload */
/**/
mod_export char *scriptfilename;
/* != 0 if we are in a new style completion function */
/**/
mod_export int incompfunc;
#ifdef MULTIBYTE_SUPPORT
struct widechar_array {
wchar_t *chars;
size_t len;
};
typedef struct widechar_array *Widechar_array;
/*
* The wordchars variable turned into a wide character array.
* This is much more convenient for testing.
*/
static struct widechar_array wordchars_wide;
/*
* The same for the separators (IFS) array.
*/
static struct widechar_array ifs_wide;
/* Function to set one of the above from the multibyte array */
static void
set_widearray(char *mb_array, Widechar_array wca)
{
if (wca->chars) {
free(wca->chars);
wca->chars = NULL;
}
wca->len = 0;
if (!isset(MULTIBYTE))
return;
if (mb_array) {
VARARR(wchar_t, tmpwcs, strlen(mb_array));
wchar_t *wcptr = tmpwcs;
wint_t wci;
mb_charinit();
while (*mb_array) {
int mblen;
if (STOUC(*mb_array) <= 0x7f) {
mb_array++;
*wcptr++ = (wchar_t)*mb_array;
continue;
}
mblen = mb_metacharlenconv(mb_array, &wci);
if (!mblen)
break;
/* No good unless all characters are convertible */
if (wci == WEOF)
return;
*wcptr++ = (wchar_t)wci;
#ifdef DEBUG
/*
* This generates a warning from the compiler (and is
* indeed useless) if chars are unsigned. It's
* extreme paranoia anyway.
*/
if (wcptr[-1] < 0)
fprintf(stderr, "BUG: Bad cast to wchar_t\n");
#endif
mb_array += mblen;
}
wca->len = wcptr - tmpwcs;
wca->chars = (wchar_t *)zalloc(wca->len * sizeof(wchar_t));
wmemcpy(wca->chars, tmpwcs, wca->len);
}
}
#endif
/* Print an error
The following functions use the following printf-like format codes
(implemented by zerrmsg()):
Code Argument types Prints
%s const char * C string (null terminated)
%l const char *, int C string of given length (null not required)
%L long decimal value
%d int decimal value
%% (none) literal '%'
%c int character at that codepoint
%e int strerror() message (argument is typically 'errno')
*/
static void
zwarning(const char *cmd, const char *fmt, va_list ap)
{
if (isatty(2))
zleentry(ZLE_CMD_TRASH);
char *prefix = scriptname ? scriptname : (argzero ? argzero : "");
if (cmd) {
if (unset(SHINSTDIN) || locallevel) {
nicezputs(prefix, stderr);
fputc((unsigned char)':', stderr);
}
nicezputs(cmd, stderr);
fputc((unsigned char)':', stderr);
} else {
/*
* scriptname is set when sourcing scripts, so that we get the
* correct name instead of the generic name of whatever
* program/script is running. It's also set in shell functions,
* so test locallevel, too.
*/
nicezputs((isset(SHINSTDIN) && !locallevel) ? "zsh" : prefix, stderr);
fputc((unsigned char)':', stderr);
}
zerrmsg(stderr, fmt, ap);
}
/**/
mod_export void
zerr(VA_ALIST1(const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs) {
if (noerrs < 2)
errflag |= ERRFLAG_ERROR;
return;
}
errflag |= ERRFLAG_ERROR;
VA_START(ap, fmt);
VA_GET_ARG(ap, fmt, const char *);
zwarning(NULL, fmt, ap);
va_end(ap);
}
/**/
mod_export void
zerrnam(VA_ALIST2(const char *cmd, const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *cmd);
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs)
return;
errflag |= ERRFLAG_ERROR;
VA_START(ap, fmt);
VA_GET_ARG(ap, cmd, const char *);
VA_GET_ARG(ap, fmt, const char *);
zwarning(cmd, fmt, ap);
va_end(ap);
}
/**/
mod_export void
zwarn(VA_ALIST1(const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs)
return;
VA_START(ap, fmt);
VA_GET_ARG(ap, fmt, const char *);
zwarning(NULL, fmt, ap);
va_end(ap);
}
/**/
mod_export void
zwarnnam(VA_ALIST2(const char *cmd, const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *cmd);
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs)
return;
VA_START(ap, fmt);
VA_GET_ARG(ap, cmd, const char *);
VA_GET_ARG(ap, fmt, const char *);
zwarning(cmd, fmt, ap);
va_end(ap);
}
#ifdef DEBUG
/**/
mod_export void
dputs(VA_ALIST1(const char *message))
VA_DCL
{
char *filename;
FILE *file;
va_list ap;
VA_DEF_ARG(const char *message);
VA_START(ap, message);
VA_GET_ARG(ap, message, const char *);
if ((filename = getsparam_u("ZSH_DEBUG_LOG")) != NULL &&
(file = fopen(filename, "a")) != NULL) {
zerrmsg(file, message, ap);
fclose(file);
} else
zerrmsg(stderr, message, ap);
va_end(ap);
}
#endif /* DEBUG */
#ifdef __CYGWIN__
/*
* This works around an occasional problem with dllwrap on Cygwin, seen
* on at least two installations. It fails to find the last symbol
* exported in alphabetical order (in our case zwarnnam). Until this is
* properly categorised and fixed we add a dummy symbol at the end.
*/
mod_export void
zz_plural_z_alpha(void)
{
}
#endif
/**/
void
zerrmsg(FILE *file, const char *fmt, va_list ap)
{
const char *str;
int num;
#ifdef DEBUG
long lnum;
#endif
#ifdef HAVE_STRERROR_R
#define ERRBUFSIZE (80)
int olderrno;
char errbuf[ERRBUFSIZE];
#endif
char *errmsg;
if ((unset(SHINSTDIN) || locallevel) && lineno) {
#if defined(ZLONG_IS_LONG_LONG) && defined(PRINTF_HAS_LLD)
fprintf(file, "%lld: ", lineno);
#else
fprintf(file, "%ld: ", (long)lineno);
#endif
} else
fputc((unsigned char)' ', file);
while (*fmt)
if (*fmt == '%') {
fmt++;
switch (*fmt++) {
case 's':
str = va_arg(ap, const char *);
nicezputs(str, file);
break;
case 'l': {
char *s;
str = va_arg(ap, const char *);
num = va_arg(ap, int);
num = metalen(str, num);
s = zhalloc(num + 1);
memcpy(s, str, num);
s[num] = '\0';
nicezputs(s, file);
break;
}
#ifdef DEBUG
case 'L':
lnum = va_arg(ap, long);
fprintf(file, "%ld", lnum);
break;
#endif
case 'd':
num = va_arg(ap, int);
fprintf(file, "%d", num);
break;
case '%':
putc('%', file);
break;
case 'c':
num = va_arg(ap, int);
#ifdef MULTIBYTE_SUPPORT
mb_charinit();
zputs(wcs_nicechar(num, NULL, NULL), file);
#else
zputs(nicechar(num), file);
#endif
break;
case 'e':
/* print the corresponding message for this errno */
num = va_arg(ap, int);
if (num == EINTR) {
fputs("interrupt\n", file);
errflag |= ERRFLAG_ERROR;
return;
}
errmsg = strerror(num);
/* If the message is not about I/O problems, it looks better *
* if we uncapitalize the first letter of the message */
if (num == EIO)
fputs(errmsg, file);
else {
fputc(tulower(errmsg[0]), file);
fputs(errmsg + 1, file);
}
break;
/* When adding format codes, update the comment above zwarning(). */
}
} else {
putc(*fmt == Meta ? *++fmt ^ 32 : *fmt, file);
fmt++;
}
putc('\n', file);
fflush(file);
}
/* Output a single character, for the termcap routines. *
* This is used instead of putchar since it can be a macro. */
/**/
mod_export int
putraw(int c)
{
putc(c, stdout);
return 0;
}
/* Output a single character, for the termcap routines. */
/**/
mod_export int
putshout(int c)
{
putc(c, shout);
return 0;
}
#ifdef MULTIBYTE_SUPPORT
/*
* Turn a character into a visible representation thereof. The visible
* string is put together in a static buffer, and this function returns
* a pointer to it. Printable characters stand for themselves, DEL is
* represented as "^?", newline and tab are represented as "\n" and
* "\t", and normal control characters are represented in "^C" form.
* Characters with bit 7 set, if unprintable, are represented as "\M-"
* followed by the visible representation of the character with bit 7
* stripped off. Tokens are interpreted, rather than being treated as
* literal characters.
*
* Note that the returned string is metafied, so that it must be
* treated like any other zsh internal string (and not, for example,
* output directly).
*
* This function is used even if MULTIBYTE_SUPPORT is defined: we
* use it as a fallback in case we couldn't identify a wide character
* in a multibyte string.
*/
/**/
mod_export char *
nicechar_sel(int c, int quotable)
{
static char buf[10];
char *s = buf;
c &= 0xff;
if (ZISPRINT(c))
goto done;
if (c & 0x80) {
if (isset(PRINTEIGHTBIT))
goto done;
*s++ = '\\';
*s++ = 'M';
*s++ = '-';
c &= 0x7f;
if(ZISPRINT(c))
goto done;
}
if (c == 0x7f) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c = '?';
} else if (c == '\n') {
*s++ = '\\';
c = 'n';
} else if (c == '\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c += 0x40;
}
done:
/*
* The resulting string is still metafied, so check if
* we are returning a character in the range that needs metafication.
* This can't happen if the character is printed "nicely", so
* this results in a maximum of two bytes total (plus the null).
*/
if (imeta(c)) {
*s++ = Meta;
*s++ = c ^ 32;
} else
*s++ = c;
*s = 0;
return buf;
}
/**/
mod_export char *
nicechar(int c)
{
return nicechar_sel(c, 0);
}
#else /* MULTIBYTE_SUPPORT */
/**/
mod_export char *
nicechar(int c)
{
static char buf[10];
char *s = buf;
c &= 0xff;
if (ZISPRINT(c))
goto done;
if (c & 0x80) {
if (isset(PRINTEIGHTBIT))
goto done;
*s++ = '\\';
*s++ = 'M';
*s++ = '-';
c &= 0x7f;
if(ZISPRINT(c))
goto done;
}
if (c == 0x7f) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
c = '?';
} else if (c == '\n') {
*s++ = '\\';
c = 'n';
} else if (c == '\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
c += 0x40;
}
done:
/*
* The resulting string is still metafied, so check if
* we are returning a character in the range that needs metafication.
* This can't happen if the character is printed "nicely", so
* this results in a maximum of two bytes total (plus the null).
*/
if (imeta(c)) {
*s++ = Meta;
*s++ = c ^ 32;
} else
*s++ = c;
*s = 0;
return buf;
}
#endif /* MULTIBYTE_SUPPORT */
/*
* Return 1 if nicechar() would reformat this character.
*/
/**/
mod_export int
is_nicechar(int c)
{
c &= 0xff;
if (ZISPRINT(c))
return 0;
if (c & 0x80)
return !isset(PRINTEIGHTBIT);
return (c == 0x7f || c == '\n' || c == '\t' || c < 0x20);
}
/**/
#ifdef MULTIBYTE_SUPPORT
static mbstate_t mb_shiftstate;
/*
* Initialise multibyte state: called before a sequence of
* wcs_nicechar(), mb_metacharlenconv(), or
* mb_charlenconv().
*/
/**/
mod_export void
mb_charinit(void)
{
memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
}
/*
* The number of bytes we need to allocate for a "nice" representation
* of a multibyte character.
*
* We double MB_CUR_MAX to take account of the fact that
* we may need to metafy. In fact the representation probably
* doesn't allow every character to be in the meta range, but
* we don't need to be too pedantic.
*
* The 12 is for the output of a UCS-4 code; we don't actually
* need this at the same time as MB_CUR_MAX, but again it's
* not worth calculating more exactly.
*/
#define NICECHAR_MAX (12 + 2*MB_CUR_MAX)
/*
* Input a wide character. Output a printable representation,
* which is a metafied multibyte string. With widthp return
* the printing width.
*
* swide, if non-NULL, is used to help the completion code, which needs
* to know the printing width of the each part of the representation.
* *swide is set to the part of the returned string where the wide
* character starts. Any string up to that point is ASCII characters,
* so the width of it is (*swide - <return_value>). Anything left is
* a single wide character corresponding to the remaining width.
* Either the initial ASCII part or the wide character part may be empty
* (but not both). (Note the complication that the wide character
* part may contain metafied characters.)
*
* The caller needs to call mb_charinit() before the first call, to
* set up the multibyte shift state for a range of characters.
*/
/**/
mod_export char *
wcs_nicechar_sel(wchar_t c, size_t *widthp, char **swidep, int quotable)
{
static char *buf;
static int bufalloc = 0, newalloc;
char *s, *mbptr;
int ret = 0;
VARARR(char, mbstr, MB_CUR_MAX);
/*
* We want buf to persist beyond the return. MB_CUR_MAX and hence
* NICECHAR_MAX may not be constant, so we have to allocate this at
* run time. (We could probably get away with just allocating a
* large buffer, in practice.) For efficiency, only reallocate if
* we really need to, since this function will be called frequently.
*/
newalloc = NICECHAR_MAX;
if (bufalloc != newalloc)
{
bufalloc = newalloc;
buf = (char *)zrealloc(buf, bufalloc);
}
s = buf;
if (!iswprint(c) && (c < 0x80 || !isset(PRINTEIGHTBIT))) {
if (c == 0x7f) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c = '?';
} else if (c == L'\n') {
*s++ = '\\';
c = 'n';
} else if (c == L'\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c += 0x40;
} else if (c >= 0x80) {
ret = -1;
}
}
if (ret != -1)
ret = wcrtomb(mbstr, c, &mb_shiftstate);
if (ret == -1) {
memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
/*
* Can't or don't want to convert character: use UCS-2 or
* UCS-4 code in print escape format.
*
* This comparison fails and generates a compiler warning
* if wchar_t is 16 bits, but the code is still correct.
*/
if (c >= 0x10000) {
sprintf(buf, "\\U%.8x", (unsigned int)c);
if (widthp)
*widthp = 10;
} else if (c >= 0x100) {
sprintf(buf, "\\u%.4x", (unsigned int)c);
if (widthp)
*widthp = 6;
} else {
strcpy(buf, nicechar((int)c));
/*
* There may be metafied characters from nicechar(),
* so compute width and end position independently.
*/
if (widthp)
*widthp = ztrlen(buf);
if (swidep)
*swidep = buf + strlen(buf);
return buf;
}
if (swidep)
*swidep = widthp ? buf + *widthp : buf;
return buf;
}
if (widthp) {
int wcw = WCWIDTH(c);
*widthp = (s - buf);
if (wcw >= 0)
*widthp += wcw;
else
(*widthp)++;
}
if (swidep)
*swidep = s;
for (mbptr = mbstr; ret; s++, mbptr++, ret--) {
DPUTS(s >= buf + NICECHAR_MAX,
"BUG: buffer too small in wcs_nicechar");
if (imeta(*mbptr)) {
*s++ = Meta;
DPUTS(s >= buf + NICECHAR_MAX,
"BUG: buffer too small for metafied char in wcs_nicechar");
*s = *mbptr ^ 32;
} else {
*s = *mbptr;
}
}
*s = 0;
return buf;
}
/**/
mod_export char *
wcs_nicechar(wchar_t c, size_t *widthp, char **swidep)
{
return wcs_nicechar_sel(c, widthp, swidep, 0);
}
/*
* Return 1 if wcs_nicechar() would reformat this character for display.
*/
/**/
mod_export int is_wcs_nicechar(wchar_t c)
{
if (!iswprint(c) && (c < 0x80 || !isset(PRINTEIGHTBIT))) {
if (c == 0x7f || c == L'\n' || c == L'\t' || c < 0x20)
return 1;
if (c >= 0x80) {
return (c >= 0x100);
}
}
return 0;
}
/**/
mod_export int
zwcwidth(wint_t wc)
{
int wcw;
/* assume a single-byte character if not valid */
if (wc == WEOF || unset(MULTIBYTE))
return 1;
wcw = WCWIDTH(wc);
/* if not printable, assume width 1 */
if (wcw < 0)
return 1;
return wcw;
}
/**/
#endif /* MULTIBYTE_SUPPORT */
/*
* Search the path for prog and return the file name.
* The returned value is unmetafied and in the unmeta storage
* area (N.B. should be duplicated if not used immediately and not
* equal to *namep).
*
* If namep is not NULL, *namep is set to the metafied programme
* name, which is in heap storage.
*/
/**/
char *
pathprog(char *prog, char **namep)
{
char **pp, ppmaxlen = 0, *buf, *funmeta;
struct stat st;
for (pp = path; *pp; pp++)
{
int len = strlen(*pp);
if (len > ppmaxlen)
ppmaxlen = len;
}
buf = zhalloc(ppmaxlen + strlen(prog) + 2);
for (pp = path; *pp; pp++) {
sprintf(buf, "%s/%s", *pp, prog);
funmeta = unmeta(buf);
if (access(funmeta, F_OK) == 0 &&
stat(funmeta, &st) >= 0 &&
!S_ISDIR(st.st_mode)) {
if (namep)
*namep = buf;
return funmeta;
}
}
return NULL;
}
/* get a symlink-free pathname for s relative to PWD */
/**/
char *
findpwd(char *s)
{
char *t;
if (*s == '/')
return xsymlink(s, 0);
s = tricat((pwd[1]) ? pwd : "", "/", s);
t = xsymlink(s, 0);
zsfree(s);
return t;
}
/* Check whether a string contains the *
* name of the present directory. */
/**/
int
ispwd(char *s)
{
struct stat sbuf, tbuf;
/* POSIX: environment PWD must be absolute */
if (*s != '/')
return 0;
if (stat((s = unmeta(s)), &sbuf) == 0 && stat(".", &tbuf) == 0)
if (sbuf.st_dev == tbuf.st_dev && sbuf.st_ino == tbuf.st_ino) {
/* POSIX: No element of $PWD may be "." or ".." */
while (*s) {
if (s[0] == '.' &&
(!s[1] || s[1] == '/' ||
(s[1] == '.' && (!s[2] || s[2] == '/'))))
break;
while (*s++ != '/' && *s)
continue;
}
return !*s;
}
return 0;
}
static char xbuf[PATH_MAX*2+1];
/**/
static char **
slashsplit(char *s)
{
char *t, **r, **q;
int t0;
if (!*s)
return (char **) zshcalloc(sizeof(char *));
for (t = s, t0 = 0; *t; t++)
if (*t == '/')
t0++;
q = r = (char **) zalloc(sizeof(char *) * (t0 + 2));
while ((t = strchr(s, '/'))) {
*q++ = ztrduppfx(s, t - s);
while (*t == '/')
t++;
if (!*t) {
*q = NULL;
return r;
}
s = t;
}
*q++ = ztrdup(s);
*q = NULL;
return r;
}
/* expands symlinks and .. or . expressions */
/**/
static int
xsymlinks(char *s, int full)
{
char **pp, **opp;
char xbuf2[PATH_MAX*3+1], xbuf3[PATH_MAX*2+1];
int t0, ret = 0;
zulong xbuflen = strlen(xbuf);
opp = pp = slashsplit(s);
for (; xbuflen < sizeof(xbuf) && *pp && ret >= 0; pp++) {
if (!strcmp(*pp, "."))
continue;
if (!strcmp(*pp, "..")) {
char *p;
if (!strcmp(xbuf, "/"))
continue;
if (!*xbuf)
continue;
p = xbuf + xbuflen;
while (*--p != '/')
xbuflen--;
*p = '\0';
/* The \0 isn't included in the length */
xbuflen--;
continue;
}
sprintf(xbuf2, "%s/%s", xbuf, *pp);
t0 = readlink(unmeta(xbuf2), xbuf3, PATH_MAX);
if (t0 == -1) {
zulong pplen = strlen(*pp) + 1;
if ((xbuflen += pplen) < sizeof(xbuf)) {
strcat(xbuf, "/");
strcat(xbuf, *pp);
} else {
*xbuf = 0;
ret = -1;
break;
}
} else {
ret = 1;
metafy(xbuf3, t0, META_NOALLOC);
if (!full) {
/*
* If only one expansion requested, ensure the
* full path is in xbuf.
*/
zulong len = xbuflen;
if (*xbuf3 == '/')
strcpy(xbuf, xbuf3);
else if ((len += strlen(xbuf3) + 1) < sizeof(xbuf)) {
strcpy(xbuf + xbuflen, "/");
strcpy(xbuf + xbuflen + 1, xbuf3);
} else {
*xbuf = 0;
ret = -1;
break;
}
while (*++pp) {
zulong newlen = len + strlen(*pp) + 1;
if (newlen < sizeof(xbuf)) {
strcpy(xbuf + len, "/");
strcpy(xbuf + len + 1, *pp);
len = newlen;
} else {
*xbuf = 01;
ret = -1;
break;
}
}
/*
* No need to update xbuflen, we're finished
* the expansion (for now).
*/
break;
}
if (*xbuf3 == '/') {
strcpy(xbuf, "");
if (xsymlinks(xbuf3 + 1, 1) < 0)
ret = -1;
else
xbuflen = strlen(xbuf);
} else
if (xsymlinks(xbuf3, 1) < 0)
ret = -1;
else
xbuflen = strlen(xbuf);
}
}
freearray(opp);
return ret;
}
/*
* expand symlinks in s, and remove other weird things:
* note that this always expands symlinks.
*
* 'heap' indicates whether to malloc() or allocate on the heap.
*/
/**/
char *
xsymlink(char *s, int heap)
{
if (*s != '/')
return NULL;
*xbuf = '\0';
if (xsymlinks(s + 1, 1) < 0)
zwarn("path expansion failed, using root directory");
if (!*xbuf)
return heap ? dupstring("/") : ztrdup("/");
return heap ? dupstring(xbuf) : ztrdup(xbuf);
}
/**/
void
print_if_link(char *s, int all)
{
if (*s == '/') {
*xbuf = '\0';
if (all) {
char *start = s + 1;
char xbuflink[PATH_MAX+1];
for (;;) {
if (xsymlinks(start, 0) > 0) {
printf(" -> ");
zputs(*xbuf ? xbuf : "/", stdout);
if (!*xbuf)
break;
strcpy(xbuflink, xbuf);
start = xbuflink + 1;
*xbuf = '\0';
} else {
break;
}
}
} else {
if (xsymlinks(s + 1, 1) > 0)
printf(" -> "), zputs(*xbuf ? xbuf : "/", stdout);
}
}
}
/* print a directory */
/**/
void
fprintdir(char *s, FILE *f)
{
Nameddir d = finddir(s);
if (!d)
fputs(unmeta(s), f);
else {
putc('~', f);
fputs(unmeta(d->node.nam), f);
fputs(unmeta(s + strlen(d->dir)), f);
}
}
/*
* Substitute a directory using a name.
* If there is none, return the original argument.
*
* At this level all strings involved are metafied.
*/
/**/
char *
substnamedir(char *s)
{
Nameddir d = finddir(s);
if (!d)
return quotestring(s, QT_BACKSLASH);
return zhtricat("~", d->node.nam, quotestring(s + strlen(d->dir),
QT_BACKSLASH));
}
/* Returns the current username. It caches the username *
* and uid to try to avoid requerying the password files *
* or NIS/NIS+ database. */
/**/
uid_t cached_uid;
/**/
char *cached_username;
/**/
char *
get_username(void)
{
#ifdef HAVE_GETPWUID
struct passwd *pswd;
uid_t current_uid;
current_uid = getuid();
if (current_uid != cached_uid) {
cached_uid = current_uid;
zsfree(cached_username);
if ((pswd = getpwuid(current_uid)))
cached_username = ztrdup(pswd->pw_name);
else
cached_username = ztrdup("");
}
#else /* !HAVE_GETPWUID */
cached_uid = getuid();
#endif /* !HAVE_GETPWUID */
return cached_username;
}
/* static variables needed by finddir(). */
static char *finddir_full;
static Nameddir finddir_last;
static int finddir_best;
/* ScanFunc used by finddir(). */
/**/
static void
finddir_scan(HashNode hn, UNUSED(int flags))
{
Nameddir nd = (Nameddir) hn;
if(nd->diff > finddir_best && !dircmp(nd->dir, finddir_full)
&& !(nd->node.flags & ND_NOABBREV)) {
finddir_last=nd;
finddir_best=nd->diff;
}
}
/*
* See if a path has a named directory as its prefix.
* If passed a NULL argument, it will invalidate any
* cached information.
*
* s here is metafied.
*/
/**/
Nameddir
finddir(char *s)
{
static struct nameddir homenode = { {NULL, "", 0}, NULL, 0 };
static int ffsz;
char **ares;
int len;
/* Invalidate directory cache if argument is NULL. This is called *
* whenever a node is added to or removed from the hash table, and *
* whenever the value of $HOME changes. (On startup, too.) */
if (!s) {
homenode.dir = home ? home : "";
homenode.diff = home ? strlen(home) : 0;
if(homenode.diff==1)
homenode.diff = 0;
if(!finddir_full)
finddir_full = zalloc(ffsz = PATH_MAX+1);
finddir_full[0] = 0;
return finddir_last = NULL;
}
#if 0
/*
* It's not safe to use the cache while we have function
* transformations, and it's not clear it's worth the
* complexity of guessing here whether subst_string_by_hook
* is going to turn up the goods.
*/
if (!strcmp(s, finddir_full) && *finddir_full)
return finddir_last;
#endif
if ((int)strlen(s) >= ffsz) {
free(finddir_full);
finddir_full = zalloc(ffsz = strlen(s) * 2);
}
strcpy(finddir_full, s);
finddir_best=0;
finddir_last=NULL;
finddir_scan(&homenode.node, 0);
scanhashtable(nameddirtab, 0, 0, 0, finddir_scan, 0);
ares = subst_string_by_hook("zsh_directory_name", "d", finddir_full);
if (ares && arrlen_ge(ares, 2) &&
(len = (int)zstrtol(ares[1], NULL, 10)) > finddir_best) {
/* better duplicate this string since it's come from REPLY */
finddir_last = (Nameddir)hcalloc(sizeof(struct nameddir));
finddir_last->node.nam = zhtricat("[", dupstring(ares[0]), "]");
finddir_last->dir = dupstrpfx(finddir_full, len);
finddir_last->diff = len - strlen(finddir_last->node.nam);
finddir_best = len;
}
return finddir_last;
}
/* add a named directory */
/**/
mod_export void
adduserdir(char *s, char *t, int flags, int always)
{
Nameddir nd;
char *eptr;
/* We don't maintain a hash table in non-interactive shells. */
if (!interact)
return;
/* The ND_USERNAME flag means that this possible hash table *
* entry is derived from a passwd entry. Such entries are *
* subordinate to explicitly generated entries. */
if ((flags & ND_USERNAME) && nameddirtab->getnode2(nameddirtab, s))
return;
/* Normal parameter assignments generate calls to this function, *
* with always==0. Unless the AUTO_NAME_DIRS option is set, we *
* don't let such assignments actually create directory names. *
* Instead, a reference to the parameter as a directory name can *
* cause the actual creation of the hash table entry. */
if (!always && unset(AUTONAMEDIRS) &&
!nameddirtab->getnode2(nameddirtab, s))
return;
if (!t || *t != '/' || strlen(t) >= PATH_MAX) {
/* We can't use this value as a directory, so simply remove *
* the corresponding entry in the hash table, if any. */
HashNode hn = nameddirtab->removenode(nameddirtab, s);
if(hn)
nameddirtab->freenode(hn);
return;
}
/* add the name */
nd = (Nameddir) zshcalloc(sizeof *nd);
nd->node.flags = flags;
eptr = t + strlen(t);
while (eptr > t && eptr[-1] == '/')
eptr--;
if (eptr == t) {
/*
* Don't abbreviate multiple slashes at the start of a
* named directory, since these are sometimes used for
* special purposes.
*/
nd->dir = ztrdup(t);
} else
nd->dir = ztrduppfx(t, eptr - t);
/* The variables PWD and OLDPWD are not to be displayed as ~PWD etc. */
if (!strcmp(s, "PWD") || !strcmp(s, "OLDPWD"))
nd->node.flags |= ND_NOABBREV;
nameddirtab->addnode(nameddirtab, ztrdup(s), nd);
}
/* Get a named directory: this function can cause a directory name *
* to be added to the hash table, if it isn't there already. */
/**/
char *
getnameddir(char *name)
{
Param pm;
char *str;
Nameddir nd;
/* Check if it is already in the named directory table */
if ((nd = (Nameddir) nameddirtab->getnode(nameddirtab, name)))
return dupstring(nd->dir);
/* Check if there is a scalar parameter with this name whose value *
* begins with a `/'. If there is, add it to the hash table and *
* return the new value. */
if ((pm = (Param) paramtab->getnode(paramtab, name)) &&
(PM_TYPE(pm->node.flags) == PM_SCALAR) &&
(str = getsparam(name)) && *str == '/') {
pm->node.flags |= PM_NAMEDDIR;
adduserdir(name, str, 0, 1);
return str;
}
#ifdef HAVE_GETPWNAM
{
/* Retrieve an entry from the password table/database for this user. */
struct passwd *pw;
if ((pw = getpwnam(name))) {
char *dir = isset(CHASELINKS) ? xsymlink(pw->pw_dir, 0)
: ztrdup(pw->pw_dir);
if (dir) {
adduserdir(name, dir, ND_USERNAME, 1);
str = dupstring(dir);
zsfree(dir);
return str;
} else
return dupstring(pw->pw_dir);
}
}
#endif /* HAVE_GETPWNAM */
/* There are no more possible sources of directory names, so give up. */
return NULL;
}
/*
* Compare directories. Both are metafied.
*/
/**/
static int
dircmp(char *s, char *t)
{
if (s) {
for (; *s == *t; s++, t++)
if (!*s)
return 0;
if (!*s && *t == '/')
return 0;
}
return 1;
}
/*
* Extra functions to call before displaying the prompt.
* The data is a Prepromptfn.
*/
static LinkList prepromptfns;
/* Add a function to the list of pre-prompt functions. */
/**/
mod_export void
addprepromptfn(voidvoidfnptr_t func)
{
Prepromptfn ppdat = (Prepromptfn)zalloc(sizeof(struct prepromptfn));
ppdat->func = func;
if (!prepromptfns)
prepromptfns = znewlinklist();
zaddlinknode(prepromptfns, ppdat);
}
/* Remove a function from the list of pre-prompt functions. */
/**/
mod_export void
delprepromptfn(voidvoidfnptr_t func)
{
LinkNode ln;
for (ln = firstnode(prepromptfns); ln; ln = nextnode(ln)) {
Prepromptfn ppdat = (Prepromptfn)getdata(ln);
if (ppdat->func == func) {
(void)remnode(prepromptfns, ln);
zfree(ppdat, sizeof(struct prepromptfn));
return;
}
}
#ifdef DEBUG
dputs("BUG: failed to delete node from prepromptfns");
#endif
}
/*
* Functions to call at a particular time even if not at
* the prompt. This is handled by zle. The data is a
* Timedfn. The functions must be in time order, but this
* is enforced by addtimedfn().
*
* Note on debugging: the code in sched.c currently assumes it's
* the only user of timedfns for the purposes of checking whether
* there's a function on the list. If this becomes no longer the case,
* the DPUTS() tests in sched.c need rewriting.
*/
/**/
mod_export LinkList timedfns;
/* Add a function to the list of timed functions. */
/**/
mod_export void
addtimedfn(voidvoidfnptr_t func, time_t when)
{
Timedfn tfdat = (Timedfn)zalloc(sizeof(struct timedfn));
tfdat->func = func;
tfdat->when = when;
if (!timedfns) {
timedfns = znewlinklist();
zaddlinknode(timedfns, tfdat);
} else {
LinkNode ln = firstnode(timedfns);
/*
* Insert the new element in the linked list. We do
* rather too much work here since the standard
* functions insert after a given node, whereas we
* want to insert the new data before the first element
* with a greater time.
*
* In practice, the only use of timed functions is
* sched, which only adds the one function; so this
* whole branch isn't used beyond the following block.
*/
if (!ln) {
zaddlinknode(timedfns, tfdat);
return;
}
for (;;) {
Timedfn tfdat2;
LinkNode next = nextnode(ln);
if (!next) {
zaddlinknode(timedfns, tfdat);
return;
}
tfdat2 = (Timedfn)getdata(next);
if (when < tfdat2->when) {
zinsertlinknode(timedfns, ln, tfdat);
return;
}
ln = next;
}
}
}
/*
* Delete a function from the list of timed functions.
* Note that if the function apperas multiple times only
* the first occurrence will be removed.
*
* Note also that when zle calls the function it does *not*
* automatically delete the entry from the list. That must
* be done by the function called. This is recommended as otherwise
* the function will keep being called immediately. (It just so
* happens this "feature" fits in well with the only current use
* of timed functions.)
*/
/**/
mod_export void
deltimedfn(voidvoidfnptr_t func)
{
LinkNode ln;
for (ln = firstnode(timedfns); ln; ln = nextnode(ln)) {
Timedfn ppdat = (Timedfn)getdata(ln);
if (ppdat->func == func) {
(void)remnode(timedfns, ln);
zfree(ppdat, sizeof(struct timedfn));
return;
}
}
#ifdef DEBUG
dputs("BUG: failed to delete node from timedfns");
#endif
}
/* the last time we checked mail */
/**/
time_t lastmailcheck;
/* the last time we checked the people in the WATCH variable */
/**/
time_t lastwatch;
/*
* Call a function given by "name" with optional arguments
* "lnklist". If these are present the first argument is the function name.
*
* If "arrayp" is not zero, we also look through
* the array "name"_functions and execute functions found there.
*
* If "retval" is not NULL, the return value of the first hook function to
* return non-zero is stored in *"retval". The return value is not otherwise
* available as the calling context is restored.
*
* Returns 0 if at least one function was called (regardless of that function's
* exit status), and 1 otherwise.
*/
/**/
mod_export int
callhookfunc(char *name, LinkList lnklst, int arrayp, int *retval)
{
Shfunc shfunc;
/*
* Save stopmsg, since user doesn't get a chance to respond
* to a list of jobs generated in a hook.
*/
int osc = sfcontext, osm = stopmsg, stat = 1, ret = 0;
int old_incompfunc = incompfunc;
sfcontext = SFC_HOOK;
incompfunc = 0;
if ((shfunc = getshfunc(name))) {
ret = doshfunc(shfunc, lnklst, 1);
stat = 0;
}
if (arrayp) {
char **arrptr;
int namlen = strlen(name);
VARARR(char, arrnam, namlen + HOOK_SUFFIX_LEN);
memcpy(arrnam, name, namlen);
memcpy(arrnam + namlen, HOOK_SUFFIX, HOOK_SUFFIX_LEN);
if ((arrptr = getaparam(arrnam))) {
arrptr = arrdup(arrptr);
for (; *arrptr; arrptr++) {
if ((shfunc = getshfunc(*arrptr))) {
int newret = doshfunc(shfunc, lnklst, 1);
if (!ret)
ret = newret;
stat = 0;
}
}
}
}
sfcontext = osc;
stopmsg = osm;
incompfunc = old_incompfunc;
if (retval)
*retval = ret;
return stat;
}
/* do pre-prompt stuff */
/**/
void
preprompt(void)
{
static time_t lastperiodic;
time_t currentmailcheck;
LinkNode ln;
zlong period = getiparam("PERIOD");
zlong mailcheck = getiparam("MAILCHECK");
/*
* Handle any pending window size changes before we compute prompts,
* then block them again to avoid interrupts during prompt display.
*/
winch_unblock();
winch_block();
if (isset(PROMPTSP) && isset(PROMPTCR) && !use_exit_printed && shout) {
/* The PROMPT_SP heuristic will move the prompt down to a new line
* if there was any dangling output on the line (assuming the terminal
* has automatic margins, but we try even if hasam isn't set).
* Unfortunately it interacts badly with ZLE displaying message
* when ^D has been pressed. So just disable PROMPT_SP logic in
* this case */
char *eolmark = getsparam("PROMPT_EOL_MARK");
char *str;
int percents = opts[PROMPTPERCENT], w = 0;
if (!eolmark)
eolmark = "%B%S%#%s%b";
opts[PROMPTPERCENT] = 1;
str = promptexpand(eolmark, 1, NULL, NULL, NULL);
countprompt(str, &w, 0, -1);
opts[PROMPTPERCENT] = percents;
zputs(str, shout);
fprintf(shout, "%*s\r%*s\r", (int)zterm_columns - w - !hasxn,
"", w, "");
fflush(shout);
free(str);
}
/* If NOTIFY is not set, then check for completed *
* jobs before we print the prompt. */
if (unset(NOTIFY))
scanjobs();
if (errflag)
return;
/* If a shell function named "precmd" exists, *
* then execute it. */
callhookfunc("precmd", NULL, 1, NULL);
if (errflag)
return;
/* If 1) the parameter PERIOD exists, 2) a hook function for *
* "periodic" exists, 3) it's been greater than PERIOD since we *
* executed any such hook, then execute it now. */
if (period && ((zlong)time(NULL) > (zlong)lastperiodic + period) &&
!callhookfunc("periodic", NULL, 1, NULL))
lastperiodic = time(NULL);
if (errflag)
return;
/* If WATCH is set, then check for the *
* specified login/logout events. */
if (watch) {
if ((int) difftime(time(NULL), lastwatch) > getiparam("LOGCHECK")) {
dowatch();
lastwatch = time(NULL);
}
}
if (errflag)
return;
/* Check mail */
currentmailcheck = time(NULL);
if (mailcheck &&
(zlong) difftime(currentmailcheck, lastmailcheck) > mailcheck) {
char *mailfile;
if (mailpath && *mailpath && **mailpath)
checkmailpath(mailpath);
else {
queue_signals();
if ((mailfile = getsparam("MAIL")) && *mailfile) {
char *x[2];
x[0] = mailfile;
x[1] = NULL;
checkmailpath(x);
}
unqueue_signals();
}
lastmailcheck = currentmailcheck;
}
if (prepromptfns) {
for(ln = firstnode(prepromptfns); ln; ln = nextnode(ln)) {
Prepromptfn ppnode = (Prepromptfn)getdata(ln);
ppnode->func();
}
}
}
/**/
static void
checkmailpath(char **s)
{
struct stat st;
char *v, *u, c;
while (*s) {
for (v = *s; *v && *v != '?'; v++);
c = *v;
*v = '\0';
if (c != '?')
u = NULL;
else
u = v + 1;
if (**s == 0) {
*v = c;
zerr("empty MAILPATH component: %s", *s);
} else if (mailstat(unmeta(*s), &st) == -1) {
if (errno != ENOENT)
zerr("%e: %s", errno, *s);
} else if (S_ISDIR(st.st_mode)) {
LinkList l;
DIR *lock = opendir(unmeta(*s));
char buf[PATH_MAX * 2 + 1], **arr, **ap;
int ct = 1;
if (lock) {
char *fn;
pushheap();
l = newlinklist();
while ((fn = zreaddir(lock, 1)) && !errflag) {
if (u)
sprintf(buf, "%s/%s?%s", *s, fn, u);
else
sprintf(buf, "%s/%s", *s, fn);
addlinknode(l, dupstring(buf));
ct++;
}
closedir(lock);
ap = arr = (char **) zhalloc(ct * sizeof(char *));
while ((*ap++ = (char *)ugetnode(l)));
checkmailpath(arr);
popheap();
}
} else if (shout) {
if (st.st_size && st.st_atime <= st.st_mtime &&
st.st_mtime >= lastmailcheck) {
if (!u) {
fprintf(shout, "You have new mail.\n");
fflush(shout);
} else {
char *usav;
int uusav = underscoreused;
usav = zalloc(underscoreused);
if (usav)
memcpy(usav, zunderscore, underscoreused);
setunderscore(*s);
u = dupstring(u);
if (!parsestr(&u)) {
singsub(&u);
zputs(u, shout);
fputc('\n', shout);
fflush(shout);
}
if (usav) {
setunderscore(usav);
zfree(usav, uusav);
}
}
}
if (isset(MAILWARNING) && st.st_atime > st.st_mtime &&
st.st_atime > lastmailcheck && st.st_size) {
fprintf(shout, "The mail in %s has been read.\n", unmeta(*s));
fflush(shout);
}
}
*v = c;
s++;
}
}
/* This prints the XTRACE prompt. */
/**/
FILE *xtrerr = 0;
/**/
void
printprompt4(void)
{
if (!xtrerr)
xtrerr = stderr;
if (prompt4) {
int l, t = opts[XTRACE];
char *s = dupstring(prompt4);
opts[XTRACE] = 0;
unmetafy(s, &l);
s = unmetafy(promptexpand(metafy(s, l, META_NOALLOC),
0, NULL, NULL, NULL), &l);
opts[XTRACE] = t;
fprintf(xtrerr, "%s", s);
free(s);
}
}
/**/
mod_export void
freestr(void *a)
{
zsfree(a);
}
/**/
mod_export void
gettyinfo(struct ttyinfo *ti)
{
if (SHTTY != -1) {
#ifdef HAVE_TERMIOS_H
# ifdef HAVE_TCGETATTR
if (tcgetattr(SHTTY, &ti->tio) == -1)
# else
if (ioctl(SHTTY, TCGETS, &ti->tio) == -1)
# endif
zerr("bad tcgets: %e", errno);
#else
# ifdef HAVE_TERMIO_H
ioctl(SHTTY, TCGETA, &ti->tio);
# else
ioctl(SHTTY, TIOCGETP, &ti->sgttyb);
ioctl(SHTTY, TIOCLGET, &ti->lmodes);
ioctl(SHTTY, TIOCGETC, &ti->tchars);
ioctl(SHTTY, TIOCGLTC, &ti->ltchars);
# endif
#endif
}
}
/**/
mod_export void
settyinfo(struct ttyinfo *ti)
{
if (SHTTY != -1) {
#ifdef HAVE_TERMIOS_H
# ifdef HAVE_TCGETATTR
# ifndef TCSADRAIN
# define TCSADRAIN 1 /* XXX Princeton's include files are screwed up */
# endif
while (tcsetattr(SHTTY, TCSADRAIN, &ti->tio) == -1 && errno == EINTR)
;
# else
while (ioctl(SHTTY, TCSETS, &ti->tio) == -1 && errno == EINTR)
;
# endif
/* zerr("settyinfo: %e",errno);*/
#else
# ifdef HAVE_TERMIO_H
ioctl(SHTTY, TCSETA, &ti->tio);
# else
ioctl(SHTTY, TIOCSETN, &ti->sgttyb);
ioctl(SHTTY, TIOCLSET, &ti->lmodes);
ioctl(SHTTY, TIOCSETC, &ti->tchars);
ioctl(SHTTY, TIOCSLTC, &ti->ltchars);
# endif
#endif
}
}
/* the default tty state */
/**/
mod_export struct ttyinfo shttyinfo;
/* != 0 if we need to call resetvideo() */
/**/
mod_export int resetneeded;
#ifdef TIOCGWINSZ
/* window size changed */
/**/
mod_export int winchanged;
#endif
static int
adjustlines(int signalled)
{
int oldlines = zterm_lines;
#ifdef TIOCGWINSZ
if (signalled || zterm_lines <= 0)
zterm_lines = shttyinfo.winsize.ws_row;
else
shttyinfo.winsize.ws_row = zterm_lines;
#endif /* TIOCGWINSZ */
if (zterm_lines <= 0) {
DPUTS(signalled, "BUG: Impossible TIOCGWINSZ rows");
zterm_lines = tclines > 0 ? tclines : 24;
}
if (zterm_lines > 2)
termflags &= ~TERM_SHORT;
else
termflags |= TERM_SHORT;
return (zterm_lines != oldlines);
}
static int
adjustcolumns(int signalled)
{
int oldcolumns = zterm_columns;
#ifdef TIOCGWINSZ
if (signalled || zterm_columns <= 0)
zterm_columns = shttyinfo.winsize.ws_col;
else
shttyinfo.winsize.ws_col = zterm_columns;
#endif /* TIOCGWINSZ */
if (zterm_columns <= 0) {
DPUTS(signalled, "BUG: Impossible TIOCGWINSZ cols");
zterm_columns = tccolumns > 0 ? tccolumns : 80;
}
if (zterm_columns > 2)
termflags &= ~TERM_NARROW;
else
termflags |= TERM_NARROW;
return (zterm_columns != oldcolumns);
}
/* check the size of the window and adjust if necessary. *
* The value of from: *
* 0: called from update_job or setupvals *
* 1: called from the SIGWINCH handler *
* 2: called from the LINES parameter callback *
* 3: called from the COLUMNS parameter callback */
/**/
void
adjustwinsize(int from)
{
static int getwinsz = 1;
#ifdef TIOCGWINSZ
int ttyrows = shttyinfo.winsize.ws_row;
int ttycols = shttyinfo.winsize.ws_col;
#endif
int resetzle = 0;
if (getwinsz || from == 1) {
#ifdef TIOCGWINSZ
if (SHTTY == -1)
return;
if (ioctl(SHTTY, TIOCGWINSZ, (char *)&shttyinfo.winsize) == 0) {
resetzle = (ttyrows != shttyinfo.winsize.ws_row ||
ttycols != shttyinfo.winsize.ws_col);
if (from == 0 && resetzle && ttyrows && ttycols)
from = 1; /* Signal missed while a job owned the tty? */
ttyrows = shttyinfo.winsize.ws_row;
ttycols = shttyinfo.winsize.ws_col;
} else {
/* Set to value from environment on failure */
shttyinfo.winsize.ws_row = zterm_lines;
shttyinfo.winsize.ws_col = zterm_columns;
resetzle = (from == 1);
}
#else
resetzle = from == 1;
#endif /* TIOCGWINSZ */
} /* else
return; */
switch (from) {
case 0:
case 1:
getwinsz = 0;
/* Calling setiparam() here calls this function recursively, but *
* because we've already called adjustlines() and adjustcolumns() *
* here, recursive calls are no-ops unless a signal intervenes. *
* The commented "else return;" above might be a safe shortcut, *
* but I'm concerned about what happens on race conditions; e.g., *
* suppose the user resizes his xterm during `eval $(resize)'? */
if (adjustlines(from) && zgetenv("LINES"))
setiparam("LINES", zterm_lines);
if (adjustcolumns(from) && zgetenv("COLUMNS"))
setiparam("COLUMNS", zterm_columns);
getwinsz = 1;
break;
case 2:
resetzle = adjustlines(0);
break;
case 3:
resetzle = adjustcolumns(0);
break;
}
#ifdef TIOCGWINSZ
if (interact && from >= 2 &&
(shttyinfo.winsize.ws_row != ttyrows ||
shttyinfo.winsize.ws_col != ttycols)) {
/* shttyinfo.winsize is already set up correctly */
/* ioctl(SHTTY, TIOCSWINSZ, (char *)&shttyinfo.winsize); */
}
#endif /* TIOCGWINSZ */
if (zleactive && resetzle) {
#ifdef TIOCGWINSZ
winchanged =
#endif /* TIOCGWINSZ */
resetneeded = 1;
zleentry(ZLE_CMD_RESET_PROMPT);
zleentry(ZLE_CMD_REFRESH);
}
}
/*
* Ensure the fdtable is large enough for fd, and that the
* maximum fd is set appropriately.
*/
static void
check_fd_table(int fd)
{
if (fd <= max_zsh_fd)
return;
if (fd >= fdtable_size) {
int old_size = fdtable_size;
while (fd >= fdtable_size)
fdtable = zrealloc(fdtable,
(fdtable_size *= 2)*sizeof(*fdtable));
memset(fdtable + old_size, 0,
(fdtable_size - old_size) * sizeof(*fdtable));
}
max_zsh_fd = fd;
}
/* Move a fd to a place >= 10 and mark the new fd in fdtable. If the fd *
* is already >= 10, it is not moved. If it is invalid, -1 is returned. */
/**/
mod_export int
movefd(int fd)
{
if(fd != -1 && fd < 10) {
#ifdef F_DUPFD
int fe = fcntl(fd, F_DUPFD, 10);
#else
int fe = movefd(dup(fd));
#endif
/*
* To close or not to close if fe is -1?
* If it is -1, we haven't moved the fd, so if we close
* it we lose it; but we're probably not going to be able
* to use it in situ anyway. So probably better to avoid a leak.
*/
zclose(fd);
fd = fe;
}
if(fd != -1) {
check_fd_table(fd);
fdtable[fd] = FDT_INTERNAL;
}
return fd;
}
/*
* Move fd x to y. If x == -1, fd y is closed.
* Returns y for success, -1 for failure.
*/
/**/
mod_export int
redup(int x, int y)
{
int ret = y;
if(x < 0)
zclose(y);
else if (x != y) {
if (dup2(x, y) == -1) {
ret = -1;
} else {
check_fd_table(y);
fdtable[y] = fdtable[x];
if (fdtable[y] == FDT_FLOCK || fdtable[y] == FDT_FLOCK_EXEC)
fdtable[y] = FDT_INTERNAL;
}
/*
* Closing any fd to the locked file releases the lock.
* This isn't expected to happen, it's here for completeness.
*/
if (fdtable[x] == FDT_FLOCK)
fdtable_flocks--;
zclose(x);
}
return ret;
}
/*
* Add an fd opened ithin a module.
*
* fdt is the type of the fd; see the FDT_ definitions in zsh.h.
* The most likely falures are:
*
* FDT_EXTERNAL: the fd can be used within the shell for normal I/O but
* it will not be closed automatically or by normal shell syntax.
*
* FDT_MODULE: as FDT_EXTERNAL, but it can only be closed by the module
* (which should included zclose() as part of the sequence), not by
* the standard shell syntax for closing file descriptors.
*
* FDT_INTERNAL: fd is treated like others created by the shell for
* internal use; it can be closed and will be closed by the shell if it
* exec's or performs an exec with a fork optimised out.
*
* Safe if fd is -1 to indicate failure.
*/
/**/
mod_export void
addmodulefd(int fd, int fdt)
{
if (fd >= 0) {
check_fd_table(fd);
fdtable[fd] = fdt;
}
}
/**/
/*
* Indicate that an fd has a file lock; if cloexec is 1 it will be closed
* on exec.
* The fd should already be known to fdtable (e.g. by movefd).
* Note the fdtable code doesn't care what sort of lock
* is used; this simply prevents the main shell exiting prematurely
* when it holds a lock.
*/
/**/
mod_export void
addlockfd(int fd, int cloexec)
{
if (cloexec) {
if (fdtable[fd] != FDT_FLOCK)
fdtable_flocks++;
fdtable[fd] = FDT_FLOCK;
} else {
fdtable[fd] = FDT_FLOCK_EXEC;
}
}
/* Close the given fd, and clear it from fdtable. */
/**/
mod_export int
zclose(int fd)
{
if (fd >= 0) {
/*
* Careful: we allow closing of arbitrary fd's, beyond
* max_zsh_fd. In that case we don't try anything clever.
*/
if (fd <= max_zsh_fd) {
if (fdtable[fd] == FDT_FLOCK)
fdtable_flocks--;
fdtable[fd] = FDT_UNUSED;
while (max_zsh_fd > 0 && fdtable[max_zsh_fd] == FDT_UNUSED)
max_zsh_fd--;
if (fd == coprocin)
coprocin = -1;
if (fd == coprocout)
coprocout = -1;
}
return close(fd);
}
return -1;
}
/*
* Close an fd returning 0 if used for locking; return -1 if it isn't.
*/
/**/
mod_export int
zcloselockfd(int fd)
{
if (fd > max_zsh_fd)
return -1;
if (fdtable[fd] != FDT_FLOCK && fdtable[fd] != FDT_FLOCK_EXEC)
return -1;
zclose(fd);
return 0;
}
#ifdef HAVE__MKTEMP
extern char *_mktemp(char *);
#endif
/* Get a unique filename for use as a temporary file. If "prefix" is
* NULL, the name is relative to $TMPPREFIX; If it is non-NULL, the
* unique suffix includes a prefixed '.' for improved readability. If
* "use_heap" is true, we allocate the returned name on the heap.
* The string passed as "prefix" is expected to be metafied. */
/**/
mod_export char *
gettempname(const char *prefix, int use_heap)
{
char *ret, *suffix = prefix ? ".XXXXXX" : "XXXXXX";
queue_signals();
if (!prefix && !(prefix = getsparam("TMPPREFIX")))
prefix = DEFAULT_TMPPREFIX;
if (use_heap)
ret = dyncat(unmeta(prefix), suffix);
else
ret = bicat(unmeta(prefix), suffix);
#ifdef HAVE__MKTEMP
/* Zsh uses mktemp() safely, so silence the warnings */
ret = (char *) _mktemp(ret);
#else
ret = (char *) mktemp(ret);
#endif
unqueue_signals();
return ret;
}
/* The gettempfile() "prefix" is expected to be metafied, see hist.c
* and gettempname(). */
/**/
mod_export int
gettempfile(const char *prefix, int use_heap, char **tempname)
{
char *fn;
int fd;
#if HAVE_MKSTEMP
char *suffix = prefix ? ".XXXXXX" : "XXXXXX";
queue_signals();
if (!prefix && !(prefix = getsparam("TMPPREFIX")))
prefix = DEFAULT_TMPPREFIX;
if (use_heap)
fn = dyncat(unmeta(prefix), suffix);
else
fn = bicat(unmeta(prefix), suffix);
fd = mkstemp(fn);
if (fd < 0) {
if (!use_heap)
free(fn);
fn = NULL;
}
#else
int failures = 0;
queue_signals();
do {
if (!(fn = gettempname(prefix, use_heap))) {
fd = -1;
break;
}
if ((fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0600)) >= 0)
break;
if (!use_heap)
free(fn);
fn = NULL;
} while (errno == EEXIST && ++failures < 16);
#endif
*tempname = fn;
unqueue_signals();
return fd;
}
/* Check if a string contains a token */
/**/
mod_export int
has_token(const char *s)
{
while(*s)
if(itok(*s++))
return 1;
return 0;
}
/* Delete a character in a string */
/**/
mod_export void
chuck(char *str)
{
while ((str[0] = str[1]))
str++;
}
/**/
mod_export int
tulower(int c)
{
c &= 0xff;
return (isupper(c) ? tolower(c) : c);
}
/**/
mod_export int
tuupper(int c)
{
c &= 0xff;
return (islower(c) ? toupper(c) : c);
}
/* copy len chars from t into s, and null terminate */
/**/
void
ztrncpy(char *s, char *t, int len)
{
while (len--)
*s++ = *t++;
*s = '\0';
}
/* copy t into *s and update s */
/**/
mod_export void
strucpy(char **s, char *t)
{
char *u = *s;
while ((*u++ = *t++));
*s = u - 1;
}
/**/
mod_export void
struncpy(char **s, char *t, int n)
{
char *u = *s;
while (n--)
*u++ = *t++;
*s = u;
*u = '\0';
}
/* Return the number of elements in an array of pointers. *
* It doesn't count the NULL pointer at the end. */
/**/
mod_export int
arrlen(char **s)
{
int count;
for (count = 0; *s; s++, count++);
return count;
}
/* Return TRUE iff arrlen(s) >= lower_bound, but more efficiently. */
/**/
mod_export char
arrlen_ge(char **s, unsigned lower_bound)
{
while (lower_bound--)
if (!*s++)
return 0 /* FALSE */;
return 1 /* TRUE */;
}
/* Return TRUE iff arrlen(s) > lower_bound, but more efficiently. */
/**/
mod_export char
arrlen_gt(char **s, unsigned lower_bound)
{
return arrlen_ge(s, 1+lower_bound);
}
/* Return TRUE iff arrlen(s) <= upper_bound, but more efficiently. */
/**/
mod_export char
arrlen_le(char **s, unsigned upper_bound)
{
return arrlen_lt(s, 1+upper_bound);
}
/* Return TRUE iff arrlen(s) < upper_bound, but more efficiently. */
/**/
mod_export char
arrlen_lt(char **s, unsigned upper_bound)
{
return !arrlen_ge(s, upper_bound);
}
/* Skip over a balanced pair of parenthesis. */
/**/
mod_export int
skipparens(char inpar, char outpar, char **s)
{
int level;
if (**s != inpar)
return -1;
for (level = 1; *++*s && level;)
if (**s == inpar)
++level;
else if (**s == outpar)
--level;
return level;
}
/**/
mod_export zlong
zstrtol(const char *s, char **t, int base)
{
return zstrtol_underscore(s, t, base, 0);
}
/* Convert string to zlong (see zsh.h). This function (without the z) *
* is contained in the ANSI standard C library, but a lot of them seem *
* to be broken. */
/**/
mod_export zlong
zstrtol_underscore(const char *s, char **t, int base, int underscore)
{
const char *inp, *trunc = NULL;
zulong calc = 0, newcalc = 0;
int neg;
while (inblank(*s))
s++;
if ((neg = (*s == '-')))
s++;
else if (*s == '+')
s++;
if (!base) {
if (*s != '0')
base = 10;
else if (*++s == 'x' || *s == 'X')
base = 16, s++;
else if (*s == 'b' || *s == 'B')
base = 2, s++;
else
base = 8;
}
inp = s;
if (base < 2 || base > 36) {
zerr("invalid base (must be 2 to 36 inclusive): %d", base);
return (zlong)0;
} else if (base <= 10) {
for (; (*s >= '0' && *s < ('0' + base)) ||
(underscore && *s == '_'); s++) {
if (trunc || *s == '_')
continue;
newcalc = calc * base + *s - '0';
if (newcalc < calc)
{
trunc = s;
continue;
}
calc = newcalc;
}
} else {
for (; idigit(*s) || (*s >= 'a' && *s < ('a' + base - 10))
|| (*s >= 'A' && *s < ('A' + base - 10))
|| (underscore && *s == '_'); s++) {
if (trunc || *s == '_')
continue;
newcalc = calc*base + (idigit(*s) ? (*s - '0') : (*s & 0x1f) + 9);
if (newcalc < calc)
{
trunc = s;
continue;
}
calc = newcalc;
}
}
/*
* Special case: check for a number that was just too long for
* signed notation.
* Extra special case: the lowest negative number would trigger
* the first test, but is actually representable correctly.
* This is a 1 in the top bit, all others zero, so test for
* that explicitly.
*/
if (!trunc && (zlong)calc < 0 &&
(!neg || calc & ~((zulong)1 << (8*sizeof(zulong)-1))))
{
trunc = s - 1;
calc /= base;
}
if (trunc)
zwarn("number truncated after %d digits: %s", (int)(trunc - inp), inp);
if (t)
*t = (char *)s;
return neg ? -(zlong)calc : (zlong)calc;
}
/**/
mod_export int
setblock_fd(int turnonblocking, int fd, long *modep)
{
#ifdef O_NDELAY
# ifdef O_NONBLOCK
# define NONBLOCK (O_NDELAY|O_NONBLOCK)
# else /* !O_NONBLOCK */
# define NONBLOCK O_NDELAY
# endif /* !O_NONBLOCK */
#else /* !O_NDELAY */
# ifdef O_NONBLOCK
# define NONBLOCK O_NONBLOCK
# else /* !O_NONBLOCK */
# define NONBLOCK 0
# endif /* !O_NONBLOCK */
#endif /* !O_NDELAY */
#if NONBLOCK
struct stat st;
if (!fstat(fd, &st) && !S_ISREG(st.st_mode)) {
*modep = fcntl(fd, F_GETFL, 0);
if (*modep != -1) {
if (!turnonblocking) {
/* We want to know if blocking was off */
if ((*modep & NONBLOCK) ||
!fcntl(fd, F_SETFL, *modep | NONBLOCK))
return 1;
} else if ((*modep & NONBLOCK) &&
!fcntl(fd, F_SETFL, *modep & ~NONBLOCK)) {
/* Here we want to know if the state changed */
return 1;
}
}
} else
#endif /* NONBLOCK */
*modep = -1;
return 0;
#undef NONBLOCK
}
/**/
int
setblock_stdin(void)
{
long mode;
return setblock_fd(1, 0, &mode);
}
/*
* Check for pending input on fd. If polltty is set, we may need to
* use termio to look for input. As a final resort, go to non-blocking
* input and try to read a character, which in this case will be
* returned in *readchar.
*
* Note that apart from setting (and restoring) non-blocking input,
* this function does not change the input mode. The calling function
* should have set cbreak mode if necessary.
*
* fd may be -1 to sleep until the timeout in microseconds. This is a
* fallback for old systems that don't have nanosleep(). Some very old
* systems might not have select: get with it, daddy-o.
*/
/**/
mod_export int
read_poll(int fd, int *readchar, int polltty, zlong microseconds)
{
int ret = -1;
long mode = -1;
char c;
#ifdef HAVE_SELECT
fd_set foofd;
struct timeval expire_tv;
#else
#ifdef FIONREAD
int val;
#endif
#endif
#ifdef HAS_TIO
struct ttyinfo ti;
#endif
if (fd < 0 || (polltty && !isatty(fd)))
polltty = 0; /* no tty to poll */
#if defined(HAS_TIO) && !defined(__CYGWIN__)
/*
* Under Solaris, at least, reading from the terminal in non-canonical
* mode requires that we use the VMIN mechanism to poll. Any attempt
* to check any other way, or to set the terminal to non-blocking mode
* and poll that way, fails; it will just for canonical mode input.
* We should probably use this mechanism if the user has set non-canonical
* mode, in which case testing here for isatty() and ~ICANON would be
* better than testing whether bin_read() set it, but for now we've got
* enough problems.
*
* Under Cygwin, you won't be surprised to here, this mechanism,
* although present, doesn't work, and we *have* to use ordinary
* non-blocking reads to find out if there is a character present
* in non-canonical mode.
*
* I am assuming Solaris is nearer the UNIX norm. This is not necessarily
* as plausible as it sounds, but it seems the right way to guess.
* pws 2000/06/26
*/
if (polltty && fd >= 0) {
gettyinfo(&ti);
if ((polltty = ti.tio.c_cc[VMIN])) {
ti.tio.c_cc[VMIN] = 0;
/* termios timeout is 10ths of a second */
ti.tio.c_cc[VTIME] = (int) (microseconds / (zlong)100000);
settyinfo(&ti);
}
}
#else
polltty = 0;
#endif
#ifdef HAVE_SELECT
expire_tv.tv_sec = (int) (microseconds / (zlong)1000000);
expire_tv.tv_usec = microseconds % (zlong)1000000;
FD_ZERO(&foofd);
if (fd > -1) {
FD_SET(fd, &foofd);
ret = select(fd+1, (SELECT_ARG_2_T) &foofd, NULL, NULL, &expire_tv);
} else
ret = select(0, NULL, NULL, NULL, &expire_tv);
#else
if (fd < 0) {
/* OK, can't do that. Just quietly sleep for a second. */
sleep(1);
return 1;
}
#ifdef FIONREAD
if (ioctl(fd, FIONREAD, (char *) &val) == 0)
ret = (val > 0);
#endif
#endif
if (fd >= 0 && ret < 0 && !errflag) {
/*
* Final attempt: set non-blocking read and try to read a character.
* Praise Bill, this works under Cygwin (nothing else seems to).
*/
if ((polltty || setblock_fd(0, fd, &mode)) && read(fd, &c, 1) > 0) {
*readchar = c;
ret = 1;
}
if (mode != -1)
fcntl(fd, F_SETFL, mode);
}
#ifdef HAS_TIO
if (polltty) {
ti.tio.c_cc[VMIN] = 1;
ti.tio.c_cc[VTIME] = 0;
settyinfo(&ti);
}
#endif
return (ret > 0);
}
/*
* Sleep for the given number of microseconds --- must be within
* range of a long at the moment, but this is only used for
* limited internal purposes.
*/
/**/
int
zsleep(long us)
{
#ifdef HAVE_NANOSLEEP
struct timespec sleeptime;
sleeptime.tv_sec = (time_t)us / (time_t)1000000;
sleeptime.tv_nsec = (us % 1000000L) * 1000L;
for (;;) {
struct timespec rem;
int ret = nanosleep(&sleeptime, &rem);
if (ret == 0)
return 1;
else if (errno != EINTR)
return 0;
sleeptime = rem;
}
#else
int dummy;
return read_poll(-1, &dummy, 0, us);
#endif
}
/**
* Sleep for time (fairly) randomly up to max_us microseconds.
* Don't let the wallclock time extend beyond end_time.
* Return 1 if that seemed to work, else 0.
*
* For best results max_us should be a multiple of 2**16 or large
* enough that it doesn't matter.
*/
/**/
int
zsleep_random(long max_us, time_t end_time)
{
long r;
time_t now = time(NULL);
/*
* Randomish backoff. Doesn't need to be fundamentally
* unpredictable, just probably unlike the value another
* exiting shell is using. On some systems the bottom 16
* bits aren't that random but the use here doesn't
* really care.
*/
r = (long)(rand() & 0xFFFF);
/*
* Turn this into a fraction of sleep_us. Again, this
* doesn't need to be particularly accurate and the base time
* is sufficient that we can do the division first and not
* worry about the range.
*/
r = (max_us >> 16) * r;
/*
* Don't sleep beyond timeout.
* Not that important as timeout is ridiculously long, but
* if there's an interface, interface to it...
*/
while (r && now + (time_t)(r / 1000000) > end_time)
r >>= 1;
if (r) /* pedantry */
return zsleep(r);
return 0;
}
/**/
int
checkrmall(char *s)
{
DIR *rmd;
int count = 0;
if (!shout)
return 1;
if (*s != '/') {
if (pwd[1])
s = zhtricat(pwd, "/", s);
else
s = dyncat("/", s);
}
const int max_count = 100;
if ((rmd = opendir(unmeta(s)))) {
int ignoredots = !isset(GLOBDOTS);
while (zreaddir(rmd, ignoredots)) {
count++;
if (count > max_count)
break;
}
closedir(rmd);
}
if (count > max_count)
fprintf(shout, "zsh: sure you want to delete more than %d files in ",
max_count);
else if (count == 1)
fprintf(shout, "zsh: sure you want to delete the only file in ");
else if (count > 0)
fprintf(shout, "zsh: sure you want to delete all %d files in ",
count);
else
fprintf(shout, "zsh: sure you want to delete all the files in ");
nicezputs(s, shout);
if(isset(RMSTARWAIT)) {
fputs("? (waiting ten seconds)", shout);
fflush(shout);
zbeep();
sleep(10);
fputc('\n', shout);
}
if (errflag)
return 0;
fputs(" [yn]? ", shout);
fflush(shout);
zbeep();
return (getquery("ny", 1) == 'y');
}
/**/
mod_export ssize_t
read_loop(int fd, char *buf, size_t len)
{
ssize_t got = len;
while (1) {
ssize_t ret = read(fd, buf, len);
if (ret == len)
break;
if (ret <= 0) {
if (ret < 0) {
if (errno == EINTR)
continue;
if (fd != SHTTY)
zwarn("read failed: %e", errno);
}
return ret;
}
buf += ret;
len -= ret;
}
return got;
}
/**/
mod_export ssize_t
write_loop(int fd, const char *buf, size_t len)
{
ssize_t wrote = len;
while (1) {
ssize_t ret = write(fd, buf, len);
if (ret == len)
break;
if (ret < 0) {
if (errno == EINTR)
continue;
if (fd != SHTTY)
zwarn("write failed: %e", errno);
return -1;
}
buf += ret;
len -= ret;
}
return wrote;
}
static int
read1char(int echo)
{
char c;
int q = queue_signal_level();
dont_queue_signals();
while (read(SHTTY, &c, 1) != 1) {
if (errno != EINTR || errflag || retflag || breaks || contflag) {
restore_queue_signals(q);
return -1;
}
}
restore_queue_signals(q);
if (echo)
write_loop(SHTTY, &c, 1);
return STOUC(c);
}
/**/
mod_export int
noquery(int purge)
{
int val = 0;
#ifdef FIONREAD
char c;
ioctl(SHTTY, FIONREAD, (char *)&val);
if (purge) {
for (; val; val--) {
if (read(SHTTY, &c, 1) != 1) {
/* Do nothing... */
}
}
}
#endif
return val;
}
/**/
int
getquery(char *valid_chars, int purge)
{
int c, d, nl = 0;
int isem = !strcmp(term, "emacs");
struct ttyinfo ti;
attachtty(mypgrp);
gettyinfo(&ti);
#ifdef HAS_TIO
ti.tio.c_lflag &= ~ECHO;
if (!isem) {
ti.tio.c_lflag &= ~ICANON;
ti.tio.c_cc[VMIN] = 1;
ti.tio.c_cc[VTIME] = 0;
}
#else
ti.sgttyb.sg_flags &= ~ECHO;
if (!isem)
ti.sgttyb.sg_flags |= CBREAK;
#endif
settyinfo(&ti);
if (noquery(purge)) {
if (!isem)
settyinfo(&shttyinfo);
write_loop(SHTTY, "n\n", 2);
return 'n';
}
while ((c = read1char(0)) >= 0) {
if (c == 'Y')
c = 'y';
else if (c == 'N')
c = 'n';
if (!valid_chars)
break;
if (c == '\n') {
c = *valid_chars;
nl = 1;
break;
}
if (strchr(valid_chars, c)) {
nl = 1;
break;
}
zbeep();
}
if (c >= 0) {
char buf = (char)c;
write_loop(SHTTY, &buf, 1);
}
if (nl)
write_loop(SHTTY, "\n", 1);
if (isem) {
if (c != '\n')
while ((d = read1char(1)) >= 0 && d != '\n');
} else {
if (c != '\n' && !valid_chars) {
#ifdef MULTIBYTE_SUPPORT
if (isset(MULTIBYTE) && c >= 0) {
/*
* No waiting for a valid character, and no draining;
* we should ensure we haven't stopped in the middle
* of a multibyte character.
*/
mbstate_t mbs;
char cc = (char)c;
memset(&mbs, 0, sizeof(mbs));
for (;;) {
size_t ret = mbrlen(&cc, 1, &mbs);
if (ret != MB_INCOMPLETE)
break;
c = read1char(1);
if (c < 0)
break;
cc = (char)c;
}
}
#endif
write_loop(SHTTY, "\n", 1);
}
}
settyinfo(&shttyinfo);
return c;
}
static int d;
static char *guess, *best;
static Patprog spckpat, spnamepat;
/**/
static void
spscan(HashNode hn, UNUSED(int scanflags))
{
int nd;
if (spckpat && pattry(spckpat, hn->nam))
return;
nd = spdist(hn->nam, guess, (int) strlen(guess) / 4 + 1);
if (nd <= d) {
best = hn->nam;
d = nd;
}
}
/* spellcheck a word */
/* fix s ; if hist is nonzero, fix the history list too */
/**/
mod_export void
spckword(char **s, int hist, int cmd, int ask)
{
char *t, *correct_ignore;
char ic = '\0';
int preflen = 0;
int autocd = cmd && isset(AUTOCD) && strcmp(*s, ".") && strcmp(*s, "..");
if ((histdone & HISTFLAG_NOEXEC) || **s == '-' || **s == '%')
return;
if (!strcmp(*s, "in"))
return;
if (!(*s)[0] || !(*s)[1])
return;
if (cmd) {
if (shfunctab->getnode(shfunctab, *s) ||
builtintab->getnode(builtintab, *s) ||
cmdnamtab->getnode(cmdnamtab, *s) ||
aliastab->getnode(aliastab, *s) ||
reswdtab->getnode(reswdtab, *s))
return;
else if (isset(HASHLISTALL)) {
cmdnamtab->filltable(cmdnamtab);
if (cmdnamtab->getnode(cmdnamtab, *s))
return;
}
}
t = *s;
if (*t == Tilde || *t == Equals || *t == String)
t++;
for (; *t; t++)
if (itok(*t))
return;
best = NULL;
for (t = *s; *t; t++)
if (*t == '/')
break;
if (**s == Tilde && !*t)
return;
if ((correct_ignore = getsparam("CORRECT_IGNORE")) != NULL) {
tokenize(correct_ignore = dupstring(correct_ignore));
remnulargs(correct_ignore);
spckpat = patcompile(correct_ignore, 0, NULL);
} else
spckpat = NULL;
if ((correct_ignore = getsparam("CORRECT_IGNORE_FILE")) != NULL) {
tokenize(correct_ignore = dupstring(correct_ignore));
remnulargs(correct_ignore);
spnamepat = patcompile(correct_ignore, 0, NULL);
} else
spnamepat = NULL;
if (**s == String && !*t) {
guess = *s + 1;
if (itype_end(guess, IIDENT, 1) == guess)
return;
ic = String;
d = 100;
scanhashtable(paramtab, 1, 0, 0, spscan, 0);
} else if (**s == Equals) {
if (*t)
return;
if (hashcmd(guess = *s + 1, pathchecked))
return;
d = 100;
ic = Equals;
scanhashtable(aliastab, 1, 0, 0, spscan, 0);
scanhashtable(cmdnamtab, 1, 0, 0, spscan, 0);
} else {
guess = *s;
if (*guess == Tilde || *guess == String) {
int ne;
ic = *guess;
if (!*++t)
return;
guess = dupstring(guess);
ne = noerrs;
noerrs = 2;
singsub(&guess);
noerrs = ne;
if (!guess)
return;
preflen = strlen(guess) - strlen(t);
}
if (access(unmeta(guess), F_OK) == 0)
return;
best = spname(guess);
if (!*t && cmd) {
if (hashcmd(guess, pathchecked))
return;
d = 100;
scanhashtable(reswdtab, 1, 0, 0, spscan, 0);
scanhashtable(aliastab, 1, 0, 0, spscan, 0);
scanhashtable(shfunctab, 1, 0, 0, spscan, 0);
scanhashtable(builtintab, 1, 0, 0, spscan, 0);
scanhashtable(cmdnamtab, 1, 0, 0, spscan, 0);
if (autocd) {
char **pp;
for (pp = cdpath; *pp; pp++) {
char bestcd[PATH_MAX + 1];
int thisdist;
/* Less than d here, instead of less than or equal *
* as used in spscan(), so that an autocd is chosen *
* only when it is better than anything so far, and *
* so we prefer directories earlier in the cdpath. */
if ((thisdist = mindist(*pp, *s, bestcd, 1)) < d) {
best = dupstring(bestcd);
d = thisdist;
}
}
}
}
}
if (errflag)
return;
if (best && (int)strlen(best) > 1 && strcmp(best, guess)) {
int x;
if (ic) {
char *u;
if (preflen) {
/* do not correct the result of an expansion */
if (strncmp(guess, best, preflen))
return;
/* replace the temporarily expanded prefix with the original */
u = (char *) zhalloc(t - *s + strlen(best + preflen) + 1);
strncpy(u, *s, t - *s);
strcpy(u + (t - *s), best + preflen);
} else {
u = (char *) zhalloc(strlen(best) + 2);
*u = '\0';
strcpy(u + 1, best);
}
best = u;
guess = *s;
*guess = *best = ztokens[ic - Pound];
}
if (ask) {
if (noquery(0)) {
x = 'n';
} else if (shout) {
char *pptbuf;
pptbuf = promptexpand(sprompt, 0, best, guess, NULL);
zputs(pptbuf, shout);
free(pptbuf);
fflush(shout);
zbeep();
x = getquery("nyae", 0);
if (cmd && x == 'n')
pathchecked = path;
} else
x = 'n';
} else
x = 'y';
if (x == 'y') {
*s = dupstring(best);
if (hist)
hwrep(best);
} else if (x == 'a') {
histdone |= HISTFLAG_NOEXEC;
} else if (x == 'e') {
histdone |= HISTFLAG_NOEXEC | HISTFLAG_RECALL;
}
if (ic)
**s = ic;
}
}
/*
* Helper for ztrftime. Called with a pointer to the length left
* in the buffer, and a new string length to decrement from that.
* Returns 0 if the new length fits, 1 otherwise. We assume a terminating
* NUL and return 1 if that doesn't fit.
*/
static int
ztrftimebuf(int *bufsizeptr, int decr)
{
if (*bufsizeptr <= decr)
return 1;
*bufsizeptr -= decr;
return 0;
}
/*
* Like the system function, this returns the number of characters
* copied, not including the terminating NUL. This may be zero
* if the string didn't fit.
*
* As an extension, try to detect an error in strftime --- typically
* not enough memory --- and return -1. Not guaranteed to be portable,
* since the strftime() interface doesn't make any guarantees about
* the state of the buffer if it returns zero.
*
* fmt is metafied, but we need to unmetafy it on the fly to
* pass into strftime / combine with the output from strftime.
* The return value in buf is not metafied.
*/
/**/
mod_export int
ztrftime(char *buf, int bufsize, char *fmt, struct tm *tm, long usec)
{
int hr12;
#ifdef HAVE_STRFTIME
int decr;
char *fmtstart;
#else
static char *astr[] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static char *estr[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"};
#endif
char *origbuf = buf;
while (*fmt) {
if (*fmt == Meta) {
int chr = fmt[1] ^ 32;
if (ztrftimebuf(&bufsize, 1))
return -1;
*buf++ = chr;
fmt += 2;
} else if (*fmt == '%') {
int strip;
int digs = 3;
#ifdef HAVE_STRFTIME
fmtstart =
#endif
fmt++;
if (*fmt == '-') {
strip = 1;
fmt++;
} else
strip = 0;
if (idigit(*fmt)) {
/* Digit --- only useful with . */
char *dstart = fmt;
char *dend = fmt+1;
while (idigit(*dend))
dend++;
if (*dend == '.') {
fmt = dend;
digs = atoi(dstart);
}
}
/*
* Assume this format will take up at least two
* characters. Not always true, but if that matters
* we are so close to the edge it's not a big deal.
* Fix up some longer cases specially when we get to them.
*/
if (ztrftimebuf(&bufsize, 2))
return -1;
#ifdef HAVE_STRFTIME
/* Our internal handling doesn't handle padding and other gnu extensions,
* so here we detect them and pass over to strftime(). We don't want
* to do this unconditionally though, as we have some extensions that
* strftime() doesn't have (%., %f, %L and %K) */
morefmt:
if (!((fmt - fmtstart == 1) || (fmt - fmtstart == 2 && strip) || *fmt == '.')) {
while (*fmt && strchr("OE^#_-0123456789", *fmt))
fmt++;
if (*fmt) {
fmt++;
goto strftimehandling;
}
}
#endif
switch (*fmt++) {
case '.':
if (ztrftimebuf(&bufsize, digs))
return -1;
if (digs > 6)
digs = 6;
if (digs < 6) {
int trunc;
for (trunc = 5 - digs; trunc; trunc--)
usec /= 10;
usec = (usec + 5) / 10;
}
sprintf(buf, "%0*ld", digs, usec);
buf += digs;
break;
case '\0':
/* Guard against premature end of string */
*buf++ = '%';
fmt--;
break;
case 'f':
strip = 1;
/* FALLTHROUGH */
case 'e':
if (tm->tm_mday > 9)
*buf++ = '0' + tm->tm_mday / 10;
else if (!strip)
*buf++ = ' ';
*buf++ = '0' + tm->tm_mday % 10;
break;
case 'K':
strip = 1;
/* FALLTHROUGH */
case 'H':
case 'k':
if (tm->tm_hour > 9)
*buf++ = '0' + tm->tm_hour / 10;
else if (!strip) {
if (fmt[-1] == 'H')
*buf++ = '0';
else
*buf++ = ' ';
}
*buf++ = '0' + tm->tm_hour % 10;
break;
case 'L':
strip = 1;
/* FALLTHROUGH */
case 'l':
hr12 = tm->tm_hour % 12;
if (hr12 == 0)
hr12 = 12;
if (hr12 > 9)
*buf++ = '1';
else if (!strip)
*buf++ = ' ';
*buf++ = '0' + (hr12 % 10);
break;
case 'd':
if (tm->tm_mday > 9 || !strip)
*buf++ = '0' + tm->tm_mday / 10;
*buf++ = '0' + tm->tm_mday % 10;
break;
case 'm':
if (tm->tm_mon > 8 || !strip)
*buf++ = '0' + (tm->tm_mon + 1) / 10;
*buf++ = '0' + (tm->tm_mon + 1) % 10;
break;
case 'M':
if (tm->tm_min > 9 || !strip)
*buf++ = '0' + tm->tm_min / 10;
*buf++ = '0' + tm->tm_min % 10;
break;
case 'S':
if (tm->tm_sec > 9 || !strip)
*buf++ = '0' + tm->tm_sec / 10;
*buf++ = '0' + tm->tm_sec % 10;
break;
case 'y':
if (tm->tm_year > 9 || !strip)
*buf++ = '0' + (tm->tm_year / 10) % 10;
*buf++ = '0' + tm->tm_year % 10;
break;
#ifndef HAVE_STRFTIME
case 'Y':
{
int year, digits, testyear;
year = tm->tm_year + 1900;
digits = 1;
testyear = year;
while (testyear > 9) {
digits++;
testyear /= 10;
}
if (ztrftimebuf(&bufsize, digits))
return -1;
sprintf(buf, "%d", year);
buf += digits;
break;
}
case 'a':
if (ztrftimebuf(&bufsize, strlen(astr[tm->tm_wday]) - 2))
return -1;
strucpy(&buf, astr[tm->tm_wday]);
break;
case 'b':
if (ztrftimebuf(&bufsize, strlen(estr[tm->tm_mon]) - 2))
return -1;
strucpy(&buf, estr[tm->tm_mon]);
break;
case 'p':
*buf++ = (tm->tm_hour > 11) ? 'p' : 'a';
*buf++ = 'm';
break;
default:
*buf++ = '%';
if (fmt[-1] != '%')
*buf++ = fmt[-1];
#else
case 'E':
case 'O':
case '^':
case '#':
case '_':
case '-':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
goto morefmt;
strftimehandling:
default:
/*
* Remember we've already allowed for two characters
* in the accounting in bufsize (but nowhere else).
*/
{
char origchar = fmt[-1];
int size = fmt - fmtstart;
char *tmp, *last;
tmp = zhalloc(size + 1);
strncpy(tmp, fmtstart, size);
last = fmt-1;
if (*last == Meta) {
/*
* This is for consistency in counting:
* a metafiable character isn't actually
* a valid strftime descriptor.
*
* Previous characters were explicitly checked,
* so can't be metafied.
*/
*last = *++fmt ^ 32;
}
tmp[size] = '\0';
*buf = '\1';
if (!strftime(buf, bufsize + 2, tmp, tm))
{
/*
* Some locales don't have strings for
* AM/PM, so empty output is valid.
*/
if (*buf || (origchar != 'p' && origchar != 'P')) {
if (*buf) {
buf[0] = '\0';
return -1;
}
return 0;
}
}
decr = strlen(buf);
buf += decr;
bufsize -= decr - 2;
}
#endif
break;
}
} else {
if (ztrftimebuf(&bufsize, 1))
return -1;
*buf++ = *fmt++;
}
}
*buf = '\0';
return buf - origbuf;
}
/**/
mod_export char *
zjoin(char **arr, int delim, int heap)
{
int len = 0;
char **s, *ret, *ptr;
for (s = arr; *s; s++)
len += strlen(*s) + 1 + (imeta(delim) ? 1 : 0);
if (!len)
return heap? "" : ztrdup("");
ptr = ret = (char *) (heap ? zhalloc(len) : zalloc(len));
for (s = arr; *s; s++) {
strucpy(&ptr, *s);
if (imeta(delim)) {
*ptr++ = Meta;
*ptr++ = delim ^ 32;
}
else
*ptr++ = delim;
}
ptr[-1 - (imeta(delim) ? 1 : 0)] = '\0';
return ret;
}
/* Split a string containing a colon separated list *
* of items into an array of strings. */
/**/
mod_export char **
colonsplit(char *s, int uniq)
{
int ct;
char *t, **ret, **ptr, **p;
for (t = s, ct = 0; *t; t++) /* count number of colons */
if (*t == ':')
ct++;
ptr = ret = (char **) zalloc(sizeof(char *) * (ct + 2));
t = s;
do {
s = t;
/* move t to point at next colon */
for (; *t && *t != ':'; t++);
if (uniq)
for (p = ret; p < ptr; p++)
if ((int)strlen(*p) == t - s && ! strncmp(*p, s, t - s))
goto cont;
*ptr = (char *) zalloc((t - s) + 1);
ztrncpy(*ptr++, s, t - s);
cont: ;
}
while (*t++);
*ptr = NULL;
return ret;
}
/**/
static int
skipwsep(char **s)
{
char *t = *s;
int i = 0;
/*
* Don't need to handle mutlibyte characters, they can't
* be IWSEP. Do need to check for metafication.
*/
while (*t && iwsep(*t == Meta ? t[1] ^ 32 : *t)) {
if (*t == Meta)
t++;
t++;
i++;
}
*s = t;
return i;
}
/*
* haven't worked out what allownull does; it's passed down from
* sepsplit but all the cases it's used are either 0 or 1 without
* a comment. it seems to be something to do with the `nulstring'
* which i think is some kind of a metafication thing, so probably
* allownull's value is associated with whether we are using
* metafied strings.
* see findsep() below for handling of `quote' argument
*/
/**/
mod_export char **
spacesplit(char *s, int allownull, int heap, int quote)
{
char *t, **ret, **ptr;
int l = sizeof(*ret) * (wordcount(s, NULL, -!allownull) + 1);
char *(*dup)(const char *) = (heap ? dupstring : ztrdup);
/* ### TODO: s/calloc/alloc/ */
ptr = ret = (char **) (heap ? hcalloc(l) : zshcalloc(l));
if (quote) {
/*
* we will be stripping quoted separators by hacking string,
* so make sure it's hackable.
*/
s = dupstring(s);
}
t = s;
skipwsep(&s);
MB_METACHARINIT();
if (*s && itype_end(s, ISEP, 1) != s)
*ptr++ = dup(allownull ? "" : nulstring);
else if (!allownull && t != s)
*ptr++ = dup("");
while (*s) {
char *iend = itype_end(s, ISEP, 1);
if (iend != s) {
s = iend;
skipwsep(&s);
}
else if (quote && *s == '\\') {
s++;
skipwsep(&s);
}
t = s;
(void)findsep(&s, NULL, quote);
if (s > t || allownull) {
*ptr = (char *) (heap ? zhalloc((s - t) + 1) :
zalloc((s - t) + 1));
ztrncpy(*ptr++, t, s - t);
} else
*ptr++ = dup(nulstring);
t = s;
skipwsep(&s);
}
if (!allownull && t != s)
*ptr++ = dup("");
*ptr = NULL;
return ret;
}
/*
* Find a separator. Return 0 if already at separator, 1 if separator
* found later, else -1. (Historical note: used to return length into
* string but this is all that is necessary and is less ambiguous with
* multibyte characters around.)
*
* *s is the string we are looking along, which will be updated
* to the point we have got to.
*
* sep is a possibly multicharacter separator to look for. If NULL,
* use normal separator characters. If *sep is NULL, split on individual
* characters.
*
* quote is a flag that '\<sep>' should not be treated as a separator.
* in this case we need to be able to strip the backslash directly
* in the string, so the calling function must have sent us something
* modifiable. currently this only works for sep == NULL. also in
* in this case only, we need to turn \\ into \.
*/
/**/
static int
findsep(char **s, char *sep, int quote)
{
/*
*/
int i, ilen;
char *t, *tt;
convchar_t c;
MB_METACHARINIT();
if (!sep) {
for (t = *s; *t; t += ilen) {
if (quote && *t == '\\') {
if (t[1] == '\\') {
chuck(t);
ilen = 1;
continue;
} else {
ilen = MB_METACHARLENCONV(t+1, &c);
if (WC_ZISTYPE(c, ISEP)) {
chuck(t);
/* then advance over new character, length ilen */
} else {
/* treat *t (backslash) as normal byte */
if (isep(*t))
break;
ilen = 1;
}
}
} else {
ilen = MB_METACHARLENCONV(t, &c);
if (WC_ZISTYPE(c, ISEP))
break;
}
}
i = (t > *s);
*s = t;
return i;
}
if (!sep[0]) {
/*
* NULL separator just means advance past first character,
* if any.
*/
if (**s) {
*s += MB_METACHARLEN(*s);
return 1;
}
return -1;
}
for (i = 0; **s; i++) {
/*
* The following works for multibyte characters by virtue of
* the fact that sep may be a string (and we don't care how
* it divides up, we need to match all of it).
*/
for (t = sep, tt = *s; *t && *tt && *t == *tt; t++, tt++);
if (!*t)
return (i > 0);
*s += MB_METACHARLEN(*s);
}
return -1;
}
/**/
char *
findword(char **s, char *sep)
{
char *r, *t;
int sl;
if (!**s)
return NULL;
if (sep) {
sl = strlen(sep);
r = *s;
while (! findsep(s, sep, 0)) {
r = *s += sl;
}
return r;
}
MB_METACHARINIT();
for (t = *s; *t; t += sl) {
convchar_t c;
sl = MB_METACHARLENCONV(t, &c);
if (!WC_ZISTYPE(c, ISEP))
break;
}
*s = t;
(void)findsep(s, sep, 0);
return t;
}
/**/
int
wordcount(char *s, char *sep, int mul)
{
int r, sl, c;
if (sep) {
r = 1;
sl = strlen(sep);
for (; (c = findsep(&s, sep, 0)) >= 0; s += sl)
if ((c || mul) && (sl || *(s + sl)))
r++;
} else {
char *t = s;
r = 0;
if (mul <= 0)
skipwsep(&s);
if ((*s && itype_end(s, ISEP, 1) != s) ||
(mul < 0 && t != s))
r++;
for (; *s; r++) {
char *ie = itype_end(s, ISEP, 1);
if (ie != s) {
s = ie;
if (mul <= 0)
skipwsep(&s);
}
(void)findsep(&s, NULL, 0);
t = s;
if (mul <= 0)
skipwsep(&s);
}
if (mul < 0 && t != s)
r++;
}
return r;
}
/**/
mod_export char *
sepjoin(char **s, char *sep, int heap)
{
char *r, *p, **t;
int l, sl;
char sepbuf[2];
if (!*s)
return heap ? "" : ztrdup("");
if (!sep) {
/* optimise common case that ifs[0] is space */
if (ifs && *ifs != ' ') {
MB_METACHARINIT();
sep = dupstrpfx(ifs, MB_METACHARLEN(ifs));
} else {
p = sep = sepbuf;
*p++ = ' ';
*p = '\0';
}
}
sl = strlen(sep);
for (t = s, l = 1 - sl; *t; l += strlen(*t) + sl, t++);
r = p = (char *) (heap ? zhalloc(l) : zalloc(l));
t = s;
while (*t) {
strucpy(&p, *t);
if (*++t)
strucpy(&p, sep);
}
*p = '\0';
return r;
}
/**/
char **
sepsplit(char *s, char *sep, int allownull, int heap)
{
int n, sl;
char *t, *tt, **r, **p;
/* Null string? Treat as empty string. */
if (s[0] == Nularg && !s[1])
s++;
if (!sep)
return spacesplit(s, allownull, heap, 0);
sl = strlen(sep);
n = wordcount(s, sep, 1);
r = p = (char **) (heap ? zhalloc((n + 1) * sizeof(char *)) :
zalloc((n + 1) * sizeof(char *)));
for (t = s; n--;) {
tt = t;
(void)findsep(&t, sep, 0);
*p = (char *) (heap ? zhalloc(t - tt + 1) :
zalloc(t - tt + 1));
strncpy(*p, tt, t - tt);
(*p)[t - tt] = '\0';
p++;
t += sl;
}
*p = NULL;
return r;
}
/* Get the definition of a shell function */
/**/
mod_export Shfunc
getshfunc(char *nam)
{
return (Shfunc) shfunctab->getnode(shfunctab, nam);
}
/*
* Call the function func to substitute string orig by setting
* the parameter reply.
* Return the array from reply, or NULL if the function returned
* non-zero status.
* The returned value comes directly from the parameter and
* so should be used before there is any chance of that
* being changed or unset.
* If arg1 is not NULL, it is used as an initial argument to
* the function, with the original string as the second argument.
*/
/**/
char **
subst_string_by_func(Shfunc func, char *arg1, char *orig)
{
int osc = sfcontext, osm = stopmsg, old_incompfunc = incompfunc;
LinkList l = newlinklist();
char **ret;
addlinknode(l, func->node.nam);
if (arg1)
addlinknode(l, arg1);
addlinknode(l, orig);
sfcontext = SFC_SUBST;
incompfunc = 0;
if (doshfunc(func, l, 1))
ret = NULL;
else
ret = getaparam("reply");
sfcontext = osc;
stopmsg = osm;
incompfunc = old_incompfunc;
return ret;
}
/**
* Front end to subst_string_by_func to use hook-like logic.
* name can refer to a function, and name + "_hook" can refer
* to an array containing a list of functions. The functions
* are tried in order until one returns success.
*/
/**/
char **
subst_string_by_hook(char *name, char *arg1, char *orig)
{
Shfunc func;
char **ret = NULL;
if ((func = getshfunc(name))) {
ret = subst_string_by_func(func, arg1, orig);
}
if (!ret) {
char **arrptr;
int namlen = strlen(name);
VARARR(char, arrnam, namlen + HOOK_SUFFIX_LEN);
memcpy(arrnam, name, namlen);
memcpy(arrnam + namlen, HOOK_SUFFIX, HOOK_SUFFIX_LEN);
if ((arrptr = getaparam(arrnam))) {
/* Guard against internal modification of the array */
arrptr = arrdup(arrptr);
for (; *arrptr; arrptr++) {
if ((func = getshfunc(*arrptr))) {
ret = subst_string_by_func(func, arg1, orig);
if (ret)
break;
}
}
}
}
return ret;
}
/**/
mod_export char **
mkarray(char *s)
{
char **t = (char **) zalloc((s) ? (2 * sizeof s) : (sizeof s));
if ((*t = s))
t[1] = NULL;
return t;
}
/**/
mod_export char **
hmkarray(char *s)
{
char **t = (char **) zhalloc((s) ? (2 * sizeof s) : (sizeof s));
if ((*t = s))
t[1] = NULL;
return t;
}
/**/
mod_export void
zbeep(void)
{
char *vb;
queue_signals();
if ((vb = getsparam_u("ZBEEP"))) {
int len;
vb = getkeystring(vb, &len, GETKEYS_BINDKEY, NULL);
write_loop(SHTTY, vb, len);
} else if (isset(BEEP))
write_loop(SHTTY, "\07", 1);
unqueue_signals();
}
/**/
mod_export void
freearray(char **s)
{
char **t = s;
DPUTS(!s, "freearray() with zero argument");
while (*s)
zsfree(*s++);
free(t);
}
/**/
int
equalsplit(char *s, char **t)
{
for (; *s && *s != '='; s++);
if (*s == '=') {
*s++ = '\0';
*t = s;
return 1;
}
return 0;
}
/* the ztypes table */
/**/
mod_export short int typtab[256];
static int typtab_flags = 0;
/* initialize the ztypes table */
/**/
void
inittyptab(void)
{
int t0;
char *s;
if (!(typtab_flags & ZTF_INIT)) {
typtab_flags = ZTF_INIT;
if (interact && isset(SHINSTDIN))
typtab_flags |= ZTF_INTERACT;
}
queue_signals();
memset(typtab, 0, sizeof(typtab));
for (t0 = 0; t0 != 32; t0++)
typtab[t0] = typtab[t0 + 128] = ICNTRL;
typtab[127] = ICNTRL;
for (t0 = '0'; t0 <= '9'; t0++)
typtab[t0] = IDIGIT | IALNUM | IWORD | IIDENT | IUSER;
for (t0 = 'a'; t0 <= 'z'; t0++)
typtab[t0] = typtab[t0 - 'a' + 'A'] = IALPHA | IALNUM | IIDENT | IUSER | IWORD;
#ifndef MULTIBYTE_SUPPORT
/*
* This really doesn't seem to me the right thing to do when
* we have multibyte character support... it was a hack to assume
* eight bit characters `worked' for some values of work before
* we could test for them properly. I'm not 100% convinced
* having IIDENT here is a good idea at all, but this code
* should disappear into history...
*/
for (t0 = 0240; t0 != 0400; t0++)
typtab[t0] = IALPHA | IALNUM | IIDENT | IUSER | IWORD;
#endif
/* typtab['.'] |= IIDENT; */ /* Allow '.' in variable names - broken */
typtab['_'] = IIDENT | IUSER;
typtab['-'] = typtab['.'] = typtab[STOUC(Dash)] = IUSER;
typtab[' '] |= IBLANK | INBLANK;
typtab['\t'] |= IBLANK | INBLANK;
typtab['\n'] |= INBLANK;
typtab['\0'] |= IMETA;
typtab[STOUC(Meta) ] |= IMETA;
typtab[STOUC(Marker)] |= IMETA;
for (t0 = (int)STOUC(Pound); t0 <= (int)STOUC(LAST_NORMAL_TOK); t0++)
typtab[t0] |= ITOK | IMETA;
for (t0 = (int)STOUC(Snull); t0 <= (int)STOUC(Nularg); t0++)
typtab[t0] |= ITOK | IMETA | INULL;
for (s = ifs ? ifs : EMULATION(EMULATE_KSH|EMULATE_SH) ?
DEFAULT_IFS_SH : DEFAULT_IFS; *s; s++) {
int c = STOUC(*s == Meta ? *++s ^ 32 : *s);
#ifdef MULTIBYTE_SUPPORT
if (!isascii(c)) {
/* see comment for wordchars below */
continue;
}
#endif
if (inblank(c)) {
if (s[1] == c)
s++;
else
typtab[c] |= IWSEP;
}
typtab[c] |= ISEP;
}
for (s = wordchars ? wordchars : DEFAULT_WORDCHARS; *s; s++) {
int c = STOUC(*s == Meta ? *++s ^ 32 : *s);
#ifdef MULTIBYTE_SUPPORT
if (!isascii(c)) {
/*
* If we have support for multibyte characters, we don't
* handle non-ASCII characters here; instead, we turn
* wordchars into a wide character array.
* (We may actually have a single-byte 8-bit character set,
* but it works the same way.)
*/
continue;
}
#endif
typtab[c] |= IWORD;
}
#ifdef MULTIBYTE_SUPPORT
set_widearray(wordchars, &wordchars_wide);
set_widearray(ifs ? ifs : EMULATION(EMULATE_KSH|EMULATE_SH) ?
DEFAULT_IFS_SH : DEFAULT_IFS, &ifs_wide);
#endif
for (s = SPECCHARS; *s; s++)
typtab[STOUC(*s)] |= ISPECIAL;
if (typtab_flags & ZTF_SP_COMMA)
typtab[STOUC(',')] |= ISPECIAL;
if (isset(BANGHIST) && bangchar && (typtab_flags & ZTF_INTERACT)) {
typtab_flags |= ZTF_BANGCHAR;
typtab[bangchar] |= ISPECIAL;
} else
typtab_flags &= ~ZTF_BANGCHAR;
for (s = PATCHARS; *s; s++)
typtab[STOUC(*s)] |= IPATTERN;
unqueue_signals();
}
/**/
mod_export void
makecommaspecial(int yesno)
{
if (yesno != 0) {
typtab_flags |= ZTF_SP_COMMA;
typtab[STOUC(',')] |= ISPECIAL;
} else {
typtab_flags &= ~ZTF_SP_COMMA;
typtab[STOUC(',')] &= ~ISPECIAL;
}
}
/**/
mod_export void
makebangspecial(int yesno)
{
/* Name and call signature for congruence with makecommaspecial(),
* but in this case when yesno is nonzero we defer to the state
* saved by inittyptab().
*/
if (yesno == 0) {
typtab[bangchar] &= ~ISPECIAL;
} else if (typtab_flags & ZTF_BANGCHAR) {
typtab[bangchar] |= ISPECIAL;
}
}
/**/
#ifdef MULTIBYTE_SUPPORT
/* A wide-character version of the iblank() macro. */
/**/
mod_export int
wcsiblank(wint_t wc)
{
if (iswspace(wc) && wc != L'\n')
return 1;
return 0;
}
/*
* zistype macro extended to support wide characters.
* Works for IIDENT, IWORD, IALNUM, ISEP.
* We don't need this for IWSEP because that only applies to
* a fixed set of ASCII characters.
* Note here that use of multibyte mode is not tested:
* that's because for ZLE this is unconditional,
* not dependent on the option. The caller must decide.
*/
/**/
mod_export int
wcsitype(wchar_t c, int itype)
{
int len;
mbstate_t mbs;
VARARR(char, outstr, MB_CUR_MAX);
if (!isset(MULTIBYTE))
return zistype(c, itype);
/*
* Strategy: the shell requires that the multibyte representation
* be an extension of ASCII. So see if converting the character
* produces an ASCII character. If it does, use zistype on that.
* If it doesn't, use iswalnum on the original character.
* If that fails, resort to the appropriate wide character array.
*/
memset(&mbs, 0, sizeof(mbs));
len = wcrtomb(outstr, c, &mbs);
if (len == 0) {
/* NULL is special */
return zistype(0, itype);
} else if (len == 1 && isascii(outstr[0])) {
return zistype(outstr[0], itype);
} else {
switch (itype) {
case IIDENT:
if (!isset(POSIXIDENTIFIERS))
return 0;
return iswalnum(c);
case IWORD:
if (iswalnum(c))
return 1;
/*
* If we are handling combining characters, any punctuation
* characters with zero width needs to be considered part of
* a word. If we are not handling combining characters then
* logically they are still part of the word, even if they
* don't get displayed properly, so always do this.
*/
if (IS_COMBINING(c))
return 1;
return !!wmemchr(wordchars_wide.chars, c, wordchars_wide.len);
case ISEP:
return !!wmemchr(ifs_wide.chars, c, ifs_wide.len);
default:
return iswalnum(c);
}
}
}
/**/
#endif
/*
* Find the end of a set of characters in the set specified by itype;
* one of IALNUM, IIDENT, IWORD or IUSER. For non-ASCII characters, we assume
* alphanumerics are part of the set, with the exception that
* identifiers are not treated that way if POSIXIDENTIFIERS is set.
*
* See notes above for identifiers.
* Returns the same pointer as passed if not on an identifier character.
* If "once" is set, just test the first character, i.e. (outptr !=
* inptr) tests whether the first character is valid in an identifier.
*
* Currently this is only called with itype IIDENT, IUSER or ISEP.
*/
/**/
mod_export char *
itype_end(const char *ptr, int itype, int once)
{
#ifdef MULTIBYTE_SUPPORT
if (isset(MULTIBYTE) &&
(itype != IIDENT || !isset(POSIXIDENTIFIERS))) {
mb_charinit();
while (*ptr) {
int len;
if (itok(*ptr)) {
/* Not untokenised yet --- can happen in raw command line */
len = 1;
if (!zistype(*ptr,itype))
break;
} else {
wint_t wc;
len = mb_metacharlenconv(ptr, &wc);
if (!len)
break;
if (wc == WEOF) {
/* invalid, treat as single character */
int chr = STOUC(*ptr == Meta ? ptr[1] ^ 32 : *ptr);
/* in this case non-ASCII characters can't match */
if (chr > 127 || !zistype(chr,itype))
break;
} else if (len == 1 && isascii(*ptr)) {
/* ASCII: can't be metafied, use standard test */
if (!zistype(*ptr,itype))
break;
} else {
/*
* Valid non-ASCII character.
*/
switch (itype) {
case IWORD:
if (!iswalnum(wc) &&
!wmemchr(wordchars_wide.chars, wc,
wordchars_wide.len))
return (char *)ptr;
break;
case ISEP:
if (!wmemchr(ifs_wide.chars, wc, ifs_wide.len))
return (char *)ptr;
break;
default:
if (!iswalnum(wc))
return (char *)ptr;
}
}
}
ptr += len;
if (once)
break;
}
} else
#endif
for (;;) {
int chr = STOUC(*ptr == Meta ? ptr[1] ^ 32 : *ptr);
if (!zistype(chr,itype))
break;
ptr += (*ptr == Meta) ? 2 : 1;
if (once)
break;
}
/*
* Nasty. The first argument is const char * because we
* don't modify it here. However, we really want to pass
* back the same type as was passed down, to allow idioms like
* p = itype_end(p, IIDENT, 0);
* So returning a const char * isn't really the right thing to do.
* Without having two different functions the following seems
* to be the best we can do.
*/
return (char *)ptr;
}
/**/
mod_export char **
arrdup(char **s)
{
char **x, **y;
y = x = (char **) zhalloc(sizeof(char *) * (arrlen(s) + 1));
while ((*x++ = dupstring(*s++)));
return y;
}
/* Duplicate at most max elements of the array s with heap memory */
/**/
mod_export char **
arrdup_max(char **s, unsigned max)
{
char **x, **y, **send;
int len = 0;
if (max)
len = arrlen(s);
/* Limit has sense only if not equal to len */
if (max > len)
max = len;
y = x = (char **) zhalloc(sizeof(char *) * (max + 1));
send = s + max;
while (s < send)
*x++ = dupstring(*s++);
*x = NULL;
return y;
}
/**/
mod_export char **
zarrdup(char **s)
{
char **x, **y;
y = x = (char **) zalloc(sizeof(char *) * (arrlen(s) + 1));
while ((*x++ = ztrdup(*s++)));
return y;
}
/**/
#ifdef MULTIBYTE_SUPPORT
/**/
mod_export wchar_t **
wcs_zarrdup(wchar_t **s)
{
wchar_t **x, **y;
y = x = (wchar_t **) zalloc(sizeof(wchar_t *) * (arrlen((char **)s) + 1));
while ((*x++ = wcs_ztrdup(*s++)));
return y;
}
/**/
#endif /* MULTIBYTE_SUPPORT */
/**/
static char *
spname(char *oldname)
{
char *p, spnameguess[PATH_MAX + 1], spnamebest[PATH_MAX + 1];
static char newname[PATH_MAX + 1];
char *new = newname, *old = oldname;
int bestdist = 0, thisdist, thresh, maxthresh = 0;
/* This loop corrects each directory component of the path, stopping *
* when any correction distance would exceed the distance threshold. *
* NULL is returned only if the first component cannot be corrected; *
* otherwise a copy of oldname with a corrected prefix is returned. *
* Rationale for this, if there ever was any, has been forgotten. */
for (;;) {
while (*old == '/') {
if ((new - newname) >= (sizeof(newname)-1))
return NULL;
*new++ = *old++;
}
*new = '\0';
if (*old == '\0')
return newname;
p = spnameguess;
for (; *old != '/' && *old != '\0'; old++)
if (p < spnameguess + PATH_MAX)
*p++ = *old;
*p = '\0';
/* Every component is allowed a single distance 2 correction or two *
* distance 1 corrections. Longer ones get additional corrections. */
thresh = (int)(p - spnameguess) / 4 + 1;
if (thresh < 3)
thresh = 3;
else if (thresh > 100)
thresh = 100;
thisdist = mindist(newname, spnameguess, spnamebest, *old == '/');
if (thisdist >= thresh) {
/* The next test is always true, except for the first path *
* component. We could initialize bestdist to some large *
* constant instead, and then compare to that constant here, *
* because an invariant is that we've never exceeded the *
* threshold for any component so far; but I think that looks *
* odd to the human reader, and we may make use of the total *
* distance for all corrections at some point in the future. */
if (bestdist < maxthresh) {
strcpy(new, spnameguess);
strcat(new, old);
return newname;
} else
return NULL;
} else {
maxthresh = bestdist + thresh;
bestdist += thisdist;
}
for (p = spnamebest; (*new = *p++);)
new++;
}
}
/**/
static int
mindist(char *dir, char *mindistguess, char *mindistbest, int wantdir)
{
int mindistd, nd;
DIR *dd;
char *fn;
char *buf;
struct stat st;
size_t dirlen;
if (dir[0] == '\0')
dir = ".";
mindistd = 100;
if (!(buf = zalloc((dirlen = strlen(dir)) + strlen(mindistguess) + 2)))
return 0;
sprintf(buf, "%s/%s", dir, mindistguess);
if (stat(unmeta(buf), &st) == 0 && (!wantdir || S_ISDIR(st.st_mode))) {
strcpy(mindistbest, mindistguess);
free(buf);
return 0;
}
if ((dd = opendir(unmeta(dir)))) {
while ((fn = zreaddir(dd, 0))) {
if (spnamepat && pattry(spnamepat, fn))
continue;
nd = spdist(fn, mindistguess,
(int)strlen(mindistguess) / 4 + 1);
if (nd <= mindistd) {
if (wantdir) {
if (!(buf = zrealloc(buf, dirlen + strlen(fn) + 2)))
continue;
sprintf(buf, "%s/%s", dir, fn);
if (stat(unmeta(buf), &st) != 0 || !S_ISDIR(st.st_mode))
continue;
}
strcpy(mindistbest, fn);
mindistd = nd;
if (mindistd == 0)
break;
}
}
closedir(dd);
}
free(buf);
return mindistd;
}
/**/
static int
spdist(char *s, char *t, int thresh)
{
/* TODO: Correction for non-ASCII and multibyte-input keyboards. */
char *p, *q;
const char qwertykeymap[] =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t1234567890-=\t\
\tqwertyuiop[]\t\
\tasdfghjkl;'\n\t\
\tzxcvbnm,./\t\t\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t!@#$%^&*()_+\t\
\tQWERTYUIOP{}\t\
\tASDFGHJKL:\"\n\t\
\tZXCVBNM<>?\n\n\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
const char dvorakkeymap[] =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t1234567890[]\t\
\t',.pyfgcrl/=\t\
\taoeuidhtns-\n\t\
\t;qjkxbmwvz\t\t\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t!@#$%^&*(){}\t\
\t\"<>PYFGCRL?+\t\
\tAOEUIDHTNS_\n\t\
\t:QJKXBMWVZ\n\n\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
const char *keymap;
if ( isset( DVORAK ) )
keymap = dvorakkeymap;
else
keymap = qwertykeymap;
if (!strcmp(s, t))
return 0;
/* any number of upper/lower mistakes allowed (dist = 1) */
for (p = s, q = t; *p && tulower(*p) == tulower(*q); p++, q++);
if (!*p && !*q)
return 1;
if (!thresh)
return 200;
for (p = s, q = t; *p && *q; p++, q++)
if (*p == *q)
continue; /* don't consider "aa" transposed, ash */
else if (p[1] == q[0] && q[1] == p[0]) /* transpositions */
return spdist(p + 2, q + 2, thresh - 1) + 1;
else if (p[1] == q[0]) /* missing letter */
return spdist(p + 1, q + 0, thresh - 1) + 2;
else if (p[0] == q[1]) /* missing letter */
return spdist(p + 0, q + 1, thresh - 1) + 2;
else if (*p != *q)
break;
if ((!*p && strlen(q) == 1) || (!*q && strlen(p) == 1))
return 2;
for (p = s, q = t; *p && *q; p++, q++)
if (p[0] != q[0] && p[1] == q[1]) {
int t0;
char *z;
/* mistyped letter */
if (!(z = strchr(keymap, p[0])) || *z == '\n' || *z == '\t')
return spdist(p + 1, q + 1, thresh - 1) + 1;
t0 = z - keymap;
if (*q == keymap[t0 - 15] || *q == keymap[t0 - 14] ||
*q == keymap[t0 - 13] ||
*q == keymap[t0 - 1] || *q == keymap[t0 + 1] ||
*q == keymap[t0 + 13] || *q == keymap[t0 + 14] ||
*q == keymap[t0 + 15])
return spdist(p + 1, q + 1, thresh - 1) + 2;
return 200;
} else if (*p != *q)
break;
return 200;
}
/* set cbreak mode, or the equivalent */
/**/
void
setcbreak(void)
{
struct ttyinfo ti;
ti = shttyinfo;
#ifdef HAS_TIO
ti.tio.c_lflag &= ~ICANON;
ti.tio.c_cc[VMIN] = 1;
ti.tio.c_cc[VTIME] = 0;
#else
ti.sgttyb.sg_flags |= CBREAK;
#endif
settyinfo(&ti);
}
/* give the tty to some process */
/**/
mod_export void
attachtty(pid_t pgrp)
{
static int ep = 0;
if (jobbing && interact) {
#ifdef HAVE_TCSETPGRP
if (SHTTY != -1 && tcsetpgrp(SHTTY, pgrp) == -1 && !ep)
#else
# if ardent
if (SHTTY != -1 && setpgrp() == -1 && !ep)
# else
int arg = pgrp;
if (SHTTY != -1 && ioctl(SHTTY, TIOCSPGRP, &arg) == -1 && !ep)
# endif
#endif
{
if (pgrp != mypgrp && kill(-pgrp, 0) == -1)
attachtty(mypgrp);
else {
if (errno != ENOTTY)
{
zwarn("can't set tty pgrp: %e", errno);
fflush(stderr);
}
opts[MONITOR] = 0;
ep = 1;
}
}
}
}
/* get the process group associated with the tty */
/**/
pid_t
gettygrp(void)
{
pid_t arg;
if (SHTTY == -1)
return -1;
#ifdef HAVE_TCSETPGRP
arg = tcgetpgrp(SHTTY);
#else
ioctl(SHTTY, TIOCGPGRP, &arg);
#endif
return arg;
}
/* Escape tokens and null characters. Buf is the string which should be *
* escaped. len is the length of the string. If len is -1, buf should be *
* null terminated. If len is non-negative and the third parameter is not *
* META_DUP, buf should point to an at least len+1 long memory area. The *
* return value points to the quoted string. If the given string does not *
* contain any special character which should be quoted and the third *
* parameter is not META_(HEAP|)DUP, buf is returned unchanged (a *
* terminating null character is appended to buf if necessary). Otherwise *
* the third `heap' argument determines the method used to allocate space *
* for the result. It can have the following values: *
* META_REALLOC: use zrealloc on buf *
* META_HREALLOC: use hrealloc on buf *
* META_USEHEAP: get memory from the heap. This leaves buf unchanged. *
* META_NOALLOC: buf points to a memory area which is long enough to hold *
* the quoted form, just quote it and return buf. *
* META_STATIC: store the quoted string in a static area. The original *
* string should be at most PATH_MAX long. *
* META_ALLOC: allocate memory for the new string with zalloc(). *
* META_DUP: leave buf unchanged and allocate space for the return *
* value even if buf does not contains special characters *
* META_HEAPDUP: same as META_DUP, but uses the heap */
/**/
mod_export char *
metafy(char *buf, int len, int heap)
{
int meta = 0;
char *t, *p, *e;
static char mbuf[PATH_MAX*2+1];
if (len == -1) {
for (e = buf, len = 0; *e; len++)
if (imeta(*e++))
meta++;
} else
for (e = buf; e < buf + len;)
if (imeta(*e++))
meta++;
if (meta || heap == META_DUP || heap == META_HEAPDUP) {
switch (heap) {
case META_REALLOC:
buf = zrealloc(buf, len + meta + 1);
break;
case META_HREALLOC:
buf = hrealloc(buf, len, len + meta + 1);
break;
case META_ALLOC:
case META_DUP:
buf = memcpy(zalloc(len + meta + 1), buf, len);
break;
case META_USEHEAP:
case META_HEAPDUP:
buf = memcpy(zhalloc(len + meta + 1), buf, len);
break;
case META_STATIC:
#ifdef DEBUG
if (len > PATH_MAX) {
fprintf(stderr, "BUG: len = %d > PATH_MAX in metafy\n", len);
fflush(stderr);
}
#endif
buf = memcpy(mbuf, buf, len);
break;
#ifdef DEBUG
case META_NOALLOC:
break;
default:
fprintf(stderr, "BUG: metafy called with invalid heap value\n");
fflush(stderr);
break;
#endif
}
p = buf + len;
e = t = buf + len + meta;
while (meta) {
if (imeta(*--t = *--p)) {
*t-- ^= 32;
*t = Meta;
meta--;
}
}
}
*e = '\0';
return buf;
}
/*
* Duplicate a string, metafying it as we go.
*
* Typically, this is used only for strings imported from outside
* zsh, as strings internally are either already metafied or passed
* around with an associated length.
*/
/**/
mod_export char *
ztrdup_metafy(const char *s)
{
/* To mimic ztrdup() behaviour */
if (!s)
return NULL;
/*
* metafy() does lots of different things, so the pointer
* isn't const. Using it with META_DUP should be safe.
*/
return metafy((char *)s, -1, META_DUP);
}
/*
* Take a null-terminated, metafied string in s into a literal
* representation by converting in place. The length is in *len
* len is non-NULL; if len is NULL, you don't know the length of
* the final string, but if it's to be supplied to some system
* routine that always uses NULL termination, such as a filename
* interpreter, that doesn't matter. Note the NULL termination
* is always copied for purposes of that kind.
*/
/**/
mod_export char *
unmetafy(char *s, int *len)
{
char *p, *t;
for (p = s; *p && *p != Meta; p++);
for (t = p; (*t = *p++);)
if (*t++ == Meta && *p)
t[-1] = *p++ ^ 32;
if (len)
*len = t - s;
return s;
}
/* Return the character length of a metafied substring, given the *
* unmetafied substring length. */
/**/
mod_export int
metalen(const char *s, int len)
{
int mlen = len;
while (len--) {
if (*s++ == Meta) {
mlen++;
s++;
}
}
return mlen;
}
/*
* This function converts a zsh internal string to a form which can be
* passed to a system call as a filename. The result is stored in a
* single static area, sized to fit. If there is no Meta character
* the original string is returned.
*/
/**/
mod_export char *
unmeta(const char *file_name)
{
static char *fn;
static int sz;
char *p;
const char *t;
int newsz, meta;
if (!file_name)
return NULL;
meta = 0;
for (t = file_name; *t; t++) {
if (*t == Meta)
meta = 1;
}
if (!meta) {
/*
* don't need allocation... free if it's long, see below
*/
if (sz > 4 * PATH_MAX) {
zfree(fn, sz);
fn = NULL;
sz = 0;
}
return (char *) file_name;
}
newsz = (t - file_name) + 1;
/*
* Optimisation: don't resize if we don't have to.
* We need a new allocation if
* - nothing was allocated before
* - the new string is larger than the old one
* - the old string was larger than an arbitrary limit but the
* new string isn't so that we free up significant space by resizing.
*/
if (!fn || newsz > sz || (sz > 4 * PATH_MAX && newsz <= 4 * PATH_MAX))
{
if (fn)
zfree(fn, sz);
sz = newsz;
fn = (char *)zalloc(sz);
if (!fn) {
sz = 0;
/*
* will quite likely crash in the caller anyway...
*/
return NULL;
}
}
for (t = file_name, p = fn; *t; p++)
if ((*p = *t++) == Meta && *t)
*p = *t++ ^ 32;
*p = '\0';
return fn;
}
/*
* Unmetafy and compare two strings, comparing unsigned character values.
* "a\0" sorts after "a".
*
* Currently this is only used in hash table sorting, where the
* keys are names of hash nodes and where we don't use strcoll();
* it's not clear if that's right but it does guarantee the ordering
* of shell structures on output.
*
* As we don't use strcoll(), it seems overkill to convert multibyte
* characters to wide characters for comparison every time. In the case
* of UTF-8, Unicode ordering is preserved when sorted raw, and for
* other character sets we rely on an extension of ASCII so the result,
* while it may not be correct, is at least rational.
*/
/**/
int
ztrcmp(char const *s1, char const *s2)
{
int c1, c2;
while(*s1 && *s1 == *s2) {
s1++;
s2++;
}
if(!(c1 = *s1))
c1 = -1;
else if(c1 == STOUC(Meta))
c1 = *++s1 ^ 32;
if(!(c2 = *s2))
c2 = -1;
else if(c2 == STOUC(Meta))
c2 = *++s2 ^ 32;
if(c1 == c2)
return 0;
else if(c1 < c2)
return -1;
else
return 1;
}
/* Return the unmetafied length of a metafied string. */
/**/
mod_export int
ztrlen(char const *s)
{
int l;
for (l = 0; *s; l++) {
if (*s++ == Meta) {
#ifdef DEBUG
if (! *s) {
fprintf(stderr, "BUG: unexpected end of string in ztrlen()\n");
break;
} else
#endif
s++;
}
}
return l;
}
#ifndef MULTIBYTE_SUPPORT
/*
* ztrlen() but with explicit end point for non-null-terminated
* segments. eptr may not be NULL.
*/
/**/
mod_export int
ztrlenend(char const *s, char const *eptr)
{
int l;
for (l = 0; s < eptr; l++) {
if (*s++ == Meta) {
#ifdef DEBUG
if (! *s) {
fprintf(stderr,
"BUG: unexpected end of string in ztrlenend()\n");
break;
} else
#endif
s++;
}
}
return l;
}
#endif /* MULTIBYTE_SUPPORT */
/* Subtract two pointers in a metafied string. */
/**/
mod_export int
ztrsub(char const *t, char const *s)
{
int l = t - s;
while (s != t) {
if (*s++ == Meta) {
#ifdef DEBUG
if (! *s || s == t)
fprintf(stderr, "BUG: substring ends in the middle of a metachar in ztrsub()\n");
else
#endif
s++;
l--;
}
}
return l;
}
/**/
mod_export char *
zreaddir(DIR *dir, int ignoredots)
{
struct dirent *de;
#if defined(HAVE_ICONV) && defined(__APPLE__)
static iconv_t conv_ds = (iconv_t)0;
static char *conv_name = 0;
char *conv_name_ptr, *orig_name_ptr;
size_t conv_name_len, orig_name_len;
#endif
do {
de = readdir(dir);
if(!de)
return NULL;
} while(ignoredots && de->d_name[0] == '.' &&
(!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])));
#if defined(HAVE_ICONV) && defined(__APPLE__)
if (!conv_ds)
conv_ds = iconv_open("UTF-8", "UTF-8-MAC");
if (conv_ds != (iconv_t)(-1)) {
/* Force initial state in case re-using conv_ds */
(void) iconv(conv_ds, 0, &orig_name_len, 0, &conv_name_len);
orig_name_ptr = de->d_name;
orig_name_len = strlen(de->d_name);
conv_name = zrealloc(conv_name, orig_name_len+1);
conv_name_ptr = conv_name;
conv_name_len = orig_name_len;
if (iconv(conv_ds,
&orig_name_ptr, &orig_name_len,
&conv_name_ptr, &conv_name_len) != (size_t)(-1) &&
orig_name_len == 0) {
/* Completely converted, metafy and return */
*conv_name_ptr = '\0';
return metafy(conv_name, -1, META_STATIC);
}
/* Error, or conversion incomplete, keep the original name */
}
#endif
return metafy(de->d_name, -1, META_STATIC);
}
/* Unmetafy and output a string. Tokens are skipped. */
/**/
mod_export int
zputs(char const *s, FILE *stream)
{
int c;
while (*s) {
if (*s == Meta)
c = *++s ^ 32;
else if(itok(*s)) {
s++;
continue;
} else
c = *s;
s++;
if (fputc(c, stream) < 0)
return EOF;
}
return 0;
}
#ifndef MULTIBYTE_SUPPORT
/* Create a visibly-represented duplicate of a string. */
/**/
mod_export char *
nicedup(char const *s, int heap)
{
int c, len = strlen(s) * 5 + 1;
VARARR(char, buf, len);
char *p = buf, *n;
while ((c = *s++)) {
if (itok(c)) {
if (c <= Comma)
c = ztokens[c - Pound];
else
continue;
}
if (c == Meta)
c = *s++ ^ 32;
/* The result here is metafied */
n = nicechar(c);
while(*n)
*p++ = *n++;
}
*p = '\0';
return heap ? dupstring(buf) : ztrdup(buf);
}
#endif
/**/
mod_export char *
nicedupstring(char const *s)
{
return nicedup(s, 1);
}
#ifndef MULTIBYTE_SUPPORT
/* Unmetafy and output a string, displaying special characters readably. */
/**/
mod_export int
nicezputs(char const *s, FILE *stream)
{
int c;
while ((c = *s++)) {
if (itok(c)) {
if (c <= Comma)
c = ztokens[c - Pound];
else
continue;
}
if (c == Meta)
c = *s++ ^ 32;
if(zputs(nicechar(c), stream) < 0)
return EOF;
}
return 0;
}
/* Return the length of the visible representation of a metafied string. */
/**/
mod_export size_t
niceztrlen(char const *s)
{
size_t l = 0;
int c;
while ((c = *s++)) {
if (itok(c)) {
if (c <= Comma)
c = ztokens[c - Pound];
else
continue;
}
if (c == Meta)
c = *s++ ^ 32;
l += strlen(nicechar(c));
}
return l;
}
#endif
/**/
#ifdef MULTIBYTE_SUPPORT
/*
* Version of both nicezputs() and niceztrlen() for use with multibyte
* characters. Input is a metafied string; output is the screen width of
* the string.
*
* If the FILE * is not NULL, output to that, too.
*
* If outstrp is not NULL, set *outstrp to a zalloc'd version of
* the output (still metafied).
*
* If flags contains NICEFLAG_HEAP, use the heap for *outstrp, else
* zalloc.
* If flags contsins NICEFLAG_QUOTE, the output is going to be within
* $'...', so quote "'" with a backslash.
*/
/**/
mod_export size_t
mb_niceformat(const char *s, FILE *stream, char **outstrp, int flags)
{
size_t l = 0, newl;
int umlen, outalloc, outleft, eol = 0;
wchar_t c;
char *ums, *ptr, *fmt, *outstr, *outptr;
mbstate_t mbs;
if (outstrp) {
outleft = outalloc = 5 * strlen(s);
outptr = outstr = zalloc(outalloc);
} else {
outleft = outalloc = 0;
outptr = outstr = NULL;
}
ums = ztrdup(s);
/*
* is this necessary at this point? niceztrlen does this
* but it's used in lots of places. however, one day this may
* be, too.
*/
untokenize(ums);
ptr = unmetafy(ums, ¨en);
memset(&mbs, 0, sizeof mbs);
while (umlen > 0) {
size_t cnt = eol ? MB_INVALID : mbrtowc(&c, ptr, umlen, &mbs);
switch (cnt) {
case MB_INCOMPLETE:
eol = 1;
/* FALL THROUGH */
case MB_INVALID:
/* The byte didn't convert, so output it as a \M-... sequence. */
fmt = nicechar_sel(*ptr, flags & NICEFLAG_QUOTE);
newl = strlen(fmt);
cnt = 1;
/* Get mbs out of its undefined state. */
memset(&mbs, 0, sizeof mbs);
break;
case 0:
/* Careful: converting '\0' returns 0, but a '\0' is a
* real character for us, so we should consume 1 byte. */
cnt = 1;
/* FALL THROUGH */
default:
if (c == L'\'' && (flags & NICEFLAG_QUOTE)) {
fmt = "\\'";
newl = 2;
}
else
fmt = wcs_nicechar_sel(c, &newl, NULL, flags & NICEFLAG_QUOTE);
break;
}
umlen -= cnt;
ptr += cnt;
l += newl;
if (stream)
zputs(fmt, stream);
if (outstr) {
/* Append to output string */
int outlen = strlen(fmt);
if (outlen >= outleft) {
/* Reallocate to twice the length */
int outoffset = outptr - outstr;
outleft += outalloc;
outalloc *= 2;
outstr = zrealloc(outstr, outalloc);
outptr = outstr + outoffset;
}
memcpy(outptr, fmt, outlen);
/* Update start position */
outptr += outlen;
/* Update available bytes */
outleft -= outlen;
}
}
free(ums);
if (outstrp) {
*outptr = '\0';
/* Use more efficient storage for returned string */
if (flags & NICEFLAG_NODUP)
*outstrp = outstr;
else {
*outstrp = (flags & NICEFLAG_HEAP) ? dupstring(outstr) :
ztrdup(outstr);
free(outstr);
}
}
return l;
}
/*
* Return 1 if mb_niceformat() would reformat this string, else 0.
*/
/**/
mod_export int
is_mb_niceformat(const char *s)
{
int umlen, eol = 0, ret = 0;
wchar_t c;
char *ums, *ptr;
mbstate_t mbs;
ums = ztrdup(s);
untokenize(ums);
ptr = unmetafy(ums, ¨en);
memset(&mbs, 0, sizeof mbs);
while (umlen > 0) {
size_t cnt = eol ? MB_INVALID : mbrtowc(&c, ptr, umlen, &mbs);
switch (cnt) {
case MB_INCOMPLETE:
eol = 1;
/* FALL THROUGH */
case MB_INVALID:
/* The byte didn't convert, so output it as a \M-... sequence. */
if (is_nicechar(*ptr)) {
ret = 1;
break;
}
cnt = 1;
/* Get mbs out of its undefined state. */
memset(&mbs, 0, sizeof mbs);
break;
case 0:
/* Careful: converting '\0' returns 0, but a '\0' is a
* real character for us, so we should consume 1 byte. */
cnt = 1;
/* FALL THROUGH */
default:
if (is_wcs_nicechar(c))
ret = 1;
break;
}
if (ret)
break;
umlen -= cnt;
ptr += cnt;
}
free(ums);
return ret;
}
/* ztrdup multibyte string with nice formatting */
/**/
mod_export char *
nicedup(const char *s, int heap)
{
char *retstr;
(void)mb_niceformat(s, NULL, &retstr, heap ? NICEFLAG_HEAP : 0);
return retstr;
}
/*
* The guts of mb_metacharlenconv(). This version assumes we are
* processing a true multibyte character string without tokens, and
* takes the shift state as an argument.
*/
/**/
mod_export int
mb_metacharlenconv_r(const char *s, wint_t *wcp, mbstate_t *mbsp)
{
size_t ret = MB_INVALID;
char inchar;
const char *ptr;
wchar_t wc;
if (STOUC(*s) <= 0x7f) {
if (wcp)
*wcp = (wint_t)*s;
return 1;
}
for (ptr = s; *ptr; ) {
if (*ptr == Meta) {
inchar = *++ptr ^ 32;
DPUTS(!*ptr,
"BUG: unexpected end of string in mb_metacharlen()\n");
} else if (imeta(*ptr)) {
/*
* As this is metafied input, this is a token --- this
* can't be a part of the string. It might be
* something on the end of an unbracketed parameter
* reference, for example.
*/
break;
} else
inchar = *ptr;
ptr++;
ret = mbrtowc(&wc, &inchar, 1, mbsp);
if (ret == MB_INVALID)
break;
if (ret == MB_INCOMPLETE)
continue;
if (wcp)
*wcp = wc;
return ptr - s;
}
if (wcp)
*wcp = WEOF;
/* No valid multibyte sequence */
memset(mbsp, 0, sizeof(*mbsp));
if (ptr > s) {
return 1 + (*s == Meta); /* Treat as single byte character */
} else
return 0; /* Probably shouldn't happen */
}
/*
* Length of metafied string s which contains the next multibyte
* character; single (possibly metafied) character if string is not null
* but character is not valid (e.g. possibly incomplete at end of string).
* Returned value is guaranteed not to reach beyond the end of the
* string (assuming correct metafication).
*
* If wcp is not NULL, the converted wide character is stored there.
* If no conversion could be done WEOF is used.
*/
/**/
mod_export int
mb_metacharlenconv(const char *s, wint_t *wcp)
{
if (!isset(MULTIBYTE) || STOUC(*s) <= 0x7f) {
/* treat as single byte, possibly metafied */
if (wcp)
*wcp = (wint_t)(*s == Meta ? s[1] ^ 32 : *s);
return 1 + (*s == Meta);
}
/*
* We have to handle tokens here, since we may be looking
* through a tokenized input. Obviously this isn't
* a valid multibyte character, so just return WEOF
* and let the caller handle it as a single character.
*
* TODO: I've a sneaking suspicion we could do more here
* to prevent the caller always needing to handle invalid
* characters specially, but sometimes it may need to know.
*/
if (itok(*s)) {
if (wcp)
*wcp = WEOF;
return 1;
}
return mb_metacharlenconv_r(s, wcp, &mb_shiftstate);
}
/*
* Total number of multibyte characters in metafied string s.
* Same answer as iterating mb_metacharlen() and counting calls
* until end of string.
*
* If width is 1, return total character width rather than number.
* If width is greater than 1, return 1 if character has non-zero width,
* else 0.
*
* Ends if either *ptr is '\0', the normal case (eptr may be NULL for
* this), or ptr is eptr (i.e. *eptr is where the null would be if null
* terminated) for strings not delimited by nulls --- note these are
* still metafied.
*/
/**/
mod_export int
mb_metastrlenend(char *ptr, int width, char *eptr)
{
char inchar, *laststart;
size_t ret;
wchar_t wc;
int num, num_in_char, complete;
if (!isset(MULTIBYTE))
return ztrlen(ptr);
laststart = ptr;
ret = MB_INVALID;
num = num_in_char = 0;
complete = 1;
memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
while (*ptr && !(eptr && ptr >= eptr)) {
if (*ptr == Meta)
inchar = *++ptr ^ 32;
else
inchar = *ptr;
ptr++;
if (complete && STOUC(inchar) <= STOUC(0x7f)) {
/*
* We rely on 7-bit US-ASCII as a subset, so skip
* multibyte handling if we have such a character.
*/
num++;
laststart = ptr;
num_in_char = 0;
continue;
}
ret = mbrtowc(&wc, &inchar, 1, &mb_shiftstate);
if (ret == MB_INCOMPLETE) {
/*
* "num_in_char" is only used for incomplete characters.
* The assumption is that we will output all trailing octets
* that form part of an incomplete character as a single
* character (of single width) if we don't get a complete
* character. This is purely pragmatic --- I'm not aware
* of a standard way of dealing with incomplete characters.
*
* If we do get a complete character, num_in_char
* becomes irrelevant and is set to zero
*
* This is in contrast to "num" which counts the characters
* or widths in complete characters. The two are summed,
* so we don't count characters twice.
*/
num_in_char++;
complete = 0;
} else {
if (ret == MB_INVALID) {
/* Reset, treat as single character */
memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
ptr = laststart + (*laststart == Meta) + 1;
num++;
} else if (width) {
/*
* Returns -1 if not a printable character. We
* turn this into 0.
*/
int wcw = WCWIDTH(wc);
if (wcw > 0) {
if (width == 1)
num += wcw;
else
num++;
}
} else
num++;
laststart = ptr;
num_in_char = 0;
complete = 1;
}
}
/* If incomplete, treat remainder as trailing single character */
return num + (num_in_char ? 1 : 0);
}
/*
* The equivalent of mb_metacharlenconv_r() for
* strings that aren't metafied and hence have
* explicit lengths.
*/
/**/
mod_export int
mb_charlenconv_r(const char *s, int slen, wint_t *wcp, mbstate_t *mbsp)
{
size_t ret = MB_INVALID;
char inchar;
const char *ptr;
wchar_t wc;
if (slen && STOUC(*s) <= 0x7f) {
if (wcp)
*wcp = (wint_t)*s;
return 1;
}
for (ptr = s; slen; ) {
inchar = *ptr;
ptr++;
slen--;
ret = mbrtowc(&wc, &inchar, 1, mbsp);
if (ret == MB_INVALID)
break;
if (ret == MB_INCOMPLETE)
continue;
if (wcp)
*wcp = wc;
return ptr - s;
}
if (wcp)
*wcp = WEOF;
/* No valid multibyte sequence */
memset(mbsp, 0, sizeof(*mbsp));
if (ptr > s) {
return 1; /* Treat as single byte character */
} else
return 0; /* Probably shouldn't happen */
}
/*
* The equivalent of mb_metacharlenconv() for
* strings that aren't metafied and hence have
* explicit lengths;
*/
/**/
mod_export int
mb_charlenconv(const char *s, int slen, wint_t *wcp)
{
if (!isset(MULTIBYTE) || STOUC(*s) <= 0x7f) {
if (wcp)
*wcp = (wint_t)*s;
return 1;
}
return mb_charlenconv_r(s, slen, wcp, &mb_shiftstate);
}
/**/
#else
/* Simple replacement for mb_metacharlenconv */
/**/
mod_export int
metacharlenconv(const char *x, int *c)
{
/*
* Here we don't use STOUC() on the chars since they
* may be compared against other chars and this will fail
* if chars are signed and the high bit is set.
*/
if (*x == Meta) {
if (c)
*c = x[1] ^ 32;
return 2;
}
if (c)
*c = (char)*x;
return 1;
}
/* Simple replacement for mb_charlenconv */
/**/
mod_export int
charlenconv(const char *x, int len, int *c)
{
if (!len) {
if (c)
*c = '\0';
return 0;
}
if (c)
*c = (char)*x;
return 1;
}
/**/
#endif /* MULTIBYTE_SUPPORT */
/*
* Expand tabs to given width, with given starting position on line.
* len is length of unmetafied string in bytes.
* Output to fout.
* Return the end position on the line, i.e. if this is 0 modulo width
* the next character is aligned with a tab stop.
*
* If all is set, all tabs are expanded, else only leading tabs.
*/
/**/
mod_export int
zexpandtabs(const char *s, int len, int width, int startpos, FILE *fout,
int all)
{
int at_start = 1;
#ifdef MULTIBYTE_SUPPORT
mbstate_t mbs;
size_t ret;
wchar_t wc;
memset(&mbs, 0, sizeof(mbs));
#endif
while (len) {
if (*s == '\t') {
if (all || at_start) {
s++;
len--;
if (width <= 0 || !(startpos % width)) {
/* always output at least one space */
fputc(' ', fout);
startpos++;
}
if (width <= 0)
continue; /* paranoia */
while (startpos % width) {
fputc(' ', fout);
startpos++;
}
} else {
/*
* Leave tab alone.
* Guess width to apply... we might get this wrong.
* This is only needed if there's a following string
* that needs tabs expanding, which is unusual.
*/
startpos += width - startpos % width;
s++;
len--;
fputc('\t', fout);
}
continue;
} else if (*s == '\n' || *s == '\r') {
fputc(*s, fout);
s++;
len--;
startpos = 0;
at_start = 1;
continue;
}
at_start = 0;
#ifdef MULTIBYTE_SUPPORT
if (isset(MULTIBYTE)) {
const char *sstart = s;
ret = mbrtowc(&wc, s, len, &mbs);
if (ret == MB_INVALID) {
/* Assume single character per character */
memset(&mbs, 0, sizeof(mbs));
s++;
len--;
} else if (ret == MB_INCOMPLETE) {
/* incomplete at end --- assume likewise, best we've got */
s++;
len--;
} else {
s += ret;
len -= (int)ret;
}
if (ret == MB_INVALID || ret == MB_INCOMPLETE) {
startpos++;
} else {
int wcw = WCWIDTH(wc);
if (wcw > 0) /* paranoia */
startpos += wcw;
}
fwrite(sstart, s - sstart, 1, fout);
continue;
}
#endif /* MULTIBYTE_SUPPORT */
fputc(*s, fout);
s++;
len--;
startpos++;
}
return startpos;
}
/* check for special characters in the string */
/**/
mod_export int
hasspecial(char const *s)
{
for (; *s; s++) {
if (ispecial(*s == Meta ? *++s ^ 32 : *s))
return 1;
}
return 0;
}
static char *
addunprintable(char *v, const char *u, const char *uend)
{
for (; u < uend; u++) {
/*
* Just do this byte by byte; there's no great
* advantage in being clever with multibyte
* characters if we don't think they're printable.
*/
int c;
if (*u == Meta)
c = STOUC(*++u ^ 32);
else
c = STOUC(*u);
switch (c) {
case '\0':
*v++ = '\\';
*v++ = '0';
if ('0' <= u[1] && u[1] <= '7') {
*v++ = '0';
*v++ = '0';
}
break;
case '\007': *v++ = '\\'; *v++ = 'a'; break;
case '\b': *v++ = '\\'; *v++ = 'b'; break;
case '\f': *v++ = '\\'; *v++ = 'f'; break;
case '\n': *v++ = '\\'; *v++ = 'n'; break;
case '\r': *v++ = '\\'; *v++ = 'r'; break;
case '\t': *v++ = '\\'; *v++ = 't'; break;
case '\v': *v++ = '\\'; *v++ = 'v'; break;
default:
*v++ = '\\';
*v++ = '0' + ((c >> 6) & 7);
*v++ = '0' + ((c >> 3) & 7);
*v++ = '0' + (c & 7);
break;
}
}
return v;
}
/*
* Quote the string s and return the result as a string from the heap.
*
* The last argument is a QT_ value defined in zsh.h other than QT_NONE.
*
* Most quote styles other than backslash assume the quotes are to
* be added outside quotestring(). QT_SINGLE_OPTIONAL is different:
* the single quotes are only added where necessary, so the
* whole expression is handled here.
*
* The string may be metafied and contain tokens.
*/
/**/
mod_export char *
quotestring(const char *s, int instring)
{
const char *u;
char *v;
int alloclen;
char *buf;
int shownull = 0;
/*
* quotesub is used with QT_SINGLE_OPTIONAL.
* quotesub = 0: mechanism not active
* quotesub = 1: mechanism pending, no "'" yet;
* needs adding at quotestart.
* quotesub = 2: mechanism active, added opening "'"; need
* closing "'".
*/
int quotesub = 0, slen;
char *quotestart;
convchar_t cc;
const char *uend;
slen = strlen(s);
switch (instring)
{
case QT_BACKSLASH_SHOWNULL:
shownull = 1;
instring = QT_BACKSLASH;
/*FALLTHROUGH*/
case QT_BACKSLASH:
/*
* With QT_BACKSLASH we may need to use $'\300' stuff.
* Keep memory usage within limits by allocating temporary
* storage and using heap for correct size at end.
*/
alloclen = slen * 7 + 1;
break;
case QT_BACKSLASH_PATTERN:
alloclen = slen * 2 + 1;
break;
case QT_SINGLE_OPTIONAL:
/*
* Here, we may need to add single quotes.
* Always show empty strings.
*/
alloclen = slen * 4 + 3;
quotesub = shownull = 1;
break;
default:
alloclen = slen * 4 + 1;
break;
}
if (!*s && shownull)
alloclen += 2; /* for '' */
quotestart = v = buf = zshcalloc(alloclen);
DPUTS(instring < QT_BACKSLASH || instring == QT_BACKTICK ||
instring > QT_BACKSLASH_PATTERN,
"BUG: bad quote type in quotestring");
u = s;
if (instring == QT_DOLLARS) {
/*
* The only way to get Nularg here is when
* it is placeholding for the empty string?
*/
if (inull(*u))
u++;
/*
* As we test for printability here we need to be able
* to look for multibyte characters.
*/
MB_METACHARINIT();
while (*u) {
uend = u + MB_METACHARLENCONV(u, &cc);
if (
#ifdef MULTIBYTE_SUPPORT
cc != WEOF &&
#endif
WC_ISPRINT(cc)) {
switch (cc) {
case ZWC('\\'):
case ZWC('\''):
*v++ = '\\';
break;
default:
if (isset(BANGHIST) && cc == (wchar_t)bangchar)
*v++ = '\\';
break;
}
while (u < uend)
*v++ = *u++;
} else {
/* Not printable */
v = addunprintable(v, u, uend);
u = uend;
}
}
} else if (instring == QT_BACKSLASH_PATTERN) {
while (*u) {
if (ipattern(*u))
*v++ = '\\';
*v++ = *u++;
}
} else {
if (shownull) {
/* We can't show an empty string with just backslash quoting. */
if (!*u) {
*v++ = '\'';
*v++ = '\'';
}
}
/*
* Here there are syntactic special characters, so
* we start by going through bytewise.
*/
while (*u) {
int dobackslash = 0;
if (*u == Tick || *u == Qtick) {
char c = *u++;
*v++ = c;
while (*u && *u != c)
*v++ = *u++;
*v++ = c;
if (*u)
u++;
continue;
} else if ((*u == Qstring || *u == '$') && u[1] == '\'' &&
instring == QT_DOUBLE) {
/*
* We don't need to quote $'...' inside a double-quoted
* string. This is largely cosmetic; it looks neater
* if we don't but it doesn't do any harm since the
* \ is stripped.
*/
*v++ = *u++;
} else if ((*u == String || *u == Qstring) &&
(u[1] == Inpar || u[1] == Inbrack || u[1] == Inbrace)) {
char c = (u[1] == Inpar ? Outpar : (u[1] == Inbrace ?
Outbrace : Outbrack));
char beg = *u;
int level = 0;
*v++ = *u++;
*v++ = *u++;
while (*u && (*u != c || level)) {
if (*u == beg)
level++;
else if (*u == c)
level--;
*v++ = *u++;
}
if (*u)
*v++ = *u++;
continue;
}
else if (ispecial(*u) &&
((*u != '=' && *u != '~') ||
u == s ||
(isset(MAGICEQUALSUBST) &&
(u[-1] == '=' || u[-1] == ':')) ||
(*u == '~' && isset(EXTENDEDGLOB))) &&
(instring == QT_BACKSLASH ||
instring == QT_SINGLE_OPTIONAL ||
(isset(BANGHIST) && *u == (char)bangchar &&
instring != QT_SINGLE) ||
(instring == QT_DOUBLE &&
(*u == '$' || *u == '`' || *u == '\"' || *u == '\\')) ||
(instring == QT_SINGLE && *u == '\''))) {
if (instring == QT_SINGLE_OPTIONAL) {
if (quotesub == 1) {
/*
* We haven't yet had to quote at the start.
*/
if (*u == '\'') {
/*
* We don't need to.
*/
*v++ = '\\';
} else {
/*
* It's now time to add quotes.
*/
if (v > quotestart)
{
char *addq;
for (addq = v; addq > quotestart; addq--)
*addq = addq[-1];
}
*quotestart = '\'';
v++;
quotesub = 2;
}
*v++ = *u++;
/*
* Next place to start quotes is here.
*/
quotestart = v;
} else if (*u == '\'') {
if (unset(RCQUOTES)) {
*v++ = '\'';
*v++ = '\\';
*v++ = '\'';
/* Don't restart quotes unless we need them */
quotesub = 1;
quotestart = v;
} else {
/* simplest just to use '' always */
*v++ = '\'';
*v++ = '\'';
}
/* dealt with */
u++;
} else {
/* else already quoting, just add */
*v++ = *u++;
}
continue;
} else if (*u == '\n' ||
(instring == QT_SINGLE && *u == '\'')) {
if (*u == '\n') {
*v++ = '$';
*v++ = '\'';
*v++ = '\\';
*v++ = 'n';
*v++ = '\'';
} else if (unset(RCQUOTES)) {
*v++ = '\'';
if (*u == '\'')
*v++ = '\\';
*v++ = *u;
*v++ = '\'';
} else
*v++ = '\'', *v++ = '\'';
u++;
continue;
} else {
/*
* We'll need a backslash, but don't add it
* yet since if the character isn't printable
* we'll have to upgrade it to $'...'.
*/
dobackslash = 1;
}
}
if (itok(*u) || instring != QT_BACKSLASH) {
/* Needs to be passed straight through. */
if (dobackslash)
*v++ = '\\';
if (*u == Inparmath) {
/*
* Already syntactically quoted: don't
* add more.
*/
int inmath = 1;
*v++ = *u++;
for (;;) {
char uc = *u;
*v++ = *u++;
if (uc == '\0')
break;
else if (uc == Outparmath && !--inmath)
break;
else if (uc == Inparmath)
++inmath;
}
} else
*v++ = *u++;
continue;
}
/*
* Now check if the output is unprintable in the
* current character set.
*/
uend = u + MB_METACHARLENCONV(u, &cc);
if (
#ifdef MULTIBYTE_SUPPORT
cc != WEOF &&
#endif
WC_ISPRINT(cc)) {
if (dobackslash)
*v++ = '\\';
while (u < uend) {
if (*u == Meta)
*v++ = *u++;
*v++ = *u++;
}
} else {
/* Not printable */
*v++ = '$';
*v++ = '\'';
v = addunprintable(v, u, uend);
*v++ = '\'';
u = uend;
}
}
}
if (quotesub == 2)
*v++ = '\'';
*v = '\0';
v = dupstring(buf);
zfree(buf, alloclen);
return v;
}
/*
* Unmetafy and output a string, quoted if it contains special
* characters.
*
* If stream is NULL, return the same output with any allocation on the
* heap.
*/
/**/
mod_export char *
quotedzputs(char const *s, FILE *stream)
{
int inquote = 0, c;
char *outstr, *ptr;
/* check for empty string */
if(!*s) {
if (!stream)
return dupstring("''");
fputs("''", stream);
return NULL;
}
#ifdef MULTIBYTE_SUPPORT
if (is_mb_niceformat(s)) {
if (stream) {
fputs("$'", stream);
mb_niceformat(s, stream, NULL, NICEFLAG_QUOTE);
fputc('\'', stream);
return NULL;
} else {
char *substr;
mb_niceformat(s, NULL, &substr, NICEFLAG_QUOTE|NICEFLAG_NODUP);
outstr = (char *)zhalloc(4 + strlen(substr));
sprintf(outstr, "$'%s'", substr);
free(substr);
return outstr;
}
}
#endif /* MULTIBYTE_SUPPORT */
if (!hasspecial(s)) {
if (stream) {
zputs(s, stream);
return NULL;
} else {
return dupstring(s);
}
}
if (!stream) {
const char *cptr;
int l = strlen(s) + 2;
for (cptr = s; *cptr; cptr++) {
if (*cptr == Meta)
cptr++;
else if (*cptr == '\'')
l += isset(RCQUOTES) ? 1 : 3;
}
ptr = outstr = zhalloc(l + 1);
} else {
ptr = outstr = NULL;
}
if (isset(RCQUOTES)) {
/* use rc-style quotes-within-quotes for the whole string */
if (stream) {
if (fputc('\'', stream) < 0)
return NULL;
} else
*ptr++ = '\'';
while(*s) {
if (*s == Meta)
c = *++s ^ 32;
else
c = *s;
s++;
if (c == '\'') {
if (stream) {
if (fputc('\'', stream) < 0)
return NULL;
} else
*ptr++ = '\'';
} else if (c == '\n' && isset(CSHJUNKIEQUOTES)) {
if (stream) {
if (fputc('\\', stream) < 0)
return NULL;
} else
*ptr++ = '\\';
}
if (stream) {
if (fputc(c, stream) < 0)
return NULL;
} else {
if (imeta(c)) {
*ptr++ = Meta;
*ptr++ = c ^ 32;
} else
*ptr++ = c;
}
}
if (stream) {
if (fputc('\'', stream) < 0)
return NULL;
} else
*ptr++ = '\'';
} else {
/* use Bourne-style quoting, avoiding empty quoted strings */
while (*s) {
if (*s == Meta)
c = *++s ^ 32;
else
c = *s;
s++;
if (c == '\'') {
if (inquote) {
if (stream) {
if (putc('\'', stream) < 0)
return NULL;
} else
*ptr++ = '\'';
inquote=0;
}
if (stream) {
if (fputs("\\'", stream) < 0)
return NULL;
} else {
*ptr++ = '\\';
*ptr++ = '\'';
}
} else {
if (!inquote) {
if (stream) {
if (fputc('\'', stream) < 0)
return NULL;
} else
*ptr++ = '\'';
inquote=1;
}
if (c == '\n' && isset(CSHJUNKIEQUOTES)) {
if (stream) {
if (fputc('\\', stream) < 0)
return NULL;
} else
*ptr++ = '\\';
}
if (stream) {
if (fputc(c, stream) < 0)
return NULL;
} else {
if (imeta(c)) {
*ptr++ = Meta;
*ptr++ = c ^ 32;
} else
*ptr++ = c;
}
}
}
if (inquote) {
if (stream) {
if (fputc('\'', stream) < 0)
return NULL;
} else
*ptr++ = '\'';
}
}
if (!stream)
*ptr++ = '\0';
return outstr;
}
/* Double-quote a metafied string. */
/**/
mod_export char *
dquotedztrdup(char const *s)
{
int len = strlen(s) * 4 + 2;
char *buf = zalloc(len);
char *p = buf, *ret;
if(isset(CSHJUNKIEQUOTES)) {
int inquote = 0;
while(*s) {
int c = *s++;
if (c == Meta)
c = *s++ ^ 32;
switch(c) {
case '"':
case '$':
case '`':
if(inquote) {
*p++ = '"';
inquote = 0;
}
*p++ = '\\';
*p++ = c;
break;
default:
if(!inquote) {
*p++ = '"';
inquote = 1;
}
if(c == '\n')
*p++ = '\\';
*p++ = c;
break;
}
}
if (inquote)
*p++ = '"';
} else {
int pending = 0;
*p++ = '"';
while(*s) {
int c = *s++;
if (c == Meta)
c = *s++ ^ 32;
switch(c) {
case '\\':
if(pending)
*p++ = '\\';
*p++ = '\\';
pending = 1;
break;
case '"':
case '$':
case '`':
if(pending)
*p++ = '\\';
*p++ = '\\';
/* FALL THROUGH */
default:
*p++ = c;
pending = 0;
break;
}
}
if(pending)
*p++ = '\\';
*p++ = '"';
}
ret = metafy(buf, p - buf, META_DUP);
zfree(buf, len);
return ret;
}
/* Unmetafy and output a string, double quoting it in its entirety. */
#if 0 /**/
int
dquotedzputs(char const *s, FILE *stream)
{
char *d = dquotedztrdup(s);
int ret = zputs(d, stream);
zsfree(d);
return ret;
}
#endif
# if defined(HAVE_NL_LANGINFO) && defined(CODESET) && !defined(__STDC_ISO_10646__)
/* Convert a character from UCS4 encoding to UTF-8 */
static size_t
ucs4toutf8(char *dest, unsigned int wval)
{
size_t len;
if (wval < 0x80)
len = 1;
else if (wval < 0x800)
len = 2;
else if (wval < 0x10000)
len = 3;
else if (wval < 0x200000)
len = 4;
else if (wval < 0x4000000)
len = 5;
else
len = 6;
switch (len) { /* falls through except to the last case */
case 6: dest[5] = (wval & 0x3f) | 0x80; wval >>= 6;
case 5: dest[4] = (wval & 0x3f) | 0x80; wval >>= 6;
case 4: dest[3] = (wval & 0x3f) | 0x80; wval >>= 6;
case 3: dest[2] = (wval & 0x3f) | 0x80; wval >>= 6;
case 2: dest[1] = (wval & 0x3f) | 0x80; wval >>= 6;
*dest = wval | ((0xfc << (6 - len)) & 0xfc);
break;
case 1: *dest = wval;
}
return len;
}
#endif
/*
* The following only occurs once or twice in the code, but in different
* places depending how character set conversion is implemented.
*/
#define CHARSET_FAILED() \
if (how & GETKEY_DOLLAR_QUOTE) { \
while ((*tdest++ = *++s)) { \
if (how & GETKEY_UPDATE_OFFSET) { \
if (s - sstart > *misc) \
(*misc)++; \
} \
if (*s == Snull) { \
*len = (s - sstart) + 1; \
*tdest = '\0'; \
return buf; \
} \
} \
*len = tdest - buf; \
return buf; \
} \
*t = '\0'; \
*len = t - buf; \
return buf
/*
* Decode a key string, turning it into the literal characters.
* The value returned is a newly allocated string from the heap.
*
* The length is returned in *len. This is usually the length of
* the final unmetafied string. The exception is the case of
* a complete GETKEY_DOLLAR_QUOTE conversion where *len is the
* length of the input string which has been used (up to and including
* the terminating single quote); as the final string is metafied and
* NULL-terminated its length is not required. If both GETKEY_DOLLAR_QUOTE
* and GETKEY_UPDATE_OFFSET are present in "how", the string is not
* expected to be terminated (this is used in completion to parse
* a partial $'...'-quoted string) and the length passed back is
* that of the converted string. Note in both cases that this is a length
* in bytes (i.e. the same as given by a raw pointer difference), not
* characters, which may occupy multiple bytes.
*
* how is a set of bits from the GETKEY_ values defined in zsh.h;
* not all combinations of bits are useful. Callers will typically
* use one of the GETKEYS_ values which define sets of bits.
* Note, for example that:
* - GETKEY_SINGLE_CHAR must not be combined with GETKEY_DOLLAR_QUOTE.
* - GETKEY_UPDATE_OFFSET is only allowed if GETKEY_DOLLAR_QUOTE is
* also present.
*
* *misc is used for various purposes:
* - If GETKEY_BACKSLASH_MINUS is set, it indicates the presence
* of \- in the input.
* - If GETKEY_BACKSLASH_C is set, it indicates the presence
* of \c in the input.
* - If GETKEY_UPDATE_OFFSET is set, it is set on input to some
* mystical completion offset and is updated to a new offset based
* on the converted characters. All Hail the Completion System
* [makes the mystic completion system runic sign in the air].
*
* The return value is unmetafied unless GETKEY_DOLLAR_QUOTE is
* in use.
*/
/**/
mod_export char *
getkeystring(char *s, int *len, int how, int *misc)
{
char *buf, tmp[1];
char *t, *tdest = NULL, *u = NULL, *sstart = s, *tbuf = NULL;
char svchar = '\0';
int meta = 0, control = 0, ignoring = 0;
int i;
#if defined(HAVE_WCHAR_H) && defined(HAVE_WCTOMB) && defined(__STDC_ISO_10646__)
wint_t wval;
int count;
#else
unsigned int wval;
# if defined(HAVE_NL_LANGINFO) && defined(CODESET)
# if defined(HAVE_ICONV)
iconv_t cd;
char inbuf[4];
size_t inbytes, outbytes;
# endif
size_t count;
# endif
#endif
DPUTS((how & GETKEY_UPDATE_OFFSET) &&
(how & ~(GETKEYS_DOLLARS_QUOTE|GETKEY_UPDATE_OFFSET)),
"BUG: offset updating in getkeystring only supported with $'.");
DPUTS((how & (GETKEY_DOLLAR_QUOTE|GETKEY_SINGLE_CHAR)) ==
(GETKEY_DOLLAR_QUOTE|GETKEY_SINGLE_CHAR),
"BUG: incompatible options in getkeystring");
if (how & GETKEY_SINGLE_CHAR)
t = buf = tmp;
else {
/* Length including terminating NULL */
int maxlen = 1;
/*
* We're not necessarily guaranteed the output string will
* be no longer than the input with \u and \U when output
* characters need to be metafied. As this is the only
* case where the string can get longer (?I think),
* include it in the allocation length here but don't
* bother taking account of other factors.
*/
for (t = s; *t; t++) {
if (*t == '\\') {
if (!t[1]) {
maxlen++;
break;
}
if (t[1] == 'u' || t[1] == 'U')
maxlen += MB_CUR_MAX * 2;
else
maxlen += 2;
/* skip the backslash and the following character */
t++;
} else
maxlen++;
}
if (how & GETKEY_DOLLAR_QUOTE) {
/*
* We're going to unmetafy into a new string, but
* to get a proper metafied input we're going to metafy
* into an intermediate buffer. This is necessary if we have
* \u and \U's with multiple metafied bytes. We can't
* simply remetafy the entire string because there may
* be tokens (indeed, we know there are lexical nulls floating
* around), so we have to be aware character by character
* what we are converting.
*
* In this case, buf is the final buffer (as usual),
* but t points into a temporary buffer that just has
* to be long enough to hold the result of one escape
* code transformation. We count this is a full multibyte
* character (MB_CUR_MAX) with every character metafied
* (*2) plus a little bit of fuzz (for e.g. the odd backslash).
*/
buf = tdest = zhalloc(maxlen);
t = tbuf = zhalloc(MB_CUR_MAX * 3 + 1);
} else {
t = buf = zhalloc(maxlen);
}
}
for (; *s; s++) {
if (*s == '\\' && s[1]) {
int miscadded;
if ((how & GETKEY_UPDATE_OFFSET) && s - sstart < *misc) {
(*misc)--;
miscadded = 1;
} else
miscadded = 0;
switch (*++s) {
case 'a':
#ifdef __STDC__
*t++ = '\a';
#else
*t++ = '\07';
#endif
break;
case 'n':
*t++ = '\n';
break;
case 'b':
*t++ = '\b';
break;
case 't':
*t++ = '\t';
break;
case 'v':
*t++ = '\v';
break;
case 'f':
*t++ = '\f';
break;
case 'r':
*t++ = '\r';
break;
case 'E':
if (!(how & GETKEY_EMACS)) {
*t++ = '\\', s--;
if (miscadded)
(*misc)++;
continue;
}
/* FALL THROUGH */
case 'e':
*t++ = '\033';
break;
case 'M':
/* HERE: GETKEY_UPDATE_OFFSET */
if (how & GETKEY_EMACS) {
if (s[1] == '-')
s++;
meta = 1 + control; /* preserve the order of ^ and meta */
} else {
if (miscadded)
(*misc)++;
*t++ = '\\', s--;
}
continue;
case 'C':
/* HERE: GETKEY_UPDATE_OFFSET */
if (how & GETKEY_EMACS) {
if (s[1] == '-')
s++;
control = 1;
} else {
if (miscadded)
(*misc)++;
*t++ = '\\', s--;
}
continue;
case Meta:
if (miscadded)
(*misc)++;
*t++ = '\\', s--;
break;
case '-':
if (how & GETKEY_BACKSLASH_MINUS) {
*misc = 1;
break;
}
goto def;
case 'c':
if (how & GETKEY_BACKSLASH_C) {
*misc = 1;
*t = '\0';
*len = t - buf;
return buf;
}
goto def;
case 'U':
if ((how & GETKEY_UPDATE_OFFSET) && s - sstart < *misc)
(*misc) -= 4;
/* FALLTHROUGH */
case 'u':
if ((how & GETKEY_UPDATE_OFFSET) && s - sstart < *misc) {
(*misc) -= 6; /* HERE don't really believe this */
/*
* We've now adjusted the offset for all the input
* characters, so we need to add for each
* byte of output below.
*/
}
wval = 0;
for (i=(*s == 'u' ? 4 : 8); i>0; i--) {
if (*++s && idigit(*s))
wval = wval * 16 + (*s - '0');
else if (*s && ((*s >= 'a' && *s <= 'f') ||
(*s >= 'A' && *s <= 'F')))
wval = wval * 16 + (*s & 0x1f) + 9;
else {
s--;
break;
}
}
if (how & GETKEY_SINGLE_CHAR) {
*misc = wval;
return s+1;
}
#if defined(HAVE_WCHAR_H) && defined(HAVE_WCTOMB) && defined(__STDC_ISO_10646__)
count = wctomb(t, (wchar_t)wval);
if (count == -1) {
zerr("character not in range");
CHARSET_FAILED();
}
if ((how & GETKEY_UPDATE_OFFSET) && s - sstart < *misc)
(*misc) += count;
t += count;
# else
# if defined(HAVE_NL_LANGINFO) && defined(CODESET)
if (!strcmp(nl_langinfo(CODESET), "UTF-8")) {
count = ucs4toutf8(t, wval);
t += count;
if ((how & GETKEY_UPDATE_OFFSET) && s - sstart < *misc)
(*misc) += count;
} else {
# ifdef HAVE_ICONV
ICONV_CONST char *inptr = inbuf;
const char *codesetstr = nl_langinfo(CODESET);
inbytes = 4;
outbytes = 6;
/* store value in big endian form */
for (i=3;i>=0;i--) {
inbuf[i] = wval & 0xff;
wval >>= 8;
}
/*
* If the code set isn't handled, we'd better
* assume it's US-ASCII rather than just failing
* hopelessly. Solaris has a weird habit of
* returning 646. This is handled by the
* native iconv(), but not by GNU iconv; what's
* more, some versions of the native iconv don't
* handle standard names like ASCII.
*
* This should only be a problem if there's a
* mismatch between the NLS and the iconv in use,
* which probably only means if libiconv is in use.
* We checked at configure time if our libraries
* pulled in _libiconv_version, which should be
* a good test.
*
* It shouldn't ever be NULL, but while we're
* being paranoid...
*/
#ifdef ICONV_FROM_LIBICONV
if (!codesetstr || !*codesetstr)
codesetstr = "US-ASCII";
#endif
cd = iconv_open(codesetstr, "UCS-4BE");
#ifdef ICONV_FROM_LIBICONV
if (cd == (iconv_t)-1 && !strcmp(codesetstr, "646")) {
codesetstr = "US-ASCII";
cd = iconv_open(codesetstr, "UCS-4BE");
}
#endif
if (cd == (iconv_t)-1) {
zerr("cannot do charset conversion (iconv failed)");
CHARSET_FAILED();
}
count = iconv(cd, &inptr, &inbytes, &t, &outbytes);
iconv_close(cd);
if (count == (size_t)-1) {
zerr("character not in range");
CHARSET_FAILED();
}
if ((how & GETKEY_UPDATE_OFFSET) && s - sstart < *misc)
(*misc) += count;
# else
zerr("cannot do charset conversion (iconv not available)");
CHARSET_FAILED();
# endif
}
# else
zerr("cannot do charset conversion (NLS not supported)");
CHARSET_FAILED();
# endif
# endif
if (how & GETKEY_DOLLAR_QUOTE) {
char *t2;
for (t2 = tbuf; t2 < t; t2++) {
if (imeta(*t2)) {
*tdest++ = Meta;
*tdest++ = *t2 ^ 32;
} else
*tdest++ = *t2;
}
/* reset temporary buffer after handling */
t = tbuf;
}
continue;
case '\'':
case '\\':
if (how & GETKEY_DOLLAR_QUOTE) {
/*
* Usually \' and \\ will have the initial
* \ turned into a Bnull, however that's not
* necessarily the case when called from
* completion.
*/
*t++ = *s;
break;
}
/* FALLTHROUGH */
default:
def:
/* HERE: GETKEY_UPDATE_OFFSET? */
if ((idigit(*s) && *s < '8') || *s == 'x') {
if (!(how & GETKEY_OCTAL_ESC)) {
if (*s == '0')
s++;
else if (*s != 'x') {
*t++ = '\\', s--;
continue;
}
}
if (s[1] && s[2] && s[3]) {
svchar = s[3];
s[3] = '\0';
u = s;
}
*t++ = zstrtol(s + (*s == 'x'), &s,
(*s == 'x') ? 16 : 8);
if ((how & GETKEY_PRINTF_PERCENT) && t[-1] == '%')
*t++ = '%';
if (svchar) {
u[3] = svchar;
svchar = '\0';
}
s--;
} else {
if (!(how & GETKEY_EMACS) && *s != '\\') {
if (miscadded)
(*misc)++;
*t++ = '\\';
}
*t++ = *s;
}
break;
}
} else if ((how & GETKEY_DOLLAR_QUOTE) && *s == Snull) {
/* return length to following character */
*len = (s - sstart) + 1;
*tdest = '\0';
return buf;
} else if (*s == '^' && !control && (how & GETKEY_CTRL) && s[1]) {
control = 1;
continue;
#ifdef MULTIBYTE_SUPPORT
} else if ((how & GETKEY_SINGLE_CHAR) &&
isset(MULTIBYTE) && STOUC(*s) > 127) {
wint_t wc;
int len;
len = mb_metacharlenconv(s, &wc);
if (wc != WEOF) {
*misc = (int)wc;
return s + len;
}
#endif
} else if (*s == Meta)
*t++ = *++s ^ 32;
else {
if (itok(*s)) {
/*
* We need to be quite careful here. We haven't
* necessarily got an input stream with all tokens
* removed, so the majority of tokens need passing
* through untouched and without Meta handling.
* However, me may need to handle tokenized
* backslashes.
*/
if (meta || control) {
/*
* Presumably we should be using meta or control
* on the character representing the token.
*
* Special case: $'\M-\\' where the token is a Bnull.
* This time we dump the Bnull since we're
* replacing the whole thing. The lexer
* doesn't know about the meta or control modifiers.
*/
if ((how & GETKEY_DOLLAR_QUOTE) && *s == Bnull)
*t++ = *++s;
else
*t++ = ztokens[*s - Pound];
} else if (how & GETKEY_DOLLAR_QUOTE) {
/*
* We don't want to metafy this, it's a real
* token.
*/
*tdest++ = *s;
if (*s == Bnull) {
/*
* Bnull is a backslash which quotes a couple
* of special characters that always appear
* literally next. See strquote handling
* in gettokstr() in lex.c. We need
* to retain the Bnull (as above) so that quote
* handling in completion can tell where the
* backslash was.
*/
*tdest++ = *++s;
}
/* reset temporary buffer, now handled */
t = tbuf;
continue;
} else
*t++ = *s;
} else
*t++ = *s;
}
if (meta == 2) {
t[-1] |= 0x80;
meta = 0;
}
if (control) {
if (t[-1] == '?')
t[-1] = 0x7f;
else
t[-1] &= 0x9f;
control = 0;
}
if (meta) {
t[-1] |= 0x80;
meta = 0;
}
if (how & GETKEY_DOLLAR_QUOTE) {
char *t2;
for (t2 = tbuf; t2 < t; t2++) {
/*
* In POSIX mode, an embedded NULL is discarded and
* terminates processing. It just does, that's why.
*/
if (isset(POSIXSTRINGS)) {
if (*t2 == '\0')
ignoring = 1;
if (ignoring)
break;
}
if (imeta(*t2)) {
*tdest++ = Meta;
*tdest++ = *t2 ^ 32;
} else {
*tdest++ = *t2;
}
}
/*
* Reset use of temporary buffer.
*/
t = tbuf;
}
if ((how & GETKEY_SINGLE_CHAR) && t != tmp) {
*misc = STOUC(tmp[0]);
return s + 1;
}
}
/*
* When called from completion, where we use GETKEY_UPDATE_OFFSET to
* update the index into the metafied editor line, we don't necessarily
* have the end of a $'...' quotation, else we should do.
*/
DPUTS((how & (GETKEY_DOLLAR_QUOTE|GETKEY_UPDATE_OFFSET)) ==
GETKEY_DOLLAR_QUOTE, "BUG: unterminated $' substitution");
*t = '\0';
if (how & GETKEY_DOLLAR_QUOTE)
*tdest = '\0';
if (how & GETKEY_SINGLE_CHAR)
*misc = 0;
else
*len = ((how & GETKEY_DOLLAR_QUOTE) ? tdest : t) - buf;
return buf;
}
/* Return non-zero if s is a prefix of t. */
/**/
mod_export int
strpfx(const char *s, const char *t)
{
while (*s && *s == *t)
s++, t++;
return !*s;
}
/* Return non-zero if s is a suffix of t. */
/**/
mod_export int
strsfx(char *s, char *t)
{
int ls = strlen(s), lt = strlen(t);
if (ls <= lt)
return !strcmp(t + lt - ls, s);
return 0;
}
/**/
static int
upchdir(int n)
{
char buf[PATH_MAX+1];
char *s;
int err = -1;
while (n > 0) {
for (s = buf; s < buf + PATH_MAX - 4 && n--; )
*s++ = '.', *s++ = '.', *s++ = '/';
s[-1] = '\0';
if (chdir(buf))
return err;
err = -2;
}
return 0;
}
/*
* Initialize a "struct dirsav".
* The structure will be set to the directory we want to save
* the first time we change to a different directory.
*/
/**/
mod_export void
init_dirsav(Dirsav d)
{
d->ino = d->dev = 0;
d->dirname = NULL;
d->dirfd = d->level = -1;
}
/*
* Change directory, without following symlinks. Returns 0 on success, -1
* on failure. Sets errno to ENOTDIR if any symlinks are encountered. If
* fchdir() fails, or the current directory is unreadable, we might end up
* in an unwanted directory in case of failure.
*
* path is an unmetafied but null-terminated string, as needed by system
* calls.
*/
/**/
mod_export int
lchdir(char const *path, struct dirsav *d, int hard)
{
char const *pptr;
int level;
struct stat st1;
struct dirsav ds;
#ifdef HAVE_LSTAT
char buf[PATH_MAX + 1], *ptr;
int err;
struct stat st2;
#endif
#ifdef HAVE_FCHDIR
int close_dir = 0;
#endif
if (!d) {
init_dirsav(&ds);
d = &ds;
}
#ifdef HAVE_LSTAT
if ((*path == '/' || !hard) &&
(d != &ds || hard)){
#else
if (*path == '/') {
#endif
level = -1;
#ifndef HAVE_FCHDIR
if (!d->dirname)
zgetdir(d);
#endif
} else {
level = 0;
if (!d->dev && !d->ino) {
stat(".", &st1);
d->dev = st1.st_dev;
d->ino = st1.st_ino;
}
}
#ifdef HAVE_LSTAT
if (!hard)
#endif
{
if (d != &ds) {
for (pptr = path; *pptr; level++) {
while (*pptr && *pptr++ != '/');
while (*pptr == '/')
pptr++;
}
d->level = level;
}
return zchdir((char *) path);
}
#ifdef HAVE_LSTAT
#ifdef HAVE_FCHDIR
if (d->dirfd < 0) {
close_dir = 1;
if ((d->dirfd = open(".", O_RDONLY | O_NOCTTY)) < 0 &&
zgetdir(d) && *d->dirname != '/')
d->dirfd = open("..", O_RDONLY | O_NOCTTY);
}
#endif
if (*path == '/')
if (chdir("/") < 0)
zwarn("failed to chdir(/): %e", errno);
for(;;) {
while(*path == '/')
path++;
if(!*path) {
if (d == &ds)
zsfree(ds.dirname);
else
d->level = level;
#ifdef HAVE_FCHDIR
if (d->dirfd >=0 && close_dir) {
close(d->dirfd);
d->dirfd = -1;
}
#endif
return 0;
}
for(pptr = path; *++pptr && *pptr != '/'; ) ;
if(pptr - path > PATH_MAX) {
err = ENAMETOOLONG;
break;
}
for(ptr = buf; path != pptr; )
*ptr++ = *path++;
*ptr = 0;
if(lstat(buf, &st1)) {
err = errno;
break;
}
if(!S_ISDIR(st1.st_mode)) {
err = ENOTDIR;
break;
}
if(chdir(buf)) {
err = errno;
break;
}
if (level >= 0)
level++;
if(lstat(".", &st2)) {
err = errno;
break;
}
if(st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
err = ENOTDIR;
break;
}
}
if (restoredir(d)) {
int restoreerr = errno;
int i;
/*
* Failed to restore the directory.
* Just be definite, cd to root and report the result.
*/
for (i = 0; i < 2; i++) {
const char *cdest;
if (i)
cdest = "/";
else {
if (!home)
continue;
cdest = home;
}
zsfree(pwd);
pwd = ztrdup(cdest);
if (chdir(pwd) == 0)
break;
}
if (i == 2)
zerr("lost current directory, failed to cd to /: %e", errno);
else
zerr("lost current directory: %e: changed to `%s'", restoreerr,
pwd);
if (d == &ds)
zsfree(ds.dirname);
#ifdef HAVE_FCHDIR
if (d->dirfd >=0 && close_dir) {
close(d->dirfd);
d->dirfd = -1;
}
#endif
errno = err;
return -2;
}
if (d == &ds)
zsfree(ds.dirname);
#ifdef HAVE_FCHDIR
if (d->dirfd >=0 && close_dir) {
close(d->dirfd);
d->dirfd = -1;
}
#endif
errno = err;
return -1;
#endif /* HAVE_LSTAT */
}
/**/
mod_export int
restoredir(struct dirsav *d)
{
int err = 0;
struct stat sbuf;
if (d->dirname && *d->dirname == '/')
return chdir(d->dirname);
#ifdef HAVE_FCHDIR
if (d->dirfd >= 0) {
if (!fchdir(d->dirfd)) {
if (!d->dirname) {
return 0;
} else if (chdir(d->dirname)) {
close(d->dirfd);
d->dirfd = -1;
err = -2;
}
} else {
close(d->dirfd);
d->dirfd = err = -1;
}
} else
#endif
if (d->level > 0)
err = upchdir(d->level);
else if (d->level < 0)
err = -1;
if (d->dev || d->ino) {
stat(".", &sbuf);
if (sbuf.st_ino != d->ino || sbuf.st_dev != d->dev)
err = -2;
}
return err;
}
/* Check whether the shell is running with privileges in effect. *
* This is the case if EITHER the euid is zero, OR (if the system *
* supports POSIX.1e (POSIX.6) capability sets) the process' *
* Effective or Inheritable capability sets are non-empty. */
/**/
int
privasserted(void)
{
if(!geteuid())
return 1;
#ifdef HAVE_CAP_GET_PROC
{
cap_t caps = cap_get_proc();
if(caps) {
/* POSIX doesn't define a way to test whether a capability set *
* is empty or not. Typical. I hope this is conforming... */
cap_flag_value_t val;
cap_value_t n;
for(n = 0; !cap_get_flag(caps, n, CAP_EFFECTIVE, &val); n++)
if(val) {
cap_free(caps);
return 1;
}
}
cap_free(caps);
}
#endif /* HAVE_CAP_GET_PROC */
return 0;
}
/**/
mod_export int
mode_to_octal(mode_t mode)
{
int m = 0;
if(mode & S_ISUID)
m |= 04000;
if(mode & S_ISGID)
m |= 02000;
if(mode & S_ISVTX)
m |= 01000;
if(mode & S_IRUSR)
m |= 00400;
if(mode & S_IWUSR)
m |= 00200;
if(mode & S_IXUSR)
m |= 00100;
if(mode & S_IRGRP)
m |= 00040;
if(mode & S_IWGRP)
m |= 00020;
if(mode & S_IXGRP)
m |= 00010;
if(mode & S_IROTH)
m |= 00004;
if(mode & S_IWOTH)
m |= 00002;
if(mode & S_IXOTH)
m |= 00001;
return m;
}
#ifdef MAILDIR_SUPPORT
/*
* Stat a file. If it's a maildir, check all messages
* in the maildir and present the grand total as a file.
* The fields in the 'struct stat' are from the mail directory.
* The following fields are emulated:
*
* st_nlink always 1
* st_size total number of bytes in all files
* st_blocks total number of messages
* st_atime access time of newest file in maildir
* st_mtime modify time of newest file in maildir
* st_mode S_IFDIR changed to S_IFREG
*
* This is good enough for most mail-checking applications.
*/
/**/
int
mailstat(char *path, struct stat *st)
{
DIR *dd;
struct dirent *fn;
struct stat st_ret, st_tmp;
static struct stat st_ret_last;
char *dir, *file = 0;
int i;
time_t atime = 0, mtime = 0;
size_t plen = strlen(path), dlen;
/* First see if it's a directory. */
if ((i = stat(path, st)) != 0 || !S_ISDIR(st->st_mode))
return i;
st_ret = *st;
st_ret.st_nlink = 1;
st_ret.st_size = 0;
st_ret.st_blocks = 0;
st_ret.st_mode &= ~S_IFDIR;
st_ret.st_mode |= S_IFREG;
/* See if cur/ is present */
dir = appstr(ztrdup(path), "/cur");
if (stat(dir, &st_tmp) || !S_ISDIR(st_tmp.st_mode)) return 0;
st_ret.st_atime = st_tmp.st_atime;
/* See if tmp/ is present */
dir[plen] = 0;
dir = appstr(dir, "/tmp");
if (stat(dir, &st_tmp) || !S_ISDIR(st_tmp.st_mode)) return 0;
st_ret.st_mtime = st_tmp.st_mtime;
/* And new/ */
dir[plen] = 0;
dir = appstr(dir, "/new");
if (stat(dir, &st_tmp) || !S_ISDIR(st_tmp.st_mode)) return 0;
st_ret.st_mtime = st_tmp.st_mtime;
#if THERE_IS_EXACTLY_ONE_MAILDIR_IN_MAILPATH
{
static struct stat st_new_last;
/* Optimization - if new/ didn't change, nothing else did. */
if (st_tmp.st_dev == st_new_last.st_dev &&
st_tmp.st_ino == st_new_last.st_ino &&
st_tmp.st_atime == st_new_last.st_atime &&
st_tmp.st_mtime == st_new_last.st_mtime) {
*st = st_ret_last;
return 0;
}
st_new_last = st_tmp;
}
#endif
/* Loop over new/ and cur/ */
for (i = 0; i < 2; i++) {
dir[plen] = 0;
dir = appstr(dir, i ? "/cur" : "/new");
if ((dd = opendir(dir)) == NULL) {
zsfree(file);
zsfree(dir);
return 0;
}
dlen = strlen(dir) + 1; /* include the "/" */
while ((fn = readdir(dd)) != NULL) {
if (fn->d_name[0] == '.')
continue;
if (file) {
file[dlen] = 0;
file = appstr(file, fn->d_name);
} else {
file = tricat(dir, "/", fn->d_name);
}
if (stat(file, &st_tmp) != 0)
continue;
st_ret.st_size += st_tmp.st_size;
st_ret.st_blocks++;
if (st_tmp.st_atime != st_tmp.st_mtime &&
st_tmp.st_atime > atime)
atime = st_tmp.st_atime;
if (st_tmp.st_mtime > mtime)
mtime = st_tmp.st_mtime;
}
closedir(dd);
}
zsfree(file);
zsfree(dir);
if (atime) st_ret.st_atime = atime;
if (mtime) st_ret.st_mtime = mtime;
*st = st_ret_last = st_ret;
return 0;
}
#endif
|