mingw: make isatty() recognize MSys pseudo terminals (/dev/pty*)

MSys2 emulates pseudo terminals via named pipes, and isatty() returns 0
for such file descriptors. Therefore, some interactive functionality (such
as launching a pager, asking if a failed unlink should be repeated etc.)
doesn't work when run in a terminal emulator that uses MSys ptys (such as
mintty).

However, MSys uses special names for its pty pipes ('msys-*-pty*'), which
allows us to distinguish them from normal piped input / output.

On startup, check if stdin / stdout / stderr are connected to such pipes
using the NtQueryObject API from NTDll.dll. If the names match, adjust the
flags in MSVCRT's ioinfo structure accordingly.

Signed-off-by: Karsten Blees <blees@dcon.de>
This commit is contained in:
Karsten Blees
2015-04-20 19:24:29 +02:00
parent 22a888ee66
commit 7d57fb4511
2 changed files with 58 additions and 5 deletions

View File

@@ -7,6 +7,11 @@
#include <wingdi.h>
#include <winreg.h>
#ifdef USE_NTDLL
#include <winternl.h>
#include <ntstatus.h>
#endif
/*
ANSI codes used by git: m, K
@@ -483,6 +488,7 @@ static size_t sizeof_ioinfo = 0;
#define IOINFO_L2E 5
#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
#define FPIPE 0x08
#define FDEV 0x40
static inline ioinfo* _pioinfo(int fd)
@@ -530,6 +536,44 @@ static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
return old_handle;
}
#ifdef USE_NTDLL
static void msystty_init(int fd)
{
ULONG result;
BYTE buffer[1024];
POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer;
PWSTR name;
/* check if fd is a pipe */
HANDLE h = (HANDLE) _get_osfhandle(fd);
if (GetFileType(h) != FILE_TYPE_PIPE)
return;
/* get pipe name */
if (!NT_SUCCESS(NtQueryObject(h, ObjectNameInformation,
buffer, sizeof(buffer) - 2, &result)))
return;
name = nameinfo->Name.Buffer;
name[nameinfo->Name.Length] = 0;
/* check if this could be a msys pty pipe ('msys-XXXX-ptyN-XX') */
if (!wcsstr(name, L"msys-") || !wcsstr(name, L"-pty"))
return;
/* init ioinfo size if we haven't done so */
if (init_sizeof_ioinfo())
return;
/* set FDEV flag, reset FPIPE flag */
_pioinfo(fd)->osflags &= ~FPIPE;
_pioinfo(fd)->osflags |= FDEV;
}
#else
#define msystty_init(fd) (void)0
#endif
void winansi_init(void)
{
int con1, con2;
@@ -538,8 +582,13 @@ void winansi_init(void)
/* check if either stdout or stderr is a console output screen buffer */
con1 = is_console(1);
con2 = is_console(2);
if (!con1 && !con2)
if (!con1 && !con2) {
/* check if stdin / stdout / stderr are msys pty pipes */
msystty_init(0);
msystty_init(1);
msystty_init(2);
return;
}
/* create a named pipe to communicate with the console thread */
sprintf(name, "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
@@ -575,8 +624,11 @@ void winansi_init(void)
HANDLE winansi_get_osfhandle(int fd)
{
HANDLE hnd = (HANDLE) _get_osfhandle(fd);
if ((fd == 1 || fd == 2) && isatty(fd)
&& GetFileType(hnd) == FILE_TYPE_PIPE)
return (fd == 1) ? hconsole1 : hconsole2;
if (isatty(fd) && GetFileType(hnd) == FILE_TYPE_PIPE) {
if (fd == 1 && hconsole1)
return hconsole1;
else if (fd == 2 && hconsole2)
return hconsole2;
}
return hnd;
}

View File

@@ -548,7 +548,8 @@ else
BASIC_LDFLAGS += -Wl,--large-address-aware
endif
CC = gcc
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DUSE_NTDLL
EXTLIBS += -lntdll
INSTALL = /bin/install
NO_R_TO_GCC_LINKER = YesPlease
INTERNAL_QSORT = YesPlease