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");
104 hangupnote(void *a, char *msg)
106 if(strcmp(msg, "hangup") == 0 && pid != 0){
107 postnote(PNGROUP, pid, "hangup");
110 if(strstr(msg, "child")){
118 threadmain(int argc, char **argv)
126 dump = onestring(argc, argv);
133 name = EARGF(usage());
152 * notedisable("sys: write on closed pipe");
153 * not okay to disable the note, because that
154 * gets inherited by the subshell, so that something
155 * as simple as "yes | sed 10q" never exits.
156 * call notifyoff instead. (is notedisable ever safe?)
158 notifyoff("sys: write on closed pipe");
160 noteenable("sys: child");
163 if((fs = nsmount("acme", "")) == 0)
164 sysfatal("nsmount acme: %r");
165 ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
166 if(ctlfd == 0 || fsread(ctlfd, buf, 12) != 12)
169 snprint(buf, sizeof buf, "%d", id);
170 putenv("winid", buf);
171 sprint(buf, "%d/tag", id);
172 fd = fsopenfd(fs, buf, OWRITE|OCEXEC);
173 write(fd, " Send Noscroll", 1+4+1+8);
175 sprint(buf, "%d/event", id);
176 eventfd = fsopen(fs, buf, ORDWR|OCEXEC);
177 sprint(buf, "%d/addr", id);
178 addrfd = fsopen(fs, buf, ORDWR|OCEXEC);
179 sprint(buf, "%d/data", id);
180 datafd = fsopen(fs, buf, ORDWR|OCEXEC);
181 sprint(buf, "%d/body", id);
182 /* bodyfd = fsopenfd(fs, buf, ORDWR|OCEXEC); */
183 if(eventfd==nil || addrfd==nil || datafd==nil)
184 sysfatal("data files: %r");
186 if(eventfd<0 || addrfd<0 || datafd<0 || bodyfd<0)
187 sysfatal("data files: %r");
191 cwait = threadwaitchan();
192 threadcreate(waitthread, nil, STACK);
193 pid = rcstart(argc, argv, &rcfd, nil);
195 sysfatal("exec failed");
197 getwd(buf1, sizeof buf1);
198 sprint(buf, "name %s/-%s\n0\n", buf1, name);
199 fswrite(ctlfd, buf, strlen(buf));
200 sprint(buf, "dumpdir %s/\n", buf1);
201 fswrite(ctlfd, buf, strlen(buf));
202 sprint(buf, "dump %s\n", dump);
203 fswrite(ctlfd, buf, strlen(buf));
204 sprint(buf, "scroll");
205 fswrite(ctlfd, buf, strlen(buf));
207 updatewinsize(25, 80, 0, 0);
208 proccreate(stdoutproc, nil, STACK);
219 s = vsmprint(s, arg);
221 fprint(2, "win: %s: %r\n", s);
224 postnote(PNGROUP, pid, "hangup");
229 onestring(int argc, char **argv)
233 static char buf[1024];
238 for(i=0; i<argc; i++){
240 if(p+n+1 >= buf+sizeof buf)
242 memmove(p, argv[i], n);
253 static char buf[8192];
258 nbuf = fsread(efd, buf, sizeof buf);
273 while('0'<=(c=getec(efd)) && c<='9')
276 error("event number syntax");
281 geter(CFid *efd, char *buf, int *nb)
291 while(!fullrune(buf, n))
292 buf[n++] = getec(efd);
300 gete(CFid *efd, Event *e)
308 e->flag = geten(efd);
310 if(e->nr > EVENTSIZE)
311 error("event string too long");
313 for(i=0; i<e->nr; i++){
314 e->r[i] = geter(efd, e->b+e->nb, &nb);
319 if(getec(efd) != '\n')
320 error("event syntax 2");
324 nrunes(char *s, int nb)
331 i += chartorune(&r, s+i);
349 fprint(2, "typing[%d,%d)\n", q.p, q.p+ntyper);
352 fprint(2, "msg %c%c q[%d,%d)... ", e.c1, e.c2, e.q0, e.q1);
357 print("unknown message %c%c\n", e.c1, e.c2);
360 case 'E': /* write to body; can't affect us */
362 fprint(2, "shift typing %d... ", e.q1-e.q0);
366 case 'F': /* generated by our actions; ignore */
373 if(e.nr == 1 && e.r[0] == 0x7F) {
375 fsprint(addrfd, "#%ud,#%ud", e.q0, e.q1);
376 fswrite(datafd, "", 0);
383 fprint(2, "shift typing %d... ", e.q1-e.q0);
386 else if(e.q0 <= q.p+ntyper){
388 fprint(2, "type... ");
389 type(&e, fd0, afd, dfd);
405 if(e.flag&1 || (e.c2=='x' && e.nr==0 && e2.nr==0)){
406 /* send it straight back */
407 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
410 if(e.q0==e.q1 && (e.flag&2)){
415 snprint(buf, sizeof buf, "%.*S", e.nr, e.r);
416 if(cistrcmp(buf, "scroll") == 0) {
417 fsprint(ctlfd, "scroll\nshow");
420 if(cistrcmp(buf, "noscroll") == 0) {
421 fsprint(ctlfd, "noscroll");
426 sende(&e, fd0, cfd, afd, dfd, 0);
427 sende(&blank, fd0, cfd, afd, dfd, 0);
429 sende(&e3, fd0, cfd, afd, dfd, 1);
430 }else if(e.q1 != e.q0)
431 sende(&e, fd0, cfd, afd, dfd, 1);
436 /* just send it back */
439 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
463 char x[16], hold[UTFmax];
466 buf = malloc(8192+UTFmax+1);
469 /* Let typing have a go -- maybe there's a rubout waiting. */
471 n = read(fd1, buf+npart, 8192);
476 s = memchr(buf+npart, 0, n);
478 for(t=s; s<buf+npart+n; s++)
479 if(*t = *s) /* assign = */
486 /* hold on to final partial rune */
488 while(n>0 && (buf[n-1]&0xC0)){
491 if((buf[n]&0xC0)!=0x80){
492 if(fullrune(buf+n, npart)){
493 w = chartorune(&r, buf+n);
501 memmove(hold, buf+n, npart);
506 m = sprint(x, "#%d", q.p);
507 if(fswrite(afd, x, m) != m){
508 fprint(2, "stdout writing address: %r; resetting\n");
509 fswrite(afd, "$", 1);
510 m = fsread(afd, x, sizeof x-1);
516 if(fswrite(dfd, buf, n) != n)
517 error("stdout writing body");
518 q.p += nrunes(buf, n);
520 memmove(buf, hold, npart);
527 label(char *sr, int n)
529 char *sl, *el, *er, *r, *p;
532 for(r=er-1; r>=sr; r--)
539 if(el-sr > sizeof wdir - strlen(name) - 20)
540 sr = el - sizeof wdir - strlen(name) - 20;
541 for(sl=el-3; sl>=sr; sl--)
542 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
549 * add /-sysname if not present
551 snprint(wdir, sizeof wdir, "name %s", sl+3);
552 p = strrchr(wdir, '/');
553 if(p==nil || *(p+1) != '-'){
554 p = wdir+strlen(wdir);
560 strcat(wdir, "\n0\n");
561 fswrite(ctlfd, wdir, strlen(wdir));
563 memmove(sl, el, er-el);
595 addtype(int c, uint p0, char *b, int nb, int nr)
602 for(i=0; i<nb; i+=w){
603 w = chartorune(&r, b+i);
604 if((r==0x7F||r==3) && c=='K'){
605 write(rcfd, "\x7F", 1);
606 /* toss all typing */
611 /* buglet: more than one delete ignored */
614 if(r=='\n' || r==0x04)
617 typing = realloc(typing, ntypeb+nb);
621 memmove(typing+ntypeb, b, nb);
624 for(p=0; p<p0 && b0<typing+ntypeb; p++){
625 w = chartorune(&r, b0+i);
629 error("typing: findrune");
630 memmove(b0+nb, b0, (typing+ntypeb)-b0);
643 for(i=0; i<ntypeb; i++)
644 if(typing[i]=='\n' || typing[i]==0x04){
647 if(write(fd0, typing, n) != n)
648 error("sending to program");
649 nr = nrunes(typing, i);
653 memmove(typing, typing+i, ntypeb);
657 print("no breakchar\n");
664 deltype(uint p0, uint p1)
672 for(p=0; p<p0 && b0<ntypeb; p++){
673 w = chartorune(&r, typing+b0);
680 for(; p<p1 && b1<ntypeb; p++){
681 w = chartorune(&r, typing+b1);
683 if(r=='\n' || r==0x04)
688 memmove(typing+b0, typing+b1, ntypeb-b1);
694 type(Event *e, int fd0, CFid *afd, CFid *dfd)
700 addtype(e->c1, e->q0-q.p, e->b, e->nb, e->nr);
704 n = sprint(buf, "#%d", m);
705 fswrite(afd, buf, n);
706 n = fsread(dfd, buf, sizeof buf);
709 do; while(n>0 && (buf[--n]&0xC0)==0x80);
714 addtype(e->c1, m-q.p, buf, n, nr);
722 sende(Event *e, int fd0, CFid *cfd, CFid *afd, CFid *dfd, int donl)
724 int l, m, n, nr, lastc, end;
725 char abuf[16], buf[128];
728 l = sprint(abuf, "#%d", end);
729 fswrite(afd, abuf, l);
731 fswrite(dfd, e->b, e->nb);
732 addtype(e->c1, ntyper, e->b, e->nb, e->nr);
733 lastc = e->r[e->nr-1];
738 n = sprint(buf, "#%d", m);
739 fswrite(afd, buf, n);
740 n = fsread(dfd, buf, sizeof buf);
743 do; while(n>0 && (buf[--n]&0xC0)==0x80);
748 l = sprint(abuf, "#%d", end);
749 fswrite(afd, abuf, l);
750 fswrite(dfd, buf, n);
751 addtype(e->c1, ntyper, buf, n, nr);
757 if(donl && lastc!='\n'){
758 fswrite(dfd, "\n", 1);
759 addtype(e->c1, ntyper, "\n", 1, 1);
761 fswrite(cfd, "dot=addr", 8);