13 typedef struct Event Event;
25 char b[EVENTSIZE*UTFmax+1];
65 int label(char*, int);
66 void error(char*, ...);
67 void stdinproc(void*);
68 void stdoutproc(void*);
69 void type(Event*, int, CFid*, CFid*);
70 void sende(Event*, int, CFid*, CFid*, CFid*, int);
71 char *onestring(int, char**);
73 void deltype(uint, uint);
77 fsfidprint(CFid *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 hangupnote(void *a, char *msg)
115 if(strcmp(msg, "hangup") == 0 && pid != 0){
116 postnote(PNGROUP, pid, "hangup");
119 if(strstr(msg, "child")){
127 threadmain(int argc, char **argv)
135 dump = onestring(argc, argv);
142 name = EARGF(usage());
160 notedisable("sys: write on closed pipe");
161 noteenable("sys: child");
164 if((fs = nsmount("acme", "")) == 0)
165 sysfatal("nsmount acme: %r");
166 ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
167 if(ctlfd == 0 || fsread(ctlfd, buf, 12) != 12)
170 snprint(buf, sizeof buf, "%d", id);
171 putenv("winid", buf);
172 sprint(buf, "%d/tag", id);
173 fd = fsopenfd(fs, buf, OWRITE|OCEXEC);
174 write(fd, " Send Delete", 12);
176 sprint(buf, "%d/event", id);
177 eventfd = fsopen(fs, buf, ORDWR|OCEXEC);
178 sprint(buf, "%d/addr", id);
179 addrfd = fsopen(fs, buf, ORDWR|OCEXEC);
180 sprint(buf, "%d/data", id);
181 datafd = fsopen(fs, buf, ORDWR|OCEXEC);
182 sprint(buf, "%d/body", id);
183 /* bodyfd = fsopenfd(fs, buf, ORDWR|OCEXEC); */
184 if(eventfd==nil || addrfd==nil || datafd==nil)
185 sysfatal("data files: %r");
187 if(eventfd<0 || addrfd<0 || datafd<0 || bodyfd<0)
188 sysfatal("data files: %r");
192 cwait = threadwaitchan();
193 threadcreate(waitthread, nil, STACK);
194 pid = rcstart(argc, argv, &rcfd, nil);
196 sysfatal("exec failed");
198 getwd(buf1, sizeof buf1);
199 sprint(buf, "name %s/-%s\n0\n", buf1, name);
200 fswrite(ctlfd, buf, strlen(buf));
201 sprint(buf, "dumpdir %s/\n", buf1);
202 fswrite(ctlfd, buf, strlen(buf));
203 sprint(buf, "dump %s\n", dump);
204 fswrite(ctlfd, buf, strlen(buf));
206 updatewinsize(25, 80, 0, 0);
207 proccreate(stdoutproc, nil, STACK);
218 s = vsmprint(s, arg);
220 fprint(2, "win: %s: %r\n", s);
223 postnote(PNGROUP, pid, "hangup");
228 onestring(int argc, char **argv)
232 static char buf[1024];
237 for(i=0; i<argc; i++){
239 if(p+n+1 >= buf+sizeof buf)
241 memmove(p, argv[i], n);
252 static char buf[8192];
257 nbuf = fsread(efd, buf, sizeof buf);
272 while('0'<=(c=getec(efd)) && c<='9')
275 error("event number syntax");
280 geter(CFid *efd, char *buf, int *nb)
290 while(!fullrune(buf, n))
291 buf[n++] = getec(efd);
299 gete(CFid *efd, Event *e)
307 e->flag = geten(efd);
309 if(e->nr > EVENTSIZE)
310 error("event string too long");
312 for(i=0; i<e->nr; i++){
313 e->r[i] = geter(efd, e->b+e->nb, &nb);
318 if(getec(efd) != '\n')
319 error("event syntax 2");
323 nrunes(char *s, int nb)
330 i += chartorune(&r, s+i);
348 fprint(2, "typing[%d,%d)\n", q.p, q.p+ntyper);
351 fprint(2, "msg %c%c q[%d,%d)... ", e.c1, e.c2, e.q0, e.q1);
356 print("unknown message %c%c\n", e.c1, e.c2);
359 case 'E': /* write to body; can't affect us */
361 fprint(2, "shift typing %d... ", e.q1-e.q0);
365 case 'F': /* generated by our actions; ignore */
374 fprint(2, "shift typing %d... ", e.q1-e.q0);
377 else if(e.q0 <= q.p+ntyper){
379 fprint(2, "type... ");
380 type(&e, fd0, afd, dfd);
396 if(e.flag&1 || (e.c2=='x' && e.nr==0 && e2.nr==0)){
397 /* send it straight back */
398 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
401 if(e.q0==e.q1 && (e.flag&2)){
407 sende(&e, fd0, cfd, afd, dfd, 0);
408 sende(&blank, fd0, cfd, afd, dfd, 0);
410 sende(&e3, fd0, cfd, afd, dfd, 1);
411 }else if(e.q1 != e.q0)
412 sende(&e, fd0, cfd, afd, dfd, 1);
417 /* just send it back */
420 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
444 char x[16], hold[UTFmax];
447 buf = malloc(8192+UTFmax+1);
450 /* Let typing have a go -- maybe there's a rubout waiting. */
452 n = read(fd1, buf+npart, 8192);
457 s = memchr(buf+npart, 0, n);
459 for(t=s; s<buf+npart+n; s++)
460 if(*t = *s) /* assign = */
467 /* hold on to final partial rune */
469 while(n>0 && (buf[n-1]&0xC0)){
472 if((buf[n]&0xC0)!=0x80){
473 if(fullrune(buf+n, npart)){
474 w = chartorune(&r, buf+n);
482 memmove(hold, buf+n, npart);
487 m = sprint(x, "#%d", q.p);
488 if(fswrite(afd, x, m) != m){
489 fprint(2, "stdout writing address: %r; resetting\n");
490 fswrite(afd, "$", 1);
491 m = fsread(afd, x, sizeof x-1);
497 if(fswrite(dfd, buf, n) != n)
498 error("stdout writing body");
499 q.p += nrunes(buf, n);
501 memmove(buf, hold, npart);
508 label(char *sr, int n)
510 char *sl, *el, *er, *r, *p;
513 for(r=er-1; r>=sr; r--)
520 if(el-sr > sizeof wdir - strlen(name) - 20)
521 sr = el - sizeof wdir - strlen(name) - 20;
522 for(sl=el-3; sl>=sr; sl--)
523 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
530 * add /-sysname if not present
532 snprint(wdir, sizeof wdir, "name %s", sl+3);
533 p = strrchr(wdir, '/');
534 if(p==nil || *(p+1) != '-'){
535 p = wdir+strlen(wdir);
541 strcat(wdir, "\n0\n");
542 fswrite(ctlfd, wdir, strlen(wdir));
544 memmove(sl, el, er-el);
576 addtype(int c, uint p0, char *b, int nb, int nr)
583 for(i=0; i<nb; i+=w){
584 w = chartorune(&r, b+i);
585 if((r==0x7F||r==3) && c=='K'){
586 write(rcfd, "\x7F", 1);
587 /* toss all typing */
592 /* buglet: more than one delete ignored */
595 if(r=='\n' || r==0x04)
598 typing = realloc(typing, ntypeb+nb);
602 memmove(typing+ntypeb, b, nb);
605 for(p=0; p<p0 && b0<typing+ntypeb; p++){
606 w = chartorune(&r, b0+i);
610 error("typing: findrune");
611 memmove(b0+nb, b0, (typing+ntypeb)-b0);
624 for(i=0; i<ntypeb; i++)
625 if(typing[i]=='\n' || typing[i]==0x04){
628 if(write(fd0, typing, n) != n)
629 error("sending to program");
630 nr = nrunes(typing, i);
634 memmove(typing, typing+i, ntypeb);
638 print("no breakchar\n");
645 deltype(uint p0, uint p1)
653 for(p=0; p<p0 && b0<ntypeb; p++){
654 w = chartorune(&r, typing+b0);
661 for(; p<p1 && b1<ntypeb; p++){
662 w = chartorune(&r, typing+b1);
664 if(r=='\n' || r==0x04)
669 memmove(typing+b0, typing+b1, ntypeb-b1);
675 type(Event *e, int fd0, CFid *afd, CFid *dfd)
681 addtype(e->c1, e->q0-q.p, e->b, e->nb, e->nr);
685 n = sprint(buf, "#%d", m);
686 fswrite(afd, buf, n);
687 n = fsread(dfd, buf, sizeof buf);
690 do; while(n>0 && (buf[--n]&0xC0)==0x80);
695 addtype(e->c1, m-q.p, buf, n, nr);
703 sende(Event *e, int fd0, CFid *cfd, CFid *afd, CFid *dfd, int donl)
705 int l, m, n, nr, lastc, end;
706 char abuf[16], buf[128];
709 l = sprint(abuf, "#%d", end);
710 fswrite(afd, abuf, l);
712 fswrite(dfd, e->b, e->nb);
713 addtype(e->c1, ntyper, e->b, e->nb, e->nr);
714 lastc = e->r[e->nr-1];
719 n = sprint(buf, "#%d", m);
720 fswrite(afd, buf, n);
721 n = fsread(dfd, buf, sizeof buf);
724 do; while(n>0 && (buf[--n]&0xC0)==0x80);
729 l = sprint(abuf, "#%d", end);
730 fswrite(afd, abuf, l);
731 fswrite(dfd, buf, n);
732 addtype(e->c1, ntyper, buf, n, nr);
738 if(donl && lastc!='\n'){
739 fswrite(dfd, "\n", 1);
740 addtype(e->c1, ntyper, "\n", 1, 1);
742 fswrite(cfd, "dot=addr", 8);