plan9.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <mouse.h>
  14. #include <keyboard.h>
  15. #include <frame.h>
  16. #include <plumb.h>
  17. #include "flayer.h"
  18. #include "samterm.h"
  19. static char exname[64];
  20. void
  21. usage(void)
  22. {
  23. fprint(2, "usage: samterm [-a]\n");
  24. threadexitsall("usage");
  25. }
  26. void
  27. getscreen(int argc, char **argv)
  28. {
  29. char *t;
  30. ARGBEGIN{
  31. case 'a':
  32. autoindent = 1;
  33. break;
  34. default:
  35. usage();
  36. }ARGEND
  37. if(initdraw(panic1, nil, "sam") < 0){
  38. fprint(2, "samterm: initdraw: %r\n");
  39. threadexitsall("init");
  40. }
  41. t = getenv("tabstop");
  42. if(t != nil)
  43. maxtab = strtoul(t, nil, 0);
  44. draw(screen, screen->clipr, display->white, nil, ZP);
  45. }
  46. int
  47. screensize(int *w, int *h)
  48. {
  49. int fd, n;
  50. char buf[5*12+1];
  51. fd = open("/dev/screen", OREAD);
  52. if(fd < 0)
  53. return 0;
  54. n = read(fd, buf, sizeof(buf)-1);
  55. close(fd);
  56. if (n != sizeof(buf)-1)
  57. return 0;
  58. buf[n] = 0;
  59. if (h) {
  60. *h = atoi(buf+4*12)-atoi(buf+2*12);
  61. if (*h < 0)
  62. return 0;
  63. }
  64. if (w) {
  65. *w = atoi(buf+3*12)-atoi(buf+1*12);
  66. if (*w < 0)
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. int
  72. snarfswap(char *fromsam, int nc, char **tosam)
  73. {
  74. char *s1;
  75. int f, n, ss;
  76. f = open("/dev/snarf", 0);
  77. if(f < 0)
  78. return -1;
  79. ss = SNARFSIZE;
  80. if(hversion < 2)
  81. ss = 4096;
  82. *tosam = s1 = alloc(ss);
  83. n = read(f, s1, ss-1);
  84. close(f);
  85. if(n < 0)
  86. n = 0;
  87. if (n == 0) {
  88. *tosam = 0;
  89. free(s1);
  90. } else
  91. s1[n] = 0;
  92. f = create("/dev/snarf", 1, 0666);
  93. if(f >= 0){
  94. write(f, fromsam, nc);
  95. close(f);
  96. }
  97. return n;
  98. }
  99. void
  100. dumperrmsg(int count, int type, int count0, int c)
  101. {
  102. fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
  103. count, type, count0, c, rcvstring());
  104. }
  105. void
  106. removeextern(void)
  107. {
  108. remove(exname);
  109. }
  110. Readbuf hostbuf[2];
  111. Readbuf plumbbuf[2];
  112. void
  113. extproc(void *argv)
  114. {
  115. Channel *c;
  116. int i, n, which, *fdp;
  117. void **arg;
  118. arg = argv;
  119. c = arg[0];
  120. fdp = arg[1];
  121. i = 0;
  122. for(;;){
  123. i = 1-i; /* toggle */
  124. n = read(*fdp, plumbbuf[i].data, sizeof plumbbuf[i].data);
  125. if(n <= 0){
  126. fprint(2, "samterm: extern read error: %r\n");
  127. threadexits("extern"); /* not a fatal error */
  128. }
  129. plumbbuf[i].n = n;
  130. which = i;
  131. send(c, &which);
  132. }
  133. }
  134. void
  135. extstart(void)
  136. {
  137. char buf[32];
  138. int fd;
  139. static int p[2];
  140. static void *arg[2];
  141. if(pipe(p) < 0)
  142. return;
  143. sprint(exname, "/srv/sam.%s", getuser());
  144. fd = create(exname, 1, 0600);
  145. if(fd < 0){ /* assume existing guy is more important */
  146. Err:
  147. close(p[0]);
  148. close(p[1]);
  149. return;
  150. }
  151. sprint(buf, "%d", p[0]);
  152. if(write(fd, buf, strlen(buf)) <= 0)
  153. goto Err;
  154. close(fd);
  155. /*
  156. * leave p[0] open so if the file is removed the event
  157. * library won't get an error
  158. */
  159. plumbc = chancreate(sizeof(int), 0);
  160. arg[0] = plumbc;
  161. arg[1] = &p[1];
  162. proccreate(extproc, arg, 1024);
  163. atexit(removeextern);
  164. }
  165. int
  166. plumbformat(int i)
  167. {
  168. Plumbmsg *m;
  169. char *addr, *data, *act;
  170. int n;
  171. data = (char*)plumbbuf[i].data;
  172. m = plumbunpack(data, plumbbuf[i].n);
  173. if(m == nil)
  174. return 0;
  175. n = m->ndata;
  176. if(n == 0){
  177. plumbfree(m);
  178. return 0;
  179. }
  180. act = plumblookup(m->attr, "action");
  181. if(act!=nil && strcmp(act, "showfile")!=0){
  182. /* can't handle other cases yet */
  183. plumbfree(m);
  184. return 0;
  185. }
  186. addr = plumblookup(m->attr, "addr");
  187. if(addr){
  188. if(addr[0] == '\0')
  189. addr = nil;
  190. else
  191. addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
  192. }
  193. memmove(data, "B ", 2); /* we know there's enough room for this */
  194. memmove(data+2, m->data, n);
  195. n += 2;
  196. if(data[n-1] != '\n')
  197. data[n++] = '\n';
  198. if(addr != nil){
  199. if(n+strlen(addr)+1+1 <= READBUFSIZE)
  200. n += sprint(data+n, "%s\n", addr);
  201. free(addr);
  202. }
  203. plumbbuf[i].n = n;
  204. plumbfree(m);
  205. return 1;
  206. }
  207. void
  208. plumbproc(void *argv)
  209. {
  210. Channel *c;
  211. int i, n, which, *fdp;
  212. void **arg;
  213. arg = argv;
  214. c = arg[0];
  215. fdp = arg[1];
  216. i = 0;
  217. for(;;){
  218. i = 1-i; /* toggle */
  219. n = read(*fdp, plumbbuf[i].data, READBUFSIZE);
  220. if(n <= 0){
  221. fprint(2, "samterm: plumb read error: %r\n");
  222. threadexits("plumb"); /* not a fatal error */
  223. }
  224. plumbbuf[i].n = n;
  225. if(plumbformat(i)){
  226. which = i;
  227. send(c, &which);
  228. }
  229. }
  230. }
  231. int
  232. plumbstart(void)
  233. {
  234. static int fd;
  235. static void *arg[2];
  236. plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
  237. fd = plumbopen("edit", OREAD|OCEXEC);
  238. if(fd < 0)
  239. return -1;
  240. plumbc = chancreate(sizeof(int), 0);
  241. if(plumbc == nil){
  242. close(fd);
  243. return -1;
  244. }
  245. arg[0] =plumbc;
  246. arg[1] = &fd;
  247. proccreate(plumbproc, arg, 4096);
  248. return 1;
  249. }
  250. void
  251. hostproc(void *arg)
  252. {
  253. Channel *c;
  254. int i, n, which;
  255. c = arg;
  256. i = 0;
  257. for(;;){
  258. i = 1-i; /* toggle */
  259. n = read(0, hostbuf[i].data, sizeof hostbuf[i].data);
  260. if(n <= 0){
  261. if(n==0){
  262. if(exiting)
  263. threadexits(nil);
  264. werrstr("unexpected eof");
  265. }
  266. fprint(2, "samterm: host read error: %r\n");
  267. threadexitsall("host");
  268. }
  269. hostbuf[i].n = n;
  270. which = i;
  271. send(c, &which);
  272. }
  273. }
  274. void
  275. hoststart(void)
  276. {
  277. hostc = chancreate(sizeof(int), 0);
  278. proccreate(hostproc, hostc, 1024);
  279. }