1
0

fs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <fcall.h>
  4. #include <thread.h>
  5. #include <9p.h>
  6. #include "dat.h"
  7. Channel *fschan;
  8. Channel *writechan;
  9. static File *devcons, *devnew;
  10. static void
  11. fsread(Req *r)
  12. {
  13. Fsevent e;
  14. if(r->fid->file == devnew){
  15. if(r->fid->aux==nil){
  16. respond(r, "phase error");
  17. return;
  18. }
  19. readstr(r, r->fid->aux);
  20. respond(r, nil);
  21. return;
  22. }
  23. assert(r->fid->file == devcons);
  24. e.type = 'r';
  25. e.r = r;
  26. send(fschan, &e);
  27. }
  28. static void
  29. fsflush(Req *r)
  30. {
  31. Fsevent e;
  32. e.type = 'f';
  33. e.r = r;
  34. send(fschan, &e);
  35. }
  36. static void
  37. fswrite(Req *r)
  38. {
  39. static Event *e[4];
  40. Event *ep;
  41. int i, j, ei, nb, wid, pid;
  42. Rune rune;
  43. char *s;
  44. char tmp[UTFmax], *t;
  45. static int n, partial;
  46. if(r->fid->file == devnew){
  47. if(r->fid->aux){
  48. respond(r, "already created a window");
  49. return;
  50. }
  51. s = emalloc(r->ifcall.count+1);
  52. memmove(s, r->ifcall.data, r->ifcall.count);
  53. s[r->ifcall.count] = 0;
  54. pid = strtol(s, &t, 0);
  55. if(*t==' ')
  56. t++;
  57. i = newpipewin(pid, t);
  58. free(s);
  59. s = emalloc(32);
  60. sprint(s, "%lud", (ulong)i);
  61. r->fid->aux = s;
  62. r->ofcall.count = r->ifcall.count;
  63. respond(r, nil);
  64. return;
  65. }
  66. assert(r->fid->file == devcons);
  67. if(e[0] == nil){
  68. for(i=0; i<nelem(e); i++){
  69. e[i] = emalloc(sizeof(Event));
  70. e[i]->c1 = 'S';
  71. }
  72. }
  73. ep = e[n];
  74. n = (n+1)%nelem(e);
  75. assert(r->ifcall.count <= 8192); /* is this guaranteed by lib9p? */
  76. nb = r->ifcall.count;
  77. memmove(ep->b+partial, r->ifcall.data, nb);
  78. nb += partial;
  79. ep->b[nb] = '\0';
  80. if(strlen(ep->b) < nb){ /* nulls in data */
  81. t = ep->b;
  82. for(i=j=0; i<nb; i++)
  83. if(ep->b[i] != '\0')
  84. t[j++] = ep->b[i];
  85. nb = j;
  86. t[j] = '\0';
  87. }
  88. ei = nb>8192? 8192 : nb;
  89. /* process bytes into runes, transferring terminal partial runes into next buffer */
  90. for(i=j=0; i<ei && fullrune(ep->b+i, ei-i); i+=wid,j++)
  91. wid = chartorune(&rune, ep->b+i);
  92. memmove(tmp, ep->b+i, nb-i);
  93. partial = nb-i;
  94. ep->nb = i;
  95. ep->nr = j;
  96. ep->b[i] = '\0';
  97. if(i != 0){
  98. sendp(win->cevent, ep);
  99. recvp(writechan);
  100. }
  101. partial = nb-i;
  102. memmove(e[n]->b, tmp, partial);
  103. r->ofcall.count = r->ifcall.count;
  104. respond(r, nil);
  105. }
  106. void
  107. fsdestroyfid(Fid *fid)
  108. {
  109. if(fid->aux)
  110. free(fid->aux);
  111. }
  112. Srv fs = {
  113. .read= fsread,
  114. .write= fswrite,
  115. .flush= fsflush,
  116. .destroyfid= fsdestroyfid,
  117. .leavefdsopen= 1,
  118. };
  119. void
  120. mountcons(void)
  121. {
  122. fschan = chancreate(sizeof(Fsevent), 0);
  123. writechan = chancreate(sizeof(void*), 0);
  124. fs.tree = alloctree("win", "win", DMDIR|0555, nil);
  125. devcons = createfile(fs.tree->root, "cons", "win", 0666, nil);
  126. if(devcons == nil)
  127. sysfatal("creating /dev/cons: %r");
  128. devnew = createfile(fs.tree->root, "wnew", "win", 0666, nil);
  129. if(devnew == nil)
  130. sysfatal("creating /dev/wnew: %r");
  131. threadpostmountsrv(&fs, nil, "/dev", MBEFORE);
  132. }