4 extern jmp_buf mainloop;
7 String plan9cmd; /* null terminated */
32 p = seprint(p, e, "%lud", 1+nlcount(f, 0, f->dot.r.p1));
33 p = seprint(p+1, e, "%lud", f->dot.r.p1);
34 p = seprint(p+1, e, "%lud", f->dot.r.p2);
37 if((fd = create("/env/%dot", OWRITE, 0666)) < 0)
38 fprint(2, "updateenv create: %r\n");
39 if(write(fd, buf, n) != n)
40 fprint(2, "updateenv write: %r\n");
44 plan9(File *f, int type, String *s, int nest)
51 int pipe1[2], pipe2[2];
53 if(s->s[0]==0 && plan9cmd.s[0]==0)
56 Strduplstr(&plan9cmd, s);
61 if(type!='!' && pipe(pipe1)==-1)
63 if(type=='|' || type=='_')
64 snarf(f, addr.r.p1, addr.r.p2, &plan9buf, 1);
65 if((pid=fork()) == 0){
66 if(downloaded){ /* also put nasty fd's into errfile */
67 fd = create(errfile, 1, 0666L);
69 fd = create("/dev/null", 1, 0666L);
72 /* 2 now points at err file */
77 fd = open("/dev/null", 0);
90 if(type == '|' || type == '_'){
93 if((pid = fork())==0){
95 * It's ok if we get SIGPIPE here
99 if(retcode=!setjmp(mainloop)){ /* assignment = */
101 for(l = 0; l<plan9buf.nc; l+=m){
105 bufread(&plan9buf, l, genbuf, m);
107 c = Strtoc(tmprstr(genbuf, m+1));
108 Write(pipe2[1], c, strlen(c));
112 exits(retcode? "error" : 0);
115 fprint(2, "Can't fork?!\n");
122 if(type=='<' || type=='^'){
123 close(0); /* so it won't read from terminal */
124 open("/dev/null", 0);
127 execl(SHPATH, SH, "-c", Strtoc(&plan9cmd), nil);
132 if(type=='<' || type=='|'){
134 if(downloaded && addr.r.p1 != addr.r.p2)
135 outTl(Hsnarflen, addr.r.p2-addr.r.p1);
136 snarf(f, addr.r.p1, addr.r.p2, &snarfbuf, 0);
137 logdelete(f, addr.r.p1, addr.r.p2);
141 f->ndot.r.p2 = addr.r.p2+readio(f, &nulls, 0, FALSE);
142 f->ndot.r.p1 = addr.r.p2;
151 }else if(type == '^' || type == '_'){
154 bufload(&cmdbuf, cmdbufpos, pipe1[0], &nulls);
157 retmsg = waitfor(pid);
158 if(type=='|' || type=='<' || type=='_' || type=='^')
160 warn_s(Wbadstatus, retmsg);
165 return retmsg[0] ? -1 : 0;
176 if(statfile(errfile, 0, 0, 0, &l, 0) > 0 && l != 0){
177 if((f=open((char *)errfile, 0)) != -1){
178 if((n=read(f, buf, sizeof buf-1)) > 0){
179 for(nl=0,p=buf; nl<3 && p<&buf[n]; p++)
185 dprint("(sam: more in %s)\n", errfile);
190 remove((char *)errfile);