Repository navigation
Expand file tree
/
Copy pathwin32_cli_wrapper.cpp
More file actions
327 lines (289 loc) · 11.1 KB
/
Copy pathwin32_cli_wrapper.cpp
File metadata and controls
327 lines (289 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
/** @file cli/win32_cli_wrapper.cpp
*
* Windows is stupid with regards to consoles:
* - a program is either 'GUI' or 'console'
* - gui programs do not inherit the console of the parent
* - hence, there is NO way to printf from a gui program to an existing console
*
* This wrapper hacks around that by opening pipes for std{in,our,err}.
* The gui program (MSE) can then use stdout as usual, while this program passes
* the text to the real stdout.
*
* In addition, this wrapping allows us to translate a (subset of) ANSI escape codes
* so we can use fancy colors.
*
*/
#ifdef WIN32 // only needed on windows (duh)
// ----------------------------------------------------------------------------- : Includes
#define UNICODE
#define _UNICODE
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
// ----------------------------------------------------------------------------- : Prototypes
/// How to transfer data from one handle to another
struct Transfer {
HANDLE from, &to;
bool escapes;
};
DWORD WINAPI TransferThread(Transfer*);
BOOL WINAPI HandleCtrlEvent(DWORD type);
void CopyFileBuffer(HANDLE output, char* buffer, DWORD size);
void InitEscapeTranslation(HANDLE console);
void FinishEscapeTranslation(HANDLE console);
void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]);
/// The child process
PROCESS_INFORMATION child_process_info;
/// Pipes / console handles
HANDLE in_mine, in_theirs, in_real;
HANDLE out_mine, out_theirs, out_real;
HANDLE err_mine, err_theirs, err_real;
bool need_redirection;
// ----------------------------------------------------------------------------- : Main function
const char* redirect_flags[] = {"-?","/?","--help","-v","--version","--cli","-c","--export","--export-images","--create-installer"};
int main(int argc, char** argv) {
// determine whether we need to wrap console i/o
need_redirection = false;
for (int i = 1 ; i < argc ; ++i) {
for (int j = 0 ; j < sizeof(redirect_flags)/sizeof(redirect_flags[0]) ; ++j) {
if (strcmp(argv[i],redirect_flags[j]) == 0) {
need_redirection = true;
goto break_2;
}
}
}
break_2:
// command line
TCHAR* command_line = GetCommandLine();
if (need_redirection) {
// update command line : add --color flag
TCHAR* my_command_line = command_line;
command_line = new TCHAR[_tcsclen(command_line) + 10];
_tcscpy(command_line, my_command_line);
_tcscat(command_line, _T(" --color"));
}
// application name
TCHAR app_path[2048];
GetModuleFileName(NULL/*current process*/, app_path, sizeof(app_path)/sizeof(TCHAR));
size_t app_path_length = _tcsclen(app_path);
if (app_path_length > 4 && _tcsicmp(app_path + app_path_length - 4, _T(".com")) == 0) {
// replace ".com" with ".exe"
_tcscpy(app_path + app_path_length - 4, _T(".exe"));
} else {
// not a .com file, error message
fprintf(stderr, "This executable should be named <something>.com\n");
}
// win32 structures for child program
STARTUPINFO child_startup_info;
memset(&child_startup_info, 0, sizeof(child_startup_info));
memset(&child_process_info, 0, sizeof(child_process_info));
child_startup_info.cb = sizeof(child_startup_info);
// setup redirection
if (need_redirection) {
// Ctrl+C handler
SetConsoleCtrlHandler(HandleCtrlEvent, TRUE);
// create pipes
CreatePipe(&in_theirs, &in_mine, NULL, 0);
CreatePipe(&out_mine, &out_theirs, NULL, 0);
CreatePipe(&err_mine, &err_theirs, NULL, 0);
// the actual handles
in_real = GetStdHandle(STD_INPUT_HANDLE);
out_real = GetStdHandle(STD_OUTPUT_HANDLE);
err_real = GetStdHandle(STD_ERROR_HANDLE);
InitEscapeTranslation(out_real);
InitEscapeTranslation(err_real);
// start threads
Transfer tranfer_in = {in_real, in_mine, false};
Transfer tranfer_out = {out_mine, out_real, true};
Transfer tranfer_err = {err_mine, err_real, true};
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)TransferThread,&tranfer_in, 0,NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)TransferThread,&tranfer_out,0,NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)TransferThread,&tranfer_err,0,NULL);
// give (inheritable copies of) handles to child process
HANDLE me = GetCurrentProcess();
DuplicateHandle(me,in_theirs, me,&child_startup_info.hStdInput, 0,TRUE,DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
DuplicateHandle(me,out_theirs,me,&child_startup_info.hStdOutput,0,TRUE,DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
DuplicateHandle(me,err_theirs,me,&child_startup_info.hStdError, 0,TRUE,DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
child_startup_info.dwFlags = STARTF_USESTDHANDLES;
}
// start the child program
if (!CreateProcess(app_path,command_line,NULL,NULL,TRUE,0,NULL,NULL,&child_startup_info,&child_process_info)) {
fprintf(stderr, "Unable to start child process.\n");
ExitProcess(1);
}
// wait for program to terminate
DWORD exit_code = 0;
if (need_redirection) {
delete [] command_line;
WaitForSingleObject(child_process_info.hProcess, INFINITE);
GetExitCodeProcess(child_process_info.hProcess, &exit_code);
}
// That's all folks!
if (need_redirection) {
FinishEscapeTranslation(out_real);
FinishEscapeTranslation(err_real);
}
return exit_code;
}
// ----------------------------------------------------------------------------- : Terminating
/// Handle Ctrl+C
BOOL WINAPI HandleCtrlEvent(DWORD type) {
DWORD exit_code = 1;
// try to exit child process cleanly
// TODO: don't exit child on Ctrl+C
bool wait = false;
if (in_mine != INVALID_HANDLE_VALUE) {
CopyFileBuffer(in_mine,":quit\n",6);
CopyFileBuffer(out_real,":quit\n",6);
wait = true;
}
if (wait && WaitForSingleObject(child_process_info.hProcess,100) == WAIT_OBJECT_0) {
GetExitCodeProcess(child_process_info.hProcess, &exit_code);
} else {
TerminateProcess(child_process_info.hProcess,1);
}
// exit this process
if (need_redirection) {
FinishEscapeTranslation(out_real);
FinishEscapeTranslation(err_real);
}
ExitProcess(exit_code);
return TRUE;
}
// ----------------------------------------------------------------------------- : I/O redirection
/// Copy a buffer to an output handle
void CopyFileBuffer(HANDLE output, char* buffer, DWORD size) {
DWORD pos = 0, bytes_written;
while (pos < size) {
WriteFile(output, buffer + pos, size - pos, &bytes_written, NULL);
pos += bytes_written;
}
}
/// Copy a buffer to an output handle, handling escape code
void CopyFileBufferWithEscape(HANDLE output, char* buffer, DWORD size, bool handle_escapes) {
if (!handle_escapes) {
CopyFileBuffer(output, buffer, size);
return;
}
DWORD pos = 0;
while (pos < size) {
// find next escape code, "\x1B["
DWORD next_pos = pos;
while (next_pos < size &&
(buffer[next_pos] != '\x1B' ||
(next_pos + 1 >= size || buffer[next_pos+1] != '['))) ++next_pos;
// copy part before next escape
CopyFileBuffer(output, buffer+pos, next_pos-pos);
pos = next_pos;
// is this an escape code?
if (pos + 1 < size) {
// handle escape code
int argv[10] = {0}, argc = 1;
for (pos += 2 ; pos < size ; ++pos) {
if (buffer[pos] == ';') {
++argc;
} else if (buffer[pos] >= '0' && buffer[pos] <= '9') {
argv[argc-1] = 10 * argv[argc-1] + buffer[pos] - '0';
} else {
PerformEscapeCode(output, buffer[pos], argc, argv);
pos++;
break;
}
}
}
}
}
/// Thread to transfer text from one handle to another
DWORD WINAPI TransferThread(Transfer* transfer) {
while (true) {
// read
char buffer[1024];
DWORD bytes_read;
BOOL ok = ReadFile(transfer->from, buffer, sizeof(buffer), &bytes_read, NULL);
if (ok && bytes_read == 0) {
// end of file: close handle
CloseHandle(transfer->to);
transfer->to = INVALID_HANDLE_VALUE;
break;
}
// write
CopyFileBufferWithEscape(transfer->to, buffer, bytes_read, transfer->escapes);
}
return 0;
}
// ----------------------------------------------------------------------------- : Escape codes
#define FOREGROUND_COLOR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define BACKGROUND_COLOR (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
WORD original_attributes;
/// Initialization for escape translation
void InitEscapeTranslation(HANDLE console) {
CONSOLE_SCREEN_BUFFER_INFO screen_buffer;
GetConsoleScreenBufferInfo(console, &screen_buffer);
original_attributes = screen_buffer.wAttributes;
}
/// Cleanup for escape translation: put things back to normal
void FinishEscapeTranslation(HANDLE console) {
SetConsoleTextAttribute(console, original_attributes);
}
/// Perform an escape code
void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]) {
switch (command) {
case 'm': {
CONSOLE_SCREEN_BUFFER_INFO screen_buffer;
GetConsoleScreenBufferInfo(console, &screen_buffer);
WORD attributes = screen_buffer.wAttributes;
for (int i = 0 ; i < argc ; ++i) {
int major = argv[i] / 10, minor = argv[i] % 10;
if (argv[i] == 0) { // reset
attributes = original_attributes;
} else if (argv[i] == 1) { // bold
attributes |= FOREGROUND_INTENSITY;
} else if (argv[i] == 7) { // reverse
#if BACKGROUND_RED != FOREGROUND_RED << 4
#error Color codes are not what I expected them to be
#endif
attributes = (attributes & (FOREGROUND_COLOR | FOREGROUND_INTENSITY)) << 4
| (attributes & (BACKGROUND_COLOR | BACKGROUND_INTENSITY)) >> 4;
} else if (argv[i] == 21 || argv[i] == 22) { // not bold
attributes &= ~FOREGROUND_INTENSITY;
} else if (major == 3) {
// foreground color
attributes &= ~FOREGROUND_COLOR;
if (minor == 9) { // reset
attributes |= original_attributes & FOREGROUND_COLOR;
} else {
attributes |= (minor & 1 ? FOREGROUND_RED : 0)
| (minor & 2 ? FOREGROUND_GREEN : 0)
| (minor & 4 ? FOREGROUND_BLUE : 0);
}
} else if (major == 4) {
// background color
attributes &= ~BACKGROUND_COLOR;
if (minor == 9) { // reset
attributes |= original_attributes & BACKGROUND_COLOR;
} else {
attributes |= (minor & 1 ? BACKGROUND_RED : 0)
| (minor & 2 ? BACKGROUND_GREEN : 0)
| (minor & 4 ? BACKGROUND_BLUE : 0);
}
} else {
// other, ignore
}
}
if (attributes != screen_buffer.wAttributes) {
SetConsoleTextAttribute(console, attributes);
}
break;
}
default:
break; // unsupported command, ignore
}
}
// ----------------------------------------------------------------------------- : EOF
#endif // WIN32