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
|
implement Fsmodule;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
sh: Sh;
Context: import sh;
include "fslib.m";
fslib: Fslib;
Option, Value, Entrychan, Report: import fslib;
# usage: exec [-n nfiles] [-t endcmd] [-pP] command entries
types(): string
{
return "vct-ns-tc-p-P";
}
badmod(p: string)
{
sys->fprint(sys->fildes(2), "fs: exec: cannot load %s: %r\n", p);
raise "fail:bad module";
}
init()
{
sys = load Sys Sys->PATH;
fslib = load Fslib Fslib->PATH;
if(fslib == nil)
badmod(Fslib->PATH);
sh = load Sh Sh->PATH;
if(sh == nil)
badmod(Sh->PATH);
sh->initialise();
}
run(drawctxt: ref Draw->Context, report: ref Report,
opts: list of Option, args: list of ref Value): ref Value
{
n := 1;
pflag := 0;
tcmd: ref Sh->Cmd;
for(; opts != nil; opts = tl opts){
o := hd opts;
case o.opt {
'n' =>
if((n = int (hd o.args).s().i) <= 0){
sys->fprint(sys->fildes(2), "fs: exec: invalid argument to -n\n");
return nil;
}
't' =>
tcmd = (hd o.args).c().i;
'p' =>
pflag = 1;
'P' =>
pflag = 2;
}
}
if(pflag && n > 1){
sys->fprint(sys->fildes(2), "fs: exec: cannot specify -p with -n %d\n", n);
return nil;
}
cmd := (hd args).c().i;
c := (hd tl args).t().i;
sync := chan of int;
spawn execproc(drawctxt, sync, n, pflag, c, cmd, tcmd, report.start("exec"));
sync <-= 1;
return ref Value.V(sync);
}
execproc(drawctxt: ref Draw->Context, sync: chan of int, n, pflag: int,
c: Entrychan, cmd, tcmd: ref Sh->Cmd, errorc: chan of string)
{
sys->pctl(Sys->NEWFD, 0::1::2::nil);
ctxt := Context.new(drawctxt);
<-sync;
if(<-sync == 0){
c.sync <-= 0;
errorc <-= nil;
exit;
}
c.sync <-= 1;
argv := ref Sh->Listnode(cmd, nil) :: nil;
fl: list of ref Sh->Listnode;
nf := 0;
while(((d, p, nil) := <-c.c).t0 != nil){
fl = ref Sh->Listnode(nil, p) :: fl;
if(++nf >= n){
ctxt.set("file", rev(fl));
if(pflag)
setstatenv(ctxt, d, pflag);
fl = nil;
nf = 0;
{ctxt.run(argv, 0);} exception {"fail:*" =>;}
}
}
if(nf > 0){
ctxt.set("file", rev(fl));
{ctxt.run(argv, 0);} exception {"fail:*" =>;}
}
if(tcmd != nil){
ctxt.set("file", nil);
{ctxt.run(ref Sh->Listnode(tcmd, nil) :: nil, 0);} exception {"fail:*" =>;}
}
errorc <-= nil;
}
setenv(ctxt: ref Context, var: string, val: list of string)
{
ctxt.set(var, sh->stringlist2list(val));
}
setstatenv(ctxt: ref Context, dir: ref Sys->Dir, pflag: int)
{
setenv(ctxt, "mode", modes(dir.mode) :: nil);
setenv(ctxt, "uid", dir.uid :: nil);
setenv(ctxt, "mtime", string dir.mtime :: nil);
setenv(ctxt, "length", string dir.length :: nil);
if(pflag > 1){
setenv(ctxt, "name", dir.name :: nil);
setenv(ctxt, "gid", dir.gid :: nil);
setenv(ctxt, "muid", dir.muid :: nil);
setenv(ctxt, "qid", sys->sprint("16r%ubx", dir.qid.path) :: string dir.qid.vers :: nil);
setenv(ctxt, "atime", string dir.atime :: nil);
setenv(ctxt, "dtype", sys->sprint("%c", dir.dtype) :: nil);
setenv(ctxt, "dev", string dir.dev :: nil);
}
}
mtab := array[] of {
"---", "--x", "-w-", "-wx",
"r--", "r-x", "rw-", "rwx"
};
modes(mode: int): string
{
s: string;
if(mode & Sys->DMDIR)
s = "d";
else if(mode & Sys->DMAPPEND)
s = "a";
else if(mode & Sys->DMAUTH)
s = "A";
else
s = "-";
if(mode & Sys->DMEXCL)
s += "l";
else
s += "-";
s += mtab[(mode>>6)&7]+mtab[(mode>>3)&7]+mtab[mode&7];
return s;
}
rev[T](x: list of T): list of T
{
l: list of T;
for(; x != nil; x = tl x)
l = hd x :: l;
return l;
}
|