// Running programs (std/command, ADR 0085): no shell, nothing inherited, // bounded output, a timeout that sends SIGTERM (the runtime blocks it in // its own threads; the child must not inherit that), then SIGKILL. import std/bytes import std/command. {type Command, type Output, Command, Exited, Found, Scratch, TimedOut} import std/io import std/list import std/string import std/time.{type clock} fn text :: b: Bytes -> String { string.trim (bytes.to_string_lossy b) } fn shown :: r: Result -> String { match r { Ok(out) -> show out.exit <> " [" <> text out.stdout <> "]" , Error(e) -> "error " <> show e } } fn sh :: script: String -> Command { command.new "/bin/sh" ["-c", script] } pub fn main :: -> () / { io.println ( shown ( command.run ( command.new "/usr/bin/printf" ["%s|", "a b", "$HOME", "x;y", "'q'"] ) ) ) io.println ( shown ( command.run (Command(..command.new "/usr/bin/env" [], env: [("ONLY", "this")])) ) ) io.println ( shown ( command.run ( Command( ..command.new "printf" ["found"] , env: [("PATH", "/usr/bin:/bin")] ) ) ) ) match command.run (command.new "printf" ["x"]) { Error(command.NotFound(_)) -> io.println "no PATH: not found" , other -> io.println ("unexpected: " <> shown other) } match command.run (command.new "/usr/bin/printf" ["a\0b"]) { Error(command.BadArgument(_)) -> io.println "NUL: bad argument" , other -> io.println ("unexpected: " <> shown other) } io.println (shown (command.run (sh "exit 3"))) io.println (shown (command.run (sh "kill -9 $$"))) let echoed = command.run ( Command( ..command.new "/bin/cat" [] , stdin: bytes.from_string "round trip" ) ) io.println (shown echoed) match command.run ( Command( ..command.new "/usr/bin/head" ["-c", "300000", "/dev/zero"] , max_stdout: 1000 ) ) { Ok(out) -> io.println ( show out.exit <> " " <> show (bytes.length out.stdout) <> " cut " <> show out.stdout_cut ) , Error(e) -> io.println ("error " <> show e) } let started = time.monotonic () match command.run (Command(..command.new "/bin/sleep" ["10"], timeout_ms: 100)) { Ok(out) -> io.println ( show out.exit <> " quick " <> show ( time.in_milliseconds (time.elapsed (time.monotonic ()) started) < 1500 ) ) , Error(e) -> io.println ("error " <> show e) } io.println ( shown ( command.run ( Command( ..sh "trap '' TERM; sleep 10" , timeout_ms: 100 , grace_ms: 100 ) ) ) ) let scratch = Scratch([("in", bytes.from_string "hello")], ["out", "missing"], 100) match command.run (Command(..sh "cat in > out", workdir: scratch)) { Ok(out) -> io.println (show (list.map out.files fn f { describe f })) , Error(e) -> io.println ("error " <> show e) } match command.run (Command(..sh "true", workdir: Scratch([], ["../x"], 1))) { Error(command.BadArgument(_)) -> io.println "../x: bad argument" , other -> io.println ("unexpected: " <> shown other) } let faked = command.scripted fn _ { Ok( command.Output( Exited(0) , bytes.from_string "fake" , bytes.empty () , False , False , [] ) ) } fn { command.run_command (command.new "/nowhere" []) } io.println (shown faked) } fn describe :: f: command.Collected -> String { match f { Found(name, data) -> name <> "=" <> text data , command.TooLarge(name, size) -> name <> " too large " <> show size , command.Absent(name) -> name <> " absent" } } //> Exited(0) [a b|$HOME|x;y|'q'|] //> Exited(0) [ONLY=this] //> Exited(0) [found] //> no PATH: not found //> NUL: bad argument //> Exited(3) [] //> Signalled(9) [] //> Exited(0) [round trip] //> Exited(0) 1000 cut True //> TimedOut(15) quick True //> TimedOut(9) [] //> [out=hello, missing absent] //> ../x: bad argument //> Exited(0) [fake]