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
|
//
// System.Console.cs
//
// Author:
// Dietmar Maurer (dietmar@ximian.com)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) Ximian, Inc. http://www.ximian.com
// (C) 2004,2005 Novell, Inc. (http://www.novell.com)
// Copyright 2013 Xamarin Inc. (http://www.xamarin.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.
//
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
namespace System
{
public static partial class Console
{
#if MONO_FEATURE_CONSOLE
private class WindowsConsole
{
public static bool ctrlHandlerAdded = false;
private delegate bool WindowsCancelHandler (int keyCode);
private static WindowsCancelHandler cancelHandler = new WindowsCancelHandler (DoWindowsConsoleCancelEvent);
[DllImport ("kernel32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern int GetConsoleCP ();
[DllImport ("kernel32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern int GetConsoleOutputCP ();
[DllImport ("kernel32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern bool SetConsoleCtrlHandler (WindowsCancelHandler handler, bool addHandler);
// Only call the event handler if Control-C was pressed (code == 0), nothing else
private static bool DoWindowsConsoleCancelEvent (int keyCode)
{
if (keyCode == 0)
DoConsoleCancelEvent ();
return keyCode == 0;
}
[MethodImpl (MethodImplOptions.NoInlining)]
public static int GetInputCodePage ()
{
return GetConsoleCP ();
}
[MethodImpl (MethodImplOptions.NoInlining)]
public static int GetOutputCodePage ()
{
return GetConsoleOutputCP ();
}
public static void AddCtrlHandler ()
{
SetConsoleCtrlHandler (cancelHandler, true);
ctrlHandlerAdded = true;
}
public static void RemoveCtrlHandler ()
{
SetConsoleCtrlHandler (cancelHandler, false);
ctrlHandlerAdded = false;
}
}
#endif
internal static TextWriter stdout;
private static TextWriter stderr;
private static TextReader stdin;
static Console ()
{
#if MONO_FEATURE_CONSOLE
if (Environment.IsRunningOnWindows) {
//
// On Windows, follow the Windows tradition
//
try {
inputEncoding = Encoding.GetEncoding (WindowsConsole.GetInputCodePage ());
outputEncoding = Encoding.GetEncoding (WindowsConsole.GetOutputCodePage ());
// ArgumentException and NotSupportedException can be thrown as well
} catch {
// FIXME: I18N assemblies are not available when compiling mcs
// Use Latin 1 as it is fast and UTF-8 is never used as console code page
inputEncoding = outputEncoding = Encoding.Default;
}
} else
#endif
{
//
// On Unix systems (128), do not output the
// UTF-8 ZWNBSP (zero-width non-breaking space).
//
int code_page = 0;
EncodingHelper.InternalCodePage (ref code_page);
if (code_page != -1 && ((code_page & 0x0fffffff) == 3 // UTF8Encoding.UTF8_CODE_PAGE
|| ((code_page & 0x10000000) != 0)))
inputEncoding = outputEncoding = EncodingHelper.UTF8Unmarked;
else
inputEncoding = outputEncoding = Encoding.Default;
}
SetupStreams (inputEncoding, outputEncoding);
}
static void SetupStreams (Encoding inputEncoding, Encoding outputEncoding)
{
#if MONO_FEATURE_CONSOLE
if (!Environment.IsRunningOnWindows && ConsoleDriver.IsConsole) {
stdin = new CStreamReader (OpenStandardInput (0), inputEncoding);
stdout = TextWriter.Synchronized (new CStreamWriter (OpenStandardOutput (0), outputEncoding, true) { AutoFlush = true });
stderr = TextWriter.Synchronized (new CStreamWriter (OpenStandardError (0), outputEncoding, true) { AutoFlush = true });
} else
#endif
{
stdin = TextReader.Synchronized (new UnexceptionalStreamReader (OpenStandardInput (0), inputEncoding));
#if MONOTOUCH
stdout = new NSLogWriter ();
stderr = new NSLogWriter ();
#else
stdout = TextWriter.Synchronized (new UnexceptionalStreamWriter (OpenStandardOutput (0), outputEncoding) { AutoFlush = true });
stderr = TextWriter.Synchronized (new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding) { AutoFlush = true });
#if MONODROID && !MOBILE_DESKTOP_HOST
if (LogcatTextWriter.IsRunningOnAndroid ()) {
stdout = TextWriter.Synchronized (new LogcatTextWriter ("mono-stdout", stdout));
stderr = TextWriter.Synchronized (new LogcatTextWriter ("mono-stderr", stderr));
}
#endif // MONODROID
#endif // MONOTOUCH
}
GC.SuppressFinalize (stdout);
GC.SuppressFinalize (stderr);
GC.SuppressFinalize (stdin);
}
public static TextWriter Error {
get {
return stderr;
}
}
public static TextWriter Out {
get {
return stdout;
}
}
public static TextReader In {
get {
return stdin;
}
}
private static Stream Open (IntPtr handle, FileAccess access, int bufferSize)
{
try {
// TODO: Should use __ConsoleStream from reference sources
var stream = new FileStream (handle, access, false, bufferSize, false, true);
// Don't run the finalizer on the underlying stream so that System.WriteLine can be
// called inside a finalizer during shutdown or domain unload.
GC.SuppressFinalize (stream);
return stream;
} catch (IOException) {
return Stream.Null;
}
}
public static Stream OpenStandardError ()
{
return OpenStandardError (0);
}
// calling any FileStream constructor with a handle normally
// requires permissions UnmanagedCode permissions. In this
// case we assert this permission so the console can be used
// in partial trust (i.e. without having UnmanagedCode).
[SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
public static Stream OpenStandardError (int bufferSize)
{
return Open (MonoIO.ConsoleError, FileAccess.Write, bufferSize);
}
public static Stream OpenStandardInput ()
{
return OpenStandardInput (0);
}
// calling any FileStream constructor with a handle normally
// requires permissions UnmanagedCode permissions. In this
// case we assert this permission so the console can be used
// in partial trust (i.e. without having UnmanagedCode).
[SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
public static Stream OpenStandardInput (int bufferSize)
{
return Open (MonoIO.ConsoleInput, FileAccess.Read, bufferSize);
}
public static Stream OpenStandardOutput ()
{
return OpenStandardOutput (0);
}
// calling any FileStream constructor with a handle normally
// requires permissions UnmanagedCode permissions. In this
// case we assert this permission so the console can be used
// in partial trust (i.e. without having UnmanagedCode).
[SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
public static Stream OpenStandardOutput (int bufferSize)
{
return Open (MonoIO.ConsoleOutput, FileAccess.Write, bufferSize);
}
[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
public static void SetError (TextWriter newError)
{
if (newError == null)
throw new ArgumentNullException ("newError");
stderr = TextWriter.Synchronized (newError);
}
[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
public static void SetIn (TextReader newIn)
{
if (newIn == null)
throw new ArgumentNullException ("newIn");
stdin = TextReader.Synchronized (newIn);
}
[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
public static void SetOut (TextWriter newOut)
{
if (newOut == null)
throw new ArgumentNullException ("newOut");
stdout = TextWriter.Synchronized (newOut);
}
public static void Write (bool value)
{
stdout.Write (value);
}
public static void Write (char value)
{
stdout.Write (value);
}
public static void Write (char[] buffer)
{
stdout.Write (buffer);
}
public static void Write (decimal value)
{
stdout.Write (value);
}
public static void Write (double value)
{
stdout.Write (value);
}
public static void Write (int value)
{
stdout.Write (value);
}
public static void Write (long value)
{
stdout.Write (value);
}
public static void Write (object value)
{
stdout.Write (value);
}
public static void Write (float value)
{
stdout.Write (value);
}
public static void Write (string value)
{
stdout.Write (value);
}
[CLSCompliant (false)]
public static void Write (uint value)
{
stdout.Write (value);
}
[CLSCompliant (false)]
public static void Write (ulong value)
{
stdout.Write (value);
}
public static void Write (string format, object arg0)
{
stdout.Write (format, arg0);
}
public static void Write (string format, params object[] arg)
{
if (arg == null)
stdout.Write (format);
else
stdout.Write (format, arg);
}
public static void Write (char[] buffer, int index, int count)
{
stdout.Write (buffer, index, count);
}
public static void Write (string format, object arg0, object arg1)
{
stdout.Write (format, arg0, arg1);
}
public static void Write (string format, object arg0, object arg1, object arg2 )
{
stdout.Write (format, arg0, arg1, arg2);
}
[CLSCompliant (false)]
public static void Write (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
{
ArgIterator iter = new ArgIterator (__arglist);
int argCount = iter.GetRemainingCount();
object[] args = new object [argCount + 4];
args [0] = arg0;
args [1] = arg1;
args [2] = arg2;
args [3] = arg3;
for (int i = 0; i < argCount; i++) {
TypedReference typedRef = iter.GetNextArg ();
args [i + 4] = TypedReference.ToObject (typedRef);
}
stdout.Write (String.Format (format, args));
}
public static void WriteLine ()
{
stdout.WriteLine ();
}
public static void WriteLine (bool value)
{
stdout.WriteLine (value);
}
public static void WriteLine (char value)
{
stdout.WriteLine (value);
}
public static void WriteLine (char[] buffer)
{
stdout.WriteLine (buffer);
}
public static void WriteLine (decimal value)
{
stdout.WriteLine (value);
}
public static void WriteLine (double value)
{
stdout.WriteLine (value);
}
public static void WriteLine (int value)
{
stdout.WriteLine (value);
}
public static void WriteLine (long value)
{
stdout.WriteLine (value);
}
public static void WriteLine (object value)
{
stdout.WriteLine (value);
}
public static void WriteLine (float value)
{
stdout.WriteLine (value);
}
public static void WriteLine (string value)
{
stdout.WriteLine (value);
}
[CLSCompliant (false)]
public static void WriteLine (uint value)
{
stdout.WriteLine (value);
}
[CLSCompliant (false)]
public static void WriteLine (ulong value)
{
stdout.WriteLine (value);
}
public static void WriteLine (string format, object arg0)
{
stdout.WriteLine (format, arg0);
}
public static void WriteLine (string format, params object[] arg)
{
if (arg == null)
stdout.WriteLine (format);
else
stdout.WriteLine (format, arg);
}
public static void WriteLine (char[] buffer, int index, int count)
{
stdout.WriteLine (buffer, index, count);
}
public static void WriteLine (string format, object arg0, object arg1)
{
stdout.WriteLine (format, arg0, arg1);
}
public static void WriteLine (string format, object arg0, object arg1, object arg2)
{
stdout.WriteLine (format, arg0, arg1, arg2);
}
[CLSCompliant (false)]
public static void WriteLine (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
{
ArgIterator iter = new ArgIterator (__arglist);
int argCount = iter.GetRemainingCount();
object[] args = new object [argCount + 4];
args [0] = arg0;
args [1] = arg1;
args [2] = arg2;
args [3] = arg3;
for (int i = 0; i < argCount; i++) {
TypedReference typedRef = iter.GetNextArg ();
args [i + 4] = TypedReference.ToObject (typedRef);
}
stdout.WriteLine (String.Format (format, args));
}
#if MONO_FEATURE_CONSOLE
public static int Read ()
{
if ((stdin is CStreamReader) && ConsoleDriver.IsConsole) {
return ConsoleDriver.Read ();
} else {
return stdin.Read ();
}
}
public static string ReadLine ()
{
if ((stdin is CStreamReader) && ConsoleDriver.IsConsole) {
return ConsoleDriver.ReadLine ();
} else {
return stdin.ReadLine ();
}
}
#else
public static int Read ()
{
return stdin.Read ();
}
public static string ReadLine ()
{
return stdin.ReadLine ();
}
#endif
// FIXME: Console should use these encodings when changed
static Encoding inputEncoding;
static Encoding outputEncoding;
public static Encoding InputEncoding {
get { return inputEncoding; }
set {
inputEncoding = value;
SetupStreams (inputEncoding, outputEncoding);
}
}
public static Encoding OutputEncoding {
get { return outputEncoding; }
set {
outputEncoding = value;
SetupStreams (inputEncoding, outputEncoding);
}
}
#if MONO_FEATURE_CONSOLE
public static ConsoleColor BackgroundColor {
get { return ConsoleDriver.BackgroundColor; }
set { ConsoleDriver.BackgroundColor = value; }
}
public static int BufferHeight {
get { return ConsoleDriver.BufferHeight; }
[MonoLimitation ("Works only on Windows, or with some Xterm-based terminals")]
set { ConsoleDriver.BufferHeight = value; }
}
public static int BufferWidth {
get { return ConsoleDriver.BufferWidth; }
[MonoLimitation ("Works only on Windows, or with some Xterm-based terminals")]
set { ConsoleDriver.BufferWidth = value; }
}
[MonoLimitation ("Implemented only on Windows")]
public static bool CapsLock {
get { return ConsoleDriver.CapsLock; }
}
public static int CursorLeft {
get { return ConsoleDriver.CursorLeft; }
set { ConsoleDriver.CursorLeft = value; }
}
public static int CursorTop {
get { return ConsoleDriver.CursorTop; }
set { ConsoleDriver.CursorTop = value; }
}
public static int CursorSize {
get { return ConsoleDriver.CursorSize; }
set { ConsoleDriver.CursorSize = value; }
}
public static bool CursorVisible {
get { return ConsoleDriver.CursorVisible; }
set { ConsoleDriver.CursorVisible = value; }
}
public static ConsoleColor ForegroundColor {
get { return ConsoleDriver.ForegroundColor; }
set { ConsoleDriver.ForegroundColor = value; }
}
public static bool KeyAvailable {
get { return ConsoleDriver.KeyAvailable; }
}
public static int LargestWindowHeight {
get { return ConsoleDriver.LargestWindowHeight; }
}
public static int LargestWindowWidth {
get { return ConsoleDriver.LargestWindowWidth; }
}
public static bool NumberLock {
get { return ConsoleDriver.NumberLock; }
}
public static string Title {
get { return ConsoleDriver.Title; }
set { ConsoleDriver.Title = value; }
}
public static bool TreatControlCAsInput {
get { return ConsoleDriver.TreatControlCAsInput; }
set { ConsoleDriver.TreatControlCAsInput = value; }
}
public static int WindowHeight {
get { return ConsoleDriver.WindowHeight; }
[MonoLimitation ("Works only on Windows, or with some Xterm-based terminals")]
set { ConsoleDriver.WindowHeight = value; }
}
public static int WindowLeft {
get { return ConsoleDriver.WindowLeft; }
[MonoLimitation ("Works only on Windows")]
set { ConsoleDriver.WindowLeft = value; }
}
public static int WindowTop {
get { return ConsoleDriver.WindowTop; }
[MonoLimitation ("Works only on Windows")]
set { ConsoleDriver.WindowTop = value; }
}
public static int WindowWidth {
get { return ConsoleDriver.WindowWidth; }
[MonoLimitation ("Works only on Windows, or with some Xterm-based terminals")]
set { ConsoleDriver.WindowWidth = value; }
}
public static bool IsErrorRedirected {
get {
return ConsoleDriver.IsErrorRedirected;
}
}
public static bool IsOutputRedirected {
get {
return ConsoleDriver.IsOutputRedirected;
}
}
public static bool IsInputRedirected {
get {
return ConsoleDriver.IsInputRedirected;
}
}
public static void Beep ()
{
Beep (1000, 500);
}
public static void Beep (int frequency, int duration)
{
if (frequency < 37 || frequency > 32767)
throw new ArgumentOutOfRangeException ("frequency");
if (duration <= 0)
throw new ArgumentOutOfRangeException ("duration");
ConsoleDriver.Beep (frequency, duration);
}
public static void Clear ()
{
ConsoleDriver.Clear ();
}
[MonoLimitation ("Implemented only on Windows")]
public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
int targetLeft, int targetTop)
{
ConsoleDriver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight, targetLeft, targetTop);
}
[MonoLimitation ("Implemented only on Windows")]
public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
int targetLeft, int targetTop, Char sourceChar,
ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
{
ConsoleDriver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight, targetLeft, targetTop,
sourceChar, sourceForeColor, sourceBackColor);
}
public static ConsoleKeyInfo ReadKey ()
{
return ReadKey (false);
}
public static ConsoleKeyInfo ReadKey (bool intercept)
{
return ConsoleDriver.ReadKey (intercept);
}
public static void ResetColor ()
{
ConsoleDriver.ResetColor ();
}
[MonoLimitation ("Works only on Windows, or with some Xterm-based terminals")]
public static void SetBufferSize (int width, int height)
{
ConsoleDriver.SetBufferSize (width, height);
}
public static void SetCursorPosition (int left, int top)
{
ConsoleDriver.SetCursorPosition (left, top);
}
[MonoLimitation ("Works only on Windows")]
public static void SetWindowPosition (int left, int top)
{
ConsoleDriver.SetWindowPosition (left, top);
}
[MonoLimitation ("Works only on Windows, or with some Xterm-based terminals")]
public static void SetWindowSize (int width, int height)
{
ConsoleDriver.SetWindowSize (width, height);
}
static ConsoleCancelEventHandler cancel_event;
public static event ConsoleCancelEventHandler CancelKeyPress {
add {
if (ConsoleDriver.Initialized == false)
ConsoleDriver.Init ();
cancel_event += value;
if (Environment.IsRunningOnWindows && !WindowsConsole.ctrlHandlerAdded)
WindowsConsole.AddCtrlHandler();
}
remove {
if (ConsoleDriver.Initialized == false)
ConsoleDriver.Init ();
cancel_event -= value;
if (cancel_event == null && Environment.IsRunningOnWindows)
{
// Need to remove our hook if there's nothing left in the event
if (WindowsConsole.ctrlHandlerAdded)
WindowsConsole.RemoveCtrlHandler();
}
}
}
static void DoConsoleCancelEventInBackground ()
{
ThreadPool.UnsafeQueueUserWorkItem (_ => DoConsoleCancelEvent(), null);
}
static void DoConsoleCancelEvent ()
{
bool exit = true;
if (cancel_event != null) {
ConsoleCancelEventArgs args = new ConsoleCancelEventArgs (ConsoleSpecialKey.ControlC);
Delegate [] delegates = cancel_event.GetInvocationList ();
foreach (ConsoleCancelEventHandler d in delegates){
try {
// Sender is always null here.
d (null, args);
} catch {} // Ignore any exception.
}
exit = !args.Cancel;
}
if (exit)
Environment.Exit (58);
}
#else
// largely inspired by https://github.com/dotnet/corefx/blob/be8d2ce3964968cec9322a64211e37682085db70/src/System.Console/src/System/ConsolePal.WinRT.cs, because it's a similar platform where a console might not be available
// provide simply color tracking that allows round-tripping
internal const ConsoleColor UnknownColor = (ConsoleColor)(-1);
private static ConsoleColor s_trackedForegroundColor = UnknownColor;
private static ConsoleColor s_trackedBackgroundColor = UnknownColor;
public static ConsoleColor ForegroundColor
{
get
{
return s_trackedForegroundColor;
}
set
{
lock (Console.Out) // synchronize with other writers
{
s_trackedForegroundColor = value;
}
}
}
public static ConsoleColor BackgroundColor
{
get
{
return s_trackedBackgroundColor;
}
set
{
lock (Console.Out) // synchronize with other writers
{
s_trackedBackgroundColor = value;
}
}
}
public static int BufferWidth
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static int BufferHeight
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static bool CapsLock { get { throw new PlatformNotSupportedException (); } }
public static int CursorLeft
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static int CursorTop
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static int CursorSize
{
get { return 100; }
set { throw new PlatformNotSupportedException(); }
}
public static bool CursorVisible
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static bool KeyAvailable { get { throw new PlatformNotSupportedException (); } }
public static int LargestWindowWidth
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static int LargestWindowHeight
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static bool NumberLock { get { throw new PlatformNotSupportedException (); } }
public static string Title
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static bool TreatControlCAsInput
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static int WindowHeight
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static int WindowLeft
{
get { return 0; }
set { throw new PlatformNotSupportedException (); }
}
public static int WindowTop
{
get { return 0; }
set { throw new PlatformNotSupportedException (); }
}
public static int WindowWidth
{
get { throw new PlatformNotSupportedException (); }
set { throw new PlatformNotSupportedException (); }
}
public static bool IsErrorRedirected { get { throw new PlatformNotSupportedException (); } }
public static bool IsInputRedirected { get { throw new PlatformNotSupportedException (); } }
public static bool IsOutputRedirected { get { throw new PlatformNotSupportedException (); } }
public static void Beep () { throw new PlatformNotSupportedException (); }
public static void Beep (int frequency, int duration) { throw new PlatformNotSupportedException (); }
public static void Clear () { throw new PlatformNotSupportedException (); }
public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop) { throw new PlatformNotSupportedException(); }
public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop, char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor) { throw new PlatformNotSupportedException(); }
public static ConsoleKeyInfo ReadKey ()
{
return ReadKey (false);
}
public static ConsoleKeyInfo ReadKey (bool intercept) { throw new PlatformNotSupportedException (); }
public static void ResetColor ()
{
lock (Console.Out) // synchronize with other writers
{
s_trackedForegroundColor = UnknownColor;
s_trackedBackgroundColor = UnknownColor;
}
}
public static void SetBufferSize (int width, int height) { throw new PlatformNotSupportedException (); }
public static void SetCursorPosition (int left, int top) { throw new PlatformNotSupportedException (); }
public static void SetWindowPosition (int left, int top) { throw new PlatformNotSupportedException (); }
public static void SetWindowSize (int width, int height) { throw new PlatformNotSupportedException (); }
public static event ConsoleCancelEventHandler CancelKeyPress {
add {
throw new PlatformNotSupportedException ();
}
remove {
throw new PlatformNotSupportedException ();
}
}
#endif
}
}
|