diff --git a/.gitignore b/.gitignore index d73e005..7c512db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .cache -build \ No newline at end of file +build +.vscode +.vs \ No newline at end of file diff --git a/main.c b/main.c index 80f33fa..2ea9509 100644 --- a/main.c +++ b/main.c @@ -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; } @@ -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);