diff options
author | Ron Yorston <rmy@pobox.com> | 2020-01-08 09:42:49 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2020-01-08 09:42:49 +0000 |
commit | b0b7ab792bc1f45963f4b84b94faaf05054e1613 (patch) | |
tree | f0cf356438a7ac9f78bdfd1918ee5fc45c8b543f | |
parent | 98b794afbc510633066f32c6d91356078c229dbe (diff) | |
download | busybox-w32-b0b7ab792bc1f45963f4b84b94faaf05054e1613.tar.gz busybox-w32-b0b7ab792bc1f45963f4b84b94faaf05054e1613.tar.bz2 busybox-w32-b0b7ab792bc1f45963f4b84b94faaf05054e1613.zip |
winansi: code shrink
Rework the ANSI emulation code to reduce its size.
- Fetch console attributes when required rather than caching them.
The init() function is no longer required; the only remaining
initialisation is now performed in is_console().
- Turning off inverse video (ESC[27m) didn't work properly. This has
been improved though it still doesn't work in some unlikely cases
(ESC[7;27m).
These changes save 180 bytes.
-rw-r--r-- | win32/winansi.c | 68 |
1 files changed, 31 insertions, 37 deletions
diff --git a/win32/winansi.c b/win32/winansi.c index 0d8cf662a..41ca3ccca 100644 --- a/win32/winansi.c +++ b/win32/winansi.c | |||
@@ -21,41 +21,35 @@ | |||
21 | #undef read | 21 | #undef read |
22 | #undef getc | 22 | #undef getc |
23 | 23 | ||
24 | static WORD plain_attr; | 24 | #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) |
25 | static WORD attr; | 25 | #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) |
26 | static int negative; | 26 | |
27 | static WORD plain_attr = 0; | ||
27 | 28 | ||
28 | static HANDLE get_console(void) | 29 | static HANDLE get_console(void) |
29 | { | 30 | { |
30 | return GetStdHandle(STD_OUTPUT_HANDLE); | 31 | return GetStdHandle(STD_OUTPUT_HANDLE); |
31 | } | 32 | } |
32 | 33 | ||
33 | static void init(void) | 34 | static WORD get_console_attr(void) |
34 | { | 35 | { |
35 | static int initialised = FALSE; | ||
36 | HANDLE console; | ||
37 | CONSOLE_SCREEN_BUFFER_INFO sbi; | 36 | CONSOLE_SCREEN_BUFFER_INFO sbi; |
38 | 37 | ||
39 | if (initialised) | 38 | if (GetConsoleScreenBufferInfo(get_console(), &sbi)) |
40 | return; | 39 | return sbi.wAttributes; |
41 | initialised = TRUE; | ||
42 | 40 | ||
43 | console = get_console(); | 41 | return FOREGROUND_ALL; |
44 | if (GetConsoleScreenBufferInfo(console, &sbi)) { | ||
45 | attr = plain_attr = sbi.wAttributes; | ||
46 | negative = 0; | ||
47 | } | ||
48 | } | 42 | } |
49 | 43 | ||
50 | static int is_console(int fd) | 44 | static int is_console(int fd) |
51 | { | 45 | { |
52 | init(); | 46 | if (!plain_attr) |
47 | plain_attr = get_console_attr(); | ||
53 | return isatty(fd) && get_console() != INVALID_HANDLE_VALUE; | 48 | return isatty(fd) && get_console() != INVALID_HANDLE_VALUE; |
54 | } | 49 | } |
55 | 50 | ||
56 | static int is_console_in(int fd) | 51 | static int is_console_in(int fd) |
57 | { | 52 | { |
58 | init(); | ||
59 | return isatty(fd) && GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE; | 53 | return isatty(fd) && GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE; |
60 | } | 54 | } |
61 | 55 | ||
@@ -91,11 +85,8 @@ static HANDLE dup_handle(HANDLE h) | |||
91 | static void use_alt_buffer(int flag) | 85 | static void use_alt_buffer(int flag) |
92 | { | 86 | { |
93 | static HANDLE console_orig = INVALID_HANDLE_VALUE; | 87 | static HANDLE console_orig = INVALID_HANDLE_VALUE; |
94 | CONSOLE_SCREEN_BUFFER_INFO sbi; | ||
95 | HANDLE console, h; | 88 | HANDLE console, h; |
96 | 89 | ||
97 | init(); | ||
98 | |||
99 | console = get_console(); | 90 | console = get_console(); |
100 | console_orig = dup_handle(console); | 91 | console_orig = dup_handle(console); |
101 | if (console_orig == INVALID_HANDLE_VALUE) | 92 | if (console_orig == INVALID_HANDLE_VALUE) |
@@ -103,6 +94,7 @@ static void use_alt_buffer(int flag) | |||
103 | 94 | ||
104 | if (flag) { | 95 | if (flag) { |
105 | SECURITY_ATTRIBUTES sa; | 96 | SECURITY_ATTRIBUTES sa; |
97 | CONSOLE_SCREEN_BUFFER_INFO sbi; | ||
106 | 98 | ||
107 | // handle should be inheritable | 99 | // handle should be inheritable |
108 | memset(&sa, 0, sizeof(sa)); | 100 | memset(&sa, 0, sizeof(sa)); |
@@ -134,31 +126,28 @@ static void use_alt_buffer(int flag) | |||
134 | _open_osfhandle((intptr_t)console, O_RDWR|O_BINARY); | 126 | _open_osfhandle((intptr_t)console, O_RDWR|O_BINARY); |
135 | } | 127 | } |
136 | 128 | ||
137 | #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) | 129 | static void set_console_attr(WORD attributes, int invert) |
138 | #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) | ||
139 | |||
140 | static void set_console_attr(void) | ||
141 | { | 130 | { |
142 | HANDLE console = get_console(); | 131 | HANDLE console = get_console(); |
143 | WORD attributes = attr; | 132 | if (invert) { |
144 | if (negative) { | 133 | WORD save = attributes; |
145 | attributes &= ~FOREGROUND_ALL; | 134 | attributes &= ~FOREGROUND_ALL; |
146 | attributes &= ~BACKGROUND_ALL; | 135 | attributes &= ~BACKGROUND_ALL; |
147 | 136 | ||
148 | /* This could probably use a bitmask | 137 | /* This could probably use a bitmask |
149 | instead of a series of ifs */ | 138 | instead of a series of ifs */ |
150 | if (attr & FOREGROUND_RED) | 139 | if (save & FOREGROUND_RED) |
151 | attributes |= BACKGROUND_RED; | 140 | attributes |= BACKGROUND_RED; |
152 | if (attr & FOREGROUND_GREEN) | 141 | if (save & FOREGROUND_GREEN) |
153 | attributes |= BACKGROUND_GREEN; | 142 | attributes |= BACKGROUND_GREEN; |
154 | if (attr & FOREGROUND_BLUE) | 143 | if (save & FOREGROUND_BLUE) |
155 | attributes |= BACKGROUND_BLUE; | 144 | attributes |= BACKGROUND_BLUE; |
156 | 145 | ||
157 | if (attr & BACKGROUND_RED) | 146 | if (save & BACKGROUND_RED) |
158 | attributes |= FOREGROUND_RED; | 147 | attributes |= FOREGROUND_RED; |
159 | if (attr & BACKGROUND_GREEN) | 148 | if (save & BACKGROUND_GREEN) |
160 | attributes |= FOREGROUND_GREEN; | 149 | attributes |= FOREGROUND_GREEN; |
161 | if (attr & BACKGROUND_BLUE) | 150 | if (save & BACKGROUND_BLUE) |
162 | attributes |= FOREGROUND_BLUE; | 151 | attributes |= FOREGROUND_BLUE; |
163 | } | 152 | } |
164 | SetConsoleTextAttribute(console, attributes); | 153 | SetConsoleTextAttribute(console, attributes); |
@@ -252,6 +241,9 @@ static char *process_escape(char *pos) | |||
252 | const char *str, *func; | 241 | const char *str, *func; |
253 | char *bel; | 242 | char *bel; |
254 | size_t len; | 243 | size_t len; |
244 | WORD attr = get_console_attr(); | ||
245 | int invert = FALSE; | ||
246 | static int inverse = 0; | ||
255 | 247 | ||
256 | switch (pos[1]) { | 248 | switch (pos[1]) { |
257 | case '[': | 249 | case '[': |
@@ -281,7 +273,7 @@ static char *process_escape(char *pos) | |||
281 | switch (val) { | 273 | switch (val) { |
282 | case 0: /* reset */ | 274 | case 0: /* reset */ |
283 | attr = plain_attr; | 275 | attr = plain_attr; |
284 | negative = 0; | 276 | inverse = 0; |
285 | break; | 277 | break; |
286 | case 1: /* bold */ | 278 | case 1: /* bold */ |
287 | attr |= FOREGROUND_INTENSITY; | 279 | attr |= FOREGROUND_INTENSITY; |
@@ -311,11 +303,13 @@ static char *process_escape(char *pos) | |||
311 | case 25: /* no blink */ | 303 | case 25: /* no blink */ |
312 | attr &= ~BACKGROUND_INTENSITY; | 304 | attr &= ~BACKGROUND_INTENSITY; |
313 | break; | 305 | break; |
314 | case 7: /* negative */ | 306 | case 7: /* inverse on */ |
315 | negative = 1; | 307 | invert = !inverse; |
308 | inverse = 1; | ||
316 | break; | 309 | break; |
317 | case 27: /* positive */ | 310 | case 27: /* inverse off */ |
318 | negative = 0; | 311 | invert = inverse; |
312 | inverse = 0; | ||
319 | break; | 313 | break; |
320 | case 8: /* conceal */ | 314 | case 8: /* conceal */ |
321 | case 28: /* reveal */ | 315 | case 28: /* reveal */ |
@@ -404,7 +398,7 @@ static char *process_escape(char *pos) | |||
404 | str++; | 398 | str++; |
405 | } while (*(str-1) == ';'); | 399 | } while (*(str-1) == ';'); |
406 | 400 | ||
407 | set_console_attr(); | 401 | set_console_attr(attr, invert); |
408 | break; | 402 | break; |
409 | case 'A': /* up */ | 403 | case 'A': /* up */ |
410 | move_cursor_row(-strtol(str, (char **)&str, 10)); | 404 | move_cursor_row(-strtol(str, (char **)&str, 10)); |