fs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, 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. /* process bytes into runes, transferring terminal partial runes into next buffer */
  89. for(i=j=0; i<nb && fullrune(ep->b+i, nb-i); i+=wid,j++)
  90. wid = chartorune(&rune, ep->b+i);
  91. memmove(tmp, ep->b+i, nb-i);
  92. partial = nb-i;
  93. ep->nb = i;
  94. ep->nr = j;
  95. ep->b[i] = '\0';
  96. if(i != 0){
  97. sendp(win->cevent, ep);
  98. recvp(writechan);
  99. }
  100. partial = nb-i;
  101. memmove(e[n]->b, tmp, partial);
  102. r->ofcall.count = r->ifcall.count;
  103. respond(r, nil);
  104. }
  105. void
  106. fsdestroyfid(Fid *fid)
  107. {
  108. if(fid->aux)
  109. free(fid->aux);
  110. }
  111. Srv fs = {
  112. .read= fsread,
  113. .write= fswrite,
  114. .flush= fsflush,
  115. .destroyfid= fsdestroyfid,
  116. .leavefdsopen= 1,
  117. };
  118. void
  119. mountcons(void)
  120. {
  121. fschan = chancreate(sizeof(Fsevent), 0);
  122. writechan = chancreate(sizeof(void*), 0);
  123. fs.tree = alloctree("win", "win", DMDIR|0555, nil);
  124. devcons = createfile(fs.tree->root, "cons", "win", 0666, nil);
  125. if(devcons == nil)
  126. sysfatal("creating /dev/cons: %r");
  127. devnew = createfile(fs.tree->root, "wnew", "win", 0666, nil);
  128. if(devnew == nil)
  129. sysfatal("creating /dev/wnew: %r");
  130. threadpostmountsrv(&fs, nil, "/dev", MBEFORE);
  131. }