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);
58 draw(screen, screen->clipr, display->white, nil, ZP);
62 screensize(int *w, int *h)
67 fd = open("/dev/screen", OREAD);
70 n = read(fd, buf, sizeof(buf)-1);
72 if (n != sizeof(buf)-1)
76 *h = atoi(buf+4*12)-atoi(buf+2*12);
81 *w = atoi(buf+3*12)-atoi(buf+1*12);
89 snarfswap(char *fromsam, int nc, char **tosam)
96 return s ? strlen(s) : 0;
100 dumperrmsg(int count, int type, int count0, int c)
102 fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
103 count, type, count0, c, rcvstring());
128 i = 1-i; /* toggle */
129 n = read(fd, plumbbuf[i].data, sizeof plumbbuf[i].data);
130 if(0) fprint(2, "ext %d\n", n);
132 fprint(2, "samterm: extern read error: %r\n");
133 threadexits("extern"); /* not a fatal error */
148 user = getenv("USER");
151 disp = getenv("DISPLAY");
153 exname = smprint("/tmp/.sam.%s.%s", user, disp);
155 exname = smprint("/tmp/.sam.%s", user);
157 fprint(2, "not posting for B: out of memory\n");
161 if(mkfifo(exname, 0600) < 0){
163 if(errno != EEXIST || stat(exname, &st) < 0)
165 if(!S_ISFIFO(st.st_mode)){
167 if(mkfifo(exname, 0600) < 0)
172 fd = open(exname, OREAD|ONONBLOCK);
179 * Turn off no-delay and provide ourselves as a lingering
180 * writer so as not to get end of file on read.
182 flags = fcntl(fd, F_GETFL, 0);
183 if(flags<0 || fcntl(fd, F_SETFL, flags&~O_NONBLOCK)<0
184 ||open(exname, OWRITE) < 0){
190 plumbc = chancreate(sizeof(int), 0);
191 chansetname(plumbc, "plumbc");
194 proccreate(extproc, arg, STACK);
195 atexit(removeextern);
199 plumbformat(Plumbmsg *m, int i)
201 char *addr, *data, *act;
204 data = (char*)plumbbuf[i].data;
206 if(n == 0 || 2+n+2 >= READBUFSIZE){
210 act = plumblookup(m->attr, "action");
211 if(act!=nil && strcmp(act, "showfile")!=0){
212 /* can't handle other cases yet */
216 addr = plumblookup(m->attr, "addr");
221 addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
223 memmove(data, "B ", 2); /* we know there's enough room for this */
224 memmove(data+2, m->data, n);
226 if(data[n-1] != '\n')
229 if(n+strlen(addr)+1+1 <= READBUFSIZE)
230 n += sprint(data+n, "%s\n", addr);
248 m = plumbrecvfid(fid);
250 fprint(2, "samterm: plumb read error: %r\n");
251 threadexits("plumb"); /* not a fatal error */
253 if(plumbformat(m, i)){
255 i = 1-i; /* toggle */
265 plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
266 fid = plumbopenfid("edit", OREAD|OCEXEC);
269 plumbc = chancreate(sizeof(int), 0);
270 chansetname(plumbc, "plumbc");
275 threadcreate(plumbproc, fid, STACK);
289 i = 1-i; /* toggle */
290 n = read(hostfd[0], hostbuf[i].data, sizeof hostbuf[i].data);
291 if(0) fprint(2, "hostproc %d\n", n);
296 werrstr("unexpected eof");
298 fprint(2, "samterm: host read error: %r\n");
299 threadexitsall("host");
303 if(0) fprint(2, "hostproc send %d\n", which);
311 hostc = chancreate(sizeof(int), 0);
312 chansetname(hostc, "hostc");
313 proccreate(hostproc, hostc, STACK);