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)
17 #ifdef HAVE_SYS_TIME_H
18 # include <sys/time.h>
20 #include <sys/types.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/object-internals.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/domain-internals.h>
28 #include <mono/metadata/metadata.h>
29 #include <mono/metadata/threadpool.h>
31 /* On solaris, curses.h must come before both termios.h and term.h */
42 /* Needed for FIONREAD under solaris */
43 #ifdef HAVE_SYS_FILIO_H
44 # include <sys/filio.h>
47 # ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
52 #include <mono/metadata/console-io.h>
53 #include <mono/metadata/exception.h>
55 static gboolean setup_finished;
56 static gboolean atexit_called;
58 /* The string used to return the terminal to its previous state */
59 static gchar *teardown_str;
61 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
62 static gchar *keypad_xmit_str;
65 /* This is the last state used by Mono, used after a CONT signal is received */
66 static struct termios mono_attr;
70 mono_console_init (void)
74 /* Make sure the standard file descriptors are opened */
75 fd = open ("/dev/null", O_RDWR);
76 while (fd >= 0 && fd < 3) {
77 fd = open ("/dev/null", O_RDWR);
82 static struct termios initial_attr;
85 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
89 return isatty (GPOINTER_TO_INT (handle));
93 set_property (gint property, gboolean value)
96 gboolean callset = FALSE;
101 if (tcgetattr (STDIN_FILENO, &attr) == -1)
104 check = (attr.c_lflag & property) != 0;
105 if ((value || check) && !(value && check)) {
108 attr.c_lflag |= property;
110 attr.c_lflag &= ~property;
116 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
124 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
127 return set_property (ECHO, want_echo);
131 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
133 return set_property (IGNBRK, !want_break);
137 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
141 struct timeval *tvptr;
149 FD_SET (STDIN_FILENO, &rfds);
151 divvy = div (timeout, 1000);
152 tv.tv_sec = divvy.quot;
153 tv.tv_usec = divvy.rem;
158 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
159 } while (ret == -1 && errno == EINTR);
163 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
168 return (ret > 0) ? ret : 0;
171 static gint32 cols_and_lines;
175 terminal_get_dimensions (void)
179 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
180 return (ws.ws_col << 16) | ws.ws_row;
186 terminal_get_dimensions (void)
200 if (teardown_str != NULL) {
201 write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
202 g_free (teardown_str);
206 tcflush (STDIN_FILENO, TCIFLUSH);
207 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
208 set_property (ECHO, TRUE);
209 setup_finished = FALSE;
213 do_console_cancel_event (void)
215 static MonoClassField *cancel_handler_field;
216 MonoDomain *domain = mono_domain_get ();
218 MonoDelegate *load_value;
220 MonoMethodMessage *msg;
226 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
230 if (cancel_handler_field == NULL) {
231 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
232 g_assert (cancel_handler_field);
235 mono_field_static_get_value (mono_class_vtable (domain, klass), cancel_handler_field, &load_value);
236 if (load_value == NULL)
239 klass = load_value->object.vtable->klass;
240 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
241 g_assert (method != NULL);
242 im = mono_get_delegate_invoke (method->klass);
243 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
244 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
247 static gboolean in_sigint;
249 sigint_handler (int signo)
257 do_console_cancel_event ();
261 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
264 sigcont_handler (int signo, void *the_siginfo, void *data)
266 // Ignore error, there is not much we can do in the sigcont handler.
267 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
269 if (keypad_xmit_str != NULL)
270 write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
272 // Call previous handler
273 if (save_sigcont.sa_sigaction != NULL &&
274 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
275 save_sigcont.sa_sigaction != (void *)SIG_IGN)
276 (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
280 sigwinch_handler (int signo, void *the_siginfo, void *data)
282 int dims = terminal_get_dimensions ();
284 cols_and_lines = dims;
286 // Call previous handler
287 if (save_sigwinch.sa_sigaction != NULL &&
288 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
289 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
290 (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
294 console_set_signal_handlers ()
296 struct sigaction sigcont, sigint, sigwinch;
298 memset (&sigcont, 0, sizeof (struct sigaction));
299 memset (&sigint, 0, sizeof (struct sigaction));
300 memset (&sigwinch, 0, sizeof (struct sigaction));
303 sigcont.sa_handler = (void *) sigcont_handler;
304 sigcont.sa_flags = 0;
305 sigemptyset (&sigcont.sa_mask);
306 sigaction (SIGCONT, &sigcont, &save_sigcont);
309 sigint.sa_handler = sigint_handler;
311 sigemptyset (&sigint.sa_mask);
312 sigaction (SIGINT, &sigint, &save_sigint);
314 // Window size changed
315 sigwinch.sa_handler = (void *) sigwinch_handler;
316 sigwinch.sa_flags = 0;
317 sigemptyset (&sigwinch.sa_mask);
318 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
322 // Currently unused, should we ever call the restore handler?
323 // Perhaps before calling into Process.Start?
326 console_restore_signal_handlers ()
328 sigaction (SIGCONT, &save_sigcont, NULL);
329 sigaction (SIGINT, &save_sigint, NULL);
330 sigaction (SIGWINCH, &save_sigwinch, NULL);
334 set_control_chars (MonoArray *control_chars, const guchar *cc)
336 /* The index into the array comes from corlib/System/ControlCharacters.cs */
338 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
341 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
344 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
347 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
350 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
353 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
356 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
359 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
362 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
365 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
368 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
371 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
374 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
377 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
380 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
383 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
386 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
391 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
397 dims = terminal_get_dimensions ();
399 int cols = 0, rows = 0;
401 char *str = getenv ("COLUMNS");
404 str = getenv ("LINES");
408 if (cols != 0 && rows != 0)
409 cols_and_lines = (cols << 16) | rows;
413 cols_and_lines = dims;
416 *size = &cols_and_lines;
418 /* 17 is the number of entries set in set_control_chars() above.
419 * NCCS is the total size, but, by now, we only care about those 17 values*/
420 *control_chars = mono_array_new (mono_domain_get (), mono_defaults.byte_class, 17);
421 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
424 mono_attr = initial_attr;
425 mono_attr.c_lflag &= ~(ICANON);
426 mono_attr.c_iflag &= ~(IXON|IXOFF);
427 mono_attr.c_cc [VMIN] = 1;
428 mono_attr.c_cc [VTIME] = 0;
429 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
432 set_control_chars (*control_chars, mono_attr.c_cc);
433 /* If initialized from another appdomain... */
437 keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
439 console_set_signal_handlers ();
440 setup_finished = TRUE;
441 if (!atexit_called) {
442 if (teardown != NULL)
443 teardown_str = mono_string_to_utf8 (teardown);
445 atexit (tty_teardown);