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
|
#include "sam.h"
#include "parse.h"
extern jmp_buf mainloop;
char errfile[64];
String plan9cmd; /* null terminated */
Buffer plan9buf;
void checkerrs(void);
Buffer cmdbuf;
int cmdbufpos;
static void
updateenv(File *f)
{
static int fd = -1;
int n;
char buf[64], *p, *e;
if(f == nil){
putenv("%", "");
putenv("%dot", "");
return;
}
p = Strtoc(&f->name);
putenv("%", p);
free(p);
p = buf;
e = buf+sizeof(buf);
p = seprint(p, e, "%lud", 1+nlcount(f, 0, f->dot.r.p1));
p = seprint(p+1, e, "%lud", f->dot.r.p1);
p = seprint(p+1, e, "%lud", f->dot.r.p2);
n = p - buf;
if(fd == -1)
if((fd = create("/env/%dot", OWRITE, 0666)) < 0)
fprint(2, "updateenv create: %r\n");
if(write(fd, buf, n) != n)
fprint(2, "updateenv write: %r\n");
}
int
plan9(File *f, int type, String *s, int nest)
{
long l;
int m;
int pid, fd;
int retcode;
char *retmsg;
int pipe1[2], pipe2[2];
if(s->s[0]==0 && plan9cmd.s[0]==0)
error(Enocmd);
else if(s->s[0])
Strduplstr(&plan9cmd, s);
if(downloaded){
samerr(errfile);
remove(errfile);
}
if(type!='!' && pipe(pipe1)==-1)
error(Epipe);
if(type=='|' || type=='_')
snarf(f, addr.r.p1, addr.r.p2, &plan9buf, 1);
if((pid=fork()) == 0){
if(downloaded){ /* also put nasty fd's into errfile */
fd = create(errfile, 1, 0666L);
if(fd < 0)
fd = create("/dev/null", 1, 0666L);
dup(fd, 2);
close(fd);
/* 2 now points at err file */
if(type == '>')
dup(2, 1);
else if(type=='!'){
dup(2, 1);
fd = open("/dev/null", 0);
dup(fd, 0);
close(fd);
}
}
if(type != '!') {
if(type == '>')
dup(pipe1[0], 0);
else
dup(pipe1[1], 1);
close(pipe1[0]);
close(pipe1[1]);
}
if(type == '|' || type == '_'){
if(pipe(pipe2) == -1)
exits("pipe");
if((pid = fork())==0){
/*
* It's ok if we get SIGPIPE here
*/
close(pipe2[0]);
io = pipe2[1];
if(retcode=!setjmp(mainloop)){ /* assignment = */
char *c;
for(l = 0; l<plan9buf.nc; l+=m){
m = plan9buf.nc-l;
if(m>BLOCKSIZE-1)
m = BLOCKSIZE-1;
bufread(&plan9buf, l, genbuf, m);
genbuf[m] = 0;
c = Strtoc(tmprstr(genbuf, m+1));
Write(pipe2[1], c, strlen(c));
free(c);
}
}
exits(retcode? "error" : 0);
}
if(pid==-1){
fprint(2, "Can't fork?!\n");
exits("fork");
}
dup(pipe2[0], 0);
close(pipe2[0]);
close(pipe2[1]);
}
if(type=='<' || type=='^'){
close(0); /* so it won't read from terminal */
open("/dev/null", 0);
}
updateenv(f);
execl(SHPATH, SH, "-c", Strtoc(&plan9cmd), nil);
exits("exec");
}
if(pid == -1)
error(Efork);
if(type=='<' || type=='|'){
int nulls;
if(downloaded && addr.r.p1 != addr.r.p2)
outTl(Hsnarflen, addr.r.p2-addr.r.p1);
snarf(f, addr.r.p1, addr.r.p2, &snarfbuf, 0);
logdelete(f, addr.r.p1, addr.r.p2);
close(pipe1[1]);
io = pipe1[0];
f->tdot.p1 = -1;
f->ndot.r.p2 = addr.r.p2+readio(f, &nulls, 0, FALSE);
f->ndot.r.p1 = addr.r.p2;
closeio((Posn)-1);
}else if(type=='>'){
close(pipe1[0]);
io = pipe1[1];
bpipeok = 1;
writeio(f);
bpipeok = 0;
closeio((Posn)-1);
}else if(type == '^' || type == '_'){
int nulls;
close(pipe1[1]);
bufload(&cmdbuf, cmdbufpos, pipe1[0], &nulls);
close(pipe1[0]);
}
retmsg = waitfor(pid);
if(type=='|' || type=='<' || type=='_' || type=='^')
if(retmsg[0]!=0)
warn_s(Wbadstatus, retmsg);
if(downloaded)
checkerrs();
if(!nest)
dprint("!\n");
return retmsg[0] ? -1 : 0;
}
void
checkerrs(void)
{
char buf[256];
int f, n, nl;
char *p;
long l;
long r;
r = 0; // total amount of characters read
nl = 3; // maximum number of newlines
if(statfile(errfile, 0, 0, 0, &l, 0) > 0 && l != 0){
if((f=open((char *)errfile, 0)) != -1){
while (r<l-1 && nl>0 && (n=read(f, buf, sizeof buf-1)) > 0) {
for(p=buf; nl>0 && p<&buf[n]; p++)
if(*p=='\n')
nl--;
r += n;
*p = 0;
dprint("%s", buf);
}
if (r<l-1)
dprint("(sam: more in %s)\n", errfile);
close(f);
}
}else
remove((char *)errfile);
}
|