2 * console-io.c: ConsoleDriver internal calls for Unix systems.
5 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 * Copyright (C) 2005-2009 Novell, Inc. (http://www.novell.com)
9 #if defined(__native_client__)
10 #include "console-null.c"
20 #ifdef HAVE_SYS_SELECT_H
21 # include <sys/select.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
26 #include <sys/types.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/object-internals.h>
32 #include <mono/metadata/class-internals.h>
33 #include <mono/metadata/domain-internals.h>
34 #include <mono/metadata/gc-internal.h>
35 #include <mono/metadata/metadata.h>
36 #include <mono/metadata/threadpool.h>
37 #include <mono/utils/mono-signal-handler.h>
38 #include <mono/utils/mono-proclib.h>
40 /* On solaris, curses.h must come before both termios.h and term.h */
51 /* Needed for FIONREAD under solaris */
52 #ifdef HAVE_SYS_FILIO_H
53 # include <sys/filio.h>
55 #ifdef HAVE_SYS_IOCTL_H
56 # include <sys/ioctl.h>
59 #include <mono/metadata/console-io.h>
60 #include <mono/metadata/exception.h>
62 static gboolean setup_finished;
63 static gboolean atexit_called;
65 /* The string used to return the terminal to its previous state */
66 static gchar *teardown_str;
68 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
69 static gchar *keypad_xmit_str;
72 /* This is the last state used by Mono, used after a CONT signal is received */
73 static struct termios mono_attr;
76 /* static void console_restore_signal_handlers (void); */
77 static void console_set_signal_handlers (void);
80 mono_console_init (void)
84 /* Make sure the standard file descriptors are opened */
85 fd = open ("/dev/null", O_RDWR);
86 while (fd >= 0 && fd < 3) {
87 fd = open ("/dev/null", O_RDWR);
92 static struct termios initial_attr;
95 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
99 return isatty (GPOINTER_TO_INT (handle));
103 set_property (gint property, gboolean value)
106 gboolean callset = FALSE;
111 if (tcgetattr (STDIN_FILENO, &attr) == -1)
114 check = (attr.c_lflag & property) != 0;
115 if ((value || check) && !(value && check)) {
118 attr.c_lflag |= property;
120 attr.c_lflag &= ~property;
126 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
134 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
137 return set_property (ECHO, want_echo);
141 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
143 return set_property (IGNBRK, !want_break);
147 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
151 struct timeval *tvptr;
159 FD_SET (STDIN_FILENO, &rfds);
161 divvy = div (timeout, 1000);
162 tv.tv_sec = divvy.quot;
163 tv.tv_usec = divvy.rem;
168 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
169 } while (ret == -1 && errno == EINTR);
173 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
178 return (ret > 0) ? ret : 0;
181 static gint32 cols_and_lines;
185 terminal_get_dimensions (void)
189 int save_errno = errno;
191 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0){
192 ret = (ws.ws_col << 16) | ws.ws_row;
200 terminal_get_dimensions (void)
216 if (teardown_str != NULL) {
217 unused = write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
218 g_free (teardown_str);
222 tcflush (STDIN_FILENO, TCIFLUSH);
223 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
224 set_property (ECHO, TRUE);
225 setup_finished = FALSE;
229 do_console_cancel_event (void)
231 static MonoClassField *cancel_handler_field;
232 MonoDomain *domain = mono_domain_get ();
234 MonoDelegate *load_value;
236 MonoMethodMessage *msg;
240 /* FIXME: this should likely iterate all the domains, instead */
244 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
248 if (cancel_handler_field == NULL) {
249 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
250 g_assert (cancel_handler_field);
253 vtable = mono_class_vtable_full (domain, klass, FALSE);
256 mono_field_static_get_value (vtable, cancel_handler_field, &load_value);
257 if (load_value == NULL)
260 klass = load_value->object.vtable->klass;
261 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
262 g_assert (method != NULL);
263 im = mono_get_delegate_invoke (method->klass);
264 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
265 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
268 static int need_cancel = FALSE;
269 /* this is executed from the finalizer thread */
271 mono_console_handle_async_ops (void)
275 do_console_cancel_event ();
279 static gboolean in_sigint;
281 MONO_SIG_HANDLER_FUNC (static, sigint_handler)
292 mono_gc_finalize_notify ();
297 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
299 MONO_SIG_HANDLER_FUNC (static, sigcont_handler)
302 // Ignore error, there is not much we can do in the sigcont handler.
303 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
305 if (keypad_xmit_str != NULL)
306 unused = write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
308 // Call previous handler
309 if (save_sigcont.sa_sigaction != NULL &&
310 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
311 save_sigcont.sa_sigaction != (void *)SIG_IGN)
312 (*save_sigcont.sa_sigaction) (MONO_SIG_HANDLER_PARAMS);
315 MONO_SIG_HANDLER_FUNC (static, sigwinch_handler)
317 int dims = terminal_get_dimensions ();
319 cols_and_lines = dims;
321 // Call previous handler
322 if (save_sigwinch.sa_sigaction != NULL &&
323 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
324 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
325 (*save_sigwinch.sa_sigaction) (MONO_SIG_HANDLER_PARAMS);
329 * console_set_signal_handlers:
331 * Installs various signals handlers for the use of the console, as
334 * SIGCONT: this is received after the application has resumed execution
335 * if it was suspended with Control-Z before. This signal handler needs
336 * to resend the terminal sequence to send keyboard in keypad mode (this
337 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
340 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
341 * a thread from the thread pool which notifies all registered cancel_event
344 * SIGWINCH: is used to track changes to the console window when a GUI
345 * terminal is resized. It sets an internal variable that is checked
346 * by System.Console when the Terminfo driver has been activated.
349 console_set_signal_handlers ()
351 struct sigaction sigcont, sigint, sigwinch;
353 memset (&sigcont, 0, sizeof (struct sigaction));
354 memset (&sigint, 0, sizeof (struct sigaction));
355 memset (&sigwinch, 0, sizeof (struct sigaction));
358 sigcont.sa_handler = (void *) sigcont_handler;
359 sigcont.sa_flags = 0;
360 sigemptyset (&sigcont.sa_mask);
361 sigaction (SIGCONT, &sigcont, &save_sigcont);
364 sigint.sa_handler = (void *) sigint_handler;
366 sigemptyset (&sigint.sa_mask);
367 sigaction (SIGINT, &sigint, &save_sigint);
369 // Window size changed
370 sigwinch.sa_handler = (void *) sigwinch_handler;
371 sigwinch.sa_flags = 0;
372 sigemptyset (&sigwinch.sa_mask);
373 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
376 #if currently_unuused
378 // Currently unused, should we ever call the restore handler?
379 // Perhaps before calling into Process.Start?
382 console_restore_signal_handlers ()
384 sigaction (SIGCONT, &save_sigcont, NULL);
385 sigaction (SIGINT, &save_sigint, NULL);
386 sigaction (SIGWINCH, &save_sigwinch, NULL);
391 set_control_chars (MonoArray *control_chars, const guchar *cc)
393 /* The index into the array comes from corlib/System/ControlCharacters.cs */
395 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
398 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
401 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
404 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
407 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
410 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
413 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
416 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
419 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
422 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
425 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
428 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
431 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
434 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
437 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
440 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
443 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
448 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
454 dims = terminal_get_dimensions ();
456 int cols = 0, rows = 0;
458 const char *str = g_getenv ("COLUMNS");
461 str = g_getenv ("LINES");
465 if (cols != 0 && rows != 0)
466 cols_and_lines = (cols << 16) | rows;
470 cols_and_lines = dims;
473 *size = &cols_and_lines;
475 /* 17 is the number of entries set in set_control_chars() above.
476 * NCCS is the total size, but, by now, we only care about those 17 values*/
477 mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) mono_array_new (mono_domain_get (), mono_defaults.byte_class, 17));
478 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
481 mono_attr = initial_attr;
482 mono_attr.c_lflag &= ~(ICANON);
483 mono_attr.c_iflag &= ~(IXON|IXOFF);
484 mono_attr.c_cc [VMIN] = 1;
485 mono_attr.c_cc [VTIME] = 0;
487 /* Disable C-y being used as a suspend character on OSX */
488 mono_attr.c_cc [VDSUSP] = 255;
490 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
493 set_control_chars (*control_chars, mono_attr.c_cc);
494 /* If initialized from another appdomain... */
498 keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
500 console_set_signal_handlers ();
501 setup_finished = TRUE;
502 if (!atexit_called) {
503 if (teardown != NULL)
504 teardown_str = mono_string_to_utf8 (teardown);
506 mono_atexit (tty_teardown);
511 #endif /* #if defined(__native_client__) */