summaryrefslogtreecommitdiff
path: root/appl/cmd/disk/kfscmd.b
blob: e1b023a9cfc707838921e3130d228a3e0665a5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
implement Kfscmd;

include "sys.m";
	sys:	Sys;

include "draw.m";
include "arg.m";

Kfscmd: module
{
	init:	fn(nil: ref Draw->Context, nil: list of string);
};

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;

	arg := load Arg Arg->PATH;
	if (arg == nil)
		err(sys->sprint("can't load %s: %r", Arg->PATH));

	cfs := "main";
	arg->init(args);
	arg->setusage("disk/kfscmd [-n fsname] cmd ...");
	while((c := arg->opt()) != 0)
		case c {
		'n' =>
			cfs = arg->earg();
		* =>
			arg->usage();
		}
	args = arg->argv();
	arg = nil;

	ctlf := "/chan/kfs."+cfs+".cmd";
	ctl := sys->open(ctlf, Sys->ORDWR);
	if(ctl == nil)
		err(sys->sprint("can't open %s: %r", ctlf));
	for(; args != nil; args = tl args){
		if(sys->fprint(ctl, "%s", hd args) > 0){
			buf := array[1024] of byte;
			while((n := sys->read(ctl, buf, len buf)) > 0)
				sys->write(sys->fildes(1), buf, n);
		}else
			err(sys->sprint("%q: %r", hd args));
	}
}

err(s: string)
{
	sys->fprint(sys->fildes(2), "kfscmd: %s\n", s);
	raise "fail:error";
}