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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
implement Sh;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
sh: Sh;
myself: Shellbuiltin;
mysh: Sh;
Namespace: adt {
name: string;
madecmd: array of int;
mods: list of (string, Shellbuiltin);
builtins: array of list of (string, Shellbuiltin);
};
Builtin, Sbuiltin: con iota;
namespaces: list of ref Namespace;
pending: list of (string, int, Shellbuiltin);
lock: chan of int;
BUILTINPATH: con "/dis/sh";
initbuiltin(c: ref Sh->Context, shmod: Sh): string
{
sys = load Sys Sys->PATH;
sh = shmod;
mysh = load Sh "$self";
myself = load Shellbuiltin "$self";
sh->c.addbuiltin("mload", myself);
sh->c.addbuiltin("munload", myself);
lock = chan[1] of int;
return nil;
}
runbuiltin(ctxt: ref Sh->Context, nil: Sh,
argv: list of ref Sh->Listnode, last: int): string
{
cmd := (hd argv).word;
case cmd {
"mload" or "munload" =>
if(tl argv == nil)
ctxt.fail("usage", "usage: "+cmd+" name [module...]");
# by doing this lock, we're relying on modules not to invoke a command
# in initbuiltin that calls back into mload. since they shouldn't be running
# any commands in initbuiltin anyway, this seems like a reasonable assumption.
lock <-= 1;
{
name := (hd tl argv).word;
for(argv = tl tl argv; argv != nil; argv = tl argv){
if((hd argv).cmd != nil)
ctxt.fail("usage", "usage: "+cmd+" namespace [module...]");
if(cmd == "mload")
mload(ctxt, name, (hd argv).word);
else
munload(ctxt, name, (hd argv).word);
}
}exception{
"fail:*" =>
<-lock;
raise;
}
<-lock;
return nil;
* =>
if(len argv < 2)
ctxt.fail("usage", sys->sprint("usage: %s command", (hd argv).word));
b := lookup(ctxt, (hd argv).word, (hd tl argv).word, Builtin, nil);
return b->runbuiltin(ctxt, mysh, tl argv, last);
}
}
mload(ctxt: ref Sh->Context, name, modname: string): string
{
ns := nslookup(name);
if(ns == nil){
ns = ref Namespace(name, array[2] of {* => 0}, nil, array[2] of list of (string, Shellbuiltin));
namespaces = ns :: namespaces;
}
for(nsm := ns.mods; nsm != nil; nsm = tl nsm)
if((hd nsm).t0 == modname)
return nil;
path := modname;
if (len path < 4 || path[len path-4:] != ".dis")
path += ".dis";
if (path[0] != '/' && path[0:2] != "./")
path = BUILTINPATH + "/" + path;
mod := load Shellbuiltin path;
if (mod == nil)
ctxt.fail("bad module", sys->sprint("load: cannot load %s: %r", path));
s := mod->initbuiltin(ctxt, mysh);
if(s != nil){
munload(ctxt, name, modname);
pending = nil;
ctxt.fail("init", "mload: init "+modname+" failed: "+s);
}
mod = mod->getself();
ns.mods = (modname, mod) :: ns.mods;
for(; pending != nil; pending = tl pending){
(cmd, which, pmod) := hd pending;
if(pmod != mod)
sys->fprint(sys->fildes(2), "mload: unexpected module when loading %#q", name);
else
lookup(ctxt, name, cmd, which, mod);
}
return nil;
}
munload(ctxt: ref Sh->Context, name, modname: string): string
{
ns := nslookup(name);
if(ns == nil){
sys->fprint(sys->fildes(2), "munload: no such namespace %#q\n", name);
return "fail";
}
nm: list of (string, Shellbuiltin);
mod: Shellbuiltin;
for(m := ns.mods; m != nil; m = tl m)
if((hd m).t0 == modname)
mod = (hd m).t1;
else
nm = hd m :: nm;
if(mod == nil){
sys->fprint(sys->fildes(2), "munload: no such module %#q\n", modname);
return "fail";
}
ns.mods = nm;
for(i := 0; i < 2; i++){
nb: list of (string, Shellbuiltin) = nil;
for(b := ns.builtins[i]; b != nil; b = tl b)
if((hd b).t1 != mod)
nb = hd b :: nb;
ns.builtins[i] = nb;
if(ns.builtins[i] == nil){
if(i == Builtin)
sh->ctxt.removebuiltin(name, myself);
else
sh->ctxt.removesbuiltin(name, myself);
}
}
return nil;
}
runsbuiltin(ctxt: ref Sh->Context, nil: Sh,
argv: list of ref Sh->Listnode): list of ref Sh->Listnode
{
if(len argv < 2)
ctxt.fail("usage", sys->sprint("usage: %s command", (hd argv).word));
b := lookup(ctxt, (hd argv).word, (hd tl argv).word, Sbuiltin, nil);
return b->runsbuiltin(ctxt, mysh, tl argv);
}
searchns(mod: Shellbuiltin): string
{
for(m := namespaces; m != nil; m = tl m)
for(b := (hd m).mods; b != nil; b = tl b)
if((hd b).t1 == mod)
return (hd m).name;
return nil;
}
lookup(ctxt: ref Sh->Context, name, cmd: string, which: int, sb: Shellbuiltin): Shellbuiltin
{
for(m := namespaces; m != nil; m = tl m)
if((hd m).name == name)
break;
if(m == nil)
ctxt.fail("unknown", sys->sprint("unknown namespace %q", name));
ns := hd m;
for(b := ns.builtins[which]; b != nil; b = tl b)
if((hd b).t0 == cmd)
break;
if(b == nil){
if(sb != nil){
ns.builtins[which] = (cmd, sb) :: ns.builtins[which];
if(!ns.madecmd[which]){
if(which == Builtin)
sh->ctxt.addbuiltin(name, myself);
else
sh->ctxt.addsbuiltin(name, myself);
ns.madecmd[which] = 1;
}
return sb;
}
ctxt.fail("unknown cmd", sys->sprint("unknown command %q", cmd));
}
return (hd b).t1;
}
Context.addbuiltin(c: self ref Context, modname: string, mod: Shellbuiltin)
{
name := searchns(mod);
if(name == nil)
pending = (modname, Builtin, mod) :: pending;
else
lookup(c, name, modname, Builtin, mod);
}
Context.addsbuiltin(c: self ref Context, modname: string, mod: Shellbuiltin)
{
name := searchns(mod);
if(name == nil)
pending = (modname, Sbuiltin, mod) :: pending;
else
lookup(c, name, modname, Sbuiltin, mod);
}
Context.removebuiltin(c: self ref Context, nil: string, nil: Shellbuiltin)
{
c.fail("nope", "mload: remove builtin not implemented");
}
Context.removesbuiltin(c: self ref Context, nil: string, nil: Shellbuiltin)
{
c.fail("nope", "mload: remove sbuiltin not implemented");
}
Context.addmodule(nil: self ref Context, name: string, nil: Shellbuiltin)
{
sys->fprint(sys->fildes(2), "mload: addmodule not allowed (%s)\n", name);
}
nslookup(name: string): ref Namespace
{
for(m := namespaces; m != nil; m = tl m)
if((hd m).name == name)
return hd m;
return nil;
}
whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
{
return nil;
}
getself(): Shellbuiltin
{
return myself;
}
initialise()
{
return sh->initialise();
}
init(ctxt: ref Draw->Context, argv: list of string)
{
return sh->init(ctxt, argv);
}
system(ctxt: ref Draw->Context, cmd: string): string
{
return sh->system(ctxt, cmd);
}
run(ctxt: ref Draw->Context, argv: list of string): string
{
return sh->run(ctxt, argv);
}
parse(s: string): (ref Cmd, string)
{
return sh->parse(s);
}
cmd2string(c: ref Cmd): string
{
return sh->cmd2string(c);
}
list2stringlist(nl: list of ref Listnode): list of string
{
return sh->list2stringlist(nl);
}
stringlist2list(sl: list of string): list of ref Listnode
{
return sh->stringlist2list(sl);
}
quoted(val: list of ref Listnode, quoteblocks: int): string
{
return sh->quoted(val, quoteblocks);
}
Context.new(drawcontext: ref Draw->Context): ref Context
{
return sh->Context.new(drawcontext);
}
Context.get(c: self ref Context, name: string): list of ref Listnode
{
return sh->c.get(name);
}
Context.set(c: self ref Context, name: string, val: list of ref Listnode)
{
return sh->c.set(name, val);
}
Context.setlocal(c: self ref Context, name: string, val: list of ref Listnode)
{
return sh->c.setlocal(name, val);
}
Context.envlist(c: self ref Context): list of (string, list of ref Listnode)
{
return sh->c.envlist();
}
Context.push(c: self ref Context)
{
return sh->c.push();
}
Context.pop(c: self ref Context)
{
return sh->c.pop();
}
Context.copy(c: self ref Context, copyenv: int): ref Context
{
return sh->c.copy(copyenv);
}
Context.run(c: self ref Context, args: list of ref Listnode, last: int): string
{
return sh->c.run(args, last);
}
Context.fail(c: self ref Context, ename, msg: string)
{
return sh->c.fail(ename, msg);
}
Context.options(c: self ref Context): int
{
return sh->c.options();
}
Context.setoptions(c: self ref Context, flags, on: int): int
{
return sh->c.setoptions(flags, on);
}
|