diff --git a/arangosh/Shell/ConsoleFeature.cpp b/arangosh/Shell/ConsoleFeature.cpp index 8eb4cca915..0ae48dc0b4 100644 --- a/arangosh/Shell/ConsoleFeature.cpp +++ b/arangosh/Shell/ConsoleFeature.cpp @@ -120,7 +120,7 @@ void ConsoleFeature::collectOptions(std::shared_ptr options) { void ConsoleFeature::prepare() { #if _WIN32 - if (getenv("SHELL") != nullptr) { + if (_is_cyg_tty (STDOUT_FILENO) || getenv("SHELL") != nullptr) { _cygwinShell = true; } #endif diff --git a/lib/Basics/win-utils.cpp b/lib/Basics/win-utils.cpp index 4d48164587..8aeef8adf0 100644 --- a/lib/Basics/win-utils.cpp +++ b/lib/Basics/win-utils.cpp @@ -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; +} diff --git a/lib/Basics/win-utils.h b/lib/Basics/win-utils.h index b5601fd2c2..6b918e20f2 100644 --- a/lib/Basics/win-utils.h +++ b/lib/Basics/win-utils.h @@ -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