From 80df86df3df946bbdd3cbd46813193ffb4b1b4da Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 7 Feb 2026 11:08:29 +0100 Subject: [PATCH] Stabilize process execution and editor cleanup --- src/basic/basic_os.cpp | 8 ++-- src/basic/basic_os.h | 3 +- src/text_editor/data_desc.cpp | 1 + .../plugin_search_open_buffers.cpp | 2 +- src/text_editor/process.cpp | 47 ++++++++++--------- src/text_editor/ui.cpp | 3 +- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/basic/basic_os.cpp b/src/basic/basic_os.cpp index 672d5ed..0b8adb1 100644 --- a/src/basic/basic_os.cpp +++ b/src/basic/basic_os.cpp @@ -726,8 +726,8 @@ API double GetTimeSeconds() { return GetTimeMicros() / 1000000.0; } -API String WriteTempFile(String data) { - Scratch scratch; +API String WriteTempFile(Allocator allocator, String data) { + Scratch scratch(allocator); #if OS_WINDOWS int buffer_len = MAX_PATH+1; @@ -737,12 +737,12 @@ API String WriteTempFile(String data) { Assert(result != 0); String16 temp16 = {buffer, result}; NormalizePathInPlace(temp16); - String temp_directory = ToString(scratch, temp16); + String temp_directory = ToString(allocator, temp16); #else String temp_directory = "/tmp"; #endif - String temp_filename = Format(scratch, "%S/temp%llu", temp_directory, GetTimeNanos()); + String temp_filename = Format(allocator, "%S/temp%llu", temp_directory, GetTimeNanos()); bool done = WriteFile(temp_filename, data); Assert(done); return temp_filename; diff --git a/src/basic/basic_os.h b/src/basic/basic_os.h index d3d8137..26711ba 100644 --- a/src/basic/basic_os.h +++ b/src/basic/basic_os.h @@ -32,6 +32,7 @@ bool IsFile(String path); String GetWorkingDir(Allocator arena); bool IsAbsolute(String path); int64_t GetFileModTime(String file); +String WriteTempFile(Allocator allocator, String data); @@ -45,4 +46,4 @@ enum MakeDirResult { MakeDirResult_ErrorOther, }; -MakeDirResult MakeDir(String path); \ No newline at end of file +MakeDirResult MakeDir(String path); diff --git a/src/text_editor/data_desc.cpp b/src/text_editor/data_desc.cpp index c6fd6e8..f662791 100644 --- a/src/text_editor/data_desc.cpp +++ b/src/text_editor/data_desc.cpp @@ -118,6 +118,7 @@ void LexDigit(Lexer *lex, Token *t) { } bool IsOkForIdent(Lexer *lex, char c) { + Unused(lex); bool result = IsAlphanumeric(c) || c == '_' || c == '/'; return result; } diff --git a/src/text_editor/plugin_search_open_buffers.cpp b/src/text_editor/plugin_search_open_buffers.cpp index ec93b26..1a6f9f3 100644 --- a/src/text_editor/plugin_search_open_buffers.cpp +++ b/src/text_editor/plugin_search_open_buffers.cpp @@ -70,7 +70,7 @@ void Coro_SearchOpenBuffers(mco_coro *co) { if (out_buffer == NULL) { return; } - Buffer *it = GetBuffer(id, NULL); + it = GetBuffer(id, NULL); if (it == NULL) { goto skip_buffer; } diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index 98cb8db..7aaeebc 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -233,22 +233,23 @@ Array SplitCommand(Allocator allocator, String command_line) { return cmd; } -Process SpawnProcess(String command_line, String working_dir, String write_stdin, Array enviroment) { +Process SpawnProcess(ExecArgs args) { Scratch scratch; const int PIPE_READ = 0; const int PIPE_WRITE = 1; bool error = false; - working_dir = Copy(scratch, working_dir); + String working_dir = Copy(scratch, args.cwd); chdir(working_dir.data); Process process = {}; process.args = args; UnixProcess *plat = (UnixProcess *)&process.platform; - Array args = SplitCommand(scratch, command_line); + Array args_cmd = SplitCommand(scratch, args.cmd); Array env = {scratch}; + char *exe = Copy(scratch, args.exe).data; - For (enviroment) { + For (args.env) { Add(&env, Copy(scratch, it).data); } @@ -308,20 +309,22 @@ Process SpawnProcess(String command_line, String working_dir, String write_stdin return process; } - error = posix_spawn_file_actions_addclose(&actions, stdin_desc[PIPE_WRITE]) != 0; - if (error) { - Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno)); - return process; - } + if (args.open_stdin) { + error = posix_spawn_file_actions_addclose(&actions, stdin_desc[PIPE_WRITE]) != 0; + if (error) { + Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno)); + return process; + } - error = posix_spawn_file_actions_adddup2(&actions, stdin_desc[PIPE_READ], STDIN_FILENO) != 0; - if (error) { - Error("Libc function failed: posix_spawn_file_actions_adddup2 STDIN_FILENO, with error: %s", strerror(errno)); - return process; + error = posix_spawn_file_actions_adddup2(&actions, stdin_desc[PIPE_READ], STDIN_FILENO) != 0; + if (error) { + Error("Libc function failed: posix_spawn_file_actions_adddup2 STDIN_FILENO, with error: %s", strerror(errno)); + return process; + } } pid_t process_pid = 0; - error = posix_spawnp(&process_pid, args[0], &actions, NULL, args.data, env.data) != 0; + error = posix_spawnp(&process_pid, exe, &actions, NULL, args_cmd.data, env.data) != 0; if (error) { Error("Libc function failed: failed to create process\n, with error: %s", strerror(errno)); return process; @@ -329,12 +332,9 @@ Process SpawnProcess(String command_line, String working_dir, String write_stdin plat->child_stdout_read = stdout_desc[PIPE_READ]; - plat->stdin_write = stdin_desc[PIPE_WRITE]; plat->pid = process_pid; - - if (write_stdin.len) { - WriteStdin(&process, write_stdin); - CloseStdin(&process); + if (args.open_stdin) { + plat->stdin_write = stdin_desc[PIPE_WRITE]; } process.id = process_pid; @@ -431,8 +431,13 @@ String FindPython(Allocator allocator) { for (int i = 0; i < Lengthof(tries); i += 1) { For (paths) { String path_it = Format(scratch, "%S/%S" IF_OS_WINDOWS_ELSE(".exe", ""), it, tries[i]); +#if OS_WINDOWS bool is_bad_bad_ms = EndsWith(it, "AppData\\Local\\Microsoft\\WindowsApps") || EndsWith(it, "AppData/Local/Microsoft/WindowsApps"); - if (FileExists(path_it) IF_OS_WINDOWS(&& !is_bad_bad_ms)) { + if (is_bad_bad_ms) { + continue; + } +#endif + if (FileExists(path_it)) { return Copy(allocator, path_it); } } @@ -446,7 +451,7 @@ void SetShell(Allocator allocator, String *exe, String *cmd, String in_cmd) { *cmd = Format(allocator, "%S /C %S", *exe, in_cmd); #else *exe = "/usr/bin/bash"; - String temp_file = WriteTempFile(in_cmd); // @todo: maybe try to pass by stdio here + String temp_file = WriteTempFile(allocator, in_cmd); // @todo: maybe try to pass by stdio here *cmd = Format(allocator, "%S %S", *exe, temp_file); #endif } diff --git a/src/text_editor/ui.cpp b/src/text_editor/ui.cpp index 87067bc..49baadd 100644 --- a/src/text_editor/ui.cpp +++ b/src/text_editor/ui.cpp @@ -294,7 +294,6 @@ void TestInsertVariable() { String a = "Thing/@(ProjectFolder)/Another"; String b = "Thing/@ProjectFolder/Another"; Assert(InsertVariables(scratch, a) == InsertVariables(scratch, b)); - int c = 10; } RegisterFunction(&TestFunctions, TestInsertVariable); @@ -545,7 +544,7 @@ BSet Open(Window *window, String path, ResolveOpenMeta meta, bool set_active = t if (o.use_python_shell == 1) { args.exe = FindPython(scratch); - String temp_file = WriteTempFile(o.path); + String temp_file = WriteTempFile(scratch, o.path); args.cmd = Format(scratch, "%S %S", args.exe, temp_file); } else { SetShell(scratch, &args.exe, &args.cmd, o.path);