1 84b1cb73 2003-09-30 devnull #include <u.h>
2 84b1cb73 2003-09-30 devnull #include <libc.h>
3 84b1cb73 2003-09-30 devnull #include <draw.h>
4 84b1cb73 2003-09-30 devnull #include <thread.h>
5 84b1cb73 2003-09-30 devnull #include <mouse.h>
6 84b1cb73 2003-09-30 devnull #include <cursor.h>
7 84b1cb73 2003-09-30 devnull #include <keyboard.h>
8 84b1cb73 2003-09-30 devnull #include <frame.h>
9 84b1cb73 2003-09-30 devnull #include "flayer.h"
10 84b1cb73 2003-09-30 devnull #include "samterm.h"
12 84b1cb73 2003-09-30 devnull static char exname[64];
15 84b1cb73 2003-09-30 devnull getscreen(int argc, char **argv)
19 84b1cb73 2003-09-30 devnull USED(argc);
20 84b1cb73 2003-09-30 devnull USED(argv);
21 84b1cb73 2003-09-30 devnull if(initdraw(panic1, nil, "sam") < 0){
22 84b1cb73 2003-09-30 devnull fprint(2, "samterm: initimage: %r\n");
23 84b1cb73 2003-09-30 devnull threadexitsall("init");
25 84b1cb73 2003-09-30 devnull t = getenv("tabstop");
26 84b1cb73 2003-09-30 devnull if(t != nil)
27 84b1cb73 2003-09-30 devnull maxtab = strtoul(t, nil, 0);
28 84b1cb73 2003-09-30 devnull draw(screen, screen->clipr, display->white, nil, ZP);
32 84b1cb73 2003-09-30 devnull screensize(int *w, int *h)
34 84b1cb73 2003-09-30 devnull int fd, n;
35 84b1cb73 2003-09-30 devnull char buf[5*12+1];
37 84b1cb73 2003-09-30 devnull fd = open("/dev/screen", OREAD);
38 84b1cb73 2003-09-30 devnull if(fd < 0)
39 84b1cb73 2003-09-30 devnull return 0;
40 84b1cb73 2003-09-30 devnull n = read(fd, buf, sizeof(buf)-1);
41 84b1cb73 2003-09-30 devnull close(fd);
42 84b1cb73 2003-09-30 devnull if (n != sizeof(buf)-1)
43 84b1cb73 2003-09-30 devnull return 0;
44 84b1cb73 2003-09-30 devnull buf[n] = 0;
46 84b1cb73 2003-09-30 devnull *h = atoi(buf+4*12)-atoi(buf+2*12);
47 84b1cb73 2003-09-30 devnull if (*h < 0)
48 84b1cb73 2003-09-30 devnull return 0;
51 84b1cb73 2003-09-30 devnull *w = atoi(buf+3*12)-atoi(buf+1*12);
52 84b1cb73 2003-09-30 devnull if (*w < 0)
53 84b1cb73 2003-09-30 devnull return 0;
55 84b1cb73 2003-09-30 devnull return 1;
59 84b1cb73 2003-09-30 devnull snarfswap(char *fromsam, int nc, char **tosam)
61 84b1cb73 2003-09-30 devnull char *s1;
62 84b1cb73 2003-09-30 devnull int f, n, ss;
64 84b1cb73 2003-09-30 devnull f = open("/dev/snarf", 0);
65 84b1cb73 2003-09-30 devnull if(f < 0)
66 84b1cb73 2003-09-30 devnull return -1;
67 84b1cb73 2003-09-30 devnull ss = SNARFSIZE;
68 84b1cb73 2003-09-30 devnull if(hversion < 2)
69 84b1cb73 2003-09-30 devnull ss = 4096;
70 84b1cb73 2003-09-30 devnull *tosam = s1 = alloc(ss);
71 84b1cb73 2003-09-30 devnull n = read(f, s1, ss-1);
72 84b1cb73 2003-09-30 devnull close(f);
73 84b1cb73 2003-09-30 devnull if(n < 0)
75 84b1cb73 2003-09-30 devnull if (n == 0) {
76 84b1cb73 2003-09-30 devnull *tosam = 0;
77 84b1cb73 2003-09-30 devnull free(s1);
79 84b1cb73 2003-09-30 devnull s1[n] = 0;
81 84b1cb73 2003-09-30 devnull f = create("/dev/snarf", 1, 0666);
82 84b1cb73 2003-09-30 devnull if(f >= 0){
83 84b1cb73 2003-09-30 devnull write(f, fromsam, nc);
84 84b1cb73 2003-09-30 devnull close(f);
87 84b1cb73 2003-09-30 devnull return n;
91 84b1cb73 2003-09-30 devnull dumperrmsg(int count, int type, int count0, int c)
93 84b1cb73 2003-09-30 devnull fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
94 84b1cb73 2003-09-30 devnull count, type, count0, c, rcvstring());
98 84b1cb73 2003-09-30 devnull removeextern(void)
100 84b1cb73 2003-09-30 devnull remove(exname);
103 84b1cb73 2003-09-30 devnull Readbuf hostbuf[2];
105 84b1cb73 2003-09-30 devnull Readbuf plumbbuf[2];
108 84b1cb73 2003-09-30 devnull extproc(void *argv)
110 84b1cb73 2003-09-30 devnull Channel *c;
111 84b1cb73 2003-09-30 devnull int i, n, which, *fdp;
112 84b1cb73 2003-09-30 devnull void **arg;
114 84b1cb73 2003-09-30 devnull arg = argv;
115 84b1cb73 2003-09-30 devnull c = arg[0];
116 84b1cb73 2003-09-30 devnull fdp = arg[1];
119 84b1cb73 2003-09-30 devnull for(;;){
120 84b1cb73 2003-09-30 devnull i = 1-i; / * toggle * /
121 84b1cb73 2003-09-30 devnull n = read(*fdp, plumbbuf[i].data, sizeof plumbbuf[i].data);
122 84b1cb73 2003-09-30 devnull if(n <= 0){
123 84b1cb73 2003-09-30 devnull fprint(2, "samterm: extern read error: %r\n");
124 84b1cb73 2003-09-30 devnull threadexits("extern"); / * not a fatal error * /
126 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
127 84b1cb73 2003-09-30 devnull which = i;
128 84b1cb73 2003-09-30 devnull send(c, &which);
134 84b1cb73 2003-09-30 devnull extstart(void)
136 84b1cb73 2003-09-30 devnull char buf[32];
138 84b1cb73 2003-09-30 devnull static int p[2];
139 84b1cb73 2003-09-30 devnull static void *arg[2];
142 84b1cb73 2003-09-30 devnull if(pipe(p) < 0)
144 84b1cb73 2003-09-30 devnull sprint(exname, "/srv/sam.%s", "rsc"/*getuser()*/);
145 84b1cb73 2003-09-30 devnull fd = open(exname, 1, 0600);/* BUG was create */
146 84b1cb73 2003-09-30 devnull if(fd < 0){ /* assume existing guy is more important */
148 84b1cb73 2003-09-30 devnull close(p[0]);
149 84b1cb73 2003-09-30 devnull close(p[1]);
152 84b1cb73 2003-09-30 devnull sprint(buf, "%d", p[0]);
153 84b1cb73 2003-09-30 devnull if(write(fd, buf, strlen(buf)) <= 0)
154 84b1cb73 2003-09-30 devnull goto Err;
155 84b1cb73 2003-09-30 devnull close(fd);
157 84b1cb73 2003-09-30 devnull * leave p[0] open so if the file is removed the event
158 84b1cb73 2003-09-30 devnull * library won't get an error
161 84b1cb73 2003-09-30 devnull plumbc = chancreate(sizeof(int), 0);
162 84b1cb73 2003-09-30 devnull arg[0] = plumbc;
163 84b1cb73 2003-09-30 devnull arg[1] = &p[1];
164 84b1cb73 2003-09-30 devnull proccreate(extproc, arg, 1024);
166 84b1cb73 2003-09-30 devnull atexit(removeextern);
171 84b1cb73 2003-09-30 devnull plumbformat(int i)
173 84b1cb73 2003-09-30 devnull Plumbmsg *m;
174 84b1cb73 2003-09-30 devnull char *addr, *data, *act;
177 84b1cb73 2003-09-30 devnull data = (char*)plumbbuf[i].data;
178 84b1cb73 2003-09-30 devnull m = plumbunpack(data, plumbbuf[i].n);
179 84b1cb73 2003-09-30 devnull if(m == nil)
180 84b1cb73 2003-09-30 devnull return 0;
181 84b1cb73 2003-09-30 devnull n = m->ndata;
182 84b1cb73 2003-09-30 devnull if(n == 0){
183 84b1cb73 2003-09-30 devnull plumbfree(m);
184 84b1cb73 2003-09-30 devnull return 0;
186 84b1cb73 2003-09-30 devnull act = plumblookup(m->attr, "action");
187 84b1cb73 2003-09-30 devnull if(act!=nil && strcmp(act, "showfile")!=0){
188 84b1cb73 2003-09-30 devnull /* can't handle other cases yet */
189 84b1cb73 2003-09-30 devnull plumbfree(m);
190 84b1cb73 2003-09-30 devnull return 0;
192 84b1cb73 2003-09-30 devnull addr = plumblookup(m->attr, "addr");
193 84b1cb73 2003-09-30 devnull if(addr){
194 84b1cb73 2003-09-30 devnull if(addr[0] == '\0')
195 84b1cb73 2003-09-30 devnull addr = nil;
197 84b1cb73 2003-09-30 devnull addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
199 84b1cb73 2003-09-30 devnull memmove(data, "B ", 2); /* we know there's enough room for this */
200 84b1cb73 2003-09-30 devnull memmove(data+2, m->data, n);
202 84b1cb73 2003-09-30 devnull if(data[n-1] != '\n')
203 84b1cb73 2003-09-30 devnull data[n++] = '\n';
204 84b1cb73 2003-09-30 devnull if(addr != nil){
205 84b1cb73 2003-09-30 devnull if(n+strlen(addr)+1+1 <= READBUFSIZE)
206 84b1cb73 2003-09-30 devnull n += sprint(data+n, "%s\n", addr);
207 84b1cb73 2003-09-30 devnull free(addr);
209 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
210 84b1cb73 2003-09-30 devnull plumbfree(m);
211 84b1cb73 2003-09-30 devnull return 1;
215 84b1cb73 2003-09-30 devnull plumbproc(void *argv)
217 84b1cb73 2003-09-30 devnull Channel *c;
218 84b1cb73 2003-09-30 devnull int i, n, which, *fdp;
219 84b1cb73 2003-09-30 devnull void **arg;
221 84b1cb73 2003-09-30 devnull arg = argv;
222 84b1cb73 2003-09-30 devnull c = arg[0];
223 84b1cb73 2003-09-30 devnull fdp = arg[1];
226 84b1cb73 2003-09-30 devnull for(;;){
227 84b1cb73 2003-09-30 devnull i = 1-i; /* toggle */
228 84b1cb73 2003-09-30 devnull n = read(*fdp, plumbbuf[i].data, READBUFSIZE);
229 84b1cb73 2003-09-30 devnull if(n <= 0){
230 84b1cb73 2003-09-30 devnull fprint(2, "samterm: plumb read error: %r\n");
231 84b1cb73 2003-09-30 devnull threadexits("plumb"); /* not a fatal error */
233 84b1cb73 2003-09-30 devnull plumbbuf[i].n = n;
234 84b1cb73 2003-09-30 devnull if(plumbformat(i)){
235 84b1cb73 2003-09-30 devnull which = i;
236 84b1cb73 2003-09-30 devnull send(c, &which);
242 84b1cb73 2003-09-30 devnull plumbstart(void)
244 84b1cb73 2003-09-30 devnull static int fd;
245 84b1cb73 2003-09-30 devnull static void *arg[2];
247 84b1cb73 2003-09-30 devnull plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
248 84b1cb73 2003-09-30 devnull fd = plumbopen("edit", OREAD|OCEXEC);
249 84b1cb73 2003-09-30 devnull if(fd < 0)
250 84b1cb73 2003-09-30 devnull return -1;
251 84b1cb73 2003-09-30 devnull plumbc = chancreate(sizeof(int), 0);
252 84b1cb73 2003-09-30 devnull if(plumbc == nil){
253 84b1cb73 2003-09-30 devnull close(fd);
254 84b1cb73 2003-09-30 devnull return -1;
256 84b1cb73 2003-09-30 devnull arg[0] =plumbc;
257 84b1cb73 2003-09-30 devnull arg[1] = &fd;
258 84b1cb73 2003-09-30 devnull proccreate(plumbproc, arg, 4096);
259 84b1cb73 2003-09-30 devnull return 1;
264 84b1cb73 2003-09-30 devnull plumbstart(void)
266 84b1cb73 2003-09-30 devnull return -1;
270 84b1cb73 2003-09-30 devnull hostproc(void *arg)
272 84b1cb73 2003-09-30 devnull Channel *c;
273 84b1cb73 2003-09-30 devnull int i, n, which;
275 84b1cb73 2003-09-30 devnull c = arg;
278 84b1cb73 2003-09-30 devnull for(;;){
279 84b1cb73 2003-09-30 devnull i = 1-i; /* toggle */
280 84b1cb73 2003-09-30 devnull n = read(0, hostbuf[i].data, sizeof hostbuf[i].data);
281 84b1cb73 2003-09-30 devnull if(n <= 0){
282 84b1cb73 2003-09-30 devnull fprint(2, "samterm: host read error: %r\n");
283 84b1cb73 2003-09-30 devnull threadexitsall("host");
285 84b1cb73 2003-09-30 devnull hostbuf[i].n = n;
286 84b1cb73 2003-09-30 devnull which = i;
287 84b1cb73 2003-09-30 devnull send(c, &which);
292 84b1cb73 2003-09-30 devnull hoststart(void)
294 84b1cb73 2003-09-30 devnull hostc = chancreate(sizeof(int), 0);
295 84b1cb73 2003-09-30 devnull proccreate(hostproc, hostc, 1024);