17 void doabort(Text*, Text*, Text*, int, int, Rune*, int);
18 void del(Text*, Text*, Text*, int, int, Rune*, int);
19 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
20 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
21 void dump(Text*, Text*, Text*, int, int, Rune*, int);
22 void edit(Text*, Text*, Text*, int, int, Rune*, int);
23 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
24 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
25 void get(Text*, Text*, Text*, int, int, Rune*, int);
26 void id(Text*, Text*, Text*, int, int, Rune*, int);
27 void incl(Text*, Text*, Text*, int, int, Rune*, int);
28 void indent(Text*, Text*, Text*, int, int, Rune*, int);
29 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
30 void local(Text*, Text*, Text*, int, int, Rune*, int);
31 void look(Text*, Text*, Text*, int, int, Rune*, int);
32 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
33 void paste(Text*, Text*, Text*, int, int, Rune*, int);
34 void put(Text*, Text*, Text*, int, int, Rune*, int);
35 void putall(Text*, Text*, Text*, int, int, Rune*, int);
36 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
37 void sort(Text*, Text*, Text*, int, int, Rune*, int);
38 void tab(Text*, Text*, Text*, int, int, Rune*, int);
39 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
41 typedef struct Exectab Exectab;
45 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
51 static Rune LAbort[] = { 'A', 'b', 'o', 'r', 't', 0 };
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 { LAbort, doabort, FALSE, XXX, XXX, },
83 { LCut, cut, TRUE, TRUE, TRUE },
84 { LDel, del, FALSE, FALSE, XXX },
85 { LDelcol, delcol, FALSE, XXX, XXX },
86 { LDelete, del, FALSE, TRUE, XXX },
87 { LDump, dump, FALSE, TRUE, XXX },
88 { LEdit, edit, FALSE, XXX, XXX },
89 { LExit, xexit, FALSE, XXX, XXX },
90 { LFont, fontx, FALSE, XXX, XXX },
91 { LGet, get, FALSE, TRUE, XXX },
92 { LID, id, FALSE, XXX, XXX },
93 { LIncl, incl, FALSE, XXX, XXX },
94 { LIndent, indent, FALSE, XXX, XXX },
95 { LKill, xkill, FALSE, XXX, XXX },
96 { LLoad, dump, FALSE, FALSE, XXX },
97 { LLocal, local, FALSE, XXX, XXX },
98 { LLook, look, FALSE, XXX, XXX },
99 { LNew, new, FALSE, XXX, XXX },
100 { LNewcol, newcol, FALSE, XXX, XXX },
101 { LPaste, paste, TRUE, TRUE, XXX },
102 { LPut, put, FALSE, XXX, XXX },
103 { LPutall, putall, FALSE, XXX, XXX },
104 { LRedo, undo, FALSE, FALSE, XXX },
105 { LSend, sendx, TRUE, XXX, XXX },
106 { LSnarf, cut, FALSE, TRUE, FALSE },
107 { LSort, sort, FALSE, XXX, XXX },
108 { LTab, tab, FALSE, XXX, XXX },
109 { LUndo, undo, FALSE, TRUE, XXX },
110 { LZerox, zeroxx, FALSE, XXX, XXX },
115 lookup(Rune *r, int n)
120 r = skipbl(r, n, &n);
125 for(e=exectab; e->name; e++)
126 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
136 return c=='<' || c=='|' || c=='>';
140 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
151 if(q1 == q0){ /* expand to find word (actually file name) */
152 /* if in selection, choose selection */
153 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
157 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
159 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
165 r = runemalloc(q1-q0);
166 bufread(&t->file->b, q0, r, q1-q0);
167 e = lookup(r, q1-q0);
168 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
172 if(q0!=aq0 || q1!=aq1){
173 bufread(&t->file->b, aq0, r, aq1-aq0);
176 aa = getbytearg(argt, TRUE, TRUE, &a);
178 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
181 warning(nil, "`argument string too long\n");
191 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
193 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
194 if(q0!=aq0 || q1!=aq1){
196 bufread(&t->file->b, q0, r, n);
198 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
200 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
203 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
205 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
207 winevent(t->w, "%c0 0 0 0 \n", c);
215 if(e->mark && seltext!=nil)
216 if(seltext->what == Body){
218 filemark(seltext->w->body.file);
220 s = skipbl(r, q1-q0, &n);
221 s = findbl(s, n, &n);
222 s = skipbl(s, n, &n);
223 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
228 b = runetobyte(r, q1-q0);
230 dir = dirname(t, nil, 0);
231 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
236 aa = getbytearg(argt, TRUE, TRUE, &a);
239 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
243 printarg(Text *argt, uint q0, uint q1)
247 if(argt->what!=Body || argt->file->name==nil)
249 buf = emalloc(argt->file->nname+32);
251 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
253 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
258 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
269 textcommit(argt, TRUE);
270 if(expand(argt, argt->q0, argt->q1, &e)){
272 if(e.nname && dofile){
273 e.name = runerealloc(e.name, e.nname+1);
275 a = printarg(argt, e.q0, e.q1);
286 *rp = runemalloc(n+1);
287 bufread(&argt->file->b, e.q0, *rp, n);
289 a = printarg(argt, e.q0, e.q1);
295 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
302 aa = getarg(argt, doaddr, dofile, &r, &n);
305 *bp = runetobyte(r, n);
311 doabort(Text *__0, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
324 warning(nil, "executing Abort again will call abort()\n");
330 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
341 c = rowadd(et->row, nil, -1);
343 winsettag(coladd(c, nil, nil, -1));
347 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
361 if(c==nil || colclean(c)==0)
363 for(i=0; i<c->nw; i++){
365 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
366 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
370 rowclose(et->col->row, et->col, TRUE);
374 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
382 if(et->col==nil || et->w == nil)
384 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
385 colclose(et->col, et->w, TRUE);
389 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
403 seqof(Window *w, int isundo)
405 /* if it's undo, see who changed with us */
407 return w->body.file->seq;
408 /* if it's redo, see who we'll be sync'ed up with */
409 return fileredoseq(w->body.file);
413 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
426 if(et==nil || et->w== nil)
428 seq = seqof(et->w, flag1);
430 /* nothing to undo */
434 * Undo the executing window first. Its display will update. other windows
435 * in the same file will not call show() and jump to a different location in the file.
436 * Simultaneous changes to other files will be chaotic, however.
438 winundo(et->w, flag1);
439 for(i=0; i<row.ncol; i++){
441 for(j=0; j<c->nw; j++){
445 if(seqof(w, flag1) == seq)
452 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
459 getarg(argt, FALSE, TRUE, &r, &n);
464 /* if are doing a Put, want to synthesize name even for non-existent file */
465 /* best guess is that file name doesn't contain a slash */
481 s = runetobyte(t->file->name, t->file->nname);
484 /* prefix with directory name if necessary */
487 if(n>0 && arg[0]!='/'){
488 dir = dirname(t, nil, 0);
489 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
496 r = runemalloc(dir.nr+n+1);
497 runemove(r, dir.r, dir.nr);
499 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
501 runemove(r+dir.nr, arg, n);
508 s = runetobyte(r, n);
518 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
530 if(t!=nil && t->w!=nil && t->w!=et->w){
539 if(t==nil || t->w==nil)
543 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
545 nw = coladd(t->w->col, nil, t->w, -1);
546 /* ugly: fix locks so w->unlock works */
547 winlock1(nw, t->w->owner);
554 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
558 int i, n, dirty, samename, isdir;
566 if(et==nil || et->w==nil)
568 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
572 name = getname(t, argt, arg, narg, FALSE);
574 warning(nil, "no file name\n");
577 if(t->file->ntext>1){
579 isdir = (d!=nil && (d->qid.type & QTDIR));
582 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
586 r = bytetorune(name, &n);
587 for(i=0; i<t->file->ntext; i++){
588 u = t->file->text[i];
589 /* second and subsequent calls with zero an already empty buffer, but OK */
593 samename = runeeq(r, n, t->file->name, t->file->nname);
594 textload(t, 0, name, samename);
596 t->file->mod = FALSE;
602 for(i=0; i<t->file->ntext; i++)
603 t->file->text[i]->w->dirty = dirty;
607 t->file->unread = FALSE;
608 for(i=0; i<t->file->ntext; i++){
609 u = t->file->text[i];
610 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
616 putfile(File *f, int q0, int q1, Rune *namer, int nname)
627 name = runetobyte(namer, nname);
629 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
630 /* f->mtime+1 because when talking over NFS it's often off by a second */
631 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime+1<d->mtime){
633 f->qidpath = d->qid.path;
636 warning(nil, "%s not written; file already exists\n", name);
638 warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
642 fd = create(name, OWRITE, 0666);
644 warning(nil, "can't create file %s: %r\n", name);
651 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
653 warning(nil, "%s not written; file is append only\n", name);
657 for(q=q0; q<q1; q+=n){
659 if(n > BUFSIZE/UTFmax)
661 bufread(&f->b, q, r, n);
662 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
663 if(write(fd, s, m) != m){
664 warning(nil, "can't write file %s: %r\n", name);
668 if(runeeq(namer, nname, f->name, f->nname)){
669 if(q0!=0 || q1!=f->b.nc){
679 f->qidpath = d->qid.path;
686 for(i=0; i<f->ntext; i++){
687 f->text[i]->w->putseq = f->seq;
688 f->text[i]->w->dirty = w->dirty;
713 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
725 if(et==nil || et->w==nil || et->w->isdir)
729 name = getname(&w->body, argt, arg, narg, TRUE);
731 warning(nil, "no file name\n");
734 namer = bytetorune(name, &nname);
735 putfile(f, 0, f->b.nc, namer, nname);
740 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
749 name = runetobyte(arg, narg);
751 getbytearg(argt, FALSE, TRUE, &name);
755 rowload(&row, name, FALSE);
760 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
762 uint q0, q1, n, locked, c;
769 /* use current window if snarfing and its selection is non-null */
770 if(et!=t && dosnarf && et->w!=nil){
771 if(et->w->body.q1>et->w->body.q0){
774 filemark(t->file); /* seq has been incremented by execute */
775 }else if(et->w->tag.q1>et->w->tag.q0)
779 /* can only happen if seltext == nil */
783 if(t->w!=nil && et->w!=t->w){
798 bufdelete(&snarfbuf, 0, snarfbuf.nc);
804 bufread(&t->file->b, q0, r, n);
805 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
812 textdelete(t, t->q0, t->q1, TRUE);
813 textsetselect(t, t->q0, t->q0);
818 }else if(dosnarf) /* Snarf command */
825 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
835 /* if(tobody), use body of executing window (Paste or Send command) */
836 if(tobody && et!=nil && et->w!=nil){
838 filemark(t->file); /* seq has been incremented by execute */
844 if(t==nil || snarfbuf.nc==0)
846 if(t->w!=nil && et->w!=t->w){
852 cut(t, t, nil, FALSE, TRUE, nil, 0);
855 q1 = t->q0+snarfbuf.nc;
863 bufread(&snarfbuf, q, r, n);
864 textinsert(t, q0, r, n, TRUE);
870 textsetselect(t, t->q0, q1);
872 textsetselect(t, q1, q1);
877 if(t->w!=nil && et->w!=t->w)
882 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
893 search(t, arg, narg);
896 getarg(argt, FALSE, FALSE, &r, &n);
900 bufread(&t->file->b, t->q0, r, n);
907 static Rune Lnl[] = { '\n', 0 };
910 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
922 cut(t, t, nil, TRUE, FALSE, nil, 0);
923 textsetselect(t, t->file->b.nc, t->file->b.nc);
924 paste(t, t, nil, TRUE, TRUE, nil, 0);
925 if(textreadc(t, t->file->b.nc-1) != '\n'){
926 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
927 textsetselect(t, t->file->b.nc, t->file->b.nc);
932 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
943 getarg(argt, FALSE, TRUE, &r, &len);
949 editcmd(et, arg, narg);
953 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
970 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
985 for(i=0; i<row.ncol; i++){
987 for(j=0; j<c->nw; j++){
989 if(w->isscratch || w->isdir || w->body.file->nname==0)
991 if(w->nopen[QWevent] > 0)
993 a = runetobyte(w->body.file->name, w->body.file->nname);
995 if(w->body.file->mod || w->body.ncache)
997 warning(nil, "no auto-Put of %s: %r\n", a);
999 wincommit(w, &w->body);
1000 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1009 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1020 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1024 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1033 aa = getbytearg(argt, TRUE, TRUE, &a);
1035 dir = dirname(et, nil, 0);
1036 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1041 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1045 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1055 getarg(argt, FALSE, FALSE, &r, &na);
1057 xkill(nil, nil, nil, 0, 0, r, na);
1058 /* loop condition: *arg is not a blank */
1060 a = findbl(arg, narg, &na);
1063 cmd = runemalloc(narg-na+1);
1064 runemove(cmd, arg, narg-na);
1066 arg = skipbl(a, na, &narg);
1070 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1071 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1074 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1076 Rune *a, *r, *flag, *file;
1086 if(et==nil || et->w==nil)
1091 /* loop condition: *arg is not a blank */
1094 a = findbl(arg, narg, &na);
1097 r = runemalloc(narg-na+1);
1098 runemove(r, arg, narg-na);
1099 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1107 arg = skipbl(a, na, &narg);
1109 getarg(argt, FALSE, TRUE, &r, &na);
1111 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1121 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1122 else if(file == nil){
1123 newfont = rfget(FALSE, FALSE, FALSE, nil);
1125 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1128 aa = runetobyte(file, nf);
1129 newfont = rfget(fix, flag!=nil, FALSE, aa);
1132 newfont = rfget(fix, FALSE, FALSE, nil);
1134 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1135 rfclose(t->reffont);
1136 t->reffont = newfont;
1137 t->fr.font = newfont->f;
1140 t->all.min.x++; /* force recolumnation; disgusting! */
1141 for(i=0; i<t->w->ndl; i++){
1143 aa = runetobyte(dp->r, dp->nr);
1144 dp->wid = stringwidth(newfont->f, aa);
1148 /* avoid shrinking of window due to quantization */
1149 colgrow(t->w->col, t->w, -1);
1156 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1166 if(et==nil || et->w==nil)
1170 getarg(argt, FALSE, TRUE, &r, &len);
1173 winaddincl(w, r, len);
1175 /* loop condition: *arg is not a blank */
1177 a = findbl(arg, narg, &na);
1180 r = runemalloc(narg-na+1);
1181 runemove(r, arg, narg-na);
1183 winaddincl(w, r, narg-na);
1184 arg = skipbl(a, na, &narg);
1186 if(n==0 && w->nincl){
1187 for(n=w->nincl; --n>=0; )
1188 warning(nil, "%S ", w->incl[n]);
1193 static Rune LON[] = { 'O', 'N', 0 };
1194 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1195 static Rune Lon[] = { 'o', 'n', 0 };
1205 indentval(Rune *s, int n)
1209 if(runestrncmp(s, LON, n) == 0){
1210 globalautoindent = TRUE;
1211 warning(nil, "Indent ON\n");
1214 if(runestrncmp(s, LOFF, n) == 0){
1215 globalautoindent = FALSE;
1216 warning(nil, "Indent OFF\n");
1219 return runestrncmp(s, Lon, n) == 0;
1223 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1227 int na, len, autoindent;
1234 if(et!=nil && et->w!=nil)
1236 autoindent = IError;
1237 getarg(argt, FALSE, TRUE, &r, &len);
1239 autoindent = indentval(r, len);
1241 a = findbl(arg, narg, &na);
1243 autoindent = indentval(arg, narg-na);
1249 w->autoindent = autoindent;
1252 w->autoindent = globalautoindent;
1259 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1270 if(et==nil || et->w==nil)
1273 getarg(argt, FALSE, TRUE, &r, &len);
1275 if(r!=nil && len>0){
1276 p = runetobyte(r, len);
1277 if('0'<=p[0] && p[0]<='9')
1281 a = findbl(arg, narg, &na);
1283 p = runetobyte(arg, narg-na);
1284 if('0'<=p[0] && p[0]<='9')
1290 if(w->body.tabstop != tab){
1291 w->body.tabstop = tab;
1292 winresize(w, w->r, 1);
1295 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1299 runproc(void *argvp)
1313 char *e, *t, *name, *filename, *dir, **av, *news;
1315 int ac, w, inarg, i, n, fd, nincl, winid;
1321 //static void *parg[2];
1330 ndir = (int)argv[3];
1331 newns = (int)argv[4];
1336 iseditcmd = (int)argv[9];
1340 while(*t==' ' || *t=='\n' || *t=='\t')
1343 if(*e==' ' || *e=='\n' || *e=='\t' )
1345 name = emalloc((e-t)+2);
1346 memmove(name, t, e-t);
1348 e = utfrrune(name, '/');
1350 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1351 strcat(name, " "); /* add blank here for ease in waittask */
1352 c->name = bytetorune(name, &c->nname);
1355 if(*t=='<' || *t=='|' || *t=='>')
1357 c->iseditcmd = iseditcmd;
1363 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1366 incl = emalloc(nincl*sizeof(Rune*));
1367 for(i=0; i<nincl; i++){
1368 n = runestrlen(win->incl[i]);
1369 incl[i] = runemalloc(n+1);
1370 runemove(incl[i], win->incl[i], n);
1378 winid = activewin->id;
1380 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1381 sprint(buf, "%d", winid);
1382 putenv("winid", buf);
1385 putenv("%", filename);
1388 c->md = fsysmount(rdir, ndir, incl, nincl);
1390 fprint(2, "child: can't allocate mntdir: %r\n");
1391 threadexits("fsysmount");
1393 sprint(buf, "%d", c->md->id);
1394 if((fs = nsmount("acme", buf)) == nil){
1395 fprint(2, "child: can't mount acme: %r\n");
1398 threadexits("nsmount");
1400 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1401 sprint(buf, "%d/rdsel", winid);
1402 sfd[0] = fsopenfd(fs, buf, OREAD);
1404 sfd[0] = open("/dev/null", OREAD);
1405 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1408 sprint(buf, "%d/editout", winid);
1410 sprint(buf, "editout");
1412 sprint(buf, "%d/wrsel", winid);
1413 sfd[1] = fsopenfd(fs, buf, OWRITE);
1414 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1416 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1421 rfork(RFFDG|RFNOTEG);
1423 sfd[0] = open("/dev/null", OREAD);
1424 sfd[1] = open("/dev/null", OWRITE);
1425 sfd[2] = dup(erroutfd, -1);
1431 putenv("acmeaddr", argaddr);
1432 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1436 w = chartorune(&r, e);
1437 if(r==' ' || r=='\t')
1441 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1452 w = chartorune(&r, e);
1453 if(r==' ' || r=='\t'){
1460 av = realloc(av, (ac+1)*sizeof(char**));
1464 av = realloc(av, (ac+2)*sizeof(char**));
1470 * clumsy -- we're not running in a separate thread
1471 * so we have to save the current directory and put
1472 * it back when we're done. if this gets to be a regular
1473 * thing we could change threadexec to take a directory too.
1477 olddir = open(".", OREAD);
1478 dir = runetobyte(rdir, ndir);
1479 chdir(dir); /* ignore error: probably app. window */
1482 ret = threadspawn(sfd, av[0], av);
1492 /* libthread uses execvp so no need to do this */
1495 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1498 sprint(buf, "%s/%s", cputype, av[0]);
1499 procexec(cpid, sfd, buf, av);
1501 sprint(buf, "/bin/%s", av[0]);
1502 procexec(cpid, sfd, buf, av);
1508 * ugly: set path = (. $cputype /bin)
1509 * should honor $path if unusual.
1513 memmove(buf+n, ".", 2);
1515 i = strlen(cputype)+1;
1516 memmove(buf+n, cputype, i);
1518 memmove(buf+n, "/bin", 5);
1520 fd = create("/env/path", OWRITE, 0666);
1526 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1528 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1536 olddir = open(".", OREAD);
1537 dir = runetobyte(rdir, ndir);
1538 chdir(dir); /* ignore error: probably app. window */
1545 ret = threadspawn(sfd, rcarg[0], rcarg);
1555 warning(nil, "exec rc: %r\n");
1558 /* threadexec hasn't happened, so send a zero */
1561 if(sfd[2] != sfd[1])
1568 runwaittask(void *v)
1574 threadsetname("runwaittask");
1580 c->pid = recvul(cpid);
1581 while(c->pid == ~0);
1583 if(c->pid != 0) /* successful exec */
1596 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1605 arg = emalloc(10*sizeof(void*));
1606 c = emalloc(sizeof *c);
1607 cpid = chancreate(sizeof(ulong), 0);
1608 chansetname(cpid, "cpid %s", s);
1612 arg[3] = (void*)ndir;
1613 arg[4] = (void*)newns;
1618 arg[9] = (void*)iseditcmd;
1619 threadcreate(runproc, arg, STACK);
1620 /* mustn't block here because must be ready to answer mount() call in run() */
1621 arg = emalloc(2*sizeof(void*));
1624 threadcreate(runwaittask, arg, STACK);