3 * ConsoleDriver internal calls for Unix systems.
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright (C) 2005-2009 Novell, Inc. (http://www.novell.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #if defined(__native_client__)
12 #include "console-null.c"
22 #ifdef HAVE_SYS_SELECT_H
23 # include <sys/select.h>
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
28 #include <sys/types.h>
32 #include <mono/metadata/appdomain.h>
33 #include <mono/metadata/object-internals.h>
34 #include <mono/metadata/class-internals.h>
35 #include <mono/metadata/domain-internals.h>
36 #include <mono/metadata/gc-internals.h>
37 #include <mono/metadata/metadata.h>
38 #include <mono/metadata/threadpool.h>
39 #include <mono/utils/mono-signal-handler.h>
40 #include <mono/utils/mono-proclib.h>
41 #include <mono/utils/w32api.h>
43 /* On solaris, curses.h must come before both termios.h and term.h */
54 /* Needed for FIONREAD under solaris */
55 #ifdef HAVE_SYS_FILIO_H
56 # include <sys/filio.h>
58 #ifdef HAVE_SYS_IOCTL_H
59 # include <sys/ioctl.h>
62 #include <mono/metadata/console-io.h>
63 #include <mono/metadata/exception.h>
65 static gboolean setup_finished;
66 static gboolean atexit_called;
68 /* The string used to return the terminal to its previous state */
69 static gchar *teardown_str;
71 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
72 static gchar *keypad_xmit_str;
75 /* This is the last state used by Mono, used after a CONT signal is received */
76 static struct termios mono_attr;
79 /* static void console_restore_signal_handlers (void); */
80 static void console_set_signal_handlers (void);
83 mono_console_init (void)
87 /* Make sure the standard file descriptors are opened */
88 fd = open ("/dev/null", O_RDWR);
89 while (fd >= 0 && fd < 3) {
90 fd = open ("/dev/null", O_RDWR);
95 static struct termios initial_attr;
98 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
100 return isatty (GPOINTER_TO_INT (handle));
104 set_property (gint property, gboolean value)
107 gboolean callset = FALSE;
110 if (tcgetattr (STDIN_FILENO, &attr) == -1)
113 check = (attr.c_lflag & property) != 0;
114 if ((value || check) && !(value && check)) {
117 attr.c_lflag |= property;
119 attr.c_lflag &= ~property;
125 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
133 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
136 return set_property (ECHO, want_echo);
140 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
142 return set_property (IGNBRK, !want_break);
146 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
150 struct timeval *tvptr;
156 FD_SET (STDIN_FILENO, &rfds);
158 divvy = div (timeout, 1000);
159 tv.tv_sec = divvy.quot;
160 tv.tv_usec = divvy.rem;
165 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
166 } while (ret == -1 && errno == EINTR);
170 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
175 return (ret > 0) ? ret : 0;
178 static gint32 cols_and_lines;
182 terminal_get_dimensions (void)
186 int save_errno = errno;
188 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0){
189 ret = (ws.ws_col << 16) | ws.ws_row;
197 terminal_get_dimensions (void)
206 int unused G_GNUC_UNUSED;
211 if (teardown_str != NULL) {
212 unused = write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
213 g_free (teardown_str);
217 tcflush (STDIN_FILENO, TCIFLUSH);
218 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
219 set_property (ECHO, TRUE);
220 setup_finished = FALSE;
224 do_console_cancel_event (void)
226 static MonoClassField *cancel_handler_field;
228 MonoDomain *domain = mono_domain_get ();
230 MonoDelegate *load_value;
234 /* FIXME: this should likely iterate all the domains, instead */
238 klass = mono_class_try_load_from_name (mono_defaults.corlib, "System", "Console");
242 if (cancel_handler_field == NULL) {
243 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
244 g_assert (cancel_handler_field);
247 vtable = mono_class_vtable_full (domain, klass, &error);
248 if (vtable == NULL || !is_ok (&error)) {
249 mono_error_cleanup (&error);
252 mono_field_static_get_value_checked (vtable, cancel_handler_field, &load_value, &error);
253 if (load_value == NULL || !is_ok (&error)) {
254 mono_error_cleanup (&error);
258 klass = load_value->object.vtable->klass;
259 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
260 g_assert (method != NULL);
262 mono_threadpool_begin_invoke (domain, (MonoObject*) load_value, method, NULL, &error);
263 if (!is_ok (&error)) {
264 g_warning ("Couldn't invoke System.Console cancel handler due to %s", mono_error_get_message (&error));
265 mono_error_cleanup (&error);
269 static int need_cancel = FALSE;
270 /* this is executed from the finalizer thread */
272 mono_console_handle_async_ops (void)
276 do_console_cancel_event ();
280 static gboolean in_sigint;
282 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)
301 int unused G_GNUC_UNUSED;
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 #if defined(HAVE_SIGACTION)
352 struct sigaction sigcont, sigint, sigwinch;
354 memset (&sigcont, 0, sizeof (struct sigaction));
355 memset (&sigint, 0, sizeof (struct sigaction));
356 memset (&sigwinch, 0, sizeof (struct sigaction));
359 sigcont.sa_handler = (void (*)(int)) sigcont_handler;
360 sigcont.sa_flags = SA_RESTART;
361 sigemptyset (&sigcont.sa_mask);
362 sigaction (SIGCONT, &sigcont, &save_sigcont);
365 sigint.sa_handler = (void (*)(int)) sigint_handler;
366 sigint.sa_flags = SA_RESTART;
367 sigemptyset (&sigint.sa_mask);
368 sigaction (SIGINT, &sigint, &save_sigint);
370 // Window size changed
371 sigwinch.sa_handler = (void (*)(int)) sigwinch_handler;
372 sigwinch.sa_flags = SA_RESTART;
373 sigemptyset (&sigwinch.sa_mask);
374 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
378 #if currently_unuused
380 // Currently unused, should we ever call the restore handler?
381 // Perhaps before calling into Process.Start?
384 console_restore_signal_handlers ()
386 sigaction (SIGCONT, &save_sigcont, NULL);
387 sigaction (SIGINT, &save_sigint, NULL);
388 sigaction (SIGWINCH, &save_sigwinch, NULL);
393 set_control_chars (MonoArray *control_chars, const guchar *cc)
395 /* The index into the array comes from corlib/System/ControlCharacters.cs */
397 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
400 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
403 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
406 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
409 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
412 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
415 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
418 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
421 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
424 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
427 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
430 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
433 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
436 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
439 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
442 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
445 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
450 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
456 dims = terminal_get_dimensions ();
458 int cols = 0, rows = 0;
460 char *str = g_getenv ("COLUMNS");
465 str = g_getenv ("LINES");
471 if (cols != 0 && rows != 0)
472 cols_and_lines = (cols << 16) | rows;
476 cols_and_lines = dims;
479 *size = &cols_and_lines;
481 /* 17 is the number of entries set in set_control_chars() above.
482 * NCCS is the total size, but, by now, we only care about those 17 values*/
483 MonoArray *control_chars_arr = mono_array_new_checked (mono_domain_get (), mono_defaults.byte_class, 17, &error);
484 if (mono_error_set_pending_exception (&error))
486 mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) control_chars_arr);
487 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
490 mono_attr = initial_attr;
491 mono_attr.c_lflag &= ~(ICANON);
492 mono_attr.c_iflag &= ~(IXON|IXOFF);
493 mono_attr.c_cc [VMIN] = 1;
494 mono_attr.c_cc [VTIME] = 0;
496 /* Disable C-y being used as a suspend character on OSX */
497 mono_attr.c_cc [VDSUSP] = 255;
499 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
502 set_control_chars (*control_chars, mono_attr.c_cc);
503 /* If initialized from another appdomain... */
507 keypad_xmit_str = NULL;
508 if (keypad != NULL) {
509 keypad_xmit_str = mono_string_to_utf8_checked (keypad, &error);
510 if (mono_error_set_pending_exception (&error))
514 console_set_signal_handlers ();
515 setup_finished = TRUE;
516 if (!atexit_called) {
517 if (teardown != NULL) {
518 teardown_str = mono_string_to_utf8_checked (teardown, &error);
519 if (mono_error_set_pending_exception (&error))
523 mono_atexit (tty_teardown);
528 #endif /* #if defined(__native_client__) */