Index: llvm/lib/Support/Unix/Process.inc =================================================================== --- llvm/lib/Support/Unix/Process.inc +++ llvm/lib/Support/Unix/Process.inc @@ -331,6 +331,23 @@ static ManagedStatic TermColorMutex; #endif +bool checkTerminalEnvironmentForColors() { + if (const char *TermStr = std::getenv("TERM")) { + return StringSwitch(TermStr) + .Case("ansi", true) + .Case("cygwin", true) + .Case("linux", true) + .StartsWith("screen", true) + .StartsWith("xterm", true) + .StartsWith("vt100", true) + .StartsWith("rxvt", true) + .EndsWith("color", true) + .Default(false); + } + + return false; +} + static bool terminalHasColors(int fd) { #ifdef LLVM_ENABLE_TERMINFO // First, acquire a global lock because these C routines are thread hostile. @@ -356,7 +373,8 @@ // // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if // the terminfo says that no colors are supported. - bool HasColors = tigetnum(const_cast("colors")) > 0; + int colors_ti = tigetnum(const_cast("colors")); + bool HasColors = colors_ti >= 0 ? colors_ti : checkTerminalEnvironmentForColors(); // Now extract the structure allocated by setupterm and free its memory // through a really silly dance. @@ -364,27 +382,12 @@ (void)del_curterm(termp); // Drop any errors here. // Return true if we found a color capabilities for the current terminal. - if (HasColors) - return true; + return HasColors; #else // When the terminfo database is not available, check if the current terminal // is one of terminals that are known to support ANSI color escape codes. - if (const char *TermStr = std::getenv("TERM")) { - return StringSwitch(TermStr) - .Case("ansi", true) - .Case("cygwin", true) - .Case("linux", true) - .StartsWith("screen", true) - .StartsWith("xterm", true) - .StartsWith("vt100", true) - .StartsWith("rxvt", true) - .EndsWith("color", true) - .Default(false); - } + return checkTerminalEnvironmentForColors(); #endif - - // Otherwise, be conservative. - return false; } bool Process::FileDescriptorHasColors(int fd) {