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