11 typedef struct Event Event;
23 char b[EVENTSIZE*UTFmax+1];
63 int label(char*, int);
65 void stdinproc(void*);
66 void stdoutproc(void*);
67 void type(Event*, int, Fid*, Fid*);
68 void sende(Event*, int, Fid*, Fid*, Fid*, int);
69 char *onestring(int, char**);
71 void deltype(uint, uint);
75 fsfidprint(Fid *fid, char *fmt, ...)
82 n = vsnprint(buf, sizeof buf, fmt, arg);
84 return fswrite(fid, buf, n);
90 fprint(2, "usage: win cmd args...\n");
91 threadexitsall("usage");
95 nopipes(void *v, char *msg)
98 if(strcmp(msg, "sys: write on closed pipe") == 0)
111 threadmain(int argc, char **argv)
135 threadnotify(nopipes, 1);
136 if((fs = nsmount("acme", "")) == 0)
137 sysfatal("nsmount acme: %r");
138 ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
139 if(ctlfd == 0 || fsread(ctlfd, buf, 12) != 12)
142 sprint(buf, "%d/tag", id);
143 fd = fsopenfd(fs, buf, OWRITE|OCEXEC);
144 write(fd, " Send Delete", 12);
146 sprint(buf, "%d/event", id);
147 eventfd = fsopen(fs, buf, ORDWR|OCEXEC);
148 sprint(buf, "%d/addr", id);
149 addrfd = fsopen(fs, buf, ORDWR|OCEXEC);
150 sprint(buf, "%d/data", id);
151 datafd = fsopen(fs, buf, ORDWR|OCEXEC);
152 sprint(buf, "%d/body", id);
153 /* bodyfd = fsopenfd(fs, buf, ORDWR|OCEXEC); */
154 if(eventfd==nil || addrfd==nil || datafd==nil)
155 sysfatal("data files: %r");
157 if(eventfd<0 || addrfd<0 || datafd<0 || bodyfd<0)
158 sysfatal("data files: %r");
162 cwait = threadwaitchan();
163 threadcreate(waitthread, nil, STACK);
164 pid = rcstart(argc, argv, &rcfd);
166 sysfatal("exec failed");
168 getwd(buf1, sizeof buf1);
169 sprint(buf, "name %s/-%s\n0\n", buf1, name);
170 fswrite(ctlfd, buf, strlen(buf));
171 sprint(buf, "dumpdir %s/\n", buf1);
172 fswrite(ctlfd, buf, strlen(buf));
173 sprint(buf, "dump %s\n", onestring(argc, argv));
174 fswrite(ctlfd, buf, strlen(buf));
176 threadcreate(stdoutproc, nil, STACK);
184 fprint(2, "win: %s: %r\n", s);
188 postnote(PNGROUP, pid, "hangup");
193 onestring(int argc, char **argv)
197 static char buf[1024];
202 for(i=0; i<argc; i++){
204 if(p+n+1 >= buf+sizeof buf)
206 memmove(p, argv[i], n);
217 static char buf[8192];
222 nbuf = fsread(efd, buf, sizeof buf);
237 while('0'<=(c=getec(efd)) && c<='9')
240 error("event number syntax");
245 geter(Fid *efd, char *buf, int *nb)
255 while(!fullrune(buf, n))
256 buf[n++] = getec(efd);
264 gete(Fid *efd, Event *e)
272 e->flag = geten(efd);
274 if(e->nr > EVENTSIZE)
275 error("event string too long");
277 for(i=0; i<e->nr; i++){
278 e->r[i] = geter(efd, e->b+e->nb, &nb);
283 if(getec(efd) != '\n')
284 error("event syntax 2");
288 nrunes(char *s, int nb)
295 i += chartorune(&r, s+i);
313 fprint(2, "typing[%d,%d)\n", q.p, q.p+ntyper);
316 fprint(2, "msg %c%c q[%d,%d)... ", e.c1, e.c2, e.q0, e.q1);
321 print("unknown message %c%c\n", e.c1, e.c2);
324 case 'E': /* write to body; can't affect us */
326 fprint(2, "shift typing %d... ", e.q1-e.q0);
330 case 'F': /* generated by our actions; ignore */
339 fprint(2, "shift typing %d... ", e.q1-e.q0);
342 else if(e.q0 <= q.p+ntyper){
344 fprint(2, "type... ");
345 type(&e, fd0, afd, dfd);
361 if(e.flag&1 || (e.c2=='x' && e.nr==0 && e2.nr==0)){
362 /* send it straight back */
363 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
366 if(e.q0==e.q1 && (e.flag&2)){
372 sende(&e, fd0, cfd, afd, dfd, 0);
373 sende(&blank, fd0, cfd, afd, dfd, 0);
375 sende(&e3, fd0, cfd, afd, dfd, 1);
376 }else if(e.q1 != e.q0)
377 sende(&e, fd0, cfd, afd, dfd, 1);
382 /* just send it back */
385 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
409 char x[16], hold[UTFmax];
412 threadnotify(nopipes, 1);
413 buf = malloc(8192+UTFmax+1);
416 /* Let typing have a go -- maybe there's a rubout waiting. */
418 n = threadread(fd1, buf+npart, 8192);
425 s = memchr(buf+npart, 0, n);
427 for(t=s; s<buf+npart+n; s++)
428 if(*t = *s) /* assign = */
435 /* hold on to final partial rune */
437 while(n>0 && (buf[n-1]&0xC0)){
440 if((buf[n]&0xC0)!=0x80){
441 if(fullrune(buf+n, npart)){
442 w = chartorune(&r, buf+n);
450 memmove(hold, buf+n, npart);
455 m = sprint(x, "#%d", q.p);
456 if(fswrite(afd, x, m) != m)
457 error("stdout writing address");
458 if(fswrite(dfd, buf, n) != n)
459 error("stdout writing body");
460 q.p += nrunes(buf, n);
462 memmove(buf, hold, npart);
469 label(char *sr, int n)
471 char *sl, *el, *er, *r;
474 for(r=er-1; r>=sr; r--)
481 if(el-sr > sizeof wdir)
482 sr = el - sizeof wdir;
483 for(sl=el-3; sl>=sr; sl--)
484 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
490 snprint(wdir, sizeof wdir, "name %s/-%s\n0\n", sl+3, name);
491 fswrite(ctlfd, wdir, strlen(wdir));
493 memmove(sl, el, er-el);
525 addtype(int c, uint p0, char *b, int nb, int nr)
532 for(i=0; i<nb; i+=w){
533 w = chartorune(&r, b+i);
534 if((r==0x7F||r==3) && c=='K'){
535 write(rcfd, "\x7F", 1);
536 /* toss all typing */
541 /* buglet: more than one delete ignored */
544 if(r=='\n' || r==0x04)
547 typing = realloc(typing, ntypeb+nb);
551 memmove(typing+ntypeb, b, nb);
554 for(p=0; p<p0 && b0<typing+ntypeb; p++){
555 w = chartorune(&r, b0+i);
559 error("typing: findrune");
560 memmove(b0+nb, b0, (typing+ntypeb)-b0);
573 for(i=0; i<ntypeb; i++)
574 if(typing[i]=='\n' || typing[i]==0x04){
575 n = i + (typing[i] == '\n');
577 if(write(fd0, typing, n) != n)
578 error("sending to program");
579 nr = nrunes(typing, i);
583 memmove(typing, typing+i, ntypeb);
587 print("no breakchar\n");
594 deltype(uint p0, uint p1)
602 for(p=0; p<p0 && b0<ntypeb; p++){
603 w = chartorune(&r, typing+b0);
610 for(; p<p1 && b1<ntypeb; p++){
611 w = chartorune(&r, typing+b1);
613 if(r=='\n' || r==0x04)
618 memmove(typing+b0, typing+b1, ntypeb-b1);
624 type(Event *e, int fd0, Fid *afd, Fid *dfd)
630 addtype(e->c1, e->q0-q.p, e->b, e->nb, e->nr);
634 n = sprint(buf, "#%d", m);
635 fswrite(afd, buf, n);
636 n = fsread(dfd, buf, sizeof buf);
639 do; while(n>0 && (buf[--n]&0xC0)==0x80);
644 addtype(e->c1, m-q.p, buf, n, nr);
652 sende(Event *e, int fd0, Fid *cfd, Fid *afd, Fid *dfd, int donl)
654 int l, m, n, nr, lastc, end;
655 char abuf[16], buf[128];
658 l = sprint(abuf, "#%d", end);
659 fswrite(afd, abuf, l);
661 fswrite(dfd, e->b, e->nb);
662 addtype(e->c1, ntyper, e->b, e->nb, e->nr);
663 lastc = e->r[e->nr-1];
668 n = sprint(buf, "#%d", m);
669 fswrite(afd, buf, n);
670 n = fsread(dfd, buf, sizeof buf);
673 do; while(n>0 && (buf[--n]&0xC0)==0x80);
678 l = sprint(abuf, "#%d", end);
679 fswrite(afd, abuf, l);
680 fswrite(dfd, buf, n);
681 addtype(e->c1, ntyper, buf, n, nr);
687 if(donl && lastc!='\n'){
688 fswrite(dfd, "\n", 1);
689 addtype(e->c1, ntyper, "\n", 1, 1);
691 fswrite(cfd, "dot=addr", 8);