Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include <plumb.h>
11 #include <9pclient.h>
12 #include "dat.h"
13 #include "fns.h"
15 Buffer snarfbuf;
17 /*
18 * These functions get called as:
19 *
20 * fn(et, t, argt, flag1, flag1, flag2, s, n);
21 *
22 * Where the arguments are:
23 *
24 * et: the Text* in which the executing event (click) occurred
25 * t: the Text* containing the current selection (Edit, Cut, Snarf, Paste)
26 * argt: the Text* containing the argument for a 2-1 click.
27 * e->flag1: from Exectab entry
28 * e->flag2: from Exectab entry
29 * s: the command line remainder (e.g., "x" if executing "Dump x")
30 * n: length of s (s is *not* NUL-terminated)
31 */
33 void doabort(Text*, Text*, Text*, int, int, Rune*, int);
34 void del(Text*, Text*, Text*, int, int, Rune*, int);
35 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
36 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
37 void dump(Text*, Text*, Text*, int, int, Rune*, int);
38 void edit(Text*, Text*, Text*, int, int, Rune*, int);
39 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
40 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
41 void get(Text*, Text*, Text*, int, int, Rune*, int);
42 void id(Text*, Text*, Text*, int, int, Rune*, int);
43 void incl(Text*, Text*, Text*, int, int, Rune*, int);
44 void indent(Text*, Text*, Text*, int, int, Rune*, int);
45 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
46 void local(Text*, Text*, Text*, int, int, Rune*, int);
47 void look(Text*, Text*, Text*, int, int, Rune*, int);
48 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
49 void paste(Text*, Text*, Text*, int, int, Rune*, int);
50 void put(Text*, Text*, Text*, int, int, Rune*, int);
51 void putall(Text*, Text*, Text*, int, int, Rune*, int);
52 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
53 void sort(Text*, Text*, Text*, int, int, Rune*, int);
54 void tab(Text*, Text*, Text*, int, int, Rune*, int);
55 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
57 typedef struct Exectab Exectab;
58 struct Exectab
59 {
60 Rune *name;
61 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
62 int mark;
63 int flag1;
64 int flag2;
65 };
67 static Rune LAbort[] = { 'A', 'b', 'o', 'r', 't', 0 };
68 static Rune LCut[] = { 'C', 'u', 't', 0 };
69 static Rune LDel[] = { 'D', 'e', 'l', 0 };
70 static Rune LDelcol[] = { 'D', 'e', 'l', 'c', 'o', 'l', 0 };
71 static Rune LDelete[] = { 'D', 'e', 'l', 'e', 't', 'e', 0 };
72 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
73 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
74 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
75 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
76 static Rune LGet[] = { 'G', 'e', 't', 0 };
77 static Rune LID[] = { 'I', 'D', 0 };
78 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
79 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
80 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
81 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
82 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
83 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
84 static Rune LNew[] = { 'N', 'e', 'w', 0 };
85 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
86 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
87 static Rune LPut[] = { 'P', 'u', 't', 0 };
88 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
89 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
90 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
91 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
92 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
93 static Rune LTab[] = { 'T', 'a', 'b', 0 };
94 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
95 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
97 Exectab exectab[] = {
98 { LAbort, doabort, FALSE, XXX, XXX, },
99 { LCut, cut, TRUE, TRUE, TRUE },
100 { LDel, del, FALSE, FALSE, XXX },
101 { LDelcol, delcol, FALSE, XXX, XXX },
102 { LDelete, del, FALSE, TRUE, XXX },
103 { LDump, dump, FALSE, TRUE, XXX },
104 { LEdit, edit, FALSE, XXX, XXX },
105 { LExit, xexit, FALSE, XXX, XXX },
106 { LFont, fontx, FALSE, XXX, XXX },
107 { LGet, get, FALSE, TRUE, XXX },
108 { LID, id, FALSE, XXX, XXX },
109 { LIncl, incl, FALSE, XXX, XXX },
110 { LIndent, indent, FALSE, XXX, XXX },
111 { LKill, xkill, FALSE, XXX, XXX },
112 { LLoad, dump, FALSE, FALSE, XXX },
113 { LLocal, local, FALSE, XXX, XXX },
114 { LLook, look, FALSE, XXX, XXX },
115 { LNew, new, FALSE, XXX, XXX },
116 { LNewcol, newcol, FALSE, XXX, XXX },
117 { LPaste, paste, TRUE, TRUE, XXX },
118 { LPut, put, FALSE, XXX, XXX },
119 { LPutall, putall, FALSE, XXX, XXX },
120 { LRedo, undo, FALSE, FALSE, XXX },
121 { LSend, sendx, TRUE, XXX, XXX },
122 { LSnarf, cut, FALSE, TRUE, FALSE },
123 { LSort, sort, FALSE, XXX, XXX },
124 { LTab, tab, FALSE, XXX, XXX },
125 { LUndo, undo, FALSE, TRUE, XXX },
126 { LZerox, zeroxx, FALSE, XXX, XXX },
127 { nil, 0, 0, 0, 0 }
128 };
130 Exectab*
131 lookup(Rune *r, int n)
133 Exectab *e;
134 int nr;
136 r = skipbl(r, n, &n);
137 if(n == 0)
138 return nil;
139 findbl(r, n, &nr);
140 nr = n-nr;
141 for(e=exectab; e->name; e++)
142 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
143 return e;
144 return nil;
147 int
148 isexecc(int c)
150 if(isfilec(c))
151 return 1;
152 return c=='<' || c=='|' || c=='>';
155 void
156 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
158 uint q0, q1;
159 Rune *r, *s;
160 char *b, *a, *aa;
161 Exectab *e;
162 int c, n, f;
163 Runestr dir;
165 q0 = aq0;
166 q1 = aq1;
167 if(q1 == q0){ /* expand to find word (actually file name) */
168 /* if in selection, choose selection */
169 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
170 q0 = t->q0;
171 q1 = t->q1;
172 }else{
173 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
174 q1++;
175 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
176 q0--;
177 if(q1 == q0)
178 return;
181 r = runemalloc(q1-q0);
182 bufread(&t->file->b, q0, r, q1-q0);
183 e = lookup(r, q1-q0);
184 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
185 f = 0;
186 if(e)
187 f |= 1;
188 if(q0!=aq0 || q1!=aq1){
189 bufread(&t->file->b, aq0, r, aq1-aq0);
190 f |= 2;
192 aa = getbytearg(argt, TRUE, TRUE, &a);
193 if(a){
194 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
195 free(aa);
196 free(a);
197 warning(nil, "argument string too long\n");
198 return;
200 f |= 8;
202 c = 'x';
203 if(t->what == Body)
204 c = 'X';
205 n = aq1-aq0;
206 if(n <= EVENTSIZE)
207 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
208 else
209 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
210 if(q0!=aq0 || q1!=aq1){
211 n = q1-q0;
212 bufread(&t->file->b, q0, r, n);
213 if(n <= EVENTSIZE)
214 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
215 else
216 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
218 if(a){
219 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
220 if(aa)
221 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
222 else
223 winevent(t->w, "%c0 0 0 0 \n", c);
225 free(r);
226 free(aa);
227 free(a);
228 return;
230 if(e){
231 if(e->mark && seltext!=nil)
232 if(seltext->what == Body){
233 seq++;
234 filemark(seltext->w->body.file);
236 s = skipbl(r, q1-q0, &n);
237 s = findbl(s, n, &n);
238 s = skipbl(s, n, &n);
239 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
240 free(r);
241 return;
244 b = runetobyte(r, q1-q0);
245 free(r);
246 dir = dirname(t, nil, 0);
247 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
248 free(dir.r);
249 dir.r = nil;
250 dir.nr = 0;
252 aa = getbytearg(argt, TRUE, TRUE, &a);
253 if(t->w)
254 incref(&t->w->ref);
255 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
258 char*
259 printarg(Text *argt, uint q0, uint q1)
261 char *buf;
263 if(argt->what!=Body || argt->file->name==nil)
264 return nil;
265 buf = emalloc(argt->file->nname+32);
266 if(q0 == q1)
267 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
268 else
269 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
270 return buf;
273 char*
274 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
276 int n;
277 Expand e;
278 char *a;
280 *rp = nil;
281 *nrp = 0;
282 if(argt == nil)
283 return nil;
284 a = nil;
285 textcommit(argt, TRUE);
286 if(expand(argt, argt->q0, argt->q1, &e)){
287 free(e.bname);
288 if(e.nname && dofile){
289 e.name = runerealloc(e.name, e.nname+1);
290 if(doaddr)
291 a = printarg(argt, e.q0, e.q1);
292 *rp = e.name;
293 *nrp = e.nname;
294 return a;
296 free(e.name);
297 }else{
298 e.q0 = argt->q0;
299 e.q1 = argt->q1;
301 n = e.q1 - e.q0;
302 *rp = runemalloc(n+1);
303 bufread(&argt->file->b, e.q0, *rp, n);
304 if(doaddr)
305 a = printarg(argt, e.q0, e.q1);
306 *nrp = n;
307 return a;
310 char*
311 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
313 Rune *r;
314 int n;
315 char *aa;
317 *bp = nil;
318 aa = getarg(argt, doaddr, dofile, &r, &n);
319 if(r == nil)
320 return nil;
321 *bp = runetobyte(r, n);
322 free(r);
323 return aa;
326 void
327 doabort(Text *__0, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
329 static int n;
331 USED(__0);
332 USED(_0);
333 USED(_1);
334 USED(_2);
335 USED(_3);
336 USED(_4);
337 USED(_5);
339 if(n++ == 0)
340 warning(nil, "executing Abort again will call abort()\n");
341 else
342 abort();
345 void
346 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
348 Column *c;
350 USED(_0);
351 USED(_1);
352 USED(_2);
353 USED(_3);
354 USED(_4);
355 USED(_5);
357 c = rowadd(et->row, nil, -1);
358 if(c)
359 winsettag(coladd(c, nil, nil, -1));
362 void
363 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
365 int i;
366 Column *c;
367 Window *w;
369 USED(_0);
370 USED(_1);
371 USED(_2);
372 USED(_3);
373 USED(_4);
374 USED(_5);
376 c = et->col;
377 if(c==nil || colclean(c)==0)
378 return;
379 for(i=0; i<c->nw; i++){
380 w = c->w[i];
381 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
382 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
383 return;
386 rowclose(et->col->row, et->col, TRUE);
389 void
390 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
392 USED(_0);
393 USED(_1);
394 USED(_2);
395 USED(_3);
396 USED(_4);
398 if(et->col==nil || et->w == nil)
399 return;
400 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
401 colclose(et->col, et->w, TRUE);
404 void
405 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
407 USED(_0);
408 USED(_1);
409 USED(_2);
410 USED(_3);
411 USED(_4);
412 USED(_5);
414 if(et->col)
415 colsort(et->col);
418 uint
419 seqof(Window *w, int isundo)
421 /* if it's undo, see who changed with us */
422 if(isundo)
423 return w->body.file->seq;
424 /* if it's redo, see who we'll be sync'ed up with */
425 return fileredoseq(w->body.file);
428 void
429 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
431 int i, j;
432 Column *c;
433 Window *w;
434 uint seq;
436 USED(_0);
437 USED(_1);
438 USED(_2);
439 USED(_3);
440 USED(_4);
442 if(et==nil || et->w== nil)
443 return;
444 seq = seqof(et->w, flag1);
445 if(seq == 0){
446 /* nothing to undo */
447 return;
449 /*
450 * Undo the executing window first. Its display will update. other windows
451 * in the same file will not call show() and jump to a different location in the file.
452 * Simultaneous changes to other files will be chaotic, however.
453 */
454 winundo(et->w, flag1);
455 for(i=0; i<row.ncol; i++){
456 c = row.col[i];
457 for(j=0; j<c->nw; j++){
458 w = c->w[j];
459 if(w == et->w)
460 continue;
461 if(seqof(w, flag1) == seq)
462 winundo(w, flag1);
467 char*
468 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
470 char *s;
471 Rune *r;
472 int i, n, promote;
473 Runestr dir;
475 getarg(argt, FALSE, TRUE, &r, &n);
476 promote = FALSE;
477 if(r == nil)
478 promote = TRUE;
479 else if(isput){
480 /* if are doing a Put, want to synthesize name even for non-existent file */
481 /* best guess is that file name doesn't contain a slash */
482 promote = TRUE;
483 for(i=0; i<n; i++)
484 if(r[i] == '/'){
485 promote = FALSE;
486 break;
488 if(promote){
489 t = argt;
490 arg = r;
491 narg = n;
494 if(promote){
495 n = narg;
496 if(n <= 0){
497 s = runetobyte(t->file->name, t->file->nname);
498 return s;
500 /* prefix with directory name if necessary */
501 dir.r = nil;
502 dir.nr = 0;
503 if(n>0 && arg[0]!='/'){
504 dir = dirname(t, nil, 0);
505 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
506 free(dir.r);
507 dir.r = nil;
508 dir.nr = 0;
511 if(dir.r){
512 r = runemalloc(dir.nr+n+1);
513 runemove(r, dir.r, dir.nr);
514 free(dir.r);
515 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
516 r[dir.nr++] = '/';
517 runemove(r+dir.nr, arg, n);
518 n += dir.nr;
519 }else{
520 r = runemalloc(n+1);
521 runemove(r, arg, n);
524 s = runetobyte(r, n);
525 free(r);
526 if(strlen(s) == 0){
527 free(s);
528 s = nil;
530 return s;
533 void
534 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
536 Window *nw;
537 int c, locked;
539 USED(_1);
540 USED(_2);
541 USED(_3);
542 USED(_4);
543 USED(_5);
545 locked = FALSE;
546 if(t!=nil && t->w!=nil && t->w!=et->w){
547 locked = TRUE;
548 c = 'M';
549 if(et->w)
550 c = et->w->owner;
551 winlock(t->w, c);
553 if(t == nil)
554 t = et;
555 if(t==nil || t->w==nil)
556 return;
557 t = &t->w->body;
558 if(t->w->isdir)
559 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
560 else{
561 nw = coladd(t->w->col, nil, t->w, -1);
562 /* ugly: fix locks so w->unlock works */
563 winlock1(nw, t->w->owner);
565 if(locked)
566 winunlock(t->w);
569 void
570 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
572 char *name;
573 Rune *r;
574 int i, n, dirty, samename, isdir;
575 Window *w;
576 Text *u;
577 Dir *d;
579 USED(_0);
581 if(flag1)
582 if(et==nil || et->w==nil)
583 return;
584 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
585 return;
586 w = et->w;
587 t = &w->body;
588 name = getname(t, argt, arg, narg, FALSE);
589 if(name == nil){
590 warning(nil, "no file name\n");
591 return;
593 if(t->file->ntext>1){
594 d = dirstat(name);
595 isdir = (d!=nil && (d->qid.type & QTDIR));
596 free(d);
597 if(isdir){
598 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
599 return;
602 r = bytetorune(name, &n);
603 for(i=0; i<t->file->ntext; i++){
604 u = t->file->text[i];
605 /* second and subsequent calls with zero an already empty buffer, but OK */
606 textreset(u);
607 windirfree(u->w);
609 samename = runeeq(r, n, t->file->name, t->file->nname);
610 textload(t, 0, name, samename);
611 if(samename){
612 t->file->mod = FALSE;
613 dirty = FALSE;
614 }else{
615 t->file->mod = TRUE;
616 dirty = TRUE;
618 for(i=0; i<t->file->ntext; i++)
619 t->file->text[i]->w->dirty = dirty;
620 free(name);
621 free(r);
622 winsettag(w);
623 t->file->unread = FALSE;
624 for(i=0; i<t->file->ntext; i++){
625 u = t->file->text[i];
626 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
627 textscrdraw(u);
631 void
632 putfile(File *f, int q0, int q1, Rune *namer, int nname)
634 uint n, m;
635 Rune *r;
636 char *s, *name;
637 int i, fd, q;
638 Dir *d, *d1;
639 Window *w;
640 int isapp;
642 w = f->curtext->w;
643 name = runetobyte(namer, nname);
644 d = dirstat(name);
645 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
646 /* f->mtime+1 because when talking over NFS it's often off by a second */
647 if(f->dev!=d->dev || f->qidpath!=d->qid.path || abs(f->mtime-d->mtime) > 1){
648 if(f->unread)
649 warning(nil, "%s not written; file already exists\n", name);
650 else
651 warning(nil, "%s modified%s%s since last read\n\twas %t; now %t\n", name, d->muid[0]?" by ":"", d->muid, f->mtime, d->mtime);
652 f->dev = d->dev;
653 f->qidpath = d->qid.path;
654 f->mtime = d->mtime;
655 goto Rescue1;
658 fd = create(name, OWRITE, 0666);
659 if(fd < 0){
660 warning(nil, "can't create file %s: %r\n", name);
661 goto Rescue1;
663 r = fbufalloc();
664 s = fbufalloc();
665 free(d);
666 d = dirfstat(fd);
667 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
668 if(isapp){
669 warning(nil, "%s not written; file is append only\n", name);
670 goto Rescue2;
673 for(q=q0; q<q1; q+=n){
674 n = q1 - q;
675 if(n > BUFSIZE/UTFmax)
676 n = BUFSIZE/UTFmax;
677 bufread(&f->b, q, r, n);
678 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
679 if(write(fd, s, m) != m){
680 warning(nil, "can't write file %s: %r\n", name);
681 goto Rescue2;
684 if(runeeq(namer, nname, f->name, f->nname)){
685 if(q0!=0 || q1!=f->b.nc){
686 f->mod = TRUE;
687 w->dirty = TRUE;
688 f->unread = TRUE;
689 }else{
690 // In case the file is on NFS, reopen the fd
691 // before dirfstat to cause the attribute cache
692 // to be updated (otherwise the mtime in the
693 // dirfstat below will be stale and not match
694 // what NFS sees). The file is already written,
695 // so this should be a no-op when not on NFS.
696 // Opening for OWRITE (but no truncation)
697 // in case we don't have read permission.
698 // (The create above worked, so we probably
699 // still have write permission.)
700 close(fd);
701 fd = open(name, OWRITE);
703 d1 = dirfstat(fd);
704 if(d1 != nil){
705 free(d);
706 d = d1;
708 f->qidpath = d->qid.path;
709 f->dev = d->dev;
710 f->mtime = d->mtime;
711 f->mod = FALSE;
712 w->dirty = FALSE;
713 f->unread = FALSE;
715 for(i=0; i<f->ntext; i++){
716 f->text[i]->w->putseq = f->seq;
717 f->text[i]->w->dirty = w->dirty;
720 fbuffree(s);
721 fbuffree(r);
722 free(d);
723 free(namer);
724 free(name);
725 close(fd);
726 winsettag(w);
727 return;
729 Rescue2:
730 fbuffree(s);
731 fbuffree(r);
732 close(fd);
733 /* fall through */
735 Rescue1:
736 free(d);
737 free(namer);
738 free(name);
741 void
742 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
744 int nname;
745 Rune *namer;
746 Window *w;
747 File *f;
748 char *name;
750 USED(_0);
751 USED(_1);
752 USED(_2);
754 if(et==nil || et->w==nil || et->w->isdir)
755 return;
756 w = et->w;
757 f = w->body.file;
758 name = getname(&w->body, argt, arg, narg, TRUE);
759 if(name == nil){
760 warning(nil, "no file name\n");
761 return;
763 namer = bytetorune(name, &nname);
764 putfile(f, 0, f->b.nc, namer, nname);
765 free(name);
768 void
769 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
771 char *name;
773 USED(_0);
774 USED(_1);
775 USED(_2);
777 if(narg)
778 name = runetobyte(arg, narg);
779 else
780 getbytearg(argt, FALSE, TRUE, &name);
781 if(isdump)
782 rowdump(&row, name);
783 else
784 rowload(&row, name, FALSE);
785 free(name);
788 void
789 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
791 uint q0, q1, n, locked, c;
792 Rune *r;
794 USED(_0);
795 USED(_2);
796 USED(_3);
798 /*
799 * if not executing a mouse chord (et != t) and snarfing (dosnarf)
800 * and executed Cut or Snarf in window tag (et->w != nil),
801 * then use the window body selection or the tag selection
802 * or do nothing at all.
803 */
804 if(et!=t && dosnarf && et->w!=nil){
805 if(et->w->body.q1>et->w->body.q0){
806 t = &et->w->body;
807 if(docut)
808 filemark(t->file); /* seq has been incremented by execute */
809 }else if(et->w->tag.q1>et->w->tag.q0)
810 t = &et->w->tag;
811 else
812 t = nil;
814 if(t == nil) /* no selection */
815 return;
817 locked = FALSE;
818 if(t->w!=nil && et->w!=t->w){
819 locked = TRUE;
820 c = 'M';
821 if(et->w)
822 c = et->w->owner;
823 winlock(t->w, c);
825 if(t->q0 == t->q1){
826 if(locked)
827 winunlock(t->w);
828 return;
830 if(dosnarf){
831 q0 = t->q0;
832 q1 = t->q1;
833 bufdelete(&snarfbuf, 0, snarfbuf.nc);
834 r = fbufalloc();
835 while(q0 < q1){
836 n = q1 - q0;
837 if(n > RBUFSIZE)
838 n = RBUFSIZE;
839 bufread(&t->file->b, q0, r, n);
840 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
841 q0 += n;
843 fbuffree(r);
844 acmeputsnarf();
846 if(docut){
847 textdelete(t, t->q0, t->q1, TRUE);
848 textsetselect(t, t->q0, t->q0);
849 if(t->w){
850 textscrdraw(t);
851 winsettag(t->w);
853 }else if(dosnarf) /* Snarf command */
854 argtext = t;
855 if(locked)
856 winunlock(t->w);
859 void
860 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
862 int c;
863 uint q, q0, q1, n;
864 Rune *r;
866 USED(_0);
867 USED(_1);
868 USED(_2);
870 /* if(tobody), use body of executing window (Paste or Send command) */
871 if(tobody && et!=nil && et->w!=nil){
872 t = &et->w->body;
873 filemark(t->file); /* seq has been incremented by execute */
875 if(t == nil)
876 return;
878 acmegetsnarf();
879 if(t==nil || snarfbuf.nc==0)
880 return;
881 if(t->w!=nil && et->w!=t->w){
882 c = 'M';
883 if(et->w)
884 c = et->w->owner;
885 winlock(t->w, c);
887 cut(t, t, nil, FALSE, TRUE, nil, 0);
888 q = 0;
889 q0 = t->q0;
890 q1 = t->q0+snarfbuf.nc;
891 r = fbufalloc();
892 while(q0 < q1){
893 n = q1 - q0;
894 if(n > RBUFSIZE)
895 n = RBUFSIZE;
896 if(r == nil)
897 r = runemalloc(n);
898 bufread(&snarfbuf, q, r, n);
899 textinsert(t, q0, r, n, TRUE);
900 q += n;
901 q0 += n;
903 fbuffree(r);
904 if(selectall)
905 textsetselect(t, t->q0, q1);
906 else
907 textsetselect(t, q1, q1);
908 if(t->w){
909 textscrdraw(t);
910 winsettag(t->w);
912 if(t->w!=nil && et->w!=t->w)
913 winunlock(t->w);
916 void
917 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
919 Rune *r;
920 int n;
922 USED(_0);
923 USED(_1);
925 if(et && et->w){
926 t = &et->w->body;
927 if(narg > 0){
928 search(t, arg, narg);
929 return;
931 getarg(argt, FALSE, FALSE, &r, &n);
932 if(r == nil){
933 n = t->q1-t->q0;
934 r = runemalloc(n);
935 bufread(&t->file->b, t->q0, r, n);
937 search(t, r, n);
938 free(r);
942 static Rune Lnl[] = { '\n', 0 };
944 void
945 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
947 USED(_0);
948 USED(_1);
949 USED(_2);
950 USED(_3);
951 USED(_4);
953 if(et->w==nil)
954 return;
955 t = &et->w->body;
956 if(t->q0 != t->q1)
957 cut(t, t, nil, TRUE, FALSE, nil, 0);
958 textsetselect(t, t->file->b.nc, t->file->b.nc);
959 paste(t, t, nil, TRUE, TRUE, nil, 0);
960 if(textreadc(t, t->file->b.nc-1) != '\n'){
961 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
962 textsetselect(t, t->file->b.nc, t->file->b.nc);
963 textshow(t, t->q1, t->q1, 1);
967 void
968 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
970 Rune *r;
971 int len;
973 USED(_0);
974 USED(_1);
975 USED(_2);
977 if(et == nil)
978 return;
979 getarg(argt, FALSE, TRUE, &r, &len);
980 seq++;
981 if(r != nil){
982 editcmd(et, r, len);
983 free(r);
984 }else
985 editcmd(et, arg, narg);
988 void
989 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
991 USED(et);
992 USED(_0);
993 USED(_1);
994 USED(_2);
995 USED(_3);
996 USED(_4);
997 USED(_5);
999 if(rowclean(&row)){
1000 sendul(cexit, 0);
1001 threadexits(nil);
1005 void
1006 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1008 int i, j, e;
1009 Window *w;
1010 Column *c;
1011 char *a;
1013 USED(et);
1014 USED(_0);
1015 USED(_1);
1016 USED(_2);
1017 USED(_3);
1018 USED(_4);
1019 USED(_5);
1021 for(i=0; i<row.ncol; i++){
1022 c = row.col[i];
1023 for(j=0; j<c->nw; j++){
1024 w = c->w[j];
1025 if(w->isscratch || w->isdir || w->body.file->nname==0)
1026 continue;
1027 if(w->nopen[QWevent] > 0)
1028 continue;
1029 a = runetobyte(w->body.file->name, w->body.file->nname);
1030 e = access(a, 0);
1031 if(w->body.file->mod || w->body.ncache)
1032 if(e < 0)
1033 warning(nil, "no auto-Put of %s: %r\n", a);
1034 else{
1035 wincommit(w, &w->body);
1036 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1038 free(a);
1044 void
1045 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1047 USED(_0);
1048 USED(_1);
1049 USED(_2);
1050 USED(_3);
1051 USED(_4);
1052 USED(_5);
1054 if(et && et->w)
1055 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1058 void
1059 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1061 char *a, *aa;
1062 Runestr dir;
1064 USED(_0);
1065 USED(_1);
1066 USED(_2);
1068 aa = getbytearg(argt, TRUE, TRUE, &a);
1070 dir = dirname(et, nil, 0);
1071 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1072 free(dir.r);
1073 dir.r = nil;
1074 dir.nr = 0;
1076 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1079 void
1080 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1082 Rune *a, *cmd, *r;
1083 int na;
1085 USED(_0);
1086 USED(_1);
1087 USED(_2);
1088 USED(_3);
1090 getarg(argt, FALSE, FALSE, &r, &na);
1091 if(r)
1092 xkill(nil, nil, nil, 0, 0, r, na);
1093 /* loop condition: *arg is not a blank */
1094 for(;;){
1095 a = findbl(arg, narg, &na);
1096 if(a == arg)
1097 break;
1098 cmd = runemalloc(narg-na+1);
1099 runemove(cmd, arg, narg-na);
1100 sendp(ckill, cmd);
1101 arg = skipbl(a, na, &narg);
1105 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1106 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1108 void
1109 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1111 Rune *a, *r, *flag, *file;
1112 int na, nf;
1113 char *aa;
1114 Reffont *newfont;
1115 Dirlist *dp;
1116 int i, fix;
1118 USED(_0);
1119 USED(_1);
1121 if(et==nil || et->w==nil)
1122 return;
1123 t = &et->w->body;
1124 flag = nil;
1125 file = nil;
1126 /* loop condition: *arg is not a blank */
1127 nf = 0;
1128 for(;;){
1129 a = findbl(arg, narg, &na);
1130 if(a == arg)
1131 break;
1132 r = runemalloc(narg-na+1);
1133 runemove(r, arg, narg-na);
1134 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1135 free(flag);
1136 flag = r;
1137 }else{
1138 free(file);
1139 file = r;
1140 nf = narg-na;
1142 arg = skipbl(a, na, &narg);
1144 getarg(argt, FALSE, TRUE, &r, &na);
1145 if(r)
1146 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1147 free(flag);
1148 flag = r;
1149 }else{
1150 free(file);
1151 file = r;
1152 nf = na;
1154 fix = 1;
1155 if(flag)
1156 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1157 else if(file == nil){
1158 newfont = rfget(FALSE, FALSE, FALSE, nil);
1159 if(newfont)
1160 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1162 if(file){
1163 aa = runetobyte(file, nf);
1164 newfont = rfget(fix, flag!=nil, FALSE, aa);
1165 free(aa);
1166 }else
1167 newfont = rfget(fix, FALSE, FALSE, nil);
1168 if(newfont){
1169 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1170 rfclose(t->reffont);
1171 t->reffont = newfont;
1172 t->fr.font = newfont->f;
1173 frinittick(&t->fr);
1174 if(t->w->isdir){
1175 t->all.min.x++; /* force recolumnation; disgusting! */
1176 for(i=0; i<t->w->ndl; i++){
1177 dp = t->w->dlp[i];
1178 aa = runetobyte(dp->r, dp->nr);
1179 dp->wid = stringwidth(newfont->f, aa);
1180 free(aa);
1183 /* avoid shrinking of window due to quantization */
1184 colgrow(t->w->col, t->w, -1);
1186 free(file);
1187 free(flag);
1190 void
1191 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1193 Rune *a, *r;
1194 Window *w;
1195 int na, n, len;
1197 USED(_0);
1198 USED(_1);
1199 USED(_2);
1201 if(et==nil || et->w==nil)
1202 return;
1203 w = et->w;
1204 n = 0;
1205 getarg(argt, FALSE, TRUE, &r, &len);
1206 if(r){
1207 n++;
1208 winaddincl(w, r, len);
1210 /* loop condition: *arg is not a blank */
1211 for(;;){
1212 a = findbl(arg, narg, &na);
1213 if(a == arg)
1214 break;
1215 r = runemalloc(narg-na+1);
1216 runemove(r, arg, narg-na);
1217 n++;
1218 winaddincl(w, r, narg-na);
1219 arg = skipbl(a, na, &narg);
1221 if(n==0 && w->nincl){
1222 for(n=w->nincl; --n>=0; )
1223 warning(nil, "%S ", w->incl[n]);
1224 warning(nil, "\n");
1228 static Rune LON[] = { 'O', 'N', 0 };
1229 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1230 static Rune Lon[] = { 'o', 'n', 0 };
1232 enum {
1233 IGlobal = -2,
1234 IError = -1,
1235 Ion = 0,
1236 Ioff = 1
1239 static int
1240 indentval(Rune *s, int n)
1242 if(n < 2)
1243 return IError;
1244 if(runestrncmp(s, LON, n) == 0){
1245 globalautoindent = TRUE;
1246 warning(nil, "Indent ON\n");
1247 return IGlobal;
1249 if(runestrncmp(s, LOFF, n) == 0){
1250 globalautoindent = FALSE;
1251 warning(nil, "Indent OFF\n");
1252 return IGlobal;
1254 return runestrncmp(s, Lon, n) == 0;
1257 static void
1258 fixindent(Window *w, void *arg)
1260 USED(arg);
1261 w->autoindent = globalautoindent;
1264 void
1265 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1267 Rune *a, *r;
1268 Window *w;
1269 int na, len, autoindent;
1271 USED(_0);
1272 USED(_1);
1273 USED(_2);
1275 w = nil;
1276 if(et!=nil && et->w!=nil)
1277 w = et->w;
1278 autoindent = IError;
1279 getarg(argt, FALSE, TRUE, &r, &len);
1280 if(r!=nil && len>0)
1281 autoindent = indentval(r, len);
1282 else{
1283 a = findbl(arg, narg, &na);
1284 if(a != arg)
1285 autoindent = indentval(arg, narg-na);
1287 if(autoindent == IGlobal)
1288 allwindows(fixindent, nil);
1289 else if(w != nil && autoindent >= 0)
1290 w->autoindent = autoindent;
1293 void
1294 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1296 Rune *a, *r;
1297 Window *w;
1298 int na, len, tab;
1299 char *p;
1301 USED(_0);
1302 USED(_1);
1303 USED(_2);
1305 if(et==nil || et->w==nil)
1306 return;
1307 w = et->w;
1308 getarg(argt, FALSE, TRUE, &r, &len);
1309 tab = 0;
1310 if(r!=nil && len>0){
1311 p = runetobyte(r, len);
1312 if('0'<=p[0] && p[0]<='9')
1313 tab = atoi(p);
1314 free(p);
1315 }else{
1316 a = findbl(arg, narg, &na);
1317 if(a != arg){
1318 p = runetobyte(arg, narg-na);
1319 if('0'<=p[0] && p[0]<='9')
1320 tab = atoi(p);
1321 free(p);
1324 if(tab > 0){
1325 if(w->body.tabstop != tab){
1326 w->body.tabstop = tab;
1327 winresize(w, w->r, FALSE, TRUE);
1329 }else
1330 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1333 void
1334 runproc(void *argvp)
1336 /* args: */
1337 Window *win;
1338 char *s;
1339 Rune *rdir;
1340 int ndir;
1341 int newns;
1342 char *argaddr;
1343 char *arg;
1344 Command *c;
1345 Channel *cpid;
1346 int iseditcmd;
1347 /* end of args */
1348 char *e, *t, *name, *filename, *dir, **av, *news;
1349 Rune r, **incl;
1350 int ac, w, inarg, i, n, fd, nincl, winid;
1351 int sfd[3];
1352 int pipechar;
1353 char buf[512];
1354 int olddir;
1355 int ret;
1356 /*static void *parg[2]; */
1357 char *rcarg[4];
1358 void **argv;
1359 CFsys *fs;
1361 threadsetname("runproc");
1363 argv = argvp;
1364 win = argv[0];
1365 s = argv[1];
1366 rdir = argv[2];
1367 ndir = (uintptr)argv[3];
1368 newns = (uintptr)argv[4];
1369 argaddr = argv[5];
1370 arg = argv[6];
1371 c = argv[7];
1372 cpid = argv[8];
1373 iseditcmd = (uintptr)argv[9];
1374 free(argv);
1376 t = s;
1377 while(*t==' ' || *t=='\n' || *t=='\t')
1378 t++;
1379 for(e=t; *e; e++)
1380 if(*e==' ' || *e=='\n' || *e=='\t' )
1381 break;
1382 name = emalloc((e-t)+2);
1383 memmove(name, t, e-t);
1384 name[e-t] = 0;
1385 e = utfrrune(name, '/');
1386 if(e)
1387 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1388 strcat(name, " "); /* add blank here for ease in waittask */
1389 c->name = bytetorune(name, &c->nname);
1390 free(name);
1391 pipechar = 0;
1392 if(*t=='<' || *t=='|' || *t=='>')
1393 pipechar = *t++;
1394 c->iseditcmd = iseditcmd;
1395 c->text = s;
1396 if(newns){
1397 nincl = 0;
1398 incl = nil;
1399 if(win){
1400 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1401 nincl = win->nincl;
1402 if(nincl > 0){
1403 incl = emalloc(nincl*sizeof(Rune*));
1404 for(i=0; i<nincl; i++){
1405 n = runestrlen(win->incl[i]);
1406 incl[i] = runemalloc(n+1);
1407 runemove(incl[i], win->incl[i], n);
1410 winid = win->id;
1411 }else{
1412 filename = nil;
1413 winid = 0;
1414 if(activewin)
1415 winid = activewin->id;
1417 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1418 sprint(buf, "%d", winid);
1419 putenv("winid", buf);
1421 if(filename){
1422 putenv("%", filename);
1423 free(filename);
1425 c->md = fsysmount(rdir, ndir, incl, nincl);
1426 if(c->md == nil){
1427 fprint(2, "child: can't allocate mntdir: %r\n");
1428 threadexits("fsysmount");
1430 sprint(buf, "%d", c->md->id);
1431 if((fs = nsmount("acme", buf)) == nil){
1432 fprint(2, "child: can't mount acme: %r\n");
1433 fsysdelid(c->md);
1434 c->md = nil;
1435 threadexits("nsmount");
1437 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1438 sprint(buf, "%d/rdsel", winid);
1439 sfd[0] = fsopenfd(fs, buf, OREAD);
1440 }else
1441 sfd[0] = open("/dev/null", OREAD);
1442 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1443 if(iseditcmd){
1444 if(winid > 0)
1445 sprint(buf, "%d/editout", winid);
1446 else
1447 sprint(buf, "editout");
1448 }else
1449 sprint(buf, "%d/wrsel", winid);
1450 sfd[1] = fsopenfd(fs, buf, OWRITE);
1451 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1452 }else{
1453 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1454 sfd[2] = sfd[1];
1456 fsunmount(fs);
1457 }else{
1458 rfork(RFFDG|RFNOTEG);
1459 fsysclose();
1460 sfd[0] = open("/dev/null", OREAD);
1461 sfd[1] = open("/dev/null", OWRITE);
1462 sfd[2] = dup(erroutfd, -1);
1464 if(win)
1465 winclose(win);
1467 if(argaddr)
1468 putenv("acmeaddr", argaddr);
1469 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1470 goto Hard;
1471 inarg = FALSE;
1472 for(e=t; *e; e+=w){
1473 w = chartorune(&r, e);
1474 if(r==' ' || r=='\t')
1475 continue;
1476 if(r < ' ')
1477 goto Hard;
1478 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1479 goto Hard;
1480 inarg = TRUE;
1482 if(!inarg)
1483 goto Fail;
1485 ac = 0;
1486 av = nil;
1487 inarg = FALSE;
1488 for(e=t; *e; e+=w){
1489 w = chartorune(&r, e);
1490 if(r==' ' || r=='\t'){
1491 inarg = FALSE;
1492 *e = 0;
1493 continue;
1495 if(!inarg){
1496 inarg = TRUE;
1497 av = realloc(av, (ac+1)*sizeof(char**));
1498 av[ac++] = e;
1501 av = realloc(av, (ac+2)*sizeof(char**));
1502 av[ac++] = arg;
1503 av[ac] = nil;
1504 c->av = av;
1507 * clumsy -- we're not running in a separate thread
1508 * so we have to save the current directory and put
1509 * it back when we're done. if this gets to be a regular
1510 * thing we could change threadexec to take a directory too.
1512 olddir = -1;
1513 if(rdir != nil){
1514 olddir = open(".", OREAD);
1515 dir = runetobyte(rdir, ndir);
1516 chdir(dir); /* ignore error: probably app. window */
1517 free(dir);
1519 ret = threadspawn(sfd, av[0], av);
1520 if(olddir >= 0){
1521 fchdir(olddir);
1522 close(olddir);
1524 if(ret >= 0){
1525 if(cpid)
1526 sendul(cpid, ret);
1527 threadexits("");
1529 /* libthread uses execvp so no need to do this */
1530 #if 0
1531 e = av[0];
1532 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1533 goto Fail;
1534 if(cputype){
1535 sprint(buf, "%s/%s", cputype, av[0]);
1536 procexec(cpid, sfd, buf, av);
1538 sprint(buf, "/bin/%s", av[0]);
1539 procexec(cpid, sfd, buf, av);
1540 #endif
1541 goto Fail;
1543 Hard:
1545 * ugly: set path = (. $cputype /bin)
1546 * should honor $path if unusual.
1548 if(cputype){
1549 n = 0;
1550 memmove(buf+n, ".", 2);
1551 n += 2;
1552 i = strlen(cputype)+1;
1553 memmove(buf+n, cputype, i);
1554 n += i;
1555 memmove(buf+n, "/bin", 5);
1556 n += 5;
1557 fd = create("/env/path", OWRITE, 0666);
1558 write(fd, buf, n);
1559 close(fd);
1562 if(arg){
1563 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1564 if(news){
1565 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1566 free(s);
1567 t = news;
1568 c->text = news;
1571 olddir = -1;
1572 if(rdir != nil){
1573 olddir = open(".", OREAD);
1574 dir = runetobyte(rdir, ndir);
1575 chdir(dir); /* ignore error: probably app. window */
1576 free(dir);
1578 rcarg[0] = "rc";
1579 rcarg[1] = "-c";
1580 rcarg[2] = t;
1581 rcarg[3] = nil;
1582 ret = threadspawn(sfd, rcarg[0], rcarg);
1583 if(olddir >= 0){
1584 fchdir(olddir);
1585 close(olddir);
1587 if(ret >= 0){
1588 if(cpid)
1589 sendul(cpid, ret);
1590 threadexits(nil);
1592 warning(nil, "exec rc: %r\n");
1594 Fail:
1595 /* threadexec hasn't happened, so send a zero */
1596 close(sfd[0]);
1597 close(sfd[1]);
1598 if(sfd[2] != sfd[1])
1599 close(sfd[2]);
1600 sendul(cpid, 0);
1601 threadexits(nil);
1604 void
1605 runwaittask(void *v)
1607 Command *c;
1608 Channel *cpid;
1609 void **a;
1611 threadsetname("runwaittask");
1612 a = v;
1613 c = a[0];
1614 cpid = a[1];
1615 free(a);
1617 c->pid = recvul(cpid);
1618 while(c->pid == ~0);
1619 free(c->av);
1620 if(c->pid != 0) /* successful exec */
1621 sendp(ccommand, c);
1622 else{
1623 if(c->iseditcmd)
1624 sendul(cedit, 0);
1625 free(c->name);
1626 free(c->text);
1627 free(c);
1629 chanfree(cpid);
1632 void
1633 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1635 void **arg;
1636 Command *c;
1637 Channel *cpid;
1639 if(s == nil)
1640 return;
1642 arg = emalloc(10*sizeof(void*));
1643 c = emalloc(sizeof *c);
1644 cpid = chancreate(sizeof(ulong), 0);
1645 chansetname(cpid, "cpid %s", s);
1646 arg[0] = win;
1647 arg[1] = s;
1648 arg[2] = rdir;
1649 arg[3] = (void*)(uintptr)ndir;
1650 arg[4] = (void*)(uintptr)newns;
1651 arg[5] = argaddr;
1652 arg[6] = xarg;
1653 arg[7] = c;
1654 arg[8] = cpid;
1655 arg[9] = (void*)(uintptr)iseditcmd;
1656 threadcreate(runproc, arg, STACK);
1657 /* mustn't block here because must be ready to answer mount() call in run() */
1658 arg = emalloc(2*sizeof(void*));
1659 arg[0] = c;
1660 arg[1] = cpid;
1661 threadcreate(runwaittask, arg, STACK);