diff options
| author | Igor Dreher <igor.dreher@gmail.com> | 2022-07-31 21:42:51 -0300 |
|---|---|---|
| committer | Igor Dreher <igor.dreher@gmail.com> | 2022-07-31 21:50:12 -0300 |
| commit | 5c94e68ebb7c217be2d5dd74fc484a3b3ac9467a (patch) | |
| tree | fd0c15ef5110077d327285a3acba3be1664ae854 /src/common | |
| parent | 7da73a562eb017708ac89ec6658db982fc941689 (diff) | |
Implement platform independent `run_executable`
This should make diagnostics work on other platforms besides Windows.
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/util.odin | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/common/util.odin b/src/common/util.odin index 6e31563..b28154e 100644 --- a/src/common/util.odin +++ b/src/common/util.odin @@ -4,6 +4,9 @@ import "core:fmt" import "core:os" import "core:strings" import "core:path/filepath" +foreign import libc "system:c" +import "core:mem" +import "core:bytes" when ODIN_OS == .Windows { delimiter :: ";" @@ -35,3 +38,35 @@ lookup_in_path :: proc(name: string) -> (string, bool) { return "", false } + +run_executable :: proc(command: string, stdout: ^[]byte) -> (u32, bool, []byte) { + fp := popen(strings.clone_to_cstring(command, context.temp_allocator), "r") + if fp == nil { + return 0, false, stdout[0:] + } + defer pclose(fp) + + read_buffer: [50]byte + index: int + + for fgets(&read_buffer[0], size_of(read_buffer), fp) != nil { + read := bytes.index_byte(read_buffer[:], 0) + defer index += cast(int)read + + if read > 0 && index + cast(int)read <= len(stdout) { + mem.copy(&stdout[index], &read_buffer[0], cast(int)read) + } + } + + return 0, true, stdout[0:index] +} + +foreign libc +{ + popen :: proc(command: cstring, type: cstring) -> ^FILE --- + pclose :: proc(stream: ^FILE) -> i32 --- + fgets :: proc "cdecl" (s: [^]byte, n: i32, stream: ^FILE) -> [^]u8 --- + fgetc :: proc "cdecl" (stream: ^FILE) -> i32 --- +} + +FILE :: struct {} |