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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
implement Rexecsrv;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
sh: Sh;
include "alphabet/endpoints.m";
endpoints: Endpoints;
Endpoint: import endpoints;
include "alphabet/reports.m";
reports: Reports;
Report: import reports;
include "alphabet.m";
alphabet: Alphabet;
Value: import alphabet;
include "alphabet/abc.m";
include "alphabet/abctypes.m";
include "string.m";
str: String;
Rexecsrv: module {
init: fn(nil: ref Draw->Context, argv: list of string);
};
drawctxt: ref Draw->Context;
init(ctxt: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
endpoints = load Endpoints Endpoints->PATH;
if(endpoints == nil)
fatal(sys->sprint("cannot load %s: %r", Endpoints->PATH));
endpoints->init();
sh = load Sh Sh->PATH;
if(sh == nil)
fatal(sys->sprint("cannot load %s: %r", Sh->PATH));
sh->initialise();
reports = load Reports Reports->PATH;
if(reports == nil)
fatal(sys->sprint("cannot load %s: %r", Reports->PATH));
str = load String String->PATH;
if(str == nil)
fatal(sys->sprint("cannot load %s: %r", String->PATH));
if(len argv != 3)
fatal("usage: rexecsrv dir {decls}");
drawctxt = ctxt;
if(sys->stat("/n/endpoint/local/clone").t0 == -1)
fatal("no local endpoints available");
dir := hd tl argv;
decls := parse(hd tl tl argv);
if(sys->bind("#s", dir, Sys->MREPL) == -1)
fatal(sys->sprint("cannot bind #s onto %q: %r", dir));
alphabet = declares(decls);
fio := sys->file2chan(dir, "exec");
sync := chan of int;
spawn rexecproc(sync, fio);
<-sync;
}
stderr(): ref Sys->FD
{
return sys->fildes(2);
}
# use one alphabet module to bootstrap another
# with the desired declarations that we can use to
# execute external commands.
declares(decls: ref Sh->Cmd): Alphabet
{
alphabet0 := load Alphabet Alphabet->PATH;
if(alphabet0 == nil)
fatal(sys->sprint("cannot load %s: %r", Alphabet->PATH));
alphabet0->init();
abctypes := load Abctypes Abctypes->PATH;
if(abctypes == nil)
fatal(sys->sprint("cannot load %s: %r", Abctypes->PATH));
Abccvt: import abctypes;
abc := load Abc Abc->PATH;
if(abc == nil)
fatal(sys->sprint("cannot load %s: %r", Abc->PATH));
abc->init();
Value: import abc;
(c, nil, abccvt) := abctypes->proxy0();
spawn reports->reportproc(errorc := chan of string, nil, reply := chan of ref Report);
r := <-reply;
if((err := alphabet0->loadtypeset("/abc", c, nil)) != nil)
fatal("cannot load typeset /abc: "+err);
alphabet0->setautodeclare(1);
spawn alphabet0->eval0(
parse("{(/cmd);"+
"/abc/abc |"+
"/abc/declares $1"+
"}"
),
"/abc/abc",
nil,
r,
r.start("evaldecls"),
ref (Alphabet->Value).Vc(decls) :: nil,
vc := chan of ref Alphabet->Value
);
r.enable();
av: ref Alphabet->Value;
wait:
for(;;)alt{
av = <-vc =>
;
msg := <-errorc =>
if(msg == nil)
break wait;
sys->fprint(stderr(), "rexecsrv: %s\n", msg);
}
if(av == nil)
fatal("declarations failed");
v := abccvt.ext2int(av).dup();
alphabet0->av.free(1);
pick xv := v {
VA =>
return xv.i.alphabet;
}
return nil;
}
parse(s: string): ref Sh->Cmd
{
(c, err) := sh->parse(s);
if(c== nil)
fatal(sys->sprint("cannot parse %q: %s", s, err));
return c;
}
lc(cmd: ref Sh->Cmd): ref Sh->Listnode
{
return ref Sh->Listnode(cmd, nil);
}
lw(word: string): ref Sh->Listnode
{
return ref Sh->Listnode(nil, word);
}
# write endpoints, cmd
# read endpoints
rexecproc(sync: chan of int, fio: ref Sys->FileIO)
{
sys->pctl(Sys->FORKNS, nil);
pending: list of (int, string);
sync <-= 1;
for(;;) alt {
(nil, data, fid, wc) := <-fio.write =>
if(wc == nil)
break;
req := string data;
l := str->unquoted(req);
if(len l != 2 || Endpoint.mk(hd l).addr == nil){
wc <-= (0, "bad request");
break;
}
pending = (fid, req) :: pending;
wc <-= (0, nil);
(offset, nil, fid, rc) := <-fio.read =>
if(rc == nil){
(pending, nil) = removefid(fid, pending);
break;
}
if(offset > 0){
rc <-= (nil, nil);
break;
}
req: string;
(pending, req) = removefid(fid, pending);
if(req == nil){
rc <-= (nil, "no pending exec");
break;
}
l := str->unquoted(req);
spawn exec(sync1 := chan of int, Endpoint.mk(hd l), hd tl l, rc);
<-sync1;
}
}
gather(errorc: chan of string)
{
s := "";
while((e := <-errorc) != nil)
s += e + "\n";
errorc <-= s;
}
exec(sync: chan of int, ep: Endpoint, expr: string,
rc: chan of (array of byte, string))
{
sys->pctl(Sys->FORKNS, nil);
sync <-= 1;
spawn gather(errorc := chan of string);
(c, err) := alphabet->parse(expr);
if(c == nil){
rc <-= (nil, "parse error: "+err);
return;
}
usage: string;
(c, usage) = alphabet->rewrite(c, "/fd", errorc);
errorc <-= nil;
err = <-errorc;
if(c == nil){
rc <-= (nil, err);
return;
}
if(!alphabet->typecompat("/fd -> /fd", usage).t0)
rc <-= (nil, "incompatible type: "+usage);
fd0: ref Sys->FD;
(fd0, err) = endpoints->open(nil, ep);
if(fd0 == nil){
rc <-= (nil, err);
return;
}
(fd1, ep1) := endpoints->create("local");
if(fd1 == nil){
rc <-= (nil, "cannot make endpoints: "+ep1.about);
return;
}
rc <-= (array of byte ep1.text(), nil);
runcmd(c, fd0, fd1);
}
fdproc(f: chan of ref Sys->FD, fd0: ref Sys->FD)
{
f <-= fd0;
fd1 := <-f;
if(fd1 == nil)
exit;
buf := array[Sys->ATOMICIO] of byte;
while((n := sys->read(fd0, buf, len buf)) > 0)
if(sys->write(fd1, buf, n) == -1)
break;
}
runcmd(c: ref Sh->Cmd, fd0, fd1: ref Sys->FD)
{
f := chan of ref Sys->FD;
spawn fdproc(f, fd0);
spawn reports->reportproc(errorc := chan of string, nil, reply := chan of ref Report);
r := <-reply;
spawn alphabet->eval0(
c,
"/fd",
drawctxt,
r,
r.start("evalcmd"),
ref (Alphabet->Value).Vf(f) :: nil,
vc := chan of ref Alphabet->Value
);
r.enable();
av: ref Alphabet->Value;
wait:
for(;;)alt{
av = <-vc =>
if(av == nil){
sys->fprint(stderr(), "rexecsrv: no value received\n");
break;
}
pick v := av {
Vf =>
<-v.i;
v.i <-= fd1;
* =>
sys->fprint(stderr(), "rexecsrv: can't happen: expression has wrong type '%c'\n",
alphabet->v.typec());
}
msg := <-errorc =>
if(msg == nil)
break wait;
# XXX could queue diagnostics back to caller here.
sys->fprint(stderr(), "rexecsrv: %s\n", msg);
}
sys->write(fd1, array[0] of byte, 0);
}
removefid(fid: int, l: list of (int, string)): (list of (int, string), string)
{
if(l == nil)
return (nil, nil);
if((hd l).t0 == fid)
return (removefid(fid, tl l).t0, (hd l).t1);
(rl, d) := removefid(fid, tl l);
return (hd l :: rl, d);
}
fatal(e: string)
{
sys->fprint(sys->fildes(2), "rexecsrv: %s\n", e);
raise "fail:error";
}
|