shell.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "sam.h"
  10. #include "parse.h"
  11. extern jmp_buf mainloop;
  12. char errfile[64];
  13. String plan9cmd; /* null terminated */
  14. Buffer plan9buf;
  15. void checkerrs(void);
  16. int
  17. plan9(File *f, int type, String *s, int nest)
  18. {
  19. long l;
  20. int m;
  21. int pid, fd;
  22. int retcode;
  23. char *retmsg;
  24. int pipe1[2], pipe2[2];
  25. if(s->s[0]==0 && plan9cmd.s[0]==0)
  26. error(Enocmd);
  27. else if(s->s[0])
  28. Strduplstr(&plan9cmd, s);
  29. if(downloaded){
  30. samerr(errfile);
  31. remove(errfile);
  32. }
  33. if(type!='!' && pipe(pipe1)==-1)
  34. error(Epipe);
  35. if(type=='|')
  36. snarf(f, addr.r.p1, addr.r.p2, &plan9buf, 1);
  37. if((pid=fork()) == 0){
  38. if(downloaded){ /* also put nasty fd's into errfile */
  39. fd = create(errfile, 1, 0666L);
  40. if(fd < 0)
  41. fd = create("/dev/null", 1, 0666L);
  42. dup(fd, 2);
  43. close(fd);
  44. /* 2 now points at err file */
  45. if(type == '>')
  46. dup(2, 1);
  47. else if(type=='!'){
  48. dup(2, 1);
  49. fd = open("/dev/null", 0);
  50. dup(fd, 0);
  51. close(fd);
  52. }
  53. }
  54. if(type != '!') {
  55. if(type=='<' || type=='|')
  56. dup(pipe1[1], 1);
  57. else if(type == '>')
  58. dup(pipe1[0], 0);
  59. close(pipe1[0]);
  60. close(pipe1[1]);
  61. }
  62. if(type == '|'){
  63. if(pipe(pipe2) == -1)
  64. exits("pipe");
  65. if((pid = fork())==0){
  66. /*
  67. * It's ok if we get SIGPIPE here
  68. */
  69. close(pipe2[0]);
  70. io = pipe2[1];
  71. if(retcode=!setjmp(mainloop)){ /* assignment = */
  72. char *c;
  73. for(l = 0; l<plan9buf.nc; l+=m){
  74. m = plan9buf.nc-l;
  75. if(m>BLOCKSIZE-1)
  76. m = BLOCKSIZE-1;
  77. bufread(&plan9buf, l, genbuf, m);
  78. genbuf[m] = 0;
  79. c = Strtoc(tmprstr(genbuf, m+1));
  80. Write(pipe2[1], c, strlen(c));
  81. free(c);
  82. }
  83. }
  84. exits(retcode? "error" : 0);
  85. }
  86. if(pid==-1){
  87. fprint(2, "Can't fork?!\n");
  88. exits("fork");
  89. }
  90. dup(pipe2[0], 0);
  91. close(pipe2[0]);
  92. close(pipe2[1]);
  93. }
  94. if(type=='<'){
  95. close(0); /* so it won't read from terminal */
  96. open("/dev/null", 0);
  97. }
  98. execl(SHPATH, SH, "-c", Strtoc(&plan9cmd), nil);
  99. exits("exec");
  100. }
  101. if(pid == -1)
  102. error(Efork);
  103. if(type=='<' || type=='|'){
  104. int nulls;
  105. if(downloaded && addr.r.p1 != addr.r.p2)
  106. outTl(Hsnarflen, addr.r.p2-addr.r.p1);
  107. snarf(f, addr.r.p1, addr.r.p2, &snarfbuf, 0);
  108. logdelete(f, addr.r.p1, addr.r.p2);
  109. close(pipe1[1]);
  110. io = pipe1[0];
  111. f->tdot.p1 = -1;
  112. f->ndot.r.p2 = addr.r.p2+readio(f, &nulls, 0, FALSE);
  113. f->ndot.r.p1 = addr.r.p2;
  114. closeio((Posn)-1);
  115. }else if(type=='>'){
  116. close(pipe1[0]);
  117. io = pipe1[1];
  118. bpipeok = 1;
  119. writeio(f);
  120. bpipeok = 0;
  121. closeio((Posn)-1);
  122. }
  123. retmsg = waitfor(pid);
  124. if(type=='|' || type=='<')
  125. if(retmsg[0]!=0)
  126. warn_s(Wbadstatus, retmsg);
  127. if(downloaded)
  128. checkerrs();
  129. if(!nest)
  130. dprint("!\n");
  131. return retmsg[0] ? -1 : 0;
  132. }
  133. void
  134. checkerrs(void)
  135. {
  136. char buf[256];
  137. int f, n, nl;
  138. char *p;
  139. long l;
  140. if(statfile(errfile, 0, 0, 0, &l, 0) > 0 && l != 0){
  141. if((f=open((char *)errfile, 0)) != -1){
  142. if((n=read(f, buf, sizeof buf-1)) > 0){
  143. for(nl=0,p=buf; nl<3 && p<&buf[n]; p++)
  144. if(*p=='\n')
  145. nl++;
  146. *p = 0;
  147. dprint("%s", buf);
  148. if(p-buf < l-1)
  149. dprint("(sam: more in %s)\n", errfile);
  150. }
  151. close(f);
  152. }
  153. }else
  154. remove((char *)errfile);
  155. }