summaryrefslogtreecommitdiff
path: root/appl/cmd/plumb.b
blob: 2e4d3af3b723e19d75df8a089d515054e9c1e40f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
implement Plumb;

include "sys.m";
	sys: Sys;

include "draw.m";

include "arg.m";

include "plumbmsg.m";
	plumbmsg: Plumbmsg;
	Msg, Attr: import plumbmsg;

include "workdir.m";
	workdir: Workdir;

Plumb: 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;
	plumbmsg = load Plumbmsg Plumbmsg->PATH;
	if(plumbmsg == nil)
		nomod(Plumbmsg->PATH);
	workdir = load Workdir Workdir->PATH;
	if(workdir == nil)
		nomod(Workdir->PATH);

	if(plumbmsg->init(1, nil, 0) < 0)
		err(sys->sprint("can't connect to plumb: %r"));

	attrs: list of ref Attr;
	input := 0;
	m := ref Msg("plumb", nil, workdir->init(), "text", nil, nil);
	arg := load Arg Arg->PATH;
	arg->init(args);
	arg->setusage("plumb [-s src] [-d dest] [-w wdir] [-t type] [-a name val] -i | ... data ...");
	while((c := arg->opt()) != 0)
		case c {
		's' =>
			m.src = arg->earg();
		'd' =>
			m.dst = arg->earg();
		'w' or 'D' =>
			m.dir = arg->earg();
		'i' =>
			input++;
		't' or 'k'=>
			m.kind = arg->arg();
		'a' =>
			name := arg->earg();
			val := arg->earg();
			attrs = tack(attrs, ref Attr(name, val));
		* =>
			arg->usage();
		}
	args = arg->argv();
	if(input && args != nil || !input && args == nil)
		arg->usage();
	arg = nil;

	if(input){
		m.data = gather(sys->fildes(0));
		(notfound, nil) := plumbmsg->lookup(plumbmsg->string2attrs(m.attr), "action");
		if(notfound)
			tack(attrs, ref Attr("action", "showdata"));
		m.attr = plumbmsg->attrs2string(attrs);
		if(m.send() < 0)
			err(sys->sprint("can't send message: %r"));
		exit;
	}
	
	nb := 0;
	for(a := args; a != nil; a = tl a)
		nb += len array of byte hd a;
	nb += len args;
	buf := array[nb] of byte;
	nb = 0;
	for(a = args; a != nil; a = tl a){
		b := array of byte hd a;
		buf[nb++] = byte ' ';
		buf[nb:] = b;
		nb += len b;
	}
	m.data = buf[1:];
	m.attr = plumbmsg->attrs2string(attrs);
	if(m.send() < 0)
		err(sys->sprint("can't plumb message: %r"));
}

gather(fd: ref Sys->FD): array of byte
{
	Chunk: con 8192;	# arbitrary
	ndata := 0;
	buf := array[Chunk] of byte;
	while((n := sys->read(fd, buf[ndata:], len buf - ndata)) > 0){
		ndata += n;
		if(len buf - ndata < Chunk){
			t := array[len buf+Chunk] of byte;
			t[0:] = buf[0: ndata];
			buf = t;
		}
	}
	if(n < 0)
		err(sys->sprint("error reading input: %r"));
	return buf[0: ndata];
}

tack(l: list of ref Attr, v: ref Attr): list of ref Attr
{
	if(l == nil)
		return v :: nil;
	return hd l :: tack(tl l, v);
}

nomod(m: string)
{
	err(sys->sprint("can't load %s: %r", m));
}

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