Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 7079 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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