1
0
Fork 0

better detect cygwin i/o

This commit is contained in:
Wilfried Goesgens 2017-04-07 14:00:35 +02:00
parent f33d14f0d1
commit 6ab23bd56d
3 changed files with 54 additions and 1 deletions

View File

@ -120,7 +120,7 @@ void ConsoleFeature::collectOptions(std::shared_ptr<ProgramOptions> options) {
void ConsoleFeature::prepare() {
#if _WIN32
if (getenv("SHELL") != nullptr) {
if (_is_cyg_tty (STDOUT_FILENO) || getenv("SHELL") != nullptr) {
_cygwinShell = true;
}
#endif

View File

@ -653,3 +653,51 @@ _cyg_isatty (int fd)
errno = EINVAL;
return 0;
}
// Detect cygwin ssh / terminals
int
_is_cyg_tty (int fd)
{
// detect standard windows ttys:
if (_isatty (fd)) {
return 0;
}
HANDLE fh;
char buff[sizeof(FILE_NAME_INFO) + sizeof(WCHAR)*MAX_PATH];
FILE_NAME_INFO *FileInformation = (FILE_NAME_INFO*) buff;
/* get the HANDLE for the filedescriptor. */
fh = (HANDLE) _get_osfhandle (fd);
if (!fh || fh == INVALID_HANDLE_VALUE) {
return 0;
}
/* Cygwin consoles are pipes. If its not, no reason to continue: */
if (GetFileType (fh) != FILE_TYPE_PIPE) {
return 0;
}
if (!GetFileInformationByHandleEx(fh, FileNameInfo,
FileInformation, sizeof(buff))) {
return 0;
}
// we expect something along the lines of: \cygwin-0eb90a57d5759b7b-pty3-to-master?? - if we find it its a tty.
PWCHAR cp = (PWCHAR) FileInformation->FileName;
if (!wcsncmp (cp, L"\\cygwin-", 8)
&& !wcsncmp (cp + 24, L"-pty", 4)) {
cp = wcschr (cp + 28, '-');
if (!cp) {
return 0;
}
if (!wcsncmp (cp, L"-from-master", sizeof("-from-master") - 1) ||
!wcsncmp (cp, L"-to-master", sizeof("-to-master") -1)) {
return 1;
}
}
errno = EINVAL;
return 0;
}

View File

@ -141,4 +141,9 @@ void TRI_WindowsEmergencyLog(char const* func, char const* file, int line,
////////////////////////////////////////////////////////////////////////////////
int _cyg_isatty (int fd);
////////////////////////////////////////////////////////////////////////////////
/// @brief detects whether an FD is connected to a cygwin-tty.
////////////////////////////////////////////////////////////////////////////////
int _is_cyg_tty (int fd);
#endif