aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2020-04-13 13:28:24 +0100
committerRon Yorston <rmy@pobox.com>2020-04-13 13:35:57 +0100
commitc2f7974901118ff4ec004da1e71a7386b900a505 (patch)
tree4ba730efd71ff68dbea8f6232690690994f5d4f6
parent026bf90b29314486d64f93c0633dffda8ac58379 (diff)
downloadbusybox-w32-c2f7974901118ff4ec004da1e71a7386b900a505.tar.gz
busybox-w32-c2f7974901118ff4ec004da1e71a7386b900a505.tar.bz2
busybox-w32-c2f7974901118ff4ec004da1e71a7386b900a505.zip
winansi: more fine-grained control of ANSI emulation
Change the meaning of the BB_SKIP_ANSI_EMULATION variable: - if it isn't set or if it has any value other than 1 or 2, use ANSI emulation; - if it's set to 1, always emit ANSI escape codes; - if it's set to 2, attempt to enable virtual terminal processing in the current console. If that works emit ANSI escape codes, if not revert to ANSI emulation. On all platforms I've tested (Windows 10, Windows 8.1, ReactOS 0.4.13, plus ConEmu) BB_SKIP_ANSI_EMULATION=2 does the right thing.
-rw-r--r--win32/winansi.c36
1 files changed, 34 insertions, 2 deletions
diff --git a/win32/winansi.c b/win32/winansi.c
index c4c7ee87c..dd99b42af 100644
--- a/win32/winansi.c
+++ b/win32/winansi.c
@@ -53,12 +53,44 @@ static int is_console_in(int fd)
53 return isatty(fd) && GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE; 53 return isatty(fd) && GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE;
54} 54}
55 55
56#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
57#define DISABLE_NEWLINE_AUTO_RETURN 0x0008
58
56int skip_ansi_emulation(int reset) 59int skip_ansi_emulation(int reset)
57{ 60{
58 static int skip = -1; 61 static int skip = -1;
59 62
60 if (skip < 0 || reset) 63 if (skip < 0 || reset) {
61 skip = getenv(bb_skip_ansi_emulation) != NULL; 64 const char *var = getenv(bb_skip_ansi_emulation);
65 skip = var != NULL;
66 if (skip) {
67 switch (xatou(var)) {
68 case 1:
69 break;
70 case 2:
71 skip = 2;
72 break;
73 default:
74 skip = 0;
75 break;
76 }
77 }
78
79 if (is_console(STDOUT_FILENO)) {
80 HANDLE h = get_console();
81 DWORD mode;
82
83 if (GetConsoleMode(h, &mode)) {
84 if (skip)
85 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
86 else
87 mode &= ~ENABLE_VIRTUAL_TERMINAL_PROCESSING;
88 mode &= ~DISABLE_NEWLINE_AUTO_RETURN;
89 if (!SetConsoleMode(h, mode) && skip == 2)
90 skip = 0;
91 }
92 }
93 }
62 94
63 return skip; 95 return skip;
64} 96}