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)
137 threadnotify(nopipes, 1);
138 if((fs = nsmount("acme", "")) == 0)
139 sysfatal("nsmount acme: %r");
140 ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
141 if(ctlfd == 0 || fsread(ctlfd, buf, 12) != 12)
144 sprint(buf, "%d/tag", id);
145 fd = fsopenfd(fs, buf, OWRITE|OCEXEC);
146 write(fd, " Send Delete", 12);
148 sprint(buf, "%d/event", id);
149 eventfd = fsopen(fs, buf, ORDWR|OCEXEC);
150 sprint(buf, "%d/addr", id);
151 addrfd = fsopen(fs, buf, ORDWR|OCEXEC);
152 sprint(buf, "%d/data", id);
153 datafd = fsopen(fs, buf, ORDWR|OCEXEC);
154 sprint(buf, "%d/body", id);
155 /* bodyfd = fsopenfd(fs, buf, ORDWR|OCEXEC); */
156 if(eventfd==nil || addrfd==nil || datafd==nil)
157 sysfatal("data files: %r");
159 if(eventfd<0 || addrfd<0 || datafd<0 || bodyfd<0)
160 sysfatal("data files: %r");
164 cwait = threadwaitchan();
165 threadcreate(waitthread, nil, STACK);
166 pid = rcstart(argc, argv, &rcfd, nil);
168 sysfatal("exec failed");
170 getwd(buf1, sizeof buf1);
171 sprint(buf, "name %s/-%s\n0\n", buf1, name);
172 fswrite(ctlfd, buf, strlen(buf));
173 sprint(buf, "dumpdir %s/\n", buf1);
174 fswrite(ctlfd, buf, strlen(buf));
175 sprint(buf, "dump %s\n", onestring(argc, argv));
176 fswrite(ctlfd, buf, strlen(buf));
178 threadcreate(stdoutproc, nil, STACK);
186 fprint(2, "win: %s: %r\n", s);
190 postnote(PNGROUP, pid, "hangup");
195 onestring(int argc, char **argv)
199 static char buf[1024];
204 for(i=0; i<argc; i++){
206 if(p+n+1 >= buf+sizeof buf)
208 memmove(p, argv[i], n);
219 static char buf[8192];
224 nbuf = fsread(efd, buf, sizeof buf);
239 while('0'<=(c=getec(efd)) && c<='9')
242 error("event number syntax");
247 geter(Fid *efd, char *buf, int *nb)
257 while(!fullrune(buf, n))
258 buf[n++] = getec(efd);
266 gete(Fid *efd, Event *e)
274 e->flag = geten(efd);
276 if(e->nr > EVENTSIZE)
277 error("event string too long");
279 for(i=0; i<e->nr; i++){
280 e->r[i] = geter(efd, e->b+e->nb, &nb);
285 if(getec(efd) != '\n')
286 error("event syntax 2");
290 nrunes(char *s, int nb)
297 i += chartorune(&r, s+i);
315 fprint(2, "typing[%d,%d)\n", q.p, q.p+ntyper);
318 fprint(2, "msg %c%c q[%d,%d)... ", e.c1, e.c2, e.q0, e.q1);
323 print("unknown message %c%c\n", e.c1, e.c2);
326 case 'E': /* write to body; can't affect us */
328 fprint(2, "shift typing %d... ", e.q1-e.q0);
332 case 'F': /* generated by our actions; ignore */
341 fprint(2, "shift typing %d... ", e.q1-e.q0);
344 else if(e.q0 <= q.p+ntyper){
346 fprint(2, "type... ");
347 type(&e, fd0, afd, dfd);
363 if(e.flag&1 || (e.c2=='x' && e.nr==0 && e2.nr==0)){
364 /* send it straight back */
365 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
368 if(e.q0==e.q1 && (e.flag&2)){
374 sende(&e, fd0, cfd, afd, dfd, 0);
375 sende(&blank, fd0, cfd, afd, dfd, 0);
377 sende(&e3, fd0, cfd, afd, dfd, 1);
378 }else if(e.q1 != e.q0)
379 sende(&e, fd0, cfd, afd, dfd, 1);
384 /* just send it back */
387 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
411 char x[16], hold[UTFmax];
414 threadnotify(nopipes, 1);
415 buf = malloc(8192+UTFmax+1);
418 /* Let typing have a go -- maybe there's a rubout waiting. */
420 n = threadread(fd1, buf+npart, 8192);
427 s = memchr(buf+npart, 0, n);
429 for(t=s; s<buf+npart+n; s++)
430 if(*t = *s) /* assign = */
437 /* hold on to final partial rune */
439 while(n>0 && (buf[n-1]&0xC0)){
442 if((buf[n]&0xC0)!=0x80){
443 if(fullrune(buf+n, npart)){
444 w = chartorune(&r, buf+n);
452 memmove(hold, buf+n, npart);
457 m = sprint(x, "#%d", q.p);
458 if(fswrite(afd, x, m) != m)
459 error("stdout writing address");
460 if(fswrite(dfd, buf, n) != n)
461 error("stdout writing body");
462 q.p += nrunes(buf, n);
464 memmove(buf, hold, npart);
471 label(char *sr, int n)
473 char *sl, *el, *er, *r;
476 for(r=er-1; r>=sr; r--)
483 if(el-sr > sizeof wdir)
484 sr = el - sizeof wdir;
485 for(sl=el-3; sl>=sr; sl--)
486 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
492 snprint(wdir, sizeof wdir, "name %s/-%s\n0\n", sl+3, name);
493 fswrite(ctlfd, wdir, strlen(wdir));
495 memmove(sl, el, er-el);
527 addtype(int c, uint p0, char *b, int nb, int nr)
534 for(i=0; i<nb; i+=w){
535 w = chartorune(&r, b+i);
536 if((r==0x7F||r==3) && c=='K'){
537 write(rcfd, "\x7F", 1);
538 /* toss all typing */
543 /* buglet: more than one delete ignored */
546 if(r=='\n' || r==0x04)
549 typing = realloc(typing, ntypeb+nb);
553 memmove(typing+ntypeb, b, nb);
556 for(p=0; p<p0 && b0<typing+ntypeb; p++){
557 w = chartorune(&r, b0+i);
561 error("typing: findrune");
562 memmove(b0+nb, b0, (typing+ntypeb)-b0);
575 for(i=0; i<ntypeb; i++)
576 if(typing[i]=='\n' || typing[i]==0x04){
579 if(write(fd0, typing, n) != n)
580 error("sending to program");
581 nr = nrunes(typing, i);
585 memmove(typing, typing+i, ntypeb);
589 print("no breakchar\n");
596 deltype(uint p0, uint p1)
604 for(p=0; p<p0 && b0<ntypeb; p++){
605 w = chartorune(&r, typing+b0);
612 for(; p<p1 && b1<ntypeb; p++){
613 w = chartorune(&r, typing+b1);
615 if(r=='\n' || r==0x04)
620 memmove(typing+b0, typing+b1, ntypeb-b1);
626 type(Event *e, int fd0, Fid *afd, Fid *dfd)
632 addtype(e->c1, e->q0-q.p, e->b, e->nb, e->nr);
636 n = sprint(buf, "#%d", m);
637 fswrite(afd, buf, n);
638 n = fsread(dfd, buf, sizeof buf);
641 do; while(n>0 && (buf[--n]&0xC0)==0x80);
646 addtype(e->c1, m-q.p, buf, n, nr);
654 sende(Event *e, int fd0, Fid *cfd, Fid *afd, Fid *dfd, int donl)
656 int l, m, n, nr, lastc, end;
657 char abuf[16], buf[128];
660 l = sprint(abuf, "#%d", end);
661 fswrite(afd, abuf, l);
663 fswrite(dfd, e->b, e->nb);
664 addtype(e->c1, ntyper, e->b, e->nb, e->nr);
665 lastc = e->r[e->nr-1];
670 n = sprint(buf, "#%d", m);
671 fswrite(afd, buf, n);
672 n = fsread(dfd, buf, sizeof buf);
675 do; while(n>0 && (buf[--n]&0xC0)==0x80);
680 l = sprint(abuf, "#%d", end);
681 fswrite(afd, abuf, l);
682 fswrite(dfd, buf, n);
683 addtype(e->c1, ntyper, buf, n, nr);
689 if(donl && lastc!='\n'){
690 fswrite(dfd, "\n", 1);
691 addtype(e->c1, ntyper, "\n", 1, 1);
693 fswrite(cfd, "dot=addr", 8);