2009-03-02 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / console-io.c
index 4d870613688bc5d55c0fb63030be0839a8d380ba..42bd547d7d869665834270f700d99f8ce6e486b5 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Gonzalo Paniagua Javier (gonzalo@ximian.com)
  *
- * Copyright (C) 2005 Novell, Inc. (http://www.novell.com)
+ * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
  */
 
 #include <config.h>
 #include <string.h>
 #include <errno.h>
 #include <signal.h>
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
 #include <sys/types.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#include <mono/metadata/appdomain.h>
+#include <mono/metadata/object-internals.h>
+#include <mono/metadata/class-internals.h>
+#include <mono/metadata/domain-internals.h>
+#include <mono/metadata/metadata.h>
+#include <mono/metadata/threadpool.h>
 /* On solaris, curses.h must come before both termios.h and term.h */
 #ifdef HAVE_CURSES_H
 #include <curses.h>
@@ -30,8 +40,8 @@
 #ifdef HAVE_SYS_FILIO_H
 #include <sys/filio.h>
 #endif
-#ifndef PLATFORM_WIN32
 #ifndef TIOCGWINSZ
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
 #endif
 
 static gboolean setup_finished;
 static gboolean atexit_called;
+
+/* The string used to return the terminal to its previous state */
 static gchar *teardown_str;
 
-#ifdef PLATFORM_WIN32
+/* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
+static gchar *keypad_xmit_str;
+
+#ifdef HAVE_TERMIOS_H
+/* This is the last state used by Mono, used after a CONT signal is received */
+static struct termios mono_attr;
+#endif
+
+#if defined (PLATFORM_WIN32) || defined (MONO_NULL_TTYDRIVER)
 MonoBoolean
 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
 {
@@ -71,7 +91,7 @@ ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
 }
 
 MonoBoolean
-ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown)
+ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
 {
        return FALSE;
 }
@@ -114,12 +134,14 @@ set_property (gint property, gboolean value)
        if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
                return FALSE;
 
+       mono_attr = attr;
        return TRUE;
 }
 
 MonoBoolean
 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
 {
+       
        return set_property (ECHO, want_echo);
 }
 
@@ -164,6 +186,27 @@ ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
        return (ret > 0) ? ret : 0;
 }
 
+static gint32 cols_and_lines;
+
+#ifdef TIOCGWINSZ
+static int
+terminal_get_dimensions (void)
+{
+       struct winsize ws;
+
+       if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
+               return (ws.ws_col << 16) | ws.ws_row;
+
+       return -1;
+}
+#else
+static int
+terminal_get_dimensions (void)
+{
+       return -1;
+}
+#endif
+
 static void
 tty_teardown (void)
 {
@@ -180,26 +223,183 @@ tty_teardown (void)
 
        tcflush (STDIN_FILENO, TCIFLUSH);
        tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
+       set_property (ECHO, TRUE);
        setup_finished = FALSE;
 }
 
