18 * These functions get called as:
20 * fn(et, t, argt, flag1, flag1, flag2, s, n);
22 * Where the arguments are:
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)
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;
61 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
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 };
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 },
131 lookup(Rune *r, int n)
136 r = skipbl(r, n, &n);
141 for(e=exectab; e->name; e++)
142 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
152 return c=='<' || c=='|' || c=='>';
156 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
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){
173 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
175 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
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){
188 if(q0!=aq0 || q1!=aq1){
189 bufread(&t->file->b, aq0, r, aq1-aq0);
192 aa = getbytearg(argt, TRUE, TRUE, &a);
194 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
197 warning(nil, "argument string too long\n");
207 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
209 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
210 if(q0!=aq0 || q1!=aq1){
212 bufread(&t->file->b, q0, r, n);
214 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
216 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
219 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
221 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
223 winevent(t->w, "%c0 0 0 0 \n", c);
231 if(e->mark && seltext!=nil)
232 if(seltext->what == Body){
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);
244 b = runetobyte(r, q1-q0);
246 dir = dirname(t, nil, 0);
247 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
252 aa = getbytearg(argt, TRUE, TRUE, &a);
255 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
259 printarg(Text *argt, uint q0, uint q1)
263 if(argt->what!=Body || argt->file->name==nil)
265 buf = emalloc(argt->file->nname+32);
267 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
269 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
274 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
285 textcommit(argt, TRUE);
286 if(expand(argt, argt->q0, argt->q1, &e)){
288 if(e.nname && dofile){
289 e.name = runerealloc(e.name, e.nname+1);
291 a = printarg(argt, e.q0, e.q1);
302 *rp = runemalloc(n+1);
303 bufread(&argt->file->b, e.q0, *rp, n);
305 a = printarg(argt, e.q0, e.q1);
311 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
318 aa = getarg(argt, doaddr, dofile, &r, &n);
321 *bp = runetobyte(r, n);
327 doabort(Text *__0, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
340 warning(nil, "executing Abort again will call abort()\n");
346 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
357 c = rowadd(et->row, nil, -1);
359 winsettag(coladd(c, nil, nil, -1));
363 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
377 if(c==nil || colclean(c)==0)
379 for(i=0; i<c->nw; 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);
386 rowclose(et->col->row, et->col, TRUE);
390 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
398 if(et->col==nil || et->w == nil)
400 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
401 colclose(et->col, et->w, TRUE);
405 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
419 seqof(Window *w, int isundo)
421 /* if it's undo, see who changed with us */
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);
429 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
442 if(et==nil || et->w== nil)
444 seq = seqof(et->w, flag1);
446 /* nothing to undo */
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.
454 winundo(et->w, flag1);
455 for(i=0; i<row.ncol; i++){
457 for(j=0; j<c->nw; j++){
461 if(seqof(w, flag1) == seq)
468 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
475 getarg(argt, FALSE, TRUE, &r, &n);
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 */
497 s = runetobyte(t->file->name, t->file->nname);
500 /* prefix with directory name if necessary */
503 if(n>0 && arg[0]!='/'){
504 dir = dirname(t, nil, 0);
505 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
512 r = runemalloc(dir.nr+n+1);
513 runemove(r, dir.r, dir.nr);
515 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
517 runemove(r+dir.nr, arg, n);
524 s = runetobyte(r, n);
534 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
546 if(t!=nil && t->w!=nil && t->w!=et->w){
555 if(t==nil || t->w==nil)
559 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
561 nw = coladd(t->w->col, nil, t->w, -1);
562 /* ugly: fix locks so w->unlock works */
563 winlock1(nw, t->w->owner);
570 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
574 int i, n, dirty, samename, isdir;
582 if(et==nil || et->w==nil)
584 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
588 name = getname(t, argt, arg, narg, FALSE);
590 warning(nil, "no file name\n");
593 if(t->file->ntext>1){
595 isdir = (d!=nil && (d->qid.type & QTDIR));
598 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
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 */
609 samename = runeeq(r, n, t->file->name, t->file->nname);
610 textload(t, 0, name, samename);
612 t->file->mod = FALSE;
618 for(i=0; i<t->file->ntext; i++)
619 t->file->text[i]->w->dirty = dirty;
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);
632 putfile(File *f, int q0, int q1, Rune *namer, int nname)
643 name = runetobyte(namer, nname);
645 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
646 /* f->mtime+1 because when talking over NFS it's often off by a second */
647 if(f->dev!=d->dev || f->qidpath!=d->qid.path || abs(f->mtime-d->mtime) > 1){
649 warning(nil, "%s not written; file already exists\n", name);
651 warning(nil, "%s modified%s%s since last read\n\twas %t; now %t\n", name, d->muid[0]?" by ":"", d->muid, f->mtime, d->mtime);
653 f->qidpath = d->qid.path;
658 fd = create(name, OWRITE, 0666);
660 warning(nil, "can't create file %s: %r\n", name);
667 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
669 warning(nil, "%s not written; file is append only\n", name);
673 for(q=q0; q<q1; q+=n){
675 if(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);
684 if(runeeq(namer, nname, f->name, f->nname)){
685 if(q0!=0 || q1!=f->b.nc){
690 // In case the file is on NFS, reopen the fd
691 // before dirfstat to cause the attribute cache
692 // to be updated (otherwise the mtime in the
693 // dirfstat below will be stale and not match
694 // what NFS sees). The file is already written,
695 // so this should be a no-op when not on NFS.
696 // Opening for OWRITE (but no truncation)
697 // in case we don't have read permission.
698 // (The create above worked, so we probably
699 // still have write permission.)
701 fd = open(name, OWRITE);
708 f->qidpath = d->qid.path;
715 for(i=0; i<f->ntext; i++){
716 f->text[i]->w->putseq = f->seq;
717 f->text[i]->w->dirty = w->dirty;
742 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
754 if(et==nil || et->w==nil || et->w->isdir)
758 name = getname(&w->body, argt, arg, narg, TRUE);
760 warning(nil, "no file name\n");
763 namer = bytetorune(name, &nname);
764 putfile(f, 0, f->b.nc, namer, nname);
769 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
778 name = runetobyte(arg, narg);
780 getbytearg(argt, FALSE, TRUE, &name);
784 rowload(&row, name, FALSE);
789 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
791 uint q0, q1, n, locked, c;
799 * if not executing a mouse chord (et != t) and snarfing (dosnarf)
800 * and executed Cut or Snarf in window tag (et->w != nil),
801 * then use the window body selection or the tag selection
802 * or do nothing at all.
804 if(et!=t && dosnarf && et->w!=nil){
805 if(et->w->body.q1>et->w->body.q0){
808 filemark(t->file); /* seq has been incremented by execute */
809 }else if(et->w->tag.q1>et->w->tag.q0)
814 if(t == nil) /* no selection */
818 if(t->w!=nil && et->w!=t->w){
833 bufdelete(&snarfbuf, 0, snarfbuf.nc);
839 bufread(&t->file->b, q0, r, n);
840 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
847 textdelete(t, t->q0, t->q1, TRUE);
848 textsetselect(t, t->q0, t->q0);
853 }else if(dosnarf) /* Snarf command */
860 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
870 /* if(tobody), use body of executing window (Paste or Send command) */
871 if(tobody && et!=nil && et->w!=nil){
873 filemark(t->file); /* seq has been incremented by execute */
879 if(t==nil || snarfbuf.nc==0)
881 if(t->w!=nil && et->w!=t->w){
887 cut(t, t, nil, FALSE, TRUE, nil, 0);
890 q1 = t->q0+snarfbuf.nc;
898 bufread(&snarfbuf, q, r, n);
899 textinsert(t, q0, r, n, TRUE);
905 textsetselect(t, t->q0, q1);
907 textsetselect(t, q1, q1);
912 if(t->w!=nil && et->w!=t->w)
917 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
928 search(t, arg, narg);
931 getarg(argt, FALSE, FALSE, &r, &n);
935 bufread(&t->file->b, t->q0, r, n);
942 static Rune Lnl[] = { '\n', 0 };
945 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
957 cut(t, t, nil, TRUE, FALSE, nil, 0);
958 textsetselect(t, t->file->b.nc, t->file->b.nc);
959 paste(t, t, nil, TRUE, TRUE, nil, 0);
960 if(textreadc(t, t->file->b.nc-1) != '\n'){
961 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
962 textsetselect(t, t->file->b.nc, t->file->b.nc);
965 textshow(t, t->q1, t->q1, 1);
969 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
980 getarg(argt, FALSE, TRUE, &r, &len);
986 editcmd(et, arg, narg);
990 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1007 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1022 for(i=0; i<row.ncol; i++){
1024 for(j=0; j<c->nw; j++){
1026 if(w->isscratch || w->isdir || w->body.file->nname==0)
1028 if(w->nopen[QWevent] > 0)
1030 a = runetobyte(w->body.file->name, w->body.file->nname);
1032 if(w->body.file->mod || w->body.ncache)
1034 warning(nil, "no auto-Put of %s: %r\n", a);
1036 wincommit(w, &w->body);
1037 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1046 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1056 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1060 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1069 aa = getbytearg(argt, TRUE, TRUE, &a);
1071 dir = dirname(et, nil, 0);
1072 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1077 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1081 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1091 getarg(argt, FALSE, FALSE, &r, &na);
1093 xkill(nil, nil, nil, 0, 0, r, na);
1094 /* loop condition: *arg is not a blank */
1096 a = findbl(arg, narg, &na);
1099 cmd = runemalloc(narg-na+1);
1100 runemove(cmd, arg, narg-na);
1102 arg = skipbl(a, na, &narg);
1106 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1107 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1110 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1112 Rune *a, *r, *flag, *file;
1122 if(et==nil || et->w==nil)
1127 /* loop condition: *arg is not a blank */
1130 a = findbl(arg, narg, &na);
1133 r = runemalloc(narg-na+1);
1134 runemove(r, arg, narg-na);
1135 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1143 arg = skipbl(a, na, &narg);
1145 getarg(argt, FALSE, TRUE, &r, &na);
1147 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1157 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1158 else if(file == nil){
1159 newfont = rfget(FALSE, FALSE, FALSE, nil);
1161 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1164 aa = runetobyte(file, nf);
1165 newfont = rfget(fix, flag!=nil, FALSE, aa);
1168 newfont = rfget(fix, FALSE, FALSE, nil);
1170 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1171 rfclose(t->reffont);
1172 t->reffont = newfont;
1173 t->fr.font = newfont->f;
1176 t->all.min.x++; /* force recolumnation; disgusting! */
1177 for(i=0; i<t->w->ndl; i++){
1179 aa = runetobyte(dp->r, dp->nr);
1180 dp->wid = stringwidth(newfont->f, aa);
1184 /* avoid shrinking of window due to quantization */
1185 colgrow(t->w->col, t->w, -1);
1192 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1202 if(et==nil || et->w==nil)
1206 getarg(argt, FALSE, TRUE, &r, &len);
1209 winaddincl(w, r, len);
1211 /* loop condition: *arg is not a blank */
1213 a = findbl(arg, narg, &na);
1216 r = runemalloc(narg-na+1);
1217 runemove(r, arg, narg-na);
1219 winaddincl(w, r, narg-na);
1220 arg = skipbl(a, na, &narg);
1222 if(n==0 && w->nincl){
1223 for(n=w->nincl; --n>=0; )
1224 warning(nil, "%S ", w->incl[n]);
1229 static Rune LON[] = { 'O', 'N', 0 };
1230 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1231 static Rune Lon[] = { 'o', 'n', 0 };
1241 indentval(Rune *s, int n)
1245 if(runestrncmp(s, LON, n) == 0){
1246 globalautoindent = TRUE;
1247 warning(nil, "Indent ON\n");
1250 if(runestrncmp(s, LOFF, n) == 0){
1251 globalautoindent = FALSE;
1252 warning(nil, "Indent OFF\n");
1255 return runestrncmp(s, Lon, n) == 0;
1259 fixindent(Window *w, void *arg)
1262 w->autoindent = globalautoindent;
1266 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1270 int na, len, autoindent;
1277 if(et!=nil && et->w!=nil)
1279 autoindent = IError;
1280 getarg(argt, FALSE, TRUE, &r, &len);
1282 autoindent = indentval(r, len);
1284 a = findbl(arg, narg, &na);
1286 autoindent = indentval(arg, narg-na);
1288 if(autoindent == IGlobal)
1289 allwindows(fixindent, nil);
1290 else if(w != nil && autoindent >= 0)
1291 w->autoindent = autoindent;
1295 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1306 if(et==nil || et->w==nil)
1309 getarg(argt, FALSE, TRUE, &r, &len);
1311 if(r!=nil && len>0){
1312 p = runetobyte(r, len);
1313 if('0'<=p[0] && p[0]<='9')
1317 a = findbl(arg, narg, &na);
1319 p = runetobyte(arg, narg-na);
1320 if('0'<=p[0] && p[0]<='9')
1326 if(w->body.tabstop != tab){
1327 w->body.tabstop = tab;
1328 winresize(w, w->r, FALSE, TRUE);
1331 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1335 runproc(void *argvp)
1349 char *e, *t, *name, *filename, *dir, **av, *news;
1351 int ac, w, inarg, i, n, fd, nincl, winid;
1357 /*static void *parg[2]; */
1362 threadsetname("runproc");
1368 ndir = (uintptr)argv[3];
1369 newns = (uintptr)argv[4];
1374 iseditcmd = (uintptr)argv[9];
1378 while(*t==' ' || *t=='\n' || *t=='\t')
1381 if(*e==' ' || *e=='\n' || *e=='\t' )
1383 name = emalloc((e-t)+2);
1384 memmove(name, t, e-t);
1386 e = utfrrune(name, '/');
1388 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1389 strcat(name, " "); /* add blank here for ease in waittask */
1390 c->name = bytetorune(name, &c->nname);
1393 if(*t=='<' || *t=='|' || *t=='>')
1395 c->iseditcmd = iseditcmd;
1401 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1404 incl = emalloc(nincl*sizeof(Rune*));
1405 for(i=0; i<nincl; i++){
1406 n = runestrlen(win->incl[i]);
1407 incl[i] = runemalloc(n+1);
1408 runemove(incl[i], win->incl[i], n);
1416 winid = activewin->id;
1418 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1419 sprint(buf, "%d", winid);
1420 putenv("winid", buf);
1423 putenv("%", filename);
1426 c->md = fsysmount(rdir, ndir, incl, nincl);
1428 fprint(2, "child: can't allocate mntdir: %r\n");
1429 threadexits("fsysmount");
1431 sprint(buf, "%d", c->md->id);
1432 if((fs = nsmount("acme", buf)) == nil){
1433 fprint(2, "child: can't mount acme: %r\n");
1436 threadexits("nsmount");
1438 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1439 sprint(buf, "%d/rdsel", winid);
1440 sfd[0] = fsopenfd(fs, buf, OREAD);
1442 sfd[0] = open("/dev/null", OREAD);
1443 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1446 sprint(buf, "%d/editout", winid);
1448 sprint(buf, "editout");
1450 sprint(buf, "%d/wrsel", winid);
1451 sfd[1] = fsopenfd(fs, buf, OWRITE);
1452 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1454 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1459 rfork(RFFDG|RFNOTEG);
1461 sfd[0] = open("/dev/null", OREAD);
1462 sfd[1] = open("/dev/null", OWRITE);
1463 sfd[2] = dup(erroutfd, -1);
1469 putenv("acmeaddr", argaddr);
1470 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1474 w = chartorune(&r, e);
1475 if(r==' ' || r=='\t')
1479 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1490 w = chartorune(&r, e);
1491 if(r==' ' || r=='\t'){
1498 av = realloc(av, (ac+1)*sizeof(char**));
1502 av = realloc(av, (ac+2)*sizeof(char**));
1508 * clumsy -- we're not running in a separate thread
1509 * so we have to save the current directory and put
1510 * it back when we're done. if this gets to be a regular
1511 * thing we could change threadexec to take a directory too.
1515 olddir = open(".", OREAD);
1516 dir = runetobyte(rdir, ndir);
1517 chdir(dir); /* ignore error: probably app. window */
1520 ret = threadspawn(sfd, av[0], av);
1530 /* libthread uses execvp so no need to do this */
1533 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1536 sprint(buf, "%s/%s", cputype, av[0]);
1537 procexec(cpid, sfd, buf, av);
1539 sprint(buf, "/bin/%s", av[0]);
1540 procexec(cpid, sfd, buf, av);
1546 * ugly: set path = (. $cputype /bin)
1547 * should honor $path if unusual.
1551 memmove(buf+n, ".", 2);
1553 i = strlen(cputype)+1;
1554 memmove(buf+n, cputype, i);
1556 memmove(buf+n, "/bin", 5);
1558 fd = create("/env/path", OWRITE, 0666);
1564 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1566 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1574 olddir = open(".", OREAD);
1575 dir = runetobyte(rdir, ndir);
1576 chdir(dir); /* ignore error: probably app. window */
1583 ret = threadspawn(sfd, rcarg[0], rcarg);
1593 warning(nil, "exec rc: %r\n");
1596 /* threadexec hasn't happened, so send a zero */
1599 if(sfd[2] != sfd[1])
1606 runwaittask(void *v)
1612 threadsetname("runwaittask");
1618 c->pid = recvul(cpid);
1619 while(c->pid == ~0);
1621 if(c->pid != 0) /* successful exec */
1634 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1643 arg = emalloc(10*sizeof(void*));
1644 c = emalloc(sizeof *c);
1645 cpid = chancreate(sizeof(ulong), 0);
1646 chansetname(cpid, "cpid %s", s);
1650 arg[3] = (void*)(uintptr)ndir;
1651 arg[4] = (void*)(uintptr)newns;
1656 arg[9] = (void*)(uintptr)iseditcmd;
1657 threadcreate(runproc, arg, STACK);
1658 /* mustn't block here because must be ready to answer mount() call in run() */
1659 arg = emalloc(2*sizeof(void*));
1662 threadcreate(runwaittask, arg, STACK);