
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // Running programs (std/command, ADR 0085): no shell, nothing inherited, |
| 2 | // bounded output, a timeout that sends SIGTERM (the runtime blocks it in |
| 3 | // its own threads; the child must not inherit that), then SIGKILL. |
| 4 | |
| 5 | import std/bytes |
| 6 | import std/command. |
| 7 | {type Command, type Output, Command, Exited, Found, Scratch, TimedOut} |
| 8 | import std/io |
| 9 | import std/list |
| 10 | import std/string |
| 11 | import std/time.{type clock} |
| 12 | |
| 13 | fn text :: b: Bytes -> String |
| 14 | { |
| 15 | string.trim (bytes.to_string_lossy b) |
| 16 | } |
| 17 | |
| 18 | fn shown :: r: Result<Output, command.Error> -> String |
| 19 | { |
| 20 | match r |
| 21 | { Ok(out) -> show out.exit <> " [" <> text out.stdout <> "]" |
| 22 | , Error(e) -> "error " <> show e |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn sh :: script: String -> Command |
| 27 | { |
| 28 | command.new "/bin/sh" ["-c", script] |
| 29 | } |
| 30 | |
| 31 | pub fn main :: -> () / <io, clock> |
| 32 | { |
| 33 | io.println |
| 34 | ( shown |
| 35 | ( command.run |
| 36 | ( command.new |
| 37 | "/usr/bin/printf" |
| 38 | ["%s|", "a b", "$HOME", "x;y", "'q'"] |
| 39 | ) |
| 40 | ) |
| 41 | ) |
| 42 | io.println |
| 43 | ( shown |
| 44 | ( command.run |
| 45 | (Command(..command.new "/usr/bin/env" [], env: [("ONLY", "this")])) |
| 46 | ) |
| 47 | ) |
| 48 | io.println |
| 49 | ( shown |
| 50 | ( command.run |
| 51 | ( Command( ..command.new "printf" ["found"] |
| 52 | , env: [("PATH", "/usr/bin:/bin")] |
| 53 | ) |
| 54 | ) |
| 55 | ) |
| 56 | ) |
| 57 | match command.run (command.new "printf" ["x"]) |
| 58 | { Error(command.NotFound(_)) -> io.println "no PATH: not found" |
| 59 | , other -> io.println ("unexpected: " <> shown other) |
| 60 | } |
| 61 | match command.run (command.new "/usr/bin/printf" ["a\0b"]) |
| 62 | { Error(command.BadArgument(_)) -> io.println "NUL: bad argument" |
| 63 | , other -> io.println ("unexpected: " <> shown other) |
| 64 | } |
| 65 | io.println (shown (command.run (sh "exit 3"))) |
| 66 | io.println (shown (command.run (sh "kill -9 $$"))) |
| 67 | let echoed = |
| 68 | command.run |
| 69 | ( Command( ..command.new "/bin/cat" [] |
| 70 | , stdin: bytes.from_string "round trip" |
| 71 | ) |
| 72 | ) |
| 73 | io.println (shown echoed) |
| 74 | match command.run |
| 75 | ( Command( ..command.new "/usr/bin/head" ["-c", "300000", "/dev/zero"] |
| 76 | , max_stdout: 1000 |
| 77 | ) |
| 78 | ) |
| 79 | { Ok(out) -> |
| 80 | io.println |
| 81 | ( show out.exit <> " " <> show (bytes.length out.stdout) <> " cut " |
| 82 | <> show out.stdout_cut |
| 83 | ) |
| 84 | , Error(e) -> io.println ("error " <> show e) |
| 85 | } |
| 86 | let started = time.monotonic () |
| 87 | match command.run |
| 88 | (Command(..command.new "/bin/sleep" ["10"], timeout_ms: 100)) |
| 89 | { Ok(out) -> |
| 90 | io.println |
| 91 | ( show out.exit <> " quick " |
| 92 | <> show |
| 93 | ( time.in_milliseconds (time.elapsed (time.monotonic ()) started) |
| 94 | < 1500 |
| 95 | ) |
| 96 | ) |
| 97 | , Error(e) -> io.println ("error " <> show e) |
| 98 | } |
| 99 | io.println |
| 100 | ( shown |
| 101 | ( command.run |
| 102 | ( Command( ..sh "trap '' TERM; sleep 10" |
| 103 | , timeout_ms: 100 |
| 104 | , grace_ms: 100 |
| 105 | ) |
| 106 | ) |
| 107 | ) |
| 108 | ) |
| 109 | let scratch = |
| 110 | Scratch([("in", bytes.from_string "hello")], ["out", "missing"], 100) |
| 111 | match command.run (Command(..sh "cat in > out", workdir: scratch)) |
| 112 | { Ok(out) -> io.println (show (list.map out.files fn f { describe f })) |
| 113 | , Error(e) -> io.println ("error " <> show e) |
| 114 | } |
| 115 | match command.run (Command(..sh "true", workdir: Scratch([], ["../x"], 1))) |
| 116 | { Error(command.BadArgument(_)) -> io.println "../x: bad argument" |
| 117 | , other -> io.println ("unexpected: " <> shown other) |
| 118 | } |
| 119 | let faked = |
| 120 | command.scripted |
| 121 | fn _ |
| 122 | { |
| 123 | Ok( command.Output( Exited(0) |
| 124 | , bytes.from_string "fake" |
| 125 | , bytes.empty () |
| 126 | , False |
| 127 | , False |
| 128 | , [] |
| 129 | ) |
| 130 | ) |
| 131 | } |
| 132 | fn { command.run_command (command.new "/nowhere" []) } |
| 133 | io.println (shown faked) |
| 134 | } |
| 135 | |
| 136 | fn describe :: f: command.Collected -> String |
| 137 | { |
| 138 | match f |
| 139 | { Found(name, data) -> name <> "=" <> text data |
| 140 | , command.TooLarge(name, size) -> name <> " too large " <> show size |
| 141 | , command.Absent(name) -> name <> " absent" |
| 142 | } |
| 143 | } |
| 144 | //> Exited(0) [a b|$HOME|x;y|'q'|] |
| 145 | //> Exited(0) [ONLY=this] |
| 146 | //> Exited(0) [found] |
| 147 | //> no PATH: not found |
| 148 | //> NUL: bad argument |
| 149 | //> Exited(3) [] |
| 150 | //> Signalled(9) [] |
| 151 | //> Exited(0) [round trip] |
| 152 | //> Exited(0) 1000 cut True |
| 153 | //> TimedOut(15) quick True |
| 154 | //> TimedOut(9) [] |
| 155 | //> [out=hello, missing absent] |
| 156 | //> ../x: bad argument |
| 157 | //> Exited(0) [fake] |