8 const char *termprog = "win";
13 typedef struct Event Event;
25 char b[EVENTSIZE*UTFmax+1];
68 int label(char*, int);
69 void error(char*, ...);
70 void stdinproc(void*);
71 void stdoutproc(void*);
72 void type(Event*, int, CFid*, CFid*);
73 void sende(Event*, int, CFid*, CFid*, CFid*, int);
74 char *onestring(int, char**);
76 void deltype(uint, uint);
77 void sendbs(int, int);
81 fsfidprint(CFid *fid, char *fmt, ...)
88 n = vsnprint(buf, sizeof buf, fmt, arg);
90 return fswrite(fid, buf, n);
96 fprint(2, "usage: win cmd args...\n");
97 threadexitsall("usage");
108 hangupnote(void *a, char *msg)
110 if(strcmp(msg, "hangup") == 0 && pid != 0){
111 postnote(PNGROUP, pid, "hangup");
114 if(strstr(msg, "child")){
118 n = awaitnohang(buf, sizeof buf-1);
130 threadmain(int argc, char **argv)
138 dump = onestring(argc, argv);
145 name = EARGF(usage());
164 * notedisable("sys: write on closed pipe");
165 * not okay to disable the note, because that
166 * gets inherited by the subshell, so that something
167 * as simple as "yes | sed 10q" never exits.
168 * call notifyoff instead. (is notedisable ever safe?)
170 notifyoff("sys: write on closed pipe");
172 noteenable("sys: child");
175 if((fs = nsmount("acme", "")) == 0)
176 sysfatal("nsmount acme: %r");
177 ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
178 if(ctlfd == 0 || fsread(ctlfd, buf, 12) != 12)
181 snprint(buf, sizeof buf, "%d", id);
182 putenv("winid", buf);
183 sprint(buf, "%d/tag", id);
184 fd = fsopenfd(fs, buf, OWRITE|OCEXEC);
185 write(fd, " Send", 1+4);
187 sprint(buf, "%d/event", id);
188 eventfd = fsopen(fs, buf, ORDWR|OCEXEC);
189 sprint(buf, "%d/addr", id);
190 addrfd = fsopen(fs, buf, ORDWR|OCEXEC);
191 sprint(buf, "%d/data", id);
192 datafd = fsopen(fs, buf, ORDWR|OCEXEC);
193 sprint(buf, "%d/body", id);
194 /* bodyfd = fsopenfd(fs, buf, ORDWR|OCEXEC); */
195 if(eventfd==nil || addrfd==nil || datafd==nil)
196 sysfatal("data files: %r");
198 if(eventfd<0 || addrfd<0 || datafd<0 || bodyfd<0)
199 sysfatal("data files: %r");
203 cwait = threadwaitchan();
204 threadcreate(waitthread, nil, STACK);
205 pid = rcstart(argc, argv, &rcfd, nil);
207 sysfatal("exec failed");
209 getwd(buf1, sizeof buf1);
210 sprint(buf, "name %s/-%s\n0\n", buf1, name);
211 fswrite(ctlfd, buf, strlen(buf));
212 sprint(buf, "dumpdir %s/\n", buf1);
213 fswrite(ctlfd, buf, strlen(buf));
214 sprint(buf, "dump %s\n", dump);
215 fswrite(ctlfd, buf, strlen(buf));
216 sprint(buf, "scroll");
217 fswrite(ctlfd, buf, strlen(buf));
219 updatewinsize(25, 80, 0, 0);
220 proccreate(stdoutproc, nil, STACK);
231 s = vsmprint(s, arg);
233 fprint(2, "win: %s: %r\n", s);
236 postnote(PNGROUP, pid, "hangup");
241 onestring(int argc, char **argv)
245 static char buf[1024];
250 for(i=0; i<argc; i++){
252 if(p+n+1 >= buf+sizeof buf)
254 memmove(p, argv[i], n);
265 static char buf[8192];
270 nbuf = fsread(efd, buf, sizeof buf);
285 while('0'<=(c=getec(efd)) && c<='9')
288 error("event number syntax");
293 geter(CFid *efd, char *buf, int *nb)
303 while(!fullrune(buf, n))
304 buf[n++] = getec(efd);
312 gete(CFid *efd, Event *e)
320 e->flag = geten(efd);
322 if(e->nr > EVENTSIZE)
323 error("event string too long");
325 for(i=0; i<e->nr; i++){
326 e->r[i] = geter(efd, e->b+e->nb, &nb);
331 if(getec(efd) != '\n')
332 error("event syntax 2");
336 nrunes(char *s, int nb)
343 i += chartorune(&r, s+i);
362 fprint(2, "typing[%d,%d)\n", q.p, q.p+ntyper);
365 fprint(2, "msg %c%c q[%d,%d)... ", e.c1, e.c2, e.q0, e.q1);
370 print("unknown message %c%c\n", e.c1, e.c2);
373 case 'E': /* write to body or tag; can't affect us */
378 fprint(2, "shift typing %d... ", e.q1-e.q0);
391 case 'F': /* generated by our actions; ignore */
398 if(e.nr == 1 && e.r[0] == 0x7F) {
400 fsprint(addrfd, "#%ud,#%ud", e.q0, e.q1);
401 fswrite(datafd, "", 0);
408 fprint(2, "shift typing %d... ", e.q1-e.q0);
411 else if(e.q0 <= q.p+ntyper){
413 fprint(2, "type... ");
414 type(&e, fd0, afd, dfd);
421 if(israw(fd0) && e.q1 >= q.p+n)
433 if(e.flag&1 || (e.c2=='x' && e.nr==0 && e2.nr==0)){
434 /* send it straight back */
435 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
438 if(e.q0==e.q1 && (e.flag&2)){
443 snprint(buf, sizeof buf, "%.*S", e.nr, e.r);
444 if(cistrcmp(buf, "cook") == 0) {
448 if(cistrcmp(buf, "nocook") == 0) {
454 sende(&e, fd0, cfd, afd, dfd, 0);
455 sende(&blank, fd0, cfd, afd, dfd, 0);
457 sende(&e3, fd0, cfd, afd, dfd, 1);
458 }else if(e.q1 != e.q0)
459 sende(&e, fd0, cfd, afd, dfd, 1);
464 /* just send it back */
467 fsfidprint(efd, "%c%c%d %d\n", e.c1, e.c2, e.q0, e.q1);
483 dropcr(char *p, int n)
497 while(i<n-1 && *(r+1) == '\r') {
501 if(i<n && *(r+1) != '\n') {
503 while(q>p && *(q-1) != '\n')
530 char x[16], hold[UTFmax];
533 buf = malloc(8192+UTFmax+1);
536 /* Let typing have a go -- maybe there's a rubout waiting. */
538 n = read(fd1, buf+npart, 8192);
542 n = echocancel(buf+npart, n);
546 n = dropcrnl(buf+npart, n);
550 n = dropcr(buf+npart, n);
555 s = memchr(buf+npart, 0, n);
557 for(t=s; s<buf+npart+n; s++)
558 if(*t = *s) /* assign = */
565 /* hold on to final partial rune */
567 while(n>0 && (buf[n-1]&0xC0)){
570 if((buf[n]&0xC0)!=0x80){
571 if(fullrune(buf+n, npart)){
572 w = chartorune(&r, buf+n);
580 memmove(hold, buf+n, npart);
585 // clumsy but effective: notice password
586 // prompts so we can disable echo.
588 if(cistrstr(buf, "password") || cistrstr(buf, "passphrase")) {
592 while(i > 0 && buf[i-1] == ' ')
594 password = i > 0 && buf[i-1] == ':';
598 m = sprint(x, "#%d", q.p);
599 if(fswrite(afd, x, m) != m){
600 fprint(2, "stdout writing address %s: %r; resetting\n", x);
601 if(fswrite(afd, "$", 1) < 0)
602 fprint(2, "reset: %r\n");
604 m = fsread(afd, x, sizeof x-1);
610 if(fswrite(dfd, buf, n) != n)
611 error("stdout writing body");
612 /* Make sure acme scrolls to the end of the above write. */
613 if(fswrite(dfd, nil, 0) != 0)
614 error("stdout flushing body");
615 q.p += nrunes(buf, n);
617 memmove(buf, hold, npart);
624 label(char *sr, int n)
626 char *sl, *el, *er, *r, *p;
629 for(r=er-1; r>=sr; r--)
636 if(el-sr > sizeof wdir - strlen(name) - 20)
637 sr = el - (sizeof wdir - strlen(name) - 20);
638 for(sl=el-3; sl>=sr; sl--)
639 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
645 if(strcmp(sl+3, "*9term-hold+") != 0) {
647 * add /-sysname if not present
649 snprint(wdir, sizeof wdir, "name %s", sl+3);
650 p = strrchr(wdir, '/');
651 if(p==nil || *(p+1) != '-'){
652 p = wdir+strlen(wdir);
658 strcat(wdir, "\n0\n");
659 fswrite(ctlfd, wdir, strlen(wdir));
662 memmove(sl, el, er-el);
694 addtype(int c, uint p0, char *b, int nb, int nr)
701 for(i=0; i<nb; i+=w){
702 w = chartorune(&r, b+i);
703 if((r==0x7F||r==3) && c=='K'){
704 write(rcfd, "\x7F", 1);
705 /* toss all typing */
710 /* buglet: more than one delete ignored */
713 if(r=='\n' || r==0x04)
716 typing = realloc(typing, ntypeb+nb);
720 memmove(typing+ntypeb, b, nb);
723 for(p=0; p<p0 && b0<typing+ntypeb; p++){
724 w = chartorune(&r, b0+i);
728 error("typing: findrune");
729 memmove(b0+nb, b0, (typing+ntypeb)-b0);
739 return (!cook || password) && !isecho(fd0);
748 while(ntypebreak || (raw && ntypeb > 0)){
749 for(i=0; i<ntypeb; i++)
750 if(typing[i]=='\n' || typing[i]==0x04 || (i==ntypeb-1 && raw)){
751 if((typing[i] == '\n' || typing[i] == 0x04) && ntypebreak > 0)
757 if(write(fd0, typing, n) != n)
758 error("sending to program");
759 nr = nrunes(typing, i);
763 memmove(typing, typing+i, ntypeb);
766 print("no breakchar\n");
773 sendbs(int fd0, int n)
778 memset(buf, 0x08, sizeof buf);
789 deltype(uint p0, uint p1)
797 for(p=0; p<p0 && b0<ntypeb; p++){
798 w = chartorune(&r, typing+b0);
805 for(; p<p1 && b1<ntypeb; p++){
806 w = chartorune(&r, typing+b1);
808 if(r=='\n' || r==0x04)
813 memmove(typing+b0, typing+b1, ntypeb-b1);
819 type(Event *e, int fd0, CFid *afd, CFid *dfd)
825 addtype(e->c1, e->q0-q.p, e->b, e->nb, e->nr);
829 n = sprint(buf, "#%d", m);
830 fswrite(afd, buf, n);
831 n = fsread(dfd, buf, sizeof buf);
834 do; while(n>0 && (buf[--n]&0xC0)==0x80);
839 addtype(e->c1, m-q.p, buf, n, nr);
844 n = sprint(buf, "#%d,#%d", e->q0, e->q1);
845 fswrite(afd, buf, n);
847 q.p -= e->q1 - e->q0;
850 if(e->nb > 0 && e->b[e->nb-1] == '\n')
855 sende(Event *e, int fd0, CFid *cfd, CFid *afd, CFid *dfd, int donl)
857 int l, m, n, nr, lastc, end;
858 char abuf[16], buf[128];
861 l = sprint(abuf, "#%d", end);
862 fswrite(afd, abuf, l);
864 fswrite(dfd, e->b, e->nb);
865 addtype(e->c1, ntyper, e->b, e->nb, e->nr);
866 lastc = e->r[e->nr-1];
871 n = sprint(buf, "#%d", m);
872 fswrite(afd, buf, n);
873 n = fsread(dfd, buf, sizeof buf);
876 do; while(n>0 && (buf[--n]&0xC0)==0x80);
881 l = sprint(abuf, "#%d", end);
882 fswrite(afd, abuf, l);
883 fswrite(dfd, buf, n);
884 addtype(e->c1, ntyper, buf, n, nr);
890 if(donl && lastc!='\n'){
891 fswrite(dfd, "\n", 1);
892 addtype(e->c1, ntyper, "\n", 1, 1);
894 fswrite(cfd, "dot=addr", 8);