17 void del(Text*, Text*, Text*, int, int, Rune*, int);
18 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
19 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
20 void dump(Text*, Text*, Text*, int, int, Rune*, int);
21 void edit(Text*, Text*, Text*, int, int, Rune*, int);
22 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
23 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
24 void get(Text*, Text*, Text*, int, int, Rune*, int);
25 void id(Text*, Text*, Text*, int, int, Rune*, int);
26 void incl(Text*, Text*, Text*, int, int, Rune*, int);
27 void indent(Text*, Text*, Text*, int, int, Rune*, int);
28 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
29 void local(Text*, Text*, Text*, int, int, Rune*, int);
30 void look(Text*, Text*, Text*, int, int, Rune*, int);
31 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
32 void paste(Text*, Text*, Text*, int, int, Rune*, int);
33 void put(Text*, Text*, Text*, int, int, Rune*, int);
34 void putall(Text*, Text*, Text*, int, int, Rune*, int);
35 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
36 void sort(Text*, Text*, Text*, int, int, Rune*, int);
37 void tab(Text*, Text*, Text*, int, int, Rune*, int);
38 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
40 typedef struct Exectab Exectab;
44 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
50 static Rune LCut[] = { 'C', 'u', 't', 0 };
51 static Rune LDel[] = { 'D', 'e', 'l', 0 };
52 static Rune LDelcol[] = { 'D', 'e', 'l', 'c', 'o', 'l', 0 };
53 static Rune LDelete[] = { 'D', 'e', 'l', 'e', 't', 'e', 0 };
54 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
55 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
56 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
57 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
58 static Rune LGet[] = { 'G', 'e', 't', 0 };
59 static Rune LID[] = { 'I', 'D', 0 };
60 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
61 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
62 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
63 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
64 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
65 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
66 static Rune LNew[] = { 'N', 'e', 'w', 0 };
67 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
68 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
69 static Rune LPut[] = { 'P', 'u', 't', 0 };
70 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
71 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
72 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
73 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
74 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
75 static Rune LTab[] = { 'T', 'a', 'b', 0 };
76 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
77 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
80 { LCut, cut, TRUE, TRUE, TRUE },
81 { LDel, del, FALSE, FALSE, XXX },
82 { LDelcol, delcol, FALSE, XXX, XXX },
83 { LDelete, del, FALSE, TRUE, XXX },
84 { LDump, dump, FALSE, TRUE, XXX },
85 { LEdit, edit, FALSE, XXX, XXX },
86 { LExit, xexit, FALSE, XXX, XXX },
87 { LFont, fontx, FALSE, XXX, XXX },
88 { LGet, get, FALSE, TRUE, XXX },
89 { LID, id, FALSE, XXX, XXX },
90 { LIncl, incl, FALSE, XXX, XXX },
91 { LIndent, indent, FALSE, XXX, XXX },
92 { LKill, xkill, FALSE, XXX, XXX },
93 { LLoad, dump, FALSE, FALSE, XXX },
94 { LLocal, local, FALSE, XXX, XXX },
95 { LLook, look, FALSE, XXX, XXX },
96 { LNew, new, FALSE, XXX, XXX },
97 { LNewcol, newcol, FALSE, XXX, XXX },
98 { LPaste, paste, TRUE, TRUE, XXX },
99 { LPut, put, FALSE, XXX, XXX },
100 { LPutall, putall, FALSE, XXX, XXX },
101 { LRedo, undo, FALSE, FALSE, XXX },
102 { LSend, sendx, TRUE, XXX, XXX },
103 { LSnarf, cut, FALSE, TRUE, FALSE },
104 { LSort, sort, FALSE, XXX, XXX },
105 { LTab, tab, FALSE, XXX, XXX },
106 { LUndo, undo, FALSE, TRUE, XXX },
107 { LZerox, zeroxx, FALSE, XXX, XXX },
112 lookup(Rune *r, int n)
117 r = skipbl(r, n, &n);
122 for(e=exectab; e->name; e++)
123 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
133 return c=='<' || c=='|' || c=='>';
137 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
148 if(q1 == q0){ /* expand to find word (actually file name) */
149 /* if in selection, choose selection */
150 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
154 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
156 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
162 r = runemalloc(q1-q0);
163 bufread(&t->file->b, q0, r, q1-q0);
164 e = lookup(r, q1-q0);
165 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
169 if(q0!=aq0 || q1!=aq1){
170 bufread(&t->file->b, aq0, r, aq1-aq0);
173 aa = getbytearg(argt, TRUE, TRUE, &a);
175 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
178 warning(nil, "`argument string too long\n");
188 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
190 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
191 if(q0!=aq0 || q1!=aq1){
193 bufread(&t->file->b, q0, r, n);
195 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
197 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
200 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
202 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
204 winevent(t->w, "%c0 0 0 0 \n", c);
212 if(e->mark && seltext!=nil)
213 if(seltext->what == Body){
215 filemark(seltext->w->body.file);
217 s = skipbl(r, q1-q0, &n);
218 s = findbl(s, n, &n);
219 s = skipbl(s, n, &n);
220 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
225 b = runetobyte(r, q1-q0);
227 dir = dirname(t, nil, 0);
228 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
233 aa = getbytearg(argt, TRUE, TRUE, &a);
236 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
240 printarg(Text *argt, uint q0, uint q1)
244 if(argt->what!=Body || argt->file->name==nil)
246 buf = emalloc(argt->file->nname+32);
248 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
250 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
255 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
266 textcommit(argt, TRUE);
267 if(expand(argt, argt->q0, argt->q1, &e)){
269 if(e.nname && dofile){
270 e.name = runerealloc(e.name, e.nname+1);
272 a = printarg(argt, e.q0, e.q1);
283 *rp = runemalloc(n+1);
284 bufread(&argt->file->b, e.q0, *rp, n);
286 a = printarg(argt, e.q0, e.q1);
292 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
299 aa = getarg(argt, doaddr, dofile, &r, &n);
302 *bp = runetobyte(r, n);
308 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
319 c = rowadd(et->row, nil, -1);
321 winsettag(coladd(c, nil, nil, -1));
325 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
339 if(c==nil || colclean(c)==0)
341 for(i=0; i<c->nw; i++){
343 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
344 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
348 rowclose(et->col->row, et->col, TRUE);
352 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
360 if(et->col==nil || et->w == nil)
362 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
363 colclose(et->col, et->w, TRUE);
367 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
381 seqof(Window *w, int isundo)
383 /* if it's undo, see who changed with us */
385 return w->body.file->seq;
386 /* if it's redo, see who we'll be sync'ed up with */
387 return fileredoseq(w->body.file);
391 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
404 if(et==nil || et->w== nil)
406 seq = seqof(et->w, flag1);
408 /* nothing to undo */
412 * Undo the executing window first. Its display will update. other windows
413 * in the same file will not call show() and jump to a different location in the file.
414 * Simultaneous changes to other files will be chaotic, however.
416 winundo(et->w, flag1);
417 for(i=0; i<row.ncol; i++){
419 for(j=0; j<c->nw; j++){
423 if(seqof(w, flag1) == seq)
430 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
437 getarg(argt, FALSE, TRUE, &r, &n);
442 /* if are doing a Put, want to synthesize name even for non-existent file */
443 /* best guess is that file name doesn't contain a slash */
459 s = runetobyte(t->file->name, t->file->nname);
462 /* prefix with directory name if necessary */
465 if(n>0 && arg[0]!='/'){
466 dir = dirname(t, nil, 0);
467 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
474 r = runemalloc(dir.nr+n+1);
475 runemove(r, dir.r, dir.nr);
477 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
479 runemove(r+dir.nr, arg, n);
486 s = runetobyte(r, n);
496 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
508 if(t!=nil && t->w!=nil && t->w!=et->w){
517 if(t==nil || t->w==nil)
521 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
523 nw = coladd(t->w->col, nil, t->w, -1);
524 /* ugly: fix locks so w->unlock works */
525 winlock1(nw, t->w->owner);
532 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
536 int i, n, dirty, samename, isdir;
544 if(et==nil || et->w==nil)
546 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
550 name = getname(t, argt, arg, narg, FALSE);
552 warning(nil, "no file name\n");
555 if(t->file->ntext>1){
557 isdir = (d!=nil && (d->qid.type & QTDIR));
560 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
564 r = bytetorune(name, &n);
565 for(i=0; i<t->file->ntext; i++){
566 u = t->file->text[i];
567 /* second and subsequent calls with zero an already empty buffer, but OK */
571 samename = runeeq(r, n, t->file->name, t->file->nname);
572 textload(t, 0, name, samename);
574 t->file->mod = FALSE;
580 for(i=0; i<t->file->ntext; i++)
581 t->file->text[i]->w->dirty = dirty;
585 t->file->unread = FALSE;
586 for(i=0; i<t->file->ntext; i++){
587 u = t->file->text[i];
588 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
594 putfile(File *f, int q0, int q1, Rune *namer, int nname)
605 name = runetobyte(namer, nname);
607 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
608 /* f->mtime+1 because when talking over NFS it's often off by a second */
609 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime+1<d->mtime){
611 f->qidpath = d->qid.path;
614 warning(nil, "%s not written; file already exists\n", name);
616 warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
620 fd = create(name, OWRITE, 0666);
622 warning(nil, "can't create file %s: %r\n", name);
629 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
631 warning(nil, "%s not written; file is append only\n", name);
635 for(q=q0; q<q1; q+=n){
637 if(n > BUFSIZE/UTFmax)
639 bufread(&f->b, q, r, n);
640 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
641 if(write(fd, s, m) != m){
642 warning(nil, "can't write file %s: %r\n", name);
646 if(runeeq(namer, nname, f->name, f->nname)){
647 if(q0!=0 || q1!=f->b.nc){
657 f->qidpath = d->qid.path;
664 for(i=0; i<f->ntext; i++){
665 f->text[i]->w->putseq = f->seq;
666 f->text[i]->w->dirty = w->dirty;
691 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
703 if(et==nil || et->w==nil || et->w->isdir)
707 name = getname(&w->body, argt, arg, narg, TRUE);
709 warning(nil, "no file name\n");
712 namer = bytetorune(name, &nname);
713 putfile(f, 0, f->b.nc, namer, nname);
718 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
727 name = runetobyte(arg, narg);
729 getbytearg(argt, FALSE, TRUE, &name);
733 rowload(&row, name, FALSE);
738 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
740 uint q0, q1, n, locked, c;
747 /* use current window if snarfing and its selection is non-null */
748 if(et!=t && dosnarf && et->w!=nil){
749 if(et->w->body.q1>et->w->body.q0){
752 filemark(t->file); /* seq has been incremented by execute */
753 }else if(et->w->tag.q1>et->w->tag.q0)
757 /* can only happen if seltext == nil */
761 if(t->w!=nil && et->w!=t->w){
776 bufdelete(&snarfbuf, 0, snarfbuf.nc);
782 bufread(&t->file->b, q0, r, n);
783 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
790 textdelete(t, t->q0, t->q1, TRUE);
791 textsetselect(t, t->q0, t->q0);
796 }else if(dosnarf) /* Snarf command */
803 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
813 /* if(tobody), use body of executing window (Paste or Send command) */
814 if(tobody && et!=nil && et->w!=nil){
816 filemark(t->file); /* seq has been incremented by execute */
822 if(t==nil || snarfbuf.nc==0)
824 if(t->w!=nil && et->w!=t->w){
830 cut(t, t, nil, FALSE, TRUE, nil, 0);
833 q1 = t->q0+snarfbuf.nc;
841 bufread(&snarfbuf, q, r, n);
842 textinsert(t, q0, r, n, TRUE);
848 textsetselect(t, t->q0, q1);
850 textsetselect(t, q1, q1);
855 if(t->w!=nil && et->w!=t->w)
860 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
871 search(t, arg, narg);
874 getarg(argt, FALSE, FALSE, &r, &n);
878 bufread(&t->file->b, t->q0, r, n);
885 static Rune Lnl[] = { '\n', 0 };
888 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
900 cut(t, t, nil, TRUE, FALSE, nil, 0);
901 textsetselect(t, t->file->b.nc, t->file->b.nc);
902 paste(t, t, nil, TRUE, TRUE, nil, 0);
903 if(textreadc(t, t->file->b.nc-1) != '\n'){
904 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
905 textsetselect(t, t->file->b.nc, t->file->b.nc);
910 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
921 getarg(argt, FALSE, TRUE, &r, &len);
927 editcmd(et, arg, narg);
931 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
948 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
963 for(i=0; i<row.ncol; i++){
965 for(j=0; j<c->nw; j++){
967 if(w->isscratch || w->isdir || w->body.file->nname==0)
969 if(w->nopen[QWevent] > 0)
971 a = runetobyte(w->body.file->name, w->body.file->nname);
973 if(w->body.file->mod || w->body.ncache)
975 warning(nil, "no auto-Put of %s: %r\n", a);
977 wincommit(w, &w->body);
978 put(&w->body, nil, nil, XXX, XXX, nil, 0);
987 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
998 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1002 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1011 aa = getbytearg(argt, TRUE, TRUE, &a);
1013 dir = dirname(et, nil, 0);
1014 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1019 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1023 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1033 getarg(argt, FALSE, FALSE, &r, &na);
1035 xkill(nil, nil, nil, 0, 0, r, na);
1036 /* loop condition: *arg is not a blank */
1038 a = findbl(arg, narg, &na);
1041 cmd = runemalloc(narg-na+1);
1042 runemove(cmd, arg, narg-na);
1044 arg = skipbl(a, na, &narg);
1048 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1049 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1052 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1054 Rune *a, *r, *flag, *file;
1064 if(et==nil || et->w==nil)
1069 /* loop condition: *arg is not a blank */
1072 a = findbl(arg, narg, &na);
1075 r = runemalloc(narg-na+1);
1076 runemove(r, arg, narg-na);
1077 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1085 arg = skipbl(a, na, &narg);
1087 getarg(argt, FALSE, TRUE, &r, &na);
1089 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1099 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1100 else if(file == nil){
1101 newfont = rfget(FALSE, FALSE, FALSE, nil);
1103 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1106 aa = runetobyte(file, nf);
1107 newfont = rfget(fix, flag!=nil, FALSE, aa);
1110 newfont = rfget(fix, FALSE, FALSE, nil);
1112 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1113 rfclose(t->reffont);
1114 t->reffont = newfont;
1115 t->fr.font = newfont->f;
1118 t->all.min.x++; /* force recolumnation; disgusting! */
1119 for(i=0; i<t->w->ndl; i++){
1121 aa = runetobyte(dp->r, dp->nr);
1122 dp->wid = stringwidth(newfont->f, aa);
1126 /* avoid shrinking of window due to quantization */
1127 colgrow(t->w->col, t->w, -1);
1134 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1144 if(et==nil || et->w==nil)
1148 getarg(argt, FALSE, TRUE, &r, &len);
1151 winaddincl(w, r, len);
1153 /* loop condition: *arg is not a blank */
1155 a = findbl(arg, narg, &na);
1158 r = runemalloc(narg-na+1);
1159 runemove(r, arg, narg-na);
1161 winaddincl(w, r, narg-na);
1162 arg = skipbl(a, na, &narg);
1164 if(n==0 && w->nincl){
1165 for(n=w->nincl; --n>=0; )
1166 warning(nil, "%S ", w->incl[n]);
1171 static Rune LON[] = { 'O', 'N', 0 };
1172 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1173 static Rune Lon[] = { 'o', 'n', 0 };
1183 indentval(Rune *s, int n)
1187 if(runestrncmp(s, LON, n) == 0){
1188 globalautoindent = TRUE;
1189 warning(nil, "Indent ON\n");
1192 if(runestrncmp(s, LOFF, n) == 0){
1193 globalautoindent = FALSE;
1194 warning(nil, "Indent OFF\n");
1197 return runestrncmp(s, Lon, n) == 0;
1201 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1205 int na, len, autoindent;
1212 if(et!=nil && et->w!=nil)
1214 autoindent = IError;
1215 getarg(argt, FALSE, TRUE, &r, &len);
1217 autoindent = indentval(r, len);
1219 a = findbl(arg, narg, &na);
1221 autoindent = indentval(arg, narg-na);
1227 w->autoindent = autoindent;
1230 w->autoindent = globalautoindent;
1237 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1248 if(et==nil || et->w==nil)
1251 getarg(argt, FALSE, TRUE, &r, &len);
1253 if(r!=nil && len>0){
1254 p = runetobyte(r, len);
1255 if('0'<=p[0] && p[0]<='9')
1259 a = findbl(arg, narg, &na);
1261 p = runetobyte(arg, narg-na);
1262 if('0'<=p[0] && p[0]<='9')
1268 if(w->body.tabstop != tab){
1269 w->body.tabstop = tab;
1270 winresize(w, w->r, 1);
1273 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1277 runproc(void *argvp)
1291 char *e, *t, *name, *filename, *dir, **av, *news;
1293 int ac, w, inarg, i, n, fd, nincl, winid;
1299 //static void *parg[2];
1308 ndir = (int)argv[3];
1309 newns = (int)argv[4];
1314 iseditcmd = (int)argv[9];
1318 while(*t==' ' || *t=='\n' || *t=='\t')
1321 if(*e==' ' || *e=='\n' || *e=='\t' )
1323 name = emalloc((e-t)+2);
1324 memmove(name, t, e-t);
1326 e = utfrrune(name, '/');
1328 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1329 strcat(name, " "); /* add blank here for ease in waittask */
1330 c->name = bytetorune(name, &c->nname);
1333 if(*t=='<' || *t=='|' || *t=='>')
1335 c->iseditcmd = iseditcmd;
1341 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1344 incl = emalloc(nincl*sizeof(Rune*));
1345 for(i=0; i<nincl; i++){
1346 n = runestrlen(win->incl[i]);
1347 incl[i] = runemalloc(n+1);
1348 runemove(incl[i], win->incl[i], n);
1356 winid = activewin->id;
1358 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1359 sprint(buf, "%d", winid);
1360 putenv("winid", buf);
1363 putenv("%", filename);
1366 c->md = fsysmount(rdir, ndir, incl, nincl);
1368 fprint(2, "child: can't allocate mntdir: %r\n");
1369 threadexits("fsysmount");
1371 sprint(buf, "%d", c->md->id);
1372 if((fs = nsmount("acme", buf)) == nil){
1373 fprint(2, "child: can't mount acme: %r\n");
1376 threadexits("nsmount");
1378 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1379 sprint(buf, "%d/rdsel", winid);
1380 sfd[0] = fsopenfd(fs, buf, OREAD);
1382 sfd[0] = open("/dev/null", OREAD);
1383 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1386 sprint(buf, "%d/editout", winid);
1388 sprint(buf, "editout");
1390 sprint(buf, "%d/wrsel", winid);
1391 sfd[1] = fsopenfd(fs, buf, OWRITE);
1392 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1394 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1399 rfork(RFFDG|RFNOTEG);
1401 sfd[0] = open("/dev/null", OREAD);
1402 sfd[1] = open("/dev/null", OWRITE);
1403 sfd[2] = dup(erroutfd, -1);
1409 putenv("acmeaddr", argaddr);
1410 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1414 w = chartorune(&r, e);
1415 if(r==' ' || r=='\t')
1419 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1430 w = chartorune(&r, e);
1431 if(r==' ' || r=='\t'){
1438 av = realloc(av, (ac+1)*sizeof(char**));
1442 av = realloc(av, (ac+2)*sizeof(char**));
1448 * clumsy -- we're not running in a separate thread
1449 * so we have to save the current directory and put
1450 * it back when we're done. if this gets to be a regular
1451 * thing we could change threadexec to take a directory too.
1455 olddir = open(".", OREAD);
1456 dir = runetobyte(rdir, ndir);
1457 chdir(dir); /* ignore error: probably app. window */
1460 ret = threadspawn(sfd, av[0], av);
1470 /* libthread uses execvp so no need to do this */
1473 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1476 sprint(buf, "%s/%s", cputype, av[0]);
1477 procexec(cpid, sfd, buf, av);
1479 sprint(buf, "/bin/%s", av[0]);
1480 procexec(cpid, sfd, buf, av);
1486 * ugly: set path = (. $cputype /bin)
1487 * should honor $path if unusual.
1491 memmove(buf+n, ".", 2);
1493 i = strlen(cputype)+1;
1494 memmove(buf+n, cputype, i);
1496 memmove(buf+n, "/bin", 5);
1498 fd = create("/env/path", OWRITE, 0666);
1504 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1506 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1514 olddir = open(".", OREAD);
1515 dir = runetobyte(rdir, ndir);
1516 chdir(dir); /* ignore error: probably app. window */
1523 ret = threadspawn(sfd, rcarg[0], rcarg);
1533 warning(nil, "exec rc: %r\n");
1536 /* threadexec hasn't happened, so send a zero */
1539 if(sfd[2] != sfd[1])
1546 runwaittask(void *v)
1552 threadsetname("runwaittask");
1558 c->pid = recvul(cpid);
1559 while(c->pid == ~0);
1561 if(c->pid != 0) /* successful exec */
1574 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1583 arg = emalloc(10*sizeof(void*));
1584 c = emalloc(sizeof *c);
1585 cpid = chancreate(sizeof(ulong), 0);
1586 chansetname(cpid, "cpid %s", s);
1590 arg[3] = (void*)ndir;
1591 arg[4] = (void*)newns;
1596 arg[9] = (void*)iseditcmd;
1597 threadcreate(runproc, arg, STACK);
1598 /* mustn't block here because must be ready to answer mount() call in run() */
1599 arg = emalloc(2*sizeof(void*));
1602 threadcreate(runwaittask, arg, STACK);