Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.cache
build
build
.vscode
.vs
71 changes: 35 additions & 36 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,54 +63,50 @@ int main(int argc, const char* argv[]) {

DWORD consoleMode = 0;
GetConsoleMode(ctx.stdOut, &consoleMode);
hr = SetConsoleMode(ctx.stdOut, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
? S_OK
: GetLastError();
if (S_OK == hr) {
HPCON hpcon = INVALID_HANDLE_VALUE;

hr = CreatePseudoConsoleAndPipes(&hpcon, &ctx);
if (S_OK == hr) {
HANDLE pipeListener = (HANDLE) _beginthread(PipeListener, 0, &ctx);
SetConsoleMode(ctx.stdOut, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

STARTUPINFOEXA startupInfo = {0};
if (S_OK == InitializeStartupInfoAttachedToPseudoConsole(&startupInfo, hpcon)) {
PROCESS_INFORMATION cmdProc;
hr = CreateProcessA(NULL, (char*) ctx.args.cmd, NULL, NULL, FALSE,
EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
&startupInfo.StartupInfo, &cmdProc)
? S_OK
: GetLastError();
HPCON hpcon = INVALID_HANDLE_VALUE;

if (S_OK == hr) {
ctx.events[1] = cmdProc.hThread;
hr = CreatePseudoConsoleAndPipes(&hpcon, &ctx);
if (S_OK == hr) {
HANDLE pipeListener = (HANDLE) _beginthread(PipeListener, 0, &ctx);

HANDLE inputHandler = (HANDLE) _beginthread(InputHandlerThread, 0, &ctx);
STARTUPINFOEXA startupInfo = {0};
if (S_OK == InitializeStartupInfoAttachedToPseudoConsole(&startupInfo, hpcon)) {
PROCESS_INFORMATION cmdProc;
hr = CreateProcessA(NULL, (char*) ctx.args.cmd, NULL, NULL, FALSE,
EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
&startupInfo.StartupInfo, &cmdProc)
? S_OK
: GetLastError();

WaitForMultipleObjects(sizeof(ctx.events) / sizeof(HANDLE), ctx.events, FALSE,
INFINITE);
}
if (S_OK == hr) {
ctx.events[1] = cmdProc.hThread;

CloseHandle(cmdProc.hThread);
CloseHandle(cmdProc.hProcess);
HANDLE inputHandler = (HANDLE) _beginthread(InputHandlerThread, 0, &ctx);

DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
free(startupInfo.lpAttributeList);
WaitForMultipleObjects(sizeof(ctx.events) / sizeof(HANDLE), ctx.events, FALSE,
INFINITE);
}

ClosePseudoConsole(hpcon);
CloseHandle(cmdProc.hThread);
CloseHandle(cmdProc.hProcess);

if (INVALID_HANDLE_VALUE != ctx.pipeOut) {
CloseHandle(ctx.pipeOut);
}
if (INVALID_HANDLE_VALUE != ctx.pipeIn) {
CloseHandle(ctx.pipeIn);
}
DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
free(startupInfo.lpAttributeList);
}

ClosePseudoConsole(hpcon);

CloseHandle(ctx.events[0]);
if (INVALID_HANDLE_VALUE != ctx.pipeOut) {
CloseHandle(ctx.pipeOut);
}
if (INVALID_HANDLE_VALUE != ctx.pipeIn) {
CloseHandle(ctx.pipeIn);
}
}

CloseHandle(ctx.events[0]);
}
return S_OK == hr ? EXIT_SUCCESS : EXIT_FAILURE;
}

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

Expand Down