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>
46 #ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
50 #include <mono/metadata/console-io.h>
51 #include <mono/metadata/exception.h>
53 static gboolean setup_finished
;
54 static gboolean atexit_called
;
56 /* The string used to return the terminal to its previous state */
57 static gchar
*teardown_str
;
59 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
60 static gchar
*keypad_xmit_str
;
63 /* This is the last state used by Mono, used after a CONT signal is received */
64 static struct termios mono_attr
;
67 /* static void console_restore_signal_handlers (void); */
68 static void console_set_signal_handlers (void);
71 mono_console_init (void)
75 /* Make sure the standard file descriptors are opened */
76 fd
= open ("/dev/null", O_RDWR
);
77 while (fd
>= 0 && fd
< 3) {
78 fd
= open ("/dev/null", O_RDWR
);
83 static struct termios initial_attr
;
86 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle
)
90 return isatty (GPOINTER_TO_INT (handle
));
94 set_property (gint property
, gboolean value
)
97 gboolean callset
= FALSE
;
102 if (tcgetattr (STDIN_FILENO
, &attr
) == -1)
105 check
= (attr
.c_lflag
& property
) != 0;
106 if ((value
|| check
) && !(value
&& check
)) {
109 attr
.c_lflag
|= property
;
111 attr
.c_lflag
&= ~property
;
117 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &attr
) == -1)
125 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo
)
128 return set_property (ECHO
, want_echo
);
132 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break
)
134 return set_property (IGNBRK
, !want_break
);
138 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout
)
142 struct timeval
*tvptr
;
150 FD_SET (STDIN_FILENO
, &rfds
);
152 divvy
= div (timeout
, 1000);
153 tv
.tv_sec
= divvy
.quot
;
154 tv
.tv_usec
= divvy
.rem
;
159 ret
= select (STDIN_FILENO
+ 1, &rfds
, NULL
, NULL
, tvptr
);
160 } while (ret
== -1 && errno
== EINTR
);
164 ret
= ioctl (STDIN_FILENO
, FIONREAD
, &nbytes
);
169 return (ret
> 0) ? ret
: 0;
172 static gint32 cols_and_lines
;
176 terminal_get_dimensions (void)
180 int save_errno
= errno
;
182 if (ioctl (STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0){
183 ret
= (ws
.ws_col
<< 16) | ws
.ws_row
;
191 terminal_get_dimensions (void)
205 if (teardown_str
!= NULL
) {
206 write (STDOUT_FILENO
, teardown_str
, strlen (teardown_str
));
207 g_free (teardown_str
);
211 tcflush (STDIN_FILENO
, TCIFLUSH
);
212 tcsetattr (STDIN_FILENO
, TCSANOW
, &initial_attr
);
213 set_property (ECHO
, TRUE
);
214 setup_finished
= FALSE
;
218 do_console_cancel_event (void)
220 static MonoClassField
*cancel_handler_field
;
221 MonoDomain
*domain
= mono_domain_get ();
223 MonoDelegate
*load_value
;
225 MonoMethodMessage
*msg
;
232 klass
= mono_class_from_name (mono_defaults
.corlib
, "System", "Console");
236 if (cancel_handler_field
== NULL
) {
237 cancel_handler_field
= mono_class_get_field_from_name (klass
, "cancel_handler");
238 g_assert (cancel_handler_field
);
241 vtable
= mono_class_vtable_full (domain
, klass
, FALSE
);
244 mono_field_static_get_value (vtable
, cancel_handler_field
, &load_value
);
245 if (load_value
== NULL
)
248 klass
= load_value
->object
.vtable
->klass
;
249 method
= mono_class_get_method_from_name (klass
, "BeginInvoke", -1);
250 g_assert (method
!= NULL
);
251 im
= mono_get_delegate_invoke (method
->klass
);
252 msg
= mono_method_call_message_new (method
, NULL
, im
, NULL
, NULL
);
253 mono_thread_pool_add ((MonoObject
*) load_value
, msg
, NULL
, NULL
);
256 static gboolean in_sigint
;
258 sigint_handler (int signo
)
268 do_console_cancel_event ();
273 static struct sigaction save_sigcont
, save_sigint
, save_sigwinch
;
276 sigcont_handler (int signo
, void *the_siginfo
, void *data
)
278 // Ignore error, there is not much we can do in the sigcont handler.
279 tcsetattr (STDIN_FILENO
, TCSANOW
, &mono_attr
);
281 if (keypad_xmit_str
!= NULL
)
282 write (STDOUT_FILENO
, keypad_xmit_str
, strlen (keypad_xmit_str
));
284 // Call previous handler
285 if (save_sigcont
.sa_sigaction
!= NULL
&&
286 save_sigcont
.sa_sigaction
!= (void *)SIG_DFL
&&
287 save_sigcont
.sa_sigaction
!= (void *)SIG_IGN
)
288 (*save_sigcont
.sa_sigaction
) (signo
, the_siginfo
, data
);
292 sigwinch_handler (int signo
, void *the_siginfo
, void *data
)
294 int dims
= terminal_get_dimensions ();
296 cols_and_lines
= dims
;
298 // Call previous handler
299 if (save_sigwinch
.sa_sigaction
!= NULL
&&
300 save_sigwinch
.sa_sigaction
!= (void *)SIG_DFL
&&
301 save_sigwinch
.sa_sigaction
!= (void *)SIG_IGN
)
302 (*save_sigwinch
.sa_sigaction
) (signo
, the_siginfo
, data
);
306 * console_set_signal_handlers:
308 * Installs various signals handlers for the use of the console, as
311 * SIGCONT: this is received after the application has resumed execution
312 * if it was suspended with Control-Z before. This signal handler needs
313 * to resend the terminal sequence to send keyboard in keypad mode (this
314 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
317 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
318 * a thread from the thread pool which notifies all registered cancel_event
321 * SIGWINCH: is used to track changes to the console window when a GUI
322 * terminal is resized. It sets an internal variable that is checked
323 * by System.Console when the Terminfo driver has been activated.
326 console_set_signal_handlers ()
328 struct sigaction sigcont
, sigint
, sigwinch
;
330 memset (&sigcont
, 0, sizeof (struct sigaction
));
331 memset (&sigint
, 0, sizeof (struct sigaction
));
332 memset (&sigwinch
, 0, sizeof (struct sigaction
));
335 sigcont
.sa_handler
= (void *) sigcont_handler
;
336 sigcont
.sa_flags
= 0;
337 sigemptyset (&sigcont
.sa_mask
);
338 sigaction (SIGCONT
, &sigcont
, &save_sigcont
);
341 sigint
.sa_handler
= sigint_handler
;
343 sigemptyset (&sigint
.sa_mask
);
344 sigaction (SIGINT
, &sigint
, &save_sigint
);
346 // Window size changed
347 sigwinch
.sa_handler
= (void *) sigwinch_handler
;
348 sigwinch
.sa_flags
= 0;
349 sigemptyset (&sigwinch
.sa_mask
);
350 sigaction (SIGWINCH
, &sigwinch
, &save_sigwinch
);
353 #if currently_unuused
355 // Currently unused, should we ever call the restore handler?
356 // Perhaps before calling into Process.Start?
359 console_restore_signal_handlers ()
361 sigaction (SIGCONT
, &save_sigcont
, NULL
);
362 sigaction (SIGINT
, &save_sigint
, NULL
);
363 sigaction (SIGWINCH
, &save_sigwinch
, NULL
);
368 set_control_chars (MonoArray
*control_chars
, const guchar
*cc
)
370 /* The index into the array comes from corlib/System/ControlCharacters.cs */
372 mono_array_set (control_chars
, gchar
, 0, cc
[VINTR
]);
375 mono_array_set (control_chars
, gchar
, 1, cc
[VQUIT
]);
378 mono_array_set (control_chars
, gchar
, 2, cc
[VERASE
]);
381 mono_array_set (control_chars
, gchar
, 3, cc
[VKILL
]);
384 mono_array_set (control_chars
, gchar
, 4, cc
[VEOF
]);
387 mono_array_set (control_chars
, gchar
, 5, cc
[VTIME
]);
390 mono_array_set (control_chars
, gchar
, 6, cc
[VMIN
]);
393 mono_array_set (control_chars
, gchar
, 7, cc
[VSWTC
]);
396 mono_array_set (control_chars
, gchar
, 8, cc
[VSTART
]);
399 mono_array_set (control_chars
, gchar
, 9, cc
[VSTOP
]);
402 mono_array_set (control_chars
, gchar
, 10, cc
[VSUSP
]);
405 mono_array_set (control_chars
, gchar
, 11, cc
[VEOL
]);
408 mono_array_set (control_chars
, gchar
, 12, cc
[VREPRINT
]);
411 mono_array_set (control_chars
, gchar
, 13, cc
[VDISCARD
]);
414 mono_array_set (control_chars
, gchar
, 14, cc
[VWERASE
]);
417 mono_array_set (control_chars
, gchar
, 15, cc
[VLNEXT
]);
420 mono_array_set (control_chars
, gchar
, 16, cc
[VEOL2
]);
425 ves_icall_System_ConsoleDriver_TtySetup (MonoString
*keypad
, MonoString
*teardown
, MonoArray
**control_chars
, int **size
)
431 dims
= terminal_get_dimensions ();
433 int cols
= 0, rows
= 0;
435 char *str
= getenv ("COLUMNS");
438 str
= getenv ("LINES");
442 if (cols
!= 0 && rows
!= 0)
443 cols_and_lines
= (cols
<< 16) | rows
;
447 cols_and_lines
= dims
;
450 *size
= &cols_and_lines
;
452 /* 17 is the number of entries set in set_control_chars() above.
453 * NCCS is the total size, but, by now, we only care about those 17 values*/
454 mono_gc_wbarrier_generic_store (control_chars
, (MonoObject
*) mono_array_new (mono_domain_get (), mono_defaults
.byte_class
, 17));
455 if (tcgetattr (STDIN_FILENO
, &initial_attr
) == -1)
458 mono_attr
= initial_attr
;
459 mono_attr
.c_lflag
&= ~(ICANON
);
460 mono_attr
.c_iflag
&= ~(IXON
|IXOFF
);
461 mono_attr
.c_cc
[VMIN
] = 1;
462 mono_attr
.c_cc
[VTIME
] = 0;
464 /* Disable C-y being used as a suspend character on OSX */
465 mono_attr
.c_cc
[VDSUSP
] = 255;
467 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &mono_attr
) == -1)
470 set_control_chars (*control_chars
, mono_attr
.c_cc
);
471 /* If initialized from another appdomain... */
475 keypad_xmit_str
= keypad
!= NULL
? mono_string_to_utf8 (keypad
) : NULL
;
477 console_set_signal_handlers ();
478 setup_finished
= TRUE
;
479 if (!atexit_called
) {
480 if (teardown
!= NULL
)
481 teardown_str
= mono_string_to_utf8 (teardown
);
483 atexit (tty_teardown
);