pipe.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <fcall.h>
  12. #include <thread.h>
  13. #include <9p.h>
  14. #include "dat.h"
  15. typedef struct Wpid Wpid;
  16. struct Wpid
  17. {
  18. int pid;
  19. Window *w;
  20. Wpid *next;
  21. };
  22. void pipectl(void*);
  23. int pipefd;
  24. Wpid *wpid;
  25. int snarffd;
  26. Channel *newpipechan;
  27. int
  28. newpipewin(int pid, char *p)
  29. {
  30. int id;
  31. Window *w;
  32. Wpid *wp;
  33. w = newwindow();
  34. winname(w, p);
  35. wintagwrite(w, "Send ", 5);
  36. wp = emalloc(sizeof(Wpid));
  37. wp->pid = pid;
  38. wp->w = w;
  39. wp->next = wpid; /* BUG: this happens in fsread proc (we don't use wpid, so it's okay) */
  40. wpid = wp;
  41. id = w->id;
  42. sendp(newpipechan, w);
  43. return id;
  44. }
  45. int
  46. pipecommand(Window *w, char *s)
  47. {
  48. uint32_t q0, q1;
  49. char tmp[32], *t;
  50. int n, k;
  51. while(*s==' ' || *s=='\t' || *s=='\n')
  52. s++;
  53. if(strcmp(s, "Delete")==0){
  54. windel(w, 1);
  55. threadexits(nil);
  56. return 1;
  57. }
  58. if(strcmp(s, "Del")==0){
  59. if(windel(w, 0))
  60. threadexits(nil);
  61. return 1;
  62. }
  63. if(strcmp(s, "Send") == 0){
  64. if(w->addr < 0)
  65. w->addr = winopenfile(w, "addr");
  66. ctlprint(w->ctl, "addr=dot\n");
  67. seek(w->addr, 0UL, 0);
  68. if(read(w->addr, tmp, 2*12) == 2*12){
  69. q0 = atol(tmp+0*12);
  70. q1 = atol(tmp+1*12);
  71. if(q0 == q1){
  72. t = nil;
  73. k = 0;
  74. if(snarffd > 0){
  75. seek(0, snarffd, 0);
  76. for(;;){
  77. t = realloc(t, k+8192+2);
  78. if(t == nil)
  79. error("alloc failed: %r\n");
  80. n = read(snarffd, t+k, 8192);
  81. if(n <= 0)
  82. break;
  83. k += n;
  84. }
  85. t[k] = 0;
  86. }
  87. }else{
  88. t = emalloc((q1-q0)*UTFmax+2);
  89. winread(w, q0, q1, t);
  90. k = strlen(t);
  91. }
  92. if(t!=nil && t[0]!='\0'){
  93. if(t[k-1]!='\n' && t[k-1]!='\004'){
  94. t[k++] = '\n';
  95. t[k] = '\0';
  96. }
  97. sendit(t);
  98. }
  99. free(t);
  100. }
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. void
  106. pipectl(void *v)
  107. {
  108. Window *w;
  109. Event *e;
  110. w = v;
  111. proccreate(wineventproc, w, STACK);
  112. windormant(w);
  113. winsetaddr(w, "0", 0);
  114. for(;;){
  115. e = recvp(w->cevent);
  116. switch(e->c1){
  117. default:
  118. Unknown:
  119. fprint(2, "unknown message %c%c\n", e->c1, e->c2);
  120. break;
  121. case 'E': /* write to body; can't affect us */
  122. break;
  123. case 'F': /* generated by our actions; ignore */
  124. break;
  125. case 'K': /* ignore */
  126. break;
  127. case 'M':
  128. switch(e->c2){
  129. case 'x':
  130. case 'X':
  131. execevent(w, e, pipecommand);
  132. break;
  133. case 'l': /* reflect all searches back to acme */
  134. case 'L':
  135. if(e->flag & 2)
  136. recvp(w->cevent);
  137. winwriteevent(w, e);
  138. break;
  139. case 'I': /* modify away; we don't care */
  140. case 'i':
  141. case 'D':
  142. case 'd':
  143. break;
  144. default:
  145. goto Unknown;
  146. }
  147. }
  148. }
  149. }
  150. void
  151. newpipethread(void*)
  152. {
  153. Window *w;
  154. while(w = recvp(newpipechan))
  155. threadcreate(pipectl, w, STACK);
  156. }
  157. void
  158. startpipe(void)
  159. {
  160. newpipechan = chancreate(sizeof(Window*), 0);
  161. threadcreate(newpipethread, nil, STACK);
  162. snarffd = open("/dev/snarf", OREAD|OCEXEC);
  163. }