From aa0e54906b703c139a362e781a82ec1af9c35510 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Sun, 7 May 2017 09:17:37 -0400 Subject: [PATCH] main, win: create test runner in windows --- include/mini/common.h | 2 ++ src/spawn-win.c | 70 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/include/mini/common.h b/include/mini/common.h index 3f52667..8942c73 100644 --- a/include/mini/common.h +++ b/include/mini/common.h @@ -6,6 +6,8 @@ #include #include +#define BUFSIZE 4096 + #define CHECK(VALUE, MESSAGE) \ do { \ if ((VALUE)) break; \ diff --git a/src/spawn-win.c b/src/spawn-win.c index 9f3239f..49e73d6 100644 --- a/src/spawn-win.c +++ b/src/spawn-win.c @@ -1,10 +1,76 @@ +#include +#include +#include +#include +#include + #include "include/mini/common.h" +static char executable_path[BUFSIZE]; + +int popen_complete(char* cmd) +{ + STARTUPINFO StartupInfo; + ZeroMemory(&StartupInfo, sizeof(StartupInfo)); + StartupInfo.cb = sizeof(StartupInfo); + + PROCESS_INFORMATION ProcessInformation; + ZeroMemory(&ProcessInformation, sizeof(ProcessInformation)); + + // BOOL WINAPI CreateProcess( + // _In_opt_ LPCTSTR lpApplicationName, + // _Inout_opt_ LPTSTR lpCommandLine, + // _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, + // _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, + // _In_ BOOL bInheritHandles, + // _In_ DWORD dwCreationFlags, + // _In_opt_ LPVOID lpEnvironment, + // _In_opt_ LPCTSTR lpCurrentDirectory, + // _In_ LPSTARTUPINFO lpStartupInfo, + // Out_ LPPROCESS_INFORMATION lpProcessInformation + // ); + BOOL ret = CreateProcess( + NULL, // No module name (use command line) + cmd, // Command line + NULL, // Process handle not inheritable + NULL, // Thread handle not inheritable + FALSE, // Set handle inheritance to FALSE + NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, + NULL, // Use parent's environment block + NULL, // Use parent's starting directory + &StartupInfo, // Pointer to STARTUPINFO structure + &ProcessInformation // Pointer to PROCESS_INFORMATION structure + ); + CHECK_EQ(ret, TRUE, "CreateProcess failed"); + + // Wait until child process exits. + DWORD wait_ret = WaitForSingleObject(ProcessInformation.hProcess, INFINITE); + CHECK_EQ(wait_ret, WAIT_OBJECT_0, "Process ended strange"); + + // Get exit code (-1 is good) + DWORD lpExitCode = 0; + BOOL exit_ret = GetExitCodeProcess(ProcessInformation.hProcess, &lpExitCode); + + // Close process and thread handles. + CloseHandle(ProcessInformation.hProcess); + CloseHandle(ProcessInformation.hThread); + + return lpExitCode; +} + + void mini_prepare_runner(const char* main) { - CHECK(0, "implement me"); + TCHAR** lppPart = { NULL }; + GetFullPathName(main, BUFSIZE, executable_path, lppPart); + CHECK_NE(executable_path, NULL, "realpath(argv[0])"); } int mini_run_single(const char* test) { - CHECK(0, "implement me"); + const char cmd[BUFSIZE]; + int print_len = sprintf_s(cmd, BUFSIZE, "\"%s\" %s", executable_path, test); + CHECK_NE(print_len, -1, "sprintf_s to format cmd failed"); + int err = popen_complete(cmd); + CHECK_NE(err, -1, "popen() failure"); + return 0; }