-MonoBoolean
-ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown)
+static void
+do_console_cancel_event (void)
 {
-       struct termios attr;
+       static MonoClassField *cancel_handler_field;
+       MonoDomain *domain = mono_domain_get ();
+       MonoClass *klass;
+       MonoDelegate *load_value;
+       MonoMethod *method;
+       MonoMethodMessage *msg;
+       MonoMethod *im;
+
+       if (!domain->domain)
+               return;
+
+       klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
+       if (klass == NULL)
+               return;
+
+       if (cancel_handler_field == NULL) {
+               cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
+               g_assert (cancel_handler_field);
+       }
+
+       mono_field_static_get_value (mono_class_vtable (domain, klass), cancel_handler_field, &load_value);
+       if (load_value == NULL)
+               return;
+
+       klass = load_value->object.vtable->klass;
+       method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
+       g_assert (method != NULL);
+       im = mono_get_delegate_invoke (method->klass);
+       msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
+       mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
+}
+
+static gboolean in_sigint;
+static void
+sigint_handler (int signo)
+{
+       MONO_ARCH_SAVE_REGS;
+
+       if (in_sigint)
+               return;
+
+       in_sigint = TRUE;
+       do_console_cancel_event ();
+       in_sigint = FALSE;
+}
+
+static struct sigaction save_sigcont, save_sigint, save_sigwinch;
+
+static void
+sigcont_handler (int signo, void *the_siginfo, void *data)
+{
+       // Ignore error, there is not much we can do in the sigcont handler.
+       tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
+
+       if (keypad_xmit_str != NULL)
+               write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
+
+       // Call previous handler
+       if (save_sigcont.sa_sigaction != NULL &&
+           save_sigcont.sa_sigaction != (void *)SIG_DFL &&
+           save_sigcont.sa_sigaction != (void *)SIG_IGN)
+               (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
+}
+
+static void
+sigwinch_handler (int signo, void *the_siginfo, void *data)
+{
+       int dims = terminal_get_dimensions ();
+       if (dims != -1)
+               cols_and_lines = dims;
+       
+       // Call previous handler
+       if (save_sigwinch.sa_sigaction != NULL &&
+           save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
+           save_sigwinch.sa_sigaction != (void *)SIG_IGN)
+               (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
+}
+
+void
+console_set_signal_handlers ()
+{
+       struct sigaction sigcont, sigint, sigwinch;
+
+       memset (&sigcont, 0, sizeof (struct sigaction));
+       memset (&sigint, 0, sizeof (struct sigaction));
+       memset (&sigwinch, 0, sizeof (struct sigaction));
+       
+       // Continuing
+       sigcont.sa_handler = (void *) sigcont_handler;
+       sigcont.sa_flags = 0;
+       sigemptyset (&sigcont.sa_mask);
+       sigaction (SIGCONT, &sigcont, &save_sigcont);
        
+       // Interrupt handler
+       sigint.sa_handler = sigint_handler;
+       sigint.sa_flags = 0;
+       sigemptyset (&sigint.sa_mask);
+       sigaction (SIGINT, &sigint, &save_sigint);
+
+       // Window size changed
+       sigwinch.sa_handler = (void *) sigwinch_handler;
+       sigwinch.sa_flags = 0;
+       sigemptyset (&sigwinch.sa_mask);
+       sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
+}
+
+//
+// Currently unused, should we ever call the restore handler?
+// Perhaps before calling into Process.Start?
+//
+void
+console_restore_signal_handlers ()
+{
+       sigaction (SIGCONT, &save_sigcont, NULL);
+       sigaction (SIGINT, &save_sigint, NULL);
+       sigaction (SIGWINCH, &save_sigwinch, NULL);
+}
+
+MonoBoolean
+ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
+{
+       int dims;
+
        MONO_ARCH_SAVE_REGS;
 
+       dims = terminal_get_dimensions ();
+       if (dims == -1){
+               int cols = 0, rows = 0;
+                                     
+               char *str = getenv ("COLUMNS");
+               if (str != NULL)
+                       cols = atoi (str);
+               str = getenv ("LINES");
+               if (str != NULL)
+                       rows = atoi (str);
+
+               if (cols != 0 && rows != 0)
+                       cols_and_lines = (cols << 16) | rows;
+               else
+                       cols_and_lines = -1;
+       } else {
+               cols_and_lines = dims;
+       }
+       
+       *size = &cols_and_lines;
+       
+       *verase = '\0';
+       *vsusp = '\0';
+       *intr = '\0';
        if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
                return FALSE;
 
-       attr = initial_attr;
-       attr.c_lflag &= ~ICANON;
-       attr.c_cc [VMIN] = 1;
-       attr.c_cc [VTIME] = 0;
-       if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
+       mono_attr = initial_attr;
+       mono_attr.c_lflag &= ~(ICANON);
+       mono_attr.c_iflag &= ~(IXON|IXOFF);
+       mono_attr.c_cc [VMIN] = 1;
+       mono_attr.c_cc [VTIME] = 0;
+       if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
                return FALSE;
 
+       *verase = initial_attr.c_cc [VERASE];
+       *vsusp = initial_attr.c_cc [VSUSP];
+       *intr = initial_attr.c_cc [VINTR];
+       /* If initialized from another appdomain... */
+       if (setup_finished)
+               return TRUE;
+
+       keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
+       
+       console_set_signal_handlers ();
        setup_finished = TRUE;
        if (!atexit_called) {
                if (teardown != NULL)
@@ -210,5 +410,5 @@ ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown)
 
        return TRUE;
 }
-#endif
 
+#endif /* !PLATFORM_WIN32 */