19 void del(Text*, Text*, Text*, int, int, Rune*, int);
20 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
21 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
22 void dump(Text*, Text*, Text*, int, int, Rune*, int);
23 void edit(Text*, Text*, Text*, int, int, Rune*, int);
24 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
25 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
26 void get(Text*, Text*, Text*, int, int, Rune*, int);
27 void id(Text*, Text*, Text*, int, int, Rune*, int);
28 void incl(Text*, Text*, Text*, int, int, Rune*, int);
29 void indent(Text*, Text*, Text*, int, int, Rune*, int);
30 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
31 void local(Text*, Text*, Text*, int, int, Rune*, int);
32 void look(Text*, Text*, Text*, int, int, Rune*, int);
33 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
34 void paste(Text*, Text*, Text*, int, int, Rune*, int);
35 void put(Text*, Text*, Text*, int, int, Rune*, int);
36 void putall(Text*, Text*, Text*, int, int, Rune*, int);
37 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
38 void sort(Text*, Text*, Text*, int, int, Rune*, int);
39 void tab(Text*, Text*, Text*, int, int, Rune*, int);
40 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
42 typedef struct Exectab Exectab;
46 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
52 static Rune LCut[] = { 'C', 'u', 't', 0 };
53 static Rune LDel[] = { 'D', 'e', 'l', 0 };
54 static Rune LDelcol[] = { 'D', 'e', 'l', 'c', 'o', 'l', 0 };
55 static Rune LDelete[] = { 'D', 'e', 'l', 'e', 't', 'e', 0 };
56 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
57 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
58 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
59 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
60 static Rune LGet[] = { 'G', 'e', 't', 0 };
61 static Rune LID[] = { 'I', 'D', 0 };
62 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
63 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
64 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
65 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
66 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
67 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
68 static Rune LNew[] = { 'N', 'e', 'w', 0 };
69 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
70 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
71 static Rune LPut[] = { 'P', 'u', 't', 0 };
72 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
73 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
74 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
75 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
76 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
77 static Rune LTab[] = { 'T', 'a', 'b', 0 };
78 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
79 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
82 { LCut, cut, TRUE, TRUE, TRUE },
83 { LDel, del, FALSE, FALSE, XXX },
84 { LDelcol, delcol, FALSE, XXX, XXX },
85 { LDelete, del, FALSE, TRUE, XXX },
86 { LDump, dump, FALSE, TRUE, XXX },
87 { LEdit, edit, FALSE, XXX, XXX },
88 { LExit, xexit, FALSE, XXX, XXX },
89 { LFont, fontx, FALSE, XXX, XXX },
90 { LGet, get, FALSE, TRUE, XXX },
91 { LID, id, FALSE, XXX, XXX },
92 { LIncl, incl, FALSE, XXX, XXX },
93 { LIndent, indent, FALSE, XXX, XXX },
94 { LKill, xkill, FALSE, XXX, XXX },
95 { LLoad, dump, FALSE, FALSE, XXX },
96 { LLocal, local, FALSE, XXX, XXX },
97 { LLook, look, FALSE, XXX, XXX },
98 { LNew, new, FALSE, XXX, XXX },
99 { LNewcol, newcol, FALSE, XXX, XXX },
100 { LPaste, paste, TRUE, TRUE, XXX },
101 { LPut, put, FALSE, XXX, XXX },
102 { LPutall, putall, FALSE, XXX, XXX },
103 { LRedo, undo, FALSE, FALSE, XXX },
104 { LSend, sendx, TRUE, XXX, XXX },
105 { LSnarf, cut, FALSE, TRUE, FALSE },
106 { LSort, sort, FALSE, XXX, XXX },
107 { LTab, tab, FALSE, XXX, XXX },
108 { LUndo, undo, FALSE, TRUE, XXX },
109 { LZerox, zeroxx, FALSE, XXX, XXX },
114 lookup(Rune *r, int n)
119 r = skipbl(r, n, &n);
124 for(e=exectab; e->name; e++)
125 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
135 return c=='<' || c=='|' || c=='>';
139 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
150 if(q1 == q0){ /* expand to find word (actually file name) */
151 /* if in selection, choose selection */
152 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
156 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
158 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
164 r = runemalloc(q1-q0);
165 bufread(&t->file->b, q0, r, q1-q0);
166 e = lookup(r, q1-q0);
167 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
171 if(q0!=aq0 || q1!=aq1){
172 bufread(&t->file->b, aq0, r, aq1-aq0);
175 aa = getbytearg(argt, TRUE, TRUE, &a);
177 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
180 warning(nil, "`argument string too long\n");
190 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
192 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
193 if(q0!=aq0 || q1!=aq1){
195 bufread(&t->file->b, q0, r, n);
197 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
199 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
202 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
204 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
206 winevent(t->w, "%c0 0 0 0 \n", c);
214 if(e->mark && seltext!=nil)
215 if(seltext->what == Body){
217 filemark(seltext->w->body.file);
219 s = skipbl(r, q1-q0, &n);
220 s = findbl(s, n, &n);
221 s = skipbl(s, n, &n);
222 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
227 b = runetobyte(r, q1-q0);
229 dir = dirname(t, nil, 0);
230 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
235 aa = getbytearg(argt, TRUE, TRUE, &a);
238 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
242 printarg(Text *argt, uint q0, uint q1)
246 if(argt->what!=Body || argt->file->name==nil)
248 buf = emalloc(argt->file->nname+32);
250 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
252 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
257 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
268 textcommit(argt, TRUE);
269 if(expand(argt, argt->q0, argt->q1, &e)){
271 if(e.nname && dofile){
272 e.name = runerealloc(e.name, e.nname+1);
274 a = printarg(argt, e.q0, e.q1);
285 *rp = runemalloc(n+1);
286 bufread(&argt->file->b, e.q0, *rp, n);
288 a = printarg(argt, e.q0, e.q1);
294 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
301 aa = getarg(argt, doaddr, dofile, &r, &n);
304 *bp = runetobyte(r, n);
310 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
321 c = rowadd(et->row, nil, -1);
323 winsettag(coladd(c, nil, nil, -1));
327 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
341 if(c==nil || colclean(c)==0)
343 for(i=0; i<c->nw; i++){
345 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
346 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
350 rowclose(et->col->row, et->col, TRUE);
354 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
362 if(et->col==nil || et->w == nil)
364 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
365 colclose(et->col, et->w, TRUE);
369 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
383 seqof(Window *w, int isundo)
385 /* if it's undo, see who changed with us */
387 return w->body.file->seq;
388 /* if it's redo, see who we'll be sync'ed up with */
389 return fileredoseq(w->body.file);
393 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
406 if(et==nil || et->w== nil)
408 seq = seqof(et->w, flag1);
410 /* nothing to undo */
414 * Undo the executing window first. Its display will update. other windows
415 * in the same file will not call show() and jump to a different location in the file.
416 * Simultaneous changes to other files will be chaotic, however.
418 winundo(et->w, flag1);
419 for(i=0; i<row.ncol; i++){
421 for(j=0; j<c->nw; j++){
425 if(seqof(w, flag1) == seq)
432 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
439 getarg(argt, FALSE, TRUE, &r, &n);
444 /* if are doing a Put, want to synthesize name even for non-existent file */
445 /* best guess is that file name doesn't contain a slash */
461 s = runetobyte(t->file->name, t->file->nname);
464 /* prefix with directory name if necessary */
467 if(n>0 && arg[0]!='/'){
468 dir = dirname(t, nil, 0);
469 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
476 r = runemalloc(dir.nr+n+1);
477 runemove(r, dir.r, dir.nr);
479 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
481 runemove(r+dir.nr, arg, n);
488 s = runetobyte(r, n);
498 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
510 if(t!=nil && t->w!=nil && t->w!=et->w){
519 if(t==nil || t->w==nil)
523 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
525 nw = coladd(t->w->col, nil, t->w, -1);
526 /* ugly: fix locks so w->unlock works */
527 winlock1(nw, t->w->owner);
534 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
538 int i, n, dirty, samename, isdir;
546 if(et==nil || et->w==nil)
548 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
552 name = getname(t, argt, arg, narg, FALSE);
554 warning(nil, "no file name\n");
557 if(t->file->ntext>1){
559 isdir = (d!=nil && (d->qid.type & QTDIR));
562 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
566 r = bytetorune(name, &n);
567 for(i=0; i<t->file->ntext; i++){
568 u = t->file->text[i];
569 /* second and subsequent calls with zero an already empty buffer, but OK */
573 samename = runeeq(r, n, t->file->name, t->file->nname);
574 textload(t, 0, name, samename);
576 t->file->mod = FALSE;
582 for(i=0; i<t->file->ntext; i++)
583 t->file->text[i]->w->dirty = dirty;
587 t->file->unread = FALSE;
588 for(i=0; i<t->file->ntext; i++){
589 u = t->file->text[i];
590 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
596 putfile(File *f, int q0, int q1, Rune *namer, int nname)
607 name = runetobyte(namer, nname);
609 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
610 /* f->mtime+1 because when talking over NFS it's often off by a second */
611 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime+1<d->mtime){
613 f->qidpath = d->qid.path;
616 warning(nil, "%s not written; file already exists\n", name);
618 warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
622 fd = create(name, OWRITE, 0666);
624 warning(nil, "can't create file %s: %r\n", name);
631 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
633 warning(nil, "%s not written; file is append only\n", name);
637 for(q=q0; q<q1; q+=n){
639 if(n > BUFSIZE/UTFmax)
641 bufread(&f->b, q, r, n);
642 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
643 if(write(fd, s, m) != m){
644 warning(nil, "can't write file %s: %r\n", name);
648 if(runeeq(namer, nname, f->name, f->nname)){
649 if(q0!=0 || q1!=f->b.nc){
659 f->qidpath = d->qid.path;
666 for(i=0; i<f->ntext; i++){
667 f->text[i]->w->putseq = f->seq;
668 f->text[i]->w->dirty = w->dirty;
693 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
705 if(et==nil || et->w==nil || et->w->isdir)
709 name = getname(&w->body, argt, arg, narg, TRUE);
711 warning(nil, "no file name\n");
714 namer = bytetorune(name, &nname);
715 putfile(f, 0, f->b.nc, namer, nname);
720 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
729 name = runetobyte(arg, narg);
731 getbytearg(argt, FALSE, TRUE, &name);
735 rowload(&row, name, FALSE);
740 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
742 uint q0, q1, n, locked, c;
749 /* use current window if snarfing and its selection is non-null */
750 if(et!=t && dosnarf && et->w!=nil){
751 if(et->w->body.q1>et->w->body.q0){
754 filemark(t->file); /* seq has been incremented by execute */
755 }else if(et->w->tag.q1>et->w->tag.q0)
759 /* can only happen if seltext == nil */
763 if(t->w!=nil && et->w!=t->w){
778 bufdelete(&snarfbuf, 0, snarfbuf.nc);
784 bufread(&t->file->b, q0, r, n);
785 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
792 textdelete(t, t->q0, t->q1, TRUE);
793 textsetselect(t, t->q0, t->q0);
798 }else if(dosnarf) /* Snarf command */
805 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
815 /* if(tobody), use body of executing window (Paste or Send command) */
816 if(tobody && et!=nil && et->w!=nil){
818 filemark(t->file); /* seq has been incremented by execute */
824 if(t==nil || snarfbuf.nc==0)
826 if(t->w!=nil && et->w!=t->w){
832 cut(t, t, nil, FALSE, TRUE, nil, 0);
835 q1 = t->q0+snarfbuf.nc;
843 bufread(&snarfbuf, q, r, n);
844 textinsert(t, q0, r, n, TRUE);
850 textsetselect(t, t->q0, q1);
852 textsetselect(t, q1, q1);
857 if(t->w!=nil && et->w!=t->w)
862 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
873 search(t, arg, narg);
876 getarg(argt, FALSE, FALSE, &r, &n);
880 bufread(&t->file->b, t->q0, r, n);
887 static Rune Lnl[] = { '\n', 0 };
890 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
902 cut(t, t, nil, TRUE, FALSE, nil, 0);
903 textsetselect(t, t->file->b.nc, t->file->b.nc);
904 paste(t, t, nil, TRUE, TRUE, nil, 0);
905 if(textreadc(t, t->file->b.nc-1) != '\n'){
906 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
907 textsetselect(t, t->file->b.nc, t->file->b.nc);
912 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
923 getarg(argt, FALSE, TRUE, &r, &len);
929 editcmd(et, arg, narg);
933 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
950 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
965 for(i=0; i<row.ncol; i++){
967 for(j=0; j<c->nw; j++){
969 if(w->isscratch || w->isdir || w->body.file->nname==0)
971 if(w->nopen[QWevent] > 0)
973 a = runetobyte(w->body.file->name, w->body.file->nname);
975 if(w->body.file->mod || w->body.ncache)
977 warning(nil, "no auto-Put of %s: %r\n", a);
979 wincommit(w, &w->body);
980 put(&w->body, nil, nil, XXX, XXX, nil, 0);
989 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1000 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1004 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1013 aa = getbytearg(argt, TRUE, TRUE, &a);
1015 dir = dirname(et, nil, 0);
1016 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1021 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1025 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1035 getarg(argt, FALSE, FALSE, &r, &na);
1037 xkill(nil, nil, nil, 0, 0, r, na);
1038 /* loop condition: *arg is not a blank */
1040 a = findbl(arg, narg, &na);
1043 cmd = runemalloc(narg-na+1);
1044 runemove(cmd, arg, narg-na);
1046 arg = skipbl(a, na, &narg);
1050 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1051 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1054 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1056 Rune *a, *r, *flag, *file;
1066 if(et==nil || et->w==nil)
1071 /* loop condition: *arg is not a blank */
1074 a = findbl(arg, narg, &na);
1077 r = runemalloc(narg-na+1);
1078 runemove(r, arg, narg-na);
1079 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1087 arg = skipbl(a, na, &narg);
1089 getarg(argt, FALSE, TRUE, &r, &na);
1091 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1101 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1102 else if(file == nil){
1103 newfont = rfget(FALSE, FALSE, FALSE, nil);
1105 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1108 aa = runetobyte(file, nf);
1109 newfont = rfget(fix, flag!=nil, FALSE, aa);
1112 newfont = rfget(fix, FALSE, FALSE, nil);
1114 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1115 rfclose(t->reffont);
1116 t->reffont = newfont;
1117 t->fr.font = newfont->f;
1120 t->all.min.x++; /* force recolumnation; disgusting! */
1121 for(i=0; i<t->w->ndl; i++){
1123 aa = runetobyte(dp->r, dp->nr);
1124 dp->wid = stringwidth(newfont->f, aa);
1128 /* avoid shrinking of window due to quantization */
1129 colgrow(t->w->col, t->w, -1);
1136 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1146 if(et==nil || et->w==nil)
1150 getarg(argt, FALSE, TRUE, &r, &len);
1153 winaddincl(w, r, len);
1155 /* loop condition: *arg is not a blank */
1157 a = findbl(arg, narg, &na);
1160 r = runemalloc(narg-na+1);
1161 runemove(r, arg, narg-na);
1163 winaddincl(w, r, narg-na);
1164 arg = skipbl(a, na, &narg);
1166 if(n==0 && w->nincl){
1167 for(n=w->nincl; --n>=0; )
1168 warning(nil, "%S ", w->incl[n]);
1173 static Rune LON[] = { 'O', 'N', 0 };
1174 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1175 static Rune Lon[] = { 'o', 'n', 0 };
1185 indentval(Rune *s, int n)
1189 if(runestrncmp(s, LON, n) == 0){
1190 globalautoindent = TRUE;
1191 warning(nil, "Indent ON\n");
1194 if(runestrncmp(s, LOFF, n) == 0){
1195 globalautoindent = FALSE;
1196 warning(nil, "Indent OFF\n");
1199 return runestrncmp(s, Lon, n) == 0;
1203 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1207 int na, len, autoindent;
1214 if(et!=nil && et->w!=nil)
1216 autoindent = IError;
1217 getarg(argt, FALSE, TRUE, &r, &len);
1219 autoindent = indentval(r, len);
1221 a = findbl(arg, narg, &na);
1223 autoindent = indentval(arg, narg-na);
1229 w->autoindent = autoindent;
1232 w->autoindent = globalautoindent;
1239 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1250 if(et==nil || et->w==nil)
1253 getarg(argt, FALSE, TRUE, &r, &len);
1255 if(r!=nil && len>0){
1256 p = runetobyte(r, len);
1257 if('0'<=p[0] && p[0]<='9')
1261 a = findbl(arg, narg, &na);
1263 p = runetobyte(arg, narg-na);
1264 if('0'<=p[0] && p[0]<='9')
1270 if(w->body.tabstop != tab){
1271 w->body.tabstop = tab;
1272 winresize(w, w->r, 1);
1275 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1279 runproc(void *argvp)
1293 char *e, *t, *name, *filename, *dir, **av, *news;
1295 int ac, w, inarg, i, n, fd, nincl, winid;
1301 //static void *parg[2];
1310 ndir = (int)argv[3];
1311 newns = (int)argv[4];
1316 iseditcmd = (int)argv[9];
1320 while(*t==' ' || *t=='\n' || *t=='\t')
1323 if(*e==' ' || *e=='\n' || *e=='\t' )
1325 name = emalloc((e-t)+2);
1326 memmove(name, t, e-t);
1328 e = utfrrune(name, '/');
1330 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1331 strcat(name, " "); /* add blank here for ease in waittask */
1332 c->name = bytetorune(name, &c->nname);
1335 if(*t=='<' || *t=='|' || *t=='>')
1337 c->iseditcmd = iseditcmd;
1343 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1346 incl = emalloc(nincl*sizeof(Rune*));
1347 for(i=0; i<nincl; i++){
1348 n = runestrlen(win->incl[i]);
1349 incl[i] = runemalloc(n+1);
1350 runemove(incl[i], win->incl[i], n);
1358 winid = activewin->id;
1360 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1361 sprint(buf, "%d", winid);
1362 putenv("winid", buf);
1365 putenv("%", filename);
1368 c->md = fsysmount(rdir, ndir, incl, nincl);
1370 fprint(2, "child: can't allocate mntdir: %r\n");
1371 threadexits("fsysmount");
1373 sprint(buf, "%d", c->md->id);
1374 if((fs = nsmount("acme", buf)) == nil){
1375 fprint(2, "child: can't mount acme: %r\n");
1378 threadexits("nsmount");
1380 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1381 sprint(buf, "%d/rdsel", winid);
1382 sfd[0] = fsopenfd(fs, buf, OREAD);
1384 sfd[0] = open("/dev/null", OREAD);
1385 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1388 sprint(buf, "%d/editout", winid);
1390 sprint(buf, "editout");
1392 sprint(buf, "%d/wrsel", winid);
1393 sfd[1] = fsopenfd(fs, buf, OWRITE);
1394 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1396 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1401 rfork(RFFDG|RFNOTEG);
1403 sfd[0] = open("/dev/null", OREAD);
1404 sfd[1] = open("/dev/null", OWRITE);
1405 sfd[2] = dup(erroutfd, -1);
1411 putenv("acmeaddr", argaddr);
1412 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1416 w = chartorune(&r, e);
1417 if(r==' ' || r=='\t')
1421 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1432 w = chartorune(&r, e);
1433 if(r==' ' || r=='\t'){
1440 av = realloc(av, (ac+1)*sizeof(char**));
1444 av = realloc(av, (ac+2)*sizeof(char**));
1450 * clumsy -- we're not running in a separate thread
1451 * so we have to save the current directory and put
1452 * it back when we're done. if this gets to be a regular
1453 * thing we could change threadexec to take a directory too.
1457 olddir = open(".", OREAD);
1458 dir = runetobyte(rdir, ndir);
1459 chdir(dir); /* ignore error: probably app. window */
1462 ret = threadspawn(sfd, av[0], av);
1472 /* libthread uses execvp so no need to do this */
1475 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1478 sprint(buf, "%s/%s", cputype, av[0]);
1479 procexec(cpid, sfd, buf, av);
1481 sprint(buf, "/bin/%s", av[0]);
1482 procexec(cpid, sfd, buf, av);
1488 * ugly: set path = (. $cputype /bin)
1489 * should honor $path if unusual.
1493 memmove(buf+n, ".", 2);
1495 i = strlen(cputype)+1;
1496 memmove(buf+n, cputype, i);
1498 memmove(buf+n, "/bin", 5);
1500 fd = create("/env/path", OWRITE, 0666);
1506 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1508 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1516 olddir = open(".", OREAD);
1517 dir = runetobyte(rdir, ndir);
1518 chdir(dir); /* ignore error: probably app. window */
1525 ret = threadspawn(sfd, rcarg[0], rcarg);
1535 warning(nil, "exec rc: %r\n");
1538 /* threadexec hasn't happened, so send a zero */
1541 if(sfd[2] != sfd[1])
1548 runwaittask(void *v)
1554 threadsetname("runwaittask");
1560 c->pid = recvul(cpid);
1561 while(c->pid == ~0);
1563 if(c->pid != 0) /* successful exec */
1576 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1585 arg = emalloc(10*sizeof(void*));
1586 c = emalloc(sizeof *c);
1587 cpid = chancreate(sizeof(ulong), 0);
1591 arg[3] = (void*)ndir;
1592 arg[4] = (void*)newns;
1597 arg[9] = (void*)iseditcmd;
1598 threadcreate(runproc, arg, STACK);
1599 /* mustn't block here because must be ready to answer mount() call in run() */
1600 arg = emalloc(2*sizeof(void*));
1603 threadcreate(runwaittask, arg, STACK);