1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# Functions for running system commands
use ./commands.c
extern run_command:func(exe:Text, args:List(Text), env:Table(Text,Text), input:List(Byte), output:&List(Byte), error:&List(Byte) -> Int32)
enum ExitType(Exited(status:Int32), Signaled(signal:Int32), Failed)
struct ProgramResult(stdout:List(Byte), stderr:List(Byte), exit_type:ExitType):
func or_fail(r:ProgramResult -> ProgramResult):
when r.exit_type is Exited(status):
if status == 0:
return r
else: fail("Program failed: $r")
fail("Program failed: $r")
func output_text(r:ProgramResult, trim_newline=yes -> Text?):
when r.exit_type is Exited(status):
if status == 0:
if text := Text.from_bytes(r.stdout):
if trim_newline:
text = text:trim($/{1 nl}/, trim_left=no, trim_right=yes)
return text
else: return none
return none
func error_text(r:ProgramResult -> Text?):
when r.exit_type is Exited(status):
if status == 0:
return Text.from_bytes(r.stderr)
else: return none
return none
struct Command(command:Text, args=[:Text], env={:Text,Text}):
func from_path(path:Path, args=[:Text], env={:Text,Text} -> Command):
return Command(Text(path), args, env)
func run(command:Command, input="", input_bytes=[:Byte] -> ProgramResult):
if input.length > 0:
(&input_bytes):insert_all(input:bytes())
stdout := [:Byte]
stderr := [:Byte]
status := run_command(command.command, command.args, command.env, input_bytes, &stdout, &stderr)
if inline C : Bool { WIFEXITED(_$status) }:
return ProgramResult(stdout, stderr, ExitType.Exited(inline C : Int32 { WEXITSTATUS(_$status) }))
if inline C : Bool { WIFSIGNALED(_$status) }:
return ProgramResult(stdout, stderr, ExitType.Signaled(inline C : Int32 { WTERMSIG(_$status) }))
return ProgramResult(stdout, stderr, ExitType.Failed)
func get_output(command:Command, input="", trim_newline=yes -> Text?):
return command:run(input=input):output_text(trim_newline=trim_newline)
func get_output_bytes(command:Command, input="", input_bytes=[:Byte] -> List(Byte)?):
result := command:run(input=input, input_bytes=input_bytes)
when result.exit_type is Exited(status):
if status == 0: return result.stdout
return none
else: return none
func main(command:Text, args:List(Text), input=""):
cmd := Command(command, args)
say(cmd:get_output(input=input)!)
|