Skip to content

Commit 76b44ae

Browse files
authored
Merge pull request #7 from lxb007981/main
Fix Output Redirection Issue
2 parents e45af17 + d9fe2ab commit 76b44ae

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.cache
2-
build
2+
build
3+
.vscode
4+
.vs

main.c

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,54 +63,50 @@ int main(int argc, const char* argv[]) {
6363

6464
DWORD consoleMode = 0;
6565
GetConsoleMode(ctx.stdOut, &consoleMode);
66-
hr = SetConsoleMode(ctx.stdOut, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
67-
? S_OK
68-
: GetLastError();
69-
if (S_OK == hr) {
70-
HPCON hpcon = INVALID_HANDLE_VALUE;
71-
72-
hr = CreatePseudoConsoleAndPipes(&hpcon, &ctx);
73-
if (S_OK == hr) {
74-
HANDLE pipeListener = (HANDLE) _beginthread(PipeListener, 0, &ctx);
66+
SetConsoleMode(ctx.stdOut, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7567

76-
STARTUPINFOEXA startupInfo = {0};
77-
if (S_OK == InitializeStartupInfoAttachedToPseudoConsole(&startupInfo, hpcon)) {
78-
PROCESS_INFORMATION cmdProc;
79-
hr = CreateProcessA(NULL, (char*) ctx.args.cmd, NULL, NULL, FALSE,
80-
EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
81-
&startupInfo.StartupInfo, &cmdProc)
82-
? S_OK
83-
: GetLastError();
68+
HPCON hpcon = INVALID_HANDLE_VALUE;
8469

85-
if (S_OK == hr) {
86-
ctx.events[1] = cmdProc.hThread;
70+
hr = CreatePseudoConsoleAndPipes(&hpcon, &ctx);
71+
if (S_OK == hr) {
72+
HANDLE pipeListener = (HANDLE) _beginthread(PipeListener, 0, &ctx);
8773

88-
HANDLE inputHandler = (HANDLE) _beginthread(InputHandlerThread, 0, &ctx);
74+
STARTUPINFOEXA startupInfo = {0};
75+
if (S_OK == InitializeStartupInfoAttachedToPseudoConsole(&startupInfo, hpcon)) {
76+
PROCESS_INFORMATION cmdProc;
77+
hr = CreateProcessA(NULL, (char*) ctx.args.cmd, NULL, NULL, FALSE,
78+
EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
79+
&startupInfo.StartupInfo, &cmdProc)
80+
? S_OK
81+
: GetLastError();
8982

90-
WaitForMultipleObjects(sizeof(ctx.events) / sizeof(HANDLE), ctx.events, FALSE,
91-
INFINITE);
92-
}
83+
if (S_OK == hr) {
84+
ctx.events[1] = cmdProc.hThread;
9385

94-
CloseHandle(cmdProc.hThread);
95-
CloseHandle(cmdProc.hProcess);
86+
HANDLE inputHandler = (HANDLE) _beginthread(InputHandlerThread, 0, &ctx);
9687

97-
DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
98-
free(startupInfo.lpAttributeList);
88+
WaitForMultipleObjects(sizeof(ctx.events) / sizeof(HANDLE), ctx.events, FALSE,
89+
INFINITE);
9990
}
10091

101-
ClosePseudoConsole(hpcon);
92+
CloseHandle(cmdProc.hThread);
93+
CloseHandle(cmdProc.hProcess);
10294

103-
if (INVALID_HANDLE_VALUE != ctx.pipeOut) {
104-
CloseHandle(ctx.pipeOut);
105-
}
106-
if (INVALID_HANDLE_VALUE != ctx.pipeIn) {
107-
CloseHandle(ctx.pipeIn);
108-
}
95+
DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
96+
free(startupInfo.lpAttributeList);
97+
}
98+
99+
ClosePseudoConsole(hpcon);
109100

110-
CloseHandle(ctx.events[0]);
101+
if (INVALID_HANDLE_VALUE != ctx.pipeOut) {
102+
CloseHandle(ctx.pipeOut);
103+
}
104+
if (INVALID_HANDLE_VALUE != ctx.pipeIn) {
105+
CloseHandle(ctx.pipeIn);
111106
}
112-
}
113107

108+
CloseHandle(ctx.events[0]);
109+
}
114110
return S_OK == hr ? EXIT_SUCCESS : EXIT_FAILURE;
115111
}
116112

@@ -203,6 +199,9 @@ static HRESULT CreatePseudoConsoleAndPipes(HPCON* hpcon, Context* ctx) {
203199
if (GetConsoleScreenBufferInfo(hConsole, &csbi)) {
204200
consoleSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
205201
consoleSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
202+
} else {
203+
consoleSize.X = 120;
204+
consoleSize.Y = 25;
206205
}
207206
hr = CreatePseudoConsole(consoleSize, pipePtyIn, pipePtyOut, 0, hpcon);
208207

0 commit comments

Comments
 (0)