Merge pull request #2216 from akoeplinger/fix-array-sort
[mono.git] / mcs / class / corlib / System / ConsoleDriver.cs
index 13cc0545fc5b424be98a71cd1b67acad742e26e6..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 class ConsoleDriver {
                internal static IConsoleDriver driver;
                static bool is_console;
                static bool called_isatty;
 
                static ConsoleDriver ()
                {
+                       // Put the actual new statements into separate methods to avoid initalizing
+                       // three classes when only one is needed.
                        if (!IsConsole) {
-                               driver = new NullConsoleDriver ();
+                               driver = CreateNullConsoleDriver ();
                        } else if (Environment.IsRunningOnWindows) {
-                               driver = new WindowsConsoleDriver ();
+                               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);
                        }
                }
 
+               [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; }
                }
@@ -152,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);
@@ -240,19 +283,13 @@ namespace System {
                internal static extern int InternalKeyAvailable (int ms_timeout);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal static extern bool TtySetup (string teardown, out byte verase, out byte vsusp, out byte intr);
+               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);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                internal static extern bool SetBreak (bool wantBreak);
-
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal static extern bool GetTtySize (IntPtr handle, out int width, out int height);
-
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal static extern void Suspend ();
        }
 }
 #endif