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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
implement Fslib;
#
# Copyright © 2003 Vita Nuova Holdings Limited
#
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
include "fslib.m";
# Fsdata stream conventions:
#
# Fsdata: adt {
# dir: ref Sys->Dir;
# data: array of byte;
# };
# Fschan: type chan of (Fsdata, chan of int);
# c: Fschan;
#
# a stream of values sent on c represent the contents of a directory
# hierarchy. after each value has been received, the associated reply
# channel must be used to prompt the sender how next to proceed.
#
# the first item sent on an fsdata channel represents the root directory
# (it must be a directory), and its name holds the full path of the
# hierarchy that's being transferred. the items that follow represent
# the contents of the root directory.
#
# the set of valid sequences of values can be described by a yacc-style
# grammar, where the terminal tokens describe data values (Fsdata adts)
# passed down the channel. this grammar describes the case where the
# entire fs tree is traversed in its entirety:
#
# dir: DIR dircontents NIL
# | DIR NIL
# dircontents: entry
# | dircontents entry
# entry: FILE filecontents NIL
# | FILE NIL
# | dir
# filecontents: DATA
# | filecontents DATA
#
# the tests for the various terminal token types, given a token (of type
# Fsdata) t:
#
# FILE t.dir != nil && (t.dir.mode & Sys->DMDIR) == 0
# DIR t.dir != nil && (t.dir.mode & Sys->DMDIR)
# DATA t.data != nil
# NIL t.data == nil && t.dir == nil
#
# when a token is received, there are four possible replies:
# Quit
# terminate the stream immediately. no more tokens will
# be on the channel.
#
# Down
# descend one level in the hierarchy, if possible. the next tokens
# will represent the contents of the current entry.
#
# Next
# get the next entry in a directory, or the next data
# block in a file, or travel one up the hierarchy if
# it's the last entry or data block in that directory or file.
#
# Skip
# skip to the end of a directory or file's contents.
# if we're already at the end, this is a no-op (same as Next)
#
# grammar including replies is different. a token is the tuple (t, reply),
# where reply is the value that was sent over the reply channel. Quit
# always causes the grammar to terminate, so it is omitted for clarity.
# thus there are 12 possible tokens (DIR_DOWN, DIR_NEXT, DIR_SKIP, FILE_DOWN, etc...)
#
# dir: DIR_DOWN dircontents NIL_NEXT
# | DIR_DOWN dircontents NIL_SKIP
# | DIR_DOWN dircontents NIL_DOWN
# | DIR_NEXT
# dircontents:
# | FILE_SKIP
# | DIR_SKIP
# | file dircontents
# | dir dircontents
# file: FILE_DOWN filecontents NIL_NEXT
# | FILE_DOWN filecontents NIL_SKIP
# | FILE_DOWN filecontents NIL_DOWN
# | FILE_NEXT
# filecontents:
# | data
# | data DATA_SKIP
# data: DATA_NEXT
# | data DATA_NEXT
#
# both the producer and consumer of fs data on the channel must between
# them conform to the second grammar. if a stream of fs data
# is sent with no reply channel, the stream must conform to the first grammar.
valuec := array[] of {
tagof(Value.V) => 'v',
tagof(Value.X) => 'x',
tagof(Value.P) => 'p',
tagof(Value.S) => 's',
tagof(Value.C) => 'c',
tagof(Value.T) => 't',
tagof(Value.M) => 'm',
};
init()
{
sys = load Sys Sys->PATH;
}
# copy the contents (not the entry itself) of a directory from src to dst.
copy(src, dst: Fschan): int
{
indent := 1;
myreply := chan of int;
for(;;){
(d, reply) := <-src;
dst <-= (d, myreply);
r := <-myreply;
case reply <-= r {
Quit =>
return Quit;
Next =>
if(d.dir == nil && d.data == nil)
if(--indent == 0)
return Next;
Skip =>
if(--indent == 0)
return Next;
Down =>
if(d.dir != nil || d.data != nil)
indent++;
}
}
}
Report.new(): ref Report
{
r := ref Report(chan of string, chan of (string, chan of string), chan of int);
spawn reportproc(r.startc, r.enablec, r.reportc);
return r;
}
Report.start(r: self ref Report, name: string): chan of string
{
if(r == nil)
return nil;
errorc := chan of string;
r.startc <-= (name, errorc);
return errorc;
}
Report.enable(r: self ref Report)
{
r.enablec <-= 0;
}
reportproc(startc: chan of (string, chan of string), startreports: chan of int, errorc: chan of string)
{
realc := array[2] of chan of string;
p := array[len realc] of string;
a := array[0] of chan of string;;
n := 0;
for(;;) alt{
(prefix, c) := <-startc =>
if(n == len realc){
realc = (array[n * 2] of chan of string)[0:] = realc;
p = (array[n * 2] of string)[0:] = p;
}
realc[n] = c;
p[n] = prefix;
n++;
<-startreports =>
if(n == 0){
errorc <-= nil;
exit;
}
a = realc;
(x, report) := <-a =>
if(report == nil){
# errorc <-= "exit " + p[x];
--n;
if(n != x){
a[x] = a[n];
a[n] = nil;
p[x] = p[n];
p[n] = nil;
}
if(n == 0){
errorc <-= nil;
exit;
}
}else if(a == realc)
errorc <-= p[x] + ": " + report;
}
}
type2s(c: int): string
{
case c{
'a' =>
return "any";
'x' =>
return "fs";
's' =>
return "string";
'v' =>
return "void";
'p' =>
return "gate";
'c' =>
return "command";
't' =>
return "entries";
'm' =>
return "selector";
* =>
return sys->sprint("unknowntype('%c')", c);
}
}
typeerror(tc: int, v: ref Value): string
{
sys->fprint(sys->fildes(2), "fs: bad type conversion, expected %s, was actually %s\n", type2s(tc), type2s(valuec[tagof v]));
return "type conversion error";
}
Value.t(v: self ref Value): ref Value.T
{
pick xv :=v {T => return xv;}
raise typeerror('t', v);
}
Value.c(v: self ref Value): ref Value.C
{
pick xv :=v {C => return xv;}
raise typeerror('c', v);
}
Value.s(v: self ref Value): ref Value.S
{
pick xv :=v {S => return xv;}
raise typeerror('s', v);
}
Value.p(v: self ref Value): ref Value.P
{
pick xv :=v {P => return xv;}
raise typeerror('p', v);
}
Value.x(v: self ref Value): ref Value.X
{
pick xv :=v {X => return xv;}
raise typeerror('x', v);
}
Value.v(v: self ref Value): ref Value.V
{
pick xv :=v {V => return xv;}
raise typeerror('v', v);
}
Value.m(v: self ref Value): ref Value.M
{
pick xv :=v {M => return xv;}
raise typeerror('m', v);
}
Value.typec(v: self ref Value): int
{
return valuec[tagof v];
}
Value.discard(v: self ref Value)
{
if(v == nil)
return;
pick xv := v {
X =>
(<-xv.i).t1 <-= Quit;
P =>
xv.i <-= (Nilentry, nil);
M =>
xv.i <-= (nil, nil, nil);
V =>
xv.i <-= 0;
T =>
xv.i.sync <-= 0;
}
}
sendnulldir(c: Fschan): int
{
reply := chan of int;
c <-= ((ref Sys->nulldir, nil), reply);
if((r := <-reply) == Down){
c <-= ((nil, nil), reply);
if(<-reply != Quit)
return Quit;
return Next;
}
return r;
}
quit(errorc: chan of string)
{
if(errorc != nil)
errorc <-= nil;
exit;
}
report(errorc: chan of string, err: string)
{
if(errorc != nil)
errorc <-= err;
}
# true if a module with type sig t1 is compatible with a caller that expects t0
typecompat(t0, t1: string): int
{
(rt0, at0, ot0) := splittype(t0);
(rt1, at1, ot1) := splittype(t1);
if((rt0 != rt1 && rt0 != 'a') || at0 != at1) # XXX could do better for repeated args.
return 0;
for(i := 1; i < len ot0; i++){
for(j := i; j < len ot0; j++)
if(ot0[j] == '-')
break;
(ok, t) := opttypes(ot0[i], ot1);
if(ok == -1 || ot0[i:j] != t)
return 0;
i = j + 1;
}
return 1;
}
splittype(t: string): (int, string, string)
{
if(t == nil)
return (-1, nil, nil);
for(i := 1; i < len t; i++)
if(t[i] == '-')
break;
return (t[0], t[1:i], t[i:]);
}
opttypes(opt: int, opts: string): (int, string)
{
for(i := 1; i < len opts; i++){
if(opts[i] == opt && opts[i-1] == '-'){
for(j := i+1; j < len opts; j++)
if(opts[j] == '-')
break;
return (0, opts[i+1:j]);
}
}
return (-1, nil);
}
cmdusage(s, t: string): string
{
if(s == nil)
return nil;
for(oi := 0; oi < len t; oi++)
if(t[oi] == '-')
break;
if(oi < len t){
single, multi: string;
for(i := oi; i < len t - 1;){
for(j := i + 1; j < len t; j++)
if(t[j] == '-')
break;
optargs := t[i+2:j];
if(optargs == nil)
single[len single] = t[i+1];
else{
multi += sys->sprint(" [-%c", t[i+1]);
for (k := 0; k < len optargs; k++)
multi += " " + type2s(optargs[k]);
multi += "]";
}
i = j;
}
if(single != nil)
s += " [-" + single + "]";
s += multi;
}
multi := 0;
if(oi > 2 && t[oi - 1] == '*'){
multi = 1;
oi -= 2;
}
for(k := 1; k < oi; k++)
s += " " + type2s(t[k]);
if(multi)
s += " [" + type2s(t[k]) + "...]";
s += " -> " + type2s(t[0]);
return s;
}
|