From 43a6d458a519d6498abb6e5d8717ba7a86e1176d Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Sun, 26 May 2024 16:14:50 +0200 Subject: [PATCH] try out os::exec and pipe --- backend/cmd/proctest/main.ha | 35 +++++++++++++++++++++++++++++++++++ backend/run.sh | 9 +++++++++ 2 files changed, 44 insertions(+) create mode 100644 backend/cmd/proctest/main.ha create mode 100755 backend/run.sh diff --git a/backend/cmd/proctest/main.ha b/backend/cmd/proctest/main.ha new file mode 100644 index 0000000..8e48cc1 --- /dev/null +++ b/backend/cmd/proctest/main.ha @@ -0,0 +1,35 @@ +use fmt; +use io; +use os::exec; +use os; +use strings; + +export fn main() void = { + let cmd = exec::cmd("./run.sh")!; + + let stdout_pipe = exec::pipe(); + exec::addfile(&cmd, os::stdout_file, stdout_pipe.1); + + let stderr_pipe = exec::pipe(); + exec::addfile(&cmd, os::stderr_file, stderr_pipe.1); + + let proc = exec::start(&cmd)!; + io::close(stdout_pipe.1)!; + io::close(stderr_pipe.1)!; + + let stdout_data = io::drain(stdout_pipe.0)!; + io::close(stdout_pipe.0)!; + + let stderr_data = io::drain(stderr_pipe.0)!; + io::close(stderr_pipe.0)!; + + let status = exec::wait(&proc)!; + let s = exec::exit(&status) as int; + fmt::printfln("status: {}", s)!; + + let out = strings::fromutf8(stdout_data) as str; + fmt::printfln("stdout:\n{}", out)!; + + let out = strings::fromutf8(stderr_data) as str; + fmt::printfln("stderr:\n{}", out)!; +}; diff --git a/backend/run.sh b/backend/run.sh new file mode 100755 index 0000000..a2d74db --- /dev/null +++ b/backend/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +echo "first line" +echo "error 1" >&2 +echo "second line" +echo "error 2" >&2 +echo "third line" + +exit 17