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