Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include <plumb.h>
11 #define Fid FsFid
12 #include <fs.h>
13 #undef Fid
14 #include "dat.h"
15 #include "fns.h"
17 Buffer snarfbuf;
19 void del(Text*, Text*, Text*, int, int, Rune*, int);
20 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
21 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
22 void dump(Text*, Text*, Text*, int, int, Rune*, int);
23 void edit(Text*, Text*, Text*, int, int, Rune*, int);
24 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
25 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
26 void get(Text*, Text*, Text*, int, int, Rune*, int);
27 void id(Text*, Text*, Text*, int, int, Rune*, int);
28 void incl(Text*, Text*, Text*, int, int, Rune*, int);
29 void indent(Text*, Text*, Text*, int, int, Rune*, int);
30 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
31 void local(Text*, Text*, Text*, int, int, Rune*, int);
32 void look(Text*, Text*, Text*, int, int, Rune*, int);
33 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
34 void paste(Text*, Text*, Text*, int, int, Rune*, int);
35 void put(Text*, Text*, Text*, int, int, Rune*, int);
36 void putall(Text*, Text*, Text*, int, int, Rune*, int);
37 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
38 void sort(Text*, Text*, Text*, int, int, Rune*, int);
39 void tab(Text*, Text*, Text*, int, int, Rune*, int);
40 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
42 typedef struct Exectab Exectab;
43 struct Exectab
44 {
45 Rune *name;
46 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
47 int mark;
48 int flag1;
49 int flag2;
50 };
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 LDotfiles[] = { 'D', 'o', 't', 'f', 'i', 'l', 'e', 's', 0 };
57 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
58 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
59 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
60 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
61 static Rune LGet[] = { 'G', 'e', 't', 0 };
62 static Rune LID[] = { 'I', 'D', 0 };
63 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
64 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
65 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
66 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
67 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
68 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
69 static Rune LNew[] = { 'N', 'e', 'w', 0 };
70 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
71 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
72 static Rune LPut[] = { 'P', 'u', 't', 0 };
73 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
74 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
75 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
76 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
77 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
78 static Rune LTab[] = { 'T', 'a', 'b', 0 };
79 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
80 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
82 Exectab exectab[] = {
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 { LDotfiles, dotfiles, FALSE, XXX, XXX },
88 { LDump, dump, FALSE, TRUE, XXX },
89 { LEdit, edit, FALSE, XXX, XXX },
90 { LExit, xexit, FALSE, XXX, XXX },
91 { LFont, fontx, FALSE, XXX, XXX },
92 { LGet, get, FALSE, TRUE, XXX },
93 { LID, id, FALSE, XXX, XXX },
94 { LIncl, incl, FALSE, XXX, XXX },
95 { LIndent, indent, FALSE, XXX, XXX },
96 { LKill, xkill, FALSE, XXX, XXX },
97 { LLoad, dump, FALSE, FALSE, XXX },
98 { LLocal, local, FALSE, XXX, XXX },
99 { LLook, look, FALSE, XXX, XXX },
100 { LNew, new, FALSE, XXX, XXX },
101 { LNewcol, newcol, FALSE, XXX, XXX },
102 { LPaste, paste, TRUE, TRUE, XXX },
103 { LPut, put, FALSE, XXX, XXX },
104 { LPutall, putall, FALSE, XXX, XXX },
105 { LRedo, undo, FALSE, FALSE, XXX },
106 { LSend, sendx, TRUE, XXX, XXX },
107 { LSnarf, cut, FALSE, TRUE, FALSE },
108 { LSort, sort, FALSE, XXX, XXX },
109 { LTab, tab, FALSE, XXX, XXX },
110 { LUndo, undo, FALSE, TRUE, XXX },
111 { LZerox, zeroxx, FALSE, XXX, XXX },
112 { nil, 0, 0, 0, 0 },
113 };
115 Exectab*
116 lookup(Rune *r, int n)
118 Exectab *e;
119 int nr;
121 r = skipbl(r, n, &n);
122 if(n == 0)
123 return nil;
124 findbl(r, n, &nr);
125 nr = n-nr;
126 for(e=exectab; e->name; e++)
127 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
128 return e;
129 return nil;
132 int
133 isexecc(int c)
135 if(isfilec(c))
136 return 1;
137 return c=='<' || c=='|' || c=='>';
140 void
141 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
143 uint q0, q1;
144 Rune *r, *s;
145 char *b, *a, *aa;
146 Exectab *e;
147 int c, n, f;
148 Runestr dir;
150 q0 = aq0;
151 q1 = aq1;
152 if(q1 == q0){ /* expand to find word (actually file name) */
153 /* if in selection, choose selection */
154 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
155 q0 = t->q0;
156 q1 = t->q1;
157 }else{
158 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
159 q1++;
160 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
161 q0--;
162 if(q1 == q0)
163 return;
166 r = runemalloc(q1-q0);
167 bufread(&t->file->b, q0, r, q1-q0);
168 e = lookup(r, q1-q0);
169 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
170 f = 0;
171 if(e)
172 f |= 1;
173 if(q0!=aq0 || q1!=aq1){
174 bufread(&t->file->b, aq0, r, aq1-aq0);
175 f |= 2;
177 aa = getbytearg(argt, TRUE, TRUE, &a);
178 if(a){
179 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
180 free(aa);
181 free(a);
182 warning(nil, "`argument string too long\n");
183 return;
185 f |= 8;
187 c = 'x';
188 if(t->what == Body)
189 c = 'X';
190 n = aq1-aq0;
191 if(n <= EVENTSIZE)
192 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
193 else
194 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
195 if(q0!=aq0 || q1!=aq1){
196 n = q1-q0;
197 bufread(&t->file->b, q0, r, n);
198 if(n <= EVENTSIZE)
199 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
200 else
201 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
203 if(a){
204 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
205 if(aa)
206 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
207 else
208 winevent(t->w, "%c0 0 0 0 \n", c);
210 free(r);
211 free(aa);
212 free(a);
213 return;
215 if(e){
216 if(e->mark && seltext!=nil)
217 if(seltext->what == Body){
218 seq++;
219 filemark(seltext->w->body.file);
221 s = skipbl(r, q1-q0, &n);
222 s = findbl(s, n, &n);
223 s = skipbl(s, n, &n);
224 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
225 free(r);
226 return;
229 b = runetobyte(r, q1-q0);
230 free(r);
231 dir = dirname(t, nil, 0);
232 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
233 free(dir.r);
234 dir.r = nil;
235 dir.nr = 0;
237 aa = getbytearg(argt, TRUE, TRUE, &a);
238 if(t->w)
239 incref(&t->w->ref);
240 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
243 char*
244 printarg(Text *argt, uint q0, uint q1)
246 char *buf;
248 if(argt->what!=Body || argt->file->name==nil)
249 return nil;
250 buf = emalloc(argt->file->nname+32);
251 if(q0 == q1)
252 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
253 else
254 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
255 return buf;
258 char*
259 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
261 int n;
262 Expand e;
263 char *a;
265 *rp = nil;
266 *nrp = 0;
267 if(argt == nil)
268 return nil;
269 a = nil;
270 textcommit(argt, TRUE);
271 if(expand(argt, argt->q0, argt->q1, &e)){
272 free(e.bname);
273 if(e.nname && dofile){
274 e.name = runerealloc(e.name, e.nname+1);
275 if(doaddr)
276 a = printarg(argt, e.q0, e.q1);
277 *rp = e.name;
278 *nrp = e.nname;
279 return a;
281 free(e.name);
282 }else{
283 e.q0 = argt->q0;
284 e.q1 = argt->q1;
286 n = e.q1 - e.q0;
287 *rp = runemalloc(n+1);
288 bufread(&argt->file->b, e.q0, *rp, n);
289 if(doaddr)
290 a = printarg(argt, e.q0, e.q1);
291 *nrp = n;
292 return a;
295 char*
296 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
298 Rune *r;
299 int n;
300 char *aa;
302 *bp = nil;
303 aa = getarg(argt, doaddr, dofile, &r, &n);
304 if(r == nil)
305 return nil;
306 *bp = runetobyte(r, n);
307 free(r);
308 return aa;
311 void
312 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
314 Column *c;
316 USED(_0);
317 USED(_1);
318 USED(_2);
319 USED(_3);
320 USED(_4);
321 USED(_5);
323 c = rowadd(et->row, nil, -1);
324 if(c)
325 winsettag(coladd(c, nil, nil, -1));
328 void
329 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
331 int i;
332 Column *c;
333 Window *w;
335 USED(_0);
336 USED(_1);
337 USED(_2);
338 USED(_3);
339 USED(_4);
340 USED(_5);
342 c = et->col;
343 if(c==nil || colclean(c)==0)
344 return;
345 for(i=0; i<c->nw; i++){
346 w = c->w[i];
347 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata] > 0){
348 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
349 return;
352 rowclose(et->col->row, et->col, TRUE);
355 void
356 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
358 USED(_0);
359 USED(_1);
360 USED(_2);
361 USED(_3);
362 USED(_4);
364 if(et->col==nil || et->w == nil)
365 return;
366 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
367 colclose(et->col, et->w, TRUE);
370 void
371 dotfiles(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
373 USED(_0);
374 USED(_1);
375 USED(_2);
376 USED(_3);
377 USED(_4);
378 USED(_5);
379 USED(et);
381 nodotfiles = !nodotfiles;
382 warning(nil, "%s dot files\n", nodotfiles ? "omitting" : "showing");
385 void
386 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
388 USED(_0);
389 USED(_1);
390 USED(_2);
391 USED(_3);
392 USED(_4);
393 USED(_5);
395 if(et->col)
396 colsort(et->col);
399 uint
400 seqof(Window *w, int isundo)
402 /* if it's undo, see who changed with us */
403 if(isundo)
404 return w->body.file->seq;
405 /* if it's redo, see who we'll be sync'ed up with */
406 return fileredoseq(w->body.file);
409 void
410 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
412 int i, j;
413 Column *c;
414 Window *w;
415 uint seq;
417 USED(_0);
418 USED(_1);
419 USED(_2);
420 USED(_3);
421 USED(_4);
423 if(et==nil || et->w== nil)
424 return;
425 seq = seqof(et->w, flag1);
426 if(seq == 0){
427 /* nothing to undo */
428 return;
430 /*
431 * Undo the executing window first. Its display will update. other windows
432 * in the same file will not call show() and jump to a different location in the file.
433 * Simultaneous changes to other files will be chaotic, however.
434 */
435 winundo(et->w, flag1);
436 for(i=0; i<row.ncol; i++){
437 c = row.col[i];
438 for(j=0; j<c->nw; j++){
439 w = c->w[j];
440 if(w == et->w)
441 continue;
442 if(seqof(w, flag1) == seq)
443 winundo(w, flag1);
448 char*
449 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
451 char *s;
452 Rune *r;
453 int i, n, promote;
454 Runestr dir;
456 getarg(argt, FALSE, TRUE, &r, &n);
457 promote = FALSE;
458 if(r == nil)
459 promote = TRUE;
460 else if(isput){
461 /* if are doing a Put, want to synthesize name even for non-existent file */
462 /* best guess is that file name doesn't contain a slash */
463 promote = TRUE;
464 for(i=0; i<n; i++)
465 if(r[i] == '/'){
466 promote = FALSE;
467 break;
469 if(promote){
470 t = argt;
471 arg = r;
472 narg = n;
475 if(promote){
476 n = narg;
477 if(n <= 0){
478 s = runetobyte(t->file->name, t->file->nname);
479 return s;
481 /* prefix with directory name if necessary */
482 dir.r = nil;
483 dir.nr = 0;
484 if(n>0 && arg[0]!='/'){
485 dir = dirname(t, nil, 0);
486 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
487 free(dir.r);
488 dir.r = nil;
489 dir.nr = 0;
492 if(dir.r){
493 r = runemalloc(dir.nr+n+1);
494 runemove(r, dir.r, dir.nr);
495 free(dir.r);
496 runemove(r+dir.nr, arg, n);
497 n += dir.nr;
498 }else{
499 r = runemalloc(n+1);
500 runemove(r, arg, n);
503 s = runetobyte(r, n);
504 free(r);
505 if(strlen(s) == 0){
506 free(s);
507 s = nil;
509 return s;
512 void
513 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
515 Window *nw;
516 int c, locked;
518 USED(_1);
519 USED(_2);
520 USED(_3);
521 USED(_4);
522 USED(_5);
524 locked = FALSE;
525 if(t!=nil && t->w!=nil && t->w!=et->w){
526 locked = TRUE;
527 c = 'M';
528 if(et->w)
529 c = et->w->owner;
530 winlock(t->w, c);
532 if(t == nil)
533 t = et;
534 if(t==nil || t->w==nil)
535 return;
536 t = &t->w->body;
537 if(t->w->isdir)
538 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
539 else{
540 nw = coladd(t->w->col, nil, t->w, -1);
541 /* ugly: fix locks so w->unlock works */
542 winlock1(nw, t->w->owner);
544 if(locked)
545 winunlock(t->w);
548 void
549 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
551 char *name;
552 Rune *r;
553 int i, n, dirty, samename, isdir;
554 Window *w;
555 Text *u;
556 Dir *d;
558 USED(_0);
560 if(flag1)
561 if(et==nil || et->w==nil)
562 return;
563 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
564 return;
565 w = et->w;
566 t = &w->body;
567 name = getname(t, argt, arg, narg, FALSE);
568 if(name == nil){
569 warning(nil, "no file name\n");
570 return;
572 if(t->file->ntext>1){
573 d = dirstat(name);
574 isdir = (d!=nil && (d->qid.type & QTDIR));
575 free(d);
576 if(isdir)
577 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
578 return;
580 r = bytetorune(name, &n);
581 for(i=0; i<t->file->ntext; i++){
582 u = t->file->text[i];
583 /* second and subsequent calls with zero an already empty buffer, but OK */
584 textreset(u);
585 windirfree(u->w);
587 samename = runeeq(r, n, t->file->name, t->file->nname);
588 textload(t, 0, name, samename);
589 if(samename){
590 t->file->mod = FALSE;
591 dirty = FALSE;
592 }else{
593 t->file->mod = TRUE;
594 dirty = TRUE;
596 for(i=0; i<t->file->ntext; i++)
597 t->file->text[i]->w->dirty = dirty;
598 free(name);
599 free(r);
600 winsettag(w);
601 t->file->unread = FALSE;
602 for(i=0; i<t->file->ntext; i++){
603 u = t->file->text[i];
604 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
605 textscrdraw(u);
609 void
610 putfile(File *f, int q0, int q1, Rune *namer, int nname)
612 uint n, m;
613 Rune *r;
614 char *s, *name;
615 int i, fd, q;
616 Dir *d, *d1;
617 Window *w;
618 int isapp;
620 w = f->curtext->w;
621 name = runetobyte(namer, nname);
622 d = dirstat(name);
623 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
624 /* f->mtime+1 because when talking over NFS it's often off by a second */
625 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime+1<d->mtime){
626 f->dev = d->dev;
627 f->qidpath = d->qid.path;
628 f->mtime = d->mtime;
629 if(f->unread)
630 warning(nil, "%s not written; file already exists\n", name);
631 else
632 warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
633 goto Rescue1;
636 fd = create(name, OWRITE, 0666);
637 if(fd < 0){
638 warning(nil, "can't create file %s: %r\n", name);
639 goto Rescue1;
641 r = fbufalloc();
642 s = fbufalloc();
643 free(d);
644 d = dirfstat(fd);
645 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
646 if(isapp){
647 warning(nil, "%s not written; file is append only\n", name);
648 goto Rescue2;
651 for(q=q0; q<q1; q+=n){
652 n = q1 - q;
653 if(n > BUFSIZE/UTFmax)
654 n = BUFSIZE/UTFmax;
655 bufread(&f->b, q, r, n);
656 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
657 if(write(fd, s, m) != m){
658 warning(nil, "can't write file %s: %r\n", name);
659 goto Rescue2;
662 if(runeeq(namer, nname, f->name, f->nname)){
663 if(q0!=0 || q1!=f->b.nc){
664 f->mod = TRUE;
665 w->dirty = TRUE;
666 f->unread = TRUE;
667 }else{
668 d1 = dirfstat(fd);
669 if(d1 != nil){
670 free(d);
671 d = d1;
673 f->qidpath = d->qid.path;
674 f->dev = d->dev;
675 f->mtime = d->mtime;
676 f->mod = FALSE;
677 w->dirty = FALSE;
678 f->unread = FALSE;
680 for(i=0; i<f->ntext; i++){
681 f->text[i]->w->putseq = f->seq;
682 f->text[i]->w->dirty = w->dirty;
685 fbuffree(s);
686 fbuffree(r);
687 free(d);
688 free(namer);
689 free(name);
690 close(fd);
691 winsettag(w);
692 return;
694 Rescue2:
695 fbuffree(s);
696 fbuffree(r);
697 close(fd);
698 /* fall through */
700 Rescue1:
701 free(d);
702 free(namer);
703 free(name);
706 void
707 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
709 int nname;
710 Rune *namer;
711 Window *w;
712 File *f;
713 char *name;
715 USED(_0);
716 USED(_1);
717 USED(_2);
719 if(et==nil || et->w==nil || et->w->isdir)
720 return;
721 w = et->w;
722 f = w->body.file;
723 name = getname(&w->body, argt, arg, narg, TRUE);
724 if(name == nil){
725 warning(nil, "no file name\n");
726 return;
728 namer = bytetorune(name, &nname);
729 putfile(f, 0, f->b.nc, namer, nname);
730 free(name);
733 void
734 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
736 char *name;
738 USED(_0);
739 USED(_1);
740 USED(_2);
742 if(narg)
743 name = runetobyte(arg, narg);
744 else
745 getbytearg(argt, FALSE, TRUE, &name);
746 if(isdump)
747 rowdump(&row, name);
748 else
749 rowload(&row, name, FALSE);
750 free(name);
753 void
754 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
756 uint q0, q1, n, locked, c;
757 Rune *r;
759 USED(_0);
760 USED(_2);
761 USED(_3);
763 /* use current window if snarfing and its selection is non-null */
764 if(et!=t && dosnarf && et->w!=nil){
765 if(et->w->body.q1>et->w->body.q0){
766 t = &et->w->body;
767 if(docut)
768 filemark(t->file); /* seq has been incremented by execute */
769 }else if(et->w->tag.q1>et->w->tag.q0)
770 t = &et->w->tag;
772 if(t == nil){
773 /* can only happen if seltext == nil */
774 return;
776 locked = FALSE;
777 if(t->w!=nil && et->w!=t->w){
778 locked = TRUE;
779 c = 'M';
780 if(et->w)
781 c = et->w->owner;
782 winlock(t->w, c);
784 if(t->q0 == t->q1){
785 if(locked)
786 winunlock(t->w);
787 return;
789 if(dosnarf){
790 q0 = t->q0;
791 q1 = t->q1;
792 bufdelete(&snarfbuf, 0, snarfbuf.nc);
793 r = fbufalloc();
794 while(q0 < q1){
795 n = q1 - q0;
796 if(n > RBUFSIZE)
797 n = RBUFSIZE;
798 bufread(&t->file->b, q0, r, n);
799 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
800 q0 += n;
802 fbuffree(r);
803 acmeputsnarf();
805 if(docut){
806 textdelete(t, t->q0, t->q1, TRUE);
807 textsetselect(t, t->q0, t->q0);
808 if(t->w){
809 textscrdraw(t);
810 winsettag(t->w);
812 }else if(dosnarf) /* Snarf command */
813 argtext = t;
814 if(locked)
815 winunlock(t->w);
818 void
819 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
821 int c;
822 uint q, q0, q1, n;
823 Rune *r;
825 USED(_0);
826 USED(_1);
827 USED(_2);
829 /* if(tobody), use body of executing window (Paste or Send command) */
830 if(tobody && et!=nil && et->w!=nil){
831 t = &et->w->body;
832 filemark(t->file); /* seq has been incremented by execute */
834 if(t == nil)
835 return;
837 acmegetsnarf();
838 if(t==nil || snarfbuf.nc==0)
839 return;
840 if(t->w!=nil && et->w!=t->w){
841 c = 'M';
842 if(et->w)
843 c = et->w->owner;
844 winlock(t->w, c);
846 cut(t, t, nil, FALSE, TRUE, nil, 0);
847 q = 0;
848 q0 = t->q0;
849 q1 = t->q0+snarfbuf.nc;
850 r = fbufalloc();
851 while(q0 < q1){
852 n = q1 - q0;
853 if(n > RBUFSIZE)
854 n = RBUFSIZE;
855 if(r == nil)
856 r = runemalloc(n);
857 bufread(&snarfbuf, q, r, n);
858 textinsert(t, q0, r, n, TRUE);
859 q += n;
860 q0 += n;
862 fbuffree(r);
863 if(selectall)
864 textsetselect(t, t->q0, q1);
865 else
866 textsetselect(t, q1, q1);
867 if(t->w){
868 textscrdraw(t);
869 winsettag(t->w);
871 if(t->w!=nil && et->w!=t->w)
872 winunlock(t->w);
875 void
876 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
878 Rune *r;
879 int n;
881 USED(_0);
882 USED(_1);
884 if(et && et->w){
885 t = &et->w->body;
886 if(narg > 0){
887 search(t, arg, narg);
888 return;
890 getarg(argt, FALSE, FALSE, &r, &n);
891 if(r == nil){
892 n = t->q1-t->q0;
893 r = runemalloc(n);
894 bufread(&t->file->b, t->q0, r, n);
896 search(t, r, n);
897 free(r);
901 static Rune Lnl[] = { '\n', 0 };
903 void
904 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
906 USED(_0);
907 USED(_1);
908 USED(_2);
909 USED(_3);
910 USED(_4);
912 if(et->w==nil)
913 return;
914 t = &et->w->body;
915 if(t->q0 != t->q1)
916 cut(t, t, nil, TRUE, FALSE, nil, 0);
917 textsetselect(t, t->file->b.nc, t->file->b.nc);
918 paste(t, t, nil, TRUE, TRUE, nil, 0);
919 if(textreadc(t, t->file->b.nc-1) != '\n'){
920 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
921 textsetselect(t, t->file->b.nc, t->file->b.nc);
925 void
926 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
928 Rune *r;
929 int len;
931 USED(_0);
932 USED(_1);
933 USED(_2);
935 if(et == nil)
936 return;
937 getarg(argt, FALSE, TRUE, &r, &len);
938 seq++;
939 if(r != nil){
940 editcmd(et, r, len);
941 free(r);
942 }else
943 editcmd(et, arg, narg);
946 void
947 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
949 USED(et);
950 USED(_0);
951 USED(_1);
952 USED(_2);
953 USED(_3);
954 USED(_4);
955 USED(_5);
957 if(rowclean(&row)){
958 sendul(cexit, 0);
959 threadexits(nil);
963 void
964 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
966 int i, j, e;
967 Window *w;
968 Column *c;
969 char *a;
971 USED(et);
972 USED(_0);
973 USED(_1);
974 USED(_2);
975 USED(_3);
976 USED(_4);
977 USED(_5);
979 for(i=0; i<row.ncol; i++){
980 c = row.col[i];
981 for(j=0; j<c->nw; j++){
982 w = c->w[j];
983 if(w->isscratch || w->isdir || w->body.file->nname==0)
984 continue;
985 if(w->nopen[QWevent] > 0)
986 continue;
987 a = runetobyte(w->body.file->name, w->body.file->nname);
988 e = access(a, 0);
989 if(w->body.file->mod || w->body.ncache)
990 if(e < 0)
991 warning(nil, "no auto-Put of %s: %r\n", a);
992 else{
993 wincommit(w, &w->body);
994 put(&w->body, nil, nil, XXX, XXX, nil, 0);
996 free(a);
1002 void
1003 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1005 USED(et);
1006 USED(_0);
1007 USED(_1);
1008 USED(_2);
1009 USED(_3);
1010 USED(_4);
1011 USED(_5);
1013 if(et && et->w)
1014 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1017 void
1018 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1020 char *a, *aa;
1021 Runestr dir;
1023 USED(_0);
1024 USED(_1);
1025 USED(_2);
1027 aa = getbytearg(argt, TRUE, TRUE, &a);
1029 dir = dirname(et, nil, 0);
1030 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1031 free(dir.r);
1032 dir.r = nil;
1033 dir.nr = 0;
1035 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1038 void
1039 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1041 Rune *a, *cmd, *r;
1042 int na;
1044 USED(_0);
1045 USED(_1);
1046 USED(_2);
1047 USED(_3);
1049 getarg(argt, FALSE, FALSE, &r, &na);
1050 if(r)
1051 xkill(nil, nil, nil, 0, 0, r, na);
1052 /* loop condition: *arg is not a blank */
1053 for(;;){
1054 a = findbl(arg, narg, &na);
1055 if(a == arg)
1056 break;
1057 cmd = runemalloc(narg-na+1);
1058 runemove(cmd, arg, narg-na);
1059 sendp(ckill, cmd);
1060 arg = skipbl(a, na, &narg);
1064 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1065 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1067 void
1068 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1070 Rune *a, *r, *flag, *file;
1071 int na, nf;
1072 char *aa;
1073 Reffont *newfont;
1074 Dirlist *dp;
1075 int i, fix;
1077 USED(_0);
1078 USED(_1);
1080 if(et==nil || et->w==nil)
1081 return;
1082 t = &et->w->body;
1083 flag = nil;
1084 file = nil;
1085 /* loop condition: *arg is not a blank */
1086 nf = 0;
1087 for(;;){
1088 a = findbl(arg, narg, &na);
1089 if(a == arg)
1090 break;
1091 r = runemalloc(narg-na+1);
1092 runemove(r, arg, narg-na);
1093 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1094 free(flag);
1095 flag = r;
1096 }else{
1097 free(file);
1098 file = r;
1099 nf = narg-na;
1101 arg = skipbl(a, na, &narg);
1103 getarg(argt, FALSE, TRUE, &r, &na);
1104 if(r)
1105 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1106 free(flag);
1107 flag = r;
1108 }else{
1109 free(file);
1110 file = r;
1111 nf = na;
1113 fix = 1;
1114 if(flag)
1115 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1116 else if(file == nil){
1117 newfont = rfget(FALSE, FALSE, FALSE, nil);
1118 if(newfont)
1119 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1121 if(file){
1122 aa = runetobyte(file, nf);
1123 newfont = rfget(fix, flag!=nil, FALSE, aa);
1124 free(aa);
1125 }else
1126 newfont = rfget(fix, FALSE, FALSE, nil);
1127 if(newfont){
1128 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1129 rfclose(t->reffont);
1130 t->reffont = newfont;
1131 t->fr.font = newfont->f;
1132 frinittick(&t->fr);
1133 if(t->w->isdir){
1134 t->all.min.x++; /* force recolumnation; disgusting! */
1135 for(i=0; i<t->w->ndl; i++){
1136 dp = t->w->dlp[i];
1137 aa = runetobyte(dp->r, dp->nr);
1138 dp->wid = stringwidth(newfont->f, aa);
1139 free(aa);
1142 /* avoid shrinking of window due to quantization */
1143 colgrow(t->w->col, t->w, -1);
1145 free(file);
1146 free(flag);
1149 void
1150 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1152 Rune *a, *r;
1153 Window *w;
1154 int na, n, len;
1156 USED(_0);
1157 USED(_1);
1158 USED(_2);
1160 if(et==nil || et->w==nil)
1161 return;
1162 w = et->w;
1163 n = 0;
1164 getarg(argt, FALSE, TRUE, &r, &len);
1165 if(r){
1166 n++;
1167 winaddincl(w, r, len);
1169 /* loop condition: *arg is not a blank */
1170 for(;;){
1171 a = findbl(arg, narg, &na);
1172 if(a == arg)
1173 break;
1174 r = runemalloc(narg-na+1);
1175 runemove(r, arg, narg-na);
1176 n++;
1177 winaddincl(w, r, narg-na);
1178 arg = skipbl(a, na, &narg);
1180 if(n==0 && w->nincl){
1181 for(n=w->nincl; --n>=0; )
1182 warning(nil, "%S ", w->incl[n]);
1183 warning(nil, "\n");
1187 static Rune LON[] = { 'O', 'N', 0 };
1188 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1189 static Rune Lon[] = { 'o', 'n', 0 };
1191 static int
1192 indentval(Rune *s, int n)
1194 if(n < 2)
1195 return -1;
1196 if(runestrncmp(s, LON, n) == 0){
1197 globalautoindent = TRUE;
1198 warning(nil, "Indent ON\n");
1199 return -2;
1201 if(runestrncmp(s, LOFF, n) == 0){
1202 globalautoindent = FALSE;
1203 warning(nil, "Indent OFF\n");
1204 return -2;
1206 return runestrncmp(s, Lon, n) == 0;
1209 void
1210 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1212 Rune *a, *r;
1213 Window *w;
1214 int na, len, autoindent;
1216 USED(_0);
1217 USED(_1);
1218 USED(_2);
1220 if(et==nil || et->w==nil)
1221 return;
1222 w = et->w;
1223 autoindent = -1;
1224 getarg(argt, FALSE, TRUE, &r, &len);
1225 if(r!=nil && len>0)
1226 autoindent = indentval(r, len);
1227 else{
1228 a = findbl(arg, narg, &na);
1229 if(a != arg)
1230 autoindent = indentval(arg, narg-na);
1232 if(autoindent >= 0)
1233 w->autoindent = autoindent;
1234 if(autoindent != 2)
1235 warning(nil, "%.*S: Indent %s\n", w->body.file->nname, w->body.file->name,
1236 w->autoindent ? "on" : "off");
1239 void
1240 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1242 Rune *a, *r;
1243 Window *w;
1244 int na, len, tab;
1245 char *p;
1247 USED(_0);
1248 USED(_1);
1249 USED(_2);
1251 if(et==nil || et->w==nil)
1252 return;
1253 w = et->w;
1254 getarg(argt, FALSE, TRUE, &r, &len);
1255 tab = 0;
1256 if(r!=nil && len>0){
1257 p = runetobyte(r, len);
1258 if('0'<=p[0] && p[0]<='9')
1259 tab = atoi(p);
1260 free(p);
1261 }else{
1262 a = findbl(arg, narg, &na);
1263 if(a != arg){
1264 p = runetobyte(arg, narg-na);
1265 if('0'<=p[0] && p[0]<='9')
1266 tab = atoi(p);
1267 free(p);
1270 if(tab > 0){
1271 if(w->body.tabstop != tab){
1272 w->body.tabstop = tab;
1273 winresize(w, w->r, 1);
1275 }else
1276 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1279 void
1280 runproc(void *argvp)
1282 /* args: */
1283 Window *win;
1284 char *s;
1285 Rune *rdir;
1286 int ndir;
1287 int newns;
1288 char *argaddr;
1289 char *arg;
1290 Command *c;
1291 Channel *cpid;
1292 int iseditcmd;
1293 /* end of args */
1294 char *e, *t, *name, *filename, *dir, **av, *news;
1295 Rune r, **incl;
1296 int ac, w, inarg, i, n, fd, nincl, winid;
1297 int sfd[3];
1298 int pipechar;
1299 char buf[512];
1300 //static void *parg[2];
1301 void **argv;
1302 Fsys *fs;
1304 argv = argvp;
1305 win = argv[0];
1306 s = argv[1];
1307 rdir = argv[2];
1308 ndir = (int)argv[3];
1309 newns = (int)argv[4];
1310 argaddr = argv[5];
1311 arg = argv[6];
1312 c = argv[7];
1313 cpid = argv[8];
1314 iseditcmd = (int)argv[9];
1315 free(argv);
1317 t = s;
1318 while(*t==' ' || *t=='\n' || *t=='\t')
1319 t++;
1320 for(e=t; *e; e++)
1321 if(*e==' ' || *e=='\n' || *e=='\t' )
1322 break;
1323 name = emalloc((e-t)+2);
1324 memmove(name, t, e-t);
1325 name[e-t] = 0;
1326 e = utfrrune(name, '/');
1327 if(e)
1328 strcpy(name, e+1);
1329 strcat(name, " "); /* add blank here for ease in waittask */
1330 c->name = bytetorune(name, &c->nname);
1331 free(name);
1332 pipechar = 0;
1333 if(*t=='<' || *t=='|' || *t=='>')
1334 pipechar = *t++;
1335 c->iseditcmd = iseditcmd;
1336 c->text = s;
1337 if(rdir != nil){
1338 dir = runetobyte(rdir, ndir);
1339 chdir(dir); /* ignore error: probably app. window */
1340 free(dir);
1342 if(newns){
1343 nincl = 0;
1344 incl = nil;
1345 if(win){
1346 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1347 nincl = win->nincl;
1348 if(nincl > 0){
1349 incl = emalloc(nincl*sizeof(Rune*));
1350 for(i=0; i<nincl; i++){
1351 n = runestrlen(win->incl[i]);
1352 incl[i] = runemalloc(n+1);
1353 runemove(incl[i], win->incl[i], n);
1356 winid = win->id;
1357 }else{
1358 filename = nil;
1359 winid = 0;
1360 if(activewin)
1361 winid = activewin->id;
1363 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1364 sprint(buf, "%d", winid);
1365 putenv("winid", buf);
1367 if(filename){
1368 putenv("%", filename);
1369 free(filename);
1371 c->md = fsysmount(rdir, ndir, incl, nincl);
1372 if(c->md == nil){
1373 fprint(2, "child: can't allocate mntdir: %r\n");
1374 threadexits("fsysmount");
1376 sprint(buf, "%d", c->md->id);
1377 if((fs = nsmount("acme", buf)) == nil){
1378 fprint(2, "child: can't mount acme: %r\n");
1379 fsysdelid(c->md);
1380 c->md = nil;
1381 threadexits("nsmount");
1383 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1384 sprint(buf, "%d/rdsel", winid);
1385 sfd[0] = fsopenfd(fs, buf, OREAD);
1386 }else
1387 sfd[0] = open("/dev/null", OREAD);
1388 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1389 if(iseditcmd){
1390 if(winid > 0)
1391 sprint(buf, "%d/editout", winid);
1392 else
1393 sprint(buf, "editout");
1394 }else
1395 sprint(buf, "%d/wrsel", winid);
1396 sfd[1] = fsopenfd(fs, buf, OWRITE);
1397 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1398 }else{
1399 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1400 sfd[2] = sfd[1];
1402 fsunmount(fs);
1403 }else{
1404 rfork(RFFDG|RFNOTEG);
1405 fsysclose();
1406 sfd[0] = open("/dev/null", OREAD);
1407 sfd[1] = open("/dev/null", OWRITE);
1408 sfd[2] = dup(erroutfd, -1);
1410 if(win)
1411 winclose(win);
1413 if(argaddr)
1414 putenv("acmeaddr", argaddr);
1415 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1416 goto Hard;
1417 inarg = FALSE;
1418 for(e=t; *e; e+=w){
1419 w = chartorune(&r, e);
1420 if(r==' ' || r=='\t')
1421 continue;
1422 if(r < ' ')
1423 goto Hard;
1424 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1425 goto Hard;
1426 inarg = TRUE;
1428 if(!inarg)
1429 goto Fail;
1431 ac = 0;
1432 av = nil;
1433 inarg = FALSE;
1434 for(e=t; *e; e+=w){
1435 w = chartorune(&r, e);
1436 if(r==' ' || r=='\t'){
1437 inarg = FALSE;
1438 *e = 0;
1439 continue;
1441 if(!inarg){
1442 inarg = TRUE;
1443 av = realloc(av, (ac+1)*sizeof(char**));
1444 av[ac++] = e;
1447 av = realloc(av, (ac+2)*sizeof(char**));
1448 av[ac++] = arg;
1449 av[ac] = nil;
1450 c->av = av;
1451 threadexec(cpid, sfd, av[0], av);
1452 /* libthread uses execvp so no need to do this */
1453 #if 0
1454 e = av[0];
1455 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1456 goto Fail;
1457 if(cputype){
1458 sprint(buf, "%s/%s", cputype, av[0]);
1459 procexec(cpid, sfd, buf, av);
1461 sprint(buf, "/bin/%s", av[0]);
1462 procexec(cpid, sfd, buf, av);
1463 #endif
1464 goto Fail;
1466 Hard:
1468 * ugly: set path = (. $cputype /bin)
1469 * should honor $path if unusual.
1471 if(cputype){
1472 n = 0;
1473 memmove(buf+n, ".", 2);
1474 n += 2;
1475 i = strlen(cputype)+1;
1476 memmove(buf+n, cputype, i);
1477 n += i;
1478 memmove(buf+n, "/bin", 5);
1479 n += 5;
1480 fd = create("/env/path", OWRITE, 0666);
1481 write(fd, buf, n);
1482 close(fd);
1485 if(arg){
1486 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1487 if(news){
1488 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1489 free(s);
1490 t = news;
1491 c->text = news;
1494 threadexecl(cpid, sfd, "rc", "rc", "-c", t, nil);
1495 warning(nil, "exec rc: %r\n");
1497 Fail:
1498 /* threadexec hasn't happened, so send a zero */
1499 close(sfd[0]);
1500 close(sfd[1]);
1501 if(sfd[2] != sfd[1])
1502 close(sfd[2]);
1503 sendul(cpid, 0);
1504 threadexits(nil);
1507 void
1508 runwaittask(void *v)
1510 Command *c;
1511 Channel *cpid;
1512 void **a;
1514 threadsetname("runwaittask");
1515 a = v;
1516 c = a[0];
1517 cpid = a[1];
1518 free(a);
1520 c->pid = recvul(cpid);
1521 while(c->pid == ~0);
1522 free(c->av);
1523 if(c->pid != 0) /* successful exec */
1524 sendp(ccommand, c);
1525 else{
1526 if(c->iseditcmd)
1527 sendul(cedit, 0);
1528 free(c->name);
1529 free(c->text);
1530 free(c);
1532 chanfree(cpid);
1535 void
1536 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1538 void **arg;
1539 Command *c;
1540 Channel *cpid;
1542 if(s == nil)
1543 return;
1545 arg = emalloc(10*sizeof(void*));
1546 c = emalloc(sizeof *c);
1547 cpid = chancreate(sizeof(ulong), 0);
1548 arg[0] = win;
1549 arg[1] = s;
1550 arg[2] = rdir;
1551 arg[3] = (void*)ndir;
1552 arg[4] = (void*)newns;
1553 arg[5] = argaddr;
1554 arg[6] = xarg;
1555 arg[7] = c;
1556 arg[8] = cpid;
1557 arg[9] = (void*)iseditcmd;
1558 threadcreate(runproc, arg, STACK);
1559 /* mustn't block here because must be ready to answer mount() call in run() */
1560 arg = emalloc(2*sizeof(void*));
1561 arg[0] = c;
1562 arg[1] = cpid;
1563 threadcreate(runwaittask, arg, STACK);