Skip to content

Commit

Permalink
Detect mintty as a terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Nov 29, 2017
1 parent 2d732a1 commit 4fdc5fa
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/nvim/os/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include <uv.h>

#ifdef WIN32
#include <Windows.h>
#endif

#include "nvim/api/private/defs.h"
#include "nvim/os/input.h"
#include "nvim/event/loop.h"
Expand Down Expand Up @@ -170,13 +174,77 @@ void input_disable_events(void)
events_enabled--;
}

#ifdef WIN32
// Hack to detect mintty, ported from vim
// https://fossies.org/linux/vim/src/iscygpty.c
// See https://github.com/BurntSushi/ripgrep/issues/94#issuecomment-261745480 for a good explanation
static bool msys_tty_on_handle(int fd) {
const int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
WCHAR *p = NULL;

const HANDLE h = (HANDLE) _get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
return false;
}
/* Cygwin/msys's pty is a pipe. */
if (GetFileType(h) != FILE_TYPE_PIPE) {
return false;
}
FILE_NAME_INFO *nameinfo = malloc(size);
if (nameinfo == NULL) {
return false;
}
/* Check the name of the pipe:
* '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' */
if (GetFileInformationByHandleEx(h, FileNameInfo, nameinfo, size)) {
nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
p = nameinfo->FileName;
if (wcsstr(p, L"\\cygwin-") == p) { /* Cygwin */
p += 8;
} else if (wcsstr(p, L"\\msys-") == p) { /* MSYS and MSYS2 */
p += 6;
} else {
p = NULL;
}
if (p != NULL) {
while (*p && isxdigit(*p)) /* Skip 16-digit hexadecimal. */
++p;
if (wcsstr(p, L"-pty") == p) {
p += 4;
} else {
p = NULL;
}
}
if (p != NULL) {
while (*p && isdigit(*p)) /* Skip pty number. */
++p;
if (wcsstr(p, L"-from-master") == p) {
//p += 12;
} else if (wcsstr(p, L"-to-master") == p) {
//p += 10;
} else {
p = NULL;
}
}
}
free(nameinfo);
return (p != NULL);
}
#endif

/// Test whether a file descriptor refers to a terminal.
///
/// @param fd File descriptor.
/// @return `true` if file descriptor refers to a terminal.
bool os_isatty(int fd)
{
return uv_guess_handle(fd) == UV_TTY;
if (uv_guess_handle(fd) == UV_TTY)
return true;
#ifdef WIN32
return msys_tty_on_handle(fd);
#else
return false;
#endif
}

size_t input_enqueue(String keys)
Expand Down

0 comments on commit 4fdc5fa

Please sign in to comment.