summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Hood <jadoxa@yahoo.com.au>2019-02-12 23:18:47 +1000
committerNico Williams <nico@cryptonector.com>2019-02-16 20:40:41 -0600
commit87594f81d6f63556f2644fff4ed493aaac97974c (patch)
tree059c0297dd47da6906cea200238903b639286e62
parentdef42a31caef46dbc122cb41f213784d915d318e (diff)
Prevent redirecting to NUL crash
Windows regards the `NUL` device as tty, which causes `WriteFile` to fail (the bytes written pointer cannot be `NULL` in this case). Tweak the color test to ensure tty is accurate.
-rw-r--r--src/main.c24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/main.c b/src/main.c
index 233d130b..b8c2857d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -533,21 +533,19 @@ int main(int argc, char* argv[]) {
#ifdef USE_ISATTY
if (isatty(STDOUT_FILENO)) {
- dumpopts |= JV_PRINT_ISATTY;
#ifndef WIN32
- /* Disable color by default on Windows builds as Windows
- terminals tend not to display it correctly */
- dumpopts |= JV_PRINT_COLOR;
+ dumpopts |= JV_PRINT_ISATTY | JV_PRINT_COLOR;
#else
- // Unless ANSICON is installed, or it's Windows 10
- if (getenv("ANSICON") != NULL)
- dumpopts |= JV_PRINT_COLOR;
- else {
- DWORD mode;
- HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
- if (GetConsoleMode(con, &mode) &&
- SetConsoleMode(con, mode | 4/*ENABLE_VIRTUAL_TERMINAL_PROCESSING*/))
- dumpopts |= JV_PRINT_COLOR;
+ /* Verify we actually have the console, as the NUL device is also regarded as
+ tty. Windows can handle color if ANSICON (or ConEmu) is installed, or
+ Windows 10 supports the virtual terminal */
+ DWORD mode;
+ HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
+ if (GetConsoleMode(con, &mode)) {
+ dumpopts |= JV_PRINT_ISATTY;
+ if (getenv("ANSICON") != NULL ||
+ SetConsoleMode(con, mode | 4/*ENABLE_VIRTUAL_TERMINAL_PROCESSING*/))
+ dumpopts |= JV_PRINT_COLOR;
}
#endif
}