Rev 8120 | Rev 8864 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
7674 | bpr | 18 | /* Common routines in interfaces */ |
8120 | bpr | 19 | #include "common.h" |
10 | reyssat | 20 | #define ch_root "bin/ch..root" |
21 | |||
22 | int mypid; |
||
23 | int must_chroot=0; |
||
24 | char inputfname[256], outputfname[256]; |
||
25 | char pidfname[256]; |
||
26 | char parmbuf[parmlim+16]; |
||
27 | char *inpf, *outpf, *errorf; |
||
28 | char *parm; |
||
29 | char *tmp_dir; |
||
30 | char cmdbuf[1024]; |
||
31 | char inputbuf[inputlim]; |
||
32 | char *inputptr, *inputend; |
||
33 | char stdinbuf[MAX_LINELEN+1]; |
||
34 | char *cmdparm; |
||
35 | int isabout=0; |
||
36 | char *multiexec_random; |
||
37 | int multiexec=0, mxpid, notfirst; |
||
38 | int pipe_in[2], pipe_out[2]; |
||
39 | int debug=0; |
||
40 | FILE *goin; |
||
7674 | bpr | 41 | unsigned int seed; /* random seed value */ |
10 | reyssat | 42 | char aboutbuf[aboutlen]; |
7674 | bpr | 43 | /* this is only a default. It should usually be reset. */ |
10 | reyssat | 44 | char *tmpdir="/tmp"; |
45 | char startstring[256], endstring[256]; |
||
46 | char *obuf; |
||
47 | |||
48 | void check_parm(char *p); |
||
49 | void output(char *p); |
||
50 | void about(void); |
||
51 | char *dynsetup(char *p, char *end); |
||
52 | |||
7674 | bpr | 53 | /* strip trailing spaces; return string end. */ |
8100 | bpr | 54 | char *strip_trailing_spaces2(char *p) |
10 | reyssat | 55 | { |
56 | char *pp; |
||
57 | if(*p==0) return p; |
||
58 | for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0); |
||
59 | return pp; |
||
60 | } |
||
61 | |||
8149 | bpr | 62 | |
7674 | bpr | 63 | /* Find first occurrence of word */ |
8100 | bpr | 64 | char *wordchr2(char *p, char *w) |
10 | reyssat | 65 | { |
66 | char *r; |
||
67 | |||
68 | if(*w==0) return NULL; |
||
7674 | bpr | 69 | for(r=strstr(p,w);r!=NULL && |
70 | ( (r>p && !isspace(*(r-1))) || (!isspace(*(r+strlen(w))) && *(r+strlen(w))!=0) ); |
||
71 | r=strstr(r+1,w)); |
||
10 | reyssat | 72 | return r; |
73 | } |
||
74 | |||
7674 | bpr | 75 | /* find matching parenthesis */ |
8100 | bpr | 76 | char *find_matching2(char *p, char c) |
10 | reyssat | 77 | { |
78 | char *pp; |
||
79 | int parenth, brak, brace; |
||
80 | parenth=brak=brace=0; |
||
81 | for(pp=p; *pp!=0; pp++) { |
||
7674 | bpr | 82 | switch(*pp) { |
83 | case '[': brak++; break; |
||
84 | case ']': brak--; break; |
||
85 | case '(': parenth++; break; |
||
86 | case ')': parenth--; break; |
||
87 | case '{': brace++; break; |
||
88 | case '}': brace--; break; |
||
89 | default: continue; |
||
90 | } |
||
91 | if(parenth<0 || brak<0 || brace<0) { |
||
92 | if(*pp!=c || parenth>0 || brak>0 || brace>0) return NULL; |
||
93 | else break; |
||
94 | } |
||
10 | reyssat | 95 | } |
96 | if(*pp!=c) return NULL; |
||
97 | return pp; |
||
98 | } |
||
99 | |||
7674 | bpr | 100 | /* Read/write to a file with variable parms to print filename */ |
10 | reyssat | 101 | void accessfile(char *content, char *type, char *s,...) |
102 | { |
||
103 | va_list vp; |
||
104 | char buf[MAX_LINELEN+1]; |
||
105 | FILE *f; |
||
106 | int l; |
||
107 | |||
108 | va_start(vp,s); |
||
109 | vsnprintf(buf,sizeof(buf),s,vp); |
||
110 | va_end(vp); |
||
111 | f=fopen(buf,type); if(f==NULL) { |
||
7674 | bpr | 112 | if(*type=='r') content[0]=0; return; |
10 | reyssat | 113 | } |
114 | switch(*type) { |
||
7674 | bpr | 115 | case 'a': |
116 | case 'w': { |
||
117 | l=strlen(content); fwrite(content,1,l,f); break; |
||
118 | } |
||
119 | case 'r': { |
||
120 | l=fread(content,1,MAX_LINELEN-1,f); |
||
121 | if(l>0 && l<MAX_LINELEN) content[l]=0; |
||
122 | else content[0]=0; |
||
123 | break; |
||
124 | } |
||
125 | default: { |
||
126 | content[0]=0; break; |
||
127 | } |
||
10 | reyssat | 128 | } |
129 | fclose(f); |
||
130 | } |
||
131 | |||
132 | void getstdin(void) |
||
133 | { |
||
134 | char *p; |
||
135 | int i,j,k; |
||
136 | i=0; k=MAX_LINELEN; stdinbuf[0]=0; |
||
137 | do { |
||
7674 | bpr | 138 | j=read(0,stdinbuf+i,k); |
139 | if(j<0 || j>k) exit(1); |
||
140 | i+=j; k-=j; stdinbuf[i]=0; |
||
141 | p=strstr(stdinbuf,multiexec_random); |
||
142 | if(p) {*p=0; break;} |
||
10 | reyssat | 143 | } |
144 | while(k>0); |
||
145 | } |
||
146 | |||
7674 | bpr | 147 | /* add a pid to the list of running childs */ |
10 | reyssat | 148 | void addpid(int pid) |
149 | { |
||
150 | char buf[MAX_LINELEN+1], pidbuf[32]; |
||
151 | int l; |
||
152 | snprintf(pidbuf,sizeof(pidbuf),"%u",pid); |
||
153 | accessfile(buf,"r",pidfname); l=strlen(buf); |
||
154 | if(l>=MAX_LINELEN-64) return; |
||
8100 | bpr | 155 | if(wordchr2(buf,pidbuf)==NULL) { |
7674 | bpr | 156 | snprintf(buf+l,sizeof(buf)-l," %s",pidbuf); |
157 | accessfile(buf,"w",pidfname); |
||
10 | reyssat | 158 | } |
159 | } |
||
160 | |||
7674 | bpr | 161 | /* Remove a pidname to the list of running childs */ |
10 | reyssat | 162 | void rmpid(int pid) |
163 | { |
||
164 | char buf[MAX_LINELEN+1], pidbuf[32], *p; |
||
165 | snprintf(pidbuf,sizeof(pidbuf),"%u",pid); |
||
166 | accessfile(buf,"r",pidfname); |
||
8100 | bpr | 167 | p=wordchr2(buf,pidbuf); |
10 | reyssat | 168 | if(p!=NULL) { |
7674 | bpr | 169 | ovlstrcpy(p,find_word_start(find_word_end(p))); |
170 | accessfile(buf,"w",pidfname); |
||
10 | reyssat | 171 | } |
172 | } |
||
173 | |||
7674 | bpr | 174 | int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *args) |
10 | reyssat | 175 | { |
176 | pid_t pid; |
||
177 | int i; |
||
178 | struct stat st; |
||
179 | char *cm, *p, *p2, abuf[MAX_LINELEN+1]; |
||
180 | char *arg[1024]; |
||
181 | |||
7079 | bpr | 182 | for(cm=cmdf; isspace(*cm); cm++){}; |
7076 | obado | 183 | if(*cmdf==0) return -1; |
7674 | bpr | 184 | fflush(NULL); |
185 | /* flush all output streams before forking |
||
186 | * otherwise they will be doubled */ |
||
10 | reyssat | 187 | pid=fork(); if(pid==-1) return -1; |
7674 | bpr | 188 | if(!pid) { /* child */ |
189 | if(!inf) { |
||
190 | dup2(pipe_in[0],0); close(pipe_in[1]); |
||
191 | } |
||
192 | else if (freopen(inf,"r",stdin) == NULL) |
||
3843 | kbelabas | 193 | fprintf(stderr,"freopen failed"); |
7674 | bpr | 194 | if(!outf) { |
195 | dup2(pipe_out[1],1); close(pipe_out[0]); |
||
196 | } |
||
197 | else if (freopen(outf,"w",stdout) == NULL) |
||
3843 | kbelabas | 198 | fprintf(stderr,"freopen failed"); |
7674 | bpr | 199 | if(errf && freopen(errf,"w",stderr) == NULL) |
3843 | kbelabas | 200 | fprintf(stderr,"freopen failed"); |
7674 | bpr | 201 | snprintf(abuf,sizeof(abuf),"%s",find_word_start(args)); |
202 | if(stat("../chroot/tmp/sessions/.chroot",&st)==0 || must_chroot) { |
||
203 | arg[0]=ch_root; i=1; |
||
204 | } |
||
205 | else { |
||
206 | i=0; |
||
207 | setreuid(getuid(),getuid());setregid(getgid(),getgid()); |
||
208 | } |
||
209 | arg[i++]=cmdf; |
||
210 | for(p=abuf; *p && i<1000; i++, p=find_word_start(p2)) { |
||
211 | arg[i]=p; p2=find_word_end(p); if(*p2) *p2++=0; |
||
212 | } |
||
213 | arg[i]=NULL; |
||
214 | if(strchr(arg[0],'/')) execv(arg[0],arg); |
||
215 | else execvp(arg[0],arg); |
||
216 | fprintf(stderr,"%s not_INStalled",progname); |
||
217 | exit(127); |
||
10 | reyssat | 218 | } |
7674 | bpr | 219 | else { /* parent */ |
220 | addpid(pid); mxpid=pid; |
||
221 | if(outf) { |
||
222 | int status; |
||
223 | close(pipe_in[1]); |
||
224 | if(waitpid(pid,&status,0)==pid) { |
||
225 | rmpid(pid); mxpid=-1; |
||
226 | } |
||
227 | } |
||
10 | reyssat | 228 | } |
229 | return 0; |
||
230 | } |
||
231 | |||
7674 | bpr | 232 | /* verify illegal strings in parms. */ |
10 | reyssat | 233 | void find_illegal(char *p) |
234 | { |
||
235 | char *pp, *pe; |
||
236 | int i, l; |
||
237 | for(pp=p;*pp;pp++) { |
||
7674 | bpr | 238 | if((*pp<' ' && *pp!='\n' && *pp!=' ') || *pp>=127) *pp=' '; |
10 | reyssat | 239 | } |
240 | for(i=0;i<illpart_no;i++) { |
||
7674 | bpr | 241 | pe=illpart[i]; l=strlen(pe); |
242 | for(pp=strstr(p,pe); pp; pp=strstr(pp+1,pe)) { |
||
243 | if(!isupper(pe[0]) || !islower(*(pp+l))) { |
||
244 | if(pp[1]!='j' && pp[1]!='J') pp[1]='J'; |
||
245 | else pp[1]='Z'; |
||
246 | } |
||
247 | } |
||
10 | reyssat | 248 | } |
249 | for(i=0;i<illegal_no;i++) { |
||
7674 | bpr | 250 | pe=illegal[i]; l=strlen(pe); |
251 | for(pp=strstr(p,pe); pp; pp=strstr(pp+1,pe)) { |
||
252 | if((pp==p || !isalnum(*(pp-1))) && !isalnum(*(pp+l))) { |
||
253 | if(pp[1]!='j' && pp[1]!='J') pp[1]='J'; |
||
254 | else pp[1]='Z'; |
||
255 | } |
||
256 | } |
||
10 | reyssat | 257 | } |
258 | } |
||
259 | |||
7674 | bpr | 260 | /* strip trailing zeros */ |
10 | reyssat | 261 | void strip_zeros(char *p) |
262 | { |
||
263 | char *pp, *p2, *numend, *ee; |
||
264 | int i; |
||
265 | for(pp=p;*pp!=0;pp++) { |
||
7674 | bpr | 266 | if(!isdigit(*pp)) continue; |
267 | i=0; |
||
268 | for(numend=pp;isdigit(*numend) || *numend=='.';numend++) |
||
269 | if(*numend=='.') i=1; |
||
270 | if(i==0) { |
||
271 | pp=numend-1;continue; |
||
272 | } |
||
273 | for(p2=numend;p2>pp && *(p2-1)=='0';p2--); |
||
274 | for(ee=numend;isspace(*ee);ee++); |
||
275 | if(*(pp+1)=='.' && (*ee=='E' || *ee=='e') |
||
276 | && *(ee+1)=='-') { |
||
277 | int k=0; |
||
278 | char *pt=ee+2; |
||
279 | while(isdigit(*pt)) { |
||
280 | k*=10;k+=*pt-'0';pt++; |
||
281 | } |
||
282 | if(precision>8 && (k>precision*2 || (k>precision && *pp=='0'))) { |
||
283 | |||
284 | sprintf(pp,"0.0%s",pt); |
||
285 | |||
286 | pp+=strlen("0.0")-1; |
||
287 | continue; |
||
288 | } |
||
289 | } |
||
290 | |||
291 | if(*(p2-1)=='.' && p2<numend) p2++; |
||
292 | |||
293 | if(p2<numend) { |
||
294 | ovlstrcpy(p2,numend);numend=p2; |
||
295 | } |
||
296 | pp=numend-1; |
||
10 | reyssat | 297 | } |
298 | } |
||
299 | |||
300 | char *hname[]={"", "_1","_2","_3","_4","_5","_6","_7","_8"}; |
||
301 | #define hnameno (sizeof(hname)/sizeof(hname[0])) |
||
302 | |||
303 | void putheader(void) |
||
304 | { |
||
305 | char hbuf[64]; |
||
306 | char *p; |
||
307 | int i; |
||
7674 | bpr | 308 | |
10 | reyssat | 309 | inputptr=dynsetup(inputptr,inputend); |
310 | snprintf(inputptr,inputend-inputptr,"%s",header); |
||
311 | inputptr+=strlen(inputptr); |
||
312 | for(i=0;i<hnameno;i++) { |
||
7674 | bpr | 313 | snprintf(hbuf,sizeof(hbuf),"w_%s_header%s",progname,hname[i]); |
314 | p=getenv(hbuf); if(p!=NULL && *p!=0) { |
||
315 | snprintf(parmbuf,parmlim,"%s",p); |
||
316 | check_parm(parmbuf); |
||
317 | snprintf(inputptr,inputend-inputptr,"%s\n",parmbuf); |
||
318 | inputptr+=strlen(inputptr); |
||
319 | } |
||
10 | reyssat | 320 | } |
321 | } |
||
322 | |||
323 | void putparm(void) |
||
324 | { |
||
325 | snprintf(parmbuf,parmlim,"%s",parm); check_parm(parmbuf); |
||
326 | snprintf(inputptr,inputend-inputptr,stringprinter,startstring); |
||
327 | inputptr+=strlen(inputptr); |
||
328 | snprintf(inputptr,inputend-inputptr,"%s",parmbuf); |
||
329 | inputptr+=strlen(inputptr); |
||
330 | if(inputptr<inputend && inputptr>inputbuf && inputptr[-1]!='\n') |
||
331 | *inputptr++='\n'; |
||
332 | snprintf(inputptr,inputend-inputptr,stringprinter,endstring); |
||
333 | inputptr+=strlen(inputptr); |
||
334 | *inputptr=0; |
||
335 | } |
||
336 | |||
7674 | bpr | 337 | /* general first preparation */ |
10 | reyssat | 338 | void prepare1(void) |
339 | { |
||
340 | char *p, buf[256]; |
||
341 | int i; |
||
342 | unsigned int r[4]; |
||
343 | struct timeval t; |
||
344 | |||
345 | parm=getenv("wims_exec_parm"); |
||
7674 | bpr | 346 | /* nothing to do if no calling parameter */ |
10 | reyssat | 347 | if(parm==NULL || *parm==0) exit(0); |
348 | inputptr=inputbuf; inputend=inputbuf+inputlim-1; |
||
349 | multiexec_random=getenv("multiexec_random"); |
||
350 | if(multiexec_random==NULL) multiexec_random=""; |
||
351 | if(*parm && strcmp(parm,multiexec_random)==0) { |
||
7674 | bpr | 352 | multiexec=1; |
353 | getstdin(); parm=stdinbuf; |
||
354 | if(parm[0]==0) exit(0); |
||
10 | reyssat | 355 | } |
356 | if(pipe(pipe_in)<0 || pipe(pipe_out)<0) { |
||
7674 | bpr | 357 | fprintf(stderr,"%s: unable to create pipe.\n",progname); |
358 | exit(1); |
||
10 | reyssat | 359 | } |
360 | /* i=fcntl(pipe_in[1],F_GETFL); if(i>=0) fcntl(pipe_in[1],F_SETFL,i|O_NONBLOCK); |
||
361 | i=fcntl(pipe_out[0],F_GETFL); if(i>=0) fcntl(pipe_out[0],F_SETFL,i|O_NONBLOCK); |
||
362 | */ tmp_dir=getenv("tmp_dir"); if(tmp_dir==NULL || *tmp_dir==0) tmp_dir=tmpdir; |
||
363 | setenv("TMPDIR",tmp_dir,1); |
||
364 | mypid=getpid(); |
||
365 | gettimeofday(&t,NULL); seed=t.tv_usec*t.tv_sec+mypid; |
||
366 | srandom(seed); |
||
367 | for(i=0;i<4;i++) r[i]=random(); |
||
368 | snprintf(startstring,sizeof(startstring), |
||
7674 | bpr | 369 | "Start line %s %u %u %u %u",progname,r[0],r[1],r[2],r[3]); |
10 | reyssat | 370 | snprintf(endstring,sizeof(endstring), |
7674 | bpr | 371 | "End line %s %u %u %u %u",progname,r[0],r[1],r[2],r[3]); |
10 | reyssat | 372 | snprintf(buf,sizeof(buf),"%s_command",progname); p=getenv(buf); |
373 | if(p!=NULL && *find_word_start(p)!=0) nameofcmd=find_word_start(p); |
||
374 | snprintf(cmdbuf,sizeof(cmdbuf),"%s",nameofcmd); nameofcmd=cmdbuf; |
||
375 | cmdparm=find_word_end(nameofcmd); if(*cmdparm) *cmdparm++=0; |
||
376 | cmdparm=find_word_start(cmdparm); |
||
377 | snprintf(pidfname,sizeof(pidfname),"%s/exec.pid",tmp_dir); addpid(mypid); |
||
378 | snprintf(inputfname,sizeof(inputfname),"%s/%s_input.%d",tmp_dir,progname,mypid); |
||
379 | snprintf(outputfname,sizeof(outputfname),"%s/%s_output.%d",tmp_dir,progname,mypid); |
||
380 | errorf=NULL; |
||
381 | inpf=inputfname; outpf=outputfname; |
||
382 | isabout=0; p=find_word_start(parm); i=strlen("about"); |
||
383 | if(memcmp(p,"about",i)==0 && *find_word_start(p+i)==0) isabout=1; |
||
384 | p=getenv("multiexec_debug"); if(p && strcmp(p,"yes")==0) debug=1; |
||
385 | } |
||
386 | |||
387 | void prepabout(char *cmd, char *outf, char *errf) |
||
388 | { |
||
3843 | kbelabas | 389 | (void)write(pipe_in[1],cmd,strlen(cmd)); |
10 | reyssat | 390 | execredirected(nameofcmd,NULL,outf,errf,cmdparm); |
391 | } |
||
392 | |||
7674 | bpr | 393 | /* read to aboutbuf. Returns content length. */ |
10 | reyssat | 394 | int readabout(void) |
395 | { |
||
396 | FILE *ff; |
||
397 | long int l; |
||
398 | char *p; |
||
7674 | bpr | 399 | |
10 | reyssat | 400 | aboutbuf[0]=0; ff=fopen(outputfname,"r"); |
401 | if(ff!=NULL) { |
||
7674 | bpr | 402 | fseek(ff,0,SEEK_END); l=ftell(ff); fseek(ff,0,SEEK_SET); |
403 | l=fread(aboutbuf,1,aboutlen-10,ff); fclose(ff); |
||
404 | if(l>0 && l<aboutlen) aboutbuf[l]=0; else aboutbuf[0]=0; |
||
405 | p=find_word_start(aboutbuf); if(p>aboutbuf) ovlstrcpy(aboutbuf,p); |
||
10 | reyssat | 406 | } |
407 | return strlen(aboutbuf); |
||
408 | } |
||
409 | |||
7674 | bpr | 410 | /* read result of execution */ |
10 | reyssat | 411 | void readresult(void) |
412 | { |
||
413 | FILE *ff; |
||
414 | char *p, *pp, *pe; |
||
415 | |||
416 | if(debug) { |
||
7674 | bpr | 417 | ff=fopen(outputfname,"a"); if(ff) { |
418 | fputs(obuf,ff); fclose(ff); |
||
419 | } |
||
10 | reyssat | 420 | } |
421 | pe=NULL; |
||
422 | p=strstr(obuf,startstring); |
||
423 | if(p) { |
||
7674 | bpr | 424 | p+=strlen(startstring); |
425 | pe=strstr(p,endstring); |
||
426 | if(pe) { |
||
427 | for(pp=pe-1; pp>=p && pp>pe-64 && *pp!='\n'; pp--); |
||
428 | if(pp>=p && *pp=='\n') pe=pp; |
||
429 | } |
||
10 | reyssat | 430 | } |
431 | if(pe) { |
||
7674 | bpr | 432 | *pe=0; |
433 | while((pe=strstr(p,startstring))!=NULL) { |
||
434 | p=pe+strlen(startstring); |
||
435 | } |
||
436 | output(p); |
||
10 | reyssat | 437 | } |
438 | else { |
||
7674 | bpr | 439 | if(mxpid>=0) { |
440 | if(kill(mxpid,0)<0 && errno==ESRCH) { /* this doesn't work */ |
||
441 | fprintf(stderr,"%s not_INStalled",progname); |
||
442 | } |
||
443 | else { |
||
444 | fprintf(stderr,"%s: execution error or time out.\n",progname); |
||
445 | kill(mxpid,SIGKILL); |
||
446 | } |
||
447 | rmpid(mxpid); mxpid=-1; |
||
448 | } |
||
449 | else { |
||
450 | fprintf(stderr,"%s: aborted on earlier error.\n",progname); |
||
451 | } |
||
10 | reyssat | 452 | } |
453 | if(multiexec && *multiexec_random) printf("%s\n",multiexec_random); |
||
454 | fflush(stdout); |
||
455 | } |
||
456 | |||
457 | #ifdef linebyline1 |
||
458 | |||
7674 | bpr | 459 | /* this one is for maxima but not used */ |
10 | reyssat | 460 | void dopipes(void) |
461 | { |
||
462 | long int i, l; |
||
463 | char *outptr; |
||
464 | char *p1, *p2, *pp, *pe; |
||
465 | struct timeval t; |
||
466 | fd_set rset; |
||
467 | |||
468 | if(mxpid<0) { |
||
7674 | bpr | 469 | *obuf=0; return; |
10 | reyssat | 470 | } |
471 | outptr=obuf; pp=pe=NULL; |
||
472 | for(p1=ibuf; *p1; p1=p2) { |
||
7674 | bpr | 473 | p2=strchr(p1,'\n'); if(p2) p2++; else p2=p1+strlen(p1); |
474 | write(pipe_in[1],p1,p2-p1); |
||
475 | |||
476 | if(strstr(p1,startstring)!=NULL) iactive=1; |
||
477 | reread: |
||
478 | l=read(fifofd2,lp,MAX_LINELEN-(lp-lbuf)); |
||
479 | if(!iactive) continue; |
||
480 | if(l<0 || l>MAX_LINELEN-(lp-lbuf)) l=0; |
||
481 | lp+=l; *lp=0; |
||
482 | if(active==0) { |
||
483 | char *pp; |
||
484 | pp=strstr(lbuf,startstring); |
||
485 | if(pp!=NULL) { |
||
486 | active=1; ovlstrcpy(lbuf,pp); lp=lbuf+strlen(lbuf); |
||
487 | } |
||
488 | } |
||
489 | if(active!=0 && strstr(lbuf,linebyline)!=NULL) { |
||
490 | fprintf(ff,"%s",lbuf); lp=lbuf; |
||
491 | if(strstr(lbuf,endstring)!=NULL) {lbuf[0]=0; active=-1; break;} |
||
492 | lbuf[0]=0; continue; |
||
493 | } |
||
494 | /* time out allowance between each silence */ |
||
495 | t.tv_sec=3; t.tv_usec=400000; |
||
496 | i=select(fifofd2+1,&rset,NULL,NULL,&t); |
||
497 | if(i>0) goto reread; |
||
498 | else break; /* time out or error */ |
||
10 | reyssat | 499 | } |
500 | } |
||
501 | |||
502 | #else |
||
503 | |||
504 | void dopipes(void) |
||
505 | { |
||
506 | long int i, k, l; |
||
7674 | bpr | 507 | int interval=20000; /* in microseconds */ |
508 | int timeout =300; /* in intervals */ |
||
10 | reyssat | 509 | char *outptr; |
510 | char *p1, *p2, *inp, *pw; |
||
511 | struct timeval t; |
||
512 | fd_set rset, wset; |
||
513 | |||
514 | *obuf=0; if(mxpid<0) return; |
||
7674 | bpr | 515 | outptr=obuf; inp=inputbuf; k=0; |
10 | reyssat | 516 | while(inp<inputptr) { |
7674 | bpr | 517 | t.tv_sec=0; t.tv_usec=interval; |
518 | FD_ZERO(&rset); FD_SET(pipe_out[0],&rset); |
||
519 | FD_ZERO(&wset); FD_SET(pipe_in[1],&wset); |
||
520 | i=pipe_out[0]; if(i<pipe_in[1]) i=pipe_in[1]; |
||
521 | i=select(i+1,&rset,&wset,NULL,&t); |
||
522 | if(i<=0) { |
||
523 | k++; if(k>=timeout) return; /* time out */ |
||
524 | } |
||
525 | if(FD_ISSET(pipe_in[1],&wset)) { |
||
526 | /* Writing must be line by line, for otherwise |
||
527 | * it will block when the input is large. */ |
||
528 | for(pw=inp; pw<inputptr && *pw!='\n'; pw++); |
||
529 | if(pw<inputptr) pw++; |
||
530 | l=write(pipe_in[1],inp,pw-inp); |
||
531 | if(l<0 || l>inputptr-inp) return; |
||
532 | inp+=l; |
||
533 | } |
||
534 | if(FD_ISSET(pipe_out[0],&rset)) { |
||
535 | l=read(pipe_out[0],outptr,fsizelim-(outptr-obuf)-1); |
||
536 | if(l<=0 || l>fsizelim-(outptr-obuf)-1) { |
||
537 | *outptr=0; break; |
||
538 | } |
||
539 | outptr+=l; *outptr=0; |
||
540 | } |
||
10 | reyssat | 541 | } |
542 | p2=NULL; |
||
543 | p1=strstr(obuf,startstring); |
||
544 | if(p1) { |
||
7674 | bpr | 545 | p1+=strlen(startstring); |
546 | p2=strstr(p1,endstring); |
||
10 | reyssat | 547 | } |
548 | while(!p2) { |
||
7674 | bpr | 549 | t.tv_sec=0; t.tv_usec=interval; |
550 | FD_ZERO(&rset); FD_SET(pipe_out[0],&rset); |
||
551 | i=select(pipe_out[0]+1,&rset,NULL,NULL,&t); |
||
552 | if(i>0) { |
||
553 | l=read(pipe_out[0],outptr,fsizelim-(outptr-obuf)-1); |
||
554 | if(l<=0 || l>fsizelim-(outptr-obuf)-1) { |
||
555 | *outptr=0; break; |
||
556 | } |
||
557 | outptr+=l; *outptr=0; |
||
558 | } |
||
559 | else { |
||
560 | k++; if(k>=timeout) break; |
||
561 | } |
||
562 | if(!p1) { |
||
563 | p1=strstr(obuf,startstring); |
||
564 | if(p1) p1+=strlen(startstring); |
||
565 | } |
||
566 | if(p1) p2=strstr(p1,endstring); |
||
10 | reyssat | 567 | } |
568 | } |
||
569 | |||
570 | #endif |
||
571 | |||
572 | void run(void) |
||
573 | { |
||
574 | FILE *ff; |
||
7674 | bpr | 575 | |
10 | reyssat | 576 | if(isabout) {about(); goto end;} |
577 | execredirected(nameofcmd,NULL,NULL,NULL,cmdparm); |
||
578 | putheader(); |
||
579 | obuf=xmalloc(fsizelim); if(!obuf) return; |
||
580 | rep: |
||
581 | putparm(); |
||
582 | if(!multiexec) { |
||
7674 | bpr | 583 | snprintf(inputptr,inputend-inputptr,"%s",quitstring); |
584 | inputptr+=strlen(inputptr); |
||
10 | reyssat | 585 | } |
586 | if(debug) { |
||
7674 | bpr | 587 | ff=fopen(inputfname,"a"); if(ff) { |
588 | fputs(inputbuf,ff); fclose(ff); |
||
589 | } |
||
10 | reyssat | 590 | } |
591 | dopipes(); |
||
592 | readresult(); |
||
593 | if(multiexec) { |
||
7674 | bpr | 594 | getstdin(); inputptr=inputbuf; inputbuf[0]=0; |
595 | goto rep; |
||
10 | reyssat | 596 | } |
597 | end: if(strstr(tmp_dir,"tmp/sessions/")==NULL) { |
||
7674 | bpr | 598 | unlink(inputfname); unlink(outputfname); |
10 | reyssat | 599 | } |
600 | free(obuf); rmpid(mypid); |
||
601 | if(mxpid>0) {kill(mxpid,SIGKILL); rmpid(mxpid);} |
||
602 | } |
||
603 |