1 be22ae2d 2004-03-26 devnull #include <u.h>
2 941c9f36 2003-10-11 devnull #include <sys/types.h>
3 941c9f36 2003-10-11 devnull #include <sys/stat.h>
4 941c9f36 2003-10-11 devnull #include <errno.h>
6 84b1cb73 2003-09-30 devnull #include <u.h>
7 84b1cb73 2003-09-30 devnull #include <libc.h>
8 84b1cb73 2003-09-30 devnull #include <draw.h>
9 84b1cb73 2003-09-30 devnull #include <thread.h>
10 84b1cb73 2003-09-30 devnull #include <mouse.h>
11 84b1cb73 2003-09-30 devnull #include <cursor.h>
12 84b1cb73 2003-09-30 devnull #include <keyboard.h>
13 84b1cb73 2003-09-30 devnull #include <frame.h>
14 2277c5d7 2004-03-21 devnull #define Tversion Tversion9p
15 2277c5d7 2004-03-21 devnull #define Twrite Twrite9p
16 2277c5d7 2004-03-21 devnull #include <fcall.h>
17 2277c5d7 2004-03-21 devnull #undef Tversion
18 2277c5d7 2004-03-21 devnull #undef Twrite
19 2277c5d7 2004-03-21 devnull #include <fs.h>
20 af78a4cd 2004-03-02 devnull #include <plumb.h>
21 84b1cb73 2003-09-30 devnull #include "flayer.h"
22 84b1cb73 2003-09-30 devnull #include "samterm.h"
24 941c9f36 2003-10-11 devnull static char *exname;
26 64bcfff3 2003-11-25 devnull #define STACK 16384
29 84b1cb73 2003-09-30 devnull getscreen(int argc, char **argv)
33 84b1cb73 2003-09-30 devnull USED(argc);
34 84b1cb73 2003-09-30 devnull USED(argv);
35 84b1cb73 2003-09-30 devnull if(initdraw(panic1, nil, "sam") < 0){
36 941c9f36 2003-10-11 devnull fprint(2, "samterm: initdraw: %r\n");
37 84b1cb73 2003-09-30 devnull threadexitsall("init");
39 84b1cb73 2003-09-30 devnull t = getenv("tabstop");
40 84b1cb73 2003-09-30 devnull if(t != nil)
41 84b1cb73 2003-09-30 devnull maxtab = strtoul(t, nil, 0);
42 84b1cb73 2003-09-30 devnull draw(screen, screen->clipr, display->white, nil, ZP);
46 84b1cb73 2003-09-30 devnull screensize(int *w, int *h)
48 84b1cb73 2003-09-30 devnull int fd, n;
49 84b1cb73 2003-09-30 devnull char buf[5*12+1];
51 84b1cb73 2003-09-30 devnull fd = open("/dev/screen", OREAD);
52 84b1cb73 2003-09-30 devnull if(fd < 0)
53 84b1cb73 2003-09-30 devnull return 0;
54 84b1cb73 2003-09-30 devnull n = read(fd, buf, sizeof(buf)-1);
55 84b1cb73 2003-09-30 devnull close(fd);
56 84b1cb73 2003-09-30 devnull if (n != sizeof(buf)-1)
57 84b1cb73 2003-09-30 devnull return 0;
58 84b1cb73 2003-09-30 devnull buf[n] = 0;
60 84b1cb73 2003-09-30 devnull *h = atoi(buf+4*12)-atoi(buf+2*12);
61 84b1cb73 2003-09-30 devnull if (*h < 0)
62 84b1cb73 2003-09-30 devnull return 0;
65 84b1cb73 2003-09-30 devnull *w = atoi(buf+3*12)-atoi(buf+1*12);
66 84b1cb73 2003-09-30 devnull if (*w < 0)
67 84b1cb73 2003-09-30 devnull return 0;
69 84b1cb73 2003-09-30 devnull return 1;
73 84b1cb73 2003-09-30 devnull snarfswap(char *fromsam, int nc, char **tosam)
77 941c9f36 2003-10-11 devnull s = getsnarf();
78 941c9f36 2003-10-11 devnull putsnarf(fromsam);
79 941c9f36 2003-10-11 devnull *tosam = s;
80 941c9f36 2003-10-11 devnull return s ? strlen(s) : 0;
84 84b1cb73 2003-09-30 devnull dumperrmsg(int count, int type, int count0, int c)
86 84b1cb73 2003-09-30 devnull fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
87 84b1cb73 2003-09-30 devnull count, type, count0, c, rcvstring());
91 84b1cb73 2003-09-30 devnull removeextern(void)
93 84b1cb73 2003-09-30 devnull remove(exname);
96 84b1cb73 2003-09-30 devnull Readbuf hostbuf[2];
97 84b1cb73 2003-09-30 devnull Readbuf plumbbuf[2];
100 84b1cb73 2003-09-30 devnull extproc(void *argv)
102 84b1cb73 2003-09-30 devnull Channel *c;
103 941c9f36 2003-10-11 devnull int i, n, which, fd;
104 84b1cb73 2003-09-30 devnull void **arg;
106 84b1cb73 2003-09-30 devnull arg = argv;
107 84b1cb73 2003-09-30 devnull c = arg[0];
108 941c9f36 2003-10-11 devnull fd = (int)arg[1];
110 5a8e63b2 2004-02-29 devnull threadfdnoblock(fd);
112 84b1cb73 2003-09-30 devnull for(;;){
113 941c9f36 2003-10-11 devnull i = 1-i; /* toggle */
114 5a8e63b2 2004-02-29 devnull n = threadread(fd, plumbbuf[i].data, sizeof plumbbuf[i].data);
115 84b1cb73 2003-09-30 devnull if(n <= 0){
116 84b1cb73 2003-09-30 devnull fprint(2, "samterm: extern read error: %r\n");
117 941c9f36 2003-10-11 devnull threadexits("extern"); /* not a fatal error */
119 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
120 84b1cb73 2003-09-30 devnull which = i;
121 84b1cb73 2003-09-30 devnull send(c, &which);
126 84b1cb73 2003-09-30 devnull extstart(void)
128 941c9f36 2003-10-11 devnull char *user, *disp;
129 941c9f36 2003-10-11 devnull int fd, flags;
130 84b1cb73 2003-09-30 devnull static void *arg[2];
132 941c9f36 2003-10-11 devnull user = getenv("USER");
133 941c9f36 2003-10-11 devnull if(user == nil)
135 941c9f36 2003-10-11 devnull disp = getenv("DISPLAY");
136 941c9f36 2003-10-11 devnull if(disp)
137 941c9f36 2003-10-11 devnull exname = smprint("/tmp/.sam.%s.%s", user, disp);
139 941c9f36 2003-10-11 devnull exname = smprint("/tmp/.sam.%s", user);
140 941c9f36 2003-10-11 devnull if(exname == nil){
141 941c9f36 2003-10-11 devnull fprint(2, "not posting for B: out of memory\n");
145 941c9f36 2003-10-11 devnull if(mkfifo(exname, 0600) < 0){
146 941c9f36 2003-10-11 devnull struct stat st;
147 941c9f36 2003-10-11 devnull if(errno != EEXIST || stat(exname, &st) < 0)
149 941c9f36 2003-10-11 devnull if(!S_ISFIFO(st.st_mode)){
150 941c9f36 2003-10-11 devnull removeextern();
151 941c9f36 2003-10-11 devnull if(mkfifo(exname, 0600) < 0)
156 941c9f36 2003-10-11 devnull fd = open(exname, OREAD|O_NONBLOCK);
157 941c9f36 2003-10-11 devnull if(fd == -1){
158 941c9f36 2003-10-11 devnull removeextern();
163 941c9f36 2003-10-11 devnull * Turn off no-delay and provide ourselves as a lingering
164 941c9f36 2003-10-11 devnull * writer so as not to get end of file on read.
166 941c9f36 2003-10-11 devnull flags = fcntl(fd, F_GETFL, 0);
167 941c9f36 2003-10-11 devnull if(flags<0 || fcntl(fd, F_SETFL, flags&~O_NONBLOCK)<0
168 941c9f36 2003-10-11 devnull ||open(exname, OWRITE) < 0){
169 941c9f36 2003-10-11 devnull close(fd);
170 941c9f36 2003-10-11 devnull removeextern();
174 84b1cb73 2003-09-30 devnull plumbc = chancreate(sizeof(int), 0);
175 84b1cb73 2003-09-30 devnull arg[0] = plumbc;
176 941c9f36 2003-10-11 devnull arg[1] = (void*)fd;
177 5a8e63b2 2004-02-29 devnull threadcreate(extproc, arg, STACK);
178 84b1cb73 2003-09-30 devnull atexit(removeextern);
182 af78a4cd 2004-03-02 devnull plumbformat(Plumbmsg *m, int i)
184 84b1cb73 2003-09-30 devnull char *addr, *data, *act;
187 84b1cb73 2003-09-30 devnull data = (char*)plumbbuf[i].data;
188 84b1cb73 2003-09-30 devnull n = m->ndata;
189 af78a4cd 2004-03-02 devnull if(n == 0 || 2+n+2 >= READBUFSIZE){
190 84b1cb73 2003-09-30 devnull plumbfree(m);
191 84b1cb73 2003-09-30 devnull return 0;
193 84b1cb73 2003-09-30 devnull act = plumblookup(m->attr, "action");
194 84b1cb73 2003-09-30 devnull if(act!=nil && strcmp(act, "showfile")!=0){
195 84b1cb73 2003-09-30 devnull /* can't handle other cases yet */
196 84b1cb73 2003-09-30 devnull plumbfree(m);
197 84b1cb73 2003-09-30 devnull return 0;
199 84b1cb73 2003-09-30 devnull addr = plumblookup(m->attr, "addr");
200 84b1cb73 2003-09-30 devnull if(addr){
201 84b1cb73 2003-09-30 devnull if(addr[0] == '\0')
202 84b1cb73 2003-09-30 devnull addr = nil;
204 84b1cb73 2003-09-30 devnull addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
206 84b1cb73 2003-09-30 devnull memmove(data, "B ", 2); /* we know there's enough room for this */
207 84b1cb73 2003-09-30 devnull memmove(data+2, m->data, n);
209 84b1cb73 2003-09-30 devnull if(data[n-1] != '\n')
210 84b1cb73 2003-09-30 devnull data[n++] = '\n';
211 84b1cb73 2003-09-30 devnull if(addr != nil){
212 84b1cb73 2003-09-30 devnull if(n+strlen(addr)+1+1 <= READBUFSIZE)
213 84b1cb73 2003-09-30 devnull n += sprint(data+n, "%s\n", addr);
214 84b1cb73 2003-09-30 devnull free(addr);
216 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
217 84b1cb73 2003-09-30 devnull plumbfree(m);
218 84b1cb73 2003-09-30 devnull return 1;
222 2277c5d7 2004-03-21 devnull plumbproc(void *arg)
224 2277c5d7 2004-03-21 devnull Fid *fid;
226 af78a4cd 2004-03-02 devnull Plumbmsg *m;
228 2277c5d7 2004-03-21 devnull fid = arg;
230 84b1cb73 2003-09-30 devnull for(;;){
231 2277c5d7 2004-03-21 devnull m = plumbrecvfid(fid);
232 af78a4cd 2004-03-02 devnull if(m == nil){
233 84b1cb73 2003-09-30 devnull fprint(2, "samterm: plumb read error: %r\n");
234 84b1cb73 2003-09-30 devnull threadexits("plumb"); /* not a fatal error */
236 af78a4cd 2004-03-02 devnull if(plumbformat(m, i)){
237 2277c5d7 2004-03-21 devnull send(plumbc, &i);
238 af78a4cd 2004-03-02 devnull i = 1-i; /* toggle */
244 84b1cb73 2003-09-30 devnull plumbstart(void)
246 2277c5d7 2004-03-21 devnull Fid *fid;
248 84b1cb73 2003-09-30 devnull plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
249 2277c5d7 2004-03-21 devnull fid = plumbopenfid("edit", OREAD|OCEXEC);
250 2277c5d7 2004-03-21 devnull if(fid == nil)
251 84b1cb73 2003-09-30 devnull return -1;
252 84b1cb73 2003-09-30 devnull plumbc = chancreate(sizeof(int), 0);
253 84b1cb73 2003-09-30 devnull if(plumbc == nil){
254 2277c5d7 2004-03-21 devnull fsclose(fid);
255 84b1cb73 2003-09-30 devnull return -1;
257 2277c5d7 2004-03-21 devnull threadcreate(plumbproc, fid, STACK);
258 84b1cb73 2003-09-30 devnull return 1;
262 84b1cb73 2003-09-30 devnull hostproc(void *arg)
264 84b1cb73 2003-09-30 devnull Channel *c;
265 84b1cb73 2003-09-30 devnull int i, n, which;
267 84b1cb73 2003-09-30 devnull c = arg;
270 5a8e63b2 2004-02-29 devnull threadfdnoblock(hostfd[0]);
271 84b1cb73 2003-09-30 devnull for(;;){
272 84b1cb73 2003-09-30 devnull i = 1-i; /* toggle */
273 5a8e63b2 2004-02-29 devnull n = threadread(hostfd[0], hostbuf[i].data, sizeof hostbuf[i].data);
274 84b1cb73 2003-09-30 devnull if(n <= 0){
275 84b1cb73 2003-09-30 devnull fprint(2, "samterm: host read error: %r\n");
276 84b1cb73 2003-09-30 devnull threadexitsall("host");
278 84b1cb73 2003-09-30 devnull hostbuf[i].n = n;
279 84b1cb73 2003-09-30 devnull which = i;
280 84b1cb73 2003-09-30 devnull send(c, &which);
285 84b1cb73 2003-09-30 devnull hoststart(void)
287 84b1cb73 2003-09-30 devnull hostc = chancreate(sizeof(int), 0);
288 5a8e63b2 2004-02-29 devnull threadcreate(hostproc, hostc, STACK);