Merge pull request #656 from LogosBible/collection_lock
[mono.git] / mcs / class / corlib / System / Console.cs
index ea9365e689fae20a2f144e5a859ac854217e1d35..08f8a4c1da1bfe92ecd879883d42b60b86374e1e 100644 (file)
@@ -7,7 +7,7 @@
 //
 // (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
@@ -39,16 +39,31 @@ using System.Text;
 
 namespace System
 {
-       public static class Console
+       public static partial class Console
        {
 #if !NET_2_1
                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 ()
                        {
@@ -60,8 +75,21 @@ namespace System
                        {
                                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;
@@ -106,27 +134,56 @@ namespace System
                                        inputEncoding = outputEncoding = Encoding.Default;
                        }
 
-                       stderr = new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding); 
-                       ((StreamWriter)stderr).AutoFlush = true;
-                       stderr = TextWriter.Synchronized (stderr, true);
+                       SetupStreams (inputEncoding, outputEncoding);
+               }
 
+               static void SetupStreams (Encoding inputEncoding, Encoding outputEncoding)
+               {
 #if !NET_2_1
                        if (!Environment.IsRunningOnWindows && ConsoleDriver.IsConsole) {
                                StreamWriter w = new CStreamWriter (OpenStandardOutput (0), outputEncoding);
                                w.AutoFlush = true;
                                stdout = TextWriter.Synchronized (w, true);
+
+                               w = new CStreamWriter (OpenStandardOutput (0), outputEncoding);
+                               w.AutoFlush = true;
+                               stderr = TextWriter.Synchronized (w, true);
+                               
                                stdin = new CStreamReader (OpenStandardInput (0), inputEncoding);
                        } else {
 #endif
+// FULL_AOT_RUNTIME is used (instead of MONOTOUCH) since we only want this code when running on 
+// iOS (simulator or devices) and *not* when running tools (e.g. btouch #12179) that needs to use 
+// the mscorlib.dll shipped with Xamarin.iOS
+#if MONOTOUCH && FULL_AOT_RUNTIME
+                               stdout = new NSLogWriter ();
+#else
                                stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), outputEncoding);
                                ((StreamWriter)stdout).AutoFlush = true;
+#endif
                                stdout = TextWriter.Synchronized (stdout, true);
+
+#if MONOTOUCH && FULL_AOT_RUNTIME
+                               stderr = new NSLogWriter ();
+#else
+                               stderr = new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding); 
+                               ((StreamWriter)stderr).AutoFlush = true;
+#endif
+                               stderr = TextWriter.Synchronized (stderr, true);
+
                                stdin = new UnexceptionalStreamReader (OpenStandardInput (0), inputEncoding);
                                stdin = TextReader.Synchronized (stdin);
 #if !NET_2_1
                        }
 #endif
 
+#if MONODROID
+                       if (LogcatTextWriter.IsRunningOnAndroid ()) {
+                               stdout = TextWriter.Synchronized (new LogcatTextWriter ("mono-stdout", stdout));
+                               stderr = TextWriter.Synchronized (new LogcatTextWriter ("mono-stderr", stderr));
+                       }
+#endif  // MONODROID
+
                        GC.SuppressFinalize (stdout);
                        GC.SuppressFinalize (stderr);
                        GC.SuppressFinalize (stdin);
@@ -152,10 +209,6 @@ namespace System
 
                private static Stream Open (IntPtr handle, FileAccess access, int bufferSize)
                {
-#if MOONLIGHT
-                       if (SecurityManager.SecurityEnabled && !Debugger.IsAttached && Environment.GetEnvironmentVariable ("MOONLIGHT_ENABLE_CONSOLE") == null)
-                               return new NullStream ();
-#endif
                        try {
                                return new FileStream (handle, access, false, bufferSize, false, bufferSize == 0);
                        } catch (IOException) {
@@ -304,7 +357,10 @@ namespace System
 
                public static void Write (string format, params object[] arg)
                {
-                       stdout.Write (format, arg);
+                       if (arg == null)
+                               stdout.Write (format);
+                       else
+                               stdout.Write (format, arg);
                }
 
                public static void Write (char[] buffer, int index, int count)
@@ -415,7 +471,10 @@ namespace System
 
                public static void WriteLine (string format, params object[] arg)
                {
-                       stdout.WriteLine (format, arg);
+                       if (arg == null)
+                               stdout.WriteLine (format);
+                       else
+                               stdout.WriteLine (format, arg);
                }
 
                public static void WriteLine (char[] buffer, int index, int count)
@@ -490,12 +549,18 @@ namespace System
 
                public static Encoding InputEncoding {
                        get { return inputEncoding; }
-                       set { inputEncoding = value; }
+                       set {
+                               inputEncoding = value;
+                               SetupStreams (inputEncoding, outputEncoding);
+                       }
                }
 
                public static Encoding OutputEncoding {
                        get { return outputEncoding; }
-                       set { outputEncoding = value; }
+                       set {
+                               outputEncoding = value;
+                               SetupStreams (inputEncoding, outputEncoding);
+                       }
                }
 
                public static ConsoleColor BackgroundColor {
@@ -596,6 +661,26 @@ namespace System
                        set { ConsoleDriver.WindowWidth = value; }
                }
 
+#if NET_4_5
+               public static bool IsErrorRedirected {
+                       get {
+                               return ConsoleDriver.IsErrorRedirected;
+                       }
+               }
+
+               public static bool IsOutputRedirected {
+                       get {
+                               return ConsoleDriver.IsOutputRedirected;
+                       }
+               }
+
+               public static bool IsInputRedirected {
+                       get {
+                               return ConsoleDriver.IsInputRedirected;
+                       }
+               }
+#endif
+
                public static void Beep ()
                {
                        Beep (1000, 500);
@@ -676,12 +761,22 @@ namespace System
                                        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();
+                               }
                        }
                }