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
|
/*
* AnsiTerm.cpp
* Windows ANSI Terminal Emulation
*
* Author: Robert Nelson robertnelson at users.sourceforge.net
* Copyright (c) 2009 Robert Nelson
*
* 10/07/09 Robert Nelson - Created
* 10/08/09 Robert Nelson
* - Fixed bug with resetting attribute to Black on Black on exit
* - Fixed setting of attribute when erasing
* - Added automatic handling of buffer resize events
* - Added display of unrecognized escape sequences
* 10/15/09 Robert Nelson
* - Fixed display problems caused by custom ColorTable used by cmd.exe
* - Fixed cursor positioning problems with OriginMode.
* - Changed to use block cursor because I think its more terminal like :-)
* - Added Reset handling
* - Added Cursor Show / Hide
* 10/16/09 Robert Nelson
* - Better handling of ColorTable.
* 10/17/09 Robert Nelson
* - Use GetProcAddress for (Get/Set)ConsoleScreenBufferInfoEx since they
* are only available on Vista and beyond.
* 01/27/2015 Andy Cress
* - handle ProcessRM asserts
*
* Todo:
* - Implement soft tabs
*/
/*
Copyright (c) 2009, Robert Nelson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
a.. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
b.. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include "AnsiTerm.h"
extern "C" unsigned char fCRLF;
extern "C" void dbglog( char *pattn, ... );
CAnsiTerm::CSICode CAnsiTerm::s_CSITable[] =
{
{ '[', DS_CSIParam },
{ '#', DS_DECPrivate },
{ '(', DS_SelectG0 },
{ ')', DS_SelectG1 },
{ '=', DS_None, &ProcessDECKPAM }, // Keypad Application Mode
{ '>', DS_None, &ProcessDECKPNM }, // Keypad Numeric Mode
{ '7', DS_None, &ProcessDECSC }, // Save Cursor
{ '8', DS_None, &ProcessDECRC }, // Restore Cursor
{ 'D', DS_None, &ProcessIND }, // Index
{ 'E', DS_None, &ProcessNEL }, // Next Line
{ 'H', DS_None, &ProcessHTS }, // Horizontal Tab Set
{ 'M', DS_None, &ProcessRI }, // Reverse Index
{ 'Z', DS_None, &ProcessDECID }, // Identify Terminal
{ 'c', DS_None, &ProcessRIS }, // Reset to Initial State
{ 's', DS_None, &ProcessSCP }, // Save Cursor Position
{ 'u', DS_None, &ProcessRCP }, // Restore Cursor Position
{ '\0' }
};
CAnsiTerm::CSIFunction CAnsiTerm::s_DECFunction[] =
{
{ '3', &ProcessDECDHLT }, // Double Height Line Top
{ '4', &ProcessDECDHLB }, // Double Height Line Bottom
{ '5', &ProcessDECSWL }, // Single Width Line
{ '6', &ProcessDECDWL }, // Double Width Line
{ '8', &ProcessDECALN }, // Screen Alignment Display
{ '\0' }
};
CAnsiTerm::CSIFunction CAnsiTerm::s_CSIFunction[] =
{
{ 'A', &ProcessCUU }, // Cursor Up
{ 'B', &ProcessCUD }, // Cursor Down
{ 'C', &ProcessCUF }, // Cursor Forward
{ 'D', &ProcessCUB }, // Cursor Backward
{ 'H', &ProcessCUP }, // Cursor Position
{ 'J', &ProcessED }, // Erase in Display
{ 'K', &ProcessEL }, // Erase in Line
{ 'c', &ProcessDA }, // Device Attributes
{ 'f', &ProcessHVP }, // Horizontal and Vertical Position
{ 'g', &ProcessTBC }, // Tabulation Clear
{ 'h', &ProcessSM }, // Set Mode
{ 'l', &ProcessRM }, // Reset Mode
{ 'm', &ProcessSGR }, // Select Graphics Rendition
{ 'n', &ProcessDSR }, // Device Status Report
{ 'q', &ProcessDECLL }, // DEC Load LEDs
{ 'r', &ProcessDECSTBM }, // DEC Set Top and Bottom Margins
{ 'x', &ProcessDECREQTPARM }, // Request Terminal Parameters
{ 'y', &ProcessDECTST }, // Invoke Confidence Test
{ '\0' }
};
wchar_t CAnsiTerm::s_GraphicChars[kMaxGraphicsChar - kMinGraphicsChar + 1] =
{
0x0020, // 0137 5F 95 _ Blank
0x2666, // 0140 60 96 ` Diamond
0x2592, // 0141 61 97 a Checkerboard
0x2409, // 0142 62 98 b Horizontal Tab
0x240C, // 0143 63 99 c Form Feed
0x240D, // 0144 64 100 d Carriage Return
0x240A, // 0145 65 101 e Line Feed
0x00B0, // 0146 66 102 f Degree Symbol
0x00B1, // 0147 67 103 g Plus/Minus
0x2424, // 0150 68 104 h New Line
0x240B, // 0151 69 105 i Vertical Tab
0x2518, // 0152 6A 106 j Lower-right corner
0x2510, // 0153 6B 107 k Upper-right corner
0x250C, // 0154 6C 108 l Upper-left corner
0x2514, // 0155 6D 109 m Lower-left corner
0x253C, // 0156 6E 110 n Crossing Lines
0x00AF, // 0157 6F 111 o Horizontal Line - Scan 1
0x0070, // 0160 70 112 p Horizontal Line - Scan 3 (No translation)
0x2500, // 0161 71 113 q Horizontal Line - Scan 5
0x0072, // 0162 72 114 r Horizontal Line - Scan 7 (No translation)
0x005F, // 0163 73 115 s Horizontal Line - Scan 9
0x251C, // 0164 74 116 t Left "T"
0x2524, // 0165 75 117 u Right "T"
0x2534, // 0166 76 118 v Bottom "T"
0x252C, // 0167 77 119 w Top "T"
0x2502, // 0170 78 120 x | Vertical bar
0x2264, // 0171 79 121 y Less than or equal to
0x2265, // 0172 7A 122 z Greater than or equal to
0x03C0, // 0173 7B 123 { Pi
0x2260, // 0174 7C 124 | Not equal to
0x00A3, // 0175 7D 125 } UK Pound Sign
0x00B7 // 0176 7E 126 ~ Centered dot
};
wchar_t CAnsiTerm::s_OemToUnicode[256] =
{
0x2007, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, // 00-07
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C, // 08-0F
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, // 10-17
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC, // 18-1F
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, // 20-27
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, // 28-2F
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, // 30-37
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, // 38-3F
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, // 40-47
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, // 48-4F
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, // 50-57
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, // 58-5F
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, // 60-67
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, // 68-6F
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, // 70-77
0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, // 78-7F
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, // 80-87
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, // 88-8F
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, // 90-97
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, // 98-9F
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, // A0-A7
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, // A8-AF
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, // B0-B7
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, // B8-BF
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, // C0-C7
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, // C8-CF
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, // D0-D7
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, // D8-DF
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, // E0-E7
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, // E8-EF
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, // F0-F7
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 // F8-FF
};
COLORREF CAnsiTerm::s_ColorTable[kColorTableSize] =
{
RGB(0, 0, 0),
RGB(0, 0, 128),
RGB(0, 128, 0),
RGB(0, 128, 128),
RGB(128, 0, 0),
RGB(128, 0, 128),
RGB(128, 128, 0),
RGB(192, 192, 192),
RGB(128, 128, 128),
RGB(0, 0, 255),
RGB(0, 255, 0),
RGB(0, 255, 255),
RGB(255, 0, 0),
RGB(255, 0, 255),
RGB(255, 255, 0),
RGB(255, 255, 255)
};
CAnsiTerm::PFN_GetConsoleScreenBufferInfoEx CAnsiTerm::s_pfnGetConsoleScreenBufferInfoEx;
CAnsiTerm::PFN_SetConsoleScreenBufferInfoEx CAnsiTerm::s_pfnSetConsoleScreenBufferInfoEx;
CAnsiTerm::CAnsiTerm(void)
{
m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(m_hConsole, &m_dwOrigConsoleMode);
SetConsoleMode(m_hConsole, ENABLE_PROCESSED_OUTPUT);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(m_hConsole, &cursorInfo);
m_dwOrigCursorSize = cursorInfo.dwSize;
WindowSizeChanged(true);
}
CAnsiTerm::~CAnsiTerm(void)
{
CONSOLE_CURSOR_INFO cursorInfo = { m_dwOrigCursorSize, TRUE };
SetConsoleCursorInfo(m_hConsole, &cursorInfo);
SetConsoleMode(m_hConsole, m_dwOrigConsoleMode);
SetConsoleTextAttribute(m_hConsole, m_wOrigConsoleAttribute);
if (m_bResetColorTable)
{
CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { sizeof(consoleInfo) };
s_pfnGetConsoleScreenBufferInfoEx(m_hConsole, &consoleInfo);
memcpy(consoleInfo.ColorTable, m_OrigColorTable, kColorTableSize * sizeof(consoleInfo.ColorTable[0]));
// There is a bug between GetConsoleScreenBufferInfoEx and SetConsoleScreenBufferInfoEx.
// The first treats srWindow.Right and srWindow.Bottom as inclusive and the latter as exclusive.
consoleInfo.srWindow.Right++;
consoleInfo.srWindow.Bottom++;
s_pfnSetConsoleScreenBufferInfoEx(m_hConsole, &consoleInfo);
// Reset the attributes on existing lines so that at least white on black looks
// correct.
COORD coordStart = { 0, 0 };
DWORD dwWritten;
FillConsoleOutputAttribute(m_hConsole, m_wOrigConsoleAttribute, m_BufferSize.X * m_BufferSize.Y, coordStart, &dwWritten);
}
CloseHandle(m_hConsole);
}
void
CAnsiTerm::WindowSizeChanged(bool bInitial)
{
WORD wCurrentAttribute;
COORD dwCurrentCursorPosition;
if (bInitial)
{
HMODULE hKernel32 = GetModuleHandle("kernel32");
if (hKernel32 != NULL)
{
s_pfnGetConsoleScreenBufferInfoEx = (PFN_GetConsoleScreenBufferInfoEx)GetProcAddress(hKernel32, "GetConsoleScreenBufferInfoEx");
s_pfnSetConsoleScreenBufferInfoEx = (PFN_SetConsoleScreenBufferInfoEx)GetProcAddress(hKernel32, "SetConsoleScreenBufferInfoEx");
if (s_pfnGetConsoleScreenBufferInfoEx == NULL || s_pfnSetConsoleScreenBufferInfoEx == NULL)
{
s_pfnGetConsoleScreenBufferInfoEx = NULL;
s_pfnSetConsoleScreenBufferInfoEx = NULL;
}
}
m_bResetColorTable = false;
}
if (s_pfnGetConsoleScreenBufferInfoEx != NULL)
{
CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { sizeof(consoleInfo) };
s_pfnGetConsoleScreenBufferInfoEx(m_hConsole, &consoleInfo);
m_BufferSize = consoleInfo.dwSize;
m_WindowSize.X = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
m_WindowSize.Y = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
wCurrentAttribute = consoleInfo.wAttributes;
dwCurrentCursorPosition = consoleInfo.dwCursorPosition;
if (bInitial)
{
m_bResetColorTable = memcmp(consoleInfo.ColorTable, s_ColorTable, sizeof(s_ColorTable)) != 0;
if (m_bResetColorTable)
{
// The command prompt (cmd.exe) uses a nonstandard color table
// So we save it away and reset it to match the Console API documentation
size_t colorCopyLen = kColorTableSize * sizeof(consoleInfo.ColorTable[0]);
memcpy(m_OrigColorTable, consoleInfo.ColorTable, colorCopyLen);
memcpy(consoleInfo.ColorTable, s_ColorTable, colorCopyLen);
// There is a bug between GetConsoleScreenBufferInfoEx and SetConsoleScreenBufferInfoEx.
// The first treats srWindow.Right and srWindow.Bottom as inclusive and the latter as exclusive.
consoleInfo.srWindow.Right++;
consoleInfo.srWindow.Bottom++;
s_pfnSetConsoleScreenBufferInfoEx(m_hConsole, &consoleInfo);
// Reset the attributes on existing lines so that at least white on black looks
// correct.
COORD coordStart = { 0, 0 };
DWORD dwWritten;
FillConsoleOutputAttribute(m_hConsole, kDefaultAttribute, m_BufferSize.X * m_BufferSize.Y, coordStart, &dwWritten);
}
}
}
else
{
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
GetConsoleScreenBufferInfo(m_hConsole, &consoleInfo);
m_BufferSize = consoleInfo.dwSize;
m_WindowSize.X = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
m_WindowSize.Y = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
wCurrentAttribute = consoleInfo.wAttributes;
dwCurrentCursorPosition = consoleInfo.dwCursorPosition;
}
m_WindowOrigin.X = 0;
m_WindowOrigin.Y = m_BufferSize.Y - m_WindowSize.Y;
if (bInitial)
{
m_wOrigConsoleAttribute = wCurrentAttribute;
SHORT nLines = dwCurrentCursorPosition.Y - m_WindowOrigin.Y;
if (nLines != 0)
{
SMALL_RECT rectSource = { 0, 0, m_BufferSize.X - 1, m_BufferSize.Y - 1 };
COORD coordDest = { 0, 0 };
CHAR_INFO charInfo = { ' ', kDefaultAttribute };
if (nLines > 0)
{
rectSource.Top = nLines;
}
else
{
coordDest.Y -= nLines;
rectSource.Bottom += nLines;
}
ScrollConsoleScreenBuffer(m_hConsole, &rectSource, NULL, coordDest, &charInfo);
}
}
ResetTerm();
}
int
CAnsiTerm::ProcessInput(CAnsiTerm::KeyCode keyCode, unsigned char *pOutput, int iOutputSize)
{
int iOutputLength = 0;
if (pOutput == NULL || iOutputSize < 1)
{
return 0;
}
if (!keyCode.bKeyDown)
{
return 0;
}
if (VK_F1 <= keyCode.VirtualKeyCode && keyCode.VirtualKeyCode <= VK_F12)
{
pOutput[iOutputLength++] = CH_ESC;
pOutput[iOutputLength++] = 'O';
pOutput[iOutputLength++] = 'P' + keyCode.VirtualKeyCode - VK_F1;
}
else
if (keyCode.VirtualKeyCode == VK_UP ||
keyCode.VirtualKeyCode == VK_DOWN ||
keyCode.VirtualKeyCode == VK_RIGHT ||
keyCode.VirtualKeyCode == VK_LEFT)
{
pOutput[iOutputLength++] = CH_ESC;
if ((m_bCursorKeyMode && !keyCode.bControl) ||
(!m_bCursorKeyMode && keyCode.bControl))
{
pOutput[iOutputLength++] = 'O';
}
else
{
pOutput[iOutputLength++] = '[';
}
switch (keyCode.VirtualKeyCode)
{
case VK_UP:
pOutput[iOutputLength++] = 'A';
break;
case VK_DOWN:
pOutput[iOutputLength++] = 'B';
break;
case VK_RIGHT:
pOutput[iOutputLength++] = 'C';
break;
case VK_LEFT:
pOutput[iOutputLength++] = 'D';
break;
}
}
else
if (keyCode.VirtualKeyCode == VK_HOME ||
keyCode.VirtualKeyCode == VK_INSERT ||
keyCode.VirtualKeyCode == VK_DELETE ||
keyCode.VirtualKeyCode == VK_END ||
keyCode.VirtualKeyCode == VK_PRIOR ||
keyCode.VirtualKeyCode == VK_NEXT)
{
pOutput[iOutputLength++] = CH_ESC;
pOutput[iOutputLength++] = '[';
switch (keyCode.VirtualKeyCode)
{
case VK_HOME:
pOutput[iOutputLength++] = '1';
break;
case VK_INSERT:
pOutput[iOutputLength++] = '2';
break;
case VK_DELETE:
pOutput[iOutputLength++] = '3';
break;
case VK_END:
pOutput[iOutputLength++] = '4';
break;
case VK_PRIOR:
pOutput[iOutputLength++] = '5';
break;
case VK_NEXT:
pOutput[iOutputLength++] = '6';
break;
}
pOutput[iOutputLength++] = '~';
}
else
if (keyCode.VirtualKeyCode == VK_RETURN)
{
pOutput[iOutputLength++] = CH_CR;
if (fCRLF == 1)
{
pOutput[iOutputLength++] = CH_LF;
}
}
else
if (keyCode.AsciiChar != '\0')
{
pOutput[iOutputLength++] = keyCode.AsciiChar;
}
return iOutputLength;
}
bool
CAnsiTerm::ProcessOutput(const unsigned char *szData, int iLength)
{
const unsigned char *pEnd = &szData[iLength];
for (const unsigned char *pCurrent = szData; pCurrent < pEnd; pCurrent++)
{
if (*pCurrent < 0x20 || *pCurrent == 0x7F)
{
dbglog("ProcessOutput: control_ch = %02x\n",*pCurrent);
OutputText();
switch (*pCurrent)
{
case CH_NUL:
case CH_ENQ:
case CH_DEL:
// These are ignored
break;
case CH_BEL:
MessageBeep(MB_ICONASTERISK);
break;
case CH_BS:
ProcessBackspace();
break;
case CH_HT:
ProcessTab();
break;
case CH_LF:
case CH_VT:
case CH_FF:
ProcessLinefeed(m_bLineFeedNewLineMode);
break;
case CH_CR:
ProcessReturn();
break;
case CH_SO:
m_SelectedCharset = CharsetG1;
break;
case CH_SI:
m_SelectedCharset = CharsetG0;
break;
case CH_XON:
// Not yet implemented
break;
case CH_XOF:
// Not yet implemented
break;
#if 0
case CH_CAN:
case CH_SUB:
// Output error character
break;
#endif
case CH_ESC:
m_State = DS_Escape;
break;
default:
AddOutputData(s_OemToUnicode[*pCurrent]);
break;
}
}
else
{
/* db b3 or b0 b3 */
if (*pCurrent & 0x80)
dbglog("ProcessOutput: state=%d ch=%02x\n",m_State,*pCurrent);
switch (m_State)
{
case DS_Normal:
if (*pCurrent & 0x80)
{
// Could be start of a UTF-8 sequence or an ANSI extended character
if ((*pCurrent & 0xE0) == 0xC0)
{
m_UTF8Size = 2;
}
else if ((*pCurrent & 0xF0) == 0xE0)
{
m_UTF8Size = 3;
}
else
{
// Not a UTF-8 lead character
AddOutputData(s_OemToUnicode[*pCurrent]);
break;
}
m_UTF8Count = 1;
m_UTF8Buffer[0] = *pCurrent;
m_State = DS_UTF8;
break;
}
if ((m_SelectedCharset == CharsetG0 && m_G0Charset == SpecialGraphicsCharset) ||
(m_SelectedCharset == CharsetG1 && m_G1Charset == SpecialGraphicsCharset))
{
if (kMinGraphicsChar <= *pCurrent && *pCurrent <= kMaxGraphicsChar)
{
AddOutputData(s_GraphicChars[*pCurrent - kMinGraphicsChar]);
}
else
{
AddOutputData(*pCurrent);
}
}
else
{
AddOutputData(*pCurrent);
}
break;
case DS_UTF8:
if ((*pCurrent & 0xC0) != 0x80)
{
for (int index = 0; index < m_UTF8Count; index++)
{
AddOutputData(s_OemToUnicode[m_UTF8Buffer[index]]);
}
if (*pCurrent & 0x80)
{
AddOutputData(s_OemToUnicode[*pCurrent]);
}
else
{
AddOutputData(*pCurrent);
}
m_State = DS_Normal;
}
else
{
m_UTF8Buffer[m_UTF8Count++] = *pCurrent;
if (m_UTF8Count == m_UTF8Size)
{
wchar_t wchUTF16;
if (m_UTF8Size == 2)
{
wchUTF16 = ((m_UTF8Buffer[0] & 0x1F) << 6) | (m_UTF8Buffer[1] & 0x3F);
}
else
{
wchUTF16 = ((m_UTF8Buffer[0] & 0x0F) << 12) | ((m_UTF8Buffer[1] & 0x3F) << 6) | (m_UTF8Buffer[2] & 0x3F);
}
AddOutputData(wchUTF16);
m_State = DS_Normal;
}
}
break;
case DS_Escape:
for (int index = 0; s_CSITable[index].chCode != '\0'; index++)
{
if (*pCurrent == s_CSITable[index].chCode)
{
if (s_CSITable[index].dsNextState != DS_None)
{
m_State = s_CSITable[index].dsNextState;
m_Parameters[0] = 0;
m_ParameterCount = 0;
m_bParametersStart = true;
}
else
{
(this->*s_CSITable[index].pfnProcess)();
m_State = DS_Normal;
}
break;
}
}
if (m_State == DS_Escape)
{
AddOutputData(L'^');
AddOutputData(L'[');
AddOutputData(*pCurrent);
m_State = DS_Normal;
}
break;
case DS_CSIParam:
if (m_bParametersStart)
{
m_bParametersStart = false;
if (*pCurrent == '?')
{
m_bPrivateParameters = true;
break;
}
else
{
m_bPrivateParameters = false;
}
}
if ('0' <= *pCurrent && *pCurrent <= '9')
{
if (m_ParameterCount < kMaxParameterCount)
{
m_Parameters[m_ParameterCount] *= 10;
m_Parameters[m_ParameterCount] += *pCurrent - '0';
}
}
else if (*pCurrent == ';')
{
if (m_ParameterCount < kMaxParameterCount)
{
m_ParameterCount++;
}
if (m_ParameterCount < kMaxParameterCount)
{
m_Parameters[m_ParameterCount] = 0;
}
}
else
{
if (m_ParameterCount < kMaxParameterCount)
{
m_ParameterCount++;
}
for (int index = 0; s_CSIFunction[index].chCode != '\0'; index++)
{
if (*pCurrent == s_CSIFunction[index].chCode)
{
(this->*s_CSIFunction[index].pfnProcess)();
m_State = DS_Normal;
break;
}
}
if (m_State != DS_Normal)
{
DisplayCSI(*pCurrent);
m_State = DS_Normal;
}
}
break;
case DS_DECPrivate:
for (int index = 0; s_DECFunction[index].chCode != '\0'; index++)
{
if (*pCurrent == s_DECFunction[index].chCode)
{
(this->*s_DECFunction[index].pfnProcess)();
m_State = DS_Normal;
break;
}
}
if (m_State != DS_Normal)
{
AddOutputData(L'^');
AddOutputData(L'[');
AddOutputData(L'#');
AddOutputData(*pCurrent);
}
break;
case DS_SelectG0:
ProcessSCSG0(*pCurrent);
m_State = DS_Normal;
break;
case DS_SelectG1:
ProcessSCSG1(*pCurrent);
m_State = DS_Normal;
break;
default:
dbglog("ProcessOutput: illegal m_State=%d\n",m_State);
assert(false);
break;
}
}
}
OutputText();
return true;
}
void
CAnsiTerm::DisplayCSI(char ch)
{
char szParam[15];
AddOutputData(L'^');
AddOutputData(L'[');
AddOutputData(L'[');
for (int idxParam = 0; idxParam < m_ParameterCount; idxParam++)
{
if (idxParam > 0)
{
AddOutputData(L';');
}
int iLenParam = sprintf(szParam, "%d", m_Parameters[idxParam]);
for (int idxChar = 0; idxChar < iLenParam; idxChar++)
{
AddOutputData(szParam[idxChar]);
}
}
AddOutputData(ch);
}
bool
CAnsiTerm::ResetTerm(void)
{
m_State = DS_Normal;
m_SelectedCharset = CharsetG0;
m_G0Charset = AsciiCharset;
m_G1Charset = SpecialGraphicsCharset;
m_Cursor.X = 0;
m_Cursor.Y = 0;
m_SavedCursor = m_Cursor;
m_sTopMargin = 0;
m_sBottomMargin = m_WindowSize.Y - 1;
m_Attribute = kDefaultAttribute;
UpdateTextAttribute();
m_dwOutputCount = 0;
// if (fCRLF == 1)
m_bLineFeedNewLineMode = true; /*was false*/
m_bCursorKeyMode = false;
m_bAnsiMode = true;
m_bColumnMode = true;
m_bScrollingMode = false;
m_bScreenMode = false;
m_bOriginMode = false;
m_bAutoRepeatingMode = true;
m_bInterlaceMode = false;
m_bDisplayCursor = true;
m_bAutoWrapMode = true; /*default to wrap*/
EraseDisplay(EraseAll);
SetCursorPosition();
DisplayCursor();
return true;
}
bool
CAnsiTerm::DisplayCursor(void)
{
CONSOLE_CURSOR_INFO cursorInfo = { 100, m_bDisplayCursor };
return SetConsoleCursorInfo(m_hConsole, &cursorInfo) != FALSE;
}
bool
CAnsiTerm::UpdateTextAttribute(void)
{
return SetConsoleTextAttribute(m_hConsole, m_Attribute) != FALSE;
}
bool
CAnsiTerm::GetCursorPosition(void)
{
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
if (GetConsoleScreenBufferInfo(m_hConsole, &bufferInfo))
{
m_Cursor = bufferInfo.dwCursorPosition;
m_Cursor.Y -= m_WindowOrigin.Y + (m_bOriginMode ? m_sTopMargin : 0);
return true;
}
return false;
}
bool
CAnsiTerm::SetCursorPosition(void)
{
COORD cursor = m_Cursor;
cursor.Y += m_WindowOrigin.Y + (m_bOriginMode ? m_sTopMargin : 0);
return SetConsoleCursorPosition(m_hConsole, cursor) != FALSE;
}
bool
CAnsiTerm::ScrollDisplay(int n, bool bWindowOnly)
{
SHORT nLines = (SHORT)n;
if (nLines == 0)
{
return true;
}
SMALL_RECT rectSource = { 0, 0, m_BufferSize.X - 1, m_BufferSize.Y - 1 };
COORD coordDest = { 0, 0 };
CHAR_INFO charInfo = { ' ', m_Attribute };
if (nLines > 0)
{
if (bWindowOnly || m_sTopMargin > 0)
{
coordDest.Y = m_WindowOrigin.Y + m_sTopMargin;
rectSource.Top = m_WindowOrigin.Y + m_sTopMargin + nLines;
rectSource.Bottom = m_WindowOrigin.Y + m_sBottomMargin;
}
else
{
rectSource.Top = nLines;
rectSource.Bottom = m_WindowOrigin.Y + m_sBottomMargin;
}
}
else
{
if (bWindowOnly)
{
coordDest.Y = m_WindowOrigin.Y + m_sTopMargin - nLines;
rectSource.Top = m_WindowOrigin.Y + m_sTopMargin;
rectSource.Bottom = m_WindowOrigin.Y + m_sBottomMargin + 1 + nLines;
}
else
{
coordDest.Y -= nLines;
}
rectSource.Bottom += nLines;
}
return ScrollConsoleScreenBuffer(m_hConsole, &rectSource, NULL, coordDest, &charInfo) != FALSE;
}
bool
CAnsiTerm::EraseLine(EraseType eType)
{
DWORD dwLength;
COORD coordStart = m_WindowOrigin;
coordStart.Y += m_Cursor.Y + (m_bOriginMode ? m_sTopMargin : 0);
switch (eType)
{
case EraseCursorToEnd:
if (m_Cursor.X < m_WindowSize.X)
{
coordStart.X += m_Cursor.X;
dwLength = m_BufferSize.X - m_Cursor.X;
}
else
{
dwLength = 0;
}
break;
case EraseBeginningToCursor:
if (m_Cursor.X < m_WindowSize.X)
{
dwLength = m_Cursor.X + 1;
}
else
{
dwLength = m_BufferSize.X;
}
break;
case EraseAll:
dwLength = m_BufferSize.X;
break;
default:
return false;
}
if (dwLength > 0)
{
DWORD dwWritten;
FillConsoleOutputAttribute(m_hConsole, m_Attribute, dwLength, coordStart, &dwWritten);
return FillConsoleOutputCharacter(m_hConsole, ' ', dwLength, coordStart, &dwWritten) != FALSE;
}
else
{
return true;
}
}
bool
CAnsiTerm::EraseDisplay(EraseType eType)
{
COORD coordStart = m_WindowOrigin;
DWORD dwLength;
switch (eType)
{
case EraseCursorToEnd:
if (m_Cursor.X < m_WindowSize.X)
{
coordStart.X += m_Cursor.X;
coordStart.Y += m_Cursor.Y + (m_bOriginMode ? m_sTopMargin : 0);
dwLength = (m_WindowSize.Y - m_Cursor.Y - (m_bOriginMode ? m_sTopMargin : 0)) * m_BufferSize.X - m_Cursor.X;
}
else if (m_Cursor.Y < (m_WindowSize.Y - 1))
{
coordStart.X = 0;
coordStart.Y += m_Cursor.Y + 1;
dwLength = (m_WindowSize.Y - m_Cursor.Y - (m_bOriginMode ? m_sTopMargin : 0) - 1) * m_BufferSize.X;
}
else
{
dwLength = 0;
}
break;
case EraseBeginningToCursor:
if (m_Cursor.X < m_WindowSize.X)
{
dwLength = (m_Cursor.Y + (m_bOriginMode ? m_sTopMargin : 0)) * m_BufferSize.X + m_Cursor.X;
}
else
{
dwLength = (m_Cursor.Y + (m_bOriginMode ? m_sTopMargin : 0) + 1) * m_BufferSize.X;
}
break;
case EraseAll:
dwLength = m_BufferSize.X * m_WindowSize.Y;
break;
default:
return false;
}
if (dwLength > 0)
{
DWORD dwWritten;
FillConsoleOutputAttribute(m_hConsole, m_Attribute, dwLength, coordStart, &dwWritten);
return FillConsoleOutputCharacter(m_hConsole, ' ', dwLength, coordStart, &dwWritten) != FALSE;
}
else
{
return true;
}
}
DWORD
CAnsiTerm::OutputText(void)
{
if (m_dwOutputCount == 0)
{
return 0;
}
DWORD dwTotalWritten = 0;
wchar_t * pwszCurrent = m_OutputBuffer;
DWORD dwLeftToWrite = m_dwOutputCount;
while (dwLeftToWrite > 0)
{
DWORD dwWritten;
if (m_Cursor.X >= m_WindowSize.X)
{
if (m_bAutoWrapMode)
{
ProcessLinefeed(true);
}
else
{
m_Cursor.X = m_WindowSize.X - 1;
SetCursorPosition();
if (WriteConsoleW(m_hConsole, &m_OutputBuffer[m_dwOutputCount - 1], 1, &dwWritten, NULL))
{
dbglog("OutputTest: WriteConsoleW error1\n");
assert(dwWritten == 1);
}
m_Cursor.X++;
dwTotalWritten += dwLeftToWrite;
break;
}
}
DWORD dwPartialCount = min(dwLeftToWrite, (DWORD)(m_WindowSize.X - m_Cursor.X));
if (WriteConsoleW(m_hConsole, pwszCurrent, dwPartialCount, &dwWritten, NULL))
{
dbglog("OutputTest: WriteConsoleW error2\n");
assert(dwWritten == dwPartialCount);
}
else
{
DWORD dwError = GetLastError();
}
m_Cursor.X += (SHORT)dwPartialCount;
pwszCurrent += dwPartialCount;
dwTotalWritten += dwPartialCount;
dwLeftToWrite -= dwPartialCount;
}
m_dwOutputCount = 0;
return dwTotalWritten;
}
bool
CAnsiTerm::ProcessBackspace(void)
{
if (m_Cursor.X > 0)
{
if (m_Cursor.X < m_WindowSize.X)
{
m_Cursor.X--;
}
else
{
m_Cursor.X = m_WindowSize.X - 1;
}
return SetCursorPosition();
}
return true;
}
bool
CAnsiTerm::ProcessTab(void)
{
if (m_Cursor.X >= m_WindowSize.X)
{
if (m_bAutoWrapMode)
{
ProcessLinefeed(true);
}
else
{
return true;
}
}
int newX = m_Cursor.X + 8;
newX &= ~7;
if (newX >= m_WindowSize.X)
{
newX = m_WindowSize.X - 1;
}
int cntSpaces = newX - m_Cursor.X;
for (int index = 0; index < cntSpaces; index++)
{
AddOutputData(L' ');
}
return true;
}
bool
CAnsiTerm::ProcessReverseLinefeed(void)
{
if (m_Cursor.Y == 0)
{
ScrollDisplay(-1, true);
}
else
{
m_Cursor.Y--;
}
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessLinefeed(bool bNewLine)
{
if (bNewLine)
{
m_Cursor.X = 0;
}
if (m_bOriginMode || (m_Cursor.Y >= m_sTopMargin && m_Cursor.Y <= m_sBottomMargin))
{
if (m_Cursor.Y >= (m_sBottomMargin - m_sTopMargin))
{
ScrollDisplay(1, false);
m_Cursor.Y = m_sBottomMargin;
}
else
{
m_Cursor.Y++;
}
}
else if (m_Cursor.Y < m_sBottomMargin)
{
m_Cursor.Y++;
}
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessReturn(void)
{
m_Cursor.X = 0;
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessDECALN(void)
{
// Fill the display with 'E' for adjusting the CRT on a VT100 - Ignore it
return true;
}
bool
CAnsiTerm::ProcessDECDHLB(void)
{
// Double Height Line Bottom - Not supported
return true;
}
bool
CAnsiTerm::ProcessDECDHLT(void)
{
// Double Height Line Top - Not supported
return true;
}
bool
CAnsiTerm::ProcessDECDWL(void)
{
// Double Width Line - Not supported
return true;
}
bool
CAnsiTerm::ProcessDECID(void)
{
return ProcessDA();
}
bool
CAnsiTerm::ProcessDECKPAM(void)
{
// Keypad Application Mode - Not yet implemented
return true;
}
bool
CAnsiTerm::ProcessDECKPNM(void)
{
// Keypad Numeric Mode - Not yet implemented
return true;
}
bool
CAnsiTerm::ProcessDECLL(void)
{
// Load LEDs - Not Supported
return true;
}
bool
CAnsiTerm::ProcessDECRC(void)
{
return ProcessRCP();
}
bool
CAnsiTerm::ProcessDECREQTPARM(void)
{
// Request Terminal Parameters (Baud Rate, Parity, etc) - Not supported
return true;
}
bool
CAnsiTerm::ProcessDECSC(void)
{
return ProcessSCP();
}
bool
CAnsiTerm::ProcessDECSTBM(void)
{
assert(m_ParameterCount >= 1);
if (m_Parameters[0] > 0)
{
m_Parameters[0]--;
}
if (m_ParameterCount < 2)
{
m_Parameters[1] = m_WindowSize.Y - 1;
}
else
{
if (m_Parameters[1] > 0)
{
m_Parameters[1]--;
}
else
{
m_Parameters[1] = m_WindowSize.Y - 1;
}
}
if (m_Parameters[0] >= m_WindowSize.Y ||
m_Parameters[1] >= m_WindowSize.Y ||
m_Parameters[0] >= m_Parameters[1])
{
return false;
}
m_sTopMargin = (SHORT)m_Parameters[0];
m_sBottomMargin = (SHORT)m_Parameters[1];
m_Cursor.X = 0;
m_Cursor.Y = 0;
SetCursorPosition();
return true;
}
bool
CAnsiTerm::ProcessDECSWL(void)
{
// Single Width Line - Since Double Width Line isn't supported, this isn't necessary
return true;
}
bool
CAnsiTerm::ProcessDECTST(void)
{
// Perform Self Test - Not supported
return true;
}
bool
CAnsiTerm::ProcessCUB(void)
{
if (m_Parameters[0] == 0)
{
m_Parameters[0]++;
}
m_Cursor.X -= min(m_Cursor.X, m_Parameters[0]);
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessCUD(void)
{
if (m_Parameters[0] == 0)
{
m_Parameters[0]++;
}
m_Cursor.Y += min((m_bOriginMode ? m_sBottomMargin : m_WindowSize.Y - 1) - m_Cursor.Y, m_Parameters[0]);
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessCUF(void)
{
if (m_Parameters[0] == 0)
{
m_Parameters[0]++;
}
m_Cursor.X += min(m_WindowSize.X - m_Cursor.X - 1, m_Parameters[0]);
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessCUP(void)
{
return ProcessHVP();
}
bool
CAnsiTerm::ProcessCUU(void)
{
if (m_Parameters[0] == 0)
{
m_Parameters[0]++;
}
m_Cursor.Y -= min(m_Cursor.Y, m_Parameters[0]);
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessDA(void)
{
// Send Device Attributes - Not supported
return true;
}
bool
CAnsiTerm::ProcessDSR(void)
{
// Send Device Status Request - Not supported
return true;
}
bool
CAnsiTerm::ProcessED(void)
{
return EraseDisplay((EraseType)m_Parameters[0]);
}
bool
CAnsiTerm::ProcessEL(void)
{
return EraseLine((EraseType)m_Parameters[0]);
}
bool
CAnsiTerm::ProcessHTS(void)
{
// Soft Tab Set - Not implemented yet
return true;
}
bool
CAnsiTerm::ProcessHVP(void)
{
assert(m_ParameterCount >= 1);
if (m_Parameters[0] > 0)
{
m_Parameters[0]--;
}
if (m_ParameterCount < 2)
{
m_Parameters[1] = 0;
}
else
{
if (m_Parameters[1] > 0)
{
m_Parameters[1]--;
}
}
if (m_bOriginMode)
{
if (m_Parameters[0] >= (m_sBottomMargin - m_sTopMargin + 1))
{
m_Parameters[0] = m_sBottomMargin - m_sTopMargin;
}
}
else
{
if (m_Parameters[0] >= m_WindowSize.Y)
{
m_Parameters[0] = m_WindowSize.Y - 1;
}
}
if (m_Parameters[1] >= m_WindowSize.X)
{
m_Parameters[1] = m_WindowSize.X - 1;
}
m_Cursor.Y = (SHORT)m_Parameters[0];
m_Cursor.X = (SHORT)m_Parameters[1];
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessIND(void)
{
return ProcessLinefeed(false);
}
bool
CAnsiTerm::ProcessNEL(void)
{
return ProcessLinefeed(true);
}
bool
CAnsiTerm::ProcessRCP(void)
{
m_Cursor = m_SavedCursor;
return SetCursorPosition();
}
bool
CAnsiTerm::ProcessRI(void)
{
return ProcessReverseLinefeed();
}
bool
CAnsiTerm::ProcessRIS(void)
{
return ResetTerm();
}
bool
CAnsiTerm::ProcessRM(void)
{
bool bret = true;
if (m_bPrivateParameters)
{
for (int index = 0; index < m_ParameterCount; index++)
{
switch (m_Parameters[index])
{
case 0:
default:
dbglog("ProcessRM: illegal private param %d\n",m_Parameters[index]);
bret = false;
break;
case DECCKM:
m_bCursorKeyMode = false;
break;
case DECANM:
m_bAnsiMode = false;
break;
case DECCOLM:
m_bColumnMode = false;
break;
case DECSCLM:
m_bScrollingMode = false;
break;
case DECSCNM:
m_bScreenMode = false;
break;
case DECOM:
m_bOriginMode = false;
m_Cursor.X = 0;
m_Cursor.Y = 0;
SetCursorPosition();
break;
case DECAWM:
m_bAutoWrapMode = false;
break;
case DECARM:
m_bAutoRepeatingMode = false;
break;
case DECINLM:
m_bInterlaceMode = false;
break;
case DECTCEM:
m_bDisplayCursor = false;
DisplayCursor();
break;
}
}
}
else
{
for (int index = 0; index < m_ParameterCount; index++)
{
switch (m_Parameters[index])
{
case 20: m_bLineFeedNewLineMode = false; break; // LNM
default:
dbglog("ProcessRM: illegal public param %d\n",m_Parameters[index]);
bret = false; break;
}
}
}
return bret;
}
bool
CAnsiTerm::ProcessSCP(void)
{
m_SavedCursor = m_Cursor;
return true;
}
bool
CAnsiTerm::ProcessSCSG0(char ch)
{
switch (ch)
{
case 'B': // ASCII Charset
m_G0Charset = AsciiCharset;
break;
case '0': // Special Graphics Charset
m_G0Charset = SpecialGraphicsCharset;
break;
case 'A': // UK Charset
case '1': // Alternate Character ROM Standard Charset
case '2': // Alternate Character ROM Special Graphics Charset
default: // Unsupported
return false;
}
return true;
}
bool
CAnsiTerm::ProcessSCSG1(char ch)
{
switch (ch)
{
case 'B': // ASCII Charset
m_G1Charset = AsciiCharset;
break;
case '0': // Special Graphics Charset
m_G1Charset = SpecialGraphicsCharset;
break;
case 'A': // UK Charset
case '1': // Alternate Character ROM Standard Charset
case '2': // Alternate Character ROM Special Graphics Charset
default: // Unsupported
return false;
}
return true;
}
bool
CAnsiTerm::ProcessSGR(void)
{
for (int index = 0; index < m_ParameterCount; index++)
{
switch (m_Parameters[index])
{
case 0:
m_Attribute = kDefaultAttribute;
break;
case 1:
m_Attribute |= FOREGROUND_INTENSITY;
break;
case 4:
m_Attribute |= COMMON_LVB_UNDERSCORE;
break;
case 5:
// Blinking isn't supported
break;
case 7:
m_Attribute |= COMMON_LVB_REVERSE_VIDEO;
break;
case 22:
m_Attribute &= ~FOREGROUND_INTENSITY;
break;
case 24:
m_Attribute &= ~COMMON_LVB_UNDERSCORE;
break;
case 25:
// Blinking isn't supported
break;
case 27:
m_Attribute &= ~COMMON_LVB_REVERSE_VIDEO;
break;
case 30: // Black text
m_Attribute &= ~(FOREGROUND_INTENSITY | COMMON_LVB_UNDERSCORE |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
case 31: // Red text
m_Attribute &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
m_Attribute |= FOREGROUND_RED;
break;
case 32: // Green text
m_Attribute &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
m_Attribute |= FOREGROUND_GREEN;
break;
case 33: // Yellow text
m_Attribute &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
m_Attribute |= FOREGROUND_RED | FOREGROUND_GREEN;
break;
case 34: // Blue text
m_Attribute &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
m_Attribute |= FOREGROUND_BLUE;
break;
case 35: // Magenta text
m_Attribute &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
m_Attribute |= FOREGROUND_RED | FOREGROUND_BLUE;
break;
case 36: // Cyan text
m_Attribute &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
m_Attribute |= FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case 37: // White text
m_Attribute |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case 40: // Black background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
break;
case 41: // Red background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
m_Attribute |= BACKGROUND_RED;
break;
case 42: // Green background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
m_Attribute |= BACKGROUND_GREEN;
break;
case 43: // Yellow background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
m_Attribute |= BACKGROUND_RED | BACKGROUND_GREEN;
break;
case 44: // Blue background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
m_Attribute |= BACKGROUND_BLUE;
break;
case 45: // Magenta background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
m_Attribute |= BACKGROUND_RED | BACKGROUND_BLUE;
break;
case 46: // Cyan background
m_Attribute &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
m_Attribute |= BACKGROUND_GREEN | BACKGROUND_BLUE;
break;
case 47: // White background
m_Attribute |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
break;
default:
dbglog("ERROR: bad SGR param %d (0x%02x)\n",index,index);
m_Attribute = kDefaultAttribute;
break;
}
}
UpdateTextAttribute();
return true;
}
bool
CAnsiTerm::ProcessSM(void)
{
dbglog("ProcessSM: start, priv=%d param0=%d cnt=%d\n",
m_bPrivateParameters,m_Parameters[0],m_ParameterCount);
if (m_bPrivateParameters)
{
for (int index = 0; index < m_ParameterCount; index++)
{
switch (m_Parameters[0])
{
case 0:
default:
dbglog("ERROR: bad private SM param %d\n",m_Parameters[0]);
assert(false);
break;
case DECCKM:
m_bCursorKeyMode = true;
break;
case DECANM:
m_bAnsiMode = true;
break;
case DECCOLM:
m_bColumnMode = true;
break;
case DECSCLM:
m_bScrollingMode = true;
break;
case DECSCNM:
m_bScreenMode = true;
break;
case DECOM:
m_bOriginMode = true;
m_Cursor.X = 0;
m_Cursor.Y = 0;
m_SavedCursor = m_Cursor;
SetCursorPosition();
break;
case DECAWM:
m_bAutoWrapMode = true;
break;
case DECARM:
m_bAutoRepeatingMode = true;
break;
case DECINLM:
m_bInterlaceMode = true;
break;
case DECTCEM:
m_bDisplayCursor = true;
DisplayCursor();
break;
}
}
}
else
{
for (int index = 0; index < m_ParameterCount; index++)
{
switch (m_Parameters[0])
{
case 20: m_bLineFeedNewLineMode = true; break; // LNM
default:
dbglog("ProcessSM: param %d != 20\n",m_Parameters[0]);
assert(false);
break;
}
}
}
return true;
}
bool
CAnsiTerm::ProcessTBC(void)
{
// Soft Tab Clear - Not implemented yet
return true;
}
static CAnsiTerm *g_pDisplay;
typedef unsigned char uchar;
extern "C"
{
void console_open(char fdebugcmd)
{
g_pDisplay = new CAnsiTerm();
}
void console_close(void)
{
delete g_pDisplay;
g_pDisplay = NULL;
}
int console_in(DWORD keydata, uchar *pdata, int len)
{
if (keydata == ~0)
{
g_pDisplay->WindowSizeChanged(false);
return 0;
}
return g_pDisplay->ProcessInput(*(CAnsiTerm::KeyCode *)&keydata, pdata, len);
}
void console_out(uchar *pdata, int len)
{
if (len > 0)
{
g_pDisplay->ProcessOutput(pdata, len);
}
}
}
|