Index: lib/Support/Unix/Process.inc =================================================================== --- lib/Support/Unix/Process.inc +++ lib/Support/Unix/Process.inc @@ -353,11 +353,15 @@ // First, acquire a global lock because these C routines are thread hostile. MutexGuard G(*TermColorMutex); + // Save the current term (if set) to be restored after the test. + struct term *OrigTerm = set_curterm((struct term *)nullptr); int errret = 0; - if (setupterm((char *)nullptr, fd, &errret) != 0) + if (setupterm((char *)nullptr, fd, &errret) != 0) { // Regardless of why, if we can't get terminfo, we shouldn't try to print // colors. + (void)set_curterm(OrigTerm); return false; + } // Test whether the terminal as set up supports color output. How to do this // isn't entirely obvious. We can use the curses routine 'has_colors' but it @@ -375,8 +379,9 @@ bool HasColors = tigetnum(const_cast("colors")) > 0; // Now extract the structure allocated by setupterm and free its memory - // through a really silly dance. - struct term *termp = set_curterm((struct term *)nullptr); + // through a really silly dance. Also restore the previously saved term + // here. + struct term *termp = set_curterm(OrigTerm); (void)del_curterm(termp); // Drop any errors here. // Return true if we found a color capabilities for the current terminal.