summaryrefslogtreecommitdiff
path: root/appl/lib/wait.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/wait.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/lib/wait.b')
-rw-r--r--appl/lib/wait.b55
1 files changed, 55 insertions, 0 deletions
diff --git a/appl/lib/wait.b b/appl/lib/wait.b
new file mode 100644
index 00000000..f24be0f2
--- /dev/null
+++ b/appl/lib/wait.b
@@ -0,0 +1,55 @@
+implement Wait;
+
+#
+# Copyright © 2003 Vita Nuova Holdings Limited. All rights reserved.
+#
+
+include "sys.m";
+ sys: Sys;
+
+include "wait.m";
+
+init()
+{
+ sys = load Sys Sys->PATH;
+}
+
+read(fd: ref Sys->FD): (int, string, string)
+{
+ buf := array[2*Sys->WAITLEN] of byte;
+ n := sys->read(fd, buf, len buf);
+ if(n <= 0)
+ return (n, nil, sys->sprint("%r"));
+ return parse(string buf[0:n]);
+}
+
+monitor(fd: ref Sys->FD): (int, chan of (int, string, string))
+{
+ pid := chan of int;
+ out := chan of (int, string, string);
+ spawn waitreader(fd, pid, out);
+ return (<-pid, out);
+}
+
+waitreader(fd: ref Sys->FD, pid: chan of int, out: chan of (int, string, string))
+{
+ pid <-= sys->pctl(0, nil);
+ for(;;){
+ (child, modname, status) := read(fd);
+ out <-= (child, modname, status);
+ if(child <= 0)
+ break; # exit on error
+ }
+}
+
+parse(status: string): (int, string, string)
+{
+ for (i := 0; i < len status; i++)
+ if (status[i] == ' ')
+ break;
+ j := i+2; # skip space and "
+ for (i = j; i < len status; i++)
+ if (status[i] == '"')
+ break;
+ return (int status, status[j:i], status[i+2:]);
+}