14 #define Tversion Tversion9p
15 #define Twrite Twrite9p
31 fprint(2, "usage: samterm -a -W winsize\n");
32 threadexitsall("usage");
36 getscreen(int argc, char **argv)
45 winsize = EARGF(usage());
51 if(initdraw(panic1, nil, "sam") < 0){
52 fprint(2, "samterm: initdraw: %r\n");
53 threadexitsall("init");
55 t = getenv("tabstop");
57 maxtab = strtoul(t, nil, 0);
60 draw(screen, screen->clipr, display->white, nil, ZP);
64 screensize(int *w, int *h)
69 fd = open("/dev/screen", OREAD);
72 n = read(fd, buf, sizeof(buf)-1);
74 if (n != sizeof(buf)-1)
78 *h = atoi(buf+4*12)-atoi(buf+2*12);
83 *w = atoi(buf+3*12)-atoi(buf+1*12);
91 snarfswap(char *fromsam, int nc, char **tosam)
98 return s ? strlen(s) : 0;
102 dumperrmsg(int count, int type, int count0, int c)
104 fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
105 count, type, count0, c, rcvstring());
126 fd = (int)(uintptr)arg[1];
130 i = 1-i; /* toggle */
131 n = read(fd, plumbbuf[i].data, sizeof plumbbuf[i].data);
132 if(0) fprint(2, "ext %d\n", n);
134 fprint(2, "samterm: extern read error: %r\n");
135 threadexits("extern"); /* not a fatal error */
150 user = getenv("USER");
153 disp = getenv("DISPLAY");
155 exname = smprint("/tmp/.sam.%s.%s", user, disp);
159 exname = smprint("/tmp/.sam.%s", user);
162 fprint(2, "not posting for B: out of memory\n");
166 if(mkfifo(exname, 0600) < 0){
168 if(errno != EEXIST || stat(exname, &st) < 0)
170 if(!S_ISFIFO(st.st_mode)){
172 if(mkfifo(exname, 0600) < 0)
177 fd = open(exname, OREAD|ONONBLOCK);
184 * Turn off no-delay and provide ourselves as a lingering
185 * writer so as not to get end of file on read.
187 flags = fcntl(fd, F_GETFL, 0);
188 if(flags<0 || fcntl(fd, F_SETFL, flags&~O_NONBLOCK)<0
189 ||open(exname, OWRITE) < 0){
195 plumbc = chancreate(sizeof(int), 0);
196 chansetname(plumbc, "plumbc");
198 arg[1] = (void*)(uintptr)fd;
199 proccreate(extproc, arg, STACK);
200 atexit(removeextern);
204 plumbformat(Plumbmsg *m, int i)
206 char *addr, *data, *act;
209 data = (char*)plumbbuf[i].data;
211 if(n == 0 || 2+n+2 >= READBUFSIZE){
215 act = plumblookup(m->attr, "action");
216 if(act!=nil && strcmp(act, "showfile")!=0){
217 /* can't handle other cases yet */
221 addr = plumblookup(m->attr, "addr");
226 addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
228 memmove(data, "B ", 2); /* we know there's enough room for this */
229 memmove(data+2, m->data, n);
231 if(data[n-1] != '\n')
234 if(n+strlen(addr)+1+1 <= READBUFSIZE)
235 n += sprint(data+n, "%s\n", addr);
253 m = plumbrecvfid(fid);
255 fprint(2, "samterm: plumb read error: %r\n");
256 threadexits("plumb"); /* not a fatal error */
258 if(plumbformat(m, i)){
260 i = 1-i; /* toggle */
270 plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
271 fid = plumbopenfid("edit", OREAD|OCEXEC);
274 plumbc = chancreate(sizeof(int), 0);
275 chansetname(plumbc, "plumbc");
280 threadcreate(plumbproc, fid, STACK);
294 i = 1-i; /* toggle */
295 n = read(hostfd[0], hostbuf[i].data, sizeof hostbuf[i].data);
296 if(0) fprint(2, "hostproc %d\n", n);
301 werrstr("unexpected eof");
303 fprint(2, "samterm: host read error: %r\n");
304 threadexitsall("host");
308 if(0) fprint(2, "hostproc send %d\n", which);
316 hostc = chancreate(sizeof(int), 0);
317 chansetname(hostc, "hostc");
318 proccreate(hostproc, hostc, STACK);