Merge pull request #2216 from akoeplinger/fix-array-sort
[mono.git] / mcs / class / corlib / System / ConsoleDriver.cs
index 039ae8926cf82af3d291bb2bfc194008b8dc272d..cf1faa8da1028708951c04e175ad55a97b9343e2 100644 (file)
 // 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
+#if !NET_2_1
 using System.IO;
 using System.Runtime.CompilerServices;
 
 namespace System {
-       class ConsoleDriver {
-               static IConsoleDriver driver;
+       static class ConsoleDriver {
+               internal static IConsoleDriver driver;
+               static bool is_console;
+               static bool called_isatty;
 
                static ConsoleDriver ()
                {
-                       if (Environment.IsRunningOnWindows) {
-                               driver = new WindowsConsoleDriver ();
+                       // Put the actual new statements into separate methods to avoid initalizing
+                       // three classes when only one is needed.
+                       if (!IsConsole) {
+                               driver = CreateNullConsoleDriver ();
+                       } else if (Environment.IsRunningOnWindows) {
+                               driver = CreateWindowsConsoleDriver ();
                        } else {
-                               driver = new TermInfoDriver (Environment.GetEnvironmentVariable ("TERM"));
+                               string term = Environment.GetEnvironmentVariable ("TERM");
+
+                               // Perhaps we should let the Terminfo driver return a
+                               // success/failure flag based on the terminal properties
+                               if (term == "dumb"){
+                                       is_console = false;
+                                       driver = CreateNullConsoleDriver ();
+                               } else
+                                       driver = CreateTermInfoDriver (term);
                        }
                }
 
-               public static bool Echo {
-                       get { return driver.Echo; }
-                       set { driver.Echo = value; }
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               static IConsoleDriver CreateNullConsoleDriver () {
+                       return new NullConsoleDriver ();
                }
 
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               static IConsoleDriver CreateWindowsConsoleDriver () {
+                       return new WindowsConsoleDriver ();
+               }
+
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               static IConsoleDriver CreateTermInfoDriver (string term) {
+                       return new TermInfoDriver (term);
+               }
+               
                public static bool Initialized {
                        get { return driver.Initialized; }
                }
@@ -153,6 +177,24 @@ namespace System {
                        set { driver.WindowWidth = value; }
                }
 
+               public static bool IsErrorRedirected {
+                       get {
+                               return !Isatty (MonoIO.ConsoleError);
+                       }
+               }
+
+               public static bool IsOutputRedirected {
+                       get {
+                               return !Isatty (MonoIO.ConsoleOutput);
+                       }
+               }
+
+               public static bool IsInputRedirected {
+                       get {
+                               return !Isatty (MonoIO.ConsoleInput);
+                       }
+               }
+
                public static void Beep (int frequency, int duration)
                {
                        driver.Beep (frequency, duration);
@@ -178,6 +220,21 @@ namespace System {
                                        targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
                }
 
+               public static void Init ()
+               {
+                       driver.Init ();
+               }
+
+               public static int Read ()
+               {
+                       return ReadKey (false).KeyChar;
+               }
+
+               public static string ReadLine ()
+               {
+                       return driver.ReadLine ();
+               }
+
                public static ConsoleKeyInfo ReadKey (bool intercept)
                {
                        return driver.ReadKey (intercept);
@@ -208,14 +265,25 @@ namespace System {
                        driver.SetWindowSize (width, height);
                }
 
+               public static bool IsConsole {
+                       get {
+                               if (called_isatty)
+                                       return is_console;
+
+                               is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
+                               called_isatty = true;
+                               return is_console;
+                       }
+               }
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal static extern bool Isatty (IntPtr handle);
+               static extern bool Isatty (IntPtr handle);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                internal static extern int InternalKeyAvailable (int ms_timeout);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal static extern bool TtySetup (string teardown);
+               unsafe internal static extern bool TtySetup (string keypadXmit, string teardown, out byte [] control_characters, out int *address);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                internal static extern bool SetEcho (bool wantEcho);