From de0d35dbabf220e47d50e5af7e7c5e9cbe977a13 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 9 Mar 2015 17:38:35 +0100 Subject: [PATCH] mingw: Work around MSVCRT's isatty() not knowing about MSys2 MSys2 has a slightly different notion of what constitutes a tty than the Microsoft C runtime. The former knows whether stdin/stdout/stderr was redirected or not, while the latter looks for a Win32 Console. In particular when we want to know whether to spawn a pager or not, we would rather want to know what MSys2 thinks. We are about to introduce a change to the msys2-runtime that sets an environment variable MSYS_TTY_HANDLES to a list of Win32 handles that correspond to stdin/stdout/stderr, respectively, *but skips* handles that MSys2 does not think are terminals. This commit handles that input to augment the isatty() function to return 1 also when MSYS_TTY_HANDLES contains the corresponding handle. The only time when Git needs to know whether a Console is attached or not is when winansi.c is asked to Do Its Thing, therefore we refrain from overriding isatty there. Note: this was an issue with MSys1-based Git for Windows, too, hidden by the fact that Git for Windows used `cmd.exe` as a terminal -- which is backed by a real Win32 Console. Had MSys1 used, say, rxvt as its default terminal, the symptom would have been that "git log" does not spawn a pager by default but instead outputs the entire history (without color coding, too). In MSys2, the default terminal is mintty, therefore we finally could not avoid to address the issue. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 37 +++++++++++++++++++++++++++++++++++++ compat/mingw.h | 3 +++ compat/winansi.c | 2 ++ 3 files changed, 42 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index ad9e3311a6..38a79d0aff 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -6,6 +6,8 @@ #include "../run-command.h" #include "../cache.h" +#undef isatty + static const int delay[] = { 0, 1, 10, 20, 40 }; unsigned int _CRT_fmode = _O_BINARY; @@ -2287,3 +2289,38 @@ void mingw_startup() /* init length of current directory for handle_long_path */ current_directory_len = GetCurrentDirectoryW(0, NULL); } + +int mingw_isatty(int fd) { + static DWORD id[] = { + STD_INPUT_HANDLE, + STD_OUTPUT_HANDLE, + STD_ERROR_HANDLE + }; + static unsigned initialized; + static int is_tty[ARRAY_SIZE(id)]; + + if (fd < 0 || fd >= ARRAY_SIZE(is_tty)) + return isatty(fd); + + if (isatty(fd)) + return 1; + + if (!initialized) { + const char *env = getenv("MSYS_TTY_HANDLES"); + + if (env) { + int i; + char buffer[64]; + + for (i = 0; i < ARRAY_SIZE(is_tty); i++) { + sprintf(buffer, " %ld ", (unsigned long) + GetStdHandle(id[i])); + is_tty[i] = !!strstr(env, buffer); + } + } + + initialized = 1; + } + + return is_tty[fd]; +} diff --git a/compat/mingw.h b/compat/mingw.h index a628b2d48d..02ead6a9ee 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -367,6 +367,9 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler); int mingw_raise(int sig); #define raise mingw_raise +int mingw_isatty(int fd); +#define isatty mingw_isatty + /* * ANSI emulation wrappers */ diff --git a/compat/winansi.c b/compat/winansi.c index aac430a952..71fb0a4f25 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -7,6 +7,8 @@ #include #include +#undef isatty + /* ANSI codes used by git: m, K