win.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <bio.h>
  12. #include <thread.h>
  13. #include "win.h"
  14. void*
  15. erealloc(void *p, uint n)
  16. {
  17. p = realloc(p, n);
  18. if(p == nil)
  19. fprint(2, "realloc failed: %r");
  20. return p;
  21. }
  22. void
  23. wnew(Win *w)
  24. {
  25. char buf[12];
  26. w->ctl = open("/mnt/acme/new/ctl", ORDWR);
  27. if(w->ctl<0 || read(w->ctl, buf, 12)!=12)
  28. fprint (2, "can't open window ctl file: %r");
  29. ctlwrite(w, "noscroll\n");
  30. w->winid = atoi(buf);
  31. w->event = openfile(w, "event");
  32. w->addr = -1; /* will be opened when needed */
  33. w->body = nil;
  34. w->data = -1;
  35. }
  36. int
  37. openfile(Win *w, char *f)
  38. {
  39. char buf[64];
  40. int fd;
  41. sprint(buf, "/mnt/acme/%d/%s", w->winid, f);
  42. fd = open(buf, ORDWR|OCEXEC);
  43. if(fd < 0)
  44. fprint (2,"can't open window %s file: %r", f);
  45. return fd;
  46. }
  47. void
  48. openbody(Win *w, int mode)
  49. {
  50. char buf[64];
  51. sprint(buf, "/mnt/acme/%d/body", w->winid);
  52. w->body = Bopen(buf, mode|OCEXEC);
  53. if(w->body == nil)
  54. fprint(2,"can't open window body file: %r");
  55. }
  56. void
  57. wwritebody(Win *w, char *s, int n)
  58. {
  59. if(w->body == nil)
  60. openbody(w, OWRITE);
  61. if(Bwrite(w->body, s, n) != n)
  62. fprint(2,"write error to window: %r");
  63. Bflush(w->body);
  64. }
  65. void
  66. wreplace(Win *w, char *addr, char *repl, int nrepl)
  67. {
  68. if(w->addr < 0)
  69. w->addr = openfile(w, "addr");
  70. if(w->data < 0)
  71. w->data = openfile(w, "data");
  72. if(write(w->addr, addr, strlen(addr)) < 0){
  73. fprint(2, "mail: warning: badd address %s:%r\n", addr);
  74. return;
  75. }
  76. if(write(w->data, repl, nrepl) != nrepl)
  77. fprint(2, "writing data: %r");
  78. }
  79. static int
  80. nrunes(char *s, int nb)
  81. {
  82. int i, n;
  83. Rune r;
  84. n = 0;
  85. for(i=0; i<nb; n++)
  86. i += chartorune(&r, s+i);
  87. return n;
  88. }
  89. void
  90. wread(Win *w, uint q0, uint q1, char *data)
  91. {
  92. int m, n, nr;
  93. char buf[256];
  94. if(w->addr < 0)
  95. w->addr = openfile(w, "addr");
  96. if(w->data < 0)
  97. w->data = openfile(w, "data");
  98. m = q0;
  99. while(m < q1){
  100. n = sprint(buf, "#%d", m);
  101. if(write(w->addr, buf, n) != n)
  102. fprint(2,"writing addr: %r");
  103. n = read(w->data, buf, sizeof buf);
  104. if(n <= 0)
  105. fprint(2,"reading data: %r");
  106. nr = nrunes(buf, n);
  107. while(m+nr >q1){
  108. do; while(n>0 && (buf[--n]&0xC0)==0x80);
  109. --nr;
  110. }
  111. if(n == 0)
  112. break;
  113. memmove(data, buf, n);
  114. data += n;
  115. *data = 0;
  116. m += nr;
  117. }
  118. }
  119. void
  120. wselect(Win *w, char *addr)
  121. {
  122. if(w->addr < 0)
  123. w->addr = openfile(w, "addr");
  124. if(write(w->addr, addr, strlen(addr)) < 0)
  125. fprint(2,"writing addr");
  126. ctlwrite(w, "dot=addr\n");
  127. }
  128. void
  129. wtagwrite(Win *w, char *s, int n)
  130. {
  131. int fd;
  132. fd = openfile(w, "tag");
  133. if(write(fd, s, n) != n)
  134. fprint(2,"tag write: %r");
  135. close(fd);
  136. }
  137. void
  138. ctlwrite(Win *w, char *s)
  139. {
  140. int n;
  141. n = strlen(s);
  142. if(write(w->ctl, s, n) != n)
  143. fprint(2,"write error to ctl file: %r");
  144. }
  145. int
  146. wdel(Win *w)
  147. {
  148. if(write(w->ctl, "del\n", 4) != 4)
  149. return False;
  150. wdormant(w);
  151. close(w->ctl);
  152. w->ctl = -1;
  153. close(w->event);
  154. w->event = -1;
  155. return True;
  156. }
  157. void
  158. wname(Win *w, char *s)
  159. {
  160. char buf[128];
  161. sprint(buf, "name %s\n", s);
  162. ctlwrite(w, buf);
  163. }
  164. void
  165. wclean(Win *w)
  166. {
  167. if(w->body)
  168. Bflush(w->body);
  169. ctlwrite(w, "clean\n");
  170. }
  171. void
  172. wdormant(Win *w)
  173. {
  174. if(w->addr >= 0){
  175. close(w->addr);
  176. w->addr = -1;
  177. }
  178. if(w->body != nil){
  179. Bterm(w->body);
  180. w->body = nil;
  181. }
  182. if(w->data >= 0){
  183. close(w->data);
  184. w->data = -1;
  185. }
  186. }
  187. int
  188. getec(Win *w)
  189. {
  190. if(w->nbuf == 0){
  191. w->nbuf = read(w->event, w->buf, sizeof w->buf);
  192. if(w->nbuf <= 0)
  193. fprint(2,"event read error: %r");
  194. w->bufp = w->buf;
  195. }
  196. w->nbuf--;
  197. return *w->bufp++;
  198. }
  199. int
  200. geten(Win *w)
  201. {
  202. int n, c;
  203. n = 0;
  204. while('0'<=(c=getec(w)) && c<='9')
  205. n = n*10+(c-'0');
  206. if(c != ' ')
  207. fprint(2, "event number syntax");
  208. return n;
  209. }
  210. int
  211. geter(Win *w, char *buf, int *nb)
  212. {
  213. Rune r;
  214. int n;
  215. r = getec(w);
  216. buf[0] = r;
  217. n = 1;
  218. if(r < Runeself)
  219. goto Return;
  220. while(!fullrune(buf, n))
  221. buf[n++] = getec(w);
  222. chartorune(&r, buf);
  223. Return:
  224. *nb = n;
  225. return r;
  226. }
  227. void
  228. wevent(Win *w, Event *e)
  229. {
  230. int i, nb;
  231. e->c1 = getec(w);
  232. e->c2 = getec(w);
  233. e->q0 = geten(w);
  234. e->q1 = geten(w);
  235. e->flag = geten(w);
  236. e->nr = geten(w);
  237. if(e->nr > EVENTSIZE)
  238. fprint(2, "wevent: event string too long");
  239. e->nb = 0;
  240. for(i=0; i<e->nr; i++){
  241. e->r[i] = geter(w, e->b+e->nb, &nb);
  242. e->nb += nb;
  243. }
  244. e->r[e->nr] = 0;
  245. e->b[e->nb] = 0;
  246. if(getec(w) != '\n')
  247. fprint(2, "wevent: event syntax 2");
  248. }
  249. void
  250. wslave(Win *w, Channel *ce)
  251. {
  252. Event e;
  253. while(recv(ce, &e) >= 0)
  254. wevent(w, &e);
  255. }
  256. void
  257. wwriteevent(Win *w, Event *e)
  258. {
  259. fprint(w->event, "%c%c%d %d\n", e->c1, e->c2, e->q0, e->q1);
  260. }
  261. int
  262. wreadall(Win *w, char **sp)
  263. {
  264. char *s;
  265. int m, na, n;
  266. if(w->body != nil)
  267. Bterm(w->body);
  268. openbody(w, OREAD);
  269. s = nil;
  270. na = 0;
  271. n = 0;
  272. for(;;){
  273. if(na < n+512){
  274. na += 1024;
  275. s = erealloc(s, na+1);
  276. }
  277. m = Bread(w->body, s+n, na-n);
  278. if(m <= 0)
  279. break;
  280. n += m;
  281. }
  282. s[n] = 0;
  283. Bterm(w->body);
  284. w->body = nil;
  285. *sp = s;
  286. return n;
  287. }