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);
964 t->iq1 = t->q1;
965 textshow(t, t->q1, t->q1, 1);
968 void
969 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
971 Rune *r;
972 int len;
974 USED(_0);
975 USED(_1);
976 USED(_2);
978 if(et == nil)
979 return;
980 getarg(argt, FALSE, TRUE, &r, &len);
981 seq++;
982 if(r != nil){
983 editcmd(et, r, len);
984 free(r);
985 }else
986 editcmd(et, arg, narg);
989 void
990 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
992 USED(et);
993 USED(_0);
994 USED(_1);
995 USED(_2);
996 USED(_3);
997 USED(_4);
998 USED(_5);
1000 if(rowclean(&row)){
1001 sendul(cexit, 0);
1002 threadexits(nil);
1006 void
1007 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1009 int i, j, e;
1010 Window *w;
1011 Column *c;
1012 char *a;
1014 USED(et);
1015 USED(_0);
1016 USED(_1);
1017 USED(_2);
1018 USED(_3);
1019 USED(_4);
1020 USED(_5);
1022 for(i=0; i<row.ncol; i++){
1023 c = row.col[i];
1024 for(j=0; j<c->nw; j++){
1025 w = c->w[j];
1026 if(w->isscratch || w->isdir || w->body.file->nname==0)
1027 continue;
1028 if(w->nopen[QWevent] > 0)
1029 continue;
1030 a = runetobyte(w->body.file->name, w->body.file->nname);
1031 e = access(a, 0);
1032 if(w->body.file->mod || w->body.ncache)
1033 if(e < 0)
1034 warning(nil, "no auto-Put of %s: %r\n", a);
1035 else{
1036 wincommit(w, &w->body);
1037 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1039 free(a);
1045 void
1046 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1048 USED(_0);
1049 USED(_1);
1050 USED(_2);
1051 USED(_3);
1052 USED(_4);
1053 USED(_5);
1055 if(et && et->w)
1056 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1059 void
1060 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1062 char *a, *aa;
1063 Runestr dir;
1065 USED(_0);
1066 USED(_1);
1067 USED(_2);
1069 aa = getbytearg(argt, TRUE, TRUE, &a);
1071 dir = dirname(et, nil, 0);
1072 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1073 free(dir.r);
1074 dir.r = nil;
1075 dir.nr = 0;
1077 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1080 void
1081 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1083 Rune *a, *cmd, *r;
1084 int na;
1086 USED(_0);
1087 USED(_1);
1088 USED(_2);
1089 USED(_3);
1091 getarg(argt, FALSE, FALSE, &r, &na);
1092 if(r)
1093 xkill(nil, nil, nil, 0, 0, r, na);
1094 /* loop condition: *arg is not a blank */
1095 for(;;){
1096 a = findbl(arg, narg, &na);
1097 if(a == arg)
1098 break;
1099 cmd = runemalloc(narg-na+1);
1100 runemove(cmd, arg, narg-na);
1101 sendp(ckill, cmd);
1102 arg = skipbl(a, na, &narg);
1106 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1107 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1109 void
1110 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1112 Rune *a, *r, *flag, *file;
1113 int na, nf;
1114 char *aa;
1115 Reffont *newfont;
1116 Dirlist *dp;
1117 int i, fix;
1119 USED(_0);
1120 USED(_1);
1122 if(et==nil || et->w==nil)
1123 return;
1124 t = &et->w->body;
1125 flag = nil;
1126 file = nil;
1127 /* loop condition: *arg is not a blank */
1128 nf = 0;
1129 for(;;){
1130 a = findbl(arg, narg, &na);
1131 if(a == arg)
1132 break;
1133 r = runemalloc(narg-na+1);
1134 runemove(r, arg, narg-na);
1135 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1136 free(flag);
1137 flag = r;
1138 }else{
1139 free(file);
1140 file = r;
1141 nf = narg-na;
1143 arg = skipbl(a, na, &narg);
1145 getarg(argt, FALSE, TRUE, &r, &na);
1146 if(r)
1147 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1148 free(flag);
1149 flag = r;
1150 }else{
1151 free(file);
1152 file = r;
1153 nf = na;
1155 fix = 1;
1156 if(flag)
1157 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1158 else if(file == nil){
1159 newfont = rfget(FALSE, FALSE, FALSE, nil);
1160 if(newfont)
1161 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1163 if(file){
1164 aa = runetobyte(file, nf);
1165 newfont = rfget(fix, flag!=nil, FALSE, aa);
1166 free(aa);
1167 }else
1168 newfont = rfget(fix, FALSE, FALSE, nil);
1169 if(newfont){
1170 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1171 rfclose(t->reffont);
1172 t->reffont = newfont;
1173 t->fr.font = newfont->f;
1174 frinittick(&t->fr);
1175 if(t->w->isdir){
1176 t->all.min.x++; /* force recolumnation; disgusting! */
1177 for(i=0; i<t->w->ndl; i++){
1178 dp = t->w->dlp[i];
1179 aa = runetobyte(dp->r, dp->nr);
1180 dp->wid = stringwidth(newfont->f, aa);
1181 free(aa);
1184 /* avoid shrinking of window due to quantization */
1185 colgrow(t->w->col, t->w, -1);
1187 free(file);
1188 free(flag);
1191 void
1192 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1194 Rune *a, *r;
1195 Window *w;
1196 int na, n, len;
1198 USED(_0);
1199 USED(_1);
1200 USED(_2);
1202 if(et==nil || et->w==nil)
1203 return;
1204 w = et->w;
1205 n = 0;
1206 getarg(argt, FALSE, TRUE, &r, &len);
1207 if(r){
1208 n++;
1209 winaddincl(w, r, len);
1211 /* loop condition: *arg is not a blank */
1212 for(;;){
1213 a = findbl(arg, narg, &na);
1214 if(a == arg)
1215 break;
1216 r = runemalloc(narg-na+1);
1217 runemove(r, arg, narg-na);
1218 n++;
1219 winaddincl(w, r, narg-na);
1220 arg = skipbl(a, na, &narg);
1222 if(n==0 && w->nincl){
1223 for(n=w->nincl; --n>=0; )
1224 warning(nil, "%S ", w->incl[n]);
1225 warning(nil, "\n");
1229 static Rune LON[] = { 'O', 'N', 0 };
1230 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1231 static Rune Lon[] = { 'o', 'n', 0 };
1233 enum {
1234 IGlobal = -2,
1235 IError = -1,
1236 Ion = 0,
1237 Ioff = 1
1240 static int
1241 indentval(Rune *s, int n)
1243 if(n < 2)
1244 return IError;
1245 if(runestrncmp(s, LON, n) == 0){
1246 globalautoindent = TRUE;
1247 warning(nil, "Indent ON\n");
1248 return IGlobal;
1250 if(runestrncmp(s, LOFF, n) == 0){
1251 globalautoindent = FALSE;
1252 warning(nil, "Indent OFF\n");
1253 return IGlobal;
1255 return runestrncmp(s, Lon, n) == 0;
1258 static void
1259 fixindent(Window *w, void *arg)
1261 USED(arg);
1262 w->autoindent = globalautoindent;
1265 void
1266 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1268 Rune *a, *r;
1269 Window *w;
1270 int na, len, autoindent;
1272 USED(_0);
1273 USED(_1);
1274 USED(_2);
1276 w = nil;
1277 if(et!=nil && et->w!=nil)
1278 w = et->w;
1279 autoindent = IError;
1280 getarg(argt, FALSE, TRUE, &r, &len);
1281 if(r!=nil && len>0)
1282 autoindent = indentval(r, len);
1283 else{
1284 a = findbl(arg, narg, &na);
1285 if(a != arg)
1286 autoindent = indentval(arg, narg-na);
1288 if(autoindent == IGlobal)
1289 allwindows(fixindent, nil);
1290 else if(w != nil && autoindent >= 0)
1291 w->autoindent = autoindent;
1294 void
1295 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1297 Rune *a, *r;
1298 Window *w;
1299 int na, len, tab;
1300 char *p;
1302 USED(_0);
1303 USED(_1);
1304 USED(_2);
1306 if(et==nil || et->w==nil)
1307 return;
1308 w = et->w;
1309 getarg(argt, FALSE, TRUE, &r, &len);
1310 tab = 0;
1311 if(r!=nil && len>0){
1312 p = runetobyte(r, len);
1313 if('0'<=p[0] && p[0]<='9')
1314 tab = atoi(p);
1315 free(p);
1316 }else{
1317 a = findbl(arg, narg, &na);
1318 if(a != arg){
1319 p = runetobyte(arg, narg-na);
1320 if('0'<=p[0] && p[0]<='9')
1321 tab = atoi(p);
1322 free(p);
1325 if(tab > 0){
1326 if(w->body.tabstop != tab){
1327 w->body.tabstop = tab;
1328 winresize(w, w->r, FALSE, TRUE);
1330 }else
1331 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1334 void
1335 runproc(void *argvp)
1337 /* args: */
1338 Window *win;
1339 char *s;
1340 Rune *rdir;
1341 int ndir;
1342 int newns;
1343 char *argaddr;
1344 char *arg;
1345 Command *c;
1346 Channel *cpid;
1347 int iseditcmd;
1348 /* end of args */
1349 char *e, *t, *name, *filename, *dir, **av, *news;
1350 Rune r, **incl;
1351 int ac, w, inarg, i, n, fd, nincl, winid;
1352 int sfd[3];
1353 int pipechar;
1354 char buf[512];
1355 int olddir;
1356 int ret;
1357 /*static void *parg[2]; */
1358 char *rcarg[4];
1359 void **argv;
1360 CFsys *fs;
1362 threadsetname("runproc");
1364 argv = argvp;
1365 win = argv[0];
1366 s = argv[1];
1367 rdir = argv[2];
1368 ndir = (uintptr)argv[3];
1369 newns = (uintptr)argv[4];
1370 argaddr = argv[5];
1371 arg = argv[6];
1372 c = argv[7];
1373 cpid = argv[8];
1374 iseditcmd = (uintptr)argv[9];
1375 free(argv);
1377 t = s;
1378 while(*t==' ' || *t=='\n' || *t=='\t')
1379 t++;
1380 for(e=t; *e; e++)
1381 if(*e==' ' || *e=='\n' || *e=='\t' )
1382 break;
1383 name = emalloc((e-t)+2);
1384 memmove(name, t, e-t);
1385 name[e-t] = 0;
1386 e = utfrrune(name, '/');
1387 if(e)
1388 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1389 strcat(name, " "); /* add blank here for ease in waittask */
1390 c->name = bytetorune(name, &c->nname);
1391 free(name);
1392 pipechar = 0;
1393 if(*t=='<' || *t=='|' || *t=='>')
1394 pipechar = *t++;
1395 c->iseditcmd = iseditcmd;
1396 c->text = s;
1397 if(newns){
1398 nincl = 0;
1399 incl = nil;
1400 if(win){
1401 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1402 nincl = win->nincl;
1403 if(nincl > 0){
1404 incl = emalloc(nincl*sizeof(Rune*));
1405 for(i=0; i<nincl; i++){
1406 n = runestrlen(win->incl[i]);
1407 incl[i] = runemalloc(n+1);
1408 runemove(incl[i], win->incl[i], n);
1411 winid = win->id;
1412 }else{
1413 filename = nil;
1414 winid = 0;
1415 if(activewin)
1416 winid = activewin->id;
1418 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1419 sprint(buf, "%d", winid);
1420 putenv("winid", buf);
1422 if(filename){
1423 putenv("%", filename);
1424 free(filename);
1426 c->md = fsysmount(rdir, ndir, incl, nincl);
1427 if(c->md == nil){
1428 fprint(2, "child: can't allocate mntdir: %r\n");
1429 threadexits("fsysmount");
1431 sprint(buf, "%d", c->md->id);
1432 if((fs = nsmount("acme", buf)) == nil){
1433 fprint(2, "child: can't mount acme: %r\n");
1434 fsysdelid(c->md);
1435 c->md = nil;
1436 threadexits("nsmount");
1438 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1439 sprint(buf, "%d/rdsel", winid);
1440 sfd[0] = fsopenfd(fs, buf, OREAD);
1441 }else
1442 sfd[0] = open("/dev/null", OREAD);
1443 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1444 if(iseditcmd){
1445 if(winid > 0)
1446 sprint(buf, "%d/editout", winid);
1447 else
1448 sprint(buf, "editout");
1449 }else
1450 sprint(buf, "%d/wrsel", winid);
1451 sfd[1] = fsopenfd(fs, buf, OWRITE);
1452 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1453 }else{
1454 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1455 sfd[2] = sfd[1];
1457 fsunmount(fs);
1458 }else{
1459 rfork(RFFDG|RFNOTEG);
1460 fsysclose();
1461 sfd[0] = open("/dev/null", OREAD);
1462 sfd[1] = open("/dev/null", OWRITE);
1463 sfd[2] = dup(erroutfd, -1);
1465 if(win)
1466 winclose(win);
1468 if(argaddr)
1469 putenv("acmeaddr", argaddr);
1470 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1471 goto Hard;
1472 inarg = FALSE;
1473 for(e=t; *e; e+=w){
1474 w = chartorune(&r, e);
1475 if(r==' ' || r=='\t')
1476 continue;
1477 if(r < ' ')
1478 goto Hard;
1479 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1480 goto Hard;
1481 inarg = TRUE;
1483 if(!inarg)
1484 goto Fail;
1486 ac = 0;
1487 av = nil;
1488 inarg = FALSE;
1489 for(e=t; *e; e+=w){
1490 w = chartorune(&r, e);
1491 if(r==' ' || r=='\t'){
1492 inarg = FALSE;
1493 *e = 0;
1494 continue;
1496 if(!inarg){
1497 inarg = TRUE;
1498 av = realloc(av, (ac+1)*sizeof(char**));
1499 av[ac++] = e;
1502 av = realloc(av, (ac+2)*sizeof(char**));
1503 av[ac++] = arg;
1504 av[ac] = nil;
1505 c->av = av;
1508 * clumsy -- we're not running in a separate thread
1509 * so we have to save the current directory and put
1510 * it back when we're done. if this gets to be a regular
1511 * thing we could change threadexec to take a directory too.
1513 olddir = -1;
1514 if(rdir != nil){
1515 olddir = open(".", OREAD);
1516 dir = runetobyte(rdir, ndir);
1517 chdir(dir); /* ignore error: probably app. window */
1518 free(dir);
1520 ret = threadspawn(sfd, av[0], av);
1521 if(olddir >= 0){
1522 fchdir(olddir);
1523 close(olddir);
1525 if(ret >= 0){
1526 if(cpid)
1527 sendul(cpid, ret);
1528 threadexits("");
1530 /* libthread uses execvp so no need to do this */
1531 #if 0
1532 e = av[0];
1533 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1534 goto Fail;
1535 if(cputype){
1536 sprint(buf, "%s/%s", cputype, av[0]);
1537 procexec(cpid, sfd, buf, av);
1539 sprint(buf, "/bin/%s", av[0]);
1540 procexec(cpid, sfd, buf, av);
1541 #endif
1542 goto Fail;
1544 Hard:
1546 * ugly: set path = (. $cputype /bin)
1547 * should honor $path if unusual.
1549 if(cputype){
1550 n = 0;
1551 memmove(buf+n, ".", 2);
1552 n += 2;
1553 i = strlen(cputype)+1;
1554 memmove(buf+n, cputype, i);
1555 n += i;
1556 memmove(buf+n, "/bin", 5);
1557 n += 5;
1558 fd = create("/env/path", OWRITE, 0666);
1559 write(fd, buf, n);
1560 close(fd);
1563 if(arg){
1564 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1565 if(news){
1566 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1567 free(s);
1568 t = news;
1569 c->text = news;
1572 olddir = -1;
1573 if(rdir != nil){
1574 olddir = open(".", OREAD);
1575 dir = runetobyte(rdir, ndir);
1576 chdir(dir); /* ignore error: probably app. window */
1577 free(dir);
1579 rcarg[0] = "rc";
1580 rcarg[1] = "-c";
1581 rcarg[2] = t;
1582 rcarg[3] = nil;
1583 ret = threadspawn(sfd, rcarg[0], rcarg);
1584 if(olddir >= 0){
1585 fchdir(olddir);
1586 close(olddir);
1588 if(ret >= 0){
1589 if(cpid)
1590 sendul(cpid, ret);
1591 threadexits(nil);
1593 warning(nil, "exec rc: %r\n");
1595 Fail:
1596 /* threadexec hasn't happened, so send a zero */
1597 close(sfd[0]);
1598 close(sfd[1]);
1599 if(sfd[2] != sfd[1])
1600 close(sfd[2]);
1601 sendul(cpid, 0);
1602 threadexits(nil);
1605 void
1606 runwaittask(void *v)
1608 Command *c;
1609 Channel *cpid;
1610 void **a;
1612 threadsetname("runwaittask");
1613 a = v;
1614 c = a[0];
1615 cpid = a[1];
1616 free(a);
1618 c->pid = recvul(cpid);
1619 while(c->pid == ~0);
1620 free(c->av);
1621 if(c->pid != 0) /* successful exec */
1622 sendp(ccommand, c);
1623 else{
1624 if(c->iseditcmd)
1625 sendul(cedit, 0);
1626 free(c->name);
1627 free(c->text);
1628 free(c);
1630 chanfree(cpid);
1633 void
1634 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1636 void **arg;
1637 Command *c;
1638 Channel *cpid;
1640 if(s == nil)
1641 return;
1643 arg = emalloc(10*sizeof(void*));
1644 c = emalloc(sizeof *c);
1645 cpid = chancreate(sizeof(ulong), 0);
1646 chansetname(cpid, "cpid %s", s);
1647 arg[0] = win;
1648 arg[1] = s;
1649 arg[2] = rdir;
1650 arg[3] = (void*)(uintptr)ndir;
1651 arg[4] = (void*)(uintptr)newns;
1652 arg[5] = argaddr;
1653 arg[6] = xarg;
1654 arg[7] = c;
1655 arg[8] = cpid;
1656 arg[9] = (void*)(uintptr)iseditcmd;
1657 threadcreate(runproc, arg, STACK);
1658 /* mustn't block here because must be ready to answer mount() call in run() */
1659 arg = emalloc(2*sizeof(void*));
1660 arg[0] = c;
1661 arg[1] = cpid;
1662 threadcreate(runwaittask, arg, STACK);