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
|
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Routines which deal with the characteristics of the terminal.
* Uses termcap to be as terminal-independent as possible.
*/
#include "less.h"
#include "cmd.h"
#if MSDOS_COMPILER
#include "pckeys.h"
#if MSDOS_COMPILER==MSOFTC
#include <graph.h>
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
#include <conio.h>
#if MSDOS_COMPILER==DJGPPC
#include <pc.h>
extern int fd0;
#endif
#else
#if MSDOS_COMPILER==WIN32C
#include <windows.h>
#endif
#endif
#endif
#include <time.h>
#ifndef FOREGROUND_BLUE
#define FOREGROUND_BLUE 0x0001
#endif
#ifndef FOREGROUND_GREEN
#define FOREGROUND_GREEN 0x0002
#endif
#ifndef FOREGROUND_RED
#define FOREGROUND_RED 0x0004
#endif
#ifndef FOREGROUND_INTENSITY
#define FOREGROUND_INTENSITY 0x0008
#endif
#else
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
#include <termios.h>
#else
#if HAVE_TERMIO_H
#include <termio.h>
#else
#if HAVE_SGSTAT_H
#include <sgstat.h>
#else
#include <sgtty.h>
#endif
#endif
#endif
#if HAVE_NCURSESW_TERMCAP_H
#include <ncursesw/termcap.h>
#else
#if HAVE_NCURSES_TERMCAP_H
#include <ncurses/termcap.h>
#else
#if HAVE_TERMCAP_H
#include <termcap.h>
#endif
#endif
#endif
#ifdef _OSK
#include <signal.h>
#endif
#if OS2
#include <sys/signal.h>
#include "pckeys.h"
#endif
#if HAVE_SYS_STREAM_H
#include <sys/stream.h>
#endif
#if HAVE_SYS_PTEM_H
#include <sys/ptem.h>
#endif
#endif /* MSDOS_COMPILER */
/*
* Check for broken termios package that forces you to manually
* set the line discipline.
*/
#ifdef __ultrix__
#define MUST_SET_LINE_DISCIPLINE 1
#else
#define MUST_SET_LINE_DISCIPLINE 0
#endif
#if OS2
#define DEFAULT_TERM "ansi"
static char *windowid;
#else
#define DEFAULT_TERM "unknown"
#endif
#if MSDOS_COMPILER==MSOFTC
static int videopages;
static long msec_loops;
static int flash_created = 0;
#define SET_FG_COLOR(fg) _settextcolor(fg)
#define SET_BG_COLOR(bg) _setbkcolor(bg)
#define SETCOLORS(fg,bg) { SET_FG_COLOR(fg); SET_BG_COLOR(bg); }
#endif
#if MSDOS_COMPILER==BORLANDC
static unsigned short *whitescreen;
static int flash_created = 0;
#endif
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
#define _settextposition(y,x) gotoxy(x,y)
#define _clearscreen(m) clrscr()
#define _outtext(s) cputs(s)
#define SET_FG_COLOR(fg) textcolor(fg)
#define SET_BG_COLOR(bg) textbackground(bg)
#define SETCOLORS(fg,bg) { SET_FG_COLOR(fg); SET_BG_COLOR(bg); }
extern int sc_height;
#endif
#if MSDOS_COMPILER==WIN32C
#define UTF8_MAX_LENGTH 4
static WORD curr_attr;
static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
extern int utf_mode;
extern int quitting;
static void win32_init_term();
static void win32_deinit_term();
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4
#endif
#define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
#define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
#define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
#define APPLY_COLORS() { if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
error("SETCOLORS failed", NULL_PARG); }
#define SET_FG_COLOR(fg) { curr_attr &= ~0x0f; curr_attr |= (fg); APPLY_COLORS(); }
#define SET_BG_COLOR(bg) { curr_attr &= ~0xf0; curr_attr |= ((bg)<<4); APPLY_COLORS(); }
#define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); APPLY_COLORS(); }
#endif
#if MSDOS_COMPILER
public int nm_fg_color = CV_ERROR; /* Color of normal text */
public int nm_bg_color = CV_ERROR;
public int nm_attr = 0;
public int bo_fg_color = CV_ERROR; /* Color of bold text */
public int bo_bg_color = CV_ERROR;
public int bo_attr = 0;
public int ul_fg_color = CV_ERROR; /* Color of underlined text */
public int ul_bg_color = CV_ERROR;
public int ul_attr = 0;
public int so_fg_color = CV_ERROR; /* Color of standout text */
public int so_bg_color = CV_ERROR;
public int so_attr = 0;
public int bl_fg_color = CV_ERROR; /* Color of blinking text */
public int bl_bg_color = CV_ERROR;
public int bl_attr = 0;
static int sy_fg_color; /* Color of system text (before less) */
static int sy_bg_color;
public int sgr_mode; /* Honor ANSI sequences rather than using above */
#if MSDOS_COMPILER==WIN32C
static DWORD init_console_output_mode;
extern DWORD init_console_input_mode;
extern DWORD curr_console_input_mode;
extern DWORD base_console_input_mode;
extern DWORD mouse_console_input_mode;
public int vt_enabled = -1; /* Is virtual terminal processing available? */
#endif
#else
/*
* Strings passed to tputs() to do various terminal functions.
*/
static constant char
*sc_pad, /* Pad string */
*sc_home, /* Cursor home */
*sc_addline, /* Add line, scroll down following lines */
*sc_lower_left, /* Cursor to last line, first column */
*sc_return, /* Cursor to beginning of current line */
*sc_move, /* General cursor positioning */
*sc_clear, /* Clear screen */
*sc_eol_clear, /* Clear to end of line */
*sc_eos_clear, /* Clear to end of screen */
*sc_s_in, /* Enter standout (highlighted) mode */
*sc_s_out, /* Exit standout mode */
*sc_u_in, /* Enter underline mode */
*sc_u_out, /* Exit underline mode */
*sc_b_in, /* Enter bold mode */
*sc_b_out, /* Exit bold mode */
*sc_bl_in, /* Enter blink mode */
*sc_bl_out, /* Exit blink mode */
*sc_visual_bell, /* Visual bell (flash screen) sequence */
*sc_backspace, /* Backspace cursor */
*sc_s_keypad, /* Start keypad mode */
*sc_e_keypad, /* End keypad mode */
*sc_s_mousecap, /* Start mouse capture mode */
*sc_e_mousecap, /* End mouse capture mode */
*sc_init, /* Startup terminal initialization */
*sc_deinit; /* Exit terminal de-initialization */
static int attrcolor = -1;
#endif
static int init_done = 0;
public int auto_wrap; /* Terminal does \r\n when write past margin */
public int ignaw; /* Terminal ignores \n immediately after wrap */
public int erase_char; /* The user's erase char */
public int erase2_char; /* The user's other erase char */
public int kill_char; /* The user's line-kill char */
public int werase_char; /* The user's word-erase char */
public int sc_width, sc_height; /* Height & width of screen */
public int bo_s_width, bo_e_width; /* Printing width of boldface seq */
public int ul_s_width, ul_e_width; /* Printing width of underline seq */
public int so_s_width, so_e_width; /* Printing width of standout seq */
public int bl_s_width, bl_e_width; /* Printing width of blink seq */
public int above_mem, below_mem; /* Memory retained above/below screen */
public int can_goto_line; /* Can move cursor to any line */
public int clear_bg; /* Clear fills with background color */
public int missing_cap = 0; /* Some capability is missing */
public constant char *kent = NULL; /* Keypad ENTER sequence */
public lbool term_init_done = FALSE;
public lbool full_screen = TRUE;
static int attrmode = AT_NORMAL;
static int termcap_debug = -1;
static int no_alt_screen; /* sc_init does not switch to alt screen */
extern int binattr;
extern int one_screen;
#if !MSDOS_COMPILER
static constant char *cheaper(constant char *t1, constant char *t2, constant char *def);
static void tmodes(constant char *incap, constant char *outcap, constant char **instr,
constant char **outstr, constant char *def_instr, constant char *def_outstr, char **spp);
#endif
/*
* These two variables are sometimes defined in,
* and needed by, the termcap library.
*/
#if MUST_DEFINE_OSPEED
extern short ospeed; /* Terminal output baud rate */
extern char PC; /* Pad character */
#endif
#ifdef _OSK
short ospeed;
char PC_, *UP, *BC;
#endif
extern int quiet; /* If VERY_QUIET, use visual bell for bell */
extern int no_vbell;
extern int no_back_scroll;
extern int no_init;
extern int no_keypad;
extern int sigs;
extern int top_scroll;
extern int quit_if_one_screen;
extern int oldbot;
extern int mousecap;
extern int is_tty;
extern int use_color;
#if HILITE_SEARCH
extern int hilite_search;
#endif
#if MSDOS_COMPILER==WIN32C
extern int wscroll;
extern HANDLE tty;
#else
extern int tty;
#endif
#if (HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS) || defined(TCGETA)
/*
* Set termio flags for use by less.
*/
static void set_termio_flags(
#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
struct termios *s
#else
struct termio *s
#endif
)
{
s->c_lflag &= ~(0
#ifdef ICANON
| ICANON
#endif
#ifdef ECHO
| ECHO
#endif
#ifdef ECHOE
| ECHOE
#endif
#ifdef ECHOK
| ECHOK
#endif
#ifdef ECHONL
| ECHONL
#endif
);
s->c_oflag |= (0
#ifdef OXTABS
| OXTABS
#else
#ifdef TAB3
| TAB3
#else
#ifdef XTABS
| XTABS
#endif
#endif
#endif
#ifdef OPOST
| OPOST
#endif
#ifdef ONLCR
| ONLCR
#endif
);
s->c_oflag &= ~(0
#ifdef ONOEOT
| ONOEOT
#endif
#ifdef OCRNL
| OCRNL
#endif
#ifdef ONOCR
| ONOCR
#endif
#ifdef ONLRET
| ONLRET
#endif
);
}
#endif
/*
* Change terminal to "raw mode", or restore to "normal" mode.
* "Raw mode" means
* 1. An outstanding read will complete on receipt of a single keystroke.
* 2. Input is not echoed.
* 3. On output, \n is mapped to \r\n.
* 4. \t is NOT expanded into spaces.
* 5. Signal-causing characters such as ctrl-C (interrupt),
* etc. are NOT disabled.
* It doesn't matter whether an input \n is mapped to \r, or vice versa.
*/
public void raw_mode(int on)
{
static int curr_on = 0;
if (on == curr_on)
return;
erase2_char = '\b'; /* in case OS doesn't know about erase2 */
#if LESSTEST
if (is_lesstest())
{
/* {{ For consistent conditions when running tests. }} */
erase_char = '\b';
kill_char = CONTROL('U');
werase_char = CONTROL('W');
} else
#endif /*LESSTEST*/
#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
{
struct termios s;
static struct termios save_term;
static int saved_term = 0;
if (on)
{
/*
* Get terminal modes.
*/
if (tcgetattr(tty, &s) < 0)
{
erase_char = '\b';
kill_char = CONTROL('U');
werase_char = CONTROL('W');
} else
{
/*
* Save modes and set certain variables dependent on modes.
*/
if (!saved_term)
{
save_term = s;
saved_term = 1;
}
#if HAVE_OSPEED
switch (cfgetospeed(&s))
{
#ifdef B0
case B0: ospeed = 0; break;
#endif
#ifdef B50
case B50: ospeed = 1; break;
#endif
#ifdef B75
case B75: ospeed = 2; break;
#endif
#ifdef B110
case B110: ospeed = 3; break;
#endif
#ifdef B134
case B134: ospeed = 4; break;
#endif
#ifdef B150
case B150: ospeed = 5; break;
#endif
#ifdef B200
case B200: ospeed = 6; break;
#endif
#ifdef B300
case B300: ospeed = 7; break;
#endif
#ifdef B600
case B600: ospeed = 8; break;
#endif
#ifdef B1200
case B1200: ospeed = 9; break;
#endif
#ifdef B1800
case B1800: ospeed = 10; break;
#endif
#ifdef B2400
case B2400: ospeed = 11; break;
#endif
#ifdef B4800
case B4800: ospeed = 12; break;
#endif
#ifdef B9600
case B9600: ospeed = 13; break;
#endif
#ifdef EXTA
case EXTA: ospeed = 14; break;
#endif
#ifdef EXTB
case EXTB: ospeed = 15; break;
#endif
#ifdef B57600
case B57600: ospeed = 16; break;
#endif
#ifdef B115200
case B115200: ospeed = 17; break;
#endif
default: ;
}
#endif
erase_char = s.c_cc[VERASE];
#ifdef VERASE2
erase2_char = s.c_cc[VERASE2];
#endif
kill_char = s.c_cc[VKILL];
#ifdef VWERASE
werase_char = s.c_cc[VWERASE];
#else
werase_char = CONTROL('W');
#endif
/*
* Set the modes to the way we want them.
*/
set_termio_flags(&s);
s.c_cc[VMIN] = 1;
s.c_cc[VTIME] = 0;
#ifdef VLNEXT
s.c_cc[VLNEXT] = 0;
#endif
#ifdef VDSUSP
s.c_cc[VDSUSP] = 0;
#endif
#ifdef VSTOP
s.c_cc[VSTOP] = 0;
#endif
#ifdef VSTART
s.c_cc[VSTART] = 0;
#endif
#ifdef VDISCARD
s.c_cc[VDISCARD] = 0;
#endif
#if MUST_SET_LINE_DISCIPLINE
/*
* System's termios is broken; need to explicitly
* request TERMIODISC line discipline.
*/
s.c_line = TERMIODISC;
#endif
}
} else
{
/*
* Restore saved modes.
*/
s = save_term;
}
#if HAVE_FSYNC
fsync(tty);
#endif
tcsetattr(tty, TCSADRAIN, &s);
#if MUST_SET_LINE_DISCIPLINE
if (!on)
{
/*
* Broken termios *ignores* any line discipline
* except TERMIODISC. A different old line discipline
* is therefore not restored, yet. Restore the old
* line discipline by hand.
*/
ioctl(tty, TIOCSETD, &save_term.c_line);
}
#endif
}
#else
#ifdef TCGETA
{
struct termio s;
static struct termio save_term;
static int saved_term = 0;
if (on)
{
/*
* Get terminal modes.
*/
ioctl(tty, TCGETA, &s);
/*
* Save modes and set certain variables dependent on modes.
*/
if (!saved_term)
{
save_term = s;
saved_term = 1;
}
#if HAVE_OSPEED
ospeed = s.c_cflag & CBAUD;
#endif
erase_char = s.c_cc[VERASE];
kill_char = s.c_cc[VKILL];
#ifdef VWERASE
werase_char = s.c_cc[VWERASE];
#else
werase_char = CONTROL('W');
#endif
/*
* Set the modes to the way we want them.
*/
set_termio_flags(&s);
s.c_cc[VMIN] = 1;
s.c_cc[VTIME] = 0;
#ifdef VSTOP
s.c_cc[VSTOP] = 0;
#endif
#ifdef VSTART
s.c_cc[VSTART] = 0;
#endif
} else
{
/*
* Restore saved modes.
*/
s = save_term;
}
ioctl(tty, TCSETAW, &s);
}
#else
#ifdef TIOCGETP
{
struct sgttyb s;
static struct sgttyb save_term;
static int saved_term = 0;
if (on)
{
/*
* Get terminal modes.
*/
ioctl(tty, TIOCGETP, &s);
/*
* Save modes and set certain variables dependent on modes.
*/
if (!saved_term)
{
save_term = s;
saved_term = 1;
}
#if HAVE_OSPEED
ospeed = s.sg_ospeed;
#endif
erase_char = s.sg_erase;
kill_char = s.sg_kill;
werase_char = CONTROL('W');
/*
* Set the modes to the way we want them.
*/
s.sg_flags |= CBREAK;
s.sg_flags &= ~(ECHO|XTABS);
} else
{
/*
* Restore saved modes.
*/
s = save_term;
}
ioctl(tty, TIOCSETN, &s);
}
#else
#ifdef _OSK
{
struct sgbuf s;
static struct sgbuf save_term;
static int saved_term = 0;
if (on)
{
/*
* Get terminal modes.
*/
_gs_opt(tty, &s);
/*
* Save modes and set certain variables dependent on modes.
*/
if (!saved_term)
{
save_term = s;
saved_term = 1;
}
erase_char = s.sg_bspch;
kill_char = s.sg_dlnch;
werase_char = CONTROL('W');
/*
* Set the modes to the way we want them.
*/
s.sg_echo = 0;
s.sg_eofch = 0;
s.sg_pause = 0;
s.sg_psch = 0;
} else
{
/*
* Restore saved modes.
*/
s = save_term;
}
_ss_opt(tty, &s);
}
#else
/* MS-DOS, Windows, or OS2 */
#if OS2
/* OS2 */
LSIGNAL(SIGINT, SIG_IGN);
#endif
erase_char = '\b';
#if MSDOS_COMPILER==DJGPPC
kill_char = CONTROL('U');
/*
* So that when we shell out or run another program, its
* stdin is in cooked mode. We do not switch stdin to binary
* mode if fd0 is zero, since that means we were called before
* tty was reopened in open_getchr, in which case we would be
* changing the original stdin device outside less.
*/
if (fd0 != 0)
setmode(0, on ? O_BINARY : O_TEXT);
#else
kill_char = ESC;
#endif
werase_char = CONTROL('W');
#endif
#endif
#endif
#endif
curr_on = on;
}
#if !MSDOS_COMPILER
/*
* Some glue to prevent calling termcap functions if tgetent() failed.
*/
static int hardcopy;
static constant char * ltget_env(constant char *capname)
{
char name[64];
if (termcap_debug)
{
struct env { struct env *next; char *name; char *value; };
static struct env *envs = NULL;
struct env *p;
for (p = envs; p != NULL; p = p->next)
if (strcmp(p->name, capname) == 0)
return p->value;
p = (struct env *) ecalloc(1, sizeof(struct env));
p->name = save(capname);
p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
sprintf(p->value, "<%s>", capname);
p->next = envs;
envs = p;
return p->value;
}
SNPRINTF1(name, sizeof(name), "LESS_TERMCAP_%s", capname);
return (lgetenv(name));
}
static int ltgetflag(constant char *capname)
{
constant char *s;
if ((s = ltget_env(capname)) != NULL)
return (*s != '\0' && *s != '0');
if (hardcopy)
return (0);
return (tgetflag(capname));
}
static int ltgetnum(constant char *capname)
{
constant char *s;
if ((s = ltget_env(capname)) != NULL)
return (atoi(s));
if (hardcopy)
return (-1);
return (tgetnum(capname));
}
static constant char * ltgetstr(constant char *capname, char **pp)
{
constant char *s;
if ((s = ltget_env(capname)) != NULL)
return (s);
if (hardcopy)
return (NULL);
return (tgetstr(capname, pp));
}
#endif /* MSDOS_COMPILER */
/*
* Get size of the output screen.
*/
static void scrsize(void)
{
constant char *s;
int sys_height;
int sys_width;
#if !MSDOS_COMPILER
int n;
#endif
#define DEF_SC_WIDTH 80
#if MSDOS_COMPILER
#define DEF_SC_HEIGHT 25
#else
#define DEF_SC_HEIGHT 24
#endif
sys_width = sys_height = 0;
#if LESSTEST
if (0) /* can't use is_lesstest(): ttyin_name may not be set by scan_option yet */
#endif /*LESSTEST*/
{
#if MSDOS_COMPILER==MSOFTC
{
struct videoconfig w;
_getvideoconfig(&w);
sys_height = w.numtextrows;
sys_width = w.numtextcols;
}
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
{
struct text_info w;
gettextinfo(&w);
sys_height = w.screenheight;
sys_width = w.screenwidth;
}
#else
#if MSDOS_COMPILER==WIN32C
{
CONSOLE_SCREEN_BUFFER_INFO scr;
GetConsoleScreenBufferInfo(con_out, &scr);
sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
}
#else
#if OS2
{
int s[2];
_scrsize(s);
sys_width = s[0];
sys_height = s[1];
/*
* When using terminal emulators for XFree86/OS2, the
* _scrsize function does not work well.
* Call the scrsize.exe program to get the window size.
*/
windowid = getenv("WINDOWID");
if (windowid != NULL)
{
FILE *fd = popen("scrsize", "rt");
if (fd != NULL)
{
int w, h;
fscanf(fd, "%i %i", &w, &h);
if (w > 0 && h > 0)
{
sys_width = w;
sys_height = h;
}
pclose(fd);
}
}
}
#else
#ifdef TIOCGWINSZ
{
struct winsize w;
if (ioctl(2, TIOCGWINSZ, &w) == 0)
{
if (w.ws_row > 0)
sys_height = w.ws_row;
if (w.ws_col > 0)
sys_width = w.ws_col;
}
}
#else
#ifdef WIOCGETD
{
struct uwdata w;
if (ioctl(2, WIOCGETD, &w) == 0)
{
if (w.uw_height > 0)
sys_height = w.uw_height / w.uw_vs;
if (w.uw_width > 0)
sys_width = w.uw_width / w.uw_hs;
}
}
#endif
#endif
#endif
#endif
#endif
#endif
}
if (sys_height > 0)
sc_height = sys_height;
else if ((s = lgetenv("LINES")) != NULL)
sc_height = atoi(s);
#if !MSDOS_COMPILER
else if ((n = ltgetnum("li")) > 0)
sc_height = n;
#endif
if ((s = lgetenv("LESS_LINES")) != NULL)
{
int height = atoi(s);
sc_height = (height < 0) ? sc_height + height : height;
full_screen = FALSE;
}
if (sc_height <= 0)
sc_height = DEF_SC_HEIGHT;
if (sys_width > 0)
sc_width = sys_width;
else if ((s = lgetenv("COLUMNS")) != NULL)
sc_width = atoi(s);
#if !MSDOS_COMPILER
else if ((n = ltgetnum("co")) > 0)
sc_width = n;
#endif
if ((s = lgetenv("LESS_COLUMNS")) != NULL)
{
int width = atoi(s);
sc_width = (width < 0) ? sc_width + width : width;
}
if (sc_width <= 0)
sc_width = DEF_SC_WIDTH;
screen_size_changed();
}
/*
* Recalculate things that depend on the screen size.
*/
public void screen_size_changed(void)
{
calc_jump_sline();
calc_shift_count();
calc_match_shift();
}
#if MSDOS_COMPILER==MSOFTC
/*
* Figure out how many empty loops it takes to delay a millisecond.
*/
static void get_clock(void)
{
clock_t start;
/*
* Get synchronized at the start of a tick.
*/
start = clock();
while (clock() == start)
;
/*
* Now count loops till the next tick.
*/
start = clock();
msec_loops = 0;
while (clock() == start)
msec_loops++;
/*
* Convert from (loops per clock) to (loops per millisecond).
*/
msec_loops *= CLOCKS_PER_SEC;
msec_loops /= 1000;
}
/*
* Delay for a specified number of milliseconds.
*/
static void delay(int msec)
{
long i;
while (msec-- > 0)
{
for (i = 0; i < msec_loops; i++)
(void) clock();
}
}
#endif
/*
* Return the characters actually input by a "special" key.
*/
public constant char * special_key_str(int key)
{
static char tbuf[40];
constant char *s;
#if MSDOS_COMPILER || OS2
static char k_right[] = { '\340', PCK_RIGHT, 0 };
static char k_left[] = { '\340', PCK_LEFT, 0 };
static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
static char k_insert[] = { '\340', PCK_INSERT, 0 };
static char k_delete[] = { '\340', PCK_DELETE, 0 };
static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
static char k_ctl_backspace[] = { '\177', 0 };
static char k_backspace[] = { '\b', 0 };
static char k_home[] = { '\340', PCK_HOME, 0 };
static char k_end[] = { '\340', PCK_END, 0 };
static char k_up[] = { '\340', PCK_UP, 0 };
static char k_down[] = { '\340', PCK_DOWN, 0 };
static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
static char k_f1[] = { '\340', PCK_F1, 0 };
#endif
#if !MSDOS_COMPILER
char *sp = tbuf;
#endif
switch (key)
{
#if OS2
/*
* If windowid is not NULL, assume less is executed in
* the XFree86 environment.
*/
case SK_RIGHT_ARROW:
s = windowid ? ltgetstr("kr", &sp) : k_right;
break;
case SK_LEFT_ARROW:
s = windowid ? ltgetstr("kl", &sp) : k_left;
break;
case SK_UP_ARROW:
s = windowid ? ltgetstr("ku", &sp) : k_up;
break;
case SK_DOWN_ARROW:
s = windowid ? ltgetstr("kd", &sp) : k_down;
break;
case SK_PAGE_UP:
s = windowid ? ltgetstr("kP", &sp) : k_pageup;
break;
case SK_PAGE_DOWN:
s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
break;
case SK_HOME:
s = windowid ? ltgetstr("kh", &sp) : k_home;
break;
case SK_END:
s = windowid ? ltgetstr("@7", &sp) : k_end;
break;
case SK_DELETE:
s = windowid ? ltgetstr("kD", &sp) : k_delete;
if (s == NULL)
{
tbuf[0] = '\177';
tbuf[1] = '\0';
s = tbuf;
}
break;
#endif
#if MSDOS_COMPILER
case SK_RIGHT_ARROW:
s = k_right;
break;
case SK_LEFT_ARROW:
s = k_left;
break;
case SK_UP_ARROW:
s = k_up;
break;
case SK_DOWN_ARROW:
s = k_down;
break;
case SK_PAGE_UP:
s = k_pageup;
break;
case SK_PAGE_DOWN:
s = k_pagedown;
break;
case SK_HOME:
s = k_home;
break;
case SK_END:
s = k_end;
break;
case SK_DELETE:
s = k_delete;
break;
#endif
#if MSDOS_COMPILER || OS2
case SK_INSERT:
s = k_insert;
break;
case SK_CTL_LEFT_ARROW:
s = k_ctl_left;
break;
case SK_CTL_RIGHT_ARROW:
s = k_ctl_right;
break;
case SK_CTL_BACKSPACE:
s = k_ctl_backspace;
break;
case SK_CTL_DELETE:
s = k_ctl_delete;
break;
case SK_BACKSPACE:
s = k_backspace;
break;
case SK_F1:
s = k_f1;
break;
case SK_BACKTAB:
s = k_backtab;
break;
#else
case SK_RIGHT_ARROW:
s = ltgetstr("kr", &sp);
break;
case SK_LEFT_ARROW:
s = ltgetstr("kl", &sp);
break;
case SK_UP_ARROW:
s = ltgetstr("ku", &sp);
break;
case SK_DOWN_ARROW:
s = ltgetstr("kd", &sp);
break;
case SK_PAGE_UP:
s = ltgetstr("kP", &sp);
break;
case SK_PAGE_DOWN:
s = ltgetstr("kN", &sp);
break;
case SK_HOME:
s = ltgetstr("kh", &sp);
break;
case SK_END:
s = ltgetstr("@7", &sp);
break;
case SK_DELETE:
s = ltgetstr("kD", &sp);
if (s == NULL)
{
tbuf[0] = '\177';
tbuf[1] = '\0';
s = tbuf;
}
break;
case SK_BACKSPACE:
s = ltgetstr("kb", &sp);
if (s == NULL)
{
tbuf[0] = '\b';
tbuf[1] = '\0';
s = tbuf;
}
break;
#endif
case SK_CONTROL_K:
tbuf[0] = CONTROL('K');
tbuf[1] = '\0';
s = tbuf;
break;
default:
return (NULL);
}
return (s);
}
#if MSDOS_COMPILER
public void init_win_colors(void)
{
if (nm_fg_color == CV_ERROR || nm_fg_color == CV_NOCHANGE) nm_fg_color = sy_fg_color;
if (nm_bg_color == CV_ERROR || nm_bg_color == CV_NOCHANGE) nm_bg_color = sy_bg_color;
if (bo_fg_color == CV_NOCHANGE) bo_fg_color = sy_fg_color; else if (bo_fg_color == CV_ERROR) bo_fg_color = sy_fg_color | 8;
if (bo_bg_color == CV_NOCHANGE) bo_bg_color = sy_bg_color; else if (bo_bg_color == CV_ERROR) bo_bg_color = sy_bg_color;
if (ul_fg_color == CV_NOCHANGE) ul_fg_color = sy_fg_color; else if (ul_fg_color == CV_ERROR) ul_fg_color = (sy_bg_color == 3 || sy_bg_color == 11) ? 0 : 11;
if (ul_bg_color == CV_NOCHANGE) ul_bg_color = sy_bg_color; else if (ul_bg_color == CV_ERROR) ul_bg_color = sy_bg_color;
if (so_fg_color == CV_NOCHANGE) so_fg_color = sy_fg_color; else if (so_fg_color == CV_ERROR) so_fg_color = sy_bg_color;
if (so_bg_color == CV_NOCHANGE) so_bg_color = sy_bg_color; else if (so_bg_color == CV_ERROR) so_bg_color = sy_fg_color;
if (bl_fg_color == CV_NOCHANGE) bl_fg_color = sy_fg_color; else if (bl_fg_color == CV_ERROR) bl_fg_color = ul_bg_color;
if (bl_bg_color == CV_NOCHANGE) bl_bg_color = sy_bg_color; else if (bl_bg_color == CV_ERROR) bl_bg_color = ul_fg_color;
nm_fg_color |= nm_attr;
bo_fg_color |= bo_attr;
ul_fg_color |= ul_attr;
so_fg_color |= so_attr;
bl_fg_color |= bl_attr;
}
#endif /* MSDOS_COMPILER */
/*
* Get terminal capabilities via termcap.
*/
public void get_term(void)
{
termcap_debug = !isnullenv(lgetenv("LESS_TERMCAP_DEBUG"));
#if MSDOS_COMPILER
auto_wrap = 1;
ignaw = 0;
can_goto_line = 1;
clear_bg = 1;
/*
* Set up default colors.
* The xx_s_width and xx_e_width vars are already initialized to 0.
*/
#if MSDOS_COMPILER==MSOFTC
sy_bg_color = _getbkcolor();
sy_fg_color = _gettextcolor();
get_clock();
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
{
struct text_info w;
gettextinfo(&w);
sy_bg_color = (w.attribute >> 4) & 0x0F;
sy_fg_color = (w.attribute >> 0) & 0x0F;
}
#else
#if MSDOS_COMPILER==WIN32C
{
CONSOLE_SCREEN_BUFFER_INFO scr;
con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
/*
* Always open stdin in binary. Note this *must* be done
* before any file operations have been done on fd0.
*/
SET_BINARY(0);
GetConsoleMode(con_out, &init_console_output_mode);
GetConsoleScreenBufferInfo(con_out, &scr);
curr_attr = scr.wAttributes;
sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
sy_fg_color = curr_attr & FG_COLORS;
}
#endif
#endif
#endif
init_win_colors();
/*
* Get size of the screen.
*/
scrsize();
pos_init();
#else /* !MSDOS_COMPILER */
{
char *sp;
constant char *t1;
constant char *t2;
constant char *term;
/*
* Some termcap libraries assume termbuf is static
* (accessible after tgetent returns).
*/
static char termbuf[TERMBUF_SIZE];
static char sbuf[TERMSBUF_SIZE];
#if OS2
/*
* Make sure the termcap database is available.
*/
constant char *cp = lgetenv("TERMCAP");
if (isnullenv(cp))
{
char *termcap;
if ((sp = homefile("termcap.dat")) != NULL)
{
termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
sprintf(termcap, "TERMCAP=%s", sp);
free(sp);
putenv(termcap);
}
}
#endif
/*
* Find out what kind of terminal this is.
*/
if ((term = lgetenv("TERM")) == NULL)
term = DEFAULT_TERM;
hardcopy = 0;
/* {{ Should probably just pass NULL instead of termbuf. }} */
if (tgetent(termbuf, term) != TGETENT_OK)
hardcopy = 1;
if (ltgetflag("hc"))
hardcopy = 1;
/*
* Get size of the screen.
*/
scrsize();
pos_init();
auto_wrap = ltgetflag("am");
ignaw = ltgetflag("xn");
above_mem = ltgetflag("da");
below_mem = ltgetflag("db");
clear_bg = ltgetflag("ut");
no_alt_screen = ltgetflag("NR");
/*
* Assumes termcap variable "sg" is the printing width of:
* the standout sequence, the end standout sequence,
* the underline sequence, the end underline sequence,
* the boldface sequence, and the end boldface sequence.
*/
if ((so_s_width = ltgetnum("sg")) < 0)
so_s_width = 0;
so_e_width = so_s_width;
bo_s_width = bo_e_width = so_s_width;
ul_s_width = ul_e_width = so_s_width;
bl_s_width = bl_e_width = so_s_width;
#if HILITE_SEARCH
if (so_s_width > 0 || so_e_width > 0)
/*
* Disable highlighting by default on magic cookie terminals.
* Turning on highlighting might change the displayed width
* of a line, causing the display to get messed up.
* The user can turn it back on with -g,
* but she won't like the results.
*/
hilite_search = 0;
#endif
/*
* Get various string-valued capabilities.
*/
sp = sbuf;
#if HAVE_OSPEED
sc_pad = ltgetstr("pc", &sp);
if (sc_pad != NULL)
PC = *sc_pad;
#endif
sc_s_keypad = ltgetstr("ks", &sp);
if (sc_s_keypad == NULL)
sc_s_keypad = "";
sc_e_keypad = ltgetstr("ke", &sp);
if (sc_e_keypad == NULL)
sc_e_keypad = "";
kent = ltgetstr("@8", &sp);
sc_s_mousecap = ltgetstr("MOUSE_START", &sp);
if (sc_s_mousecap == NULL)
sc_s_mousecap = ESCS "[?1000h" ESCS "[?1006h";
sc_e_mousecap = ltgetstr("MOUSE_END", &sp);
if (sc_e_mousecap == NULL)
sc_e_mousecap = ESCS "[?1006l" ESCS "[?1000l";
sc_init = ltgetstr("ti", &sp);
if (sc_init == NULL)
sc_init = "";
sc_deinit= ltgetstr("te", &sp);
if (sc_deinit == NULL)
sc_deinit = "";
sc_eol_clear = ltgetstr("ce", &sp);
if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
{
missing_cap = 1;
sc_eol_clear = "";
}
sc_eos_clear = ltgetstr("cd", &sp);
if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
{
missing_cap = 1;
sc_eos_clear = "";
}
sc_clear = ltgetstr("cl", &sp);
if (sc_clear == NULL || *sc_clear == '\0')
{
missing_cap = 1;
sc_clear = "\n\n";
}
sc_move = ltgetstr("cm", &sp);
if (sc_move == NULL || *sc_move == '\0')
{
/*
* This is not an error here, because we don't
* always need sc_move.
* We need it only if we don't have home or lower-left.
*/
sc_move = "";
can_goto_line = 0;
} else
can_goto_line = 1;
tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
sc_visual_bell = ltgetstr("vb", &sp);
if (sc_visual_bell == NULL)
sc_visual_bell = "";
if (ltgetflag("bs"))
sc_backspace = "\b";
else
{
sc_backspace = ltgetstr("bc", &sp);
if (sc_backspace == NULL || *sc_backspace == '\0')
sc_backspace = "\b";
}
/*
* Choose between using "ho" and "cm" ("home" and "cursor move")
* to move the cursor to the upper left corner of the screen.
*/
t1 = ltgetstr("ho", &sp);
if (t1 == NULL)
t1 = "";
if (*sc_move == '\0')
t2 = "";
else
{
strcpy(sp, tgoto(sc_move, 0, 0));
t2 = sp;
sp += strlen(sp) + 1;
}
sc_home = cheaper(t1, t2, "|\b^");
/*
* Choose between using "ll" and "cm" ("lower left" and "cursor move")
* to move the cursor to the lower left corner of the screen.
*/
t1 = ltgetstr("ll", &sp);
if (t1 == NULL || !full_screen)
t1 = "";
if (*sc_move == '\0')
t2 = "";
else
{
strcpy(sp, tgoto(sc_move, 0, sc_height-1));
t2 = sp;
sp += strlen(sp) + 1;
}
sc_lower_left = cheaper(t1, t2, "\r");
/*
* Get carriage return string.
*/
sc_return = ltgetstr("cr", &sp);
if (sc_return == NULL)
sc_return = "\r";
/*
* Choose between using "al" or "sr" ("add line" or "scroll reverse")
* to add a line at the top of the screen.
*/
t1 = ltgetstr("al", &sp);
if (t1 == NULL)
t1 = "";
t2 = ltgetstr("sr", &sp);
if (t2 == NULL)
t2 = "";
#if OS2
if (*t1 == '\0' && *t2 == '\0')
sc_addline = "";
else
#endif
if (above_mem)
sc_addline = t1;
else
sc_addline = cheaper(t1, t2, "");
if (*sc_addline == '\0')
{
/*
* Force repaint on any backward movement.
*/
no_back_scroll = 1;
}
}
#endif /* MSDOS_COMPILER */
}
#if !MSDOS_COMPILER
/*
* Return the cost of displaying a termcap string.
* We use the trick of calling tputs, but as a char printing function
* we give it inc_costcount, which just increments "costcount".
* This tells us how many chars would be printed by using this string.
* {{ Couldn't we just use strlen? }}
*/
static int costcount;
/*ARGSUSED*/
static int inc_costcount(int c)
{
costcount++;
return (c);
}
static int cost(constant char *t)
{
costcount = 0;
tputs(t, sc_height, inc_costcount);
return (costcount);
}
/*
* Return the "best" of the two given termcap strings.
* The best, if both exist, is the one with the lower
* cost (see cost() function).
*/
static constant char * cheaper(constant char *t1, constant char *t2, constant char *def)
{
if (*t1 == '\0' && *t2 == '\0')
{
missing_cap = 1;
return (def);
}
if (*t1 == '\0')
return (t2);
if (*t2 == '\0')
return (t1);
if (cost(t1) < cost(t2))
return (t1);
return (t2);
}
static void tmodes(constant char *incap, constant char *outcap, constant char **instr, constant char **outstr, constant char *def_instr, constant char *def_outstr, char **spp)
{
*instr = ltgetstr(incap, spp);
if (*instr == NULL)
{
/* Use defaults. */
*instr = def_instr;
*outstr = def_outstr;
return;
}
*outstr = ltgetstr(outcap, spp);
if (*outstr == NULL)
/* No specific out capability; use "me". */
*outstr = ltgetstr("me", spp);
if (*outstr == NULL)
/* Don't even have "me"; use a null string. */
*outstr = "";
}
#endif /* MSDOS_COMPILER */
/*
* Below are the functions which perform all the
* terminal-specific screen manipulation.
*/
#if MSDOS_COMPILER
#if MSDOS_COMPILER==WIN32C
static void _settextposition(int row, int col)
{
COORD cpos;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(con_out, &csbi);
cpos.X = csbi.srWindow.Left + (col - 1);
cpos.Y = csbi.srWindow.Top + (row - 1);
SetConsoleCursorPosition(con_out, cpos);
}
#endif
/*
* Initialize the screen to the correct color at startup.
*/
static void initcolor(void)
{
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
intensevideo();
#endif
SETCOLORS(nm_fg_color, nm_bg_color);
#if 0
/*
* This clears the screen at startup. This is different from
* the behavior of other versions of less. Disable it for now.
*/
char *blanks;
int row;
int col;
/*
* Create a complete, blank screen using "normal" colors.
*/
SETCOLORS(nm_fg_color, nm_bg_color);
blanks = (char *) ecalloc(width+1, sizeof(char));
for (col = 0; col < sc_width; col++)
blanks[col] = ' ';
blanks[sc_width] = '\0';
for (row = 0; row < sc_height; row++)
_outtext(blanks);
free(blanks);
#endif
}
#endif
#if MSDOS_COMPILER==WIN32C
/*
* Enable virtual terminal processing, if available.
*/
static void win32_init_vt_term(void)
{
DWORD console_output_mode;
if (vt_enabled == 0 || (vt_enabled == 1 && con_out == con_out_ours))
return;
console_output_mode = init_console_output_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
vt_enabled = SetConsoleMode(con_out, console_output_mode);
if (vt_enabled)
{
auto_wrap = 0;
ignaw = 1;
}
}
static void win32_deinit_vt_term(void)
{
if (vt_enabled == 1 && con_out == con_out_save)
SetConsoleMode(con_out, init_console_output_mode);
}
/*
* Termcap-like init with a private win32 console.
*/
static void win32_init_term(void)
{
CONSOLE_SCREEN_BUFFER_INFO scr;
COORD size;
if (con_out_save == INVALID_HANDLE_VALUE)
return;
GetConsoleScreenBufferInfo(con_out_save, &scr);
if (con_out_ours == INVALID_HANDLE_VALUE)
{
/*
* Create our own screen buffer, so that we
* may restore the original when done.
*/
con_out_ours = CreateConsoleScreenBuffer(
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
CONSOLE_TEXTMODE_BUFFER,
(LPVOID) NULL);
}
size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
SetConsoleScreenBufferSize(con_out_ours, size);
SetConsoleActiveScreenBuffer(con_out_ours);
con_out = con_out_ours;
}
/*
* Restore the startup console.
*/
static void win32_deinit_term(void)
{
if (con_out_save == INVALID_HANDLE_VALUE)
return;
if (quitting)
(void) CloseHandle(con_out_ours);
SetConsoleActiveScreenBuffer(con_out_save);
con_out = con_out_save;
}
#endif
#if !MSDOS_COMPILER
static void do_tputs(constant char *str, int affcnt, int (*f_putc)(int))
{
#if LESSTEST
if (is_lesstest() && f_putc == putchr)
putstr(str);
else
#endif /*LESSTEST*/
tputs(str, affcnt, f_putc);
}
/*
* Like tputs but we handle $<...> delay strings here because
* some implementations of tputs don't perform delays correctly.
*/
static void ltputs(constant char *str, int affcnt, int (*f_putc)(int))
{
while (str != NULL && *str != '\0')
{
#if HAVE_STRSTR
constant char *obrac = strstr(str, "$<");
if (obrac != NULL)
{
char str2[64];
size_t slen = ptr_diff(obrac, str);
if (slen < sizeof(str2))
{
int delay;
/* Output first part of string (before "$<"). */
memcpy(str2, str, slen);
str2[slen] = '\0';
do_tputs(str2, affcnt, f_putc);
str += slen + 2;
/* Perform the delay. */
delay = lstrtoic(str, &str, 10);
if (*str == '*')
if (ckd_mul(&delay, delay, affcnt))
delay = INT_MAX;
flush();
sleep_ms(delay);
/* Skip past closing ">" at end of delay string. */
str = strstr(str, ">");
if (str != NULL)
str++;
continue;
}
}
#endif
/* Pass the rest of the string to tputs and we're done. */
do_tputs(str, affcnt, f_putc);
break;
}
}
#endif /* MSDOS_COMPILER */
/*
* Configure the terminal so mouse clicks and wheel moves
* produce input to less.
*/
public void init_mouse(void)
{
#if !MSDOS_COMPILER
ltputs(sc_s_mousecap, sc_height, putchr);
#else
#if MSDOS_COMPILER==WIN32C
curr_console_input_mode = mouse_console_input_mode;
SetConsoleMode(tty, curr_console_input_mode);
#endif
#endif
}
/*
* Configure the terminal so mouse clicks and wheel moves
* are handled by the system (so text can be selected, etc).
*/
public void deinit_mouse(void)
{
#if !MSDOS_COMPILER
ltputs(sc_e_mousecap, sc_height, putchr);
#else
#if MSDOS_COMPILER==WIN32C
curr_console_input_mode = base_console_input_mode;
SetConsoleMode(tty, curr_console_input_mode);
#endif
#endif
}
/*
* Initialize terminal
*/
public void init(void)
{
clear_bot_if_needed();
#if !MSDOS_COMPILER
if (!(quit_if_one_screen && one_screen))
{
if (!no_init)
{
ltputs(sc_init, sc_height, putchr);
/*
* Some terminals leave the cursor unmoved when switching
* to the alt screen. To avoid having the text appear at
* a seemingly random line on the alt screen, move to
* lower left if we are using an alt screen.
*/
if (*sc_init != '\0' && *sc_deinit != '\0' && !no_alt_screen)
lower_left();
term_init_done = 1;
}
if (!no_keypad)
ltputs(sc_s_keypad, sc_height, putchr);
if (mousecap)
init_mouse();
}
init_done = 1;
if (top_scroll)
{
int i;
/*
* This is nice to terminals with no alternate screen,
* but with saved scrolled-off-the-top lines. This way,
* no previous line is lost, but we start with a whole
* screen to ourself.
*/
for (i = 1; i < sc_height; i++)
putchr('\n');
} else
line_left();
#else
#if MSDOS_COMPILER==WIN32C
if (!(quit_if_one_screen && one_screen))
{
if (!no_init)
{
win32_init_term();
term_init_done = 1;
}
if (mousecap)
init_mouse();
}
win32_init_vt_term();
#endif
init_done = 1;
initcolor();
flush();
#endif
}
/*
* Deinitialize terminal
*/
public void deinit(void)
{
if (!init_done)
return;
#if !MSDOS_COMPILER
if (!(quit_if_one_screen && one_screen))
{
if (mousecap)
deinit_mouse();
if (!no_keypad)
ltputs(sc_e_keypad, sc_height, putchr);
if (!no_init)
ltputs(sc_deinit, sc_height, putchr);
}
#else
/* Restore system colors. */
SETCOLORS(sy_fg_color, sy_bg_color);
#if MSDOS_COMPILER==WIN32C
win32_deinit_vt_term();
if (!(quit_if_one_screen && one_screen))
{
if (mousecap)
deinit_mouse();
if (!no_init)
win32_deinit_term();
}
#else
/* Need clreol to make SETCOLORS take effect. */
clreol();
#endif
#endif
init_done = 0;
}
/*
* Are we interactive (ie. writing to an initialized tty)?
*/
public int interactive(void)
{
return (is_tty && init_done);
}
static void assert_interactive(void)
{
if (interactive()) return;
/* abort(); */
}
/*
* Home cursor (move to upper left corner of screen).
*/
public void home(void)
{
assert_interactive();
#if !MSDOS_COMPILER
ltputs(sc_home, 1, putchr);
#else
flush();
_settextposition(1,1);
#endif
}
#if LESSTEST
public void dump_screen(void)
{
char dump_cmd[32];
SNPRINTF1(dump_cmd, sizeof(dump_cmd), ESCS"0;0;%dR", sc_width * sc_height);
ltputs(dump_cmd, sc_height, putchr);
flush();
}
#endif /*LESSTEST*/
/*
* Add a blank line (called with cursor at home).
* Should scroll the display down.
*/
public void add_line(void)
{
assert_interactive();
#if !MSDOS_COMPILER
ltputs(sc_addline, sc_height, putchr);
#else
flush();
#if MSDOS_COMPILER==MSOFTC
_scrolltextwindow(_GSCROLLDOWN);
_settextposition(1,1);
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
movetext(1,1, sc_width,sc_height-1, 1,2);
gotoxy(1,1);
clreol();
#else
#if MSDOS_COMPILER==WIN32C
{
CHAR_INFO fillchar;
SMALL_RECT rcSrc, rcClip;
COORD new_org;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(con_out,&csbi);
/* The clip rectangle is the entire visible screen. */
rcClip.Left = csbi.srWindow.Left;
rcClip.Top = csbi.srWindow.Top;
rcClip.Right = csbi.srWindow.Right;
rcClip.Bottom = csbi.srWindow.Bottom;
/* The source rectangle is the visible screen minus the last line. */
rcSrc = rcClip;
rcSrc.Bottom--;
/* Move the top left corner of the source window down one row. */
new_org.X = rcSrc.Left;
new_org.Y = rcSrc.Top + 1;
/* Fill the right character and attributes. */
fillchar.Char.AsciiChar = ' ';
curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
fillchar.Attributes = curr_attr;
ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
_settextposition(1,1);
}
#endif
#endif
#endif
#endif
}
#if 0
/*
* Remove the n topmost lines and scroll everything below it in the
* window upward. This is needed to stop leaking the topmost line
* into the scrollback buffer when we go down-one-line (in WIN32).
*/
public void remove_top(int n)
{
#if MSDOS_COMPILER==WIN32C
SMALL_RECT rcSrc, rcClip;
CHAR_INFO fillchar;
COORD new_org;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
if (n >= sc_height - 1)
{
clear();
home();
return;
}
flush();
GetConsoleScreenBufferInfo(con_out, &csbi);
/* Get the extent of all-visible-rows-but-the-last. */
rcSrc.Left = csbi.srWindow.Left;
rcSrc.Top = csbi.srWindow.Top + n;
rcSrc.Right = csbi.srWindow.Right;
rcSrc.Bottom = csbi.srWindow.Bottom;
/* Get the clip rectangle. */
rcClip.Left = rcSrc.Left;
rcClip.Top = csbi.srWindow.Top;
rcClip.Right = rcSrc.Right;
rcClip.Bottom = rcSrc.Bottom ;
/* Move the source window up n rows. */
new_org.X = rcSrc.Left;
new_org.Y = rcSrc.Top - n;
/* Fill the right character and attributes. */
fillchar.Char.AsciiChar = ' ';
curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
fillchar.Attributes = curr_attr;
ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
/* Position cursor on first blank line. */
goto_line(sc_height - n - 1);
#endif
}
#endif
#if MSDOS_COMPILER==WIN32C
/*
* Clear the screen.
*/
static void win32_clear(void)
{
/*
* This will clear only the currently visible rows of the NT
* console buffer, which means none of the precious scrollback
* rows are touched making for faster scrolling. Note that, if
* the window has fewer columns than the console buffer (i.e.
* there is a horizontal scrollbar as well), the entire width
* of the visible rows will be cleared.
*/
COORD topleft;
DWORD nchars;
DWORD winsz;
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* get the number of cells in the current buffer */
GetConsoleScreenBufferInfo(con_out, &csbi);
winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
topleft.X = 0;
topleft.Y = csbi.srWindow.Top;
curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
}
/*
* Remove the n topmost lines and scroll everything below it in the
* window upward.
*/
public void win32_scroll_up(int n)
{
SMALL_RECT rcSrc, rcClip;
CHAR_INFO fillchar;
COORD topleft;
COORD new_org;
DWORD nchars;
DWORD size;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (n <= 0)
return;
if (n >= sc_height - 1)
{
win32_clear();
_settextposition(1,1);
return;
}
/* Get the extent of what will remain visible after scrolling. */
GetConsoleScreenBufferInfo(con_out, &csbi);
rcSrc.Left = csbi.srWindow.Left;
rcSrc.Top = csbi.srWindow.Top + n;
rcSrc.Right = csbi.srWindow.Right;
rcSrc.Bottom = csbi.srWindow.Bottom;
/* Get the clip rectangle. */
rcClip.Left = rcSrc.Left;
rcClip.Top = csbi.srWindow.Top;
rcClip.Right = rcSrc.Right;
rcClip.Bottom = rcSrc.Bottom ;
/* Move the source text to the top of the screen. */
new_org.X = rcSrc.Left;
new_org.Y = rcClip.Top;
/* Fill the right character and attributes. */
fillchar.Char.AsciiChar = ' ';
fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
/* Scroll the window. */
SetConsoleTextAttribute(con_out, fillchar.Attributes);
ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
/* Clear remaining lines at bottom. */
topleft.X = csbi.dwCursorPosition.X;
topleft.Y = rcSrc.Bottom - n;
size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
FillConsoleOutputCharacter(con_out, ' ', size, topleft,
&nchars);
FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
&nchars);
SetConsoleTextAttribute(con_out, curr_attr);
/* Move cursor n lines up from where it was. */
csbi.dwCursorPosition.Y -= n;
SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
}
#endif
/*
* Move cursor to lower left corner of screen.
*/
public void lower_left(void)
{
assert_interactive();
#if !MSDOS_COMPILER
ltputs(sc_lower_left, 1, putchr);
#else
flush();
_settextposition(sc_height, 1);
#endif
}
/*
* Move cursor to left position of current line.
*/
public void line_left(void)
{
assert_interactive();
#if !MSDOS_COMPILER
ltputs(sc_return, 1, putchr);
#else
{
int row;
flush();
#if MSDOS_COMPILER==WIN32C
{
CONSOLE_SCREEN_BUFFER_INFO scr;
GetConsoleScreenBufferInfo(con_out, &scr);
row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
}
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
row = wherey();
#else
{
struct rccoord tpos = _gettextposition();
row = tpos.row;
}
#endif
#endif
_settextposition(row, 1);
}
#endif
}
/*
* Check if the console size has changed and reset internals
* (in lieu of SIGWINCH for WIN32).
*/
public void check_winch(void)
{
#if MSDOS_COMPILER==WIN32C
CONSOLE_SCREEN_BUFFER_INFO scr;
COORD size;
if (con_out == INVALID_HANDLE_VALUE)
return;
flush();
GetConsoleScreenBufferInfo(con_out, &scr);
size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
if (size.Y != sc_height || size.X != sc_width)
{
sc_height = size.Y;
sc_width = size.X;
if (!no_init && con_out_ours == con_out)
SetConsoleScreenBufferSize(con_out, size);
pos_init();
wscroll = (sc_height + 1) / 2;
screen_trashed();
}
#endif
}
/*
* Goto a specific line on the screen.
*/
public void goto_line(int sindex)
{
assert_interactive();
#if !MSDOS_COMPILER
ltputs(tgoto(sc_move, 0, sindex), 1, putchr);
#else
flush();
_settextposition(sindex+1, 1);
#endif
}
#if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
/*
* Create an alternate screen which is all white.
* This screen is used to create a "flash" effect, by displaying it
* briefly and then switching back to the normal screen.
* {{ Yuck! There must be a better way to get a visual bell. }}
*/
static void create_flash(void)
{
#if MSDOS_COMPILER==MSOFTC
struct videoconfig w;
char *blanks;
int row, col;
_getvideoconfig(&w);
videopages = w.numvideopages;
if (videopages < 2)
{
at_enter(AT_STANDOUT);
at_exit();
} else
{
_setactivepage(1);
at_enter(AT_STANDOUT);
blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
for (col = 0; col < w.numtextcols; col++)
blanks[col] = ' ';
for (row = w.numtextrows; row > 0; row--)
_outmem(blanks, w.numtextcols);
_setactivepage(0);
_setvisualpage(0);
free(blanks);
at_exit();
}
#else
#if MSDOS_COMPILER==BORLANDC
int n;
whitescreen = (unsigned short *)
malloc(sc_width * sc_height * sizeof(short));
if (whitescreen == NULL)
return;
for (n = 0; n < sc_width * sc_height; n++)
whitescreen[n] = 0x7020;
#endif
#endif
flash_created = 1;
}
#endif /* MSDOS_COMPILER */
/*
* Output the "visual bell", if there is one.
*/
public void vbell(void)
{
if (no_vbell)
return;
#if !MSDOS_COMPILER
if (*sc_visual_bell == '\0')
return;
ltputs(sc_visual_bell, sc_height, putchr);
#else
#if MSDOS_COMPILER==DJGPPC
ScreenVisualBell();
#else
#if MSDOS_COMPILER==MSOFTC
/*
* Create a flash screen on the second video page.
* Switch to that page, then switch back.
*/
if (!flash_created)
create_flash();
if (videopages < 2)
return;
_setvisualpage(1);
delay(100);
_setvisualpage(0);
#else
#if MSDOS_COMPILER==BORLANDC
unsigned short *currscreen;
/*
* Get a copy of the current screen.
* Display the flash screen.
* Then restore the old screen.
*/
if (!flash_created)
create_flash();
if (whitescreen == NULL)
return;
currscreen = (unsigned short *)
malloc(sc_width * sc_height * sizeof(short));
if (currscreen == NULL) return;
gettext(1, 1, sc_width, sc_height, currscreen);
puttext(1, 1, sc_width, sc_height, whitescreen);
delay(100);
puttext(1, 1, sc_width, sc_height, currscreen);
free(currscreen);
#else
#if MSDOS_COMPILER==WIN32C
/* paint screen with an inverse color */
clear();
/* leave it displayed for 100 msec. */
Sleep(100);
/* restore with a redraw */
repaint();
#endif
#endif
#endif
#endif
#endif
}
/*
* Make a noise.
*/
static void beep(void)
{
#if !MSDOS_COMPILER
putchr(CONTROL('G'));
#else
#if MSDOS_COMPILER==WIN32C
MessageBeep(0);
#else
write(1, "\7", 1);
#endif
#endif
}
/*
* Ring the terminal bell.
*/
public void bell(void)
{
if (quiet == VERY_QUIET)
vbell();
else
beep();
}
/*
* Clear the screen.
*/
public void clear(void)
{
assert_interactive();
#if !MSDOS_COMPILER
ltputs(sc_clear, sc_height, putchr);
#else
flush();
#if MSDOS_COMPILER==WIN32C
win32_clear();
#else
_clearscreen(_GCLEARSCREEN);
#endif
#endif
}
/*
* Clear from the cursor to the end of the cursor's line.
* {{ This must not move the cursor. }}
*/
public void clear_eol(void)
{
/* assert_interactive();*/
#if !MSDOS_COMPILER
ltputs(sc_eol_clear, 1, putchr);
#else
#if MSDOS_COMPILER==MSOFTC
short top, left;
short bot, right;
struct rccoord tpos;
flush();
/*
* Save current state.
*/
tpos = _gettextposition();
_gettextwindow(&top, &left, &bot, &right);
/*
* Set a temporary window to the current line,
* from the cursor's position to the right edge of the screen.
* Then clear that window.
*/
_settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
_clearscreen(_GWINDOW);
/*
* Restore state.
*/
_settextwindow(top, left, bot, right);
_settextposition(tpos.row, tpos.col);
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
flush();
clreol();
#else
#if MSDOS_COMPILER==WIN32C
DWORD nchars;
COORD cpos;
CONSOLE_SCREEN_BUFFER_INFO scr;
flush();
memset(&scr, 0, sizeof(scr));
GetConsoleScreenBufferInfo(con_out, &scr);
cpos.X = scr.dwCursorPosition.X;
cpos.Y = scr.dwCursorPosition.Y;
curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
FillConsoleOutputAttribute(con_out, curr_attr,
scr.dwSize.X - cpos.X, cpos, &nchars);
FillConsoleOutputCharacter(con_out, ' ',
scr.dwSize.X - cpos.X, cpos, &nchars);
#endif
#endif
#endif
#endif
}
/*
* Clear the current line.
* Clear the screen if there's off-screen memory below the display.
*/
static void clear_eol_bot(void)
{
assert_interactive();
#if MSDOS_COMPILER
clear_eol();
#else
if (below_mem)
ltputs(sc_eos_clear, 1, putchr);
else
ltputs(sc_eol_clear, 1, putchr);
#endif
}
/*
* Clear the bottom line of the display.
* Leave the cursor at the beginning of the bottom line.
*/
public void clear_bot(void)
{
/*
* If we're in a non-normal attribute mode, temporarily exit
* the mode while we do the clear. Some terminals fill the
* cleared area with the current attribute.
*/
if (oldbot)
lower_left();
else
line_left();
if (attrmode == AT_NORMAL)
clear_eol_bot();
else
{
int saved_attrmode = attrmode;
at_exit();
clear_eol_bot();
at_enter(saved_attrmode);
}
}
/*
* Color string may be "x[y]" where x and y are 4-bit color chars,
* or "N[.M]" where N and M are decimal integers>
* Any of x,y,N,M may also be "-" to mean "unchanged".
*/
/*
* Parse a 4-bit color char.
*/
static int parse_color4(char ch)
{
switch (ch)
{
case 'k': return 0;
case 'r': return CV_RED;
case 'g': return CV_GREEN;
case 'y': return CV_RED|CV_GREEN;
case 'b': return CV_BLUE;
case 'm': return CV_RED|CV_BLUE;
case 'c': return CV_GREEN|CV_BLUE;
case 'w': return CV_RED|CV_GREEN|CV_BLUE;
case 'K': return 0|CV_BRIGHT;
case 'R': return CV_RED|CV_BRIGHT;
case 'G': return CV_GREEN|CV_BRIGHT;
case 'Y': return CV_RED|CV_GREEN|CV_BRIGHT;
case 'B': return CV_BLUE|CV_BRIGHT;
case 'M': return CV_RED|CV_BLUE|CV_BRIGHT;
case 'C': return CV_GREEN|CV_BLUE|CV_BRIGHT;
case 'W': return CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT;
case '-': return CV_NOCHANGE;
default: return CV_ERROR;
}
}
/*
* Parse a color as a decimal integer.
*/
static int parse_color6(constant char **ps)
{
if (**ps == '-')
{
(*ps)++;
return CV_NOCHANGE;
} else
{
constant char *os = *ps;
int color = lstrtoic(os, ps, 10);
if (color < 0 || *ps == os)
return CV_ERROR;
return color;
}
}
/*
* Parse a color pair and return the foreground/background/attribute values.
* Return type of color specifier:
* CV_4BIT: fg/bg values are OR of CV_{RGB} bits.
* CV_6BIT: fg/bg values are integers entered by user.
*/
public COLOR_TYPE parse_color(constant char *str, mutable int *p_fg, mutable int *p_bg, mutable CHAR_ATTR *p_cattr)
{
int fg;
int bg = CV_ERROR;
CHAR_ATTR cattr = CATTR_NULL;
COLOR_TYPE type = CT_NULL;
if (str == NULL || *str == '\0')
return CT_NULL;
if (*str == '+')
str++; /* ignore leading + */
fg = parse_color4(*str);
if (fg != CV_ERROR)
{
if (str[1] == '\0' || strchr("*~_&dsul", str[1]) != NULL)
{
bg = CV_NOCHANGE;
str++; /* skip the fg char */
} else
{
bg = parse_color4(str[1]);
if (bg != CV_ERROR)
str += 2; /* skip both fg and bg chars */
}
}
if (fg != CV_ERROR && bg != CV_ERROR)
type = CT_4BIT;
else
{
fg = (*str == '.') ? CV_NOCHANGE : parse_color6(&str);
if (fg != CV_ERROR)
{
if (*str != '.')
bg = CV_NOCHANGE;
else
{
str++; /* skip the dot */
bg = parse_color6(&str);
}
}
if (fg != CV_ERROR && bg != CV_ERROR)
type = CT_6BIT;
}
if (type != CT_NULL)
{
for (;; str++)
{
if (*str == '*' || *str == 'd')
cattr |= CATTR_BOLD;
else if (*str == '~' || *str == 's')
cattr |= CATTR_STANDOUT;
else if (*str == '_' || *str == 'u')
cattr |= CATTR_UNDERLINE;
else if (*str == '&' || *str == 'l') /* can't use 'k' because of conflict with "black" */
cattr |= CATTR_BLINK;
else
break;
}
if (p_fg != NULL) *p_fg = fg;
if (p_bg != NULL) *p_bg = bg;
if (p_cattr != NULL) *p_cattr = cattr;
}
return type;
}
#if !MSDOS_COMPILER
static int sgr_color(int color)
{
switch (color)
{
case 0: return 30;
case CV_RED: return 31;
case CV_GREEN: return 32;
case CV_RED|CV_GREEN: return 33;
case CV_BLUE: return 34;
case CV_RED|CV_BLUE: return 35;
case CV_GREEN|CV_BLUE: return 36;
case CV_RED|CV_GREEN|CV_BLUE: return 37;
case CV_BRIGHT: return 90;
case CV_RED|CV_BRIGHT: return 91;
case CV_GREEN|CV_BRIGHT: return 92;
case CV_RED|CV_GREEN|CV_BRIGHT: return 93;
case CV_BLUE|CV_BRIGHT: return 94;
case CV_RED|CV_BLUE|CV_BRIGHT: return 95;
case CV_GREEN|CV_BLUE|CV_BRIGHT: return 96;
case CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT: return 97;
default: return color;
}
}
static void tput_fmt(constant char *fmt, int color, int (*f_putc)(int))
{
char buf[INT_STRLEN_BOUND(int)+16];
if (color == attrcolor)
return;
SNPRINTF1(buf, sizeof(buf), fmt, color);
ltputs(buf, 1, f_putc);
attrcolor = color;
}
static void tput_char_cattr(CHAR_ATTR cattr, int (*f_putc)(int))
{
if (cattr & CATTR_UNDERLINE)
ltputs(sc_u_in, 1, f_putc);
if (cattr & CATTR_BOLD)
ltputs(sc_b_in, 1, f_putc);
if (cattr & CATTR_BLINK)
ltputs(sc_bl_in, 1, f_putc);
if (cattr & CATTR_STANDOUT)
ltputs(sc_s_in, 1, f_putc);
}
static void tput_color(constant char *str, int (*f_putc)(int))
{
int fg;
int bg;
CHAR_ATTR cattr;
if (str != NULL && strcmp(str, "*") == 0)
{
/* Special case: reset to normal */
tput_fmt(ESCS"[m", -1, f_putc);
return;
}
switch (parse_color(str, &fg, &bg, &cattr))
{
case CT_4BIT:
if (fg >= 0)
tput_fmt(ESCS"[%dm", sgr_color(fg), f_putc);
if (bg >= 0)
tput_fmt(ESCS"[%dm", sgr_color(bg)+10, f_putc);
tput_char_cattr(cattr, f_putc);
break;
case CT_6BIT:
if (fg >= 0)
tput_fmt(ESCS"[38;5;%dm", fg, f_putc);
if (bg >= 0)
tput_fmt(ESCS"[48;5;%dm", bg, f_putc);
tput_char_cattr(cattr, f_putc);
break;
default:
break;
}
}
static void tput_inmode(constant char *mode_str, int attr, int attr_bit, int (*f_putc)(int))
{
constant char *color_str;
if ((attr & attr_bit) == 0)
return;
color_str = get_color_map(attr_bit);
if (color_str == NULL || *color_str == '\0' || *color_str == '+')
{
ltputs(mode_str, 1, f_putc);
if (color_str == NULL || *color_str++ != '+')
return;
}
/* Color overrides mode string */
tput_color(color_str, f_putc);
}
static void tput_outmode(constant char *mode_str, int attr_bit, int (*f_putc)(int))
{
if ((attrmode & attr_bit) == 0)
return;
ltputs(mode_str, 1, f_putc);
}
#else /* MSDOS_COMPILER */
#if MSDOS_COMPILER==WIN32C
static lbool WIN32put_fmt(constant char *fmt, int color)
{
char buf[INT_STRLEN_BOUND(int)+16];
int len = (size_t) SNPRINTF1(buf, sizeof(buf), fmt, color);
if (len > 0)
WIN32textout(buf, (size_t) len);
return TRUE;
}
static void win_set_cattr(CHAR_ATTR cattr)
{
if (cattr & CATTR_UNDERLINE)
WIN32textout(ESCS"[4m", 4);
if (cattr & CATTR_BOLD)
WIN32textout(ESCS"[1m", 4);
if (cattr & CATTR_BLINK)
WIN32textout(ESCS"[5m", 4);
if (cattr & CATTR_STANDOUT)
WIN32textout(ESCS"[7m", 4);
}
#endif
static lbool win_set_color(int attr)
{
int fg;
int bg;
CHAR_ATTR cattr;
lbool out = FALSE;
constant char *str = get_color_map(attr);
if (str == NULL || str[0] == '\0')
return FALSE;
switch (parse_color(str, &fg, &bg, &cattr))
{
case CT_4BIT:
if (fg >= 0 && bg >= 0)
{
SETCOLORS(fg, bg);
out = TRUE;
} else if (fg >= 0)
{
SET_FG_COLOR(fg);
out = TRUE;
} else if (bg >= 0)
{
SET_BG_COLOR(bg);
out = TRUE;
}
#if MSDOS_COMPILER==WIN32C
if (vt_enabled)
win_set_cattr(cattr);
#endif
break;
#if MSDOS_COMPILER==WIN32C
case CT_6BIT:
if (vt_enabled)
{
if (fg > 0)
out = WIN32put_fmt(ESCS"[38;5;%dm", fg);
if (bg > 0)
out = WIN32put_fmt(ESCS"[48;5;%dm", bg);
win_set_cattr(cattr);
}
break;
#endif
default:
break;
}
return out;
}
#endif /* MSDOS_COMPILER */
public void at_enter(int attr)
{
attr = apply_at_specials(attr);
#if !MSDOS_COMPILER
/* The one with the most priority is last. */
tput_inmode(sc_u_in, attr, AT_UNDERLINE, putchr);
tput_inmode(sc_b_in, attr, AT_BOLD, putchr);
tput_inmode(sc_bl_in, attr, AT_BLINK, putchr);
/* Don't use standout and color at the same time. */
if (use_color && (attr & AT_COLOR))
tput_color(get_color_map(attr), putchr);
else
tput_inmode(sc_s_in, attr, AT_STANDOUT, putchr);
#else
flush();
/* The one with the most priority is first. */
if ((attr & AT_COLOR) && use_color)
{
win_set_color(attr);
} else if (attr & AT_STANDOUT)
{
SETCOLORS(so_fg_color, so_bg_color);
} else if (attr & AT_BLINK)
{
SETCOLORS(bl_fg_color, bl_bg_color);
} else if (attr & AT_BOLD)
{
SETCOLORS(bo_fg_color, bo_bg_color);
} else if (attr & AT_UNDERLINE)
{
SETCOLORS(ul_fg_color, ul_bg_color);
}
#endif
attrmode = attr;
}
public void at_exit(void)
{
#if !MSDOS_COMPILER
/* Undo things in the reverse order we did them. */
tput_color("*", putchr);
tput_outmode(sc_s_out, AT_STANDOUT, putchr);
tput_outmode(sc_bl_out, AT_BLINK, putchr);
tput_outmode(sc_b_out, AT_BOLD, putchr);
tput_outmode(sc_u_out, AT_UNDERLINE, putchr);
#else
flush();
SETCOLORS(nm_fg_color, nm_bg_color);
#endif
attrmode = AT_NORMAL;
}
public void at_switch(int attr)
{
int new_attrmode = apply_at_specials(attr);
int ignore_modes = AT_ANSI;
if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
{
at_exit();
at_enter(attr);
}
}
public lbool is_at_equiv(int attr1, int attr2)
{
attr1 = apply_at_specials(attr1);
attr2 = apply_at_specials(attr2);
return (attr1 == attr2);
}
public int apply_at_specials(int attr)
{
if (attr & AT_BINARY)
attr |= binattr;
if (attr & AT_HILITE)
attr |= AT_STANDOUT;
attr &= ~(AT_BINARY|AT_HILITE);
return attr;
}
/*
* Output a plain backspace, without erasing the previous char.
*/
public void putbs(void)
{
if (termcap_debug)
putstr("<bs>");
else
{
#if !MSDOS_COMPILER
ltputs(sc_backspace, 1, putchr);
#else
int row, col;
flush();
{
#if MSDOS_COMPILER==MSOFTC
struct rccoord tpos;
tpos = _gettextposition();
row = tpos.row;
col = tpos.col;
#else
#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
row = wherey();
col = wherex();
#else
#if MSDOS_COMPILER==WIN32C
CONSOLE_SCREEN_BUFFER_INFO scr;
GetConsoleScreenBufferInfo(con_out, &scr);
row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
#endif
#endif
#endif
}
if (col <= 1)
return;
_settextposition(row, col-1);
#endif /* MSDOS_COMPILER */
}
}
#if MSDOS_COMPILER==WIN32C
#define WIN32_MAX_REPEAT 3
#define LAST_DOWN_COUNT 8
static LWCHAR last_downs[LAST_DOWN_COUNT] = { 0 };
static int last_down_index = 0;
static LWCHAR hi_surr = 0;
typedef struct XINPUT_RECORD {
INPUT_RECORD ir;
LWCHAR ichar; /* because ir...UnicodeChar is only 16 bits */
} XINPUT_RECORD;
typedef struct WIN32_CHAR {
struct WIN32_CHAR *wc_next;
char wc_ch;
} WIN32_CHAR;
static WIN32_CHAR *win32_queue = NULL;
/*
* Is the win32_queue nonempty?
*/
static int win32_queued_char(void)
{
return (win32_queue != NULL);
}
/*
* Push a char onto the back of the win32_queue.
*/
static void win32_enqueue(char ch)
{
WIN32_CHAR *wch = (WIN32_CHAR *) ecalloc(1, sizeof(WIN32_CHAR));
wch->wc_ch = ch;
wch->wc_next = NULL;
if (win32_queue == NULL)
win32_queue = wch;
else
{
WIN32_CHAR *pch;
for (pch = win32_queue; pch->wc_next != NULL; pch = pch->wc_next)
continue;
pch->wc_next = wch;
}
}
/*
* Push a char onto the front of the win32_queue.
* Makes the next call to WIN32getch return ch.
*/
public void WIN32ungetch(int ch)
{
WIN32_CHAR *wch = (WIN32_CHAR *) ecalloc(1, sizeof(WIN32_CHAR));
wch->wc_ch = ch;
wch->wc_next = win32_queue;
win32_queue = wch;
}
/*
* Get a char from the front of the win32_queue.
*/
static char win32_get_queue(void)
{
WIN32_CHAR *wch = win32_queue;
char ch = wch->wc_ch;
win32_queue = wch->wc_next;
free(wch);
return ch;
}
/*
* Handle a mouse input event.
*/
static lbool win32_mouse_event(XINPUT_RECORD *xip)
{
char b;
if (!mousecap || xip->ir.EventType != MOUSE_EVENT ||
xip->ir.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
return (FALSE);
/* Generate an X11 mouse sequence from the mouse event. */
/* TODO: switch to the 1006 protocol to allow specific-button-up reports */
switch (xip->ir.Event.MouseEvent.dwEventFlags)
{
case 0: /* press or release */
if (xip->ir.Event.MouseEvent.dwButtonState == 0)
b = X11MOUSE_OFFSET + X11MOUSE_BUTTON_REL;
else if (xip->ir.Event.MouseEvent.dwButtonState == 1) /* leftmost */
b = X11MOUSE_OFFSET + X11MOUSE_BUTTON1;
else if (xip->ir.Event.MouseEvent.dwButtonState == 2) /* rightmost */
b = X11MOUSE_OFFSET + X11MOUSE_BUTTON3;
else if (xip->ir.Event.MouseEvent.dwButtonState == 4) /* middle ("next-to-leftmost") */
b = X11MOUSE_OFFSET + X11MOUSE_BUTTON2;
else /* don't bother to figure out what changed */
return (FALSE);
break;
case MOUSE_WHEELED:
b = X11MOUSE_OFFSET + (((int)xip->ir.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP);
break;
default:
return (FALSE);
}
/* {{ TODO: change to X11 1006 format. }} */
win32_enqueue(ESC);
win32_enqueue('[');
win32_enqueue('M');
win32_enqueue(b);
win32_enqueue(X11MOUSE_OFFSET + xip->ir.Event.MouseEvent.dwMousePosition.X + 1);
win32_enqueue(X11MOUSE_OFFSET + xip->ir.Event.MouseEvent.dwMousePosition.Y + 1);
return (TRUE);
}
static void set_last_down(LWCHAR ch)
{
if (ch == 0) return;
last_downs[last_down_index] = ch;
if (++last_down_index >= LAST_DOWN_COUNT)
last_down_index = 0;
}
static LWCHAR *find_last_down(LWCHAR ch)
{
int i;
for (i = 0; i < LAST_DOWN_COUNT; ++i)
if (last_downs[i] == ch)
return &last_downs[i];
return NULL;
}
/*
* Get an input char from an INPUT_RECORD and store in xip->ichar.
* Handles surrogate chars, and KeyUp without previous corresponding KeyDown.
*/
static lbool win32_get_ichar(XINPUT_RECORD *xip)
{
LWCHAR ch = xip->ir.Event.KeyEvent.uChar.UnicodeChar;
xip->ichar = ch;
if (!is_ascii_char(ch))
{
int is_down = xip->ir.Event.KeyEvent.bKeyDown;
LWCHAR *last_down = find_last_down(ch);
if (last_down == NULL) { /* key was up */
if (is_down) { /* key was up, now is down */
set_last_down(ch);
} else { /* key up without previous down: pretend this is a down. */
xip->ir.Event.KeyEvent.bKeyDown = 1;
}
} else if (!is_down) { /* key was down, now is up */
*last_down = 0; /* use this last_down only once */
}
if (ch >= 0xD800 && ch < 0xDC00) { /* high surrogate */
hi_surr = 0x10000 + ((ch - 0xD800) << 10);
return (FALSE); /* get next input, which should be the low surrogate */
}
if (ch >= 0xDC00 && ch < 0xE000) { /* low surrogate */
xip->ichar = hi_surr + (ch - 0xDC00);
hi_surr = 0;
}
}
return (TRUE);
}
/*
* Handle a scan code (non-ASCII) key input.
*/
static lbool win32_scan_code(XINPUT_RECORD *xip)
{
int scan = -1;
if (xip->ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
{
switch (xip->ir.Event.KeyEvent.wVirtualScanCode)
{
case PCK_RIGHT: /* right arrow */
scan = PCK_CTL_RIGHT;
break;
case PCK_LEFT: /* left arrow */
scan = PCK_CTL_LEFT;
break;
case PCK_DELETE: /* delete */
scan = PCK_CTL_DELETE;
break;
}
} else if (xip->ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
{
if (xip->ichar == '\t')
scan = PCK_SHIFT_TAB;
}
if (scan < 0 && xip->ichar == 0)
scan = xip->ir.Event.KeyEvent.wVirtualScanCode;
if (scan < 0)
return (FALSE);
/*
* An extended key returns a 2 byte sequence consisting of
* a zero byte followed by the scan code.
*/
win32_enqueue('\0');
win32_enqueue(scan);
return (TRUE);
}
/*
* Handle a key input event.
*/
static lbool win32_key_event(XINPUT_RECORD *xip)
{
int repeat;
char utf8[UTF8_MAX_LENGTH];
char *up;
if (xip->ir.EventType != KEY_EVENT ||
((xip->ir.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED)) == (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED) && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) ||
(xip->ir.Event.KeyEvent.wVirtualScanCode == 0 && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) ||
xip->ir.Event.KeyEvent.wVirtualScanCode == PCK_CAPS_LOCK ||
xip->ir.Event.KeyEvent.wVirtualScanCode == PCK_NUM_LOCK ||
(xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_MENU && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) ||
xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_KANJI ||
xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL)
return (FALSE);
if (!win32_get_ichar(xip))
return (FALSE);
if (!xip->ir.Event.KeyEvent.bKeyDown)
return (FALSE);
if (win32_scan_code(xip))
return (TRUE);
repeat = xip->ir.Event.KeyEvent.wRepeatCount;
if (repeat > WIN32_MAX_REPEAT)
repeat = WIN32_MAX_REPEAT;
up = utf8;
put_wchar(&up, xip->ichar);
for (; repeat > 0; --repeat)
{
constant char *p;
for (p = utf8; p < up; ++p)
win32_enqueue(*p);
}
return (TRUE);
}
/*
* Determine whether an input character is waiting to be read.
*/
public lbool win32_kbhit(void)
{
XINPUT_RECORD xip;
if (win32_queued_char())
return (TRUE);
for (;;)
{
DWORD nread;
DWORD console_input_mode;
/*
* When an input pipe closes, cmd may reset the console mode,
* so set the mode every time we read input.
*/
if (GetConsoleMode(tty, &console_input_mode) && console_input_mode != curr_console_input_mode)
SetConsoleMode(tty, curr_console_input_mode);
PeekConsoleInputW(tty, &xip.ir, 1, &nread);
if (nread == 0)
return (FALSE);
ReadConsoleInputW(tty, &xip.ir, 1, &nread);
if (nread == 0)
return (FALSE);
if (win32_mouse_event(&xip) || win32_key_event(&xip))
break;
}
return (TRUE);
}
/*
* Read a character from the keyboard.
*/
public char WIN32getch(void)
{
while (!win32_kbhit())
{
Sleep(20);
if (ABORT_SIGS())
return ('\003');
}
return (win32_get_queue());
}
public void win32_getch_clear(void)
{
while (win32_kbhit())
(void) WIN32getch();
}
#endif /* MSDOS_COMPILER==WIN32C */
#if MSDOS_COMPILER
/*
*/
public void WIN32setcolors(int fg, int bg)
{
SETCOLORS(fg, bg);
}
/*
*/
public void WIN32textout(constant char *text, size_t len)
{
#if MSDOS_COMPILER==WIN32C
DWORD written;
if (utf_mode == 2)
{
/*
* We've got UTF-8 text in a non-UTF-8 console. Convert it to
* wide and use WriteConsoleW.
* Biggest input len is OUTBUF_SIZE of obuf from win_flush,
* which is also the biggest output count if it's ASCII.
* "static" wtext is not a state - only avoid 16K on stack.
*/
static WCHAR wtext[OUTBUF_SIZE];
len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext, countof(wtext));
WriteConsoleW(con_out, wtext, len, &written, NULL);
} else
WriteConsole(con_out, text, len, &written, NULL);
#else
char buf[2048];
if (len >= sizeof(buf))
len = sizeof(buf) - 1;
memcpy(buf, text, len);
buf[len] = 0;
cputs(buf);
#endif
}
#endif
|