Cygwin: pty: add pseudo console support.

- Support pseudo console in PTY. Pseudo console is a new feature
  in Windows 10 1809, which provides console APIs on virtual
  terminal. With this patch, native console applications can work
  in PTYs such as mintty, ssh, gnu screen or tmux.
This commit is contained in:
Takashi Yano
2019-08-28 03:04:02 +09:00
committed by Corinna Vinschen
parent 398476acd2
commit 169d65a577
12 changed files with 2016 additions and 55 deletions

View File

@@ -147,6 +147,36 @@ dtable::get_debugger_info ()
void
dtable::stdio_init ()
{
bool need_fixup_handle = false;
fhandler_pty_slave *ptys = NULL;
bool is_pty[3] = {false, false, false};
for (int fd = 0; fd < 3; fd ++)
{
fhandler_base *fh = cygheap->fdtab[fd];
if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
ptys = (fhandler_pty_slave *) fh;
if (ptys->getPseudoConsole ())
{
is_pty[fd] = true;
bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
if (!attached)
{
/* Not attached to pseudo console in fork() or spawn()
by some reason. This happens if the executable is
a windows GUI binary, such as mintty. */
FreeConsole ();
AttachConsole (ptys->getHelperProcessId ());
need_fixup_handle = true;
}
ptys->reset_switch_to_pcon ();
}
}
}
if (need_fixup_handle)
goto fixup_handle;
if (myself->cygstarted || ISSTATE (myself, PID_CYGPARENT))
{
tty_min *t = cygwin_shared->tty.get_cttyp ();
@@ -155,6 +185,27 @@ dtable::stdio_init ()
return;
}
fixup_handle:
if (need_fixup_handle)
{
HANDLE h;
h = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, 0);
if (is_pty[0])
{
SetStdHandle (STD_INPUT_HANDLE, h);
ptys->set_handle (h);
}
h = CreateFile ("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0);
if (is_pty[1])
SetStdHandle (STD_OUTPUT_HANDLE, h);
if (is_pty[2])
SetStdHandle (STD_ERROR_HANDLE, h);
if (is_pty[1] || is_pty[2])
ptys->set_output_handle (h);
}
HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
HANDLE err = GetStdHandle (STD_ERROR_HANDLE);