-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
343 lines (307 loc) · 5.42 KB
/
cmd.c
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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include "dat.h"
#include "fns.h"
extern Channel *pidc;
Dot dot;
int bound = Bend;
static int epfd[2];
static int
setright(char *s)
{
vlong off, m;
off = atoll(s) * Sampsz;
if(dot.cur > off){
m = Rate * Sampsz;
setjump(off - m >= 0 ? off - m : 0);
}
return setloop(off);
}
static int
setleft(char *s)
{
vlong off;
off = atoll(s) * Sampsz;
if(dot.cur < off)
setjump(off + Sampsz);
return setloop(off);
}
static int
jumpto(char *s)
{
return setjump(atoll(s) * Sampsz);
}
static int
paste(char *)
{
int r;
qlock(&lsync);
r = cpaste(&dot) == 0 ? 1 : -1;
qunlock(&lsync);
return r;
}
static int
copy(char *)
{
ccopy(&dot);
return 0;
}
static int
cut(char *)
{
dprint(nil, "cmd/cut %Δ\n", &dot);
if(dot.from == 0 && dot.to == dot.totalsz){
werrstr("cut: can't cut entire buffer");
return -1;
}
qlock(&lsync);
ccut(&dot);
qunlock(&lsync);
return 1;
}
static int
crop(char *)
{
dprint(nil, "cmd/crop %Δ\n", &dot);
qlock(&lsync);
ccrop(&dot);
qunlock(&lsync);
return 1;
}
static int
writebuf(int fd)
{
int nio;
uchar *b;
usize n, m, k;
Dot d;
d = dot;
d.cur = d.from;
if((nio = iounit(fd)) == 0)
nio = 8192;
for(m=d.to-d.from, b=(uchar*)&d; m>0; m-=n, d.cur+=n){
k = nio < m ? nio : m;
if((b = getslice(&d, k, &k)) == nil || k <= 0){
fprint(2, "writebuf: couldn\'t snarf: %r\n");
return -1;
}
if((n = write(fd, b, k)) != k){
fprint(2, "writebuf: short write not %zd: %r\n", k);
return -1;
}
}
write(fd, b, 0); /* close pipe */
return 0;
}
static void
rc(void *s)
{
close(epfd[1]);
dup(epfd[0], 0);
dup(epfd[0], 1);
close(epfd[0]);
procexecl(pidc, "/bin/rc", "rc", "-c", s, nil);
sysfatal("procexec: %r");
}
static void
wproc(void *efd)
{
int fd;
fd = (intptr)efd;
writebuf(fd);
close(fd);
threadexits(nil);
}
static void
rproc(void *efd)
{
int fd;
Dot d, cd;
Chunk *c;
d = dot;
if(d.from != 0 && d.to != d.totalsz)
d.off = -1;
fd = (intptr)efd;
if((c = loadfile(fd, &cd)) == nil){
fprint(2, "failed reading from pipe: %r");
threadexits("read error");
}
close(fd);
qlock(&lsync);
chold(c, &d);
dot = d;
qunlock(&lsync);
if(paste(nil) < 0)
fprint(2, "paste: %r\n");
redraw(1);
threadexits(nil);
}
static int
pipeline(char *arg, int rr, int wr)
{
if(nslots == 0){
fprint(2, "pipeline: too many backgrounded processes\n");
return -1;
}
if(rr)
reader = 0;
if(pipe(epfd) < 0)
sysfatal("pipe: %r");
if(procrfork(rc, arg, mainstacksize, RFFDG|RFNOTEG|RFNAMEG) < 0)
sysfatal("procrfork: %r");
close(epfd[0]);
if(wr && procrfork(wproc, (int*)dup(epfd[1], -1), mainstacksize, RFFDG|RFNOTEG|RFNAMEG) < 0){
fprint(2, "procrfork: %r\n");
return -1;
}
if(rr && procrfork(rproc, (int*)dup(epfd[1], -1), mainstacksize, RFFDG) < 0){
fprint(2, "procrfork: %r\n");
return -1;
}
close(epfd[1]);
return 0;
}
static int
pipeto(char *arg)
{
return pipeline(arg, 0, 1);
}
static int
pipefrom(char *arg)
{
return pipeline(arg, 1, 0);
}
static int
pipethrough(char *arg)
{
return pipeline(arg, 1, 1);
}
static int
pipeselflessly(char *arg)
{
return pipeline(arg, 0, 0);
}
static int
replicate(char *)
{
static char u[256];
snprint(u, sizeof u, "<[3=0] window -m %s /fd/3", argv0);
return pipeto(u);
}
static int
readfrom(char *s)
{
int fd;
if((fd = open(s, OREAD)) < 0)
return -1;
if(procrfork(rproc, (int*)fd, mainstacksize, RFFDG) < 0){
fprint(2, "procrfork: %r\n");
return -1;
}
return 0;
}
/* the entire string is treated as the filename, ie. spaces
* and any other weird characters will be part of it */
static int
writeto(char *arg)
{
int fd;
if((fd = create(arg, OWRITE, 0664)) < 0){
werrstr("writeto: %r");
return -1;
}
if(procrfork(wproc, (int*)fd, mainstacksize, RFFDG|RFNOTEG|RFNAMEG) < 0){
fprint(2, "procrfork: %r\n");
return -1;
}
close(fd);
return 0;
}
int
cmd(char *s)
{
int n, x;
int (*fn)(char*);
Rune r, r´;
assert(s != nil);
s += chartorune(&r, s);
for(;;){
n = chartorune(&r´, s);
if(r´ == Runeerror){
werrstr("malformed input");
return -1;
}
if(r´ == 0 || r´ != ' ' && r´ != '\t')
break;
s += n;
}
dprint(dot.norris, "current dot=%Δ\n", &dot);
if(reader >= 0){
switch(r){
case '<':
case '^':
case 'c':
case 'd':
//case 'm':
case 'p':
case 'r':
case 'U':
case 'u':
case 'w':
case 'x':
werrstr("still reading from external process\n");
return -1;
}
}
x = 666;
fn = nil;
switch(r){
case 'q': threadexitsall(nil);
case 'D': killreader(); break;
case '<': fn = pipefrom; break;
case '^': fn = pipethrough; break;
case '|': fn = pipeto; break;
case '!': fn = pipeselflessly; break;
case 'L': fn = setleft; break;
case 'R': fn = setright; break;
case 'c': fn = copy; break;
case 'd': fn = cut; break;
case 'j': fn = jumpto; break;
//case 'm': fn = mark; break;
case 'p': fn = paste; break;
case 'r': fn = readfrom; break;
case 's': fn = replicate; break;
case 'U': fn = unpop; break;
case 'u': fn = popop; break;
case 'w': fn = writeto; break;
case 'x': fn = crop; break;
default:
werrstr("unknown command %C", r);
return -1;
}
if(fn != nil){
x = fn(s);
dprint(dot.norris, "final dot=%Δ\n", &dot);
}
return x;
}
static void
advanceone(Dot *d, usize n)
{
usize m;
if(d->cur < d->from || d->cur >= d->to)
d->cur = d->from;
while(n > 0){
m = d->to - d->cur > n ? n : d->to - d->cur;
n -= m;
d->cur += m;
if(d->cur == d->to)
d->cur = d->from;
}
}
void
advance(usize n)
{
advanceone(&dot, n);
}