13 typedef struct Event Event;
25 char b[EVENTSIZE*UTFmax+1];
65 int label(char*, int);
67 void stdinproc(void*);
68 void stdoutproc(void*);
69 void type(Event*, int, Fid*, Fid*);
70 void sende(Event*, int, Fid*, Fid*, Fid*, int);
71 char *onestring(int, char**);
73 void deltype(uint, uint);
77 fsfidprint(Fid *fid, char *fmt, ...)
84 n = vsnprint(buf, sizeof buf, fmt, arg);
86 return fswrite(fid, buf, n);
92 fprint(2, "usage: win cmd args...\n");
93 threadexitsall("usage");
97 nopipes(void *v, char *msg)
100 if(strcmp(msg, "sys: write on closed pipe") == 0)
113 threadmain(int argc, char **argv)
125 name = EARGF(usage());
143 threadnotify(nopipes, 1);
144 if((fs = nsmount("acme", "")) == 0)
145 sysfatal("nsmount acme: %r");
146 ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
147 if(ctlfd == 0 || fsread(ctlfd, buf, 12) != 12)
150 snprint(buf, sizeof buf, "%d", id);
151 putenv("winid", buf);
152 sprint(buf, "%d/tag", id);
153 fd = fsopenfd(fs, buf, OWRITE|OCEXEC);
154 write(fd, " Send Delete", 12);
156 sprint(buf, "%d/event", id);
157 eventfd = fsopen(fs, buf, ORDWR|OCEXEC);
158 sprint(buf, "%d/addr", id);
159 addrfd = fsopen(fs, buf, ORDWR|OCEXEC);
160 sprint(buf, "%d/data", id);
161 datafd = fsopen(fs, buf, ORDWR|OCEXEC);
162 sprint(buf, "%d/body", id);
163 /* bodyfd = fsopenfd(fs, buf, ORDWR|OCEXEC); */
164 if(eventfd==nil || addrfd==nil || datafd==nil)
165 sysfatal("data files: %r");
167 if(eventfd<0 || addrfd<0 || datafd<0 || bodyfd<0)
168 sysfatal("data files: %r");
172 cwait = threadwaitchan();
173 threadcreate(waitthread, nil, STACK);
174 pid = rcstart(argc, argv, &rcfd, nil);
176 sysfatal("exec failed");
178 getwd(buf1, sizeof buf1);
179 sprint(buf, "name %s/-%s\n0\n", buf1, name);
180 fswrite(ctlfd, buf, strlen(buf));
181 sprint(buf, "dumpdir %s/\n", buf1);
182 fswrite(ctlfd, buf, strlen(buf));
183 sprint(buf, "dump %s\n", onestring(argc, argv));
184 fswrite(ctlfd, buf, strlen(buf));
186 updatewinsize(25, 80, 0, 0);
187 threadcreate(stdoutproc, nil, STACK);
195 fprint(2, "win: %s: %r\n", s);
199 postnote(PNGROUP, pid, "hangup");
204 onestring(int argc, char **argv)
208 static char buf[1024];
213 for(i=0; i<argc; i++){
215 if(p+n+1 >= buf+sizeof buf)
217 memmove(p, argv[i], n);
228 static char buf[8192];
233 nbuf = fsread(efd, buf, sizeof buf);
248 while('0'<=(c=getec(efd)) && c<='9')
251 error("event number syntax");
256 geter(Fid *efd, char *buf, int *nb)
266 while(!fullrune(buf, n))
267 buf[n++] = getec(efd);
275 gete(Fid *efd, Event *e)
283 e->flag = geten(efd);
285 if(e->nr > EVENTSIZE)
286 error("event string too long");
288 for(i=0; i<e->nr; i++){
289 e->r[i] = geter(efd, e->b+e->nb, &nb);
294 if(getec(efd) != '\n')
295 error("event syntax 2");
299 nrunes(char *s, int nb)
306 i += chartorune(&r, s+i);
324 fprint(2, "typing[%d,%d)\n", q.p, q.p+ntyper);
327 fprint(2, "msg %c%c q[%d,%d)... ", e.c1, e.c2, e.q0, e.q1);
332 print("unknown message %c%c\n", e.c1, e.c2);
335 case 'E': /* write to body; can't affect us */
337 fprint(2, "shift typing %d... ", e.q1-e.q0);
341 case 'F': /* generated by our actions; ignore */
350 fprint(2, "shift typing %d... ", e.q1-e.q0);
353 else if(e.q0 <= q.p+ntyper){
355 fprint(2, "type... ");
356 type(&e, fd0, afd, dfd);
372 if(e.flag&1 || (e.c2=='x' && e.nr==0 && e2.nr==0)){
373 /* send it straight back */
374 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
377 if(e.q0==e.q1 && (e.flag&2)){
383 sende(&e, fd0, cfd, afd, dfd, 0);
384 sende(&blank, fd0, cfd, afd, dfd, 0);
386 sende(&e3, fd0, cfd, afd, dfd, 1);
387 }else if(e.q1 != e.q0)
388 sende(&e, fd0, cfd, afd, dfd, 1);
393 /* just send it back */
396 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
420 char x[16], hold[UTFmax];
423 threadnotify(nopipes, 1);
424 buf = malloc(8192+UTFmax+1);
427 /* Let typing have a go -- maybe there's a rubout waiting. */
429 n = threadread(fd1, buf+npart, 8192);
436 s = memchr(buf+npart, 0, n);
438 for(t=s; s<buf+npart+n; s++)
439 if(*t = *s) /* assign = */
446 /* hold on to final partial rune */
448 while(n>0 && (buf[n-1]&0xC0)){
451 if((buf[n]&0xC0)!=0x80){
452 if(fullrune(buf+n, npart)){
453 w = chartorune(&r, buf+n);
461 memmove(hold, buf+n, npart);
466 m = sprint(x, "#%d", q.p);
467 if(fswrite(afd, x, m) != m)
468 error("stdout writing address");
469 if(fswrite(dfd, buf, n) != n)
470 error("stdout writing body");
471 q.p += nrunes(buf, n);
473 memmove(buf, hold, npart);
480 label(char *sr, int n)
482 char *sl, *el, *er, *r;
485 for(r=er-1; r>=sr; r--)
492 if(el-sr > sizeof wdir)
493 sr = el - sizeof wdir;
494 for(sl=el-3; sl>=sr; sl--)
495 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
501 snprint(wdir, sizeof wdir, "name %s/-%s\n0\n", sl+3, name);
502 fswrite(ctlfd, wdir, strlen(wdir));
504 memmove(sl, el, er-el);
536 addtype(int c, uint p0, char *b, int nb, int nr)
543 for(i=0; i<nb; i+=w){
544 w = chartorune(&r, b+i);
545 if((r==0x7F||r==3) && c=='K'){
546 write(rcfd, "\x7F", 1);
547 /* toss all typing */
552 /* buglet: more than one delete ignored */
555 if(r=='\n' || r==0x04)
558 typing = realloc(typing, ntypeb+nb);
562 memmove(typing+ntypeb, b, nb);
565 for(p=0; p<p0 && b0<typing+ntypeb; p++){
566 w = chartorune(&r, b0+i);
570 error("typing: findrune");
571 memmove(b0+nb, b0, (typing+ntypeb)-b0);
584 for(i=0; i<ntypeb; i++)
585 if(typing[i]=='\n' || typing[i]==0x04){
588 if(write(fd0, typing, n) != n)
589 error("sending to program");
590 nr = nrunes(typing, i);
594 memmove(typing, typing+i, ntypeb);
598 print("no breakchar\n");
605 deltype(uint p0, uint p1)
613 for(p=0; p<p0 && b0<ntypeb; p++){
614 w = chartorune(&r, typing+b0);
621 for(; p<p1 && b1<ntypeb; p++){
622 w = chartorune(&r, typing+b1);
624 if(r=='\n' || r==0x04)
629 memmove(typing+b0, typing+b1, ntypeb-b1);
635 type(Event *e, int fd0, Fid *afd, Fid *dfd)
641 addtype(e->c1, e->q0-q.p, e->b, e->nb, e->nr);
645 n = sprint(buf, "#%d", m);
646 fswrite(afd, buf, n);
647 n = fsread(dfd, buf, sizeof buf);
650 do; while(n>0 && (buf[--n]&0xC0)==0x80);
655 addtype(e->c1, m-q.p, buf, n, nr);
663 sende(Event *e, int fd0, Fid *cfd, Fid *afd, Fid *dfd, int donl)
665 int l, m, n, nr, lastc, end;
666 char abuf[16], buf[128];
669 l = sprint(abuf, "#%d", end);
670 fswrite(afd, abuf, l);
672 fswrite(dfd, e->b, e->nb);
673 addtype(e->c1, ntyper, e->b, e->nb, e->nr);
674 lastc = e->r[e->nr-1];
679 n = sprint(buf, "#%d", m);
680 fswrite(afd, buf, n);
681 n = fsread(dfd, buf, sizeof buf);
684 do; while(n>0 && (buf[--n]&0xC0)==0x80);
689 l = sprint(abuf, "#%d", end);
690 fswrite(afd, abuf, l);
691 fswrite(dfd, buf, n);
692 addtype(e->c1, ntyper, buf, n, nr);
698 if(donl && lastc!='\n'){
699 fswrite(dfd, "\n", 1);
700 addtype(e->c1, ntyper, "\n", 1, 1);
702 fswrite(cfd, "dot=addr", 8);