From 14811a586d77ab9a1fa8bef360250992aead55d1 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 17 Feb 2021 18:12:33 -0500 Subject: started process-win.c --- build.c | 15 ++++++++++---- filesystem-win.c | 8 +++++++- main.c | 5 ----- process-win.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ process.h | 2 +- util.c | 8 ++++++++ 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 process-win.c diff --git a/build.c b/build.c index e9d9ddc..e0cf2be 100644 --- a/build.c +++ b/build.c @@ -14,17 +14,17 @@ static void build_start(Ted *ted) { bool cargo = false; - chdir(ted->cwd); + change_directory(ted->cwd); strcpy(ted->build_dir, ted->cwd); if (fs_file_exists("Cargo.toml")) { cargo = true; } else if (fs_file_exists(".." PATH_SEPARATOR_STR "Cargo.toml")) { - chdir(".."); + change_directory(".."); ted_full_path(ted, "..", ted->build_dir, sizeof ted->build_dir); cargo = true; } else if (fs_file_exists(".." PATH_SEPARATOR_STR ".." PATH_SEPARATOR_STR "Cargo.toml")) { - chdir(".." PATH_SEPARATOR_STR ".."); + change_directory(".." PATH_SEPARATOR_STR ".."); ted_full_path(ted, "../..", ted->build_dir, sizeof ted->build_dir); cargo = true; } @@ -40,7 +40,14 @@ static void build_start(Ted *ted) { argv[2] = "make"; } #else - #error "TODO" + char *program = NULL; + char *argv[2] = {NULL, NULL}; + if (cargo) { + program = "cargo"; + argv[0] = "build"; + } else { + program = "make"; + } #endif process_exec(&ted->build_process, program, argv); diff --git a/filesystem-win.c b/filesystem-win.c index 8d7a95a..c6c64dd 100644 --- a/filesystem-win.c +++ b/filesystem-win.c @@ -1,6 +1,7 @@ #include "filesystem.h" #include #include +#include FsType fs_path_type(char const *path) { struct _stat statbuf = {0}; @@ -13,7 +14,12 @@ FsType fs_path_type(char const *path) { return FS_OTHER; } -#error "TODO: fs_path_permission" +FsPermission fs_path_permission(char const *path) { + FsPermission permission = 0; + if (_access(path, 04) == 0) permission |= FS_PERMISSION_READ; + if (_access(path, 02) == 0) permission |= FS_PERMISSION_WRITE; + return permission; +} bool fs_file_exists(char const *path) { return fs_path_type(path) == FS_FILE; diff --git a/main.c b/main.c index 98d0f3a..878afbe 100644 --- a/main.c +++ b/main.c @@ -118,11 +118,6 @@ static void ted_update_window_dimensions(Ted *ted) { gl_window_height = ted->window_height = (float)h; } -void chld_handler(int x, siginfo_t *y, void *z) { - (void)x; (void)y; (void)z; - printf("got singal!\n"); -} - #if _WIN32 INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) { diff --git a/process-win.c b/process-win.c new file mode 100644 index 0000000..a2f97d4 --- /dev/null +++ b/process-win.c @@ -0,0 +1,62 @@ +#include "process.h" + +struct Process { + HANDLE pipe_read, pipe_write; + PROCESS_INFORMATION process_info; +}; + +bool process_exec(Process *process, char const *program, char **argv) { + // thanks to https://stackoverflow.com/a/35658917 + bool success = false; + memset(process, 0, sizeof *process); + HANDLE pipe_read, pipe_write; + SECURITY_ATTRIBUTES security_attrs = {sizeof(SECURITY_ATTRIBUTES)}; + security_attrs.bInheritHandle = TRUE; + if (CreatePipe(&pipe_read, &pipe_write, &security_attrs, 0)) { + STARTUPINFOA startup = {sizeof(STARTUPINFOA)}; + startup.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; + startup.hStdOutput = pipe_write; + startup.hStdError = pipe_write; + startup.wShowWindow = SW_HIDE; + + // fuckin windows + char command_line[4096]; + strbuf_cpy(command_line, program); + strbuf_catf(command_line, " "); + for (int i = 0; argv[i]; ++i) { + strbuf_catf(command_line, "%s ", argv[i]); + } + + if (CreateProcessA(NULL, command_line, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, + NULL, NULL, &startup, &process->process_info)) { + process->pipe_read = pipe_read; + process->pipe_write = pipe_write; + success = true; + } + if (!success) { + CloseHandle(pipe_read); + CloseHandle(pipe_write); + } + } + return success; +} + +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) + return -1; + else + return bytes_read; + } else { + return -2; + } +} + +void process_kill(Process *process) { + // @TODO: kill process + CloseHandle(process->pipe_read); + CloseHandle(process->pipe_write); + CloseHandle(process->process_info.hProcess); + CloseHandle(process->process_info.hThread); +} \ No newline at end of file diff --git a/process.h b/process.h index 023145c..2e91598 100644 --- a/process.h +++ b/process.h @@ -4,7 +4,7 @@ typedef struct Process Process; -// returns NULL on failure +// returns false on failure bool process_exec(Process *process, char const *program, char **argv); // returns the error last error produced, or NULL if there was no error. char const *process_geterr(Process *process); diff --git a/util.c b/util.c index 24c6971..2872c37 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,6 @@ #if _WIN32 #include +#include #endif static u8 util_popcount(u64 x) { @@ -274,3 +275,10 @@ static void path_full(char const *dir, char const *relpath, char *abspath, size_ } } +static void change_directory(char const *path) { +#if _WIN32 + _chdir(path); +#else + chdir(path); +#endif +} \ No newline at end of file -- cgit v1.2.3