1 941c9f36 2003-10-11 devnull #include <sys/types.h>
2 941c9f36 2003-10-11 devnull #include <sys/stat.h>
3 941c9f36 2003-10-11 devnull #include <errno.h>
5 84b1cb73 2003-09-30 devnull #include <u.h>
6 84b1cb73 2003-09-30 devnull #include <libc.h>
7 84b1cb73 2003-09-30 devnull #include <draw.h>
8 84b1cb73 2003-09-30 devnull #include <thread.h>
9 84b1cb73 2003-09-30 devnull #include <mouse.h>
10 84b1cb73 2003-09-30 devnull #include <cursor.h>
11 84b1cb73 2003-09-30 devnull #include <keyboard.h>
12 84b1cb73 2003-09-30 devnull #include <frame.h>
13 84b1cb73 2003-09-30 devnull #include "flayer.h"
14 84b1cb73 2003-09-30 devnull #include "samterm.h"
16 941c9f36 2003-10-11 devnull static char *exname;
18 64bcfff3 2003-11-25 devnull #define STACK 16384
21 84b1cb73 2003-09-30 devnull getscreen(int argc, char **argv)
25 84b1cb73 2003-09-30 devnull USED(argc);
26 84b1cb73 2003-09-30 devnull USED(argv);
27 84b1cb73 2003-09-30 devnull if(initdraw(panic1, nil, "sam") < 0){
28 941c9f36 2003-10-11 devnull fprint(2, "samterm: initdraw: %r\n");
29 84b1cb73 2003-09-30 devnull threadexitsall("init");
31 84b1cb73 2003-09-30 devnull t = getenv("tabstop");
32 84b1cb73 2003-09-30 devnull if(t != nil)
33 84b1cb73 2003-09-30 devnull maxtab = strtoul(t, nil, 0);
34 84b1cb73 2003-09-30 devnull draw(screen, screen->clipr, display->white, nil, ZP);
38 84b1cb73 2003-09-30 devnull screensize(int *w, int *h)
40 84b1cb73 2003-09-30 devnull int fd, n;
41 84b1cb73 2003-09-30 devnull char buf[5*12+1];
43 84b1cb73 2003-09-30 devnull fd = open("/dev/screen", OREAD);
44 84b1cb73 2003-09-30 devnull if(fd < 0)
45 84b1cb73 2003-09-30 devnull return 0;
46 84b1cb73 2003-09-30 devnull n = read(fd, buf, sizeof(buf)-1);
47 84b1cb73 2003-09-30 devnull close(fd);
48 84b1cb73 2003-09-30 devnull if (n != sizeof(buf)-1)
49 84b1cb73 2003-09-30 devnull return 0;
50 84b1cb73 2003-09-30 devnull buf[n] = 0;
52 84b1cb73 2003-09-30 devnull *h = atoi(buf+4*12)-atoi(buf+2*12);
53 84b1cb73 2003-09-30 devnull if (*h < 0)
54 84b1cb73 2003-09-30 devnull return 0;
57 84b1cb73 2003-09-30 devnull *w = atoi(buf+3*12)-atoi(buf+1*12);
58 84b1cb73 2003-09-30 devnull if (*w < 0)
59 84b1cb73 2003-09-30 devnull return 0;
61 84b1cb73 2003-09-30 devnull return 1;
65 84b1cb73 2003-09-30 devnull snarfswap(char *fromsam, int nc, char **tosam)
69 941c9f36 2003-10-11 devnull s = getsnarf();
70 941c9f36 2003-10-11 devnull putsnarf(fromsam);
71 941c9f36 2003-10-11 devnull *tosam = s;
72 941c9f36 2003-10-11 devnull return s ? strlen(s) : 0;
76 84b1cb73 2003-09-30 devnull dumperrmsg(int count, int type, int count0, int c)
78 84b1cb73 2003-09-30 devnull fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
79 84b1cb73 2003-09-30 devnull count, type, count0, c, rcvstring());
83 84b1cb73 2003-09-30 devnull removeextern(void)
85 84b1cb73 2003-09-30 devnull remove(exname);
88 84b1cb73 2003-09-30 devnull Readbuf hostbuf[2];
89 84b1cb73 2003-09-30 devnull Readbuf plumbbuf[2];
92 84b1cb73 2003-09-30 devnull extproc(void *argv)
94 84b1cb73 2003-09-30 devnull Channel *c;
95 941c9f36 2003-10-11 devnull int i, n, which, fd;
96 84b1cb73 2003-09-30 devnull void **arg;
98 84b1cb73 2003-09-30 devnull arg = argv;
99 84b1cb73 2003-09-30 devnull c = arg[0];
100 941c9f36 2003-10-11 devnull fd = (int)arg[1];
103 84b1cb73 2003-09-30 devnull for(;;){
104 941c9f36 2003-10-11 devnull i = 1-i; /* toggle */
105 941c9f36 2003-10-11 devnull n = read(fd, plumbbuf[i].data, sizeof plumbbuf[i].data);
106 84b1cb73 2003-09-30 devnull if(n <= 0){
107 84b1cb73 2003-09-30 devnull fprint(2, "samterm: extern read error: %r\n");
108 941c9f36 2003-10-11 devnull threadexits("extern"); /* not a fatal error */
110 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
111 84b1cb73 2003-09-30 devnull which = i;
112 84b1cb73 2003-09-30 devnull send(c, &which);
117 84b1cb73 2003-09-30 devnull extstart(void)
119 941c9f36 2003-10-11 devnull char *user, *disp;
120 941c9f36 2003-10-11 devnull int fd, flags;
121 84b1cb73 2003-09-30 devnull static void *arg[2];
123 941c9f36 2003-10-11 devnull user = getenv("USER");
124 941c9f36 2003-10-11 devnull if(user == nil)
126 941c9f36 2003-10-11 devnull disp = getenv("DISPLAY");
127 941c9f36 2003-10-11 devnull if(disp)
128 941c9f36 2003-10-11 devnull exname = smprint("/tmp/.sam.%s.%s", user, disp);
130 941c9f36 2003-10-11 devnull exname = smprint("/tmp/.sam.%s", user);
131 941c9f36 2003-10-11 devnull if(exname == nil){
132 941c9f36 2003-10-11 devnull fprint(2, "not posting for B: out of memory\n");
136 941c9f36 2003-10-11 devnull if(mkfifo(exname, 0600) < 0){
137 941c9f36 2003-10-11 devnull struct stat st;
138 941c9f36 2003-10-11 devnull if(errno != EEXIST || stat(exname, &st) < 0)
140 941c9f36 2003-10-11 devnull if(!S_ISFIFO(st.st_mode)){
141 941c9f36 2003-10-11 devnull removeextern();
142 941c9f36 2003-10-11 devnull if(mkfifo(exname, 0600) < 0)
147 941c9f36 2003-10-11 devnull fd = open(exname, OREAD|O_NONBLOCK);
148 941c9f36 2003-10-11 devnull if(fd == -1){
149 941c9f36 2003-10-11 devnull removeextern();
154 941c9f36 2003-10-11 devnull * Turn off no-delay and provide ourselves as a lingering
155 941c9f36 2003-10-11 devnull * writer so as not to get end of file on read.
157 941c9f36 2003-10-11 devnull flags = fcntl(fd, F_GETFL, 0);
158 941c9f36 2003-10-11 devnull if(flags<0 || fcntl(fd, F_SETFL, flags&~O_NONBLOCK)<0
159 941c9f36 2003-10-11 devnull ||open(exname, OWRITE) < 0){
160 941c9f36 2003-10-11 devnull close(fd);
161 941c9f36 2003-10-11 devnull removeextern();
165 84b1cb73 2003-09-30 devnull plumbc = chancreate(sizeof(int), 0);
166 84b1cb73 2003-09-30 devnull arg[0] = plumbc;
167 941c9f36 2003-10-11 devnull arg[1] = (void*)fd;
168 912fba95 2003-11-24 devnull proccreate(extproc, arg, STACK);
169 84b1cb73 2003-09-30 devnull atexit(removeextern);
174 84b1cb73 2003-09-30 devnull plumbformat(int i)
176 84b1cb73 2003-09-30 devnull Plumbmsg *m;
177 84b1cb73 2003-09-30 devnull char *addr, *data, *act;
180 84b1cb73 2003-09-30 devnull data = (char*)plumbbuf[i].data;
181 84b1cb73 2003-09-30 devnull m = plumbunpack(data, plumbbuf[i].n);
182 84b1cb73 2003-09-30 devnull if(m == nil)
183 84b1cb73 2003-09-30 devnull return 0;
184 84b1cb73 2003-09-30 devnull n = m->ndata;
185 84b1cb73 2003-09-30 devnull if(n == 0){
186 84b1cb73 2003-09-30 devnull plumbfree(m);
187 84b1cb73 2003-09-30 devnull return 0;
189 84b1cb73 2003-09-30 devnull act = plumblookup(m->attr, "action");
190 84b1cb73 2003-09-30 devnull if(act!=nil && strcmp(act, "showfile")!=0){
191 84b1cb73 2003-09-30 devnull /* can't handle other cases yet */
192 84b1cb73 2003-09-30 devnull plumbfree(m);
193 84b1cb73 2003-09-30 devnull return 0;
195 84b1cb73 2003-09-30 devnull addr = plumblookup(m->attr, "addr");
196 84b1cb73 2003-09-30 devnull if(addr){
197 84b1cb73 2003-09-30 devnull if(addr[0] == '\0')
198 84b1cb73 2003-09-30 devnull addr = nil;
200 84b1cb73 2003-09-30 devnull addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
202 84b1cb73 2003-09-30 devnull memmove(data, "B ", 2); /* we know there's enough room for this */
203 84b1cb73 2003-09-30 devnull memmove(data+2, m->data, n);
205 84b1cb73 2003-09-30 devnull if(data[n-1] != '\n')
206 84b1cb73 2003-09-30 devnull data[n++] = '\n';
207 84b1cb73 2003-09-30 devnull if(addr != nil){
208 84b1cb73 2003-09-30 devnull if(n+strlen(addr)+1+1 <= READBUFSIZE)
209 84b1cb73 2003-09-30 devnull n += sprint(data+n, "%s\n", addr);
210 84b1cb73 2003-09-30 devnull free(addr);
212 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
213 84b1cb73 2003-09-30 devnull plumbfree(m);
214 84b1cb73 2003-09-30 devnull return 1;
218 84b1cb73 2003-09-30 devnull plumbproc(void *argv)
220 84b1cb73 2003-09-30 devnull Channel *c;
221 84b1cb73 2003-09-30 devnull int i, n, which, *fdp;
222 84b1cb73 2003-09-30 devnull void **arg;
224 84b1cb73 2003-09-30 devnull arg = argv;
225 84b1cb73 2003-09-30 devnull c = arg[0];
226 84b1cb73 2003-09-30 devnull fdp = arg[1];
229 84b1cb73 2003-09-30 devnull for(;;){
230 84b1cb73 2003-09-30 devnull i = 1-i; /* toggle */
231 84b1cb73 2003-09-30 devnull n = read(*fdp, plumbbuf[i].data, READBUFSIZE);
232 84b1cb73 2003-09-30 devnull if(n <= 0){
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 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
237 84b1cb73 2003-09-30 devnull if(plumbformat(i)){
238 84b1cb73 2003-09-30 devnull which = i;
239 84b1cb73 2003-09-30 devnull send(c, &which);
245 84b1cb73 2003-09-30 devnull plumbstart(void)
247 84b1cb73 2003-09-30 devnull static int fd;
248 84b1cb73 2003-09-30 devnull static void *arg[2];
250 84b1cb73 2003-09-30 devnull plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
251 84b1cb73 2003-09-30 devnull fd = plumbopen("edit", OREAD|OCEXEC);
252 84b1cb73 2003-09-30 devnull if(fd < 0)
253 84b1cb73 2003-09-30 devnull return -1;
254 84b1cb73 2003-09-30 devnull plumbc = chancreate(sizeof(int), 0);
255 84b1cb73 2003-09-30 devnull if(plumbc == nil){
256 84b1cb73 2003-09-30 devnull close(fd);
257 84b1cb73 2003-09-30 devnull return -1;
259 84b1cb73 2003-09-30 devnull arg[0] =plumbc;
260 84b1cb73 2003-09-30 devnull arg[1] = &fd;
261 912fba95 2003-11-24 devnull proccreate(plumbproc, arg, STACK);
262 84b1cb73 2003-09-30 devnull return 1;
267 84b1cb73 2003-09-30 devnull plumbstart(void)
269 84b1cb73 2003-09-30 devnull return -1;
273 84b1cb73 2003-09-30 devnull hostproc(void *arg)
275 84b1cb73 2003-09-30 devnull Channel *c;
276 84b1cb73 2003-09-30 devnull int i, n, which;
278 84b1cb73 2003-09-30 devnull c = arg;
281 84b1cb73 2003-09-30 devnull for(;;){
282 84b1cb73 2003-09-30 devnull i = 1-i; /* toggle */
283 941c9f36 2003-10-11 devnull n = read(hostfd[0], hostbuf[i].data, sizeof hostbuf[i].data);
284 84b1cb73 2003-09-30 devnull if(n <= 0){
285 84b1cb73 2003-09-30 devnull fprint(2, "samterm: host read error: %r\n");
286 84b1cb73 2003-09-30 devnull threadexitsall("host");
288 84b1cb73 2003-09-30 devnull hostbuf[i].n = n;
289 84b1cb73 2003-09-30 devnull which = i;
290 84b1cb73 2003-09-30 devnull send(c, &which);
295 84b1cb73 2003-09-30 devnull hoststart(void)
297 84b1cb73 2003-09-30 devnull hostc = chancreate(sizeof(int), 0);
298 912fba95 2003-11-24 devnull proccreate(hostproc, hostc, STACK);