20 * These functions get called as:
22 * fn(et, t, argt, flag1, flag1, flag2, s, n);
24 * Where the arguments are:
26 * et: the Text* in which the executing event (click) occurred
27 * t: the Text* containing the current selection (Edit, Cut, Snarf, Paste)
28 * argt: the Text* containing the argument for a 2-1 click.
29 * e->flag1: from Exectab entry
30 * e->flag2: from Exectab entry
31 * s: the command line remainder (e.g., "x" if executing "Dump x")
32 * n: length of s (s is *not* NUL-terminated)
35 void doabort(Text*, Text*, Text*, int, int, Rune*, int);
36 void del(Text*, Text*, Text*, int, int, Rune*, int);
37 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
38 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
39 void dump(Text*, Text*, Text*, int, int, Rune*, int);
40 void edit(Text*, Text*, Text*, int, int, Rune*, int);
41 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
42 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
43 void get(Text*, Text*, Text*, int, int, Rune*, int);
44 void id(Text*, Text*, Text*, int, int, Rune*, int);
45 void incl(Text*, Text*, Text*, int, int, Rune*, int);
46 void indent(Text*, Text*, Text*, int, int, Rune*, int);
47 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
48 void local(Text*, Text*, Text*, int, int, Rune*, int);
49 void look(Text*, Text*, Text*, int, int, Rune*, int);
50 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
51 void paste(Text*, Text*, Text*, int, int, Rune*, int);
52 void put(Text*, Text*, Text*, int, int, Rune*, int);
53 void putall(Text*, Text*, Text*, int, int, Rune*, int);
54 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
55 void sort(Text*, Text*, Text*, int, int, Rune*, int);
56 void tab(Text*, Text*, Text*, int, int, Rune*, int);
57 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
59 typedef struct Exectab Exectab;
63 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
69 static Rune LAbort[] = { 'A', 'b', 'o', 'r', 't', 0 };
70 static Rune LCut[] = { 'C', 'u', 't', 0 };
71 static Rune LDel[] = { 'D', 'e', 'l', 0 };
72 static Rune LDelcol[] = { 'D', 'e', 'l', 'c', 'o', 'l', 0 };
73 static Rune LDelete[] = { 'D', 'e', 'l', 'e', 't', 'e', 0 };
74 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
75 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
76 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
77 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
78 static Rune LGet[] = { 'G', 'e', 't', 0 };
79 static Rune LID[] = { 'I', 'D', 0 };
80 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
81 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
82 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
83 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
84 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
85 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
86 static Rune LNew[] = { 'N', 'e', 'w', 0 };
87 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
88 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
89 static Rune LPut[] = { 'P', 'u', 't', 0 };
90 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
91 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
92 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
93 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
94 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
95 static Rune LTab[] = { 'T', 'a', 'b', 0 };
96 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
97 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
100 { LAbort, doabort, FALSE, XXX, XXX, },
101 { LCut, cut, TRUE, TRUE, TRUE },
102 { LDel, del, FALSE, FALSE, XXX },
103 { LDelcol, delcol, FALSE, XXX, XXX },
104 { LDelete, del, FALSE, TRUE, XXX },
105 { LDump, dump, FALSE, TRUE, XXX },
106 { LEdit, edit, FALSE, XXX, XXX },
107 { LExit, xexit, FALSE, XXX, XXX },
108 { LFont, fontx, FALSE, XXX, XXX },
109 { LGet, get, FALSE, TRUE, XXX },
110 { LID, id, FALSE, XXX, XXX },
111 { LIncl, incl, FALSE, XXX, XXX },
112 { LIndent, indent, FALSE, XXX, XXX },
113 { LKill, xkill, FALSE, XXX, XXX },
114 { LLoad, dump, FALSE, FALSE, XXX },
115 { LLocal, local, FALSE, XXX, XXX },
116 { LLook, look, FALSE, XXX, XXX },
117 { LNew, new, FALSE, XXX, XXX },
118 { LNewcol, newcol, FALSE, XXX, XXX },
119 { LPaste, paste, TRUE, TRUE, XXX },
120 { LPut, put, FALSE, XXX, XXX },
121 { LPutall, putall, FALSE, XXX, XXX },
122 { LRedo, undo, FALSE, FALSE, XXX },
123 { LSend, sendx, TRUE, XXX, XXX },
124 { LSnarf, cut, FALSE, TRUE, FALSE },
125 { LSort, sort, FALSE, XXX, XXX },
126 { LTab, tab, FALSE, XXX, XXX },
127 { LUndo, undo, FALSE, TRUE, XXX },
128 { LZerox, zeroxx, FALSE, XXX, XXX },
133 lookup(Rune *r, int n)
138 r = skipbl(r, n, &n);
143 for(e=exectab; e->name; e++)
144 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
154 return c=='<' || c=='|' || c=='>';
158 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
169 if(q1 == q0){ /* expand to find word (actually file name) */
170 /* if in selection, choose selection */
171 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
175 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
177 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
183 r = runemalloc(q1-q0);
184 bufread(&t->file->b, q0, r, q1-q0);
185 e = lookup(r, q1-q0);
186 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
190 if(q0!=aq0 || q1!=aq1){
191 bufread(&t->file->b, aq0, r, aq1-aq0);
194 aa = getbytearg(argt, TRUE, TRUE, &a);
196 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
200 warning(nil, "argument string too long\n");
210 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
212 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f);
213 if(q0!=aq0 || q1!=aq1){
215 bufread(&t->file->b, q0, r, n);
217 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
219 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1);
222 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
224 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
226 winevent(t->w, "%c0 0 0 0 \n", c);
234 if(e->mark && seltext!=nil)
235 if(seltext->what == Body){
237 filemark(seltext->w->body.file);
239 s = skipbl(r, q1-q0, &n);
240 s = findbl(s, n, &n);
241 s = skipbl(s, n, &n);
242 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
247 b = runetobyte(r, q1-q0);
249 dir = dirname(t, nil, 0);
250 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
255 aa = getbytearg(argt, TRUE, TRUE, &a);
258 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
262 printarg(Text *argt, uint q0, uint q1)
266 if(argt->what!=Body || argt->file->name==nil)
268 buf = emalloc(argt->file->nname+32);
270 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
272 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
277 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
288 textcommit(argt, TRUE);
289 if(expand(argt, argt->q0, argt->q1, &e)){
291 if(e.nname && dofile){
292 e.name = runerealloc(e.name, e.nname+1);
294 a = printarg(argt, e.q0, e.q1);
305 *rp = runemalloc(n+1);
306 bufread(&argt->file->b, e.q0, *rp, n);
308 a = printarg(argt, e.q0, e.q1);
314 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
321 aa = getarg(argt, doaddr, dofile, &r, &n);
324 *bp = runetobyte(r, n);
330 doabort(Text *__0, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
343 warning(nil, "executing Abort again will call abort()\n");
349 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
361 c = rowadd(et->row, nil, -1);
363 w = coladd(c, nil, nil, -1);
370 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
384 if(c==nil || colclean(c)==0)
386 for(i=0; i<c->nw; i++){
388 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
389 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
393 rowclose(et->col->row, et->col, TRUE);
397 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
405 if(et->col==nil || et->w == nil)
407 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
408 colclose(et->col, et->w, TRUE);
412 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
426 seqof(Window *w, int isundo)
428 /* if it's undo, see who changed with us */
430 return w->body.file->seq;
431 /* if it's redo, see who we'll be sync'ed up with */
432 return fileredoseq(w->body.file);
436 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
449 if(et==nil || et->w== nil)
451 seq = seqof(et->w, flag1);
453 /* nothing to undo */
457 * Undo the executing window first. Its display will update. other windows
458 * in the same file will not call show() and jump to a different location in the file.
459 * Simultaneous changes to other files will be chaotic, however.
461 winundo(et->w, flag1);
462 for(i=0; i<row.ncol; i++){
464 for(j=0; j<c->nw; j++){
468 if(seqof(w, flag1) == seq)
475 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
482 getarg(argt, FALSE, TRUE, &r, &n);
487 /* if are doing a Put, want to synthesize name even for non-existent file */
488 /* best guess is that file name doesn't contain a slash */
504 s = runetobyte(t->file->name, t->file->nname);
507 /* prefix with directory name if necessary */
510 if(n>0 && arg[0]!='/'){
511 dir = dirname(t, nil, 0);
512 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
519 r = runemalloc(dir.nr+n+1);
520 runemove(r, dir.r, dir.nr);
522 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
524 runemove(r+dir.nr, arg, n);
531 s = runetobyte(r, n);
541 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
553 if(t!=nil && t->w!=nil && t->w!=et->w){
562 if(t==nil || t->w==nil)
566 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
568 nw = coladd(t->w->col, nil, t->w, -1);
569 /* ugly: fix locks so w->unlock works */
570 winlock1(nw, t->w->owner);
571 xfidlog(nw, "zerox");
577 typedef struct TextAddr TextAddr;
579 long lorigin; // line+rune for origin
581 long lq0; // line+rune for q0
583 long lq1; // line+rune for q1
588 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
592 int i, n, dirty, samename, isdir;
602 if(et==nil || et->w==nil)
604 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
608 name = getname(t, argt, arg, narg, FALSE);
610 warning(nil, "no file name\n");
613 if(t->file->ntext>1){
615 isdir = (d!=nil && (d->qid.type & QTDIR));
618 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
622 addr = emalloc((t->file->ntext)*sizeof(TextAddr));
623 for(i=0; i<t->file->ntext; i++) {
625 u = t->file->text[i];
626 a->lorigin = nlcount(u, 0, u->org, &a->rorigin);
627 a->lq0 = nlcount(u, 0, u->q0, &a->rq0);
628 a->lq1 = nlcount(u, u->q0, u->q1, &a->rq1);
630 r = bytetorune(name, &n);
631 for(i=0; i<t->file->ntext; i++){
632 u = t->file->text[i];
633 /* second and subsequent calls with zero an already empty buffer, but OK */
637 samename = runeeq(r, n, t->file->name, t->file->nname);
638 textload(t, 0, name, samename);
640 t->file->mod = FALSE;
646 for(i=0; i<t->file->ntext; i++)
647 t->file->text[i]->w->dirty = dirty;
651 t->file->unread = FALSE;
652 for(i=0; i<t->file->ntext; i++){
653 u = t->file->text[i];
654 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
657 // warning(nil, "%d %d %d %d %d %d\n", a->lorigin, a->rorigin, a->lq0, a->rq0, a->lq1, a->rq1);
658 q0 = nlcounttopos(u, 0, a->lq0, a->rq0);
659 q1 = nlcounttopos(u, q0, a->lq1, a->rq1);
660 textsetselect(u, q0, q1);
661 q0 = nlcounttopos(u, 0, a->lorigin, a->rorigin);
662 textsetorigin(u, q0, FALSE);
671 checksha1(char *name, File *f, Dir *d)
678 fd = open(name, OREAD);
681 h = sha1(nil, 0, nil, nil);
683 while((n = read(fd, buf, 8192)) > 0)
684 sha1(buf, n, nil, h);
687 sha1(nil, 0, out, h);
688 if(memcmp(out, f->sha1, sizeof out) == 0) {
690 f->qidpath = d->qid.path;
696 putfile(File *f, int q0, int q1, Rune *namer, int nname)
702 int i, fd, q, ret, retc;
709 name = runetobyte(namer, nname);
711 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
712 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime != d->mtime)
713 checksha1(name, f, d);
714 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime != d->mtime) {
716 warning(nil, "%s not written; file already exists\n", name);
718 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);
720 f->qidpath = d->qid.path;
726 fd = create(name, OWRITE, 0666);
728 warning(nil, "can't create file %s: %r\n", name);
731 // Use bio in order to force the writes to be large and
732 // block-aligned (bio's default is 8K). This is not strictly
733 // necessary; it works around some buggy underlying
734 // file systems that mishandle unaligned writes.
735 // https://codereview.appspot.com/89550043/
736 b = emalloc(sizeof *b);
737 Binit(b, fd, OWRITE);
742 h = sha1(nil, 0, nil, nil);
743 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
745 warning(nil, "%s not written; file is append only\n", name);
749 for(q=q0; q<q1; q+=n){
751 if(n > BUFSIZE/UTFmax)
753 bufread(&f->b, q, r, n);
754 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
755 sha1((uchar*)s, m, nil, h);
756 if(Bwrite(b, s, m) != m){
757 warning(nil, "can't write file %s: %r\n", name);
762 warning(nil, "can't write file %s: %r\n", name);
769 if(ret < 0 || retc < 0) {
770 warning(nil, "can't write file %s: %r\n", name);
771 goto Rescue2; // flush or close failed
773 if(runeeq(namer, nname, f->name, f->nname)){
774 if(q0!=0 || q1!=f->b.nc){
779 // In case the file is on NFS, reopen the fd
780 // before dirfstat to cause the attribute cache
781 // to be updated (otherwise the mtime in the
782 // dirfstat below will be stale and not match
783 // what NFS sees). The file is already written,
784 // so this should be a no-op when not on NFS.
785 // Opening for OWRITE (but no truncation)
786 // in case we don't have read permission.
787 // (The create above worked, so we probably
788 // still have write permission.)
789 fd = open(name, OWRITE);
796 f->qidpath = d->qid.path;
799 sha1(nil, 0, f->sha1, h);
805 for(i=0; i<f->ntext; i++){
806 f->text[i]->w->putseq = f->seq;
807 f->text[i]->w->dirty = w->dirty;
843 uint q0, n, delstart;
850 if(t->w!=nil && et->w!=t->w){
851 /* can this happen when t == &et->w->body? */
860 delstart = q0; /* end of current space run, or 0 if no active run; = q0 to delete spaces before EOF */
866 bufread(&f->b, q0, r, n);
868 if(i == 0 || (r[i-1] != ' ' && r[i-1] != '\t')) {
869 // Found non-space or start of buffer. Delete active space run.
870 if(q0+i < delstart) {
876 textdelete(t, q0+i, delstart, TRUE);
879 /* keep run active into tail of next buffer */
886 delstart = q0+i-1; /* delete spaces before this newline */
892 if(t->w!=nil && et->w!=t->w)
897 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
909 if(et==nil || et->w==nil || et->w->isdir)
913 name = getname(&w->body, argt, arg, narg, TRUE);
915 warning(nil, "no file name\n");
920 namer = bytetorune(name, &nname);
921 putfile(f, 0, f->b.nc, namer, nname);
927 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
936 name = runetobyte(arg, narg);
938 getbytearg(argt, FALSE, TRUE, &name);
942 rowload(&row, name, FALSE);
947 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
949 uint q0, q1, n, locked, c;
957 * if not executing a mouse chord (et != t) and snarfing (dosnarf)
958 * and executed Cut or Snarf in window tag (et->w != nil),
959 * then use the window body selection or the tag selection
960 * or do nothing at all.
962 if(et!=t && dosnarf && et->w!=nil){
963 if(et->w->body.q1>et->w->body.q0){
966 filemark(t->file); /* seq has been incremented by execute */
967 }else if(et->w->tag.q1>et->w->tag.q0)
972 if(t == nil) /* no selection */
976 if(t->w!=nil && et->w!=t->w){
991 bufdelete(&snarfbuf, 0, snarfbuf.nc);
997 bufread(&t->file->b, q0, r, n);
998 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
1005 textdelete(t, t->q0, t->q1, TRUE);
1006 textsetselect(t, t->q0, t->q0);
1011 }else if(dosnarf) /* Snarf command */
1018 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
1028 /* if(tobody), use body of executing window (Paste or Send command) */
1029 if(tobody && et!=nil && et->w!=nil){
1031 filemark(t->file); /* seq has been incremented by execute */
1037 if(t==nil || snarfbuf.nc==0)
1039 if(t->w!=nil && et->w!=t->w){
1045 cut(t, t, nil, FALSE, TRUE, nil, 0);
1048 q1 = t->q0+snarfbuf.nc;
1056 bufread(&snarfbuf, q, r, n);
1057 textinsert(t, q0, r, n, TRUE);
1063 textsetselect(t, t->q0, q1);
1065 textsetselect(t, q1, q1);
1070 if(t->w!=nil && et->w!=t->w)
1075 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1086 search(t, arg, narg);
1089 getarg(argt, FALSE, FALSE, &r, &n);
1093 bufread(&t->file->b, t->q0, r, n);
1100 static Rune Lnl[] = { '\n', 0 };
1103 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
1115 cut(t, t, nil, TRUE, FALSE, nil, 0);
1116 textsetselect(t, t->file->b.nc, t->file->b.nc);
1117 paste(t, t, nil, TRUE, TRUE, nil, 0);
1118 if(textreadc(t, t->file->b.nc-1) != '\n'){
1119 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
1120 textsetselect(t, t->file->b.nc, t->file->b.nc);
1123 textshow(t, t->q1, t->q1, 1);
1127 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1138 getarg(argt, FALSE, TRUE, &r, &len);
1141 editcmd(et, r, len);
1144 editcmd(et, arg, narg);
1148 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1165 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1180 for(i=0; i<row.ncol; i++){
1182 for(j=0; j<c->nw; j++){
1184 if(w->isscratch || w->isdir || w->body.file->nname==0)
1186 if(w->nopen[QWevent] > 0)
1188 a = runetobyte(w->body.file->name, w->body.file->nname);
1190 if(w->body.file->mod || w->body.ncache)
1192 warning(nil, "no auto-Put of %s: %r\n", a);
1194 wincommit(w, &w->body);
1195 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1204 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1214 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1218 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1227 aa = getbytearg(argt, TRUE, TRUE, &a);
1229 dir = dirname(et, nil, 0);
1230 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1235 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1239 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1249 getarg(argt, FALSE, FALSE, &r, &na);
1251 xkill(nil, nil, nil, 0, 0, r, na);
1252 /* loop condition: *arg is not a blank */
1254 a = findbl(arg, narg, &na);
1257 cmd = runemalloc(narg-na+1);
1258 runemove(cmd, arg, narg-na);
1260 arg = skipbl(a, na, &narg);
1264 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1265 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1268 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1270 Rune *a, *r, *flag, *file;
1280 if(et==nil || et->w==nil)
1285 /* loop condition: *arg is not a blank */
1288 a = findbl(arg, narg, &na);
1291 r = runemalloc(narg-na+1);
1292 runemove(r, arg, narg-na);
1293 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1301 arg = skipbl(a, na, &narg);
1303 getarg(argt, FALSE, TRUE, &r, &na);
1305 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1315 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1316 else if(file == nil){
1317 newfont = rfget(FALSE, FALSE, FALSE, nil);
1319 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1322 aa = runetobyte(file, nf);
1323 newfont = rfget(fix, flag!=nil, FALSE, aa);
1326 newfont = rfget(fix, FALSE, FALSE, nil);
1328 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1329 rfclose(t->reffont);
1330 t->reffont = newfont;
1331 t->fr.font = newfont->f;
1334 t->all.min.x++; /* force recolumnation; disgusting! */
1335 for(i=0; i<t->w->ndl; i++){
1337 aa = runetobyte(dp->r, dp->nr);
1338 dp->wid = stringwidth(newfont->f, aa);
1342 /* avoid shrinking of window due to quantization */
1343 colgrow(t->w->col, t->w, -1);
1350 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1360 if(et==nil || et->w==nil)
1364 getarg(argt, FALSE, TRUE, &r, &len);
1367 winaddincl(w, r, len);
1369 /* loop condition: *arg is not a blank */
1371 a = findbl(arg, narg, &na);
1374 r = runemalloc(narg-na+1);
1375 runemove(r, arg, narg-na);
1377 winaddincl(w, r, narg-na);
1378 arg = skipbl(a, na, &narg);
1380 if(n==0 && w->nincl){
1381 for(n=w->nincl; --n>=0; )
1382 warning(nil, "%S ", w->incl[n]);
1387 static Rune LON[] = { 'O', 'N', 0 };
1388 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1389 static Rune Lon[] = { 'o', 'n', 0 };
1399 indentval(Rune *s, int n)
1403 if(runestrncmp(s, LON, n) == 0){
1404 globalautoindent = TRUE;
1405 warning(nil, "Indent ON\n");
1408 if(runestrncmp(s, LOFF, n) == 0){
1409 globalautoindent = FALSE;
1410 warning(nil, "Indent OFF\n");
1413 return runestrncmp(s, Lon, n) == 0;
1417 fixindent(Window *w, void *arg)
1420 w->autoindent = globalautoindent;
1424 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1428 int na, len, autoindent;
1435 if(et!=nil && et->w!=nil)
1437 autoindent = IError;
1438 getarg(argt, FALSE, TRUE, &r, &len);
1440 autoindent = indentval(r, len);
1442 a = findbl(arg, narg, &na);
1444 autoindent = indentval(arg, narg-na);
1446 if(autoindent == IGlobal)
1447 allwindows(fixindent, nil);
1448 else if(w != nil && autoindent >= 0)
1449 w->autoindent = autoindent;
1453 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1464 if(et==nil || et->w==nil)
1467 getarg(argt, FALSE, TRUE, &r, &len);
1469 if(r!=nil && len>0){
1470 p = runetobyte(r, len);
1471 if('0'<=p[0] && p[0]<='9')
1475 a = findbl(arg, narg, &na);
1477 p = runetobyte(arg, narg-na);
1478 if('0'<=p[0] && p[0]<='9')
1484 if(w->body.tabstop != tab){
1485 w->body.tabstop = tab;
1486 winresize(w, w->r, FALSE, TRUE);
1489 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1493 runproc(void *argvp)
1507 char *e, *t, *name, *filename, *dir, **av, *news;
1509 int ac, w, inarg, i, n, fd, nincl, winid;
1514 /*static void *parg[2]; */
1520 threadsetname("runproc");
1526 ndir = (uintptr)argv[3];
1527 newns = (uintptr)argv[4];
1532 iseditcmd = (uintptr)argv[9];
1536 while(*t==' ' || *t=='\n' || *t=='\t')
1539 if(*e==' ' || *e=='\n' || *e=='\t' )
1541 name = emalloc((e-t)+2);
1542 memmove(name, t, e-t);
1544 e = utfrrune(name, '/');
1546 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1547 strcat(name, " "); /* add blank here for ease in waittask */
1548 c->name = bytetorune(name, &c->nname);
1551 if(*t=='<' || *t=='|' || *t=='>')
1553 c->iseditcmd = iseditcmd;
1559 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1562 incl = emalloc(nincl*sizeof(Rune*));
1563 for(i=0; i<nincl; i++){
1564 n = runestrlen(win->incl[i]);
1565 incl[i] = runemalloc(n+1);
1566 runemove(incl[i], win->incl[i], n);
1574 winid = activewin->id;
1576 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1577 sprint(buf, "%d", winid);
1578 putenv("winid", buf);
1581 putenv("%", filename);
1582 putenv("samfile", filename);
1585 c->md = fsysmount(rdir, ndir, incl, nincl);
1587 fprint(2, "child: can't allocate mntdir: %r\n");
1588 threadexits("fsysmount");
1590 sprint(buf, "%d", c->md->id);
1591 if((fs = nsmount("acme", buf)) == nil){
1592 fprint(2, "child: can't mount acme: %r\n");
1595 threadexits("nsmount");
1597 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1598 sprint(buf, "%d/rdsel", winid);
1599 sfd[0] = fsopenfd(fs, buf, OREAD);
1601 sfd[0] = open("/dev/null", OREAD);
1602 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1605 sprint(buf, "%d/editout", winid);
1607 sprint(buf, "editout");
1609 sprint(buf, "%d/wrsel", winid);
1610 sfd[1] = fsopenfd(fs, buf, OWRITE);
1611 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1613 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1618 rfork(RFFDG|RFNOTEG);
1620 sfd[0] = open("/dev/null", OREAD);
1621 sfd[1] = open("/dev/null", OWRITE);
1622 sfd[2] = dup(erroutfd, -1);
1628 putenv("acmeaddr", argaddr);
1629 if(acmeshell != nil)
1631 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1635 w = chartorune(&r, e);
1636 if(r==' ' || r=='\t')
1640 if(utfrune("#;&|^$=`'{}()<>[]*?^~`/", r))
1651 w = chartorune(&r, e);
1652 if(r==' ' || r=='\t'){
1659 av = realloc(av, (ac+1)*sizeof(char**));
1663 av = realloc(av, (ac+2)*sizeof(char**));
1670 dir = runetobyte(rdir, ndir);
1671 ret = threadspawnd(sfd, av[0], av, dir);
1678 /* libthread uses execvp so no need to do this */
1681 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1684 sprint(buf, "%s/%s", cputype, av[0]);
1685 procexec(cpid, sfd, buf, av);
1687 sprint(buf, "/bin/%s", av[0]);
1688 procexec(cpid, sfd, buf, av);
1694 * ugly: set path = (. $cputype /bin)
1695 * should honor $path if unusual.
1699 memmove(buf+n, ".", 2);
1701 i = strlen(cputype)+1;
1702 memmove(buf+n, cputype, i);
1704 memmove(buf+n, "/bin", 5);
1706 fd = create("/env/path", OWRITE, 0666);
1712 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1714 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1722 dir = runetobyte(rdir, ndir);
1730 ret = threadspawnd(sfd, rcarg[0], rcarg, dir);
1737 warning(nil, "exec %s: %r\n", shell);
1740 /* threadexec hasn't happened, so send a zero */
1743 if(sfd[2] != sfd[1])
1750 runwaittask(void *v)
1756 threadsetname("runwaittask");
1762 c->pid = recvul(cpid);
1763 while(c->pid == ~0);
1765 if(c->pid != 0) /* successful exec */
1778 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1787 arg = emalloc(10*sizeof(void*));
1788 c = emalloc(sizeof *c);
1789 cpid = chancreate(sizeof(ulong), 0);
1790 chansetname(cpid, "cpid %s", s);
1794 arg[3] = (void*)(uintptr)ndir;
1795 arg[4] = (void*)(uintptr)newns;
1800 arg[9] = (void*)(uintptr)iseditcmd;
1801 threadcreate(runproc, arg, STACK);
1802 /* mustn't block here because must be ready to answer mount() call in run() */
1803 arg = emalloc(2*sizeof(void*));
1806 threadcreate(runwaittask, arg, STACK);