Blob


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