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)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 #if defined(__native_client__)
11 #include "console-null.c"
21 #ifdef HAVE_SYS_SELECT_H
22 # include <sys/select.h>
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
27 #include <sys/types.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/object-internals.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/metadata/domain-internals.h>
35 #include <mono/metadata/gc-internals.h>
36 #include <mono/metadata/metadata.h>
37 #include <mono/metadata/threadpool-ms.h>
38 #include <mono/utils/mono-signal-handler.h>
39 #include <mono/utils/mono-proclib.h>
40 #include <mono/io-layer/io-layer.h>
42 /* On solaris, curses.h must come before both termios.h and term.h */
53 /* Needed for FIONREAD under solaris */
54 #ifdef HAVE_SYS_FILIO_H
55 # include <sys/filio.h>
57 #ifdef HAVE_SYS_IOCTL_H
58 # include <sys/ioctl.h>
61 #include <mono/metadata/console-io.h>
62 #include <mono/metadata/exception.h>
64 static gboolean setup_finished;
65 static gboolean atexit_called;
67 /* The string used to return the terminal to its previous state */
68 static gchar *teardown_str;
70 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
71 static gchar *keypad_xmit_str;
74 /* This is the last state used by Mono, used after a CONT signal is received */
75 static struct termios mono_attr;
78 /* static void console_restore_signal_handlers (void); */
79 static void console_set_signal_handlers (void);
82 mono_console_init (void)
86 /* Make sure the standard file descriptors are opened */
87 fd = open ("/dev/null", O_RDWR);
88 while (fd >= 0 && fd < 3) {
89 fd = open ("/dev/null", O_RDWR);
94 static struct termios initial_attr;
97 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;
109 if (tcgetattr (STDIN_FILENO, &attr) == -1)
112 check = (attr.c_lflag & property) != 0;
113 if ((value || check) && !(value && check)) {
116 attr.c_lflag |= property;
118 attr.c_lflag &= ~property;
124 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
132 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
135 return set_property (ECHO, want_echo);
139 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
141 return set_property (IGNBRK, !want_break);
145 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
149 struct timeval *tvptr;
155 FD_SET (STDIN_FILENO, &rfds);
157 divvy = div (timeout, 1000);
158 tv.tv_sec = divvy.quot;
159 tv.tv_usec = divvy.rem;
164 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
165 } while (ret == -1 && errno == EINTR);
169 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
174 return (ret > 0) ? ret : 0;
177 static gint32 cols_and_lines;
181 terminal_get_dimensions (void)
185 int save_errno = errno;
187 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0){
188 ret = (ws.ws_col << 16) | ws.ws_row;
196 terminal_get_dimensions (void)
205 int unused G_GNUC_UNUSED;
210 if (teardown_str != NULL) {
211 unused = write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
212 g_free (teardown_str);
216 tcflush (STDIN_FILENO, TCIFLUSH);
217 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
218 set_property (ECHO, TRUE);
219 setup_finished = FALSE;
223 do_console_cancel_event (void)
225 static MonoClassField *cancel_handler_field;
227 MonoDomain *domain = mono_domain_get ();
229 MonoDelegate *load_value;
233 /* FIXME: this should likely iterate all the domains, instead */
237 klass = mono_class_try_load_from_name (mono_defaults.corlib, "System", "Console");
241 if (cancel_handler_field == NULL) {
242 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
243 g_assert (cancel_handler_field);
246 vtable = mono_class_vtable_full (domain, klass, &error);
247 if (vtable == NULL || !is_ok (&error)) {
248 mono_error_cleanup (&error);
251 mono_field_static_get_value_checked (vtable, cancel_handler_field, &load_value, &error);
252 if (load_value == NULL || !is_ok (&error)) {
253 mono_error_cleanup (&error);
257 klass = load_value->object.vtable->klass;
258 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
259 g_assert (method != NULL);
261 mono_threadpool_ms_begin_invoke (domain, (MonoObject*) load_value, method, NULL, &error);
262 if (!is_ok (&error)) {
263 g_warning ("Couldn't invoke System.Console cancel handler due to %s", mono_error_get_message (&error));
264 mono_error_cleanup (&error);
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)
291 mono_gc_finalize_notify ();
296 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
298 MONO_SIG_HANDLER_FUNC (static, sigcont_handler)
300 int unused G_GNUC_UNUSED;
301 // Ignore error, there is not much we can do in the sigcont handler.
302 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
304 if (keypad_xmit_str != NULL)
305 unused = write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
307 // Call previous handler
308 if (save_sigcont.sa_sigaction != NULL &&
309 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
310 save_sigcont.sa_sigaction != (void *)SIG_IGN)
311 (*save_sigcont.sa_sigaction) (MONO_SIG_HANDLER_PARAMS);
314 MONO_SIG_HANDLER_FUNC (static, sigwinch_handler)
316 int dims = terminal_get_dimensions ();
318 cols_and_lines = dims;
320 // Call previous handler
321 if (save_sigwinch.sa_sigaction != NULL &&
322 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
323 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
324 (*save_sigwinch.sa_sigaction) (MONO_SIG_HANDLER_PARAMS);
328 * console_set_signal_handlers:
330 * Installs various signals handlers for the use of the console, as
333 * SIGCONT: this is received after the application has resumed execution
334 * if it was suspended with Control-Z before. This signal handler needs
335 * to resend the terminal sequence to send keyboard in keypad mode (this
336 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
339 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
340 * a thread from the thread pool which notifies all registered cancel_event
343 * SIGWINCH: is used to track changes to the console window when a GUI
344 * terminal is resized. It sets an internal variable that is checked
345 * by System.Console when the Terminfo driver has been activated.
348 console_set_signal_handlers ()
350 #if defined(HAVE_SIGACTION)
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 (*)(int)) sigcont_handler;
359 sigcont.sa_flags = SA_RESTART;
360 sigemptyset (&sigcont.sa_mask);
361 sigaction (SIGCONT, &sigcont, &save_sigcont);
364 sigint.sa_handler = (void (*)(int)) sigint_handler;
365 sigint.sa_flags = SA_RESTART;
366 sigemptyset (&sigint.sa_mask);
367 sigaction (SIGINT, &sigint, &save_sigint);
369 // Window size changed
370 sigwinch.sa_handler = (void (*)(int)) sigwinch_handler;
371 sigwinch.sa_flags = SA_RESTART;
372 sigemptyset (&sigwinch.sa_mask);
373 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
377 #if currently_unuused
379 // Currently unused, should we ever call the restore handler?
380 // Perhaps before calling into Process.Start?
383 console_restore_signal_handlers ()
385 sigaction (SIGCONT, &save_sigcont, NULL);
386 sigaction (SIGINT, &save_sigint, NULL);
387 sigaction (SIGWINCH, &save_sigwinch, NULL);
392 set_control_chars (MonoArray *control_chars, const guchar *cc)
394 /* The index into the array comes from corlib/System/ControlCharacters.cs */
396 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
399 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
402 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
405 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
408 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
411 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
414 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
417 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
420 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
423 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
426 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
429 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
432 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
435 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
438 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
441 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
444 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
449 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
455 dims = terminal_get_dimensions ();
457 int cols = 0, rows = 0;
459 const char *str = g_getenv ("COLUMNS");
462 str = g_getenv ("LINES");
466 if (cols != 0 && rows != 0)
467 cols_and_lines = (cols << 16) | rows;
471 cols_and_lines = dims;
474 *size = &cols_and_lines;
476 /* 17 is the number of entries set in set_control_chars() above.
477 * NCCS is the total size, but, by now, we only care about those 17 values*/
478 MonoArray *control_chars_arr = mono_array_new_checked (mono_domain_get (), mono_defaults.byte_class, 17, &error);
479 if (mono_error_set_pending_exception (&error))
481 mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) control_chars_arr);
482 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
485 mono_attr = initial_attr;
486 mono_attr.c_lflag &= ~(ICANON);
487 mono_attr.c_iflag &= ~(IXON|IXOFF);
488 mono_attr.c_cc [VMIN] = 1;
489 mono_attr.c_cc [VTIME] = 0;
491 /* Disable C-y being used as a suspend character on OSX */
492 mono_attr.c_cc [VDSUSP] = 255;
494 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
497 set_control_chars (*control_chars, mono_attr.c_cc);
498 /* If initialized from another appdomain... */
502 keypad_xmit_str = NULL;
503 if (keypad != NULL) {
504 keypad_xmit_str = mono_string_to_utf8_checked (keypad, &error);
505 if (mono_error_set_pending_exception (&error))
509 console_set_signal_handlers ();
510 setup_finished = TRUE;
511 if (!atexit_called) {
512 if (teardown != NULL) {
513 teardown_str = mono_string_to_utf8_checked (teardown, &error);
514 if (mono_error_set_pending_exception (&error))
518 mono_atexit (tty_teardown);
523 #endif /* #if defined(__native_client__) */