Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/inferno/appl/cmd/cmdhit.b

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


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");
		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;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.