2018-03-20 21:03:28 +08:00
|
|
|
#include "command.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-03-20 21:03:28 +08:00
|
|
|
#include "log.h"
|
|
|
|
#include "strutil.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
|
|
|
HANDLE cmd_execute(const char *path, const char *const argv[]) {
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
|
|
|
|
// Windows command-line parsing is WTF:
|
|
|
|
// <http://daviddeley.com/autohotkey/parameters/parameters.htm#WINPASS>
|
|
|
|
// only make it work for this very specific program
|
|
|
|
// (don't handle escaping nor quotes)
|
|
|
|
char cmd[256];
|
|
|
|
size_t ret = xstrjoin(cmd, argv, ' ', sizeof(cmd));
|
|
|
|
if (ret >= sizeof(cmd)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGE("Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1);
|
2017-12-12 22:12:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-16 05:42:37 +08:00
|
|
|
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
|
2017-12-12 22:12:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pi.hProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool cmd_terminate(HANDLE handle) {
|
2018-02-12 23:35:23 +08:00
|
|
|
return TerminateProcess(handle, 1) && CloseHandle(handle);
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool cmd_simple_wait(HANDLE handle, DWORD *exit_code) {
|
|
|
|
DWORD code;
|
|
|
|
if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0 || !GetExitCodeProcess(handle, &code)) {
|
|
|
|
// cannot wait or retrieve the exit code
|
|
|
|
code = -1; // max value, it's unsigned
|
|
|
|
}
|
|
|
|
if (exit_code) {
|
|
|
|
*exit_code = code;
|
|
|
|
}
|
|
|
|
return !code;
|
|
|
|
}
|