summaryrefslogtreecommitdiffstats
path: root/virtual-programs/unix-commands.folk
blob: 814fdf3cc35a8a3b78620e03e6f94bc6a9a32327 (plain)
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
set ::unixjobs [dict create]
set ::nextunixjobid 0
proc ::readline {jobid channel} {
	if {[gets $channel line] >= 0} {
		dict with ::unixjobs $jobid {
			lappend log $line
			Retract $object is running Unix command $command with job id $jobid output /something/
			Assert $object is running Unix command $command with job id $jobid output $log
			Step
		}
	} elseif {[eof $channel]} {
		close $channel
	}
}

When /someone/ wishes /p/ runs Unix command /c/ {
	set jobid [incr ::nextunixjobid]
	dict set ::unixjobs $jobid [dict create object $p command $c log [list]]

	lassign [chan pipe] reader writer
	set pid [exec {*}$c >@$writer 2>@1 &]
	close $writer

	fconfigure $reader -blocking 0
	fileevent $reader readable [list ::readline $jobid $reader]

	When /p/ is running Unix command /c/ with job id $jobid output /log/ {
		Wish $p is labelled [join $log "\n"]
	}

	On unmatch [list exec kill $pid]
}