implement cmdhit;
# this is a wrapper for the cmd command
# cmd wants you to keep the ctl file open.
# I just wanted a one shot deal, give it the command and get the result
# M Heath 2003
# latest version http://www.proweb.co.uk/~matt/inferno/cmdhit.b
# example use cmdhit 'ssh domain.com "ls /"'
# of course ssh will only work with passwordless keys
include "sys.m";
include "draw.m";
cmdhit : module {
init: fn(nil: ref Draw->Context, argv: list of string);
};
init (nil: ref Draw->Context, argv: list of string) {
sys := load Sys Sys->PATH;
buf := array[1024] of byte;
ctl := sys->open("/cmd/clone", sys->ORDWR);
dir := sys->read(ctl, buf, len buf -1);
if(dir == -1) {
sys->fprint(sys->fildes(2), "cmdhit: clone failed, maybe you didn't do bind -a '#C' /\n");
sys->raise("cmdhit: clone failed");
}
buf[dir] = byte 0;
data_fn := sys->sprint("/cmd/%s/data", string buf);
data := sys->open(data_fn, sys->ORDWR);
cmdstr := "exec";
arg : string;
for(argv = tl argv; argv != nil; argv = tl argv) {
arg = hd argv;
cmdstr += " " + arg;
}
cmdbytes := array of byte cmdstr;
sys->write(ctl, cmdbytes, len cmdbytes);
stdout := sys->fildes(1);
for(output := sys->read(data, buf, len buf); output > 0; output = sys->read(data, buf, len buf)) {
sys->write(stdout, buf, output);
}
ctl = nil;
data = nil;
}
|