aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-04-06 10:43:56 +0100
committerRon Yorston <rmy@pobox.com>2019-04-06 10:43:56 +0100
commit379a8b313923db1f4892a3dd0d8d923bc6683d8a (patch)
tree024ac839501554c0ca34084f704f6192f0b332c3
parent65ae5b24cc08f898e81b36421b616fc7fc25d2b1 (diff)
downloadbusybox-w32-379a8b313923db1f4892a3dd0d8d923bc6683d8a.tar.gz
busybox-w32-379a8b313923db1f4892a3dd0d8d923bc6683d8a.tar.bz2
busybox-w32-379a8b313923db1f4892a3dd0d8d923bc6683d8a.zip
winansi: code shrink
Use INVALID_HANDLE_VALUE (not NULL) to indicate that the console and console_in handles haven't been initialised. Replace many explicit comparisons against INVALID_HANDLE_VALUE with tests on the return value of GetConsoleScreenBufferInfo() which fails for an invalid handle.
-rw-r--r--win32/winansi.c63
1 files changed, 17 insertions, 46 deletions
diff --git a/win32/winansi.c b/win32/winansi.c
index 098b57c5d..a2c49b7e7 100644
--- a/win32/winansi.c
+++ b/win32/winansi.c
@@ -21,15 +21,8 @@
21#undef read 21#undef read
22#undef getc 22#undef getc
23 23
24/* 24static HANDLE console = INVALID_HANDLE_VALUE;
25 ANSI codes used by git: m, K 25static HANDLE console_in = INVALID_HANDLE_VALUE;
26
27 This file is git-specific. Therefore, this file does not attempt
28 to implement any codes that are not used by git.
29*/
30
31static HANDLE console;
32static HANDLE console_in;
33static WORD plain_attr; 26static WORD plain_attr;
34static WORD attr; 27static WORD attr;
35static int negative; 28static int negative;
@@ -38,38 +31,28 @@ static void init(void)
38{ 31{
39 CONSOLE_SCREEN_BUFFER_INFO sbi; 32 CONSOLE_SCREEN_BUFFER_INFO sbi;
40 33
41 static int initialized = 0; 34 if (console != INVALID_HANDLE_VALUE || console_in != INVALID_HANDLE_VALUE)
42 if (initialized)
43 return; 35 return;
44 36
45 console_in = GetStdHandle(STD_INPUT_HANDLE);
46 if (console_in == INVALID_HANDLE_VALUE)
47 console_in = NULL;
48
49 console = GetStdHandle(STD_OUTPUT_HANDLE); 37 console = GetStdHandle(STD_OUTPUT_HANDLE);
50 if (console == INVALID_HANDLE_VALUE) 38 console_in = GetStdHandle(STD_INPUT_HANDLE);
51 console = NULL;
52
53 if (!console)
54 return;
55
56 GetConsoleScreenBufferInfo(console, &sbi);
57 attr = plain_attr = sbi.wAttributes;
58 negative = 0;
59 39
60 initialized = 1; 40 if (GetConsoleScreenBufferInfo(console, &sbi)) {
41 attr = plain_attr = sbi.wAttributes;
42 negative = 0;
43 }
61} 44}
62 45
63static int is_console(int fd) 46static int is_console(int fd)
64{ 47{
65 init(); 48 init();
66 return isatty(fd) && console; 49 return isatty(fd) && console != INVALID_HANDLE_VALUE;
67} 50}
68 51
69static int is_console_in(int fd) 52static int is_console_in(int fd)
70{ 53{
71 init(); 54 init();
72 return isatty(fd) && console_in; 55 return isatty(fd) && console_in != INVALID_HANDLE_VALUE;
73} 56}
74 57
75static int skip_ansi_emulation(void) 58static int skip_ansi_emulation(void)
@@ -126,10 +109,8 @@ static void erase_in_line(void)
126 CONSOLE_SCREEN_BUFFER_INFO sbi; 109 CONSOLE_SCREEN_BUFFER_INFO sbi;
127 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */ 110 DWORD dummy; /* Needed for Windows 7 (or Vista) regression */
128 111
129 if (!console) 112 if (!GetConsoleScreenBufferInfo(console, &sbi))
130 return; 113 return;
131
132 GetConsoleScreenBufferInfo(console, &sbi);
133 FillConsoleOutputCharacterA(console, ' ', 114 FillConsoleOutputCharacterA(console, ' ',
134 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition, 115 sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition,
135 &dummy); 116 &dummy);
@@ -143,10 +124,8 @@ static void erase_till_end_of_screen(void)
143 CONSOLE_SCREEN_BUFFER_INFO sbi; 124 CONSOLE_SCREEN_BUFFER_INFO sbi;
144 DWORD dummy, len; 125 DWORD dummy, len;
145 126
146 if (!console) 127 if(!GetConsoleScreenBufferInfo(console, &sbi))
147 return; 128 return;
148
149 GetConsoleScreenBufferInfo(console, &sbi);
150 len = sbi.dwSize.X - sbi.dwCursorPosition.X + 129 len = sbi.dwSize.X - sbi.dwCursorPosition.X +
151 sbi.dwSize.X * (sbi.srWindow.Bottom - sbi.dwCursorPosition.Y); 130 sbi.dwSize.X * (sbi.srWindow.Bottom - sbi.dwCursorPosition.Y);
152 131
@@ -162,11 +141,9 @@ void reset_screen(void)
162 COORD pos; 141 COORD pos;
163 DWORD dummy, len; 142 DWORD dummy, len;
164 143
165 if (!console)
166 return;
167
168 /* move to start of screen buffer and clear it all */ 144 /* move to start of screen buffer and clear it all */
169 GetConsoleScreenBufferInfo(console, &sbi); 145 if (!GetConsoleScreenBufferInfo(console, &sbi))
146 return;
170 pos.X = 0; 147 pos.X = 0;
171 pos.Y = 0; 148 pos.Y = 0;
172 SetConsoleCursorPosition(console, pos); 149 SetConsoleCursorPosition(console, pos);
@@ -179,10 +156,8 @@ void move_cursor_row(int n)
179{ 156{
180 CONSOLE_SCREEN_BUFFER_INFO sbi; 157 CONSOLE_SCREEN_BUFFER_INFO sbi;
181 158
182 if (!console) 159 if(!GetConsoleScreenBufferInfo(console, &sbi))
183 return; 160 return;
184
185 GetConsoleScreenBufferInfo(console, &sbi);
186 sbi.dwCursorPosition.Y += n; 161 sbi.dwCursorPosition.Y += n;
187 SetConsoleCursorPosition(console, sbi.dwCursorPosition); 162 SetConsoleCursorPosition(console, sbi.dwCursorPosition);
188} 163}
@@ -191,10 +166,8 @@ static void move_cursor_column(int n)
191{ 166{
192 CONSOLE_SCREEN_BUFFER_INFO sbi; 167 CONSOLE_SCREEN_BUFFER_INFO sbi;
193 168
194 if (!console) 169 if (!GetConsoleScreenBufferInfo(console, &sbi))
195 return; 170 return;
196
197 GetConsoleScreenBufferInfo(console, &sbi);
198 sbi.dwCursorPosition.X += n; 171 sbi.dwCursorPosition.X += n;
199 SetConsoleCursorPosition(console, sbi.dwCursorPosition); 172 SetConsoleCursorPosition(console, sbi.dwCursorPosition);
200} 173}
@@ -204,10 +177,8 @@ static void move_cursor(int x, int y)
204 COORD pos; 177 COORD pos;
205 CONSOLE_SCREEN_BUFFER_INFO sbi; 178 CONSOLE_SCREEN_BUFFER_INFO sbi;
206 179
207 if (!console) 180 if (!GetConsoleScreenBufferInfo(console, &sbi))
208 return; 181 return;
209
210 GetConsoleScreenBufferInfo(console, &sbi);
211 pos.X = sbi.srWindow.Left + x; 182 pos.X = sbi.srWindow.Left + x;
212 pos.Y = sbi.srWindow.Top + y; 183 pos.Y = sbi.srWindow.Top + y;
213 SetConsoleCursorPosition(console, pos); 184 SetConsoleCursorPosition(console, pos);