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
|
//
// System.ConsoleDriver
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2005,2006 Novell, Inc (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if (NET_2_0||BOOTSTRAP_NET_2_0) && !NET_2_1
//#define DEBUG
using System.Collections;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace System {
class TermInfoDriver : IConsoleDriver {
// This points to a variable that is updated by unmanage code on window size changes.
unsafe static int *native_terminal_size;
// The current size that we believe we have
static int terminal_size;
//static uint flag = 0xdeadbeef;
static string [] locations = { "/etc/terminfo", "/usr/share/terminfo", "/usr/lib/terminfo" };
TermInfoReader reader;
int cursorLeft;
int cursorTop;
string title = String.Empty;
string titleFormat = String.Empty;
bool cursorVisible = true;
string csrVisible;
string csrInvisible;
string clear;
string bell;
string term;
StreamReader stdin;
CStreamWriter stdout;
int windowWidth;
int windowHeight;
//int windowTop;
//int windowLeft;
int bufferHeight;
int bufferWidth;
char [] buffer;
int readpos;
int writepos;
string keypadXmit, keypadLocal;
bool controlCAsInput;
bool inited;
object initLock = new object ();
bool initKeys;
string origPair;
string origColors;
string cursorAddress;
ConsoleColor fgcolor = ConsoleColor.White;
ConsoleColor bgcolor = ConsoleColor.Black;
bool color16 = false; // terminal supports 16 colors
string setlfgcolor;
string setlbgcolor;
string setfgcolor;
string setbgcolor;
bool noGetPosition;
Hashtable keymap;
ByteMatcher rootmap;
bool home_1_1; // if true, we have to add 1 to x and y when using cursorAddress
int rl_startx = -1, rl_starty = -1;
byte [] control_characters; // Indexed by ControlCharacters.XXXXXX
#if DEBUG
StreamWriter logger;
#endif
static string SearchTerminfo (string term)
{
if (term == null || term == String.Empty)
return null;
// Ignore TERMINFO and TERMINFO_DIRS by now
//string terminfo = Environment.GetEnvironmentVariable ("TERMINFO");
//string terminfoDirs = Environment.GetEnvironmentVariable ("TERMINFO_DIRS");
foreach (string dir in locations) {
if (!Directory.Exists (dir))
continue;
string path = Path.Combine (dir, term.Substring (0, 1));
if (!Directory.Exists (dir))
continue;
path = Path.Combine (path, term);
if (!File.Exists (path))
continue;
return path;
}
return null;
}
void WriteConsole (string str)
{
if (str == null)
return;
stdout.InternalWriteString (str);
}
public TermInfoDriver ()
: this (Environment.GetEnvironmentVariable ("TERM"))
{
}
public TermInfoDriver (string term)
{
#if DEBUG
File.Delete ("console.log");
logger = new StreamWriter (File.OpenWrite ("console.log"));
#endif
this.term = term;
if (term == "xterm") {
reader = new TermInfoReader (term, KnownTerminals.xterm);
color16 = true;
} else if (term == "linux") {
reader = new TermInfoReader (term, KnownTerminals.linux);
color16 = true;
} else {
string filename = SearchTerminfo (term);
if (filename != null)
reader = new TermInfoReader (term, filename);
}
if (reader == null)
reader = new TermInfoReader (term, KnownTerminals.ansi);
if (!(Console.stdout is CStreamWriter)) {
// Application set its own stdout, we need a reference to the real stdout
stdout = new CStreamWriter (Console.OpenStandardOutput (0), Console.OutputEncoding);
((StreamWriter) stdout).AutoFlush = true;
} else {
stdout = (CStreamWriter) Console.stdout;
}
}
public bool Initialized {
get { return inited; }
}
public void Init ()
{
if (inited)
return;
lock (initLock){
if (inited)
return;
inited = true;
/* This should not happen any more, since it is checked for in Console */
if (!ConsoleDriver.IsConsole)
throw new IOException ("Not a tty.");
ConsoleDriver.SetEcho (false);
string endString = null;
keypadXmit = reader.Get (TermInfoStrings.KeypadXmit);
keypadLocal = reader.Get (TermInfoStrings.KeypadLocal);
if (keypadXmit != null) {
WriteConsole (keypadXmit); // Needed to get the arrows working
if (keypadLocal != null)
endString += keypadLocal;
}
origPair = reader.Get (TermInfoStrings.OrigPair);
origColors = reader.Get (TermInfoStrings.OrigColors);
setfgcolor = MangleParameters (reader.Get (TermInfoStrings.SetAForeground));
setbgcolor = MangleParameters (reader.Get (TermInfoStrings.SetABackground));
// lighter fg colours are 90 -> 97 rather than 30 -> 37
setlfgcolor = color16 ? setfgcolor.Replace ("[3", "[9") : setfgcolor;
// lighter bg colours are 100 -> 107 rather than 40 -> 47
setlbgcolor = color16 ? setbgcolor.Replace ("[4", "[10") : setbgcolor;
string resetColors = (origColors == null) ? origPair : origColors;
if (resetColors != null)
endString += resetColors;
unsafe {
if (!ConsoleDriver.TtySetup (keypadXmit, endString, out control_characters, out native_terminal_size))
throw new IOException ("Error initializing terminal.");
}
stdin = new StreamReader (Console.OpenStandardInput (0), Console.InputEncoding);
clear = reader.Get (TermInfoStrings.ClearScreen);
bell = reader.Get (TermInfoStrings.Bell);
if (clear == null) {
clear = reader.Get (TermInfoStrings.CursorHome);
clear += reader.Get (TermInfoStrings.ClrEos);
}
csrVisible = reader.Get (TermInfoStrings.CursorNormal);
if (csrVisible == null)
csrVisible = reader.Get (TermInfoStrings.CursorVisible);
csrInvisible = reader.Get (TermInfoStrings.CursorInvisible);
if (term == "cygwin" || term == "linux" || (term != null && term.StartsWith ("xterm")) ||
term == "rxvt" || term == "dtterm") {
titleFormat = "\x1b]0;{0}\x7"; // icon + window title
} else if (term == "iris-ansi") {
titleFormat = "\x1bP1.y{0}\x1b\\"; // not tested
} else if (term == "sun-cmd") {
titleFormat = "\x1b]l{0}\x1b\\"; // not tested
}
cursorAddress = reader.Get (TermInfoStrings.CursorAddress);
if (cursorAddress != null) {
string result = cursorAddress.Replace ("%i", String.Empty);
home_1_1 = (cursorAddress != result);
cursorAddress = MangleParameters (result);
}
GetCursorPosition ();
#if DEBUG
logger.WriteLine ("noGetPosition: {0} left: {1} top: {2}", noGetPosition, cursorLeft, cursorTop);
logger.Flush ();
#endif
if (noGetPosition) {
WriteConsole (clear);
cursorLeft = 0;
cursorTop = 0;
}
}
}
static string MangleParameters (string str)
{
if (str == null)
return null;
str = str.Replace ("{", "{{");
str = str.Replace ("}", "}}");
str = str.Replace ("%p1%d", "{0}");
return str.Replace ("%p2%d", "{1}");
}
static int TranslateColor (ConsoleColor desired, out bool light)
{
switch (desired) {
// Dark colours
case ConsoleColor.Black:
light = false;
return 0;
case ConsoleColor.DarkRed:
light = false;
return 1;
case ConsoleColor.DarkGreen:
light = false;
return 2;
case ConsoleColor.DarkYellow:
light = false;
return 3;
case ConsoleColor.DarkBlue:
light = false;
return 4;
case ConsoleColor.DarkMagenta:
light = false;
return 5;
case ConsoleColor.DarkCyan:
light = false;
return 6;
case ConsoleColor.Gray:
light = false;
return 7;
// Light colours
case ConsoleColor.DarkGray:
light = true;
return 0;
case ConsoleColor.Red:
light = true;
return 1;
case ConsoleColor.Green:
light = true;
return 2;
case ConsoleColor.Yellow:
light = true;
return 3;
case ConsoleColor.Blue:
light = true;
return 4;
case ConsoleColor.Magenta:
light = true;
return 5;
case ConsoleColor.Cyan:
light = true;
return 6;
case ConsoleColor.White:
light = true;
return 7;
}
light = false;
return 0;
}
void IncrementX ()
{
cursorLeft++;
if (cursorLeft >= WindowWidth) {
cursorTop++;
cursorLeft = 0;
if (cursorTop >= WindowHeight) {
// Writing beyond the initial screen
if (rl_starty != -1) rl_starty--;
cursorTop--;
}
}
}
// Should never get called unless inited
public void WriteSpecialKey (ConsoleKeyInfo key)
{
switch (key.Key) {
case ConsoleKey.Backspace:
if (cursorLeft > 0) {
if (cursorLeft <= rl_startx && cursorTop == rl_starty)
break;
cursorLeft--;
SetCursorPosition (cursorLeft, cursorTop);
WriteConsole (" ");
SetCursorPosition (cursorLeft, cursorTop);
}
#if DEBUG
logger.WriteLine ("BS left: {0} top: {1}", cursorLeft, cursorTop);
logger.Flush ();
#endif
break;
case ConsoleKey.Tab:
int n = 8 - (cursorLeft % 8);
for (int i = 0; i < n; i++){
IncrementX ();
}
WriteConsole ("\t");
break;
case ConsoleKey.Clear:
WriteConsole (clear);
cursorLeft = 0;
cursorTop = 0;
break;
case ConsoleKey.Enter:
break;
default:
break;
}
#if DEBUG
logger.WriteLine ("left: {0} top: {1}", cursorLeft, cursorTop);
logger.Flush ();
#endif
}
// Should never get called unless inited
public void WriteSpecialKey (char c)
{
WriteSpecialKey (CreateKeyInfoFromInt (c, false));
}
public bool IsSpecialKey (ConsoleKeyInfo key)
{
if (!inited)
return false;
switch (key.Key) {
case ConsoleKey.Backspace:
return true;
case ConsoleKey.Tab:
return true;
case ConsoleKey.Clear:
return true;
case ConsoleKey.Enter:
cursorLeft = 0;
cursorTop++;
if (cursorTop >= WindowHeight) {
cursorTop--;
//TODO: scroll up
}
return false;
default:
// CStreamWriter will handle writing this key
IncrementX ();
return false;
}
}
public bool IsSpecialKey (char c)
{
return IsSpecialKey (CreateKeyInfoFromInt (c, false));
}
public ConsoleColor BackgroundColor {
get {
if (!inited) {
Init ();
}
return bgcolor;
}
set {
if (!inited) {
Init ();
}
bgcolor = value;
bool light;
int colour = TranslateColor (value, out light);
if (light)
WriteConsole (String.Format (setlbgcolor, colour));
else
WriteConsole (String.Format (setbgcolor, colour));
}
}
public ConsoleColor ForegroundColor {
get {
if (!inited) {
Init ();
}
return fgcolor;
}
set {
if (!inited) {
Init ();
}
fgcolor = value;
bool light;
int colour = TranslateColor (value, out light);
if (light)
WriteConsole (String.Format (setlfgcolor, colour));
else
WriteConsole (String.Format (setfgcolor, colour));
}
}
void GetCursorPosition ()
{
int row = 0, col = 0;
int b;
// First, get any data in the input buffer. Merely reduces the likelyhood of getting an error
int inqueue = ConsoleDriver.InternalKeyAvailable (1000);
while (inqueue-- > 0){
b = stdin.Read ();
AddToBuffer (b);
}
// Then try to probe for the cursor coordinates
WriteConsole ("\x1b[6n");
if (ConsoleDriver.InternalKeyAvailable (1000) <= 0) {
noGetPosition = true;
return;
}
b = stdin.Read ();
while (b != '\x1b') {
AddToBuffer (b);
if (ConsoleDriver.InternalKeyAvailable (100) <= 0)
return;
b = stdin.Read ();
}
b = stdin.Read ();
if (b != '[') {
AddToBuffer ('\x1b');
AddToBuffer (b);
return;
}
b = stdin.Read ();
if (b != ';') {
row = b - '0';
b = stdin.Read ();
while ((b >= '0') && (b <= '9')) {
row = row * 10 + b - '0';
b = stdin.Read ();
}
// Row/col is 0 based
row --;
}
b = stdin.Read ();
if (b != 'R') {
col = b - '0';
b = stdin.Read ();
while ((b >= '0') && (b <= '9')) {
col = col * 10 + b - '0';
b = stdin.Read ();
}
// Row/col is 0 based
col --;
}
#if DEBUG
logger.WriteLine ("GetCursorPosition: {0}, {1}", col, row);
logger.Flush ();
#endif
cursorLeft = col;
cursorTop = row;
}
public int BufferHeight {
get {
if (!inited) {
Init ();
}
return bufferHeight;
}
set {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public int BufferWidth {
get {
if (!inited) {
Init ();
}
return bufferWidth;
}
set {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public bool CapsLock {
get {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public int CursorLeft {
get {
if (!inited) {
Init ();
}
return cursorLeft;
}
set {
if (!inited) {
Init ();
}
SetCursorPosition (value, CursorTop);
}
}
public int CursorTop {
get {
if (!inited) {
Init ();
}
return cursorTop;
}
set {
if (!inited) {
Init ();
}
SetCursorPosition (CursorLeft, value);
}
}
public bool CursorVisible {
get {
if (!inited) {
Init ();
}
return cursorVisible;
}
set {
if (!inited) {
Init ();
}
cursorVisible = value;
WriteConsole ((value ? csrVisible : csrInvisible));
}
}
// we have CursorNormal vs. CursorVisible...
[MonoTODO]
public int CursorSize {
get {
if (!inited) {
Init ();
}
return 1;
}
set {
if (!inited) {
Init ();
}
}
}
public bool KeyAvailable {
get {
if (!inited) {
Init ();
}
return (writepos > readpos || ConsoleDriver.InternalKeyAvailable (0) > 0);
}
}
// We don't know these next 2 values, so return something reasonable
public int LargestWindowHeight {
get { return WindowHeight; }
}
public int LargestWindowWidth {
get { return WindowWidth; }
}
public bool NumberLock {
get {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public string Title {
get {
if (!inited) {
Init ();
}
return title;
}
set {
if (!inited) {
Init ();
}
title = value;
WriteConsole (String.Format (titleFormat, value));
}
}
public bool TreatControlCAsInput {
get {
if (!inited) {
Init ();
}
return controlCAsInput;
}
set {
if (!inited) {
Init ();
}
if (controlCAsInput == value)
return;
ConsoleDriver.SetBreak (value);
controlCAsInput = value;
}
}
//
// Requries that caller calls Init () if not !inited.
//
unsafe void CheckWindowDimensions ()
{
if (native_terminal_size == null || terminal_size == *native_terminal_size)
return;
if (*native_terminal_size == -1){
int c = reader.Get (TermInfoNumbers.Columns);
if (c != 0)
windowWidth = c;
c = reader.Get (TermInfoNumbers.Lines);
if (c != 0)
windowHeight = c;
} else {
terminal_size = *native_terminal_size;
windowWidth = terminal_size >> 16;
windowHeight = terminal_size & 0xffff;
}
bufferHeight = windowHeight;
bufferWidth = windowWidth;
}
public int WindowHeight {
get {
if (!inited) {
Init ();
}
CheckWindowDimensions ();
return windowHeight;
}
set {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public int WindowLeft {
get {
if (!inited) {
Init ();
}
//CheckWindowDimensions ();
return 0;
}
set {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public int WindowTop {
get {
if (!inited) {
Init ();
}
//CheckWindowDimensions ();
return 0;
}
set {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public int WindowWidth {
get {
if (!inited) {
Init ();
}
CheckWindowDimensions ();
return windowWidth;
}
set {
if (!inited) {
Init ();
}
throw new NotSupportedException ();
}
}
public void Clear ()
{
if (!inited) {
Init ();
}
WriteConsole (clear);
cursorLeft = 0;
cursorTop = 0;
}
public void Beep (int frequency, int duration)
{
if (!inited) {
Init ();
}
WriteConsole (bell);
}
public void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
int targetLeft, int targetTop, Char sourceChar,
ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
{
if (!inited) {
Init ();
}
throw new NotImplementedException ();
}
void AddToBuffer (int b)
{
if (buffer == null) {
buffer = new char [1024];
} else if (writepos >= buffer.Length) {
char [] newbuf = new char [buffer.Length * 2];
Buffer.BlockCopy (buffer, 0, newbuf, 0, buffer.Length);
buffer = newbuf;
}
buffer [writepos++] = (char) b;
}
void AdjustBuffer ()
{
if (readpos >= writepos) {
readpos = writepos = 0;
}
}
ConsoleKeyInfo CreateKeyInfoFromInt (int n, bool alt)
{
char c = (char) n;
ConsoleKey key = (ConsoleKey)n;
bool shift = false;
bool ctrl = false;
switch (n){
case 10:
key = ConsoleKey.Enter;
break;
case 0x20:
key = ConsoleKey.Spacebar;
break;
case 45:
key = ConsoleKey.Subtract;
break;
case 43:
key = ConsoleKey.Add;
break;
case 47:
key = ConsoleKey.Divide;
break;
case 42:
key = ConsoleKey.Multiply;
break;
case 8: case 9: case 12: case 13: case 19:
/* Values in ConsoleKey */
break;
case 27:
key = ConsoleKey.Escape;
break;
default:
if (n >= 1 && n <= 26) {
// For Ctrl-a to Ctrl-z.
ctrl = true;
key = ConsoleKey.A + n - 1;
} else if (n >= 'a' && n <= 'z') {
key = ConsoleKey.A - 'a' + n;
} else if (n >= 'A' && n <= 'Z') {
shift = true;
} else if (n >= '0' && n <= '9') {
} else
key = 0;
break;
}
return new ConsoleKeyInfo (c, key, shift, alt, ctrl);
}
object GetKeyFromBuffer (bool cooked)
{
if (readpos >= writepos)
return null;
int next = buffer [readpos];
if (!cooked || !rootmap.StartsWith (next)) {
readpos++;
AdjustBuffer ();
return CreateKeyInfoFromInt (next, false);
}
int used;
TermInfoStrings str = rootmap.Match (buffer, readpos, writepos - readpos, out used);
if ((int) str == -1){
// Escape sequences: alt keys are sent as ESC-key
if (buffer [readpos] == 27 && (writepos - readpos) >= 2){
readpos += 2;
AdjustBuffer ();
if (buffer [readpos+1] == 127)
return new ConsoleKeyInfo ((char)8, ConsoleKey.Backspace, false, true, false);
return CreateKeyInfoFromInt (buffer [readpos+1], true);
} else
return null;
}
ConsoleKeyInfo key;
if (keymap [str] != null) {
key = (ConsoleKeyInfo) keymap [str];
} else {
readpos++;
AdjustBuffer ();
return CreateKeyInfoFromInt (next, false);
}
readpos += used;
AdjustBuffer ();
return key;
}
ConsoleKeyInfo ReadKeyInternal (out bool fresh)
{
if (!inited)
Init ();
InitKeys ();
object o;
if ((o = GetKeyFromBuffer (true)) == null) {
do {
if (ConsoleDriver.InternalKeyAvailable (150) > 0) {
do {
AddToBuffer (stdin.Read ());
} while (ConsoleDriver.InternalKeyAvailable (0) > 0);
} else if (stdin.DataAvailable ()) {
do {
AddToBuffer (stdin.Read ());
} while (stdin.DataAvailable ());
} else {
if ((o = GetKeyFromBuffer (false)) != null)
break;
AddToBuffer (stdin.Read ());
}
o = GetKeyFromBuffer (true);
} while (o == null);
// freshly read character
fresh = true;
} else {
// this char was pre-buffered (e.g. not fresh)
fresh = false;
}
return (ConsoleKeyInfo) o;
}
#region Input echoing optimization
bool InputPending ()
{
// check if we've got pending input we can read immediately
return readpos < writepos || stdin.DataAvailable ();
}
char [] echobuf = null;
int echon = 0;
// Queues a character to be echo'd back to the console
void QueueEcho (char c)
{
if (echobuf == null)
echobuf = new char [1024];
echobuf[echon++] = c;
if (echon == echobuf.Length || !InputPending ()) {
// blit our echo buffer to the console
stdout.InternalWriteChars (echobuf, echon);
echon = 0;
}
}
// Queues a key to be echo'd back to the console
void Echo (ConsoleKeyInfo key)
{
if (!IsSpecialKey (key)) {
QueueEcho (key.KeyChar);
return;
}
// flush pending echo's
EchoFlush ();
WriteSpecialKey (key);
}
// Flush the pending echo queue
void EchoFlush ()
{
if (echon == 0)
return;
// flush our echo buffer to the console
stdout.InternalWriteChars (echobuf, echon);
echon = 0;
}
#endregion
public int Read ([In, Out] char [] dest, int index, int count)
{
bool fresh, echo = false;
StringBuilder sbuf;
ConsoleKeyInfo key;
int BoL = 0; // Beginning-of-Line marker (can't backspace beyond this)
object o;
char c;
sbuf = new StringBuilder ();
// consume buffered keys first (do not echo, these have already been echo'd)
while (true) {
if ((o = GetKeyFromBuffer (true)) == null)
break;
key = (ConsoleKeyInfo) o;
c = key.KeyChar;
if (key.Key != ConsoleKey.Backspace) {
if (key.Key == ConsoleKey.Enter)
BoL = sbuf.Length;
sbuf.Append (c);
} else if (sbuf.Length > BoL) {
sbuf.Length--;
}
}
// continue reading until Enter is hit
rl_startx = cursorLeft;
rl_starty = cursorTop;
do {
key = ReadKeyInternal (out fresh);
echo = echo || fresh;
c = key.KeyChar;
if (key.Key != ConsoleKey.Backspace) {
if (key.Key == ConsoleKey.Enter)
BoL = sbuf.Length;
sbuf.Append (c);
} else if (sbuf.Length > BoL) {
sbuf.Length--;
} else {
continue;
}
// echo fresh keys back to the console
if (echo)
Echo (key);
} while (key.Key != ConsoleKey.Enter);
EchoFlush ();
rl_startx = -1;
rl_starty = -1;
// copy up to count chars into dest
int nread = 0;
while (count > 0 && nread < sbuf.Length) {
dest[index + nread] = sbuf[nread];
nread++;
count--;
}
// put the rest back into our key buffer
for (int i = nread; i < sbuf.Length; i++)
AddToBuffer (sbuf[i]);
return nread;
}
public ConsoleKeyInfo ReadKey (bool intercept)
{
bool fresh;
ConsoleKeyInfo key = ReadKeyInternal (out fresh);
if (!intercept && fresh) {
// echo the fresh key back to the console
Echo (key);
EchoFlush ();
}
return key;
}
public string ReadLine ()
{
if (!inited)
Init ();
// Hack to make Iron Python work (since it goes behind our backs
// when writing to the console thus preventing us from keeping
// cursor state normally).
GetCursorPosition ();
StringBuilder builder = new StringBuilder ();
bool fresh, echo = false;
ConsoleKeyInfo key;
char c;
rl_startx = cursorLeft;
rl_starty = cursorTop;
char eof = (char) control_characters [ControlCharacters.EOF];
do {
key = ReadKeyInternal (out fresh);
echo = echo || fresh;
c = key.KeyChar;
// EOF -> Ctrl-D (EOT) pressed.
if (c == eof && c != 0 && builder.Length == 0)
return null;
if (key.Key != ConsoleKey.Enter) {
if (key.Key != ConsoleKey.Backspace) {
builder.Append (c);
} else if (builder.Length > 0) {
builder.Length--;
} else {
// skips over echoing the key to the console
continue;
}
}
// echo fresh keys back to the console
if (echo)
Echo (key);
} while (key.Key != ConsoleKey.Enter);
EchoFlush ();
rl_startx = -1;
rl_starty = -1;
return builder.ToString ();
}
public void ResetColor ()
{
if (!inited) {
Init ();
}
string str = (origPair != null) ? origPair : origColors;
WriteConsole (str);
}
public void SetBufferSize (int width, int height)
{
if (!inited) {
Init ();
}
throw new NotImplementedException (String.Empty);
}
public void SetCursorPosition (int left, int top)
{
if (!inited) {
Init ();
}
CheckWindowDimensions ();
if (left < 0 || left >= bufferWidth)
throw new ArgumentOutOfRangeException ("left", "Value must be positive and below the buffer width.");
if (top < 0 || top >= bufferHeight)
throw new ArgumentOutOfRangeException ("top", "Value must be positive and below the buffer height.");
// Either CursorAddress or nothing.
// We might want to play with up/down/left/right/home when ca is not available.
if (cursorAddress == null)
throw new NotSupportedException ("This terminal does not suport setting the cursor position.");
int one = (home_1_1 ? 1 : 0);
WriteConsole (String.Format (cursorAddress, top + one, left + one));
cursorLeft = left;
cursorTop = top;
}
public void SetWindowPosition (int left, int top)
{
if (!inited) {
Init ();
}
// No need to throw exceptions here.
//throw new NotSupportedException ();
}
public void SetWindowSize (int width, int height)
{
if (!inited) {
Init ();
}
// No need to throw exceptions here.
//throw new NotSupportedException ();
}
void CreateKeyMap ()
{
keymap = new Hashtable ();
keymap [TermInfoStrings.KeyBackspace] = new ConsoleKeyInfo ('\0', ConsoleKey.Backspace, false, false, false);
keymap [TermInfoStrings.KeyClear] = new ConsoleKeyInfo ('\0', ConsoleKey.Clear, false, false, false);
// Delete character...
keymap [TermInfoStrings.KeyDown] = new ConsoleKeyInfo ('\0', ConsoleKey.DownArrow, false, false, false);
keymap [TermInfoStrings.KeyF1] = new ConsoleKeyInfo ('\0', ConsoleKey.F1, false, false, false);
keymap [TermInfoStrings.KeyF10] = new ConsoleKeyInfo ('\0', ConsoleKey.F10, false, false, false);
keymap [TermInfoStrings.KeyF2] = new ConsoleKeyInfo ('\0', ConsoleKey.F2, false, false, false);
keymap [TermInfoStrings.KeyF3] = new ConsoleKeyInfo ('\0', ConsoleKey.F3, false, false, false);
keymap [TermInfoStrings.KeyF4] = new ConsoleKeyInfo ('\0', ConsoleKey.F4, false, false, false);
keymap [TermInfoStrings.KeyF5] = new ConsoleKeyInfo ('\0', ConsoleKey.F5, false, false, false);
keymap [TermInfoStrings.KeyF6] = new ConsoleKeyInfo ('\0', ConsoleKey.F6, false, false, false);
keymap [TermInfoStrings.KeyF7] = new ConsoleKeyInfo ('\0', ConsoleKey.F7, false, false, false);
keymap [TermInfoStrings.KeyF8] = new ConsoleKeyInfo ('\0', ConsoleKey.F8, false, false, false);
keymap [TermInfoStrings.KeyF9] = new ConsoleKeyInfo ('\0', ConsoleKey.F9, false, false, false);
keymap [TermInfoStrings.KeyHome] = new ConsoleKeyInfo ('\0', ConsoleKey.Home, false, false, false);
keymap [TermInfoStrings.KeyLeft] = new ConsoleKeyInfo ('\0', ConsoleKey.LeftArrow, false, false, false);
keymap [TermInfoStrings.KeyLl] = new ConsoleKeyInfo ('\0', ConsoleKey.NumPad1, false, false, false);
keymap [TermInfoStrings.KeyNpage] = new ConsoleKeyInfo ('\0', ConsoleKey.PageDown, false, false, false);
keymap [TermInfoStrings.KeyPpage] = new ConsoleKeyInfo ('\0', ConsoleKey.PageUp, false, false, false);
keymap [TermInfoStrings.KeyRight] = new ConsoleKeyInfo ('\0', ConsoleKey.RightArrow, false, false, false);
keymap [TermInfoStrings.KeySf] = new ConsoleKeyInfo ('\0', ConsoleKey.PageDown, false, false, false);
keymap [TermInfoStrings.KeySr] = new ConsoleKeyInfo ('\0', ConsoleKey.PageUp, false, false, false);
keymap [TermInfoStrings.KeyUp] = new ConsoleKeyInfo ('\0', ConsoleKey.UpArrow, false, false, false);
keymap [TermInfoStrings.KeyA1] = new ConsoleKeyInfo ('\0', ConsoleKey.NumPad7, false, false, false);
keymap [TermInfoStrings.KeyA3] = new ConsoleKeyInfo ('\0', ConsoleKey.NumPad9, false, false, false);
keymap [TermInfoStrings.KeyB2] = new ConsoleKeyInfo ('\0', ConsoleKey.NumPad5, false, false, false);
keymap [TermInfoStrings.KeyC1] = new ConsoleKeyInfo ('\0', ConsoleKey.NumPad1, false, false, false);
keymap [TermInfoStrings.KeyC3] = new ConsoleKeyInfo ('\0', ConsoleKey.NumPad3, false, false, false);
keymap [TermInfoStrings.KeyBtab] = new ConsoleKeyInfo ('\0', ConsoleKey.Tab, true, false, false);
keymap [TermInfoStrings.KeyBeg] = new ConsoleKeyInfo ('\0', ConsoleKey.Home, false, false, false);
keymap [TermInfoStrings.KeyCopy] = new ConsoleKeyInfo ('C', ConsoleKey.C, false, true, false);
keymap [TermInfoStrings.KeyEnd] = new ConsoleKeyInfo ('\0', ConsoleKey.End, false, false, false);
keymap [TermInfoStrings.KeyEnter] = new ConsoleKeyInfo ('\n', ConsoleKey.Enter, false, false, false);
keymap [TermInfoStrings.KeyHelp] = new ConsoleKeyInfo ('\0', ConsoleKey.Help, false, false, false);
keymap [TermInfoStrings.KeyPrint] = new ConsoleKeyInfo ('\0', ConsoleKey.Print, false, false, false);
keymap [TermInfoStrings.KeyUndo] = new ConsoleKeyInfo ('Z', ConsoleKey.Z , false, true, false);
keymap [TermInfoStrings.KeySbeg] = new ConsoleKeyInfo ('\0', ConsoleKey.Home, true, false, false);
keymap [TermInfoStrings.KeyScopy] = new ConsoleKeyInfo ('C', ConsoleKey.C , true, true, false);
keymap [TermInfoStrings.KeySdc] = new ConsoleKeyInfo ('\x9', ConsoleKey.Delete, true, false, false);
keymap [TermInfoStrings.KeyShelp] = new ConsoleKeyInfo ('\0', ConsoleKey.Help, true, false, false);
keymap [TermInfoStrings.KeyShome] = new ConsoleKeyInfo ('\0', ConsoleKey.Home, true, false, false);
keymap [TermInfoStrings.KeySleft] = new ConsoleKeyInfo ('\0', ConsoleKey.LeftArrow, true, false, false);
keymap [TermInfoStrings.KeySprint] = new ConsoleKeyInfo ('\0', ConsoleKey.Print, true, false, false);
keymap [TermInfoStrings.KeySright] = new ConsoleKeyInfo ('\0', ConsoleKey.RightArrow, true, false, false);
keymap [TermInfoStrings.KeySundo] = new ConsoleKeyInfo ('Z', ConsoleKey.Z, true, false, false);
keymap [TermInfoStrings.KeyF11] = new ConsoleKeyInfo ('\0', ConsoleKey.F11, false, false, false);
keymap [TermInfoStrings.KeyF12] = new ConsoleKeyInfo ('\0', ConsoleKey.F12 , false, false, false);
keymap [TermInfoStrings.KeyF13] = new ConsoleKeyInfo ('\0', ConsoleKey.F13, false, false, false);
keymap [TermInfoStrings.KeyF14] = new ConsoleKeyInfo ('\0', ConsoleKey.F14, false, false, false);
keymap [TermInfoStrings.KeyF15] = new ConsoleKeyInfo ('\0', ConsoleKey.F15, false, false, false);
keymap [TermInfoStrings.KeyF16] = new ConsoleKeyInfo ('\0', ConsoleKey.F16, false, false, false);
keymap [TermInfoStrings.KeyF17] = new ConsoleKeyInfo ('\0', ConsoleKey.F17, false, false, false);
keymap [TermInfoStrings.KeyF18] = new ConsoleKeyInfo ('\0', ConsoleKey.F18, false, false, false);
keymap [TermInfoStrings.KeyF19] = new ConsoleKeyInfo ('\0', ConsoleKey.F19, false, false, false);
keymap [TermInfoStrings.KeyF20] = new ConsoleKeyInfo ('\0', ConsoleKey.F20, false, false, false);
keymap [TermInfoStrings.KeyF21] = new ConsoleKeyInfo ('\0', ConsoleKey.F21, false, false, false);
keymap [TermInfoStrings.KeyF22] = new ConsoleKeyInfo ('\0', ConsoleKey.F22, false, false, false);
keymap [TermInfoStrings.KeyF23] = new ConsoleKeyInfo ('\0', ConsoleKey.F23, false, false, false);
keymap [TermInfoStrings.KeyF24] = new ConsoleKeyInfo ('\0', ConsoleKey.F24, false, false, false);
// These were previously missing:
keymap [TermInfoStrings.KeyDc] = new ConsoleKeyInfo ('\0', ConsoleKey.Delete, false, false, false);
keymap [TermInfoStrings.KeyIc] = new ConsoleKeyInfo ('\0', ConsoleKey.Insert, false, false, false);
}
//
// The keys that we know about and use
//
static TermInfoStrings [] UsedKeys = {
TermInfoStrings.KeyBackspace,
TermInfoStrings.KeyClear,
TermInfoStrings.KeyDown,
TermInfoStrings.KeyF1,
TermInfoStrings.KeyF10,
TermInfoStrings.KeyF2,
TermInfoStrings.KeyF3,
TermInfoStrings.KeyF4,
TermInfoStrings.KeyF5,
TermInfoStrings.KeyF6,
TermInfoStrings.KeyF7,
TermInfoStrings.KeyF8,
TermInfoStrings.KeyF9,
TermInfoStrings.KeyHome,
TermInfoStrings.KeyLeft,
TermInfoStrings.KeyLl,
TermInfoStrings.KeyNpage,
TermInfoStrings.KeyPpage,
TermInfoStrings.KeyRight,
TermInfoStrings.KeySf,
TermInfoStrings.KeySr,
TermInfoStrings.KeyUp,
TermInfoStrings.KeyA1,
TermInfoStrings.KeyA3,
TermInfoStrings.KeyB2,
TermInfoStrings.KeyC1,
TermInfoStrings.KeyC3,
TermInfoStrings.KeyBtab,
TermInfoStrings.KeyBeg,
TermInfoStrings.KeyCopy,
TermInfoStrings.KeyEnd,
TermInfoStrings.KeyEnter,
TermInfoStrings.KeyHelp,
TermInfoStrings.KeyPrint,
TermInfoStrings.KeyUndo,
TermInfoStrings.KeySbeg,
TermInfoStrings.KeyScopy,
TermInfoStrings.KeySdc,
TermInfoStrings.KeyShelp,
TermInfoStrings.KeyShome,
TermInfoStrings.KeySleft,
TermInfoStrings.KeySprint,
TermInfoStrings.KeySright,
TermInfoStrings.KeySundo,
TermInfoStrings.KeyF11,
TermInfoStrings.KeyF12,
TermInfoStrings.KeyF13,
TermInfoStrings.KeyF14,
TermInfoStrings.KeyF15,
TermInfoStrings.KeyF16,
TermInfoStrings.KeyF17,
TermInfoStrings.KeyF18,
TermInfoStrings.KeyF19,
TermInfoStrings.KeyF20,
TermInfoStrings.KeyF21,
TermInfoStrings.KeyF22,
TermInfoStrings.KeyF23,
TermInfoStrings.KeyF24,
// These were missing
TermInfoStrings.KeyDc,
TermInfoStrings.KeyIc
};
void InitKeys ()
{
if (initKeys)
return;
CreateKeyMap ();
rootmap = new ByteMatcher ();
foreach (TermInfoStrings tis in UsedKeys)
AddStringMapping (tis);
rootmap.AddMapping (TermInfoStrings.KeyBackspace, new byte [] { control_characters [ControlCharacters.Erase] });
rootmap.Sort ();
initKeys = true;
}
void AddStringMapping (TermInfoStrings s)
{
byte [] bytes = reader.GetStringBytes (s);
if (bytes == null)
return;
rootmap.AddMapping (s, bytes);
}
}
class ByteMatcher {
Hashtable map = new Hashtable ();
Hashtable starts = new Hashtable ();
public void AddMapping (TermInfoStrings key, byte [] val)
{
if (val.Length == 0)
return;
map [val] = key;
starts [(int) val [0]] = true;
}
public void Sort ()
{
}
public bool StartsWith (int c)
{
return (starts [c] != null);
}
public TermInfoStrings Match (char [] buffer, int offset, int length, out int used)
{
foreach (byte [] bytes in map.Keys) {
for (int i = 0; i < bytes.Length && i < length; i++) {
if ((char) bytes [i] != buffer [offset + i])
break;
if (bytes.Length - 1 == i) {
used = bytes.Length;
return (TermInfoStrings) map [bytes];
}
}
}
used = 0;
return (TermInfoStrings) (-1);
}
}
}
#endif
|