From 37da2899f40661e3e9631e497da8dc59b971cbd0 Mon Sep 17 00:00:00 2001 From: "Charles.Forsyth" Date: Fri, 22 Dec 2006 17:07:39 +0000 Subject: 20060303a --- appl/lib/wait.b | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 appl/lib/wait.b (limited to 'appl/lib/wait.b') 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:]); +} -- cgit v1.2.3