win.c 4.7 KB

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