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 || f->mtime+1<d->mtime){
648 f->dev = d->dev;
649 f->qidpath = d->qid.path;
650 f->mtime = d->mtime;
651 if(f->unread)
652 warning(nil, "%s not written; file already exists\n", name);
653 else
654 warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
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 d1 = dirfstat(fd);
691 if(d1 != nil){
692 free(d);
693 d = d1;
695 f->qidpath = d->qid.path;
696 f->dev = d->dev;
697 f->mtime = d->mtime;
698 f->mod = FALSE;
699 w->dirty = FALSE;
700 f->unread = FALSE;
702 for(i=0; i<f->ntext; i++){
703 f->text[i]->w->putseq = f->seq;
704 f->text[i]->w->dirty = w->dirty;
707 fbuffree(s);
708 fbuffree(r);
709 free(d);
710 free(namer);
711 free(name);
712 close(fd);
713 winsettag(w);
714 return;
716 Rescue2:
717 fbuffree(s);
718 fbuffree(r);
719 close(fd);
720 /* fall through */
722 Rescue1:
723 free(d);
724 free(namer);
725 free(name);
728 void
729 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
731 int nname;
732 Rune *namer;
733 Window *w;
734 File *f;
735 char *name;
737 USED(_0);
738 USED(_1);
739 USED(_2);
741 if(et==nil || et->w==nil || et->w->isdir)
742 return;
743 w = et->w;
744 f = w->body.file;
745 name = getname(&w->body, argt, arg, narg, TRUE);
746 if(name == nil){
747 warning(nil, "no file name\n");
748 return;
750 namer = bytetorune(name, &nname);
751 putfile(f, 0, f->b.nc, namer, nname);
752 free(name);
755 void
756 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
758 char *name;
760 USED(_0);
761 USED(_1);
762 USED(_2);
764 if(narg)
765 name = runetobyte(arg, narg);
766 else
767 getbytearg(argt, FALSE, TRUE, &name);
768 if(isdump)
769 rowdump(&row, name);
770 else
771 rowload(&row, name, FALSE);
772 free(name);
775 void
776 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
778 uint q0, q1, n, locked, c;
779 Rune *r;
781 USED(_0);
782 USED(_2);
783 USED(_3);
785 /*
786 * if not executing a mouse chord (et != t) and snarfing (dosnarf)
787 * and executed Cut or Snarf in window tag (et->w != nil),
788 * then use the window body selection or the tag selection
789 * or do nothing at all.
790 */
791 if(et!=t && dosnarf && et->w!=nil){
792 if(et->w->body.q1>et->w->body.q0){
793 t = &et->w->body;
794 if(docut)
795 filemark(t->file); /* seq has been incremented by execute */
796 }else if(et->w->tag.q1>et->w->tag.q0)
797 t = &et->w->tag;
798 else
799 t = nil;
801 if(t == nil) /* no selection */
802 return;
804 locked = FALSE;
805 if(t->w!=nil && et->w!=t->w){
806 locked = TRUE;
807 c = 'M';
808 if(et->w)
809 c = et->w->owner;
810 winlock(t->w, c);
812 if(t->q0 == t->q1){
813 if(locked)
814 winunlock(t->w);
815 return;
817 if(dosnarf){
818 q0 = t->q0;
819 q1 = t->q1;
820 bufdelete(&snarfbuf, 0, snarfbuf.nc);
821 r = fbufalloc();
822 while(q0 < q1){
823 n = q1 - q0;
824 if(n > RBUFSIZE)
825 n = RBUFSIZE;
826 bufread(&t->file->b, q0, r, n);
827 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
828 q0 += n;
830 fbuffree(r);
831 acmeputsnarf();
833 if(docut){
834 textdelete(t, t->q0, t->q1, TRUE);
835 textsetselect(t, t->q0, t->q0);
836 if(t->w){
837 textscrdraw(t);
838 winsettag(t->w);
840 }else if(dosnarf) /* Snarf command */
841 argtext = t;
842 if(locked)
843 winunlock(t->w);
846 void
847 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
849 int c;
850 uint q, q0, q1, n;
851 Rune *r;
853 USED(_0);
854 USED(_1);
855 USED(_2);
857 /* if(tobody), use body of executing window (Paste or Send command) */
858 if(tobody && et!=nil && et->w!=nil){
859 t = &et->w->body;
860 filemark(t->file); /* seq has been incremented by execute */
862 if(t == nil)
863 return;
865 acmegetsnarf();
866 if(t==nil || snarfbuf.nc==0)
867 return;
868 if(t->w!=nil && et->w!=t->w){
869 c = 'M';
870 if(et->w)
871 c = et->w->owner;
872 winlock(t->w, c);
874 cut(t, t, nil, FALSE, TRUE, nil, 0);
875 q = 0;
876 q0 = t->q0;
877 q1 = t->q0+snarfbuf.nc;
878 r = fbufalloc();
879 while(q0 < q1){
880 n = q1 - q0;
881 if(n > RBUFSIZE)
882 n = RBUFSIZE;
883 if(r == nil)
884 r = runemalloc(n);
885 bufread(&snarfbuf, q, r, n);
886 textinsert(t, q0, r, n, TRUE);
887 q += n;
888 q0 += n;
890 fbuffree(r);
891 if(selectall)
892 textsetselect(t, t->q0, q1);
893 else
894 textsetselect(t, q1, q1);
895 if(t->w){
896 textscrdraw(t);
897 winsettag(t->w);
899 if(t->w!=nil && et->w!=t->w)
900 winunlock(t->w);
903 void
904 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
906 Rune *r;
907 int n;
909 USED(_0);
910 USED(_1);
912 if(et && et->w){
913 t = &et->w->body;
914 if(narg > 0){
915 search(t, arg, narg);
916 return;
918 getarg(argt, FALSE, FALSE, &r, &n);
919 if(r == nil){
920 n = t->q1-t->q0;
921 r = runemalloc(n);
922 bufread(&t->file->b, t->q0, r, n);
924 search(t, r, n);
925 free(r);
929 static Rune Lnl[] = { '\n', 0 };
931 void
932 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
934 USED(_0);
935 USED(_1);
936 USED(_2);
937 USED(_3);
938 USED(_4);
940 if(et->w==nil)
941 return;
942 t = &et->w->body;
943 if(t->q0 != t->q1)
944 cut(t, t, nil, TRUE, FALSE, nil, 0);
945 textsetselect(t, t->file->b.nc, t->file->b.nc);
946 paste(t, t, nil, TRUE, TRUE, nil, 0);
947 if(textreadc(t, t->file->b.nc-1) != '\n'){
948 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
949 textsetselect(t, t->file->b.nc, t->file->b.nc);
953 void
954 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
956 Rune *r;
957 int len;
959 USED(_0);
960 USED(_1);
961 USED(_2);
963 if(et == nil)
964 return;
965 getarg(argt, FALSE, TRUE, &r, &len);
966 seq++;
967 if(r != nil){
968 editcmd(et, r, len);
969 free(r);
970 }else
971 editcmd(et, arg, narg);
974 void
975 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
977 USED(et);
978 USED(_0);
979 USED(_1);
980 USED(_2);
981 USED(_3);
982 USED(_4);
983 USED(_5);
985 if(rowclean(&row)){
986 sendul(cexit, 0);
987 threadexits(nil);
991 void
992 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
994 int i, j, e;
995 Window *w;
996 Column *c;
997 char *a;
999 USED(et);
1000 USED(_0);
1001 USED(_1);
1002 USED(_2);
1003 USED(_3);
1004 USED(_4);
1005 USED(_5);
1007 for(i=0; i<row.ncol; i++){
1008 c = row.col[i];
1009 for(j=0; j<c->nw; j++){
1010 w = c->w[j];
1011 if(w->isscratch || w->isdir || w->body.file->nname==0)
1012 continue;
1013 if(w->nopen[QWevent] > 0)
1014 continue;
1015 a = runetobyte(w->body.file->name, w->body.file->nname);
1016 e = access(a, 0);
1017 if(w->body.file->mod || w->body.ncache)
1018 if(e < 0)
1019 warning(nil, "no auto-Put of %s: %r\n", a);
1020 else{
1021 wincommit(w, &w->body);
1022 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1024 free(a);
1030 void
1031 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1033 USED(_0);
1034 USED(_1);
1035 USED(_2);
1036 USED(_3);
1037 USED(_4);
1038 USED(_5);
1040 if(et && et->w)
1041 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1044 void
1045 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1047 char *a, *aa;
1048 Runestr dir;
1050 USED(_0);
1051 USED(_1);
1052 USED(_2);
1054 aa = getbytearg(argt, TRUE, TRUE, &a);
1056 dir = dirname(et, nil, 0);
1057 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1058 free(dir.r);
1059 dir.r = nil;
1060 dir.nr = 0;
1062 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1065 void
1066 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1068 Rune *a, *cmd, *r;
1069 int na;
1071 USED(_0);
1072 USED(_1);
1073 USED(_2);
1074 USED(_3);
1076 getarg(argt, FALSE, FALSE, &r, &na);
1077 if(r)
1078 xkill(nil, nil, nil, 0, 0, r, na);
1079 /* loop condition: *arg is not a blank */
1080 for(;;){
1081 a = findbl(arg, narg, &na);
1082 if(a == arg)
1083 break;
1084 cmd = runemalloc(narg-na+1);
1085 runemove(cmd, arg, narg-na);
1086 sendp(ckill, cmd);
1087 arg = skipbl(a, na, &narg);
1091 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1092 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1094 void
1095 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1097 Rune *a, *r, *flag, *file;
1098 int na, nf;
1099 char *aa;
1100 Reffont *newfont;
1101 Dirlist *dp;
1102 int i, fix;
1104 USED(_0);
1105 USED(_1);
1107 if(et==nil || et->w==nil)
1108 return;
1109 t = &et->w->body;
1110 flag = nil;
1111 file = nil;
1112 /* loop condition: *arg is not a blank */
1113 nf = 0;
1114 for(;;){
1115 a = findbl(arg, narg, &na);
1116 if(a == arg)
1117 break;
1118 r = runemalloc(narg-na+1);
1119 runemove(r, arg, narg-na);
1120 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1121 free(flag);
1122 flag = r;
1123 }else{
1124 free(file);
1125 file = r;
1126 nf = narg-na;
1128 arg = skipbl(a, na, &narg);
1130 getarg(argt, FALSE, TRUE, &r, &na);
1131 if(r)
1132 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1133 free(flag);
1134 flag = r;
1135 }else{
1136 free(file);
1137 file = r;
1138 nf = na;
1140 fix = 1;
1141 if(flag)
1142 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1143 else if(file == nil){
1144 newfont = rfget(FALSE, FALSE, FALSE, nil);
1145 if(newfont)
1146 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1148 if(file){
1149 aa = runetobyte(file, nf);
1150 newfont = rfget(fix, flag!=nil, FALSE, aa);
1151 free(aa);
1152 }else
1153 newfont = rfget(fix, FALSE, FALSE, nil);
1154 if(newfont){
1155 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1156 rfclose(t->reffont);
1157 t->reffont = newfont;
1158 t->fr.font = newfont->f;
1159 frinittick(&t->fr);
1160 if(t->w->isdir){
1161 t->all.min.x++; /* force recolumnation; disgusting! */
1162 for(i=0; i<t->w->ndl; i++){
1163 dp = t->w->dlp[i];
1164 aa = runetobyte(dp->r, dp->nr);
1165 dp->wid = stringwidth(newfont->f, aa);
1166 free(aa);
1169 /* avoid shrinking of window due to quantization */
1170 colgrow(t->w->col, t->w, -1);
1172 free(file);
1173 free(flag);
1176 void
1177 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1179 Rune *a, *r;
1180 Window *w;
1181 int na, n, len;
1183 USED(_0);
1184 USED(_1);
1185 USED(_2);
1187 if(et==nil || et->w==nil)
1188 return;
1189 w = et->w;
1190 n = 0;
1191 getarg(argt, FALSE, TRUE, &r, &len);
1192 if(r){
1193 n++;
1194 winaddincl(w, r, len);
1196 /* loop condition: *arg is not a blank */
1197 for(;;){
1198 a = findbl(arg, narg, &na);
1199 if(a == arg)
1200 break;
1201 r = runemalloc(narg-na+1);
1202 runemove(r, arg, narg-na);
1203 n++;
1204 winaddincl(w, r, narg-na);
1205 arg = skipbl(a, na, &narg);
1207 if(n==0 && w->nincl){
1208 for(n=w->nincl; --n>=0; )
1209 warning(nil, "%S ", w->incl[n]);
1210 warning(nil, "\n");
1214 static Rune LON[] = { 'O', 'N', 0 };
1215 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1216 static Rune Lon[] = { 'o', 'n', 0 };
1218 enum {
1219 IGlobal = -2,
1220 IError = -1,
1221 Ion = 0,
1222 Ioff = 1
1225 static int
1226 indentval(Rune *s, int n)
1228 if(n < 2)
1229 return IError;
1230 if(runestrncmp(s, LON, n) == 0){
1231 globalautoindent = TRUE;
1232 warning(nil, "Indent ON\n");
1233 return IGlobal;
1235 if(runestrncmp(s, LOFF, n) == 0){
1236 globalautoindent = FALSE;
1237 warning(nil, "Indent OFF\n");
1238 return IGlobal;
1240 return runestrncmp(s, Lon, n) == 0;
1243 static void
1244 fixindent(Window *w, void *arg)
1246 USED(arg);
1247 w->autoindent = globalautoindent;
1250 void
1251 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1253 Rune *a, *r;
1254 Window *w;
1255 int na, len, autoindent;
1257 USED(_0);
1258 USED(_1);
1259 USED(_2);
1261 w = nil;
1262 if(et!=nil && et->w!=nil)
1263 w = et->w;
1264 autoindent = IError;
1265 getarg(argt, FALSE, TRUE, &r, &len);
1266 if(r!=nil && len>0)
1267 autoindent = indentval(r, len);
1268 else{
1269 a = findbl(arg, narg, &na);
1270 if(a != arg)
1271 autoindent = indentval(arg, narg-na);
1273 if(autoindent == IGlobal)
1274 allwindows(fixindent, nil);
1275 else if(w != nil && autoindent >= 0)
1276 w->autoindent = autoindent;
1279 void
1280 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1282 Rune *a, *r;
1283 Window *w;
1284 int na, len, tab;
1285 char *p;
1287 USED(_0);
1288 USED(_1);
1289 USED(_2);
1291 if(et==nil || et->w==nil)
1292 return;
1293 w = et->w;
1294 getarg(argt, FALSE, TRUE, &r, &len);
1295 tab = 0;
1296 if(r!=nil && len>0){
1297 p = runetobyte(r, len);
1298 if('0'<=p[0] && p[0]<='9')
1299 tab = atoi(p);
1300 free(p);
1301 }else{
1302 a = findbl(arg, narg, &na);
1303 if(a != arg){
1304 p = runetobyte(arg, narg-na);
1305 if('0'<=p[0] && p[0]<='9')
1306 tab = atoi(p);
1307 free(p);
1310 if(tab > 0){
1311 if(w->body.tabstop != tab){
1312 w->body.tabstop = tab;
1313 winresize(w, w->r, TRUE, TRUE);
1315 }else
1316 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1319 void
1320 runproc(void *argvp)
1322 /* args: */
1323 Window *win;
1324 char *s;
1325 Rune *rdir;
1326 int ndir;
1327 int newns;
1328 char *argaddr;
1329 char *arg;
1330 Command *c;
1331 Channel *cpid;
1332 int iseditcmd;
1333 /* end of args */
1334 char *e, *t, *name, *filename, *dir, **av, *news;
1335 Rune r, **incl;
1336 int ac, w, inarg, i, n, fd, nincl, winid;
1337 int sfd[3];
1338 int pipechar;
1339 char buf[512];
1340 int olddir;
1341 int ret;
1342 /*static void *parg[2]; */
1343 char *rcarg[4];
1344 void **argv;
1345 CFsys *fs;
1347 threadsetname("runproc");
1349 argv = argvp;
1350 win = argv[0];
1351 s = argv[1];
1352 rdir = argv[2];
1353 ndir = (uintptr)argv[3];
1354 newns = (uintptr)argv[4];
1355 argaddr = argv[5];
1356 arg = argv[6];
1357 c = argv[7];
1358 cpid = argv[8];
1359 iseditcmd = (uintptr)argv[9];
1360 free(argv);
1362 t = s;
1363 while(*t==' ' || *t=='\n' || *t=='\t')
1364 t++;
1365 for(e=t; *e; e++)
1366 if(*e==' ' || *e=='\n' || *e=='\t' )
1367 break;
1368 name = emalloc((e-t)+2);
1369 memmove(name, t, e-t);
1370 name[e-t] = 0;
1371 e = utfrrune(name, '/');
1372 if(e)
1373 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1374 strcat(name, " "); /* add blank here for ease in waittask */
1375 c->name = bytetorune(name, &c->nname);
1376 free(name);
1377 pipechar = 0;
1378 if(*t=='<' || *t=='|' || *t=='>')
1379 pipechar = *t++;
1380 c->iseditcmd = iseditcmd;
1381 c->text = s;
1382 if(newns){
1383 nincl = 0;
1384 incl = nil;
1385 if(win){
1386 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1387 nincl = win->nincl;
1388 if(nincl > 0){
1389 incl = emalloc(nincl*sizeof(Rune*));
1390 for(i=0; i<nincl; i++){
1391 n = runestrlen(win->incl[i]);
1392 incl[i] = runemalloc(n+1);
1393 runemove(incl[i], win->incl[i], n);
1396 winid = win->id;
1397 }else{
1398 filename = nil;
1399 winid = 0;
1400 if(activewin)
1401 winid = activewin->id;
1403 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1404 sprint(buf, "%d", winid);
1405 putenv("winid", buf);
1407 if(filename){
1408 putenv("%", filename);
1409 free(filename);
1411 c->md = fsysmount(rdir, ndir, incl, nincl);
1412 if(c->md == nil){
1413 fprint(2, "child: can't allocate mntdir: %r\n");
1414 threadexits("fsysmount");
1416 sprint(buf, "%d", c->md->id);
1417 if((fs = nsmount("acme", buf)) == nil){
1418 fprint(2, "child: can't mount acme: %r\n");
1419 fsysdelid(c->md);
1420 c->md = nil;
1421 threadexits("nsmount");
1423 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1424 sprint(buf, "%d/rdsel", winid);
1425 sfd[0] = fsopenfd(fs, buf, OREAD);
1426 }else
1427 sfd[0] = open("/dev/null", OREAD);
1428 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1429 if(iseditcmd){
1430 if(winid > 0)
1431 sprint(buf, "%d/editout", winid);
1432 else
1433 sprint(buf, "editout");
1434 }else
1435 sprint(buf, "%d/wrsel", winid);
1436 sfd[1] = fsopenfd(fs, buf, OWRITE);
1437 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1438 }else{
1439 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1440 sfd[2] = sfd[1];
1442 fsunmount(fs);
1443 }else{
1444 rfork(RFFDG|RFNOTEG);
1445 fsysclose();
1446 sfd[0] = open("/dev/null", OREAD);
1447 sfd[1] = open("/dev/null", OWRITE);
1448 sfd[2] = dup(erroutfd, -1);
1450 if(win)
1451 winclose(win);
1453 if(argaddr)
1454 putenv("acmeaddr", argaddr);
1455 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1456 goto Hard;
1457 inarg = FALSE;
1458 for(e=t; *e; e+=w){
1459 w = chartorune(&r, e);
1460 if(r==' ' || r=='\t')
1461 continue;
1462 if(r < ' ')
1463 goto Hard;
1464 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1465 goto Hard;
1466 inarg = TRUE;
1468 if(!inarg)
1469 goto Fail;
1471 ac = 0;
1472 av = nil;
1473 inarg = FALSE;
1474 for(e=t; *e; e+=w){
1475 w = chartorune(&r, e);
1476 if(r==' ' || r=='\t'){
1477 inarg = FALSE;
1478 *e = 0;
1479 continue;
1481 if(!inarg){
1482 inarg = TRUE;
1483 av = realloc(av, (ac+1)*sizeof(char**));
1484 av[ac++] = e;
1487 av = realloc(av, (ac+2)*sizeof(char**));
1488 av[ac++] = arg;
1489 av[ac] = nil;
1490 c->av = av;
1493 * clumsy -- we're not running in a separate thread
1494 * so we have to save the current directory and put
1495 * it back when we're done. if this gets to be a regular
1496 * thing we could change threadexec to take a directory too.
1498 olddir = -1;
1499 if(rdir != nil){
1500 olddir = open(".", OREAD);
1501 dir = runetobyte(rdir, ndir);
1502 chdir(dir); /* ignore error: probably app. window */
1503 free(dir);
1505 ret = threadspawn(sfd, av[0], av);
1506 if(olddir >= 0){
1507 fchdir(olddir);
1508 close(olddir);
1510 if(ret >= 0){
1511 if(cpid)
1512 sendul(cpid, ret);
1513 threadexits("");
1515 /* libthread uses execvp so no need to do this */
1516 #if 0
1517 e = av[0];
1518 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1519 goto Fail;
1520 if(cputype){
1521 sprint(buf, "%s/%s", cputype, av[0]);
1522 procexec(cpid, sfd, buf, av);
1524 sprint(buf, "/bin/%s", av[0]);
1525 procexec(cpid, sfd, buf, av);
1526 #endif
1527 goto Fail;
1529 Hard:
1531 * ugly: set path = (. $cputype /bin)
1532 * should honor $path if unusual.
1534 if(cputype){
1535 n = 0;
1536 memmove(buf+n, ".", 2);
1537 n += 2;
1538 i = strlen(cputype)+1;
1539 memmove(buf+n, cputype, i);
1540 n += i;
1541 memmove(buf+n, "/bin", 5);
1542 n += 5;
1543 fd = create("/env/path", OWRITE, 0666);
1544 write(fd, buf, n);
1545 close(fd);
1548 if(arg){
1549 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1550 if(news){
1551 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1552 free(s);
1553 t = news;
1554 c->text = news;
1557 olddir = -1;
1558 if(rdir != nil){
1559 olddir = open(".", OREAD);
1560 dir = runetobyte(rdir, ndir);
1561 chdir(dir); /* ignore error: probably app. window */
1562 free(dir);
1564 rcarg[0] = "rc";
1565 rcarg[1] = "-c";
1566 rcarg[2] = t;
1567 rcarg[3] = nil;
1568 ret = threadspawn(sfd, rcarg[0], rcarg);
1569 if(olddir >= 0){
1570 fchdir(olddir);
1571 close(olddir);
1573 if(ret >= 0){
1574 if(cpid)
1575 sendul(cpid, ret);
1576 threadexits(nil);
1578 warning(nil, "exec rc: %r\n");
1580 Fail:
1581 /* threadexec hasn't happened, so send a zero */
1582 close(sfd[0]);
1583 close(sfd[1]);
1584 if(sfd[2] != sfd[1])
1585 close(sfd[2]);
1586 sendul(cpid, 0);
1587 threadexits(nil);
1590 void
1591 runwaittask(void *v)
1593 Command *c;
1594 Channel *cpid;
1595 void **a;
1597 threadsetname("runwaittask");
1598 a = v;
1599 c = a[0];
1600 cpid = a[1];
1601 free(a);
1603 c->pid = recvul(cpid);
1604 while(c->pid == ~0);
1605 free(c->av);
1606 if(c->pid != 0) /* successful exec */
1607 sendp(ccommand, c);
1608 else{
1609 if(c->iseditcmd)
1610 sendul(cedit, 0);
1611 free(c->name);
1612 free(c->text);
1613 free(c);
1615 chanfree(cpid);
1618 void
1619 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1621 void **arg;
1622 Command *c;
1623 Channel *cpid;
1625 if(s == nil)
1626 return;
1628 arg = emalloc(10*sizeof(void*));
1629 c = emalloc(sizeof *c);
1630 cpid = chancreate(sizeof(ulong), 0);
1631 chansetname(cpid, "cpid %s", s);
1632 arg[0] = win;
1633 arg[1] = s;
1634 arg[2] = rdir;
1635 arg[3] = (void*)(uintptr)ndir;
1636 arg[4] = (void*)(uintptr)newns;
1637 arg[5] = argaddr;
1638 arg[6] = xarg;
1639 arg[7] = c;
1640 arg[8] = cpid;
1641 arg[9] = (void*)(uintptr)iseditcmd;
1642 threadcreate(runproc, arg, STACK);
1643 /* mustn't block here because must be ready to answer mount() call in run() */
1644 arg = emalloc(2*sizeof(void*));
1645 arg[0] = c;
1646 arg[1] = cpid;
1647 threadcreate(runwaittask, arg, STACK);