1implement Wait; 2 3# 4# Copyright © 2003 Vita Nuova Holdings Limited. All rights reserved. 5# 6 7include "sys.m"; 8 sys: Sys; 9 10include "wait.m"; 11 12init() 13{ 14 sys = load Sys Sys->PATH; 15} 16 17read(fd: ref Sys->FD): (int, string, string) 18{ 19 buf := array[2*Sys->WAITLEN] of byte; 20 n := sys->read(fd, buf, len buf); 21 if(n <= 0) 22 return (n, nil, sys->sprint("%r")); 23 return parse(string buf[0:n]); 24} 25 26monitor(fd: ref Sys->FD): (int, chan of (int, string, string)) 27{ 28 pid := chan of int; 29 out := chan of (int, string, string); 30 spawn waitreader(fd, pid, out); 31 return (<-pid, out); 32} 33 34waitreader(fd: ref Sys->FD, pid: chan of int, out: chan of (int, string, string)) 35{ 36 pid <-= sys->pctl(0, nil); 37 for(;;){ 38 (child, modname, status) := read(fd); 39 out <-= (child, modname, status); 40 if(child <= 0) 41 break; # exit on error 42 } 43} 44 45parse(status: string): (int, string, string) 46{ 47 for (i := 0; i < len status; i++) 48 if (status[i] == ' ') 49 break; 50 j := i+2; # skip space and " 51 for (i = j; i < len status; i++) 52 if (status[i] == '"') 53 break; 54 return (int status, status[j:i], status[i+2:]); 55} 56