From e239b66691200163c1983b645c28b34825f32ea4 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Thu, 18 Feb 2021 12:23:28 -0500 Subject: :build working on windows --- build.c | 17 +++++++++++------ process-posix.c | 2 +- process-win.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/build.c b/build.c index e0cf2be..60cd70f 100644 --- a/build.c +++ b/build.c @@ -45,17 +45,22 @@ static void build_start(Ted *ted) { if (cargo) { program = "cargo"; argv[0] = "build"; + } else if (fs_file_exists("make.bat")) { + program = "make.bat"; } else { program = "make"; } #endif - process_exec(&ted->build_process, program, argv); - ted->building = true; - ted->build_shown = true; - buffer_new_file(&ted->build_buffer, NULL); - ted->build_buffer.store_undo_events = false; // don't need undo events for build output buffer - ted->build_buffer.view_only = true; + if (process_exec(&ted->build_process, program, argv)) { + ted->building = true; + ted->build_shown = true; + buffer_new_file(&ted->build_buffer, NULL); + ted->build_buffer.store_undo_events = false; // don't need undo events for build output buffer + ted->build_buffer.view_only = true; + } else { + ted_seterr("Couldn't start build: %s", process_geterr(&ted->build_process)); + } } static void build_stop(Ted *ted) { diff --git a/process-posix.c b/process-posix.c index 9be7e10..6434f6e 100644 --- a/process-posix.c +++ b/process-posix.c @@ -43,7 +43,7 @@ bool process_exec(Process *proc, char const *program, char **argv) { } char const *process_geterr(Process *p) { - return p->error; + return *p->error ? p->error : NULL; } long long process_read(Process *proc, char *data, size_t size) { diff --git a/process-win.c b/process-win.c index a2f97d4..95f3fbe 100644 --- a/process-win.c +++ b/process-win.c @@ -3,6 +3,7 @@ struct Process { HANDLE pipe_read, pipe_write; PROCESS_INFORMATION process_info; + char error[64]; }; bool process_exec(Process *process, char const *program, char **argv) { @@ -32,31 +33,66 @@ bool process_exec(Process *process, char const *program, char **argv) { process->pipe_read = pipe_read; process->pipe_write = pipe_write; success = true; + } else { + strbuf_printf(process->error, "Couldn't create process (error code %u)", (unsigned)GetLastError()); } if (!success) { CloseHandle(pipe_read); CloseHandle(pipe_write); } + } else { + strbuf_printf(process->error, "Couldn't create pipe (error code %u)", (unsigned)GetLastError()); } return success; } +char const *process_geterr(Process *p) { + return *p->error ? p->error : NULL; +} + long long process_read(Process *process, char *data, size_t size) { DWORD bytes_read = 0, bytes_avail = 0, bytes_left = 0; if (PeekNamedPipe(process->pipe_read, data, (DWORD)size, &bytes_read, &bytes_avail, &bytes_left)) { - if (bytes_read == 0) + if (bytes_read == 0) { return -1; - else + } else { + ReadFile(process->pipe_read, data, (DWORD)size, &bytes_read, NULL); // make sure data is removed from pipe return bytes_read; + } } else { - return -2; + strbuf_printf(process->error, "Couldn't read from pipe (error code %u)", (unsigned)GetLastError()); + return -2; } } void process_kill(Process *process) { - // @TODO: kill process + TerminateProcess(process->process_info.hProcess, 1); CloseHandle(process->pipe_read); CloseHandle(process->pipe_write); CloseHandle(process->process_info.hProcess); CloseHandle(process->process_info.hThread); +} + +int process_check_status(Process *process, char *message, size_t message_size) { + HANDLE hProcess = process->process_info.hProcess; + DWORD exit_code = 1; + if (GetExitCodeProcess(hProcess, &exit_code)) { + if (exit_code == STILL_ACTIVE) { + return 0; + } else { + process_kill(process); + if (exit_code == 0) { + str_printf(message, message_size, "exited successfully"); + return +1; + } else { + str_printf(message, message_size, "exited with code %d", (int)exit_code); + return -1; + } + } + } else { + // something has gone wrong. + str_printf(message, message_size, "couldn't get process exit status"); + process_kill(process); + return -1; + } } \ No newline at end of file -- cgit v1.2.3