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_TIME_H
21 # include <sys/time.h>
23 #include <sys/types.h>
27 #include <mono/metadata/appdomain.h>
28 #include <mono/metadata/object-internals.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/domain-internals.h>
31 #include <mono/metadata/gc-internal.h>
32 #include <mono/metadata/metadata.h>
33 #include <mono/metadata/threadpool.h>
34 #include <mono/utils/mono-signal-handler.h>
36 /* On solaris, curses.h must come before both termios.h and term.h */
47 /* Needed for FIONREAD under solaris */
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
51 #ifdef HAVE_SYS_IOCTL_H
52 # include <sys/ioctl.h>
55 #include <mono/metadata/console-io.h>
56 #include <mono/metadata/exception.h>
58 static gboolean setup_finished;
59 static gboolean atexit_called;
61 /* The string used to return the terminal to its previous state */
62 static gchar *teardown_str;
64 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
65 static gchar *keypad_xmit_str;
68 /* This is the last state used by Mono, used after a CONT signal is received */
69 static struct termios mono_attr;
72 /* static void console_restore_signal_handlers (void); */
73 static void console_set_signal_handlers (void);
76 mono_console_init (void)
80 /* Make sure the standard file descriptors are opened */
81 fd = open ("/dev/null", O_RDWR);
82 while (fd >= 0 && fd < 3) {
83 fd = open ("/dev/null", O_RDWR);
88 static struct termios initial_attr;
91 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
95 return isatty (GPOINTER_TO_INT (handle));
99 set_property (gint property, gboolean value)
102 gboolean callset = FALSE;
107 if (tcgetattr (STDIN_FILENO, &attr) == -1)
110 check = (attr.c_lflag & property) != 0;
111 if ((value || check) && !(value && check)) {
114 attr.c_lflag |= property;
116 attr.c_lflag &= ~property;
122 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
130 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
133 return set_property (ECHO, want_echo);
137 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
139 return set_property (IGNBRK, !want_break);
143 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
147 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)
212 if (teardown_str != NULL) {
213 unused = write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
214 g_free (teardown_str);
218 tcflush (STDIN_FILENO, TCIFLUSH);
219 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
220 set_property (ECHO, TRUE);
221 setup_finished = FALSE;
225 do_console_cancel_event (void)
227 static MonoClassField *cancel_handler_field;
228 MonoDomain *domain = mono_domain_get ();
230 MonoDelegate *load_value;
232 MonoMethodMessage *msg;
236 /* FIXME: this should likely iterate all the domains, instead */
240 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
244 if (cancel_handler_field == NULL) {
245 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
246 g_assert (cancel_handler_field);
249 vtable = mono_class_vtable_full (domain, klass, FALSE);
252 mono_field_static_get_value (vtable, cancel_handler_field, &load_value);
253 if (load_value == NULL)
256 klass = load_value->object.vtable->klass;
257 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
258 g_assert (method != NULL);
259 im = mono_get_delegate_invoke (method->klass);
260 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
261 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
264 static int need_cancel = FALSE;
265 /* this is executed from the finalizer thread */
267 mono_console_handle_async_ops (void)
271 do_console_cancel_event ();
275 static gboolean in_sigint;
277 MONO_SIGNAL_HANDLER_FUNC (static, sigint_handler, (int signo))
288 mono_gc_finalize_notify ();
293 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
295 MONO_SIGNAL_HANDLER_FUNC (static, sigcont_handler, (int signo, void *the_siginfo, void *data))
298 // Ignore error, there is not much we can do in the sigcont handler.
299 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
301 if (keypad_xmit_str != NULL)
302 unused = write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
304 // Call previous handler
305 if (save_sigcont.sa_sigaction != NULL &&
306 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
307 save_sigcont.sa_sigaction != (void *)SIG_IGN)
308 (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
311 MONO_SIGNAL_HANDLER_FUNC (static, sigwinch_handler, (int signo, void *the_siginfo, void *data))
313 int dims = terminal_get_dimensions ();
315 cols_and_lines = dims;
317 // Call previous handler
318 if (save_sigwinch.sa_sigaction != NULL &&
319 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
320 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
321 (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
325 * console_set_signal_handlers:
327 * Installs various signals handlers for the use of the console, as
330 * SIGCONT: this is received after the application has resumed execution
331 * if it was suspended with Control-Z before. This signal handler needs
332 * to resend the terminal sequence to send keyboard in keypad mode (this
333 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
336 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
337 * a thread from the thread pool which notifies all registered cancel_event
340 * SIGWINCH: is used to track changes to the console window when a GUI
341 * terminal is resized. It sets an internal variable that is checked
342 * by System.Console when the Terminfo driver has been activated.
345 console_set_signal_handlers ()
347 struct sigaction sigcont, sigint, sigwinch;
349 memset (&sigcont, 0, sizeof (struct sigaction));
350 memset (&sigint, 0, sizeof (struct sigaction));
351 memset (&sigwinch, 0, sizeof (struct sigaction));
354 sigcont.sa_handler = (void *) sigcont_handler;
355 sigcont.sa_flags = 0;
356 sigemptyset (&sigcont.sa_mask);
357 sigaction (SIGCONT, &sigcont, &save_sigcont);
360 sigint.sa_handler = (void *) sigint_handler;
362 sigemptyset (&sigint.sa_mask);
363 sigaction (SIGINT, &sigint, &save_sigint);
365 // Window size changed
366 sigwinch.sa_handler = (void *) sigwinch_handler;
367 sigwinch.sa_flags = 0;
368 sigemptyset (&sigwinch.sa_mask);
369 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
372 #if currently_unuused
374 // Currently unused, should we ever call the restore handler?
375 // Perhaps before calling into Process.Start?
378 console_restore_signal_handlers ()
380 sigaction (SIGCONT, &save_sigcont, NULL);
381 sigaction (SIGINT, &save_sigint, NULL);
382 sigaction (SIGWINCH, &save_sigwinch, NULL);
387 set_control_chars (MonoArray *control_chars, const guchar *cc)
389 /* The index into the array comes from corlib/System/ControlCharacters.cs */
391 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
394 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
397 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
400 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
403 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
406 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
409 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
412 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
415 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
418 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
421 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
424 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
427 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
430 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
433 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
436 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
439 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
444 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
450 dims = terminal_get_dimensions ();
452 int cols = 0, rows = 0;
454 const char *str = g_getenv ("COLUMNS");
457 str = g_getenv ("LINES");
461 if (cols != 0 && rows != 0)
462 cols_and_lines = (cols << 16) | rows;
466 cols_and_lines = dims;
469 *size = &cols_and_lines;
471 /* 17 is the number of entries set in set_control_chars() above.
472 * NCCS is the total size, but, by now, we only care about those 17 values*/
473 mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) mono_array_new (mono_domain_get (), mono_defaults.byte_class, 17));
474 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
477 mono_attr = initial_attr;
478 mono_attr.c_lflag &= ~(ICANON);
479 mono_attr.c_iflag &= ~(IXON|IXOFF);
480 mono_attr.c_cc [VMIN] = 1;
481 mono_attr.c_cc [VTIME] = 0;
483 /* Disable C-y being used as a suspend character on OSX */
484 mono_attr.c_cc [VDSUSP] = 255;
486 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
489 set_control_chars (*control_chars, mono_attr.c_cc);
490 /* If initialized from another appdomain... */
494 keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
496 console_set_signal_handlers ();
497 setup_finished = TRUE;
498 if (!atexit_called) {
499 if (teardown != NULL)
500 teardown_str = mono_string_to_utf8 (teardown);
502 atexit (tty_teardown);
507 #endif /* #if defined(__native_client__) */